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.
182 lines
6.2 KiB
182 lines
6.2 KiB
import { defineStore } from 'pinia'
|
|
import Socket from '@/socket'
|
|
import { useSettingStore } from './setting'
|
|
import { useUserStore } from './user'
|
|
import { useOperatorStore } from './operator'
|
|
import { useDeviceStore } from './device'
|
|
import { useTestStore } from './test'
|
|
|
|
export const useWebSocketStore = defineStore({
|
|
id: 'websocket', // id必填,且需要唯一
|
|
// state
|
|
state: () => {
|
|
return {
|
|
// 命令websocket 实例
|
|
socketCommandInstance: null,
|
|
// 事件上报websocket 实例
|
|
socketEventInstance: null,
|
|
}
|
|
},
|
|
// actions
|
|
actions: {
|
|
initCommandSocket() {
|
|
const url = import.meta.env.VITE_BASE_WS1_URL
|
|
const init = new Socket(url)
|
|
const settingStore = useSettingStore()
|
|
const userStore = useUserStore()
|
|
const testStore = useTestStore()
|
|
const deviceStore = useDeviceStore()
|
|
const operatorStore = useOperatorStore()
|
|
init.connect()
|
|
init.ws.onmessage = function (ev) {
|
|
// console.log(JSON.parse(ev.data))
|
|
const { ackcode, messageId } = JSON.parse(ev.data)
|
|
|
|
if (ackcode == 0) {
|
|
switch (messageId) {
|
|
case 'getState':
|
|
// 初始化完毕
|
|
const { state } = JSON.parse(ev.data)
|
|
const {
|
|
workState,
|
|
estimatedRemainingTimeS,
|
|
disinfection_id,
|
|
isLogin,
|
|
permissionLevel,
|
|
sensor_data,
|
|
} = state || {}
|
|
if (!isLogin) {
|
|
window.location.href = '/login'
|
|
return
|
|
}
|
|
const {
|
|
h2o2_1,
|
|
h2o2_2,
|
|
h2o2_3,
|
|
humid_1,
|
|
humid_2,
|
|
humid_3,
|
|
temp_1,
|
|
temp_2,
|
|
temp_3,
|
|
airCompressor,
|
|
disinfectant_volume,
|
|
heatingStrip,
|
|
airBlower,
|
|
sprinklerPump,
|
|
chargingPump,
|
|
} = sensor_data
|
|
// 将sensor_data中的数据更新到store中
|
|
testStore.updateAirCompressorObj(airCompressor)
|
|
testStore.updateAirBlowerObj(airBlower)
|
|
testStore.updateHeatingStripObj(heatingStrip)
|
|
testStore.updateSprinklerPump(sprinklerPump)
|
|
testStore.updateChargingPump(chargingPump)
|
|
settingStore.updateDeviceIp('127.0.0.1')
|
|
deviceStore.updateDisinfectantCapacity(disinfectant_volume)
|
|
deviceStore.updateBinTemperature(temp_1)
|
|
deviceStore.updateBinHumidity(humid_1)
|
|
deviceStore.updateBinHP(h2o2_1)
|
|
deviceStore.updateEnvirTemperature1(temp_2)
|
|
deviceStore.updateEnvirHumidity1(humid_2)
|
|
deviceStore.updateEnvirHP1(h2o2_2)
|
|
deviceStore.updateEnvirTemperature2(temp_3)
|
|
deviceStore.updateEnvirHumidity2(humid_3)
|
|
deviceStore.updateEnvirHP2(h2o2_3)
|
|
|
|
userStore.updatePermission(permissionLevel)
|
|
settingStore.updateInitLoading()
|
|
operatorStore.updateDisinfectStatus(workState)
|
|
operatorStore.updateEstimatedRemainingTimeS(
|
|
estimatedRemainingTimeS,
|
|
)
|
|
operatorStore.updateDisinfectionId(disinfection_id)
|
|
// 将sensor_data中的数据更新到store中
|
|
break
|
|
case 'getAllUser':
|
|
const { dbval } = JSON.parse(ev.data)
|
|
userStore.updateUserList(dbval)
|
|
break
|
|
case 'chpasswd':
|
|
break
|
|
case 'startDisinfection':
|
|
break
|
|
case 'stopDisinfection':
|
|
break
|
|
case 'login':
|
|
break
|
|
case 'getAllRecords':
|
|
break
|
|
case 'getAllSetting':
|
|
const { dbval: allSetting } = JSON.parse(ev.data)
|
|
settingStore.updateAllSettingList(allSetting)
|
|
break
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
}
|
|
this.socketCommandInstance = init
|
|
},
|
|
sendCommandMsg(message) {
|
|
this.socketCommandInstance?.msg(message)
|
|
},
|
|
initEventSocket() {
|
|
const url = import.meta.env.VITE_BASE_WS2_URL
|
|
const init = new Socket(url)
|
|
init.connect()
|
|
const deviceStore = useDeviceStore()
|
|
const testStore = useTestStore()
|
|
const settingStore = useSettingStore()
|
|
init.ws.onmessage = function (ev) {
|
|
// console.log(JSON.parse(ev.data))
|
|
const { command } = JSON.parse(ev.data)
|
|
switch (command) {
|
|
case 'RealtimeSensorDataReport':
|
|
const { sensor_data } = JSON.parse(ev.data)
|
|
const {
|
|
h2o2_1,
|
|
h2o2_2,
|
|
h2o2_3,
|
|
humid_1,
|
|
humid_2,
|
|
humid_3,
|
|
temp_1,
|
|
temp_2,
|
|
temp_3,
|
|
airCompressor,
|
|
disinfectant_volume,
|
|
heatingStrip,
|
|
airBlower,
|
|
sprinklerPump,
|
|
chargingPump,
|
|
} = sensor_data
|
|
// 将sensor_data中的数据更新到store中
|
|
testStore.updateAirCompressorObj(airCompressor)
|
|
testStore.updateAirBlowerObj(airBlower)
|
|
testStore.updateHeatingStripObj(heatingStrip)
|
|
testStore.updateSprinklerPump(sprinklerPump)
|
|
testStore.updateChargingPump(chargingPump)
|
|
settingStore.updateDeviceIp('127.0.0.1')
|
|
deviceStore.updateDisinfectantCapacity(disinfectant_volume)
|
|
deviceStore.updateBinTemperature(temp_1)
|
|
deviceStore.updateBinHumidity(humid_1)
|
|
deviceStore.updateBinHP(h2o2_1)
|
|
deviceStore.updateEnvirTemperature1(temp_2)
|
|
deviceStore.updateEnvirHumidity1(humid_2)
|
|
deviceStore.updateEnvirHP1(h2o2_2)
|
|
deviceStore.updateEnvirTemperature2(temp_3)
|
|
deviceStore.updateEnvirHumidity2(humid_3)
|
|
deviceStore.updateEnvirHP2(h2o2_3)
|
|
break
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
this.socketEventInstance = init
|
|
},
|
|
sendEventMsg(message) {
|
|
this.socketEventInstance?.msg(message)
|
|
},
|
|
},
|
|
})
|