package com.fzzy.protocol.weightyh;
|
|
import com.fzzy.api.utils.BytesUtil;
|
import com.fzzy.api.utils.SpringUtil;
|
import com.fzzy.gateway.GatewayUtils;
|
import com.fzzy.gateway.api.GatewayRemoteManager;
|
import com.fzzy.gateway.data.BaseReqData;
|
import com.fzzy.gateway.entity.GatewayDevice;
|
import com.ld.io.api.IoMsgConsumer;
|
import com.ld.io.api.IoSession;
|
import lombok.extern.slf4j.Slf4j;
|
|
/**
|
* 地磅信息推送
|
*
|
* @author: andy.jia
|
* @description:
|
* @version:
|
* @data:2020年3月19日
|
*/
|
@Slf4j
|
public class MessageConsumer implements IoMsgConsumer {
|
|
private long timeTag = 0;
|
|
private GatewayRemoteManager gatewayRemoteManager;
|
|
@Override
|
public void consume(IoSession session, byte[] bytes) {
|
analysisInfo(session.getAddress(), session.getPort(), bytes);
|
}
|
|
/**
|
* 02 2B 30 30 31 31 38 30 30 31 33 03 02开始 03 结束
|
*/
|
public void analysisInfo(String ip, Integer port, byte[] bytes) {
|
String strMsg = BytesUtil.bytesToString(bytes);
|
|
if (strMsg.length() < 22) {
|
return;
|
}
|
|
// 每间隔2.5执行一次
|
if (System.currentTimeMillis() - timeTag <= 2500) {
|
return;
|
}
|
timeTag = System.currentTimeMillis();
|
|
String temp = strMsg.substring(2, 4);
|
//String symbol = HexASCIIToConvert(temp);
|
temp = strMsg.substring(4, 16);
|
String w = HexASCIIToConvert(temp);
|
temp = strMsg.substring(16, 18);
|
int d = getProduct(Integer.parseInt(HexASCIIToConvert(temp)));
|
|
//最终重量结果
|
double weigh = Double.valueOf(w) / d;
|
|
log.debug("WEIGHT-MESSAGE=" + strMsg);
|
log.debug("----------地磅称重数值解析------{}", weigh);
|
|
//当前地磅协议不支持传递SN,使用局域网IP作为SN
|
String sn = ip;
|
|
//根据信息获取设备
|
GatewayDevice device = GatewayUtils.getCacheByDeviceSn(sn);
|
|
if (null == device) {
|
log.error("----------没有获取到设备信息--------{}", sn);
|
return;
|
}
|
|
//直接调用实现类,更多实现类单独调用
|
if (null == gatewayRemoteManager) {
|
gatewayRemoteManager = SpringUtil.getBean(GatewayRemoteManager.class);
|
}
|
|
//数据封装推送
|
BaseReqData reqData = new BaseReqData();
|
reqData.setDevice(device);
|
reqData.setDeviceId(device.getDeviceId());
|
reqData.setProductId(device.getProductId());
|
reqData.setDeviceName(device.getDeviceName());
|
reqData.setWeight(weigh);
|
|
if (weigh > 0) {
|
gatewayRemoteManager.getDeviceReportService(device.getPushProtocol()).reportWeightData(reqData);
|
}
|
}
|
|
/**
|
* 根据小数点位数计算原数字需要除以多少
|
*
|
* @param i
|
*/
|
private int getProduct(int i) {
|
if (i == 0)
|
return 1;
|
int num = 1;
|
for (int a = 0; a < i; a++) {
|
num = num * 10;
|
}
|
return num;
|
}
|
|
private String HexASCIIToConvert(String value) {
|
// String value = "30 30 31 31 38 30 30";
|
StringBuffer sbu = new StringBuffer();
|
char t = 0;
|
int size = value.length() / 2;
|
for (int i = 0; i < size; i++) {
|
String tmp = value.substring(i * 2, (i + 1) * 2);
|
t = (char) Integer.parseInt(""
|
+ BytesUtil.bytesToInt(BytesUtil.hexStrToBytes(tmp)));
|
sbu.append(t);
|
}
|
|
return sbu.toString();
|
}
|
|
|
}
|