atlascoder
atlascoder

Reputation: 3063

AWS IoT thing connectivity status not reliable

I need to get IoT devices status reliable.

Now, I have Lambda connected to SELECT * FROM '$aws/events/presence/#' events on IoT.

But I can't get reliable device status in the case when a connected device was disconnected and connected back within ~ 40 seconds. The result of this scenario - events in the order: 1. Connected - shortly after device was connected again 2. Disconnected - after ~ 40 seconds.

It looks like the message disconnected is not discarded when device is connected back and emitted after connection timeout in any case.

I've found a workaround - request device connectivity from AWS_Things IoT index. In fact, I also receive previous connectivity state, but it has timestamp field. Then, I just compare the current event.timestamp with the timestamp from index and if it higher that 30 seconds - I discard disconnected event silently. But this approach is not reliable, because I am still able get wrong behavior when switching device faster - with 5 seconds interval. This is not acceptable for my project.

Is it possible to use IoT events to solve my problem? I wouldn't like to go in devices index polling..

Upvotes: 3

Views: 977

Answers (2)

Patrick W.
Patrick W.

Reputation: 149

You can also use an sqs delay queue and check after 5 secs if the disconnect is true. That is way cheaper than using step functions. This is also the official solution.

Handling client disconnections The best practice is to always have a wait state implemented for lifecycle events, including Last Will and Testament (LWT) messages. When a disconnect message is received, your code should wait a period of time and verify a device is still offline before taking action. One way to do this is by using SQS Delay Queues. When a client receives a LWT or a lifecycle event, you can enqueue a message (for example, for 5 seconds). When that message becomes available and is processed (by Lambda or another service), you can first check if the device is still offline before taking further action.

https://docs.aws.amazon.com/iot/latest/developerguide/life-cycle-events.html#connect-disconnect

Upvotes: 3

atlascoder
atlascoder

Reputation: 3063

Well, on this moment I just use StepFunction, connected to SELECT * FROM '$aws/events/presence/#' event that makes check of actual thing state after 10s delay:

step function diagram

{
  "StartAt": "ChoiceEvent",
  "States": {
    "ChoiceEvent": {
      "Type": "Choice",
      "Choices": [
          {
            "Variable": "$.eventType",
            "StringEquals": "disconnected",
            "Next": "WaitDelay"
          }
      ],
      "Default": "CheckStatus"
    },
    "WaitDelay": {
      "Type": "Wait",
      "Seconds": 30,
      "Next": "CheckStatus"
    },
    "CheckStatus": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:us-east-1:xxxxxxx:function:connectivity-check",
      "End": true
    }
  }
}

The connectivity-check lambda just checks actual thing state in IoT registry when eventType is disconnected

Upvotes: 1

Related Questions