Пример #1
0
        /// <summary>
        /// 根据LBS扩展信息,获得经纬度
        /// </summary>
        /// <param name="fullLBS"></param>
        /// <param name="position"></param>
        /// <returns></returns>
        private byte[] GetLbsPosition(KKS_FullLBS_Info fullLBS, ref CELL_Position_Info position)
        {
            byte[] bret = null;

            string strKey = fullLBS.MNC.ToString() + "#" + fullLBS.LAC.ToString();
            if (_MobileCellInfo.ContainsKey(strKey))
            {
                IDictionary<UInt32, CELL_Position_Info> cell_lac_info = _MobileCellInfo[strKey];
                if (cell_lac_info.ContainsKey(fullLBS.CellID))
                {
                    position = cell_lac_info[fullLBS.CellID];
                    bret = new byte[8];

                    int iLatitude = (int)(position.Latitude * 500 * 3600);
                    byte[] bLatitude = BitConverter.GetBytes(iLatitude);
                    Array.Copy(bLatitude, 0, bret, 0, 4);

                    int iLongitude = (int)(position.Longitude * 500 * 3600);
                    byte[] bLongitude = BitConverter.GetBytes(iLongitude);
                    Array.Copy(bLongitude, 0, bret, 4, 4);
                }
            }

            return bret;
        }
Пример #2
0
        public GPSDataEntity Parse(byte[] buffer, out List<byte[]> responseList, out string fullGpsCode, string sessionID)
        {
            GPSDataEntity dataEntity = null;
            responseList = new List<byte[]>();
            fullGpsCode = string.Empty;
            string debuggerLog = string.Empty;//测试期间用到的LOG,待KKS网关稳定后删除
            try
            {
                //GT02
                if (buffer != null && buffer[0] == 0x68 && buffer[1] == 0x68)
                {
                    KKSGT02Frame kksFrame = new KKSGT02Frame();
                    kksFrame.ReadBuffer(buffer);
                    if (!kksFrame.IsValidData)
                    {
                        return null;
                    }
                    fullGpsCode = base.CodePrefix + GetHexString((long)kksFrame.TerminalIMEI, 30);

                    if (string.IsNullOrEmpty(fullGpsCode))
                        return null;
                    //将连接加入关系表
                    OnAddConnectionEvent(fullGpsCode, sessionID);

                    switch (kksFrame.ProtocolNo)
                    {
                        case KKSProtocolNO.GPSInfo:
                            KKSGT02GPSInfo kksGpsInfo = new KKSGT02GPSInfo();
                            kksGpsInfo.ReadBuffer(kksFrame.Data);
                            dataEntity = GetGPSDataEntity4GT02(fullGpsCode, kksGpsInfo);
                            break;
                        case KKSProtocolNO.GPS_Caller_QueryInfo://心跳
                            KKSProtocolNO? lastProticol = GetLastDataProtocolNoByGpsCode(fullGpsCode);
                            if (lastProticol.HasValue && lastProticol.Value == KKSProtocolNO.GPS_Caller_QueryInfo)
                            {
                                dataEntity = GetPreGpsDataEntity(fullGpsCode);
                                if (dataEntity == null)
                                {
                                    Logger.Warn("收到的定位数据全是无效数据,或者没有从gps_currentinfo中取到" + fullGpsCode + "的数据");
                                }
                                else
                                {
                                    dataEntity = SetStatusValue4GPSDataEntity(dataEntity);
                                    AddCodeVehicleGpsData(fullGpsCode, dataEntity);
                                    dataEntity.GPSCode = fullGpsCode.Substring(4);
                                    _CodeVehicleGpsData[fullGpsCode] = dataEntity;
                                }
                            }
                            responseList.Add(GetResponseMsg4GT02());
                            break;
                        case KKSProtocolNO.GT02_GPS_Caller_QueryInfo:
                            {
                                KKSGT02_Mobile_QueryInfo mobileQuery = new KKSGT02_Mobile_QueryInfo();
                                mobileQuery.ReadBuffer(kksFrame.Data);
                                dataEntity = GetGPSDataEntity4GT02(fullGpsCode, mobileQuery.GPSInfo);
                                if (dataEntity != null)
                                {
                                    byte[] byteAddress = GetAddressByte(dataEntity, Address_PACK_Type.GPS_GetAddress, mobileQuery.Mobile, kksFrame.SerialNum);
                                    responseList.Add(GetResponseMsg(0x1A, byteAddress));
                                }
                            }
                            break;

                    }
                    UpdateLastProtocolNo(fullGpsCode, kksFrame.ProtocolNo);
                }
                //GT06
                else  if (buffer != null && buffer[0] == 0x78 && buffer[1] == 0x78)
                {
                    fullGpsCode = GetGPSCode(sessionID);
                    KKSFrame kksFrame = new KKSFrame();
                    kksFrame.ReadBuffer(buffer);
                    if (!kksFrame.IsValidData)
                    {
                        return null;
                    }

                    if (kksFrame.ProtocolNo == KKSProtocolNO.LoginReg)
                    {
                        KKSLogin kksLogin = new KKSLogin();
                        kksLogin.ReadBuffer(kksFrame.Data);
                        fullGpsCode = base.CodePrefix + GetHexString((long)kksLogin.TerminalIMEI, 30);

                        if (!string.IsNullOrEmpty(fullGpsCode))
                        {
                            AddCodeSessionTb(fullGpsCode, sessionID);
                            OnAddConnectionEvent(fullGpsCode, sessionID);
                            AddCodeGpsTypeRelation(fullGpsCode, kksLogin.HDType);
                            if (kksLogin.HDType == KKS_HDType.GT07 || kksLogin.HDType == KKS_HDType.GT06 || kksLogin.HDType==KKS_HDType.GT03)
                            {
                                UpdateLastProtocolNo(fullGpsCode, kksFrame.ProtocolNo);
                            }
                        }
                        responseList.Add(GetResponseMsg(0x01, kksFrame.SerialNum));                        
                    }
                    debuggerLog += "ProtocolNo:" + kksFrame.ProtocolNo.ToString();

                    if (!string.IsNullOrEmpty(fullGpsCode))
                    {
                        debuggerLog += "--fullGpsCode:" + fullGpsCode;
                        switch (kksFrame.ProtocolNo)
                        {
                            case KKSProtocolNO.StatusInfo://0x13
                                if (_CodeGpsTypeRelation.ContainsKey(fullGpsCode) && (_CodeGpsTypeRelation[fullGpsCode] == KKS_HDType.GT06 || _CodeGpsTypeRelation[fullGpsCode] == KKS_HDType.GT03))
                                {
                                    KKStatus status = new KKStatus();
                                    status.ReadBuffer(kksFrame.Data);

                                    AddCodeVehicleStatusTb(fullGpsCode, status);
                                    dataEntity = GetGPSDataEntity(fullGpsCode, status);
                                    UpdateLastProtocolNo(fullGpsCode, kksFrame.ProtocolNo);
                                }
                                else if (_CodeGpsTypeRelation.ContainsKey(fullGpsCode) && (_CodeGpsTypeRelation[fullGpsCode] == KKS_HDType.GS503 || _CodeGpsTypeRelation[fullGpsCode] == KKS_HDType.GK301))
                                {
                                    KKS_Mobile_Status status = new KKS_Mobile_Status();
                                    status.ReadBuffer(kksFrame.Data);
                                    if (status.LocationStatus == 1)
                                    {
                                        bool isPlunder = status.PlunderStatus == 100;
                                        dataEntity = GetGPSDataEntity(fullGpsCode, isPlunder);
                                    }
                                    else
                                    {
                                        Logger.Warn("手机终端收到心跳包,但是未定位");
                                    }
                                }
                                else if (_CodeGpsTypeRelation.ContainsKey(fullGpsCode) && _CodeGpsTypeRelation[fullGpsCode] == KKS_HDType.GT07)
                                {
                                    if (_LastReceiveDataProtocolNo[fullGpsCode] == KKSProtocolNO.StatusInfo)
                                    {
                                        if (_CodeVehicleGpsData.ContainsKey(fullGpsCode))
                                        {
                                            dataEntity = _CodeVehicleGpsData[fullGpsCode];
                                        }
                                        else
                                        {
                                            dataEntity = FetchData.GetInstance().GetLastGpsEntityByGpsCode(fullGpsCode);
                                        }
                                        if (dataEntity == null)
                                        {
                                            Logger.Warn("收到的定位数据全是无效数据,或者没有从gps_currentinfo中取到" + fullGpsCode + "的数据");
                                        }
                                        else
                                        {
                                            dataEntity = SetStatusValue4GPSDataEntity(dataEntity);
                                            AddCodeVehicleGpsData(fullGpsCode, dataEntity);
                                            dataEntity.GPSCode = fullGpsCode.Substring(4);
                                        }
                                    }
                                    else
                                    {
                                    }
                                    UpdateLastProtocolNo(fullGpsCode, kksFrame.ProtocolNo);
                                }
                                responseList.Add(GetResponseMsg(0x13, kksFrame.SerialNum));

                                break;
                            case KKSProtocolNO.GPSInfo://0x10
                                base.OnDebugDataEvent(base.TransfersType.ToString(), fullGpsCode, "收到GPS包");
                                if (_CodeGpsTypeRelation.ContainsKey(fullGpsCode) && (_CodeGpsTypeRelation[fullGpsCode] == KKS_HDType.GS503 || _CodeGpsTypeRelation[fullGpsCode] == KKS_HDType.GK301 || _CodeGpsTypeRelation[fullGpsCode] == KKS_HDType.GT03))
                                {
                                    KKSGPSInfo gpsInfo = new KKSGPSInfo();
                                    gpsInfo.ReadBuffer(kksFrame.Data);
                                    dataEntity = GetGPSDataEntity(fullGpsCode, gpsInfo);
                                    AddCodeVehicleGpsData(fullGpsCode, dataEntity);
                                    responseList.Add(GetResponseMsg(0x10, kksFrame.SerialNum));
                                }
                                break;
                            case KKSProtocolNO.LBSInfo://0x11
                                if (_CodeGpsTypeRelation.ContainsKey(fullGpsCode) && (_CodeGpsTypeRelation[fullGpsCode] == KKS_HDType.GS503 || _CodeGpsTypeRelation[fullGpsCode] == KKS_HDType.GK301 || _CodeGpsTypeRelation[fullGpsCode] == KKS_HDType.GT03))
                                {
                                    KKSLBSInfo lbsInfo = new KKSLBSInfo();
                                    lbsInfo.ReadBuffer(kksFrame.Data, LBS_PACK_Type.MovoLBS);
                                    CELL_Position_Info position = new CELL_Position_Info();
                                    byte[] bResponse = GetLbsPosition(lbsInfo, ref position);

                                    if (bResponse != null)
                                    {
                                        dataEntity = GetGPSDataEntity(fullGpsCode, lbsInfo.ReportTime, position, false);
                                        AddCodeVehicleGpsData(fullGpsCode, dataEntity);
                                    }
                                    responseList.Add(GetResponseMsg(0x11, kksFrame.SerialNum));
                                }
                                break;
                            case KKSProtocolNO.ET200_GPS_LBS_Info://0x22
                                KKSGPS_LBS_Info gps_lbs_info1 = new KKSGPS_LBS_Info();
                                gps_lbs_info1.ReadBuffer(kksFrame.Data);
                                dataEntity = GetGPSDataEntity(fullGpsCode, gps_lbs_info1.GPS);
                                dataEntity.ACCState = kksFrame.Data[kksFrame.Data.Length - 3];
                                UpdateLastProtocolNo(fullGpsCode, kksFrame.ProtocolNo);
                                responseList.Add(GetResponseMsg(0x22, kksFrame.SerialNum));
                                break;
                            case KKSProtocolNO.GPS_LBS_Info://0x12
                                KKSGPS_LBS_Info gps_lbs_info = new KKSGPS_LBS_Info();
                                gps_lbs_info.ReadBuffer(kksFrame.Data);
                                if (_CodeGpsTypeRelation.ContainsKey(fullGpsCode) && (_CodeGpsTypeRelation[fullGpsCode] == KKS_HDType.GT06 || _CodeGpsTypeRelation[fullGpsCode] == KKS_HDType.GT03))
                                {
                                    dataEntity = GetGPSDataEntity(fullGpsCode, gps_lbs_info.GPS, false);
                                    UpdateLastProtocolNo(fullGpsCode, kksFrame.ProtocolNo);
                                }
                                else if (_CodeGpsTypeRelation.ContainsKey(fullGpsCode) && _CodeGpsTypeRelation[fullGpsCode] == KKS_HDType.GT07)
                                {
                                    dataEntity = GetGPSDataEntity(fullGpsCode, gps_lbs_info.GPS);
                                    UpdateLastProtocolNo(fullGpsCode, kksFrame.ProtocolNo);
                                }
                                break;
                            case KKSProtocolNO.ET200_GPS_LBS_Status_Info://0x26
                                KKSMobile_GPS_LBS_StatusInfo gps_lbs_statusinfo1 = new KKSMobile_GPS_LBS_StatusInfo();
                                gps_lbs_statusinfo1.ReadBuffer(kksFrame.Data);
                                dataEntity = GetGPSDataEntity(fullGpsCode, gps_lbs_statusinfo1.GPS);
                                GPSDataEntity pr=GetPreGpsDataEntity(fullGpsCode);
                                if (pr != null)
                                {
                                    dataEntity.ACCState = pr.ACCState;
                                }

                                byte[] byteAddress2 = GetAddressByte(dataEntity, Address_PACK_Type.GPS_Plunder, new byte[21], kksFrame.SerialNum);
                                responseList.Add(GetResponseMsg(0x26, byteAddress2));
                                AddCodeVehicleGpsData(fullGpsCode, dataEntity);
                                break;
                            case KKSProtocolNO.GPS_LBS_Status_Info://0x16
                                if (_CodeGpsTypeRelation.ContainsKey(fullGpsCode) && (_CodeGpsTypeRelation[fullGpsCode] == KKS_HDType.GT06 || _CodeGpsTypeRelation[fullGpsCode] == KKS_HDType.GT03))
                                {
                                    KKSGPS_LBS_StatusInfo gps_lbs_statusinfo = new KKSGPS_LBS_StatusInfo();
                                    gps_lbs_statusinfo.ReadBuffer(kksFrame.Data);
                                    bool isPunderAlarm = false;

                                    KKStatus objStatus = gps_lbs_statusinfo.Status;

                                    //由于硬件缺陷采用临时方案
                                    switch (gps_lbs_statusinfo.Status.AlarmType)
                                    {
                                        case KKSAlarmType.PowerOff:
                                            objStatus.PowerState = 1;
                                            break;
                                        case KKSAlarmType.SOS:
                                            objStatus.AlarmType = KKSAlarmType.Normal;
                                            isPunderAlarm = true;
                                            break;
                                    }
                                    AddCodeVehicleStatusTb(fullGpsCode, objStatus);
                                    UpdateLastProtocolNo(fullGpsCode, kksFrame.ProtocolNo);
                                    dataEntity = GetGPSDataEntity(fullGpsCode, gps_lbs_statusinfo.GPS, isPunderAlarm);
                                    responseList.Add(GetResponseMsg(0x16, kksFrame.SerialNum));
                                }
                                else if (_CodeGpsTypeRelation.ContainsKey(fullGpsCode) && _CodeGpsTypeRelation[fullGpsCode] == KKS_HDType.GT07)
                                {
                                    UpdateLastProtocolNo(fullGpsCode, kksFrame.ProtocolNo);
                                }
                                else if (_CodeGpsTypeRelation.ContainsKey(fullGpsCode) && (_CodeGpsTypeRelation[fullGpsCode] == KKS_HDType.GS503 || _CodeGpsTypeRelation[fullGpsCode] == KKS_HDType.GK301))
                                {
                                    KKSMobile_GPS_LBS_StatusInfo gps_lbs_statusinfo = new KKSMobile_GPS_LBS_StatusInfo();
                                    gps_lbs_statusinfo.ReadBuffer(kksFrame.Data);
                                    dataEntity = GetGPSDataEntity(fullGpsCode, gps_lbs_statusinfo.GPS);
                                    if (dataEntity != null)
                                    {
                                        byte[] byteAddress = GetAddressByte(dataEntity, Address_PACK_Type.GPS_Plunder, new byte[21], kksFrame.SerialNum);
                                        responseList.Add(GetResponseMsg(0x16, byteAddress));

                                        AddCodeVehicleGpsData(fullGpsCode, dataEntity);
                                    }
                                }
                                break;
                            case KKSProtocolNO.StringInfo://0x15
                                string strClientMsg = Encoding.ASCII.GetString(kksFrame.Data);
                                base.OnDebugDataEvent(base.TransfersType.ToString(), fullGpsCode, "[Reply]:" + strClientMsg);
                                Logger.Trace("收到客户端应答[" + fullGpsCode + "]:" + strClientMsg);
                                break;
                            case KKSProtocolNO.LBS_Caller_QueryInfo://0x17
                                {
                                    if (_CodeGpsTypeRelation.ContainsKey(fullGpsCode) && (_CodeGpsTypeRelation[fullGpsCode] == KKS_HDType.GS503 || _CodeGpsTypeRelation[fullGpsCode] == KKS_HDType.GK301 || _CodeGpsTypeRelation[fullGpsCode] == KKS_HDType.GT03))
                                    {
                                        KKS_LBS_MobileInfo lbs_mobile = new KKS_LBS_MobileInfo();
                                        lbs_mobile.ReadBuffer(kksFrame.Data);
                                        CELL_Position_Info position = new CELL_Position_Info();
                                        byte[] bResponse = GetLbsPosition(lbs_mobile.LBS, ref position);
                                        if (bResponse != null)
                                        {
                                            dataEntity = GetGPSDataEntity(fullGpsCode, lbs_mobile.LBS.ReportTime, position, false);
                                            AddCodeVehicleGpsData(fullGpsCode, dataEntity);
                                            if (dataEntity != null)
                                            {
                                                byte[] byteAddress = GetAddressByte(dataEntity, Address_PACK_Type.LBS_GetAddress, lbs_mobile.Mobile, kksFrame.SerialNum);
                                                responseList.Add(GetResponseMsg(0x17, byteAddress));
                                            }
                                        }
                                    }
                                }
                                break;
                            case KKSProtocolNO.Full_LBS_Info://0x18
                                {
                                    if (_CodeGpsTypeRelation.ContainsKey(fullGpsCode) && (_CodeGpsTypeRelation[fullGpsCode] == KKS_HDType.GS503 || _CodeGpsTypeRelation[fullGpsCode] == KKS_HDType.GK301 || _CodeGpsTypeRelation[fullGpsCode] == KKS_HDType.GT03))
                                    {
                                        KKS_FullLBS_Info lbsInfo = new KKS_FullLBS_Info();
                                        lbsInfo.ReadBuffer(kksFrame.Data);
                                        CELL_Position_Info position = new CELL_Position_Info();
                                        byte[] bResponse = GetLbsPosition(lbsInfo, ref position);

                                        if (bResponse != null)
                                        {
                                            dataEntity = GetGPSDataEntity(fullGpsCode, lbsInfo.ReportTime, position, false);
                                            AddCodeVehicleGpsData(fullGpsCode, dataEntity);
                                        }
                                        responseList.Add(GetResponseMsg(0x18, kksFrame.SerialNum));
                                    }
                                }
                                break;
                            case KKSProtocolNO.GPS_Full_LBS_Info://0x1E
                                {
                                    if (_CodeGpsTypeRelation.ContainsKey(fullGpsCode) && (_CodeGpsTypeRelation[fullGpsCode] == KKS_HDType.GS503 || _CodeGpsTypeRelation[fullGpsCode] == KKS_HDType.GK301))
                                    {
                                        KKS_GPS_FullLBS_Info gps_fullLBS = new KKS_GPS_FullLBS_Info();
                                        gps_fullLBS.ReadBuffer(kksFrame.Data);
                                        dataEntity = GetGPSDataEntity(fullGpsCode, gps_fullLBS.GPS);
                                        AddCodeVehicleGpsData(fullGpsCode, dataEntity);
                                    }
                                }
                                break;
                            case KKSProtocolNO.LBS_Status_Info://0x19
                                {
                                    if (_CodeGpsTypeRelation.ContainsKey(fullGpsCode) && (_CodeGpsTypeRelation[fullGpsCode] == KKS_HDType.GS503 || _CodeGpsTypeRelation[fullGpsCode] == KKS_HDType.GK301 || _CodeGpsTypeRelation[fullGpsCode] == KKS_HDType.GT03))
                                    {
                                        KKS_Mobile_LBS_Status lbs_Status = new KKS_Mobile_LBS_Status();
                                        lbs_Status.ReadBuffer(kksFrame.Data);
                                        CELL_Position_Info position = new CELL_Position_Info();
                                        byte[] bResponse = GetLbsPosition(lbs_Status.LBS, ref position);

                                        if (bResponse != null)
                                        {
                                            dataEntity = GetGPSDataEntity(fullGpsCode, DateTime.Now, position, lbs_Status.Status.PlunderStatus == 100);
                                            AddCodeVehicleGpsData(fullGpsCode, dataEntity);
                                        }
                                        if (dataEntity != null)
                                        {
                                            byte[] byteAddress = GetAddressByte(dataEntity, Address_PACK_Type.LBS_Plunder, new byte[21], kksFrame.SerialNum);
                                            responseList.Add(GetResponseMsg(0x17, byteAddress));

                                        }
                                    }
                                }
                                break;
                            case KKSProtocolNO.ET200_GPS_Caller_QueryInfo://0x2a
                            case KKSProtocolNO.GPS_Caller_QueryInfo://0x1a
                                KKS_GPS_MobileInfo gps_mobile = new KKS_GPS_MobileInfo();
                                gps_mobile.ReadBuffer(kksFrame.Data);
                                dataEntity = GetGPSDataEntity(fullGpsCode, gps_mobile.GPS);
                                if (dataEntity != null)
                                {
                                    byte[] byteAddress = GetAddressByte(dataEntity, Address_PACK_Type.GPS_GetAddress, gps_mobile.Mobile, kksFrame.SerialNum);
                                    responseList.Add(GetResponseMsg(0x1A, byteAddress));
                                }
                                break;
                            case KKSProtocolNO.Set_InstructionInfo:
                            case KKSProtocolNO.Get_InstructionInfo:
                                Instruction_Info instructionInfo = new Instruction_Info();
                                instructionInfo.ReadBuffer(kksFrame.Data);
                                string responseStr = Encoding.ASCII.GetString(instructionInfo.InstructionContent);
                                int flag = responseStr.IndexOf('\0');
                                if (flag > 0)
                                {
                                    responseStr = responseStr.Substring(0, flag);
                                }
                                bool IsAnswer = true;
                                if (responseStr.StartsWith("SEESOS"))
                                    OnSendAnswerToWebEvent(responseStr, true, fullGpsCode, ControlCmdType.GetFriendNumber, true);
                                else if (responseStr.StartsWith("SOS=Success!"))
                                    OnSendAnswerToWebEvent(responseStr, true, fullGpsCode, ControlCmdType.SetFriendNumber, true);
                                else if (responseStr.StartsWith("GPSON"))
                                    OnSendAnswerToWebEvent(responseStr, true, fullGpsCode, ControlCmdType.OpenGPS, true);
                                else if (responseStr.StartsWith("GPSON=OverTime Off!"))
                                {
                                    IsAnswer = false;
                                }
                                else
                                    IsAnswer = false;
                                if (IsAnswer)
                                    Logger.Trace("---gps应答:" + kksFrame.ProtocolNo.ToString() + "----" + fullGpsCode + "---" + responseStr);
                                break;

                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
            if (dataEntity != null)
            {
                debuggerLog += "--dataEntity:" + GetStringByGPSDataEntity(dataEntity);
            }
            Logger.Warn(debuggerLog);
            return dataEntity;
        }