forked from enesbcs/rpieasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_C008_GenHTTP.py
83 lines (76 loc) · 3.04 KB
/
_C008_GenHTTP.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3
#############################################################################
################## Generic HTTP Controller for RPIEasy ######################
#############################################################################
#
# Only one way data sending supported for obvious reasons.
#
# Copyright (C) 2019 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
import controller
import rpieGlobals
import misc
import urllib.request
import urllib.parse
import threading
import webserver
import Settings
import commands
class Controller(controller.ControllerProto):
CONTROLLER_ID = 8
CONTROLLER_NAME = "Generic HTTP"
def __init__(self,controllerindex):
controller.ControllerProto.__init__(self,controllerindex)
self.usesID = False
self.usesAccount = False
self.usesPassword = False
self.controllerport = 80
self.inchannel = "demo.php?name=%sysname%&task=%tskname%&taskid=%id%&valuename=%valname%&value=%value%"
self.templatestr = ""
def controller_init(self,enablecontroller=None):
if enablecontroller != None:
self.enabled = enablecontroller
self.templatestr, state = commands.parseruleline(self.inchannel) # replace global variables
self.templatestr = self.templatestr.replace("==","=")
self.initialized = True
return True
def webform_load(self):
webserver.addFormTextBox("Report template","inchannel",self.inchannel,255)
return True
def webform_save(self,params): # process settings post reply
self.inchannel = webserver.arg("inchannel",params)
return True
def senddata(self,idx,sensortype,value,userssi=-1,usebattery=-1,tasknum=-1,changedvalue=-1): # called by plugin
if tasknum is None:
return False
if tasknum!=-1 and self.enabled:
if tasknum<len(Settings.Tasks):
if Settings.Tasks[tasknum] != False:
procarr = []
try:
hn = str(urllib.parse.quote(str(Settings.Tasks[tasknum].gettaskname())))
templatestra = self.templatestr.replace('%tskname%',hn)
templatestra = templatestra.replace('%id%',str(tasknum+1))
for u in range(Settings.Tasks[tasknum].valuecount):
vn = str(Settings.Tasks[tasknum].valuenames[u]).strip()
if vn!="":
vn = str(urllib.parse.quote(vn))
templatestr = templatestra.replace('%valname%',vn)
val = str(urllib.parse.quote(str(Settings.Tasks[tasknum].uservar[u])))
templatestr = templatestr.replace('%value%',val)
urlstr = "http://"+str(self.controllerip)+":"+str(self.controllerport)+"/"+templatestr
misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,urlstr) # sendviahttp
t = threading.Thread(target=self.urlget, args=(urlstr,)) # use threading to avoid blocking
t.daemon = True
procarr.append(t)
t.start()
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,e)
if len(procarr)>0:
for process in procarr:
process.join()
def urlget(self,url):
try:
content = urllib.request.urlopen(url,None,2)
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"Controller: "+self.controllerip+" connection failed "+str(e))