merged list devices (no impact)
This commit is contained in:
@@ -1,27 +1,35 @@
|
||||
from azure.iot.hub import IoTHubRegistryManager
|
||||
from azure.iot.hub.models import Twin, TwinProperties
|
||||
from dotenv import load_dotenv
|
||||
import pandas as pd
|
||||
|
||||
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
|
||||
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_SAFT_PROD"))
|
||||
CONNECTION_STRING = str(os.getenv("CONNECTION_STRING_INOX_PROD"))
|
||||
if CONNECTION_STRING == "":
|
||||
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):
|
||||
@@ -34,6 +42,18 @@ def create_device(device_name, number):
|
||||
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)
|
||||
@@ -44,13 +64,24 @@ def create_device(device_name, number):
|
||||
})
|
||||
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:
|
||||
create_device(device_name, index + 1)
|
||||
if device_name and device_name != 'nan':
|
||||
conn_str = create_device(device_name, index + 1)
|
||||
if conn_str:
|
||||
df.at[index, 1] = conn_str
|
||||
|
||||
print("Device provisioning completed.")
|
||||
# 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.")
|
||||
|
||||
Reference in New Issue
Block a user