Forgotten file
authorBenjamin Braatz <benjamin.braatz@graph-it.com>
Wed, 10 Feb 2021 13:25:47 +0000 (14:25 +0100)
committerBenjamin Braatz <benjamin.braatz@graph-it.com>
Wed, 10 Feb 2021 13:25:47 +0000 (14:25 +0100)
schaltschrank/config.py

index 9190762c46060edad233cd0d22f2f15d992977ea..7c537d0d8c0281b8dda8c938a96d4ea8c5a19896 100644 (file)
@@ -1,7 +1,7 @@
 import functools
 import asyncio
 import serialio
-from gpin import PCF8574Output, PCF8574Input, GPIOInputPin
+from gpin import PCF8574Output, PCF8574Input, GPIOInputPin, AndAggregatePin
 from gmodbus.transport import SerialClient
 from gmodbus.hitachi import SJP1Fu
 
@@ -17,6 +17,7 @@ async def pin_handler(queues, pin_name, pin):
             for out_queue in queues:
                 await out_queue.put({'name': 'pinstate', 'pin': pin_name,
                                      'value': pin.value, 'changed': False})
+        in_queue.task_done()
 
 
 def pin_callback(queues, pin_name, value):
@@ -26,6 +27,7 @@ def pin_callback(queues, pin_name, value):
 
 
 async def process_card_conf(card_conf, queues):
+    pins = {}
     tasks = []
     card = None
     if card_conf['type'] == 'output':
@@ -39,9 +41,22 @@ async def process_card_conf(card_conf, queues):
             pin = card.getPin(i)
             pin_names = card_conf['pins'][i]
             for pin_name in pin_names:
+                pins[pin_name] = pin
                 pin.on('change', functools.partial(pin_callback, queues, pin_name))
                 tasks.append(asyncio.create_task(pin_handler(queues, pin_name, pin)))
-    return tasks
+    return pins, tasks
+
+
+async def process_andpin_conf(andpin_conf, queues, pins):
+    andpin_name = andpin_conf['name']
+    aggregated = []
+    for pin_name in andpin_conf['pins']:
+        if pin_name in pins:
+            aggregated.append(pins)
+        else:
+            print(f"'{pin_name}' not found in conf of '{andpin_name}'.")
+    andpin = AndAggregatePin(aggregated)
+    andpin.on('change', functools.partial(pin_callback, queues, andpin_name))
 
 
 async def fu_handler(queues):
@@ -49,22 +64,40 @@ async def fu_handler(queues):
     queues.append(in_queue)
     while True:
         event = await in_queue.get()
-        if event['name'] == 'setparameters':
-            await fu.set_parameters()
-        elif event['name'] == 'setfrequency':
-            await fu.set_frequency(event['frequency'])
-        elif event['name'] == 'getfrequency':
-            frequency = await fu.get_frequency()
+        if event['name'] == 'motorstop':
+            await fu.stop_inverter()
+            for out_queue in queues:
+                await out_queue.put({'name': 'motoraus'})
+        elif event['name'] == 'dauervor':
+            for out_queue in queues:
+                await out_queue.put({'name': 'motoran'})
+            await fu.set_frequency(50)
+            await fu.start_inverter()
+        elif event['name'] == 'tippenvor':
+            for out_queue in queues:
+                await out_queue.put({'name': 'motoran'})
+            await fu.set_frequency(25)
+            await fu.start_inverter()
+            await asyncio.sleep(0.15)
+            await fu.stop_inverter()
+            for out_queue in queues:
+                await out_queue.put({'name': 'motoraus'})
+            await asyncio.sleep(1)
+            for out_queue in queues:
+                await out_queue.put({'name': 'tippenfertig'})
+        elif event['name'] == 'tippenrück':
             for out_queue in queues:
-                await out_queue.put({'name': 'frequency', 'frequency': frequency})
-        elif event['name'] == 'startinverter':
+                await out_queue.put({'name': 'motoran'})
+            await fu.set_frequency(-25)
             await fu.start_inverter()
-        elif event['name'] == 'stopinverter':
+            await asyncio.sleep(0.15)
             await fu.stop_inverter()
-        elif event['name'] == 'getinverter':
-            active = await fu.inverter_active
             for out_queue in queues:
-                await out_queue.put({'name': 'inverterstate', 'active': active})
+                await out_queue.put({'name': 'motoraus'})
+            await asyncio.sleep(1)
+            for out_queue in queues:
+                await out_queue.put({'name': 'tippenfertig'})
+        in_queue.task_done()
 
 
 async def process_modbus_conf(modbus_conf, queues):
@@ -76,13 +109,56 @@ async def process_modbus_conf(modbus_conf, queues):
     return asyncio.create_task(fu_handler(queues))
 
 
+async def state_handler(sm_conf, queues):
+    in_queue = asyncio.Queue()
+    queues.append(in_queue)
+    sm_name = sm_conf['name']
+    states = sm_conf['states']
+    current_state = sm_conf['init']
+    while True:
+        if current_state in states:
+            for command in states[current_state]['commands']:
+                for out_queue in queues:
+                    await out_queue.put(command)
+            current_state = new_state
+        else:
+            print(f"'{current_state}' not found in conf of '{sm_name}'.")
+        while True:
+            event = await in_queue.get()
+            for trigger in states[current_state]['events']:
+                triggered = True
+                for key in trigger:
+                    if key != 'state':
+                        if key in event:
+                            if event[key] != trigger[key]:
+                                triggered = False
+                                break
+                        else:
+                            triggered = False
+                            break
+                if triggered:
+                    current_state = trigger['state']
+                    break
+                triggered = False
+            in_queue.task_done()
+            if triggered:
+                break
+
+
 async def process_conf(conf, queues):
+    pins = {}
     tasks = []
     if 'i/o cards' in conf:
         for card_conf in conf['i/o cards']:
-            tasks.extend(await process_card_conf(card_conf, queues))
-    pins['T1-1'].value = True
-    fu = None
+            new_pins, new_tasks = await process_card_conf(card_conf, queues))
+            pins.update(new_pins)
+            tasks.extend(new_tasks)
+    if 'andpins' in conf:
+        for andpin_conf in conf['andpins']:
+            await process_andpin_conf(andpin_conf, queues, pins)
     if 'modbus' in conf:
         tasks.append(await process_modbus_conf(conf['modbus'], queues))
+    if 'statemachines' in conf:
+        for sm_conf in conf['statemachines']:
+            tasks.append(asyncio.create_task(state_handler(sm_conf, queues)))
     return tasks