Files
DPM/src/config.js
T

64 lines
2.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* =========================================================
运行配置 —— 后端地址 / MQTT Broker
优先级:window.__DPM_*__ 全局变量(运行时可被 /api/config 引导覆盖)
> Vite 环境变量(.env.local,构建时)> 内置默认
内置默认(打包部署):Tauri 内 WebView 的 hostname 为 tauri.localhost
后端与 MQTT Broker 若与展播程序同机部署,回退到 192.168.1.9
浏览器开发/局域网访问时回退到当前主机 hostname。
========================================================= */
const env = import.meta.env || {};
/* 运行期解析(惰性读取 window.__DPM_*__,供 main.jsx bootstrap 覆盖后再生效) */
function resolveHost() {
const h = window.location.hostname || '192.168.1.9';
// Tauri 打包 WebViewtauri.localhost / localhost)→ 服务同机部署用 192.168.1.9
if (h === 'tauri.localhost' || h === 'localhost' || h === '192.168.1.9' || h.includes('tauri')) {
return '192.168.1.9';
}
return h; // 浏览器开发/局域网:当前主机 IP
}
export const API_BASE = window.__DPM_API__ || env.VITE_API_BASE || `http://${resolveHost()}:10085`;
export const MQTT_URL = window.__DPM_MQTT__ || env.VITE_MQTT_URL || `ws://${resolveHost()}:8083/mqtt`;
export const MQTT_USERNAME = window.__DPM_MQTT_USER__ || env.VITE_MQTT_USERNAME || '';
export const MQTT_PASSWORD = window.__DPM_MQTT_PASS__ || env.VITE_MQTT_PASSWORD || '';
/** 惰性读取 API 基址(允许 bootstrap 从后端 /api/config 覆盖后再生效) */
export function getApiBase() {
return window.__DPM_API__ || API_BASE;
}
/** 惰性读取 MQTT 地址(允许 bootstrap 覆盖后再生效) */
export function getMqttUrl() {
return window.__DPM_MQTT__ || MQTT_URL;
}
export function getMqttCredentials() {
return {
username: window.__DPM_MQTT_USER__ || MQTT_USERNAME,
password: window.__DPM_MQTT_PASS__ || MQTT_PASSWORD,
};
}
/** 惰性读取 s2s 实时语音 WebSocket 地址。
* 优先 config.json / VITE_VOICE_WS 显式配置;
* 否则默认跟随 `api_base` 的主机名(s2s 由 s2s_bridge 在后端同机 :8765 启动),
* 而不是页面 hostname(打包后是 tauri.localhost/192.168.1.9,会导致连本机回环而失败)。 */
export function getVoiceWsUrl() {
if (window.__DPM_VOICE_WS__) return window.__DPM_VOICE_WS__;
const baked = env.VITE_VOICE_WS;
if (baked) return baked;
let host;
try {
host = new URL(getApiBase()).hostname;
} catch {
host = resolveHost();
}
return `ws://${host}:8765/v1/realtime`;
}
export const MQTT_TOPIC_COMMAND = 'opc/display/command';
export const MQTT_TOPIC_TICK = 'opc/dashboard/tick';
export const MQTT_TOPIC_HEARTBEAT = 'opc/display/heartbeat';