-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdyndb.py
More file actions
105 lines (90 loc) · 3.57 KB
/
dyndb.py
File metadata and controls
105 lines (90 loc) · 3.57 KB
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
from __future__ import print_function # Python 2/3 compatibility
import boto3
import decimal
import time
from boto3.dynamodb.conditions import Key, Attr
class Tempdb:
def __init__(self,url="http://localhost:8000",region="eu-central-1"):
self.dynamodb = boto3.resource('dynamodb', region_name=region,
endpoint_url=url)
self.table_log = self.dynamodb.Table('Temperature')
self.table_cmd = self.dynamodb.Table('Commands')
def put_values(self,temperature1,temperature2,thermo_name="InsideHall",target_t=100,is_heating=0, boiler_state=-1):
try:
response=self.table_log.put_item(
Item={
'Thermometer': thermo_name,
'GetDate': get_now(),
'val1': get_val(temperature1),
'val2': get_val(temperature2),
'target': get_val(target_t),
'heating': get_val(is_heating),
'boiler_state': get_val(boiler_state)
}
)
except decimal.Inexact:
response=self.table_log.put_item(
Item={
'Thermometer': thermo_name,
'GetDate': get_now(),
'val1': float_to_decimal(temperature1),
'val2': float_to_decimal(temperature2),
'target': float_to_decimal(target_t),
'heating': get_val(int(is_heating)),
'boiler_state': get_val(int(boiler_state)),
}
)
return response
def thermo_get_minutes(self,minutes,thermo_name="InsideHall"):
response = self.table_log.query(
KeyConditionExpression=Key('Thermometer').eq(thermo_name) &
Key('GetDate').gt(get_val(time.time()-
minutes*60))
)
return response
def thermo_cmd(self,target,heating,boiler_name="InsideHall",thermo_name="InsideHall"):
response = self.table_cmd.update_item(
Key={
'Thermometr': thermo_name,
'Boiler': boiler_name
},
UpdateExpression="set target = :t, heating=:h, utime=:u",
ExpressionAttributeValues={
':t': float_to_decimal(target),
':h': get_val(heating),
':u': get_now()
},
ReturnValues="UPDATED_NEW"
)
return response
def get_cmd(self,boiler_name="InsideHall",thermo_name="InsideHall"):
response = self.table_cmd.get_item(
Key={
'Thermometr': thermo_name,
'Boiler': boiler_name
}
)
return response
def get_val(thermo):
return decimal.Decimal(thermo)
def get_now():
return decimal.Decimal(time.time())
def float_to_decimal(float_value):
"""
Convert a floating point value to a decimal that DynamoDB can store,
and allow rounding.
"""
# Perform the conversion using a copy of the decimal context that boto3
# uses. Doing so causes this routine to preserve as much precision as
# boto3 will allow.
with decimal.localcontext(boto3.dynamodb.types.DYNAMODB_CONTEXT) as \
decimalcontext:
# Allow rounding.
decimalcontext.traps[decimal.Inexact] = 0
decimalcontext.traps[decimal.Rounded] = 0
decimal_value = decimalcontext.create_decimal_from_float(float_value)
return decimal_value
#db=Tempdb()
#db.put_values('25.5','30')
#db.put_values('35.5','30')
#db.put_values('35.5','10')