Skip to content

Instantly share code, notes, and snippets.

@drbh
Created July 26, 2020 00:41
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/a5640f4f294523785ca736e0b1323f38 to your computer and use it in GitHub Desktop.
Save drbh/a5640f4f294523785ca736e0b1323f38 to your computer and use it in GitHub Desktop.
Get human readable state of the door from dynamo table
import json
import boto3
import decimal
import datetime
import calendar
class DecimalEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, decimal.Decimal):
return float(o)
return super(DecimalEncoder, self).default(o)
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table("<DOOR-STATE-TABLE-NAME>")
def lambda_handler(event, context):
value = table.get_item( Key = {'timestamp': 0} ).get("Item")
door_is = "unlocked"
emoji="🔓"
if value.get("currentstate") == 1:
emoji="🔐"
door_is = "locked"
time = value.get("time")
real_time = datetime.datetime.strptime(time, '%Y-%m-%d %H:%M:%S.%f%z')
when = "{hour} on {day}".format(
hour=real_time.strftime("%I:%M") ,
day=calendar.day_name[real_time.weekday()])
value["message"] = "{emoji} Door was last {state} at {time}".format(emoji=emoji,state=door_is, time=when)
return {
'statusCode': 200,
'body': json.dumps(value, cls=DecimalEncoder)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment