From 2f5184742fa7e580601e8b7ad2e086e02e4c58ef Mon Sep 17 00:00:00 2001 From: Bastian Schroll Date: Mon, 28 Oct 2019 21:20:05 +0100 Subject: [PATCH] some refactorings --- boswatch/inputSource/inputSource.py | 90 +++++++++++++++++++++++++++++ module/descriptor.py | 4 +- module/filter/modeFilter.py | 4 +- module/filter/regexFilter.py | 4 +- module/{module.py => moduleBase.py} | 4 +- module/template_module.py | 4 +- plugin/{plugin.py => pluginBase.py} | 4 +- plugin/template_plugin.py | 4 +- 8 files changed, 104 insertions(+), 14 deletions(-) create mode 100644 boswatch/inputSource/inputSource.py rename module/{module.py => moduleBase.py} (98%) rename plugin/{plugin.py => pluginBase.py} (99%) diff --git a/boswatch/inputSource/inputSource.py b/boswatch/inputSource/inputSource.py new file mode 100644 index 0000000..a4b8766 --- /dev/null +++ b/boswatch/inputSource/inputSource.py @@ -0,0 +1,90 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +"""! + ____ ____ ______ __ __ __ _____ + / __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ / + / __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ < + / /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ / +/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/ + German BOS Information Script + by Bastian Schroll + +@file: sdrInput.py +@date: 28.10.2018 +@author: Bastian Schroll +@description: Input source for sdr with rtl_fm +""" +import time +import logging +import threading +from boswatch.utils import paths +from boswatch.processManager import ProcessManager + +logging.debug("- %s loaded", __name__) + + +class SdrInput: + """!Worker class to check internet connection""" + + def __init__(self): + self._isRunning = False + self._mmThread = None + + def start(self, packetQueue, inputConfig, decoderConfig): + self._isRunning = True + self._mmThread = threading.Thread(target=self._handleSDRInput, name="mmReader", + args=(packetQueue, inputConfig, decoderConfig)) + self._mmThread.daemon = True + self._mmThread.start() + + def shutdown(self): + self._isRunning = False + self._mmThread.join() + + def _handleSDRInput(self, dataQueue, sdrConfig, decoderConfig): # todo exception handling inside + sdrProc = ProcessManager(str(sdrConfig.get("rtlPath", default="rtl_fm"))) + sdrProc.addArgument("-d " + str(sdrConfig.get("device", default="0"))) # device id + sdrProc.addArgument("-f " + sdrConfig.get("frequency")) # frequencies + sdrProc.addArgument("-p " + str(sdrConfig.get("error", default="0"))) # frequency error in ppm + sdrProc.addArgument("-l " + str(sdrConfig.get("squelch", default="1"))) # squelch + sdrProc.addArgument("-g " + str(sdrConfig.get("gain", default="100"))) # gain + sdrProc.addArgument("-M fm") # set mode to fm + sdrProc.addArgument("-E DC") # set DC filter + sdrProc.addArgument("-s 22050") # bit rate of audio stream + sdrProc.setStderr(open(paths.LOG_PATH + "rtl_fm.log", "a")) + sdrProc.start() + + mmProc = ProcessManager(str(sdrConfig.get("mmPath", default="multimon-ng")), textMode=True) + if decoderConfig.get("fms", default=0): + mmProc.addArgument("-a FMSFSK") + if decoderConfig.get("zvei", default=0): + mmProc.addArgument("-a ZVEI1") + if decoderConfig.get("poc512", default=0): + mmProc.addArgument("-a POCSAG512") + if decoderConfig.get("poc1200", default=0): + mmProc.addArgument("-a POCSAG1200") + if decoderConfig.get("poc2400", default=0): + mmProc.addArgument("-a POCSAG2400") + mmProc.addArgument("-f alpha") + mmProc.addArgument("-t raw -") + mmProc.setStdin(sdrProc.stdout) + mmProc.setStderr(open(paths.LOG_PATH + "multimon-ng.log", "a")) + mmProc.start() + + logging.info("start decoding") + while self._isRunning: + if not sdrProc.isRunning: + logging.warning("rtl_fm was down - try to restart") + sdrProc.start() + elif not mmProc.isRunning: + logging.warning("multimon was down - try to restart") + mmProc.start() + elif sdrProc.isRunning and mmProc.isRunning: + line = mmProc.readline() + if line: + dataQueue.put_nowait((line, time.time())) + logging.debug("Add data to queue") + print(line) + logging.debug("stopping thread") + mmProc.stop() + sdrProc.stop() diff --git a/module/descriptor.py b/module/descriptor.py index 7ef926f..164f25e 100644 --- a/module/descriptor.py +++ b/module/descriptor.py @@ -15,7 +15,7 @@ @description: Module to add descriptions to bwPackets """ import logging -from module.module import Module +from module.moduleBase import ModuleBase # ###################### # # Custom plugin includes # @@ -25,7 +25,7 @@ from module.module import Module logging.debug("- %s loaded", __name__) -class BoswatchModule(Module): +class BoswatchModuleBase(ModuleBase): """!Adds descriptions to bwPackets""" def __init__(self, config): """!Do not change anything here!""" diff --git a/module/filter/modeFilter.py b/module/filter/modeFilter.py index 2e97d1b..7d8b193 100644 --- a/module/filter/modeFilter.py +++ b/module/filter/modeFilter.py @@ -15,7 +15,7 @@ @description: Filter module for the packet type """ import logging -from module.module import Module +from module.moduleBase import ModuleBase # ###################### # # Custom plugin includes # @@ -25,7 +25,7 @@ from module.module import Module logging.debug("- %s loaded", __name__) -class BoswatchModule(Module): +class BoswatchModuleBase(ModuleBase): """!Filter of specific bwPacket mode""" def __init__(self, config): """!Do not change anything here!""" diff --git a/module/filter/regexFilter.py b/module/filter/regexFilter.py index b0f74bf..622b08f 100644 --- a/module/filter/regexFilter.py +++ b/module/filter/regexFilter.py @@ -15,7 +15,7 @@ @description: Regex filter module """ import logging -from module.module import Module +from module.moduleBase import ModuleBase # ###################### # # Custom plugin includes # @@ -25,7 +25,7 @@ import re logging.debug("- %s loaded", __name__) -class BoswatchModule(Module): +class BoswatchModuleBase(ModuleBase): """!Regex based filter mechanism""" def __init__(self, config): """!Do not change anything here!""" diff --git a/module/module.py b/module/moduleBase.py similarity index 98% rename from module/module.py rename to module/moduleBase.py index 7598ce7..7682db6 100644 --- a/module/module.py +++ b/module/moduleBase.py @@ -9,7 +9,7 @@ German BOS Information Script by Bastian Schroll -@file: module.py +@file: moduleBase.py @date: 01.03.2019 @author: Bastian Schroll @description: Module main class to inherit @@ -22,7 +22,7 @@ from boswatch import wildcard logging.debug("- %s loaded", __name__) -class Module: +class ModuleBase: """!Main module class""" _modulesActive = [] diff --git a/module/template_module.py b/module/template_module.py index a0d7d15..1066954 100644 --- a/module/template_module.py +++ b/module/template_module.py @@ -15,7 +15,7 @@ @description: Template Module File """ import logging -from module.module import Module +from module.moduleBase import ModuleBase # ###################### # # Custom plugin includes # @@ -25,7 +25,7 @@ from module.module import Module logging.debug("- %s loaded", __name__) -class BoswatchModule(Module): +class BoswatchModuleBase(ModuleBase): """!Description of the Module""" def __init__(self, config): """!Do not change anything here!""" diff --git a/plugin/plugin.py b/plugin/pluginBase.py similarity index 99% rename from plugin/plugin.py rename to plugin/pluginBase.py index bf60771..12ad6ea 100644 --- a/plugin/plugin.py +++ b/plugin/pluginBase.py @@ -9,7 +9,7 @@ German BOS Information Script by Bastian Schroll -@file: plugin.py +@file: pluginBase.py @date: 08.01.2018 @author: Bastian Schroll @description: Plugin main class to inherit @@ -22,7 +22,7 @@ from boswatch import wildcard logging.debug("- %s loaded", __name__) -class Plugin: +class PluginBase: """!Main plugin class""" _pluginsActive = [] diff --git a/plugin/template_plugin.py b/plugin/template_plugin.py index 2c876b3..987d1df 100644 --- a/plugin/template_plugin.py +++ b/plugin/template_plugin.py @@ -15,7 +15,7 @@ @description: Template Plugin File """ import logging -from plugin.plugin import Plugin +from plugin.pluginBase import PluginBase # ###################### # # Custom plugin includes # @@ -25,7 +25,7 @@ from plugin.plugin import Plugin logging.debug("- %s loaded", __name__) -class BoswatchPlugin(Plugin): +class BoswatchPluginBase(PluginBase): """!Description of the Plugin""" def __init__(self, config): """!Do not change anything here!"""