BW3-Core/plugin/pluginBase.py
KoenigMjr 0b9387af08 [feat/multicast]: add multi-instance multicast module with active trigger system
Introduce a robust multicast processing module for POCSAG that correlates
empty tone-RICs (recipients) with subsequent text-RICs (content).

Key Features:
- Four Output Modes: Internally supports 'complete', 'incomplete', 'single',
  and 'control'. Functional alarms are delivered as the first three, while
  technical 'control' packets (Delimiters/NetIdent) are filtered by default.
- Active Trigger System: Implements a loss-free deferred delivery mechanism
  using a loopback socket (TCP) to re-inject wakeup packets, flushing the
  internal queue during auto-clear timeouts.
- Shared State & Multi-Instance: State is shared across instances but
  separated by frequency to prevent crosstalk in multi-frequency setups.
- Data Aggregation: Automatically generates '{FIELD}_list' wildcards (e.g.,
  RIC_LIST, DESCRIPTION_LIST) for all collected recipients, enabling
  consolidated notifications in downstream plugins.
- Dynamic Filtering: Automatically blocks Delimiter and NetIdent RICs from
  reaching subsequent plugins if they are defined in the configuration.

Infrastructural Changes:
- ModuleBase: Expanded return semantics to support:
  * False: Explicitly blocks/drops a packet.
  * List: Allows a module to expand one input into multiple output packets.
- PluginBase: Updated to handle lists of packets, ensuring a full
  setup->alarm->teardown lifecycle for every individual element.
2026-03-05 13:52:48 +01:00

202 lines
6.5 KiB
Python

#!/usr/bin/python
# -*- coding: utf-8 -*-
r"""!
____ ____ ______ __ __ __ _____
/ __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ /
/ __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ <
/ /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ /
/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/
German BOS Information Script
by Bastian Schroll
@file: pluginBase.py
@date: 08.01.2018
@author: Bastian Schroll
@description: Plugin main class to inherit
"""
import logging
import time
from abc import ABC
from boswatch import wildcard
logging.debug("- %s loaded", __name__)
class PluginBase(ABC):
r"""!Main plugin class"""
_pluginsActive = []
def __init__(self, pluginName, config):
r"""!init preload some needed locals and then call onLoad() directly"""
self._pluginName = pluginName
self.config = config
self._pluginsActive.append(self)
# 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
# for statistics
self._runCount = 0
self._setupErrorCount = 0
self._alarmErrorCount = 0
self._teardownErrorCount = 0
logging.debug("[%s] onLoad()", pluginName)
self.onLoad()
def _cleanup(self):
r"""!Cleanup routine calls onUnload() directly"""
logging.debug("[%s] onUnload()", self._pluginName)
self._pluginsActive.remove(self)
self.onUnload()
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"""
# --- FIX: Multicast list support ---
if isinstance(bwPacket, list):
# if we got a list of packets, we have to run each packet through the complete alarm process (Setup -> Alarm -> Teardown)
for single_packet in bwPacket:
self._run(single_packet)
return None
# ---------------------------------------------------------------------
self._runCount += 1
logging.debug("[%s] run #%d", self._pluginName, self._runCount)
self._bwPacket = bwPacket
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() - tmpTime
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() - tmpTime
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() - tmpTime
self._sumTime = self._setupTime + self._alarmTime + self._teardownTime
self._cumTime += self._sumTime
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):
r"""!Returns statistical information's from last plugin run
@return Statistics as pyton dict"""
stats = {"type": "plugin",
"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):
r"""!Called by import of the plugin
can be inherited"""
pass
def setup(self):
r"""!Called before alarm
can be inherited"""
pass
def fms(self, bwPacket):
r"""!Called on FMS alarm
can be inherited
@param bwPacket: bwPacket instance"""
logging.warning("ZVEI not implemented in %s", self._pluginName)
def pocsag(self, bwPacket):
r"""!Called on POCSAG alarm
can be inherited
@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
@param bwPacket: bwPacket instance"""
logging.warning("ZVEI not implemented in %s", self._pluginName)
def msg(self, bwPacket):
r"""!Called on MSG packet
can be inherited
@param bwPacket: bwPacket instance"""
logging.warning("MSG not implemented in %s", self._pluginName)
def teardown(self):
r"""!Called after alarm
can be inherited"""
pass
def onUnload(self):
r"""!Called on shutdown of boswatch
can be inherited"""
pass
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
return wildcard.replaceWildcards(msg, self._bwPacket)