BW3-Core/boswatch/plugin/plugin.py

198 lines
6.4 KiB
Python
Raw Normal View History

2018-01-08 19:57:48 +01:00
#!/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
2018-01-14 16:28:11 +01:00
from boswatch.utils import paths
2019-02-27 12:41:29 +01:00
from boswatch import configYaml
2018-02-22 13:36:50 +01:00
from boswatch.utils import wildcard
2018-01-14 16:28:11 +01:00
2018-01-08 19:57:48 +01:00
logging.debug("- %s loaded", __name__)
class Plugin:
"""!Main plugin class"""
_pluginsActive = 0
def __init__(self, pluginName):
2018-01-09 13:35:54 +01:00
"""!init preload some needed locals and then call onLoad() directly"""
2018-01-08 19:57:48 +01:00
self._pluginName = pluginName
self._pluginsActive += 1
2018-02-22 13:36:50 +01:00
# to save the packet while alarm is running for other functions
self._bwPacket = None
2018-01-08 19:57:48 +01:00
# for time counting
2018-01-09 13:35:54 +01:00
self._sumTime = 0
2018-01-14 23:21:22 +01:00
self._cumTime = 0
2018-01-08 19:57:48 +01:00
self._setupTime = 0
self._alarmTime = 0
self._teardownTime = 0
2018-09-18 05:59:38 +02:00
self._endTime = 0
2018-01-09 13:35:54 +01:00
self._tmpTime = 0
2018-01-08 19:57:48 +01:00
# for statistics
self._runCount = 0
self._setupErrorCount = 0
self._alarmErrorCount = 0
self._teardownErrorCount = 0
2019-02-27 12:41:29 +01:00
if paths.fileExist(paths.PLUGIN_PATH + pluginName + "/" + pluginName + ".yaml"):
self.config = configYaml.loadConfigFile(paths.PLUGIN_PATH + pluginName + "/" + pluginName + ".yaml")
2018-01-14 20:06:31 +01:00
else:
logging.debug("no config for %s found", pluginName)
2018-01-14 16:28:11 +01:00
logging.debug("[%s] onLoad()", pluginName)
2018-01-08 19:57:48 +01:00
self.onLoad()
def __del__(self):
"""!Destructor calls onUnload() directly"""
logging.debug("[%s] onUnload()", self._pluginName)
2018-01-08 19:57:48 +01:00
self._pluginsActive -= 1
self.onUnload()
2018-01-08 23:42:26 +01:00
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"""
2018-01-08 19:57:48 +01:00
self._runCount += 1
logging.debug("[%s] run #%d", self._pluginName, self._runCount)
2018-01-08 19:57:48 +01:00
self._bwPacket = bwPacket
2018-01-09 13:35:54 +01:00
self._tmpTime = time.time()
2018-01-08 19:57:48 +01:00
try:
logging.debug("[%s] setup()", self._pluginName)
2018-01-08 19:57:48 +01:00
self.setup()
except:
self._setupErrorCount += 1
logging.exception("[%s] error in setup()", self._pluginName)
2018-01-08 19:57:48 +01:00
2018-01-09 13:35:54 +01:00
self._setupTime = time.time() - self._tmpTime
self._tmpTime = time.time()
2018-01-08 19:57:48 +01:00
try:
2018-01-14 20:06:31 +01:00
2018-09-18 05:59:38 +02:00
if bwPacket.get("mode") == "fms":
2018-01-14 20:06:31 +01:00
logging.debug("[%s] fms()", self._pluginName)
self.fms(bwPacket)
2018-09-18 05:59:38 +02:00
if bwPacket.get("mode") == "pocsag":
2018-01-14 20:06:31 +01:00
logging.debug("[%s] pocsag()", self._pluginName)
self.pocsag(bwPacket)
2018-09-18 05:59:38 +02:00
if bwPacket.get("mode") == "zvei":
2018-01-14 20:06:31 +01:00
logging.debug("[%s] zvei()", self._pluginName)
self.zvei(bwPacket)
2018-09-18 05:59:38 +02:00
if bwPacket.get("mode") == "msg":
2018-02-22 08:28:02 +01:00
logging.debug("[%s] msg()", self._pluginName)
self.msg(bwPacket)
2018-01-08 19:57:48 +01:00
except:
self._alarmErrorCount += 1
2018-01-14 20:06:31 +01:00
logging.exception("[%s] alarm error", self._pluginName)
2018-01-08 19:57:48 +01:00
2018-01-09 13:35:54 +01:00
self._alarmTime = time.time() - self._tmpTime
self._tmpTime = time.time()
2018-01-08 19:57:48 +01:00
try:
logging.debug("[%s] teardown()", self._pluginName)
2018-01-08 19:57:48 +01:00
self.teardown()
except:
self._teardownErrorCount += 1
logging.exception("[%s] error in teardown()", self._pluginName)
2018-01-08 19:57:48 +01:00
2018-01-09 13:35:54 +01:00
self._teardownTime = time.time() - self._tmpTime
self._sumTime = self._setupTime + self._alarmTime + self._teardownTime
2018-01-14 23:21:22 +01:00
self._cumTime += self._sumTime
2018-01-08 19:57:48 +01:00
self._endTime = time.time()
2018-02-22 13:36:50 +01:00
self._bwPacket = None
2018-01-09 13:35:54 +01:00
logging.debug("[%s] took %0.3f seconds", self._pluginName, self._sumTime)
2018-01-10 12:57:20 +01:00
# logging.debug("- setup: %0.2f sec.", self._setupTime)
# logging.debug("- alarm: %0.2f sec.", self._alarmTime)
# logging.debug("- teardown: %0.2f sec.", self._teardownTime)
2018-01-09 13:35:54 +01:00
def _getStatistics(self):
"""!Returns statistical information's from last plugin run
2018-01-12 12:33:41 +01:00
@return Statistics as pyton dict"""
2018-01-14 23:21:22 +01:00
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}
2018-01-12 12:33:41 +01:00
return stats
2018-01-08 19:57:48 +01:00
def onLoad(self):
"""!Called by import of the plugin
Must be inherit"""
pass
def setup(self):
"""!Called before alarm
Must be inherit"""
pass
2018-01-14 20:06:31 +01:00
def fms(self, bwPacket):
"""!Called on FMS alarm
2018-01-08 19:57:48 +01:00
Must be inherit
@param bwPacket: bwPacket instance"""
2018-01-14 20:06:31 +01:00
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)
2018-01-08 19:57:48 +01:00
2018-02-22 08:28:02 +01:00
def msg(self, bwPacket):
"""!Called on MSG packet
Must be inherit
@param bwPacket: bwPacket instance"""
logging.warning("MSG not implemented in %s", self._pluginName)
2018-01-08 19:57:48 +01:00
def teardown(self):
"""!Called after alarm
Must be inherit"""
pass
def onUnload(self):
"""!Called by destruction of the plugin
Must be inherit"""
pass
2018-02-22 13:36:50 +01:00
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
2018-02-22 13:36:50 +01:00
return wildcard.replaceWildcards(self._bwPacket, msg)