package com.fzzy.igds.quantity.analysis;
|
|
import com.alibaba.fastjson.JSON;
|
import com.fzzy.igds.constant.Constant;
|
import com.fzzy.igds.data.QuantityProgressData;
|
import com.fzzy.igds.domain.Depot;
|
import com.fzzy.igds.domain.FileInfo;
|
import com.fzzy.igds.domain.Quantity;
|
import com.fzzy.igds.domain.QuantityConf;
|
import com.fzzy.igds.notify.NotifyWebInvoker;
|
import com.fzzy.igds.quantity.command.BhznRemoteQuantityImpl;
|
import com.fzzy.igds.quantity.command.CommandBuilder;
|
import com.fzzy.igds.quantity.dto.IoMessage;
|
import com.fzzy.igds.quantity.dto.Resp2003;
|
import com.fzzy.igds.quantity.dto.Resp2004;
|
import com.fzzy.igds.quantity.dto.Resp2006;
|
import com.fzzy.igds.quantity.server.BhznQuantityServerEngine;
|
import com.fzzy.igds.quantity.util.ServerUtils;
|
import com.fzzy.igds.request.QuantityRequest;
|
import com.fzzy.igds.service.DepotService;
|
import com.fzzy.igds.service.FileService;
|
import com.fzzy.igds.service.QuantityService;
|
import com.fzzy.igds.utils.ContextUtil;
|
import com.ruoyi.common.config.FrameworkConfig;
|
import com.ruoyi.common.core.redis.RedisCache;
|
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;
|
|
/**
|
* @Description 协议解析入口
|
* @Author CZT
|
* @Date 2026/01/08 16:36
|
*/
|
@Slf4j
|
@Component("fzzy.quantityAnalysisService")
|
public class AnalysisService{
|
|
@Autowired
|
private DepotService depotService;
|
@Autowired
|
private QuantityService quantityService;
|
@Autowired
|
private NotifyWebInvoker notifyWebInvoker;
|
@Autowired
|
private FileService fileService;
|
@Autowired
|
private RedisCache 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 = quantityService.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);
|
|
quantityService.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.getCacheObject(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 = depotService.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() >= 1 && progressData.getProgress() <= 25) {
|
remoteQuantity.sendMsg(ip, port, CommandBuilder.getInstance().build2006Message(message));
|
}
|
if (progressData.getProgress() > 25 && progressData.getProgress() <= 50) {
|
remoteQuantity.sendMsg(ip, port, CommandBuilder.getInstance().build2006Message(message));
|
}
|
if (progressData.getProgress() > 50 && progressData.getProgress() <= 75) {
|
remoteQuantity.sendMsg(ip, port, CommandBuilder.getInstance().build2006Message(message));
|
}
|
if (progressData.getProgress() > 75 && progressData.getProgress() <= 99) {
|
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.getCacheObject(key);
|
|
if (request == null) {
|
log.error("没有获取到发送命令,取消解析:" + message.toString());
|
return;
|
}
|
Depot depot = depotService.getCacheDepot(conf.getCompanyId(), conf.getDepotId());
|
Quantity quantityData = new Quantity();
|
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("");
|
quantityService.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.getCacheObject(key);
|
if (res == null) {
|
log.error("没有获取到发送命令,取消解析:" + message.toString());
|
return;
|
}
|
String fileName = ContextUtil.generateId()+ ".jpg";
|
String savePath = fileService.getFileSavePath("QUANTITY");
|
String filePath = savePath+fileName;
|
fileService.baseImg2Disk(filePath, content.getData());
|
FileInfo data = new FileInfo();
|
data.setId(ContextUtil.UUID());
|
data.setCreateTime(new Date());
|
data.setFileName(fileName);
|
data.setFilePath(filePath.replace(FrameworkConfig.getProfile(), "/profile/"));
|
data.setBizId(res.getBatchId());
|
data.setBizTag("quantity");
|
data.setCompanyId(conf.getCompanyId());
|
fileService.saveFile(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;
|
}
|
|
}
|