Browse Source

数据变更时提交到后台

master
sige 1 year ago
parent
commit
893f3c2b2e
  1. 22
      src/components/info/ExtDeviceInfo.vue
  2. 42
      src/store/modules/websocket.js

22
src/components/info/ExtDeviceInfo.vue

@ -10,7 +10,7 @@
<p class="value dark" @click="dehumidifyBeforeDisinfectionThresholdValueEditEnable=true" <p class="value dark" @click="dehumidifyBeforeDisinfectionThresholdValueEditEnable=true"
>{{dehumidifyBeforeDisinfectionThreshold}}%</p> >{{dehumidifyBeforeDisinfectionThreshold}}%</p>
<div class="act-btn" <div class="act-btn"
:class="{disabled:!externalDeviceConnected,danger:!dehumidifyBeforeDisinfectionEnable}"
:class="{disabled:!isEditable,danger:!dehumidifyBeforeDisinfectionEnable}"
@click="actionToggleDehumidifyBeforeDisinfectionEnable" @click="actionToggleDehumidifyBeforeDisinfectionEnable"
>{{ dehumidifyBeforeDisinfectionEnable ? '使能' : '未使能'}}</div> >{{ dehumidifyBeforeDisinfectionEnable ? '使能' : '未使能'}}</div>
</div> </div>
@ -19,14 +19,14 @@
<p class="value light" @click="dehumidifyAfterDisinfectionThresholdValueEditEnable=true" <p class="value light" @click="dehumidifyAfterDisinfectionThresholdValueEditEnable=true"
>{{dehumidifyAfterDisinfectionThreshold}}%</p> >{{dehumidifyAfterDisinfectionThreshold}}%</p>
<div class="act-btn" <div class="act-btn"
:class="{disabled:!externalDeviceConnected,danger:!dehumidifyAfterDisinfectionEnable}"
:class="{disabled:!isEditable,danger:!dehumidifyAfterDisinfectionEnable}"
@click="actionToggleDehumidifyAfterDisinfectionEnable" @click="actionToggleDehumidifyAfterDisinfectionEnable"
>{{ dehumidifyAfterDisinfectionEnable ? '使能' : '未使能' }}</div> >{{ dehumidifyAfterDisinfectionEnable ? '使能' : '未使能' }}</div>
</div> </div>
<div class="line"> <div class="line">
<p class="title">消毒后降解</p> <p class="title">消毒后降解</p>
<div class="act-btn" <div class="act-btn"
:class="{disabled:!externalDeviceConnected,danger:!degradeAfterDisinfectionEnable}"
:class="{disabled:!isEditable,danger:!degradeAfterDisinfectionEnable}"
@click="actionToggleDegradeAfterDisinfectionEnable" @click="actionToggleDegradeAfterDisinfectionEnable"
>{{ degradeAfterDisinfectionEnable ? '使能' : '未使能' }}</div> >{{ degradeAfterDisinfectionEnable ? '使能' : '未使能' }}</div>
</div> </div>
@ -70,9 +70,13 @@
<script setup> <script setup>
import { useDeviceStore } from '@/store' import { useDeviceStore } from '@/store'
import { storeToRefs } from 'pinia' import { storeToRefs } from 'pinia'
import { ref } from 'vue'
import { computed, ref } from 'vue'
import Modal from 'cpns/dialogs/Modal.vue' import Modal from 'cpns/dialogs/Modal.vue'
import { useWebSocketStore } from '../../store/modules/websocket'
import { useOperatorStore } from '../../store/modules/operator'
const operatorStore = useOperatorStore();
const deviceStore = useDeviceStore() const deviceStore = useDeviceStore()
const websocketStore = useWebSocketStore();
const { const {
// 湿 // 湿
dehumidifyBeforeDisinfectionEnable, dehumidifyBeforeDisinfectionEnable,
@ -108,34 +112,42 @@ const dehumidifyBeforeDisinfectionThresholdValueEditEnable = ref(false);
const dehumidifyAfterDisinfectionThresholdValue = ref([dehumidifyAfterDisinfectionThreshold.value]); const dehumidifyAfterDisinfectionThresholdValue = ref([dehumidifyAfterDisinfectionThreshold.value]);
// 湿 // 湿
const dehumidifyAfterDisinfectionThresholdValueEditEnable = ref(false); const dehumidifyAfterDisinfectionThresholdValueEditEnable = ref(false);
//
const isEditable = computed(() => {
return [0,5].includes(operatorStore.disinfectStatus) && externalDeviceConnected.value;
});
// 使/湿 // 使/湿
function actionToggleDehumidifyBeforeDisinfectionEnable() { function actionToggleDehumidifyBeforeDisinfectionEnable() {
let status = !dehumidifyBeforeDisinfectionEnable.value; let status = !dehumidifyBeforeDisinfectionEnable.value;
deviceStore.setDehumidifyBeforeDisinfectionEnable(status); deviceStore.setDehumidifyBeforeDisinfectionEnable(status);
websocketStore.call('setDehumidifyBeforeDisinfectionEnable', {enable:status});
} }
// 使/湿 // 使/湿
function actionToggleDehumidifyAfterDisinfectionEnable() { function actionToggleDehumidifyAfterDisinfectionEnable() {
let status = !dehumidifyAfterDisinfectionEnable.value; let status = !dehumidifyAfterDisinfectionEnable.value;
deviceStore.setDehumidifyAfterDisinfectionEnable(status); deviceStore.setDehumidifyAfterDisinfectionEnable(status);
websocketStore.call('setDehumidifyAfterDisinfectionEnable', {enable:status});
} }
// 使/ // 使/
function actionToggleDegradeAfterDisinfectionEnable() { function actionToggleDegradeAfterDisinfectionEnable() {
let status = !degradeAfterDisinfectionEnable.value; let status = !degradeAfterDisinfectionEnable.value;
deviceStore.setDegradeAfterDisinfectionEnable(status); deviceStore.setDegradeAfterDisinfectionEnable(status);
websocketStore.call('setDegradeAfterDisinfectionEnable', {enable:status});
} }
// 湿 // 湿
function actionDehumidifyBeforeDisinfectionThresholdValueChange() { function actionDehumidifyBeforeDisinfectionThresholdValueChange() {
deviceStore.setDehumidifyBeforeDisinfectionThreshold(dehumidifyBeforeDisinfectionThresholdValue.value[0]); deviceStore.setDehumidifyBeforeDisinfectionThreshold(dehumidifyBeforeDisinfectionThresholdValue.value[0]);
websocketStore.call('setDehumidifyBeforeDisinfectionThreshold', {value:dehumidifyBeforeDisinfectionThresholdValue.value[0]});
} }
// 湿 // 湿
function actionDehumidifyAfterDisinfectionThresholdValueChange() { function actionDehumidifyAfterDisinfectionThresholdValueChange() {
deviceStore.setDehumidifyAfterDisinfectionThreshold(dehumidifyAfterDisinfectionThresholdValue.value[0]); deviceStore.setDehumidifyAfterDisinfectionThreshold(dehumidifyAfterDisinfectionThresholdValue.value[0]);
websocketStore.call('setDehumidifyAfterDisinfectionThreshold', {value:dehumidifyAfterDisinfectionThresholdValue.value[0]});
} }
</script> </script>

42
src/store/modules/websocket.js

@ -24,6 +24,11 @@ export const useWebSocketStore = defineStore({
socketCommandInstance: null, socketCommandInstance: null,
// 事件上报websocket 实例 // 事件上报websocket 实例
socketEventInstance: null, socketEventInstance: null,
// Call ID Counter
callIdCounter : 0,
// Call Resolve Handler Map
callPromiseHandlers : {},
} }
}, },
// actions // actions
@ -43,8 +48,23 @@ export const useWebSocketStore = defineStore({
const historyStore = useHistoryStore() const historyStore = useHistoryStore()
const sealStore = useSealStore() const sealStore = useSealStore()
init.connect() init.connect()
let $this = this;
init.ws.onmessage = function (ev) { init.ws.onmessage = function (ev) {
const { messageId, timeStamp } = JSON.parse(ev.data) const { messageId, timeStamp } = JSON.parse(ev.data)
if ( undefined !== $this.callPromiseHandlers[messageId] ) {
let response = JSON.parse(ev.data);
const handler = $this.callPromiseHandlers[messageId];
delete $this.callPromiseHandlers[messageId];
console.log('call-response', response);
if ( 0 === response.ackcode ) {
handler.reject(response.reason);
} else {
handler.resolve(response.result);
}
return;
}
console.log(JSON.parse(ev.data)) console.log(JSON.parse(ev.data))
switch (messageId) { switch (messageId) {
case 'getState': case 'getState':
@ -326,6 +346,28 @@ export const useWebSocketStore = defineStore({
sendCommandMsg(message) { sendCommandMsg(message) {
this.socketCommandInstance?.msg(message) this.socketCommandInstance?.msg(message)
}, },
// call and wait for response
call( command, params=null ) {
this.callIdCounter += 1;
if ( this.callIdCounter > 1000000 ) {
this.callIdCounter = 0;
}
const callId = `call-${this.callIdCounter}`;
return new Promise(( resolve, reject ) => {
let message = {};
message.command = command;
message.messageId = callId;
if ( null !== params ) {
message.params = params;
}
this.callPromiseHandlers[callId] = { resolve, reject, message };
console.log('call-request', message);
this.sendCommandMsg(message);
});
},
initEventSocket() { initEventSocket() {
const url = import.meta.env.VITE_BASE_WS2_URL const url = import.meta.env.VITE_BASE_WS2_URL
const init = new Socket(url) const init = new Socket(url)

Loading…
Cancel
Save