Files
ess-moxa-configuration-tools/Python/azure_iot_hub_1.py
Quentin WEPHRE 24e471f633 Updated access to Iot Hub using Python SDK
without sensitive information this time
2024-06-21 08:42:41 +02:00

24 lines
902 B
Python

from azure.iot.hub import IoTHubRegistryManager
from azure.iot.hub.protocol.models import QueryResult, QuerySpecification
import json
# Install the Azure IoT Hub SDK:
# pip install azure-iot-hub
# Authenticate to your Azure account
CONNECTION_STRING = "HostName=IotHub-CUBE-PROD.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey= ..."
if CONNECTION_STRING == "":
print("Provide a connection string for the Iot Hub before running the script!")
exit(13)
registry_manager = IoTHubRegistryManager.from_connection_string(CONNECTION_STRING)
query_spec = QuerySpecification(query="SELECT * FROM devices WHERE tags.site = 'SASK'")
query_result = registry_manager.query_iot_hub(query_spec)
ordered_devices = []
for item in query_result.items:
ordered_devices.append([int(item.tags['number']), item.tags['deviceId']])
ordered_devices.sort()
for i in ordered_devices:
print(i)