Skip to content

Commit f93d367

Browse files
author
ccharlesren
committed
python sdk: IoT Hub 为设备HTTP接入提供测试代码
Change-Id: Ib3c8c83a44994bb504c305aff64b6597e04dfd5d
1 parent 81690c5 commit f93d367

File tree

4 files changed

+106
-9
lines changed

4 files changed

+106
-9
lines changed

hub/hub.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,41 @@ def publishDevice(self,topicName,signType,payload,qos,timeout=10):
946946
self._logger.error('code: {}, error message: {}'.format(
947947
err_code, err_code['Message']))
948948
return -1, err_code['Message']
949+
950+
def httpCallback(self, on_connect, on_disconnect,
951+
on_message, on_publish,
952+
on_subscribe, on_unsubscribe):
953+
"""Register user http callback
954+
955+
Register user http callback for http
956+
Args:
957+
on_connect: http connect callback
958+
on_disconnect: http disconnect callback
959+
on_message: http message callback
960+
on_publish: http publish callback
961+
on_subscribe: http subscribe callback
962+
on_unsubscribe: http unsubscribe callback
963+
Returns:
964+
success: default
965+
fail: default
966+
"""
967+
self.__user_on_connect = on_connect
968+
self.__user_on_disconnect = on_disconnect
969+
self.__user_on_message = on_message
970+
self.__user_on_publish = on_publish
971+
self.__user_on_subscribe = on_subscribe
972+
self.__user_on_unsubscribe = on_unsubscribe
973+
974+
def isHttpConnected(self):
975+
"""Is http connected
976+
977+
Is http connected
978+
Args: None
979+
Returns:
980+
success: True/False
981+
"""
982+
return self.__hub_state == self.HubState.CONNECTED
983+
949984

950985
def isSubdevStatusOnline(self, sub_productId, sub_devName):
951986
"""Sub-device status

hub/sample/deviceReport/example_http_report.py

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import sys
2+
import logging
3+
import time
4+
from hub.hub import QcloudHub
5+
6+
provider = QcloudHub(device_file="hub/sample/device_info.json", tls=True)
7+
qcloud = provider.hub
8+
logger = qcloud.logInit(qcloud.LoggerLevel.DEBUG, "logs/log", 1024*1024*10, 5, enable=True)
9+
10+
def on_connect(flags, rc, userdata):
11+
logger.debug("%s:flags:%d,rc:%d,userdata:%s" % (sys._getframe().f_code.co_name, flags, rc, userdata))
12+
pass
13+
14+
15+
def on_disconnect(rc, userdata):
16+
logger.debug("%s:rc:%d,userdata:%s" % (sys._getframe().f_code.co_name, rc, userdata))
17+
pass
18+
19+
20+
def on_message(topic, payload, qos, userdata):
21+
logger.debug("%s:topic:%s,payload:%s,qos:%s,userdata:%s" % (sys._getframe().f_code.co_name, topic, payload, qos, userdata))
22+
pass
23+
24+
25+
def on_publish(mid, userdata):
26+
logger.debug("%s:mid:%d,userdata:%s" % (sys._getframe().f_code.co_name, mid, userdata))
27+
pass
28+
29+
30+
def on_subscribe(mid, granted_qos, userdata):
31+
logger.debug("%s:mid:%d,granted_qos:%s,userdata:%s" % (sys._getframe().f_code.co_name, mid, granted_qos, userdata))
32+
pass
33+
34+
35+
def on_unsubscribe(mid, userdata):
36+
logger.debug("%s:mid:%d,userdata:%s" % (sys._getframe().f_code.co_name, mid, userdata))
37+
pass
38+
39+
def example_http():
40+
logger.debug("\033[1;36m http test start...\033[0m")
41+
42+
qcloud.httpCallback(on_connect, on_disconnect,
43+
on_message, on_publish,
44+
on_subscribe, on_unsubscribe)
45+
qcloud.connect()
46+
47+
count = 0
48+
while True:
49+
if qcloud.isHttpConnected():
50+
break
51+
else:
52+
if count >= 3:
53+
logger.error("\033[1;31m http test fail...\033[0m")
54+
return False
55+
time.sleep(1)
56+
count += 1
57+
58+
timestamp = qcloud.getNtpAccurateTime()
59+
dt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timestamp/1000))
60+
logger.debug("current time:%s" % dt)
61+
62+
# qcloud.disconnect()
63+
logger.debug("\033[1;36m http test success...\033[0m")
64+
65+
return True

hub/sample/test.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from broadcast import example_broadcast as broadcasttest
88
from rrpc import example_rrpc as rrpctest
99
from shadow import example_shadow as shadowtest
10+
from httpAccess import example_http as httptest
1011

1112
class MyTestCase(unittest.TestCase):
1213

@@ -32,6 +33,11 @@ def test_gateway(self):
3233
self.assertEqual(ret, True)
3334
pass
3435

36+
def test_http(self):
37+
ret = httptest.example_http()
38+
self.assertEqual(ret, True)
39+
pass
40+
3541
@unittest.skip("skipping")
3642
def test_ota(self):
3743
ret = otatest.example_ota()
@@ -53,6 +59,5 @@ def test_shadow(self):
5359
self.assertEqual(ret, True)
5460
pass
5561

56-
5762
if __name__ == '__main__':
5863
unittest.main()

0 commit comments

Comments
 (0)