Authored by wangshusheng

优化

package com.yoho.rfid.controller;
import JW.UHF.JWReader;
import com.yoho.rfid.model.RfidClient;
import com.yoho.rfid.socket.SocketConstant;
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;
... ... @@ -18,7 +25,10 @@ import java.util.Map;
public class RfidController {
private Logger logger = LoggerFactory.getLogger(RfidController.class);
@Autowired
private RfidConfig rfidConfig;
@Autowired
private RfidInit rfidInit;
/**
* 查询失效的sku信息
* @return
... ... @@ -37,7 +47,7 @@ public class RfidController {
List<String> skuList = new ArrayList<String>();
for(Map.Entry<String, Long> entry : skuMap.entrySet()){
// sku离开时间
// 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());
... ... @@ -53,18 +63,24 @@ public class RfidController {
*/
@RequestMapping(params = "method=offline.leave.info")
@ResponseBody
public ApiResponse querySkuInfo(@RequestParam(value="ip", required=true) String ip,
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;
Map<String, Long> skuMap = allTagsMap.get(ip);
if(StringUtils.isNotEmpty(ip)){
result = allTagsMap.get(ip);
}else{
result = allTagsMap;
}
//组织返回
return new ApiResponse.ApiResponseBuilder().code(200).message("query leave info").data(skuMap).build();
return new ApiResponse.ApiResponseBuilder().code(200).message("query leave info").data(result).build();
}
/**
* 查询client信息
* 查询client信息,正在生效、及所有
* @return
*/
@RequestMapping(params = "method=offline.client.info")
... ... @@ -73,12 +89,71 @@ public class RfidController {
logger.info("Enter RfidController.queryClientInfo");
Map<String, RfidClient> rfidClientAllMap = SocketConstant.rfidClientAllMap;
Map<String, RfidClient> rfidClientValidMap = SocketConstant.rfidClientValidMap;
Map<String, Map<String, RfidClient>> clientMap = new HashMap<>();
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();
}
}
... ...
package com.yoho.rfid.model;
import JW.UHF.JWReader;
public class YHJWReader extends JWReader {
private String ip;
public YHJWReader(String _ip, int _port) {
super(_ip, _port);
this.ip = _ip;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
}
package com.yoho.rfid.service;
import com.yoho.rfid.model.RfidClient;
import com.yoho.rfid.socket.SocketConstant;
import com.yoho.rfid.util.SocketConstant;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
... ... @@ -11,8 +11,6 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
/**
* 配置RFID读写器
... ... @@ -24,12 +22,12 @@ public class RfidConfig {
@Value("${rfid.client.address:null}")
private String clientAddress[];
private RfidManager mRfidManager;
@Autowired
private RfidInit rfidInit;
@Autowired
private RfidMonitor rfidMonitor;
@Autowired
private RfidConnMonitor rfidConnMonitor;
@PostConstruct
private void initClient() {
... ... @@ -59,12 +57,13 @@ public class RfidConfig {
rfidInit.openRFID(rfidClient);
}
rfidMonitor.startMonitor();
rfidConnMonitor.startMonitor();
}else {
logger.info("client is not configured");
}
}
private void buildPowerInfo(RfidClient rfidClient, String power) {
public void buildPowerInfo(RfidClient rfidClient, String power) {
if(StringUtils.isEmpty(power)){
return;
}
... ... @@ -84,19 +83,4 @@ public class RfidConfig {
rfidClient.setCapacity3(Integer.valueOf(capacity[3]));
}
/*
private void openRFID(RfidClient rfidClient) {
if (mRfidManager == null) {
mRfidManager = RfidManager.getInstance();
}
FutureTask<String> task = new FutureTask<String>(new Callable<String>(){
@Override
public String call() throws Exception {
mRfidManager.openRFID(rfidClient, SocketConstant.TYPE_RFID_UNACTIVE);
return "Collection Completed";
}
});
new Thread(task).start();
}
*/
}
... ...
package com.yoho.rfid.service;
import JW.UHF.JWReader;
import com.yoho.rfid.model.RfidClient;
import com.yoho.rfid.util.SocketConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* 监控RFID读写器,没有连接的client,重新连接
*/
@Component
public class RfidConnMonitor {
private static Logger logger = LoggerFactory.getLogger(RfidConnMonitor.class);
@Autowired
private RfidInit rfidInit;
private boolean first = true;
private static final ExecutorService executorService = Executors.newSingleThreadExecutor();
public void startMonitor() {
executorService.execute(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
Map<String, JWReader> rfidJWReaderValidMap = SocketConstant.rfidJWReaderValidMap;
for(Map.Entry<String, JWReader> entry : rfidJWReaderValidMap.entrySet()){
if(!entry.getValue().IsConnected){
entry.getValue().RFID_Stop_Inventory();
entry.getValue().RFID_Close();
RfidClient rfidClient = SocketConstant.rfidClientAllMap.get(entry.getKey());
logger.info("RfidConnMonitor start openRFID, ip is {}", entry.getKey());
rfidInit.openRFID(rfidClient);
}
}
} catch (Exception e) {
logger.warn("RfidConnMonitor failed. error message is {}", e);
}
}
}
});
}
}
... ...
package com.yoho.rfid.service;
import com.yoho.rfid.model.RfidClient;
import com.yoho.rfid.socket.SocketConstant;
import com.yoho.rfid.util.SocketConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
... ... @@ -20,7 +20,7 @@ public class RfidInit {
private RfidManager mRfidManager;
public void openRFID(RfidClient rfidClient) {
logger.info("start openRfid, ip:{}", rfidClient.getIp());
logger.info("RfidInit start openRfid, ip:{}", rfidClient.getIp());
if (mRfidManager == null) {
mRfidManager = RfidManager.getInstance();
}
... ...
... ... @@ -2,8 +2,7 @@ package com.yoho.rfid.service;
import JW.UHF.*;
import com.yoho.rfid.model.RfidClient;
import com.yoho.rfid.model.YHJWReader;
import com.yoho.rfid.socket.SocketConstant;
import com.yoho.rfid.util.SocketConstant;
import java.util.ArrayList;
import java.util.HashMap;
... ... @@ -30,14 +29,13 @@ public class RfidManager {
public void onTagReported(JWReader jwReader, TagsEventArgs tagsEventArgs) {
// logger.info("data:{},epc:{},port:{},rssi:{}",tagsEventArgs.tag.DATA, tagsEventArgs.tag.EPC, tagsEventArgs.tag.PORT, tagsEventArgs.tag.RSSI);
count++;
YHJWReader myRreader = (YHJWReader)jwReader;
if(null==SocketConstant.allTags.get(myRreader.getIp())){
SocketConstant.allTags.put(myRreader.getIp(), new HashMap<>());
if(null==SocketConstant.allTags.get(jwReader.getIp())){
SocketConstant.allTags.put(jwReader.getIp(), new HashMap<>());
}
SocketConstant.allTags.get(myRreader.getIp()).put(tagsEventArgs.tag.EPC, System.currentTimeMillis());
SocketConstant.allTags.get(jwReader.getIp()).put(tagsEventArgs.tag.EPC, System.currentTimeMillis());
// logger.info("current ip is:{}, all tag is:{}", myRreader.getIp(), SocketConstant.allTags.get(myRreader.getIp()));
if(System.currentTimeMillis()-startTime>SocketConstant.STATISTIC_TIME){
logger.info("current ip is:{}, count is:{}, average is:{}", myRreader.getIp(), count, count*1000/SocketConstant.STATISTIC_TIME);
logger.info("current ip is:{}, count is:{}, average is:{}", jwReader.getIp(), count, count*1000/SocketConstant.STATISTIC_TIME);
count = 0;
startTime = System.currentTimeMillis();
}
... ... @@ -58,70 +56,40 @@ public class RfidManager {
public void openRFID(RfidClient rfidClient, int type) {
JWReader mJwReader = null;
try{
mJwReader = new YHJWReader(rfidClient.getIp(), Integer.valueOf(rfidClient.getPort()));
mJwReader = new JWReader(rfidClient.getIp(), Integer.valueOf(rfidClient.getPort()));
// 参数为是否开启心跳
Result openResult = mJwReader.RFID_Open(true);
if (openResult == Result.OK) {
logger.warn("open RFID success! ip is {}", rfidClient.getIp());
logger.info("RfidManager open RFID success! ip is {}", rfidClient.getIp());
// 配置读写器参数
RfidSetting mRfidSetting = ConfigRFID(rfidClient, type);
Result configResult = mJwReader.RFID_Set_Config(mRfidSetting);
if (configResult == Result.OK) {
logger.warn("config RFID success! ip is {}", rfidClient.getIp());
logger.info("RfidManager config RFID success! ip is {}", rfidClient.getIp());
// 向client集合中插入一个
insertRfidClientMap(rfidClient);
insertRfidClientMap(rfidClient, mJwReader);
mJwReader.addTagListener(mTagListener);
mJwReader.RFID_Start_Inventory();
} else {
logger.warn("config RFID failed! start reopen RFID, ip is {}", rfidClient.getIp());
// 后处理,以及重新连接
handlerAndReopen(mJwReader, rfidClient, type);
logger.warn("RfidManager config RFID failed! start reopen RFID, ip is {}", rfidClient.getIp());
}
} else {
logger.warn("open RFID failed! start reopen RFID, ip is {}", rfidClient.getIp());
// 后处理,以及重新连接
handlerAndReopen(mJwReader, rfidClient, type);
logger.warn("RfidManager open RFID failed! start reopen RFID, ip is {}", rfidClient.getIp());
}
}catch(Exception e){
logger.warn("open RFID exception! ip is {}, e is:{}", rfidClient.getIp(), e);
logger.warn("start reopen RFID, ip is {}", rfidClient.getIp());
// 后处理,以及重新连接
handlerAndReopen(mJwReader, rfidClient, type);
}
}
/**
* 后处理,以及重新连接
*/
private void handlerAndReopen(JWReader mJwReader, RfidClient rfidClient, int type) {
// 关闭连接
if(null!=mJwReader){
mJwReader.RFID_Close();
}
// 从client集合中remove掉
removeRfidClientMap(rfidClient);
// 重新连接
openRFID(rfidClient, type);
}
/**
* 从rfid集合中移除一个
*/
private void removeRfidClientMap(RfidClient rfidClient) {
if(null!=SocketConstant.rfidClientValidMap.get(rfidClient.getIp())){
SocketConstant.rfidClientValidMap.remove(rfidClient.getIp());
logger.warn("RfidManager open RFID exception! ip is {}, e is:{}", rfidClient.getIp(), e);
logger.warn("RfidManager start reopen RFID, ip is {}", rfidClient.getIp());
}
}
/**
* 向rfid集合中插入一个
*/
private void insertRfidClientMap(RfidClient rfidClient) {
if(null==SocketConstant.rfidClientValidMap.get(rfidClient.getIp())){
SocketConstant.rfidClientValidMap.put(rfidClient.getIp(), rfidClient);
}
private void insertRfidClientMap(RfidClient rfidClient, JWReader mJwReader) {
SocketConstant.rfidJWReaderValidMap.remove(rfidClient.getIp());
SocketConstant.rfidJWReaderValidMap.put(rfidClient.getIp(), mJwReader);
}
/**
... ... @@ -176,12 +144,6 @@ public class RfidManager {
return mRfidSetting;
}
/**
* 停止盘点
*/
public void stopInventory() {
// mJwReader.RFID_Stop_Inventory();
}
private void clearData() {
SocketConstant.allTags.clear();
... ...
package com.yoho.rfid.service;
import JW.UHF.JWReader;
import com.yoho.rfid.model.RfidClient;
import com.yoho.rfid.socket.SocketConstant;
import com.yoho.rfid.util.SocketConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -32,7 +33,7 @@ public class RfidMonitor {
try {
Thread.sleep(10000);// 暂停10s
Map<String, RfidClient> rfidClientAllMap = SocketConstant.rfidClientAllMap;
Map<String, RfidClient> rfidClientValidMap = SocketConstant.rfidClientValidMap;
Map<String, JWReader> rfidClientValidMap = SocketConstant.rfidJWReaderValidMap;
Set<String> allClientIp = rfidClientAllMap.keySet();
Set<String> validClientIp = rfidClientValidMap.keySet();
... ... @@ -44,10 +45,11 @@ public class RfidMonitor {
allClientIp.removeAll(validClientIp);
for(String ip : allClientIp){
RfidClient rfidClient = rfidClientAllMap.get(ip);
logger.info("RfidMonitor start openRFID, ip is {}", ip);
rfidInit.openRFID(rfidClient);
}
} catch (Exception e) {
logger.warn("browser send MQ error. error message is {}", e.getMessage());
logger.warn("RfidMonitor failed. error message is {}", e);
}
}
}
... ...
package com.yoho.rfid.service;
import com.yoho.rfid.model.RfidClient;
import com.yoho.rfid.util.SocketConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* RFID上报的数据处理
* 1、把指定时间30分钟没有上报的sku删掉
*/
@Component
public class RfidResultHandler {
private static Logger logger = LoggerFactory.getLogger(RfidResultHandler.class);
private static final ExecutorService executorService = Executors.newSingleThreadExecutor();
static {
executorService.execute(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(10000);// 暂停60s
Map<String, Map<String, Long>> allTagsMap = SocketConstant.allTags;
Iterator<Map.Entry<String, Map<String, Long>>> allIt = allTagsMap.entrySet().iterator();
while(allIt.hasNext()){
Map.Entry<String, Map<String, Long>> tagMap = allIt.next();
Map<String, Long> skuMap = tagMap.getValue();
Iterator<Map.Entry<String, Long>> it = skuMap.entrySet().iterator();
while(it.hasNext()){
Map.Entry<String, Long> skuEntry=it.next();
if(System.currentTimeMillis()-skuEntry.getValue() > SocketConstant.SKU_NOTBELONG_TIMEOUT){
it.remove();
}
}
}
} catch (Exception e) {
logger.warn("RfidResultHandler failed. error message is {}", e);
}
}
}
});
}
}
... ...
package com.yoho.rfid.controller;
package com.yoho.rfid.util;
import com.alibaba.fastjson.JSON;
import com.yoho.core.common.utils.MD5;
... ...
package com.yoho.rfid.controller;
package com.yoho.rfid.util;
import com.yoho.error.GatewayError;
... ...
package com.yoho.rfid.socket;
package com.yoho.rfid.util;
import java.util.ArrayList;
import java.util.HashMap;
... ... @@ -8,6 +8,7 @@ import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;
import JW.UHF.JWReader;
import JW.UHF.Tag;
import com.yoho.rfid.model.RfidClient;
... ... @@ -34,7 +35,7 @@ public class SocketConstant {
/**
* 正在生效的读写器列表
*/
public static Map<String, RfidClient> rfidClientValidMap = new ConcurrentHashMap<>();
public static Map<String, JWReader> rfidJWReaderValidMap = new ConcurrentHashMap<>();
/**
* mac和ip的对应关系
... ...
... ... @@ -22,7 +22,6 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: com.yoho.rfid:yoho-rfid-common:1.0.0-SNAPSHOT" level="project" />
<orderEntry type="module" module-name="service" />
<orderEntry type="library" name="Maven: org.springframework:spring-webmvc:4.2.2.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-beans:4.2.2.RELEASE" level="project" />
... ...