| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- import { defineStore } from 'pinia';
- import { ref, reactive, computed } from 'vue';
- import socket from "./socket";
- import icpList from "../../utils/ipc";
- import client from "./client";
- import {ElMessage} from "element-plus";
- 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 blue_tooth_scan_NO = ref('')
- const checkTime = ref(0)
- let CKTimerInterval:any = null
- let CKCamControlInterval:any = null
- let init_mcu_type = ref(true)
- const isCheckStatus = ref(true)
- const set_isCheckStatus = (value)=>{
- isCheckStatus.value = value
- //开始监听相机软件连接状态
- if(!value){
- CKCamControlInterval = setInterval(()=>{
- checkcamControl()
- },3000)
- }
- }
- //mcu 初始化
- const mcu = reactive({
- isInitSend:false,
- status:0,
- })
- // 计算完成进度
- const getProgress = computed(() => {
- let completed = 0;
- const total = Object.keys(devices).length -1; //去掉蓝牙
- for (const device of Object.values(devices)) {
- if (device.status === 2 && device.msg_type !== 'connect_bluetooth') completed++;
- }
- let value = parseFloat((completed / total * 100).toFixed(2));
- return value
- });
- // 获取错误信息
- 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 && device.msg_type !== 'connect_bluetooth') {
- 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);
- });
- }
- //第二次才算mcu正在失败
- const mcuErrorCount = ref(0);
- // 通用设备检查函数
- 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',
- data:{
- value:init_mcu_type.value
- }
- });
- mcu.isInitSend = true
- mcu.status = 1
- }
- if(mcu.isInitSend && result.msg === '设备初始化完成'){
- devices[deviceName].status = result.status;
- devices[deviceName].msg = result.msg;
- mcu.status = 2
- init_mcu_type.value = false;
- }
- if(checkTime.value >= 60 && mcu.status !== 2){
- mcu.status == -1;
- }
- }
- if([-1,0].includes(result.status)){
- if(result.status === -1 ){
- if(mcuErrorCount.value === 0){
- mcuErrorCount.value++;
- return;
- }else{
- mcuErrorCount.value = 0
- }
- }
- devices[deviceName].status = result.status;
- devices[deviceName].msg = result.msg;
- }
- }
- if(deviceName === 'blue_tooth'){
- if (result.code === 0 && result.data?.data && result.data._type === 0 && typeof(result.data.data) === 'string'){
- blue_tooth_scan_NO.value = result.data.data
- // ElMessage.success('商品货号'+result.data.data+'获取成功,请根据左右脚按遥控器上的左右脚按键启动拍摄')
- }
- 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':
- if(CKCamControlInterval) clearInterval(CKCamControlInterval)
- 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 = '遥控器未连接。';
- }*/
- if( devices.mcu.status !== 2 || mcu.status !== 2 ){
- devices.mcu.status = -1;
- mcu.status = -1;
- devices.blue_tooth.msg = '拍照机链接失败';
- }
- }
- },1000)
- await checkAction();
- };
- function set_blue_tooth_scan_NO(value){
- blue_tooth_scan_NO.value = value
- }
- return {
- getProgress,
- getErrorMsg,
- devices,
- mcu,
- isCheckStatus,
- set_isCheckStatus,
- blue_tooth_scan_NO,
- set_blue_tooth_scan_NO,
- checkAction,
- reCheckAction,
- };
- });
- export default checkInfo;
|