Add command to get whole machine
authorBenjamin Braatz <benjamin.braatz@graph-it.com>
Wed, 10 Mar 2021 08:39:58 +0000 (09:39 +0100)
committerBenjamin Braatz <benjamin.braatz@graph-it.com>
Wed, 10 Mar 2021 08:39:58 +0000 (09:39 +0100)
controlpi-plugins/statemachine.py

index 57954795ed1a16588163098879ab5b769b91cdf0..17323981ab823abc538ec2459782323da126ffd6 100644 (file)
@@ -3,7 +3,7 @@
 TODO: documentation, doctests
 """
 import jsonschema  # type: ignore
-from typing import Iterable, Mapping, Any
+from typing import Mapping, Any
 
 from controlpi import BasePlugin, Message, PluginConfiguration
 
@@ -25,9 +25,16 @@ def template_from_message(message: Message) -> Message:
 class StateMachine(BasePlugin):
     async def _receive(self, message: Message) -> None:
         if ('target' in message and message['target'] == self._name and
-                'command' in message and message['command'] == 'get state'):
-            answer = {'sender': self._name, 'state': self._current_state}
-            await self._bus.send(answer)
+                'command' in message):
+            if message['command'] == 'get state':
+                answer = {'sender': self._name,
+                          'state': self._current_state}
+                await self._bus.send(answer)
+            if message['command'] == 'get machine':
+                answer = {'sender': self._name,
+                          'init': self._init,
+                          'states': self._states}
+                await self._bus.send(answer)
         for transition in self._states[self._current_state]['transitions']:
             for trigger in transition['triggers']:
                 matches = True
@@ -53,13 +60,23 @@ class StateMachine(BasePlugin):
                     break
 
     def _process_conf(self, conf: PluginConfiguration) -> None:
-        self._current_state = conf['init']
+        self._init = conf['init']
+        self._current_state = ''
         self._states: Mapping[str, Any] = conf['states']
         sends: list[Message] = [{'event': {'const': 'changed'},
                                  'state': {'type': 'string'}},
-                                {'state': {'type': 'string'}}]
+                                {'state': {'type': 'string'}},
+                                {'init': {'type': 'string'}, 'states':
+                                 {'type': 'object', 'patternProperties':
+                                  {'.+': {'type': 'object', 'properties':
+                                   {'commands': {'type': 'array'},
+                                    'transitions': {'type': 'array'}},
+                                   'additionalProperties': False}},
+                                  'additionalProperties': False}}]
         receives: list[Message] = [{'target': {'const': self._name},
-                                    'command': {'const': 'get state'}}]
+                                    'command': {'const': 'get state'}},
+                                   {'target': {'const': self._name},
+                                    'command': {'const': 'get machine'}}]
         for state in self._states:
             for message in self._states[state]['commands']:
                 sends.append(template_from_message(message))
@@ -72,7 +89,8 @@ class StateMachine(BasePlugin):
 
     async def run(self) -> None:
         await super().run()
-        for message in self._states[self._current_state]['commands']:
+        self._current_state = self._init
+        for message in self._states[self._init]['commands']:
             complete_message = {'sender': self._name}
             complete_message.update(message)
             await self._bus.send(complete_message)