diff --git a/Moulinette/data_path_config.py b/Moulinette/data_path_config.py index 4081ccf..937f870 100644 --- a/Moulinette/data_path_config.py +++ b/Moulinette/data_path_config.py @@ -40,7 +40,7 @@ allowed_name_characters.append('.') logging.basicConfig(format='%(asctime)s %(levelname)-8s %(message)s', filename='data_config_debug.log', filemode='w', level=logging.DEBUG, datefmt='%Y%m%d%H%M%S') dir_name = 'I-Sight_Generated_Files' -input_datamodel = 'DATAMODEL_1.0.6_DANISH_TEST.xlsx' +input_datamodel = 'DATAMODEL_1.0.6_DANISH.xlsx' shell_script_name = dir_name + '/I-Sight_Configuration_' global_shell_script_name = dir_name + '/I-Sight_Global_Configuration.sh' @@ -172,6 +172,7 @@ def excel_parser(sheet_det, slave_no, slave_device, allowed_name_characters, dsh dsh_global.write("\t-H \"Content-Type: application/json\" \\\n") dsh_global.write("\t-H \"mx-api-token: ${token}\" \\\n") dsh_global.write("\t-d '" + str(slave_prop_list) + "' >> global_config.log\n\n") + dsh_global.write("echo -e \"\\n\" >> global_config.log\n\n") def common_code(dsh, dsh_global, row_device): @@ -191,6 +192,7 @@ def common_code(dsh, dsh_global, row_device): dsh_global.write("curl -s -X PUT -k https://" + row_device['device_ip_address_http'] + ":8443/api/v1/modbusmaster/control/config/apply \\\n") dsh_global.write("\t-H \"Content-Type: application/json\" \\\n") dsh_global.write("\t-H \"mx-api-token: ${token}\" >> global_config.log\n\n") + dsh_global.write("echo -e \"\\n\" >> global_config.log\n\n") def jq_filter(current_device, dsh, dsh_global, row_device): @@ -240,6 +242,8 @@ def jq_filter(current_device, dsh, dsh_global, row_device): dsh_global.write("\t-H \"Content-Type: application/json\" \\\n") dsh_global.write("\t-H \"mx-api-token: ${token}\" \\\n") dsh_global.write("\t-d '" + str(json_object) + "' >> global_config.log\n\n") + dsh_global.write("echo -e \"\\n\" >> global_config.log\n\n") + dsh_global.write("echo \"Finished work on " + str(row_device['device_name']) + "\"") STATIC_JQ_FILTER_EMISSIONDATE = "(now|todateiso8601)" @@ -341,6 +345,7 @@ def slave_script(slave_no, row_asset, dsh, dsh_global, row_device): dsh_global.write("\t-H \"Content-Type: application/json\" \\\n") dsh_global.write("\t-H \"mx-api-token: ${token}\" \\\n") dsh_global.write("\t-d '" + str(json_object) + "' >> global_config.log\n\n") + dsh_global.write("echo -e \"\\n\" >> global_config.log\n\n") # Add the contents related to each slave device sheet_det = row_asset['asset_type'] @@ -360,10 +365,37 @@ def check_string_format(input_string): def auth_moxa(dsh_global, row_device): json_object = "{\"acceptEULA\": true, \"name\": \"" + default_user + "\", \"password\": \"" + default_password + "\"}" dsh_global.write("### Authenticating " + "" + "" + str(row_device['device_name']) + "\n") + dsh_global.write("echo \"Starting work on " + str(row_device['device_name']) + "\"\n") dsh_global.write("token=$(curl -s -X POST -k https://" + row_device['device_ip_address_http'] + ":8443/api/v1/auth \\\n") dsh_global.write("\t-H \"Content-Type: application/json\" \\\n") dsh_global.write("\t-d '" + str(json_object) + "' | jq -r \".data.token\") >> global_config.log\n\n") + dsh_global.write("echo -e \"\\n\" >> global_config.log\n\n") +def clean_modbus(dsh_global, row_device): + dsh_global.write("### Cleaning Modbus " + "" + "" + str(row_device['device_name']) + "\n") + dsh_global.write("echo \"CLEANING MODBUS ON " + str(row_device['device_name']) + "\"\n") + dsh_global.write("curl -s -X DELETE -k https://" + row_device['device_ip_address_http'] + ":8443/api/v1/modbusmaster/config/remote-devs \\\n") + dsh_global.write("\t-H \"Content-Type: application/json\" \\\n") + dsh_global.write("\t-H \"mx-api-token: ${token}\" \\\n") + dsh_global.write("\t-d '" + "" + "' >> global_config.log\n\n") + dsh_global.write("echo -e \"\\n\" >> global_config.log\n\n") + +def clean_azure(dsh_global, row_device): + dsh_global.write("echo -e \"\\nCLEANING AZURE\\nCLEANING AZURE\\nCLEANING AZURE\\nCLEANING AZURE\\nCLEANING AZURE\\n\" >> global_config.log\n\n") + dsh_global.write("### Cleaning Azure telemetry " + "" + "" + str(row_device['device_name']) + "\n") + dsh_global.write("echo \"CLEANING AZURE TELEMETRY ON " + str(row_device['device_name']) + " " + str(row_device['device_ip_address_http']) + "\"\n") + dsh_global.write("azure_topics=$(curl -s -X GET -k https://" + row_device['device_ip_address_http'] + ":8443/api/v1/azure-iotedge/messages \\\n") + dsh_global.write("\t-H \"Content-Type: application/json\" \\\n") + dsh_global.write("\t-H \"mx-api-token: ${token}\" \\\n") + dsh_global.write("\t-d '" + "{}" + "') >> global_config.log\n") + dsh_global.write("echo \"${azure_topics}\" | jq -r '.data[].id' | while read -r id; do" + "\n") + dsh_global.write("\tcurl -s -X DELETE -k https://" + row_device['device_ip_address_http'] + ":8443/api/v1/azure-iotedge/messages/${id}" + "\\\n") + dsh_global.write("\t\t-H \"Content-Type: application/json\" \\\n") + dsh_global.write("\t\t-H \"mx-api-token: ${token}\" \\\n") + dsh_global.write("\t\t-d '" + "{}" + "' >> global_config.log\n\n") + dsh_global.write("done\n") + dsh_global.write("echo -e \"\\n\" >> global_config.log\n\n") + def main(): # Create the shell script to write content @@ -383,17 +415,25 @@ def main(): null_elements_1 = list(df_slave['device_name'].notnull()) null_elements_2 = list(df_slave['device_ip_address_http'].notnull()) for index in range(len(null_elements_1)): - try: if null_elements_1[index] == False: if null_elements_2[index] == False: - logging.debug("The slave device %s at index %d is not considered due to missing data model and missing IP address \n",df_slave.at[index,'Equipment Designation'],index+2) + logging.debug(f"Missing IP address and name for device at line ${index}.") else: - logging.debug("The slave device %s at index %d is not considered due to missing data model \n",df_slave.at[index,'Equipment Designation'],index+2) + logging.debug(f"Missing name for device at line ${index} ({df_slave.at[index, 'device_ip_address_http']})") else: if null_elements_2[index] == False: - logging.debug("The slave device %s at index %d is not considered due to missing IP address \n",df_slave.at[index,'Equipment Designation'],index+2) - except Exception as e: - print(e) + logging.debug(f"Missing IP address for device at line ${index} ({df_slave.at[index, 'device_name']})") + # try: + # if null_elements_1[index] == False: + # if null_elements_2[index] == False: + # logging.debug("The slave device %s at index %d is not considered due to missing data model and missing IP address \n",df_slave.at[index,'Equipment Designation'],index+2) + # else: + # logging.debug("The slave device %s at index %d is not considered due to missing data model \n",df_slave.at[index,'Equipment Designation'],index+2) + # else: + # if null_elements_2[index] == False: + # logging.debug("The slave device %s at index %d is not considered due to missing IP address \n",df_slave.at[index,'Equipment Designation'],index+2) + # except Exception as e: + # print(e) filtered_df_slave = df_slave[df_slave['device_name'].notnull()] filtered_df_slave = filtered_df_slave[filtered_df_slave['device_ip_address_http'].notnull()] @@ -407,6 +447,8 @@ def main(): for index, row_device in filtered_df_slave.iterrows(): auth_moxa(dsh_global, row_device) + clean_modbus(dsh_global, row_device) + clean_azure(dsh_global, row_device) current_device = str(row_device['device_name']) logging.debug("Defining parameters for " + current_device + " Moxa device...") dsh = open (shell_script_name + current_device + '.sh', 'w')