消毒机前端代码
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.

310 lines
11 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
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
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
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
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 moment from 'moment'
  4. import { useSettingStore } from './setting'
  5. import { useUserStore } from './user'
  6. import { useOperatorStore } from './operator'
  7. import { useDeviceStore } from './device'
  8. import { useTestStore } from './test'
  9. import { useAuditStore } from './audit'
  10. import { useFormulaStore } from './formula'
  11. import { useEchartsStore } from './echarts'
  12. import { showSuccessToast, showFailToast } from 'vant'
  13. import { saveEchartsDataToLocal } from '@/utils'
  14. export const useWebSocketStore = defineStore({
  15. id: 'websocket', // id必填,且需要唯一
  16. // state
  17. state: () => {
  18. return {
  19. // 命令websocket 实例
  20. socketCommandInstance: null,
  21. // 事件上报websocket 实例
  22. socketEventInstance: null,
  23. }
  24. },
  25. // actions
  26. actions: {
  27. initCommandSocket() {
  28. const url = import.meta.env.VITE_BASE_WS1_URL
  29. const init = new Socket(url)
  30. const settingStore = useSettingStore()
  31. const userStore = useUserStore()
  32. const testStore = useTestStore()
  33. const deviceStore = useDeviceStore()
  34. const operatorStore = useOperatorStore()
  35. const echartsStore = useEchartsStore()
  36. const auditStore = useAuditStore()
  37. const formulaStore = useFormulaStore()
  38. init.connect()
  39. init.ws.onmessage = function (ev) {
  40. const { ackcode, messageId, timeStamp } = JSON.parse(ev.data)
  41. console.log(JSON.parse(ev.data))
  42. switch (messageId) {
  43. case 'getState':
  44. // 初始化完毕
  45. const { state } = JSON.parse(ev.data)
  46. const {
  47. disinfectionWorkState,
  48. disinfectionState,
  49. preHeat,
  50. preHeatRaminTimeS,
  51. estimatedRemainingTimeS,
  52. disinfection_id,
  53. isLogin,
  54. permissionLevel,
  55. sensor_data,
  56. drainingWorkState,
  57. replenishingFluidsWorkState,
  58. } = state || {}
  59. if (!isLogin) {
  60. window.location.href = 'http://127.0.0.1/#/login'
  61. return
  62. }
  63. const {
  64. h2o2_1,
  65. h2o2_2,
  66. h2o2_3,
  67. humid_1,
  68. humid_2,
  69. humid_3,
  70. saturation_1,
  71. temp_1,
  72. temp_2,
  73. temp_3,
  74. airCompressor,
  75. disinfectant_volume,
  76. heatingStrip,
  77. airBlower,
  78. sprinklerPump,
  79. chargingPump,
  80. waterImmersionSensor1,
  81. waterImmersionSensor2,
  82. chargingPumpRPM,
  83. sprinklerPumpRPM,
  84. sprinklerPumpGPM,
  85. } = sensor_data
  86. const { nowlog } = disinfectionState || {}
  87. deviceStore.updateNowLog(nowlog)
  88. if ([1, 2, 3, 4].includes(disinfectionWorkState)) {
  89. operatorStore.updateShowStartReady(false)
  90. } else {
  91. operatorStore.updateStopReady(false)
  92. }
  93. settingStore.updateSprinklerPumpGPM(sprinklerPumpGPM)
  94. settingStore.updateChargingPumpRPM(chargingPumpRPM)
  95. settingStore.updateSprinklerPumpRPM(sprinklerPumpRPM)
  96. operatorStore.updateDrainingWorkState(drainingWorkState)
  97. operatorStore.updateReplenishingFluidsWorkState(
  98. replenishingFluidsWorkState,
  99. )
  100. testStore.updateWaterImmersionSensor1(
  101. waterImmersionSensor1 == 0 ? false : true,
  102. )
  103. testStore.updateWaterImmersionSensor2(
  104. waterImmersionSensor2 == 0 ? false : true,
  105. )
  106. // 将sensor_data中的数据更新到store中
  107. testStore.updateAirCompressorObj(airCompressor)
  108. testStore.updateAirBlowerObj(airBlower)
  109. testStore.updateHeatingStripObj(heatingStrip)
  110. testStore.updateSprinklerPump(sprinklerPump)
  111. testStore.updateChargingPump(chargingPump)
  112. settingStore.updateDeviceIp('192.168.8.10')
  113. deviceStore.updateDisinfectantCapacity(disinfectant_volume)
  114. deviceStore.updateBinTemperature(temp_1)
  115. deviceStore.updateBinHumidity(humid_1)
  116. deviceStore.updateBinHP(h2o2_1)
  117. deviceStore.updateBinSaturation(saturation_1)
  118. deviceStore.updateEnvirTemperature1(temp_2)
  119. deviceStore.updateEnvirHumidity1(humid_2)
  120. deviceStore.updateEnvirHP1(h2o2_2)
  121. deviceStore.updateEnvirTemperature2(temp_3)
  122. deviceStore.updateEnvirHumidity2(humid_3)
  123. deviceStore.updateEnvirHP2(h2o2_3)
  124. userStore.updatePermission(permissionLevel)
  125. settingStore.updateInitLoading()
  126. operatorStore.updatePreHeatRaminTimeS(preHeatRaminTimeS)
  127. operatorStore.updatePreHeat(preHeat)
  128. operatorStore.updateDisinfectStatus(disinfectionWorkState)
  129. operatorStore.updateEstimatedRemainingTimeS(estimatedRemainingTimeS)
  130. operatorStore.updateDisinfectionId(disinfection_id)
  131. // 将sensor_data中的数据更新到store中
  132. if ([1, 2, 3, 4].includes(operatorStore.disinfectStatus)) {
  133. saveEchartsDataToLocal(
  134. moment(timeStamp).format('HH:mm'),
  135. [temp_1, humid_1, h2o2_1, saturation_1],
  136. [temp_2, humid_2, h2o2_2, saturation_1],
  137. [temp_3, humid_3, h2o2_3, saturation_1],
  138. )
  139. echartsStore.updateBinCharts(
  140. JSON.parse(localStorage.getItem('bin')),
  141. )
  142. echartsStore.updateEnvir1Charts(
  143. JSON.parse(localStorage.getItem('envir1')),
  144. )
  145. echartsStore.updateEnvir2Charts(
  146. JSON.parse(localStorage.getItem('envir2')),
  147. )
  148. }
  149. break
  150. case 'getAllUser':
  151. const { dbval } = JSON.parse(ev.data)
  152. console.log(dbval, 'dbva;')
  153. userStore.updateUserList(dbval)
  154. break
  155. case 'chpasswd':
  156. if (ackcode != 0) {
  157. // 修改失败
  158. showFailToast('修改失败')
  159. } else {
  160. showSuccessToast('修改成功')
  161. }
  162. break
  163. case 'startReplenishingFluids':
  164. if (ackcode == 0) {
  165. operatorStore.updateReplenishingFluidsWorkState(1)
  166. }
  167. case 'startDraining':
  168. if (ackcode == 0) {
  169. operatorStore.updateDrainingWorkState(1)
  170. }
  171. case 'startDisinfection':
  172. break
  173. case 'stopDisinfection':
  174. break
  175. case 'exportUserBehaviorRecord':
  176. settingStore.updateExportLoading(false)
  177. break
  178. case 'exportDisinfectionRecord':
  179. settingStore.updateExportLoading(false)
  180. break
  181. case 'getAllFormula':
  182. const { formula } = JSON.parse(ev.data) || {}
  183. const { formulas } = formula || {}
  184. formulaStore.updateFormulaList(formulas || [])
  185. break
  186. case 'getUserBehaviorRecordDescJson':
  187. const { records } = JSON.parse(ev.data) || {}
  188. const { iterms, page, total, totalpage } = records || {}
  189. auditStore.updateAuditList(iterms || [])
  190. auditStore.updateTotalPage(totalpage)
  191. auditStore.updateTotal(total)
  192. if (page == 0) {
  193. page = 1
  194. }
  195. auditStore.updatePage(page)
  196. break
  197. case 'login':
  198. if (ackcode == 0) {
  199. window.location.href = 'http://127.0.0.1/'
  200. }
  201. break
  202. case 'getAllRecords':
  203. break
  204. case 'getAllSetting':
  205. const { dbval: allSetting } = JSON.parse(ev.data)
  206. console.log(JSON.parse(ev.data))
  207. settingStore.updateAllSettingList(allSetting)
  208. break
  209. default:
  210. break
  211. }
  212. }
  213. this.socketCommandInstance = init
  214. },
  215. sendCommandMsg(message) {
  216. this.socketCommandInstance?.msg(message)
  217. },
  218. initEventSocket() {
  219. const url = import.meta.env.VITE_BASE_WS2_URL
  220. const init = new Socket(url)
  221. init.connect()
  222. const deviceStore = useDeviceStore()
  223. const testStore = useTestStore()
  224. const settingStore = useSettingStore()
  225. const operatorStore = useOperatorStore()
  226. const echartsStore = useEchartsStore()
  227. init.ws.onmessage = function (ev) {
  228. // console.log(JSON.parse(ev.data))
  229. const { command, timeStamp } = JSON.parse(ev.data)
  230. switch (command) {
  231. case 'RealtimeSensorDataReport':
  232. const { sensor_data } = JSON.parse(ev.data)
  233. const {
  234. h2o2_1,
  235. h2o2_2,
  236. h2o2_3,
  237. humid_1,
  238. humid_2,
  239. humid_3,
  240. temp_1,
  241. temp_2,
  242. temp_3,
  243. saturation_1,
  244. airCompressor,
  245. disinfectant_volume,
  246. heatingStrip,
  247. airBlower,
  248. sprinklerPump,
  249. chargingPump,
  250. waterImmersionSensor1,
  251. waterImmersionSensor2,
  252. } = sensor_data
  253. // 将sensor_data中的数据更新到store中
  254. testStore.updateWaterImmersionSensor1(
  255. waterImmersionSensor1 == 0 ? false : true,
  256. )
  257. testStore.updateWaterImmersionSensor2(
  258. waterImmersionSensor2 == 0 ? false : true,
  259. )
  260. testStore.updateAirCompressorObj(airCompressor)
  261. testStore.updateAirBlowerObj(airBlower)
  262. testStore.updateHeatingStripObj(heatingStrip)
  263. testStore.updateSprinklerPump(sprinklerPump)
  264. testStore.updateChargingPump(chargingPump)
  265. settingStore.updateDeviceIp('192.168.8.10')
  266. deviceStore.updateDisinfectantCapacity(disinfectant_volume)
  267. deviceStore.updateBinTemperature(temp_1)
  268. deviceStore.updateBinHumidity(humid_1)
  269. deviceStore.updateBinHP(h2o2_1)
  270. deviceStore.updateBinSaturation(saturation_1)
  271. deviceStore.updateEnvirTemperature1(temp_2)
  272. deviceStore.updateEnvirHumidity1(humid_2)
  273. deviceStore.updateEnvirHP1(h2o2_2)
  274. deviceStore.updateEnvirTemperature2(temp_3)
  275. deviceStore.updateEnvirHumidity2(humid_3)
  276. deviceStore.updateEnvirHP2(h2o2_3)
  277. // 对数据进行处理 并存入到localstorage中
  278. if ([1, 2, 3, 4].includes(operatorStore.disinfectStatus)) {
  279. saveEchartsDataToLocal(
  280. moment(timeStamp).format('HH:mm'),
  281. [temp_1, humid_1, h2o2_1, saturation_1],
  282. [temp_2, humid_2, h2o2_2, saturation_1],
  283. [temp_3, humid_3, h2o2_3, saturation_1],
  284. )
  285. echartsStore.updateBinCharts(
  286. JSON.parse(localStorage.getItem('bin')),
  287. )
  288. echartsStore.updateEnvir1Charts(
  289. JSON.parse(localStorage.getItem('envir1')),
  290. )
  291. echartsStore.updateEnvir2Charts(
  292. JSON.parse(localStorage.getItem('envir2')),
  293. )
  294. }
  295. break
  296. default:
  297. break
  298. }
  299. }
  300. this.socketEventInstance = init
  301. },
  302. sendEventMsg(message) {
  303. this.socketEventInstance?.msg(message)
  304. },
  305. },
  306. })