-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapi.py
66 lines (54 loc) · 1.63 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
from flask import Flask, jsonify
from flask_cors import CORS
from MiRouter import *
import util
import threading
import storage
from Exceptions import LoginError
from Device import *
global api
api = MiRouter('192.168.31.1')
app = Flask(__name__)
CORS(app)
POLLING_TIME = 4
def get_data():
while 1:
data = api.get_qos_detail()
devices = data['data']['list']
for device in devices:
device_object = Device()
device_object.from_response(device)
time.sleep(POLLING_TIME)
@app.route("/login")
def login():
global api
result = api.login('adminadmin')
if(result['success']):
return success('Login successfully')
return fail('Login failed')
@app.route("/details")
def details():
global api
if(api.login_token is None):
raise LoginError
data = api.get_qos_detail()
devices = data['data']['list']
devs = []
for device in devices:
util.log(device, file=True, mode='w+')
device_object = Device()
device_object.from_response(device)
"""storage.save_device_detail(device_object)
stored_device = storage.get_by("mac", device_object.mac)
device_object.details = stored_device['data']['details']"""
devs.append(device_object.to_dict())
devs = sorted(devs, reverse = True, key = lambda i: (i['statistics']['downspeed']))
return jsonify(devs)
@app.route("/details/<mac>")
def detail(mac):
global api
if(api.login_token is None):
raise LoginError
device = storage.get_by("mac", mac)
return jsonify(device)
app.run(debug=True, host='0.0.0.0')