-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrestserv.py
More file actions
50 lines (43 loc) · 1.37 KB
/
restserv.py
File metadata and controls
50 lines (43 loc) · 1.37 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
import web
import threading
from timeutil import *
#SpaceTime object, defined when RestServ() is called
spacetime = None
urls = (
'/', 'index',
r'/set/open/(\d\d?):?(\d\d)', 'setopen',
'/set/closed?/?', 'setclosed'
)
#Initializes a webserver with a restful API to control the SpaceTime board.
#Intended to be made available on the local VHS network, but not over the internet.
#Parameter st should be an initialized SpaceTime object.
def RestServ(st):
global spacetime
spacetime = st
th = RestServThread()
th.daemon = True
th.start()
#The web server runs in a thread so that app.run() doesn't block all other execution.
class RestServThread(threading.Thread):
def run(self):
app = web.application(urls, globals())
app.run()
class index:
def GET(self):
return "SpaceTime REST API\r\n" \
+ "/set/open/15:30 - Sets SpaceTime to stay open until 15:30\r\n" \
+ "/set/closed - Sets SpaceTime to closed"
class setopen:
def GET(self, hours, mins):
#Make a HH:MM:SS time string
timestr = hours.zfill(2) + ":" + mins + ":00"
if IsTimeStr(timestr):
#Closing time is ID 1
spacetime.SetTime(1, StrToTime(timestr))
return "SpaceTime set to " + timestr + "."
return timestr + " is not a valid time."
class setclosed:
def GET(self):
#Closing time is ID 1
spacetime.ClearTime(1)
return "SpaceTime set to closed."