package com.isu.gaswellwatch.vo.command; import com.fasterxml.jackson.annotation.JsonSubTypes; import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.isu.gaswellwatch.dto.modbus.ModbusCommandDto; import com.isu.gaswellwatch.vo.command.knpcv1.mode.*; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import lombok.experimental.SuperBuilder; import org.apache.commons.lang3.StringUtils; import java.io.Serial; import java.io.Serializable; import java.security.InvalidParameterException; import java.util.Collection; import java.util.Objects; /** * @author 王仕龙 * 2024/11/26 19:59 */ @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "code") @JsonSubTypes(value = { @JsonSubTypes.Type(value = TimerMode.class, name = Command.KNPCV1_RUN_TIMER), @JsonSubTypes.Type(value = TimingMode.class, name = Command.KNPCV1_RUN_TIMING), @JsonSubTypes.Type(value = ManualMode.class, name = Command.KNPCV1_RUN_MANUAL), @JsonSubTypes.Type(value = PlungerMode.class, name = Command.KNPCV1_RUN_PLUNGER), @JsonSubTypes.Type(value = PressureMode.class, name = Command.KNPCV1_RUN_PRESSURE), @JsonSubTypes.Type(value = TimePressureMode.class, name = Command.KNPCV1_RUN_TIME_PRESSURE), @JsonSubTypes.Type(value = com.isu.gaswellwatch.vo.command.etc.TurnOn.class, name = Command.ETC_TURN_ON_THE_WELL), @JsonSubTypes.Type(value = com.isu.gaswellwatch.vo.command.etc.TurnOff.class, name = Command.ETC_TURN_OFF_THE_WELL), @JsonSubTypes.Type(value = com.isu.gaswellwatch.vo.command.scss.TurnOn.class, name = Command.SCSS_TURN_ON_THE_WELL), @JsonSubTypes.Type(value = com.isu.gaswellwatch.vo.command.scss.TurnOff.class, name = Command.SCSS_TURN_OFF_THE_WELL), @JsonSubTypes.Type(value = com.isu.gaswellwatch.vo.command.knpcv1.TurnOn.class, name = Command.KNPCV1_TURN_ON_THE_WELL), @JsonSubTypes.Type(value = com.isu.gaswellwatch.vo.command.knpcv1.TurnOff.class, name = Command.KNPCV1_TURN_OFF_THE_WELL), }) @Getter @Setter @SuperBuilder @NoArgsConstructor @AllArgsConstructor public abstract class Command implements Serializable { @Serial private static final long serialVersionUID = -2590497444442160271L; /* 点表类型:KNPCV1 start */ /* 开关井 */ public static final String KNPCV1_TURN_ON_THE_WELL = "KNPCV1.TURN_ON_THE_WELL"; public static final String KNPCV1_TURN_OFF_THE_WELL = "KNPCV1.TURN_OFF_THE_WELL"; /* 运行模式*/ public static final String KNPCV1_RUN_TIMER = "KNPCV1.RUN.TIMER"; public static final String KNPCV1_RUN_TIMING = "KNPCV1.RUN.TIMING"; public static final String KNPCV1_RUN_MANUAL = "KNPCV1.RUN.MANUAL"; public static final String KNPCV1_RUN_PLUNGER = "KNPCV1.RUN.PLUNGER"; public static final String KNPCV1_RUN_PRESSURE = "KNPCV1.RUN.PRESSURE"; public static final String KNPCV1_RUN_TIME_PRESSURE = "KNPCV1.RUN.TIME_PRESSURE"; /* 点表类型:KNPCV1 end */ /* 点表类型:SCSS start */ /* 开关井 */ public static final String SCSS_TURN_ON_THE_WELL = "SCSS.TURN_ON_THE_WELL"; public static final String SCSS_TURN_OFF_THE_WELL = "SCSS.TURN_OFF_THE_WELL"; /* 运行模式*/ /* 点表类型:SCSS end */ /* 点表类型:ETC start */ /* 开关井 */ public static final String ETC_TURN_ON_THE_WELL = "ETC.TURN_ON_THE_WELL"; public static final String ETC_TURN_OFF_THE_WELL = "ETC.TURN_OFF_THE_WELL"; /* 运行模式*/ /* 点表类型:ETC end */ @NotBlank(message = "指令编码不能为空") private String code; @NotNull(message = "下发设备标识不能为空") private Long deviceId; public Collection buildModbusCommand() { if (!this.validate()) { throw new InvalidParameterException("Invalid command"); } return this.builderModbusCommand(); } public boolean validate() { return Objects.nonNull(this.deviceId) && StringUtils.isNotBlank(this.code); } protected abstract Collection builderModbusCommand(); }