diff --git a/src/main/java/com/iflytop/gd/app/model/vo/HeatCountdownVO.java b/src/main/java/com/iflytop/gd/app/model/vo/HeatCountdownVO.java new file mode 100644 index 0000000..bdaaba3 --- /dev/null +++ b/src/main/java/com/iflytop/gd/app/model/vo/HeatCountdownVO.java @@ -0,0 +1,32 @@ +package com.iflytop.gd.app.model.vo; + +import com.fasterxml.jackson.annotation.JsonFormat; +import com.iflytop.gd.app.model.entity.Solutions; +import com.iflytop.gd.common.enums.HeatModuleCode; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import java.time.LocalDateTime; + +@Schema(description = "加热倒计时") +@Data +public class HeatCountdownVO { + @Schema(description = "加热模块code") + private HeatModuleCode heatModuleCode; + + @Schema(description = "加热剩余秒数格式化显示") + private String countdownStr; + + @Schema(description = "加热剩余秒数") + private Integer countdown; + + @Schema(description = "开始加热时间") + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private LocalDateTime startTime; + + @Schema(description = "结束加热时间") + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private LocalDateTime endTime; + + +} diff --git a/src/main/java/com/iflytop/gd/app/service/device/module/OtherModuleService.java b/src/main/java/com/iflytop/gd/app/service/device/module/OtherModuleService.java new file mode 100644 index 0000000..3ee5f09 --- /dev/null +++ b/src/main/java/com/iflytop/gd/app/service/device/module/OtherModuleService.java @@ -0,0 +1,34 @@ +package com.iflytop.gd.app.service.device.module; + +import com.iflytop.gd.hardware.drivers.DODriver.OutputIOCtrlDriver; +import com.iflytop.gd.hardware.type.IO.OutputIOMId; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +import java.util.concurrent.CompletableFuture; + +/** + * 其他模块 + */ +@Slf4j +@Service +@RequiredArgsConstructor +public class OtherModuleService { + private final OutputIOCtrlDriver outputIOCtrlDriver; + + /** + * 工艺执行完毕蜂鸣器提醒 + */ + public void craftsFinishBeepRemind(){ + CompletableFuture.runAsync(() -> { + try { + outputIOCtrlDriver.open(OutputIOMId.DO_BEEP); + Thread.sleep(2000); + outputIOCtrlDriver.close(OutputIOMId.DO_BEEP); + } catch (Exception e) { + log.error("工艺执行完毕蜂鸣器调用失败", e); + } + }); + } +} diff --git a/src/main/java/com/iflytop/gd/app/service/scheduled/HeatCountdownScheduledTask.java b/src/main/java/com/iflytop/gd/app/service/scheduled/HeatCountdownScheduledTask.java new file mode 100644 index 0000000..49e9a08 --- /dev/null +++ b/src/main/java/com/iflytop/gd/app/service/scheduled/HeatCountdownScheduledTask.java @@ -0,0 +1,86 @@ +package com.iflytop.gd.app.service.scheduled; + +import com.iflytop.gd.app.model.bo.status.device.HeatModuleState; +import com.iflytop.gd.app.model.vo.HeatCountdownVO; +import com.iflytop.gd.app.service.api.DevicePositionService; +import com.iflytop.gd.app.service.device.DeviceStateService; +import com.iflytop.gd.app.service.device.module.HeatModuleService; +import com.iflytop.gd.app.ws.server.WebSocketSender; +import com.iflytop.gd.common.enums.HeatingType; +import com.iflytop.gd.common.enums.data.DevicePositionCode; +import com.iflytop.gd.common.utils.LocalDateTimeUtil; +import jakarta.annotation.PostConstruct; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; + +import java.time.Duration; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; + +/** + * 加热倒计时 + */ +@Slf4j +@Service +@RequiredArgsConstructor +public class HeatCountdownScheduledTask { + private final DeviceStateService deviceStateService; + private final DevicePositionService devicePositionService; + private final HeatModuleService heatModuleService; + private final WebSocketSender webSocketSender; + + @Scheduled(fixedRate = 1000) + public void fetchTemperature() { + try { + LocalDateTime now = LocalDateTime.now(); + List heatCountdownVOList = new ArrayList<>(); + for (HeatModuleState heatModuleState : deviceStateService.getDeviceState().getHeatModule()) { + if (heatModuleState.getHeatingType() == HeatingType.thermostatic) {//如果这个加热模块在加热中 + if (heatModuleState.getTemperature() + 1 > heatModuleState.getHeatTemperature()) {//当前温度达到目标温度,允许有1度以内的误差 + heatModuleState.setHeatingType(HeatingType.constant);//修改状态为恒温中 + if (heatModuleState.getStartHeatTime() == null) { + //设定开始加热时间 + heatModuleState.setStartHeatTime(now); + } else { + LocalDateTime endTime = heatModuleState.getStartHeatTime().minusSeconds(heatModuleState.getTargetTime()); + //判断是否达到目标加热时间 + if (endTime.isBefore(now)) {//加热完毕 + double trayLift = devicePositionService.getPosition(DevicePositionCode.trayLift).getDistance(); + //抬起托盘 + heatModuleService.heaterMotorMove(heatModuleState.getModuleCode(), trayLift); + heatModuleState.setTrayUp(1); + //关闭加棒 + heatModuleService.heatRodClose(heatModuleState.getModuleCode()); + heatModuleState.setHeatingType(HeatingType.stop); + //还原状态 + heatModuleState.setStartHeatTime(null); + heatModuleState.setTargetTime(null); + heatModuleState.setWarmUpTemperature(null); + heatModuleState.setHeatTemperature(null); + heatModuleState.setTargetTemperature(null); + heatModuleState.setHeatingType(HeatingType.stop); + } else {//加热中 + long diffSeconds = Duration.between(now, endTime).getSeconds();//计算剩余时间 + HeatCountdownVO heatCountdownVO = new HeatCountdownVO(); + heatCountdownVO.setHeatModuleCode(heatModuleState.getModuleCode()); + heatCountdownVO.setCountdown((int) diffSeconds); + heatCountdownVO.setCountdownStr(LocalDateTimeUtil.formatSecondsToHMS(diffSeconds)); + heatCountdownVO.setStartTime(heatModuleState.getStartHeatTime()); + heatCountdownVO.setEndTime(endTime); + heatCountdownVOList.add(heatCountdownVO); + } + } + } + } + } + if (!heatCountdownVOList.isEmpty()) { + webSocketSender.pushHeatCountdown(heatCountdownVOList); + } + } catch (Exception e) { + log.error("加热倒计时错误", e); + } + } +} diff --git a/src/main/java/com/iflytop/gd/common/utils/LocalDateTimeUtil.java b/src/main/java/com/iflytop/gd/common/utils/LocalDateTimeUtil.java new file mode 100644 index 0000000..5c8a751 --- /dev/null +++ b/src/main/java/com/iflytop/gd/common/utils/LocalDateTimeUtil.java @@ -0,0 +1,18 @@ +package com.iflytop.gd.common.utils; + +public class LocalDateTimeUtil { + + /** + * 秒数格式化为 “HH:mm:ss” 格式。 + * @param diffSeconds 两个时间点相差的秒数(可以为正或负) + * @return 格式化后的 “HH:mm:ss” 字符串 + */ + public static String formatSecondsToHMS(long diffSeconds) { + long absSeconds = Math.abs(diffSeconds); + long hours = absSeconds / 3600; + long minutes = (absSeconds % 3600) / 60; + long seconds = absSeconds % 60; + return String.format("%02d:%02d:%02d", hours, minutes, seconds); + } + +} \ No newline at end of file