check.ts 10.0 KB

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