称重优化连接
This commit is contained in:
@@ -48,12 +48,12 @@ public class VmCommandController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@GetMapping("/start")
|
@PostMapping("/start")
|
||||||
public boolean startTask() {
|
public boolean startTask() {
|
||||||
return saveBmpTaskService.startTask();
|
return saveBmpTaskService.startTask();
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/stop")
|
@PostMapping("/stop")
|
||||||
public boolean stopTask() {
|
public boolean stopTask() {
|
||||||
return saveBmpTaskService.stopTask();
|
return saveBmpTaskService.stopTask();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,10 @@ import com.fazecast.jSerialComm.SerialPort;
|
|||||||
import com.fazecast.jSerialComm.SerialPortDataListener;
|
import com.fazecast.jSerialComm.SerialPortDataListener;
|
||||||
import com.fazecast.jSerialComm.SerialPortEvent;
|
import com.fazecast.jSerialComm.SerialPortEvent;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import top.continew.starter.web.model.R;
|
||||||
|
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
@@ -19,11 +22,12 @@ import java.util.regex.Pattern;
|
|||||||
/**
|
/**
|
||||||
* 安衡电子秤连接类
|
* 安衡电子秤连接类
|
||||||
*/
|
*/
|
||||||
@Component
|
@RestController
|
||||||
|
@RequestMapping("/api/weigh/ah")
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class AHDZCConnect {
|
public class AHDZCConnect {
|
||||||
|
|
||||||
private static final String PORT_NAME = "COM12";
|
private static final String PORT_NAME = "COM5";
|
||||||
private static final int BAUD_RATE = 9600;
|
private static final int BAUD_RATE = 9600;
|
||||||
private static final int DATA_BITS = 8;
|
private static final int DATA_BITS = 8;
|
||||||
private static final int STOP_BITS = 1;
|
private static final int STOP_BITS = 1;
|
||||||
@@ -47,18 +51,33 @@ public class AHDZCConnect {
|
|||||||
|
|
||||||
private ScheduledExecutorService executorService;
|
private ScheduledExecutorService executorService;
|
||||||
|
|
||||||
// @PostConstruct
|
/**
|
||||||
public void init() {
|
* 初始化并启动电子秤服务
|
||||||
// 项目启动时初始化并启动服务
|
*/
|
||||||
|
@PostMapping("/init")
|
||||||
|
public R init() {
|
||||||
|
// 先停止旧的服务,确保资源释放
|
||||||
|
stop();
|
||||||
|
// 重置状态变量
|
||||||
|
isRunning.set(true);
|
||||||
|
isConnecting.set(false);
|
||||||
|
reconnectAttempts.set(0);
|
||||||
|
currentReconnectDelay.set(INITIAL_RECONNECT_DELAY);
|
||||||
|
// 重新初始化并启动服务
|
||||||
ScaleService();
|
ScaleService();
|
||||||
start();
|
start();
|
||||||
|
return R.ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
// @PreDestroy
|
/**
|
||||||
public void destroy() {
|
* 停止电子秤服务
|
||||||
// 项目关闭时停止服务
|
*/
|
||||||
|
@PostMapping("/destroy")
|
||||||
|
public R destroy() {
|
||||||
|
log.info("调用停止电子称");
|
||||||
|
// 停止服务
|
||||||
stop();
|
stop();
|
||||||
|
return R.ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ScaleService() {
|
public void ScaleService() {
|
||||||
@@ -68,7 +87,12 @@ public class AHDZCConnect {
|
|||||||
return t;
|
return t;
|
||||||
});
|
});
|
||||||
|
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread(this::stop, "Shutdown-Hook"));
|
// 设置30分钟后自动关闭线程池
|
||||||
|
executorService.schedule(() -> {
|
||||||
|
log.info("线程池运行时间已达30分钟,准备自动关闭...");
|
||||||
|
stop();
|
||||||
|
}, 30 * 60, TimeUnit.SECONDS);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start() {
|
public void start() {
|
||||||
|
|||||||
Reference in New Issue
Block a user