From f2fcbf4ca51c3de6f808de1152162d0ec15e8f80 Mon Sep 17 00:00:00 2001 From: JHCD Date: Mon, 22 Jun 2015 18:00:39 +0200 Subject: [PATCH] add TCP-support to jsonSocket-Plugin rename plugin from jsonUDP to jsonSocket --- config/config.template.ini | 6 +- plugins/jsonSocket/jsonSocket.py | 93 +++++++++++++++++++++++++++ plugins/jsonUDP/jsonUDP.py | 107 ------------------------------- 3 files changed, 97 insertions(+), 109 deletions(-) create mode 100644 plugins/jsonSocket/jsonSocket.py delete mode 100644 plugins/jsonUDP/jsonUDP.py diff --git a/config/config.template.ini b/config/config.template.ini index 58b6727..6162d51 100644 --- a/config/config.template.ini +++ b/config/config.template.ini @@ -88,7 +88,7 @@ httpRequest = 0 eMail = 0 BosMon = 0 firEmergency = 0 -jsonUDP = 0 +jsonSocket = 0 # for developing template-module template = 0 @@ -195,7 +195,9 @@ bosmon_password = firserver = localhost firport = 9001 -[jsonUDP] +[jsonSocket] +# Protocol for socket (TCP|UDP) +protocol = UDP # Server as IP of DNS-Name (without http://) server = 192.168.0.1 port = 8888 diff --git a/plugins/jsonSocket/jsonSocket.py b/plugins/jsonSocket/jsonSocket.py new file mode 100644 index 0000000..f1a95b7 --- /dev/null +++ b/plugins/jsonSocket/jsonSocket.py @@ -0,0 +1,93 @@ +#!/usr/bin/python +# -*- coding: cp1252 -*- + +""" +jsonSocket-Plugin to dispatch FMS-, ZVEI- and POCSAG-messages via UDP/TCP + +@author: Jens Herrmann + +@requires: jsonSocket-Configuration has to be set in the config.ini +""" + +import logging # Global logger + +import socket # for connection +import json # for data-transfer + +from includes import globals # Global variables + +## +# +# Main function of jsonSocket-plugin +# will be called by the alarmHandler +# +def run(typ,freq,data): + """ + This function is the implementation of the jsonSocket-Plugin. + It will send the data via UDP/TCP + + The configuration for the Connection is set in the config.ini. + + @type typ: string (FMS|ZVEI|POC) + @param typ: Typ of the dataset for sending via UDP/TCP + @type data: map of data (structure see interface.txt) + @param data: Contains the parameter for dispatch to UDP. + @type freq: string + @keyword freq: frequency of the SDR Stick + + @requires: jsonSocket-Configuration has to be set in the config.ini + + @return: nothing + @exception: Exception if ConfigParser failed + @exception: Exception if connect to socket-Server failed + @exception: Exception if sending the socket-message failed + """ + try: + # + # ConfigParser + # + logging.debug("reading config file") + try: + for key,val in globals.config.items("jsonSocket"): + logging.debug(" - %s = %s", key, val) + + except: + logging.exception("cannot read config file") + + try: + # + # initialize to socket-Server + # + # SOCK_DGRAM is the socket type to use for UDP sockets + # SOCK_STREAM is the socket type to use for TCP sockets + if globals.config.get("jsonSocket", "protocol") == "TCP": + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.connect((globals.config.get("jsonSocket", "server"), globals.config.getint("jsonSocket", "port"))) + else: + sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + + except: + logging.exception("cannot initialize %s-socket", globals.config.get("jsonSocket", "protocol")) + + else: + + supportedTypes = ["FMS", "ZVEI", "POC"] + if typ in supportedTypes: + logging.debug("Start %s to %s", typ, globals.config.get("jsonSocket", "protocol")) + try: + # convert data to json + sendData = json.dumps(data) + # send data + sock.sendto(sendData, (globals.config.get("jsonSocket", "server"), globals.config.getint("jsonSocket", "port"))) + except: + logging.exception("%s to %s failed", typ, globals.config.get("jsonSocket", "protocol")) + + else: + logging.warning("Invalid Typ: %s", typ) + + finally: + logging.debug("close %s-Connection", globals.config.get("jsonSocket", "protocol")) + sock.close() + + except: + logging.exception("") \ No newline at end of file diff --git a/plugins/jsonUDP/jsonUDP.py b/plugins/jsonUDP/jsonUDP.py deleted file mode 100644 index ed21f8f..0000000 --- a/plugins/jsonUDP/jsonUDP.py +++ /dev/null @@ -1,107 +0,0 @@ -#!/usr/bin/python -# -*- coding: cp1252 -*- - -""" -json-Plugin to dispatch FMS-, ZVEI- and POCSAG - messages via UDP - -@author: Jens Herrmann - -@requires: jsonUDP-Configuration has to be set in the config.ini -""" - -import logging # Global logger - -import socket # for udp-connection -import json # for data - -from includes import globals # Global variables - -## -# -# Main function of jsonUDP-plugin -# will be called by the alarmHandler -# -def run(typ,freq,data): - """ - This function is the implementation of the jsonUDP-Plugin. - It will send the data via UDP - - The configuration for the UDP-Connection is set in the config.ini. - - @type typ: string (FMS|ZVEI|POC) - @param typ: Typ of the dataset for sending via UDP - @type data: map of data (structure see interface.txt) - @param data: Contains the parameter for dispatch to UDP. - @type freq: string - @keyword freq: frequency of the SDR Stick - - @requires: jsonUDP-Configuration has to be set in the config.ini - - @return: nothing - @exception: Exception if ConfigParser failed - @exception: Exception if connect to UDP-Server failed - @exception: Exception if sending the UDP-message failed - """ - try: - # - # ConfigParser - # - logging.debug("reading config file") - try: - for key,val in globals.config.items("jsonUDP"): - logging.debug(" - %s = %s", key, val) - - except: - logging.exception("cannot read config file") - - try: - # - # initialize to UDP-Server - # - # SOCK_DGRAM is the socket type to use for UDP sockets - sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - - except: - logging.exception("cannot initialize UDP-socket") - - else: - - if typ == "FMS": - logging.debug("Start FMS to UDP") - try: - # convert data to json - sendData = json.dumps(data) - # send UDP - sock.sendto(sendData, (globals.config.get("jsonUDP", "server"), globals.config.getint("jsonUDP", "port"))) - except: - logging.exception("FMS to UDP failed") - - elif typ == "ZVEI": - logging.debug("Start ZVEI to UDP") - try: - # convert data to json - sendData = json.dumps(data) - # send UDP - sock.sendto(sendData, (globals.config.get("jsonUDP", "server"), globals.config.getint("jsonUDP", "port"))) - except: - logging.exception("ZVEI to UDP failed") - - elif typ == "POC": - logging.debug("Start POC to UDP") - try: - # convert data to json - sendData = json.dumps(data) - # send UDP - sock.sendto(sendData, (globals.config.get("jsonUDP", "server"), globals.config.getint("jsonUDP", "port"))) - except: - logging.exception("POC to UDP failed") - - else: - logging.warning("Invalid Typ: %s", typ) - - finally: - logging.debug("close UDP-Connection") - sock.close() - - except: - logging.exception("") \ No newline at end of file