Changeset - 653da092aa96
[Not reviewed]
default
0 2 0
matthewreed - 9 years ago 2016-07-23 17:28:55

Added inteval-based scheduling
2 files changed with 63 insertions and 30 deletions:
0 comments (0 inline, 0 general)
hydrobot.py
Show inline comments
 
import sys
 
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
 
from influxdb import InfluxDBClient
 
from apscheduler.schedulers.background import BackgroundScheduler
 

	
 
#TODO
 
#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")
 
DEBUG_CAN = config.getboolean("debug", "can")
 
DEBUG_CAN_DETAIL = config.getboolean("debug", "can_detail")
 

	
 
@@ -133,46 +133,79 @@ class CanBus:
 
            print(self.relay_send_msg)
 
        self.dev.send(self.relay_send_msg.encode())
 
        
 
    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():
 
    
 
    database = Database()
 
    canbus = CanBus(database)
 
    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:
 
        
 
        time.sleep(1)
 
        
hydrobot_example.conf
Show inline comments
 
@@ -20,10 +20,10 @@ trigger: cron
 
on_time: [None, None, None, None, 0]
 
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]
0 comments (0 inline, 0 general)