88 lines
3.0 KiB
Python
88 lines
3.0 KiB
Python
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
|
|
|
|
load_dotenv()
|
|
|
|
INPUT_FILE = "HONEYCOMB_DEVICE_LIST.xlsx" # Path to your Excel file
|
|
SITE_NAME = "Honeycomb" # Parameterized site name
|
|
VERSION = "1.6.0" # Parameterized version
|
|
|
|
# Authenticate to your Azure account
|
|
CONNECTION_STRING = str(os.getenv("CONNECTION_STRING_INOX_PROD"))
|
|
if CONNECTION_STRING == "" or CONNECTION_STRING == "None":
|
|
print("Provide a connection string for the Iot Hub before running the script!")
|
|
exit(13)
|
|
|
|
# Extract HostName from CONNECTION_STRING
|
|
host_name = ""
|
|
for part in CONNECTION_STRING.split(';'):
|
|
if part.startswith('HostName='):
|
|
host_name = part.split('=', 1)[1]
|
|
break
|
|
|
|
df = pd.read_excel(INPUT_FILE, header=None)
|
|
|
|
# Ensure the dataframe has at least 2 columns to store the connection strings
|
|
if df.shape[1] < 2:
|
|
df[1] = ""
|
|
|
|
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}")
|
|
except Exception as e:
|
|
print(f"Error creating {device_name} (it may already exist): {e}")
|
|
try:
|
|
device = registry_manager.get_device(device_name)
|
|
print(f"Retrieved existing device: {device_name}")
|
|
except Exception as e2:
|
|
print(f"Error retrieving {device_name}: {e2}")
|
|
return None
|
|
|
|
try:
|
|
primary_key = device.authentication.symmetric_key.primary_key
|
|
connection_string = f"HostName={host_name};DeviceId={device_name};SharedAccessKey={primary_key}"
|
|
|
|
# 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}")
|
|
|
|
return connection_string
|
|
except Exception as e:
|
|
print(f"Error processing {device_name}: {e}")
|
|
return None
|
|
|
|
# Loop through the Excel file and process each device
|
|
for index, row in df.iterrows():
|
|
device_name = str(row[0]).strip()
|
|
if device_name and device_name != 'nan':
|
|
conn_str = create_device(device_name, index + 1)
|
|
if conn_str:
|
|
df.at[index, 1] = conn_str
|
|
|
|
# Save the updated dataframe back to the Excel file
|
|
try:
|
|
df.to_excel(INPUT_FILE, index=False, header=False)
|
|
print("Device provisioning completed.")
|
|
except PermissionError:
|
|
print(f"\n[ERROR] Permission denied: '{INPUT_FILE}'.")
|
|
print("Please close the Excel file if it's currently open in another program, and then try again.")
|