RfidController.java 6.16 KB
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.querySkuInfo. 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();
    }
}