86 lines
3.2 KiB
Python
Raw Normal View History

2022-12-16 22:27:19 +08:00
import hmac
2022-12-16 23:57:27 +08:00
import base64
import random
2022-12-16 22:27:19 +08:00
import mqtt
2022-12-16 23:57:27 +08:00
from PikaStdDevice import Time
2022-12-16 22:27:19 +08:00
class IOT:
def __init__(self):
self._signMethodTable = ["hmac-md5", "hmac-sha1", "hmac-sha256"]
2022-12-16 23:57:27 +08:00
def randStr(self, len):
a = ""
for i in range(len):
2022-12-17 19:12:09 +08:00
a = a + str(random.randint(0, 9))
2022-12-16 23:57:27 +08:00
return a
2022-12-17 19:12:09 +08:00
def getTimeStamp(self, t):
return int(Time.time()*1000) + t # todo 时间戳64位整形实现
2022-12-16 22:27:19 +08:00
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
2022-12-16 23:57:27 +08:00
if signMethod not in self._signMethodTable:
2022-12-17 19:12:09 +08:00
print("[Error]not support signMethod")
2022-12-16 22:27:19 +08:00
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
2022-12-17 19:12:09 +08:00
self._mqttPassword = hmac.new(deviceSecret.encode(),
msg = hmac_payload.encode(),
digestmod = signMethod).hexdigest()
2022-12-16 22:27:19 +08:00
self._mqttClientId = clientId + "|securemode=" + securemode + \
",signmethod="+signMethod.replace("-", "")+"|"
self._mqttUsername = deviceName + "&" + productKey
self._mqttUri = productKey + ".iot-as-mqtt." + regionID + ".aliyuncs.com"
2022-12-16 23:57:27 +08:00
return True
2022-12-17 19:12:09 +08:00
def tencentIotHub(self, productId, deviceName, deviceSecret, signMethod="hmac-sha1", expiryTime=3600, ssl=False):
if productId == None or deviceName == None or deviceSecret == None:
2022-12-16 23:57:27 +08:00
print("[Error]input param is None")
return False
if signMethod not in self._signMethodTable:
2022-12-17 19:12:09 +08:00
print("[Error]not support signMethod")
2022-12-16 23:57:27 +08:00
return False
connid = self.randStr(5)
2022-12-17 19:12:09 +08:00
expiry = self.getTimeStamp(expiryTime)
2022-12-16 23:57:27 +08:00
self._mqttUri = productId + ".iotcloud.tencentdevices.com"
self._mqttPort = int(1883)
2022-12-17 19:12:09 +08:00
self._mqttClientId = productId + deviceName
self._mqttUsername = self._mqttClientId + ";12010126;" + connid + ";" + str(expiry)
token = hmac.new(base64.b64decode(deviceSecret.encode()),
msg=self._mqttUsername.encode(),
2022-12-16 23:57:27 +08:00
digestmod=signMethod).hexdigest()
2022-12-17 19:12:09 +08:00
self._mqttPassword = token + ";" + signMethod.replace("-", "")
2022-12-16 23:57:27 +08:00
return True
2022-12-16 22:27:19 +08:00
def onenet(self): ...
2022-12-16 23:57:27 +08:00
def connect(self, keepalive=600):
2022-12-16 22:27:19 +08:00
self._client = mqtt.MQTT(self._mqttUri, port=self._mqttPort, clinetID=self._mqttClientId,
2022-12-16 23:57:27 +08:00
username=self._mqttUsername, password=self._mqttPassword, keepalive=keepalive)
2022-12-16 22:27:19 +08:00
return self._client.connect()
def disconnect(self):
return self._client.disconnect()
2022-12-16 23:57:27 +08:00
def subsribe(self, topic, cb, qos=0):
2022-12-16 22:27:19 +08:00
return self._client.subscribe(topic, qos, cb)
2022-12-16 23:57:27 +08:00
def publish(self, topic, payload, qos=0):
2022-12-16 22:27:19 +08:00
return self._client.publish(qos, topic, payload)
def new():
iot = IOT()
return iot