-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·74 lines (56 loc) · 1.79 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/python3
import json
import os
import time
from executors.fans import Fans
from executors.water_pump import WaterPump
from sensors.moisure import Moisure
from sensors.temperature import Temperature
from sensors.ultrasounds import Ultrasounds
# Read a dictionary and load events into an array
def load_events(events_data):
events = []
for sensor in events_data["sensors"]:
try:
for event in sensor["events"]:
event["sensor"] = sensor["id"]
events.append(event)
except:
continue
return events
# Get an event and check sensors based on event data
def event_listener(event):
sensor = sensors[event["sensor"]]
executor = executors[event["executor"]]
state = event["state"]
set_state = False
while True:
if event["sensor"] == "s3":
if event["value"] == sensor.read():
set_state = True
else:
if event["above"] != 0:
if event["above"] > sensor.read():
set_state = True
else:
set_state = False
if event["equal"] != 0:
if event["equal"] == sensor.read():
set_state = True
else:
set_state = False
if event["below"] != 0:
if event["below"] < sensor.read():
set_state = True
else:
set_state = False
if set_state:
executor.setState(state)
time.sleep(1)
events = []
sensors = {"s1": Temperature(), "s2": Ultrasounds(), "s3": Moisure()}
executors = {"e1": Fans(), "e2": WaterPump()}
file = open(os.getenv("CONFIG_PATH"), "r")
data = json.loads(file.read())
file.close()
events = load_events(data)