Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AWS Iot 1-Click Event - Python implementation (platform-agnostic)
import os
from datetime import date
import boto3
# read environment variables
TABLE_NAME = os.environ.get('TABLE_NAME', 'my_table_test')
region = os.environ.get('AWS_DEFAULT_REGION', 'eu-west-1')
# initialize DDB client
dynamodb = boto3.resource('dynamodb', region_name=region)
table = dynamodb.Table(TABLE_NAME)
class ClickHandler(object):
""" Abstract class - you can sub-class it and implement the on_* methods """
def __init__(self, click_event):
self._click_event = click_event
def run(self):
click_type = self._get_click_type()
if click_type == 'SINGLE':
self.on_single_click()
elif click_type == 'DOUBLE':
self.on_double_click()
elif click_type == 'LONG':
self.on_long_click()
else:
raise Exception("Invalid click type: %s" % click_type)
def _get_reported_time(self):
return self._click_event['reportedTime']
def _get_click_type(self):
return self._click_event['clickType']
def on_single_click(self):
""" handle single click """
print("[Not Implemented] single click")
def on_double_click(self):
""" handle double click """
print("[Not Implemented] double click")
def on_long_click(self):
""" handle long click """
print("[Not Implemented] long click")
class DailyClickHandler(ClickHandler):
def on_single_click(self):
""" create/update daily item """
table.put_item(
Item={
'day': self._get_today_key(),
'last_click_time': self._get_reported_time(),
}
)
def on_double_click(self):
""" delete daily item """
# note: nothing bad happens in the item doesn't exist yet
table.delete_item(
Key={
'day': self._get_today_key()
}
)
@staticmethod
def _get_today_key():
""" Current day in ISO format (yyyy-mm-dd) """
return date.today().isoformat()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.