-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
271 lines (228 loc) · 9.18 KB
/
server.py
File metadata and controls
271 lines (228 loc) · 9.18 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
from flask import Flask, jsonify, request
from flask_httpauth import HTTPTokenAuth
from lamp_config import Lamp, total_presets
from led_strip_config import Led_Strip
from temperature_config import read_temperature
from sensitive_information import tokens, lamp_ip, red_pin, green_pin, blue_pin
# ========== init ==========
lamp = Lamp(lamp_ip)
led_strip = Led_Strip(red_pin, green_pin, blue_pin)
backend = Flask(__name__)
auth = HTTPTokenAuth(scheme='Bearer')
# ========== END init END ==========
# ========== Return Codes ==========
# 2 = Power successfully turned off
# 1 = Power successfully turned on
# 0 = Everything is fine
# -1 = No such device
# -2 = No such action
# -3 = Missing arguments
# -4 = Incorrect argument range
# -5 = ?
# ========== END Return Codes END ==========
# ========== Debugging ==========
@backend.route('/')
def hello():
return "Hello."
@backend.route('/jsontest')
def jsontest():
return return_code(0, "Also, hello. This is the expected json output from this software.")
@auth.verify_token
def verify_token(token):
if token in tokens:
return tokens[token]
@backend.route('/authtest')
@auth.login_required
def authtest():
return return_code(0, "This is an authtest. If you can see this page, then the auth is successful.")
# ========== END Debugging END ==========
@backend.route('/read_temperature')
def temperature():
return return_code(0, read_temperature())
@backend.route('/control/<device_name>/<action>')
@backend.route('/control/<device_name>', defaults={'action' : "NO_DEVICE"})
@backend.route('/control/', defaults={'device_name' : "NO_DEVICE", 'action' : "NO_DEVICE"})
def control(device_name, action):
device_name = device_name.upper()
action = action.upper()
uppercase_request_args = uppercase_dict(request.args)
if device_name == "YEELIGHT":
if action == "TURN_ON":
lamp.TURN_ON()
return return_code(1)
elif action == "TURN_OFF":
lamp.TURN_OFF()
return return_code(2)
elif action == "TOGGLE_POWER":
lamp.TOGGLE_POWER()
power_status = lamp.GET_POWER_STATUS()
if power_status == "ON":
return return_code(1)
return return_code(2)
elif action == "SET_BRIGHTNESS":
brightness_value = uppercase_request_args.get("BRIGHTNESS_VALUE")
if isNotEmpty(brightness_value):
if brightness_isCorrectRange(brightness_value):
lamp.SET_BRIGHTNESS(brightness_value)
return return_code(0, "Setting brightness...")
return return_code(-4, 100)
return return_code(-3)
elif action == "SET_RGB":
r_value = uppercase_request_args.get("R_VALUE")
g_value = uppercase_request_args.get("G_VALUE")
b_value = uppercase_request_args.get("B_VALUE")
if isNotEmpty(r_value) and isNotEmpty(g_value) and isNotEmpty(b_value):
if RGB_isCorrectRange(r_value) and RGB_isCorrectRange(g_value) and RGB_isCorrectRange(b_value):
lamp.SET_RGB(r_value, g_value, b_value)
return return_code(0, "Changing RGB value...")
return return_code(-4, 255)
return return_code(-3)
elif action == "SET_HSV":
h_value = uppercase_request_args.get("H_VALUE")
s_value = uppercase_request_args.get("S_VALUE")
if isNotEmpty(h_value) and isNotEmpty(s_value):
if RGB_isCorrectRange(h_value) and RGB_isCorrectRange(s_value):
lamp.SET_HSV(h_value, s_value)
return return_code(0, "Changing HSV...")
return return_code(-4, 255)
return return_code(-3)
elif action == "SET_COLOR_TEMP":
temperature_value = uppercase_request_args.get("TEMP_VALUE")
if isNotEmpty(temperature_value):
if temperature_isCorrectRange(temperature_value):
lamp.SET_COLOR_TEMP(temperature_value)
return return_code(0, "Setting temperature value...")
return return_code(-4.1)
return return_code(-3)
elif action == "SET_PRESET":
preset_value = uppercase_request_args.get("PRESET_VALUE")
if isNotEmpty(preset_value):
if preset_isCorrectRange(preset_value):
lamp.SET_PRESET(preset_value)
return return_code(0, "Setting preset...")
return return_code(-4, total_presets)
return return_code(-3)
elif action == "CHANGE_TARGET_IP":
target_ip = uppercase_request_args.get("TARGET_IP")
if isNotEmpty(target_ip):
lamp.CHANGE_TARGET_IP(target_ip)
return return_code(0, "Lamp change successful.")
return return_code(-3, "Incorrect arguments.")
elif action == "GET_POWER_STATUS":
return return_code(0, lamp.GET_POWER_STATUS())
else:
return return_code(-2)
elif device_name == "LED_STRIP":
if action == "TURN_ON":
led_strip.TURN_ON(255, 255, 255, 100)
return return_code(1)
elif action == "TOGGLE_POWER":
if led_strip.is_currently_on:
led_strip.TURN_OFF()
return return_code(2)
else:
led_strip.TURN_ON_PRESET("BLUE_VIOLET", 20)
return return_code(1)
elif action == "SET_RGB":
r_value = uppercase_request_args.get("R_VALUE")
g_value = uppercase_request_args.get("G_VALUE")
b_value = uppercase_request_args.get("B_VALUE")
if isNotEmpty(r_value) and isNotEmpty(g_value) and isNotEmpty(b_value):
led_strip.SET_RGB(r_value, g_value, b_value)
return return_code(0, "Changing RGB value...")
return return_code(-3)
elif action == "TURN_OFF":
led_strip.TURN_OFF()
return return_code(2)
elif action == "GET_POWER_STATUS":
return return_code(0, led_strip.GET_POWER_STATUS())
else:
return return_code(-2)
else:
return return_code(-1)
# ========== utils ==========
# Essential to make GET arguments case insensitive
def uppercase_dict(old_dict):
new_uppercase_dict = {}
for key in old_dict:
new_uppercase_dict[key.upper()] = old_dict[key]
return new_uppercase_dict
# Make sure arguments are not empty
def isNotEmpty(argument):
return type(argument) is not type(None)
# Make sure RGB value is within range
def RGB_isCorrectRange(single_color_value):
return int(single_color_value) >= 0 and int(single_color_value) <= 255
# Make sure brightness value is within range
def brightness_isCorrectRange(brightness_value):
return int(brightness_value) >= 0 and int(brightness_value) <= 100
# Make sure temperature value is within range
def temperature_isCorrectRange(temperature_value):
return int(temperature_value) >= 1700 and int(temperature_value) <= 6500
#Make sure presets is available
def preset_isCorrectRange(preset_value):
return int(preset_value) >= 1 and int(preset_value) <= total_presets
# Return code builder
def return_code(code, ext_message = ""):
if code == 0:
return jsonify(
status="Success",
code=code,
code_details="Everything is fine.",
message=ext_message
)
elif code == 1:
return jsonify(
status="Success",
code=code,
code_details="Power has been successfully turned on.",
message=ext_message
)
elif code == 2:
return jsonify(
status="Success",
code=code,
code_details="Power has been successfully turned off.",
message=ext_message
)
elif code == -1:
return jsonify(
status="Failure",
code=code,
code_details="No such device.",
message=ext_message
)
elif code == -2:
return jsonify(
status="Failure",
code=code,
code_details="No such action.",
message=ext_message
)
elif code == -3:
return jsonify(
status="Failure",
code=code,
code_details="Missing or incorrect argument(s).",
message=ext_message
)
elif code == -4:
return jsonify(
status="Failure",
code=code,
code_details="Incorrect argument(s) range.",
message="Make sure the value is between 0-%s" %ext_message
)
elif code == -4.1:
return jsonify(
status="Failure",
code=code,
code_details="Incorrect argument(s) range.",
message="Make sure the temperature value is between 1700-6500"
)
else:
print("WARNING: There is no such error code.")
# ========== END utils END ==========
if __name__ == '__main__':
print("Starting up (native Flask)...")
backend.run(host='0.0.0.0', threaded=True)