-
Notifications
You must be signed in to change notification settings - Fork 0
/
IoT-II-2023_iot-core-ws-lambda_yaira
57 lines (41 loc) · 1.52 KB
/
IoT-II-2023_iot-core-ws-lambda_yaira
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
import math
import boto3
from decimal import Decimal
import json
# Get dynamodb resource
dynamodb = boto3.resource('dynamodb')
def lambda_handler(event, context):
# JSON object from IoT core has timestamp field
if 'timestamp' in event:
# Get table
table = dynamodb.Table('princesitayaira')
# Put item into table
response = table.put_item(
Item={
'id_key': "IoT", # Primary key
'timestamp': event['timestamp'], # Sort key
'sensor': event['sensor']
}
)
# Print put_item response
print(response)
# Get recently written item
response = table.get_item(
Key={'id_key': "IoT", 'timestamp': event['timestamp']}
)
# Print get_item response
print(response)
# Print table scan results
#print(table.scan()['Items'])
table = dynamodb.Table('ws_connection_id')
response = table.get_item(
Key={'id_key': "connectionid"}
)
print(response)
if 'Item' in response:
api_client = boto3.client('apigatewaymanagementapi',endpoint_url='https://1qjubqugf6.execute-api.us-east-1.amazonaws.com/production')
connectionId=response['Item']['id']
api_client.post_to_connection(ConnectionId=connectionId, Data=json.dumps({'sensor': event['sensor']}))
print(connectionId)
# Return
return "DB updated"