Пример #1
0
        /// <summary> 
        /// 分析GPS数据包
        /// </summary>
        /// <param name="buffer">收到的原始数据</param>
        /// <param name="responseList">应答数据</param>
        /// <param name="fullGpsCode">解析后加前缀的GPSCode</param>
        /// <param name="sessionID">会话ID:IP地址+端口号</param>
        /// <returns></returns>
        public GPSDataEntity Parse(byte[] buffer, out List<byte[]> responseList, out string fullGpsCode, string sessionID)
        {
            //传进去赋值,必须在外面创建对象
            GPSDataEntity dataEntity = null;
            responseList = new List<byte[]>();
            string strPicFile = "";
            //GPRS信息帧数据
            GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame gprsFrame = new GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame();
            gprsFrame.ReadBuffer(buffer);
            if (!gprsFrame.IsValidData)
            {
                fullGpsCode = "";
                return null;
            }
            fullGpsCode = gprsFrame.GPSCode;
            //将连接加入关系表
            if (!string.IsNullOrEmpty(fullGpsCode))
            {
                OnAddConnectionEvent(fullGpsCode, sessionID);
            }

            if (gprsFrame.IsMutiPackage)
            {

                byte[] bSerialNum = gprsFrame.SerialNum;
                gprsFrame.InitData();

                if (gprsFrame.MsgID == BuBiaoMsgIdType.MediaDataReport)
                {
                    //上传一帧,保存一帧
                    if (gprsFrame.PackageIndex == 1)
                    {
                        //第一帧已包括图片信息定位信息
                        MediaDataReport aMediaDataReport = new MediaDataReport();
                        aMediaDataReport.dataBuffer = gprsFrame.Data;
                        aMediaDataReport.ReadBuffer(gprsFrame.DataLength);

                        dataEntity = new GPSDataEntity();
                        GetGPSDataEntity(fullGpsCode, aMediaDataReport.PhotoLocation, ref dataEntity);

                        WritePicFile(fullGpsCode, aMediaDataReport.ChannelID.ToString(), aMediaDataReport.Data, aMediaDataReport.MediaFomat, aMediaDataReport.EventCode, FileMode.OpenOrCreate, ref dataEntity, ref strPicFile);

                        if (dicCurPicCameraId.ContainsKey(fullGpsCode))
                        {
                            dicCurPicCameraId.Remove(fullGpsCode);
                        }
                        dicCurPicCameraId.Add(fullGpsCode, strPicFile);
                    }
                    else
                    {
                        if (dicCurPicCameraId.ContainsKey(fullGpsCode))
                        {
                            strPicFile = dicCurPicCameraId[fullGpsCode];
                            WritePicFile(strPicFile, gprsFrame.Data, FileMode.Append);
                            dataEntity = null;
                        }
                    }
                }


                Logger.Trace("[部标:分包消息]" + fullGpsCode + "  " + string.Format("消息类型:{0}总包数:{1}, 包序号{2}", gprsFrame.MsgID.ToString(), gprsFrame.PackageCount, gprsFrame.PackageIndex));
                if (_LongMsgProcessor.AddFrame(ref gprsFrame) == false)
                {
                    //最后一包不发送通用应答
                    TMLOrPFCommonRsp PCMPackage = GetTMLOrPFCommonRsp(bSerialNum, BuBiaoMsgIdType.MediaDataReport, 0);
                    AddSerialNum(fullGpsCode);
                    GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame gprsFrameMPackage = new GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame(BuBiaoMsgIdType.PFCommonRsp, false, EncryptType.NoEncrypt
                        , Convert.ToByte(PCMPackage.getLength()), gprsFrame.TMLPhoneNumByte
                        , BuBiaoData.LocalByteToNetByteOrder(BitConverter.GetBytes(GetSerialNum(fullGpsCode))), 0, 0, PCMPackage.dataBuffer);
                    responseList.Add(gprsFrameMPackage.WriteBuffer());

                    return dataEntity;
                }

            }


            //转义之后的数据帧
            byte[] sendBuffer = null;

            switch (gprsFrame.MsgID)
            {
                case BuBiaoMsgIdType.BSJBlindZoneUpload:
                    //博实结盲区补传,跟厂家确认需要给应答,否则会导致终端重发
                    TMLOrPFCommonRsp PCBSJRsp = GetTMLOrPFCommonRsp(gprsFrame.SerialNum, BuBiaoMsgIdType.BSJBlindZoneUpload, 0);
                    AddSerialNum(fullGpsCode); ;
                    GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame gprsFrameBSJ = new GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame(BuBiaoMsgIdType.PFCommonRsp, false, EncryptType.NoEncrypt
                        , Convert.ToByte(PCBSJRsp.getLength()), gprsFrame.TMLPhoneNumByte
                        , BuBiaoData.LocalByteToNetByteOrder(BitConverter.GetBytes(GetSerialNum(fullGpsCode))), 0, 0, PCBSJRsp.dataBuffer);

                    responseList.Add(gprsFrameBSJ.WriteBuffer());

                    break;

                //终端通用应答
                case BuBiaoMsgIdType.TMLCommonRsp:
                    TMLOrPFCommonRsp TCRsp = new TMLOrPFCommonRsp();
                    TCRsp.dataBuffer = gprsFrame.Data;
                    TCRsp.ReadBuffer();

                    //GPSGatewayServer.DataAccess.FetchData.GetInstance().SaveBubiaoCommonRsp(TCRsp,fullGpsCode);
                    //base.OnDebugDataEvent(base.TransfersType.ToString(), fullGpsCode, "[通用应答] 消息类型:" + TCRsp.RspID.ToString() + " 消息流水号:" + TCRsp.SerialNum.ToString() + " 结果:" + TCRsp.Result.ToString());                    
                    Logger.Trace("[部标:通用应答]" + fullGpsCode + "  " + string.Format("消息类型:{0},消息流水号:{1}, 结果{2}", TCRsp.RspID.ToString(), TCRsp.SerialNum.ToString(), TCRsp.Result.ToString()));
                    break;
                //提问应答消息
                case BuBiaoMsgIdType.TMLAnwerQuestion:
                    TMLAnswerQuestion aTMLAnswerQuestion = new TMLAnswerQuestion();
                    aTMLAnswerQuestion.ReadBuffer(gprsFrame.Data);
                    //GPSGatewayServer.DataAccess.FetchData.GetInstance().UpdateBubiaoQuestion(aTMLAnswerQuestion, fullGpsCode);

                    TMLOrPFCommonRsp PCAnswerRsp = GetTMLOrPFCommonRsp(gprsFrame.SerialNum, BuBiaoMsgIdType.TMLAnwerQuestion, 0);
                    AddSerialNum(fullGpsCode);
                    GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame gprsFrameAnswer = new GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame(BuBiaoMsgIdType.PFCommonRsp, false, EncryptType.NoEncrypt
                        , Convert.ToByte(PCAnswerRsp.getLength()), gprsFrame.TMLPhoneNumByte
                        , BuBiaoData.LocalByteToNetByteOrder(BitConverter.GetBytes(GetSerialNum(fullGpsCode))), 0, 0, PCAnswerRsp.dataBuffer);
                    responseList.Add(gprsFrameAnswer.WriteBuffer());

                    Logger.Trace("[部标:提问应答]" + fullGpsCode + "  " + gprsFrame.Data[0]);
                    break;
                //事件上报
                case BuBiaoMsgIdType.TMLEventReport:
                    //base.OnDebugDataEvent(base.TransfersType.ToString(), fullGpsCode, "[事件报告]:" + gprsFrame.Data[0]);
                    int iEventId = (int)gprsFrame.Data[0];
                    //GPSGatewayServer.DataAccess.FetchData.GetInstance().SaveEventReport(iEventId, fullGpsCode);

                    TMLOrPFCommonRsp PCEventRsp = GetTMLOrPFCommonRsp(gprsFrame.SerialNum, BuBiaoMsgIdType.TMLEventReport, 0);
                    AddSerialNum(fullGpsCode);
                    GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame gprsFrameEvent = new GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame(BuBiaoMsgIdType.PFCommonRsp, false, EncryptType.NoEncrypt
                        , Convert.ToByte(PCEventRsp.getLength()), gprsFrame.TMLPhoneNumByte
                        , BuBiaoData.LocalByteToNetByteOrder(BitConverter.GetBytes(GetSerialNum(fullGpsCode))), 0, 0, PCEventRsp.dataBuffer);
                    responseList.Add(gprsFrameEvent.WriteBuffer());

                    Logger.Trace("[部标:事件报告]" + fullGpsCode + "  " + gprsFrame.Data[0]);
                    break;
                //车辆控制应答
                case BuBiaoMsgIdType.TMLVehicleControlRsp:
                    byte[] RspSerialNum = new byte[2];
                    Array.Copy(gprsFrame.Data, 0, RspSerialNum, 0, 2);
                    UInt16 iSerialNum = BitConverter.ToUInt16(BuBiaoData.LocalByteToNetByteOrder(RspSerialNum), 0);

                    byte[] locData = new byte[gprsFrame.Data.Length - 2];
                    Array.Copy(gprsFrame.Data, 2, locData, 0, locData.Length);
                    LocationReport VC_LR = new LocationReport();
                    VC_LR.ReadBuffer(locData, locData.Length);

                    dataEntity = new GPSDataEntity();
                    GetGPSDataEntity(fullGpsCode, VC_LR, ref dataEntity);
                    TMLOrPFCommonRsp TCVehicle = new TMLOrPFCommonRsp();
                    TCVehicle.RspID = BuBiaoMsgIdType.TMLVehicleControl;
                    TCVehicle.SerialNum = iSerialNum;
                    TCVehicle.Result = 0;
                    //GPSGatewayServer.DataAccess.FetchData.GetInstance().SaveBubiaoCommonRsp(TCVehicle, fullGpsCode);

                    TMLOrPFCommonRsp PCControlRsp = GetTMLOrPFCommonRsp(gprsFrame.SerialNum, BuBiaoMsgIdType.TMLVehicleControlRsp, 0);
                    AddSerialNum(fullGpsCode);
                    GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame gprsFrameControl = new GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame(BuBiaoMsgIdType.PFCommonRsp, false, EncryptType.NoEncrypt
                        , Convert.ToByte(PCControlRsp.getLength()), gprsFrame.TMLPhoneNumByte
                        , BuBiaoData.LocalByteToNetByteOrder(BitConverter.GetBytes(GetSerialNum(fullGpsCode))), 0, 0, PCControlRsp.dataBuffer);
                    responseList.Add(gprsFrameControl.WriteBuffer());

                    //base.OnDebugDataEvent(base.TransfersType.ToString(), fullGpsCode, "[车辆控制应答]:" + iSerialNum.ToString());
                    Logger.Trace("[部标:车辆控制应答]" + fullGpsCode + "  " + iSerialNum.ToString());
                    break;
                //信息点播或取消
                case BuBiaoMsgIdType.TMLInfoPlayOrCancel:
                    TMLMediaPlayOrCancel TCPc = new TMLMediaPlayOrCancel();
                    TCPc.dataBuffer = gprsFrame.Data;
                    TCPc.ReadBuffer();
                    //GPSGatewayServer.DataAccess.FetchData.GetInstance().UpdateInfoPlay(TCPc, fullGpsCode);                                        

                    TMLOrPFCommonRsp PCInfoPlayRsp = GetTMLOrPFCommonRsp(gprsFrame.SerialNum, BuBiaoMsgIdType.TMLInfoPlayOrCancel, 0);
                    AddSerialNum(fullGpsCode);
                    GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame gprsFrameInfoPlay = new GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame(BuBiaoMsgIdType.PFCommonRsp, false, EncryptType.NoEncrypt
                        , Convert.ToByte(PCInfoPlayRsp.getLength()), gprsFrame.TMLPhoneNumByte
                        , BuBiaoData.LocalByteToNetByteOrder(BitConverter.GetBytes(GetSerialNum(fullGpsCode))), 0, 0, PCInfoPlayRsp.dataBuffer);
                    responseList.Add(gprsFrameInfoPlay.WriteBuffer());

                    //Add2InfoService(TCPc, fullGpsCode);
                    Logger.Trace("[部标:信息点播或取消]" + fullGpsCode + "  " + string.Format("消息类型:{0},flag:{1}", TCPc.InfoType.ToString(), TCPc.InfoFlag.ToString()));
                    break;
                //心跳
                case BuBiaoMsgIdType.TMLKeepAlive:
                    TMLOrPFCommonRsp PCHeartRsp = GetTMLOrPFCommonRsp(gprsFrame.SerialNum, BuBiaoMsgIdType.TMLKeepAlive, 0);
                    AddSerialNum(fullGpsCode); ;
                    GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame gprsFrameHeart = new GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame(BuBiaoMsgIdType.PFCommonRsp, false, EncryptType.NoEncrypt
                        , Convert.ToByte(PCHeartRsp.getLength()), gprsFrame.TMLPhoneNumByte
                        , BuBiaoData.LocalByteToNetByteOrder(BitConverter.GetBytes(GetSerialNum(fullGpsCode))), 0, 0, PCHeartRsp.dataBuffer);
                    responseList.Add(gprsFrameHeart.WriteBuffer());
                    break;
                //终端注册
                case BuBiaoMsgIdType.TMLRegisterReq:
                    //解析终端注册消息
                    //if (!dicDeviceHasRegiste.ContainsKey(fullGpsCode))
                    //{
                    //    dicDeviceHasRegiste.Add(fullGpsCode, 0);
                    //}

                    //byte hasRegiste = dicDeviceHasRegiste[fullGpsCode];

                    TMLRegisterFrameReq RstReq = new TMLRegisterFrameReq();
                    RstReq.ReadBuffer(gprsFrame.Data, gprsFrame.DataLength);

                    Logger.Trace("终端注册(Device):" + RstReq.LicenceNum + " " + RstReq.DeviceId);

                    TMLRegisterFrameRsp RstRsp = new TMLRegisterFrameRsp();
                    RstRsp.SerialNumByte = gprsFrame.SerialNum;
                    //0--成功 1--车辆已注册 2--数据库无此车辆 3--终端已注册 4--数据库无此终端 
                    RstRsp.Result = 0;
                    RstRsp.Akey = gprsFrame.GPSCode;

                    //if (CheckDeviceIsVaild(fullGpsCode, RstReq.LicenceNum, RstReq.DeviceId))
                    //{
                    //    if (hasRegiste == 1)
                    //    {
                    //        //已经注册                        
                    //        RstRsp.Result = 3;
                    //    }
                    //    else
                    //    {
                    //        RstRsp.Result = 0;
                    //        RstRsp.Akey = gprsFrame.GPSCode;
                    //        dicDeviceHasRegiste[fullGpsCode] = 1;

                    //        lock (dicNeedSaveDeviceRegiste)
                    //        {
                    //            if (dicNeedSaveDeviceRegiste.ContainsKey(fullGpsCode))
                    //                dicNeedSaveDeviceRegiste.Remove(fullGpsCode);
                    //            dicNeedSaveDeviceRegiste.Add(fullGpsCode, RstReq);
                    //        }
                    //    }
                    //}
                    //else
                    //{
                    //        RstRsp.Result = 4;
                    //}

                    RstRsp.WriteBuffer();
                    AddSerialNum(fullGpsCode);
                    GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame gprsFrameSendRstRsp = new GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame(BuBiaoMsgIdType.TMLRegisterRsp, false, EncryptType.NoEncrypt
                        , Convert.ToByte(RstRsp.getLength()), gprsFrame.TMLPhoneNumByte
                        , BuBiaoData.LocalByteToNetByteOrder(BitConverter.GetBytes(GetSerialNum(fullGpsCode))), 0, 0, RstRsp.dataBuffer);
                    responseList.Add(gprsFrameSendRstRsp.WriteBuffer());

                    Logger.Trace("[部标:终端注册]" + fullGpsCode + " " + RstRsp.Result);
                    break;
                //终端注销
                case BuBiaoMsgIdType.TMLCancelReq:
                    TMLOrPFCommonRsp PCCancelRsp = GetTMLOrPFCommonRsp(gprsFrame.SerialNum, BuBiaoMsgIdType.TMLCancelReq, 0);
                    AddSerialNum(fullGpsCode); ;
                    GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame gprsFrameCancel = new GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame(BuBiaoMsgIdType.PFCommonRsp, false, EncryptType.NoEncrypt
                        , Convert.ToByte(PCCancelRsp.getLength()), gprsFrame.TMLPhoneNumByte
                        , BuBiaoData.LocalByteToNetByteOrder(BitConverter.GetBytes(GetSerialNum(fullGpsCode))), 0, 0, PCCancelRsp.dataBuffer);
                    responseList.Add(gprsFrameCancel.WriteBuffer());

                    if (dicDeviceHasRegiste.ContainsKey(fullGpsCode))
                    {
                        dicDeviceHasRegiste.Remove(fullGpsCode);
                    }
                    //GPSGatewayServer.DataAccess.FetchData.GetInstance().RemoveDeviceRegInfo(fullGpsCode);
                    Logger.Trace("[部标:终端注销]" + fullGpsCode);
                    break;
                //数据透传
                case BuBiaoMsgIdType.DataTransparentUp:
                    TransparentTranData aTransparentTranData = new TransparentTranData();
                    aTransparentTranData.dataBuffer = gprsFrame.Data;
                    aTransparentTranData.ReadBuffer();
                    //GPSGatewayServer.DataAccess.FetchData.GetInstance().SaveTransparentTranInfo(aTransparentTranData,fullGpsCode);

                    TMLOrPFCommonRsp PCTTRsp = GetTMLOrPFCommonRsp(gprsFrame.SerialNum, BuBiaoMsgIdType.DataTransparentUp, 0);
                    AddSerialNum(fullGpsCode); ;
                    GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame gprsFrameTT = new GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame(BuBiaoMsgIdType.PFCommonRsp, false, EncryptType.NoEncrypt
                        , Convert.ToByte(PCTTRsp.getLength()), gprsFrame.TMLPhoneNumByte
                        , BuBiaoData.LocalByteToNetByteOrder(BitConverter.GetBytes(GetSerialNum(fullGpsCode))), 0, 0, PCTTRsp.dataBuffer);
                    responseList.Add(gprsFrameTT.WriteBuffer());
                    Logger.Trace("[部标:数据透传]" + aTransparentTranData.DataType);

                    break;
                //鉴权
                case BuBiaoMsgIdType.TMLAuthenticationReq:

                    TMLAuthenticationReq aTMLAuthenticationReq = new TMLAuthenticationReq();
                    aTMLAuthenticationReq.ReadBuffer(gprsFrame.Data);
                    //取得鉴权码,去判断是否已经合法车辆
                    byte bResult = 0;

                    //改为从内存鉴权
                    //正式环境暂不认证
                    //if (dicPlatformAllVehilceInfo.ContainsKey(aTMLAuthenticationReq.Akey))
                    //    bResult = 0;
                    //else
                    //    bResult = 1;
                    TMLOrPFCommonRsp PCRsp = GetTMLOrPFCommonRsp(gprsFrame.SerialNum, BuBiaoMsgIdType.TMLAuthenticationReq, bResult);
                    AddSerialNum(fullGpsCode);
                    GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame gprsFrameSend = new GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame(BuBiaoMsgIdType.PFCommonRsp, false, EncryptType.NoEncrypt
                        , Convert.ToByte(PCRsp.getLength()), gprsFrame.TMLPhoneNumByte
                        , BuBiaoData.LocalByteToNetByteOrder(BitConverter.GetBytes(GetSerialNum(fullGpsCode))), 0, 0, PCRsp.dataBuffer);
                    responseList.Add(gprsFrameSend.WriteBuffer());

                    //设置参数默认值,统一通过终端参数设置

                    SetTMLPara setPara = new SetTMLPara();
                    List<TMLParaItem> paraList = new List<TMLParaItem>();
                    TMLParaItem item;
                    int BufferLen = 0;
                    //终端心跳发送间隔,单位为秒(s)
                    //item = new TMLParaItem(ParaIDType.TMLKeepAlive, 4, BuBiaoData.LocalByteToNetByteOrder(BitConverter.GetBytes(0x0000003c)));
                    //BufferLen += item.GetItemLen();
                    //paraList.Add(item);

                    //位置汇报策略
                    item = new TMLParaItem(ParaIDType.LocationReportStrategy, 4, BuBiaoData.LocalByteToNetByteOrder(BitConverter.GetBytes(0)));
                    BufferLen += item.GetItemLen();
                    paraList.Add(item);

                    //位置汇报方案
                    item = new TMLParaItem(ParaIDType.LocationReportProgram, 4, BuBiaoData.LocalByteToNetByteOrder(BitConverter.GetBytes(0)));
                    BufferLen += item.GetItemLen();
                    paraList.Add(item);

                    //缺省时间汇报间隔
                    item = new TMLParaItem(ParaIDType.DefaultReportInterval, 4, BuBiaoData.LocalByteToNetByteOrder(BitConverter.GetBytes(15)));
                    BufferLen += item.GetItemLen();
                    paraList.Add(item);

                    item = new TMLParaItem(ParaIDType.SleepReportInterval, 4, BuBiaoData.LocalByteToNetByteOrder(BitConverter.GetBytes(60)));
                    BufferLen += item.GetItemLen();
                    paraList.Add(item);

                    item = new TMLParaItem(ParaIDType.DriverNoLoginReportInterval, 4, BuBiaoData.LocalByteToNetByteOrder(BitConverter.GetBytes(60))); ;
                    BufferLen += item.GetItemLen();
                    paraList.Add(item);

                    setPara.AddTMLParaItem(paraList, BufferLen);
                    AddSerialNum(fullGpsCode);
                    gprsFrameSend = new GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame(BuBiaoMsgIdType.SetTMLPara, false, EncryptType.NoEncrypt, Convert.ToUInt16(setPara.dataBuffer.Length)
                        , gprsFrame.TMLPhoneNumByte, BuBiaoData.LocalByteToNetByteOrder(BitConverter.GetBytes(GetSerialNum(fullGpsCode))), 0, 0, setPara.dataBuffer);

                    byte[] sendbuffer = gprsFrameSend.WriteBuffer();
                    responseList.Add(sendbuffer);

                    Logger.Trace("[部标:终端签权]" + fullGpsCode + "  " + bResult);
                    break;
                //查询终端参数应答
                case BuBiaoMsgIdType.QueryTMLParaRsp:

                    CmdData cmdData = new CmdData();
                    cmdData.Data.Cmd = ControlCmdType.QueryTerminalParamRsp;
                    cmdData.DataBuffer = gprsFrame.Data;
                    cmdData.Data.DataLen = (ushort)gprsFrame.Data.Length;

                    byte[] RspBuffer = new byte[cmdData.GetLenth()];
                    cmdData.WriteBuffer(0, RspBuffer);


                    //生成公共应答
                    //QueryTMLPara tmlparam = new QueryTMLPara();
                    //tmlparam.ReadBuffer(gprsFrame.Data);
                    //TMLOrPFCommonRsp TCQuery = new TMLOrPFCommonRsp();
                    //TCQuery.RspID = BuBiaoMsgIdType.QueryTMLParaReq;
                    //TCQuery.SerialNum = tmlparam.RspSerialNumber;
                    //TCQuery.Result = 0;
                    //GPSGatewayServer.DataAccess.FetchData.GetInstance().SaveBubiaoCommonRsp(TCQuery, fullGpsCode);

                    TMLOrPFCommonRsp PCQuery = GetTMLOrPFCommonRsp(gprsFrame.SerialNum, BuBiaoMsgIdType.QueryTMLParaRsp, 0);
                    AddSerialNum(fullGpsCode);
                    GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame gprsFrameQuery = new GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame(BuBiaoMsgIdType.PFCommonRsp, false, EncryptType.NoEncrypt
                        , Convert.ToByte(PCQuery.getLength()), gprsFrame.TMLPhoneNumByte
                        , BuBiaoData.LocalByteToNetByteOrder(BitConverter.GetBytes(GetSerialNum(fullGpsCode))), 0, 0, PCQuery.dataBuffer);
                    responseList.Add(gprsFrameQuery.WriteBuffer());

                    base.OnPostAckMsgHandlerEvent(fullGpsCode, RspBuffer);
                    //base.OnDebugDataEvent(base.TransfersType.ToString(), fullGpsCode, "查询终端参数应答" + gprsFrame.DataLength);
                    Logger.Trace("[部标:查询终端参数应答]" + gprsFrame.DataLength);

                    break;
                case BuBiaoMsgIdType.LocationReport:

                    LocationReport LR = new LocationReport();
                    LR.ReadBuffer(gprsFrame.Data, gprsFrame.DataLength);

                    dataEntity = new GPSDataEntity();
                    GetGPSDataEntity(fullGpsCode, LR, ref dataEntity);
                    DealWithAlarm(LR, gprsFrame, ref responseList);

                    //TO DO

                    break;
                //驾驶员上报
                case BuBiaoMsgIdType.DriverInfoReport:
                    DriverInfoReport aDriverInfoReport = new DriverInfoReport();
                    aDriverInfoReport.dataBuffer = gprsFrame.Data;
                    aDriverInfoReport.ReadBuffer();
                    //GPSGatewayServer.DataAccess.FetchData.GetInstance().SaveDriverReport(aDriverInfoReport, fullGpsCode);

                    TMLOrPFCommonRsp PCVehicleRsp = GetTMLOrPFCommonRsp(gprsFrame.SerialNum, BuBiaoMsgIdType.DriverInfoReport, 0);
                    AddSerialNum(fullGpsCode);
                    GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame gprsFrameVehilce = new GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame(BuBiaoMsgIdType.PFCommonRsp, false, EncryptType.NoEncrypt
                        , Convert.ToByte(PCVehicleRsp.getLength()), gprsFrame.TMLPhoneNumByte
                        , BuBiaoData.LocalByteToNetByteOrder(BitConverter.GetBytes(GetSerialNum(fullGpsCode))), 0, 0, PCVehicleRsp.dataBuffer);
                    responseList.Add(gprsFrameVehilce.WriteBuffer());
                    Logger.Trace("[部标:驾驶员身份上报]" + fullGpsCode + "  " + aDriverInfoReport.DriverName);

                    break;
                //电子运单上报
                case BuBiaoMsgIdType.ConsignOrderReport:
                    ConsignOrderReport aConsignOrderReport = new ConsignOrderReport();
                    aConsignOrderReport.dataBuffer = gprsFrame.Data;
                    aConsignOrderReport.ReadBuffer();
                    //GPSGatewayServer.DataAccess.FetchData.GetInstance().SaveETransForm(aConsignOrderReport, fullGpsCode);

                    TMLOrPFCommonRsp PCOrderRsp = GetTMLOrPFCommonRsp(gprsFrame.SerialNum, BuBiaoMsgIdType.ConsignOrderReport, 0);
                    AddSerialNum(fullGpsCode);
                    GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame gprsFrameOrder = new GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame(BuBiaoMsgIdType.PFCommonRsp, false, EncryptType.NoEncrypt
                        , Convert.ToByte(PCOrderRsp.getLength()), gprsFrame.TMLPhoneNumByte
                        , BuBiaoData.LocalByteToNetByteOrder(BitConverter.GetBytes(GetSerialNum(fullGpsCode))), 0, 0, PCOrderRsp.dataBuffer);
                    responseList.Add(gprsFrameOrder.WriteBuffer());

                    Logger.Trace("[部标:电子运单上报]" + fullGpsCode);
                    break;
                //多媒体事件信息上传
                case BuBiaoMsgIdType.MediaEventInfoReport:
                    MediaEventInfoReport aMediaEventInfoReport = new MediaEventInfoReport();
                    aMediaEventInfoReport.ReadBuffer(gprsFrame.Data);

                    TMLOrPFCommonRsp PCMediaReportRsp = GetTMLOrPFCommonRsp(gprsFrame.SerialNum, BuBiaoMsgIdType.MediaEventInfoReport, 0);
                    AddSerialNum(fullGpsCode);
                    GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame gprsFrameMediaReport = new GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame(BuBiaoMsgIdType.PFCommonRsp, false, EncryptType.NoEncrypt
                        , Convert.ToByte(PCMediaReportRsp.getLength()), gprsFrame.TMLPhoneNumByte
                        , BuBiaoData.LocalByteToNetByteOrder(BitConverter.GetBytes(GetSerialNum(fullGpsCode))), 0, 0, PCMediaReportRsp.dataBuffer);
                    responseList.Add(gprsFrameMediaReport.WriteBuffer());

                    //GPSGatewayServer.DataAccess.FetchData.GetInstance().SaveMediaEventInfo(aMediaEventInfoReport, fullGpsCode);
                    Logger.Trace("[部标:多媒体事件信息上传]" + fullGpsCode);
                    break;
                //多媒体数据检索应答
                case BuBiaoMsgIdType.MediaDataSearchRsp:

                    CmdData cmdData1 = new CmdData();
                    cmdData1.Data.Cmd = ControlCmdType.MediaDataSearchRsp;
                    cmdData1.DataBuffer = gprsFrame.Data;
                    cmdData1.Data.DataLen = (ushort)gprsFrame.Data.Length;

                    byte[] RspBuffer1 = new byte[cmdData1.GetLenth()];
                    cmdData1.WriteBuffer(0, RspBuffer1);
                    base.OnPostAckMsgHandlerEvent(fullGpsCode, RspBuffer1);


                    TMLOrPFCommonRsp PCMediaSearchRsp = GetTMLOrPFCommonRsp(gprsFrame.SerialNum, BuBiaoMsgIdType.MediaDataSearchRsp, 0);
                    AddSerialNum(fullGpsCode);
                    GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame gprsFrameMediaSearch = new GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame(BuBiaoMsgIdType.PFCommonRsp, false, EncryptType.NoEncrypt
                        , Convert.ToByte(PCMediaSearchRsp.getLength()), gprsFrame.TMLPhoneNumByte
                        , BuBiaoData.LocalByteToNetByteOrder(BitConverter.GetBytes(GetSerialNum(fullGpsCode))), 0, 0, PCMediaSearchRsp.dataBuffer);
                    responseList.Add(gprsFrameMediaSearch.WriteBuffer());

                    MediaDataSearchRsp aMediaDataSearchRsp = new MediaDataSearchRsp();
                    aMediaDataSearchRsp.ReadBuffer(gprsFrame.Data);
                    TMLOrPFCommonRsp TCDataSearch = new TMLOrPFCommonRsp();
                    TCDataSearch.RspID = BuBiaoMsgIdType.MediaDataSearch;
                    TCDataSearch.SerialNum = aMediaDataSearchRsp.SerialNumber;
                    TCDataSearch.Result = 0;
                    //GPSGatewayServer.DataAccess.FetchData.GetInstance().SaveBubiaoCommonRsp(TCDataSearch, fullGpsCode);

                    Logger.Trace("[部标:多媒体数据检索应答] " + fullGpsCode);

                    break;
                //多媒体数据上传
                case BuBiaoMsgIdType.MediaDataReport:
                    MediaDataReport aMediaDataReport = new MediaDataReport();
                    aMediaDataReport.dataBuffer = gprsFrame.Data;
                    aMediaDataReport.ReadBuffer(gprsFrame.DataLength);

                    //将图片数据写成图片文件存入指定位置
                    if (dicCurPicCameraId.ContainsKey(fullGpsCode))
                    {
                        strPicFile = dicCurPicCameraId[fullGpsCode];
                        WritePicFile(strPicFile, aMediaDataReport.Data, FileMode.OpenOrCreate);
                    }

                    //应答0x8800
                    AddSerialNum(fullGpsCode);
                    byte[] bMediaDataRsp = GetMediaDataReportRsp(aMediaDataReport, null);
                    GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame gprsFrameSend3 = new GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame(BuBiaoMsgIdType.MediaDataReportRsp, false, EncryptType.NoEncrypt
                        , Convert.ToByte(bMediaDataRsp.Length), gprsFrame.TMLPhoneNumByte
                        , BuBiaoData.LocalByteToNetByteOrder(BitConverter.GetBytes(GetSerialNum(fullGpsCode))), 0, 0, bMediaDataRsp);
                    responseList.Add(gprsFrameSend3.WriteBuffer());

                    //平台下发拍照指令
                    if (aMediaDataReport.EventCode == 0)
                    {
                        if (CheckTaskThread.GetInstance().PicTaskPool.ContainsKey(fullGpsCode))
                        {
                            if (CheckTaskThread.GetInstance().PicTaskPool[fullGpsCode].Count > 0)
                            {
                                TakePicTask task = CheckTaskThread.GetInstance().PicTaskPool[fullGpsCode].Peek();
                                if (task != null)
                                {
                                    task.Status = TakePicTaskStatus.LastPicFinished;
                                    CheckTaskThread.GetInstance().DoShoot(fullGpsCode, false);
                                    Logger.Trace("[拍照]完成,更改任务状态 ChannelID" + aMediaDataReport.ChannelID + "--" + task.CameraList[task.CurrentCameraIndex]);
                                }
                            }
                        }
                    }

                    Logger.Trace("[部标:多媒体上传完成]" + fullGpsCode);
                    break;
                //行驶记录上传
                case BuBiaoMsgIdType.TravelRecordReport:
                    TravelRecordReport aTravelRecReport = new TravelRecordReport();
                    aTravelRecReport.DataBuffer = gprsFrame.Data;
                    aTravelRecReport.ReadBuffer();
                    //GPSGatewayServer.DataAccess.FetchData.GetInstance().UpdateTravelRecordReport(aTravelRecReport, fullGpsCode);


                    TMLOrPFCommonRsp PCTravelRsp = GetTMLOrPFCommonRsp(gprsFrame.SerialNum, BuBiaoMsgIdType.TravelRecordReport, 0);
                    AddSerialNum(fullGpsCode);
                    GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame gprsFrameTravel = new GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame(BuBiaoMsgIdType.PFCommonRsp, false, EncryptType.NoEncrypt
                        , Convert.ToByte(PCTravelRsp.getLength()), gprsFrame.TMLPhoneNumByte
                        , BuBiaoData.LocalByteToNetByteOrder(BitConverter.GetBytes(GetSerialNum(fullGpsCode))), 0, 0, PCTravelRsp.dataBuffer);
                    responseList.Add(gprsFrameTravel.WriteBuffer());
                    Logger.Trace("[部标:行驶记录数据上报]" + fullGpsCode);
                    break;
                case BuBiaoMsgIdType.QueryPositionRsp:
                    QueryPositionRsp aQueryPositionRsp = new QueryPositionRsp();
                    aQueryPositionRsp.dataBuffer = gprsFrame.Data;
                    aQueryPositionRsp.ReadBuffer(gprsFrame.DataLength);
                    //FetchData.GetInstance().UpdateTMLResponse(fullGpsCode, aQueryPositionRsp.SerialNum, DateTime.Now, 0);

                    dataEntity = new GPSDataEntity();
                    GetGPSDataEntity(fullGpsCode, aQueryPositionRsp.LR, ref dataEntity);

                    TMLOrPFCommonRsp PCPositionRsp = GetTMLOrPFCommonRsp(gprsFrame.SerialNum, BuBiaoMsgIdType.QueryPositionRsp, 0);
                    AddSerialNum(fullGpsCode);
                    GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame gprsFramePosition = new GPSGatewayServer.DataModel.BuBiaoData.GPRSFrame(BuBiaoMsgIdType.PFCommonRsp, false, EncryptType.NoEncrypt
                        , Convert.ToByte(PCPositionRsp.getLength()), gprsFrame.TMLPhoneNumByte
                        , BuBiaoData.LocalByteToNetByteOrder(BitConverter.GetBytes(GetSerialNum(fullGpsCode))), 0, 0, PCPositionRsp.dataBuffer);
                    responseList.Add(gprsFramePosition.WriteBuffer());


                    Logger.Trace("[部标:点名]" + fullGpsCode);
                    //DealWithAlarm(aQueryPositionRsp.LR, gprsFrame, ref responseList);
                    break;

            }
            return dataEntity;
        }
Пример #2
0
        /// <summary>
        /// 构建多媒体上传数据应答
        /// </summary>
        /// <param name="report">多媒体数据</param>
        /// <param name="ResendPkgId">重传包列表</param>
        /// <returns></returns>
        public byte[] GetMediaDataReportRsp(MediaDataReport report, IList<UInt16> ResendPkgId)
        {
            byte[] bRet = null;

            if (ResendPkgId == null)
            {
                bRet = new byte[5];
                //byte[] tmpWriteBuffer = BitConverter.GetBytes(8888);
                byte[] tmpWriteBuffer = BitConverter.GetBytes(report.MediaID);
                Array.Copy(BuBiaoData.LocalByteToNetByteOrder(tmpWriteBuffer), 0, bRet, 0, 4);
                //Array.Copy(report.MediaIDByte, 0, bRet, 0, 4);
                bRet[4] = 0;
            }
            else
            {
                if (ResendPkgId.Count > 0)
                {
                    bRet = new byte[5 + (ResendPkgId.Count * 4)];
                    int iIndex = 0;
                    byte[] tmpWriteBuffer = BitConverter.GetBytes(report.MediaID);
                    Array.Copy(BuBiaoData.LocalByteToNetByteOrder(tmpWriteBuffer), 0, bRet, iIndex, 4);
                    iIndex += 4;

                    bRet[iIndex] = (byte)ResendPkgId.Count;
                    iIndex += 1;

                    for (int i = 0; i < ResendPkgId.Count; i++)
                    {
                        UInt32 MissPkgId = ResendPkgId[i];
                        tmpWriteBuffer = BitConverter.GetBytes(MissPkgId);
                        Array.Copy(BuBiaoData.LocalByteToNetByteOrder(tmpWriteBuffer), 0, bRet, iIndex, 4);
                        iIndex += 4;
                    }
                }
            }

            return bRet;
        }