-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
91 lines (74 loc) · 2.09 KB
/
server.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
86
87
88
89
90
91
#!/usr/bin/env python3
# Copyright Pololu Corporation. For more information, see https://www.pololu.com/
from flask import Flask
from flask import render_template
from flask import redirect
from subprocess import call
from werkzeug.serving import WSGIRequestHandler
app = Flask(__name__)
app.debug = True
from a_star import AStar
a_star = AStar()
import json
led0_state = False
led1_state = False
led2_state = False
battery_millivolts = [8000]
a_star.play_notes("l32cde")
@app.route("/")
def hello():
return render_template("index.html")
@app.route("/status.json")
def status():
buttons = a_star.read_buttons()
analog = a_star.read_analog()
b = a_star.read_battery_millivolts()
battery_millivolts[0] = int((battery_millivolts[0]*7 + b[0])/8)
encoders = a_star.read_encoders()
data = {
"buttons": buttons,
"battery_millivolts": battery_millivolts,
"analog": analog,
"encoders": encoders
}
return json.dumps(data)
@app.route("/motors/<left>,<right>")
def motors(left, right):
l = int(left)
l = int(l/2)
r = int(right)
r = int(r/2)
print(l,r)
a_star.motors(l,r)
return ""
@app.route("/leds/<int:led0>,<int:led1>,<int:led2>")
def leds(led0, led1, led2):
a_star.leds(led0, led1, led2)
global led0_state
global led1_state
global led2_state
led0_state = led0
led1_state = led1
led2_state = led2
return ""
@app.route("/heartbeat/<int:state>")
def hearbeat(state):
if state == 0:
a_star.leds(led0_state, led1_state, led2_state)
else:
a_star.leds(not led0_state, not led1_state, not led2_state)
return ""
@app.route("/play_notes/<notes>")
def play_notes(notes):
a_star.play_notes(notes)
return ""
@app.route("/halt")
def halt():
call(["bash", "-c", "(sleep 2; sudo halt)&"])
return redirect("/shutting-down")
@app.route("/shutting-down")
def shutting_down():
return "Shutting down in 2 seconds! You can remove power when the green LED stops flashing."
if __name__ == "__main__":
WSGIRequestHandler.protocol_version = "HTTP/1.1"
app.run(host = "0.0.0.0")