[ [ "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 } }
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):
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)
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']
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):
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(
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,