RfidConfig.java 2.57 KB
package com.yoho.rfid.service;

import com.yoho.rfid.model.RfidClient;
import com.yoho.rfid.util.SocketConstant;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 配置RFID读写器
 */
@Component
public class RfidConfig {

	private static final Logger logger = LoggerFactory.getLogger(RfidConfig.class);

	@Value("${rfid.client.address:null}")
	private String clientAddress[];
	@Autowired
	private RfidInit rfidInit;
	@Autowired
	private RfidMonitor rfidMonitor;
	@Autowired
	private RfidConnMonitor rfidConnMonitor;

	@PostConstruct
	private void initClient() {
		if (ArrayUtils.isNotEmpty(clientAddress)) {
			for(int i=0; i<clientAddress.length; i++){
				RfidClient rfidClient = new RfidClient();
				try{
					String clientInfo[] = clientAddress[i].split(":");
					// mac
					rfidClient.setMacId(clientInfo[0]);
					// ip
					rfidClient.setIp(clientInfo[1]);
					// port
					rfidClient.setPort(clientInfo[2]);
					// power
					String power = clientInfo[3];
					buildPowerInfo(rfidClient, power);
					// speedmode
					rfidClient.setSpeedMode(Integer.valueOf(clientInfo[4]));

					SocketConstant.rfidClientAllMap.put(rfidClient.getIp(), rfidClient);
				}catch(Exception e){
					logger.warn("client info configured error, e is:{}", e);
					return;
				}
				// 连接client,开始监听上报数据
				rfidInit.openRFID(rfidClient);
			}
			rfidMonitor.startMonitor();
			rfidConnMonitor.startMonitor();
		}else {
			logger.info("client is not configured");
		}
	}

	public void buildPowerInfo(RfidClient rfidClient, String power) {
		if(StringUtils.isEmpty(power)){
			return;
		}
		String info[] = power.split("-");//是否启用-功率
		String flag[] = info[0].split("\\|");//是否启用
		String capacity[] = info[1].split("\\|");//功率
		//设置四个天线的开启状态
		rfidClient.setPower0("1".equals(flag[0]) ? true : false);
		rfidClient.setPower1("1".equals(flag[1]) ? true : false);
		rfidClient.setPower2("1".equals(flag[2]) ? true : false);
		rfidClient.setPower3("1".equals(flag[3]) ? true : false);

		//设置四个天线的功率
		rfidClient.setCapacity0(Integer.valueOf(capacity[0]));
		rfidClient.setCapacity1(Integer.valueOf(capacity[1]));
		rfidClient.setCapacity2(Integer.valueOf(capacity[2]));
		rfidClient.setCapacity3(Integer.valueOf(capacity[3]));
	}

}