-
Notifications
You must be signed in to change notification settings - Fork 0
/
cablewayserver.py
58 lines (46 loc) · 1.31 KB
/
cablewayserver.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
import cherrypy
from subprocess import call
import threading
import json
class CablewayServer(object):
def __init__(self, motorController):
self.motor = motorController
self.timer = None
@cherrypy.expose
def status(self):
data = {}
data['speed'] = self.motor.PWM / 10
return json.dumps(data)
@cherrypy.expose
def startLeft(self):
self.startTimer()
self.motor.turnLeft()
@cherrypy.expose
def startRight(self):
self.startTimer()
self.motor.turnRight()
@cherrypy.expose
def stop(self):
self.stopTimer()
self.motor.stop()
@cherrypy.expose
def faster(self):
self.motor.faster()
@cherrypy.expose
def slower(self):
self.motor.slower()
@cherrypy.expose
def shutdown(self):
call("sudo shutdown -h now", shell=True)
def startTimer(self):
if (self.timer == None):
self.timer = threading.Timer(5.0, self.stop)
self.timer.start()
else:
if (self.timer.is_alive() == False):
self.timer.start()
def stopTimer(self):
if (self.timer != None):
if (self.timer.is_alive() == True):
self.timer.cancel()
self.timer = None