-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_server.py
More file actions
58 lines (42 loc) · 1.5 KB
/
web_server.py
File metadata and controls
58 lines (42 loc) · 1.5 KB
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
import BaseHTTPServer, cgi
import os
import sys
import datetime
import shelve
import sys
import threading
import logging
logger = logging.getLogger('pump_control')
def run_manual(hours,minutes):
config = shelve.open('config',writeback=True)
#to be run with two args when pump needs to run for an hour or so.
#first arg is hours, second is minutes. Update config to run manually.
now = datetime.datetime.now()
t=datetime.timedelta(hours=hours,minutes=minutes)
end=now+t
config['manualEndTime']=end
config.close()
logger.info("Manual run written to config")
class httpServHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
if self.path.find('?') != -1:
self.path, self.query_string = self.path.split('?', 1)
else:
self.query_string = ''
#expect the following:
#manual?hours=h
#manual?minutes=m
if self.path.find('manual') != -1:
if self.query_string.find('hours') != -1:
hours=self.query_string.split("=")[1]
hours=int(hours)
run_manual(hours,0)
else:
if self.query_string.find('minutes') != -1:
minutes=self.query_string.split("=")[1]
minutes=int(minutes)
run_manual(0,minutes)
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
self.globals = dict(cgi.parse_qsl(self.query_string))