forked from XilefTech/CharlieOSX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webremote.py
87 lines (67 loc) · 2.64 KB
/
webremote.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
import lib.picoweb as picoweb
import _thread, time
class Webremote():
'''Main class for the Webremote'''
def __init__(self, config, robot, brick, logger):
self.__config = config
self.robot = robot
self.brick = brick
self.logger = logger
self.app = picoweb.WebApp("app")
self.outDict = {'x': 0, 'y': 0, 'a1': 0, 'maxSpeed': 100}
self.weblock = _thread.allocate_lock()
self.newData = True
@self.app.route("/")
def index(req, resp):
yield from picoweb.start_response(resp, content_type = "text/html")
htmlFile = open('site/site.html', 'r')
for line in htmlFile:
yield from resp.awrite(line)
@self.app.route("/api")
def api(req, resp):
yield from picoweb.start_response(resp, content_type = "text/html")
proc = req.qs.split("&")
for elm in proc:
temp = elm.split("=")
try:
self.outDict[temp[0]] = int(temp[1])
except:
self.outDict[temp[0]] = temp[1]
self.newData = True
'''
Serving all resources needed for the Webremote
'''
@self.app.route("/style.css")
def style(req, resp):
yield from picoweb.start_response(resp, content_type = "text/css")
htmlFile = open('site/style.css', 'r')
for line in htmlFile:
yield from resp.awrite(line)
@self.app.route("/code.js")
def style(req, resp):
yield from picoweb.start_response(resp, content_type = "text/javascript")
htmlFile = open('site/code.js', 'r')
for line in htmlFile:
yield from resp.awrite(line)
def run(self):
self.logger.info(self, 'Starting Webremote...')
self.startServerThread()
self.logger.info(self, 'Webremote Started!')
while not any(self.brick.buttons.pressed()):
if self.newDataAvailable():
data = self.getResponseData()
self.robot.setRemoteValues(data)
time.sleep(0.05)
def startServerThread(self):
def runWebserver():
with self.weblock:
self.app.run(debug = -1, host = self.__config['localIP'])
_thread.start_new_thread(runWebserver, ())
def getResponseData(self):
return self.outDict
def newDataAvailable(self):
if self.newData:
self.newData = False
return True
else:
return False