From daca83e0d3c406fad2ca6e98ac4f6268d1b8c72d Mon Sep 17 00:00:00 2001 From: JHCD Date: Mon, 18 May 2015 21:04:10 +0200 Subject: [PATCH] Implement BosMon-plugin - insert/change config-file - pluginloader use only enabled modules (config.ini) - changes in interface-description --- plugin_test/config/config.ini | 8 +-- plugin_test/plugin_test.py | 17 +++++- plugin_test/pluginloader.py | 16 +++++- plugin_test/plugins/BosMon/__init__.py | 66 +++++++++++++++++++++--- plugin_test/plugins/interface.txt | 11 +++- plugin_test/plugins/template/__init__.py | 6 ++- 6 files changed, 105 insertions(+), 19 deletions(-) mode change 100644 => 100755 plugin_test/plugin_test.py diff --git a/plugin_test/config/config.ini b/plugin_test/config/config.ini index 1bf64af..ba86077 100644 --- a/plugin_test/config/config.ini +++ b/plugin_test/config/config.ini @@ -16,7 +16,7 @@ template = 1 bosmon_server = 192.168.0.1 bosmon_port = 80 #channel-name of typ "Web-Telegramm" -bosmon_channel = channelname -#Use this, when you have security enabled -bosmon_user = user -bosmon_password = password +bosmon_channel = pocsag +#Use this, when BosMon has restricted access +bosmon_user = +bosmon_password = diff --git a/plugin_test/plugin_test.py b/plugin_test/plugin_test.py old mode 100644 new mode 100755 index b85c5c2..86920df --- a/plugin_test/plugin_test.py +++ b/plugin_test/plugin_test.py @@ -1,9 +1,13 @@ #!/usr/bin/python # -*- coding: cp1252 -*- +import globals # Global variables import time import pluginloader +import os #for absolute path: os.path.dirname(os.path.abspath(__file__)) +import ConfigParser #for parse the config file + #create new logger import logging logger = logging.getLogger() @@ -34,8 +38,17 @@ logger.addHandler(ch) #exception - error handler in try:exc: into the message #critical - critical error, program exit +#ConfigParser +logging.info("reading config file") +try: + script_path = os.path.dirname(os.path.abspath(__file__)) + globals.config = ConfigParser.ConfigParser() + globals.config.read(script_path+"/config/config.ini") +except: + logging.error("cannot read config file","error") -data = {"zvei":"12345"} +#data = {"zvei":"12345"} +data = {"ric":"1234567", "function":"1", "msg":"Hello World!"} while True: time.sleep(1) @@ -43,4 +56,4 @@ while True: for i in pluginloader.getPlugins(): logging.debug("Load Plugin: " + i["name"]) plugin = pluginloader.loadPlugin(i) - plugin.run("zvei","80000000",data) \ No newline at end of file + plugin.run("POC","80000000",data) \ No newline at end of file diff --git a/plugin_test/pluginloader.py b/plugin_test/pluginloader.py index 346d5d6..60e6e56 100644 --- a/plugin_test/pluginloader.py +++ b/plugin_test/pluginloader.py @@ -1,6 +1,8 @@ #!/usr/bin/python # -*- coding: cp1252 -*- +import logging # Global logger +import globals # Global variables import imp import os @@ -12,10 +14,20 @@ def getPlugins(): possibleplugins = os.listdir(PluginFolder) for i in possibleplugins: location = os.path.join(PluginFolder, i) + # plugins have to be a subdir with MainModule, if not skip if not os.path.isdir(location) or not MainModule + ".py" in os.listdir(location): continue - info = imp.find_module(MainModule, [location]) - plugins.append({"name": i, "info": info}) + logging.debug("found plugin: "+i) + # is the plugin enabled in the config-file? + try: + usePlugin = int(globals.config.get("Module", i)) + except: #no entry for plugin found in config-file, skip + continue + logging.debug("use Plugin: "+str(usePlugin)) + if usePlugin: + info = imp.find_module(MainModule, [location]) + plugins.append({"name": i, "info": info}) + logging.debug("append Plugin: "+i) return plugins def loadPlugin(plugin): diff --git a/plugin_test/plugins/BosMon/__init__.py b/plugin_test/plugins/BosMon/__init__.py index 2a42b03..00845c3 100644 --- a/plugin_test/plugins/BosMon/__init__.py +++ b/plugin_test/plugins/BosMon/__init__.py @@ -1,10 +1,60 @@ -import logging +#!/usr/bin/python +# -*- coding: cp1252 -*- + +import logging # Global logger +import globals # Global variables + +import httplib #for the HTTP request +import urllib #for the HTTP request with parameters +import base64 #for the HTTP request with User/Password def run(typ,frequenz,daten): - logging.debug("Throw Template Plugin") - try: - logging.info("ZVEI: %s wurde empfangen!", daten[0]) - logging.debug("try 5/0") - test = 5/0 - except: - logging.exception("Error in Template Plugin") \ No newline at end of file + logging.debug("BosMon Plugin called") + logging.debug(" - typ: " +typ) + try: + #get BosMon-Config + bosmon_server = globals.config.get("BosMon", "bosmon_server") + bosmon_port = globals.config.get("BosMon", "bosmon_port") + bosmon_user = globals.config.get("BosMon", "bosmon_user") + bosmon_password = globals.config.get("BosMon", "bosmon_password") + bosmon_channel = globals.config.get("BosMon", "bosmon_channel") + logging.debug(" - Server: " +bosmon_server) + logging.debug(" - Port: " +bosmon_port) + logging.debug(" - User: " +bosmon_user) + logging.debug(" - Channel: " +bosmon_channel) + + if typ == "FMS": + logging.warning("FMS not implemented in BosMon plugin") + + elif typ == "ZVEI": + logging.warning("ZVEI not implemented in BosMon plugin") + + elif typ == "POC": + logging.debug("Start POC to BosMon") + try: + #Defined data structure: + # daten["ric"] + # daten["function"] + # daten["msg"] + #BosMon-Telegramin expected "a-d" as RIC-sub/function + daten["function"] = daten["function"].replace("1", "a").replace("2", "b").replace("3", "c").replace("4", "d") + params = urllib.urlencode({'type':'pocsag', 'address':daten["ric"], 'flags':'0', 'function':daten["function"], 'message':daten["msg"]}) + logging.debug(" - Params:" +params) + headers = {} + headers['Content-type'] = "application/x-www-form-urlencoded" + headers['Accept'] = "text/plain" + if bosmon_user: + headers['Authorization'] = "Basic {0}".format(base64.b64encode("{0}:{1}".format(bosmon_user, bosmon_password))) + httprequest = httplib.HTTPConnection(bosmon_server, bosmon_port) + httprequest.request("POST", "/telegramin/"+bosmon_channel+"/input.xml", params, headers) + httpresponse = httprequest.getresponse() + if str(httpresponse.status) == "200": #Check HTTP Response an print a Log or Error + logging.debug("BosMon response: "+str(httpresponse.status)+" - "+str(httpresponse.reason)) + else: + logging.warning("BosMon response: "+str(httpresponse.status)+" - "+str(httpresponse.reason)) + except: + logging.warning("POC to BosMon failed") + else: + logging.warning("typ '"+typ+"' undefined in BosMon plugin") + except: + logging.exception("Error in BosMon Plugin") \ No newline at end of file diff --git a/plugin_test/plugins/interface.txt b/plugin_test/plugins/interface.txt index 6d487c8..e03e8eb 100644 --- a/plugin_test/plugins/interface.txt +++ b/plugin_test/plugins/interface.txt @@ -15,5 +15,12 @@ FMS: POCSAG: - ric -- sub_ric -- text +- function +- msg + +Es stehen folgende globale Objecte zur Verfügung: +1.) import logging # Global logger +logging - Object + +2.) import globals # Global variables +config - Object (typ: ConfigParser, stellt config.ini bereit) \ No newline at end of file diff --git a/plugin_test/plugins/template/__init__.py b/plugin_test/plugins/template/__init__.py index bd86c59..0e10a17 100644 --- a/plugin_test/plugins/template/__init__.py +++ b/plugin_test/plugins/template/__init__.py @@ -1,4 +1,8 @@ -import logging +#!/usr/bin/python +# -*- coding: cp1252 -*- + +import logging # Global logger +import globals # Global variables def run(typ,freq,data): logging.debug("Strat Plugin: template")