Browse Source

feat:增加虚拟模式

master
白凤吉 1 week ago
parent
commit
81c7a5a350
  1. 16
      src/main/java/com/iflytop/colortitration/app/core/event/VirtualDeviceCmdResponseEvent.java
  2. 20
      src/main/java/com/iflytop/colortitration/app/core/listener/VirtualDeviceCmdResponseEventListener.java
  3. 23
      src/main/java/com/iflytop/colortitration/app/service/DeviceCommandService.java
  4. 45
      src/main/java/com/iflytop/colortitration/app/service/VirtualDeviceService.java

16
src/main/java/com/iflytop/colortitration/app/core/event/VirtualDeviceCmdResponseEvent.java

@ -0,0 +1,16 @@
package com.iflytop.colortitration.app.core.event;
import com.iflytop.colortitration.app.core.command.DeviceCommand;
import lombok.Getter;
import org.springframework.context.ApplicationEvent;
@Getter
public class VirtualDeviceCmdResponseEvent extends ApplicationEvent {
private final DeviceCommand cmdToDevice;
public VirtualDeviceCmdResponseEvent(Object source, DeviceCommand cmdToDevice) {
super(source);
this.cmdToDevice = cmdToDevice;
}
}

20
src/main/java/com/iflytop/colortitration/app/core/listener/VirtualDeviceCmdResponseEventListener.java

@ -0,0 +1,20 @@
package com.iflytop.colortitration.app.core.listener;
import com.iflytop.colortitration.app.core.event.VirtualDeviceCmdResponseEvent;
import com.iflytop.colortitration.app.service.VirtualDeviceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
@Slf4j
@Component
@RequiredArgsConstructor
public class VirtualDeviceCmdResponseEventListener {
private final VirtualDeviceService virtualDeviceService;
@EventListener
public void handleDeviceTcpMessageEvent(VirtualDeviceCmdResponseEvent event) {
virtualDeviceService.completeCommandResponse(event.getCmdToDevice());
}
}

23
src/main/java/com/iflytop/colortitration/app/service/DeviceCommandService.java

@ -2,10 +2,12 @@ package com.iflytop.colortitration.app.service;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.iflytop.colortitration.app.common.constant.CommandStatus;
import com.iflytop.colortitration.app.core.command.CommandFuture;
import com.iflytop.colortitration.app.core.command.CyclicNumberGenerator;
import com.iflytop.colortitration.app.core.command.DeviceCommand;
import com.iflytop.colortitration.app.core.event.VirtualDeviceCmdResponseEvent;
import com.iflytop.colortitration.app.core.state.DeviceState;
import com.iflytop.colortitration.app.websocket.server.DebugGenerator;
import com.iflytop.colortitration.app.websocket.server.WebSocketSender;
@ -13,6 +15,7 @@ import com.iflytop.colortitration.hardware.HardwareService;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
@ -29,6 +32,8 @@ public class DeviceCommandService {
private final HardwareService hardwareService;
private final WebSocketSender webSocketService;
private final DeviceState deviceState;
private final ApplicationEventPublisher publisher;
/**
* 需要等待加液区空闲的龙门架机械臂指令
*/
@ -109,21 +114,9 @@ public class DeviceCommandService {
}
} else {
//虚拟模式
new Thread(() -> {
try {
String actionName = commandFuture.getDeviceCommand().getAction().name();
if (actionName.contains("move") || actionName.contains("origin")) {
Thread.sleep(300);
}
JSONObject jsonObject = new JSONObject();
jsonObject.putOnce("cmdId", cmdId);
jsonObject.putOnce("success", true);
completeCommandResponse(jsonObject);
} catch (InterruptedException e) {
// 处理中断异常
Thread.currentThread().interrupt();
}
}).start();
log.info("模拟向设备发送TCP指令:{}", JSONUtil.toJsonStr(commandFuture.getDeviceCommand()));
//模拟反馈
publisher.publishEvent(new VirtualDeviceCmdResponseEvent(this, commandFuture.getDeviceCommand()));
}
}

45
src/main/java/com/iflytop/colortitration/app/service/VirtualDeviceService.java

@ -0,0 +1,45 @@
package com.iflytop.colortitration.app.service;
import cn.hutool.json.JSONObject;
import com.iflytop.colortitration.app.core.command.DeviceCommand;
import com.iflytop.colortitration.common.enums.Action;
import com.iflytop.colortitration.common.enums.Device;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
/**
* 虚拟设备服务
*/
@Service
@RequiredArgsConstructor
public class VirtualDeviceService {
private final DeviceCommandService deviceCommandService;
public void completeCommandResponse(DeviceCommand cmdToDevice) {
new Thread(() -> {
try {
JSONObject jsonObject = new JSONObject();
jsonObject.putOnce("cmdId", cmdToDevice.getCmdId());
jsonObject.putOnce("success", true);
String code = cmdToDevice.getCmdCode();
Action action = cmdToDevice.getAction();
Device device = cmdToDevice.getDevice();
if (code.contains("controlMotorCmd")) {
if (Action.ORIGIN.equals(action)) {
Thread.sleep(3000);
} else if (!Action.SET.equals(action)) {//非设置电机参数也就是电机移动
Thread.sleep(500);
}
} else if (code.contains("getInfoCmd")) {
}
deviceCommandService.completeCommandResponse(jsonObject);
} catch (InterruptedException e) {
// 处理中断异常
Thread.currentThread().interrupt();
}
}).start();
}
}
Loading…
Cancel
Save