-
Notifications
You must be signed in to change notification settings - Fork 65
/
device_api.py
112 lines (97 loc) · 2.55 KB
/
device_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
from libs.webserver.executer import Executer
from flask import Blueprint, request, jsonify
from flask_login import login_required
import copy
device_api = Blueprint('device_api', __name__)
@device_api.route('/api/system/devices', methods=['GET'])
@login_required
def get_devices(): # pylint: disable=E0211
"""
Devices
---
tags:
- System
responses:
200:
description: OK
schema:
type: object,
example:
[
{
id: str,
name: str
},
...
]
403:
description: Could not find devices
"""
data_out = dict()
devices = Executer.instance.device_executer.get_devices()
data_out = devices
if devices is None:
return "Could not find devices: ", 403
else:
return jsonify(data_out)
@device_api.route('/api/system/devices', methods=['POST'])
@login_required
def create_device(): # pylint: disable=E0211
"""
Add new device
---
tags:
- System
responses:
200:
description: OK
schema:
type: object,
example:
{
index: int
}
"""
index = Executer.instance.device_executer.create_new_device()
data_out = {
"index": index
}
return jsonify(data_out)
@device_api.route('/api/system/devices', methods=['DELETE'])
@login_required
def delete_device(): # pylint: disable=E0211
"""
Delete device
---
tags:
- System
parameters:
- name: device
in: body
type: string
required: true
description: ID of `device` to delete
schema:
type: object,
example:
{
device: str
}
responses:
200:
description: OK
schema:
type: object,
example:
{
device: str
}
403:
description: Input data are wrong
"""
data_in = request.get_json()
data_out = copy.deepcopy(data_in)
if not Executer.instance.device_executer.validate_data_in(data_in, ("device",)):
return "Input data are wrong.", 403
Executer.instance.device_executer.delete_device(data_in["device"])
return jsonify(data_out)