some reworks

- rework configYaml
- rework router mechanism test
- move plugin and module files
This commit is contained in:
Bastian Schroll 2019-03-01 12:09:12 +01:00
parent 71d87b080f
commit a42676010e
19 changed files with 317 additions and 121 deletions

2
plugin/__init__.py Normal file
View file

@ -0,0 +1,2 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

193
plugin/plugin.py Normal file
View file

@ -0,0 +1,193 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""!
____ ____ ______ __ __ __ _____
/ __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ /
/ __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ <
/ /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ /
/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/
German BOS Information Script
by Bastian Schroll
@file: plugin.py
@date: 08.01.2018
@author: Bastian Schroll
@description: Plugin main class to inherit
"""
import logging
import time
from boswatch.utils import wildcard
logging.debug("- %s loaded", __name__)
class Plugin:
"""!Main plugin class"""
_pluginsActive = 0
def __init__(self, pluginName, config):
"""!init preload some needed locals and then call onLoad() directly"""
self._pluginName = pluginName
self.config = config
self._pluginsActive += 1
# to save the packet while alarm is running for other functions
self._bwPacket = None
# for time counting
self._sumTime = 0
self._cumTime = 0
self._setupTime = 0
self._alarmTime = 0
self._teardownTime = 0
self._endTime = 0
self._tmpTime = 0
# for statistics
self._runCount = 0
self._setupErrorCount = 0
self._alarmErrorCount = 0
self._teardownErrorCount = 0
logging.debug("[%s] onLoad()", pluginName)
self.onLoad()
def __del__(self):
"""!Destructor calls onUnload() directly"""
logging.debug("[%s] onUnload()", self._pluginName)
self._pluginsActive -= 1
self.onUnload()
def _run(self, bwPacket):
"""!start an complete running turn of an plugin.
Calls setup(), alarm() and teardown() in this order.
The alarm() method serves the BOSWatch packet to the plugin.
@param bwPacket: A BOSWatch packet instance"""
self._runCount += 1
logging.debug("[%s] run #%d", self._pluginName, self._runCount)
self._bwPacket = bwPacket
self._tmpTime = time.time()
try:
logging.debug("[%s] setup()", self._pluginName)
self.setup()
except:
self._setupErrorCount += 1
logging.exception("[%s] error in setup()", self._pluginName)
self._setupTime = time.time() - self._tmpTime
self._tmpTime = time.time()
try:
if bwPacket.get("mode") == "fms":
logging.debug("[%s] fms()", self._pluginName)
self.fms(bwPacket)
if bwPacket.get("mode") == "pocsag":
logging.debug("[%s] pocsag()", self._pluginName)
self.pocsag(bwPacket)
if bwPacket.get("mode") == "zvei":
logging.debug("[%s] zvei()", self._pluginName)
self.zvei(bwPacket)
if bwPacket.get("mode") == "msg":
logging.debug("[%s] msg()", self._pluginName)
self.msg(bwPacket)
except:
self._alarmErrorCount += 1
logging.exception("[%s] alarm error", self._pluginName)
self._alarmTime = time.time() - self._tmpTime
self._tmpTime = time.time()
try:
logging.debug("[%s] teardown()", self._pluginName)
self.teardown()
except:
self._teardownErrorCount += 1
logging.exception("[%s] error in teardown()", self._pluginName)
self._teardownTime = time.time() - self._tmpTime
self._sumTime = self._setupTime + self._alarmTime + self._teardownTime
self._cumTime += self._sumTime
self._endTime = time.time()
self._bwPacket = None
logging.debug("[%s] took %0.3f seconds", self._pluginName, self._sumTime)
# logging.debug("- setup: %0.2f sec.", self._setupTime)
# logging.debug("- alarm: %0.2f sec.", self._alarmTime)
# logging.debug("- teardown: %0.2f sec.", self._teardownTime)
return None
def _getStatistics(self):
"""!Returns statistical information's from last plugin run
@return Statistics as pyton dict"""
stats = {"runCount": self._runCount,
"sumTime": self._sumTime,
"cumTime": self._cumTime,
"setupTime": self._setupTime,
"alarmTime": self._alarmTime,
"teardownTime": self._teardownTime,
"setupErrorCount": self._setupErrorCount,
"alarmErrorCount": self._alarmErrorCount,
"teardownErrorCount": self._teardownErrorCount}
return stats
def onLoad(self):
"""!Called by import of the plugin
Must be inherit"""
pass
def setup(self):
"""!Called before alarm
Must be inherit"""
pass
def fms(self, bwPacket):
"""!Called on FMS alarm
Must be inherit
@param bwPacket: bwPacket instance"""
logging.warning("ZVEI not implemented in %s", self._pluginName)
def pocsag(self, bwPacket):
"""!Called on POCSAG alarm
Must be inherit
@param bwPacket: bwPacket instance"""
logging.warning("POCSAG not implemented in %s", self._pluginName)
def zvei(self, bwPacket):
"""!Called on ZVEI alarm
Must be inherit
@param bwPacket: bwPacket instance"""
logging.warning("ZVEI not implemented in %s", self._pluginName)
def msg(self, bwPacket):
"""!Called on MSG packet
Must be inherit
@param bwPacket: bwPacket instance"""
logging.warning("MSG not implemented in %s", self._pluginName)
def teardown(self):
"""!Called after alarm
Must be inherit"""
pass
def onUnload(self):
"""!Called by destruction of the plugin
Must be inherit"""
pass
def parseWildcards(self, msg):
"""!Return the message with parsed wildcards"""
if self._bwPacket is None:
logging.warning("wildcard replacing not allowed - no bwPacket set")
return msg
return wildcard.replaceWildcards(self._bwPacket, msg)

41
plugin/readme.md Normal file
View file

@ -0,0 +1,41 @@
## Eigene Plugins schreiben
Um ein eigenes Plugin zu schreiben, sollte man sich am besten zuerst einmal das Plugin `template` ansehen.
Dies kann als Vorlage für das eigene Plugin genutzt werden.
### 1.) Informationen anpassen
- Dateikopf anpassen
- Namen des Plugins vergeben in der `__init__` Methode `super().__init__("template")`
### 2.) Benötigte Methode überschreiben
Die Plugin Basisklasse bietet einige Methoden, welche vom Plugin überschrieben werden können.
- `onLoad()` wird direkt beim Import des Plugins ausgeführt
- `setup()` wird vor jeder Ausführung gerufen
- `fms(bwPacket)` wird bei einem FMS Paket ausgeführt
- `pocsag(bwPacket)` wird bei einem POCSAG Paket ausgeführt
- `zvei(bwPacket)` wird bei einem ZVEI Packet ausgeführt
- `msg(bwPacket)` wird bei einem Nachrichten Packet ausgeführt
- `teardown()` wird nach jeder Ausführung gerufen
- `onUnload()` wird beim Zerstören der Plugin Instanz zum Programmende ausgeführt
### 3.) Zugriff auf Config Datei
Wenn sich im Ordner des Plugins eine ini-Datei befindet,
welche exakt so wie das Plugin heißt, kann deren Inhalt
über die lokale Config-Reader Instanz
- `self.config.getBool(SECTION, KEY)`
- `self.config.getInt(SECTION, KEY)`
- `self.config.getStr(SECTION, KEY)`
abgerufen werden.
### 4.) Daten aus dem BOSWatch Paket lesen
An die Alarm Funktionen FMS, POCSAG und ZVEI wird eine Instanz eines
BOSWatch-Packet Objekts übergeben.
Aus dieser kann mittels `bwPacket.get(FELDNAME)` das entsprechende Feld
ausgelesen werden. Eine Auflistung der bereitgestellten Informationen
findet sich im entsprechenden BOSWatch-Packet Dokument.
### 5.) Wildcards parsen
Das parsen der Wildcars funktioniert komfortabel über die interne Methode `self.parseWildcards(MSG)`.
Die Platzhalter für die Wildcards findet man in `boswatch/utils/wildcard.py` oder in der `packet.md`-

70
plugin/template_plugin.py Normal file
View file

@ -0,0 +1,70 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""!
____ ____ ______ __ __ __ _____
/ __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ /
/ __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ <
/ /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ /
/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/
German BOS Information Script
by Bastian Schroll
@file: template_module.py
@date: 14.01.2018
@author: Bastian Schroll
@description: Template Plugin File
"""
import logging
from plugin.plugin import Plugin
# ###################### #
# Custom plugin includes #
# ###################### #
logging.debug("- %s loaded", __name__)
class BoswatchPlugin(Plugin):
"""!Description of the Plugin"""
def __init__(self, config):
"""!Do not change anything here!"""
super().__init__(__name__, config) # you can access the config DICT by 'self._config'
def onLoad(self):
"""!Called by import of the plugin"""
pass
def setup(self):
"""!Called before alarm"""
pass
def fms(self, bwPacket):
"""!Called on FMS alarm
@param bwPacket: bwPacket instance"""
pass
def pocsag(self, bwPacket):
"""!Called on POCSAG alarm
@param bwPacket: bwPacket instance"""
pass
def zvei(self, bwPacket):
"""!Called on ZVEI alarm
@param bwPacket: bwPacket instance"""
def msg(self, bwPacket):
"""!Called on MSG packet
@param bwPacket: bwPacket instance"""
def teardown(self):
"""!Called after alarm"""
pass
def onUnload(self):
"""!Called by destruction of the plugin"""
pass