I have an existing IoT based smart home system in charge of the switches at my house and was interested in an integration with Alexa. I'd gotten started with the official Alexa Skills guide to set up a skill and an associated AWS Lambda function in Node.js. I have a couple of endpoints on my public server at home, one of which responds with the list of devices in a format that the "context.succeed(...)" inside of the request handler needs, as well as another that simply switches the power state of the device on or off. While writing the Lambda function for this, all that I had to change in the boilerplate code of the sample was to include "http GET" requests to my server using the native "http" package of Node.js.
While Alexa, through the linked Lambda function, is able to successfully trigger my home server endpoints which ultimately discovers and switches the devices on / off, there seems to be a massive issue. It appears as though, either the request sent by Alexa or the Lambda function execution seems to be "queued" after the very first discovery request hence executing the "previous" request at every subsequent request. Let me illustrate this for a clearer picture with 3 devices - fan, tubelight and bulb.
1. I ask Alexa to initiate a discovery request. My home server logs correctly show that the discover-devices endpoint was fired by the Lambda function linked with this skill.
2. I ask Alexa to turn on the fan. There is no new log record on my home server stating that a request was made to turn the fan on. Hence the command seems to have done nothing.
3. I ask Alexa to turn off the tubelight. My server logs show that the power-state-toggle endpoint was fired by the Lambda function, but to "turn on the fan" (evidently, this is the "previous" request in #2).
4. I ask Alexa to turn off the fan. This time, just as in the #3 case, the logs show that the tubelight has been turned off (note: "turn off fan" request seems to be queued).
5. Interestingly, if I now initiate another discovery request, my server logs show that it has received 2 requests one after the other:
(a) Turn off the fan (as requested in #4)
(b) Get the list of devices
6. Any subsequent non-discovery request follows the same cycle as above.
If you notice the sequence of events, it's clear that post the first discovery request, every other power control request is cached and the "previous" power control request, if exists, is executed and finally the last discovery request executes the last "queued" power control request followed by the discovery request itself.
This behavior is super weird and balks me from proceeding any further! Even if I defer subsequent commands to Alexa by over 5m or so, the "queuing" behavior remains the same. What could be the issue? Looking forward to your assistance.