pyimouapi 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyimouapi/__init__.py +3 -0
- pyimouapi/const.py +69 -0
- pyimouapi/device.py +283 -0
- pyimouapi/exceptions.py +46 -0
- pyimouapi/openapi.py +89 -0
- pyimouapi-1.0.0.dist-info/LICENSE +21 -0
- pyimouapi-1.0.0.dist-info/METADATA +103 -0
- pyimouapi-1.0.0.dist-info/RECORD +10 -0
- pyimouapi-1.0.0.dist-info/WHEEL +5 -0
- pyimouapi-1.0.0.dist-info/top_level.txt +1 -0
pyimouapi/__init__.py
ADDED
pyimouapi/const.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# API Endpoints
|
|
2
|
+
API_ENDPOINT_ACCESS_TOKEN = "/openapi/accessToken"
|
|
3
|
+
API_ENDPOINT_LIST_DEVICE_DETAILS = "/openapi/listDeviceDetailsByPage"
|
|
4
|
+
API_ENDPOINT_CONTROL_DEVICE_PTZ = "/openapi/controlMovePTZ"
|
|
5
|
+
API_ENDPOINT_MODIFY_DEVICE_ALARM_STATUS = "/openapi/modifyDeviceAlarmStatus"
|
|
6
|
+
API_ENDPOINT_GET_DEVICE_ALARM_PARAM = "/openapi/getDeviceAlarmParam"
|
|
7
|
+
API_ENDPOINT_GET_DEVICE_STATUS = "/openapi/getDeviceCameraStatus"
|
|
8
|
+
API_ENDPOINT_SET_DEVICE_STATUS = "/openapi/setDeviceCameraStatus"
|
|
9
|
+
API_ENDPOINT_GET_DEVICE_NIGHT_VISION_MODE = "/openapi/getNightVisionMode"
|
|
10
|
+
API_ENDPOINT_SET_DEVICE_NIGHT_VISION_MODE = "/openapi/setNightVisionMode"
|
|
11
|
+
API_ENDPOINT_DEVICE_STORAGE = "/openapi/deviceStorage"
|
|
12
|
+
API_ENDPOINT_RESTART_DEVICE = "/openapi/restartDevice"
|
|
13
|
+
API_ENDPOINT_BIND_DEVICE_LIVE = "/openapi/bindDeviceLive"
|
|
14
|
+
API_ENDPOINT_GET_DEVICE_ONLINE = "/openapi/deviceOnline"
|
|
15
|
+
API_ENDPOINT_GET_DEVICE_LIVE_INFO = "/openapi/getLiveStreamInfo"
|
|
16
|
+
API_ENDPOINT_SET_DEVICE_SNAP = "/openapi/setDeviceSnapEnhanced"
|
|
17
|
+
API_ENDPOINT_GET_IOT_DEVICE_PROPERTIES = "/openapi/getIotDeviceProperties"
|
|
18
|
+
API_ENDPOINT_SET_IOT_DEVICE_PROPERTIES = "/openapi/setIotDeviceProperties"
|
|
19
|
+
API_ENDPOINT_DEVICE_SD_CARD_STATUS = "/openapi/deviceSdcardStatus"
|
|
20
|
+
|
|
21
|
+
# error_codes
|
|
22
|
+
ERROR_CODE_SUCCESS = "0"
|
|
23
|
+
ERROR_CODE_TOKEN_OVERDUE = "TK1002"
|
|
24
|
+
ERROR_CODE_INVALID_SIGN = "SN1001"
|
|
25
|
+
ERROR_CODE_INVALID_APP = "SN1004"
|
|
26
|
+
ERROR_CODE_DEVICE_OFFLINE = "DV1007"
|
|
27
|
+
|
|
28
|
+
# params key
|
|
29
|
+
PARAM_APP_ID = "appId"
|
|
30
|
+
PARAM_APP_SECRET = "appSecret"
|
|
31
|
+
PARAM_SYSTEM = "system"
|
|
32
|
+
PARAM_ACCESS_TOKEN = "accessToken"
|
|
33
|
+
PARAM_CURRENT_DOMAIN = "currentDomain"
|
|
34
|
+
PARAM_DEVICE_ID = "deviceId"
|
|
35
|
+
PARAM_CHANNEL_ID = "channelId"
|
|
36
|
+
PARAM_VER = "ver"
|
|
37
|
+
PARAM_SIGN = "sign"
|
|
38
|
+
PARAM_TIME = "time"
|
|
39
|
+
PARAM_NONCE = "nonce"
|
|
40
|
+
PARAM_PARAMS = "params"
|
|
41
|
+
PARAM_ID = "id"
|
|
42
|
+
PARAM_RESULT = "result"
|
|
43
|
+
PARAM_CODE = "code"
|
|
44
|
+
PARAM_MSG = "msg"
|
|
45
|
+
PARAM_DATA = "data"
|
|
46
|
+
PARAM_PAGE = "page"
|
|
47
|
+
PARAM_PAGE_SIZE = "pageSize"
|
|
48
|
+
PARAM_TOKEN = "token"
|
|
49
|
+
PARAM_PRODUCT_ID = "productId"
|
|
50
|
+
PARAM_CHANNEL_NUM = "channelNum"
|
|
51
|
+
PARAM_MODE = "mode"
|
|
52
|
+
PARAM_ENABLE_TYPE = "enableType"
|
|
53
|
+
PARAM_ENABLE = "enable"
|
|
54
|
+
PARAM_COUNT = "count"
|
|
55
|
+
PARAM_DEVICE_LIST = "deviceList"
|
|
56
|
+
PARAM_DEVICE_NAME = "deviceName"
|
|
57
|
+
PARAM_DEVICE_STATUS = "deviceStatus"
|
|
58
|
+
PARAM_DEVICE_ABILITY = "deviceAbility"
|
|
59
|
+
PARAM_DEVICE_VERSION = "deviceVersion"
|
|
60
|
+
PARAM_BRAND = "brand"
|
|
61
|
+
PARAM_DEVICE_MODEL = "deviceModel"
|
|
62
|
+
PARAM_CHANNEL_LIST = "channelList"
|
|
63
|
+
PARAM_CHANNEL_NAME = "channelName"
|
|
64
|
+
PARAM_CHANNEL_STATUS = "channelStatus"
|
|
65
|
+
PARAM_CHANNEL_ABILITY = "channelAbility"
|
|
66
|
+
PARAM_STREAM_ID = "streamId"
|
|
67
|
+
PARAM_OPERATION = "operation"
|
|
68
|
+
PARAM_DURATION = "duration"
|
|
69
|
+
PARAM_PROPERTIES = "properties"
|
pyimouapi/device.py
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
from .const import API_ENDPOINT_LIST_DEVICE_DETAILS, PARAM_PAGE_SIZE, PARAM_PAGE, \
|
|
2
|
+
PARAM_DEVICE_ID, PARAM_CHANNEL_ID, API_ENDPOINT_CONTROL_DEVICE_PTZ, API_ENDPOINT_MODIFY_DEVICE_ALARM_STATUS, \
|
|
3
|
+
API_ENDPOINT_GET_DEVICE_STATUS, API_ENDPOINT_SET_DEVICE_STATUS, API_ENDPOINT_GET_DEVICE_NIGHT_VISION_MODE, \
|
|
4
|
+
API_ENDPOINT_DEVICE_STORAGE, API_ENDPOINT_RESTART_DEVICE, API_ENDPOINT_BIND_DEVICE_LIVE, \
|
|
5
|
+
API_ENDPOINT_GET_DEVICE_ONLINE, API_ENDPOINT_GET_DEVICE_LIVE_INFO, API_ENDPOINT_SET_DEVICE_SNAP, PARAM_MODE, \
|
|
6
|
+
PARAM_ENABLE_TYPE, PARAM_ENABLE, PARAM_COUNT, PARAM_DEVICE_LIST, PARAM_DEVICE_NAME, PARAM_DEVICE_STATUS, \
|
|
7
|
+
PARAM_DEVICE_ABILITY, PARAM_DEVICE_VERSION, PARAM_BRAND, PARAM_DEVICE_MODEL, PARAM_CHANNEL_LIST, PARAM_CHANNEL_NAME, \
|
|
8
|
+
PARAM_CHANNEL_STATUS, PARAM_CHANNEL_ABILITY, PARAM_STREAM_ID, PARAM_OPERATION, PARAM_DURATION, \
|
|
9
|
+
API_ENDPOINT_GET_DEVICE_ALARM_PARAM, API_ENDPOINT_SET_DEVICE_NIGHT_VISION_MODE, PARAM_PRODUCT_ID, PARAM_PROPERTIES, \
|
|
10
|
+
API_ENDPOINT_GET_IOT_DEVICE_PROPERTIES, API_ENDPOINT_SET_IOT_DEVICE_PROPERTIES, API_ENDPOINT_DEVICE_SD_CARD_STATUS, \
|
|
11
|
+
PARAM_CHANNEL_NUM
|
|
12
|
+
from .openapi import ImouOpenApiClient
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class ImouChannel:
|
|
16
|
+
|
|
17
|
+
def __init__(self, channel_id: str, channel_name: str, channel_status: str, channel_ability: str):
|
|
18
|
+
self._channel_id = channel_id
|
|
19
|
+
self._channel_name = channel_name
|
|
20
|
+
self._channel_status = channel_status
|
|
21
|
+
self._channel_ability = channel_ability
|
|
22
|
+
|
|
23
|
+
@property
|
|
24
|
+
def channel_id(self) -> str:
|
|
25
|
+
return self._channel_id
|
|
26
|
+
|
|
27
|
+
@property
|
|
28
|
+
def channel_name(self) -> str:
|
|
29
|
+
return self._channel_name
|
|
30
|
+
|
|
31
|
+
@property
|
|
32
|
+
def channel_status(self) -> str:
|
|
33
|
+
return self._channel_status
|
|
34
|
+
|
|
35
|
+
@property
|
|
36
|
+
def channel_ability(self) -> str:
|
|
37
|
+
return self._channel_ability
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class ImouDevice:
|
|
41
|
+
|
|
42
|
+
def __init__(self, device_id: str, device_name: str, device_status: str,
|
|
43
|
+
device_ability: str, brand: str, device_model: str, device_version: str):
|
|
44
|
+
self._device_id = device_id
|
|
45
|
+
self._device_name = device_name
|
|
46
|
+
self._device_status = device_status
|
|
47
|
+
self._device_ability = device_ability
|
|
48
|
+
self._brand = brand
|
|
49
|
+
self._device_model = device_model
|
|
50
|
+
self._device_version = device_version
|
|
51
|
+
self._channel_number = 0
|
|
52
|
+
self._channels = []
|
|
53
|
+
self._product_id = None
|
|
54
|
+
|
|
55
|
+
@property
|
|
56
|
+
def device_id(self) -> str:
|
|
57
|
+
return self._device_id
|
|
58
|
+
|
|
59
|
+
@property
|
|
60
|
+
def device_name(self) -> str:
|
|
61
|
+
return self._device_name
|
|
62
|
+
|
|
63
|
+
@property
|
|
64
|
+
def device_status(self) -> str:
|
|
65
|
+
return self._device_status
|
|
66
|
+
|
|
67
|
+
@property
|
|
68
|
+
def channels(self) -> []:
|
|
69
|
+
return self._channels
|
|
70
|
+
|
|
71
|
+
@property
|
|
72
|
+
def device_ability(self) -> str:
|
|
73
|
+
return self._device_ability
|
|
74
|
+
|
|
75
|
+
@property
|
|
76
|
+
def brand(self) -> str:
|
|
77
|
+
return self._brand
|
|
78
|
+
|
|
79
|
+
@property
|
|
80
|
+
def device_model(self) -> str:
|
|
81
|
+
return self._device_model
|
|
82
|
+
|
|
83
|
+
@property
|
|
84
|
+
def device_version(self) -> str:
|
|
85
|
+
return self._device_version
|
|
86
|
+
|
|
87
|
+
@property
|
|
88
|
+
def product_id(self) -> str:
|
|
89
|
+
return self._product_id
|
|
90
|
+
|
|
91
|
+
@property
|
|
92
|
+
def channel_number(self) -> int:
|
|
93
|
+
return self._channel_number
|
|
94
|
+
|
|
95
|
+
def set_product_id(self, product_id: str) -> None:
|
|
96
|
+
self._product_id = product_id
|
|
97
|
+
|
|
98
|
+
def set_channels(self, channels: []) -> None:
|
|
99
|
+
self._channels = channels
|
|
100
|
+
|
|
101
|
+
def set_channel_number(self, channel_number: int):
|
|
102
|
+
self._channel_number = channel_number
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
class ImouDeviceManager:
|
|
106
|
+
def __init__(self, imouApiClient: ImouOpenApiClient):
|
|
107
|
+
self._imouApiClient = imouApiClient
|
|
108
|
+
|
|
109
|
+
async def async_get_devices(self, page: int = 1, page_size: int = 10) -> [ImouDevice]:
|
|
110
|
+
"""GET DEVICE LIST"""
|
|
111
|
+
params = {
|
|
112
|
+
PARAM_PAGE: page,
|
|
113
|
+
PARAM_PAGE_SIZE: page_size,
|
|
114
|
+
}
|
|
115
|
+
data = await self._imouApiClient.async_request_api(API_ENDPOINT_LIST_DEVICE_DETAILS, params)
|
|
116
|
+
if data[PARAM_COUNT] == 0:
|
|
117
|
+
return []
|
|
118
|
+
devices = []
|
|
119
|
+
for device in data[PARAM_DEVICE_LIST]:
|
|
120
|
+
device_id = device[PARAM_DEVICE_ID]
|
|
121
|
+
device_name = device[PARAM_DEVICE_NAME]
|
|
122
|
+
device_status = device[PARAM_DEVICE_STATUS]
|
|
123
|
+
device_ability = device[PARAM_DEVICE_ABILITY]
|
|
124
|
+
device_version = device[PARAM_DEVICE_VERSION]
|
|
125
|
+
brand = device[PARAM_BRAND]
|
|
126
|
+
device_model = device[PARAM_DEVICE_MODEL]
|
|
127
|
+
imou_device = ImouDevice(device_id, device_name, device_status, device_ability, brand, device_model,
|
|
128
|
+
device_version)
|
|
129
|
+
if PARAM_PRODUCT_ID in device:
|
|
130
|
+
imou_device.set_product_id(device[PARAM_PRODUCT_ID])
|
|
131
|
+
if PARAM_CHANNEL_NUM in device:
|
|
132
|
+
imou_device.set_channel_number(device[PARAM_CHANNEL_NUM])
|
|
133
|
+
if PARAM_CHANNEL_LIST in device:
|
|
134
|
+
channel_list = device[PARAM_CHANNEL_LIST]
|
|
135
|
+
channels = []
|
|
136
|
+
for channel in channel_list:
|
|
137
|
+
channel_id = channel[PARAM_CHANNEL_ID]
|
|
138
|
+
channel_name = channel[PARAM_CHANNEL_NAME]
|
|
139
|
+
channel_status = channel[PARAM_CHANNEL_STATUS]
|
|
140
|
+
if PARAM_CHANNEL_ABILITY in channel:
|
|
141
|
+
channel_ability = channel[PARAM_CHANNEL_ABILITY]
|
|
142
|
+
else:
|
|
143
|
+
channel_ability = device_ability
|
|
144
|
+
channel = ImouChannel(channel_id, channel_name, channel_status, channel_ability)
|
|
145
|
+
channels.append(channel)
|
|
146
|
+
imou_device.set_channels(channels)
|
|
147
|
+
devices.append(imou_device)
|
|
148
|
+
# If the return quantity is equal to the requested quantity, continue to request the next page
|
|
149
|
+
if data[PARAM_COUNT] == page_size:
|
|
150
|
+
devices.append(self.async_get_devices(page + 1, page_size))
|
|
151
|
+
return devices
|
|
152
|
+
|
|
153
|
+
async def async_control_device_ptz(self, device_id: str, channel_id: str, operation: int,
|
|
154
|
+
duration: int = 500) -> None:
|
|
155
|
+
"""control ptz"""
|
|
156
|
+
params = {
|
|
157
|
+
PARAM_DEVICE_ID: device_id,
|
|
158
|
+
PARAM_CHANNEL_ID: channel_id,
|
|
159
|
+
PARAM_OPERATION: operation,
|
|
160
|
+
PARAM_DURATION: duration
|
|
161
|
+
}
|
|
162
|
+
await self._imouApiClient.async_request_api(API_ENDPOINT_CONTROL_DEVICE_PTZ, params)
|
|
163
|
+
|
|
164
|
+
async def async_modify_device_alarm_status(self, device_id: str, channel_id: str, enabled: bool) -> None:
|
|
165
|
+
"""SET DEVICE ALARM STATUS"""
|
|
166
|
+
params = {
|
|
167
|
+
PARAM_DEVICE_ID: device_id,
|
|
168
|
+
PARAM_CHANNEL_ID: channel_id,
|
|
169
|
+
PARAM_ENABLE: enabled,
|
|
170
|
+
}
|
|
171
|
+
await self._imouApiClient.async_request_api(API_ENDPOINT_MODIFY_DEVICE_ALARM_STATUS, params)
|
|
172
|
+
|
|
173
|
+
async def async_get_device_status(self, device_id: str, channel_id: str, enable_type: str) -> dict:
|
|
174
|
+
"""obtain device capability switch status"""
|
|
175
|
+
params = {
|
|
176
|
+
PARAM_DEVICE_ID: device_id,
|
|
177
|
+
PARAM_CHANNEL_ID: channel_id,
|
|
178
|
+
PARAM_ENABLE_TYPE: enable_type
|
|
179
|
+
}
|
|
180
|
+
return await self._imouApiClient.async_request_api(API_ENDPOINT_GET_DEVICE_STATUS, params)
|
|
181
|
+
|
|
182
|
+
async def async_get_device_online_status(self, device_id: str) -> dict:
|
|
183
|
+
"""GET DEVICE ONLINE STATUS"""
|
|
184
|
+
params = {
|
|
185
|
+
PARAM_DEVICE_ID: device_id,
|
|
186
|
+
}
|
|
187
|
+
return await self._imouApiClient.async_request_api(API_ENDPOINT_GET_DEVICE_ONLINE, params)
|
|
188
|
+
|
|
189
|
+
async def async_set_device_status(self, device_id: str, channel_id: str, enable_type: str, enable: bool) -> None:
|
|
190
|
+
|
|
191
|
+
params = {
|
|
192
|
+
PARAM_DEVICE_ID: device_id,
|
|
193
|
+
PARAM_CHANNEL_ID: channel_id,
|
|
194
|
+
PARAM_ENABLE_TYPE: enable_type,
|
|
195
|
+
PARAM_ENABLE: enable
|
|
196
|
+
}
|
|
197
|
+
await self._imouApiClient.async_request_api(API_ENDPOINT_SET_DEVICE_STATUS, params)
|
|
198
|
+
|
|
199
|
+
async def async_get_device_night_vision_mode(self, device_id: str, channel_id: str) -> dict:
|
|
200
|
+
"""obtain device night vision mode"""
|
|
201
|
+
params = {
|
|
202
|
+
PARAM_DEVICE_ID: device_id,
|
|
203
|
+
PARAM_CHANNEL_ID: channel_id,
|
|
204
|
+
}
|
|
205
|
+
return await self._imouApiClient.async_request_api(API_ENDPOINT_GET_DEVICE_NIGHT_VISION_MODE, params)
|
|
206
|
+
|
|
207
|
+
async def async_set_device_night_vision_mode(self, device_id: str, channel_id: str,
|
|
208
|
+
night_vision_mode: str) -> None:
|
|
209
|
+
"""set device night vision mode"""
|
|
210
|
+
params = {
|
|
211
|
+
PARAM_DEVICE_ID: device_id,
|
|
212
|
+
PARAM_CHANNEL_ID: channel_id,
|
|
213
|
+
PARAM_MODE: night_vision_mode
|
|
214
|
+
}
|
|
215
|
+
await self._imouApiClient.async_request_api(API_ENDPOINT_SET_DEVICE_NIGHT_VISION_MODE, params)
|
|
216
|
+
|
|
217
|
+
async def async_get_device_storage(self, device_id: str) -> dict:
|
|
218
|
+
"""obtain device storage media capacity information"""
|
|
219
|
+
params = {
|
|
220
|
+
PARAM_DEVICE_ID: device_id
|
|
221
|
+
}
|
|
222
|
+
return await self._imouApiClient.async_request_api(API_ENDPOINT_DEVICE_STORAGE, params)
|
|
223
|
+
|
|
224
|
+
async def async_restart_device(self, device_id: str) -> None:
|
|
225
|
+
"""reboot device"""
|
|
226
|
+
params = {
|
|
227
|
+
PARAM_DEVICE_ID: device_id
|
|
228
|
+
}
|
|
229
|
+
await self._imouApiClient.async_request_api(API_ENDPOINT_RESTART_DEVICE, params)
|
|
230
|
+
|
|
231
|
+
async def async_get_stream_url(self, device_id: str, channel_id: str, stream_id: int = 0) -> dict:
|
|
232
|
+
"""obtain the hls stream address of the device"""
|
|
233
|
+
params = {
|
|
234
|
+
PARAM_DEVICE_ID: device_id,
|
|
235
|
+
PARAM_CHANNEL_ID: channel_id
|
|
236
|
+
}
|
|
237
|
+
return await self._imouApiClient.async_request_api(API_ENDPOINT_GET_DEVICE_LIVE_INFO, params)
|
|
238
|
+
|
|
239
|
+
async def async_get_device_snap(self, device_id: str, channel_id: str) -> dict:
|
|
240
|
+
params = {
|
|
241
|
+
PARAM_DEVICE_ID: device_id,
|
|
242
|
+
PARAM_CHANNEL_ID: channel_id
|
|
243
|
+
}
|
|
244
|
+
return await self._imouApiClient.async_request_api(API_ENDPOINT_SET_DEVICE_SNAP, params)
|
|
245
|
+
|
|
246
|
+
async def async_create_stream_url(self, device_id: str, channel_id: str, stream_id: int = 0) -> dict:
|
|
247
|
+
"""create device hls stream address"""
|
|
248
|
+
params = {
|
|
249
|
+
PARAM_DEVICE_ID: device_id,
|
|
250
|
+
PARAM_CHANNEL_ID: channel_id,
|
|
251
|
+
PARAM_STREAM_ID: stream_id
|
|
252
|
+
}
|
|
253
|
+
return await self._imouApiClient.async_request_api(API_ENDPOINT_BIND_DEVICE_LIVE, params)
|
|
254
|
+
|
|
255
|
+
async def async_get_device_alarm_param(self, device_id: str, channel_id: str) -> None:
|
|
256
|
+
"""set device alarm status"""
|
|
257
|
+
params = {
|
|
258
|
+
PARAM_DEVICE_ID: device_id,
|
|
259
|
+
PARAM_CHANNEL_ID: channel_id
|
|
260
|
+
}
|
|
261
|
+
await self._imouApiClient.async_request_api(API_ENDPOINT_GET_DEVICE_ALARM_PARAM, params)
|
|
262
|
+
|
|
263
|
+
async def async_get_iot_device_properties(self, device_id: str, product_id: str, properties: []) -> dict:
|
|
264
|
+
params = {
|
|
265
|
+
PARAM_DEVICE_ID: device_id,
|
|
266
|
+
PARAM_PRODUCT_ID: product_id,
|
|
267
|
+
PARAM_PROPERTIES: properties
|
|
268
|
+
}
|
|
269
|
+
return await self._imouApiClient.async_request_api(API_ENDPOINT_GET_IOT_DEVICE_PROPERTIES, params)
|
|
270
|
+
|
|
271
|
+
async def async_set_iot_device_properties(self, device_id: str, product_id: str, properties: dict) -> None:
|
|
272
|
+
params = {
|
|
273
|
+
PARAM_DEVICE_ID: device_id,
|
|
274
|
+
PARAM_PRODUCT_ID: product_id,
|
|
275
|
+
PARAM_PROPERTIES: properties
|
|
276
|
+
}
|
|
277
|
+
await self._imouApiClient.async_request_api(API_ENDPOINT_SET_IOT_DEVICE_PROPERTIES, params)
|
|
278
|
+
|
|
279
|
+
async def async_get_device_sd_card_status(self, device_id: str) -> dict:
|
|
280
|
+
params = {
|
|
281
|
+
PARAM_DEVICE_ID: device_id
|
|
282
|
+
}
|
|
283
|
+
return await self._imouApiClient.async_request_api(API_ENDPOINT_DEVICE_SD_CARD_STATUS, params)
|
pyimouapi/exceptions.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
import traceback
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class ImouException(Exception):
|
|
6
|
+
def __init__(self, message: str = "") -> None:
|
|
7
|
+
"""Initialize."""
|
|
8
|
+
self.message = message
|
|
9
|
+
super().__init__(self.message)
|
|
10
|
+
|
|
11
|
+
def to_string(self) -> str:
|
|
12
|
+
"""Return the exception as a string."""
|
|
13
|
+
return f"{self.__class__.__name__}: {self.message}\n" + self.traceback()
|
|
14
|
+
|
|
15
|
+
def traceback(self) -> str:
|
|
16
|
+
"""Return the traceback as a string."""
|
|
17
|
+
etype, value, trace = sys.exc_info()
|
|
18
|
+
return "".join(traceback.format_exception(etype, value, trace, None))
|
|
19
|
+
|
|
20
|
+
def get_title(self) -> str:
|
|
21
|
+
"""Return the title of the exception which will be then translated."""
|
|
22
|
+
return "generic_error"
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class ConnectFailedException(ImouException):
|
|
26
|
+
"""connectFailedException. """
|
|
27
|
+
|
|
28
|
+
def get_title(self) -> str:
|
|
29
|
+
"""Return the title of the exception which will be then translated."""
|
|
30
|
+
return "connect_failed"
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class RequestFailedException(ImouException):
|
|
34
|
+
"""requestFailedException """
|
|
35
|
+
|
|
36
|
+
def get_title(self) -> str:
|
|
37
|
+
"""Return the title of the exception which will be then translated."""
|
|
38
|
+
return "request_failed"
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class InvalidAppIdOrSecretException(ImouException):
|
|
42
|
+
"""invalidAppIdOrSecretException """
|
|
43
|
+
|
|
44
|
+
def get_title(self) -> str:
|
|
45
|
+
"""Return the title of the exception which will be then translated."""
|
|
46
|
+
return "appIdOrSecret_invalid"
|
pyimouapi/openapi.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import hashlib
|
|
2
|
+
import json
|
|
3
|
+
import logging
|
|
4
|
+
import secrets
|
|
5
|
+
import time
|
|
6
|
+
import uuid
|
|
7
|
+
|
|
8
|
+
import aiohttp
|
|
9
|
+
import async_timeout
|
|
10
|
+
|
|
11
|
+
from .const import API_ENDPOINT_ACCESS_TOKEN, ERROR_CODE_INVALID_APP, \
|
|
12
|
+
ERROR_CODE_INVALID_SIGN, \
|
|
13
|
+
ERROR_CODE_SUCCESS, ERROR_CODE_TOKEN_OVERDUE, PARAM_ACCESS_TOKEN, PARAM_SYSTEM, PARAM_VER, PARAM_SIGN, PARAM_APP_ID, \
|
|
14
|
+
PARAM_TIME, PARAM_NONCE, PARAM_PARAMS, PARAM_ID, PARAM_RESULT, PARAM_DATA, PARAM_CODE, PARAM_TOKEN, PARAM_MSG, \
|
|
15
|
+
PARAM_CURRENT_DOMAIN
|
|
16
|
+
from .exceptions import ConnectFailedException, RequestFailedException, \
|
|
17
|
+
InvalidAppIdOrSecretException
|
|
18
|
+
|
|
19
|
+
_LOGGER: logging.Logger = logging.getLogger(__package__)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class ImouOpenApiClient:
|
|
23
|
+
|
|
24
|
+
def __init__(self, app_id: str, app_secret: str, api_url: str) -> None:
|
|
25
|
+
self._app_id = app_id
|
|
26
|
+
self._app_secret = app_secret
|
|
27
|
+
self._api_url = api_url
|
|
28
|
+
# token
|
|
29
|
+
self._access_token = None
|
|
30
|
+
|
|
31
|
+
async def async_get_token(self) -> None:
|
|
32
|
+
"""get accessToken"""
|
|
33
|
+
response = await self.async_request_api(API_ENDPOINT_ACCESS_TOKEN, {})
|
|
34
|
+
self._access_token = response[PARAM_ACCESS_TOKEN]
|
|
35
|
+
self._api_url=response[PARAM_CURRENT_DOMAIN].split("://")[1]
|
|
36
|
+
|
|
37
|
+
async def async_request_api(self, endpoint: str, params: dict[any, any] = None) -> dict[any, any]:
|
|
38
|
+
# if accessToken is None , get first
|
|
39
|
+
if self._access_token is None and endpoint != API_ENDPOINT_ACCESS_TOKEN:
|
|
40
|
+
await self.async_get_token()
|
|
41
|
+
if endpoint != API_ENDPOINT_ACCESS_TOKEN:
|
|
42
|
+
params[PARAM_TOKEN] = self._access_token
|
|
43
|
+
timestamp = round(time.time())
|
|
44
|
+
nonce = secrets.token_urlsafe()
|
|
45
|
+
sign = hashlib.md5(f"time:{timestamp},nonce:{nonce},appSecret:{self._app_secret}".encode("utf-8")).hexdigest()
|
|
46
|
+
request_id = str(uuid.uuid4())
|
|
47
|
+
headers = {
|
|
48
|
+
"Content-Type": "application/json",
|
|
49
|
+
"Client-Type": "HomeAssistant"
|
|
50
|
+
}
|
|
51
|
+
body = {
|
|
52
|
+
PARAM_SYSTEM: {
|
|
53
|
+
PARAM_VER: "1.0",
|
|
54
|
+
PARAM_SIGN: sign,
|
|
55
|
+
PARAM_APP_ID: self._app_id,
|
|
56
|
+
PARAM_TIME: timestamp,
|
|
57
|
+
PARAM_NONCE: nonce,
|
|
58
|
+
},
|
|
59
|
+
PARAM_PARAMS: params,
|
|
60
|
+
PARAM_ID: request_id,
|
|
61
|
+
}
|
|
62
|
+
url = f"https://{self._api_url}{endpoint}"
|
|
63
|
+
try:
|
|
64
|
+
async with aiohttp.ClientSession() as session:
|
|
65
|
+
async with async_timeout.timeout(30):
|
|
66
|
+
response = await session.request("POST", url, json=body, headers=headers)
|
|
67
|
+
response_body = json.loads(await response.text())
|
|
68
|
+
_LOGGER.info(f"url: {url} request body: {body} response: {response_body}")
|
|
69
|
+
except Exception as exception:
|
|
70
|
+
raise ConnectFailedException(f"connect failed,{exception}") from exception
|
|
71
|
+
if response.status != 200:
|
|
72
|
+
raise RequestFailedException(f"request failed,status code {response.status}")
|
|
73
|
+
result_code = response_body[PARAM_RESULT][PARAM_CODE]
|
|
74
|
+
result_message = response_body[PARAM_RESULT][PARAM_MSG]
|
|
75
|
+
if result_code != ERROR_CODE_SUCCESS:
|
|
76
|
+
msg = result_code + ":" + result_message
|
|
77
|
+
if result_code in (ERROR_CODE_INVALID_SIGN, ERROR_CODE_INVALID_APP):
|
|
78
|
+
raise InvalidAppIdOrSecretException(msg)
|
|
79
|
+
if result_code == ERROR_CODE_TOKEN_OVERDUE:
|
|
80
|
+
await self.async_get_token()
|
|
81
|
+
return await self.async_request_api(endpoint, params)
|
|
82
|
+
raise RequestFailedException(msg)
|
|
83
|
+
response_data = response_body[PARAM_RESULT][PARAM_DATA] if PARAM_DATA in response_body[PARAM_RESULT] else {}
|
|
84
|
+
return response_data
|
|
85
|
+
|
|
86
|
+
@property
|
|
87
|
+
def access_token(self):
|
|
88
|
+
return self._access_token
|
|
89
|
+
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) [2024] [IMOU]
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: pyimouapi
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: A package for imou open api
|
|
5
|
+
Home-page: https://github.com/Imou-OpenPlatform/Py-Imou-Open-Api
|
|
6
|
+
Author: Imou-OpenPlatform
|
|
7
|
+
Author-email: cloud_openteam_service@imou.com
|
|
8
|
+
License: MIT
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
|
|
12
|
+
# Py-Imou-Open-Api
|
|
13
|
+
|
|
14
|
+
开放平台apiSDK python语言版 owner : 姚翔1 (308581)
|
|
15
|
+
|
|
16
|
+
## Getting started
|
|
17
|
+
|
|
18
|
+
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
|
|
19
|
+
|
|
20
|
+
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
|
|
21
|
+
|
|
22
|
+
## Add your files
|
|
23
|
+
|
|
24
|
+
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
|
|
25
|
+
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
cd existing_repo
|
|
29
|
+
git remote add origin https://hcyfgit.imou.com/PublicCloud/Ecology/Py-Imou-Open-Api.git
|
|
30
|
+
git branch -M main
|
|
31
|
+
git push -uf origin main
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Integrate with your tools
|
|
35
|
+
|
|
36
|
+
- [ ] [Set up project integrations](https://hcyfgit.imou.com/PublicCloud/Ecology/Py-Imou-Open-Api/-/settings/integrations)
|
|
37
|
+
|
|
38
|
+
## Collaborate with your team
|
|
39
|
+
|
|
40
|
+
- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
|
|
41
|
+
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
|
|
42
|
+
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
|
|
43
|
+
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
|
|
44
|
+
- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
|
|
45
|
+
|
|
46
|
+
## Test and Deploy
|
|
47
|
+
|
|
48
|
+
Use the built-in continuous integration in GitLab.
|
|
49
|
+
|
|
50
|
+
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
|
|
51
|
+
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
|
|
52
|
+
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
|
|
53
|
+
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
|
|
54
|
+
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
|
|
55
|
+
|
|
56
|
+
***
|
|
57
|
+
|
|
58
|
+
# Editing this README
|
|
59
|
+
|
|
60
|
+
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template.
|
|
61
|
+
|
|
62
|
+
## Suggestions for a good README
|
|
63
|
+
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
|
|
64
|
+
|
|
65
|
+
## Name
|
|
66
|
+
Choose a self-explaining name for your project.
|
|
67
|
+
|
|
68
|
+
## Description
|
|
69
|
+
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
|
|
70
|
+
|
|
71
|
+
## Badges
|
|
72
|
+
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
|
|
73
|
+
|
|
74
|
+
## Visuals
|
|
75
|
+
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
|
|
76
|
+
|
|
77
|
+
## Installation
|
|
78
|
+
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
|
|
79
|
+
|
|
80
|
+
## Usage
|
|
81
|
+
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
|
|
82
|
+
|
|
83
|
+
## Support
|
|
84
|
+
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
|
|
85
|
+
|
|
86
|
+
## Roadmap
|
|
87
|
+
If you have ideas for releases in the future, it is a good idea to list them in the README.
|
|
88
|
+
|
|
89
|
+
## Contributing
|
|
90
|
+
State if you are open to contributions and what your requirements are for accepting them.
|
|
91
|
+
|
|
92
|
+
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
|
|
93
|
+
|
|
94
|
+
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
|
|
95
|
+
|
|
96
|
+
## Authors and acknowledgment
|
|
97
|
+
Show your appreciation to those who have contributed to the project.
|
|
98
|
+
|
|
99
|
+
## License
|
|
100
|
+
For open source projects, say how it is licensed.
|
|
101
|
+
|
|
102
|
+
## Project status
|
|
103
|
+
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
pyimouapi/__init__.py,sha256=ftbYCwDwZ11OxHWMQrEEYrSRi6hbuglt4xk2NK72vrg,207
|
|
2
|
+
pyimouapi/const.py,sha256=ShEeOAfLm7oAjEZm5L_wjiqyotvRSc8VLA2-cKcvW-E,2614
|
|
3
|
+
pyimouapi/device.py,sha256=DnhZa_s9I2m0TT4alvOKMDpqb2DloJZh1ltjjBgCjdY,11882
|
|
4
|
+
pyimouapi/exceptions.py,sha256=4B1QZT_OgPc8bjmIX-azvpQ3vL8i91PP_X_ufy1mDww,1461
|
|
5
|
+
pyimouapi/openapi.py,sha256=B3138kYFTbYhPdh2DSw3tI0prFQa1giKMCAdr12Hvfs,3826
|
|
6
|
+
pyimouapi-1.0.0.dist-info/LICENSE,sha256=NOByrq7IEgI4uFnMeNYXlCRIfqL4GwXuzR6gR5Vp0yw,1086
|
|
7
|
+
pyimouapi-1.0.0.dist-info/METADATA,sha256=r5VVAY2LHtMA8GI2c4USve7EmtLtDWO22lAhJsvR4EM,6720
|
|
8
|
+
pyimouapi-1.0.0.dist-info/WHEEL,sha256=R06PA3UVYHThwHvxuRWMqaGcr-PuniXahwjmQRFMEkY,91
|
|
9
|
+
pyimouapi-1.0.0.dist-info/top_level.txt,sha256=SL2pqVxWAI-ENSxo-rt-VZ1Wxb5BKRfkd7Ils3pWp4Y,10
|
|
10
|
+
pyimouapi-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pyimouapi
|