Modbus integration
authorBenjamin Braatz <bb@bbraatz.eu>
Thu, 21 Jan 2021 14:43:40 +0000 (15:43 +0100)
committerBenjamin Braatz <bb@bbraatz.eu>
Thu, 21 Jan 2021 14:43:40 +0000 (15:43 +0100)
conf.json
graphit_controlpi/config.py
graphit_controlpi/main.py
graphit_controlpi/websocket.py

index f60ebe7ef41e09dab9c08b9d56a24fb8fb8a6665..f3d175770a9c92ee73e1ee69bd275f64f75dfe4b 100644 (file)
--- 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 } }
index 6a7f27a7cc34e1c5341f4033bf9d5725aef22180..3b44e77240d8dc52c16decd158e743e69b93ba7c 100644 (file)
@@ -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)
index 593833a5ebc3f4bcd26f93b74b8269f80e156f80..d50df05a97dc51a19026849ab79afa7829a8c0a5 100644 (file)
@@ -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__':
index 7ee7fee515973276e4cac1da619d85eb177ea311..6d3d000200ff007af79a17ee96faf78e8fe1e389 100644 (file)
@@ -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,