优化设备控制,提交后,实时获取设备返回状态信息
This commit is contained in:
@ -15,13 +15,13 @@
|
||||
<image :src="item.devicePic" class="IMG" mode="aspectFit"></image>
|
||||
</view>
|
||||
<view class="device-name">
|
||||
<view>设备:{{item.deviceName}}</view>
|
||||
<view>设备:{{ item.deviceName }}</view>
|
||||
<view class="ID">
|
||||
<view class="ID">ID:{{item.deviceImei}}</view>
|
||||
<view class="onlines" v-if="item.onlineStatus==1">在线</view>
|
||||
<view class="ID">ID:{{ item.deviceImei }}</view>
|
||||
<view class="onlines" v-if="item.onlineStatus == 1">在线</view>
|
||||
<!-- 离线状态 -->
|
||||
<view class="unlines" v-if="item.onlineStatus==0">离线</view>
|
||||
<view>电量:{{item.battery || '0'}}%</view>
|
||||
<view class="unlines" v-if="item.onlineStatus == 0">离线</view>
|
||||
<view>电量:{{ item.battery || '0' }}%</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
@ -47,12 +47,18 @@
|
||||
|
||||
<script>
|
||||
import CustomPopup from '@/components/CustomPopup/CustomPopup.vue'
|
||||
import {
|
||||
generateShortId
|
||||
} from '@/utils/function.js';
|
||||
import {
|
||||
deviceInfo,
|
||||
} from '@/api/common/index.js'
|
||||
import {
|
||||
deviceSendAlarmMessage
|
||||
} from '@/api/6170/callPolice.js'
|
||||
import {
|
||||
deviceRealTimeStatus //设备状态
|
||||
} from '@/api/6170/deviceControl.js'
|
||||
export default {
|
||||
components: {
|
||||
CustomPopup
|
||||
@ -66,7 +72,8 @@
|
||||
popupMessage: '确认要对所选设备开启强制报警?',
|
||||
popupConfirmText: '确认',
|
||||
popupType: 'force', // 'force' or 'cancel'
|
||||
pendingAlarmAction: ''
|
||||
pendingAlarmAction: '',
|
||||
sendInfo: ''
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
@ -75,6 +82,49 @@
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
/**
|
||||
* 获取设备状态(带自动轮询)
|
||||
* @param {number} val - 功能模式
|
||||
* @param {string} batchId - 批次ID
|
||||
* @param {number} [maxRetries=30] - 最大重试次数(约30秒)
|
||||
* @param {number} [interval=1000] - 轮询间隔(毫秒)
|
||||
*/
|
||||
async getdeviceSTatus(val, batchId, maxRetries = 30, interval =2000) {
|
||||
let retries = 0;
|
||||
const checkStatus = async () => {
|
||||
try {
|
||||
const data = {
|
||||
functionMode: val, //批量的传2
|
||||
batchId: batchId,
|
||||
typeName: this.sendInfo.typeName,
|
||||
deviceImei: this.sendInfo.deviceImei
|
||||
};
|
||||
const res = await deviceRealTimeStatus(data);
|
||||
if (res.code !== 200) {
|
||||
throw new Error(res.msg || '请求失败');
|
||||
}
|
||||
switch (res.data.functionAccess) {
|
||||
case 'OK':
|
||||
return res; // 成功完成
|
||||
case 'ACTIVE':
|
||||
if (++retries >= maxRetries) {
|
||||
throw new Error('操作超时');
|
||||
}
|
||||
await new Promise(r => setTimeout(r, interval));
|
||||
return checkStatus(); // 继续轮询
|
||||
case 'FAILED':
|
||||
throw new Error('设备操作失败');
|
||||
case 'TIMEOUT':
|
||||
throw new Error('设备响应超时');
|
||||
default:
|
||||
throw new Error('未知状态');
|
||||
}
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
return checkStatus();
|
||||
},
|
||||
onclosePopup() {
|
||||
this.showPopupFlag = false
|
||||
},
|
||||
@ -97,7 +147,7 @@
|
||||
this.loading = true;
|
||||
let data = {
|
||||
pageNum: 1,
|
||||
pageSize: 20,
|
||||
pageSize: 30,
|
||||
deviceType: deviceType
|
||||
}
|
||||
deviceInfo(data).then((res) => {
|
||||
@ -146,18 +196,40 @@
|
||||
this.showPopupFlag = true;
|
||||
this.pendingAlarmAction = 0
|
||||
},
|
||||
sendAlarmCommand(type) {
|
||||
async sendAlarmCommand(type) {
|
||||
const selectedDevices = this.deviceList.filter(item => item.checked);
|
||||
const deviceIds = selectedDevices.map(item => item.id);
|
||||
|
||||
let data = {
|
||||
deviceIds: deviceIds,
|
||||
instructValue: this.pendingAlarmAction, // '强制或者解除'
|
||||
}
|
||||
deviceSendAlarmMessage(data).then((res) => {
|
||||
if (res.code == 200) {
|
||||
uni.showToast({
|
||||
title: res.msg,
|
||||
const deviceImeiList = selectedDevices.map(item => item.deviceImei);
|
||||
let loadingShown = false;
|
||||
try {
|
||||
uni.showLoading({
|
||||
title: '报警中...',
|
||||
mask: true
|
||||
});
|
||||
loadingShown = true;
|
||||
// 2. 准备请求数据
|
||||
const batchId = generateShortId();
|
||||
const data = {
|
||||
sendMsg: this.messageToSend,
|
||||
deviceIds: deviceIds,
|
||||
batchId: batchId,
|
||||
typeName: this.sendInfo.typeName,
|
||||
deviceImeiList: deviceImeiList
|
||||
};
|
||||
// 3.人员信息
|
||||
const registerRes = await deviceSendAlarmMessage(data);
|
||||
if (registerRes.code !== 200) {
|
||||
uni.showToast({
|
||||
title: registerRes.msg,
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
}
|
||||
// 4. 获取设备状态
|
||||
const statusRes = await this.getdeviceSTatus(2, batchId);
|
||||
if (statusRes.data.functionAccess === 'OK') {
|
||||
uni.showToast({
|
||||
title: statusRes.msg,
|
||||
icon: 'none'
|
||||
});
|
||||
this.showPopupFlag = false
|
||||
@ -165,18 +237,20 @@
|
||||
setTimeout(() => {
|
||||
uni.navigateBack()
|
||||
}, 500)
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: res.msg,
|
||||
icon: 'none'
|
||||
});
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
uni.showToast({
|
||||
title: error.message,
|
||||
icon: 'none'
|
||||
});
|
||||
} finally {
|
||||
uni.hideLoading();
|
||||
this.isSending = false;
|
||||
}
|
||||
},
|
||||
// 点击确认状态
|
||||
onPopupConfirm() {
|
||||
this.sendAlarmCommand(this.popupType);
|
||||
|
||||
// 处理确认逻辑
|
||||
},
|
||||
},
|
||||
@ -184,7 +258,8 @@
|
||||
const eventChannel = this.getOpenerEventChannel();
|
||||
// 监听 'deviceSend' 事件,获取传过来的数据
|
||||
eventChannel.on('devicePolice', (data) => {
|
||||
this.getData(data.data)
|
||||
this.getData(data.data.id)
|
||||
this.sendInfo = data.data
|
||||
});
|
||||
// 如果需要向调用页面返回数据,可以触发 'ack' 事件
|
||||
eventChannel.emit('ack', {})
|
||||
|
Reference in New Issue
Block a user