faceWs.ts 3.3 KB

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