const axios = require('axios'); const Qs = require('qs'); const fs = require('fs'); const path = require('path'); function loadAliyunConfig() { const baseDir = (typeof process !== 'undefined' && process.pkg) ? path.dirname(process.execPath) : __dirname; const candidates = [ { path: path.join(baseDir, 'aliyun-config.json'), source: 'exedir' }, { path: path.join(__dirname, 'aliyun-config.json'), source: 'srcdir' } ]; for (const c of candidates) { try { const raw = fs.readFileSync(c.path, 'utf8'); const obj = JSON.parse(raw); // 默认值兜底 if (obj.apiBaseUrl === undefined) obj.apiBaseUrl = 'https://things.icoldchain.cn/'; if (obj.secretApiPath === undefined) obj.secretApiPath = 'device/info/getAliyunDeviceSecret'; if (obj.region === undefined) obj.region = 'cn-shanghai'; if (obj.productKeyOverride === undefined) obj.productKeyOverride = ''; if (obj.printPayload === undefined) obj.printPayload = true; obj.__meta = { source: c.source, path: c.path }; return obj; } catch (_) { /* continue */ } } const def = { apiBaseUrl: 'https://things.icoldchain.cn/', secretApiPath: 'device/info/getAliyunDeviceSecret', region: 'cn-shanghai', productKeyOverride: '', printPayload: true }; def.__meta = { source: 'default', path: null }; return def; } const aliyunCfg = loadAliyunConfig(); function getAliyunConfig() { return aliyunCfg; } function getAliyunDeviceSecret(url, deviceName) { var serve = axios({ url: url, method: 'post', baseURL: aliyunCfg.apiBaseUrl || 'https://things.icoldchain.cn/', headers: { "Content-Type": "application/x-www-form-urlencoded", }, data: Qs.stringify({ isAutoRegister: 1, deviceName: deviceName }) }) return serve; } function getDevices(url, userClient, productCode) { var serve = axios({ url: url, method: 'post', baseURL: aliyunCfg.apiBaseUrl || 'https://things.icoldchain.cn/', headers: { "Content-Type": "application/x-www-form-urlencoded", }, data: Qs.stringify({ client_code: userClient, product_code: productCode }) }) return serve; } function loadMqttConfig() { const baseDir = (typeof process !== 'undefined' && process.pkg) ? path.dirname(process.execPath) : __dirname; const candidates = [ { path: path.join(baseDir, 'mqtt-config.json'), source: 'exedir' }, { path: path.join(__dirname, 'mqtt-config.json'), source: 'srcdir' } ]; for (const c of candidates) { try { const raw = fs.readFileSync(c.path, 'utf8'); const obj = JSON.parse(raw); // 默认值兜底 if (obj.enabled === undefined) obj.enabled = false; if (obj.brokerUrl === undefined) obj.brokerUrl = 'mqtt.ihemodialysis.com'; if (obj.port === undefined) obj.port = 1883; if (obj.username === undefined) obj.username = ''; if (obj.password === undefined) obj.password = ''; if (obj.reconnectPeriod === undefined) obj.reconnectPeriod = 5000; if (obj.defaultTopicPrefix === undefined) obj.defaultTopicPrefix = 'touxiji'; obj.__meta = { source: c.source, path: c.path }; return obj; } catch (_) { /* continue */ } } const def = { enabled: false, brokerUrl: 'mqtt.ihemodialysis.com', port: 1883, username: '', password: '', reconnectPeriod: 5000, defaultTopicPrefix: 'touxiji' }; def.__meta = { source: 'default', path: null }; return def; } const mqttCfg = loadMqttConfig(); function getMqttConfig() { return mqttCfg; } module.exports = { getAliyunDeviceSecret, getDevices, getAliyunConfig, getMqttConfig }