Skip to content

Instantly share code, notes, and snippets.

@drbh
Created July 26, 2020 00:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drbh/51b5917168d1862f44b94d432076ce81 to your computer and use it in GitHub Desktop.
Save drbh/51b5917168d1862f44b94d432076ce81 to your computer and use it in GitHub Desktop.
Write door state updates to dynamo
import json
import boto3
import datetime
import dateutil.tz
eastern = dateutil.tz.gettz('US/Eastern')
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table("<DOOR-STATE-TABLE-NAME>")
def lambda_handler(event, context):
var = 99 # log 99 if there is an error
if "currentstate" in event.get("body"):
var = json.loads(event.get("body")).get("currentstate")
now = datetime.datetime.now(tz=eastern)
str_key = str(now)
int_key = int(datetime.datetime.timestamp(now))
# we add a log and update timestamp 0 to track most recent value
table.put_item(Item={"timestamp": int_key, "time": str_key, "currentstate": var })
table.put_item(Item={"timestamp": 0, "key": int_key, "time": str_key, "currentstate": var })
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment