New functions using Azure SDK for Python
This commit is contained in:
41
Python/azure_iot_hub_get_connection_strings.py
Normal file
41
Python/azure_iot_hub_get_connection_strings.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from azure.iot.hub import IoTHubRegistryManager
|
||||
from azure.iot.hub.protocol.models import QuerySpecification
|
||||
from azure.iot.hub.models import CloudToDeviceMethod, CloudToDeviceMethodResult
|
||||
from dotenv import load_dotenv
|
||||
|
||||
import json
|
||||
import os
|
||||
|
||||
load_dotenv()
|
||||
|
||||
# Authenticate to your Azure account
|
||||
# CONNECTION_STRING = str(os.getenv("CONNECTION_STRING_SAFT_PROD"))
|
||||
CONNECTION_STRING = str(os.getenv("CONNECTION_STRING_INOX_PROD"))
|
||||
if CONNECTION_STRING == "":
|
||||
print("Provide a connection string for the Iot Hub before running the script!")
|
||||
exit(13)
|
||||
|
||||
SITE_NAME = "COTTONWOOD"
|
||||
|
||||
|
||||
registry_manager = IoTHubRegistryManager.from_connection_string(CONNECTION_STRING)
|
||||
query_spec = QuerySpecification(query="SELECT * FROM devices WHERE IS_DEFINED(tags.site) AND tags.site = '" + SITE_NAME + "' AND capabilities.iotEdge = true")
|
||||
|
||||
query_result = registry_manager.query_iot_hub(query_spec)
|
||||
|
||||
devices = []
|
||||
for item in query_result.items:
|
||||
deviceId = str(item.device_id)
|
||||
site = str(item.tags['site'])
|
||||
number = int(item.tags['number'])
|
||||
cloud_version = str(item.tags['version'])
|
||||
devices.append([deviceId, site, number, cloud_version])
|
||||
|
||||
ordered_devices = sorted(devices, key = lambda x: (x[1], x[2]))
|
||||
|
||||
for i in ordered_devices:
|
||||
device_info = registry_manager.get_device(i[0])
|
||||
primary_key = device_info.authentication.symmetric_key.primary_key
|
||||
connection_string = f"HostName={CONNECTION_STRING.split(';')[0].split('=')[1]};DeviceId={i[0]};SharedAccessKey={primary_key}"
|
||||
print(i[1], i[2], i[0])
|
||||
print(connection_string)
|
||||
Reference in New Issue
Block a user