forked from nikivanov/watney
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
75 lines (57 loc) · 1.69 KB
/
Copy pathserver.py
File metadata and controls
75 lines (57 loc) · 1.69 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
from flask import Flask, send_file, request, send_from_directory, jsonify
from flask_cors import CORS
import json
from rover import Driver
import signal
import sys
from rover import MotorController
from subprocess import call
app = Flask(__name__)
CORS(app)
roverDriver = None
@app.route("/")
def getPageHTML():
return send_file("index.html")
@app.route("/camwindow.html")
def getPageCamwindow():
return send_file("camwindow.html")
@app.route('/js/<path:path>')
def send_js(path):
return send_from_directory('js', path)
@app.route("/sendCommand", methods=['POST'])
def setCommand():
commandObj = json.loads(request.data.decode("utf-8"))
newBearing = commandObj['bearing']
newLook = commandObj['look']
if newBearing in MotorController.validBearings:
if newBearing == "0":
roverDriver.stop()
else:
roverDriver.setBearing(newBearing)
else:
print("Invalid bearing {}".format(newBearing))
return "Invalid", 400
if newLook == 0:
roverDriver.lookStop()
elif newLook == -1:
roverDriver.lookUp()
elif newLook == 1:
roverDriver.lookDown()
else:
print("Invalid look at {}".format(newLook))
return "Invalid", 400
return "OK"
@app.route("/shutDown", methods=['POST'])
def shutdown():
call("sudo halt", shell=True)
@app.route("/heartbeat", methods=['POST'])
def onHeartbeat():
stats = roverDriver.onHeartbeat()
return jsonify(stats)
def signal_handler(signal, frame):
roverDriver.cleanup()
sys.exit(0)
if __name__ == "__main__":
signal.signal(signal.SIGINT, signal_handler)
roverDriver = Driver()
app.run(host='0.0.0.0', port=5000, debug=False)