| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- import { defineStore } from 'pinia';
- import { ref, reactive, computed } from 'vue';
- import socket from "./socket";
- import icpList from "../../utils/ipc";
- import client from "./client";
- const socketStore = socket();
- const clientStore = client();
- export const checkInfo = defineStore('checkInfo', () => {
- // 定义设备列表
- const devices = reactive({
- mcu: {
- status: 0,
- msg_type: "connect_mcu",
- msg: "未连接",
- },
- blue_tooth: {
- status: 0,
- msg_type: "connect_bluetooth",
- msg: "未连接",
- },
- cam_control: {
- status: 0,
- msg_type: "cam_control",
- msg: "未连接",
- },
- /* camera: {
- status: 0,
- msg_type: "camera",
- msg: "未连接",
- },*/
- });
- const checkTime = ref(0)
- let CKTimerInterval:any = null
- //mcu 初始化
- const mcu = reactive({
- isInitSend:false,
- status:0,
- })
- // 计算完成进度
- const getProgress = computed(() => {
- let completed = 0;
- const total = Object.keys(devices).length;
- for (const device of Object.values(devices)) {
- if (device.status === 2) completed++;
- }
- return parseFloat((completed / total * 100).toFixed(2));
- });
- // 获取错误信息
- const getErrorMsg = computed(() => {
- if(mcu.status === -1){
- clearInterval(CKTimerInterval)
- checkTime.value = 0
- mcu.isInitSend = false
- return '相机初始化失败,请重新监测或强制初始化!';
- }
- for (const device of Object.values(devices)) {
- if (device.status === -1) {
- clearInterval(CKTimerInterval)
- checkTime.value = 0
- mcu.isInitSend = false
- return device.msg;
- }
- }
- return null;
- });
- const checkcamControl = async ()=>{
- clientStore.ipc.removeAllListeners(icpList.camera.connect);
- clientStore.ipc.send(icpList.camera.connect);
- clientStore.ipc.on(icpList.camera.connect, async (event, result) => {
- console.log('icpList.camera.connect');
- console.log(result);
- if (result && checkTime.value > 0) {
- if([-1,0,2].includes(result.status)){
- devices.cam_control.status = result.status;
- devices.cam_control.msg = result.msg;
- }
- }
- });
- }
- const checkcamera = ()=>{
- clientStore.ipc.removeAllListeners(icpList.camera.hascamera);
- clientStore.ipc.send(icpList.camera.hascamera);
- clientStore.ipc.on(icpList.camera.hascamera, (event, result) => {
- if (result && checkTime.value > 0) {
- if([-1,0,2].includes(result.status)){
- devices.cam_control.status = result.status;
- devices.cam_control.msg = result.msg;
- }
- }
- console.log(result);
- });
- }
- // 通用设备检查函数
- const checkDevice = async (deviceName: string, messageType: string) => {
- try {
- await socketStore.connectSocket();
- await socketStore.sendMessage({ type: messageType });
- clientStore.ipc.on(icpList.socket.message+'_'+deviceName, (event, result) => {
- console.log(result);
- if (result && checkTime.value > 0) {
- if(deviceName === 'mcu'){
- console.log(result);
- if(result.status === 2 ){
- if(!mcu.isInitSend){
- socketStore.sendMessage({ type: 'init_mcu' });
- mcu.isInitSend = true
- mcu.status = 1
- }
- if(mcu.isInitSend && result.msg === '设备初始化完成'){
- devices[deviceName].status = result.status;
- devices[deviceName].msg = result.msg;
- mcu.status = 2
- }
- if(checkTime.value >= 60 && mcu.status !== 2){
- mcu.status == -1;
- }
- }
- if([-1,0].includes(result.status)){
- devices[deviceName].status = result.status;
- devices[deviceName].msg = result.msg;
- }
- }else if([-1,0,2].includes(result.status)){
- devices[deviceName].status = result.status;
- devices[deviceName].msg = result.msg;
- }
- }
- });
- } catch (error) {
- console.error(`Error checking ${deviceName}:`, error);
- devices[deviceName].status = -1;
- devices[deviceName].msg = `检查失败: ${error.message}`;
- }
- };
- // 执行所有设备检查
- const checkAction = async () => {
- for (const deviceName of Object.keys(devices)) {
- switch (deviceName){
- case 'cam_control':
- await checkcamControl();
- break;
- case 'camera':
- // await checkcamera();
- break;
- default:
- await checkDevice(deviceName, devices[deviceName].msg_type);
- }
- }
- };
- // 重新检查所有设备
- const reCheckAction = async () => {
- for (const device of Object.values(devices)) {
- device.status = 0;
- device.msg = "未连接";
- }
- checkTime.value++;
- CKTimerInterval = setInterval(()=>{
- checkTime.value++;
- if(checkTime.value >= 60){
- if( devices.blue_tooth && devices.blue_tooth.status === 0){
- devices.blue_tooth.status = -1;
- devices.blue_tooth.msg = '遥控器未连接。';
- }
- }
- },1000)
- await checkAction();
- };
- return {
- getProgress,
- getErrorMsg,
- mcu: devices.mcu,
- blueTooth: devices.blueTooth,
- camControl: devices.camControl,
- camera: devices.camera,
- checkAction,
- reCheckAction,
- };
- });
- export default checkInfo;
|