diff --git a/hydrobot.py b/hydrobot.py --- a/hydrobot.py +++ b/hydrobot.py @@ -3,6 +3,7 @@ import time import _thread import ast import configparser +import datetime from canard import can, messaging from canard.hw import socketcan from canard.file import jsondb @@ -13,7 +14,6 @@ from apscheduler.schedulers.background i #fix temperature offset #database series helper #database time interval logging -#add system name to database config = configparser.ConfigParser(allow_no_value = True) config.read("hydrobot.conf") @@ -136,8 +136,64 @@ class CanBus: def set_output(self, module, output, state): msg = self.msgdb.lookup_message(module) msg.lookup_signal(output).value = state - print(msg) + #print(msg) self.dev.send(msg.encode()) + +class Scheduler: + + def __init__(self, canbus): + self.canbus = canbus + self.apscheduler = BackgroundScheduler() + + def start(self): + self.apscheduler.start() + + for section in config.sections(): + if "timer" in section: + items = config.items(section) + for item in items: + if item[0] == 'trigger': + trigger = item[1] + if item[0] == 'module': + module = item[1] + if item[0] == 'output': + output = item[1] + if item[0] == 'on_time': + on_time = ast.literal_eval(item[1]) + if item[0] == 'off_time': + off_time = ast.literal_eval(item[1]) + if item[0] == 'on_duration': + on_duration = ast.literal_eval(item[1]) + if item[0] == 'off_duration': + off_duration = ast.literal_eval(item[1]) + if trigger == 'cron': + self.apscheduler.add_job(self.canbus.set_output, trigger, [module, output, 1], day = on_time[0], day_of_week = on_time[1], hour = on_time[2], minute = on_time[3], second = on_time[4]) + self.apscheduler.add_job(self.canbus.set_output, trigger, [module, output, 0], day = off_time[0], day_of_week = off_time[1], hour = off_time[2], minute = off_time[3], second = off_time[4]) + if trigger == 'interval': + self.apscheduler.add_job(self.interval_off, trigger, [module, output, section + "_off", section + "_on", on_duration], id = section + "_off", weeks = off_duration[0], days = off_duration[1], hours = off_duration[2], minutes = off_duration[3], seconds = off_duration[4]) + timer = self.apscheduler.add_job(self.interval_on, trigger, [module, output, section + "_off", section + "_on", off_duration], id = section + "_on", weeks = on_duration[0], days = on_duration[1], hours = on_duration[2], minutes = on_duration[3], seconds = on_duration[4]) + timer.pause() + print(off_duration[4]) + print(on_duration[4]) + + + def interval_off(self, module, output, timer_off, timer_on, on_duration): + self.canbus.set_output(module, output, 1) + self.apscheduler.get_job(timer_on).reschedule("interval", weeks = on_duration[0], days = on_duration[1], hours = on_duration[2], minutes = on_duration[3], seconds = on_duration[4]) + self.apscheduler.get_job(timer_on).resume() + self.apscheduler.get_job(timer_off).pause() + print("turning on " + timer_on + "! " + str(datetime.datetime.now().time())) + + def interval_on(self, module, output, timer_off, timer_on, off_duration): + self.canbus.set_output(module, output, 0) + self.apscheduler.get_job(timer_off).reschedule("interval", weeks = off_duration[0], days = off_duration[1], hours = off_duration[2], minutes = off_duration[3], seconds = off_duration[4]) + self.apscheduler.get_job(timer_off).resume() + self.apscheduler.get_job(timer_on).pause() + print("turning off " + timer_off + "! " + str(datetime.datetime.now().time())) + +def func(): + print(1) + def main(): @@ -146,30 +202,7 @@ def main(): canbus.start() canbus.start_receive() - scheduler = BackgroundScheduler() - - for section in config.sections(): - if "timer" in section: - items = config.items(section) - for item in items: - if item[0] == 'trigger': - trigger = item[1] - if item[0] == 'module': - module = item[1] - if item[0] == 'output': - output = item[1] - if item[0] == 'on_time': - on_time = ast.literal_eval(item[1]) - if item[0] == 'off_time': - off_time = ast.literal_eval(item[1]) - if item[0] == 'on_duration': - on_duration = ast.literal_eval(item[1]) - if trigger == 'cron': - scheduler.add_job(canbus.set_output, trigger, [module, output, 1], day = on_time[0], day_of_week = on_time[1], hour = on_time[2], minute = on_time[3], second = on_time[4]) - scheduler.add_job(canbus.set_output, trigger, [module, output, 0], day = off_time[0], day_of_week = off_time[1], hour = off_time[2], minute = off_time[3], second = off_time[4]) - if trigger == 'interval': - scheduler.add_job(canbus.set_output, trigger, [module, output, 1], weeks = on_duration[0], days = on_duration[1], hours = on_duration[2], minutes = on_duration[3], seconds = on_duration[4]) - + scheduler = Scheduler(canbus) scheduler.start() while True: diff --git a/hydrobot_example.conf b/hydrobot_example.conf --- a/hydrobot_example.conf +++ b/hydrobot_example.conf @@ -23,7 +23,7 @@ off_time: [None, None, None, None, 30] [timer2] module: RelayDriveOut output: Output2 -trigger: cron -#day(1-31), weekday(0-6), hour(0-23), minute(0-59), second(0-59) -on_time: [None, None, None, None, 0] -off_time: [None, None, None, None, 15] +trigger: interval +#weeks, days, hours, minutes, seconds +on_duration: [0, 0, 0, 0, 20] +off_duration: [0, 0, 0, 0, 10]