From 6cb62c4dbb8f35cb4f320c4236461a3ae6305ea3 Mon Sep 17 00:00:00 2001 From: Benjamin Braatz Date: Wed, 23 Aug 2023 15:51:27 +0200 Subject: [PATCH] Beispiel-Code --- doc/index.md | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 105 insertions(+), 1 deletion(-) diff --git a/doc/index.md b/doc/index.md index a40b26c..ee383d6 100644 --- a/doc/index.md +++ b/doc/index.md @@ -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 -- 2.34.1