check.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import { defineStore } from 'pinia';
  2. import { ref, reactive, computed } from 'vue';
  3. import socket from "./socket";
  4. import icpList from "../../utils/ipc";
  5. import client from "./client";
  6. const socketStore = socket();
  7. const clientStore = client();
  8. export const checkInfo = defineStore('checkInfo', () => {
  9. // 定义设备列表
  10. const devices = reactive({
  11. mcu: {
  12. status: 0,
  13. msg_type: "connect_mcu",
  14. msg: "未连接",
  15. },
  16. blue_tooth: {
  17. status: 0,
  18. msg_type: "connect_bluetooth",
  19. msg: "未连接",
  20. },
  21. cam_control: {
  22. status: 0,
  23. msg_type: "cam_control",
  24. msg: "未连接",
  25. },
  26. /* camera: {
  27. status: 0,
  28. msg_type: "camera",
  29. msg: "未连接",
  30. },*/
  31. });
  32. const checkTime = ref(0)
  33. let CKTimerInterval:any = null
  34. //mcu 初始化
  35. const mcu = reactive({
  36. isInitSend:false,
  37. status:0,
  38. })
  39. // 计算完成进度
  40. const getProgress = computed(() => {
  41. let completed = 0;
  42. const total = Object.keys(devices).length;
  43. for (const device of Object.values(devices)) {
  44. if (device.status === 2) completed++;
  45. }
  46. return parseFloat((completed / total * 100).toFixed(2));
  47. });
  48. // 获取错误信息
  49. const getErrorMsg = computed(() => {
  50. if(mcu.status === -1){
  51. clearInterval(CKTimerInterval)
  52. checkTime.value = 0
  53. mcu.isInitSend = false
  54. return '相机初始化失败,请重新监测或强制初始化!';
  55. }
  56. for (const device of Object.values(devices)) {
  57. if (device.status === -1) {
  58. clearInterval(CKTimerInterval)
  59. checkTime.value = 0
  60. mcu.isInitSend = false
  61. return device.msg;
  62. }
  63. }
  64. return null;
  65. });
  66. const checkcamControl = async ()=>{
  67. clientStore.ipc.removeAllListeners(icpList.camera.connect);
  68. clientStore.ipc.send(icpList.camera.connect);
  69. clientStore.ipc.on(icpList.camera.connect, async (event, result) => {
  70. console.log('icpList.camera.connect');
  71. console.log(result);
  72. if (result && checkTime.value > 0) {
  73. if([-1,0,2].includes(result.status)){
  74. devices.cam_control.status = result.status;
  75. devices.cam_control.msg = result.msg;
  76. }
  77. }
  78. });
  79. }
  80. const checkcamera = ()=>{
  81. clientStore.ipc.removeAllListeners(icpList.camera.hascamera);
  82. clientStore.ipc.send(icpList.camera.hascamera);
  83. clientStore.ipc.on(icpList.camera.hascamera, (event, result) => {
  84. if (result && checkTime.value > 0) {
  85. if([-1,0,2].includes(result.status)){
  86. devices.cam_control.status = result.status;
  87. devices.cam_control.msg = result.msg;
  88. }
  89. }
  90. console.log(result);
  91. });
  92. }
  93. // 通用设备检查函数
  94. const checkDevice = async (deviceName: string, messageType: string) => {
  95. try {
  96. await socketStore.connectSocket();
  97. await socketStore.sendMessage({ type: messageType });
  98. clientStore.ipc.on(icpList.socket.message+'_'+deviceName, (event, result) => {
  99. console.log(result);
  100. if (result && checkTime.value > 0) {
  101. if(deviceName === 'mcu'){
  102. console.log(result);
  103. if(result.status === 2 ){
  104. if(!mcu.isInitSend){
  105. socketStore.sendMessage({ type: 'init_mcu' });
  106. mcu.isInitSend = true
  107. mcu.status = 1
  108. }
  109. if(mcu.isInitSend && result.msg === '设备初始化完成'){
  110. devices[deviceName].status = result.status;
  111. devices[deviceName].msg = result.msg;
  112. mcu.status = 2
  113. }
  114. if(checkTime.value >= 60 && mcu.status !== 2){
  115. mcu.status == -1;
  116. }
  117. }
  118. if([-1,0].includes(result.status)){
  119. devices[deviceName].status = result.status;
  120. devices[deviceName].msg = result.msg;
  121. }
  122. }else if([-1,0,2].includes(result.status)){
  123. devices[deviceName].status = result.status;
  124. devices[deviceName].msg = result.msg;
  125. }
  126. }
  127. });
  128. } catch (error) {
  129. console.error(`Error checking ${deviceName}:`, error);
  130. devices[deviceName].status = -1;
  131. devices[deviceName].msg = `检查失败: ${error.message}`;
  132. }
  133. };
  134. // 执行所有设备检查
  135. const checkAction = async () => {
  136. for (const deviceName of Object.keys(devices)) {
  137. switch (deviceName){
  138. case 'cam_control':
  139. await checkcamControl();
  140. break;
  141. case 'camera':
  142. // await checkcamera();
  143. break;
  144. default:
  145. await checkDevice(deviceName, devices[deviceName].msg_type);
  146. }
  147. }
  148. };
  149. // 重新检查所有设备
  150. const reCheckAction = async () => {
  151. for (const device of Object.values(devices)) {
  152. device.status = 0;
  153. device.msg = "未连接";
  154. }
  155. checkTime.value++;
  156. CKTimerInterval = setInterval(()=>{
  157. checkTime.value++;
  158. if(checkTime.value >= 60){
  159. if( devices.blue_tooth && devices.blue_tooth.status === 0){
  160. devices.blue_tooth.status = -1;
  161. devices.blue_tooth.msg = '遥控器未连接。';
  162. }
  163. }
  164. },1000)
  165. await checkAction();
  166. };
  167. return {
  168. getProgress,
  169. getErrorMsg,
  170. mcu: devices.mcu,
  171. blueTooth: devices.blueTooth,
  172. camControl: devices.camControl,
  173. camera: devices.camera,
  174. checkAction,
  175. reCheckAction,
  176. };
  177. });
  178. export default checkInfo;