我试图编写一个python程序,仅通过具有集线器的连接字符串来更新给定IoT-hub的所有设备双绞线。此处给出的代码:Get started with device twins (Python)不是最新的,并且崩溃。有人在这种工作上有成功的经验吗?
答案 0 :(得分:1)
除了 @silent 注释之外,Azure IoT中心还为Import and Export IoT Hub device identities in bulk.
内置了一项特殊作业。此功能允许更新设备双胞胎,其中还包括报告的属性。 批量作业通过REST API Create Import Export Job提交给服务。
在blob文件中逐行描述了批量作业,下面的行是 device1 上 updateTwin 的示例:
{ "id":"device1", "importMode":"updateTwin", "status":"enabled", "tags":{},"properties":{"desired":{ "key1":12},"reported":{ "key2":null, "key3":"abcd"} }}
以下代码段显示了如何调用此REST服务来更新 inputBlobName 中描述的所有设备孪生:
import requests
import json
import time
import urllib
import hmac
import hashlib
import base64
# Example of the blob content for one device
# { "id":"device1", "importMode":"updateTwin", "status":"enabled", "tags":{},"properties":{"desired":{ "key1":12},"reported":{ "key2":null, "key3":"abcd" } } }
# IoT Hub and Blob Storage
iothub_connection_string = "<IoTHubConnectionString>"
blobContainerUri = "<blobContainerUriSaS>"
inputBlobName = "<inputBlobName>"
def get_sas_token(resource_uri, sas_name, sas_value):
sas = base64.b64decode(sas_value.encode('utf-8'))
expiry = str(int(time.time() + 10000))
string_to_sign = (resource_uri + '\n' + expiry).encode('utf-8')
signed_hmac_sha256 = hmac.HMAC(sas, string_to_sign, hashlib.sha256)
signature = urllib.parse.quote(base64.b64encode(signed_hmac_sha256.digest()))
return "SharedAccessSignature sr={}&sig={}&se={}&skn={}".format(resource_uri, signature, expiry, sas_name)
# iothub_sas_token from connection string
cs = dict(map(lambda x: x.split('=',1), iothub_connection_string.split(';')))
iothub_namespace = cs["HostName"].split('.')[0]
sas_token = get_sas_token(cs["HostName"], cs["SharedAccessKeyName"], cs["SharedAccessKey"])
# REST API see doc: https://docs.microsoft.com/en-us/rest/api/iothub/digitaltwinmodel/service/createimportexportjob
uri = "https://{}.azure-devices.net/jobs/create?api-version=2018-06-30".format(iothub_namespace)
headers = { 'Authorization':sas_token, 'Content-Type':'application/json;charset=utf-8' }
payload = { "inputBlobContainerUri": blobContainerUri, "outputBlobContainerUri": blobContainerUri, "inputBlobName": inputBlobName, "type":"import" }
res = requests.post(uri, data = json.dumps(payload), headers = headers)
print(res)
# check if the job has been accepted
if res.status_code != 200:
quit()
# check the job status progress
jobId = json.loads(res.content)["jobId"]
uri = "https://{}.azure-devices.net/jobs/{}?api-version=2018-06-30".format(iothub_namespace, jobId);
while(True):
time.sleep(2)
res = requests.get(uri, data = None, headers = headers)
if res.status_code == 200 and json.loads(res.content)["status"] == "running":
print(".", end="", flush=True)
else:
break
print("Done")
要创建blob容器,生成其sas uri地址等,可以使用Microsoft Azure Storage Explorer。
答案 1 :(得分:0)
在@Roman Kiss的帮助下,我使用了create job API请求-不需要使用存储。
JOB_ID = {INSERT_JOB_ID}
CONNECTION_STRING = {INSERT_CONNECTION_STRING}
IOT_HUB_NAME = {INSERT_IOT_HUB_NAME}
def create_device_twin():
return {"jobId": JOB_ID, "type": "scheduleUpdateTwin", "updateTwin": {"properties": {
"desired": {INSERT_YOUR_DESIRED_PROERTIES}
}, "etag": "*"}
}
def get_sas_token():
cs = dict(map(lambda x: x.split('=', 1), CONNECTION_STRING.split(';')))
resource_uri = cs["HostName"]
sas_name = cs["SharedAccessKeyName"]
sas_value = cs["SharedAccessKey"]
sas = base64.b64decode(sas_value.encode('utf-8'))
expiry = str(int(time.time() + 10000))
string_to_sign = (resource_uri + '\n' + expiry).encode('utf-8')
signed_hmac_sha256 = hmac.HMAC(sas, string_to_sign, hashlib.sha256)
signature = urllib.parse.quote(base64.b64encode(signed_hmac_sha256.digest()))
return "SharedAccessSignature sr={}&sig={}&se={}&skn={}".format(resource_uri, signature, expiry, sas_name)
if __name__ == '__main__':
uri = "https://{}.azure-devices.net/jobs/v2/{}?api-version=2018-06-30".format(IOT_HUB_NAME, JOB_ID)
sas_token = get_sas_token()
headers = {'Authorization': sas_token, 'Content-Type': 'application/json;charset=utf-8'}
res = requests.put(uri, headers=headers, data=json.dumps(create_device_twin()))