-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_resource_dynamo.py
83 lines (61 loc) · 2.24 KB
/
get_resource_dynamo.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
import boto3
from boto3.dynamodb.conditions import Attr
from botocore.exceptions import ClientError
from datetime import datetime, timezone
from datetime import timedelta
from config import *
def get_resource():
return boto3.resource('dynamodb',
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY,
region_name=REGION_NAME)
def get_all_data(dynamodb=None):
if not dynamodb:
dynamodb = get_resource()
table = dynamodb.Table('SensorData2')
try:
response = table.scan()
except ClientError as e:
print(e.response['Error']['Message'])
else:
return response['Items']
def get_all_by_devicename(devicename, dynamodb=None):
if not dynamodb:
dynamodb = get_resource()
table = dynamodb.Table('SensorData2')
try:
response = table.scan(FilterExpression=Attr('devicename').eq(devicename))
except ClientError as e:
print(e.response['Error']['Message'])
else:
return response['Items']
class Device:
def __init__(self, time, deviceid, devicename, humid, soil, temp):
self.time = time
self.deviceid = deviceid
self.devicename = devicename
self.humid = humid
self.soil = soil
self.temp = temp
def __repr__(self):
return f'Device({self.time}, {self.deviceid}, {self.devicename}, {self.humid}, {self.soil}, {self.temp})'
def __str__(self):
return f'Device {self.devicename} with id {self.deviceid} gave value temp: {self.temp}, soil: {self.soil}, ' \
f'humid: {self.humid} at {self.time}'
@staticmethod
def create_from_dict(dict_data):
timestamp = int(dict_data['timestamp'])
time = datetime.fromtimestamp(timestamp, timezone.utc)
deviceid = dict_data['deviceid']
devicename = dict_data['devicename']
humid = dict_data['humid']
soil = dict_data['soil']
temp = dict_data['temp']
return Device(time, deviceid, devicename, humid, soil, temp)
def main():
values = get_all_data()
values = [Device.create_from_dict(value) for value in values]
for value in values:
print(value)
if __name__ == '__main__':
main()