-
Notifications
You must be signed in to change notification settings - Fork 3
/
growbox.py
84 lines (71 loc) · 3.26 KB
/
growbox.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
84
import datetime
import json
import queue
import threading
import joblib
import schedule
import time
import RPi.GPIO as GPIO
from controllers.heat_controller import HeatController
from controllers.light_controller import LightController
from controllers.sensors_data_collector import SensorsDataCollector
from controllers.temperature_controller import TemperatureController
from controllers.watering_controller import WateringController
from db.log import save_state_to_db
from logger import LOGGER
from state import STATE, STATE_PATH
def main():
def worker_main():
while True:
job_func = jobqueue.get()
try:
job_func()
except:
LOGGER.exception('')
jobqueue.task_done()
jobqueue = queue.Queue()
try:
def log_state():
def default(o):
if isinstance(o, (datetime.date, datetime.datetime)):
return o.isoformat()
state_string = json.dumps(STATE, default=default)
LOGGER.info(state_string)
save_state_to_db(state_string)
joblib.dump(STATE, STATE_PATH)
def log_job_queue_size():
job_queue_len = jobqueue.qsize()
LOGGER.info(f'job_queue_len: {job_queue_len}')
temperature_controller = TemperatureController(STATE)
sensors_data_collector = SensorsDataCollector(STATE)
light_controller = LightController(STATE)
watering_controller_top = WateringController(STATE, 'top')
watering_controller_bottom = WateringController(STATE, 'bottom')
heat_controller = HeatController(STATE)
schedule.every(20).seconds.do(jobqueue.put, temperature_controller.update_fan_pwm)
schedule.every(10).seconds.do(jobqueue.put, sensors_data_collector.get_data)
schedule.every(300).seconds.do(jobqueue.put, light_controller.control)
schedule.every().day.at("09:30").do(jobqueue.put, watering_controller_top.control)
schedule.every().day.at("09:30").do(jobqueue.put, watering_controller_bottom.control)
schedule.every().day.at("12:00").do(jobqueue.put, watering_controller_top.control)
schedule.every().day.at("12:00").do(jobqueue.put, watering_controller_bottom.control)
schedule.every().day.at("14:30").do(jobqueue.put, watering_controller_top.control)
schedule.every().day.at("14:30").do(jobqueue.put, watering_controller_bottom.control)
schedule.every().day.at("17:00").do(jobqueue.put, watering_controller_top.control)
schedule.every().day.at("17:00").do(jobqueue.put, watering_controller_bottom.control)
schedule.every().day.at("19:30").do(jobqueue.put, watering_controller_top.control)
schedule.every().day.at("19:30").do(jobqueue.put, watering_controller_bottom.control)
schedule.every(120).seconds.do(jobqueue.put, heat_controller.control)
schedule.every(10).seconds.do(jobqueue.put, log_state)
schedule.every(10).seconds.do(jobqueue.put, log_job_queue_size)
worker_thread = threading.Thread(target=worker_main)
worker_thread.start()
while True:
schedule.run_pending()
time.sleep(1)
except:
LOGGER.exception('')
finally:
GPIO.cleanup()
if __name__ == '__main__':
main()