2015-05-20 19:31:50 +02:00
|
|
|
#!/usr/bin/python
|
2015-07-13 10:19:45 +02:00
|
|
|
# -*- coding: UTF-8 -*-
|
2015-05-20 19:31:50 +02:00
|
|
|
|
2015-05-28 08:43:08 +02:00
|
|
|
"""
|
|
|
|
|
httpRequest-Plugin to dispatch FMS-, ZVEI- and POCSAG - messages to an URL
|
|
|
|
|
|
|
|
|
|
@author: Bastian Schroll
|
|
|
|
|
|
|
|
|
|
@requires: httpRequest-Configuration has to be set in the config.ini
|
|
|
|
|
"""
|
2015-05-20 19:31:50 +02:00
|
|
|
|
2015-07-01 09:31:23 +02:00
|
|
|
import time
|
2015-05-22 16:44:23 +02:00
|
|
|
import logging # Global logger
|
2015-05-22 10:44:39 +02:00
|
|
|
import httplib #for the HTTP request
|
2015-05-28 08:43:08 +02:00
|
|
|
from urlparse import urlparse #for split the URL into url and path
|
2015-05-22 10:44:39 +02:00
|
|
|
|
2015-05-22 16:44:23 +02:00
|
|
|
from includes import globals # Global variables
|
2015-07-09 07:52:21 +02:00
|
|
|
|
2015-07-03 23:52:31 +02:00
|
|
|
from includes.helper import timeHandler
|
2015-07-07 13:17:18 +02:00
|
|
|
from includes.helper import wildcardHandler
|
2015-07-09 07:52:21 +02:00
|
|
|
from includes.helper import configHandler
|
2015-05-22 16:44:23 +02:00
|
|
|
|
2015-06-29 12:19:44 +02:00
|
|
|
##
|
|
|
|
|
#
|
2015-06-29 23:24:48 +02:00
|
|
|
# onLoad (init) function of plugin
|
|
|
|
|
# will be called one time by the pluginLoader on start
|
2015-06-29 12:19:44 +02:00
|
|
|
#
|
|
|
|
|
def onLoad():
|
|
|
|
|
"""
|
|
|
|
|
While loading the plugins by pluginLoader.loadPlugins()
|
2015-06-29 23:24:48 +02:00
|
|
|
this onLoad() routine is called one time for initialize the plugin
|
2015-06-29 12:19:44 +02:00
|
|
|
|
|
|
|
|
@requires: nothing
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2015-06-29 12:19:44 +02:00
|
|
|
@return: nothing
|
|
|
|
|
"""
|
2015-06-29 23:24:48 +02:00
|
|
|
# nothing to do for this plugin
|
|
|
|
|
return
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2015-05-22 16:44:23 +02:00
|
|
|
|
2015-05-31 12:40:43 +02:00
|
|
|
##
|
|
|
|
|
#
|
|
|
|
|
# Main function of HTTP-plugin
|
|
|
|
|
# will be called by the alarmHandler
|
|
|
|
|
#
|
2015-05-20 19:31:50 +02:00
|
|
|
def run(typ,freq,data):
|
2015-05-28 08:43:08 +02:00
|
|
|
"""
|
|
|
|
|
This function is the implementation of the httpRequest-Plugin.
|
|
|
|
|
It will send the data to an URL via http Request
|
|
|
|
|
|
|
|
|
|
@type typ: string (FMS|ZVEI|POC)
|
|
|
|
|
@param typ: Typ of the dataset
|
|
|
|
|
@type data: map of data (structure see interface.txt)
|
|
|
|
|
@param data: Contains the parameter
|
|
|
|
|
@type freq: string
|
|
|
|
|
@keyword freq: frequency of the SDR Stick
|
|
|
|
|
|
|
|
|
|
@requires: httpRequest-Configuration has to be set in the config.ini
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2015-05-28 08:43:08 +02:00
|
|
|
@return: nothing
|
|
|
|
|
"""
|
2015-05-20 19:31:50 +02:00
|
|
|
try:
|
2015-07-09 07:52:21 +02:00
|
|
|
if configHandler.checkConfig("httpRequest"): #read and debug the config
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2015-05-31 12:40:43 +02:00
|
|
|
try:
|
|
|
|
|
#
|
2015-06-29 12:25:21 +02:00
|
|
|
# Create URL
|
|
|
|
|
#
|
|
|
|
|
if typ == "FMS":
|
|
|
|
|
url = globals.config.get("httpRequest", "fms_url") #Get URL
|
2015-07-07 13:17:18 +02:00
|
|
|
url = wildcardHandler.replaceWildcards(url, data) # replace wildcards with helper function
|
2015-06-29 12:25:21 +02:00
|
|
|
elif typ == "ZVEI":
|
|
|
|
|
url = globals.config.get("httpRequest", "zvei_url") #Get URL
|
2015-07-07 13:17:18 +02:00
|
|
|
url = wildcardHandler.replaceWildcards(url, data) # replace wildcards with helper function
|
2015-06-29 12:25:21 +02:00
|
|
|
elif typ == "POC":
|
|
|
|
|
url = globals.config.get("httpRequest", "poc_url") #Get URL
|
2015-07-07 13:17:18 +02:00
|
|
|
url = wildcardHandler.replaceWildcards(url, data) # replace wildcards with helper function
|
|
|
|
|
|
2015-05-22 10:44:39 +02:00
|
|
|
else:
|
2015-07-02 09:02:49 +02:00
|
|
|
logging.warning("Invalid Typ: %s", typ)
|
2015-07-01 20:33:43 +02:00
|
|
|
return
|
2015-07-02 09:02:49 +02:00
|
|
|
|
|
|
|
|
|
2015-06-29 12:25:21 +02:00
|
|
|
#
|
|
|
|
|
# HTTP-Request
|
|
|
|
|
#
|
2015-07-07 13:17:18 +02:00
|
|
|
logging.debug("send %s HTTP request", typ)
|
2015-06-29 12:25:21 +02:00
|
|
|
url = urlparse(url) #split URL into path and querry
|
|
|
|
|
httprequest = httplib.HTTPConnection(url[2]) #connect to URL Path
|
|
|
|
|
httprequest.request("GET", url[5]) #send URL Querry per GET
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2015-06-23 22:27:48 +02:00
|
|
|
except:
|
2015-06-29 12:25:21 +02:00
|
|
|
logging.error("cannot send HTTP request")
|
|
|
|
|
logging.debug("cannot send HTTP request", exc_info=True)
|
|
|
|
|
return
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2015-06-29 12:25:21 +02:00
|
|
|
else:
|
|
|
|
|
try:
|
2015-07-02 09:02:49 +02:00
|
|
|
#
|
2015-06-29 12:25:21 +02:00
|
|
|
# check HTTP-Response
|
|
|
|
|
#
|
|
|
|
|
httpresponse = httprequest.getresponse()
|
|
|
|
|
if str(httpresponse.status) == "200": #Check HTTP Response an print a Log or Error
|
|
|
|
|
logging.debug("HTTP response: %s - %s" , str(httpresponse.status), str(httpresponse.reason))
|
|
|
|
|
else:
|
|
|
|
|
logging.warning("HTTP response: %s - %s" , str(httpresponse.status), str(httpresponse.reason))
|
|
|
|
|
except: #otherwise
|
|
|
|
|
logging.error("cannot get HTTP response")
|
|
|
|
|
logging.debug("cannot get HTTP response", exc_info=True)
|
|
|
|
|
return
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2015-06-29 12:25:21 +02:00
|
|
|
finally:
|
|
|
|
|
logging.debug("close HTTP-Connection")
|
2015-07-02 09:02:49 +02:00
|
|
|
try:
|
2015-06-29 12:25:21 +02:00
|
|
|
httprequest.close()
|
|
|
|
|
except:
|
|
|
|
|
pass
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2015-05-20 19:31:50 +02:00
|
|
|
except:
|
2015-06-23 22:27:48 +02:00
|
|
|
logging.error("unknown error")
|
2015-07-02 09:02:49 +02:00
|
|
|
logging.debug("unknown error", exc_info=True)
|