-
Notifications
You must be signed in to change notification settings - Fork 0
/
iot_lighting_socket.py
146 lines (108 loc) · 4.85 KB
/
iot_lighting_socket.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import re
import logging
import json
import bson
import datetime
import time
import os
from bson.json_util import dumps
import requests
from requests_aws4auth import AWS4Auth
from requests_aws4auth.aws4signingkey import AWS4SigningKey
import uuid
import boto3
from boto3.dynamodb.conditions import Key, Attr
import aws
import decimal
logger = logging.getLogger()
logger.setLevel(logging.INFO)
dynamodb = boto3.resource('dynamodb', region_name='us-east-1')
lambdaClient = boto3.client('lambda')
def lambda_handler(event, context):
logger.info("Event: " + dumps(event))
logger.info("Request Context: " + dumps(event['requestContext']))
requestContext = event['requestContext']
table = dynamodb.Table('iotmx-lighting-socket')
try:
if requestContext['routeKey'] == "$connect":
socketDTO = {}
socketDTO['connectionId'] = requestContext['connectionId']
socketDTO['connectionTime'] = requestContext['connectedAt']
socketDTO['domainName'] = requestContext['domainName']
socketDTO['identity'] = requestContext['identity']
socketDTO['online'] = True
socketDTO['_id'] = uuid.uuid4().hex
logger.info("Putting item in iotmx-lighting-socket: " + dumps(socketDTO))
table.put_item(Item = socketDTO)
statusCode = 200
mensaje = "Connected."
elif requestContext['routeKey'] == "message":
statusCode = 200
logger.info("Message: " + dumps(event['body']))
onlineClient = list()
response = table.scan(
FilterExpression=Attr('online').eq(True)
)
for i in response['Items']:
onlineClient.append(i['connectionId'])
if requestContext['connectionId'] in onlineClient:
onlineClient.remove(requestContext['connectionId'])
if len(onlineClient) != 0:
#Post message to connected clients
date = (datetime.datetime.utcnow()).strftime('%Y%m%d')
auth = AWS4Auth(aws.access_id, AWS4SigningKey(aws.secret_key, aws.region, 'execute-api', date))
headers = {
'Context-Type': 'application/json',
'X-Amz-Date': (datetime.datetime.utcnow()).strftime('%Y%m%dT%H%M%SZ'),
'X-Amz-Client-Context': dumps({})
}
for id in onlineClient:
endpoint = "https://" + requestContext['domainName'] + "/v1/@connections/" + str(id)
messageDTO = json.loads(event['body'])
payload = messageDTO['data']
r = requests.post(url = endpoint, data = dumps(payload), auth = auth, headers = headers)
params = json.loads(event['body'])
paramsDTO = {
"httpMethod": "POST",
"body": params['data']
}
logger.info("paramsDTO: " + dumps(paramsDTO))
response = lambdaClient.invoke(
FunctionName = 'iotmx-lighting-device',
InvocationType = 'Event',
Payload = dumps(paramsDTO)
)
logger.info("Lambda Invokation: " + dumps(response))
mensaje = "Message received."
elif requestContext['routeKey'] == "$disconnect":
client = {}
response = table.scan(
FilterExpression=Attr('connectionId').eq(requestContext['connectionId'])
)
for i in response['Items']:
client = i
if len(client) != 0:
logger.info("Connection Id: " + dumps(client['connectionId']))
table.delete_item(
Key = {
"_id": client['_id']
})
statusCode = 200
mensaje = "Desconexion de socket"
else:
statusCode = 200
mensaje = requestContext['routeKey']
except Exception as e:
logger.info("Error: " + str(e))
statusCode = 500
mensaje = "Error: " + str(e)
return {
"isBase64Encoded": False,
"statusCode": statusCode,
"body": mensaje,
"headers": {
'Content-Type': 'application/json',
'charset': 'utf8',
'Access-Control-Allow-Origin': '*'
}
}