vintlabs / fauxmoESP

Add voice control of your ESP32 and ESP8266 devices using Amazon Alexa
MIT License
379 stars 70 forks source link

Problem in using vector index as device id #179

Open yanivps opened 3 years ago

yanivps commented 3 years ago

Hi, I would like to start with a huge thanks to all of the contributors that helped in creating this library. You did a great job!

I'm working on a project where I'm creating a special controller for an existing smart home system with devices of 24-channel relays. The idea is that my ESP sends a scanning command in the smart home bus to detect available relay devices and allow the user via a simple dashboard to add some of the channels as Fauxmo devices. User can also remove devices if he doesn't want them to be controlled from Alexa anymore.

Let's say the user added 4 Fauxmo devices called "One", "Two", "Three", and "Four" Now if the user decides to remove device "Three" with fauxmo.removeDevice("Three") and also delete this device from the Alexa app, the app shows now only devices "One", "Two" and "Four" Let's say now the user add another Fauxmo device called "Five" with fauxmo.addDevice("Five") After telling Alexa to "scan devices", Alexa will say that it found two new devices, "Four" and "Five" So Alexa app will show in total: "One", "Two", "Four", "Four", "Five" Turning on each of these devices from the Alexa app will generate the following request URLs respectively:

One --> /api/2WLEDHardQrI3WHYTHoMcXHgEspsM8ZZRpSKtBQr/lights/1/state
Two --> /api/2WLEDHardQrI3WHYTHoMcXHgEspsM8ZZRpSKtBQr/lights/2/state
Four --> /api/2WLEDHardQrI3WHYTHoMcXHgEspsM8ZZRpSKtBQr/lights/4/state
Four --> /api/2WLEDHardQrI3WHYTHoMcXHgEspsM8ZZRpSKtBQr/lights/3/state
Five --> /api/2WLEDHardQrI3WHYTHoMcXHgEspsM8ZZRpSKtBQr/lights/4/state

This is because in the [FAUXMO] Handling list request part, if URL is "/lights" without any id, library creates a JSON which looks like this: {"1": {"name": "One", ...}, "2": {"name": "Two", ...}, "3": {"name": "Four", ...}, "4": {"name": "Five", ...}, } Numbers "1", "2", "3", "4" are generated with String(id+1) Alexa will see that there is a new device for index 3 that was deleted before, which is called "Four" and device with index 4 that before was called "Four" from Alexa perspective, now is called Five so it thinks this one is a new device as well.

The way I fixed it for my needs is to add:

typedef struct {
    char * name;
    bool state;
    unsigned char value;
    char uniqueid[28];
    **unsigned char alexaDeviceId;**
} fauxmoesp_device_t;

and use the request from Alexa such as /lights/4 to extract the relevant device with alexaDeviceId 4 and not the device with index 4 minus 1 such a _devices[id-1]

There was also a need to specify the alexaDeviceId in the addDevice function so I can re-add my devices upon ESP reboot, with their correct id as how Alexa registered them The full commit for this fix is here: https://github.com/yanivps/fauxmoESP/commit/c62521ca9a83110fc80f604de5812263a23a72ab

I hope you can find a way to integrate it into the library, probably writing the code better than I did :)

Thanks again for your great efforts!

barneyz commented 3 years ago

You can manage the devices by yourself, when you name the device names from "a", "b", "c", .... or "x1", "x2", "x3" or.... Rename(Alexa app) all devices you use to the name called by alexa ("a" renamed to "One", ....) When you later delete a device ("b" named as "Two" for example) and search for new devices, there will be a "new" free single letter named one (here as "b"), you can rename(Alexa app) it again and assign it to a bus channel. So no change to the fauxmo library is needed.

yanivps commented 3 years ago

You can manage the devices by yourself, when you name the device names from "a", "b", "c", .... or "x1", "x2", "x3" or.... Rename(Alexa app) all devices you use to the name called by alexa ("a" renamed to "One", ....) When you later delete a device ("b" named as "Two" for example) and search for new devices, there will be a "new" free single letter named one (here as "b"), you can rename(Alexa app) it again and assign it to a bus channel. So no change to the fauxmo library is needed.

I appreciate your solution proposal but I don't think that it makes sense to do so many manual renaming of devices just to work around an issue with the library which is using the index in the vector as the id of the device. As indexes can be changed, IDs generally should not be changed...