上海郢昱
21 小时以前 7392f08349f8df8c19422f164838d229e504cb06
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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));
    }
 
}