30 lines
825 B
JavaScript
30 lines
825 B
JavaScript
import fs from "fs";
|
|
import path from "path";
|
|
import { fileURLToPath } from "url";
|
|
import config from "../config.js";
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
|
|
const defaultMqttPath = path.join(__dirname, "..", "config", "mqtt.json");
|
|
|
|
export function loadMqttConfig() {
|
|
const configured = config.mqtt?.configPath?.trim();
|
|
const configPath = configured || defaultMqttPath;
|
|
if (!fs.existsSync(configPath)) {
|
|
return null;
|
|
}
|
|
const raw = fs.readFileSync(configPath, "utf-8");
|
|
return JSON.parse(raw);
|
|
}
|
|
|
|
export function resolveBrokerPassword(config) {
|
|
const envKey = config?.broker?.passwordEnv;
|
|
if (envKey && process.env[envKey]) {
|
|
return process.env[envKey];
|
|
}
|
|
if (config?.broker?.password) {
|
|
return config.broker.password;
|
|
}
|
|
return process.env.MQTT_PASSWORD || "";
|
|
}
|