Authored by wangshusheng

优化

1 package com.yoho.rfid.controller; 1 package com.yoho.rfid.controller;
2 2
  3 +import JW.UHF.JWReader;
3 import com.yoho.rfid.model.RfidClient; 4 import com.yoho.rfid.model.RfidClient;
4 -import com.yoho.rfid.socket.SocketConstant; 5 +import com.yoho.rfid.service.RfidConfig;
  6 +import com.yoho.rfid.service.RfidInit;
  7 +import com.yoho.rfid.util.SocketConstant;
  8 +import com.yoho.rfid.util.ApiResponse;
  9 +import com.yoho.rfid.util.GatewayException;
  10 +import org.apache.commons.lang3.StringUtils;
5 import org.slf4j.Logger; 11 import org.slf4j.Logger;
6 import org.slf4j.LoggerFactory; 12 import org.slf4j.LoggerFactory;
  13 +import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.stereotype.Controller; 14 import org.springframework.stereotype.Controller;
8 import org.springframework.web.bind.annotation.RequestMapping; 15 import org.springframework.web.bind.annotation.RequestMapping;
9 import org.springframework.web.bind.annotation.RequestParam; 16 import org.springframework.web.bind.annotation.RequestParam;
@@ -18,7 +25,10 @@ import java.util.Map; @@ -18,7 +25,10 @@ import java.util.Map;
18 public class RfidController { 25 public class RfidController {
19 26
20 private Logger logger = LoggerFactory.getLogger(RfidController.class); 27 private Logger logger = LoggerFactory.getLogger(RfidController.class);
21 - 28 + @Autowired
  29 + private RfidConfig rfidConfig;
  30 + @Autowired
  31 + private RfidInit rfidInit;
22 /** 32 /**
23 * 查询失效的sku信息 33 * 查询失效的sku信息
24 * @return 34 * @return
@@ -37,7 +47,7 @@ public class RfidController { @@ -37,7 +47,7 @@ public class RfidController {
37 47
38 List<String> skuList = new ArrayList<String>(); 48 List<String> skuList = new ArrayList<String>();
39 for(Map.Entry<String, Long> entry : skuMap.entrySet()){ 49 for(Map.Entry<String, Long> entry : skuMap.entrySet()){
40 - // sku离开时间 50 + // sku离开时间 >1.5s <30min
41 if(System.currentTimeMillis()-entry.getValue() > SocketConstant.SKU_INVALID_TIMEOUT 51 if(System.currentTimeMillis()-entry.getValue() > SocketConstant.SKU_INVALID_TIMEOUT
42 && System.currentTimeMillis()-entry.getValue() < SocketConstant.SKU_NOTBELONG_TIMEOUT){ 52 && System.currentTimeMillis()-entry.getValue() < SocketConstant.SKU_NOTBELONG_TIMEOUT){
43 skuList.add(entry.getKey()); 53 skuList.add(entry.getKey());
@@ -53,18 +63,24 @@ public class RfidController { @@ -53,18 +63,24 @@ public class RfidController {
53 */ 63 */
54 @RequestMapping(params = "method=offline.leave.info") 64 @RequestMapping(params = "method=offline.leave.info")
55 @ResponseBody 65 @ResponseBody
56 - public ApiResponse querySkuInfo(@RequestParam(value="ip", required=true) String ip, 66 + public ApiResponse querySkuInfo(@RequestParam(value="ip", required=false) String ip,
57 @RequestParam(value="mac", required=false) String mac) throws GatewayException { 67 @RequestParam(value="mac", required=false) String mac) throws GatewayException {
58 logger.info("Enter RfidController.queryLeaveSku. ip is {}, mac is {}", ip, mac); 68 logger.info("Enter RfidController.queryLeaveSku. ip is {}, mac is {}", ip, mac);
59 69
  70 + Object result = null;
60 Map<String, Map<String, Long>> allTagsMap = SocketConstant.allTags; 71 Map<String, Map<String, Long>> allTagsMap = SocketConstant.allTags;
61 - Map<String, Long> skuMap = allTagsMap.get(ip); 72 + if(StringUtils.isNotEmpty(ip)){
  73 + result = allTagsMap.get(ip);
  74 + }else{
  75 + result = allTagsMap;
  76 + }
  77 +
62 //组织返回 78 //组织返回
63 - return new ApiResponse.ApiResponseBuilder().code(200).message("query leave info").data(skuMap).build(); 79 + return new ApiResponse.ApiResponseBuilder().code(200).message("query leave info").data(result).build();
64 } 80 }
65 81
66 /** 82 /**
67 - * 查询client信息 83 + * 查询client信息,正在生效、及所有
68 * @return 84 * @return
69 */ 85 */
70 @RequestMapping(params = "method=offline.client.info") 86 @RequestMapping(params = "method=offline.client.info")
@@ -73,12 +89,71 @@ public class RfidController { @@ -73,12 +89,71 @@ public class RfidController {
73 logger.info("Enter RfidController.queryClientInfo"); 89 logger.info("Enter RfidController.queryClientInfo");
74 90
75 Map<String, RfidClient> rfidClientAllMap = SocketConstant.rfidClientAllMap; 91 Map<String, RfidClient> rfidClientAllMap = SocketConstant.rfidClientAllMap;
76 - Map<String, RfidClient> rfidClientValidMap = SocketConstant.rfidClientValidMap;  
77 - Map<String, Map<String, RfidClient>> clientMap = new HashMap<>(); 92 + Map<String, JWReader> rfidClientValidMap = SocketConstant.rfidJWReaderValidMap;
  93 + Map<String, Object> clientMap = new HashMap<>();
78 clientMap.put("allClient", rfidClientAllMap); 94 clientMap.put("allClient", rfidClientAllMap);
79 clientMap.put("validClient", rfidClientValidMap); 95 clientMap.put("validClient", rfidClientValidMap);
80 //组织返回 96 //组织返回
81 return new ApiResponse.ApiResponseBuilder().code(200).message("query client info").data(clientMap).build(); 97 return new ApiResponse.ApiResponseBuilder().code(200).message("query client info").data(clientMap).build();
82 } 98 }
83 99
  100 + /**
  101 + * 新增一个读写器
  102 + * @return
  103 + */
  104 + @RequestMapping(params = "method=offline.client.add")
  105 + @ResponseBody
  106 + public ApiResponse addNewClient(@RequestParam(value="client", required=true) String client) throws GatewayException {
  107 + logger.info("Enter RfidController.addNewClient, client is:{}", client);
  108 +
  109 + try{
  110 + RfidClient rfidClient = new RfidClient();
  111 +
  112 + String clientInfo[] = client.split(":");
  113 + // mac
  114 + rfidClient.setMacId(clientInfo[0]);
  115 + // ip
  116 + rfidClient.setIp(clientInfo[1]);
  117 + // port
  118 + rfidClient.setPort(clientInfo[2]);
  119 + // power
  120 + String power = clientInfo[3];
  121 + rfidConfig.buildPowerInfo(rfidClient, power);
  122 + // speedmode
  123 + rfidClient.setSpeedMode(Integer.valueOf(clientInfo[4]));
  124 +
  125 + SocketConstant.rfidClientAllMap.put(rfidClient.getIp(), rfidClient);
  126 +
  127 + // 连接client,开始监听上报数据
  128 + rfidInit.openRFID(rfidClient);
  129 + }catch(Exception e){
  130 + logger.warn("RfidController.addNewClient faild, e is:{}", e);
  131 + return new ApiResponse.ApiResponseBuilder().code(500).message("add new client failed").data(e).build();
  132 + }
  133 + //组织返回
  134 + return new ApiResponse.ApiResponseBuilder().code(200).message("add new client success").data(null).build();
  135 + }
  136 +
  137 + /**
  138 + * 移除一个读写器
  139 + * @return
  140 + */
  141 + @RequestMapping(params = "method=offline.client.remove")
  142 + @ResponseBody
  143 + public ApiResponse removeClient(@RequestParam(value="ip", required=true) String ip) throws GatewayException {
  144 + logger.info("Enter RfidController.removeClient, ip is:{}", ip);
  145 +
  146 + Map<String, JWReader> rfidJWReaderValidMap = SocketConstant.rfidJWReaderValidMap;
  147 + JWReader reader = rfidJWReaderValidMap.get(ip);
  148 + if(null!=reader){
  149 + reader.RFID_Stop_Inventory();
  150 + reader.RFID_Close();
  151 + SocketConstant.rfidClientAllMap.remove(ip);
  152 + rfidJWReaderValidMap.remove(ip);
  153 + SocketConstant.allTags.remove(ip);
  154 + }
  155 +
  156 + //组织返回
  157 + return new ApiResponse.ApiResponseBuilder().code(200).message("remove client success").data(null).build();
  158 + }
84 } 159 }
1 -package com.yoho.rfid.model;  
2 -  
3 -import JW.UHF.JWReader;  
4 -  
5 -public class YHJWReader extends JWReader {  
6 - private String ip;  
7 -  
8 - public YHJWReader(String _ip, int _port) {  
9 - super(_ip, _port);  
10 - this.ip = _ip;  
11 - }  
12 - public String getIp() {  
13 - return ip;  
14 - }  
15 -  
16 - public void setIp(String ip) {  
17 - this.ip = ip;  
18 - }  
19 -  
20 -}  
1 package com.yoho.rfid.service; 1 package com.yoho.rfid.service;
2 2
3 import com.yoho.rfid.model.RfidClient; 3 import com.yoho.rfid.model.RfidClient;
4 -import com.yoho.rfid.socket.SocketConstant; 4 +import com.yoho.rfid.util.SocketConstant;
5 import org.apache.commons.lang3.ArrayUtils; 5 import org.apache.commons.lang3.ArrayUtils;
6 import org.apache.commons.lang3.StringUtils; 6 import org.apache.commons.lang3.StringUtils;
7 import org.slf4j.Logger; 7 import org.slf4j.Logger;
@@ -11,8 +11,6 @@ import org.springframework.beans.factory.annotation.Value; @@ -11,8 +11,6 @@ import org.springframework.beans.factory.annotation.Value;
11 import org.springframework.stereotype.Component; 11 import org.springframework.stereotype.Component;
12 12
13 import javax.annotation.PostConstruct; 13 import javax.annotation.PostConstruct;
14 -import java.util.concurrent.Callable;  
15 -import java.util.concurrent.FutureTask;  
16 14
17 /** 15 /**
18 * 配置RFID读写器 16 * 配置RFID读写器
@@ -24,12 +22,12 @@ public class RfidConfig { @@ -24,12 +22,12 @@ public class RfidConfig {
24 22
25 @Value("${rfid.client.address:null}") 23 @Value("${rfid.client.address:null}")
26 private String clientAddress[]; 24 private String clientAddress[];
27 - private RfidManager mRfidManager;  
28 @Autowired 25 @Autowired
29 private RfidInit rfidInit; 26 private RfidInit rfidInit;
30 -  
31 @Autowired 27 @Autowired
32 private RfidMonitor rfidMonitor; 28 private RfidMonitor rfidMonitor;
  29 + @Autowired
  30 + private RfidConnMonitor rfidConnMonitor;
33 31
34 @PostConstruct 32 @PostConstruct
35 private void initClient() { 33 private void initClient() {
@@ -59,12 +57,13 @@ public class RfidConfig { @@ -59,12 +57,13 @@ public class RfidConfig {
59 rfidInit.openRFID(rfidClient); 57 rfidInit.openRFID(rfidClient);
60 } 58 }
61 rfidMonitor.startMonitor(); 59 rfidMonitor.startMonitor();
  60 + rfidConnMonitor.startMonitor();
62 }else { 61 }else {
63 logger.info("client is not configured"); 62 logger.info("client is not configured");
64 } 63 }
65 } 64 }
66 65
67 - private void buildPowerInfo(RfidClient rfidClient, String power) { 66 + public void buildPowerInfo(RfidClient rfidClient, String power) {
68 if(StringUtils.isEmpty(power)){ 67 if(StringUtils.isEmpty(power)){
69 return; 68 return;
70 } 69 }
@@ -84,19 +83,4 @@ public class RfidConfig { @@ -84,19 +83,4 @@ public class RfidConfig {
84 rfidClient.setCapacity3(Integer.valueOf(capacity[3])); 83 rfidClient.setCapacity3(Integer.valueOf(capacity[3]));
85 } 84 }
86 85
87 - /*  
88 - private void openRFID(RfidClient rfidClient) {  
89 - if (mRfidManager == null) {  
90 - mRfidManager = RfidManager.getInstance();  
91 - }  
92 - FutureTask<String> task = new FutureTask<String>(new Callable<String>(){  
93 - @Override  
94 - public String call() throws Exception {  
95 - mRfidManager.openRFID(rfidClient, SocketConstant.TYPE_RFID_UNACTIVE);  
96 - return "Collection Completed";  
97 - }  
98 - });  
99 - new Thread(task).start();  
100 - }  
101 - */  
102 } 86 }
  1 +package com.yoho.rfid.service;
  2 +
  3 +import JW.UHF.JWReader;
  4 +import com.yoho.rfid.model.RfidClient;
  5 +import com.yoho.rfid.util.SocketConstant;
  6 +import org.slf4j.Logger;
  7 +import org.slf4j.LoggerFactory;
  8 +import org.springframework.beans.factory.annotation.Autowired;
  9 +import org.springframework.stereotype.Component;
  10 +
  11 +import java.lang.reflect.Field;
  12 +import java.util.Map;
  13 +import java.util.concurrent.ExecutorService;
  14 +import java.util.concurrent.Executors;
  15 +
  16 +/**
  17 + * 监控RFID读写器,没有连接的client,重新连接
  18 + */
  19 +@Component
  20 +public class RfidConnMonitor {
  21 + private static Logger logger = LoggerFactory.getLogger(RfidConnMonitor.class);
  22 + @Autowired
  23 + private RfidInit rfidInit;
  24 + private boolean first = true;
  25 + private static final ExecutorService executorService = Executors.newSingleThreadExecutor();
  26 +
  27 + public void startMonitor() {
  28 + executorService.execute(new Runnable() {
  29 + @Override
  30 + public void run() {
  31 + while (true) {
  32 + try {
  33 + Thread.sleep(1000);
  34 + Map<String, JWReader> rfidJWReaderValidMap = SocketConstant.rfidJWReaderValidMap;
  35 + for(Map.Entry<String, JWReader> entry : rfidJWReaderValidMap.entrySet()){
  36 + if(!entry.getValue().IsConnected){
  37 + entry.getValue().RFID_Stop_Inventory();
  38 + entry.getValue().RFID_Close();
  39 + RfidClient rfidClient = SocketConstant.rfidClientAllMap.get(entry.getKey());
  40 + logger.info("RfidConnMonitor start openRFID, ip is {}", entry.getKey());
  41 + rfidInit.openRFID(rfidClient);
  42 + }
  43 + }
  44 +
  45 + } catch (Exception e) {
  46 + logger.warn("RfidConnMonitor failed. error message is {}", e);
  47 + }
  48 + }
  49 + }
  50 + });
  51 + }
  52 +}
1 package com.yoho.rfid.service; 1 package com.yoho.rfid.service;
2 2
3 import com.yoho.rfid.model.RfidClient; 3 import com.yoho.rfid.model.RfidClient;
4 -import com.yoho.rfid.socket.SocketConstant; 4 +import com.yoho.rfid.util.SocketConstant;
5 import org.slf4j.Logger; 5 import org.slf4j.Logger;
6 import org.slf4j.LoggerFactory; 6 import org.slf4j.LoggerFactory;
7 import org.springframework.stereotype.Component; 7 import org.springframework.stereotype.Component;
@@ -20,7 +20,7 @@ public class RfidInit { @@ -20,7 +20,7 @@ public class RfidInit {
20 private RfidManager mRfidManager; 20 private RfidManager mRfidManager;
21 21
22 public void openRFID(RfidClient rfidClient) { 22 public void openRFID(RfidClient rfidClient) {
23 - logger.info("start openRfid, ip:{}", rfidClient.getIp()); 23 + logger.info("RfidInit start openRfid, ip:{}", rfidClient.getIp());
24 if (mRfidManager == null) { 24 if (mRfidManager == null) {
25 mRfidManager = RfidManager.getInstance(); 25 mRfidManager = RfidManager.getInstance();
26 } 26 }
@@ -2,8 +2,7 @@ package com.yoho.rfid.service; @@ -2,8 +2,7 @@ package com.yoho.rfid.service;
2 2
3 import JW.UHF.*; 3 import JW.UHF.*;
4 import com.yoho.rfid.model.RfidClient; 4 import com.yoho.rfid.model.RfidClient;
5 -import com.yoho.rfid.model.YHJWReader;  
6 -import com.yoho.rfid.socket.SocketConstant; 5 +import com.yoho.rfid.util.SocketConstant;
7 6
8 import java.util.ArrayList; 7 import java.util.ArrayList;
9 import java.util.HashMap; 8 import java.util.HashMap;
@@ -30,14 +29,13 @@ public class RfidManager { @@ -30,14 +29,13 @@ public class RfidManager {
30 public void onTagReported(JWReader jwReader, TagsEventArgs tagsEventArgs) { 29 public void onTagReported(JWReader jwReader, TagsEventArgs tagsEventArgs) {
31 // logger.info("data:{},epc:{},port:{},rssi:{}",tagsEventArgs.tag.DATA, tagsEventArgs.tag.EPC, tagsEventArgs.tag.PORT, tagsEventArgs.tag.RSSI); 30 // logger.info("data:{},epc:{},port:{},rssi:{}",tagsEventArgs.tag.DATA, tagsEventArgs.tag.EPC, tagsEventArgs.tag.PORT, tagsEventArgs.tag.RSSI);
32 count++; 31 count++;
33 - YHJWReader myRreader = (YHJWReader)jwReader;  
34 - if(null==SocketConstant.allTags.get(myRreader.getIp())){  
35 - SocketConstant.allTags.put(myRreader.getIp(), new HashMap<>()); 32 + if(null==SocketConstant.allTags.get(jwReader.getIp())){
  33 + SocketConstant.allTags.put(jwReader.getIp(), new HashMap<>());
36 } 34 }
37 - SocketConstant.allTags.get(myRreader.getIp()).put(tagsEventArgs.tag.EPC, System.currentTimeMillis()); 35 + SocketConstant.allTags.get(jwReader.getIp()).put(tagsEventArgs.tag.EPC, System.currentTimeMillis());
38 // logger.info("current ip is:{}, all tag is:{}", myRreader.getIp(), SocketConstant.allTags.get(myRreader.getIp())); 36 // logger.info("current ip is:{}, all tag is:{}", myRreader.getIp(), SocketConstant.allTags.get(myRreader.getIp()));
39 if(System.currentTimeMillis()-startTime>SocketConstant.STATISTIC_TIME){ 37 if(System.currentTimeMillis()-startTime>SocketConstant.STATISTIC_TIME){
40 - logger.info("current ip is:{}, count is:{}, average is:{}", myRreader.getIp(), count, count*1000/SocketConstant.STATISTIC_TIME); 38 + logger.info("current ip is:{}, count is:{}, average is:{}", jwReader.getIp(), count, count*1000/SocketConstant.STATISTIC_TIME);
41 count = 0; 39 count = 0;
42 startTime = System.currentTimeMillis(); 40 startTime = System.currentTimeMillis();
43 } 41 }
@@ -58,70 +56,40 @@ public class RfidManager { @@ -58,70 +56,40 @@ public class RfidManager {
58 public void openRFID(RfidClient rfidClient, int type) { 56 public void openRFID(RfidClient rfidClient, int type) {
59 JWReader mJwReader = null; 57 JWReader mJwReader = null;
60 try{ 58 try{
61 - mJwReader = new YHJWReader(rfidClient.getIp(), Integer.valueOf(rfidClient.getPort())); 59 + mJwReader = new JWReader(rfidClient.getIp(), Integer.valueOf(rfidClient.getPort()));
62 // 参数为是否开启心跳 60 // 参数为是否开启心跳
63 Result openResult = mJwReader.RFID_Open(true); 61 Result openResult = mJwReader.RFID_Open(true);
64 if (openResult == Result.OK) { 62 if (openResult == Result.OK) {
65 - logger.warn("open RFID success! ip is {}", rfidClient.getIp()); 63 + logger.info("RfidManager open RFID success! ip is {}", rfidClient.getIp());
66 // 配置读写器参数 64 // 配置读写器参数
67 RfidSetting mRfidSetting = ConfigRFID(rfidClient, type); 65 RfidSetting mRfidSetting = ConfigRFID(rfidClient, type);
68 Result configResult = mJwReader.RFID_Set_Config(mRfidSetting); 66 Result configResult = mJwReader.RFID_Set_Config(mRfidSetting);
69 if (configResult == Result.OK) { 67 if (configResult == Result.OK) {
70 - logger.warn("config RFID success! ip is {}", rfidClient.getIp()); 68 + logger.info("RfidManager config RFID success! ip is {}", rfidClient.getIp());
71 // 向client集合中插入一个 69 // 向client集合中插入一个
72 - insertRfidClientMap(rfidClient); 70 + insertRfidClientMap(rfidClient, mJwReader);
73 71
74 mJwReader.addTagListener(mTagListener); 72 mJwReader.addTagListener(mTagListener);
75 mJwReader.RFID_Start_Inventory(); 73 mJwReader.RFID_Start_Inventory();
76 74
77 } else { 75 } else {
78 - logger.warn("config RFID failed! start reopen RFID, ip is {}", rfidClient.getIp());  
79 - // 后处理,以及重新连接  
80 - handlerAndReopen(mJwReader, rfidClient, type); 76 + logger.warn("RfidManager config RFID failed! start reopen RFID, ip is {}", rfidClient.getIp());
81 } 77 }
82 } else { 78 } else {
83 - logger.warn("open RFID failed! start reopen RFID, ip is {}", rfidClient.getIp());  
84 - // 后处理,以及重新连接  
85 - handlerAndReopen(mJwReader, rfidClient, type); 79 + logger.warn("RfidManager open RFID failed! start reopen RFID, ip is {}", rfidClient.getIp());
86 } 80 }
87 }catch(Exception e){ 81 }catch(Exception e){
88 - logger.warn("open RFID exception! ip is {}, e is:{}", rfidClient.getIp(), e);  
89 - logger.warn("start reopen RFID, ip is {}", rfidClient.getIp());  
90 - // 后处理,以及重新连接  
91 - handlerAndReopen(mJwReader, rfidClient, type);  
92 - }  
93 - }  
94 -  
95 - /**  
96 - * 后处理,以及重新连接  
97 - */  
98 - private void handlerAndReopen(JWReader mJwReader, RfidClient rfidClient, int type) {  
99 - // 关闭连接  
100 - if(null!=mJwReader){  
101 - mJwReader.RFID_Close();  
102 - }  
103 - // 从client集合中remove掉  
104 - removeRfidClientMap(rfidClient);  
105 - // 重新连接  
106 - openRFID(rfidClient, type);  
107 - }  
108 -  
109 - /**  
110 - * 从rfid集合中移除一个  
111 - */  
112 - private void removeRfidClientMap(RfidClient rfidClient) {  
113 - if(null!=SocketConstant.rfidClientValidMap.get(rfidClient.getIp())){  
114 - SocketConstant.rfidClientValidMap.remove(rfidClient.getIp()); 82 + logger.warn("RfidManager open RFID exception! ip is {}, e is:{}", rfidClient.getIp(), e);
  83 + logger.warn("RfidManager start reopen RFID, ip is {}", rfidClient.getIp());
115 } 84 }
116 } 85 }
117 86
118 /** 87 /**
119 * 向rfid集合中插入一个 88 * 向rfid集合中插入一个
120 */ 89 */
121 - private void insertRfidClientMap(RfidClient rfidClient) {  
122 - if(null==SocketConstant.rfidClientValidMap.get(rfidClient.getIp())){  
123 - SocketConstant.rfidClientValidMap.put(rfidClient.getIp(), rfidClient);  
124 - } 90 + private void insertRfidClientMap(RfidClient rfidClient, JWReader mJwReader) {
  91 + SocketConstant.rfidJWReaderValidMap.remove(rfidClient.getIp());
  92 + SocketConstant.rfidJWReaderValidMap.put(rfidClient.getIp(), mJwReader);
125 } 93 }
126 94
127 /** 95 /**
@@ -176,12 +144,6 @@ public class RfidManager { @@ -176,12 +144,6 @@ public class RfidManager {
176 return mRfidSetting; 144 return mRfidSetting;
177 } 145 }
178 146
179 - /**  
180 - * 停止盘点  
181 - */  
182 - public void stopInventory() {  
183 -// mJwReader.RFID_Stop_Inventory();  
184 - }  
185 147
186 private void clearData() { 148 private void clearData() {
187 SocketConstant.allTags.clear(); 149 SocketConstant.allTags.clear();
1 package com.yoho.rfid.service; 1 package com.yoho.rfid.service;
2 2
  3 +import JW.UHF.JWReader;
3 import com.yoho.rfid.model.RfidClient; 4 import com.yoho.rfid.model.RfidClient;
4 -import com.yoho.rfid.socket.SocketConstant; 5 +import com.yoho.rfid.util.SocketConstant;
5 import org.slf4j.Logger; 6 import org.slf4j.Logger;
6 import org.slf4j.LoggerFactory; 7 import org.slf4j.LoggerFactory;
7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.beans.factory.annotation.Autowired;
@@ -32,7 +33,7 @@ public class RfidMonitor { @@ -32,7 +33,7 @@ public class RfidMonitor {
32 try { 33 try {
33 Thread.sleep(10000);// 暂停10s 34 Thread.sleep(10000);// 暂停10s
34 Map<String, RfidClient> rfidClientAllMap = SocketConstant.rfidClientAllMap; 35 Map<String, RfidClient> rfidClientAllMap = SocketConstant.rfidClientAllMap;
35 - Map<String, RfidClient> rfidClientValidMap = SocketConstant.rfidClientValidMap; 36 + Map<String, JWReader> rfidClientValidMap = SocketConstant.rfidJWReaderValidMap;
36 37
37 Set<String> allClientIp = rfidClientAllMap.keySet(); 38 Set<String> allClientIp = rfidClientAllMap.keySet();
38 Set<String> validClientIp = rfidClientValidMap.keySet(); 39 Set<String> validClientIp = rfidClientValidMap.keySet();
@@ -44,10 +45,11 @@ public class RfidMonitor { @@ -44,10 +45,11 @@ public class RfidMonitor {
44 allClientIp.removeAll(validClientIp); 45 allClientIp.removeAll(validClientIp);
45 for(String ip : allClientIp){ 46 for(String ip : allClientIp){
46 RfidClient rfidClient = rfidClientAllMap.get(ip); 47 RfidClient rfidClient = rfidClientAllMap.get(ip);
  48 + logger.info("RfidMonitor start openRFID, ip is {}", ip);
47 rfidInit.openRFID(rfidClient); 49 rfidInit.openRFID(rfidClient);
48 } 50 }
49 } catch (Exception e) { 51 } catch (Exception e) {
50 - logger.warn("browser send MQ error. error message is {}", e.getMessage()); 52 + logger.warn("RfidMonitor failed. error message is {}", e);
51 } 53 }
52 } 54 }
53 } 55 }
  1 +package com.yoho.rfid.service;
  2 +
  3 +import com.yoho.rfid.model.RfidClient;
  4 +import com.yoho.rfid.util.SocketConstant;
  5 +import org.slf4j.Logger;
  6 +import org.slf4j.LoggerFactory;
  7 +import org.springframework.beans.factory.annotation.Autowired;
  8 +import org.springframework.stereotype.Component;
  9 +
  10 +import java.util.Iterator;
  11 +import java.util.Map;
  12 +import java.util.Set;
  13 +import java.util.concurrent.ExecutorService;
  14 +import java.util.concurrent.Executors;
  15 +
  16 +/**
  17 + * RFID上报的数据处理
  18 + * 1、把指定时间30分钟没有上报的sku删掉
  19 + */
  20 +@Component
  21 +public class RfidResultHandler {
  22 + private static Logger logger = LoggerFactory.getLogger(RfidResultHandler.class);
  23 +
  24 + private static final ExecutorService executorService = Executors.newSingleThreadExecutor();
  25 +
  26 + static {
  27 + executorService.execute(new Runnable() {
  28 + @Override
  29 + public void run() {
  30 + while (true) {
  31 + try {
  32 + Thread.sleep(10000);// 暂停60s
  33 + Map<String, Map<String, Long>> allTagsMap = SocketConstant.allTags;
  34 + Iterator<Map.Entry<String, Map<String, Long>>> allIt = allTagsMap.entrySet().iterator();
  35 + while(allIt.hasNext()){
  36 + Map.Entry<String, Map<String, Long>> tagMap = allIt.next();
  37 + Map<String, Long> skuMap = tagMap.getValue();
  38 + Iterator<Map.Entry<String, Long>> it = skuMap.entrySet().iterator();
  39 + while(it.hasNext()){
  40 + Map.Entry<String, Long> skuEntry=it.next();
  41 + if(System.currentTimeMillis()-skuEntry.getValue() > SocketConstant.SKU_NOTBELONG_TIMEOUT){
  42 + it.remove();
  43 + }
  44 + }
  45 + }
  46 + } catch (Exception e) {
  47 + logger.warn("RfidResultHandler failed. error message is {}", e);
  48 + }
  49 + }
  50 + }
  51 + });
  52 + }
  53 +
  54 +}
1 -package com.yoho.rfid.controller; 1 +package com.yoho.rfid.util;
2 2
3 import com.alibaba.fastjson.JSON; 3 import com.alibaba.fastjson.JSON;
4 import com.yoho.core.common.utils.MD5; 4 import com.yoho.core.common.utils.MD5;
1 -package com.yoho.rfid.controller; 1 +package com.yoho.rfid.util;
2 2
3 import com.yoho.error.GatewayError; 3 import com.yoho.error.GatewayError;
4 4
1 -package com.yoho.rfid.socket; 1 +package com.yoho.rfid.util;
2 2
3 import java.util.ArrayList; 3 import java.util.ArrayList;
4 import java.util.HashMap; 4 import java.util.HashMap;
@@ -8,6 +8,7 @@ import java.util.concurrent.BlockingQueue; @@ -8,6 +8,7 @@ import java.util.concurrent.BlockingQueue;
8 import java.util.concurrent.ConcurrentHashMap; 8 import java.util.concurrent.ConcurrentHashMap;
9 import java.util.concurrent.LinkedBlockingQueue; 9 import java.util.concurrent.LinkedBlockingQueue;
10 10
  11 +import JW.UHF.JWReader;
11 import JW.UHF.Tag; 12 import JW.UHF.Tag;
12 import com.yoho.rfid.model.RfidClient; 13 import com.yoho.rfid.model.RfidClient;
13 14
@@ -34,7 +35,7 @@ public class SocketConstant { @@ -34,7 +35,7 @@ public class SocketConstant {
34 /** 35 /**
35 * 正在生效的读写器列表 36 * 正在生效的读写器列表
36 */ 37 */
37 - public static Map<String, RfidClient> rfidClientValidMap = new ConcurrentHashMap<>(); 38 + public static Map<String, JWReader> rfidJWReaderValidMap = new ConcurrentHashMap<>();
38 39
39 /** 40 /**
40 * mac和ip的对应关系 41 * mac和ip的对应关系
@@ -22,7 +22,6 @@ @@ -22,7 +22,6 @@
22 </content> 22 </content>
23 <orderEntry type="inheritedJdk" /> 23 <orderEntry type="inheritedJdk" />
24 <orderEntry type="sourceFolder" forTests="false" /> 24 <orderEntry type="sourceFolder" forTests="false" />
25 - <orderEntry type="library" name="Maven: com.yoho.rfid:yoho-rfid-common:1.0.0-SNAPSHOT" level="project" />  
26 <orderEntry type="module" module-name="service" /> 25 <orderEntry type="module" module-name="service" />
27 <orderEntry type="library" name="Maven: org.springframework:spring-webmvc:4.2.2.RELEASE" level="project" /> 26 <orderEntry type="library" name="Maven: org.springframework:spring-webmvc:4.2.2.RELEASE" level="project" />
28 <orderEntry type="library" name="Maven: org.springframework:spring-beans:4.2.2.RELEASE" level="project" /> 27 <orderEntry type="library" name="Maven: org.springframework:spring-beans:4.2.2.RELEASE" level="project" />