6 changed files with 158 additions and 1 deletions
-
8src/main/java/com/iflytop/colortitration/app/controller/TestController.java
-
45src/main/java/com/iflytop/colortitration/app/core/aspect/DeviceStateChangeAspect.java
-
14src/main/java/com/iflytop/colortitration/app/core/event/StateChangeEvent.java
-
48src/main/java/com/iflytop/colortitration/app/core/state/DeviceState.java
-
41src/main/java/com/iflytop/colortitration/app/core/state/HeatModuleState.java
-
3src/main/java/com/iflytop/colortitration/app/websocket/server/WebsocketResult.java
@ -0,0 +1,45 @@ |
|||
package com.iflytop.colortitration.app.core.aspect; |
|||
|
|||
import com.iflytop.colortitration.app.core.state.DeviceState; |
|||
import com.iflytop.colortitration.app.websocket.server.WebSocketMessageType; |
|||
import com.iflytop.colortitration.app.websocket.server.WebSocketSender; |
|||
import lombok.RequiredArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.aspectj.lang.JoinPoint; |
|||
import org.aspectj.lang.annotation.After; |
|||
import org.aspectj.lang.annotation.Aspect; |
|||
import org.aspectj.lang.annotation.Before; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.util.concurrent.locks.Lock; |
|||
import java.util.concurrent.locks.ReentrantLock; |
|||
|
|||
@Slf4j |
|||
@Aspect |
|||
@Component |
|||
@RequiredArgsConstructor |
|||
public class DeviceStateChangeAspect { |
|||
private final WebSocketSender webSocketService; |
|||
private final DeviceState deviceState; |
|||
|
|||
private final Lock lock = new ReentrantLock(); |
|||
|
|||
@Before("execution(* com.iflytop.colortitration.app.core.state.*.set*(..))") |
|||
public void beforeSetMethod(JoinPoint joinPoint) { |
|||
lock.lock(); |
|||
} |
|||
|
|||
@After("execution(* com.iflytop.colortitration.app.core.state.*.set*(..))") |
|||
public void afterSetMethod(JoinPoint joinPoint) { |
|||
try { |
|||
Object[] methodArgs = joinPoint.getArgs(); |
|||
if (methodArgs != null && methodArgs.length > 0) { |
|||
webSocketService.push(WebSocketMessageType.STATUS, deviceState.toJSON()); |
|||
} |
|||
} catch (Exception e) { |
|||
log.error("处理状态变更后的值失败", e); |
|||
} finally { |
|||
lock.unlock(); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,14 @@ |
|||
package com.iflytop.colortitration.app.core.event; |
|||
import lombok.AllArgsConstructor; |
|||
import lombok.Data; |
|||
|
|||
/** |
|||
* 状态变更事件 |
|||
*/ |
|||
@Data |
|||
@AllArgsConstructor |
|||
public class StateChangeEvent { |
|||
private String fieldPath; |
|||
private Object oldValue; |
|||
private Object newValue; |
|||
} |
@ -0,0 +1,48 @@ |
|||
package com.iflytop.colortitration.app.core.state; |
|||
|
|||
import cn.hutool.json.JSONObject; |
|||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
|||
import com.iflytop.colortitration.common.enums.Device; |
|||
import com.iflytop.colortitration.common.model.entity.User; |
|||
import io.swagger.v3.oas.annotations.media.Schema; |
|||
import lombok.Data; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
|
|||
@Schema(description = "设备当前状态") |
|||
@Data |
|||
@Component |
|||
@JsonIgnoreProperties(value = {"advisors", "frozen", "preFiltered", "proxyTargetClass", "targetSource", "exposeProxy", "advisorCount", "proxiedInterfaces", "targetClass"}) |
|||
public class DeviceState { |
|||
@Schema(description = "加热模块属性") |
|||
private Map<Device, HeatModuleState> heatModule = new HashMap<>(); |
|||
|
|||
@Schema(description = "虚拟模式,true为虚拟") |
|||
private boolean virtual = false; |
|||
|
|||
@Schema(description = "初始化状态,true初始化完毕") |
|||
private boolean initComplete = false; |
|||
|
|||
@Schema(description = "自检状态,true自检完毕") |
|||
private boolean selfTest = false; |
|||
|
|||
@Schema(description = "是否是急停状态,true为急停") |
|||
private boolean emergencyStop = false; |
|||
|
|||
@Schema(description = "当前登录用户") |
|||
private User currentUser; |
|||
|
|||
public JSONObject toJSON() { |
|||
JSONObject json = new JSONObject(); |
|||
json.putOnce("heatModule", new ArrayList<>(heatModule.values())); |
|||
json.putOnce("virtual", virtual); |
|||
json.putOnce("initComplete", initComplete); |
|||
json.putOnce("selfTest", selfTest); |
|||
json.putOnce("emergencyStop", emergencyStop); |
|||
json.putOnce("currentUser", currentUser); |
|||
return json; |
|||
} |
|||
} |
@ -0,0 +1,41 @@ |
|||
package com.iflytop.colortitration.app.core.state; |
|||
|
|||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
|||
import com.iflytop.colortitration.common.enums.Device; |
|||
import io.swagger.v3.oas.annotations.media.Schema; |
|||
import lombok.Data; |
|||
import lombok.RequiredArgsConstructor; |
|||
import org.springframework.context.annotation.Scope; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.time.LocalDateTime; |
|||
|
|||
@Schema(description = "当前加热模块状态") |
|||
@Data |
|||
@Component |
|||
@Scope("prototype") |
|||
@RequiredArgsConstructor |
|||
@JsonIgnoreProperties(value = {"advisors", "frozen", "preFiltered", "proxyTargetClass", "targetSource", "exposeProxy", "advisorCount", "proxiedInterfaces", "targetClass"}) |
|||
public class HeatModuleState { |
|||
@Schema(description = "加热模块code") |
|||
private Device deviceCode; |
|||
|
|||
@Schema(description = "是否启动加热,true为正在加热,false为未在加热") |
|||
private boolean open = false; |
|||
|
|||
@Schema(description = "加热器当前温度") |
|||
private Double temperature = null; |
|||
|
|||
@Schema(description = "加热器目标温度") |
|||
private Double targetTemperature = null; |
|||
|
|||
@Schema(description = "加热器目标加热时间,单位秒") |
|||
private Integer targetTime = null; |
|||
|
|||
@Schema(description = "开始加热的时间") |
|||
private LocalDateTime startHeatTime = null; |
|||
|
|||
public HeatModuleState(Device deviceCode) { |
|||
this.deviceCode = deviceCode; |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue