称重优化

This commit is contained in:
zc
2026-04-09 11:05:57 +08:00
parent 95242f772e
commit 90eb4a1af8
12 changed files with 124 additions and 43 deletions

View File

@@ -1,6 +1,5 @@
package top.wms.admin.common.enums;
/**
* DPA6024V-2T-1.0 数字控制器命令类型枚举
* 定义控制器支持的指令字
@@ -35,6 +34,7 @@ public enum CommandTypeEnum {
/**
* 获取指令字字符
*
* @return 指令字 (如 '1', '2', '3', '4')
*/
public char getCommandCode() {
@@ -43,6 +43,7 @@ public enum CommandTypeEnum {
/**
* 根据指令字获取对应的命令类型
*
* @param commandCode 指令字字符
* @return 对应的CommandType找不到返回null
*/

View File

@@ -74,6 +74,7 @@ public class LightService {
/**
* 检查是否已连接
*
* @return 已连接返回true否则返回false
*/
public boolean isConnected() {
@@ -84,6 +85,7 @@ public class LightService {
/**
* 检查连接状态并尝试重连
*
* @return 重连成功返回true否则返回false
*/
public boolean checkAndReconnect() {
@@ -98,6 +100,7 @@ public class LightService {
/**
* 重新连接控制器
*
* @return 重连成功返回true否则返回false
*/
public boolean reconnect() {
@@ -117,6 +120,7 @@ public class LightService {
/**
* 打开指定通道
*
* @param channel 通道号 (1-2)
*/
public boolean turnOn(int channel) {
@@ -125,6 +129,7 @@ public class LightService {
/**
* 关闭指定通道
*
* @param channel 通道号 (1-2)
*/
public boolean turnOff(int channel) {
@@ -133,7 +138,8 @@ public class LightService {
/**
* 设置通道亮度
* @param channel 通道号 (1-2)
*
* @param channel 通道号 (1-2)
* @param brightness 亮度等级 (0-255)
*/
public boolean setBrightness(int channel, int brightness) {
@@ -147,6 +153,7 @@ public class LightService {
/**
* 读取通道亮度
*
* @param channel 通道号 (1-2)
* @return 亮度等级 (0-255),失败返回-1
*/
@@ -230,7 +237,7 @@ public class LightService {
char cmdChar = type.getCommandCode();
// 通道字 (1-2)
char channelChar = (char) (channel + '0');
char channelChar = (char)(channel + '0');
// 数据: 3字节亮度的十六进制表示高位在前
// 亮度值范围0-255需要转换为3个十六进制ASCII字符
@@ -286,6 +293,7 @@ public class LightService {
/**
* 便捷方法:设置所有通道亮度
*
* @param brightness 亮度等级 (0-255)
*/
public void setAllBrightness(int brightness) {

View File

@@ -1,6 +1,5 @@
package top.wms.admin.light;
import com.fazecast.jSerialComm.SerialPort;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
@@ -35,6 +34,7 @@ public class SerialPortHandler {
/**
* 构造函数
*
* @param portName 串口名称,如 "COM3" (Windows) 或 "/dev/ttyUSB0" (Linux)
* @param baudRate 波特率
*/
@@ -45,6 +45,7 @@ public class SerialPortHandler {
/**
* 打开串口连接
*
* @return 打开成功返回true失败返回false
*/
public boolean open() {
@@ -99,6 +100,7 @@ public class SerialPortHandler {
/**
* 检查串口是否已打开
*
* @return 已打开返回true否则返回false
*/
public boolean isOpen() {
@@ -107,6 +109,7 @@ public class SerialPortHandler {
/**
* 发送数据
*
* @param data 要发送的字节数组
*/
public void sendData(byte[] data) {
@@ -131,6 +134,7 @@ public class SerialPortHandler {
/**
* 发送字符串数据
*
* @param data 要发送的字符串
*/
public void sendData(String data) {
@@ -144,6 +148,7 @@ public class SerialPortHandler {
/**
* 接收响应数据
*
* @param timeoutMs 超时时间(毫秒)
* @return 接收到的字符串超时或无数据返回null
*/
@@ -163,8 +168,7 @@ public class SerialPortHandler {
// 等待数据到达
long startTime = System.currentTimeMillis();
while (inputStream.available() == 0 &&
(System.currentTimeMillis() - startTime) < timeoutMs) {
while (inputStream.available() == 0 && (System.currentTimeMillis() - startTime) < timeoutMs) {
Thread.sleep(10);
}
@@ -185,7 +189,8 @@ public class SerialPortHandler {
/**
* 接收指定长度的响应数据
* @param length 期望接收的字节数
*
* @param length 期望接收的字节数
* @param timeoutMs 超时时间(毫秒)
* @return 接收到的字符串超时或无数据返回null
*/
@@ -207,8 +212,7 @@ public class SerialPortHandler {
int bytesRead = 0;
long startTime = System.currentTimeMillis();
while (bytesRead < length &&
(System.currentTimeMillis() - startTime) < timeoutMs) {
while (bytesRead < length && (System.currentTimeMillis() - startTime) < timeoutMs) {
if (inputStream.available() > 0) {
int read = inputStream.read(buffer, bytesRead, length - bytesRead);
if (read > 0) {
@@ -276,7 +280,7 @@ public class SerialPortHandler {
}
} catch (IOException e) {
log.error("关闭串口失败: {}", e.getMessage());
}finally {
} finally {
serialPort = null;
}
}

View File

@@ -38,9 +38,9 @@ public class MaterialInfoDO extends BaseDO {
private BigDecimal unitWeight;
/*
物料规格
物料直径
*/
private String materialSpec;
private Double materialSpec;
/**
* 物料照片地址
@@ -61,4 +61,9 @@ public class MaterialInfoDO extends BaseDO {
* 灯光等级
*/
private Integer lightLevel;
/**
* 颜色
*/
private String color;
}

View File

@@ -46,10 +46,10 @@ public class MaterialImportRowReq implements Serializable {
private String lightLevelName;
/*
* 物料规格
* 物料直径
* */
@Schema(description = "物料规格")
private String materialSpec;
@Schema(description = "物料直径")
private Double materialSpec;
/**
* 物料类型名称
@@ -63,4 +63,10 @@ public class MaterialImportRowReq implements Serializable {
@Schema(description = "物料流程")
private String materialProcess;
/**
* 颜色
*/
@Schema(description = "颜色")
private String color;
}

View File

@@ -48,10 +48,10 @@ public class MaterialInfoReq implements Serializable {
private Double unitWeight;
/*
* 物料规格
* 物料直径
* */
@Schema(description = "物料规格")
private String materialSpec;
@Schema(description = "物料直径")
private Double materialSpec;
/**
* 物料照片地址
@@ -79,4 +79,10 @@ public class MaterialInfoReq implements Serializable {
*/
@Schema(description = "灯光等级")
private Integer lightLevel;
/**
* 颜色
*/
@Schema(description = "颜色")
private String color;
}

View File

@@ -45,11 +45,11 @@ public class MaterialInfoResp extends BaseDetailResp {
private Double unitWeight;
/**
* 物料规格
* 物料直径
*/
@Schema(description = "物料规格")
@ExcelProperty(value = "物料规格", order = 5)
private String materialSpec;
@Schema(description = "物料直径")
@ExcelProperty(value = "物料直径", order = 5)
private Double materialSpec;
/**
* 物料照片地址
@@ -100,4 +100,11 @@ public class MaterialInfoResp extends BaseDetailResp {
@ExcelProperty(value = "灯光等级", converter = LightLevelEnumConverter.class, order = 4)
private Integer lightLevel;
/**
* 颜色
*/
@Schema(description = "颜色")
@ExcelProperty(value = "颜色", order = 6)
private String color;
}

View File

@@ -51,7 +51,6 @@ import top.wms.admin.material.model.resp.MaterialImportParseResp;
import top.wms.admin.material.model.resp.MaterialInfoImportResp;
import top.wms.admin.material.model.resp.MaterialInfoResp;
import top.wms.admin.material.service.MaterialInfoService;
import top.wms.admin.materialProcess.model.entity.MaterialProcessDO;
import top.wms.admin.materialType.mapper.MaterialTypeMapper;
import top.wms.admin.materialType.model.entity.MaterialTypeDO;
import top.wms.admin.system.service.FileService;
@@ -238,8 +237,11 @@ public class MaterialInfoServiceImpl extends BaseServiceImpl<MaterialInfoMapper,
}
MaterialInfoDO materialDO = BeanUtil.toBeanIgnoreError(row, MaterialInfoDO.class);
materialDO.setUnitWeight(NumberUtil.isValidNumber(row.getUnitWeight()) ? row.getUnitWeight() : null);
materialDO.setMaterialSpec(StrUtil.isNotBlank(row.getMaterialSpec()) ? row.getMaterialSpec() : null);
materialDO.setMaterialProcess(StrUtil.isNotBlank(row.getMaterialProcess()) ? row.getMaterialProcess() : null);
materialDO.setMaterialSpec(row.getMaterialSpec());
materialDO.setColor(row.getColor());
materialDO.setMaterialProcess(StrUtil.isNotBlank(row.getMaterialProcess())
? row.getMaterialProcess()
: null);
materialDO.setMaterialTypeId(materialTypeMap.get(row.getTypeName()));
materialDO.setLightLevel(lightLevelMap.get(row.getLightLevelName()));
// 修改 or 新增

View File

@@ -67,10 +67,10 @@ public class WorkOrderResp extends BaseDetailResp {
private BigDecimal unitWeight;
/**
* 物料规格
* 物料直径
*/
@Schema(description = "物料规格")
private String materialSpec;
@Schema(description = "物料直径")
private Double materialSpec;
/**
* 物料图片

View File

@@ -38,7 +38,7 @@ public class LightController {
if (connected) {
return R.ok();
} else {
return R.fail("500","灯光连接失败");
return R.fail("500", "灯光连接失败");
}
}
@@ -79,12 +79,13 @@ public class LightController {
if (success) {
return R.ok();
} else {
return R.fail("500","设置灯光亮度失败,请检查连接状态");
return R.fail("500", "设置灯光亮度失败,请检查连接状态");
}
}
/**
* 读取通道亮度
*
* @param channel 通道号 (1-2)
*/
@GetMapping("/brightness")
@@ -96,6 +97,7 @@ public class LightController {
/**
* 打开指定通道
*
* @param channel 通道号 (1-2)
*/
@PostMapping("/turn-on")
@@ -107,6 +109,7 @@ public class LightController {
/**
* 关闭指定通道
*
* @param channel 通道号 (1-2)
*/
@PostMapping("/turn-off")
@@ -118,6 +121,7 @@ public class LightController {
/**
* 设置所有通道亮度
*
* @param brightness 亮度等级 (0-255)
*/
@PostMapping("/all-brightness")

View File

@@ -1,15 +1,22 @@
package top.wms.admin.controller.vm;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.continew.starter.core.exception.BusinessException;
import top.continew.starter.core.validation.CheckUtils;
import top.wms.admin.controller.tcp.config.SimpleRequestMatcher;
import top.wms.admin.controller.tcp.manager.ChannelManager;
import top.wms.admin.controller.tcp.service.SaveBmpTaskService;
import top.wms.admin.material.mapper.MaterialInfoMapper;
import top.wms.admin.material.model.entity.MaterialInfoDO;
import top.wms.admin.material.model.resp.MaterialInfoResp;
import top.wms.admin.material.service.MaterialInfoService;
@Slf4j
@RestController
@@ -25,11 +32,16 @@ public class VmCommandController {
@Autowired
private SaveBmpTaskService saveBmpTaskService;
@Autowired
private MaterialInfoMapper materialInfoMapper;
@PostMapping("/send")
public String sendAndWait(@RequestBody JSONObject js) {
String materialProcess = js.getString("materialProcess");
log.info("开始比对: {}", materialProcess);
CheckUtils.throwIf(StrUtil.isBlank(materialProcess), "物料流程编码不能为空");
String materialCode = js.getString("materialCode");
log.info("开始比对: {}", materialCode);
MaterialInfoDO materialInfoDO = materialInfoMapper.selectOne(new QueryWrapper<MaterialInfoDO>().eq("encoding", materialCode));
CheckUtils.throwIf(ObjectUtil.isEmpty(materialInfoDO), "物料数据异常");
CheckUtils.throwIf(StrUtil.isBlank(materialInfoDO.getMaterialProcess()), "物料流程编码不能为空");
// 1. 检查连接
Channel channel = channelManager.getFirstChannel();
@@ -38,16 +50,42 @@ public class VmCommandController {
}
// 2. 清空之前的响应队列,避免影响当前请求
requestMatcher.clear();
channel.writeAndFlush(materialProcess);
channel.writeAndFlush(materialInfoDO.getMaterialProcess());
// 3. 等待响应
String response = requestMatcher.waitForResponse(20);
log.info("sendAndWait-收到响应: {}", response);
CheckUtils.throwIf("TIMEOUT".equals(response), "响应超时,请重试");
CheckUtils.throwIf("ERROR".equals(response), "设备连接异常!");
//如果是圆管,数据格式为yg_yellow_139.092或者yg_yellow
if (response.contains("yg_")) {
CheckUtils.throwIf(null == materialInfoDO.getMaterialSpec(), "物料直径不能为空");
String[] split = response.split("_");
if (split.length != 3) {
throw new BusinessException("位置摆放不标准,未检查出直径");
}
if (!StrUtil.equals(split[1], materialInfoDO.getColor())) {
throw new BusinessException("颜色不匹配");
}
try {
double measuredDiameter = Double.parseDouble(split[2]);
double difference = Math.abs(measuredDiameter - materialInfoDO.getMaterialSpec());
if (difference > 5) {
throw new BusinessException("直径不匹配");
}
} catch (NumberFormatException e) {
throw new BusinessException("相机识别异常");
}
} else if (StrUtil.equals(response, materialCode)) {
response = "success";
} else {
response = "failed";
}
return response;
}
@PostMapping("/start")
public boolean startTask() {
return saveBmpTaskService.startTask();