// config.ts export interface MqttBrokerConfig { host: string; port: number; vdaInterface: string; } export interface VehicleConfig { serialNumber: string; channelName: string; channelManufacturer: string; manufacturer: string; vdaVersion: string; vdaFullVersion: string; } export interface Settings { mapId: string; stateFrequency: number; visualizationFrequency: number; actionTime: number; robotCount: number; speed: number; } export interface RawConfig { mqttBroker: MqttBrokerConfig; vehicle: VehicleConfig; settings: Settings; } export async function loadConfig(): Promise { // 配置文件可能的路径列表 const configPaths = [ // 1. 当前工作目录 "./simulator_config.json", // 2. 相对于模块的路径(开发环境) new URL("./simulator_config.json", import.meta.url).pathname, // 3. 可执行文件同目录 "./simulator_config.json", // 4. 上级目录 "../simulator_config.json", // 5. 用户主目录 `${Deno.env.get("HOME") || Deno.env.get("USERPROFILE")}/simulator_config.json`, ]; let configContent: string | null = null; let usedPath: string = ""; // 尝试从多个路径加载配置文件 for (const configPath of configPaths) { try { console.log(`🔍 Trying to load config from: ${configPath}`); configContent = await Deno.readTextFile(configPath); usedPath = configPath; console.log(`✅ Successfully loaded config from: ${configPath}`); break; } catch (error) { console.log(`❌ Failed to load config from ${configPath}: ${(error as Error).message}`); continue; } } // 如果所有路径都失败,创建默认配置 if (!configContent) { console.log("⚠️ No config file found, creating default configuration..."); const defaultConfig = { mqtt_broker: { host: "localhost", port: "1883", vda_interface: "uagv" }, vehicle: { serial_number: "AGV", channel_name: "AGV_Channel", channel_manufacturer: "Default", manufacturer: "Default Manufacturer", vda_version: "2.0.0", vda_full_version: "2.0.0" }, settings: { map_id: "default_map", state_frequency: 1000, visualization_frequency: 1000, action_time: 5000, robot_count: 1, speed: 1.0 } }; // 尝试保存默认配置到当前目录 try { const defaultConfigJson = JSON.stringify(defaultConfig, null, 2); await Deno.writeTextFile("./simulator_config.json", defaultConfigJson); console.log("📝 Created default config file: ./simulator_config.json"); configContent = defaultConfigJson; usedPath = "./simulator_config.json"; } catch (error) { console.log("⚠️ Could not save default config file, using in-memory configuration"); configContent = JSON.stringify(defaultConfig); usedPath = "default (in-memory)"; } } console.log(`📋 Using configuration from: ${usedPath}`); const raw = JSON.parse(configContent); return { mqttBroker: { host: raw.mqtt_broker.host, port: parseInt(raw.mqtt_broker.port, 10), vdaInterface: raw.mqtt_broker.vda_interface, }, vehicle: { serialNumber: raw.vehicle.serial_number, channelName: raw.vehicle.channel_name, channelManufacturer: raw.vehicle.channel_manufacturer, manufacturer: raw.vehicle.manufacturer, vdaVersion: raw.vehicle.vda_version, vdaFullVersion: raw.vehicle.vda_full_version, }, settings: { mapId: raw.settings.map_id, stateFrequency: raw.settings.state_frequency, visualizationFrequency: raw.settings.visualization_frequency, actionTime: raw.settings.action_time, robotCount: raw.settings.robot_count, speed: raw.settings.speed, }, }; }