faceWs.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. callback(e);
  24. });
  25. socketFace.on('facecontroller_result', (e: any) => {
  26. e.type = "facecontroller_result";
  27. callback(e);
  28. });
  29. socketFace.on('disconnect', (e: any) => {
  30. let obj = { type: "disconnect" };
  31. callback(obj);
  32. });
  33. }
  34. /**
  35. * 发送消息
  36. */
  37. function sendMessage(type: string, data: any, callback?: () => void) {
  38. if (socketFace == null) {
  39. return false;
  40. }
  41. if (socketFace.connected) {
  42. callback = callback || function () { };
  43. socketFace.emit(type, data, callback);
  44. }
  45. }
  46. /**
  47. * 开始连接设备信息
  48. */
  49. function startDevice(data?: any, callback?: any) {
  50. sendMessage(
  51. 'fe_device_init',
  52. {
  53. data: data
  54. },
  55. () => { }
  56. );
  57. }
  58. /**
  59. * 查看人脸识别模块是否可用
  60. */
  61. function checkFace(data?: any, callback?: any) {
  62. sendMessage(
  63. 'facecontroller',
  64. {
  65. cmd: 'check_facecontroller_available',
  66. ctrl_name: `facecontroller_${data}`
  67. },
  68. () => { }
  69. );
  70. }
  71. /**
  72. * 开启人脸识别模块
  73. */
  74. function openFace(data?: any, callback?: any) {
  75. sendMessage(
  76. 'facecontroller',
  77. {
  78. cmd: 'open_facecontroller',
  79. ctrl_name: `facecontroller_${data}`
  80. },
  81. () => { }
  82. );
  83. }
  84. /**
  85. * 关闭人脸识别模块
  86. */
  87. function terminateFace(data?: any, callback?: any) {
  88. sendMessage(
  89. 'facecontroller',
  90. {
  91. cmd: 'terminate_facecontroller',
  92. ctrl_name: `facecontroller_${data}`
  93. },
  94. () => {
  95. }
  96. );
  97. }
  98. /**
  99. * 暂停人脸识别模块
  100. */
  101. function suspendFace(data?: any, callback?: any) {
  102. sendMessage(
  103. 'facecontroller',
  104. {
  105. cmd: 'suspend_facecontroller',
  106. ctrl_name: `facecontroller_${data}`
  107. },
  108. () => { }
  109. );
  110. }
  111. /**
  112. * 重启人脸识别模块
  113. */
  114. function resumeFace(data?: any, callback?: any) {
  115. sendMessage(
  116. 'facecontroller',
  117. {
  118. cmd: 'resume_facecontroller',
  119. ctrl_name: `facecontroller_${data}`
  120. },
  121. () => { }
  122. );
  123. }
  124. /**
  125. * 获取人脸识别模块状态
  126. */
  127. function getFaceState(data?: any, callback?: any) {
  128. sendMessage(
  129. 'facecontroller',
  130. {
  131. cmd: 'get_facecontroller_state',
  132. ctrl_name: `facecontroller_${data}`
  133. },
  134. () => { }
  135. );
  136. }
  137. /**
  138. * 关闭WS
  139. */
  140. function closeWS() {
  141. if (socketFace) {
  142. socketFace.close();
  143. socketFace = null;
  144. }
  145. }
  146. onBeforeUnmount(() => {
  147. closeWS();
  148. });
  149. return { faceWs, startDevice, sendMessage, checkFace, openFace, terminateFace, suspendFace, resumeFace, getFaceState, closeWS };
  150. }