-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
579 lines (481 loc) · 17.3 KB
/
api.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
import json
import typing
import psutil
import requests
import time
import threading
import logging
from miio import Yeelight
from gpiozero import CPUTemperature
import speedtest
with open('headers.json') as f:
HEADERS = json.load(f)
COLOR_LIST = ['white', 'violet', 'moonlight', 'red', 'blue', 'lime', 'soft_white', 'daylight', 'warm_white', 'orange',
'yellow', 'green', 'coral', 'cold_white', 'raspberry', 'turquoise', 'cyan', 'purple', 'orchid']
logging.basicConfig(filename='app.log', filemode='w', format='%(name)s - %(levelname)s - %(message)s')
logger = logging.Logger('api_logs', logging.INFO)
MY_HOST = "localhost:1488"
class DeviceInfo:
def __init__(self, device_id, name, state, properties, item_type):
self.id = device_id
self.name = name
self.state = state
self.properties = properties
self.item_type = item_type
def __repr__(self):
return "".join(f"{self.__dict__.values()}")
class Iot:
@staticmethod
def set_on_off(state: bool):
return json.dumps(
{"actions": [{"type": "devices.capabilities.on_off", "state": {"instance": "on", "value": state}}]})
@staticmethod
def change_brightness(value):
if 0 <= value <= 100:
return json.dumps(
{"actions": [
{"type": "devices.capabilities.range", "state": {"instance": "brightness", "value": value}}]})
raise ValueError("value must be <= 100 an >= 1")
@staticmethod
def change_color(color):
# if color in COLOR_LIST:
return json.dumps(
{"actions": [
{"type": "devices.capabilities.color_setting", "state": {"instance": "color", "value": color}}]})
# raise ValueError("value must be in color list")
@staticmethod
def state():
url = 'https://iot.quasar.yandex.ru/m/user/devices?sync_provider_states=1'
r = requests.get(url, headers=HEADERS)
# print(r.text)
data = json.loads(r.text)
with open("test.json", "w", encoding="utf-8") as f:
f.write(r.text)
devices = data.get('rooms')[0].get('devices')
groups = data.get("groups")
# print(devices)
res = {}
for group in groups:
try:
res[group.get('id')] = group.get('capabilities')[0].get('state').get('value')
except AttributeError:
res[group.get('id')] = False
for device in devices:
try:
res[device.get('id')] = device.get('capabilities')[0].get('state').get('value')
except AttributeError:
res[device.get('id')] = False
return res
@staticmethod
def state_v3():
url = "https://iot.quasar.yandex.ru/m/v3/user/devices"
r = requests.get(url, headers=HEADERS)
print(r.text)
data = json.loads(r.text)
devices = data.get("households")[0].get("all")
res = []
for device in devices:
device_id = device.get("id")
name = device.get("name")
state = device.get("capabilities")[0].get("state")
if not state:
state = False
else:
state = state.get("value")
properties = device.get("properties")
item_type = device.get("item_type")
res.append(
DeviceInfo(device_id, name, state, properties, item_type)
)
return res
@staticmethod
def get_csrf():
r = requests.get('https://yandex.ru/quasar/iot', headers=HEADERS)
l = len('window.storage={"csrfToken2":"')
start = r.text.find('window.storage={"csrfToken2":"') + l
end = r.text.find('"', start)
return r.text[start:end]
@staticmethod
def update_csrf():
print('updating csrf')
with open('headers.json', 'w') as f:
HEADERS['x-csrf-token'] = Iot.get_csrf()
json.dump(HEADERS, f)
class Device:
def __init__(self, info: DeviceInfo):
self.id = info.id
self.name = info.name
self.device_url = f'https://iot.quasar.yandex.ru/m/user/devices/{self.id}/actions'
self.state = info.state
self.type = info.item_type
self.properties = info.properties
self.updates_url = None
def __repr__(self):
return f"{self.name} with id:{self.id}, state:{self.state}, props:{self.properties}"
def request(self, data):
r = requests.post(self.device_url, data=data, headers=HEADERS)
if r.status_code == 403:
Iot.update_csrf()
r2 = requests.post(self.device_url, data=data, headers=HEADERS)
if r2.status_code != 200:
raise requests.exceptions.HTTPError(f"{r.status_code}; {r.text}")
elif r.status_code == 200:
pass
else:
print(f'{r.status_code}; error')
raise requests.exceptions.HTTPError(f"{r.status_code}; {r.text}")
return True
def get_updates_url(self):
state_url = f'https://iot.quasar.yandex.ru/m/user/devices/{self.id}'
r = requests.get(state_url, headers=HEADERS)
data = json.loads(r.text)
self.state = bool(data.get('capabilities')[0].get('state').get('value'))
self.updates_url = data.get("updates_url")
def get_state(self):
return {
"state": self.state
}
class MyNodejsXiaomiDevice:
def __init__(self, device_id, name):
self.device_id = device_id
self.name = name
def request(self, path, **kwargs):
url = f"http://{MY_HOST}/{path}"
kwargs["device"] = self.device_id
r = requests.get(url, params=kwargs)
if r.status_code == 200:
return r.text
else:
raise requests.exceptions.HTTPError(f"{r.status_code}; {r.text}")
def turn_on(self):
return self.request('turn_on')
def turn_off(self):
return self.request('turn_off')
class Scenario(Device):
def __init__(self, device_id, children=None):
super().__init__(device_id)
self.children = children
print(self.children)
self.device_url = f'https://iot.quasar.yandex.ru/m/user/scenarios/{self.id}/actions'
self.type = "scenario"
def execute(self):
if self.children:
for child in self.children:
child.state = False
return self.request({})
class Switch(Device):
def __init__(self, info: DeviceInfo):
super().__init__(info)
logger.info(f'starting {self.id} from state {self.state}')
self.type = "switch"
def change_state(self, state):
self.state = state
return self.request(Iot.set_on_off(state))
def turn_on(self):
return self.change_state(True)
def turn_off(self):
return self.change_state(False)
def blink(self, check_state=True):
if check_state:
self.state = Iot.state().get(self.id)
thread = threading.Thread(target=self._blink)
thread.start()
return True
def _blink(self):
self.change_state(not self.state)
time.sleep(1)
return self.change_state(not self.state)
def get_state(self):
return {
"state": self.state
}
class Humidifier(Switch):
def __init__(self, info: DeviceInfo):
super().__init__(info)
try:
self.humidity = info.properties[0].get("state").get("value")
self.temperature = info.properties[1].get("state").get("value")
except AttributeError:
self.humidity = 50
self.temperature = 20
self.type = "humidifier"
def update_state(self):
state_url = f'https://iot.quasar.yandex.ru/m/user/devices/{self.id}'
r = requests.get(state_url, headers=HEADERS)
data = json.loads(r.text)
try:
self.humidity = data.get('properties')[0].get('state').get('value')
self.temperature = data.get('properties')[1].get('state').get('value')
self.state = data.get('capabilities')[0].get('state').get('value')
except AttributeError:
self.humidity = 50
self.temperature = 20
self.state = False
def get_state(self):
return {
"state": self.state,
"temperature": self.temperature,
"humidity": self.humidity
}
class Light(Switch):
def __init__(self, info: DeviceInfo):
super().__init__(info)
self.brightness = 50
self.palette = []
self.color = 'soft_white'
self.type = "light"
def change_color(self, color):
self.color = color
return self.request(Iot.change_color(color))
def change_brightness(self, value):
if not self.state:
self.turn_on()
self.brightness = value
return self.request(Iot.change_brightness(value))
def get_state(self):
state_url = f'https://iot.quasar.yandex.ru/m/user/devices/{self.id}'
r = requests.get(state_url, headers=HEADERS)
data = json.loads(r.text)
self.palette = data.get('capabilities')[1].get('parameters').get('palette')
c = data.get('capabilities')[1].get('state').get('value')
self.state = bool(data.get('capabilities')[0].get('state').get('value'))
self.color = c.get('id')
if not self.color or self.color == "":
def _calc(color_val):
return color_val.get('h') ** 2 + color_val.get('s') ** 2 + color_val.get('v') ** 2
m = 10000000, None
color_sum = _calc(c.get('value'))
for color in self.palette:
a = _calc(color.get('value'))
if abs(a - color_sum) < m[0]:
m = abs(a - color_sum), color.get('id')
self.color = m[1]
self.brightness = data.get('capabilities')[2].get('state').get('value')
return {
"state": self.state,
"color": self.color,
"brightness": self.brightness,
"palette": self.palette
}
def blink_color(self, colors, interval=1, check_state=True):
if check_state:
self.get_state()
if not self.state:
self._blink()
else:
def _():
for color in colors:
self.change_color(color)
self.change_color(self.color)
_()
return True
def blink_brightness(self, values, interval=1, check_state=True):
if check_state:
self.get_state()
def _():
for value in values:
self.change_brightness(value)
self.change_brightness(self.brightness)
_()
return True
class AdvancedLight(Light):
def __init__(self, info: DeviceInfo, ip, token):
self._device = Yeelight(ip, token)
super().__init__(info)
self.color_temp = 0
self.mode = 0
self.rgb = 0
self.get_state()
def get_brightness(self):
if self.mode != 5:
return self.brightness
return 1
def blink(self, check_state=True):
if not self._device:
super().blink(check_state)
else:
def _blink():
self._device.toggle()
time.sleep(1)
self._device.toggle()
threading.Thread(target=_blink).start()
def change_state(self, state):
self.state = state
if state:
return self._device.on()
else:
return self._device.off()
def turn_on(self):
self.state = True
return self._device.on()
def change_mode(self, mode):
self.mode = mode
return self._device.on(mode=mode)
def set_rbg(self, rgb):
self.rgb = rgb
return self._device.set_rgb(rgb)
def turn_off(self):
self.state = False
return self._device.off()
def change_brightness(self, value):
if value <= 1:
self._device.set_brightness(1)
self.brightness = 1
self._device.on(mode=5)
self.mode = 5
return
elif value > 1 and self.mode == 5:
self._device.on(mode=1)
self.mode = 1
elif not self.state:
self._device.on()
self.brightness = value
return self._device.set_brightness(value)
def get_state(self):
status = self._device.status()
if status.moonlight_mode:
mode = 5
elif status.color_flowing:
mode = 4
else:
mode = status.color_mode
self.state = status.is_on
self.rgb = status.rgb
self.mode = mode
self.brightness = status.brightness
self.color_temp = status.color_temp
return {
"state": self.state,
"rgb": self.rgb,
"mode": self.mode,
"brightness": self.brightness,
"temp": self.color_temp,
}
def update_state(self):
self.state = self._device.status().is_on
class AirBlower(MyNodejsXiaomiDevice):
def __init__(self, device_id, name=None):
super(AirBlower, self).__init__(device_id, name)
if not name:
self.name = device_id
self.power = None
self.fan_level = None
self.heater = None
self.alarm = None
self.temperature = None
self.update_state()
def request(self, path, **kwargs):
if path == 'turn_on_heat':
self.heater = True
elif path == 'turn_off_heat':
self.heater = False
elif path == 'turn_on_sound':
self.alarm = True
elif path == 'turn_off_sound':
self.alarm = False
return super().request(path, **kwargs)
def update_state(self):
state = json.loads(self.request("status"))
for attr in state:
if attr.get("name") == "power":
self.power = attr.get("value")
elif attr.get("name") == "fan_level":
self.fan_level = attr.get("value")
elif attr.get("name") == "heater":
self.heater = attr.get("value")
elif attr.get("name") == "alarm":
self.alarm = attr.get("value")
elif attr.get("name") == "temperature":
self.temperature = attr.get("value")
def set_fan_level(self, speed):
if not self.power:
self.turn_on()
self.fan_level = speed
return self.request('set_fan_level', speed=speed)
class AbstractServer:
def __init__(self, id, name, host):
self.id = id
self.name = name
self.host = host
self.internet = {"download": 0, "upload": 0, "ping": 0}
try:
self.sensors = []
self.cpu = {}
self.memory = {}
except AttributeError:
pass
# noinspection DuplicatedCode,PyUnresolvedReferences
def get_internet_speed(self):
st = speedtest.Speedtest()
st.get_best_server([])
d = st.download()
download_speed = d / 1024 / 1024
print("download completed")
upload_speed = st.upload() / 1024 / 1024
print("upload completed")
ping = min(st.get_servers([]).keys())
print("test completed")
self.internet = {"download": download_speed, "upload": upload_speed, "ping": ping}
return self.internet
def get_sensors_info(self):
raise NotImplementedError()
@property
def temperature(self):
raise NotImplementedError()
def get_state(self):
raise NotImplementedError()
class Raspberry(AbstractServer):
def get_state(self):
pass
def get_sensors_info(self):
pass
def __init__(self, id, name):
super().__init__(id, name, "localhost")
self.cpu_temp = CPUTemperature()
@property
def sensors(self):
return [{
"name": "CPU Temperature",
"value": self.temperature,
"comparative": "75",
"unit": "°C",
"comparativeName": "high",
"hasComparative": True
}]
@property
def temperature(self):
return self.cpu_temp.temperature
@property
def cpu(self):
cpu_freq = psutil.cpu_freq()
return {"percent": psutil.cpu_percent(0.7),
"freq": {"value": int(cpu_freq.current), "min": int(cpu_freq.min), "max": int(cpu_freq.max)}
}
@property
def memory(self):
_memory = psutil.virtual_memory()
return {
"percent": _memory.percent,
"total": _memory.total / 1024 / 1024 / 1024,
"used": _memory.used / 1024 / 1024 / 1024,
}
def __repr__(self):
return f"{self.name}; t: {self.temperature}"
class Server(AbstractServer):
def __init__(self, id, name, host):
super().__init__(id, name, host)
self._temperature = -1
self.sensors = []
def get_sensors_info(self):
return requests.get(f"http://{self.host}/sensors")
@property
def temperature(self):
return self._temperature
def get_state(self):
r = json.loads(requests.get(f"http://{self.host}/sensors").text)
self.sensors = r.get("sensors")
print(self.sensors)
self.cpu = r.get("cpu")
self.memory = r.get("memory")
self._temperature = r.get("temperature") if r.get("temperature") is not None else -1
return self.sensors