forked from DutchForce75/pyZodiacTri
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebserver.py
101 lines (84 loc) · 2.5 KB
/
webserver.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
# coding=utf-8
from myZodiacTriClass import *
from myWebServerClass import *
import time
import resource
from threading import Thread,Event
RS485Device = "/dev/ttyUSB0"
class MyThread(Thread):
def __init__(self, event, zodiacTriClass):
myzodiac = zodiacTriClass
Thread.__init__(self)
self.daemon = True
self.stopped = event
def run(self):
while not self.stopped.wait(5):
if myzodiac.active:
myzodiac.sendCmdProbe()
myzodiac.readMsg()
myzodiac.sendCmdSetPercentage()
myzodiac.readMsg()
class myRequestHandler( defaultRequestHandler ):
myzodiac=0
def setzodiac(self,zodiacTriClass):
self.myzodiac = zodiacTriClass
def SendIndex(self):
arg1 = time.asctime( time.localtime(time.time()))
arg9 = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
if self.myzodiac.connected:
arg2 = "checked" if (self.myzodiac.active) else ""
arg3 = str(self.myzodiac.chloricpct)
if (self.myzodiac.chloricpct==101): arg4 = "boost"
elif (self.myzodiac.chloricpct==0): arg4 = "off"
else: arg4 = str(myzodiac.chloricpct)
arg5 = str(self.myzodiac.saltlevel)
arg6 = self.myzodiac.errorstr
arg7 = str(self.myzodiac.addsalt)
arg8 = "Connected"
else:
arg2 = ""
arg3 = 0
arg4 = "-"
arg5 = "-"
arg6 = "-"
arg7 = "-"
arg8 = "No response from Zodiac!!!"
self.sendHeader200WithMimetype()
f = open("index.html" )
self.wfile.write(f.read() % ( arg1, arg9, arg2, arg3, arg4, arg5, arg6, arg7, arg8) )
f.close()
def sendDynamicWebFileRequest(self):
if re.match('/index.html',self.path):
self.SendIndex()
else:
return False
return True
def FindCommand(self):
if re.match('/toggleConnection', self.path):
self.myzodiac.active = False if (self.myzodiac.active) else True
return True
match = re.match('/setChlorine\?pct=([0-9]+)', self.path)
if match:
pct = (int(match.group(1),10))
if not (pct<0 or pct>101):
self.myzodiac.chloricpct = (int(match.group(1),10))
elif re.match('/rebootpi', self.path):
os.system("reboot")
elif re.match('/gettime', self.path):
os.system("/usr/sbin/ntpd -s")
else:
return False
return True
myzodiac = myZodiacTriClass( RS485Device , True )
myWebServer = myWebServerClass(8081, myRequestHandler)
stopFlag = Event()
thread = MyThread(stopFlag, myzodiac)
try:
thread.start()
myWebServer.theRequestHandler.myzodiac = myzodiac
myWebServer.run()
except KeyboardInterrupt:
print '^C received, shutting down the web server'
stopFlag.set()
exit()