大空间消毒机
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.

84 lines
2.4 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. import { defineStore } from 'pinia'
  2. import Socket from '@/socket'
  3. import { useSettingStore } from './setting'
  4. import { useUserStore } from './user'
  5. export const useWebSocketStore = defineStore({
  6. id: 'websocket', // id必填,且需要唯一
  7. // state
  8. state: () => {
  9. return {
  10. // 命令websocket 实例
  11. socketCommandInstance: null,
  12. // 事件上报websocket 实例
  13. socketEventInstance: null,
  14. }
  15. },
  16. // actions
  17. actions: {
  18. initCommandSocket() {
  19. const url = 'ws://192.168.8.115:19001/'
  20. const init = new Socket(url)
  21. const settingStore = useSettingStore()
  22. const userStore = useUserStore()
  23. init.connect()
  24. init.ws.onmessage = function (ev) {
  25. console.log(JSON.parse(ev.data))
  26. const { ackcode, messageId } = JSON.parse(ev.data)
  27. if (ackcode == 0) {
  28. switch (messageId) {
  29. case 'getState':
  30. // 初始化完毕
  31. settingStore.updateInitLoading()
  32. // 将sensor_data中的数据更新到store中
  33. break
  34. case 'getAllUser':
  35. const { dbval } = JSON.parse(ev.data)
  36. userStore.updateUserList(dbval)
  37. break
  38. case 'chpasswd':
  39. break
  40. case 'startDisinfection':
  41. break
  42. case 'stopDisinfection':
  43. break
  44. case 'login':
  45. break
  46. case 'getAllSetting':
  47. const { dbval: allSetting } = JSON.parse(ev.data)
  48. settingStore.updateAllSettingList(allSetting)
  49. break
  50. default:
  51. break
  52. }
  53. }
  54. }
  55. this.socketCommandInstance = init
  56. },
  57. sendCommandMsg(message) {
  58. this.socketCommandInstance?.msg(message)
  59. },
  60. initEventSocket() {
  61. const url = 'ws://192.168.8.115:19002/'
  62. const init = new Socket(url)
  63. init.connect()
  64. init.ws.onmessage = function (data) {
  65. // console.log(data)
  66. // const { command } = data
  67. // switch (command) {
  68. // case 'RealtimeSensorDataReport':
  69. // const { sensor_data } = data || {}
  70. // // 将sensor_data中的数据更新到store中
  71. // break
  72. // default:
  73. // break
  74. // }
  75. }
  76. this.socketEventInstance = init
  77. },
  78. sendEventMsg(message) {
  79. this.socketEventInstance?.msg(message)
  80. },
  81. },
  82. })