faceWs.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import io from 'socket.io-client';
  2. import { ref, onMounted, onUnmounted, onBeforeUnmount } from 'vue';
  3. export function useWebSocket() {
  4. const address: any = import.meta.env.VITE_APP_BASE_API;
  5. const token: any = localStorage.getItem('token');
  6. const myToken: any = 'JWT ' + token;
  7. let socketFace: any = null; //ws实例对象
  8. function faceWs(callback: any) {
  9. if (socketFace == null) {
  10. socketFace = io(address + '/', { transports: ['websocket', 'polling'], query: { type: 'face', Authorization: token ? myToken : '' } });
  11. }
  12. socketFace.on('my_response', (e: any) => {
  13. callback(e);
  14. });
  15. socketFace.on('fe_device_init_result', (e: any) => {
  16. e.type = "fe_device_init_result";
  17. if (e.device_info == undefined) {
  18. closeWS();
  19. }
  20. callback(e);
  21. });
  22. socketFace.on('facecontroller_ack', (e: any) => {
  23. if (e?.cmd == 'check_facecontroller_available' && e?.code != 0) {
  24. closeWS();
  25. }
  26. if (e?.cmd == 'get_facecontroller_state' && [3, 36].includes(e.state)) {
  27. closeWS();
  28. }
  29. callback(e);
  30. });
  31. socketFace.on('facecontroller_result', (e: any) => {
  32. e.type = "facecontroller_result";
  33. callback(e);
  34. });
  35. socketFace.on('disconnect', (e: any) => {
  36. callback(e);
  37. });
  38. }
  39. /**
  40. * 发送消息
  41. */
  42. function sendMessage(type: string, data: any, callback?: () => void) {
  43. if (socketFace == null) {
  44. return false;
  45. }
  46. if (socketFace.connected) {
  47. callback = callback || function () { };
  48. socketFace.emit(type, data, callback);
  49. }
  50. }
  51. /**
  52. * 开始连接设备信息
  53. */
  54. function startDevice(data?: any, callback?: any) {
  55. sendMessage(
  56. 'fe_device_init',
  57. {
  58. data: data
  59. },
  60. () => { }
  61. );
  62. }
  63. /**
  64. * 查看人脸识别模块是否可用
  65. */
  66. function checkFace(data?: any, callback?: any) {
  67. sendMessage(
  68. 'facecontroller',
  69. {
  70. cmd: 'check_facecontroller_available',
  71. ctrl_name: `facecontroller_${data}`
  72. },
  73. () => { }
  74. );
  75. }
  76. /**
  77. * 开启人脸识别模块
  78. */
  79. function openFace(data?: any, callback?: any) {
  80. sendMessage(
  81. 'facecontroller',
  82. {
  83. cmd: 'open_facecontroller',
  84. ctrl_name: `facecontroller_${data}`
  85. },
  86. () => { }
  87. );
  88. }
  89. /**
  90. * 关闭人脸识别模块
  91. */
  92. function terminateFace(data?: any, callback?: any) {
  93. sendMessage(
  94. 'facecontroller',
  95. {
  96. cmd: 'terminate_facecontroller',
  97. ctrl_name: `facecontroller_${data}`
  98. },
  99. () => { }
  100. );
  101. }
  102. /**
  103. * 暂停人脸识别模块
  104. */
  105. function suspendFace(data?: any, callback?: any) {
  106. sendMessage(
  107. 'facecontroller',
  108. {
  109. cmd: 'suspend_facecontroller',
  110. ctrl_name: `facecontroller_${data}`
  111. },
  112. () => { }
  113. );
  114. }
  115. /**
  116. * 重启人脸识别模块
  117. */
  118. function resumeFace(data?: any, callback?: any) {
  119. sendMessage(
  120. 'facecontroller',
  121. {
  122. cmd: 'resume_facecontroller',
  123. ctrl_name: `facecontroller_${data}`
  124. },
  125. () => { }
  126. );
  127. }
  128. /**
  129. * 获取人脸识别模块状态
  130. */
  131. function getFaceState(data?: any, callback?: any) {
  132. sendMessage(
  133. 'facecontroller',
  134. {
  135. cmd: 'get_facecontroller_state',
  136. ctrl_name: `facecontroller_${data}`
  137. },
  138. () => { }
  139. );
  140. }
  141. /**
  142. * 关闭WS
  143. */
  144. function closeWS() {
  145. if (socketFace) {
  146. socketFace.close();
  147. socketFace = null;
  148. }
  149. }
  150. onBeforeUnmount(() => {
  151. closeWS();
  152. });
  153. return { faceWs, startDevice, sendMessage, checkFace, openFace, terminateFace, suspendFace, resumeFace, getFaceState };
  154. }