2026-05-20 15:12:51 +08:00
|
|
|
import throttle from 'lodash/throttle';
|
2026-05-20 09:25:58 +08:00
|
|
|
import { wsManager } from '../WebSocketManager.ts';
|
2026-05-20 15:12:51 +08:00
|
|
|
import {
|
|
|
|
|
getControlRight,
|
|
|
|
|
getJurisdiction,
|
|
|
|
|
} from '../../api/device.ts';
|
|
|
|
|
|
2026-05-20 09:25:58 +08:00
|
|
|
import envConfig from '../../../env';
|
|
|
|
|
|
|
|
|
|
export default class DeviceController {
|
|
|
|
|
constructor(userInfo, messageApi) {
|
|
|
|
|
this.userInfo = userInfo;
|
|
|
|
|
this.messageApi = messageApi;
|
|
|
|
|
|
2026-05-20 15:12:51 +08:00
|
|
|
this.wsUrl =
|
|
|
|
|
envConfig.WS_URL || 'wss://ri.satabot.com/ws/';
|
|
|
|
|
|
2026-05-20 09:25:58 +08:00
|
|
|
this.connectedDeviceSn = null;
|
2026-05-20 15:12:51 +08:00
|
|
|
|
2026-05-20 09:25:58 +08:00
|
|
|
this.requestDeviceId = '';
|
2026-05-20 15:12:51 +08:00
|
|
|
|
2026-05-20 09:25:58 +08:00
|
|
|
this.isControlled = false;
|
|
|
|
|
this.isRequestControl = false;
|
|
|
|
|
this.hasControlRight = false;
|
2026-05-20 15:12:51 +08:00
|
|
|
|
2026-05-20 09:25:58 +08:00
|
|
|
this.wsStatus = 'disconnected';
|
2026-05-20 15:12:51 +08:00
|
|
|
this.wsDeviceInfo = {};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.controlTimeout = null;
|
2026-05-20 09:25:58 +08:00
|
|
|
|
|
|
|
|
this.onDeviceInfoUpdate = null;
|
|
|
|
|
this.onControlStatusChange = null;
|
|
|
|
|
this.onWsStatusChange = null;
|
2026-05-20 15:12:51 +08:00
|
|
|
this.onHasControlRightChange = null;
|
|
|
|
|
this.onRequestControlStatusChange = null;
|
2026-05-20 09:25:58 +08:00
|
|
|
this.onPermissionRequest = null;
|
|
|
|
|
|
|
|
|
|
this.handleWsMessage = this.handleWsMessage.bind(this);
|
2026-05-20 15:12:51 +08:00
|
|
|
this.handleWsStatus = this.handleWsStatus.bind(this);
|
|
|
|
|
|
|
|
|
|
this.sendGamepad = throttle(
|
|
|
|
|
this.sendGamepad.bind(this),
|
|
|
|
|
50,
|
|
|
|
|
{
|
|
|
|
|
leading: true,
|
|
|
|
|
trailing: true,
|
|
|
|
|
},
|
|
|
|
|
);
|
2026-05-20 09:25:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init() {
|
2026-05-20 15:12:51 +08:00
|
|
|
this.unSubMessage = wsManager.onMessage(
|
|
|
|
|
this.handleWsMessage,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
this.unSubStatus = wsManager.onStatusChange(
|
|
|
|
|
this.handleWsStatus,
|
|
|
|
|
);
|
2026-05-20 09:25:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
destroy() {
|
2026-05-20 15:12:51 +08:00
|
|
|
this.clearControlTimeout();
|
|
|
|
|
|
|
|
|
|
this.unSubMessage?.();
|
|
|
|
|
this.unSubStatus?.();
|
|
|
|
|
|
2026-05-20 09:25:58 +08:00
|
|
|
wsManager.close();
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-20 15:12:51 +08:00
|
|
|
handleWsStatus(status) {
|
|
|
|
|
this.wsStatus = status;
|
|
|
|
|
|
|
|
|
|
this.onWsStatusChange?.(status);
|
|
|
|
|
|
|
|
|
|
if (status === 'disconnected') {
|
2026-05-20 09:25:58 +08:00
|
|
|
this.isControlled = false;
|
2026-05-20 15:12:51 +08:00
|
|
|
|
|
|
|
|
this.onControlStatusChange?.(false);
|
2026-05-20 09:25:58 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async connectDevice(device) {
|
|
|
|
|
if (!device) return;
|
2026-05-20 15:12:51 +08:00
|
|
|
const sn =
|
|
|
|
|
device.serialNumber || device.device_sn;
|
2026-05-20 09:25:58 +08:00
|
|
|
|
|
|
|
|
this.connectedDeviceSn = null;
|
2026-05-20 15:12:51 +08:00
|
|
|
|
2026-05-20 09:25:58 +08:00
|
|
|
this.wsDeviceInfo = {};
|
|
|
|
|
|
2026-05-20 15:12:51 +08:00
|
|
|
this.isControlled = false;
|
|
|
|
|
|
|
|
|
|
this.onControlStatusChange?.(false);
|
|
|
|
|
|
|
|
|
|
wsManager.close();
|
|
|
|
|
|
2026-05-20 09:25:58 +08:00
|
|
|
try {
|
|
|
|
|
const res = await getControlRight({
|
|
|
|
|
userName: this.userInfo.username,
|
|
|
|
|
sourceType: 1,
|
|
|
|
|
token: this.userInfo.token,
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-20 15:12:51 +08:00
|
|
|
|
|
|
|
|
this.hasControlRight = res?.code === 200;
|
|
|
|
|
|
|
|
|
|
this.onHasControlRightChange?.(
|
|
|
|
|
this.hasControlRight,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!this.hasControlRight) {
|
2026-05-20 09:25:58 +08:00
|
|
|
this.messageApi.warning('其他端正在控制');
|
2026-05-20 15:12:51 +08:00
|
|
|
return;
|
2026-05-20 09:25:58 +08:00
|
|
|
}
|
2026-05-20 15:12:51 +08:00
|
|
|
|
|
|
|
|
wsManager.connect(sn, this.wsUrl);
|
|
|
|
|
|
|
|
|
|
this.connectedDeviceSn = sn;
|
|
|
|
|
|
|
|
|
|
await this.getControl(sn);
|
2026-05-20 09:25:58 +08:00
|
|
|
} catch (err) {
|
2026-05-20 15:12:51 +08:00
|
|
|
console.error(err);
|
|
|
|
|
|
2026-05-20 09:25:58 +08:00
|
|
|
this.messageApi.error('权限校验失败');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-20 15:12:51 +08:00
|
|
|
async getControl(serialnum) {
|
|
|
|
|
try {
|
|
|
|
|
const res = await getJurisdiction({
|
|
|
|
|
platform: 'web',
|
|
|
|
|
deviceId: serialnum,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.isControlled =
|
|
|
|
|
res?.code === 200 &&
|
|
|
|
|
res?.data?.remoteControl === true;
|
|
|
|
|
|
|
|
|
|
this.onControlStatusChange?.(
|
|
|
|
|
this.isControlled,
|
|
|
|
|
);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(err);
|
|
|
|
|
|
|
|
|
|
this.isControlled = false;
|
|
|
|
|
|
|
|
|
|
this.onControlStatusChange?.(false);
|
|
|
|
|
}
|
2026-05-20 09:25:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
requestControl() {
|
2026-05-20 15:12:51 +08:00
|
|
|
if (this.isRequestControl) {
|
2026-05-20 09:25:58 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2026-05-20 15:12:51 +08:00
|
|
|
|
|
|
|
|
if (!this.hasControlRight) {
|
|
|
|
|
this.messageApi.warning('无控制权限');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-20 09:25:58 +08:00
|
|
|
this.isRequestControl = true;
|
2026-05-20 15:12:51 +08:00
|
|
|
|
|
|
|
|
this.onRequestControlStatusChange?.(true);
|
|
|
|
|
|
2026-05-20 09:25:58 +08:00
|
|
|
this.messageApi.loading({
|
|
|
|
|
key: 'control',
|
2026-05-20 15:12:51 +08:00
|
|
|
content: '请求控制中...',
|
2026-05-20 09:25:58 +08:00
|
|
|
duration: 0,
|
|
|
|
|
});
|
2026-05-20 15:12:51 +08:00
|
|
|
|
|
|
|
|
this.sendPermissionRequest(
|
|
|
|
|
true,
|
|
|
|
|
'switchPermission',
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
this.clearControlTimeout();
|
|
|
|
|
|
|
|
|
|
this.controlTimeout = setTimeout(() => {
|
|
|
|
|
if (!this.isRequestControl) return;
|
|
|
|
|
|
|
|
|
|
this.isRequestControl = false;
|
|
|
|
|
|
|
|
|
|
this.onRequestControlStatusChange?.(false);
|
|
|
|
|
|
|
|
|
|
this.messageApi.destroy('control');
|
|
|
|
|
|
|
|
|
|
this.messageApi.error('控制请求超时');
|
|
|
|
|
}, 10000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clearControlTimeout() {
|
|
|
|
|
if (this.controlTimeout) {
|
|
|
|
|
clearTimeout(this.controlTimeout);
|
|
|
|
|
this.controlTimeout = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sendPermissionRequest(data, type) {
|
|
|
|
|
wsManager.send({
|
|
|
|
|
type,
|
|
|
|
|
switchResult: data,
|
|
|
|
|
token: this.userInfo.token,
|
|
|
|
|
deviceId:
|
|
|
|
|
type === 'switchPermission'
|
|
|
|
|
? this.connectedDeviceSn
|
|
|
|
|
: this.requestDeviceId,
|
|
|
|
|
userName: this.userInfo.username,
|
|
|
|
|
});
|
2026-05-20 09:25:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleWsMessage(msg) {
|
|
|
|
|
switch (msg.type) {
|
2026-05-20 15:12:51 +08:00
|
|
|
case 'deviceInfo': {
|
2026-05-20 09:25:58 +08:00
|
|
|
const data = {};
|
2026-05-20 15:12:51 +08:00
|
|
|
|
|
|
|
|
msg.data.forEach((item) => {
|
|
|
|
|
data[item.name] = item.value;
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-20 09:25:58 +08:00
|
|
|
this.wsDeviceInfo = data;
|
2026-05-20 15:12:51 +08:00
|
|
|
|
|
|
|
|
this.onDeviceInfoUpdate?.(data);
|
|
|
|
|
|
2026-05-20 09:25:58 +08:00
|
|
|
break;
|
2026-05-20 15:12:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case 'AUTH': {
|
|
|
|
|
if (msg.isAuth) {
|
|
|
|
|
this.getControl(this.connectedDeviceSn);
|
|
|
|
|
}
|
2026-05-20 09:25:58 +08:00
|
|
|
|
|
|
|
|
break;
|
2026-05-20 15:12:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case 'switchResult': {
|
2026-06-01 15:18:01 +08:00
|
|
|
console.log(msg, "msg2222222222")
|
|
|
|
|
|
2026-05-20 15:12:51 +08:00
|
|
|
this.clearControlTimeout();
|
|
|
|
|
|
|
|
|
|
this.isRequestControl = false;
|
|
|
|
|
|
|
|
|
|
this.onRequestControlStatusChange?.(false);
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
msg.success === false ||
|
|
|
|
|
msg.switchResult === false
|
|
|
|
|
) {
|
|
|
|
|
this.isControlled = false;
|
|
|
|
|
|
|
|
|
|
this.onControlStatusChange?.(false);
|
|
|
|
|
|
|
|
|
|
this.messageApi.error({
|
|
|
|
|
key: 'control',
|
|
|
|
|
content: msg.message || '控制失败',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.isControlled = true;
|
|
|
|
|
|
|
|
|
|
this.onControlStatusChange?.(true);
|
2026-05-20 09:25:58 +08:00
|
|
|
|
|
|
|
|
this.messageApi.success({
|
|
|
|
|
key: 'control',
|
2026-05-20 15:12:51 +08:00
|
|
|
content: '控制成功',
|
2026-05-20 09:25:58 +08:00
|
|
|
});
|
2026-05-20 15:12:51 +08:00
|
|
|
|
2026-05-20 09:25:58 +08:00
|
|
|
break;
|
2026-05-20 15:12:51 +08:00
|
|
|
}
|
2026-05-20 09:25:58 +08:00
|
|
|
|
2026-05-20 15:12:51 +08:00
|
|
|
case 'switchPermission': {
|
2026-06-01 15:18:01 +08:00
|
|
|
console.log(msg, "msg111111111111111")
|
2026-05-20 15:12:51 +08:00
|
|
|
const platform = (
|
|
|
|
|
msg.platform || ''
|
|
|
|
|
).toLowerCase();
|
|
|
|
|
|
|
|
|
|
if (!platform || platform === 'web' || platform == "null") {
|
|
|
|
|
return;
|
2026-05-20 09:25:58 +08:00
|
|
|
}
|
2026-05-20 15:12:51 +08:00
|
|
|
|
|
|
|
|
this.requestDeviceId = msg.deviceId;
|
|
|
|
|
|
|
|
|
|
this.onPermissionRequest?.({
|
|
|
|
|
title: '权限请求',
|
|
|
|
|
content: `${msg.platform} 请求控制设备`,
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-20 09:25:58 +08:00
|
|
|
break;
|
2026-05-20 15:12:51 +08:00
|
|
|
}
|
2026-05-20 09:25:58 +08:00
|
|
|
|
|
|
|
|
case 'detectionReport':
|
|
|
|
|
if (msg.data?.deviceId !== this.connectedDeviceSn) return;
|
|
|
|
|
const { isSecure, obstacle } = msg.data;
|
|
|
|
|
if (isSecure) return;
|
|
|
|
|
const type = obstacle?.type;
|
|
|
|
|
const distance = (obstacle?.distance || 0).toFixed(2);
|
|
|
|
|
const types = { 0: '未知', 1: '人', 2: '车辆', 3: '围栏', 4: '坑洞', 5: '草丛' };
|
|
|
|
|
this.messageApi.warning({
|
|
|
|
|
key: 'obstacle-warning',
|
|
|
|
|
content: `检测到 ${types[type] ?? '未知物'},距离 ${distance}m`,
|
|
|
|
|
duration: 3,
|
|
|
|
|
});
|
|
|
|
|
break;
|
2026-05-20 15:12:51 +08:00
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2026-05-20 09:25:58 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sendGamepad(state) {
|
2026-05-20 15:12:51 +08:00
|
|
|
if (
|
|
|
|
|
!this.isControlled ||
|
|
|
|
|
!this.connectedDeviceSn
|
|
|
|
|
) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
console.log(state, "state")
|
|
|
|
|
|
2026-05-20 09:25:58 +08:00
|
|
|
wsManager.send({
|
2026-05-20 15:12:51 +08:00
|
|
|
type: 'remoteControl',
|
2026-05-21 09:47:24 +08:00
|
|
|
remoteData: state,
|
|
|
|
|
token: this.userInfo.token,
|
|
|
|
|
userName: this.userInfo.username,
|
|
|
|
|
|
2026-05-20 09:25:58 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|