fix function and add tencent iot hub

This commit is contained in:
dreamcmi 2022-12-16 23:57:27 +08:00
parent d29057097c
commit eddcaff074
2 changed files with 37 additions and 8 deletions

View File

@ -1,17 +1,26 @@
import hmac import hmac
import base64
import random
import mqtt import mqtt
from PikaStdDevice import Time
class IOT: class IOT:
def __init__(self): def __init__(self):
self._signMethodTable = ["hmac-md5", "hmac-sha1", "hmac-sha256"] self._signMethodTable = ["hmac-md5", "hmac-sha1", "hmac-sha256"]
def randStr(self, len):
a = ""
for i in range(len):
a = a+str(random.randint(0, 9))
return a
def aliyun(self, clientId: str, productKey: str, deviceName: str, deviceSecret: str, def aliyun(self, clientId: str, productKey: str, deviceName: str, deviceSecret: str,
signMethod="hmac-md5", regionID="cn-shanghai", ssl=False): signMethod="hmac-md5", regionID="cn-shanghai", ssl=False):
if clientId == None or productKey == None or deviceName == None or deviceSecret == None: if clientId == None or productKey == None or deviceName == None or deviceSecret == None:
print("[Error]input param is None") print("[Error]input param is None")
return False return False
if not signMethod in self._signMethodTable: if signMethod not in self._signMethodTable:
print("[Error] not support signMethod") print("[Error] not support signMethod")
return False return False
if ssl: if ssl:
@ -29,22 +38,42 @@ class IOT:
",signmethod="+signMethod.replace("-", "")+"|" ",signmethod="+signMethod.replace("-", "")+"|"
self._mqttUsername = deviceName + "&" + productKey self._mqttUsername = deviceName + "&" + productKey
self._mqttUri = productKey + ".iot-as-mqtt." + regionID + ".aliyuncs.com" self._mqttUri = productKey + ".iot-as-mqtt." + regionID + ".aliyuncs.com"
return True
def tencent(self, productId, deiceName, deviceSecret, signMethod="hmac-sha1", ssl=False):
if productId == None or deiceName == None or deviceSecret == None:
print("[Error]input param is None")
return False
if signMethod not in self._signMethodTable:
print("[Error] not support signMethod")
return False
connid = self.randStr(5)
expiry = int(Time.time()) + 60 * 60
self._mqttUri = productId + ".iotcloud.tencentdevices.com"
self._mqttPort = int(1883)
self._mqttClientId = productId + deiceName
self._mqttUsername = self._mqttClientId+";" + \
deiceName+";12010126;"+connid+";"+expiry
secret = base64.b64decode(deviceSecret.encode())
token = hmac.new(secret, msg=self._mqttUsername.encode(),
digestmod=signMethod).hexdigest()
self._mqttPassword = token+";"+signMethod.replace("-", "")
return True
def tencent(self): ...
def onenet(self): ... def onenet(self): ...
def connect(self): def connect(self, keepalive=600):
self._client = mqtt.MQTT(self._mqttUri, port=self._mqttPort, clinetID=self._mqttClientId, self._client = mqtt.MQTT(self._mqttUri, port=self._mqttPort, clinetID=self._mqttClientId,
username=self._mqttUsername, password=self._mqttPassword, keepalive=600) username=self._mqttUsername, password=self._mqttPassword, keepalive=keepalive)
return self._client.connect() return self._client.connect()
def disconnect(self): def disconnect(self):
return self._client.disconnect() return self._client.disconnect()
def subsribe(self, topic, qos, cb): def subsribe(self, topic, cb, qos=0):
return self._client.subscribe(topic, qos, cb) return self._client.subscribe(topic, qos, cb)
def publish(self, topic, qos, payload): def publish(self, topic, payload, qos=0):
return self._client.publish(qos, topic, payload) return self._client.publish(qos, topic, payload)

View File

@ -22,10 +22,10 @@ def up_cb():
e = c.connect() e = c.connect()
print("connect:", e) print("connect:", e)
if e == 0: if e == 0:
print("subcribe status:", c.subsribe(topic, 0, up_cb)) print("subcribe status:", c.subsribe(topic, up_cb))
for i in range(10): for i in range(10):
print("publish status:", c.publish(topic, 0, '{"id":'+str(i)+'}')) print("publish status:", c.publish(topic, '{"id":'+str(i)+'}'))
Time.sleep_s(5) Time.sleep_s(5)
a = c.disconnect() a = c.disconnect()