-
Notifications
You must be signed in to change notification settings - Fork 0
/
IoT-II-2023-yaira-consulta-API
37 lines (32 loc) · 1.04 KB
/
IoT-II-2023-yaira-consulta-API
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
import json
import boto3
from decimal import Decimal
class JSONEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, Decimal):
return float(obj)
return json.JSONEncoder.default(self, obj)
def lambda_handler(event, context):
# Get dynamodb resource
dynamodb = boto3.resource('dynamodb')
if event['resource']=='/yaira_data':
# Get table
table = dynamodb.Table('princesitayaira')
# Print table scan results
body=table.scan()['Items']
return {
'statusCode': 200,
'headers': {
'Access-Control-Allow-Origin': '*'
},
'body': json.dumps(body,cls=JSONEncoder)
}
if event['resource'] == '/promedio':
# Get table
table = dynamodb.Table('princesitayaira')
# Print table scan results
body = table.scan()['Items']
sensor_values = []
for item in body:
sensor_values.append(item['sensor'])
average = sum(sensor_values) / len(sensor_values)