check.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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.digiCamControlPath, async (event, result) => {
  104. clientStore.ipc.removeAllListeners(icpList.camera.digiCamControlPath);
  105. if(result.code === 0 && result.data){
  106. configInfoStore.updateDigiCamControlPath(result.data)
  107. }
  108. })
  109. clientStore.ipc.on(icpList.camera.connect, async (event, result) => {
  110. console.log('icpList.camera.connect');
  111. console.log(result);
  112. if (result && checkTime.value > 0) {
  113. if([-1,0,2].includes(result.status)){
  114. devices.cam_control.status = result.status;
  115. devices.cam_control.msg = result.msg;
  116. }
  117. }
  118. });
  119. }
  120. /**
  121. * 检查相机
  122. */
  123. const checkcamera = ()=>{
  124. clientStore.ipc.removeAllListeners(icpList.camera.hascamera);
  125. clientStore.ipc.send(icpList.camera.hascamera);
  126. clientStore.ipc.on(icpList.camera.hascamera, (event, result) => {
  127. if (result && checkTime.value > 0) {
  128. if([-1,0,2].includes(result.status)){
  129. devices.cam_control.status = result.status;
  130. devices.cam_control.msg = result.msg;
  131. }
  132. }
  133. console.log(result);
  134. });
  135. }
  136. //第二次才算mcu正在失败
  137. const mcuErrorCount = ref(0);
  138. /**
  139. * 通用设备检查函数
  140. * @param deviceName 设备名称
  141. * @param messageType 消息类型
  142. */
  143. const checkDevice = async (deviceName: string, messageType: string) => {
  144. try {
  145. await socketStore.connectSocket();
  146. await socketStore.sendMessage({ type: messageType });
  147. clientStore.ipc.on(icpList.socket.message+'_'+deviceName, (event, result) => {
  148. console.log(result);
  149. if (result && checkTime.value > 0) {
  150. if(deviceName === 'mcu'){
  151. if(result.status === 2 ){
  152. if(!mcu.isInitSend){
  153. socketStore.sendMessage({
  154. type: 'init_mcu',
  155. data:{
  156. value:init_mcu_type.value
  157. }
  158. });
  159. mcu.isInitSend = true
  160. mcu.status = 1
  161. }
  162. if(mcu.isInitSend && result.msg === '设备初始化完成'){
  163. devices[deviceName].status = result.status;
  164. devices[deviceName].msg = result.msg;
  165. mcu.status = 2
  166. init_mcu_type.value = false;
  167. }
  168. if(checkTime.value >= 60 && mcu.status !== 2){
  169. mcu.status == -1;
  170. }
  171. }
  172. if([-1,0].includes(result.status)){
  173. /* if(result.status === -1 ){
  174. if(mcuErrorCount.value === 0){
  175. mcuErrorCount.value++;
  176. return;
  177. }else{
  178. mcuErrorCount.value = 0
  179. }
  180. }*/
  181. devices[deviceName].status = result.status;
  182. devices[deviceName].msg = result.msg;
  183. }
  184. }
  185. if(deviceName === 'blue_tooth'){
  186. if (result.code === 0 && result.data?.data && result.data._type === 0 && typeof(result.data.data) === 'string'){
  187. blue_tooth_scan_NO.value = result.data.data
  188. // ElMessage.success('商品货号'+result.data.data+'获取成功,请根据左右脚按遥控器上的左右脚按键启动拍摄')
  189. }
  190. if([-1,0,2].includes(result.status)){
  191. devices[deviceName].status = result.status;
  192. devices[deviceName].msg = result.msg;
  193. }
  194. }
  195. }
  196. });
  197. } catch (error) {
  198. console.error(`Error checking ${deviceName}:`, error);
  199. devices[deviceName].status = -1;
  200. devices[deviceName].msg = `检查失败: ${error.message}`;
  201. }
  202. };
  203. /**
  204. * 执行所有设备检查
  205. */
  206. const checkAction = async () => {
  207. for (const deviceName of Object.keys(devices)) {
  208. switch (deviceName){
  209. case 'cam_control':
  210. if(CKCamControlInterval) clearInterval(CKCamControlInterval)
  211. await checkcamControl();
  212. break;
  213. case 'camera':
  214. // await checkcamera();
  215. break;
  216. default:
  217. await checkDevice(deviceName, devices[deviceName].msg_type);
  218. }
  219. }
  220. };
  221. /**
  222. * 重新检查所有设备
  223. */
  224. const reCheckAction = async () => {
  225. for (const device of Object.values(devices)) {
  226. device.status = 0;
  227. device.msg = "未连接";
  228. }
  229. mcu.status = -0;
  230. mcu.isInitSend = false;
  231. checkTime.value++;
  232. CKTimerInterval = setInterval(()=>{
  233. checkTime.value++;
  234. if(checkTime.value >= 60){
  235. /* if( devices.blue_tooth && devices.blue_tooth.status === 0){
  236. devices.blue_tooth.status = -1;
  237. devices.blue_tooth.msg = '遥控器未连接。';
  238. }*/
  239. if( devices.mcu.status !== 2 || mcu.status !== 2 ){
  240. devices.mcu.status = -1;
  241. mcu.status = -1;
  242. mcu.isInitSend = false;
  243. devices.mcu.msg = '拍照机连接失败';
  244. }
  245. if( devices.cam_control.status !== 2 ){
  246. devices.cam_control.status = -1;
  247. devices.cam_control.msg = '拍照机连接失败';
  248. }
  249. }
  250. },1000)
  251. await checkAction();
  252. };
  253. /**
  254. * 设置蓝牙扫描编号
  255. * @param value 蓝牙扫描编号值
  256. */
  257. function set_blue_tooth_scan_NO(value){
  258. blue_tooth_scan_NO.value = value
  259. }
  260. return {
  261. getProgress,
  262. getErrorMsg,
  263. devices,
  264. mcu,
  265. isCheckStatus,
  266. set_isCheckStatus,
  267. blue_tooth_scan_NO,
  268. set_blue_tooth_scan_NO,
  269. checkAction,
  270. reCheckAction,
  271. };
  272. });
  273. export default checkInfo;