-
Notifications
You must be signed in to change notification settings - Fork 40
/
server.py
42 lines (31 loc) · 926 Bytes
/
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
import json
import time
from flask import Flask, request
from flask_restful import Resource, Api
import serial
ser = serial.Serial("/dev/ttyACM0", 9600)
app = Flask(__name__, static_url_path='')
app.secret_key = 'why would I tell you my secret key?'
api = Api(app)
tasks = {}
@app.route('/')
def root():
return app.send_static_file('index.html')
class Task(Resource):
@staticmethod
def get(task_id):
results = {}
try:
results = json.loads(tasks[task_id])
except ValueError:
results = tasks[task_id]
return results, 201
@staticmethod
def post(task_id):
tasks[task_id] = request.data
ser.write(request.data)
time.sleep(6)
return tasks[task_id], 204, {'Access-Control-Allow-Origin': '*'}
api.add_resource(Task, '/<string:task_id>')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)