diff --git a/maibu-common/src/main/java/com/maibu/core/business/DeviceRunParam.java b/maibu-common/src/main/java/com/maibu/core/business/DeviceRunParam.java index 5f3e381..77e6bce 100644 --- a/maibu-common/src/main/java/com/maibu/core/business/DeviceRunParam.java +++ b/maibu-common/src/main/java/com/maibu/core/business/DeviceRunParam.java @@ -1,22 +1,19 @@ package com.maibu.core.business; +import java.time.LocalDate; +import java.util.List; + import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler; import com.maibu.annotation.Excel; -import com.maibu.constant.CommandConstant; import com.maibu.core.domain.BaseDO; import lombok.Data; import lombok.EqualsAndHashCode; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.nio.charset.StandardCharsets; -import java.util.List; - @EqualsAndHashCode(callSuper = true) @Data @TableName(value = "iot_device_run_param", autoResultMap = true) @@ -25,366 +22,440 @@ public class DeviceRunParam extends BaseDO { @TableId(type = IdType.AUTO) private Long id; -// private String deviceId; + // private String deviceId; private Long siteId; private Long orgId; - private Integer runSpeed; //不下发到设备自己用 + private Integer runSpeed; // 不下发到设备自己用 @Excel(name = "设备序列号", cellType = Excel.ColumnType.STRING, prompt = "设备序列号") - private String deviceId;//设备序列号 + private String deviceId;// 设备序列号 @Excel(name = "产品型号", cellType = Excel.ColumnType.STRING, prompt = "产品型号") - private String productModel;//产品型号 + private String productModel;// 产品型号 @Excel(name = "设备类型", cellType = Excel.ColumnType.STRING, prompt = "设备类型") - private String deviceType;//设备类型:mower-割草机器人 + private String deviceType;// 设备类型:mower-割草机器人 @Excel(name = "RTK模式", cellType = Excel.ColumnType.STRING, prompt = "RTK模式") - private String rtkMode;//RTK模式:network-4G网络差分 + private String rtkMode;// RTK模式:network-4G网络差分 @Excel(name = "摄像头数量", cellType = Excel.ColumnType.STRING, prompt = "摄像头数量") - private Integer cameraNum;//摄像头数量 + private Integer cameraNum;// 摄像头数量 @Excel(name = "动力类型", cellType = Excel.ColumnType.STRING, prompt = "动力类型") - private String powerType;//动力类型:hybrid-混动 + private String powerType;// 动力类型:hybrid-混动 @Excel(name = "车轮类型", cellType = Excel.ColumnType.STRING, prompt = "车轮类型") - private String wheelType;//车轮类型:track-履带式 + private String wheelType;// 车轮类型:track-履带式 @Excel(name = "发动机类型", cellType = Excel.ColumnType.STRING, prompt = "发动机类型") - private String engineType;//发动机类型:honda-本田发动机 + private String engineType;// 发动机类型:honda-本田发动机 - @Excel(name = "是否搭载上位机",cellType = Excel.ColumnType.NUMERIC) - private Integer hasHost = 1;//是否搭载上位机 + @Excel(name = "是否搭载上位机", cellType = Excel.ColumnType.NUMERIC) + private Integer hasHost = 1;// 是否搭载上位机 @Excel(name = "设备能力列表") @TableField(exist = false) private String capabilities; + private String factoryCode; // 工厂码 + + private String lineNum;// 产线号 + + private LocalDate productionDate;// 生产日期 + + private String serialNumber;// 流水号 + + @Excel(name = "电池类型", cellType = Excel.ColumnType.STRING, prompt = "电池类型") + private String batteryType;// 电池类型 + + @Excel(name = "行走驱动类型", cellType = Excel.ColumnType.STRING, prompt = "行走驱动类型") + private String walkingDriveType;// 行走驱动类型 + + @Excel(name = "割刀驱动类型", cellType = Excel.ColumnType.STRING, prompt = "割刀驱动类型") + private String cutterDriveType;// 割刀驱动类型 + + @Excel(name = "RTK天线数", cellType = Excel.ColumnType.STRING, prompt = "RTK天线数") + private String rtkAntennasNum;// RTK天线数 + + @Excel(name = "手柄类型", cellType = Excel.ColumnType.STRING, prompt = "手柄类型") + private String handleType;// 手柄类型 + + @Excel(name = "PCB硬件版本", cellType = Excel.ColumnType.STRING, prompt = "PCB硬件版本") + private String pcbVersion;// PCB硬件版本 + + @Excel(name = "长宽高", cellType = Excel.ColumnType.STRING, prompt = "长宽高") + private String dimensions;// 长宽高 + + @Excel(name = "割刀宽度", cellType = Excel.ColumnType.STRING, prompt = "割刀宽度") + private String bladeWidth;// 割刀宽度 + + @Excel(name = "轮胎尺寸", cellType = Excel.ColumnType.STRING, prompt = "轮胎尺寸") + private String tyreSize;// 轮胎尺寸 + + @Excel(name = "转速比", cellType = Excel.ColumnType.STRING, prompt = "转速比") + private String speedRatio;// 转速比 + + @Excel(name = "蜂窝网络通信信息", cellType = Excel.ColumnType.STRING, prompt = "蜂窝网络通信信息") + private String cellularInfo;// 蜂窝网络通信信息 + + @Excel(name = "wifi名称", cellType = Excel.ColumnType.STRING, prompt = "wifi名称") + private String wifiName;// wifi名称 + + @Excel(name = "蓝牙广播名", cellType = Excel.ColumnType.STRING, prompt = "蓝牙广播名") + private String bluetoothName;// 蓝牙广播名 + + @Excel(name = "硬件标识信息", cellType = Excel.ColumnType.STRING, prompt = "硬件标识信息") + private String hardwareIdentifiers;// 硬件标识信息 + + @Excel(name = "IMU型号", cellType = Excel.ColumnType.STRING, prompt = "IMU型号") + private String imuModel;// IMU型号 + + @Excel(name = "温湿度传感器", cellType = Excel.ColumnType.STRING, prompt = "温湿度传感器") + private String tempHumiditySensor;// 温湿度传感器 + + @Excel(name = "气压计在位", cellType = Excel.ColumnType.STRING, prompt = "气压计在位") + private String barometerPresent;// 气压计在位 + + @Excel(name = "超声波在位", cellType = Excel.ColumnType.STRING, prompt = "超声波在位") + private String ultrasonicPresent;// 超声波在位 + + @Excel(name = "DAC在位", cellType = Excel.ColumnType.STRING, prompt = "DAC在位") + private String dacPresent;// DAC在位 + + @Excel(name = "质检员", cellType = Excel.ColumnType.STRING, prompt = "质检员") + private String operator;// 质检员 + + @Excel(name = "检测报告号", cellType = Excel.ColumnType.STRING, prompt = "检测报告号") + private String inspectionNo;// 检测报告号 + + @Excel(name = "固件版本", cellType = Excel.ColumnType.STRING, prompt = "固件版本") + private String firmwareVersion;// 固件版本 + + @Excel(name = "经销商", cellType = Excel.ColumnType.STRING, prompt = "经销商") + private String dealer;// DAC在位 + @TableField(typeHandler = JacksonTypeHandler.class) - private List capabilityList;//设备能力列表:heartbeat-上报心跳/location-上报位置/vehicle_status-上报整机状态/task_control-任务控制/single_point-单点作业/coverage-全覆盖割草作业/path_upload-作业路径上传 + private List capabilityList;// 设备能力列表:heartbeat-上报心跳/location-上报位置/vehicle_status-上报整机状态/task_control-任务控制/single_point-单点作业/coverage-全覆盖割草作业/path_upload-作业路径上传 + + // @TableField(exist = false) + // public byte header1; + // @TableField(exist = false) + // public byte header2; + // @TableField(exist = false) + // public byte cmd; + + // public byte chipUidSign;// 芯片UID固化标志位 + // public String chipUid;// 芯片UID + + // public byte[] remoteConfig;// 遥控器通道配置 + // public int knifeMotorMode;// 割刀电机模式 + // public int walkMotorMode;// 行走电机模式 + // public int leftMotorReverse;// 左轮电机方向极性 + // public int rightMotorReverse;// 右轮电机方向极性 + // public int swapChannel;// 左右轮通道互换标志位 + // public int use4G;// 联网标志位 + + // public short forwardSpeedLimit;// 前进速度限制 + // public short turnSpeedLimit;// 转向速度限制 + + // public int knifePolarity;// 割刀通道极性 + // public int fanPolarity;// 风门通道极性 + // public int throttlePolarity;// 油门通道极性 + // public int liftProtectTime;// 底盘升降电机保护时间 + // public int dualRtk;// RTK配置 + + // public int knifeChannel;// 割刀通道配置 + // public int fanChannel;// 风门通道配置 + + // public int throttleChannel;// 油门通道配置 + // public int remoteType;// 遥控器类型 + // public boolean relayBoard;// 是否搭载继电器板 + + // public int liftChannel;// 底盘升通道配置 + // public int chassisChannel;// 底盘降通道配置 + + // public int armChannel;// 抱闸通道配置 + // public int fuelPumpChannel;// 燃油励磁通道配置 + + // public String wifiName;// wifi名称 + // public String wifiPassword;// wifi密码 + + // public int batteryType;// 电池类型 + // public int driveType;// 行走驱动配置 + + // public float gearRatio;// 转速比 + // public float robotLength;// 机器人长度 + // public float robotWidth;// 机器人宽度 + // public float robotHeight;// 机器人高度 + // public float knifeWidth;// 割刀宽度 + // public float tyreSize;// 轮胎尺寸 + // public float leftForwardGain;// 左轮前进增益 + // public float leftBackwardGain;// 左轮后退增益 + // public float rightForwardGain;// 右轮前进增益 + // public float rightBackwardGain;// 右轮后退增益 + + // public String firmwareVersion;// 固件版本 + // @TableField(exist = false) + // public int crc16; + // @TableField(exist = false) + // public byte tail1; + // @TableField(exist = false) + // public byte tail2; + // @TableField(exist = false) + // private byte[] originalData; + + // public static byte[] toReadBytes() { + // ByteBuffer buffer = ByteBuffer.allocate(7); // 修改为27字节 + // buffer.order(ByteOrder.LITTLE_ENDIAN); + + // buffer.put((byte) 0xAB); + // buffer.put((byte) 0xAA); + + // buffer.put(CommandConstant.readConfig); + + // buffer.putShort((short) 0); + + // buffer.put((byte) 0xAA); + // buffer.put((byte) 0xAB); + + // return buffer.array(); + // } + + // public byte[] toWriteBytes() { + // // 如果没有原始数据,使用静态方法重新构建 + // if (originalData == null || originalData.length < 178) { + // return null; + // } + + // // 克隆原始数据,避免修改原始数据 + // byte[] result = originalData.clone(); + // ByteBuffer buffer = ByteBuffer.wrap(result); + // buffer.order(ByteOrder.LITTLE_ENDIAN); + + // // 修改指令为写配置(字节2) + // buffer.put(2, CommandConstant.writeConfig); + + // // 修改字节68:遥控器配置 + // byte remote = 0; + // remote |= (getKnifeMotorMode() & 0x03); + // remote |= (getWalkMotorMode() & 0x03) << 2; + // remote |= (getLeftMotorReverse() & 0x01) << 4; + // remote |= (getRightMotorReverse() & 0x01) << 5; + // remote |= (getSwapChannel() & 0x01) << 6; + // remote |= (getUse4G() & 0x01) << 7; + // buffer.put(68, remote); + + // // 修改字节69-72:速度限制 + // buffer.putShort(69, getForwardSpeedLimit()); + // buffer.putShort(71, getTurnSpeedLimit()); + + // // 修改字节73:通道极性 + // byte b73 = 0; + // b73 |= (getKnifePolarity() & 1); + // b73 |= (getFanPolarity() & 1) << 1; + // b73 |= (getThrottlePolarity() & 1) << 2; + // b73 |= (getLiftProtectTime() & 0x0F) << 3; + // b73 |= (getDualRtk() & 1) << 7; + // buffer.put(73, b73); + + // // 修改字节74-77:通道配置 + // byte b74 = 0; + // b74 |= (getKnifeChannel() & 0x0F); + // b74 |= (getFanChannel() & 0x0F) << 4; + // buffer.put(74, b74); + + // byte b75 = 0; + // b75 |= (getThrottleChannel() & 0x0F); + // b75 |= (getRemoteType() & 0x07) << 4; + // if (isRelayBoard()) + // b75 |= 1 << 7; + // buffer.put(75, b75); + + // byte b76 = 0; + // b76 |= (getLiftChannel() & 0x0F); + // b76 |= (getChassisChannel() & 0x0F) << 4; + // buffer.put(76, b76); + + // byte b77 = 0; + // b77 |= (getArmChannel() & 0x0F); + // b77 |= (getFuelPumpChannel() & 0x0F) << 4; + // buffer.put(77, b77); + + // // // 修改字节78-117:WiFi信息 + // // byte[] wifiNameBytes = new byte[20]; + // // if (getWifiName() != null) { + // // byte[] src = getWifiName().getBytes(StandardCharsets.UTF_8); + // // System.arraycopy(src, 0, wifiNameBytes, 0, Math.min(src.length, 20)); + // // } + // // System.arraycopy(wifiNameBytes, 0, result, 78, 20); + // // + // // byte[] wifiPwdBytes = new byte[20]; + // // if (getWifiPassword() != null) { + // // byte[] src = getWifiPassword().getBytes(StandardCharsets.UTF_8); + // // System.arraycopy(src, 0, wifiPwdBytes, 0, Math.min(src.length, 20)); + // // } + // // System.arraycopy(wifiPwdBytes, 0, result, 98, 20); + + // // // 修改字节118:电池和驱动配置 + // // byte b118 = 0; + // // b118 |= (getBatteryType() & 0x03); + // // b118 |= (getDriveType() & 0x03) << 2; + // // buffer.put(118, b118); + + // // 修改字节119-158:float参数 + // buffer.putFloat(119, getGearRatio()); + // buffer.putFloat(123, getRobotLength()); + // buffer.putFloat(127, getRobotWidth()); + // buffer.putFloat(131, getRobotHeight()); + // buffer.putFloat(135, getKnifeWidth()); + // buffer.putFloat(139, getTyreSize()); + // buffer.putFloat(143, getLeftForwardGain()); + // buffer.putFloat(147, getLeftBackwardGain()); + // buffer.putFloat(151, getRightForwardGain()); + // buffer.putFloat(155, getRightBackwardGain()); + + // // 修改字节159-173:固件版本 + // // byte[] fwBytes = new byte[15]; + // // if (getFirmwareVersion() != null) { + // // byte[] src = getFirmwareVersion().getBytes(StandardCharsets.UTF_8); + // // System.arraycopy(src, 0, fwBytes, 0, Math.min(src.length, 15)); + // // } + // // System.arraycopy(fwBytes, 0, result, 159, 15); + + // // 保留原始的 CRC 和尾部,不强制修改 + // // 移除以下强制修改: + // // buffer.putShort(174, (short) 0); + // // buffer.put(176, (byte) 0xAA); + // // buffer.put(177, (byte) 0xAB); + + // return result; + // } + + // public static void putFixedString(ByteBuffer buffer, String value, int len) { + + // byte[] bytes = new byte[len]; + + // if (value != null) { + // byte[] src = value.getBytes(StandardCharsets.UTF_8); + + // System.arraycopy(src, 0, bytes, 0, Math.min(src.length, len)); + // } + + // buffer.put(bytes); + // } + + // public static DeviceRunParam fromBytes(byte[] data) { + // // 183 + // if (data == null || data.length < 178) { + // throw new IllegalArgumentException("数据长度不足"); + // } + + // ByteBuffer buffer = ByteBuffer.wrap(data); + // buffer.order(ByteOrder.LITTLE_ENDIAN); + + // DeviceRunParam p = new DeviceRunParam(); + // p.setOriginalData(data); - @TableField(exist = false) - public byte header1; - @TableField(exist = false) - public byte header2; - @TableField(exist = false) - public byte cmd; - - - public byte chipUidSign;//芯片UID固化标志位 - public String chipUid;//芯片UID - - public byte[] remoteConfig;//遥控器通道配置 - public int knifeMotorMode;//割刀电机模式 - public int walkMotorMode;//行走电机模式 - public int leftMotorReverse;//左轮电机方向极性 - public int rightMotorReverse;//右轮电机方向极性 - public int swapChannel;//左右轮通道互换标志位 - public int use4G;//联网标志位 - - public short forwardSpeedLimit;//前进速度限制 - public short turnSpeedLimit;//转向速度限制 - - public int knifePolarity;//割刀通道极性 - public int fanPolarity;//风门通道极性 - public int throttlePolarity;//油门通道极性 - public int liftProtectTime;//底盘升降电机保护时间 - public int dualRtk;//RTK配置 - - - public int knifeChannel;//割刀通道配置 - public int fanChannel;//风门通道配置 - - public int throttleChannel;//油门通道配置 - public int remoteType;//遥控器类型 - public boolean relayBoard;//是否搭载继电器板 - - public int liftChannel;//底盘升通道配置 - public int chassisChannel;//底盘降通道配置 - - public int armChannel;//抱闸通道配置 - public int fuelPumpChannel;//燃油励磁通道配置 - - public String wifiName;//wifi名称 - public String wifiPassword;//wifi密码 - - public int batteryType;//电池类型 - public int driveType;//行走驱动配置 - - public float gearRatio;//转速比 - public float robotLength;//机器人长度 - public float robotWidth;//机器人宽度 - public float robotHeight;//机器人高度 - public float knifeWidth;//割刀宽度 - public float tyreSize;//轮胎尺寸 - public float leftForwardGain;//左轮前进增益 - public float leftBackwardGain;//左轮后退增益 - public float rightForwardGain;//右轮前进增益 - public float rightBackwardGain;//右轮后退增益 - - public String firmwareVersion;//固件版本 - @TableField(exist = false) - public int crc16; - @TableField(exist = false) - public byte tail1; - @TableField(exist = false) - public byte tail2; - @TableField(exist = false) - private byte[] originalData; - - public static byte[] toReadBytes() { - ByteBuffer buffer = ByteBuffer.allocate(7); // 修改为27字节 - buffer.order(ByteOrder.LITTLE_ENDIAN); - - buffer.put((byte) 0xAB); - buffer.put((byte) 0xAA); - - buffer.put(CommandConstant.readConfig); - - buffer.putShort((short) 0); - - buffer.put((byte) 0xAA); - buffer.put((byte) 0xAB); - - return buffer.array(); - } - - public byte[] toWriteBytes() { - // 如果没有原始数据,使用静态方法重新构建 - if (originalData == null || originalData.length < 178) { - return null; - } - - // 克隆原始数据,避免修改原始数据 - byte[] result = originalData.clone(); - ByteBuffer buffer = ByteBuffer.wrap(result); - buffer.order(ByteOrder.LITTLE_ENDIAN); - - // 修改指令为写配置(字节2) - buffer.put(2, CommandConstant.writeConfig); - - // 修改字节68:遥控器配置 - byte remote = 0; - remote |= (getKnifeMotorMode() & 0x03); - remote |= (getWalkMotorMode() & 0x03) << 2; - remote |= (getLeftMotorReverse() & 0x01) << 4; - remote |= (getRightMotorReverse() & 0x01) << 5; - remote |= (getSwapChannel() & 0x01) << 6; - remote |= (getUse4G() & 0x01) << 7; - buffer.put(68, remote); - - // 修改字节69-72:速度限制 - buffer.putShort(69, getForwardSpeedLimit()); - buffer.putShort(71, getTurnSpeedLimit()); - - // 修改字节73:通道极性 - byte b73 = 0; - b73 |= (getKnifePolarity() & 1); - b73 |= (getFanPolarity() & 1) << 1; - b73 |= (getThrottlePolarity() & 1) << 2; - b73 |= (getLiftProtectTime() & 0x0F) << 3; - b73 |= (getDualRtk() & 1) << 7; - buffer.put(73, b73); - - // 修改字节74-77:通道配置 - byte b74 = 0; - b74 |= (getKnifeChannel() & 0x0F); - b74 |= (getFanChannel() & 0x0F) << 4; - buffer.put(74, b74); - - byte b75 = 0; - b75 |= (getThrottleChannel() & 0x0F); - b75 |= (getRemoteType() & 0x07) << 4; - if (isRelayBoard()) b75 |= 1 << 7; - buffer.put(75, b75); - - byte b76 = 0; - b76 |= (getLiftChannel() & 0x0F); - b76 |= (getChassisChannel() & 0x0F) << 4; - buffer.put(76, b76); - - byte b77 = 0; - b77 |= (getArmChannel() & 0x0F); - b77 |= (getFuelPumpChannel() & 0x0F) << 4; - buffer.put(77, b77); - -// // 修改字节78-117:WiFi信息 -// byte[] wifiNameBytes = new byte[20]; -// if (getWifiName() != null) { -// byte[] src = getWifiName().getBytes(StandardCharsets.UTF_8); -// System.arraycopy(src, 0, wifiNameBytes, 0, Math.min(src.length, 20)); -// } -// System.arraycopy(wifiNameBytes, 0, result, 78, 20); -// -// byte[] wifiPwdBytes = new byte[20]; -// if (getWifiPassword() != null) { -// byte[] src = getWifiPassword().getBytes(StandardCharsets.UTF_8); -// System.arraycopy(src, 0, wifiPwdBytes, 0, Math.min(src.length, 20)); -// } -// System.arraycopy(wifiPwdBytes, 0, result, 98, 20); - -// // 修改字节118:电池和驱动配置 -// byte b118 = 0; -// b118 |= (getBatteryType() & 0x03); -// b118 |= (getDriveType() & 0x03) << 2; -// buffer.put(118, b118); - - // 修改字节119-158:float参数 - buffer.putFloat(119, getGearRatio()); - buffer.putFloat(123, getRobotLength()); - buffer.putFloat(127, getRobotWidth()); - buffer.putFloat(131, getRobotHeight()); - buffer.putFloat(135, getKnifeWidth()); - buffer.putFloat(139, getTyreSize()); - buffer.putFloat(143, getLeftForwardGain()); - buffer.putFloat(147, getLeftBackwardGain()); - buffer.putFloat(151, getRightForwardGain()); - buffer.putFloat(155, getRightBackwardGain()); - - // 修改字节159-173:固件版本 -// byte[] fwBytes = new byte[15]; -// if (getFirmwareVersion() != null) { -// byte[] src = getFirmwareVersion().getBytes(StandardCharsets.UTF_8); -// System.arraycopy(src, 0, fwBytes, 0, Math.min(src.length, 15)); -// } -// System.arraycopy(fwBytes, 0, result, 159, 15); - - // 保留原始的 CRC 和尾部,不强制修改 - // 移除以下强制修改: - // buffer.putShort(174, (short) 0); - // buffer.put(176, (byte) 0xAA); - // buffer.put(177, (byte) 0xAB); - - return result; - } - - public static void putFixedString(ByteBuffer buffer, String value, int len) { - - byte[] bytes = new byte[len]; - - if (value != null) { - byte[] src = value.getBytes(StandardCharsets.UTF_8); - - System.arraycopy(src, 0, bytes, 0, Math.min(src.length, len)); - } - - buffer.put(bytes); - } - - - public static DeviceRunParam fromBytes(byte[] data) { - // 183 - if (data == null || data.length < 178) { - throw new IllegalArgumentException("数据长度不足"); - } - - ByteBuffer buffer = ByteBuffer.wrap(data); - buffer.order(ByteOrder.LITTLE_ENDIAN); - - DeviceRunParam p = new DeviceRunParam(); + // p.setHeader1(buffer.get()); + // p.setHeader2(buffer.get()); - p.setOriginalData(data); + // p.setCmd(buffer.get()); - p.setHeader1(buffer.get()); - p.setHeader2(buffer.get()); + // p.setChipUidSign(buffer.get()); - p.setCmd(buffer.get()); + // p.setChipUid(readString(buffer, 45)); - p.setChipUidSign(buffer.get()); + // // 跳过字节49-67(共19字节,预留不用) + // buffer.position(buffer.position() + 19); - p.setChipUid(readString(buffer, 45)); + // byte remote = buffer.get(); - // 跳过字节49-67(共19字节,预留不用) - buffer.position(buffer.position() + 19); + // // p.setRemoteConfig(remote); - byte remote = buffer.get(); + // p.setKnifeMotorMode(remote & 0x03); + // p.setWalkMotorMode((remote >> 2) & 0x03); + // p.setLeftMotorReverse((remote >> 4) & 0x01); + // p.setRightMotorReverse((remote >> 5) & 0x01); + // p.setSwapChannel((remote >> 6) & 0x01); + // p.setUse4G((remote >> 7) & 0x01); -// p.setRemoteConfig(remote); + // p.setForwardSpeedLimit(buffer.getShort()); + // p.setTurnSpeedLimit(buffer.getShort()); - p.setKnifeMotorMode(remote & 0x03); - p.setWalkMotorMode((remote >> 2) & 0x03); - p.setLeftMotorReverse((remote >> 4) & 0x01); - p.setRightMotorReverse((remote >> 5) & 0x01); - p.setSwapChannel((remote >> 6) & 0x01); - p.setUse4G((remote >> 7) & 0x01); + // byte b73 = buffer.get(); - p.setForwardSpeedLimit(buffer.getShort()); - p.setTurnSpeedLimit(buffer.getShort()); + // p.setKnifePolarity(b73 & 1); + // p.setFanPolarity((b73 >> 1) & 1); + // p.setThrottlePolarity((b73 >> 2) & 1); + // p.setLiftProtectTime((b73 >> 3) & 0x0F); + // p.setDualRtk((b73 >> 7) & 1); - byte b73 = buffer.get(); + // byte b74 = buffer.get(); - p.setKnifePolarity(b73 & 1); - p.setFanPolarity((b73 >> 1) & 1); - p.setThrottlePolarity((b73 >> 2) & 1); - p.setLiftProtectTime((b73 >> 3) & 0x0F); - p.setDualRtk((b73 >> 7) & 1); + // p.setKnifeChannel(b74 & 0x0F); + // p.setFanChannel((b74 >> 4) & 0x0F); - byte b74 = buffer.get(); + // byte b75 = buffer.get(); - p.setKnifeChannel(b74 & 0x0F); - p.setFanChannel((b74 >> 4) & 0x0F); + // p.setThrottleChannel(b75 & 0x0F); + // p.setRemoteType((b75 >> 4) & 0x07); + // p.setRelayBoard(((b75 >> 7) & 1) == 1); - byte b75 = buffer.get(); + // byte b76 = buffer.get(); - p.setThrottleChannel(b75 & 0x0F); - p.setRemoteType((b75 >> 4) & 0x07); - p.setRelayBoard(((b75 >> 7) & 1) == 1); + // p.setLiftChannel(b76 & 0x0F); + // p.setChassisChannel((b76 >> 4) & 0x0F); - byte b76 = buffer.get(); + // byte b77 = buffer.get(); - p.setLiftChannel(b76 & 0x0F); - p.setChassisChannel((b76 >> 4) & 0x0F); + // p.setArmChannel(b77 & 0x0F); + // p.setFuelPumpChannel((b77 >> 4) & 0x0F); - byte b77 = buffer.get(); + // p.setWifiName(readString(buffer, 20)); + // p.setWifiPassword(readString(buffer, 20)); - p.setArmChannel(b77 & 0x0F); - p.setFuelPumpChannel((b77 >> 4) & 0x0F); + // byte b118 = buffer.get(); - p.setWifiName(readString(buffer, 20)); - p.setWifiPassword(readString(buffer, 20)); + // p.setBatteryType(b118 & 0x03); + // p.setDriveType((b118 >> 2) & 0x03); - byte b118 = buffer.get(); + // // float + // p.setGearRatio(buffer.getFloat()); + // p.setRobotLength(buffer.getFloat()); + // p.setRobotWidth(buffer.getFloat()); + // p.setRobotHeight(buffer.getFloat()); + // p.setKnifeWidth(buffer.getFloat()); + // p.setTyreSize(buffer.getFloat()); + // p.setLeftForwardGain(buffer.getFloat()); + // p.setLeftBackwardGain(buffer.getFloat()); + // p.setRightForwardGain(buffer.getFloat()); + // p.setRightBackwardGain(buffer.getFloat()); - p.setBatteryType(b118 & 0x03); - p.setDriveType((b118 >> 2) & 0x03); + // p.setFirmwareVersion(readString(buffer, 15)); - // float - p.setGearRatio(buffer.getFloat()); - p.setRobotLength(buffer.getFloat()); - p.setRobotWidth(buffer.getFloat()); - p.setRobotHeight(buffer.getFloat()); - p.setKnifeWidth(buffer.getFloat()); - p.setTyreSize(buffer.getFloat()); - p.setLeftForwardGain(buffer.getFloat()); - p.setLeftBackwardGain(buffer.getFloat()); - p.setRightForwardGain(buffer.getFloat()); - p.setRightBackwardGain(buffer.getFloat()); + // p.setCrc16(Short.toUnsignedInt(buffer.getShort())); - p.setFirmwareVersion(readString(buffer, 15)); + // p.setTail1(buffer.get()); + // p.setTail2(buffer.get()); - p.setCrc16(Short.toUnsignedInt(buffer.getShort())); + // return p; + // } - p.setTail1(buffer.get()); - p.setTail2(buffer.get()); + // public static String readString(ByteBuffer buffer, int len) { + // byte[] bytes = new byte[len]; + // buffer.get(bytes); - return p; - } + // int end = 0; + // while (end < bytes.length && bytes[end] != 0) { + // end++; + // } - public static String readString(ByteBuffer buffer, int len) { - byte[] bytes = new byte[len]; - buffer.get(bytes); - - int end = 0; - while (end < bytes.length && bytes[end] != 0) { - end++; - } - - return new String(bytes, 0, end, StandardCharsets.UTF_8); - } + // return new String(bytes, 0, end, StandardCharsets.UTF_8); + // } } diff --git a/maibu-common/src/main/java/com/maibu/core/business/device/NettyDevice.java b/maibu-common/src/main/java/com/maibu/core/business/device/NettyDevice.java index a8580c4..11f808e 100644 --- a/maibu-common/src/main/java/com/maibu/core/business/device/NettyDevice.java +++ b/maibu-common/src/main/java/com/maibu/core/business/device/NettyDevice.java @@ -246,24 +246,24 @@ public class NettyDevice extends Connector { } } - public void readConfigCommand() { - byte[] bytes = DeviceRunParam.toReadBytes(); - log.debug("下发读取配置的报文:{}", toHexString(bytes)); - if (this.getChannel() != null && this.getChannel().isActive()) { - this.getChannel().writeAndFlush(Unpooled.wrappedBuffer(bytes)); - } - log.debug("下发读取配置的报文完成:{}", toHexString(bytes)); - } + // public void readConfigCommand() { + // byte[] bytes = DeviceRunParam.toReadBytes(); + // log.debug("下发读取配置的报文:{}", toHexString(bytes)); + // if (this.getChannel() != null && this.getChannel().isActive()) { + // this.getChannel().writeAndFlush(Unpooled.wrappedBuffer(bytes)); + // } + // log.debug("下发读取配置的报文完成:{}", toHexString(bytes)); + // } - public void writeConfigCommand(DeviceRunParam deviceRunParam) { - log.debug("下发修改配置的属性:{}", JsonUtils.toJsonString(deviceRunParam)); - byte[] bytes = deviceRunParam.toWriteBytes(); - log.debug("下发修改配置的报文:{}", toHexString(bytes)); - if (this.getChannel() != null && this.getChannel().isActive()) { - this.getChannel().writeAndFlush(Unpooled.wrappedBuffer(bytes)); - } - log.debug("下发修改配置的报文完成:{}", toHexString(bytes)); - } + // public void writeConfigCommand(DeviceRunParam deviceRunParam) { + // log.debug("下发修改配置的属性:{}", JsonUtils.toJsonString(deviceRunParam)); + // byte[] bytes = deviceRunParam.toWriteBytes(); + // log.debug("下发修改配置的报文:{}", toHexString(bytes)); + // if (this.getChannel() != null && this.getChannel().isActive()) { + // this.getChannel().writeAndFlush(Unpooled.wrappedBuffer(bytes)); + // } + // log.debug("下发修改配置的报文完成:{}", toHexString(bytes)); + // } // todo 优化 public synchronized boolean pauseTask() { diff --git a/maibu-common/src/main/java/com/maibu/core/host/ParamDetail.java b/maibu-common/src/main/java/com/maibu/core/host/ParamDetail.java index 9c55784..73779b1 100644 --- a/maibu-common/src/main/java/com/maibu/core/host/ParamDetail.java +++ b/maibu-common/src/main/java/com/maibu/core/host/ParamDetail.java @@ -4,11 +4,11 @@ import lombok.Data; @Data public class ParamDetail { - private short left_wheel_rpm; // 左轮转速 - private short right_wheel_rpm; // 右轮转速 + // private short left_wheel_rpm; // 左轮转速 + // private short right_wheel_rpm; // 右轮转速 - private double linear_speed; // 线速度 - private double angular_speed; // 角速度 + private double linear_x; // 线速度 + private double angular_z; // 角速度 private int chassis_lift; // 底盘升降 0:停止 1:升 2:降 private int cutter; // 割刀加减速 0:停止 1:加速 2:减速 diff --git a/maibu-common/src/main/java/com/maibu/core/host/VehicleMessage.java b/maibu-common/src/main/java/com/maibu/core/host/VehicleMessage.java index dc4b601..eaa00e7 100644 --- a/maibu-common/src/main/java/com/maibu/core/host/VehicleMessage.java +++ b/maibu-common/src/main/java/com/maibu/core/host/VehicleMessage.java @@ -31,4 +31,7 @@ public class VehicleMessage { private double roll;// 实时翻滚角度 private String control_mode;// 遥控器拨杆对应的当前控制模式 + private double linear_speed; // 实时线速度 + private double angular_speed; // 实时角速度 + } diff --git a/maibu-common/src/main/java/com/maibu/mqtt/HostMessageHandler.java b/maibu-common/src/main/java/com/maibu/mqtt/HostMessageHandler.java index 924a574..833ea31 100644 --- a/maibu-common/src/main/java/com/maibu/mqtt/HostMessageHandler.java +++ b/maibu-common/src/main/java/com/maibu/mqtt/HostMessageHandler.java @@ -154,7 +154,7 @@ public class HostMessageHandler implements CustomMqttMessageHandler { String.format(MqttTopic.DEVICE_LOCATION_MESSAGE_TOPIC, deviceId), JsonUtils.toJsonString(locationRelTimeDTO)); } catch (MqttException e) { - log.error("位置实时数据推送失败: deviceId={}, error={}", deviceId, e.getMessage(), e); + log.error("位置实时数据推送失败: deviceId={}, error={}", deviceId, e.getMessage(), e); } } @@ -399,6 +399,18 @@ public class HostMessageHandler implements CustomMqttMessageHandler { control_mode.setUnit(""); transferData.add(control_mode); + DeviceStatusDetail linear_speed = new DeviceStatusDetail(); + linear_speed.setName("linear_speed"); + linear_speed.setValue(String.valueOf(vehicleRelTimeDTO.getData().getLinear_speed())); + linear_speed.setUnit(""); + transferData.add(linear_speed); + + DeviceStatusDetail angular_speed = new DeviceStatusDetail(); + angular_speed.setName("angular_speed"); + angular_speed.setValue(String.valueOf(vehicleRelTimeDTO.getData().getAngular_speed())); + angular_speed.setUnit(""); + transferData.add(angular_speed); + webStatusMessageDTO.setData(transferData); webStatusMessageDTO.setType(MesType.deviceInfo); return webStatusMessageDTO; @@ -447,13 +459,13 @@ public class HostMessageHandler implements CustomMqttMessageHandler { } } } - // todo 推送实时位置和状态消息到外部 + // todo 推送实时位置和状态消息到外部 try { GlobalMemory.customMqttDeviceMonitor.publish(mqttClientId, String.format(MqttTopic.DEVICE_VEHICLE_MESSAGE_TOPIC, deviceId), JsonUtils.toJsonString(vehicleRelTimeDTO)); } catch (MqttException e) { - log.error("位置实时数据推送失败: deviceId={}, error={}", deviceId, e.getMessage(), e); + log.error("位置实时数据推送失败: deviceId={}, error={}", deviceId, e.getMessage(), e); } } } catch (Exception e) { diff --git a/maibu-netty-server/src/main/java/com/maibu/netty/handler/CommandForwardHandler.java b/maibu-netty-server/src/main/java/com/maibu/netty/handler/CommandForwardHandler.java index 41204f7..5a417a1 100644 --- a/maibu-netty-server/src/main/java/com/maibu/netty/handler/CommandForwardHandler.java +++ b/maibu-netty-server/src/main/java/com/maibu/netty/handler/CommandForwardHandler.java @@ -83,7 +83,7 @@ public class CommandForwardHandler extends SimpleChannelInboundHandler { } for (Connector target : connectedDevices) { - if (!target.canReceive()) { + if (target == null || !target.canReceive()) { logger.debug("发送拒绝master:{},消息到达的slave设备:{},处于禁止接收状态", senderDeviceId, target.getConnectorId()); continue; } @@ -110,8 +110,8 @@ public class CommandForwardHandler extends SimpleChannelInboundHandler { .setScale(2, RoundingMode.HALF_UP) .doubleValue(); - data.setLinear_speed(linearSpeed); - data.setAngular_speed(angularSpeed); + data.setLinear_x(linearSpeed); + data.setAngular_z(angularSpeed); data.setChassis_lift(pEntity.getChassisLiftFlag()); data.setCutter(pEntity.getMowerSpeedFlag()); diff --git a/maibu-netty-server/src/main/java/com/maibu/netty/handler/DataToDataBaseHandler.java b/maibu-netty-server/src/main/java/com/maibu/netty/handler/DataToDataBaseHandler.java index 7d6d616..ecdb468 100644 --- a/maibu-netty-server/src/main/java/com/maibu/netty/handler/DataToDataBaseHandler.java +++ b/maibu-netty-server/src/main/java/com/maibu/netty/handler/DataToDataBaseHandler.java @@ -140,13 +140,13 @@ public class DataToDataBaseHandler extends SimpleChannelInboundHandler { nettyDevice.setLatestStatus(latestStatus); } } else if (bytes[2] == CommandConstant.readConfig) { - DeviceRunParam runParam = DeviceRunParam.fromBytes(bytes); - logger.debug("收到返回的设备运行参数: {}", JsonUtils.toJsonString(runParam)); - CompletableFuture future = ctx.channel().attr(Constant.READ_CONFIG_KEY).get(); - if (future != null) { - future.complete(runParam); - ctx.channel().attr(Constant.READ_CONFIG_KEY).set(null); - } + // DeviceRunParam runParam = DeviceRunParam.fromBytes(bytes); + // logger.debug("收到返回的设备运行参数: {}", JsonUtils.toJsonString(runParam)); + // CompletableFuture future = ctx.channel().attr(Constant.READ_CONFIG_KEY).get(); + // if (future != null) { + // future.complete(runParam); + // ctx.channel().attr(Constant.READ_CONFIG_KEY).set(null); + // } } ctx.fireChannelRead(msg); } catch (Exception e) { diff --git a/maibu-netty-server/src/main/java/com/maibu/scheduled/ScheduledTask.java b/maibu-netty-server/src/main/java/com/maibu/scheduled/ScheduledTask.java index 47a4a8f..85341e3 100644 --- a/maibu-netty-server/src/main/java/com/maibu/scheduled/ScheduledTask.java +++ b/maibu-netty-server/src/main/java/com/maibu/scheduled/ScheduledTask.java @@ -75,20 +75,20 @@ public class ScheduledTask { System.out.println("0点定时刷新状态执行完成"); } - public static void main(String[] args) throws DecoderException { - String hex = "AB AA 05 88 4D 43 37 30 30 50 4C 55 53 2D 43 4E 2D 4A 53 2D 31 37 38 34 38 37 31 39 37 39 31 39 30 2D 30 30 30 30 30 30 30 41 2D 36 30 00 B1 FB 7F FE FE FE 00 00 00 00 00 00 00 00 00 00 00 00 00 12 01 48 95 E8 03 E8 03 28 32 91 53 24 4D 43 37 30 30 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 31 32 33 34 35 36 37 38 00 00 00 00 00 00 00 00 00 00 00 00 00 08 00 00 00 CD CC A4 41 00 00 80 3F 33 33 33 3F B8 1E 05 3F 33 33 33 3F 9A 99 19 3E 00 00 80 3F 00 00 80 3F 00 00 80 3F 32 36 2D 34 2D 32 37 00 2D 32 37 00 50 B1 BF 00 00 AA AB"; - String cleanHex = hex.replaceAll("\\s+", ""); // 移除所有空白字符 - byte[] bytes = Hex.decodeHex(cleanHex); - DeviceRunParam runParam = DeviceRunParam.fromBytes(bytes); - System.out.println(JsonUtils.toJsonString(runParam)); + // public static void main(String[] args) throws DecoderException { + // String hex = "AB AA 05 88 4D 43 37 30 30 50 4C 55 53 2D 43 4E 2D 4A 53 2D 31 37 38 34 38 37 31 39 37 39 31 39 30 2D 30 30 30 30 30 30 30 41 2D 36 30 00 B1 FB 7F FE FE FE 00 00 00 00 00 00 00 00 00 00 00 00 00 12 01 48 95 E8 03 E8 03 28 32 91 53 24 4D 43 37 30 30 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 31 32 33 34 35 36 37 38 00 00 00 00 00 00 00 00 00 00 00 00 00 08 00 00 00 CD CC A4 41 00 00 80 3F 33 33 33 3F B8 1E 05 3F 33 33 33 3F 9A 99 19 3E 00 00 80 3F 00 00 80 3F 00 00 80 3F 32 36 2D 34 2D 32 37 00 2D 32 37 00 50 B1 BF 00 00 AA AB"; + // String cleanHex = hex.replaceAll("\\s+", ""); // 移除所有空白字符 + // byte[] bytes = Hex.decodeHex(cleanHex); + // DeviceRunParam runParam = DeviceRunParam.fromBytes(bytes); + // System.out.println(JsonUtils.toJsonString(runParam)); - runParam.setLeftBackwardGain(0.2f); - runParam.setLeftForwardGain(0.2f); - runParam.setRightForwardGain(0.2f); - runParam.setRightBackwardGain(0.2f); + // runParam.setLeftBackwardGain(0.2f); + // runParam.setLeftForwardGain(0.2f); + // runParam.setRightForwardGain(0.2f); + // runParam.setRightBackwardGain(0.2f); - byte[] newB = runParam.toWriteBytes(); - System.out.println(Hex.encodeHexString(newB)); + // byte[] newB = runParam.toWriteBytes(); + // System.out.println(Hex.encodeHexString(newB)); - } + // } } \ No newline at end of file diff --git a/maibu-netty-server/src/main/java/com/maibu/service/TransferDeviceService.java b/maibu-netty-server/src/main/java/com/maibu/service/TransferDeviceService.java index 4188a9a..eaa0050 100644 --- a/maibu-netty-server/src/main/java/com/maibu/service/TransferDeviceService.java +++ b/maibu-netty-server/src/main/java/com/maibu/service/TransferDeviceService.java @@ -43,12 +43,11 @@ public class TransferDeviceService { @Autowired private DeviceMapper deviceMapper; - public AjaxResult bind(DeviceBindDTO bindDTO, LoginUser user) { try { String deviceId = bindDTO.getDeviceId(); String deviceAlias = bindDTO.getDeviceAlias(); - //判断当前设备是否存 + // 判断当前设备是否存 Device device = iDeviceService.selectDeviceBySerialNumber(deviceId); if (device == null) { return AjaxResult.error("设备不存在"); @@ -65,7 +64,7 @@ public class TransferDeviceService { device.setDeviceAlias(deviceAlias); device.setActiveTime(new Date()); iDeviceService.updateDeviceBySelf(device); -// deviceMapper.updateById(device); + // deviceMapper.updateById(device); return AjaxResult.success(true); } } catch (Exception e) { @@ -77,7 +76,7 @@ public class TransferDeviceService { public AjaxResult unbind(DeviceBindDTO bindDTO, LoginUser user) { try { String deviceId = bindDTO.getDeviceId(); - //判断当前设备是否存 + // 判断当前设备是否存 Device device = iDeviceService.selectDeviceBySerialNumber(deviceId); if (device == null) { return AjaxResult.error("设备不存在"); @@ -93,9 +92,9 @@ public class TransferDeviceService { device.setTenantName(""); device.setUpdateTime(LocalDateTime.now()); device.setDeviceAlias(""); -// deviceMapper.updateById(device); + // deviceMapper.updateById(device); iDeviceService.updateDeviceBySelf(device); - //判断是否正在控制 如果正在控制,删除控制状态 + // 判断是否正在控制 如果正在控制,删除控制状态 LambdaQueryWrapper lambdaWrapper = new LambdaQueryWrapper<>(); lambdaWrapper.eq(UserClient::getDeviceName, deviceId); List users = userClientMapper.selectList(lambdaWrapper); @@ -108,12 +107,15 @@ public class TransferDeviceService { NettyDevice deviceMaster = sessionManager.getDevice(masterId); if (deviceMaster != null) { String oldSlave = userClient.getDeviceName(); - //删除主的连接关系 - deviceMaster.getConnectedConnectors().removeIf(x -> x.getConnectorId().equals(oldSlave)); - //删除从的连接关系 + // 删除主的连接关系 + deviceMaster.getConnectedConnectors() + .removeIf(x -> x.getConnectorId().equals(oldSlave)); + // 删除从的连接关系 NettyDevice oldSlaveDevice = sessionManager.getDevice(oldSlave); - if (oldSlaveDevice != null && !CollectionUtils.isEmpty(oldSlaveDevice.getConnectedConnectors())) { - Set connectedDevices = oldSlaveDevice.getConnectedConnectors(); + if (oldSlaveDevice != null + && !CollectionUtils.isEmpty(oldSlaveDevice.getConnectedConnectors())) { + Set connectedDevices = oldSlaveDevice + .getConnectedConnectors(); connectedDevices.removeIf(x -> x.getConnectorId().equals(masterId)); } } @@ -130,21 +132,20 @@ public class TransferDeviceService { } } - public AjaxResult updateDeviceAlias(DeviceBindDTO bindDTO, LoginUser user) { try { String deviceId = bindDTO.getDeviceId(); - //判断当前设备是否存 + // 判断当前设备是否存 Device device = iDeviceService.selectDeviceBySerialNumber(deviceId); if (device == null) { return AjaxResult.error("设备不存在"); } else { // Long tenantId = device.getTenantId(); // if (tenantId == -1) { - // return AjaxResult.error("设备未绑定"); + // return AjaxResult.error("设备未绑定"); // } // if (!tenantId.equals(user.getUserId())) { - // return AjaxResult.error("无操作权限"); + // return AjaxResult.error("无操作权限"); // } device.setDeviceAlias(bindDTO.getDeviceAlias()); device.setUpdateTime(LocalDateTime.now()); @@ -158,76 +159,76 @@ public class TransferDeviceService { } } -// public synchronized boolean switchDevice(DeviceBindDTO bindDTO, LoginUser user) { -// try { -// String slaveId = bindDTO.getDeviceId(); -// String platform = bindDTO.getPlatform(); -// String userName = user.getUsername(); -// String masterId = userName + ":" + platform; -// //todo 判断是否有权限 正在控制才能切 -// //todo 离线没有控制权的怎么切 -// log.info("switchDevice:{}", masterId); -// NettyDevice deviceMaster = sessionManager.getDevice(masterId); -// log.info("deviceMaster:{}", deviceMaster); -// if (deviceMaster != null) { -// log.info("ConnectorStatus:{}", deviceMaster.getConnectorStatus()); -// if (ConnectorStatus.RECEIVER.equals(deviceMaster.getConnectorStatus())) { -// //当前账号没有控制则可以切换为控制? -// NettyDevice activeDevice = sessionManager.getUserControl(userName); -// deviceMaster.getConnectedConnectors().clear(); -// if (activeDevice == null) { -// log.info("bindConnector ACTIVE :{}", masterId); -// sessionManager.bindConnector(masterId, slaveId); -// deviceMaster.setConnectorStatus(ConnectorStatus.ACTIVE); -// deviceMaster.setCurrentSlaveId(slaveId); -// sysUserClientService.updateAliveStatus(masterId, slaveId, 1); -// } else { -// log.info("bindConnector RECEIVER :{}", masterId); -// sessionManager.bindConnector(masterId, slaveId); -// deviceMaster.setConnectorStatus(ConnectorStatus.RECEIVER); -// deviceMaster.setCurrentSlaveId(slaveId); -// sysUserClientService.updateAliveStatus(masterId, slaveId, 2); -// } -// } else { -// //从关联其他设备切换到 当前禁言 假如有控制且不是当前更新为2 -// List controls = sessionManager.getSlaveControl(slaveId); -// if (CollectionUtils.isEmpty(controls)) { -// deviceMaster.getConnectedConnectors().clear(); -// log.info("bindConnector 1:{}", masterId); -// sessionManager.bindConnector(masterId, slaveId); -// deviceMaster.setConnectorStatus(ConnectorStatus.ACTIVE); -// deviceMaster.setCurrentSlaveId(slaveId); -// sysUserClientService.updateAliveStatus(masterId, slaveId, 1); -// } else { -// NettyDevice controlDevice = controls.get(0); -// if (!masterId.equals(controlDevice.getConnectorId())) { -// //当前的设备有控制 的主机 -// deviceMaster.getConnectedConnectors().clear(); -// log.info("bindConnector RECEIVER 2:{}", masterId); -// sessionManager.bindConnector(masterId, slaveId); -// deviceMaster.setConnectorStatus(ConnectorStatus.RECEIVER); -// deviceMaster.setCurrentSlaveId(slaveId); -// sysUserClientService.updateAliveStatus(masterId, slaveId, 2); -// } -// //当前 切当前不做操作 -// } -// } -// return true; -// } -// } catch (Exception e) { -// log.error("切换设备失败: {}", e.getMessage(), e); -// } -// return false; -// } + // public synchronized boolean switchDevice(DeviceBindDTO bindDTO, LoginUser + // user) { + // try { + // String slaveId = bindDTO.getDeviceId(); + // String platform = bindDTO.getPlatform(); + // String userName = user.getUsername(); + // String masterId = userName + ":" + platform; + // //todo 判断是否有权限 正在控制才能切 + // //todo 离线没有控制权的怎么切 + // log.info("switchDevice:{}", masterId); + // NettyDevice deviceMaster = sessionManager.getDevice(masterId); + // log.info("deviceMaster:{}", deviceMaster); + // if (deviceMaster != null) { + // log.info("ConnectorStatus:{}", deviceMaster.getConnectorStatus()); + // if (ConnectorStatus.RECEIVER.equals(deviceMaster.getConnectorStatus())) { + // //当前账号没有控制则可以切换为控制? + // NettyDevice activeDevice = sessionManager.getUserControl(userName); + // deviceMaster.getConnectedConnectors().clear(); + // if (activeDevice == null) { + // log.info("bindConnector ACTIVE :{}", masterId); + // sessionManager.bindConnector(masterId, slaveId); + // deviceMaster.setConnectorStatus(ConnectorStatus.ACTIVE); + // deviceMaster.setCurrentSlaveId(slaveId); + // sysUserClientService.updateAliveStatus(masterId, slaveId, 1); + // } else { + // log.info("bindConnector RECEIVER :{}", masterId); + // sessionManager.bindConnector(masterId, slaveId); + // deviceMaster.setConnectorStatus(ConnectorStatus.RECEIVER); + // deviceMaster.setCurrentSlaveId(slaveId); + // sysUserClientService.updateAliveStatus(masterId, slaveId, 2); + // } + // } else { + // //从关联其他设备切换到 当前禁言 假如有控制且不是当前更新为2 + // List controls = sessionManager.getSlaveControl(slaveId); + // if (CollectionUtils.isEmpty(controls)) { + // deviceMaster.getConnectedConnectors().clear(); + // log.info("bindConnector 1:{}", masterId); + // sessionManager.bindConnector(masterId, slaveId); + // deviceMaster.setConnectorStatus(ConnectorStatus.ACTIVE); + // deviceMaster.setCurrentSlaveId(slaveId); + // sysUserClientService.updateAliveStatus(masterId, slaveId, 1); + // } else { + // NettyDevice controlDevice = controls.get(0); + // if (!masterId.equals(controlDevice.getConnectorId())) { + // //当前的设备有控制 的主机 + // deviceMaster.getConnectedConnectors().clear(); + // log.info("bindConnector RECEIVER 2:{}", masterId); + // sessionManager.bindConnector(masterId, slaveId); + // deviceMaster.setConnectorStatus(ConnectorStatus.RECEIVER); + // deviceMaster.setCurrentSlaveId(slaveId); + // sysUserClientService.updateAliveStatus(masterId, slaveId, 2); + // } + // //当前 切当前不做操作 + // } + // } + // return true; + // } + // } catch (Exception e) { + // log.error("切换设备失败: {}", e.getMessage(), e); + // } + // return false; + // } public synchronized boolean switchDevice(DeviceBindDTO bindDTO) { try { String slaveId = bindDTO.getDeviceId(); -// String platform = bindDTO.getPlatform(); -// String userName = user.getUsername(); - -// String masterId = userName + ":" + platform; + // String platform = bindDTO.getPlatform(); + // String userName = user.getUsername(); + // String masterId = userName + ":" + platform; String token = bindDTO.getToken(); @@ -247,9 +248,10 @@ public class TransferDeviceService { sessionManager.lock(slaveId, token); log.info("switchDevice bindConnector slaveId:{},token:{}", slaveId, token); } else { - List connectorIds = controls.stream().map(NettyDevice::getConnectorId).collect(Collectors.toList()); + List connectorIds = controls.stream().map(NettyDevice::getConnectorId) + .collect(Collectors.toList()); if (!connectorIds.contains(token)) { - //当前的设备有控制 的主机 + // 当前的设备有控制 的主机 deviceMaster.getConnectedConnectors().clear(); sessionManager.bindConnector(token, slaveId); deviceMaster.setConnectorStatus(ConnectorStatus.RECEIVER); @@ -260,16 +262,15 @@ public class TransferDeviceService { deviceMaster.setCurrentSlaveId(slaveId); sessionManager.lock(slaveId, token); } - //当前 切当前不做操作 + // 当前 切当前不做操作 } return true; } catch (Exception e) { - log.error("切换设备失败: {}", e.getMessage(), e); + log.error("切换设备失败", e); } return false; } - public DeviceControlDTO remoteControl(DeviceBindDTO bindDTO) { try { String token = bindDTO.getToken(); @@ -290,7 +291,6 @@ public class TransferDeviceService { return null; } - public DeviceSwitchControlDTO switchControl(DeviceBindDTO bindDTO, LoginUser user) { try { String slaveId = bindDTO.getDeviceId(); @@ -299,11 +299,11 @@ public class TransferDeviceService { String masterId = userName + ":" + platform; DeviceSwitchControlDTO dto = new DeviceSwitchControlDTO(); - //收到明确的切换设备请求了 从哪边切换到另一边 - //判断当前是否有控制的设备 + // 收到明确的切换设备请求了 从哪边切换到另一边 + // 判断当前是否有控制的设备 NettyDevice masterDevice = sessionManager.getDevice(masterId); if (masterDevice == null || !masterDevice.getChannel().isActive()) { - //主机已离线 + // 主机已离线 dto.setSwitchControl(false); dto.setReason("主机已离线"); return dto; @@ -311,8 +311,8 @@ public class TransferDeviceService { NettyDevice slaveDevice = sessionManager.getDevice(slaveId); if (slaveDevice == null || !masterDevice.getChannel().isActive()) { - //从机已离线 - //主机已离线 + // 从机已离线 + // 主机已离线 dto.setSwitchControl(false); dto.setReason("从机已离线"); return dto; @@ -321,7 +321,7 @@ public class TransferDeviceService { List controlList = sessionManager.getSlaveControl(slaveId); if (!CollectionUtils.isEmpty(controlList)) { - //如果当前设备有控制 + // 如果当前设备有控制 controlList.forEach(c -> { if (!c.getConnectorId().equals(masterId)) { c.setCurrentSlaveId(null); @@ -333,7 +333,8 @@ public class TransferDeviceService { } masterDevice.setCurrentSlaveId(slaveId); masterDevice.setConnectorStatus(ConnectorStatus.ACTIVE); - // sysUserClientService.updateAliveStatus(masterDevice.getConnectorId(), slaveId, 1); + // sysUserClientService.updateAliveStatus(masterDevice.getConnectorId(), + // slaveId, 1); dto.setSwitchControl(true); dto.setHolder(platform); diff --git a/maibu-service/maibu-iot-service/src/main/java/com/maibu/controller/DeviceRunParamController.java b/maibu-service/maibu-iot-service/src/main/java/com/maibu/controller/DeviceRunParamController.java index 5794f4c..6e6104d 100644 --- a/maibu-service/maibu-iot-service/src/main/java/com/maibu/controller/DeviceRunParamController.java +++ b/maibu-service/maibu-iot-service/src/main/java/com/maibu/controller/DeviceRunParamController.java @@ -8,13 +8,20 @@ import com.maibu.dto.BaseQueryDTO; import com.maibu.service.impl.DeviceRunParamService; import com.maibu.utils.poi.ExcelUtil; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.ClassPathResource; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import java.io.IOException; +import java.io.InputStream; +import java.net.URLEncoder; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeoutException; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletResponse; + @RestController @RequestMapping("/iot/deviceRunParam") public class DeviceRunParamController extends BaseController { @@ -23,7 +30,7 @@ public class DeviceRunParamController extends BaseController { private DeviceRunParamService service; @PostMapping("/save") - public AjaxResult save(@RequestBody DeviceRunParam deviceRunParam) throws ExecutionException, InterruptedException, TimeoutException { + public AjaxResult save(@RequestBody DeviceRunParam deviceRunParam) { return AjaxResult.success(service.save(deviceRunParam)); } @@ -35,7 +42,8 @@ public class DeviceRunParamController extends BaseController { } @GetMapping("/selectByDeviceId") - public AjaxResult selectByDeviceId(@RequestParam String deviceId) throws ExecutionException, InterruptedException, TimeoutException { + public AjaxResult selectByDeviceId(@RequestParam String deviceId) + throws ExecutionException, InterruptedException, TimeoutException { return AjaxResult.success(service.selectByDeviceId(deviceId)); } @@ -43,7 +51,7 @@ public class DeviceRunParamController extends BaseController { public AjaxResult delete(@RequestBody List ids) { return AjaxResult.success(service.delete(ids)); } - + @PostMapping("/importData") public AjaxResult importData(MultipartFile file) throws Exception { ExcelUtil util = new ExcelUtil(DeviceRunParam.class); @@ -51,4 +59,31 @@ public class DeviceRunParamController extends BaseController { boolean message = service.importParam(paramList); return success(message); } + + /** + * 下载机器属性导入模板 + */ + @GetMapping("/downloadTemplate") + public void downloadTemplate(HttpServletResponse response) throws IOException { + String fileName = "机器属性导入模板.xlsx"; + + try (InputStream inputStream = new ClassPathResource("机器属性导入模板.xlsx").getInputStream()) { + + response.setContentType( + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); + response.setCharacterEncoding("UTF-8"); + response.setHeader( + "Content-Disposition", + "attachment; filename*=UTF-8''" + + URLEncoder.encode(fileName, "UTF-8").replace("+", "%20")); + + ServletOutputStream outputStream = response.getOutputStream(); + byte[] buffer = new byte[8192]; + int len; + while ((len = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, len); + } + outputStream.flush(); + } + } } diff --git a/maibu-service/maibu-iot-service/src/main/java/com/maibu/controller/OrderModelController.java b/maibu-service/maibu-iot-service/src/main/java/com/maibu/controller/OrderModelController.java index f52780d..0643470 100644 --- a/maibu-service/maibu-iot-service/src/main/java/com/maibu/controller/OrderModelController.java +++ b/maibu-service/maibu-iot-service/src/main/java/com/maibu/controller/OrderModelController.java @@ -3,14 +3,11 @@ package com.maibu.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; -import com.maibu.core.business.DeviceRunParam; import com.maibu.core.business.OrderModel; import com.maibu.core.controller.BaseController; import com.maibu.core.domain.AjaxResult; diff --git a/maibu-service/maibu-iot-service/src/main/java/com/maibu/service/impl/DeviceRunParamService.java b/maibu-service/maibu-iot-service/src/main/java/com/maibu/service/impl/DeviceRunParamService.java index 0f6d0ff..0f92622 100644 --- a/maibu-service/maibu-iot-service/src/main/java/com/maibu/service/impl/DeviceRunParamService.java +++ b/maibu-service/maibu-iot-service/src/main/java/com/maibu/service/impl/DeviceRunParamService.java @@ -1,7 +1,10 @@ package com.maibu.service.impl; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Arrays; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.concurrent.CompletableFuture; @@ -39,47 +42,55 @@ public class DeviceRunParamService { @Autowired private DeviceSessionManager sessionManager; - public boolean save(DeviceRunParam deviceRunParam) - throws ExecutionException, InterruptedException, TimeoutException { + // public boolean save(DeviceRunParam deviceRunParam) + // throws ExecutionException, InterruptedException, TimeoutException { - NettyDevice nettyDevice = sessionManager.getDevice(deviceRunParam.getDeviceId()); - if (nettyDevice == null || !nettyDevice.getChannel().isActive()) { - throw new ServiceException("设备不在线,无法操作!"); - } + // NettyDevice nettyDevice = + // sessionManager.getDevice(deviceRunParam.getDeviceId()); + // if (nettyDevice == null || !nettyDevice.getChannel().isActive()) { + // throw new ServiceException("设备不在线,无法操作!"); + // } - CompletableFuture future = new CompletableFuture<>(); - nettyDevice.getChannel().attr(Constant.READ_CONFIG_KEY).set(future); + // CompletableFuture future = new CompletableFuture<>(); + // nettyDevice.getChannel().attr(Constant.READ_CONFIG_KEY).set(future); - try { - nettyDevice.readConfigCommand(); + // try { + // nettyDevice.readConfigCommand(); - DeviceRunParam param = future.get(5, TimeUnit.SECONDS); + // DeviceRunParam param = future.get(5, TimeUnit.SECONDS); - if (param == null) { - throw new ServiceException("获取设备当前属性失败!"); - } + // if (param == null) { + // throw new ServiceException("获取设备当前属性失败!"); + // } - param.setLeftForwardGain(deviceRunParam.getLeftForwardGain()); - param.setLeftBackwardGain(deviceRunParam.getLeftBackwardGain()); - param.setRightForwardGain(deviceRunParam.getRightForwardGain()); - param.setRightBackwardGain(deviceRunParam.getRightBackwardGain()); + // param.setLeftForwardGain(deviceRunParam.getLeftForwardGain()); + // param.setLeftBackwardGain(deviceRunParam.getLeftBackwardGain()); + // param.setRightForwardGain(deviceRunParam.getRightForwardGain()); + // param.setRightBackwardGain(deviceRunParam.getRightBackwardGain()); - nettyDevice.writeConfigCommand(param); + // nettyDevice.writeConfigCommand(param); - param.setRunSpeed(deviceRunParam.getRunSpeed()); - param.setDeviceId(deviceRunParam.getDeviceId()); - param.setSiteId(deviceRunParam.getSiteId()); - param.setOrgId(deviceRunParam.getOrgId()); - param.setId(deviceRunParam.getId()); + // param.setRunSpeed(deviceRunParam.getRunSpeed()); + // param.setDeviceId(deviceRunParam.getDeviceId()); + // param.setSiteId(deviceRunParam.getSiteId()); + // param.setOrgId(deviceRunParam.getOrgId()); + // param.setId(deviceRunParam.getId()); + + // SiteMemory siteMemory = GlobalMemory.getSiteMemory(deviceRunParam.getOrgId(), + // deviceRunParam.getSiteId()); + // if (siteMemory != null) { + // siteMemory.saveDeviceRunParam(param); + // } + // return deviceRunParamMapper.saveOrUpdate(param); + // } finally { + // nettyDevice.getChannel().attr(Constant.READ_CONFIG_KEY).set(null); + // } + // } + + public boolean save(DeviceRunParam deviceRunParam) { + + return deviceRunParamMapper.saveOrUpdate(deviceRunParam); - SiteMemory siteMemory = GlobalMemory.getSiteMemory(deviceRunParam.getOrgId(), deviceRunParam.getSiteId()); - if (siteMemory != null) { - siteMemory.saveDeviceRunParam(param); - } - return deviceRunParamMapper.saveOrUpdate(param); - } finally { - nettyDevice.getChannel().attr(Constant.READ_CONFIG_KEY).set(null); - } } public List list(BaseQueryDTO dto) { @@ -127,6 +138,49 @@ public class DeviceRunParamService { // return null; // } + // public DeviceRunParam selectByDeviceId(String deviceId) + // throws ExecutionException, InterruptedException, TimeoutException { + + // LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); + // if (!StringUtils.isEmpty(deviceId)) { + // queryWrapper.eq(DeviceRunParam::getDeviceId, deviceId); + // } + // DeviceRunParam result = deviceRunParamMapper.selectOne(queryWrapper); + // if (result != null && result.getHasHost() != null && result.getHasHost() == + // 1) { + // return result; + // } else { + // NettyDevice nettyDevice = sessionManager.getDevice(deviceId); + // CompletableFuture future = new CompletableFuture<>(); + // if (nettyDevice != null && nettyDevice.getChannel() != null && + // nettyDevice.getChannel().isActive()) { + // nettyDevice.getChannel().attr(Constant.READ_CONFIG_KEY).set(future); + // try { + // nettyDevice.readConfigCommand(); + // DeviceRunParam param = future.get(5, TimeUnit.SECONDS); + + // // LambdaQueryWrapper queryWrapper = new + // LambdaQueryWrapper<>(); + // // if (!StringUtils.isEmpty(deviceId)) { + // // queryWrapper.eq(DeviceRunParam::getDeviceId, deviceId); + // // } + // // DeviceRunParam result = deviceRunParamMapper.selectOne(queryWrapper); + // if (result != null) { + // param.setId(result.getId()); + // param.setDeviceId(result.getDeviceId()); + // param.setRunSpeed(result.getRunSpeed()); + // param.setOrgId(nettyDevice.getDevice().getOrgId()); + // param.setSiteId(nettyDevice.getDevice().getSiteId()); + // } + // return param; + // } finally { + // nettyDevice.getChannel().attr(Constant.READ_CONFIG_KEY).set(null); + // } + // } + // } + // return null; + // } + public DeviceRunParam selectByDeviceId(String deviceId) throws ExecutionException, InterruptedException, TimeoutException { @@ -135,36 +189,7 @@ public class DeviceRunParamService { queryWrapper.eq(DeviceRunParam::getDeviceId, deviceId); } DeviceRunParam result = deviceRunParamMapper.selectOne(queryWrapper); - if (result != null && result.getHasHost() != null && result.getHasHost() == 1) { - return result; - } else { - NettyDevice nettyDevice = sessionManager.getDevice(deviceId); - CompletableFuture future = new CompletableFuture<>(); - if (nettyDevice != null && nettyDevice.getChannel() != null && nettyDevice.getChannel().isActive()) { - nettyDevice.getChannel().attr(Constant.READ_CONFIG_KEY).set(future); - try { - nettyDevice.readConfigCommand(); - DeviceRunParam param = future.get(5, TimeUnit.SECONDS); - - // LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); - // if (!StringUtils.isEmpty(deviceId)) { - // queryWrapper.eq(DeviceRunParam::getDeviceId, deviceId); - // } - // DeviceRunParam result = deviceRunParamMapper.selectOne(queryWrapper); - if (result != null) { - param.setId(result.getId()); - param.setDeviceId(result.getDeviceId()); - param.setRunSpeed(result.getRunSpeed()); - param.setOrgId(nettyDevice.getDevice().getOrgId()); - param.setSiteId(nettyDevice.getDevice().getSiteId()); - } - return param; - } finally { - nettyDevice.getChannel().attr(Constant.READ_CONFIG_KEY).set(null); - } - } - } - return null; + return result; } public int delete(List ids) { @@ -211,20 +236,51 @@ public class DeviceRunParamService { } if (deviceRunParam != null) { deviceRunParam.setCapabilityList(x.getCapabilityList()); + + deviceRunParam.setDeviceId(x.getDeviceId()); + + parseSn(x.getDeviceId(), deviceRunParam); + deviceRunParam.setProductModel(x.getProductModel()); - deviceRunParam.setRtkMode(x.getRtkMode()); deviceRunParam.setDeviceType(x.getDeviceType()); - deviceRunParam.setPowerType(x.getPowerType()); + deviceRunParam.setRtkMode(x.getRtkMode()); deviceRunParam.setCameraNum(x.getCameraNum()); + deviceRunParam.setPowerType(x.getPowerType()); deviceRunParam.setWheelType(x.getWheelType()); deviceRunParam.setEngineType(x.getEngineType()); deviceRunParam.setHasHost(x.getHasHost()); + + deviceRunParam.setBatteryType(x.getBatteryType()); + deviceRunParam.setWalkingDriveType(x.getWalkingDriveType()); + deviceRunParam.setCutterDriveType(x.getCutterDriveType()); + deviceRunParam.setRtkAntennasNum(x.getRtkAntennasNum()); + deviceRunParam.setHandleType(x.getHandleType()); + deviceRunParam.setPcbVersion(x.getPcbVersion()); + deviceRunParam.setDimensions(x.getDimensions()); + deviceRunParam.setBladeWidth(x.getBladeWidth()); + deviceRunParam.setTyreSize(x.getTyreSize()); + deviceRunParam.setSpeedRatio(x.getSpeedRatio()); + deviceRunParam.setCellularInfo(x.getCellularInfo()); + deviceRunParam.setWifiName(x.getWifiName()); + deviceRunParam.setBluetoothName(x.getBluetoothName()); + deviceRunParam.setHardwareIdentifiers(x.getHardwareIdentifiers()); + deviceRunParam.setImuModel(x.getImuModel()); + deviceRunParam.setTempHumiditySensor(x.getTempHumiditySensor()); + deviceRunParam.setBarometerPresent(x.getBarometerPresent()); + deviceRunParam.setUltrasonicPresent(x.getUltrasonicPresent()); + deviceRunParam.setDacPresent(x.getDacPresent()); + deviceRunParam.setOperator(x.getOperator()); + deviceRunParam.setInspectionNo(x.getInspectionNo()); + deviceRunParam.setFirmwareVersion(x.getFirmwareVersion()); + deviceRunParam.setDealer(x.getDealer()); + saveList.add(deviceRunParam); } else { + parseSn(x.getDeviceId(), x); saveList.add(x); } }); - //todo 增加 解析sn码的功能 + // todo 增加 解析sn码的功能 return deviceRunParamMapper.saveOrUpdateBatch(saveList); } } @@ -234,4 +290,21 @@ public class DeviceRunParamService { } } + public static void parseSn(String sn, DeviceRunParam deviceRunParam) { + if (sn == null || sn.length() != 21) { + throw new IllegalArgumentException("SN 必须是 21 位字符串"); + } + + deviceRunParam.setProductModel(sn.substring(0, 2)); + deviceRunParam.setFactoryCode(sn.substring(2, 4)); + deviceRunParam.setLineNum(sn.substring(4, 6)); + + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMMdd"); + LocalDate date = LocalDate.parse(sn.substring(6, 12), formatter); + deviceRunParam.setProductionDate(date); + + deviceRunParam.setSerialNumber(sn.substring(12, 17)); + + } + } diff --git a/maibu-service/maibu-iot-service/src/main/resources/机器属性导入模板.xlsx b/maibu-service/maibu-iot-service/src/main/resources/机器属性导入模板.xlsx new file mode 100755 index 0000000..7843bad Binary files /dev/null and b/maibu-service/maibu-iot-service/src/main/resources/机器属性导入模板.xlsx differ