package com.ld.igds.protocol.quantity.shuhan.analysis;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
import com.ld.igds.common.CoreCommonService;
|
import com.ld.igds.constant.Constant;
|
import com.ld.igds.constant.RedisConst;
|
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.protocol.quantity.shuhan.ShuHanUtils;
|
import com.ld.igds.protocol.quantity.shuhan.command.CommandBuilder;
|
import com.ld.igds.protocol.quantity.shuhan.command.Message;
|
import com.ld.igds.protocol.quantity.shuhan.command.MessageContent;
|
import com.ld.igds.protocol.quantity.shuhan.server.ShuhanServerEngine;
|
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.RedisUtil;
|
import com.ld.io.api.IoSession;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
|
import java.util.Date;
|
import java.util.List;
|
|
/**
|
* 协议解析入口
|
*
|
* @author jiazx
|
*/
|
@Slf4j
|
@Component(AnalysisService.BEAN_ID)
|
public class AnalysisService {
|
|
public static final String BEAN_ID = "shuhan.analysisService";
|
|
@Autowired
|
private CoreCommonService coreCommonService;
|
@Autowired
|
private CoreQuantityService coreQuantityService;
|
@Autowired
|
private NotifyWebInvoker notifyWebInvoker;
|
@Autowired
|
private RedisUtil redisUtil;
|
|
/**
|
* 协议解析
|
*
|
* @param ip
|
* @param port
|
* @param message
|
*/
|
public void analysis(String ip, Integer port, Message message) {
|
|
//添加心跳信息
|
ShuHanUtils.addHeartBeat(ip, port);
|
log.info("蜀汉数量检测命令={}", message);
|
if (message == null)
|
return;
|
switch (message.getFunctionId()) {
|
case "0":
|
analysis0(ip, port, message);
|
break;
|
case "1":
|
analysis1(ip, port, message);
|
break;
|
case "2":
|
analysis2(ip, port, message);
|
break;
|
case "3":
|
analysis3(ip, port, message);
|
break;
|
case "4":
|
analysis4(ip, port, message);
|
break;
|
default:
|
break;
|
}
|
|
}
|
|
/**
|
* 登录 初次连接
|
*
|
* @param ip
|
* @param port
|
* @param message
|
*/
|
private void analysis0(String ip, Integer port, Message message) {
|
|
QuantityConf quantityConf = coreQuantityService.getCacheQuantityConfBySn(message.getSn());
|
if (quantityConf == null) {
|
log.error("没有获取到对应数量检测配置,取消连接");
|
ShuhanServerEngine.destroy(ip, port);
|
return;
|
}
|
|
//更新所有的数量检测配置的通讯IP和端口
|
List<QuantityConf> list = coreQuantityService.getCacheQuantityConf(quantityConf.getCompanyId());
|
if(null != list && list.size() > 0){
|
for (QuantityConf conf : list) {
|
conf.setIp(ip);
|
conf.setPort(port);
|
conf.setStatus(Constant.YN_Y);
|
coreQuantityService.updateQuantityConfBySn(conf);
|
}
|
}
|
|
coreQuantityService.refreshQuantityConfCache();
|
|
log.info("++++蜀汉插件登录认证完成++++-SN={}", message.getSn());
|
}
|
|
/**
|
* 开始采集,无需解析
|
*
|
* @param ip
|
* @param port
|
* @param message
|
*/
|
private void analysis1(String ip, Integer port, Message message) {
|
// do Nothing
|
}
|
|
/**
|
* 查询状态
|
*
|
* @param ip
|
* @param port
|
* @param message
|
*/
|
private void analysis2(String ip, Integer port, Message message) {
|
|
QuantityConf conf = coreQuantityService.getCacheQuantityConfBySn(message.getSn());
|
|
if (conf == null) {
|
log.error("没有获取到对应分机,取消解析:" + message.toString());
|
return;
|
}
|
String ytIp = message.getSn();
|
|
String key = RedisConst.buildKey(conf.getCompanyId(), ytIp);
|
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());
|
MessageContent content = message.getContent();
|
if (content == null) {
|
log.error("没有找到关联配置,取消解析:" + message.toString());
|
return;
|
}
|
Depot depot = coreCommonService.getCacheDepot(conf.getCompanyId(),
|
conf.getDepotId());
|
progressData.setProgress(Integer.parseInt(content.getProgress()));
|
progressData.setState(content.getState());
|
|
if ("finished".equals(progressData.getState())) {
|
// 检测完成,发送发送计算命令。删除缓存
|
key = RedisConst.buildKey(conf.getCompanyId(), ytIp);
|
redisUtil.set(key, null, 3);
|
// redisUtil.del(ServerUtils.buildRedisKey(conf.getCompanyId(),ytIp));
|
IoSession session = ShuhanServerEngine.getSession(ip, port);
|
if (session != null) {
|
session.invoke(CommandBuilder.getResultMsg(quantityRequest)
|
.getBytes());
|
}
|
progressData.setMsg(depot.getName() + "数量监测执行进度:"
|
+ progressData.getProgress() + "%,开始计算结果!");
|
}
|
if ("failed".equals(progressData.getState())) {
|
// 检测失败,删除缓存
|
key = RedisConst.buildKey(conf.getCompanyId(), ytIp);
|
redisUtil.set(key, null, 3);
|
// redisUtil.del(ServerUtils.buildRedisKey(conf.getCompanyId(),ytIp));
|
progressData.setMsg(depot.getName() + "数量检测执行失败,进度:"
|
+ progressData.getProgress() + "%,开始检测时间:"
|
+ content.getStart_time());
|
}
|
|
log.info(depot.getName() + "数量监测执行进度:" + progressData.getProgress()
|
+ "%,开始检测时间:" + content.getStart_time());
|
progressData.setMsg(depot.getName() + "数量监测执行进度:"
|
+ progressData.getProgress() + "%,开始检测时间:"
|
+ content.getStart_time());
|
|
notifyWebInvoker.notifyQuantityProgress(progressData);
|
}
|
|
/**
|
* 返回结果
|
*
|
* @param ip
|
* @param port
|
* @param message
|
*/
|
private void analysis3(String ip, Integer port, Message message) {
|
QuantityConf conf = coreQuantityService.getCacheQuantityConfBySn(message.getSn());
|
if (conf == null) {
|
log.error("没有获取到对应分机,取消解析:" + message.toString());
|
return;
|
}
|
|
MessageContent content = message.getContent();
|
if (content == null) {
|
log.error("没有找到关联配置,取消解析:" + message.toString());
|
return;
|
}
|
Depot depot = coreCommonService.getCacheDepot(conf.getCompanyId(),
|
conf.getDepotId());
|
JSONObject msg = content.getMsg();
|
if (msg == null) {
|
log.error("没有数据,取消解析:" + message.toString());
|
return;
|
}
|
|
String key = RedisConst.buildKey(conf.getCompanyId(), conf.getYtIp());
|
QuantityRequest res = (QuantityRequest) redisUtil.get(key);
|
if (res == null) {
|
log.error("没有找到关联配置,取消解析:" + message.toString());
|
return;
|
}
|
|
QuantityData quantityData = new QuantityData();
|
|
quantityData.setBatchId(res.getBatchId());
|
quantityData.setReceiveDate(new Date());
|
quantityData.setBulk(msg.getDoubleValue("bulk"));
|
quantityData.setWeight(msg.getDoubleValue("weight"));
|
quantityData.setDepotId(depot.getId());
|
quantityData.setDeptId(depot.getDeptId());
|
quantityData.setCompanyId(depot.getCompanyId());
|
quantityData.setPoints(JSON.toJSONString(msg.getJSONArray("heights")));
|
|
coreQuantityService.saveData(quantityData);
|
|
//清除缓存
|
redisUtil.del(key);
|
}
|
|
/**
|
* 心跳判断
|
*
|
* @param ip
|
* @param port
|
* @param message
|
*/
|
private void analysis4(String ip, Integer port, Message message) {
|
log.debug("------------------蜀汉插件心跳维持----------------");
|
}
|
|
}
|