测试优化实时监控中心,接收全部的机器信息 ,优化显示图标的问题,优化且任务的时候显示 规划路径
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import throttle from 'lodash/throttle';
|
||||
import dayjs from 'dayjs';
|
||||
import { wsManager } from '../WebSocketManager.ts';
|
||||
import {
|
||||
getJurisdiction,
|
||||
@@ -9,9 +10,10 @@ import envConfig from '../../../env';
|
||||
import i18n from '../../i18n';
|
||||
|
||||
export default class DeviceController {
|
||||
constructor(userInfo, messageApi) {
|
||||
constructor(userInfo, messageApi, siteId) {
|
||||
this.userInfo = userInfo;
|
||||
this.messageApi = messageApi;
|
||||
this.siteId = siteId;
|
||||
|
||||
this.wsUrl =
|
||||
envConfig.WS_URL || 'wss://ri.satabot.com/ws/';
|
||||
@@ -44,6 +46,12 @@ export default class DeviceController {
|
||||
this.onIsFinished = null;
|
||||
this.handleArrivedPoint = null;
|
||||
|
||||
// 全量设备实时状态(mapPush)连接
|
||||
this.mapPushWs = null;
|
||||
this.mapPushManualClose = false;
|
||||
this.mapPushReconnectTimer = null;
|
||||
this.onMapPush = null;
|
||||
|
||||
this.handleWsMessage = this.handleWsMessage.bind(this);
|
||||
this.handleWsStatus = this.handleWsStatus.bind(this);
|
||||
this.handleVisibilityChange = this.handleVisibilityChange.bind(this);
|
||||
@@ -78,6 +86,98 @@ export default class DeviceController {
|
||||
this.onControlStatusChange?.(false);
|
||||
}
|
||||
|
||||
// ============ 全量设备实时状态(mapPush)连接 ============
|
||||
// 进入实时监控页时连接一次,接收后端广播的所有设备状态;退出页面时调用 closeMapPush 断开。
|
||||
initMapPush(onMapPush) {
|
||||
console.log('DeviceController initMapPush: 连接全量设备 WebSocket');
|
||||
this.onMapPush = onMapPush || null;
|
||||
this.mapPushManualClose = false;
|
||||
this.connectMapPush();
|
||||
}
|
||||
|
||||
connectMapPush() {
|
||||
if (this.mapPushWs && this.mapPushWs.readyState === WebSocket.OPEN) return;
|
||||
try {
|
||||
this.mapPushManualClose = false;
|
||||
const ws = new WebSocket(this.wsUrl);
|
||||
this.mapPushWs = ws;
|
||||
|
||||
ws.onopen = () => {
|
||||
ws.send(JSON.stringify({
|
||||
type: 'mapPush',
|
||||
token: this.userInfo?.token,
|
||||
siteId: this.siteId,
|
||||
userName: this.userInfo?.username,
|
||||
deviceId: '',
|
||||
}));
|
||||
};
|
||||
|
||||
ws.onmessage = (event) => {
|
||||
let msg = null;
|
||||
try { msg = JSON.parse(event.data); } catch (e) { return; }
|
||||
|
||||
console.log('[DeviceController] 收到全量设备状态 初始状态', msg);
|
||||
if (msg?.type !== 'mapPush') return;
|
||||
const list = Array.isArray(msg?.data) && msg.data.length
|
||||
? msg.data
|
||||
: [];
|
||||
if (!list.length) return;
|
||||
|
||||
const next = {};
|
||||
list.forEach((it) => {
|
||||
const key = String(it?.id ?? '');
|
||||
if (!key) return;
|
||||
next[key] = {
|
||||
id: key,
|
||||
lng: String(it?.lng ?? it?.lon ?? it?.longitude),
|
||||
lat: String(it?.lat ?? it?.latitude),
|
||||
yaw: it?.y,
|
||||
fix: it?.f !== undefined ? String(it.f) : undefined,
|
||||
fixStatus: it?.f !== undefined ? String(it.f) : undefined,
|
||||
altitude: it?.a !== undefined ? String(it.a) : undefined,
|
||||
status: it?.s ?? it?.status,
|
||||
taskId: it?.t ?? it?.taskId ?? null,
|
||||
battery: it?.b !== undefined ? Number(it.b) : (it?.batteryLevel !== undefined ? Number(it.batteryLevel) : undefined),
|
||||
hasAlarm: !!it?.ha || !!it?.hasAlarm,
|
||||
updateTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
|
||||
};
|
||||
});
|
||||
console.log('[DeviceController] 收到全量设备状态', next);
|
||||
this.onMapPush?.(next);
|
||||
};
|
||||
|
||||
ws.onerror = () => {
|
||||
console.error('[DeviceController] mapPush WebSocket 错误');
|
||||
};
|
||||
|
||||
ws.onclose = () => {
|
||||
if (this.mapPushWs === ws) this.mapPushWs = null;
|
||||
if (!this.mapPushManualClose) {
|
||||
this.mapPushReconnectTimer = setTimeout(() => this.connectMapPush(), 5000);
|
||||
}
|
||||
};
|
||||
} catch (err) {
|
||||
console.error('[DeviceController] 连接全量设备 WebSocket 失败', err);
|
||||
this.mapPushReconnectTimer = setTimeout(() => this.connectMapPush(), 5000);
|
||||
}
|
||||
}
|
||||
|
||||
closeMapPush() {
|
||||
this.mapPushManualClose = true;
|
||||
if (this.mapPushReconnectTimer) {
|
||||
clearTimeout(this.mapPushReconnectTimer);
|
||||
this.mapPushReconnectTimer = null;
|
||||
}
|
||||
if (this.mapPushWs) {
|
||||
this.mapPushWs.onopen = null;
|
||||
this.mapPushWs.onmessage = null;
|
||||
this.mapPushWs.onerror = null;
|
||||
this.mapPushWs.onclose = null;
|
||||
try { this.mapPushWs.close(); } catch (e) { /* ignore */ }
|
||||
this.mapPushWs = null;
|
||||
}
|
||||
}
|
||||
|
||||
handleVisibilityChange() {
|
||||
if (document.visibilityState === 'hidden') {
|
||||
console.log('Page hidden, release control');
|
||||
|
||||
Reference in New Issue
Block a user