#
zhou zhou
1 天以前 fca6edde8c69a755f1e06dd392ed224bd95a426c
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package com.zy.controller;
 
import com.core.common.R;
import com.zy.service.RFIDService;
import com.zy.service.RFIDAutoConnectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import java.util.List;
import java.util.Map;
 
/**
 * RFID设备HTTP API接口
 * 提供RFID设备连接、检测等功能
 * Created on 2026/01/10
 */
@Slf4j
@RestController
@RequestMapping("/api/rfid")
public class RFIDController {
 
    @Autowired
    private RFIDService rfidService;
    
    @Autowired
    private RFIDAutoConnectService rfidAutoConnectService;
 
    /**
     * 网络连接RFID设备
     * 
     * POST /api/rfid/connect/network
     * Body: {"ip": "10.10.10.210", "port": 27011}
     * 
     * @param params 连接参数
     * @return 连接结果
     */
    @PostMapping("/connect/network")
    public R connectNetwork(@RequestBody Map<String, Object> params) {
        String ip = (String) params.get("ip");
        Integer port = params.get("port") != null ? 
                Integer.valueOf(params.get("port").toString()) : null;
        
        if (ip == null || ip.isEmpty()) {
            return R.error("IP地址不能为空");
        }
        
        Map<String, Object> result = rfidService.connectNetwork(ip, port);
        return result.get("success").equals(true) ? R.ok(result) : R.error(result.get("message").toString());
    }
 
    /**
     * 串口连接RFID设备
     * 
     * POST /api/rfid/connect/comport
     * Body: {"port": 3, "baud": 3}
     * 
     * @param params 连接参数
     * @return 连接结果
     */
    @PostMapping("/connect/comport")
    public R connectComPort(@RequestBody Map<String, Object> params) {
        Integer port = params.get("port") != null ? 
                Integer.valueOf(params.get("port").toString()) : null;
        Integer baud = params.get("baud") != null ? 
                Integer.valueOf(params.get("baud").toString()) : null;
        
        Map<String, Object> result = rfidService.connectComPort(port, baud);
        return result.get("success").equals(true) ? R.ok(result) : R.error(result.get("message").toString());
    }
 
    /**
     * 检测RFID标签(单次检测)
     * 
     * GET /api/rfid/detect
     * GET /api/rfid/detect?scanTime=5
     * 
     * @param scanTime 扫描时间(单位: 100ms,默认5)
     * @return 检测结果
     */
    @GetMapping("/detect")
    public R detectTags(@RequestParam(required = false) Integer scanTime) {
        Map<String, Object> result = rfidService.detectTags(scanTime);
        return R.ok(result);
    }
 
    /**
     * 检测RFID标签(POST方式,可传递更多参数)
     * 
     * POST /api/rfid/detect
     * Body: {"scanTime": 5}
     * 
     * @param params 检测参数
     * @return 检测结果
     */
    @PostMapping("/detect")
    public R detectTagsPost(@RequestBody(required = false) Map<String, Object> params) {
        Integer scanTime = null;
        if (params != null && params.get("scanTime") != null) {
            scanTime = Integer.valueOf(params.get("scanTime").toString());
        }
        
        Map<String, Object> result = rfidService.detectTags(scanTime);
        return R.ok(result);
    }
 
    /**
     * 断开RFID设备连接
     * 
     * POST /api/rfid/disconnect
     * 
     * @return 断开结果
     */
    @PostMapping("/disconnect")
    public R disconnect() {
        Map<String, Object> result = rfidService.disconnect();
        return result.get("success").equals(true) ? R.ok(result) : R.error(result.get("message").toString());
    }
 
    /**
     * 获取连接状态
     * 
     * GET /api/rfid/status
     * 
     * @return 连接状态
     */
    @GetMapping("/status")
    public R getStatus() {
        Map<String, Object> result = rfidService.getConnectionStatus();
        return R.ok(result);
    }
 
    /**
     * 诊断DLL加载状态
     * 
     * GET /api/rfid/diagnose
     * 
     * @return 诊断信息
     */
    @GetMapping("/diagnose")
    public R diagnose() {
        Map<String, Object> result = rfidService.diagnoseDll();
        return R.ok(result);
    }
    
    /**
     * 获取所有RFID自动连接设备状态
     * 
     * GET /api/rfid/auto-connect/status
     * 
     * @return 所有设备状态
     */
    @GetMapping("/auto-connect/status")
    public R getAutoConnectStatus() {
        return R.ok(rfidAutoConnectService.getAllDeviceStatus());
    }
    
    /**
     * 获取指定RFID自动连接设备状态
     * 
     * GET /api/rfid/auto-connect/status/{deviceId}
     * 
     * @param deviceId 设备ID
     * @return 设备状态
     */
    @GetMapping("/auto-connect/status/{deviceId}")
    public R getAutoConnectDeviceStatus(@PathVariable Integer deviceId) {
        Map<String, Object> result = rfidAutoConnectService.getDeviceStatus(deviceId);
        return R.ok(result);
    }
    
    /**
     * 手动启动指定RFID设备自动连接
     * 
     * POST /api/rfid/auto-connect/start/{deviceId}
     * 
     * @param deviceId 设备ID
     * @return 启动结果
     */
    @PostMapping("/auto-connect/start/{deviceId}")
    public R startAutoConnect(@PathVariable Integer deviceId) {
        boolean success = rfidAutoConnectService.startDevice(deviceId);
        if (success) {
            return R.ok("RFID设备[" + deviceId + "]自动连接已启动");
        } else {
            return R.error("RFID设备[" + deviceId + "]启动失败,请检查配置或设备是否已在运行");
        }
    }
    
    /**
     * 手动停止指定RFID设备自动连接
     * 
     * POST /api/rfid/auto-connect/stop/{deviceId}
     * 
     * @param deviceId 设备ID
     * @return 停止结果
     */
    @PostMapping("/auto-connect/stop/{deviceId}")
    public R stopAutoConnect(@PathVariable Integer deviceId) {
        boolean success = rfidAutoConnectService.stopDevice(deviceId);
        if (success) {
            return R.ok("RFID设备[" + deviceId + "]自动连接已停止");
        } else {
            return R.error("RFID设备[" + deviceId + "]停止失败,设备可能未启用自动连接");
        }
    }
    
    /**
     * 手动停止所有RFID设备自动连接
     * 
     * POST /api/rfid/auto-connect/stop-all
     * 
     * @return 停止结果
     */
    @PostMapping("/auto-connect/stop-all")
    public R stopAllAutoConnect() {
        rfidAutoConnectService.stopAll();
        return R.ok("所有RFID设备自动连接已停止");
    }
    
    /**
     * 重新配置指定RFID设备的天线(用于天线插拔后重新配置)
     * 
     * POST /api/rfid/reconfigure-antenna/{deviceId}
     * 
     * @param deviceId 设备ID
     * @return 配置结果
     */
    @PostMapping("/reconfigure-antenna/{deviceId}")
    public R reconfigureAntenna(@PathVariable Integer deviceId) {
        Map<String, Object> deviceStatus = rfidAutoConnectService.getDeviceStatus(deviceId);
        if (!Boolean.TRUE.equals(deviceStatus.get("exists"))) {
            return R.error("RFID设备[" + deviceId + "]未启用自动连接或不存在");
        }
        
        if (!Boolean.TRUE.equals(deviceStatus.get("templateConnected"))) {
            return R.error("RFID设备[" + deviceId + "]未连接,无法配置天线");
        }
        
        // 获取设备管理器并重新配置天线
        boolean success = rfidAutoConnectService.reconfigureAntenna(deviceId);
        if (success) {
            return R.ok("RFID设备[" + deviceId + "]天线重新配置成功");
        } else {
            return R.error("RFID设备[" + deviceId + "]天线重新配置失败,请查看日志");
        }
    }
 
    @GetMapping("/tags")
    public R getTags(@RequestParam(required = false) Integer deviceId,
                     @RequestParam(required = false) String ip,
                     @RequestParam(required = false) Integer antenna) {
        if (deviceId == null && (ip == null || ip.isEmpty())) {
            return R.error("请提供设备ID或IP地址");
        }
        List<Map<String, Object>> tags = rfidAutoConnectService.getDeviceTags(deviceId, ip, antenna);
        return R.ok(tags);
    }
 
    @GetMapping("/query-tags")
    public R queryTags(@RequestParam(required = false) Integer deviceId,
                       @RequestParam(required = false) String ip,
                       @RequestParam(required = false) Integer antenna) {
        if (deviceId == null && (ip == null || ip.isEmpty())) {
            return R.error("请提供设备ID或IP地址");
        }
        List<Map<String, Object>> tags = rfidAutoConnectService.queryDeviceTags(deviceId, ip, antenna);
        return R.ok(tags);
    }
}