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

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