Files
ess-moxa-configuration-tools/Azure/create-moxa-list.py
Quentin WEPHRE fe82b3962f Initial commit
2024-06-18 14:59:46 +02:00

85 lines
4.0 KiB
Python

import pandas as pd
import subprocess
import argparse
import sys
import json
# This Python script will read an Excel file containing a list of devices for a site (using --serials *file name*)
# It will then create a IoT Edge device for each of the line in the file
# The user need to precise which IoT Hub is used (using --env *PROD or DEV*)
# The user need provide the site name, which will be added as a tag (using --site *site name*)
# The user need to precise which configuration of the modules should be used, according to the running firmware on the devices (using --version *version number*)
# Example:
# python create_devices_list.py --serials DANISH.xlsx --env PROD --site DANISH --version 1.5_patch
# will create all the IoT Edge devices using the serial number found in the DANISH.xlsx file
# on the PROD IoT Hub
# tag each new device with a site = DANISH
# configure the modules for each devices with the corresponding version found in moxa_ac_template_1.5_patch.json
# each device will have also be given a number = i where i is incremented automatically according to the number of device in the Excel file
# the provided Excel file will be modified, adding a column connection_string for each of the devices created with their corresponding connection string
# the output is therefore SENSITIVE
def generate_commands(serials_file, env, site, version):
# Read serial numbers from Excel file
df = pd.read_excel(serials_file)
df = df[df['device_name'].notnull()]
serials = df['device_name'].tolist()
# Initialize number
number = 1
# List to store connection strings
connection_strings = []
# Generate commands for each serial number
for serial in serials:
# Replace placeholders with actual values
print(serial, end=" ")
device_id = f"DIGIT-{serial}"
print(device_id, end=" ")
tags = f'{{"deviceId":"{device_id}","site":"{site}","number":"{number}"}}'
content = f"moxa_ac_template_{version}.json"
# Construct command strings
create_command = f"az iot hub device-identity create --device-id {device_id} --hub-name IotHub-CUBE-{env} --edge-enabled"
twin_update_command = f"az iot hub device-twin update --device-id {device_id} --hub-name IotHub-CUBE-{env} --set tags='{tags}'"
set_modules_command = f"az iot edge set-modules --device-id {device_id} --hub-name IotHub-CUBE-{env} --content {content}"
# Execute create command and get primary key
create_output = subprocess.check_output(create_command, shell=True)
create_output_json = json.loads(create_output.decode('utf-8'))
primary_key = create_output_json['authentication']['symmetricKey']['primaryKey']
print(primary_key, end=" ")
# Generate connection string
connection_string = f"HostName=IotHub-CUBE-{env}.azure-devices.net;DeviceId={device_id};SharedAccessKey={primary_key}"
print(connection_string)
connection_strings.append(connection_string)
tags_output = subprocess.run(twin_update_command, shell=True)
modules_output = subprocess.run(set_modules_command, shell=True)
# Increment number
number += 1
# Add connection strings to DataFrame
df['connection_string'] = connection_strings
# Save DataFrame to Excel file
df.to_excel(serials_file, index=False)
if __name__ == "__main__":
# Parse command line arguments
parser = argparse.ArgumentParser(description='Create devices list')
parser.add_argument('--serials', required=True, help='Excel file containing serial numbers')
parser.add_argument('--env', required=True, help='Environment (PROD or DEV)')
parser.add_argument('--site', required=True, help='Site name')
parser.add_argument('--version', required=True, help='Version number')
args = parser.parse_args()
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
# Generate commands
generate_commands(args.serials, args.env, args.site, args.version)