称重优化

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

@@ -30,7 +30,7 @@ public class LightService {
serialHandler.close();
serialHandler = null;
}
// 使用COM1串口
String portName = "COM1";
// 默认波特率9600
@@ -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) {
@@ -314,5 +322,5 @@ public class LightService {
turnOn(channel);
}
}
}

View File

@@ -1,6 +1,5 @@
package top.wms.admin.light;
import com.fazecast.jSerialComm.SerialPort;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
@@ -20,13 +19,13 @@ public class SerialPortHandler {
private InputStream inputStream;
private OutputStream outputStream;
private SerialPort serialPort;
/**
* 串口名称
*/
@Getter
private final String portName;
/**
* 波特率
*/
@@ -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) {
@@ -116,7 +119,7 @@ public class SerialPortHandler {
log.error("串口 {} 未打开,无法发送数据", portName);
return;
}
if (outputStream == null) {
log.error("串口 {} 输出流未初始化", portName);
return;
@@ -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
*/
@@ -154,7 +159,7 @@ public class SerialPortHandler {
log.error("串口 {} 未打开,无法接收数据", portName);
return null;
}
// 验证输入流是否初始化
if (inputStream == null) {
log.error("串口 {} 输入流未初始化", portName);
@@ -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
*/
@@ -196,7 +201,7 @@ public class SerialPortHandler {
log.error("串口 {} 未打开,无法接收数据", portName);
return null;
}
// 验证输入流是否初始化
if (inputStream == null) {
log.error("串口 {} 输入流未初始化", portName);
@@ -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;
/**
* 物料图片