package com.hwtd.mes.collect.controller;
|
|
import com.hwtd.mes.collect.dto.DatabaseDTO;
|
import com.hwtd.mes.collect.dto.SerialPortDTO;
|
import com.hwtd.mes.collect.handler.SerialPortListener;
|
import com.hwtd.mes.collect.service.DataCollectionService;
|
import com.hwtd.mes.collect.util.Result;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RestController;
|
|
import java.util.LinkedHashMap;
|
import java.util.Map;
|
|
@RestController
|
@RequestMapping("/collection")
|
public class DataCollectionController {
|
|
@Autowired
|
private DataCollectionService dataCollectionService;
|
|
@Autowired
|
private SerialPortListener serialPortListener;
|
|
/**
|
* 手动开启串口监听,并返回当前接收到的数据列表(队列式缓存,最多 maxCount 条,没有数据时返回空集合)。
|
* <p>
|
* 串口参数通过请求参数传入(不传的字段使用默认值,listenName 必传):
|
* listenName、baudRate、dataBits、stopBits、parity、flowControl、readTimeout、endMark、maxCount。
|
* 幂等接口:参数一致时直接复用已打开的串口、不会重复打开,参数变化时按新参数重新打开;
|
* 多线程并发调用同样只会打开一次。参数非法时由 GlobalExceptionHandler 返回具体原因。
|
*/
|
@GetMapping("/openSerialPort")
|
public Result<?> openSerialPort(SerialPortDTO serialPortDTO) {
|
if (!serialPortListener.startListening(serialPortDTO)) {
|
String listenName = serialPortDTO == null || serialPortDTO.getSerialPortName() == null
|
? "" : serialPortDTO.getSerialPortName();
|
return Result.failed("串口 " + listenName + " 监听开启失败,请检查串口是否存在、是否被其他程序占用!");
|
}
|
return Result.ok(serialPortListener.drainData());
|
}
|
|
/**
|
* 手动关闭串口监听。
|
* 幂等接口:未开启时调用也会正常返回,不会报错。
|
*/
|
@GetMapping("/closeSerialPort")
|
public Result<?> closeSerialPort() {
|
if (serialPortListener.closeListening()) {
|
return Result.ok("串口 " + serialPortListener.getListenName() + " 监听已关闭");
|
}
|
return Result.failed("串口 " + serialPortListener.getListenName() + " 监听关闭失败,请检查串口状态!");
|
}
|
|
/**
|
* 查询串口监听状态,便于前端判断是否需要调用开启 / 关闭接口
|
*/
|
@GetMapping("/serialPortStatus")
|
public Result<?> serialPortStatus() {
|
Map<String, Object> status = new LinkedHashMap<>();
|
status.put("listenName", serialPortListener.getListenName());
|
status.put("listening", serialPortListener.isListening());
|
status.put("portOpen", serialPortListener.isPortOpen());
|
status.put("dataSize", serialPortListener.dataSize());
|
return Result.ok(status);
|
}
|
|
@GetMapping("/getAccessData")
|
public Result<?> getAccessData(DatabaseDTO databaseDTO) {
|
return Result.ok(dataCollectionService.getAccessData(databaseDTO));
|
}
|
|
@GetMapping("/getPostgreSqlData")
|
public Result<?> getPostgreSqlData(DatabaseDTO databaseDTO) {
|
return Result.ok(dataCollectionService.getPostgreSqlData(databaseDTO));
|
}
|
|
}
|