package com.hikvision.cloud.iep.network.task; import java.sql.Timestamp; import java.util.ArrayList; import java.util.List; import javax.annotation.Resource; import io.netty.channel.ChannelHandlerContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import com.google.protobuf.ByteString; import com.hikvision.cloud.fastdfs.service.IUploadService; import com.hikvision.cloud.iep.network.ResultCodeType; import com.hikvision.cloud.iep.network.constant.Constants; import com.hikvision.cloud.iep.network.entity.PasscarInfo; import com.hikvision.cloud.iep.network.netty.cache.CacheService; import com.hikvision.cloud.iep.network.netty.handle.TcpServerHandler; import com.hikvision.cloud.iep.network.pb.CommunicationProto.CommandType; import com.hikvision.cloud.iep.network.pb.CommunicationProto.PassCarMsg; import com.hikvision.cloud.iep.network.pb.CommunicationProto.PassCarRecord; import com.hikvision.cloud.iep.network.pb.CommunicationProto.ProtocolMessage; import com.hikvision.cloud.iep.network.service.IDbOperService; @Component("passcarTask") @Scope("prototype") public class PasscarTask implements Runnable { private static Logger logger = LoggerFactory.getLogger(PasscarTask.class); private ChannelHandlerContext ctx; private PassCarMsg passCarMsg; private long seqId; @Resource TcpServerHandler tcpServerHandler; @Resource IUploadService uploadService; @Resource IDbOperService dbOperServiceImpl; @Resource CacheService cacheService; public PasscarTask setPassCarMsg(PassCarMsg passCarMsg) { this.passCarMsg = passCarMsg; return this; } public PasscarTask setCtx(ChannelHandlerContext ctx) { this.ctx = ctx; return this; } public PasscarTask setSeqId(long seqId) { this.seqId = seqId; return this; } @Override public void run() { String gatewayNO = passCarMsg.getGatewayNO(); logger.info("过车消息请求, 网关的编号:" + gatewayNO + " 消息的序号:" + seqId); int msgstatus = ResultCodeType.SUCCESS.value(); ListpassCarRecordList = passCarMsg.getPassCarRecordList(); try { if (passCarRecordList != null && passCarRecordList.size() > 0) { PasscarInfo passcarInfo = null; List passcarInfoList = new ArrayList (); String group = cacheService.findGroupByType(Constants.PASSCAR_TYPE); for (PassCarRecord passCarRecord : passCarRecordList) { passcarInfo = new PasscarInfo(); ByteString imgcardData = passCarRecord.getCardNumPicData(); // 车牌图片 if (imgcardData != null && imgcardData.size() > 0) { byte[] imgcardBuf = imgcardData.toByteArray(); String imgcardName = passCarRecord.getClientId() + ".jpg"; String imgcardURL = uploadService.upLoad(group,imgcardBuf, imgcardName); passcarInfo.setImgcardURL(imgcardURL); } ByteString imgCarData = passCarRecord.getCarPicData(); //车辆图片 if (imgCarData != null && imgCarData.size() > 0) { byte[] imgCarBuf = imgCarData.toByteArray(); String imgCarName = passCarRecord.getClientId() + ".jpg"; String imgCarURL = uploadService.upLoad(group,imgCarBuf, imgCarName); passcarInfo.setImgURL(imgCarURL); } passcarInfo.setClientId(passCarRecord.getClientId()); passcarInfo.setCardNum(passCarRecord.getCardNum()); passcarInfo.setCardNo(passCarRecord.getCardNo()); passcarInfo.setPasstime(new Timestamp(passCarRecord.getCrossTime()/1000*1000)); passcarInfo.setParkNo(passCarRecord.getParkNo()); passcarInfo.setCarColor(passCarRecord.getCarColor().getNumber()); passcarInfo.setCarType(passCarRecord.getCarType().getNumber()); passcarInfo.setDirection(passCarRecord.getCarDirection().getNumber()); passcarInfo.setUserName(passCarRecord.getUserName()); passcarInfo.setParkingType(passCarRecord.getParkingType()); passcarInfoList.add(passcarInfo); } int size = dbOperServiceImpl.savePasscarList(passcarInfoList, gatewayNO, Constants.PASSCARTABLENAME); logger.info("过车消息存储成功, 网关的编号:" + gatewayNO + " 消息的序号:" + seqId + " size:" + size+",收到条数:"+passCarRecordList.size()); } } catch (Exception e) { msgstatus = ResultCodeType.FAIL.value(); logger.info("过车消息存储失败, 网关的编号:" + gatewayNO + " 消息的序号:" + seqId + " 异常原因: " + e ); } ProtocolMessage protoMsg = tcpServerHandler.buildResponse(CommandType.PASSCAR_RESP, msgstatus, seqId); logger.info("过车消息响应序号:" + seqId + ",网关编号: " + gatewayNO +",状态:"+msgstatus); ctx.writeAndFlush(protoMsg); } }