pymammotion 0.4.0a5__py3-none-any.whl → 0.4.0a6__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.
- pymammotion/aliyun/cloud_gateway.py +7 -1
- pymammotion/data/model/device.py +4 -4
- pymammotion/data/model/device_info.py +2 -1
- pymammotion/data/state_manager.py +8 -9
- pymammotion/mqtt/linkkit/__init__.py +1 -1
- pymammotion/mqtt/linkkit/h2client.py +171 -135
- pymammotion/mqtt/linkkit/linkkit.py +448 -451
- pymammotion/mqtt/mammotion_mqtt.py +7 -7
- pymammotion/utility/device_config.py +657 -25
- pymammotion/utility/device_type.py +13 -1
- {pymammotion-0.4.0a5.dist-info → pymammotion-0.4.0a6.dist-info}/METADATA +3 -1
- {pymammotion-0.4.0a5.dist-info → pymammotion-0.4.0a6.dist-info}/RECORD +14 -14
- {pymammotion-0.4.0a5.dist-info → pymammotion-0.4.0a6.dist-info}/LICENSE +0 -0
- {pymammotion-0.4.0a5.dist-info → pymammotion-0.4.0a6.dist-info}/WHEEL +0 -0
@@ -16,23 +16,23 @@
|
|
16
16
|
#
|
17
17
|
#
|
18
18
|
|
19
|
-
import os
|
20
|
-
import logging
|
21
|
-
import threading
|
22
|
-
import queue
|
23
|
-
import urllib.request
|
24
|
-
import urllib.parse
|
25
|
-
import json
|
26
19
|
import hashlib
|
27
20
|
import hmac
|
21
|
+
import json
|
22
|
+
import logging
|
23
|
+
import os
|
24
|
+
import queue
|
28
25
|
import random
|
26
|
+
import re
|
29
27
|
import ssl
|
30
|
-
import socket
|
31
28
|
import string
|
32
|
-
import time
|
33
|
-
import re
|
34
29
|
import sys
|
30
|
+
import threading
|
31
|
+
import time
|
32
|
+
import urllib.parse
|
33
|
+
import urllib.request
|
35
34
|
from enum import Enum
|
35
|
+
|
36
36
|
import paho.mqtt.client as mqtt
|
37
37
|
from paho.mqtt.enums import CallbackAPIVersion
|
38
38
|
|
@@ -41,17 +41,19 @@ REQUIRED_MINOR_VERSION = 5
|
|
41
41
|
|
42
42
|
|
43
43
|
# check Python version
|
44
|
-
def lk_check_python_version(major_version, minor_version):
|
44
|
+
def lk_check_python_version(major_version, minor_version) -> None:
|
45
45
|
version = sys.version_info
|
46
46
|
if version[0] < major_version or (version[0] == major_version and version[1] < minor_version):
|
47
|
-
print(
|
48
|
-
|
47
|
+
print(
|
48
|
+
"WARNING: linkit requires Python %d.%d or higher, and the current version is %s"
|
49
|
+
% (major_version, minor_version, sys.version)
|
50
|
+
)
|
49
51
|
|
50
52
|
|
51
53
|
lk_check_python_version(REQUIRED_MAJOR_VERSION, REQUIRED_MINOR_VERSION)
|
52
54
|
|
53
55
|
|
54
|
-
class LinkKit
|
56
|
+
class LinkKit:
|
55
57
|
TAG_KEY = "attrKey"
|
56
58
|
TAG_VALUE = "attrValue"
|
57
59
|
|
@@ -67,33 +69,32 @@ class LinkKit(object):
|
|
67
69
|
class ErrorCode(Enum):
|
68
70
|
SUCCESS = 0
|
69
71
|
# 错误码一共三位数,第一位为0表示为mqtt相关的错误码
|
70
|
-
NETWORK_DISCONNECTED = -0x001
|
71
|
-
INVALID_TOPIC = -0x002
|
72
|
-
INVALID_QOS = -0x003
|
73
|
-
PAHO_MQTT_ERROR = -0x004
|
72
|
+
NETWORK_DISCONNECTED = -0x001 # 当前mqtt连接断开
|
73
|
+
INVALID_TOPIC = -0x002 # 输入的topic为空
|
74
|
+
INVALID_QOS = -0x003 # 输入的qos非法
|
75
|
+
PAHO_MQTT_ERROR = -0x004 # 底层的mqtt报错
|
74
76
|
# 0x100开头的表示为网关相关的错误码
|
75
|
-
NULL_SUBDEV_ERR = -0x101
|
76
|
-
SUBDEV_NOT_ARRAY_ERR = -0x102
|
77
|
-
ARRAY_LENGTH_ERR = -0x103
|
77
|
+
NULL_SUBDEV_ERR = -0x101 # 输入的子设备信息为空错误
|
78
|
+
SUBDEV_NOT_ARRAY_ERR = -0x102 # 输入数据并非数组错误
|
79
|
+
ARRAY_LENGTH_ERR = -0x103 # 数组长度错误
|
78
80
|
# 0x300开头的表示为动态注册相关的错误码
|
79
|
-
DYNREG_AUTH_FAILED = -0x301
|
81
|
+
DYNREG_AUTH_FAILED = -0x301 # 预注册动态注册认证失败
|
80
82
|
DYNREG_AUTH_NWL_FAILED = -0x302 # 免白动态注册认证失败
|
81
83
|
# 0x400开头表示为OTA有关的错误
|
82
|
-
OTA_INVALID_PARAM = -0x401
|
83
|
-
OTA_DIGEST_MISMATCH = -0x402
|
84
|
-
OTA_PUB_FAILED = -0x403
|
85
|
-
OTA_INVALID_SIGN_METHOD = -0x404
|
86
|
-
OTA_DOWNLOAD_FAIL = -0x405
|
87
|
-
OTA_INVALID_URL = -
|
88
|
-
OTA_INVALID_PATH = -
|
89
|
-
|
84
|
+
OTA_INVALID_PARAM = -0x401 # 参数非法
|
85
|
+
OTA_DIGEST_MISMATCH = -0x402 # 校验和不通过
|
86
|
+
OTA_PUB_FAILED = -0x403 # 上报失败
|
87
|
+
OTA_INVALID_SIGN_METHOD = -0x404 # 非法校验方法
|
88
|
+
OTA_DOWNLOAD_FAIL = -0x405 # 下载失败
|
89
|
+
OTA_INVALID_URL = -0x406 # 无法访问HTTP链接
|
90
|
+
OTA_INVALID_PATH = -0x407 # 存储路径打开失败
|
90
91
|
|
91
92
|
class StateError(Exception):
|
92
|
-
def __init__(self, err):
|
93
|
+
def __init__(self, err) -> None:
|
93
94
|
Exception.__init__(self, err)
|
94
95
|
|
95
|
-
class Shadow
|
96
|
-
def __init__(self):
|
96
|
+
class Shadow:
|
97
|
+
def __init__(self) -> None:
|
97
98
|
self.__version = None
|
98
99
|
self.__timestamp = None
|
99
100
|
self.__state = None
|
@@ -114,23 +115,23 @@ class LinkKit(object):
|
|
114
115
|
with self.__latest_shadow_lock:
|
115
116
|
return self.__state
|
116
117
|
|
117
|
-
def set_state(self, state):
|
118
|
+
def set_state(self, state) -> None:
|
118
119
|
with self.__latest_shadow_lock:
|
119
120
|
self.__state = state
|
120
121
|
|
121
|
-
def set_metadata(self, metadata):
|
122
|
+
def set_metadata(self, metadata) -> None:
|
122
123
|
with self.__latest_shadow_lock:
|
123
124
|
self.__metadata = metadata
|
124
125
|
|
125
|
-
def set_version(self, version):
|
126
|
+
def set_version(self, version) -> None:
|
126
127
|
with self.__latest_shadow_lock:
|
127
128
|
self.__version = version
|
128
129
|
|
129
|
-
def set_timestamp(self, timestamp):
|
130
|
+
def set_timestamp(self, timestamp) -> None:
|
130
131
|
with self.__latest_shadow_lock:
|
131
132
|
self.__timestamp = timestamp
|
132
133
|
|
133
|
-
def set_latest_recevied_time(self, timestamp):
|
134
|
+
def set_latest_recevied_time(self, timestamp) -> None:
|
134
135
|
with self.__latest_shadow_lock:
|
135
136
|
self.__latest_received_time = timestamp
|
136
137
|
|
@@ -138,7 +139,7 @@ class LinkKit(object):
|
|
138
139
|
with self.__latest_shadow_lock:
|
139
140
|
return self.__latest_received_time
|
140
141
|
|
141
|
-
def set_latest_recevied_payload(self, payload):
|
142
|
+
def set_latest_recevied_payload(self, payload) -> None:
|
142
143
|
with self.__latest_shadow_lock:
|
143
144
|
self.__latest_received_payload = payload
|
144
145
|
|
@@ -147,15 +148,18 @@ class LinkKit(object):
|
|
147
148
|
return self.__latest_received_payload
|
148
149
|
|
149
150
|
def to_dict(self):
|
150
|
-
return {
|
151
|
-
|
151
|
+
return {
|
152
|
+
"state": self.__state,
|
153
|
+
"metadata": self.__metadata,
|
154
|
+
"version": self.__version,
|
155
|
+
"timestamp": self.__timestamp,
|
156
|
+
}
|
152
157
|
|
153
158
|
def to_json_string(self):
|
154
159
|
return json.dumps(self.to_dict())
|
155
160
|
|
156
|
-
class __HandlerTask
|
157
|
-
|
158
|
-
def __init__(self, logger=None):
|
161
|
+
class __HandlerTask:
|
162
|
+
def __init__(self, logger=None) -> None:
|
159
163
|
self.__logger = logger
|
160
164
|
if self.__logger is not None:
|
161
165
|
self.__logger.info("HandlerTask init enter")
|
@@ -164,21 +168,18 @@ class LinkKit(object):
|
|
164
168
|
self.__started = False
|
165
169
|
self.__exited = False
|
166
170
|
self.__thread = None
|
167
|
-
pass
|
168
171
|
|
169
|
-
def register_cmd_callback(self, cmd, callback):
|
172
|
+
def register_cmd_callback(self, cmd, callback) -> int:
|
170
173
|
if self.__started is False:
|
171
174
|
if cmd != "req_exit":
|
172
175
|
self.__cmd_callback[cmd] = callback
|
173
176
|
return 0
|
174
177
|
else:
|
175
178
|
return 1
|
176
|
-
pass
|
177
179
|
else:
|
178
180
|
return 2
|
179
|
-
pass
|
180
181
|
|
181
|
-
def post_message(self, cmd, value):
|
182
|
+
def post_message(self, cmd, value) -> bool:
|
182
183
|
self.__logger.debug("post_message :%r " % cmd)
|
183
184
|
if self.__started and self.__exited is False:
|
184
185
|
try:
|
@@ -190,9 +191,8 @@ class LinkKit(object):
|
|
190
191
|
return True
|
191
192
|
self.__logger.debug("post_message fail started:%r,exited:%r" % (self.__started, self.__exited))
|
192
193
|
return False
|
193
|
-
pass
|
194
194
|
|
195
|
-
def start(self):
|
195
|
+
def start(self) -> int:
|
196
196
|
if self.__logger is not None:
|
197
197
|
self.__logger.info("HandlerTask start")
|
198
198
|
if self.__started is False:
|
@@ -207,16 +207,16 @@ class LinkKit(object):
|
|
207
207
|
return 0
|
208
208
|
return 1
|
209
209
|
|
210
|
-
def stop(self):
|
210
|
+
def stop(self) -> None:
|
211
211
|
if self.__started and self.__exited is False:
|
212
212
|
self.__exited = True
|
213
213
|
self.__message_queue.put(("req_exit", None))
|
214
214
|
|
215
|
-
def wait_stop(self):
|
215
|
+
def wait_stop(self) -> None:
|
216
216
|
if self.__started is True:
|
217
217
|
self.__thread.join()
|
218
218
|
|
219
|
-
def __thread_runnable(self):
|
219
|
+
def __thread_runnable(self) -> None:
|
220
220
|
if self.__logger is not None:
|
221
221
|
self.__logger.debug("thread runnable enter")
|
222
222
|
while True:
|
@@ -233,10 +233,9 @@ class LinkKit(object):
|
|
233
233
|
self.__started = False
|
234
234
|
if self.__logger is not None:
|
235
235
|
self.__logger.debug("thread runnable exit")
|
236
|
-
pass
|
237
236
|
|
238
|
-
class LoopThread
|
239
|
-
def __init__(self, logger=None):
|
237
|
+
class LoopThread:
|
238
|
+
def __init__(self, logger=None) -> None:
|
240
239
|
self.__logger = logger
|
241
240
|
if logger is not None:
|
242
241
|
self.__logger.info("LoopThread init enter")
|
@@ -247,7 +246,7 @@ class LinkKit(object):
|
|
247
246
|
if logger is not None:
|
248
247
|
self.__logger.info("LoopThread init exit")
|
249
248
|
|
250
|
-
def start(self, callback):
|
249
|
+
def start(self, callback) -> int:
|
251
250
|
if self.__started is True:
|
252
251
|
self.__logger.info("LoopThread already ")
|
253
252
|
return 1
|
@@ -258,11 +257,11 @@ class LinkKit(object):
|
|
258
257
|
self.__thread.start()
|
259
258
|
return 0
|
260
259
|
|
261
|
-
def stop(self):
|
260
|
+
def stop(self) -> None:
|
262
261
|
self.__req_wait.wait()
|
263
262
|
self.__req_wait.clear()
|
264
263
|
|
265
|
-
def __thread_main(self):
|
264
|
+
def __thread_main(self) -> None:
|
266
265
|
self.__started = True
|
267
266
|
try:
|
268
267
|
if self.__logger is not None:
|
@@ -276,52 +275,51 @@ class LinkKit(object):
|
|
276
275
|
self.__started = False
|
277
276
|
self.__req_wait.set()
|
278
277
|
|
279
|
-
def _on_file_upload_start(self, id, upload_file_info, user_data):
|
278
|
+
def _on_file_upload_start(self, id, upload_file_info, user_data) -> None:
|
280
279
|
if self.__on_file_upload_begin != None:
|
281
280
|
self.__on_file_upload_begin(id, upload_file_info, self.__user_data)
|
282
281
|
|
283
|
-
def _on_file_upload_end(self, id, upload_file_info, upload_file_result, user_data):
|
282
|
+
def _on_file_upload_end(self, id, upload_file_info, upload_file_result, user_data) -> None:
|
284
283
|
if self.__on_file_upload_end != None:
|
285
284
|
self.__on_file_upload_end(id, upload_file_info, upload_file_result, self.__user_data)
|
286
285
|
|
287
|
-
def _on_file_upload_progress(self, id, upload_file_result, upload_file_info, user_data):
|
286
|
+
def _on_file_upload_progress(self, id, upload_file_result, upload_file_info, user_data) -> None:
|
288
287
|
pass
|
289
288
|
|
290
|
-
class __LinkKitLog
|
291
|
-
def __init__(self):
|
289
|
+
class __LinkKitLog:
|
290
|
+
def __init__(self) -> None:
|
292
291
|
self.__logger = logging.getLogger("linkkit")
|
293
292
|
self.__enabled = False
|
294
|
-
pass
|
295
293
|
|
296
|
-
def enable_logger(self):
|
294
|
+
def enable_logger(self) -> None:
|
297
295
|
self.__enabled = True
|
298
296
|
|
299
|
-
def disable_logger(self):
|
297
|
+
def disable_logger(self) -> None:
|
300
298
|
self.__enabled = False
|
301
299
|
|
302
300
|
def is_enabled(self):
|
303
301
|
return self.__enabled
|
304
302
|
|
305
|
-
def config_logger(self, level):
|
303
|
+
def config_logger(self, level) -> None:
|
306
304
|
self.__logger.setLevel(level)
|
307
305
|
|
308
|
-
def debug(self, fmt, *args):
|
306
|
+
def debug(self, fmt, *args) -> None:
|
309
307
|
if self.__enabled:
|
310
308
|
self.__logger.debug(fmt, *args)
|
311
309
|
|
312
|
-
def warring(self, fmt, *args):
|
310
|
+
def warring(self, fmt, *args) -> None:
|
313
311
|
if self.__enabled:
|
314
312
|
self.__logger.warning(fmt, *args)
|
315
313
|
|
316
|
-
def info(self, fmt, *args):
|
314
|
+
def info(self, fmt, *args) -> None:
|
317
315
|
if self.__enabled:
|
318
316
|
self.__logger.info(fmt, *args)
|
319
317
|
|
320
|
-
def error(self, fmt, *args):
|
318
|
+
def error(self, fmt, *args) -> None:
|
321
319
|
if self.__enabled:
|
322
320
|
self.__logger.error(fmt, *args)
|
323
321
|
|
324
|
-
def critical(self, fmt, *args):
|
322
|
+
def critical(self, fmt, *args) -> None:
|
325
323
|
if self.__enabled:
|
326
324
|
self.__logger.critical(fmt, *args)
|
327
325
|
|
@@ -451,14 +449,24 @@ UxeCp6
|
|
451
449
|
-----END CERTIFICATE-----
|
452
450
|
"""
|
453
451
|
|
454
|
-
def __init__(
|
455
|
-
|
456
|
-
|
457
|
-
|
452
|
+
def __init__(
|
453
|
+
self,
|
454
|
+
host_name,
|
455
|
+
product_key,
|
456
|
+
device_name,
|
457
|
+
device_secret,
|
458
|
+
auth_type=None,
|
459
|
+
username=None,
|
460
|
+
client_id=None,
|
461
|
+
password=None,
|
462
|
+
instance_id=None,
|
463
|
+
product_secret=None,
|
464
|
+
user_data=None,
|
465
|
+
) -> None:
|
458
466
|
# logging configs
|
459
467
|
self.__just_for_pycharm_autocomplete = False
|
460
468
|
|
461
|
-
def __str_is_empty(value):
|
469
|
+
def __str_is_empty(value) -> bool:
|
462
470
|
if value is None or value == "":
|
463
471
|
return True
|
464
472
|
else:
|
@@ -576,49 +584,56 @@ UxeCp6
|
|
576
584
|
self.__user_rrpc_request_max_len = 100
|
577
585
|
|
578
586
|
# things topic - Alink
|
579
|
-
self.__thing_topic_prop_post =
|
580
|
-
(self.__product_key, self.__device_name)
|
587
|
+
self.__thing_topic_prop_post = "/sys/%s/%s/thing/event/property/post" % (self.__product_key, self.__device_name)
|
581
588
|
self.__thing_topic_prop_post_reply = self.__thing_topic_prop_post + "_reply"
|
582
|
-
self.__thing_topic_prop_set =
|
583
|
-
(self.__product_key, self.__device_name)
|
589
|
+
self.__thing_topic_prop_set = "/sys/%s/%s/thing/service/property/set" % (self.__product_key, self.__device_name)
|
584
590
|
self.__thing_topic_prop_set_reply = self.__thing_topic_prop_set + "_reply"
|
585
|
-
self.__thing_topic_prop_get =
|
586
|
-
|
587
|
-
self.
|
588
|
-
self.__gateway_topic_add_subdev_topo = '/sys/%s/%s/thing/topo/add' % \
|
589
|
-
(self.__product_key, self.__device_name)
|
591
|
+
self.__thing_topic_prop_get = "/sys/%s/%s/thing/service/property/get" % (self.__product_key, self.__device_name)
|
592
|
+
self.__thing_topic_event_post_pattern = "/sys/%s/%s/thing/event/%s/post"
|
593
|
+
self.__gateway_topic_add_subdev_topo = "/sys/%s/%s/thing/topo/add" % (self.__product_key, self.__device_name)
|
590
594
|
self.__gateway_topic_add_subdev_topo_reply = self.__gateway_topic_add_subdev_topo + "_reply"
|
591
595
|
|
592
|
-
self.__gateway_topic_delete_subdev_topo =
|
593
|
-
|
596
|
+
self.__gateway_topic_delete_subdev_topo = "/sys/%s/%s/thing/topo/delete" % (
|
597
|
+
self.__product_key,
|
598
|
+
self.__device_name,
|
599
|
+
)
|
594
600
|
self.__gateway_topic_delete_subdev_topo_reply = self.__gateway_topic_delete_subdev_topo + "_reply"
|
595
601
|
|
596
|
-
self.__gateway_topic_login_subdev =
|
597
|
-
|
602
|
+
self.__gateway_topic_login_subdev = "/ext/session/%s/%s/combine/batch_login" % (
|
603
|
+
self.__product_key,
|
604
|
+
self.__device_name,
|
605
|
+
)
|
598
606
|
self.__gateway_topic_login_subdev_reply = self.__gateway_topic_login_subdev + "_reply"
|
599
607
|
|
600
|
-
self.__gateway_topic_logout_subdev =
|
601
|
-
|
608
|
+
self.__gateway_topic_logout_subdev = "/ext/session/%s/%s/combine/batch_logout" % (
|
609
|
+
self.__product_key,
|
610
|
+
self.__device_name,
|
611
|
+
)
|
602
612
|
self.__gateway_topic_logout_subdev_reply = self.__gateway_topic_logout_subdev + "_reply"
|
603
613
|
|
604
|
-
self.__gateway_topic_register_subdev =
|
605
|
-
|
614
|
+
self.__gateway_topic_register_subdev = "/sys/%s/%s/thing/sub/register" % (
|
615
|
+
self.__product_key,
|
616
|
+
self.__device_name,
|
617
|
+
)
|
606
618
|
self.__gateway_topic_register_subdev_reply = self.__gateway_topic_register_subdev + "_reply"
|
607
619
|
|
608
|
-
self.__gateway_topic_product_register_subdev =
|
609
|
-
|
620
|
+
self.__gateway_topic_product_register_subdev = "/sys/%s/%s/thing/proxy/provisioning/product_register" % (
|
621
|
+
self.__product_key,
|
622
|
+
self.__device_name,
|
623
|
+
)
|
610
624
|
self.__gateway_topic_product_register_subdev_reply = self.__gateway_topic_product_register_subdev + "_reply"
|
611
625
|
|
612
|
-
self.__gateway_topic_topo_change =
|
613
|
-
|
614
|
-
self.
|
615
|
-
self.__dynamic_register_topic = '/ext/register'
|
626
|
+
self.__gateway_topic_topo_change = "/sys/%s/%s/thing/topo/change" % (self.__product_key, self.__device_name)
|
627
|
+
self.__dynamic_register_nwl_topic = "/ext/regnwl"
|
628
|
+
self.__dynamic_register_topic = "/ext/register"
|
616
629
|
|
617
|
-
self.__ota_report_version_topic =
|
618
|
-
self.__ota_push_topic =
|
619
|
-
self.__ota_pull_topic =
|
620
|
-
self.__ota_pull_reply_topic =
|
621
|
-
self.__product_key,
|
630
|
+
self.__ota_report_version_topic = "/ota/device/inform/%s/%s" % (self.__product_key, self.__device_name)
|
631
|
+
self.__ota_push_topic = "/ota/device/upgrade/%s/%s" % (self.__product_key, self.__device_name)
|
632
|
+
self.__ota_pull_topic = "/sys/%s/%s/thing/ota/firmware/get" % (self.__product_key, self.__device_name)
|
633
|
+
self.__ota_pull_reply_topic = "/sys/%s/%s/thing/ota/firmware/get_reply" % (
|
634
|
+
self.__product_key,
|
635
|
+
self.__device_name,
|
636
|
+
)
|
622
637
|
|
623
638
|
self.__thing_prop_post_mid = {}
|
624
639
|
self.__thing_prop_post_mid_lock = threading.Lock()
|
@@ -639,15 +654,13 @@ UxeCp6
|
|
639
654
|
self.__thing_event_post_mid = {}
|
640
655
|
self.__thing_event_post_mid_lock = threading.Lock()
|
641
656
|
|
642
|
-
self.__thing_topic_shadow_get =
|
643
|
-
|
644
|
-
self.__thing_topic_shadow_update = '/shadow/update/%s/%s' % \
|
645
|
-
(self.__product_key, self.__device_name)
|
657
|
+
self.__thing_topic_shadow_get = "/shadow/get/%s/%s" % (self.__product_key, self.__device_name)
|
658
|
+
self.__thing_topic_shadow_update = "/shadow/update/%s/%s" % (self.__product_key, self.__device_name)
|
646
659
|
self.__thing_shadow_mid = {}
|
647
660
|
self.__thing_shadow_mid_lock = threading.Lock()
|
648
661
|
|
649
662
|
# service topic
|
650
|
-
self.__thing_topic_service_pattern =
|
663
|
+
self.__thing_topic_service_pattern = "/sys/%s/%s/thing/service/%s"
|
651
664
|
self.__thing_topic_services = set()
|
652
665
|
self.__thing_topic_services_reply = set()
|
653
666
|
self.__thing_services = set()
|
@@ -655,9 +668,9 @@ UxeCp6
|
|
655
668
|
self.__thing_answer_service_mid_lock = threading.Lock()
|
656
669
|
|
657
670
|
# thing topic - raw
|
658
|
-
self.__thing_topic_raw_up =
|
671
|
+
self.__thing_topic_raw_up = "/sys/%s/%s/thing/model/up_raw" % (self.__product_key, self.__device_name)
|
659
672
|
self.__thing_topic_raw_up_reply = self.__thing_topic_raw_up + "_reply"
|
660
|
-
self.__thing_topic_raw_down =
|
673
|
+
self.__thing_topic_raw_down = "/sys/%s/%s/thing/model/down_raw" % (self.__product_key, self.__device_name)
|
661
674
|
self.__thing_topic_raw_down_reply = self.__thing_topic_raw_down + "_reply"
|
662
675
|
self.__thing_raw_up_mid = {}
|
663
676
|
self.__thing_raw_up_mid_lock = threading.Lock()
|
@@ -665,11 +678,15 @@ UxeCp6
|
|
665
678
|
self.__thing_raw_down_reply_mid_lock = threading.Lock()
|
666
679
|
|
667
680
|
# thing topic - device_info
|
668
|
-
self.__thing_topic_update_device_info_up =
|
669
|
-
|
681
|
+
self.__thing_topic_update_device_info_up = "/sys/%s/%s/thing/deviceinfo/update" % (
|
682
|
+
self.__product_key,
|
683
|
+
self.__device_name,
|
684
|
+
)
|
670
685
|
self.__thing_topic_update_device_info_reply = self.__thing_topic_update_device_info_up + "_reply"
|
671
|
-
self.__thing_topic_delete_device_info_up =
|
672
|
-
|
686
|
+
self.__thing_topic_delete_device_info_up = "/sys/%s/%s/thing/deviceinfo/delete" % (
|
687
|
+
self.__product_key,
|
688
|
+
self.__device_name,
|
689
|
+
)
|
673
690
|
self.__thing_topic_delete_device_info_reply = self.__thing_topic_delete_device_info_up + "_reply"
|
674
691
|
self.__thing_update_device_info_up_mid = {}
|
675
692
|
self.__thing_update_device_info_up_mid_lock = threading.Lock()
|
@@ -721,27 +738,32 @@ UxeCp6
|
|
721
738
|
self.__handler_task_cmd_on_publish = "on_publish"
|
722
739
|
self.__handler_task_cmd_on_subscribe = "on_subscribe"
|
723
740
|
self.__handler_task_cmd_on_unsubscribe = "on_unsubscribe"
|
724
|
-
self.__handler_task.register_cmd_callback(
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
self.__handler_task.register_cmd_callback(
|
731
|
-
|
732
|
-
|
733
|
-
|
734
|
-
|
735
|
-
|
741
|
+
self.__handler_task.register_cmd_callback(
|
742
|
+
self.__handler_task_cmd_on_connect, self.__handler_task_on_connect_callback
|
743
|
+
)
|
744
|
+
self.__handler_task.register_cmd_callback(
|
745
|
+
self.__handler_task_cmd_on_disconnect, self.__handler_task_on_disconnect_callback
|
746
|
+
)
|
747
|
+
self.__handler_task.register_cmd_callback(
|
748
|
+
self.__handler_task_cmd_on_message, self.__handler_task_on_message_callback
|
749
|
+
)
|
750
|
+
self.__handler_task.register_cmd_callback(
|
751
|
+
self.__handler_task_cmd_on_publish, self.__handler_task_on_publish_callback
|
752
|
+
)
|
753
|
+
self.__handler_task.register_cmd_callback(
|
754
|
+
self.__handler_task_cmd_on_subscribe, self.__handler_task_on_subscribe_callback
|
755
|
+
)
|
756
|
+
self.__handler_task.register_cmd_callback(
|
757
|
+
self.__handler_task_cmd_on_unsubscribe, self.__handler_task_on_unsubscribe_callback
|
758
|
+
)
|
736
759
|
self.__handler_task.start()
|
737
|
-
pass
|
738
760
|
|
739
761
|
@property
|
740
762
|
def on_device_dynamic_register(self):
|
741
763
|
return None
|
742
764
|
|
743
765
|
@on_device_dynamic_register.setter
|
744
|
-
def on_device_dynamic_register(self, value):
|
766
|
+
def on_device_dynamic_register(self, value) -> None:
|
745
767
|
self.__on_device_dynamic_register = value
|
746
768
|
|
747
769
|
@property
|
@@ -749,16 +771,15 @@ UxeCp6
|
|
749
771
|
return self.__on_connect
|
750
772
|
|
751
773
|
@on_connect.setter
|
752
|
-
def on_connect(self, value):
|
774
|
+
def on_connect(self, value) -> None:
|
753
775
|
self.__on_connect = value
|
754
|
-
pass
|
755
776
|
|
756
777
|
@property
|
757
778
|
def on_disconnect(self):
|
758
779
|
return self.__on_disconnect
|
759
780
|
|
760
781
|
@on_disconnect.setter
|
761
|
-
def on_disconnect(self, value):
|
782
|
+
def on_disconnect(self, value) -> None:
|
762
783
|
self.__on_disconnect = value
|
763
784
|
|
764
785
|
@property
|
@@ -766,7 +787,7 @@ UxeCp6
|
|
766
787
|
return None
|
767
788
|
|
768
789
|
@on_publish_topic.setter
|
769
|
-
def on_publish_topic(self, value):
|
790
|
+
def on_publish_topic(self, value) -> None:
|
770
791
|
self.__on_publish_topic = value
|
771
792
|
|
772
793
|
@property
|
@@ -774,7 +795,7 @@ UxeCp6
|
|
774
795
|
return None
|
775
796
|
|
776
797
|
@on_subscribe_topic.setter
|
777
|
-
def on_subscribe_topic(self, value):
|
798
|
+
def on_subscribe_topic(self, value) -> None:
|
778
799
|
self.__on_subscribe_topic = value
|
779
800
|
|
780
801
|
@property
|
@@ -782,7 +803,7 @@ UxeCp6
|
|
782
803
|
return None
|
783
804
|
|
784
805
|
@on_unsubscribe_topic.setter
|
785
|
-
def on_unsubscribe_topic(self, value):
|
806
|
+
def on_unsubscribe_topic(self, value) -> None:
|
786
807
|
self.__on_unsubscribe_topic = value
|
787
808
|
|
788
809
|
@property
|
@@ -790,7 +811,7 @@ UxeCp6
|
|
790
811
|
return None
|
791
812
|
|
792
813
|
@on_topic_message.setter
|
793
|
-
def on_topic_message(self, value):
|
814
|
+
def on_topic_message(self, value) -> None:
|
794
815
|
self.__on_topic_message = value
|
795
816
|
|
796
817
|
@property
|
@@ -798,7 +819,7 @@ UxeCp6
|
|
798
819
|
return None
|
799
820
|
|
800
821
|
@on_topic_rrpc_message.setter
|
801
|
-
def on_topic_rrpc_message(self, value):
|
822
|
+
def on_topic_rrpc_message(self, value) -> None:
|
802
823
|
self.__on_topic_rrpc_message = value
|
803
824
|
|
804
825
|
@property
|
@@ -806,7 +827,7 @@ UxeCp6
|
|
806
827
|
return None
|
807
828
|
|
808
829
|
@on_thing_create.setter
|
809
|
-
def on_thing_create(self, value):
|
830
|
+
def on_thing_create(self, value) -> None:
|
810
831
|
self.__on_thing_create = value
|
811
832
|
|
812
833
|
@property
|
@@ -814,7 +835,7 @@ UxeCp6
|
|
814
835
|
return None
|
815
836
|
|
816
837
|
@on_thing_enable.setter
|
817
|
-
def on_thing_enable(self, value):
|
838
|
+
def on_thing_enable(self, value) -> None:
|
818
839
|
self.__on_thing_enable = value
|
819
840
|
|
820
841
|
@property
|
@@ -822,7 +843,7 @@ UxeCp6
|
|
822
843
|
return None
|
823
844
|
|
824
845
|
@on_thing_disable.setter
|
825
|
-
def on_thing_disable(self, value):
|
846
|
+
def on_thing_disable(self, value) -> None:
|
826
847
|
self.__on_thing_disable = value
|
827
848
|
|
828
849
|
@property
|
@@ -830,7 +851,7 @@ UxeCp6
|
|
830
851
|
return None
|
831
852
|
|
832
853
|
@on_thing_raw_data_arrived.setter
|
833
|
-
def on_thing_raw_data_arrived(self, value):
|
854
|
+
def on_thing_raw_data_arrived(self, value) -> None:
|
834
855
|
self.__on_thing_raw_data_arrived = value
|
835
856
|
|
836
857
|
@property
|
@@ -842,7 +863,7 @@ UxeCp6
|
|
842
863
|
return self.__on_thing_device_info_update
|
843
864
|
|
844
865
|
@on_thing_device_info_update.setter
|
845
|
-
def on_thing_device_info_update(self, value):
|
866
|
+
def on_thing_device_info_update(self, value) -> None:
|
846
867
|
self.__on_thing_device_info_update = value
|
847
868
|
|
848
869
|
@property
|
@@ -850,11 +871,11 @@ UxeCp6
|
|
850
871
|
return self.__on_thing_device_info_delete
|
851
872
|
|
852
873
|
@on_thing_device_info_delete.setter
|
853
|
-
def on_thing_device_info_delete(self, value):
|
874
|
+
def on_thing_device_info_delete(self, value) -> None:
|
854
875
|
self.__on_thing_device_info_delete = value
|
855
876
|
|
856
877
|
@on_thing_raw_data_post.setter
|
857
|
-
def on_thing_raw_data_post(self, value):
|
878
|
+
def on_thing_raw_data_post(self, value) -> None:
|
858
879
|
self.__on_thing_raw_data_post = value
|
859
880
|
|
860
881
|
@property
|
@@ -862,7 +883,7 @@ UxeCp6
|
|
862
883
|
return None
|
863
884
|
|
864
885
|
@on_thing_call_service.setter
|
865
|
-
def on_thing_call_service(self, value):
|
886
|
+
def on_thing_call_service(self, value) -> None:
|
866
887
|
self.__on_thing_call_service = value
|
867
888
|
|
868
889
|
@property
|
@@ -870,7 +891,7 @@ UxeCp6
|
|
870
891
|
return None
|
871
892
|
|
872
893
|
@on_thing_prop_changed.setter
|
873
|
-
def on_thing_prop_changed(self, value):
|
894
|
+
def on_thing_prop_changed(self, value) -> None:
|
874
895
|
self.__on_thing_prop_changed = value
|
875
896
|
|
876
897
|
@property
|
@@ -878,7 +899,7 @@ UxeCp6
|
|
878
899
|
return self.__on_thing_event_post
|
879
900
|
|
880
901
|
@on_thing_event_post.setter
|
881
|
-
def on_thing_event_post(self, value):
|
902
|
+
def on_thing_event_post(self, value) -> None:
|
882
903
|
self.__on_thing_event_post = value
|
883
904
|
|
884
905
|
@property
|
@@ -886,7 +907,7 @@ UxeCp6
|
|
886
907
|
return self.__on_thing_prop_post
|
887
908
|
|
888
909
|
@on_thing_prop_post.setter
|
889
|
-
def on_thing_prop_post(self, value):
|
910
|
+
def on_thing_prop_post(self, value) -> None:
|
890
911
|
self.__on_thing_prop_post = value
|
891
912
|
|
892
913
|
@property
|
@@ -894,7 +915,7 @@ UxeCp6
|
|
894
915
|
return self.__on_thing_shadow_get
|
895
916
|
|
896
917
|
@on_thing_shadow_get.setter
|
897
|
-
def on_thing_shadow_get(self, value):
|
918
|
+
def on_thing_shadow_get(self, value) -> None:
|
898
919
|
self.__on_thing_shadow_get = value
|
899
920
|
|
900
921
|
@property
|
@@ -902,7 +923,7 @@ UxeCp6
|
|
902
923
|
return self.__on_file_upload_begin
|
903
924
|
|
904
925
|
@on_file_upload_begin.setter
|
905
|
-
def on_file_upload_begin(self, value):
|
926
|
+
def on_file_upload_begin(self, value) -> None:
|
906
927
|
self.__on_file_upload_begin = value
|
907
928
|
|
908
929
|
@property
|
@@ -910,7 +931,7 @@ UxeCp6
|
|
910
931
|
return self.__on_file_upload_end
|
911
932
|
|
912
933
|
@on_file_upload_end.setter
|
913
|
-
def on_file_upload_end(self, value):
|
934
|
+
def on_file_upload_end(self, value) -> None:
|
914
935
|
self.__on_file_upload_end = value
|
915
936
|
|
916
937
|
@property
|
@@ -918,7 +939,7 @@ UxeCp6
|
|
918
939
|
return None
|
919
940
|
|
920
941
|
@on_gateway_add_subdev_topo_reply.setter
|
921
|
-
def on_gateway_add_subdev_topo_reply(self, value):
|
942
|
+
def on_gateway_add_subdev_topo_reply(self, value) -> None:
|
922
943
|
self.__on_gateway_add_subdev_topo_reply = value
|
923
944
|
|
924
945
|
@property
|
@@ -926,7 +947,7 @@ UxeCp6
|
|
926
947
|
return None
|
927
948
|
|
928
949
|
@on_gateway_delete_subdev_topo_reply.setter
|
929
|
-
def on_gateway_delete_subdev_topo_reply(self, value):
|
950
|
+
def on_gateway_delete_subdev_topo_reply(self, value) -> None:
|
930
951
|
self.__on_gateway_delete_subdev_topo_reply = value
|
931
952
|
|
932
953
|
@property
|
@@ -934,7 +955,7 @@ UxeCp6
|
|
934
955
|
return None
|
935
956
|
|
936
957
|
@on_gateway_login_subdev_reply.setter
|
937
|
-
def on_gateway_login_subdev_reply(self, value):
|
958
|
+
def on_gateway_login_subdev_reply(self, value) -> None:
|
938
959
|
self.__on_gateway_login_subdev_reply = value
|
939
960
|
|
940
961
|
@property
|
@@ -942,7 +963,7 @@ UxeCp6
|
|
942
963
|
return None
|
943
964
|
|
944
965
|
@on_gateway_logout_subdev_reply.setter
|
945
|
-
def on_gateway_logout_subdev_reply(self, value):
|
966
|
+
def on_gateway_logout_subdev_reply(self, value) -> None:
|
946
967
|
self.__on_gateway_logout_subdev_reply = value
|
947
968
|
|
948
969
|
@property
|
@@ -950,7 +971,7 @@ UxeCp6
|
|
950
971
|
return None
|
951
972
|
|
952
973
|
@on_gateway_register_subdev_reply.setter
|
953
|
-
def on_gateway_register_subdev_reply(self, value):
|
974
|
+
def on_gateway_register_subdev_reply(self, value) -> None:
|
954
975
|
self.__on_gateway_register_subdev_reply = value
|
955
976
|
|
956
977
|
@property
|
@@ -958,7 +979,7 @@ UxeCp6
|
|
958
979
|
return None
|
959
980
|
|
960
981
|
@on_gateway_product_register_subdev_reply.setter
|
961
|
-
def on_gateway_product_register_subdev_reply(self, value):
|
982
|
+
def on_gateway_product_register_subdev_reply(self, value) -> None:
|
962
983
|
self.__on_gateway_product_register_subdev_reply = value
|
963
984
|
|
964
985
|
@property
|
@@ -966,7 +987,7 @@ UxeCp6
|
|
966
987
|
return None
|
967
988
|
|
968
989
|
@on_device_dynamic_register_nwl_reply.setter
|
969
|
-
def on_device_dynamic_register_nwl_reply(self, value):
|
990
|
+
def on_device_dynamic_register_nwl_reply(self, value) -> None:
|
970
991
|
self.__on_device_dynamic_register_nwl_reply = value
|
971
992
|
|
972
993
|
@property
|
@@ -974,7 +995,7 @@ UxeCp6
|
|
974
995
|
return None
|
975
996
|
|
976
997
|
@on_ota_message_arrived.setter
|
977
|
-
def on_ota_message_arrived(self, value):
|
998
|
+
def on_ota_message_arrived(self, value) -> None:
|
978
999
|
self.__on_ota_message_arrived = value
|
979
1000
|
|
980
1001
|
@property
|
@@ -982,22 +1003,22 @@ UxeCp6
|
|
982
1003
|
return None
|
983
1004
|
|
984
1005
|
@on_gateway_topo_change.setter
|
985
|
-
def on_gateway_topo_change(self, value):
|
1006
|
+
def on_gateway_topo_change(self, value) -> None:
|
986
1007
|
self.__on_gateway_topo_change = value
|
987
1008
|
|
988
|
-
def enable_logger(self, level):
|
1009
|
+
def enable_logger(self, level) -> None:
|
989
1010
|
self.__link_log.config_logger(level)
|
990
1011
|
self.__link_log.enable_logger()
|
991
1012
|
if self.__mqtt_client is not None:
|
992
1013
|
self.__mqtt_client.enable_logger(self.__PahoLog)
|
993
1014
|
self.__PahoLog.setLevel(level)
|
994
1015
|
|
995
|
-
def disable_logger(self):
|
1016
|
+
def disable_logger(self) -> None:
|
996
1017
|
self.__link_log.disable_logger()
|
997
1018
|
if self.__mqtt_client is not None:
|
998
1019
|
self.__mqtt_client.disable_logger()
|
999
1020
|
|
1000
|
-
def config_logger(self, level):
|
1021
|
+
def config_logger(self, level) -> None:
|
1001
1022
|
self.__link_log.config_logger(level)
|
1002
1023
|
if self.__mqtt_client is not None:
|
1003
1024
|
self.__PahoLog.setLevel(level)
|
@@ -1005,14 +1026,22 @@ UxeCp6
|
|
1005
1026
|
def config_http2(self, endpoint=None):
|
1006
1027
|
raise LinkKit.StateError("not supported any more")
|
1007
1028
|
|
1008
|
-
def config_mqtt(
|
1009
|
-
|
1010
|
-
|
1011
|
-
|
1012
|
-
|
1013
|
-
|
1014
|
-
|
1015
|
-
|
1029
|
+
def config_mqtt(
|
1030
|
+
self,
|
1031
|
+
port=8883,
|
1032
|
+
protocol="MQTTv311",
|
1033
|
+
transport="TCP",
|
1034
|
+
secure="TLS",
|
1035
|
+
keep_alive=60,
|
1036
|
+
clean_session=True,
|
1037
|
+
max_inflight_message=20,
|
1038
|
+
max_queued_message=40,
|
1039
|
+
auto_reconnect_min_sec=1,
|
1040
|
+
auto_reconnect_max_sec=60,
|
1041
|
+
cadata=None,
|
1042
|
+
endpoint=None,
|
1043
|
+
check_hostname=True,
|
1044
|
+
):
|
1016
1045
|
if self.__linkkit_state is not LinkKit.LinkKitState.INITIALIZED:
|
1017
1046
|
raise LinkKit.StateError("not in INITIALIZED state")
|
1018
1047
|
if port < 1 or port > 65535:
|
@@ -1066,10 +1095,10 @@ UxeCp6
|
|
1066
1095
|
self.__endpoint = endpoint
|
1067
1096
|
if check_hostname is not None:
|
1068
1097
|
self.__check_hostname = check_hostname
|
1069
|
-
if
|
1098
|
+
if check_hostname == False:
|
1070
1099
|
self.__link_log.info("skip hostname check")
|
1071
1100
|
|
1072
|
-
def config_device_info(self, interface_info):
|
1101
|
+
def config_device_info(self, interface_info) -> int:
|
1073
1102
|
if self.__linkkit_state is not LinkKit.LinkKitState.INITIALIZED:
|
1074
1103
|
raise LinkKit.StateError("LinkKit object not in INITIALIZED")
|
1075
1104
|
if not isinstance(interface_info, str):
|
@@ -1086,7 +1115,7 @@ UxeCp6
|
|
1086
1115
|
return self.__device_name
|
1087
1116
|
|
1088
1117
|
def get_endpoint(self):
|
1089
|
-
return self.__endpoint
|
1118
|
+
return self.__endpoint
|
1090
1119
|
|
1091
1120
|
def get_h2_endpoint(self):
|
1092
1121
|
raise LinkKit.StateError("not supported any more")
|
@@ -1102,40 +1131,27 @@ UxeCp6
|
|
1102
1131
|
|
1103
1132
|
def __to_str(self, payload):
|
1104
1133
|
if type(payload) is bytes:
|
1105
|
-
return str(payload,
|
1134
|
+
return str(payload, "utf-8")
|
1106
1135
|
else:
|
1107
1136
|
return payload
|
1108
1137
|
|
1109
|
-
def upload_file_sync(self, local_filename, remote_filename=None, over_write=True,
|
1110
|
-
timeout=None):
|
1138
|
+
def upload_file_sync(self, local_filename, remote_filename=None, over_write=True, timeout=None):
|
1111
1139
|
raise LinkKit.StateError("not supported any more")
|
1112
1140
|
|
1113
1141
|
def upload_file_async(self, local_filename, remote_filename=None, over_write=True):
|
1114
1142
|
raise LinkKit.StateError("not supported any more")
|
1115
1143
|
|
1116
|
-
def __upload_device_interface_info(self):
|
1144
|
+
def __upload_device_interface_info(self) -> int:
|
1117
1145
|
request_id = self.__get_thing_request_id()
|
1118
1146
|
payload = {
|
1119
1147
|
"id": request_id,
|
1120
1148
|
"version": "1.0",
|
1121
1149
|
"params": [
|
1122
|
-
{
|
1123
|
-
|
1124
|
-
|
1125
|
-
"attrValue": self.__sdk_program_language
|
1126
|
-
},
|
1127
|
-
{
|
1128
|
-
"domain": "SYSTEM",
|
1129
|
-
"attrKey": "SYS_LP_SDK_VERSION",
|
1130
|
-
"attrValue": self.__sdk_version
|
1131
|
-
},
|
1132
|
-
{
|
1133
|
-
"domain": "SYSTEM",
|
1134
|
-
"attrKey": "SYS_SDK_IF_INFO",
|
1135
|
-
"attrValue": self.__device_interface_info
|
1136
|
-
},
|
1150
|
+
{"domain": "SYSTEM", "attrKey": "SYS_SDK_LANGUAGE", "attrValue": self.__sdk_program_language},
|
1151
|
+
{"domain": "SYSTEM", "attrKey": "SYS_LP_SDK_VERSION", "attrValue": self.__sdk_version},
|
1152
|
+
{"domain": "SYSTEM", "attrKey": "SYS_SDK_IF_INFO", "attrValue": self.__device_interface_info},
|
1137
1153
|
],
|
1138
|
-
"method": "thing.deviceinfo.update"
|
1154
|
+
"method": "thing.deviceinfo.update",
|
1139
1155
|
}
|
1140
1156
|
with self.__device_info_mid_lock:
|
1141
1157
|
rc, mid = self.__mqtt_client.publish(self.__device_info_topic, json.dumps(payload), 0)
|
@@ -1145,13 +1161,15 @@ UxeCp6
|
|
1145
1161
|
else:
|
1146
1162
|
return 1
|
1147
1163
|
|
1148
|
-
def destruct(self):
|
1164
|
+
def destruct(self) -> None:
|
1149
1165
|
if self.__linkkit_state is LinkKit.LinkKitState.DESTRUCTED:
|
1150
1166
|
self.__link_log.info("LinkKit object has already destructed")
|
1151
1167
|
return
|
1152
1168
|
self.__link_log.debug("destruct enter")
|
1153
|
-
if
|
1154
|
-
|
1169
|
+
if (
|
1170
|
+
self.__linkkit_state == LinkKit.LinkKitState.CONNECTED
|
1171
|
+
or self.__linkkit_state == LinkKit.LinkKitState.CONNECTING
|
1172
|
+
):
|
1155
1173
|
self.__linkkit_state = LinkKit.LinkKitState.DESTRUCTING
|
1156
1174
|
if self.__connect_async_req:
|
1157
1175
|
with self.__worker_loop_exit_req_lock:
|
@@ -1167,9 +1185,8 @@ UxeCp6
|
|
1167
1185
|
self.__handler_task.stop()
|
1168
1186
|
self.__handler_task.wait_stop()
|
1169
1187
|
self.__linkkit_state = LinkKit.LinkKitState.DESTRUCTED
|
1170
|
-
pass
|
1171
1188
|
|
1172
|
-
def destroy(self):
|
1189
|
+
def destroy(self) -> None:
|
1173
1190
|
self.destruct()
|
1174
1191
|
|
1175
1192
|
def check_state(self):
|
@@ -1177,9 +1194,7 @@ UxeCp6
|
|
1177
1194
|
|
1178
1195
|
@staticmethod
|
1179
1196
|
def __generate_random_str(randomlength=16):
|
1180
|
-
"""
|
1181
|
-
generate radom string
|
1182
|
-
"""
|
1197
|
+
"""Generate radom string"""
|
1183
1198
|
random_str = ""
|
1184
1199
|
for i in range(randomlength):
|
1185
1200
|
random_str += random.choice(string.digits + string.ascii_letters)
|
@@ -1194,24 +1209,17 @@ UxeCp6
|
|
1194
1209
|
context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH, cadata=self.__aliyun_broker_ca_data)
|
1195
1210
|
sign_content = "deviceName%sproductKey%srandom%s" % (dn, pk, random_str)
|
1196
1211
|
sign = hmac.new(ps.encode("utf-8"), sign_content.encode("utf-8"), hashlib.sha256).hexdigest()
|
1197
|
-
post_data = {
|
1198
|
-
"productKey": pk,
|
1199
|
-
"deviceName": dn,
|
1200
|
-
"random": random_str,
|
1201
|
-
"sign": sign,
|
1202
|
-
"signMethod": "hmacsha256"
|
1203
|
-
}
|
1212
|
+
post_data = {"productKey": pk, "deviceName": dn, "random": random_str, "sign": sign, "signMethod": "hmacsha256"}
|
1204
1213
|
data = urllib.parse.urlencode(post_data)
|
1205
|
-
data = data.encode(
|
1214
|
+
data = data.encode("ascii")
|
1206
1215
|
request_url = "https://iot-auth.%s.aliyuncs.com/auth/register/device" % self.__host_name
|
1207
1216
|
with urllib.request.urlopen(request_url, data, context=context) as f:
|
1208
|
-
reply_data = f.read().decode(
|
1217
|
+
reply_data = f.read().decode("utf-8")
|
1209
1218
|
reply_obj = self.__load_json(reply_data)
|
1210
1219
|
if reply_obj["code"] == 200:
|
1211
1220
|
reply_obj_data = reply_obj["data"]
|
1212
1221
|
if reply_obj_data is not None:
|
1213
1222
|
return 0, reply_obj_data["deviceSecret"]
|
1214
|
-
pass
|
1215
1223
|
else:
|
1216
1224
|
return 1, reply_obj["message"]
|
1217
1225
|
|
@@ -1224,27 +1232,38 @@ UxeCp6
|
|
1224
1232
|
else:
|
1225
1233
|
securemode = 3
|
1226
1234
|
if self.__device_interface_info:
|
1227
|
-
sii_option =
|
1235
|
+
sii_option = "sii=%s," % (self.__device_interface_info)
|
1228
1236
|
else:
|
1229
|
-
sii_option =
|
1237
|
+
sii_option = ""
|
1230
1238
|
|
1231
1239
|
# 普通的一机一密认证方式
|
1232
1240
|
if self.__is_valid_str(self.__device_secret):
|
1233
|
-
client_id = "%s&%s|securemode=%d,signmethod=hmacsha1,ext=1,_ss=1,lan=%s,_v=%s,%stimestamp=%s|"
|
1234
|
-
|
1235
|
-
|
1241
|
+
client_id = "%s&%s|securemode=%d,signmethod=hmacsha1,ext=1,_ss=1,lan=%s,_v=%s,%stimestamp=%s|" % (
|
1242
|
+
self.__product_key,
|
1243
|
+
self.__device_name,
|
1244
|
+
securemode,
|
1245
|
+
self.__sdk_program_language,
|
1246
|
+
self.__sdk_version,
|
1247
|
+
sii_option,
|
1248
|
+
timestamp,
|
1249
|
+
)
|
1236
1250
|
username = self.__device_name + "&" + self.__product_key
|
1237
1251
|
# calc sign
|
1238
1252
|
sign_content = "clientId%sdeviceName%sproductKey%stimestamp%s" % (
|
1239
1253
|
self.__product_key + "&" + self.__device_name,
|
1240
1254
|
self.__device_name,
|
1241
1255
|
self.__product_key,
|
1242
|
-
timestamp
|
1243
|
-
|
1244
|
-
|
1256
|
+
timestamp,
|
1257
|
+
)
|
1258
|
+
password = hmac.new(
|
1259
|
+
self.__device_secret.encode("utf-8"), sign_content.encode("utf-8"), hashlib.sha1
|
1260
|
+
).hexdigest()
|
1245
1261
|
# 通过username, passwd, cilentid直连接入的方式
|
1246
|
-
elif
|
1247
|
-
|
1262
|
+
elif (
|
1263
|
+
self.__is_valid_str(self.__client_id)
|
1264
|
+
and self.__is_valid_str(self.__username)
|
1265
|
+
and self.__is_valid_str(self.__password)
|
1266
|
+
):
|
1248
1267
|
# 如果已经通过基于mqtt的免预注册一型一密协议获取到了client_id, user_name, password
|
1249
1268
|
client_id = self.__client_id
|
1250
1269
|
username = self.__username
|
@@ -1264,14 +1283,24 @@ UxeCp6
|
|
1264
1283
|
random_str = self.__generate_random_str(15)
|
1265
1284
|
if self.__is_valid_str(self.__instance_id):
|
1266
1285
|
client_id = "%s.%s|random=%s,authType=%s,securemode=2,signmethod=hmacsha256,instanceId=%s|" % (
|
1267
|
-
self.__device_name,
|
1286
|
+
self.__device_name,
|
1287
|
+
self.__product_key,
|
1288
|
+
random_str,
|
1289
|
+
auth_type_str,
|
1290
|
+
self.__instance_id,
|
1291
|
+
)
|
1268
1292
|
else:
|
1269
1293
|
client_id = "%s.%s|random=%s,authType=%s,securemode=2,signmethod=hmacsha256|" % (
|
1270
|
-
self.__device_name,
|
1294
|
+
self.__device_name,
|
1295
|
+
self.__product_key,
|
1296
|
+
random_str,
|
1297
|
+
auth_type_str,
|
1298
|
+
)
|
1271
1299
|
username = "%s&%s" % (self.__device_name, self.__product_key)
|
1272
1300
|
password_src = "deviceName%sproductKey%srandom%s" % (self.__device_name, self.__product_key, random_str)
|
1273
|
-
password = hmac.new(
|
1274
|
-
|
1301
|
+
password = hmac.new(
|
1302
|
+
self.__product_secret.encode("utf-8"), password_src.encode("utf-8"), hashlib.sha256
|
1303
|
+
).hexdigest()
|
1275
1304
|
else:
|
1276
1305
|
raise LinkKit.StateError("unknow auth error")
|
1277
1306
|
|
@@ -1281,9 +1310,9 @@ UxeCp6
|
|
1281
1310
|
mqtt_protocol_version = mqtt.MQTTv311
|
1282
1311
|
elif self.__mqtt_protocol == "MQTTv31":
|
1283
1312
|
mqtt_protocol_version = mqtt.MQTTv31
|
1284
|
-
self.__mqtt_client = mqtt.Client(
|
1285
|
-
|
1286
|
-
|
1313
|
+
self.__mqtt_client = mqtt.Client(
|
1314
|
+
client_id=client_id, clean_session=self.__mqtt_clean_session, protocol=mqtt_protocol_version
|
1315
|
+
)
|
1287
1316
|
|
1288
1317
|
if self.__link_log.is_enabled():
|
1289
1318
|
self.__mqtt_client.enable_logger(self.__PahoLog)
|
@@ -1316,8 +1345,7 @@ UxeCp6
|
|
1316
1345
|
elif self.__is_valid_str(self.__instance_id):
|
1317
1346
|
return self.__instance_id + ".mqtt.iothub.aliyuncs.com"
|
1318
1347
|
else:
|
1319
|
-
return "%s.iot-as-mqtt.%s.aliyuncs.com" %
|
1320
|
-
(self.__product_key, self.__host_name)
|
1348
|
+
return "%s.iot-as-mqtt.%s.aliyuncs.com" % (self.__product_key, self.__host_name)
|
1321
1349
|
|
1322
1350
|
def connect(self):
|
1323
1351
|
raise LinkKit.StateError("not supported")
|
@@ -1331,7 +1359,7 @@ UxeCp6
|
|
1331
1359
|
self.__worker_loop_exit_req = False
|
1332
1360
|
return self.__loop_thread.start(self.__loop_forever_internal)
|
1333
1361
|
|
1334
|
-
def disconnect(self):
|
1362
|
+
def disconnect(self) -> None:
|
1335
1363
|
self.__link_log.debug("disconnect")
|
1336
1364
|
if self.__linkkit_state is not LinkKit.LinkKitState.CONNECTED:
|
1337
1365
|
self.__link_log.info("already disconnected, return")
|
@@ -1443,8 +1471,8 @@ UxeCp6
|
|
1443
1471
|
else:
|
1444
1472
|
return 1, None
|
1445
1473
|
|
1446
|
-
def __make_rrpc_topic(self, topic):
|
1447
|
-
return
|
1474
|
+
def __make_rrpc_topic(self, topic) -> str:
|
1475
|
+
return "/ext/rrpc/+%s" % (topic)
|
1448
1476
|
|
1449
1477
|
def subscribe_rrpc_topic(self, topic):
|
1450
1478
|
if self.__linkkit_state is not LinkKit.LinkKitState.CONNECTED:
|
@@ -1536,13 +1564,14 @@ UxeCp6
|
|
1536
1564
|
else:
|
1537
1565
|
return 1, None
|
1538
1566
|
|
1539
|
-
def __on_internal_connect_safe(self, client, user_data, session_flag, rc):
|
1567
|
+
def __on_internal_connect_safe(self, client, user_data, session_flag, rc) -> None:
|
1540
1568
|
if rc == 0:
|
1541
1569
|
self.__reset_reconnect_wait()
|
1542
1570
|
self.__force_reconnect = False
|
1543
|
-
session_flag_internal = {
|
1544
|
-
self.__handler_task.post_message(
|
1545
|
-
|
1571
|
+
session_flag_internal = {"session present": session_flag}
|
1572
|
+
self.__handler_task.post_message(
|
1573
|
+
self.__handler_task_cmd_on_connect, (client, user_data, session_flag_internal, rc)
|
1574
|
+
)
|
1546
1575
|
|
1547
1576
|
def __loop_forever_internal(self):
|
1548
1577
|
self.__link_log.debug("enter")
|
@@ -1550,8 +1579,11 @@ UxeCp6
|
|
1550
1579
|
|
1551
1580
|
# 为了保持存量设备兼容,保留了基于https的需要预注册的一型一密的预注册
|
1552
1581
|
if not self.__is_valid_str(self.__device_secret) and self.__is_valid_str(self.__product_secret):
|
1553
|
-
lack_other_auth_info =
|
1554
|
-
|
1582
|
+
lack_other_auth_info = (
|
1583
|
+
not self.__is_valid_str(self.__username)
|
1584
|
+
or not self.__is_valid_str(self.__client_id)
|
1585
|
+
or not self.__is_valid_str(self.__password)
|
1586
|
+
)
|
1555
1587
|
if not self.__is_valid_str(self.__auth_type) and lack_other_auth_info:
|
1556
1588
|
rc, value = self.__dynamic_register_device()
|
1557
1589
|
try:
|
@@ -1575,8 +1607,9 @@ UxeCp6
|
|
1575
1607
|
return
|
1576
1608
|
|
1577
1609
|
try:
|
1578
|
-
self.__mqtt_client.connect_async(
|
1579
|
-
|
1610
|
+
self.__mqtt_client.connect_async(
|
1611
|
+
host=self.__host_name_internal, port=self.__mqtt_port, keepalive=self.__mqtt_keep_alive
|
1612
|
+
)
|
1580
1613
|
except Exception as e:
|
1581
1614
|
self.__link_log.error("__loop_forever_internal connect raise exception:" + str(e))
|
1582
1615
|
self.__linkkit_state = LinkKit.LinkKitState.INITIALIZED
|
@@ -1591,7 +1624,7 @@ UxeCp6
|
|
1591
1624
|
try:
|
1592
1625
|
self.__linkkit_state = LinkKit.LinkKitState.CONNECTING
|
1593
1626
|
self.__mqtt_client.reconnect()
|
1594
|
-
except
|
1627
|
+
except OSError as e:
|
1595
1628
|
self.__link_log.error(e)
|
1596
1629
|
# if isinstance(e, socket.timeout):
|
1597
1630
|
# self.__link_log.error("connect timeout")
|
@@ -1635,22 +1668,23 @@ UxeCp6
|
|
1635
1668
|
break
|
1636
1669
|
self.__reconnect_wait()
|
1637
1670
|
# 在mqtt + 一型一密免预注册的情况下,由于三元组不对,导致无法与服务器建连,因此需要退出,否则会一直重试
|
1638
|
-
if
|
1671
|
+
if self.__dynamic_register_nwl_flag == 1:
|
1639
1672
|
if self.__on_device_dynamic_register_nwl_reply is not None:
|
1640
|
-
self.__on_device_dynamic_register_nwl_reply(
|
1641
|
-
|
1673
|
+
self.__on_device_dynamic_register_nwl_reply(
|
1674
|
+
LinkKit.ErrorCode.DYNREG_AUTH_NWL_FAILED.value, None, None, None
|
1675
|
+
)
|
1642
1676
|
self.__linkkit_state = LinkKit.LinkKitState.DISCONNECTED
|
1643
1677
|
self.__dynamic_register_nwl_flag = 0
|
1644
1678
|
break
|
1645
1679
|
# 在mqtt + 一型一密预注册的情况下,由于三元组不对,导致无法与服务器建连,因此需要退出,否则会一直重试
|
1646
|
-
if
|
1680
|
+
if self.__dynamic_register_flag == 1:
|
1647
1681
|
if self.__on_device_dynamic_register is not None:
|
1648
1682
|
self.__on_device_dynamic_register(LinkKit.ErrorCode.DYNREG_AUTH_FAILED.value, None, None)
|
1649
1683
|
self.__linkkit_state = LinkKit.LinkKitState.DISCONNECTED
|
1650
1684
|
self.__dynamic_register_flag = 0
|
1651
1685
|
break
|
1652
1686
|
|
1653
|
-
def __clean_timeout_message(self):
|
1687
|
+
def __clean_timeout_message(self) -> None:
|
1654
1688
|
# self.__link_log.debug("__clean_timeout_message enter")
|
1655
1689
|
expire_timestamp = self.__timestamp() - self.__mqtt_request_timeout * 1000
|
1656
1690
|
with self.__thing_prop_post_mid_lock:
|
@@ -1668,28 +1702,27 @@ UxeCp6
|
|
1668
1702
|
self.__clean_timeout_message_item(self.__device_info_mid, expire_timestamp)
|
1669
1703
|
# self.__link_log.debug("__clean_timeout_message exit")
|
1670
1704
|
|
1671
|
-
def __clean_timeout_message_item(self, mids, expire_time):
|
1705
|
+
def __clean_timeout_message_item(self, mids, expire_time) -> None:
|
1672
1706
|
for mid in list(mids.keys()):
|
1673
1707
|
if mids[mid] < expire_time:
|
1674
1708
|
timestamp = mids.pop(mid)
|
1675
1709
|
self.__link_log.error("__clean_timeout_message_item pop:%r,timestamp:%r", mid, timestamp)
|
1676
1710
|
|
1677
|
-
def __reconnect_wait(self):
|
1711
|
+
def __reconnect_wait(self) -> None:
|
1678
1712
|
if self.__mqtt_auto_reconnect_sec == 0:
|
1679
1713
|
self.__mqtt_auto_reconnect_sec = self.__mqtt_auto_reconnect_min_sec
|
1680
1714
|
else:
|
1681
1715
|
self.__mqtt_auto_reconnect_sec = min(self.__mqtt_auto_reconnect_sec * 2, self.__mqtt_auto_reconnect_max_sec)
|
1682
1716
|
self.__mqtt_auto_reconnect_sec += random.randint(1, self.__mqtt_auto_reconnect_sec)
|
1683
1717
|
time.sleep(self.__mqtt_auto_reconnect_sec)
|
1684
|
-
pass
|
1685
1718
|
|
1686
|
-
def __reset_reconnect_wait(self):
|
1719
|
+
def __reset_reconnect_wait(self) -> None:
|
1687
1720
|
self.__mqtt_auto_reconnect_sec = 0
|
1688
1721
|
|
1689
|
-
def start_worker_loop(self):
|
1722
|
+
def start_worker_loop(self) -> None:
|
1690
1723
|
pass
|
1691
1724
|
|
1692
|
-
def thing_setup(self, file=None):
|
1725
|
+
def thing_setup(self, file=None) -> int:
|
1693
1726
|
if self.__linkkit_state is not LinkKit.LinkKitState.INITIALIZED:
|
1694
1727
|
raise LinkKit.StateError("not in INITIALIZED state")
|
1695
1728
|
if self.__thing_setup_state:
|
@@ -1699,7 +1732,7 @@ UxeCp6
|
|
1699
1732
|
self.__thing_setup_state = True
|
1700
1733
|
return 0
|
1701
1734
|
try:
|
1702
|
-
with open(file, encoding=
|
1735
|
+
with open(file, encoding="utf-8") as f:
|
1703
1736
|
tsl = json.load(f)
|
1704
1737
|
index = 0
|
1705
1738
|
while "events" in tsl and index < len(tsl["events"]):
|
@@ -1733,21 +1766,23 @@ UxeCp6
|
|
1733
1766
|
output_data_index += 1
|
1734
1767
|
else:
|
1735
1768
|
self.__thing_services.add(identifier)
|
1736
|
-
service_reply_topic = self.__thing_topic_service_pattern % (
|
1737
|
-
|
1738
|
-
|
1769
|
+
service_reply_topic = self.__thing_topic_service_pattern % (
|
1770
|
+
self.__product_key,
|
1771
|
+
self.__device_name,
|
1772
|
+
identifier + "_reply",
|
1773
|
+
)
|
1739
1774
|
self.__thing_topic_services_reply.add(service_reply_topic)
|
1740
1775
|
index += 1
|
1741
1776
|
|
1742
1777
|
for event in self.__thing_events:
|
1743
|
-
post_topic = self.__thing_topic_event_post_pattern %
|
1744
|
-
(self.__product_key, self.__device_name, event)
|
1778
|
+
post_topic = self.__thing_topic_event_post_pattern % (self.__product_key, self.__device_name, event)
|
1745
1779
|
self.__thing_topic_event_post[event] = post_topic
|
1746
1780
|
self.__thing_topic_event_post_reply.add(post_topic + "_reply")
|
1747
1781
|
# service topic
|
1748
1782
|
for service in self.__thing_services:
|
1749
|
-
self.__thing_topic_services.add(
|
1750
|
-
|
1783
|
+
self.__thing_topic_services.add(
|
1784
|
+
self.__thing_topic_service_pattern % (self.__product_key, self.__device_name, service)
|
1785
|
+
)
|
1751
1786
|
|
1752
1787
|
except Exception as e:
|
1753
1788
|
self.__link_log.info("file open error:" + str(e))
|
@@ -1786,10 +1821,11 @@ UxeCp6
|
|
1786
1821
|
return 1, None
|
1787
1822
|
request_id = self.__get_thing_request_id()
|
1788
1823
|
with self.__thing_update_device_info_up_mid_lock:
|
1789
|
-
rc, mid = self.__mqtt_client.publish(
|
1790
|
-
|
1791
|
-
|
1792
|
-
|
1824
|
+
rc, mid = self.__mqtt_client.publish(
|
1825
|
+
self.__thing_topic_update_device_info_up,
|
1826
|
+
self.__pack_alink_request(request_id, "thing.deviceinfo.update", payload),
|
1827
|
+
0,
|
1828
|
+
)
|
1793
1829
|
if rc == mqtt.MQTT_ERR_SUCCESS:
|
1794
1830
|
self.__thing_update_device_info_up_mid[mid] = self.__timestamp()
|
1795
1831
|
return rc, request_id
|
@@ -1803,10 +1839,11 @@ UxeCp6
|
|
1803
1839
|
return 1
|
1804
1840
|
request_id = self.__get_thing_request_id()
|
1805
1841
|
with self.__thing_delete_device_info_up_mid_lock:
|
1806
|
-
rc, mid = self.__mqtt_client.publish(
|
1807
|
-
|
1808
|
-
|
1809
|
-
|
1842
|
+
rc, mid = self.__mqtt_client.publish(
|
1843
|
+
self.__thing_topic_delete_device_info_up,
|
1844
|
+
self.__pack_alink_request(request_id, "thing.deviceinfo.delete", payload),
|
1845
|
+
0,
|
1846
|
+
)
|
1810
1847
|
if rc == mqtt.MQTT_ERR_SUCCESS:
|
1811
1848
|
self.__thing_delete_device_info_up_mid[mid] = self.__timestamp()
|
1812
1849
|
return rc, request_id
|
@@ -1818,7 +1855,7 @@ UxeCp6
|
|
1818
1855
|
return 1, None
|
1819
1856
|
|
1820
1857
|
payload = []
|
1821
|
-
for
|
1858
|
+
for k, v in tagMap.items():
|
1822
1859
|
payload.append({LinkKit.TAG_KEY: k, LinkKit.TAG_VALUE: v})
|
1823
1860
|
return self.thing_update_device_info(payload)
|
1824
1861
|
|
@@ -1833,12 +1870,7 @@ UxeCp6
|
|
1833
1870
|
return self.thing_delete_device_info(payload)
|
1834
1871
|
|
1835
1872
|
def __pack_alink_request(self, request_id, method, params):
|
1836
|
-
request = {
|
1837
|
-
"id": request_id,
|
1838
|
-
"version": "1.0",
|
1839
|
-
"params": params,
|
1840
|
-
"method": method
|
1841
|
-
}
|
1873
|
+
request = {"id": request_id, "version": "1.0", "params": params, "method": method}
|
1842
1874
|
return json.dumps(request)
|
1843
1875
|
|
1844
1876
|
def thing_answer_service(self, identifier, request_id, code, data=None):
|
@@ -1849,19 +1881,17 @@ UxeCp6
|
|
1849
1881
|
return 1
|
1850
1882
|
if data is None:
|
1851
1883
|
data = {}
|
1852
|
-
response = {
|
1853
|
-
"id": request_id,
|
1854
|
-
"code": code,
|
1855
|
-
"data": data
|
1856
|
-
}
|
1884
|
+
response = {"id": request_id, "code": code, "data": data}
|
1857
1885
|
|
1858
|
-
item = self.__pop_rrpc_service(
|
1886
|
+
item = self.__pop_rrpc_service("alink_" + str(request_id))
|
1859
1887
|
if item:
|
1860
|
-
service_reply_topic = item[
|
1888
|
+
service_reply_topic = item["topic"]
|
1861
1889
|
else:
|
1862
|
-
service_reply_topic = self.__thing_topic_service_pattern % (
|
1863
|
-
|
1864
|
-
|
1890
|
+
service_reply_topic = self.__thing_topic_service_pattern % (
|
1891
|
+
self.__product_key,
|
1892
|
+
self.__device_name,
|
1893
|
+
identifier + "_reply",
|
1894
|
+
)
|
1865
1895
|
with self.__thing_answer_service_mid_lock:
|
1866
1896
|
rc, mid = self.__mqtt_client.publish(service_reply_topic, json.dumps(response), 0)
|
1867
1897
|
if rc == mqtt.MQTT_ERR_SUCCESS:
|
@@ -1882,19 +1912,19 @@ UxeCp6
|
|
1882
1912
|
return str(self.__thing_request_value)
|
1883
1913
|
return None
|
1884
1914
|
|
1885
|
-
def __back_thing_request_id(self, post_id):
|
1915
|
+
def __back_thing_request_id(self, post_id) -> None:
|
1886
1916
|
with self.__thing_request_id_lock:
|
1887
1917
|
try:
|
1888
1918
|
self.__thing_request_id.pop(int(post_id))
|
1889
1919
|
except Exception as e:
|
1890
1920
|
self.__link_log.error("__back_thing_request_id pop:%r,%r" % (post_id, e))
|
1891
1921
|
|
1892
|
-
def __reset_thing_request_id(self):
|
1922
|
+
def __reset_thing_request_id(self) -> None:
|
1893
1923
|
with self.__thing_request_id_lock:
|
1894
1924
|
self.__thing_request_value = 0
|
1895
1925
|
self.__thing_request_id.clear()
|
1896
1926
|
|
1897
|
-
def __clean_thing_timeout_request_id(self):
|
1927
|
+
def __clean_thing_timeout_request_id(self) -> None:
|
1898
1928
|
with self.__thing_request_id_lock:
|
1899
1929
|
expire_timestamp = self.__timestamp() - self.__mqtt_request_timeout * 1000
|
1900
1930
|
for request_id in list(self.__thing_request_id.keys()):
|
@@ -1923,7 +1953,7 @@ UxeCp6
|
|
1923
1953
|
"params": {
|
1924
1954
|
"value": params,
|
1925
1955
|
},
|
1926
|
-
"method": "thing.event.%s.post" % event
|
1956
|
+
"method": "thing.event.%s.post" % event,
|
1927
1957
|
}
|
1928
1958
|
with self.__thing_event_post_mid_lock:
|
1929
1959
|
event_topic = self.__thing_topic_event_post[event]
|
@@ -1946,12 +1976,7 @@ UxeCp6
|
|
1946
1976
|
request_id = self.__get_thing_request_id()
|
1947
1977
|
if request_id is None:
|
1948
1978
|
return 1, None
|
1949
|
-
request = {
|
1950
|
-
"id": request_id,
|
1951
|
-
"version": "1.0",
|
1952
|
-
"params": request_params,
|
1953
|
-
"method": "thing.event.property.post"
|
1954
|
-
}
|
1979
|
+
request = {"id": request_id, "version": "1.0", "params": request_params, "method": "thing.event.property.post"}
|
1955
1980
|
with self.__thing_prop_post_mid_lock:
|
1956
1981
|
rc, mid = self.__mqtt_client.publish(self.__thing_topic_prop_post, json.dumps(request), 1)
|
1957
1982
|
if rc == mqtt.MQTT_ERR_SUCCESS:
|
@@ -1959,9 +1984,8 @@ UxeCp6
|
|
1959
1984
|
return 0, request_id
|
1960
1985
|
else:
|
1961
1986
|
return 1, None
|
1962
|
-
pass
|
1963
1987
|
|
1964
|
-
def __on_internal_async_message(self, message):
|
1988
|
+
def __on_internal_async_message(self, message) -> None:
|
1965
1989
|
self.__link_log.debug("__on_internal_async_message topic:%r" % message.topic)
|
1966
1990
|
triggered_flag = 0
|
1967
1991
|
|
@@ -1969,11 +1993,7 @@ UxeCp6
|
|
1969
1993
|
payload = self.__load_json(message.payload)
|
1970
1994
|
params = payload["params"]
|
1971
1995
|
try:
|
1972
|
-
reply = {
|
1973
|
-
"id": payload["id"],
|
1974
|
-
"code": 200,
|
1975
|
-
"data": {}
|
1976
|
-
}
|
1996
|
+
reply = {"id": payload["id"], "code": 200, "data": {}}
|
1977
1997
|
with self.__thing_prop_set_reply_mid_lock:
|
1978
1998
|
rc, mid = self.__mqtt_client.publish(self.__thing_topic_prop_set_reply, json.dumps(reply), 1)
|
1979
1999
|
if rc == 0:
|
@@ -1982,7 +2002,6 @@ UxeCp6
|
|
1982
2002
|
self.__link_log.info("prop changed reply success")
|
1983
2003
|
else:
|
1984
2004
|
self.__link_log.info("prop changed reply fail")
|
1985
|
-
pass
|
1986
2005
|
if self.__on_thing_prop_changed is not None:
|
1987
2006
|
triggered_flag = 1
|
1988
2007
|
self.__on_thing_prop_changed(params, self.__user_data)
|
@@ -2003,7 +2022,6 @@ UxeCp6
|
|
2003
2022
|
self.__on_thing_device_info_update(request_id, code, data, reply_message, self.__user_data)
|
2004
2023
|
except Exception as e:
|
2005
2024
|
self.__link_log.error("__on_thing_device_info_update process raise exception:%s" % e)
|
2006
|
-
pass
|
2007
2025
|
elif message.topic == self.__thing_topic_prop_post_reply:
|
2008
2026
|
payload = self.__load_json(message.payload)
|
2009
2027
|
request_id = payload["id"]
|
@@ -2017,11 +2035,10 @@ UxeCp6
|
|
2017
2035
|
except Exception as e:
|
2018
2036
|
self.__link_log.error("on_thing_prop_post raise exception:%s" % e)
|
2019
2037
|
self.__back_thing_request_id(request_id)
|
2020
|
-
pass
|
2021
2038
|
elif message.topic == self.__thing_topic_prop_get:
|
2022
2039
|
pass
|
2023
2040
|
elif message.topic in self.__thing_topic_event_post_reply:
|
2024
|
-
event = message.topic.split(
|
2041
|
+
event = message.topic.split("/", 7)[6]
|
2025
2042
|
payload = self.__load_json(message.payload)
|
2026
2043
|
request_id = payload["id"]
|
2027
2044
|
code = payload["code"]
|
@@ -2035,9 +2052,8 @@ UxeCp6
|
|
2035
2052
|
except Exception as e:
|
2036
2053
|
self.__link_log.error("on_thing_event_post raise exception:%s" % e)
|
2037
2054
|
self.__back_thing_request_id(request_id)
|
2038
|
-
pass
|
2039
2055
|
elif message.topic in self.__thing_topic_services:
|
2040
|
-
identifier = message.topic.split(
|
2056
|
+
identifier = message.topic.split("/", 6)[6]
|
2041
2057
|
payload = self.__load_json(message.payload)
|
2042
2058
|
try:
|
2043
2059
|
request_id = payload["id"]
|
@@ -2061,7 +2077,6 @@ UxeCp6
|
|
2061
2077
|
self.__on_thing_raw_data_post(message.payload, self.__user_data)
|
2062
2078
|
except Exception as e:
|
2063
2079
|
self.__link_log.error("on_thing_raw_post_data process raise exception:%s" % e)
|
2064
|
-
pass
|
2065
2080
|
elif message.topic == self.__thing_topic_update_device_info_reply:
|
2066
2081
|
try:
|
2067
2082
|
if self.__on_thing_device_info_update is not None:
|
@@ -2070,11 +2085,10 @@ UxeCp6
|
|
2070
2085
|
request_id = payload["id"]
|
2071
2086
|
code = payload["code"]
|
2072
2087
|
data = payload["data"]
|
2073
|
-
msg = payload[
|
2088
|
+
msg = payload["message"]
|
2074
2089
|
self.__on_thing_device_info_update(request_id, code, data, msg, self.__user_data)
|
2075
2090
|
except Exception as e:
|
2076
2091
|
self.__link_log.error("__on_thing_device_info_update process raise exception:%s" % e)
|
2077
|
-
pass
|
2078
2092
|
elif message.topic == self.__thing_topic_delete_device_info_reply:
|
2079
2093
|
try:
|
2080
2094
|
if self.__on_thing_device_info_delete is not None:
|
@@ -2083,11 +2097,10 @@ UxeCp6
|
|
2083
2097
|
request_id = payload["id"]
|
2084
2098
|
code = payload["code"]
|
2085
2099
|
data = payload["data"]
|
2086
|
-
msg = payload[
|
2100
|
+
msg = payload["message"]
|
2087
2101
|
self.__on_thing_device_info_delete(request_id, code, data, msg, self.__user_data)
|
2088
2102
|
except Exception as e:
|
2089
2103
|
self.__link_log.error("__on_thing_device_info_update process raise exception:%s" % e)
|
2090
|
-
pass
|
2091
2104
|
elif message.topic == self.__thing_topic_shadow_get:
|
2092
2105
|
self.__try_parse_try_shadow(message.payload)
|
2093
2106
|
try:
|
@@ -2096,10 +2109,8 @@ UxeCp6
|
|
2096
2109
|
self.__on_thing_shadow_get(self.__load_json(message.payload), self.__user_data)
|
2097
2110
|
except Exception as e:
|
2098
2111
|
self.__link_log.error("__on_thing_shadow_get process raise exception:%s" % e)
|
2099
|
-
pass
|
2100
2112
|
elif message.topic.startswith("/ext/rrpc/"):
|
2101
2113
|
triggered_flag = self.__try_parse_rrpc_topic(message)
|
2102
|
-
pass
|
2103
2114
|
elif message.topic == self.__gateway_topic_topo_change:
|
2104
2115
|
try:
|
2105
2116
|
payload = self.__load_json(message.payload)
|
@@ -2110,7 +2121,6 @@ UxeCp6
|
|
2110
2121
|
self.__on_gateway_topo_change(request_id, params, self.__user_data)
|
2111
2122
|
except Exception as e:
|
2112
2123
|
self.__link_log.error("__on_gateway_topo_change process raise exception:%s" % e)
|
2113
|
-
pass
|
2114
2124
|
elif message.topic == self.__gateway_topic_add_subdev_topo_reply:
|
2115
2125
|
try:
|
2116
2126
|
payload = self.__load_json(message.payload)
|
@@ -2124,7 +2134,6 @@ UxeCp6
|
|
2124
2134
|
self.__on_gateway_add_subdev_topo_reply(request_id, code, data, msg, self.__user_data)
|
2125
2135
|
except Exception as e:
|
2126
2136
|
self.__link_log.error("__on_gateway_add_subdev_topo_reply process raise exception:%s" % e)
|
2127
|
-
pass
|
2128
2137
|
elif message.topic == self.__gateway_topic_delete_subdev_topo_reply:
|
2129
2138
|
try:
|
2130
2139
|
payload = self.__load_json(message.payload)
|
@@ -2138,7 +2147,6 @@ UxeCp6
|
|
2138
2147
|
self.__on_gateway_delete_subdev_topo_reply(request_id, code, data, msg, self.__user_data)
|
2139
2148
|
except Exception as e:
|
2140
2149
|
self.__link_log.error("__on_gateway_delete_subdev_topo_reply process raise exception:%s" % e)
|
2141
|
-
pass
|
2142
2150
|
elif message.topic == self.__gateway_topic_login_subdev_reply:
|
2143
2151
|
try:
|
2144
2152
|
payload = self.__load_json(message.payload)
|
@@ -2152,7 +2160,6 @@ UxeCp6
|
|
2152
2160
|
self.__on_gateway_login_subdev_reply(request_id, code, data, msg, self.__user_data)
|
2153
2161
|
except Exception as e:
|
2154
2162
|
self.__link_log.error("__on_gateway_login_subdev_reply process raise exception:%s" % e)
|
2155
|
-
pass
|
2156
2163
|
elif message.topic == self.__gateway_topic_logout_subdev_reply:
|
2157
2164
|
try:
|
2158
2165
|
payload = self.__load_json(message.payload)
|
@@ -2166,7 +2173,6 @@ UxeCp6
|
|
2166
2173
|
self.__on_gateway_logout_subdev_reply(request_id, code, data, msg, self.__user_data)
|
2167
2174
|
except Exception as e:
|
2168
2175
|
self.__link_log.error("__on_gateway_logout_subdev_reply process raise exception:%s" % e)
|
2169
|
-
pass
|
2170
2176
|
elif message.topic == self.__gateway_topic_register_subdev_reply:
|
2171
2177
|
try:
|
2172
2178
|
payload = self.__load_json(message.payload)
|
@@ -2180,21 +2186,19 @@ UxeCp6
|
|
2180
2186
|
self.__on_gateway_register_subdev_reply(request_id, code, data, msg, self.__user_data)
|
2181
2187
|
except Exception as e:
|
2182
2188
|
self.__link_log.error("__on_gateway_register_subdev_reply process raise exception:%s" % e)
|
2183
|
-
pass
|
2184
2189
|
elif message.topic == self.__gateway_topic_product_register_subdev_reply:
|
2185
2190
|
try:
|
2186
2191
|
payload = self.__load_json(message.payload)
|
2187
2192
|
request_id = payload["id"]
|
2188
2193
|
code = payload["code"]
|
2189
2194
|
data = payload["data"]
|
2190
|
-
msg = payload[
|
2195
|
+
msg = payload["message"]
|
2191
2196
|
self.__back_thing_request_id(request_id)
|
2192
2197
|
if self.__on_gateway_product_register_subdev_reply is not None:
|
2193
2198
|
triggered_flag = 1
|
2194
2199
|
self.__on_gateway_product_register_subdev_reply(request_id, code, data, msg, self.__user_data)
|
2195
2200
|
except Exception as e:
|
2196
2201
|
self.__link_log.error("__on_gateway_product_register_subdev_reply process raise exception:%s" % e)
|
2197
|
-
pass
|
2198
2202
|
elif message.topic == self.__dynamic_register_topic:
|
2199
2203
|
try:
|
2200
2204
|
payload = self.__load_json(message.payload)
|
@@ -2206,28 +2210,29 @@ UxeCp6
|
|
2206
2210
|
self.__on_device_dynamic_register(LinkKit.ErrorCode.SUCCESS.value, device_secret, None)
|
2207
2211
|
except Exception as e:
|
2208
2212
|
self.__link_log.error("__on_device_dynamic_register process raise exception:%s" % e)
|
2209
|
-
pass
|
2210
2213
|
|
2211
2214
|
elif message.topic == self.__dynamic_register_nwl_topic:
|
2212
2215
|
# 一型一密免动态注册获取到username和token
|
2213
2216
|
try:
|
2214
2217
|
payload = self.__load_json(message.payload)
|
2215
2218
|
client_id = payload["clientId"]
|
2216
|
-
client_id = client_id +
|
2217
|
-
self.__sdk_program_language,
|
2219
|
+
client_id = client_id + "|authType=connwl,securemode=-2,_ss=1,ext=3,lan=%s,_v=%s|" % (
|
2220
|
+
self.__sdk_program_language,
|
2221
|
+
self.__sdk_version,
|
2222
|
+
)
|
2218
2223
|
product_key = payload["productKey"]
|
2219
2224
|
device_name = payload["deviceName"]
|
2220
|
-
username = device_name +
|
2221
|
-
password = payload[
|
2225
|
+
username = device_name + "&" + product_key
|
2226
|
+
password = payload["deviceToken"]
|
2222
2227
|
self.disconnect()
|
2223
2228
|
self.__dynamic_register_nwl_flag = 0
|
2224
2229
|
if self.__on_device_dynamic_register_nwl_reply is not None:
|
2225
2230
|
triggered_flag = 1
|
2226
|
-
self.__on_device_dynamic_register_nwl_reply(
|
2227
|
-
|
2231
|
+
self.__on_device_dynamic_register_nwl_reply(
|
2232
|
+
LinkKit.ErrorCode.SUCCESS.value, client_id, username, password
|
2233
|
+
)
|
2228
2234
|
except Exception as e:
|
2229
2235
|
self.__link_log.error("__on_device_dynamic_register_nwl_reply process raise exception:%s" % e)
|
2230
|
-
pass
|
2231
2236
|
elif message.topic == self.__ota_push_topic or message.topic == self.__ota_pull_reply_topic:
|
2232
2237
|
try:
|
2233
2238
|
payload = json.loads(self.__to_str(message.payload))
|
@@ -2244,8 +2249,13 @@ UxeCp6
|
|
2244
2249
|
extra = download_info.setdefault("extData", "")
|
2245
2250
|
module = download_info.setdefault("module", "default")
|
2246
2251
|
|
2247
|
-
if
|
2248
|
-
|
2252
|
+
if (
|
2253
|
+
not self.__is_valid_str(url)
|
2254
|
+
or not self.__is_valid_str(version)
|
2255
|
+
or not self.__is_valid_str(size)
|
2256
|
+
or not self.__is_valid_str(sign_method)
|
2257
|
+
or not self.__is_valid_str(sign)
|
2258
|
+
):
|
2249
2259
|
self.__link_log.error("invalid download params")
|
2250
2260
|
return
|
2251
2261
|
|
@@ -2257,11 +2267,11 @@ UxeCp6
|
|
2257
2267
|
|
2258
2268
|
if self.__on_ota_message_arrived is not None:
|
2259
2269
|
triggered_flag = 1
|
2260
|
-
self.__on_ota_message_arrived(
|
2261
|
-
|
2270
|
+
self.__on_ota_message_arrived(
|
2271
|
+
ota_notice_type, version, size, url, sign_method, sign, module, str(extra)
|
2272
|
+
)
|
2262
2273
|
except Exception as e:
|
2263
2274
|
self.__link_log.error("__on_ota_message_arrived process raise exception:%s" % e)
|
2264
|
-
pass
|
2265
2275
|
|
2266
2276
|
if triggered_flag == 1:
|
2267
2277
|
return
|
@@ -2271,10 +2281,8 @@ UxeCp6
|
|
2271
2281
|
self.__on_topic_message(message.topic, message.payload, message.qos, self.__user_data)
|
2272
2282
|
except Exception as e:
|
2273
2283
|
self.__link_log.error("on_topic_message process raise exception:%s" % e)
|
2274
|
-
pass
|
2275
2284
|
else:
|
2276
2285
|
self.__link_log.error("receive unscubscibe topic : %s" % message.topic)
|
2277
|
-
pass
|
2278
2286
|
|
2279
2287
|
def query_ota_firmware(self, module=None):
|
2280
2288
|
request_id = self.__get_thing_request_id()
|
@@ -2282,27 +2290,25 @@ UxeCp6
|
|
2282
2290
|
request_id = "1"
|
2283
2291
|
|
2284
2292
|
if self.__is_valid_str(module):
|
2285
|
-
payload =
|
2293
|
+
payload = '{"id": %s, "params": {"module": "%s"}}' % (request_id, module)
|
2286
2294
|
else:
|
2287
|
-
payload =
|
2288
|
-
pass
|
2295
|
+
payload = '{"id": %s, "params": {}}' % request_id
|
2289
2296
|
|
2290
2297
|
rc = self.__mqtt_client.publish(self.__ota_pull_topic, payload, 0)
|
2291
2298
|
if rc == mqtt.MQTT_ERR_SUCCESS:
|
2292
2299
|
return 0
|
2293
2300
|
else:
|
2294
2301
|
return LinkKit.ErrorCode.OTA_PUB_FAILED
|
2295
|
-
pass
|
2296
2302
|
|
2297
2303
|
def __cal_file_sign(self, filename, sign_method):
|
2298
|
-
if "Md5"
|
2304
|
+
if sign_method == "Md5":
|
2299
2305
|
hash_tool = hashlib.md5()
|
2300
|
-
elif "SHA256"
|
2306
|
+
elif sign_method == "SHA256":
|
2301
2307
|
hash_tool = hashlib.sha256()
|
2302
2308
|
else:
|
2303
2309
|
return LinkKit.ErrorCode.OTA_INVALID_SIGN_METHOD, -1
|
2304
2310
|
|
2305
|
-
with open(filename,
|
2311
|
+
with open(filename, "rb") as f:
|
2306
2312
|
while True:
|
2307
2313
|
chunk = f.read(4096)
|
2308
2314
|
if not chunk:
|
@@ -2316,9 +2322,9 @@ UxeCp6
|
|
2316
2322
|
|
2317
2323
|
request_id = "1"
|
2318
2324
|
if self.__is_valid_str(module):
|
2319
|
-
payload =
|
2325
|
+
payload = '{"id": %s,"params": {"version":"%s","module": "%s"}}' % (request_id, version, module)
|
2320
2326
|
else:
|
2321
|
-
payload =
|
2327
|
+
payload = '{"id": %s, "params": {"version": "%s"}}' % (request_id, version)
|
2322
2328
|
|
2323
2329
|
rc = self.__mqtt_client.publish(self.__ota_report_version_topic, payload, 0)
|
2324
2330
|
if rc == mqtt.MQTT_ERR_SUCCESS:
|
@@ -2327,8 +2333,12 @@ UxeCp6
|
|
2327
2333
|
return LinkKit.ErrorCode.OTA_PUB_FAILED
|
2328
2334
|
|
2329
2335
|
def download_ota_firmware(self, url, local_path, sign_method, sign, download_step=10 * 1024):
|
2330
|
-
if
|
2331
|
-
|
2336
|
+
if (
|
2337
|
+
not self.__is_valid_str(url)
|
2338
|
+
or not self.__is_valid_str(local_path)
|
2339
|
+
or not self.__is_valid_str(sign_method)
|
2340
|
+
or not self.__is_valid_str(sign)
|
2341
|
+
):
|
2332
2342
|
return LinkKit.ErrorCode.OTA_INVALID_PARAM
|
2333
2343
|
|
2334
2344
|
context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH, cadata=self.__aliyun_broker_ca_data)
|
@@ -2337,16 +2347,16 @@ UxeCp6
|
|
2337
2347
|
conn = urllib.request.urlopen(url, context=context)
|
2338
2348
|
except Exception as e:
|
2339
2349
|
# Return code error (e.g. 404, 501, ...)
|
2340
|
-
self.__link_log.error(
|
2350
|
+
self.__link_log.error("HTTPError: {} %s" % e)
|
2341
2351
|
return LinkKit.ErrorCode.OTA_INVALID_URL
|
2342
2352
|
else:
|
2343
2353
|
# 200
|
2344
|
-
self.__link_log.info(
|
2354
|
+
self.__link_log.info("https return 200")
|
2345
2355
|
|
2346
2356
|
try:
|
2347
|
-
file = open(local_path,
|
2348
|
-
except
|
2349
|
-
self.__link_log.error(
|
2357
|
+
file = open(local_path, "wb")
|
2358
|
+
except OSError as e:
|
2359
|
+
self.__link_log.error("open file error: {}" + e.filename)
|
2350
2360
|
return LinkKit.ErrorCode.OTA_INVALID_PATH
|
2351
2361
|
else:
|
2352
2362
|
# Download ota file
|
@@ -2355,7 +2365,7 @@ UxeCp6
|
|
2355
2365
|
try:
|
2356
2366
|
data = conn.read(download_step)
|
2357
2367
|
except Exception as e:
|
2358
|
-
self.__link_log.error(
|
2368
|
+
self.__link_log.error("download exception %s" % e)
|
2359
2369
|
return LinkKit.ErrorCode.OTA_DOWNLOAD_FAIL
|
2360
2370
|
else:
|
2361
2371
|
if len(data) <= 0:
|
@@ -2365,15 +2375,15 @@ UxeCp6
|
|
2365
2375
|
|
2366
2376
|
# Compare checksum
|
2367
2377
|
ret, firmware_sign = self.__cal_file_sign(local_path, sign_method)
|
2368
|
-
if LinkKit.ErrorCode.SUCCESS
|
2369
|
-
self.__link_log.info(
|
2378
|
+
if ret == LinkKit.ErrorCode.SUCCESS and firmware_sign == sign:
|
2379
|
+
self.__link_log.info("sign match")
|
2370
2380
|
return LinkKit.ErrorCode.SUCCESS
|
2371
2381
|
else:
|
2372
|
-
self.__link_log.error(
|
2382
|
+
self.__link_log.error("sign mismatch, expect:" + firmware_sign + ", actually:" + sign)
|
2373
2383
|
return LinkKit.ErrorCode.OTA_DIGEST_MISMATCH
|
2374
2384
|
|
2375
2385
|
def __parse_raw_topic(self, topic):
|
2376
|
-
return re.search(
|
2386
|
+
return re.search("/ext/rrpc/.*?(/.*)", topic).group(1)
|
2377
2387
|
|
2378
2388
|
def __tidy_topic(self, topic):
|
2379
2389
|
if topic == None:
|
@@ -2381,27 +2391,27 @@ UxeCp6
|
|
2381
2391
|
topic = topic.strip()
|
2382
2392
|
if len(topic) == 0:
|
2383
2393
|
return None
|
2384
|
-
if topic[0] !=
|
2385
|
-
topic =
|
2394
|
+
if topic[0] != "/":
|
2395
|
+
topic = "/" + topic
|
2386
2396
|
return topic
|
2387
2397
|
|
2388
|
-
def __push_rrpc_service(self, item):
|
2398
|
+
def __push_rrpc_service(self, item) -> None:
|
2389
2399
|
with self.__user_rrpc_request_ids_lock:
|
2390
2400
|
if len(self.__user_rrpc_request_ids) > self.__user_rrpc_request_max_len:
|
2391
2401
|
removed_item = self.__user_rrpc_request_ids.pop(0)
|
2392
|
-
del self.__user_rrpc_request_id_index_map[removed_item[
|
2402
|
+
del self.__user_rrpc_request_id_index_map[removed_item["id"]]
|
2393
2403
|
|
2394
2404
|
self.__user_rrpc_request_ids.append(item)
|
2395
|
-
self.__user_rrpc_request_id_index_map[item[
|
2405
|
+
self.__user_rrpc_request_id_index_map[item["id"]] = 0
|
2396
2406
|
|
2397
2407
|
def __pop_rrpc_service(self, id):
|
2398
2408
|
with self.__user_rrpc_request_ids_lock:
|
2399
2409
|
if id not in self.__user_rrpc_request_id_index_map:
|
2400
2410
|
return None
|
2401
2411
|
del self.__user_rrpc_request_id_index_map[id]
|
2402
|
-
for index in range(
|
2412
|
+
for index in range(len(self.__user_rrpc_request_ids)):
|
2403
2413
|
item = self.__user_rrpc_request_ids[index]
|
2404
|
-
if item[
|
2414
|
+
if item["id"] == id:
|
2405
2415
|
del self.__user_rrpc_request_ids[index]
|
2406
2416
|
return item
|
2407
2417
|
return None
|
@@ -2411,24 +2421,29 @@ UxeCp6
|
|
2411
2421
|
if item == None:
|
2412
2422
|
self.__link_log.error("answer_rrpc_topic, the id does not exist: %s" % id)
|
2413
2423
|
return 1, None
|
2414
|
-
rc, mid = self.__mqtt_client.publish(item[
|
2415
|
-
self.__link_log.debug(
|
2424
|
+
rc, mid = self.__mqtt_client.publish(item["topic"], response, 0)
|
2425
|
+
self.__link_log.debug("reply topic:%s" % item["topic"])
|
2416
2426
|
return rc, mid
|
2417
2427
|
|
2418
2428
|
def __try_parse_rrpc_topic(self, message):
|
2419
|
-
self.__link_log.debug(
|
2429
|
+
self.__link_log.debug("receive a rrpc topic:%s" % message.topic)
|
2420
2430
|
raw_topic = self.__parse_raw_topic(message.topic)
|
2421
2431
|
triggered = 0
|
2422
2432
|
# if it is a service, log it...
|
2423
|
-
if raw_topic.startswith(
|
2424
|
-
identifier = raw_topic.split(
|
2433
|
+
if raw_topic.startswith("/sys") and raw_topic in self.__thing_topic_services:
|
2434
|
+
identifier = raw_topic.split("/", 6)[6]
|
2425
2435
|
payload = self.__load_json(self.__to_str(message.payload))
|
2426
2436
|
try:
|
2427
2437
|
request_id = payload["id"]
|
2428
2438
|
params = payload["params"]
|
2429
|
-
item_id =
|
2430
|
-
item = {
|
2431
|
-
|
2439
|
+
item_id = "alink_" + request_id
|
2440
|
+
item = {
|
2441
|
+
"id": item_id,
|
2442
|
+
"request_id": request_id,
|
2443
|
+
"payload": payload,
|
2444
|
+
"identifier": identifier,
|
2445
|
+
"topic": message.topic,
|
2446
|
+
}
|
2432
2447
|
self.__push_rrpc_service(item)
|
2433
2448
|
if self.__on_thing_call_service is not None:
|
2434
2449
|
triggered = 1
|
@@ -2446,20 +2461,17 @@ UxeCp6
|
|
2446
2461
|
if not self.__on_topic_rrpc_message:
|
2447
2462
|
return
|
2448
2463
|
try:
|
2449
|
-
rrpc_id = message.topic.split(
|
2450
|
-
item_id =
|
2451
|
-
item = {
|
2464
|
+
rrpc_id = message.topic.split("/", 4)[3]
|
2465
|
+
item_id = "rrpc_" + rrpc_id
|
2466
|
+
item = {"id": item_id, "payload": message.payload, "topic": message.topic}
|
2452
2467
|
self.__push_rrpc_service(item)
|
2453
|
-
self.__on_topic_rrpc_message(rrpc_id, message.topic,
|
2454
|
-
message.payload,
|
2455
|
-
message.qos,
|
2456
|
-
self.__user_data)
|
2468
|
+
self.__on_topic_rrpc_message(rrpc_id, message.topic, message.payload, message.qos, self.__user_data)
|
2457
2469
|
# self.__mqtt_client.publish(message.topic, response, 0)
|
2458
2470
|
# self.__link_log.debug('reply topic:%s' % message.topic)
|
2459
2471
|
except Exception as e:
|
2460
2472
|
self.__link_log.error("on_topic_rrpc_message process raise exception:%r" % e)
|
2461
2473
|
|
2462
|
-
def __try_parse_try_shadow(self, payload):
|
2474
|
+
def __try_parse_try_shadow(self, payload) -> None:
|
2463
2475
|
try:
|
2464
2476
|
self.__latest_shadow.set_latest_recevied_time(self.__timestamp())
|
2465
2477
|
self.__latest_shadow.set_latest_recevied_payload(payload)
|
@@ -2467,36 +2479,32 @@ UxeCp6
|
|
2467
2479
|
# parse the pay load
|
2468
2480
|
msg = self.__load_json(payload)
|
2469
2481
|
# set version
|
2470
|
-
if
|
2471
|
-
self.__latest_shadow.set_version(msg[
|
2472
|
-
elif
|
2473
|
-
self.__latest_shadow.set_version(msg[
|
2482
|
+
if "version" in msg:
|
2483
|
+
self.__latest_shadow.set_version(msg["version"])
|
2484
|
+
elif "payload" in msg and "version" in msg["payload"]:
|
2485
|
+
self.__latest_shadow.set_version(msg["payload"]["version"])
|
2474
2486
|
|
2475
2487
|
# set timestamp
|
2476
|
-
if
|
2477
|
-
self.__latest_shadow.set_timestamp(msg[
|
2478
|
-
elif
|
2479
|
-
self.__latest_shadow.set_timestamp(msg[
|
2488
|
+
if "timestamp" in msg:
|
2489
|
+
self.__latest_shadow.set_timestamp(msg["timestamp"])
|
2490
|
+
elif "payload" in msg and "timestamp" in msg["payload"]:
|
2491
|
+
self.__latest_shadow.set_timestamp(msg["payload"]["timestamp"])
|
2480
2492
|
|
2481
2493
|
# set state and metadata
|
2482
|
-
if
|
2483
|
-
if
|
2484
|
-
self.__latest_shadow.set_state(msg[
|
2485
|
-
if
|
2486
|
-
self.__latest_shadow.set_metadata(msg[
|
2487
|
-
except Exception
|
2494
|
+
if "payload" in msg and msg["payload"]["status"] == "success":
|
2495
|
+
if "state" in msg["payload"]:
|
2496
|
+
self.__latest_shadow.set_state(msg["payload"]["state"])
|
2497
|
+
if "metadata" in msg["payload"]:
|
2498
|
+
self.__latest_shadow.set_metadata(msg["payload"]["metadata"])
|
2499
|
+
except Exception:
|
2488
2500
|
pass
|
2489
2501
|
|
2490
2502
|
def thing_update_shadow(self, reported, version):
|
2491
|
-
request = {
|
2492
|
-
'state': {'reported': reported},
|
2493
|
-
'method': 'update',
|
2494
|
-
'version': version
|
2495
|
-
}
|
2503
|
+
request = {"state": {"reported": reported}, "method": "update", "version": version}
|
2496
2504
|
return self.__thing_update_shadow(request)
|
2497
2505
|
|
2498
2506
|
def thing_get_shadow(self):
|
2499
|
-
request = {
|
2507
|
+
request = {"method": "get"}
|
2500
2508
|
return self.__thing_update_shadow(request)
|
2501
2509
|
|
2502
2510
|
def local_get_latest_shadow(self):
|
@@ -2509,57 +2517,54 @@ UxeCp6
|
|
2509
2517
|
if not self.__thing_setup_state or not self.__thing_enable_state:
|
2510
2518
|
return 1, None
|
2511
2519
|
with self.__thing_shadow_mid_lock:
|
2512
|
-
rc, mid = self.__mqtt_client.publish(self.__thing_topic_shadow_update,
|
2513
|
-
json.dumps(request), 1)
|
2520
|
+
rc, mid = self.__mqtt_client.publish(self.__thing_topic_shadow_update, json.dumps(request), 1)
|
2514
2521
|
if rc == mqtt.MQTT_ERR_SUCCESS:
|
2515
2522
|
self.__thing_shadow_mid[mid] = self.__timestamp()
|
2516
2523
|
return 0, mid
|
2517
2524
|
else:
|
2518
2525
|
return 1, None
|
2519
2526
|
|
2520
|
-
def __on_internal_message(self, client, user_data, message):
|
2527
|
+
def __on_internal_message(self, client, user_data, message) -> None:
|
2521
2528
|
self.__link_log.info("__on_internal_message")
|
2522
2529
|
self.__handler_task.post_message(self.__handler_task_cmd_on_message, (client, user_data, message))
|
2523
2530
|
# self.__worker_thread.async_post_message(message)
|
2524
|
-
pass
|
2525
2531
|
|
2526
|
-
def __handler_task_on_message_callback(self, value):
|
2532
|
+
def __handler_task_on_message_callback(self, value) -> None:
|
2527
2533
|
client, user_data, message = value
|
2528
2534
|
self.__on_internal_async_message(message)
|
2529
2535
|
|
2530
|
-
def __on_internal_connect(self, client, user_data, session_flag, rc):
|
2536
|
+
def __on_internal_connect(self, client, user_data, session_flag, rc) -> None:
|
2531
2537
|
self.__link_log.info("__on_internal_connect")
|
2532
2538
|
if rc == 0:
|
2533
2539
|
self.__reset_reconnect_wait()
|
2534
2540
|
# self.__upload_device_interface_info()
|
2535
2541
|
self.__handler_task.post_message(self.__handler_task_cmd_on_connect, (client, user_data, session_flag, rc))
|
2536
2542
|
|
2537
|
-
def __handler_task_on_connect_callback(self, value):
|
2543
|
+
def __handler_task_on_connect_callback(self, value) -> None:
|
2538
2544
|
client, user_data, session_flag, rc = value
|
2539
2545
|
self.__link_log.info("__on_internal_connect enter")
|
2540
|
-
self.__link_log.debug("session:%d, return code:%d" % (session_flag[
|
2546
|
+
self.__link_log.debug("session:%d, return code:%d" % (session_flag["session present"], rc))
|
2541
2547
|
if rc == 0:
|
2542
2548
|
self.__linkkit_state = LinkKit.LinkKitState.CONNECTED
|
2543
2549
|
# self.__worker_thread.start()
|
2544
2550
|
if self.__on_connect is not None:
|
2545
2551
|
try:
|
2546
|
-
self.__on_connect(session_flag[
|
2552
|
+
self.__on_connect(session_flag["session present"], rc, self.__user_data)
|
2547
2553
|
except Exception as e:
|
2548
2554
|
self.__link_log.error("on_connect process raise exception:%r" % e)
|
2549
|
-
pass
|
2550
2555
|
if self.__thing_setup_state:
|
2551
2556
|
self.__thing_enable_state = True
|
2552
2557
|
if self.__on_thing_enable:
|
2553
2558
|
self.__on_thing_enable(self.__user_data)
|
2554
|
-
return
|
2555
2559
|
|
2556
|
-
def __on_internal_disconnect(self, client, user_data, rc):
|
2560
|
+
def __on_internal_disconnect(self, client, user_data, rc) -> None:
|
2557
2561
|
self.__link_log.info("__on_internal_disconnect enter")
|
2558
2562
|
if self.__linkkit_state == LinkKit.LinkKitState.DESTRUCTING:
|
2559
2563
|
self.__linkkit_state = LinkKit.LinkKitState.DESTRUCTED
|
2560
|
-
elif
|
2561
|
-
self.__linkkit_state
|
2562
|
-
|
2564
|
+
elif (
|
2565
|
+
self.__linkkit_state == LinkKit.LinkKitState.DISCONNECTING
|
2566
|
+
or self.__linkkit_state == LinkKit.LinkKitState.CONNECTED
|
2567
|
+
):
|
2563
2568
|
self.__linkkit_state = LinkKit.LinkKitState.DISCONNECTED
|
2564
2569
|
elif self.__linkkit_state == LinkKit.LinkKitState.DISCONNECTED:
|
2565
2570
|
self.__link_log.error("__on_internal_disconnect enter from wrong state:%r" % self.__linkkit_state)
|
@@ -2589,7 +2594,7 @@ UxeCp6
|
|
2589
2594
|
if self.__linkkit_state == LinkKit.LinkKitState.DESTRUCTED:
|
2590
2595
|
self.__handler_task.stop()
|
2591
2596
|
|
2592
|
-
def __handler_task_on_disconnect_callback(self, value):
|
2597
|
+
def __handler_task_on_disconnect_callback(self, value) -> None:
|
2593
2598
|
self.__link_log.info("__handler_task_on_disconnect_callback enter")
|
2594
2599
|
client, user_data, rc = value
|
2595
2600
|
if self.__thing_setup_state:
|
@@ -2606,9 +2611,7 @@ UxeCp6
|
|
2606
2611
|
except Exception as e:
|
2607
2612
|
self.__link_log.error("on_disconnect process raise exception:%r" % e)
|
2608
2613
|
|
2609
|
-
|
2610
|
-
|
2611
|
-
def __on_internal_publish(self, client, user_data, mid):
|
2614
|
+
def __on_internal_publish(self, client, user_data, mid) -> None:
|
2612
2615
|
self.__handler_task.post_message(self.__handler_task_cmd_on_publish, (client, user_data, mid))
|
2613
2616
|
|
2614
2617
|
def __gateway_add_subdev_topo(self, subdev_array):
|
@@ -2632,7 +2635,7 @@ UxeCp6
|
|
2632
2635
|
"clientId": client_id,
|
2633
2636
|
"timestamp": millis,
|
2634
2637
|
"signmethod": "hmacSha256",
|
2635
|
-
"sign": sign
|
2638
|
+
"sign": sign,
|
2636
2639
|
}
|
2637
2640
|
request_params.append(params)
|
2638
2641
|
request_id = self.__get_thing_request_id()
|
@@ -2650,7 +2653,6 @@ UxeCp6
|
|
2650
2653
|
return 0, request_id
|
2651
2654
|
else:
|
2652
2655
|
return 1, None
|
2653
|
-
pass
|
2654
2656
|
|
2655
2657
|
def gateway_add_subdev_topo(self, subdev_array):
|
2656
2658
|
return self.__gateway_add_subdev_topo(subdev_array)
|
@@ -2696,7 +2698,6 @@ UxeCp6
|
|
2696
2698
|
return 0, request_id
|
2697
2699
|
else:
|
2698
2700
|
return 1, None
|
2699
|
-
pass
|
2700
2701
|
|
2701
2702
|
def gateway_delete_subdev_topo(self, subdev_array):
|
2702
2703
|
return self.__gateway_delete_subdev_topo(subdev_array)
|
@@ -2722,7 +2723,7 @@ UxeCp6
|
|
2722
2723
|
"clientId": client_id,
|
2723
2724
|
"timestamp": millis,
|
2724
2725
|
"cleanSession": "false",
|
2725
|
-
"sign": sign
|
2726
|
+
"sign": sign,
|
2726
2727
|
}
|
2727
2728
|
device_list.append(dev_params)
|
2728
2729
|
request_params = {
|
@@ -2777,7 +2778,6 @@ UxeCp6
|
|
2777
2778
|
return 0, request_id
|
2778
2779
|
else:
|
2779
2780
|
return 1, None
|
2780
|
-
pass
|
2781
2781
|
|
2782
2782
|
def __gateway_register_subdev(self, subdev_array):
|
2783
2783
|
request_params = []
|
@@ -2833,7 +2833,7 @@ UxeCp6
|
|
2833
2833
|
"deviceName": dn,
|
2834
2834
|
"random": random_str,
|
2835
2835
|
"signMethod": "hmacSha256",
|
2836
|
-
"sign": sign
|
2836
|
+
"sign": sign,
|
2837
2837
|
}
|
2838
2838
|
device_list.append(dev_params)
|
2839
2839
|
request_params = {
|
@@ -2854,7 +2854,7 @@ UxeCp6
|
|
2854
2854
|
else:
|
2855
2855
|
return 1, None
|
2856
2856
|
|
2857
|
-
def __handler_task_on_publish_callback(self, value):
|
2857
|
+
def __handler_task_on_publish_callback(self, value) -> None:
|
2858
2858
|
client, user_data, mid = value
|
2859
2859
|
self.__link_log.debug("__on_internal_publish message:%d" % mid)
|
2860
2860
|
with self.__thing_event_post_mid_lock:
|
@@ -2919,15 +2919,15 @@ UxeCp6
|
|
2919
2919
|
return
|
2920
2920
|
if self.__on_publish_topic is not None:
|
2921
2921
|
self.__on_publish_topic(mid, self.__user_data)
|
2922
|
-
pass
|
2923
2922
|
|
2924
|
-
def __on_internal_subscribe(self, client, user_data, mid, granted_qos):
|
2923
|
+
def __on_internal_subscribe(self, client, user_data, mid, granted_qos) -> None:
|
2925
2924
|
self.__handler_task.post_message(self.__handler_task_cmd_on_subscribe, (client, user_data, mid, granted_qos))
|
2926
2925
|
|
2927
|
-
def __handler_task_on_subscribe_callback(self, value):
|
2926
|
+
def __handler_task_on_subscribe_callback(self, value) -> None:
|
2928
2927
|
client, user_data, mid, granted_qos = value
|
2929
|
-
self.__link_log.debug(
|
2930
|
-
|
2928
|
+
self.__link_log.debug(
|
2929
|
+
"__on_internal_subscribe mid:%d granted_qos:%s" % (mid, str(",".join("%s" % it for it in granted_qos)))
|
2930
|
+
)
|
2931
2931
|
# try to read rrpc
|
2932
2932
|
with self.__user_rrpc_topics_subscribe_request_lock:
|
2933
2933
|
if mid in self.__user_rrpc_topics_subscribe_request:
|
@@ -2936,7 +2936,7 @@ UxeCp6
|
|
2936
2936
|
try:
|
2937
2937
|
self.__on_subscribe_rrpc_topic(mid, granted_qos, self.__user_data)
|
2938
2938
|
except Exception as err:
|
2939
|
-
self.__link_log.error(
|
2939
|
+
self.__link_log.error("Caught exception in on_subscribe_topic: %s", err)
|
2940
2940
|
return
|
2941
2941
|
|
2942
2942
|
# try to read other topic
|
@@ -2949,8 +2949,7 @@ UxeCp6
|
|
2949
2949
|
return_topics = []
|
2950
2950
|
for index in range(len(topics_requests)):
|
2951
2951
|
if granted_qos[index] < 0 or granted_qos[index] > 1:
|
2952
|
-
self.__link_log.error("topics:%s, granted wrong:%d" %
|
2953
|
-
(topics_requests[index], granted_qos[index]))
|
2952
|
+
self.__link_log.error("topics:%s, granted wrong:%d" % (topics_requests[index], granted_qos[index]))
|
2954
2953
|
else:
|
2955
2954
|
self.__user_topics[topics_requests[index][0]] = granted_qos[index]
|
2956
2955
|
return_topics.append((topics_requests[index], granted_qos[index]))
|
@@ -2958,13 +2957,12 @@ UxeCp6
|
|
2958
2957
|
try:
|
2959
2958
|
self.__on_subscribe_topic(mid, granted_qos, self.__user_data)
|
2960
2959
|
except Exception as err:
|
2961
|
-
self.__link_log.error(
|
2962
|
-
pass
|
2960
|
+
self.__link_log.error("Caught exception in on_subscribe_topic: %s", err)
|
2963
2961
|
|
2964
|
-
def __on_internal_unsubscribe(self, client, user_data, mid):
|
2962
|
+
def __on_internal_unsubscribe(self, client, user_data, mid) -> None:
|
2965
2963
|
self.__handler_task.post_message(self.__handler_task_cmd_on_unsubscribe, (client, user_data, mid))
|
2966
2964
|
|
2967
|
-
def __handler_task_on_unsubscribe_callback(self, value):
|
2965
|
+
def __handler_task_on_unsubscribe_callback(self, value) -> None:
|
2968
2966
|
client, user_data, mid = value
|
2969
2967
|
self.__link_log.debug("__on_internal_unsubscribe mid:%d" % mid)
|
2970
2968
|
unsubscribe_request = None
|
@@ -2976,13 +2974,12 @@ UxeCp6
|
|
2976
2974
|
try:
|
2977
2975
|
self.__on_unsubscribe_rrpc_topic(mid, self.__user_data)
|
2978
2976
|
except Exception as err:
|
2979
|
-
self.__link_log.error(
|
2977
|
+
self.__link_log.error("Caught exception in on_unsubscribe_rrpc_topic: %s", err)
|
2980
2978
|
return
|
2981
2979
|
|
2982
2980
|
with self.__user_topics_unsubscribe_request_lock:
|
2983
2981
|
if mid in self.__user_topics_unsubscribe_request:
|
2984
2982
|
unsubscribe_request = self.__user_topics_unsubscribe_request.pop(mid)
|
2985
|
-
pass
|
2986
2983
|
if unsubscribe_request is not None:
|
2987
2984
|
for t in unsubscribe_request:
|
2988
2985
|
self.__link_log.debug("__user_topics:%s" % str(self.__user_topics))
|
@@ -2995,25 +2992,25 @@ UxeCp6
|
|
2995
2992
|
try:
|
2996
2993
|
self.__on_unsubscribe_topic(mid, self.__user_data)
|
2997
2994
|
except Exception as err:
|
2998
|
-
self.__link_log.error(
|
2995
|
+
self.__link_log.error("Caught exception in on_unsubscribe_topic: %s", err)
|
2999
2996
|
|
3000
2997
|
def dump_user_topics(self):
|
3001
2998
|
return self.__user_topics
|
3002
2999
|
|
3003
|
-
def force_reconnect(self):
|
3000
|
+
def force_reconnect(self) -> None:
|
3004
3001
|
self.__link_log.error("force reconnecting")
|
3005
3002
|
self.__force_reconnect = True
|
3006
3003
|
|
3007
3004
|
@staticmethod
|
3008
3005
|
def to_user_topic(topic):
|
3009
|
-
topic_section = topic.split(
|
3006
|
+
topic_section = topic.split("/", 3)
|
3010
3007
|
user_topic = topic_section[3]
|
3011
3008
|
return user_topic
|
3012
3009
|
|
3013
3010
|
def to_full_topic(self, topic):
|
3014
3011
|
return self.__USER_TOPIC_PREFIX % (self.__product_key, self.__device_name, topic)
|
3015
3012
|
|
3016
|
-
def __is_valid_str(self, user_str):
|
3013
|
+
def __is_valid_str(self, user_str) -> bool:
|
3017
3014
|
if user_str is None or user_str == "":
|
3018
3015
|
return False
|
3019
3016
|
return True
|