-
Notifications
You must be signed in to change notification settings - Fork 0
/
gendata.py
36 lines (32 loc) · 1.44 KB
/
gendata.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
import requests, time, json, threading
def genActualTemp(thermostatID):
r = requests.get('http://127.0.0.1:9000/thermostat/' + str(thermostatID) +'/')
response = r.json()
if response['actual_temperature'] < response['temperature_set']:
print('Setting temp '+ str(response['actual_temperature']+1) + ' on thermostat ' + str(response['id']))
payload = {'actual_temperature': response['actual_temperature']+1}
r = requests.patch(
'http://127.0.0.1:9000/thermostat/' + str(thermostatID) +'/', data=json.dumps(payload),
headers={'Content-type': 'application/json'}
)
elif response['actual_temperature'] > response['temperature_set']:
print('Setting temp '+ str(response['actual_temperature']-1) + ' on thermostat ' + str(response['id']))
payload = {'actual_temperature': response['actual_temperature']-1}
r = requests.patch(
'http://127.0.0.1:9000/thermostat/' + str(thermostatID) +'/', data=json.dumps(payload),
headers={'Content-type': 'application/json'}
)
def roomHandling(roomObj):
for thermostat in roomObj['thermostats']:
genActualTemp(thermostat['id'])
while True:
roomThreads = list()
r = requests.get('http://127.0.0.1:9000/room/')
jsonResponse = r.json()
for room in jsonResponse:
x = threading.Thread(target=roomHandling, args=(room, ), daemon=True)
roomThreads.append(x)
x.start()
for index, thread in enumerate(roomThreads):
thread.join()
time.sleep(10)