-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlongpress-lambda.py
61 lines (53 loc) · 1.82 KB
/
longpress-lambda.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import logging, boto3, botocore.exceptions, json, urllib
USER_TABLE = "User"
EVENT_TABLE = "Event"
REGION = "us-east-1"
STATE = "HELP"
MSG = "Help on the way"
TYPE = "LONGPRESS"
# @author Pratik P
# handles the following
# {
# "serialNumber": "ABCDEYFG12345",
# "clickType": "LONGPRESS",
# "batteryVoltage": "2000 mV"
# }
def lambda_handler(event, context):
logging.getLogger().setLevel(logging.INFO)
print (event)
print (context)
clickType = event['clickType']
if (clickType != str(TYPE)):
logging.info('Click Type: {} is not supported'.format(clickType))
return False
dsn = event['serialNumber']
process_key(event, dsn, context)
return True
# Handles the following
# if key exists in USER-TABLE
#process key
# if key *doesn't* exist in EVENT-TABlE
#add row
# if key exist in EVENT-TABlE
#update row
def process_key(event, dsn, context):
logging.info('Processing dsn id: {}'.format(dsn))
userTable = boto3.resource('dynamodb', region_name=REGION) \
.Table(USER_TABLE)
response = userTable.get_item(Key={'DeviceId':str(dsn)})
print('response: ')
print(response)
if 'Item' in response:
logging.info('dsn id: {} exists in the User table. Adding/Updating Events table with LongPress'.format(dsn))
eventTable = boto3.resource('dynamodb', region_name=REGION).Table(EVENT_TABLE)
resp = eventTable.put_item(Item={'DeviceId':str(dsn), 'State':str(STATE), 'Address':'123 solutions road', 'FamilyCount':7})
print(resp)
handle_sns_event(event, context)
else :
logging.info('dsn id: {} does *not* exist in the User table'.format(dsn))
def handle_sns_event(event, context):
client = boto3.client('sns')
snsResponse = client.publish(
TargetArn='arn:aws:sns:us-east-1:597248753215:ConfirmLongPress',
Message= str(MSG),
MessageStructure='raw')