控制
This commit is contained in:
@@ -1,73 +1,99 @@
|
||||
// src/components/device/deviceController.js
|
||||
import { message } from 'antd';
|
||||
import throttle from 'lodash/throttle';
|
||||
import { wsManager } from '../WebSocketManager.ts';
|
||||
import { getControlRight, getJurisdiction } from '../../api/device.ts';
|
||||
import {
|
||||
getControlRight,
|
||||
getJurisdiction,
|
||||
} from '../../api/device.ts';
|
||||
|
||||
import envConfig from '../../../env';
|
||||
|
||||
export default class DeviceController {
|
||||
constructor(userInfo, messageApi) {
|
||||
this.userInfo = userInfo;
|
||||
this.messageApi = messageApi;
|
||||
this.wsUrl = envConfig.WS_URL || 'wss://ri.satabot.com/ws/';
|
||||
|
||||
// 状态
|
||||
this.wsUrl =
|
||||
envConfig.WS_URL || 'wss://ri.satabot.com/ws/';
|
||||
|
||||
this.connectedDeviceSn = null;
|
||||
|
||||
this.requestDeviceId = '';
|
||||
|
||||
this.isControlled = false;
|
||||
this.isRequestControl = false;
|
||||
this.hasControlRight = false;
|
||||
this.wsDeviceInfo = {};
|
||||
this.wsStatus = 'disconnected';
|
||||
|
||||
// 回调
|
||||
this.wsStatus = 'disconnected';
|
||||
this.wsDeviceInfo = {};
|
||||
|
||||
|
||||
this.controlTimeout = null;
|
||||
|
||||
this.onDeviceInfoUpdate = null;
|
||||
this.onControlStatusChange = null;
|
||||
this.onWsStatusChange = null;
|
||||
this.onHasControlRightChange = null;
|
||||
this.onRequestControlStatusChange = null;
|
||||
this.onPermissionRequest = null;
|
||||
|
||||
// 绑定 this
|
||||
this.handleWsMessage = this.handleWsMessage.bind(this);
|
||||
this.getControl = this.getControl.bind(this);
|
||||
this.connectDevice = this.connectDevice.bind(this);
|
||||
this.sendPermissionRequest = this.sendPermissionRequest.bind(this);
|
||||
this.handleWsStatus = this.handleWsStatus.bind(this);
|
||||
|
||||
this.sendGamepad = throttle(
|
||||
this.sendGamepad.bind(this),
|
||||
50,
|
||||
{
|
||||
leading: true,
|
||||
trailing: true,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// 初始化监听
|
||||
init() {
|
||||
wsManager.onMessage(this.handleWsMessage);
|
||||
wsManager.onStatusChange((status) => {
|
||||
this.wsStatus = status;
|
||||
if (this.onWsStatusChange) this.onWsStatusChange(status);
|
||||
});
|
||||
this.unSubMessage = wsManager.onMessage(
|
||||
this.handleWsMessage,
|
||||
);
|
||||
|
||||
this.unSubStatus = wsManager.onStatusChange(
|
||||
this.handleWsStatus,
|
||||
);
|
||||
}
|
||||
|
||||
// 销毁
|
||||
destroy() {
|
||||
this.clearControlTimeout();
|
||||
|
||||
this.unSubMessage?.();
|
||||
this.unSubStatus?.();
|
||||
|
||||
wsManager.close();
|
||||
}
|
||||
|
||||
// 获取设备控制权限
|
||||
async getControl(serialnum) {
|
||||
try {
|
||||
const data = { platform: 'web', deviceId: serialnum };
|
||||
const res = await getJurisdiction(data);
|
||||
const ok = res?.code === 200 && res?.data?.remoteControl === true;
|
||||
this.isControlled = ok;
|
||||
if (this.onControlStatusChange) this.onControlStatusChange(ok);
|
||||
} catch (err) {
|
||||
handleWsStatus(status) {
|
||||
this.wsStatus = status;
|
||||
|
||||
this.onWsStatusChange?.(status);
|
||||
|
||||
if (status === 'disconnected') {
|
||||
this.isControlled = false;
|
||||
if (this.onControlStatusChange) this.onControlStatusChange(false);
|
||||
|
||||
this.onControlStatusChange?.(false);
|
||||
}
|
||||
}
|
||||
|
||||
// 连接设备 WebSocket
|
||||
async connectDevice(device) {
|
||||
if (!device) return;
|
||||
const sn = device.serialNumber || device.device_sn;
|
||||
const sn =
|
||||
device.serialNumber || device.device_sn;
|
||||
|
||||
this.connectedDeviceSn = null;
|
||||
|
||||
this.wsDeviceInfo = {};
|
||||
|
||||
this.isControlled = false;
|
||||
|
||||
this.onControlStatusChange?.(false);
|
||||
|
||||
wsManager.close();
|
||||
this.connectedDeviceSn = null;
|
||||
this.wsDeviceInfo = {};
|
||||
|
||||
try {
|
||||
const res = await getControlRight({
|
||||
@@ -76,23 +102,103 @@ export default class DeviceController {
|
||||
token: this.userInfo.token,
|
||||
});
|
||||
|
||||
if (res?.code === 200) {
|
||||
this.hasControlRight = true;
|
||||
wsManager.connect(sn, this.wsUrl);
|
||||
this.connectedDeviceSn = sn;
|
||||
await this.getControl(sn);
|
||||
} else {
|
||||
this.hasControlRight = false;
|
||||
|
||||
this.hasControlRight = res?.code === 200;
|
||||
|
||||
this.onHasControlRightChange?.(
|
||||
this.hasControlRight,
|
||||
);
|
||||
|
||||
if (!this.hasControlRight) {
|
||||
this.messageApi.warning('其他端正在控制');
|
||||
return;
|
||||
}
|
||||
|
||||
wsManager.connect(sn, this.wsUrl);
|
||||
|
||||
this.connectedDeviceSn = sn;
|
||||
|
||||
await this.getControl(sn);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
|
||||
this.messageApi.error('权限校验失败');
|
||||
}
|
||||
}
|
||||
|
||||
// 发送权限请求
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
requestControl() {
|
||||
if (this.isRequestControl) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.hasControlRight) {
|
||||
this.messageApi.warning('无控制权限');
|
||||
return;
|
||||
}
|
||||
|
||||
this.isRequestControl = true;
|
||||
|
||||
this.onRequestControlStatusChange?.(true);
|
||||
|
||||
this.messageApi.loading({
|
||||
key: 'control',
|
||||
content: '请求控制中...',
|
||||
duration: 0,
|
||||
});
|
||||
|
||||
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) {
|
||||
const wsData = {
|
||||
wsManager.send({
|
||||
type,
|
||||
switchResult: data,
|
||||
token: this.userInfo.token,
|
||||
@@ -101,61 +207,86 @@ export default class DeviceController {
|
||||
? this.connectedDeviceSn
|
||||
: this.requestDeviceId,
|
||||
userName: this.userInfo.username,
|
||||
};
|
||||
wsManager.send(wsData);
|
||||
}
|
||||
|
||||
// 处理外部请求控制
|
||||
requestControl() {
|
||||
if (this.isRequestControl || !this.hasControlRight) {
|
||||
if (!this.hasControlRight) this.messageApi.warning('当前网页端无控制权限');
|
||||
return;
|
||||
}
|
||||
this.isRequestControl = true;
|
||||
this.messageApi.loading({
|
||||
key: 'control',
|
||||
content: '权限请求中...',
|
||||
duration: 0,
|
||||
});
|
||||
this.sendPermissionRequest(true, 'switchPermission');
|
||||
}
|
||||
|
||||
// 处理 WebSocket 消息
|
||||
handleWsMessage(msg) {
|
||||
switch (msg.type) {
|
||||
case 'deviceInfo':
|
||||
case 'deviceInfo': {
|
||||
const data = {};
|
||||
msg.data.forEach((item) => (data[item.name] = item.value));
|
||||
this.wsDeviceInfo = data;
|
||||
if (this.onDeviceInfoUpdate) this.onDeviceInfoUpdate(data);
|
||||
break;
|
||||
|
||||
case 'AUTH':
|
||||
if (msg.isAuth) this.getControl(this.connectedDeviceSn);
|
||||
break;
|
||||
|
||||
case 'switchResult':
|
||||
this.messageApi.success({
|
||||
content: '权限获取成功',
|
||||
key: 'control',
|
||||
duration: 2,
|
||||
msg.data.forEach((item) => {
|
||||
data[item.name] = item.value;
|
||||
});
|
||||
this.isRequestControl = false;
|
||||
this.isControlled = true;
|
||||
if (this.onControlStatusChange) this.onControlStatusChange(true);
|
||||
break;
|
||||
|
||||
case 'switchPermission':
|
||||
const platform = (msg.platform || '').toLowerCase().trim();
|
||||
if (!msg.platform || platform === 'web') return;
|
||||
this.requestDeviceId = msg.deviceId;
|
||||
if (this.onPermissionRequest) {
|
||||
this.onPermissionRequest({
|
||||
title: '权限请求',
|
||||
content: `${msg.platform} 请求控制 ${msg.deviceId}`,
|
||||
});
|
||||
}
|
||||
this.wsDeviceInfo = data;
|
||||
|
||||
this.onDeviceInfoUpdate?.(data);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 'AUTH': {
|
||||
if (msg.isAuth) {
|
||||
this.getControl(this.connectedDeviceSn);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 'switchResult': {
|
||||
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);
|
||||
|
||||
this.messageApi.success({
|
||||
key: 'control',
|
||||
content: '控制成功',
|
||||
});
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 'switchPermission': {
|
||||
const platform = (
|
||||
msg.platform || ''
|
||||
).toLowerCase();
|
||||
|
||||
if (!platform || platform === 'web' || platform == "null") {
|
||||
return;
|
||||
}
|
||||
|
||||
this.requestDeviceId = msg.deviceId;
|
||||
|
||||
this.onPermissionRequest?.({
|
||||
title: '权限请求',
|
||||
content: `${msg.platform} 请求控制设备`,
|
||||
});
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 'detectionReport':
|
||||
if (msg.data?.deviceId !== this.connectedDeviceSn) return;
|
||||
@@ -170,16 +301,28 @@ export default class DeviceController {
|
||||
duration: 3,
|
||||
});
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 发送游戏手柄指令
|
||||
sendGamepad(state) {
|
||||
if (!this.isControlled || !this.connectedDeviceSn) return;
|
||||
if (
|
||||
!this.isControlled ||
|
||||
!this.connectedDeviceSn
|
||||
) {
|
||||
return;
|
||||
}
|
||||
console.log(state, "state")
|
||||
|
||||
wsManager.send({
|
||||
type: 'gamepad',
|
||||
type: 'remoteControl',
|
||||
deviceId: this.connectedDeviceSn,
|
||||
data: { axes: state.axes, buttons: state.buttons },
|
||||
data: {
|
||||
axes: state.axes,
|
||||
buttons: state.buttons,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user