311 lines
5.7 KiB
JavaScript
311 lines
5.7 KiB
JavaScript
import BroadcastManager from "@/core/utils/broadcast.js";
|
||
|
||
// 创建单例实例
|
||
const scanner = new BroadcastManager();
|
||
|
||
// 默认回调(只用于日志)
|
||
const defaultCallbacks = {
|
||
onResult: (result) => {
|
||
console.log('[Broadcast] 收到数据:', result.data);
|
||
}
|
||
};
|
||
|
||
// 全局回调存储器
|
||
let cachedCallbacks = {
|
||
...defaultCallbacks
|
||
};
|
||
|
||
const actions = {
|
||
/**
|
||
* 初始化广播模块(只读取配置,不自动注册)
|
||
*/
|
||
async init({
|
||
commit,
|
||
rootGetters
|
||
}) {
|
||
try {
|
||
commit('setLoading', true);
|
||
|
||
// 获取配置
|
||
const broadcastSetting = rootGetters['setting/broadcastSettings'];
|
||
|
||
// 检查是否开启广播
|
||
const allSettings = rootGetters['setting/allSettings'] || {};
|
||
const broadcastEnabled = allSettings['broadcast.enabled'] !== 'false';
|
||
|
||
if (!broadcastEnabled) {
|
||
console.log('[Broadcast] 广播功能已禁用,跳过初始化');
|
||
commit('setEnabled', false);
|
||
commit('setInitialized', true);
|
||
return false;
|
||
}
|
||
|
||
if (!broadcastSetting || !broadcastSetting.action) {
|
||
throw new Error('缺少广播配置');
|
||
}
|
||
|
||
// 构建配置
|
||
const scanConfig = {
|
||
broadcastAction: broadcastSetting.action,
|
||
dataKey: broadcastSetting.key || 'value'
|
||
};
|
||
|
||
// 只使用默认回调,不合并广播配置中的回调
|
||
// 广播配置中的callbacks字段应该用于其他用途,不是给广播模块用的
|
||
const success = scanner.setConfig(scanConfig, defaultCallbacks);
|
||
|
||
if (success) {
|
||
commit('setEnabled', true);
|
||
commit('setInitialized', true);
|
||
commit('setConfig', scanConfig);
|
||
// 不再保存回调到state,回调是独立的
|
||
}
|
||
|
||
return success;
|
||
|
||
} catch (error) {
|
||
console.error('[Broadcast] 初始化失败:', error);
|
||
commit('setEnabled', false);
|
||
commit('setError', error.message);
|
||
return false;
|
||
} finally {
|
||
commit('setLoading', false);
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 开始监听(外部调用,使用缓存的回调)
|
||
*/
|
||
start({
|
||
commit,
|
||
state
|
||
}) {
|
||
if (!state || !state.initialized) {
|
||
console.error('[Broadcast] 请先初始化模块');
|
||
return false;
|
||
}
|
||
|
||
if (!state.enabled) {
|
||
console.warn('[Broadcast] 广播功能已禁用');
|
||
return false;
|
||
}
|
||
|
||
// 确保使用最新的缓存回调
|
||
if (state.config) {
|
||
scanner.setConfig(state.config, cachedCallbacks);
|
||
}
|
||
|
||
const success = scanner.start();
|
||
if (success) {
|
||
commit('setActive', true);
|
||
}
|
||
return success;
|
||
},
|
||
|
||
/**
|
||
* 停止监听
|
||
*/
|
||
stop({
|
||
commit,
|
||
state
|
||
}) {
|
||
if (!state || !state.enabled) {
|
||
return true;
|
||
}
|
||
|
||
const success = scanner.stop();
|
||
if (success) {
|
||
commit('setActive', false);
|
||
}
|
||
return success;
|
||
},
|
||
|
||
/**
|
||
* 暂停监听
|
||
*/
|
||
pause({
|
||
commit,
|
||
state
|
||
}) {
|
||
return actions.stop({
|
||
commit,
|
||
state: state || {}
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 恢复监听
|
||
*/
|
||
resume({
|
||
commit,
|
||
state
|
||
}) {
|
||
return actions.start({
|
||
commit,
|
||
state
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 更新配置
|
||
*/
|
||
setConfig({
|
||
commit,
|
||
state
|
||
}, config) {
|
||
if (!config || !config.broadcastAction) {
|
||
throw new Error('配置必须包含broadcastAction');
|
||
}
|
||
|
||
if (!state || !state.enabled) {
|
||
console.warn('[Broadcast] 广播功能已禁用,配置更新无效');
|
||
return false;
|
||
}
|
||
|
||
// 使用缓存的回调函数
|
||
const success = scanner.setConfig(config, cachedCallbacks);
|
||
|
||
if (success) {
|
||
commit('setConfig', config);
|
||
commit('setActive', false); // 配置更新后需要重新启动
|
||
}
|
||
|
||
return success;
|
||
},
|
||
|
||
/**
|
||
* 设置回调函数(独立存储,不立即生效)
|
||
*/
|
||
setCallbacks({
|
||
commit
|
||
}, callbacks) {
|
||
// 更新全局缓存回调
|
||
cachedCallbacks = {
|
||
...defaultCallbacks,
|
||
...cachedCallbacks,
|
||
...callbacks
|
||
};
|
||
|
||
console.log('[Broadcast] 回调函数已缓存,将在下次启动时生效');
|
||
return true;
|
||
},
|
||
|
||
/**
|
||
* 更新Action
|
||
*/
|
||
setAction({
|
||
commit,
|
||
state
|
||
}, action) {
|
||
if (!action) {
|
||
throw new Error('Action不能为空');
|
||
}
|
||
|
||
if (!state || !state.enabled) {
|
||
console.warn('[Broadcast] 广播功能已禁用,Action更新无效');
|
||
return false;
|
||
}
|
||
|
||
const newConfig = {
|
||
...state.config,
|
||
broadcastAction: action
|
||
};
|
||
|
||
return actions.setConfig({
|
||
commit,
|
||
state
|
||
}, newConfig);
|
||
},
|
||
|
||
/**
|
||
* 获取状态
|
||
*/
|
||
getStatus({
|
||
state
|
||
}) {
|
||
const scannerStatus = scanner.getStatus();
|
||
const safeState = state || {};
|
||
return {
|
||
...scannerStatus,
|
||
moduleEnabled: !!safeState.enabled,
|
||
moduleInitialized: !!safeState.initialized,
|
||
moduleActive: !!safeState.active
|
||
};
|
||
},
|
||
|
||
/**
|
||
* 重置模块
|
||
*/
|
||
reset({
|
||
commit
|
||
}) {
|
||
scanner.destroy();
|
||
// 重置缓存回调
|
||
cachedCallbacks = {
|
||
...defaultCallbacks
|
||
};
|
||
commit('resetState');
|
||
},
|
||
|
||
/**
|
||
* 获取当前缓存回调
|
||
*/
|
||
getCurrentCallbacks() {
|
||
return {
|
||
...cachedCallbacks
|
||
};
|
||
}
|
||
};
|
||
|
||
const mutations = {
|
||
setLoading(state, isLoading) {
|
||
state.loading = isLoading;
|
||
},
|
||
setInitialized(state, initialized) {
|
||
state.initialized = initialized;
|
||
},
|
||
setEnabled(state, enabled) {
|
||
state.enabled = enabled;
|
||
},
|
||
setActive(state, active) {
|
||
state.active = active;
|
||
},
|
||
setConfig(state, config) {
|
||
state.config = config;
|
||
},
|
||
setError(state, error) {
|
||
state.error = error;
|
||
},
|
||
resetState(state) {
|
||
state.initialized = false;
|
||
state.enabled = false;
|
||
state.active = false;
|
||
state.loading = false;
|
||
state.config = null;
|
||
state.error = null;
|
||
}
|
||
};
|
||
|
||
const state = () => ({
|
||
initialized: false,
|
||
enabled: false, // 广播是否启用(根据配置)
|
||
active: false, // 是否正在监听
|
||
loading: false,
|
||
config: null,
|
||
error: null
|
||
});
|
||
|
||
const getters = {
|
||
isEnabled: (state) => state.enabled,
|
||
isReady: (state) => state.initialized && !state.loading,
|
||
isActive: (state) => state.active,
|
||
currentConfig: (state) => state.config
|
||
};
|
||
|
||
export default {
|
||
namespaced: true,
|
||
state,
|
||
getters,
|
||
mutations,
|
||
actions
|
||
}; |