-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDeviceServer.py
114 lines (101 loc) · 4.16 KB
/
DeviceServer.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from flask import Flask, request
from flask_cors import CORS
import threading
import json
import time
from statemachine import exceptions
class DeviceServer(threading.Thread):
def __init__(self, orchestrator):
super().__init__()
self.app = Flask(__name__)
CORS(self.app)
self.orchestrator = orchestrator
self.last_scale_event = time.time()
@self.app.route('/', methods=["GET"])
def index():
return "hola"
@self.app.route('/LightSenseUnit', methods=["POST"])
def lightpad_value_changed():
id = int(request.form["ID"])
mode = int(request.form["mode"])
value = int(request.form["value"])
print("[LIGHTPAD] " + str(id) + ":" + str(value))
self.orchestrator.handle_lightpad_change(id, value)
return ""
@self.app.route('/DistanceUnit', methods=["POST"])
def distance_sensor_value_changed():
id = int(request.form["ID"])
mode = request.form["mode"]
value = float(request.form["value"])
print("[DISTANCE UNIT] " + str(id) + ":" + str(value))
if id == 0:
self.orchestrator.sizeCalculator.set_depth(value)
if id == 1:
self.orchestrator.sizeCalculator.set_width(value)
if id == 2:
self.orchestrator.sizeCalculator.set_height(value)
"""if id == 3:
try:
if self.orchestrator.sizeCalculator.gift_depth != -1:
if self.orchestrator.sizeCalculator.gift_depth -2 <= value <= self.orchestrator.sizeCalculator.gift_depth +2:
print("gift is placed")
self.orchestrator.gift_placed()
else:
self.orchestrator.gift_removed()
except exceptions.TransitionNotAllowed:
pass
self.orchestrator.sizeCalculator.set_distance_to_gift(value)"""
return ""
@self.app.route('/EncoderUnit', methods=["POST"])
def rotary_encoder_value_changed():
id = int(request.form["ID"])
mode = request.form["mode"]
value = float(request.form["value"])
print("[ENCODER UNIT] " + str(id) + ":" + str(value))
# id of the wrapping paper encoder
if id == 0:
self.orchestrator.paperLengthWatcher.new_encoder_value(value)
return ""
@self.app.route('/ButtonUnit', methods=["POST"])
def button_clicked():
id = int(request.form["ID"])
value = request.form["value"]
print("[BUTTON UNIT] " + str(id) + ":" + str(value))
try:
self.orchestrator.finish_order()
except exceptions.TransitionNotAllowed:
pass
return ""
@self.app.route('/ScaleUnit', methods=["POST"])
def scale_value_changed():
id = request.form["ID"]
mode = request.form["mode"]
value = float(request.form["value"])
print("[SCALE] " + str(id) + ":" + str(value))
if value <= -10:
if time.time() - self.last_scale_event > 5:
print("Tape teared")
self.orchestrator.tape_teared()
self.last_scale_event = time.time()
else:
print("Not enough time elapsed since last event")
return ""
@self.app.route('/order', methods=["POST"])
def on_new_order():
data = json.loads(request.data)
print(data)
"""
data: {
"id":1,
"orderProducts": {
"paperStyle": "style_id",
"paper": "paper_id",
"band": ["band1_id", "band2_id"],
"card": "card_id"
}
}"""
self.orchestrator.orderHandler.add_order(data)
return ""
def run(self):
# for debug mode the flask server must run on the main thread
self.app.run(debug=False, host='0.0.0.0')