#修改远程遥控控制方式 将左右轮速改成线速度和角速度

This commit is contained in:
2026-10-16 09:19:31 +08:00
parent 11984316d4
commit 9e3ec6a1dd
11 changed files with 249 additions and 89 deletions

View File

@@ -1,11 +1,15 @@
package com.maibu.netty.handler;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Set;
import com.maibu.memory.DeviceSessionManager;
import com.maibu.memory.GlobalMemory;
import com.maibu.mqtt.MqttTopic;
import com.maibu.utils.json.JsonUtils;
import org.apache.commons.codec.binary.Hex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -15,9 +19,13 @@ import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import com.maibu.constant.CommandConstant;
import com.maibu.constant.Constant;
import com.maibu.core.business.LawnMoverProtocolEntity;
import com.maibu.core.business.device.Connector;
import com.maibu.core.business.device.NettyDevice;
import com.maibu.core.enums.ConnectorType;
import com.maibu.core.host.DeviceRemoteControlDTO;
import com.maibu.core.host.ParamDetail;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
@@ -29,7 +37,6 @@ import io.netty.channel.SimpleChannelInboundHandler;
@ChannelHandler.Sharable
public class CommandForwardHandler extends SimpleChannelInboundHandler<String> {
private static final Logger logger = LoggerFactory.getLogger(CommandForwardHandler.class);
@Autowired
@@ -44,12 +51,11 @@ public class CommandForwardHandler extends SimpleChannelInboundHandler<String> {
byte[] bytes = Hex.decodeHex(msg.toCharArray());
if (bytes.length > 3 && (bytes[2] == CommandConstant.remoteControl
|| bytes[2] == CommandConstant.path
|| bytes[2] == CommandConstant.status
)) {
|| bytes[2] == CommandConstant.status)) {
String senderDeviceId = deviceSessionManager.getDeviceIdByChannel(currentChannel);
NettyDevice senderNettyDevice =
senderDeviceId != null ? deviceSessionManager.getDevice(senderDeviceId) : null;
NettyDevice senderNettyDevice = senderDeviceId != null ? deviceSessionManager.getDevice(senderDeviceId)
: null;
if (senderNettyDevice == null) {
logger.debug("发送拒绝,设备:{}未发送上线指令", senderDeviceId);
@@ -76,22 +82,73 @@ public class CommandForwardHandler extends SimpleChannelInboundHandler<String> {
return;
}
for (Connector target : connectedDevices) {
if (!target.isOnline()) {
logger.debug("发送拒绝master:{},消息到达的salve设备:{},已离线", senderDeviceId, target.getConnectorId());
continue;
}
if (!target.canReceive()) {
logger.debug("发送拒绝master:{},消息到达的slave设备:{},处于禁止接收状态", senderDeviceId, target.getConnectorId());
continue;
}
// todo 向上位机发送控制消息
NettyDevice targetDevice = deviceSessionManager.getDevice(target.getConnectorId());
if (targetDevice == null)
continue;
// 区分有上位机的还有无上位机的 有上位机的通过上位机下发 没有上位机的按照原来的方式下发
if (targetDevice.getDevice().getHasHost()) {
if (bytes[2] == CommandConstant.remoteControl) {
// todo 转换 原先的报文重新组装成上位机的报文格式
DeviceRemoteControlDTO deviceRemoteControlDTO = new DeviceRemoteControlDTO();
LawnMoverProtocolEntity pEntity = LawnMoverProtocolEntity.parseControlCommand(bytes);
if (pEntity != null) {
GlobalMemory.customMqttDeviceMonitor.publish(mqttClientId, String.format(MqttTopic.MOWER_CONTROL_SET, target.getConnectorId()), msg);
// Channel targetChannel = target.getChannel();
// if (targetChannel != null && targetChannel.isActive()) {
// targetChannel.writeAndFlush(msg);
// logger.debug("从master:{},向slave:{}, 转发消息:{}", senderDeviceId, target.getConnectorId(), msg);
// }
ParamDetail data = new ParamDetail();
// data.setLeft_wheel_rpm(pEntity.getLeftWheelSpeed());
// data.setRight_wheel_rpm(pEntity.getRightWheelSpeed());
double angularSpeed = BigDecimal.valueOf(pEntity.getAngularSpeed())
.setScale(2, RoundingMode.HALF_UP)
.doubleValue();
double linearSpeed = BigDecimal.valueOf(pEntity.getLinearSpeed())
.setScale(2, RoundingMode.HALF_UP)
.doubleValue();
data.setLinear_speed(linearSpeed);
data.setAngular_speed(angularSpeed);
data.setChassis_lift(pEntity.getChassisLiftFlag());
data.setCutter(pEntity.getMowerSpeedFlag());
data.setIgnition(pEntity.getIgnitionFlag());
data.setEmergency_stop(pEntity.getEmergencyStopFlag());
data.setValid_for_ms(500);
deviceRemoteControlDTO.setData(data);
deviceRemoteControlDTO
.setCommand_id(Constant.remote_control_cmd_prefix + System.currentTimeMillis());
deviceRemoteControlDTO.setSn(target.getConnectorId());
deviceRemoteControlDTO.setTimestamp(System.currentTimeMillis());
deviceRemoteControlDTO
.setMessage_id(Constant.remote_control_msg_prefix + System.currentTimeMillis());
deviceRemoteControlDTO.setProtocol_version(Constant.protocol_version);
GlobalMemory.customMqttDeviceMonitor.publish(mqttClientId,
String.format(MqttTopic.MOWER_CONTROL_SET, target.getConnectorId()),
JsonUtils.toJsonString(deviceRemoteControlDTO));
logger.debug("master:{},向slave:{} 有上位机, 转发遥控指令:{}", senderDeviceId,
target.getConnectorId(),
JsonUtils.toJsonString(deviceRemoteControlDTO));
}
}
} else {
if (!target.isOnline()) {
logger.debug("发送拒绝master:{},消息到达的salve设备:{},已离线", senderDeviceId,
target.getConnectorId());
continue;
}
Channel targetChannel = target.getChannel();
if (targetChannel != null && targetChannel.isActive()) {
targetChannel.writeAndFlush(msg);
logger.debug("从master:{},向slave:{} 无上位机, 转发消息:{}", senderDeviceId,
target.getConnectorId(), msg);
}
}
}
} else if (ConnectorType.SLAVE.equals(senderNettyDevice.getDeviceType())) {
List<NettyDevice> masters = deviceSessionManager.getMastersBySlaveId(senderDeviceId);