mirror of
https://github.com/BOSWatch/BW3-Core.git
synced 2026-04-05 14:25:45 +00:00
some reworks
- rework configYaml - rework router mechanism test - move plugin and module files
This commit is contained in:
parent
71d87b080f
commit
a42676010e
19 changed files with 317 additions and 121 deletions
2
module/__init__.py
Normal file
2
module/__init__.py
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
111
module/descriptor.py
Normal file
111
module/descriptor.py
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""!
|
||||
____ ____ ______ __ __ __ _____
|
||||
/ __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ /
|
||||
/ __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ <
|
||||
/ /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ /
|
||||
/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/
|
||||
German BOS Information Script
|
||||
by Bastian Schroll
|
||||
|
||||
@file: descriptor.py
|
||||
@date: 07.01.2018
|
||||
@author: Bastian Schroll
|
||||
@description: Descriptor to load Descriptions from csv files
|
||||
"""
|
||||
import logging
|
||||
import csv
|
||||
import re
|
||||
|
||||
from boswatch.utils import paths
|
||||
|
||||
logging.debug("- %s loaded", __name__)
|
||||
|
||||
|
||||
class Descriptor:
|
||||
"""!CSV Descriptor class to load specific descriptions from CSV files, manage and serve them"""
|
||||
|
||||
def __init__(self):
|
||||
"""!Initialise a private list for the DescriptionList objects"""
|
||||
self._lists = {}
|
||||
|
||||
def loadDescription(self, csvType):
|
||||
"""!Build a new description list from DescriptionList class
|
||||
|
||||
@param csvType: Name of the CSV file without `.csv`
|
||||
@return True or False"""
|
||||
bwDescriptionList = DescriptionList()
|
||||
if bwDescriptionList.loadCSV(csvType):
|
||||
self._lists[csvType] = bwDescriptionList
|
||||
return True
|
||||
return False
|
||||
|
||||
def addDescriptions(self, bwPacket):
|
||||
"""!Add the short and long description to a bwPacket
|
||||
|
||||
@param bwPacket: bwPacket instance to add descriptions
|
||||
@return True or False"""
|
||||
logging.debug("add descriptions to bwPacket")
|
||||
try:
|
||||
bwPacket.set("shortDescription",
|
||||
self._lists[bwPacket.get("mode")].getShortDescription(bwPacket.get(bwPacket.get("mode"))))
|
||||
|
||||
bwPacket.set("longDescription",
|
||||
self._lists[bwPacket.get("mode")].getLongDescription(bwPacket.get(bwPacket.get("mode"))))
|
||||
return True
|
||||
except: # pragma: no cover
|
||||
logging.exception("error while adding descriptions")
|
||||
return False
|
||||
|
||||
|
||||
class DescriptionList:
|
||||
def __init__(self):
|
||||
"""!Loads the given CSV file into internal list"""
|
||||
logging.debug("create new descriptionList")
|
||||
self._descriptionList = {}
|
||||
|
||||
def getShortDescription(self, checkId):
|
||||
"""!Returns the short description of given id
|
||||
|
||||
@return short description or empty string"""
|
||||
try:
|
||||
return self._descriptionList[str(checkId)]["shortDescription"]
|
||||
except:
|
||||
return ""
|
||||
|
||||
def getLongDescription(self, checkId):
|
||||
"""!Returns the long description of given id
|
||||
|
||||
@return long description or empty string"""
|
||||
try:
|
||||
return self._descriptionList[str(checkId)]["longDescription"]
|
||||
except:
|
||||
return ""
|
||||
|
||||
def loadCSV(self, csvType):
|
||||
"""!Load descriptions from an csv file
|
||||
|
||||
@param csvType: Name of the CSV file without `.csv`
|
||||
@return True or False"""
|
||||
count = 0
|
||||
logging.debug("loading csv file: %s", csvType)
|
||||
csvPath = paths.CSV_PATH + csvType + ".csv"
|
||||
try:
|
||||
csvFile = open(csvPath, 'r', -1, 'utf-8')
|
||||
reader = csv.DictReader(csvFile)
|
||||
|
||||
for line in reader:
|
||||
if re.match("^[0-9]+[A-D]?$", line["id"], re.IGNORECASE):
|
||||
self._descriptionList[line["id"]] = {"shortDescription": line["shortDescription"], "longDescription": line["longDescription"]}
|
||||
logging.debug("- %s", line)
|
||||
count += 1
|
||||
|
||||
logging.debug("%s entry's loaded", count)
|
||||
return True
|
||||
except FileNotFoundError:
|
||||
logging.error("csv file not found: %s", csvPath)
|
||||
return False
|
||||
except: # pragma: no cover
|
||||
logging.exception("error while loading descriptions")
|
||||
return False
|
||||
2
module/filter/__init__.py
Normal file
2
module/filter/__init__.py
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
75
module/filter/doubleFilter.py
Normal file
75
module/filter/doubleFilter.py
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""!
|
||||
____ ____ ______ __ __ __ _____
|
||||
/ __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ /
|
||||
/ __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ <
|
||||
/ /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ /
|
||||
/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/
|
||||
German BOS Information Script
|
||||
by Bastian Schroll
|
||||
|
||||
@file: doubleFilter.py
|
||||
@date: 15.01.2018
|
||||
@author: Bastian Schroll
|
||||
@description: Class to implement a filter for double alarms
|
||||
@todo test, refactor and document / check_msg is not implemented yet
|
||||
"""
|
||||
import logging
|
||||
import time
|
||||
|
||||
logging.debug("- %s loaded", __name__)
|
||||
|
||||
|
||||
class DoubleFilter:
|
||||
"""!Double Filter Class"""
|
||||
|
||||
def __init__(self, config):
|
||||
"""!init"""
|
||||
self._config = config
|
||||
self._filterLists = {}
|
||||
|
||||
def filter(self, bwPacket):
|
||||
|
||||
if bwPacket.get("mode") == "fms":
|
||||
scanWord = "fms"
|
||||
elif bwPacket.get("mode") == "pocsag":
|
||||
scanWord = "ric"
|
||||
elif bwPacket.get("mode") == "zvei":
|
||||
scanWord = "zvei"
|
||||
else:
|
||||
logging.error("No Filter for '%s'", bwPacket)
|
||||
return False
|
||||
|
||||
if not bwPacket.get("mode") in self._filterLists:
|
||||
logging.debug("create new doubleFilter list for '%s'", bwPacket.get("mode"))
|
||||
self._filterLists[bwPacket.get("mode")] = []
|
||||
|
||||
logging.debug("scanWord for '%s' is '%s'", bwPacket.get("mode"), scanWord)
|
||||
|
||||
return self._check(bwPacket, scanWord)
|
||||
|
||||
def _check(self, bwPacket, scanWord):
|
||||
self._filterLists[bwPacket.get("mode")].insert(0, bwPacket)
|
||||
|
||||
# delete entries that are to old
|
||||
counter = 0
|
||||
for listPacket in self._filterLists[bwPacket.get("mode")][1:]: # [1:] skip first entry, thats the new one
|
||||
if listPacket.get("timestamp") < (time.time() - self._config["ignoreTime"]):
|
||||
self._filterLists[bwPacket.get("mode")].remove(listPacket)
|
||||
counter += 1
|
||||
if counter:
|
||||
logging.debug("%d old entry(s) removed", counter)
|
||||
|
||||
# delete last entry if list is to big
|
||||
if len(self._filterLists[bwPacket.get("mode")]) > self._config["maxEntry"]:
|
||||
logging.debug("MaxEntry reached - delete oldest")
|
||||
self._filterLists[bwPacket.get("mode")].pop()
|
||||
|
||||
for listPacket in self._filterLists[bwPacket.get("mode")][1:]: # [1:] skip first entry, thats the new one
|
||||
if listPacket.get(scanWord) is bwPacket.get(scanWord):
|
||||
logging.debug("found duplicate: %s", bwPacket.get(scanWord))
|
||||
return False
|
||||
|
||||
logging.debug("doubleFilter ok")
|
||||
return True
|
||||
103
module/module.py
Normal file
103
module/module.py
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""!
|
||||
____ ____ ______ __ __ __ _____
|
||||
/ __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ /
|
||||
/ __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ <
|
||||
/ /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ /
|
||||
/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/
|
||||
German BOS Information Script
|
||||
by Bastian Schroll
|
||||
|
||||
@file: module.py
|
||||
@date: 01.03.2019
|
||||
@author: Bastian Schroll
|
||||
@description: Module main class to inherit
|
||||
"""
|
||||
import logging
|
||||
import time
|
||||
|
||||
logging.debug("- %s loaded", __name__)
|
||||
|
||||
|
||||
class Module:
|
||||
"""!Main module class"""
|
||||
|
||||
_modulesActive = 0
|
||||
|
||||
def __init__(self, moduleName, config):
|
||||
"""!init preload some needed locals and then call onLoad() directly"""
|
||||
self._moduleName = moduleName
|
||||
self.config = config
|
||||
self._modulesActive += 1
|
||||
|
||||
# for time counting
|
||||
self._cumTime = 0
|
||||
self._moduleTime = 0
|
||||
self._tmpTime = 0
|
||||
self._tmpTime = 0
|
||||
|
||||
# for statistics
|
||||
self._runCount = 0
|
||||
self._moduleErrorCount = 0
|
||||
|
||||
logging.debug("[%s] onLoad()", moduleName)
|
||||
self.onLoad()
|
||||
|
||||
def __del__(self):
|
||||
"""!Destructor calls onUnload() directly"""
|
||||
logging.debug("[%s] onUnload()", self._moduleName)
|
||||
self._modulesActive -= 1
|
||||
self.onUnload()
|
||||
|
||||
def _run(self, bwPacket):
|
||||
"""!start an rund of the module.
|
||||
|
||||
@param bwPacket: A BOSWatch packet instance
|
||||
@return bwPacket or False"""
|
||||
self._runCount += 1
|
||||
logging.debug("[%s] run #%d", self._moduleName, self._runCount)
|
||||
|
||||
self._tmpTime = time.time()
|
||||
try:
|
||||
logging.debug("[%s] doWork()", self._moduleName)
|
||||
bwPacket = self.doWork(bwPacket)
|
||||
except:
|
||||
self._moduleErrorCount += 1
|
||||
logging.exception("[%s] alarm error", self._moduleName)
|
||||
self._moduleTime = time.time() - self._tmpTime
|
||||
|
||||
self._cumTime += self._moduleTime
|
||||
self._endTime = time.time()
|
||||
|
||||
logging.debug("[%s] took %0.3f seconds", self._moduleName, self._moduleTime)
|
||||
|
||||
return bwPacket
|
||||
|
||||
def _getStatistics(self):
|
||||
"""!Returns statistical information's from last module run
|
||||
|
||||
@return Statistics as pyton dict"""
|
||||
stats = {"runCount": self._runCount,
|
||||
"cumTime": self._cumTime,
|
||||
"moduleTime": self._moduleTime,
|
||||
"moduleErrorCount": self._moduleErrorCount}
|
||||
return stats
|
||||
|
||||
def onLoad(self):
|
||||
"""!Called by import of the module
|
||||
Must be inherit"""
|
||||
pass
|
||||
|
||||
def doWork(self, bwPacket):
|
||||
"""!Called module run
|
||||
Must be inherit
|
||||
|
||||
@param bwPacket: bwPacket instance"""
|
||||
logging.warning("no functionality in module %s", self._moduleName)
|
||||
|
||||
def onUnload(self):
|
||||
"""!Called by destruction of the module
|
||||
Must be inherit"""
|
||||
pass
|
||||
|
||||
47
module/template_module.py
Normal file
47
module/template_module.py
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""!
|
||||
____ ____ ______ __ __ __ _____
|
||||
/ __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ /
|
||||
/ __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ <
|
||||
/ /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ /
|
||||
/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/
|
||||
German BOS Information Script
|
||||
by Bastian Schroll
|
||||
|
||||
@file: template_module.py
|
||||
@date: 01.03.2019
|
||||
@author: Bastian Schroll
|
||||
@description: Template Module File
|
||||
"""
|
||||
import logging
|
||||
from module.module import Module
|
||||
|
||||
# ###################### #
|
||||
# Custom plugin includes #
|
||||
|
||||
# ###################### #
|
||||
|
||||
logging.debug("- %s loaded", __name__)
|
||||
|
||||
|
||||
class BoswatchModule(Module):
|
||||
"""!Description of the Module"""
|
||||
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 doWork(self, bwPacket):
|
||||
"""!start an rund of the module.
|
||||
|
||||
@param bwPacket: A BOSWatch packet instance
|
||||
@return bwPacket or False"""
|
||||
return bwPacket
|
||||
|
||||
def onUnload(self):
|
||||
"""!Called by destruction of the plugin"""
|
||||
pass
|
||||
Loading…
Add table
Add a link
Reference in a new issue