RfidController.java
6.16 KB
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
package com.yoho.rfid.controller;
import JW.UHF.JWReader;
import com.yoho.rfid.model.RfidClient;
import com.yoho.rfid.service.RfidConfig;
import com.yoho.rfid.service.RfidInit;
import com.yoho.rfid.util.SocketConstant;
import com.yoho.rfid.util.ApiResponse;
import com.yoho.rfid.util.GatewayException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* RFID读写器接口
*/
@Controller
public class RfidController {
private Logger logger = LoggerFactory.getLogger(RfidController.class);
@Autowired
private RfidConfig rfidConfig;
@Autowired
private RfidInit rfidInit;
/**
* 查询失效的sku信息
* @return
*/
@RequestMapping(params = "method=offline.leave.sku")
@ResponseBody
public ApiResponse queryLeaveSku(@RequestParam(value="ip", required=true) String ip,
@RequestParam(value="mac", required=false) String mac) throws GatewayException {
Map<String, Map<String, Long>> allTagsMap = SocketConstant.allTags;
Map<String, Long> skuMap = allTagsMap.get(ip);
if(null==skuMap){
return new ApiResponse.ApiResponseBuilder().code(200).message("query leave sku").data(null).build();
}
List<String> skuList = new ArrayList<String>();
for(Map.Entry<String, Long> entry : skuMap.entrySet()){
// sku离开时间 >1.5s <30min
if(System.currentTimeMillis()-entry.getValue() > SocketConstant.SKU_INVALID_TIMEOUT
&& System.currentTimeMillis()-entry.getValue() < SocketConstant.SKU_NOTBELONG_TIMEOUT){
skuList.add(entry.getKey());
}
}
logger.info("out RfidController.queryLeaveSku. ip is: {}, skuList is: {}, currentTime is:{}", ip, skuList, System.currentTimeMillis());
//组织返回
return new ApiResponse.ApiResponseBuilder().code(200).message("query leave sku").data(skuList).build();
}
/**
* 查询失效的sku信息
* @return
*/
@RequestMapping(params = "method=offline.leave.info")
@ResponseBody
public ApiResponse querySkuInfo(@RequestParam(value="ip", required=false) String ip,
@RequestParam(value="mac", required=false) String mac) throws GatewayException {
logger.info("Enter RfidController.queryLeaveSku. ip is {}, mac is {}", ip, mac);
Object result = null;
Map<String, Map<String, Long>> allTagsMap = SocketConstant.allTags;
if(StringUtils.isNotEmpty(ip)){
result = allTagsMap.get(ip);
}else{
result = allTagsMap;
}
//组织返回
return new ApiResponse.ApiResponseBuilder().code(200).message("query leave info").data(result).build();
}
/**
* 查询client信息,正在生效、及所有
* @return
*/
@RequestMapping(params = "method=offline.client.info")
@ResponseBody
public ApiResponse queryClientInfo() throws GatewayException {
logger.info("Enter RfidController.queryClientInfo");
Map<String, RfidClient> rfidClientAllMap = SocketConstant.rfidClientAllMap;
Map<String, JWReader> rfidClientValidMap = SocketConstant.rfidJWReaderValidMap;
Map<String, Object> clientMap = new HashMap<>();
clientMap.put("allClient", rfidClientAllMap);
clientMap.put("validClient", rfidClientValidMap);
//组织返回
return new ApiResponse.ApiResponseBuilder().code(200).message("query client info").data(clientMap).build();
}
/**
* 新增一个读写器
* @return
*/
@RequestMapping(params = "method=offline.client.add")
@ResponseBody
public ApiResponse addNewClient(@RequestParam(value="client", required=true) String client) throws GatewayException {
logger.info("Enter RfidController.addNewClient, client is:{}", client);
try{
RfidClient rfidClient = new RfidClient();
String clientInfo[] = client.split(":");
// mac
rfidClient.setMacId(clientInfo[0]);
// ip
rfidClient.setIp(clientInfo[1]);
// port
rfidClient.setPort(clientInfo[2]);
// power
String power = clientInfo[3];
rfidConfig.buildPowerInfo(rfidClient, power);
// speedmode
rfidClient.setSpeedMode(Integer.valueOf(clientInfo[4]));
SocketConstant.rfidClientAllMap.put(rfidClient.getIp(), rfidClient);
// 连接client,开始监听上报数据
rfidInit.openRFID(rfidClient);
}catch(Exception e){
logger.warn("RfidController.addNewClient faild, e is:{}", e);
return new ApiResponse.ApiResponseBuilder().code(500).message("add new client failed").data(e).build();
}
//组织返回
return new ApiResponse.ApiResponseBuilder().code(200).message("add new client success").data(null).build();
}
/**
* 移除一个读写器
* @return
*/
@RequestMapping(params = "method=offline.client.remove")
@ResponseBody
public ApiResponse removeClient(@RequestParam(value="ip", required=true) String ip) throws GatewayException {
logger.info("Enter RfidController.removeClient, ip is:{}", ip);
Map<String, JWReader> rfidJWReaderValidMap = SocketConstant.rfidJWReaderValidMap;
JWReader reader = rfidJWReaderValidMap.get(ip);
if(null!=reader){
reader.RFID_Stop_Inventory();
reader.RFID_Close();
SocketConstant.rfidClientAllMap.remove(ip);
rfidJWReaderValidMap.remove(ip);
SocketConstant.allTags.remove(ip);
}
//组织返回
return new ApiResponse.ApiResponseBuilder().code(200).message("remove client success").data(null).build();
}
}