Beispiel-Code
authorBenjamin Braatz <benjamin.braatz@graph-it.com>
Wed, 23 Aug 2023 13:51:27 +0000 (15:51 +0200)
committerBenjamin Braatz <benjamin.braatz@graph-it.com>
Wed, 23 Aug 2023 13:51:27 +0000 (15:51 +0200)
doc/index.md

index a40b26cca87eaf0334c36e6fdf1c36296a514f71..ee383d67804e411715fae8595015d08089b6d88f 100644 (file)
@@ -139,11 +139,115 @@ Da wir aber noch nichts entwickelt, die `conf.json` noch nicht angepasst
 haben und diese noch einen Platzhalter enthält, passiert hier noch nichts.
 
 ## Implementierung des Plugins selbst
+Das Plugin wird in der Datei `controlpi_plugins/overshoot.py`implementiert,
+die zunächst einige benötigte Module, Klassen und Typen implementiert:
+```python
+"""ControlPi Plugin for Giving a State Overshoot Time."""
+import asyncio
+
+from controlpi import BasePlugin, Message, MessageTemplate
+from controlpi.baseplugin import JSONSchema
+```
 
+Jedes Plugin erbt von der abstrakten Klasse `controlpi.BasePlugin`:
+```python
+class Overshoot(BasePlugin):
+    """ControlPi plugin for Giving a State Overshoot Time."""
+```
 
+Es müssen hierbei zumindest die Konstante `CONF_SCHEMA`, die synchrone
+Methode `process_conf` und die asynchrone Methode `run` implementiert
+werden.
 
 ### Konfigurations-Schema
+Die Konstante `CONF_SCHEMA` enthält ein JSON-Schema, das definiert, wie die
+Konfiguration für dieses Plugin beschaffen ist.
+
+In unserem Fall ...
+```python
+    CONF_SCHEMA: JSONSchema = {'properties':
+                               {'state': {'type': 'string'},
+                                'overshoot': {'type': 'integer',
+                                              'default': 10}},
+                               'required': ['state']}
+```
 
-### Registrierung am Message-Bus
+### Verarbeiten der Konfiguration und Registrieren am Message-Bus
+
+```python
+    def process_conf(self) -> None:
+        """Register bus client."""
+        self._state = False
+        self.bus.register(self.name, 'Overshoot',
+                          [MessageTemplate({'event':
+                                            {'const': 'changed'},
+                                            'state':
+                                            {'type': 'boolean'}}),
+                           MessageTemplate({'state':
+                                            {'type': 'boolean'}}),
+                           MessageTemplate({'target':
+                                            {'const': self.conf['state']},
+                                            'command':
+                                            {'const': 'set state'},
+                                            'new state':
+                                            {'type': 'boolean'}})],
+                          [([MessageTemplate({'target':
+                                              {'const': self.name},
+                                              'command':
+                                              {'const': 'get state'}})],
+                            self._get_state),
+                           ([MessageTemplate({'target':
+                                              {'const': self.name},
+                                              'command':
+                                              {'const': 'set state'},
+                                              'new state':
+                                              {'type': 'boolean'}})],
+                            self._set_state)])
+```
 
 ### Callbacks
+
+```python
+    async def _get_state(self, message) -> None:
+        await self.bus.send(Message(self.name, {'state': self._state}))
+```
+
+```python
+    async def _set_state(self, message) -> None:
+```
+
+### Haupt-Routine des Plugins
+
+```python
+    async def run(self) -> None:
+        """Do nothing."""
+        pass
+```
+
+### Beispiel-Konfiguration und Test
+
+```json
+{
+    "Debug": {
+        "plugin": "WSServer",
+        "web": {
+            "/": {
+                "module": "controlpi_plugins.wsserver",
+                "location": "Debug"
+            }
+        }
+    },
+    "Log": {
+        "plugin": "Log",
+        "filter": [{}]
+    },
+    "Light": {
+        "plugin": "State"
+    },
+    "Light-Overshoot": {
+        "plugin": "Overshoot"
+    }
+}
+```
+
+### Style- und Typ-Checks