check.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. import {ElMessage} from "element-plus";
  7. const socketStore = socket();
  8. const clientStore = client();
  9. export const checkInfo = defineStore('checkInfo', () => {
  10. // 定义设备列表
  11. const devices = reactive({
  12. mcu: {
  13. status: 0,
  14. msg_type: "connect_mcu",
  15. msg: "未连接",
  16. },
  17. blue_tooth: {
  18. status: 0,
  19. msg_type: "connect_bluetooth",
  20. msg: "未连接",
  21. },
  22. cam_control: {
  23. status: 0,
  24. msg_type: "cam_control",
  25. msg: "未连接",
  26. },
  27. /* camera: {
  28. status: 0,
  29. msg_type: "camera",
  30. msg: "未连接",
  31. },*/
  32. });
  33. const blue_tooth_scan_NO = ref('')
  34. const checkTime = ref(0)
  35. let CKTimerInterval:any = null
  36. let CKCamControlInterval:any = null
  37. let init_mcu_type = ref(true)
  38. const isCheckStatus = ref(true)
  39. const set_isCheckStatus = (value)=>{
  40. isCheckStatus.value = value
  41. //开始监听相机软件连接状态
  42. if(!value){
  43. CKCamControlInterval = setInterval(()=>{
  44. checkcamControl()
  45. },3000)
  46. }
  47. }
  48. //mcu 初始化
  49. const mcu = reactive({
  50. isInitSend:false,
  51. status:0,
  52. })
  53. // 计算完成进度
  54. const getProgress = computed(() => {
  55. let completed = 0;
  56. const total = Object.keys(devices).length -1; //去掉蓝牙
  57. for (const device of Object.values(devices)) {
  58. if (device.status === 2 && device.msg_type !== 'connect_bluetooth') completed++;
  59. }
  60. let value = parseFloat((completed / total * 100).toFixed(2));
  61. return value
  62. });
  63. // 获取错误信息
  64. const getErrorMsg = computed(() => {
  65. if(mcu.status === -1){
  66. clearInterval(CKTimerInterval)
  67. checkTime.value = 0
  68. mcu.isInitSend = false
  69. return '拍照机初始化失败,请重新监测!';
  70. }
  71. for (const device of Object.values(devices)) {
  72. if (device.status === -1 && device.msg_type !== 'connect_bluetooth') {
  73. clearInterval(CKTimerInterval)
  74. checkTime.value = 0
  75. mcu.isInitSend = false
  76. return device.msg;
  77. }
  78. }
  79. return null;
  80. });
  81. const checkcamControl = async ()=>{
  82. clientStore.ipc.removeAllListeners(icpList.camera.connect);
  83. clientStore.ipc.send(icpList.camera.connect);
  84. clientStore.ipc.on(icpList.camera.connect, async (event, result) => {
  85. console.log('icpList.camera.connect');
  86. console.log(result);
  87. if (result && checkTime.value > 0) {
  88. if([-1,0,2].includes(result.status)){
  89. devices.cam_control.status = result.status;
  90. devices.cam_control.msg = result.msg;
  91. }
  92. }
  93. });
  94. }
  95. const checkcamera = ()=>{
  96. clientStore.ipc.removeAllListeners(icpList.camera.hascamera);
  97. clientStore.ipc.send(icpList.camera.hascamera);
  98. clientStore.ipc.on(icpList.camera.hascamera, (event, result) => {
  99. if (result && checkTime.value > 0) {
  100. if([-1,0,2].includes(result.status)){
  101. devices.cam_control.status = result.status;
  102. devices.cam_control.msg = result.msg;
  103. }
  104. }
  105. console.log(result);
  106. });
  107. }
  108. //第二次才算mcu正在失败
  109. const mcuErrorCount = ref(0);
  110. // 通用设备检查函数
  111. const checkDevice = async (deviceName: string, messageType: string) => {
  112. try {
  113. await socketStore.connectSocket();
  114. await socketStore.sendMessage({ type: messageType });
  115. clientStore.ipc.on(icpList.socket.message+'_'+deviceName, (event, result) => {
  116. console.log(result);
  117. if (result && checkTime.value > 0) {
  118. if(deviceName === 'mcu'){
  119. console.log(result);
  120. if(result.status === 2 ){
  121. if(!mcu.isInitSend){
  122. socketStore.sendMessage({
  123. type: 'init_mcu',
  124. data:{
  125. value:init_mcu_type.value
  126. }
  127. });
  128. mcu.isInitSend = true
  129. mcu.status = 1
  130. }
  131. if(mcu.isInitSend && result.msg === '设备初始化完成'){
  132. devices[deviceName].status = result.status;
  133. devices[deviceName].msg = result.msg;
  134. mcu.status = 2
  135. init_mcu_type.value = false;
  136. }
  137. if(checkTime.value >= 60 && mcu.status !== 2){
  138. mcu.status == -1;
  139. }
  140. }
  141. if([-1,0].includes(result.status)){
  142. if(result.status === -1 ){
  143. if(mcuErrorCount.value === 0){
  144. mcuErrorCount.value++;
  145. return;
  146. }else{
  147. mcuErrorCount.value = 0
  148. }
  149. }
  150. devices[deviceName].status = result.status;
  151. devices[deviceName].msg = result.msg;
  152. }
  153. }
  154. if(deviceName === 'blue_tooth'){
  155. if (result.code === 0 && result.data?.data && result.data._type === 0 && typeof(result.data.data) === 'string'){
  156. blue_tooth_scan_NO.value = result.data.data
  157. // ElMessage.success('商品货号'+result.data.data+'获取成功,请根据左右脚按遥控器上的左右脚按键启动拍摄')
  158. }
  159. if([-1,0,2].includes(result.status)){
  160. devices[deviceName].status = result.status;
  161. devices[deviceName].msg = result.msg;
  162. }
  163. }
  164. }
  165. });
  166. } catch (error) {
  167. console.error(`Error checking ${deviceName}:`, error);
  168. devices[deviceName].status = -1;
  169. devices[deviceName].msg = `检查失败: ${error.message}`;
  170. }
  171. };
  172. // 执行所有设备检查
  173. const checkAction = async () => {
  174. for (const deviceName of Object.keys(devices)) {
  175. switch (deviceName){
  176. case 'cam_control':
  177. if(CKCamControlInterval) clearInterval(CKCamControlInterval)
  178. await checkcamControl();
  179. break;
  180. case 'camera':
  181. // await checkcamera();
  182. break;
  183. default:
  184. await checkDevice(deviceName, devices[deviceName].msg_type);
  185. }
  186. }
  187. };
  188. // 重新检查所有设备
  189. const reCheckAction = async () => {
  190. for (const device of Object.values(devices)) {
  191. device.status = 0;
  192. device.msg = "未连接";
  193. }
  194. checkTime.value++;
  195. CKTimerInterval = setInterval(()=>{
  196. checkTime.value++;
  197. if(checkTime.value >= 60){
  198. /* if( devices.blue_tooth && devices.blue_tooth.status === 0){
  199. devices.blue_tooth.status = -1;
  200. devices.blue_tooth.msg = '遥控器未连接。';
  201. }*/
  202. if( devices.mcu.status !== 2 || mcu.status !== 2 ){
  203. devices.mcu.status = -1;
  204. mcu.status = -1;
  205. devices.blue_tooth.msg = '拍照机链接失败';
  206. }
  207. }
  208. },1000)
  209. await checkAction();
  210. };
  211. function set_blue_tooth_scan_NO(value){
  212. blue_tooth_scan_NO.value = value
  213. }
  214. return {
  215. getProgress,
  216. getErrorMsg,
  217. devices,
  218. mcu,
  219. isCheckStatus,
  220. set_isCheckStatus,
  221. blue_tooth_scan_NO,
  222. set_blue_tooth_scan_NO,
  223. checkAction,
  224. reCheckAction,
  225. };
  226. });
  227. export default checkInfo;