Changeset - 981a1d0baca8
[Not reviewed]
default
0 2 0
matthewreed - 9 years ago 2016-07-25 22:55:45

Added output logging and a logging bug fix
2 files changed with 18 insertions and 7 deletions:
0 comments (0 inline, 0 general)
hydrobot.py
Show inline comments
 
@@ -3,26 +3,25 @@ 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 influxdb import SeriesHelper
 
from apscheduler.schedulers.background import BackgroundScheduler
 

	
 
#TODO
 
#fix temperature offset
 
#database series helper
 
#fix temperature offsets
 
#database time interval logging
 

	
 
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")
 
DEBUG_TIMER = config.getboolean("debug", "timer")
 

	
 
    
 
class MySeriesHelper(SeriesHelper):
 
    
 
    # Meta class stores time series helper configuration.
 
@@ -50,29 +49,30 @@ class Database:
 
        password = config.get("database", "password")
 
        database = config.get("database", "database")
 
        self.name = config.get("system", "name")
 
        self.client = InfluxDBClient(host, port, username, password, database)
 
        MySeriesHelper.Meta.client = self.client
 
        MySeriesHelper.Meta.series_name = self.name + '.{measurement}'
 
        
 
        # To manually submit data points which are not yet written, call commit:
 
        #MySeriesHelper.commit()
 

	
 
    def log_data(self, msgdb, message):
 
        if message == msgdb.AirSense:
 
            
 
            MySeriesHelper(measurement='air_temp', value=(float)(message.Temperature.value))
 
            MySeriesHelper(measurement='air_humidity', value=(float)(message.Humidity.value))
 
            MySeriesHelper(measurement='air_pressure', value=(float)(message.Pressure.value))
 
        if message == msgdb.RelayDriveIn:
 
            MySeriesHelper(measurement='water_flow_rate', value=(float)(message.FlowRate.value))
 
            print(message.FlowRate.value)
 
            MySeriesHelper(measurement='input_1', value=(float)(message.Input1.value))
 
            MySeriesHelper(measurement='input_2', value=(float)(message.Input2.value))
 
            MySeriesHelper(measurement='input_3', value=(float)(message.Input3.value))
 
            MySeriesHelper(measurement='input_4', value=(float)(message.Input4.value))
 
        if message == msgdb.WaterSense:
 
            MySeriesHelper(measurement='water_level', value=(float)(message.PercentFull.value))
 
            MySeriesHelper(measurement='water_temp', value=(float)(message.Temperature.value))
 
            MySeriesHelper(measurement='water_raw_debug', value=(float)(message.RawDebug.value))
 

	
 

	
 
class CanBus:
 
    
 
@@ -119,24 +119,33 @@ class CanBus:
 
        self.relay_send_msg.Output4.value = 1
 
        if DEBUG_CAN:
 
            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):
 
        msg = self.msgdb.lookup_message(module)
 
        msg.lookup_signal(output).value = state
 
        self.dev.send(msg.encode())
 
        
 
        if msg.lookup_signal(output) == msg.Output1:
 
            MySeriesHelper(measurement='output_1', value=state)
 
        if msg.lookup_signal(output) == msg.Output2:
 
            MySeriesHelper(measurement='output_2', value=state)
 
        if msg.lookup_signal(output) == msg.Output3:
 
            MySeriesHelper(measurement='output_3', value=state)
 
        if msg.lookup_signal(output) == msg.Output4:
 
            MySeriesHelper(measurement='output_4', value=state)
 
        
 
        
 
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:
 
@@ -179,24 +188,25 @@ class Scheduler:
 
        self.apscheduler.get_job(timer_on).pause()
 
        if DEBUG_TIMER:
 
            print("Turning " + timer_off + "! " + str(datetime.datetime.now().time()))
 
    
 
    
 
def func():
 
    print(1)
 
    
 

	
 
def main():
 
    
 
    database = Database()
 
    
 
    canbus = CanBus(database)
 
    canbus.start()
 
    canbus.start_receive()
 
    
 
    scheduler = Scheduler(canbus)
 
    scheduler.start()
 

	
 
    while True:
 
        
 
        time.sleep(1)
 
        
 

	
hydrobot_can.json
Show inline comments
 
@@ -14,41 +14,42 @@
 
        "name": "RelayDriveOut",
 
        "id": "0x203",
 
        "signals": { "0": {"name": "Nothing", "bit_length": 32, "factor": 1, "offset": 0, "unit": ""},
 
                    "32": {"name": "Output1", "bit_length": 8, "factor": 1, "offset": 0, "unit": "bool"},
 
                    "40": {"name": "Output2", "bit_length": 8, "factor": 1, "offset": 0, "unit": "bool"},
 
                    "48": {"name": "Output3", "bit_length": 8, "factor": 1, "offset": 0, "unit": "bool"},
 
                    "56": {"name": "Output4", "bit_length": 8, "factor": 1, "offset": 0, "unit": "bool"}
 
                   }
 
        },
 
        {
 
        "name": "RelayDriveIn",
 
        "id": "0x204",
 
        "signals": { "0": {"name": "Nothing", "bit_length": 32, "factor": 1, "offset": 0, "unit": ""},
 
        "signals": { "0": {"name": "FlowRate", "bit_length": 16, "factor": 0.133, "offset": 0, "unit": "Hz"},
 
                    "16": {"name": "Nothing", "bit_length": 16, "factor": 1, "offset": 0, "unit": ""},
 
                    "32": {"name": "Input1", "bit_length": 8, "factor": 1, "offset": 0, "unit": "bool"},
 
                    "40": {"name": "Input2", "bit_length": 8, "factor": 1, "offset": 0, "unit": "bool"},
 
                    "48": {"name": "Input3", "bit_length": 8, "factor": 1, "offset": 0, "unit": "bool"},
 
                    "56": {"name": "Input4", "bit_length": 8, "factor": 1, "offset": 0, "unit": "bool"}
 
                   }
 
        },
 
        {
 
        "name": "Stuff",
 
        "id": "0x205",
 
        "signals": { "0": {"name": "Nothing", "bit_length": 32, "factor": 1, "offset": 0, "unit": ""},
 
                    "32": {"name": "Input1", "bit_length": 8, "factor": 1, "offset": 0, "unit": "bool"},
 
                    "40": {"name": "Input2", "bit_length": 8, "factor": 1, "offset": 0, "unit": "bool"},
 
                    "48": {"name": "Input3", "bit_length": 8, "factor": 1, "offset": 0, "unit": "bool"},
 
                    "56": {"name": "Input4", "bit_length": 8, "factor": 1, "offset": 0, "unit": "bool"}
 
                   }
 
        },
 
        {
 
        "name": "WaterSense",
 
        "id": "0x206",
 
        "signals": { "0": {"name": "Nothing", "bit_length": 16, "factor": 1, "offset": 0, "unit": ""},
 
                    "16": {"name": "PercentFull", "bit_length": 8, "factor": 1, "offset": 0, "unit": "bool"},
 
                    "24": {"name": "Temperature", "bit_length": 8, "factor": 0.5, "offset": 0, "unit": "bool"},
 
                    "32": {"name": "RawDebug", "bit_length": 16, "factor": 1, "offset": 0, "unit": "bool"}
 
                    "16": {"name": "PercentFull", "bit_length": 8, "factor": 1, "offset": 0, "unit": "%"},
 
                    "24": {"name": "Temperature", "bit_length": 8, "factor": 0.5, "offset": 0, "unit": "C"},
 
                    "32": {"name": "RawDebug", "bit_length": 16, "factor": 1, "offset": 0, "unit": ""}
 
                   }
 
        }
 

	
 
    ]
 
}
0 comments (0 inline, 0 general)