消毒机设备
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.3 KiB

2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
  1. import { createWebSocket } from 'libs/socket'
  2. import { nanoid } from 'nanoid'
  3. import { useSystemStore } from '@/stores/systemStore'
  4. const wsClient = createWebSocket()
  5. export async function sendCmd(resParams: System.SendCmdParams) {
  6. const { className, fnName, params = {} } = resParams
  7. const systemStore = useSystemStore()
  8. systemStore.updateConnected(wsClient.isConnected.value)
  9. const res = await wsClient.waitAndSend({
  10. messageType: 'Command',
  11. fnName,
  12. className,
  13. messageId: `msg_${nanoid()}`,
  14. params,
  15. })
  16. if (res.ackcode === 0) {
  17. return res.rely
  18. }
  19. else {
  20. throw new Error(res.message)
  21. }
  22. }
  23. export async function syncSendCmd(resParams: System.SendCmdParams) {
  24. const { className, fnName, params = {} } = resParams
  25. const systemStore = useSystemStore()
  26. systemStore.updateConnected(wsClient.isConnected.value)
  27. return await wsClient.sendRequest({
  28. messageType: 'Command',
  29. fnName,
  30. className,
  31. messageId: `msg_${nanoid()}`,
  32. params,
  33. })
  34. }
  35. // 业务状态订阅
  36. export async function subscribeEvent(fromFn: string | '*', callback: (response: Socket.WebSocketResponse) => void) {
  37. wsClient.socket.addEventListener('message', (event) => {
  38. const data = JSON.parse(event.data)
  39. if (data.messageType === 'Report' && data.fromFn === fromFn) {
  40. callback && callback(data)
  41. }
  42. })
  43. }