iotcloud.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import hmac
  2. import base64
  3. import random
  4. import mqtt
  5. import time
  6. ONENET_API = 0
  7. ONENET_DEVICE = 1
  8. class IOT:
  9. def __init__(self):
  10. self._signMethodTable = ["hmacmd5", "hmacsha1", "hmacsha256"]
  11. def randStr(self, len):
  12. a = ""
  13. for i in range(len):
  14. a = a + str(random.randint(0, 9))
  15. return a
  16. def getTimeStamp(self, t):
  17. return str(int(time.time()) + t)
  18. def urlEncode(self, s):
  19. rs = ""
  20. for i in s:
  21. if i == "+":
  22. i = "%2B"
  23. elif i == " ":
  24. i = "%20"
  25. elif i == "/":
  26. i = "%2F"
  27. elif i == "?":
  28. i = "%3F"
  29. elif i == "%":
  30. i = "%25"
  31. elif i == "#":
  32. i = "%23"
  33. elif i == "&":
  34. i = "%26"
  35. elif i == "=":
  36. i = "%3D"
  37. rs = rs + i
  38. return rs
  39. def aliyun(self, clientId: str, productKey: str, deviceName: str, deviceSecret: str,
  40. signMethod="hmacmd5", regionID="cn-shanghai", ssl=False):
  41. if clientId == None or productKey == None or deviceName == None or deviceSecret == None:
  42. print("[Error]input param is None")
  43. return False
  44. if signMethod not in self._signMethodTable:
  45. print("[Error]not support signMethod")
  46. return False
  47. if ssl:
  48. securemode = "2"
  49. self.port = int(443)
  50. else:
  51. securemode = "3"
  52. self.port = int(1883)
  53. self.address = productKey + ".iot-as-mqtt." + regionID + ".aliyuncs.com"
  54. self.username = deviceName + "&" + productKey
  55. self.clientid = clientId + "|securemode=" + securemode + ",signmethod=" + signMethod + "|"
  56. hmac_payload = "clientId" + clientId + "deviceName" + deviceName + "productKey" + productKey
  57. self.password = hmac.new(deviceSecret.encode(),
  58. msg=hmac_payload.encode(),
  59. digestmod=signMethod.replace("hmac", "")).hexdigest()
  60. return True
  61. def tencent(self, productId, deviceName, deviceSecret, signMethod="hmacsha1", expiryTime=3600, ssl=False):
  62. if productId == None or deviceName == None or deviceSecret == None:
  63. print("[Error]input param is None")
  64. return False
  65. if signMethod not in self._signMethodTable:
  66. print("[Error]not support signMethod")
  67. return False
  68. connid = self.randStr(5)
  69. expiry = self.getTimeStamp(expiryTime)
  70. self.address = productId + ".iotcloud.tencentdevices.com"
  71. self.port = int(1883)
  72. self.clientid = productId + deviceName
  73. self.username = self.clientid + ";12010126;" + connid + ";" + expiry
  74. token = hmac.new(base64.b64decode(deviceSecret.encode()),
  75. msg=self.username.encode(),
  76. digestmod=signMethod.replace("hmac", "")).hexdigest()
  77. self.password = token + ";" + signMethod
  78. return True
  79. def onenet(self, productId, deviceName, accessKey, mode=ONENET_DEVICE, signMethod="hmacmd5", expiryTime=3600, ssl=False):
  80. if productId == None or deviceName == None or accessKey == None:
  81. print("[Error]input param is None")
  82. return False
  83. if signMethod not in self._signMethodTable:
  84. print("[Error]not support signMethod")
  85. return False
  86. if ssl:
  87. self.address = "mqttstls.heclouds.com"
  88. self.port = int(8883)
  89. else:
  90. self.address = "mqtts.heclouds.com"
  91. self.port = int(1883)
  92. self.clientid = deviceName
  93. self.username = productId
  94. method = signMethod.replace("hmac", "")
  95. expiry = self.getTimeStamp(expiryTime)
  96. if mode == ONENET_DEVICE:
  97. res = "products/" + productId + "/devices/" + deviceName
  98. elif mode == ONENET_API:
  99. res = "products/" + productId
  100. else:
  101. print("[Error]onenet not support mode")
  102. return False
  103. org = expiry + '\n' + method + '\n' + res + '\n' + "2018-10-31"
  104. k = base64.b64decode(accessKey.encode())
  105. h = hmac.new(k, msg=org.encode(), digestmod=method)
  106. sign = base64.b64encode(h.digest()).decode()
  107. res = self.urlEncode(res)
  108. sign = self.urlEncode(sign)
  109. self.password = "version=2018-10-31&res=%s&et=%s&method=%s&sign=%s" % (res, expiry, method, sign)
  110. print(self.clientid)
  111. print(self.username)
  112. print(self.password)
  113. return True
  114. def onenetMulti(self, productId, deviceId, apiKey):
  115. self.address = "mqtt.heclouds.com"
  116. self.port = int(6002)
  117. self.clientid = deviceId
  118. self.username = productId
  119. self.password = apiKey
  120. def connect(self, keepalive=600):
  121. self.client = mqtt.MQTT(self.address, port=self.port, clinetID=self.clientid,
  122. username=self.username, password=self.password, keepalive=keepalive)
  123. return self.client.connect()
  124. def disconnect(self):
  125. return self.client.disconnect()
  126. def subsribe(self, topic, cb, qos=0):
  127. return self.client.subscribe(topic, cb, qos)
  128. def publish(self, topic, payload, qos=0):
  129. return self.client.publish(topic, payload, qos)
  130. def new():
  131. iot = IOT()
  132. return iot