From 691a6fc4ec4561116f904e1ac451faf47199ba78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E5=87=A4=E5=90=89?= Date: Tue, 29 Apr 2025 09:21:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E5=B7=A5=E8=89=BA=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E6=9B=B4=E5=A4=9A=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gd/app/controller/CraftsController.java | 1 - .../com/iflytop/gd/app/core/CraftsContext.java | 5 +++- .../gd/app/mapper/DeviceParamConfigMapper.java | 12 ++++++++++ .../gd/app/model/entity/DeviceParamConfig.java | 28 ++++++++++++++++++++++ .../com/iflytop/gd/app/model/vo/CraftStatusVO.java | 9 +++++++ .../com/iflytop/gd/app/service/CraftsService.java | 7 ++++++ .../gd/app/service/DeviceParamConfigService.java | 16 +++++++++++++ .../gd/infrastructure/config/WebSocketServer.java | 11 +++++---- src/main/resources/sql/init.sql | 10 ++++++++ 9 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/iflytop/gd/app/mapper/DeviceParamConfigMapper.java create mode 100644 src/main/java/com/iflytop/gd/app/model/entity/DeviceParamConfig.java create mode 100644 src/main/java/com/iflytop/gd/app/service/DeviceParamConfigService.java diff --git a/src/main/java/com/iflytop/gd/app/controller/CraftsController.java b/src/main/java/com/iflytop/gd/app/controller/CraftsController.java index 1d48f69..6358641 100644 --- a/src/main/java/com/iflytop/gd/app/controller/CraftsController.java +++ b/src/main/java/com/iflytop/gd/app/controller/CraftsController.java @@ -2,7 +2,6 @@ package com.iflytop.gd.app.controller; import com.iflytop.gd.app.model.dto.*; import com.iflytop.gd.app.model.entity.Crafts; -import com.iflytop.gd.app.model.vo.CraftStatusVO; import com.iflytop.gd.app.model.vo.SetCraftsVO; import com.iflytop.gd.app.service.CraftsService; import com.iflytop.gd.common.result.Result; diff --git a/src/main/java/com/iflytop/gd/app/core/CraftsContext.java b/src/main/java/com/iflytop/gd/app/core/CraftsContext.java index 2698057..968f09c 100644 --- a/src/main/java/com/iflytop/gd/app/core/CraftsContext.java +++ b/src/main/java/com/iflytop/gd/app/core/CraftsContext.java @@ -5,6 +5,7 @@ import com.iflytop.gd.app.common.enums.CraftEvents; import com.iflytop.gd.app.common.enums.CraftStates; import com.iflytop.gd.app.model.bo.CraftsStep; import com.iflytop.gd.app.model.entity.Crafts; +import com.iflytop.gd.app.model.entity.Ores; import com.iflytop.gd.app.service.CraftsStepService; import com.iflytop.gd.app.service.WebSocketService; import com.iflytop.gd.common.constant.WebSocketMessageType; @@ -24,6 +25,7 @@ import java.util.Map; @Getter public class CraftsContext implements Runnable { private final String heatId; + private final Ores ores; private final Crafts craft; private final List craftsStepList; private final StateMachine sm; @@ -34,12 +36,13 @@ public class CraftsContext implements Runnable { /** * 构造方法,初始化上下文并启动状态机至 READY */ - public CraftsContext(String heatId, + public CraftsContext(String heatId, Ores ores, Crafts craft, StateMachineFactory factory, WebSocketService ws, CraftsStepService craftsStepService) { this.heatId = heatId; + this.ores = ores; this.craft = craft; this.craftsStepList = JSONUtil.parseArray(craft.getSteps()).toList(CraftsStep.class); this.ws = ws; diff --git a/src/main/java/com/iflytop/gd/app/mapper/DeviceParamConfigMapper.java b/src/main/java/com/iflytop/gd/app/mapper/DeviceParamConfigMapper.java new file mode 100644 index 0000000..789cf72 --- /dev/null +++ b/src/main/java/com/iflytop/gd/app/mapper/DeviceParamConfigMapper.java @@ -0,0 +1,12 @@ +package com.iflytop.gd.app.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.iflytop.gd.app.model.entity.DeviceParamConfig; +import org.apache.ibatis.annotations.Mapper; + +/** + * 设备参数配置持久层接口 + */ +@Mapper +public interface DeviceParamConfigMapper extends BaseMapper { +} diff --git a/src/main/java/com/iflytop/gd/app/model/entity/DeviceParamConfig.java b/src/main/java/com/iflytop/gd/app/model/entity/DeviceParamConfig.java new file mode 100644 index 0000000..913fa92 --- /dev/null +++ b/src/main/java/com/iflytop/gd/app/model/entity/DeviceParamConfig.java @@ -0,0 +1,28 @@ +package com.iflytop.gd.app.model.entity; + +import com.baomidou.mybatisplus.annotation.TableName; +import com.iflytop.gd.infrastructure.repository.base.BaseEntity; +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotNull; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@EqualsAndHashCode(callSuper = true) +@Schema(description = "设备参数配置") +@TableName("device_param_config") +@Data +public class DeviceParamConfig extends BaseEntity { + + @NotNull + @Schema(description = "模块标识") + private String mid; + + @NotNull + @Schema(description = "寄存器索引") + private String regIndex; + + @NotNull + @Schema(description = "寄存器值") + private Integer regVal; + +} \ No newline at end of file diff --git a/src/main/java/com/iflytop/gd/app/model/vo/CraftStatusVO.java b/src/main/java/com/iflytop/gd/app/model/vo/CraftStatusVO.java index 2a0a6b2..58db65a 100644 --- a/src/main/java/com/iflytop/gd/app/model/vo/CraftStatusVO.java +++ b/src/main/java/com/iflytop/gd/app/model/vo/CraftStatusVO.java @@ -16,9 +16,18 @@ public class CraftStatusVO { @Schema(description = "加热区 ID") private String heatId; + @Schema(description = "矿石 ID") + private Long oresId; + + @Schema(description = "矿石名称") + private String oresName; + @Schema(description = "工艺 ID") private Long craftsId; + @Schema(description = "工艺名称") + private String craftsName; + @Schema(description = "当前状态") private CraftStates state; diff --git a/src/main/java/com/iflytop/gd/app/service/CraftsService.java b/src/main/java/com/iflytop/gd/app/service/CraftsService.java index cdffac6..8050e62 100644 --- a/src/main/java/com/iflytop/gd/app/service/CraftsService.java +++ b/src/main/java/com/iflytop/gd/app/service/CraftsService.java @@ -62,6 +62,7 @@ public class CraftsService extends ServiceImpl { Ores ores = oresService.getById(craft.getOresId()); CraftsContext ctx = new CraftsContext( heatId, + ores, craft, stateMachineFactory, webSocketService, @@ -146,7 +147,10 @@ public class CraftsService extends ServiceImpl { } CraftStatusVO vo = new CraftStatusVO(); vo.setHeatId(heatId); + vo.setOresId(ctx.getOres().getId()); + vo.setOresName(ctx.getOres().getName()); vo.setCraftsId(ctx.getCraft().getId()); + vo.setCraftsName(ctx.getCraft().getName()); vo.setState(ctx.getSm().getState().getId()); vo.setCurrentIndex(ctx.getCurrentIndex()); vo.setSteps(ctx.getCraftsStepList()); @@ -162,7 +166,10 @@ public class CraftsService extends ServiceImpl { CraftsContext ctx = entry.getValue(); CraftStatusVO vo = new CraftStatusVO(); vo.setHeatId(heatIdKey); + vo.setOresId(ctx.getOres().getId()); + vo.setOresName(ctx.getOres().getName()); vo.setCraftsId(ctx.getCraft().getId()); + vo.setCraftsName(ctx.getCraft().getName()); vo.setState(ctx.getSm().getState().getId()); vo.setCurrentIndex(ctx.getCurrentIndex()); vo.setSteps(ctx.getCraftsStepList()); diff --git a/src/main/java/com/iflytop/gd/app/service/DeviceParamConfigService.java b/src/main/java/com/iflytop/gd/app/service/DeviceParamConfigService.java new file mode 100644 index 0000000..d9b73ed --- /dev/null +++ b/src/main/java/com/iflytop/gd/app/service/DeviceParamConfigService.java @@ -0,0 +1,16 @@ +package com.iflytop.gd.app.service; + +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.iflytop.gd.app.mapper.DeviceParamConfigMapper; +import com.iflytop.gd.app.model.entity.DeviceParamConfig; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +/** + * 设备参数配置服务 + */ +@Service +@RequiredArgsConstructor +public class DeviceParamConfigService extends ServiceImpl { + private final DeviceParamConfigMapper deviceParamConfigMapper; +} diff --git a/src/main/java/com/iflytop/gd/infrastructure/config/WebSocketServer.java b/src/main/java/com/iflytop/gd/infrastructure/config/WebSocketServer.java index d8484ab..9ff39eb 100644 --- a/src/main/java/com/iflytop/gd/infrastructure/config/WebSocketServer.java +++ b/src/main/java/com/iflytop/gd/infrastructure/config/WebSocketServer.java @@ -1,6 +1,5 @@ package com.iflytop.gd.infrastructure.config; - import jakarta.websocket.*; import jakarta.websocket.server.ServerEndpoint; import lombok.extern.slf4j.Slf4j; @@ -23,7 +22,7 @@ public class WebSocketServer { try { session.getBasicRemote().sendText(message); } catch (Exception e) { - e.printStackTrace(); + log.error("发送给客户端失败 sessionId={}", session.getId(), e); } } } @@ -32,18 +31,22 @@ public class WebSocketServer { @OnOpen public void onOpen(Session session) { sessions.add(session); + log.info("新连接加入,sessionId={}", session.getId()); } @OnMessage public void onMessage(String message, Session session) { + log.info("收到消息 sessionId={},内容:{}", session.getId(), message); } @OnClose public void onClose(Session session) { - sessions.remove(session); // 移除关闭连接的 Session + sessions.remove(session); + log.info("连接已关闭,sessionId={}", session.getId()); } @OnError - public void onError(Throwable error) { + public void onError(Session session, Throwable error) { + log.error("发生错误,sessionId={}", session.getId(), error); } } diff --git a/src/main/resources/sql/init.sql b/src/main/resources/sql/init.sql index 8aa0818..700729b 100644 --- a/src/main/resources/sql/init.sql +++ b/src/main/resources/sql/init.sql @@ -77,3 +77,13 @@ VALUES (5, '过氧酸', '2025-02-18 02:46:35', '2025-02-18 02:46:35'), (6, '磷酸', '2025-02-18 02:46:43', '2025-02-18 02:46:43'), (7, '纯水', '2025-02-18 02:46:50', '2025-02-18 02:46:50'); + +-- 设备参数配置表 +CREATE TABLE IF NOT EXISTS device_param_config ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + mid TEXT, + reg_index TEXT, + reg_val INTEGER, + create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP +);