Skip to content

Commit 81690c5

Browse files
author
ccharlesren
committed
添加采用密钥认证方式对设备http的接入
Change-Id: Ice6a23cac84e67cae1c75fa529d403de23bfab13
1 parent 161f431 commit 81690c5

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

hub/hub.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,65 @@ def dynregDevice(self, timeout=10):
888888
err_code, err_code['Message']))
889889
return -1, err_code['Message']
890890

891+
def publishDevice(self,topicName,signType,payload,qos,timeout=10):
892+
"""http device report
893+
894+
Args:
895+
sineType ([int]): different sign type 0:HMAC 1:RSA
896+
"""
897+
898+
sign_format = '%s\n%s\n%s\n%s\n%s\n%d\n%d\n%s'
899+
url_format = '%s://ap-guangzhou.gateway.tencentdevices.com/device/publish'
900+
request_format = "{\"ProductId\":\"%s\",\"DeviceName\":\"%s\",\"TopicName\":\"%s\",\"Payload\":\"%s\",\"Qos\":\"%s\"}"
901+
902+
type = signType
903+
topicNameString = topicName
904+
qosLevel = qos
905+
payloadString = payload
906+
device_name = self.__device_info.device_name
907+
product_id = self.__device_info.product_id
908+
product_secret = self.__device_info.product_secret
909+
910+
request_text = request_format % (product_id, device_name,topicNameString,payloadString,qosLevel)
911+
request_hash = self.__codec.Hash.sha256_encode(request_text.encode("utf-8"))
912+
913+
nonce = random.randrange(2147483647)
914+
timestamp = int(time.time())
915+
sign_content = sign_format % (
916+
"POST", "ap-guangzhou.gateway.tencentdevices.com",
917+
"/device/publish", "", "hmacsha256", timestamp,
918+
nonce, request_hash)
919+
sign_base64 = self.__codec.Base64.encode(self.__codec.Hmac.sha256_encode(product_secret.encode("utf-8"),
920+
sign_content.encode("utf-8")))
921+
922+
header = {
923+
'Content-Type': 'application/json; charset=utf-8',
924+
"X-TC-Algorithm": "hmacsha256",
925+
"X-TC-Timestamp": timestamp,
926+
"X-TC-Nonce": nonce,
927+
"X-TC-Signature": sign_base64
928+
}
929+
data = bytes(request_text, encoding='utf-8')
930+
931+
context = None
932+
if self.__tls:
933+
request_url = url_format % 'https'
934+
context = self.__codec.Ssl().create_content()
935+
else:
936+
request_url = url_format % 'http'
937+
self._logger.info('dynreg url {}'.format(request_url))
938+
req = urllib.request.Request(request_url, data=data, headers=header)
939+
with urllib.request.urlopen(req, timeout=timeout, context=context) as url_file:
940+
reply_data = url_file.read().decode('utf-8')
941+
reply_obj = json.loads(reply_data)
942+
resp = reply_obj['Response']
943+
944+
if not resp['Error'] :
945+
err_code = resp['Error']
946+
self._logger.error('code: {}, error message: {}'.format(
947+
err_code, err_code['Message']))
948+
return -1, err_code['Message']
949+
891950
def isSubdevStatusOnline(self, sub_productId, sub_devName):
892951
"""Sub-device status
893952
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import sys
2+
import logging
3+
from hub.hub import QcloudHub
4+
5+
provider = QcloudHub(device_file="hub/sample/device_info.json", tls=True)
6+
qcloud = provider.hub
7+
logger = None
8+

0 commit comments

Comments
 (0)