From d29057097c75c5b836dda7f1f5c4aeea35873491 Mon Sep 17 00:00:00 2001 From: dreamcmi <1912544842@qq.com> Date: Fri, 16 Dec 2022 22:27:19 +0800 Subject: [PATCH] add iotcloud aliyun --- port/linux/package/pikascript/iotcloud.py | 53 +++++++++++++++++++++++ test/python/iotcloud/test_basic_aliyun.py | 32 ++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 port/linux/package/pikascript/iotcloud.py create mode 100644 test/python/iotcloud/test_basic_aliyun.py diff --git a/port/linux/package/pikascript/iotcloud.py b/port/linux/package/pikascript/iotcloud.py new file mode 100644 index 000000000..00a55c4e0 --- /dev/null +++ b/port/linux/package/pikascript/iotcloud.py @@ -0,0 +1,53 @@ +import hmac +import mqtt + + +class IOT: + def __init__(self): + self._signMethodTable = ["hmac-md5", "hmac-sha1", "hmac-sha256"] + + def aliyun(self, clientId: str, productKey: str, deviceName: str, deviceSecret: str, + signMethod="hmac-md5", regionID="cn-shanghai", ssl=False): + if clientId == None or productKey == None or deviceName == None or deviceSecret == None: + print("[Error]input param is None") + return False + if not signMethod in self._signMethodTable: + print("[Error] not support signMethod") + return False + if ssl: + securemode = "2" + self._mqttPort = int(443) + else: + securemode = "3" + self._mqttPort = int(1883) + + hmac_payload = "clientId" + clientId + "deviceName" + \ + deviceName + "productKey" + productKey + self._mqttPassword = hmac.new(deviceSecret.encode( + ), msg=hmac_payload.encode(), digestmod=signMethod).hexdigest() + self._mqttClientId = clientId + "|securemode=" + securemode + \ + ",signmethod="+signMethod.replace("-", "")+"|" + self._mqttUsername = deviceName + "&" + productKey + self._mqttUri = productKey + ".iot-as-mqtt." + regionID + ".aliyuncs.com" + + def tencent(self): ... + def onenet(self): ... + + def connect(self): + self._client = mqtt.MQTT(self._mqttUri, port=self._mqttPort, clinetID=self._mqttClientId, + username=self._mqttUsername, password=self._mqttPassword, keepalive=600) + return self._client.connect() + + def disconnect(self): + return self._client.disconnect() + + def subsribe(self, topic, qos, cb): + return self._client.subscribe(topic, qos, cb) + + def publish(self, topic, qos, payload): + return self._client.publish(qos, topic, payload) + + +def new(): + iot = IOT() + return iot diff --git a/test/python/iotcloud/test_basic_aliyun.py b/test/python/iotcloud/test_basic_aliyun.py new file mode 100644 index 000000000..464fee8f1 --- /dev/null +++ b/test/python/iotcloud/test_basic_aliyun.py @@ -0,0 +1,32 @@ + +from PikaStdDevice import Time +import iotcloud + +clientId = "pikascript" +productKey = "xxx" +deviceName = "test1" +deviceSecret = "xxxxx" + +topic = "/" + productKey + "/" + deviceName + "/user/update" + +print("iotcloud aliyun test") +c = iotcloud.new() +c.aliyun(clientId, productKey, deviceName, deviceSecret) + + +def up_cb(): + print("sub topic:", c.recv_topic) + print("sub msg:", c.recv_msg) + + +e = c.connect() +print("connect:", e) +if e == 0: + print("subcribe status:", c.subsribe(topic, 0, up_cb)) + + for i in range(10): + print("publish status:", c.publish(topic, 0, '{"id":'+str(i)+'}')) + Time.sleep_s(5) + +a = c.disconnect() +print("disconnect status:", a)