-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·69 lines (55 loc) · 1.47 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
#!/usr/bin/env python3
import chess
import serial
import sys
import queue
if len(sys.argv) <= 1:
sys.exit("Expected serial port name")
ser = serial.Serial(sys.argv[1], 115200)
eventQueues = []
def putEvent(e):
for q in eventQueues:
q.put(e)
client = chess.ChessClient(ser, putEvent)
client.start()
from flask import Flask, render_template, redirect, Response
app = Flask(__name__)
#app.debug = True
@app.route("/")
def index():
return render_template("index.html")
@app.route("/status.json")
def status():
if s := client.jsonStatus():
return s
else:
print("Error getting status")
return Response(status=503)
@app.route("/move/<i>")
def move(i):
client.move(int(i))
return ""
@app.route("/reset")
def reset():
client.reset()
return ""
@app.route("/config/<whiteAI>,<blackAI>")
def config(whiteAI, blackAI):
client.config(whiteAI == 'true', blackAI == 'true')
return ""
@app.route("/timeout/<timeout>")
def timeout(timeout):
client.timeout = int(timeout)
return ""
@app.route("/events")
def events():
def eventStream():
eventQueue = queue.Queue()
eventQueues.append(eventQueue)
while True:
# wait for event to be available, then push it
event = eventQueue.get()
yield 'data: {}\n\n'.format(event)
return Response(eventStream(), mimetype="text/event-stream")
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000, use_reloader=False)