forked from XilefTech/CharlieOSX
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebremote.py
More file actions
Latest commit
87 lines (67 loc) · 2.64 KB
/
Copy pathwebremote.py
File metadata and controls
87 lines (67 loc) · 2.64 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
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
importlib.picowebaspicoweb
import_thread, time
classWebremote():
'''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("/")
defindex(req, resp):
yieldfrompicoweb.start_response(resp, content_type="text/html")
htmlFile=open('site/site.html', 'r')
forlineinhtmlFile:
yieldfromresp.awrite(line)
@self.app.route("/api")
defapi(req, resp):
yieldfrompicoweb.start_response(resp, content_type="text/html")
proc=req.qs.split("&")
forelminproc:
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")
defstyle(req, resp):
yieldfrompicoweb.start_response(resp, content_type="text/css")
htmlFile=open('site/style.css', 'r')
forlineinhtmlFile:
yieldfromresp.awrite(line)
@self.app.route("/code.js")
defstyle(req, resp):
yieldfrompicoweb.start_response(resp, content_type="text/javascript")
htmlFile=open('site/code.js', 'r')
forlineinhtmlFile:
yieldfromresp.awrite(line)
defrun(self):
self.logger.info(self, 'Starting Webremote...')
self.startServerThread()
self.logger.info(self, 'Webremote Started!')
whilenotany(self.brick.buttons.pressed()):
ifself.newDataAvailable():
data=self.getResponseData()
self.robot.setRemoteValues(data)
time.sleep(0.05)
defstartServerThread(self):
defrunWebserver():
withself.weblock:
self.app.run(debug=-1, host=self.__config['localIP'])
_thread.start_new_thread(runWebserver, ())
defgetResponseData(self):
returnself.outDict
defnewDataAvailable(self):
ifself.newData:
self.newData=False
returnTrue
else:
returnFalse