Changeset - 22b6999f8bdd
[Not reviewed]
default
0 2 0
matthewreed - 9 years ago 2016-07-21 22:40:28

Added timers in the config file
2 files changed with 45 insertions and 28 deletions:
0 comments (0 inline, 0 general)
hydrobot.py
Show inline comments
 
import sys
 
import time
 
import _thread
 
import ast
 
import configparser
 
from canard import can, messaging
 
from canard.hw import socketcan
 
from canard.file import jsondb
 
from influxdb import InfluxDBClient
 
import configparser
 
from apscheduler.schedulers.background import BackgroundScheduler
 
from datetime import datetime
 
import os
 

	
 
#TODO
 
#fix temperature offset
 
#database serial thing
 
#database series helper
 
#database time interval logging
 
#add system name to database
 

	
 
config = configparser.ConfigParser()
 
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")
 

	
 
class Database:
 
    
 
    def __init__(self):
 
        host = config.get("database", "host")
 
        port = config.get("database", "port")
 
        username = config.get("database", "username")
 
        password = config.get("database", "password")
 
        database = config.get("database", "database")
 
        self.name = config.get("system", "name")
 
        self.client = InfluxDBClient(host, port, username, password, database)
 

	
 
    def log_data(self, msgdb, message):
 
        if message == msgdb.AirSense:
 
            json_body = [
 
                {
 
                    "measurement": "air_temp",
 
                    "measurement": self.name + "_air_temp",
 
                    "fields": {
 
                        "value": (float)(message.Temperature.value)
 
                    }
 
                },
 
                {
 
                    "measurement": "air_humidity",
 
                    "measurement": self.name + "_air_humidity",
 
                    "fields": {
 
                        "value": (float)(message.Humidity.value)
 
                    }
 
                },
 
                {
 
                    "measurement": "air_pressure",
 
                    "measurement": self.name + "_air_pressure",
 
                    "fields": {
 
                        "value": (float)(message.Pressure.value)
 
                    }
 
                }
 
            ]
 
            self.client.write_points(json_body)
 
        if message == msgdb.RelayDriveIn:
 
            json_body = [
 
                {
 
                    "measurement": "input_1",
 
                    "measurement": self.name + "_input_1",
 
                    "fields": {
 
                        "value": (float)(message.Input1.value)
 
                    }
 
                },
 
                {
 
                    "measurement": "input_2",
 
                    "measurement": self.name + "_input_2",
 
                    "fields": {
 
                        "value": (float)(message.Input2.value)
 
                    }
 
                },
 
                {
 
                    "measurement": "input_3",
 
                    "measurement": self.name + "_input_3",
 
                    "fields": {
 
                        "value": (float)(message.Input3.value)
 
                    }
 
                },
 
                {
 
                    "measurement": "input_4",
 
                    "measurement": self.name + "_input_4",
 
                    "fields": {
 
                        "value": (float)(message.Input4.value)
 
                    }
 
                }
 
            ]
 
            self.client.write_points(json_body)
 
@@ -131,36 +131,51 @@ class CanBus:
 
            print("Send CAN message! ID: " + hex(self.relay_send_msg.id))
 
        if DEBUG_CAN_DETAIL:
 
            print(self.relay_send_msg)
 
        self.dev.send(self.relay_send_msg.encode())
 
        
 
    def set_output(self, module, output, state):
 
        print(self.msgdb)
 
        print(module)
 
        #msg = self.msgdb.module
 
        msg = self.msgdb.lookup_message(module)
 
        msg.lookup_signal(output).value = state
 
        print(msg)
 
        self.dev.send(msg.encode())
 

	
 
def main():
 
    
 
    database = Database()
 
    canbus = CanBus(database)
 
    canbus.start()
 
    canbus.start_receive()
 
    
 
    scheduler = BackgroundScheduler()
 
    #scheduler.add_job(canbus.send_can, 'interval', seconds=10)
 
    trigger = config.get("timer1", "trigger")
 
    scheduler.add_job(canbus.send_can, trigger, second = 0)
 
    trigger = config.get("timer2", "trigger")
 
    scheduler.add_job(canbus.send_can, trigger, seconds = 10)
 
    
 
    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.start()
 

	
 
    while True:
 
        module = config.get("timer1", "module")
 
        output = config.get("timer1", "output")
 
        #canbus.set_output(module, output, 1)
 
        
 
        time.sleep(1)
 
        
 

	
 
if __name__ == "__main__":
 
    main()
hydrobot_example.conf
Show inline comments
 
@@ -13,15 +13,17 @@ can: True
 
can_detail: False
 

	
 
[timer1]
 
module: RelayDriveOut
 
output: Output1
 
trigger: cron
 
on_time: second = 0
 
off_time: second = 30
 
#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, 30]
 

	
 
[timer2]
 
module: RelayDriveOut
 
output: Output2
 
trigger: interval
 
on_duration: seconds = 30
 
off_duration: seconds = 30s
 
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]
0 comments (0 inline, 0 general)