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

386 lines
15 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
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 { useHistoryStore } from './history'
  12. import { useEchartsStore } from './echarts'
  13. import { useRunningStore } from './running'
  14. import { useSealStore } from './seal'
  15. import { showSuccessToast, showFailToast } from 'vant'
  16. import { saveEchartsDataToLocal } from '@/utils'
  17. export const useWebSocketStore = defineStore({
  18. id: 'websocket', // id必填,且需要唯一
  19. // state
  20. state: () => {
  21. return {
  22. // 命令websocket 实例
  23. socketCommandInstance: null,
  24. // 事件上报websocket 实例
  25. socketEventInstance: null,
  26. }
  27. },
  28. // actions
  29. actions: {
  30. initCommandSocket() {
  31. const url = import.meta.env.VITE_BASE_WS1_URL
  32. const init = new Socket(url)
  33. const settingStore = useSettingStore()
  34. const userStore = useUserStore()
  35. const testStore = useTestStore()
  36. const deviceStore = useDeviceStore()
  37. const operatorStore = useOperatorStore()
  38. const echartsStore = useEchartsStore()
  39. const auditStore = useAuditStore()
  40. const formulaStore = useFormulaStore()
  41. const runningStore = useRunningStore()
  42. const historyStore = useHistoryStore()
  43. const sealStore = useSealStore()
  44. init.connect()
  45. init.ws.onmessage = function (ev) {
  46. const { messageId, timeStamp } = JSON.parse(ev.data)
  47. console.log(JSON.parse(ev.data))
  48. switch (messageId) {
  49. case 'getState':
  50. // 初始化完毕
  51. const { state } = JSON.parse(ev.data)
  52. const {
  53. disinfectionWorkState,
  54. disinfectionState,
  55. preHeat,
  56. preHeatRaminTimeS,
  57. estimatedRemainingTimeS,
  58. disinfection_id,
  59. isLogin,
  60. permissionLevel,
  61. sensor_data,
  62. drainingWorkState,
  63. replenishingFluidsWorkState,
  64. } = state || {}
  65. if (!isLogin) {
  66. window.location.href = 'http://127.0.0.1/#/'
  67. return
  68. }
  69. const {
  70. h2o2_1,
  71. h2o2_2,
  72. h2o2_3,
  73. humid_1,
  74. humid_2,
  75. humid_3,
  76. saturation_1,
  77. temp_1,
  78. temp_2,
  79. temp_3,
  80. airCompressor,
  81. disinfectant_volume,
  82. heatingStrip,
  83. airBlower,
  84. sprinklerPump,
  85. chargingPump,
  86. waterImmersionSensor1,
  87. waterImmersionSensor2,
  88. chargingPumpRPM,
  89. sprinklerPumpRPM,
  90. sprinklerPumpGPM,
  91. pressure,
  92. AirInletProportionalValve,
  93. AirOutletProportionalValve,
  94. } = sensor_data
  95. const { channelState } = airCompressor || {}
  96. testStore.updateAirCompressorChannel(channelState)
  97. const { nowlog, targetlog } = disinfectionState || {}
  98. deviceStore.updateTargetLog(targetlog)
  99. // 必须开始的时候才改变log 否则不懂
  100. if ([1, 2, 3, 4].includes(disinfectionWorkState)) {
  101. localStorage.setItem('logVal', targetlog)
  102. }
  103. deviceStore.updateNowLog(nowlog)
  104. if ([1, 2, 3, 4].includes(disinfectionWorkState)) {
  105. operatorStore.updateShowStartReady(false)
  106. } else {
  107. operatorStore.updateStopReady(false)
  108. }
  109. sealStore.updateAllPressure(pressure)
  110. sealStore.updateCurrentAirPressure(pressure[1])
  111. settingStore.updateSprinklerPumpGPM(sprinklerPumpGPM)
  112. settingStore.updateChargingPumpRPM(chargingPumpRPM)
  113. settingStore.updateSprinklerPumpRPM(sprinklerPumpRPM)
  114. operatorStore.updateDrainingWorkState(drainingWorkState)
  115. operatorStore.updateReplenishingFluidsWorkState(
  116. replenishingFluidsWorkState,
  117. )
  118. testStore.updateWaterImmersionSensor1(
  119. waterImmersionSensor1 == 0 ? false : true,
  120. )
  121. testStore.updateWaterImmersionSensor2(
  122. waterImmersionSensor2 == 0 ? false : true,
  123. )
  124. // 将sensor_data中的数据更新到store中
  125. testStore.updateAirCompressorObj(airCompressor)
  126. testStore.updateAirBlowerObj(airBlower)
  127. testStore.updateHeatingStripObj(heatingStrip)
  128. testStore.updateSprinklerPump(sprinklerPump)
  129. testStore.updateChargingPump(chargingPump)
  130. settingStore.updateDeviceIp('192.168.8.10')
  131. deviceStore.updateDisinfectantCapacity(disinfectant_volume)
  132. deviceStore.updateBinTemperature(temp_1)
  133. deviceStore.updateBinHumidity(humid_1)
  134. deviceStore.updateBinHP(h2o2_1)
  135. deviceStore.updateBinSaturation(saturation_1)
  136. deviceStore.updateEnvirTemperature1(temp_2)
  137. deviceStore.updateEnvirHumidity1(humid_2)
  138. deviceStore.updateEnvirHP1(h2o2_2)
  139. deviceStore.updateEnvirTemperature2(temp_3)
  140. deviceStore.updateEnvirHumidity2(humid_3)
  141. deviceStore.updateEnvirHP2(h2o2_3)
  142. userStore.updatePermission(permissionLevel)
  143. settingStore.updateInitLoading()
  144. operatorStore.updatePreHeatRaminTimeS(preHeatRaminTimeS)
  145. operatorStore.updatePreHeat(preHeat)
  146. operatorStore.updateDisinfectStatus(disinfectionWorkState)
  147. operatorStore.updateEstimatedRemainingTimeS(estimatedRemainingTimeS)
  148. operatorStore.updateDisinfectionId(disinfection_id)
  149. // 将sensor_data中的数据更新到store中
  150. if ([1, 2, 3, 4].includes(operatorStore.disinfectStatus)) {
  151. saveEchartsDataToLocal(
  152. moment().utcOffset(8).format('HH:mm'),
  153. [temp_1, humid_1, h2o2_1, saturation_1],
  154. [temp_2, humid_2, h2o2_2, saturation_1],
  155. [temp_3, humid_3, h2o2_3, saturation_1],
  156. )
  157. echartsStore.updateBinCharts(
  158. JSON.parse(localStorage.getItem('bin')),
  159. )
  160. }
  161. break
  162. case 'getAllUser':
  163. const { dbval } = JSON.parse(ev.data)
  164. console.log(dbval, 'dbva;')
  165. userStore.updateUserList(dbval)
  166. break
  167. case 'AirInletProportionalValve_getState':
  168. const { ackcode: inletValAckCode, ack: inAck } = JSON.parse(ev.data)
  169. if (inletValAckCode == 0) {
  170. // 值初始化
  171. sealStore.updateAirInletProportionalInitVal(inAck || 0)
  172. }
  173. break
  174. case 'airOutletProportionalValve_getState':
  175. const { ackcode: outletValAckCode, ack: outAck } = JSON.parse(
  176. ev.data,
  177. )
  178. if (outletValAckCode == 0) {
  179. // 值初始化
  180. sealStore.updateAirOutletProportionalInitVal(outAck || 0)
  181. }
  182. break
  183. case 'airCompressor_getPressureDirect':
  184. const { ackcode: getPressureCode, ack: pressureAck } = JSON.parse(
  185. ev.data,
  186. )
  187. if (getPressureCode == 0) {
  188. // 值初始化
  189. console.log('pressureAck', pressureAck)
  190. sealStore.updateOldAirPressure(pressureAck[0].toFixed(1) || 0)
  191. }
  192. break
  193. case 'chpasswd':
  194. const { ackcode: chpasswdCode } = JSON.parse(ev.data)
  195. if (chpasswdCode != 0) {
  196. // 修改失败
  197. showFailToast('修改失败')
  198. } else {
  199. showSuccessToast('修改成功')
  200. }
  201. break
  202. case 'startReplenishingFluids':
  203. const { ackcode: startReplenishingCode } = JSON.parse(ev.data)
  204. if (startReplenishingCode == 0) {
  205. operatorStore.updateReplenishingFluidsWorkState(1)
  206. }
  207. case 'startDraining':
  208. const { ackcode: startDrainingCode } = JSON.parse(ev.data)
  209. if (startDrainingCode == 0) {
  210. operatorStore.updateDrainingWorkState(1)
  211. }
  212. case 'startDisinfection':
  213. break
  214. case 'disinfectionLogsGetList':
  215. const { ackcode: disinfectionLogsCode, disinfectionLogList } =
  216. JSON.parse(ev.data) || {}
  217. if (disinfectionLogsCode == 0) {
  218. historyStore.updateHistoryDataList(disinfectionLogList)
  219. }
  220. break
  221. case 'disinfectionLogsGetRecord':
  222. const { ackcode: logDetailCode, record } = JSON.parse(ev.data) || {}
  223. const { content } = record || {}
  224. if (logDetailCode == 0) {
  225. historyStore.updateDetailData(content)
  226. }
  227. break
  228. case 'stopDisinfection':
  229. break
  230. case 'exportUserBehaviorRecord':
  231. settingStore.updateExportLoading(false)
  232. const { ackcode: exportUserCode } = JSON.parse(ev.data) || {}
  233. if (exportUserCode == 0) {
  234. showSuccessToast('导出成功')
  235. }
  236. if (exportUserCode == 3000) {
  237. showFailToast('未检测到U盘')
  238. }
  239. if (exportUserCode != 0 && exportUserCode != 3000) {
  240. showFailToast('导出错误')
  241. }
  242. break
  243. case 'exportDisinfectionRecord':
  244. settingStore.updateExportLoading(false)
  245. const { ackcode: disinfectionRecordCode } =
  246. JSON.parse(ev.data) || {}
  247. if (disinfectionRecordCode == 0) {
  248. showSuccessToast('导出成功')
  249. }
  250. if (disinfectionRecordCode == 3000) {
  251. showFailToast('未检测到U盘')
  252. }
  253. if (disinfectionRecordCode != 0 && disinfectionRecordCode != 3000) {
  254. showFailToast('导出错误')
  255. }
  256. break
  257. case 'getAllFormula':
  258. const { formula } = JSON.parse(ev.data) || {}
  259. const { formulas } = formula || {}
  260. formulaStore.updateFormulaList(formulas || [])
  261. break
  262. case 'getUserBehaviorRecordDescJson':
  263. const { records } = JSON.parse(ev.data) || {}
  264. const { iterms, page, total, totalpage } = records || {}
  265. auditStore.updateAuditList(iterms || [])
  266. auditStore.updateTotalPage(totalpage)
  267. auditStore.updateTotal(total)
  268. auditStore.updatePage(page)
  269. auditStore.updateAuditLoading(false)
  270. break
  271. case 'getDisinfectionConfig':
  272. const { disinfectionConfig } = JSON.parse(ev.data)
  273. runningStore.updateDisinfectionConfig(disinfectionConfig)
  274. break
  275. case 'login':
  276. const { ackcode: loginCode } = JSON.parse(ev.data) || {}
  277. if (loginCode == 50001) {
  278. showFailToast('用户不存在')
  279. }
  280. if (loginCode == 5000) {
  281. showFailToast('密码错误')
  282. }
  283. break
  284. case 'getAllRecords':
  285. break
  286. case 'getAllSetting':
  287. const { dbval: allSetting } = JSON.parse(ev.data)
  288. console.log(JSON.parse(ev.data))
  289. settingStore.updateAllSettingList(allSetting)
  290. break
  291. default:
  292. break
  293. }
  294. }
  295. this.socketCommandInstance = init
  296. },
  297. sendCommandMsg(message) {
  298. this.socketCommandInstance?.msg(message)
  299. },
  300. initEventSocket() {
  301. const url = import.meta.env.VITE_BASE_WS2_URL
  302. const init = new Socket(url)
  303. init.connect()
  304. const deviceStore = useDeviceStore()
  305. const testStore = useTestStore()
  306. const settingStore = useSettingStore()
  307. const operatorStore = useOperatorStore()
  308. const echartsStore = useEchartsStore()
  309. init.ws.onmessage = function (ev) {
  310. // console.log(JSON.parse(ev.data))
  311. const { command, timeStamp } = JSON.parse(ev.data)
  312. switch (command) {
  313. case 'RealtimeSensorDataReport':
  314. const { sensor_data } = JSON.parse(ev.data)
  315. const {
  316. h2o2_1,
  317. h2o2_2,
  318. h2o2_3,
  319. humid_1,
  320. humid_2,
  321. humid_3,
  322. temp_1,
  323. temp_2,
  324. temp_3,
  325. saturation_1,
  326. airCompressor,
  327. disinfectant_volume,
  328. heatingStrip,
  329. airBlower,
  330. sprinklerPump,
  331. chargingPump,
  332. waterImmersionSensor1,
  333. waterImmersionSensor2,
  334. } = sensor_data
  335. // 将sensor_data中的数据更新到store中
  336. testStore.updateWaterImmersionSensor1(
  337. waterImmersionSensor1 == 0 ? false : true,
  338. )
  339. testStore.updateWaterImmersionSensor2(
  340. waterImmersionSensor2 == 0 ? false : true,
  341. )
  342. testStore.updateAirCompressorObj(airCompressor)
  343. testStore.updateAirBlowerObj(airBlower)
  344. testStore.updateHeatingStripObj(heatingStrip)
  345. testStore.updateSprinklerPump(sprinklerPump)
  346. testStore.updateChargingPump(chargingPump)
  347. settingStore.updateDeviceIp('192.168.8.10')
  348. deviceStore.updateDisinfectantCapacity(disinfectant_volume)
  349. deviceStore.updateBinTemperature(temp_1)
  350. deviceStore.updateBinHumidity(humid_1)
  351. deviceStore.updateBinHP(h2o2_1)
  352. deviceStore.updateBinSaturation(saturation_1)
  353. deviceStore.updateEnvirTemperature1(temp_2)
  354. deviceStore.updateEnvirHumidity1(humid_2)
  355. deviceStore.updateEnvirHP1(h2o2_2)
  356. deviceStore.updateEnvirTemperature2(temp_3)
  357. deviceStore.updateEnvirHumidity2(humid_3)
  358. deviceStore.updateEnvirHP2(h2o2_3)
  359. // 对数据进行处理 并存入到localstorage中
  360. if ([1, 2, 3, 4].includes(operatorStore.disinfectStatus)) {
  361. saveEchartsDataToLocal(
  362. moment().utcOffset(8).format('HH:mm'),
  363. [temp_1, humid_1, h2o2_1, saturation_1],
  364. [temp_2, humid_2, h2o2_2, saturation_1],
  365. [temp_3, humid_3, h2o2_3, saturation_1],
  366. )
  367. echartsStore.updateBinCharts(
  368. JSON.parse(localStorage.getItem('bin')),
  369. )
  370. }
  371. break
  372. default:
  373. break
  374. }
  375. }
  376. this.socketEventInstance = init
  377. },
  378. sendEventMsg(message) {
  379. this.socketEventInstance?.msg(message)
  380. },
  381. },
  382. })