From c753eb2d4dc622f7042f83859a971cd522a404d0 Mon Sep 17 00:00:00 2001 From: Benjamin Braatz Date: Thu, 21 Jan 2021 15:43:40 +0100 Subject: [PATCH] Modbus integration --- conf.json | 5 ++++- graphit_controlpi/config.py | 10 +++++++++- graphit_controlpi/main.py | 5 +++-- graphit_controlpi/websocket.py | 26 +++++++++++++++++++------- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/conf.json b/conf.json index f60ebe7..f3d1757 100644 --- a/conf.json +++ b/conf.json @@ -50,4 +50,7 @@ [ [ "E4-1", "T2-9" ], [ "E4-2", "T2-10" ], [ "E4-3", "T2-11" ], [ "E4-4", "T2-12" ], [ "E4-5", "T2-13" ], [ "E4-6", "T2-14" ], - [ "E4-7", "T2-15" ], [ "E4-8", "T2-16" ] ] } ] } + [ "E4-7", "T2-15" ], [ "E4-8", "T2-16" ] ] } ], + "modbus": + { "serial device": "/dev/serial1", + "slave id": 1 } } diff --git a/graphit_controlpi/config.py b/graphit_controlpi/config.py index 6a7f27a..3b44e77 100644 --- a/graphit_controlpi/config.py +++ b/graphit_controlpi/config.py @@ -1,4 +1,6 @@ from graphit_pin import PCF8574Output, PCF8574Input, GPIOInputPin +from graphit_modbus.transport import SerialPort, SerialClient +from graphit_modbus.hitachi import SJP1Fu async def process_configuration(conf, queues): @@ -29,4 +31,10 @@ async def process_configuration(conf, queues): for pin_name in pin_names: pins[pin_name] = pin pin.on('change', callback_factory(pin_name)) - return pins + fu = None + if 'modbus' in conf: + modbus_conf = conf['modbus'] + port = SerialPort(modbus_conf['serial device']) + client = SerialClient(port, modbus_conf['slave id']) + fu = SJP1Fu(client) + return (pins, fu) diff --git a/graphit_controlpi/main.py b/graphit_controlpi/main.py index 593833a..d50df05 100644 --- a/graphit_controlpi/main.py +++ b/graphit_controlpi/main.py @@ -8,11 +8,12 @@ from .websocket import setup_websocket async def setup(): pins = {} + fu = None queues = [] with open(sys.argv[1]) as json_data: conf = json.load(json_data) - pins = await process_configuration(conf, queues) - await setup_websocket(pins, queues, sys.argv[2]) + (pins, fu) = await process_configuration(conf, queues) + await setup_websocket(pins, fu, queues, sys.argv[2]) if __name__ == '__main__': diff --git a/graphit_controlpi/websocket.py b/graphit_controlpi/websocket.py index 7ee7fee..6d3d000 100644 --- a/graphit_controlpi/websocket.py +++ b/graphit_controlpi/websocket.py @@ -7,7 +7,7 @@ import websockets from http import HTTPStatus -async def process_command(command, pins, queue): +async def process_command(command, pins, fu, queue): if command['command'] == 'setpin': if command['pin'] in pins and pins[command['pin']].settable: pins[command['pin']].value = command['value'] @@ -23,12 +23,24 @@ async def process_command(command, pins, queue): await queue.put({'event': 'pinstate', 'pin': pin_name, 'settable': pin.settable, 'value': pin.value, 'changed': False}) + elif command['command'] == 'setfrequency': + await fu.set_frequency(command['value']) + elif command['command'] == 'getfrequency': + frequency = await fu.get_frequency() + await queue.put({'event': 'frequency', 'frequency': frequency}) + elif command['command'] == 'startinverter': + await fu.start_inverter() + elif command['command'] == 'stopinverter': + await fu.stop_inverter() + elif command['command'] == 'getinverter': + active = await fu.inverter_active + await queue.put({'event': 'inverterstate', 'active': active}) -async def command_handler(websocket, path, pins, queue): +async def command_handler(websocket, path, pins, fu, queue): async for message in websocket: command = json.loads(message) - await process_command(command, pins, queue) + await process_command(command, pins, fu, queue) async def event_handler(websocket, path, queue): @@ -39,11 +51,11 @@ async def event_handler(websocket, path, queue): queue.task_done() -async def handler(pins, queues, websocket, path): +async def handler(pins, fu, queues, websocket, path): queue = asyncio.Queue() queues.append(queue) command_task = asyncio.create_task(command_handler(websocket, path, - pins, queue)) + pins, fu, queue)) event_task = asyncio.create_task(event_handler(websocket, path, queue)) done, pending = await asyncio.wait( @@ -98,8 +110,8 @@ async def get_ip(): return ip -async def setup_websocket(pins, queues, server_root): - parameterised_handler = functools.partial(handler, pins, queues) +async def setup_websocket(pins, fu, queues, server_root): + parameterised_handler = functools.partial(handler, pins, fu, queues) parameterised_process_request = functools.partial(process_request, server_root) hostname = await get_ip() await websockets.serve(parameterised_handler, hostname, 80, -- 2.34.1