New functions using Azure SDK for Python

This commit is contained in:
2025-03-17 08:33:46 +01:00
parent 139f271905
commit 888352d3b8
5 changed files with 418 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
from azure.iot.hub import IoTHubRegistryManager
from azure.iot.hub.models import Twin, TwinProperties
from dotenv import load_dotenv
import pandas as pd
import os
import json
load_dotenv()
INPUT_FILE = "cottonwood_devices.xlsx" # Path to your Excel file
SITE_NAME = "COTTONWOOD" # Parameterized site name
VERSION = "1.5.0" # Parameterized version
# 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)
df = pd.read_excel(INPUT_FILE, header=None)
registry_manager = IoTHubRegistryManager.from_connection_string(CONNECTION_STRING)
def create_device(device_name, number):
try:
# Create the device
device = registry_manager.create_device_with_sas(
device_name,
primary_key="", secondary_key="",
status="enabled",
iot_edge=True
)
print(f"Created IoT Edge-enabled device: {device_name}")
# Set tags
twin = registry_manager.get_twin(device_name)
twin_patch = Twin(properties=TwinProperties(desired={}), tags={
"site": SITE_NAME,
"number": number,
"version": VERSION
})
registry_manager.update_twin(device_name, twin_patch, twin.etag)
print(f"Updated tags for {device_name}")
except Exception as e:
print(f"Error processing {device_name}: {e}")
# Loop through the Excel file and process each device
for index, row in df.iterrows():
device_name = str(row[0]).strip()
if device_name:
create_device(device_name, index + 1)
print("Device provisioning completed.")