package com.ruoyi.dataInterchange.server;
|
|
import com.alibaba.fastjson.JSON;
|
import com.ruoyi.dataInterchange.dao.*;
|
import com.ruoyi.dataInterchange.model.*;
|
import com.ruoyi.dataInterchange.model.enu.DataType;
|
import com.ruoyi.dataInterchange.netty.client.ChannelMap;
|
import com.ruoyi.dataInterchange.util.jtt809.common.Jtt809Util;
|
import com.ruoyi.dataInterchange.util.jtt809.packet.common.OuterPacket;
|
import com.ruoyi.dataInterchange.wapper.UPConnect;
|
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.Unpooled;
|
import io.netty.channel.Channel;
|
import io.netty.channel.ChannelHandlerContext;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Service;
|
|
import javax.annotation.Resource;
|
import java.time.LocalDateTime;
|
import java.util.Iterator;
|
|
/**
|
* @author zhibing.pu
|
* @Date 2025/3/4 19:42
|
*/
|
@Slf4j
|
@Service
|
public class ExgMsgService {
|
|
@Resource
|
private ConnectReqService connectReqService;
|
|
@Resource
|
private UPExgMsgRegisterDao upExgMsgRegisterDao;
|
|
@Resource
|
private UPExgMsgRealLocationDao upExgMsgRealLocationDao;
|
|
@Resource
|
private UPExgMsgHistoryLocationDao upExgMsgHistoryLocationDao;
|
|
@Resource
|
private UPExgMsgReportDriverInfoAckDao upExgMsgReportDriverInfoAckDao;
|
|
@Resource
|
private UPExgMsgTakeEwayBillAckDao upExgMsgTakeEwayBillAckDao;
|
|
|
public void up_exg_msg(ChannelHandlerContext ctx, OuterPacket out) {
|
UPExgMsg exgMsg = getExgMsg(out);
|
DataType dataType = DataType.getDataType(exgMsg.getDataType());
|
switch (dataType) {
|
case UP_EXG_MSG_REGISTER:
|
log.info("上传车辆注册信息({}):{}", DataType.UP_EXG_MSG_REGISTER.getCode(), out);
|
up_exg_msg_register(ctx, out.getGnsscenterId(), exgMsg);
|
break;
|
case UP_EXG_MSG_REAL_LOCATION:
|
log.info("上传车辆实时定位信息({}):{}", DataType.UP_EXG_MSG_REAL_LOCATION.getCode(), out);
|
up_exg_msg_real_location(ctx, out.getGnsscenterId(), exgMsg);
|
break;
|
case UP_EXG_MSG_HISTORY_LOCATION:
|
log.info("车辆定位信息自动补报请求({}):{}", DataType.UP_EXG_MSG_HISTORY_LOCATION.getCode(), out);
|
up_exg_msg_history_location(ctx, out.getGnsscenterId(), exgMsg);
|
break;
|
case UP_EXG_MSG_RETURN_STARTUP_ACK:
|
log.info("启动车辆定位信息交换应答({}):{}", DataType.UP_EXG_MSG_RETURN_STARTUP_ACK.getCode(), out);
|
break;
|
case UP_EXG_MSG_RETURN_END_ACK:
|
log.info("结束车辆定位信息交换应答({}):{}", DataType.UP_EXG_MSG_RETURN_END_ACK.getCode(), out);
|
break;
|
case UP_EXG_MSG_APPLY_FOR_MONITOR_STARTUP:
|
log.info("申请交换指定车辆定位信息请求({}):{}", DataType.UP_EXG_MSG_APPLY_FOR_MONITOR_STARTUP.getCode(), out);
|
break;
|
case UP_EXG_MSG_APPLY_FOR_MONITOR_END:
|
log.info("取消交换指定车辆定位信息请求({}):{}", DataType.UP_EXG_MSG_APPLY_FOR_MONITOR_END.getCode(), out);
|
break;
|
case UP_EXG_MSG_APPLY_HISGNSSDATA_REQ:
|
log.info("车辆定位信息补发请求({}):{}", DataType.UP_EXG_MSG_APPLY_HISGNSSDATA_REQ.getCode(), out);
|
break;
|
case UP_EXG_MSG_REPORT_DRIVER_INFO_ACK:
|
log.info("上报驾驶员身份信息应答({}):{}", DataType.UP_EXG_MSG_REPORT_DRIVER_INFO_ACK.getCode(), out);
|
up_exg_msg_report_driver_info_ack(ctx, out.getGnsscenterId(), exgMsg);
|
break;
|
case UP_EXG_MSG_TAKE_EWAYBILL_ACK:
|
log.info("上报车辆电子运单应答消息({}):{}", DataType.UP_EXG_MSG_TAKE_EWAYBILL_ACK.getCode(), out);
|
up_exg_msg_take_ewaybill_ack(ctx, out.getGnsscenterId(), exgMsg);
|
break;
|
default:
|
break;
|
}
|
}
|
|
|
/**
|
* 解析子业务数据
|
*
|
* @param out
|
* @return
|
*/
|
public UPExgMsg getExgMsg(OuterPacket out) {
|
byte[] body = out.getBody();
|
ByteBuf byteBuf = Unpooled.wrappedBuffer(body);
|
//车牌号
|
String vehicleNo = Jtt809Util.readGBKString(byteBuf, 21);
|
//车牌颜色
|
byte vehicleColor = byteBuf.readByte();
|
//子业务类型标识
|
int dataType = byteBuf.readUnsignedShort();
|
//后续数据长度
|
int dataLength = byteBuf.readInt();
|
//子业务数据包
|
byte[] data = new byte[byteBuf.readableBytes()];
|
byteBuf.readBytes(data);
|
UPExgMsg upExgMsg = new UPExgMsg();
|
upExgMsg.setVehicleNo(vehicleNo);
|
upExgMsg.setVehicleColor(vehicleColor);
|
upExgMsg.setDataType(dataType);
|
upExgMsg.setDataLength(dataLength);
|
upExgMsg.setData(data);
|
return upExgMsg;
|
}
|
|
|
/**
|
* 上传车辆注册信息
|
*
|
* @param ctx
|
*/
|
public void up_exg_msg_register(ChannelHandlerContext ctx, int inferiorPlatformId, UPExgMsg exgMsg) {
|
UPExgMsgRegister upExgMsgRegister = new UPExgMsgRegister().decode(exgMsg);
|
upExgMsgRegister.setInferiorPlatformId(inferiorPlatformId);
|
upExgMsgRegister.setCreateTime(LocalDateTime.now());
|
upExgMsgRegisterDao.save(upExgMsgRegister);
|
//上报驾驶员身份识别信息请求
|
down_exg_msg_report_driver_info(ctx, inferiorPlatformId, upExgMsgRegister.getVehicleNo(), upExgMsgRegister.getVehicleColor());
|
}
|
|
/**
|
* 上报驾驶员身份识别信息请求
|
*
|
* @param ctx
|
*/
|
public void down_exg_msg_report_driver_info(ChannelHandlerContext ctx, int inferiorPlatformId, String vehicleNo, int VehicleColor) {
|
DOWNExgMsgReportDriverInfo downExgMsgReportDriverInfo = new DOWNExgMsgReportDriverInfo();
|
downExgMsgReportDriverInfo.setVehicleNo(vehicleNo);
|
downExgMsgReportDriverInfo.setVehicleColor(VehicleColor);
|
downExgMsgReportDriverInfo.setDataType(DataType.DOWN_EXG_MSG_REPORT_DRIVER_INFO.getCode());
|
downExgMsgReportDriverInfo.setDataLength(0);
|
log.info("上报驾驶员身份识别信息请求({}):{}", DataType.DOWN_EXG_MSG_REPORT_DRIVER_INFO.getCode(), JSON.toJSONString(downExgMsgReportDriverInfo));
|
byte[] body = downExgMsgReportDriverInfo.encode();
|
OuterPacket out = new OuterPacket(DataType.DOWN_EXG_MSG_REPORT_DRIVER_INFO.getCode(), body);
|
//获取从链路通道
|
Channel channel = ChannelMap.getClientChannel(inferiorPlatformId);
|
if (null != channel && channel.isActive()) {
|
channel.writeAndFlush(out);
|
channel.flush();
|
} else {
|
ctx.writeAndFlush(out);
|
ctx.flush();
|
}
|
}
|
|
|
/**
|
* 上传车辆实时定位信息
|
*
|
* @param ctx
|
*/
|
public void up_exg_msg_real_location(ChannelHandlerContext ctx, int inferiorPlatformId, UPExgMsg exgMsg) {
|
UPExgMsgRealLocation upExgMsgRealLocation = new UPExgMsgRealLocation().decode(exgMsg);
|
upExgMsgRealLocation.setInferiorPlatformId(inferiorPlatformId);
|
upExgMsgRealLocation.setCreateTime(LocalDateTime.now());
|
upExgMsgRealLocationDao.save(upExgMsgRealLocation);
|
}
|
|
|
/**
|
* 定时任务获取车辆运单信息
|
*/
|
public void takeEwaybill() {
|
Iterator<UPExgMsgRegister> iterator = upExgMsgRegisterDao.findAll().iterator();
|
while (iterator.hasNext()) {
|
UPExgMsgRegister pojo = iterator.next();
|
down_exg_msg_take_ewaybill_req(pojo.getInferiorPlatformId(), pojo.getVehicleNo(), pojo.getVehicleColor());
|
}
|
}
|
|
|
/**
|
* 上报车辆电子运单请求
|
*
|
* @param inferiorPlatformId
|
*/
|
public void down_exg_msg_take_ewaybill_req(int inferiorPlatformId, String vehicleNo, int VehicleColor) {
|
DOWNExgMsgTakeEwaybillReq downExgMsgTakeEwaybillReq = new DOWNExgMsgTakeEwaybillReq();
|
downExgMsgTakeEwaybillReq.setVehicleNo(vehicleNo);
|
downExgMsgTakeEwaybillReq.setVehicleColor(VehicleColor);
|
downExgMsgTakeEwaybillReq.setDataType(DataType.DOWN_EXG_MSG_REPORT_DRIVER_INFO.getCode());
|
downExgMsgTakeEwaybillReq.setDataLength(0);
|
|
log.info("上报车辆电子运单请求({}):{}", DataType.DOWN_EXG_MSG_TAKE_EWAYBILL_REQ.getCode(), JSON.toJSONString(downExgMsgTakeEwaybillReq));
|
byte[] body = downExgMsgTakeEwaybillReq.encode();
|
OuterPacket out = new OuterPacket(DataType.DOWN_EXG_MSG_TAKE_EWAYBILL_REQ.getCode(), body);
|
//获取从链路通道
|
Channel channel = ChannelMap.getClientChannel(inferiorPlatformId);
|
if (null != channel && channel.isActive()) {
|
channel.writeAndFlush(out);
|
channel.flush();
|
} else {
|
//重新连接从链路
|
UPConnect ipAndPort = ChannelMap.getIpAndPort(inferiorPlatformId);
|
connectReqService.downConnect(inferiorPlatformId, ipAndPort.getDownLinkIp(), ipAndPort.getDownLinkPort(), ipAndPort.getVerifyCode());
|
}
|
}
|
|
|
/**
|
* 车辆定位信息自动补报请求
|
*
|
* @param ctx
|
* @param inferiorPlatformId
|
* @param exgMsg
|
*/
|
public void up_exg_msg_history_location(ChannelHandlerContext ctx, int inferiorPlatformId, UPExgMsg exgMsg) {
|
UPExgMsgHistoryLocation upExgMsgHistoryLocation = new UPExgMsgHistoryLocation().decode(exgMsg);
|
upExgMsgHistoryLocation.setInferiorPlatformId(inferiorPlatformId);
|
upExgMsgHistoryLocation.setCreateTime(LocalDateTime.now());
|
upExgMsgHistoryLocationDao.save(upExgMsgHistoryLocation);
|
}
|
|
|
/**
|
* 上报驾驶员身份识别信息应答
|
*
|
* @param ctx
|
* @param inferiorPlatformId
|
* @param exgMsg
|
*/
|
public void up_exg_msg_report_driver_info_ack(ChannelHandlerContext ctx, int inferiorPlatformId, UPExgMsg exgMsg) {
|
UPExgMsgReportDriverInfoAck upExgMsgReportDriverInfoAck = new UPExgMsgReportDriverInfoAck().decode(exgMsg);
|
upExgMsgReportDriverInfoAck.setInferiorPlatformId(inferiorPlatformId);
|
upExgMsgReportDriverInfoAck.setCreateTime(LocalDateTime.now());
|
upExgMsgReportDriverInfoAckDao.save(upExgMsgReportDriverInfoAck);
|
}
|
|
|
/**
|
* 上报车辆电子运单应答
|
*
|
* @param ctx
|
* @param inferiorPlatformId
|
* @param exgMsg
|
*/
|
public void up_exg_msg_take_ewaybill_ack(ChannelHandlerContext ctx, int inferiorPlatformId, UPExgMsg exgMsg) {
|
UPExgMsgTakeEwayBillAck upExgMsgTakeEwayBillAck = new UPExgMsgTakeEwayBillAck().decode(exgMsg);
|
upExgMsgTakeEwayBillAck.setInferiorPlatformId(inferiorPlatformId);
|
upExgMsgTakeEwayBillAck.setCreateTime(LocalDateTime.now());
|
upExgMsgTakeEwayBillAckDao.save(upExgMsgTakeEwayBillAck);
|
}
|
|
}
|