#!/usr/bin/env python3 import json import paho.mqtt.client as mqtt # MQTT broker settings broker_address = "localhost" config_base = "apu_tb_config/" # List of nodes node_numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24] node_ids = [f"apu{str(num).zfill(2)}" for num in node_numbers] # Predefined configuration sets predefined_configs = { "basic_setup": { "hostname": "node", "role": "mesh_node", "tx_power": 18, "reboot_required": False }, "ptp_enable": { "services": ["ptp"], "sync_interval": 1 }, "monitor_only": { "services": ["monitoring"], "log_level": "info" } } # MQTT client setup client = mqtt.Client() client.connect(broker_address) def send_config(node_id, config_payload): try: # Validate JSON parsed_json = json.loads(config_payload) payload_json = json.dumps(parsed_json) topic = config_base + node_id client.publish(topic, payload_json) print(f"Sent to {topic}: {payload_json}") except json.JSONDecodeError: print("Invalid JSON! Please correct it.") if __name__ == "__main__": while True: print("Available nodes:") for idx, node in enumerate(node_ids): print(f"{idx}: {node}") try: selection = int(input("Select node by number (or -1 to exit): ")) if selection == -1: break node_id = node_ids[selection] print("Available predefined configs:") for idx, config_name in enumerate(predefined_configs.keys()): print(f"{idx}: {config_name}") use_predefined = input("Use predefined config? (y/n): ").strip().lower() if use_predefined == "y": config_selection = int(input("Select config by number: ")) config_keys = list(predefined_configs.keys()) selected_config = predefined_configs[config_keys[config_selection]] send_config(node_id, json.dumps(selected_config)) else: config_payload = input("Enter config data in JSON format: ") send_config(node_id, config_payload) except (ValueError, IndexError): print("Invalid selection. Please try again.") # Disconnect MQTT when done client.disconnect()