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);
|
}
|
}
|