-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
51 lines (41 loc) · 1.64 KB
/
main.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
import schedule
import time
import requests
import json
from base import Base, session_factory
from models.mystrom_device import MystromDevice
from models.mystrom_result import MystromResult
@schedule.repeat(schedule.every(1).minutes)
def trigger():
for device in get_active_devices():
request_data_and_store(device)
def get_active_devices():
device_query = session.query(MystromDevice).filter(MystromDevice.active == True)
return device_query.all()
def request_data_and_store(device):
try:
response = requests.get(f'http://{device.ip}/report')
except requests.ConnectionError as e:
print(f'Device {device.name} with ip address {device.ip} seems to be not reachable.')
return
except requests.Timeout as e:
print(f'Request to device {device.name} with ip address {device.ip} timed out.')
return
except requests.RequestException as e:
print(f'Request to device {device.name} with ip address {device.ip} failed.')
return
try:
response = json.loads(response.text)
except json.decoder.JSONDecodeError:
print(f'Request to device {device.name} with ip address {device.ip} returns invalid JSON response.')
return
mystrom_result = MystromResult(device_id=device.id, power=response["power"], ws=response["Ws"], relay=1 if response["relay"] else 0, temperature=response["temperature"])
session.add(mystrom_result, device)
session.commit()
if __name__ == '__main__':
session = session_factory()
while True:
# Checks whether a scheduled task
# is pending to run or not
schedule.run_pending()
time.sleep(1)