BW3-Core/plugin/pluginBase.py

193 lines
6.1 KiB
Python
Raw Normal View History

2018-01-08 19:57:48 +01:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
r"""!
2018-01-08 19:57:48 +01:00
____ ____ ______ __ __ __ _____
/ __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ /
/ __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ <
/ /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ /
/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/
German BOS Information Script
by Bastian Schroll
2019-10-28 21:20:05 +01:00
@file: pluginBase.py
2018-01-08 19:57:48 +01:00
@date: 08.01.2018
@author: Bastian Schroll
@description: Plugin main class to inherit
"""
import logging
import time
2019-10-30 11:38:38 +01:00
from abc import ABC
2018-01-08 19:57:48 +01:00
2019-03-11 07:47:51 +01:00
from boswatch import wildcard
2018-01-14 16:28:11 +01:00
2018-01-08 19:57:48 +01:00
logging.debug("- %s loaded", __name__)
2019-10-30 11:08:26 +01:00
class PluginBase(ABC):
r"""!Main plugin class"""
2018-01-08 19:57:48 +01:00
_pluginsActive = []
2018-01-08 19:57:48 +01:00
def __init__(self, pluginName, config):
r"""!init preload some needed locals and then call onLoad() directly"""
2018-01-08 19:57:48 +01:00
self._pluginName = pluginName
self.config = config
self._pluginsActive.append(self)
2018-01-08 19:57:48 +01:00
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
# for statistics
self._runCount = 0
self._setupErrorCount = 0
self._alarmErrorCount = 0
self._teardownErrorCount = 0
logging.debug("[%s] onLoad()", pluginName)
2018-01-08 19:57:48 +01:00
self.onLoad()
2019-10-25 15:36:10 +02:00
def _cleanup(self):
r"""!Cleanup routine calls onUnload() directly"""
logging.debug("[%s] onUnload()", self._pluginName)
self._pluginsActive.remove(self)
2018-01-08 19:57:48 +01:00
self.onUnload()
2018-01-08 23:42:26 +01:00
def _run(self, bwPacket):
r"""!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
2019-10-24 23:42:52 +02:00
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
2019-10-24 23:42:52 +02:00
self._setupTime = time.time() - tmpTime
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
2019-10-24 23:42:52 +02:00
self._alarmTime = time.time() - tmpTime
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
2019-10-24 23:42:52 +02:00
self._teardownTime = time.time() - tmpTime
2018-01-09 13:35:54 +01:00
self._sumTime = self._setupTime + self._alarmTime + self._teardownTime
2018-01-14 23:21:22 +01:00
self._cumTime += self._sumTime
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
return None
2018-01-09 13:35:54 +01:00
def _getStatistics(self):
r"""!Returns statistical information's from last plugin run
2018-01-09 13:35:54 +01:00
2018-01-12 12:33:41 +01:00
@return Statistics as pyton dict"""
2019-10-25 13:25:56 +02:00
stats = {"type": "plugin",
"runCount": self._runCount,
2018-01-14 23:21:22 +01:00
"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):
r"""!Called by import of the plugin
can be inherited"""
2018-01-08 19:57:48 +01:00
pass
def setup(self):
r"""!Called before alarm
can be inherited"""
2018-01-08 19:57:48 +01:00
pass
2018-01-14 20:06:31 +01:00
def fms(self, bwPacket):
r"""!Called on FMS alarm
can be inherited
2018-01-08 19:57:48 +01:00
@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):
r"""!Called on POCSAG alarm
can be inherited
2018-01-14 20:06:31 +01:00
@param bwPacket: bwPacket instance"""
logging.warning("POCSAG not implemented in %s", self._pluginName)
def zvei(self, bwPacket):
r"""!Called on ZVEI alarm
can be inherited
2018-01-14 20:06:31 +01:00
@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):
r"""!Called on MSG packet
can be inherited
2018-02-22 08:28:02 +01:00
@param bwPacket: bwPacket instance"""
logging.warning("MSG not implemented in %s", self._pluginName)
2018-01-08 19:57:48 +01:00
def teardown(self):
r"""!Called after alarm
can be inherited"""
2018-01-08 19:57:48 +01:00
pass
def onUnload(self):
r"""!Called on shutdown of boswatch
can be inherited"""
2018-01-08 19:57:48 +01:00
pass
2018-02-22 13:36:50 +01:00
def parseWildcards(self, msg):
r"""!Return the message with parsed wildcards"""
if self._bwPacket is None:
logging.warning("wildcard replacing not allowed - no bwPacket set")
return msg
2020-02-22 23:47:13 +01:00
return wildcard.replaceWildcards(msg, self._bwPacket)