package com.ld.igds.io.fzzy.analysis;
|
|
import com.alibaba.fastjson.JSON;
|
import com.ld.igds.common.CoreCommonService;
|
import com.ld.igds.constant.Constant;
|
import com.ld.igds.file.CoreFileService;
|
import com.ld.igds.file.dto.FileData;
|
import com.ld.igds.io.fzzy.ServerUtils;
|
import com.ld.igds.io.fzzy.command.BhznRemoteQuantityImpl;
|
import com.ld.igds.io.fzzy.command.CommandBuilder;
|
import com.ld.igds.io.fzzy.dto.IoMessage;
|
import com.ld.igds.io.fzzy.dto.Resp2003;
|
import com.ld.igds.io.fzzy.dto.Resp2004;
|
import com.ld.igds.io.fzzy.dto.Resp2006;
|
import com.ld.igds.io.fzzy.server.BhznQuantityServerEngine;
|
import com.ld.igds.io.notify.NotifyWebInvoker;
|
import com.ld.igds.io.request.QuantityRequest;
|
import com.ld.igds.models.Depot;
|
import com.ld.igds.models.QuantityConf;
|
import com.ld.igds.quantity.dto.QuantityData;
|
import com.ld.igds.quantity.dto.QuantityProgressData;
|
import com.ld.igds.quantity.service.CoreQuantityService;
|
import com.ld.igds.util.ContextUtil;
|
import com.ld.igds.util.RedisUtil;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
|
import java.util.Date;
|
import java.util.UUID;
|
|
|
/**
|
* 协议解析入口
|
*
|
* @author vince
|
*/
|
@Slf4j
|
@Component(AnalysisService.BEAN_ID)
|
public class AnalysisService {
|
|
public static final String BEAN_ID = "bhzn.quantityAnalysisService";
|
|
@Autowired
|
private CoreCommonService coreCommonService;
|
@Autowired
|
private CoreQuantityService coreQuantityService;
|
@Autowired
|
private NotifyWebInvoker notifyWebInvoker;
|
@Autowired
|
private CoreFileService fileService;
|
@Autowired
|
private RedisUtil redisUtil;
|
@Autowired
|
private BhznQuantityServerEngine bhznQuantityServerEngine;
|
@Autowired
|
private BhznRemoteQuantityImpl remoteQuantity;
|
|
/**
|
* @param sessionKey ip:port
|
*/
|
public void analysis(String sessionKey, IoMessage message) throws Exception {
|
|
if (message == null) return;
|
|
String[] attr = sessionKey.split(":");
|
String ip = attr[0];
|
Integer port = Integer.valueOf(attr[1]);
|
String checkMsg = checkMsg(message);
|
|
if (null != checkMsg) {
|
log.warn("数量检测---->>>平台:,分机SN={},报文信息-={},响应吗返回不正确,无法进行解析," + checkMsg, message.getSn(), message.toString());
|
return;
|
}
|
|
QuantityConf conf = coreQuantityService.getCacheQuantityConfBySn(message.getSn());
|
|
if (null == conf) {
|
//说明当前设备未注册平台,直接踢出连接
|
log.info("数量检测---->>>平台:登陆认证SN-{}-平台未注册,直接踢出连接", message.getSn());
|
bhznQuantityServerEngine.destroy(ip, port);
|
return;
|
}
|
|
|
switch (message.getFunctionId()) {
|
case "1001"://服务端下发配置
|
//不处理
|
break;
|
case "1002"://终端请求时间同步
|
analysis1002(ip, port, conf, message);
|
break;
|
case "1003"://终端设备请求登陆
|
analysis1003(ip, port, conf, message);
|
break;
|
case "2001"://开始检测
|
//
|
break;
|
case "2002"://停止检测
|
//
|
break;
|
case "2003"://服务端查询进度
|
analysis2003(ip, port, conf, message);
|
break;
|
case "2004"://终端发起结果上报
|
analysis2004(ip, port, conf, message);
|
break;
|
case "2005"://服务端发起数据点位提取
|
analysis2005(ip, port, conf, message);
|
break;
|
case "2006"://服务端请求抓拍,终端返回图像信息
|
analysis2006(ip, port, conf, message);
|
break;
|
case "2007"://终端主动发起心跳维持
|
analysis2007(ip, port, conf, message);
|
break;
|
default:
|
break;
|
}
|
}
|
|
/**
|
* 用户登陆成功,更新数据和缓存,进入当前接口表示平台已经认证通过,直接更新并返回正常信息
|
*
|
* @param ip
|
* @param port
|
* @param conf
|
* @param message
|
*/
|
private void analysis1003(String ip, Integer port, QuantityConf conf, IoMessage message) {
|
conf.setIp(ip);
|
conf.setPort(port);
|
conf.setStatus(Constant.YN_Y);
|
|
coreQuantityService.updateQuantityConfBySn(conf);
|
|
//返回正常
|
remoteQuantity.sendMsg(ip, port, CommandBuilder.getInstance().build1003Message(message));
|
}
|
|
/**
|
* 返回给终端当前时间
|
*
|
* @param quantityConf
|
* @param message
|
*/
|
private void analysis1002(String ip, Integer port, QuantityConf quantityConf, IoMessage message) {
|
//直接返回
|
remoteQuantity.sendMsg(ip, port, CommandBuilder.getInstance().build1002Message(message));
|
}
|
|
/**
|
* 解析终端返回的采集进度
|
*
|
* @param message
|
*/
|
private void analysis2003(String ip, Integer port, QuantityConf conf, IoMessage message) {
|
|
String key = ServerUtils.buildRequestKey(conf.getCompanyId(), conf.getSn());
|
|
QuantityRequest quantityRequest = (QuantityRequest) redisUtil.get(key);
|
if (quantityRequest == null) {
|
log.error("没有找到发送的命令,可能已经超时,取消解析:" + message.toString());
|
return;
|
}
|
|
QuantityProgressData progressData = new QuantityProgressData();
|
progressData.setCompanyId(conf.getCompanyId());
|
progressData.setDepotId(conf.getDepotId());
|
progressData.setDeptId(conf.getDeptId());
|
Resp2003 content = JSON.parseObject(message.getContent(), Resp2003.class);
|
if (content == null) {
|
log.error("没有找到关联配置,取消解析:" + message.toString());
|
return;
|
}
|
Depot depot = coreCommonService.getCacheDepot(conf.getCompanyId(),
|
conf.getDepotId());
|
progressData.setProgress(content.getProgress());
|
|
log.info(depot.getName() + "数量监测执行进度:" + progressData.getProgress());
|
progressData.setMsg(depot.getName() + "数量监测执行进度:"
|
+ progressData.getProgress());
|
notifyWebInvoker.notifyQuantityProgress(progressData);
|
//根据进度抓拍
|
if (progressData.getProgress() >= 10 && progressData.getProgress() <= 20) {
|
remoteQuantity.sendMsg(ip, port, CommandBuilder.getInstance().build2006Message(message));
|
}
|
if (progressData.getProgress() >= 45 && progressData.getProgress() <= 55) {
|
remoteQuantity.sendMsg(ip, port, CommandBuilder.getInstance().build2006Message(message));
|
}
|
if (progressData.getProgress() >= 80 && progressData.getProgress() <= 90) {
|
remoteQuantity.sendMsg(ip, port, CommandBuilder.getInstance().build2006Message(message));
|
}
|
}
|
|
/**
|
* 解析终端返回的结果,并返回成功
|
*
|
* @param conf
|
* @param message
|
*/
|
private void analysis2004(String ip, Integer port, QuantityConf conf, IoMessage message) {
|
|
Resp2004 content = JSON.parseObject(message.getContent(), Resp2004.class);
|
if (content == null) {
|
log.error("没有数据,取消解析:" + message.toString());
|
return;
|
}
|
String key = ServerUtils.buildRequestKey(conf.getCompanyId(), conf.getSn());
|
|
QuantityRequest request = (QuantityRequest) redisUtil.get(key);
|
|
if (request == null) {
|
log.error("没有获取到发送命令,取消解析:" + message.toString());
|
return;
|
}
|
Depot depot = coreCommonService.getCacheDepot(conf.getCompanyId(), conf.getDepotId());
|
QuantityData quantityData = new QuantityData();
|
quantityData.setBatchId(request.getBatchId());
|
quantityData.setReceiveDate(new Date());
|
quantityData.setBulk(content.getVolume());
|
quantityData.setWeight(content.getWeight());
|
quantityData.setDepotId(depot.getId());
|
quantityData.setDeptId(depot.getDeptId());
|
quantityData.setCompanyId(depot.getCompanyId());
|
quantityData.setPoints("");
|
coreQuantityService.saveData(quantityData);
|
//直接返回
|
remoteQuantity.sendMsg(ip, port, CommandBuilder.getInstance().build2004Message(message));
|
}
|
|
/**
|
* 解析终端返回的点位数据
|
*
|
* @param quantityConf
|
* @param message
|
*/
|
private void analysis2005(String ip, Integer port, QuantityConf quantityConf, IoMessage message) {
|
// todo
|
}
|
|
/**
|
* 解析抓拍的图片信息
|
*
|
* @param conf
|
* @param message
|
*/
|
private void analysis2006(String ip, Integer port, QuantityConf conf, IoMessage message) {
|
Resp2006 content = JSON.parseObject(message.getContent(), Resp2006.class);
|
if (content == null) {
|
log.error("没有数据,取消解析:" + message.toString());
|
return;
|
}
|
String key = ServerUtils.buildRequestKey(conf.getCompanyId(), conf.getSn());
|
QuantityRequest res = (QuantityRequest) redisUtil.get(key);
|
if (res == null) {
|
log.error("没有获取到发送命令,取消解析:" + message.toString());
|
return;
|
}
|
String fileName = UUID.randomUUID().toString();
|
ServerUtils.base64ToJpg(fileService.getCommonFilePath(new Date()), fileName, content.getData());
|
FileData data = new FileData();
|
data.setCreateTime(new Date());
|
data.setBizId(res.getBatchId());
|
data.setBizTag(Constant.MODEL_QUANTITY);
|
data.setCompanyId(conf.getCompanyId());
|
data.setFileId(ContextUtil.getUUID());
|
fileService.addRecord(data);
|
}
|
|
/**
|
* 心跳反馈
|
*
|
* @param quantityConf
|
* @param message
|
*/
|
private void analysis2007(String ip, Integer port, QuantityConf quantityConf, IoMessage message) {
|
remoteQuantity.sendMsg(ip, port, CommandBuilder.getInstance().build2007Message(message));
|
}
|
|
public String checkMsg(IoMessage message) {
|
if (StringUtils.isEmpty(message.getResult())) return null;
|
if (ServerUtils.RESP_0.equals(message.getResult())) return null;
|
|
if (ServerUtils.RESP_1.equals(message.getResult())) return "命令格式错误";
|
if (ServerUtils.RESP_2.equals(message.getResult())) return "命令数据检验异常";
|
if (ServerUtils.RESP_3.equals(message.getResult())) return "发送超时错误";
|
if (ServerUtils.RESP_4.equals(message.getResult())) return "终端设备不在线";
|
if (ServerUtils.RESP_5.equals(message.getResult())) return "终端设备执行中";
|
if (ServerUtils.RESP_6.equals(message.getResult())) return "终端设备抓拍失败";
|
if (ServerUtils.RESP_9.equals(message.getResult())) return "其它错误";
|
return null;
|
}
|
|
}
|