Created
July 26, 2020 00:41
-
-
Save drbh/a5640f4f294523785ca736e0b1323f38 to your computer and use it in GitHub Desktop.
Get human readable state of the door from dynamo table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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