diff --git a/hydrobot.py b/hydrobot.py --- a/hydrobot.py +++ b/hydrobot.py @@ -59,12 +59,6 @@ def main(argv): scheduler = Scheduler(module_network, config) scheduler.start() - - #module = module_network.module_list.lookup_module_by_name("RelayDrive1") - #message = HydroBotMessage(module.uuid, (0x80 | protocol.lookup_command_key_by_name("config")), protocol.lookup_data_key_by_name("can_id"), 0, 0x300) - #module.send_message(message) - #message = HydroBotMessage(module.uuid, (0x80 | protocol.lookup_command_key_by_name("config")), protocol.lookup_data_key_by_name("data_rate"), 0, 10) - #module.send_message(message) while True: time.sleep(0.1) diff --git a/hydrobot_can.json b/hydrobot_can.json deleted file mode 100644 --- a/hydrobot_can.json +++ /dev/null @@ -1,55 +0,0 @@ - -{ - "messages": [ - { - "name": "AirSense", - "id": "0x202", - "signals": { "0": {"name": "Nothing", "bit_length": 16, "factor": 1, "offset": 0, "unit": ""}, - "16": {"name": "Temperature", "bit_length": 16, "factor": 0.1, "offset": 40, "unit": "C"}, - "32": {"name": "Humidity", "bit_length": 16, "factor": 0.1, "offset": 0, "unit": "%RH"}, - "48": {"name": "Pressure", "bit_length": 16, "factor": 0.1, "offset": 0, "unit": "hPa"} - } - }, - { - "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": "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": "Temperature", "bit_length": 8, "factor": 0.5, "offset": 40, "unit": "C"}, - "8": {"name": "Nothing", "bit_length": 16, "factor": 1, "offset": 0, "unit": ""}, - "24": {"name": "PercentFull", "bit_length": 8, "factor": 1, "offset": 0, "unit": "%"} - - } - } - - ] -} diff --git a/hydrobot_data_test.py b/hydrobot_data_test.py deleted file mode 100644 --- a/hydrobot_data_test.py +++ /dev/null @@ -1,40 +0,0 @@ -import time -import random -from influxdb import InfluxDBClient - -temp = 65.5 -humid = 0.50 -press = 950 - -while True: - - temp = random.randint(-1, 1)/2 + temp - humid = random.randint(-1, 1)/100 + humid - press = random.randint(-1, 1) + press - - json_body = [ - { - "measurement": "air_temp", - "fields": { - "value": temp - } - }, - { - "measurement": "air_humidity", - "fields": { - "value": humid - } - }, - { - "measurement": "air_pressure", - "fields": { - "value": press - } - } - ] - - client = InfluxDBClient("www.protofusion.org", 8086, 'admin', 'admin', 'hydrobot') - client.write_points(json_body) - print("yes!") - - time.sleep(60) \ No newline at end of file diff --git a/hydrobot_def.json b/hydrobot_def.json --- a/hydrobot_def.json +++ b/hydrobot_def.json @@ -10,14 +10,52 @@ "id" : "0x01", "name" : "airsense", "display" : "AirSense", - "class" : "AirSenseModule" + "class" : "AirSenseModule", + "sensors" : [ + { + "name" : "temp", + "type" : "input", + "sensor_num" : "0", + "functions" : [ + "air_temp" + ] + + }, + { + "name" : "humidity", + "type" : "input", + "sensor_num" : "0", + "functions" : [ + "air_humidity" + ] + + }, + { + "name" : "pressure", + "type" : "input", + "sensor_num" : "0", + "functions" : [ + "air_pressure" + ] + + }, + { + "name" : "light", + "type" : "input", + "sensor_num" : "0", + "functions" : [ + "ambient_light" + ] + + } + ] }, { "id" : "0x02", "name" : "relaydrive", "display" : "RelayDrive", "class" : "RelayDriveModule", - "io" : [ + "sensors" : [ { "name" : "input_1", "type" : "input", @@ -89,6 +127,78 @@ "digital_in", "freq_in" ] + }, + { + "name" : "output_1", + "type" : "output", + "sensor_num" : "1", + "functions" : [ + "digital_out", + "pwm_out" + ] + }, + { + "name" : "output_2", + "type" : "output", + "sensor_num" : "2", + "functions" : [ + "digital_out", + "pwm_out" + ] + }, + { + "name" : "output_3", + "type" : "output", + "sensor_num" : "3", + "functions" : [ + "digital_out", + "pwm_out" + ] + }, + { + "name" : "output_4", + "type" : "output", + "sensor_num" : "4", + "functions" : [ + "digital_out", + "pwm_out" + ] + }, + { + "name" : "output_5", + "type" : "output", + "sensor_num" : "5", + "functions" : [ + "digital_out", + "pwm_out" + ] + }, + { + "name" : "output_6", + "type" : "output", + "sensor_num" : "6", + "functions" : [ + "digital_out", + "pwm_out" + ] + }, + { + "name" : "output_7", + "type" : "output", + "sensor_num" : "7", + "functions" : [ + "digital_out", + "pwm_out" + ] + }, + { + "name" : "output_8", + "type" : "output", + "sensor_num" : "8", + "functions" : [ + "digital_out", + "pwm_out" + ] } ] }, @@ -96,7 +206,27 @@ "id" : "0x03", "name" : "watersense", "display" : "WaterSense", - "class" : "WaterSenseModule" + "class" : "WaterSenseModule", + "sensors" : [ + { + "name" : "temp", + "type" : "input", + "sensor_num" : "0", + "functions" : [ + "water_temp" + ] + + }, + { + "name" : "level", + "type" : "input", + "sensor_num" : "0", + "functions" : [ + "water_level" + ] + + } + ] }, { "id" : "0x04", diff --git a/hydrobot_sensor_sim.py b/hydrobot_sensor_sim.py deleted file mode 100644 --- a/hydrobot_sensor_sim.py +++ /dev/null @@ -1,31 +0,0 @@ -import time -import random -from canard import can, bus -from canard.hw import socketcan -from canard.file import jsondb - -parser = jsondb.JsonDbParser() -b = parser.parse('hydrobot_can.json') - -dev = socketcan.SocketCanDev("can1") -dev.start() - -temp = 65.5 -humid = 0.90 -press = 100 - -while True: - - temp = max(0x00, min(0xFF, random.randint(-1, 1)/2 + temp)) - humid = max(0x00, min(0xFF, random.randint(-1, 1)/100 + humid)) - press = max(0x00, min(0xFF, random.randint(-1, 1) + press)) - - print(b.AirTemp.Temperature.value) - - b.AirTemp.Temperature.value = temp - b.AirTemp.Humidity.value = humid - b.AirTemp.Pressure.value = press - - dev.send(b.AirTemp.encode()) - - time.sleep(1) diff --git a/module.py b/module.py --- a/module.py +++ b/module.py @@ -29,8 +29,14 @@ class ModuleList(): network_interface.address_lookup[module_address] = new_module.uuid network_interface.uuid_lookup[new_module.uuid] = module_address - new_module.config("data_rate", int(self.config.get(section, "data_rate"))) - new_module.config("led_brightness", int(self.config.get(section, "led_brightness"))) + new_module.config("data_rate", 0, int(self.config.get(section, "data_rate"))) + new_module.config("led_brightness", 0, int(self.config.get(section, "led_brightness"))) + + for sensor in protocol.lookup_sensors_by_device_name(module_type): + for item in self.config.items(section): + if item[0] == sensor.get('name'): + new_module.config(sensor.get('type'), int(sensor.get('sensor_num')), protocol.lookup_data_key_by_name(self.config.get(section, sensor.get('name')))) + def add_module(self, module): assert isinstance(module, Module), 'invalid module' @@ -63,14 +69,6 @@ class ModuleList(): device_class = eval(protocol.lookup_device_class_by_id(device_id)) module = device_class(address, interface, name, self.database) - - #if device_type == "airsense": - #module = AirSenseModule(address, interface, name, self.database) - #elif device_type == "relaydrive": - #module = RelayDriveModule(address, interface, name, self.database) - #else: - #module = UnknownModule(address, interface, name, self.database) - self.add_module(module) return module @@ -135,8 +133,8 @@ class AirSenseModule(Module): def update(self): pass - def config(self, data_key, value): - message = HydroBotMessage(self.uuid, (0x80 | protocol.lookup_command_key_by_name("config")), protocol.lookup_data_key_by_name(data_key), 0, value) + def config(self, data_key, sensor_num, value): + message = HydroBotMessage(self.uuid, (0x80 | protocol.lookup_command_key_by_name("config")), protocol.lookup_data_key_by_name(data_key), sensor_num, value) self.interface.send_message(message) class RelayDriveModule(Module): @@ -155,8 +153,8 @@ class RelayDriveModule(Module): def update(self): pass - def config(self, data_key, value): - message = HydroBotMessage(self.uuid, (0x80 | protocol.lookup_command_key_by_name("config")), protocol.lookup_data_key_by_name(data_key), 0, value) + def config(self, data_key, sensor_num, value): + message = HydroBotMessage(self.uuid, (0x80 | protocol.lookup_command_key_by_name("config")), protocol.lookup_data_key_by_name(data_key), sensor_num, value) self.interface.send_message(message) def set_output(self, output, value): @@ -177,7 +175,7 @@ class UnknownModule(Module): def update(self): pass - def config(self, data_key, value): - message = HydroBotMessage(self.uuid, (0x80 | protocol.lookup_command_key_by_name("config")), protocol.lookup_data_key_by_name(data_key), 0, value) + def config(self, data_key, sensor_num, value): + message = HydroBotMessage(self.uuid, (0x80 | protocol.lookup_command_key_by_name("config")), protocol.lookup_data_key_by_name(data_key), sensor_num, value) self.interface.send_message(message) diff --git a/protocol.py b/protocol.py --- a/protocol.py +++ b/protocol.py @@ -71,3 +71,9 @@ def lookup_command_display_by_key(key): if command.get('key') == "{0:#0{1}x}".format(key, 6): return command.get('display') +def lookup_sensors_by_device_name(name): + devices = _protocol_def['devices'] + for device in devices: + if device.get('name') == name: + return device.get('sensors') +