Пример #1
0
        /// <summary>
        /// Data from device. TCP
        /// </summary>
        /// <param name="arrData"></param>
        /// <param name="iStart"></param>
        /// <param name="iLength"></param>
        public override void TranslateFromDevice(byte[] arrData, int iStart, int iLength)
        {
            m_nlog.Info(GLPParser.LogBinary("GLP", arrData));

            m_parser.Write(arrData);

            GLPBase report  = null;
            bool    sendAck = false;

            while ((report = m_parser.NextReport()) != null)
            {
                if (report.sendAck)
                {
                    sendAck = true;
                }

                if (!report.parseError)
                {
                    if (report.loginPacket == false)
                    {
                        // Login
                        if (NmeaConnection.Session == null)
                        {
                            try
                            {
                                NmeaConnection.Login("IMEI", report.DeviceID, null);
                            }
                            catch (Franson.Directory.AuthenticationException)
                            {
                                throw;
                            }
                        }


                        // Pending outgoing commands.
                        ProcessPendingCommand(report);

                        if (report.CameraStatus == 2)
                        {
                            report.CameraStatus = 0;
                            NmeaConnection.ProtocolToDevice("ACK1");
                        }

                        if (report.CameraStatus == 3) //pobieranie zdjec
                        {
                            report.CameraStatus = 0;
                            GLPPictureProcessor glpPictureProcessor = CameraPluginServices.TryCreatePictureProcessor();
                            if (glpPictureProcessor != null)
                            {
                                try
                                {
                                    glpPictureProcessor.Process(report as GLPBinaryPictureData, this.NmeaConnection);
                                    NmeaConnection.ProtocolToDevice("ACK1");
                                }
                                catch
                                {
                                    NmeaConnection.ProtocolToDevice("ACK2");    //przerywamy z ponowieniem
                                }
                            }
                            else
                            {
                                NmeaConnection.ProtocolToDevice("ACK3"); //mnie ma w ogole zainstalowanej kamery wiec przerywamy calkiem
                            }
                        }
                        else if (report.TerminalData != null)
                        {
                            FRCMD  fRCMD = null;
                            string text  = UTF8Encoding.UTF8.GetString(report.TerminalData);
                            if (text == "GLP_UNDELIVERED\0")
                            {
                                text = Franson.Directory.Session.CurrentSession.Locale.Lang["GLP"].Server["GLP_UNDELIVERED"];
                            }
                            if (text == "GLP_DELIVERED\0")
                            {
                                text = Franson.Directory.Session.CurrentSession.Locale.Lang["GLP"].Server["GLP_DELIVERED"];
                            }


                            if (report.JobStatus == 100)
                            {
                                fRCMD = new ChangeAssignedWorkerStateCmdBuilder(CommandDirection.DeviceToGpsGate, report.JobID, NmeaConnection.Device.DeviceOwnerID, AssignedWorkerState.Active);
                            }
                            else if (report.JobStatus == 101)
                            {
                                fRCMD = new ChangeAssignedWorkerStateCmdBuilder(CommandDirection.DeviceToGpsGate, report.JobID, NmeaConnection.Device.DeviceOwnerID, AssignedWorkerState.Completed);
                            }
                            else if (report.JobStatus == 102)
                            {
                                fRCMD = new ChangeAssignedWorkerStateCmdBuilder(CommandDirection.DeviceToGpsGate, report.JobID, NmeaConnection.Device.DeviceOwnerID, AssignedWorkerState.Assigned);
                            }
                            else if (report.JobStatus == 103)
                            {
                                fRCMD = new ChangeAssignedWorkerStateCmdBuilder(CommandDirection.DeviceToGpsGate, report.JobID, NmeaConnection.Device.DeviceOwnerID, AssignedWorkerState.Assigned);
                            }
                            else if (report.JobStatus == 104)
                            {
                                fRCMD = new DeleteAssignedWorkerCmdBuilder(CommandDirection.DeviceToGpsGate, report.JobID, NmeaConnection.Device.DeviceOwnerID);
                            }
                            else
                            {
                                fRCMD = new FRCMD(null, "_ReceiveChatText", new string[] { text });
                            }
                            //serwer odbiera ale nie wysyła
                            //GpsGateClientCmd client = new GpsGateClientCmd("localhost", 30175);
                            //client.Connect("IMEI", report.DeviceID, null);
                            //client.CmdToServer(cmd);

                            if (fRCMD != null)
                            {
                                GpsGateClientDirect client = new GpsGateClientDirect();
                                client.Connect(NmeaConnection.Device, this.NmeaConnection);
                                client.CmdToServer(fRCMD);
                                client.Disconnect();
                            }
                        }
                        else
                        {
                            // Report to GpsGate Framework.
                            ToGpsGate(report.TrackPoint, report.Status);
                        }
                    }
                    else
                    {
                        // Login
                        if (NmeaConnection.Session == null)
                        {
                            NmeaConnection.Login("imei", report.DeviceID, null);
                        }

                        // to keep connection alive
                        ToGpsGate(null, null);
                    }
                }
            }

            if (sendAck)
            {
                NmeaConnection.ProtocolToDevice("ACK1");
            }
        }
Пример #2
0
        /// <summary>
        /// Parse data to report.
        /// </summary>
        /// <param name="arrData"></param>
        protected override void Parse(byte[] arrData)
        {
            try
            {
                parseError = false;
                int  iLength     = 0;
                bool SpeedHRMode = false;

                int noOfParams = arrData[iLength++];

                DateTime dtUTC = DateTime.MinValue;
                Single   Latitude = 0, Longitude = 0;
                Int16    Altitude = 0;
                UInt16   Heading  = 0;
                double   Speed    = 0.0;
                bool     bValid   = false;

                AddStatus("SavedUnix", Convert.ToDouble(ConvertToUnixTimestamp(DateTime.UtcNow)));

                for (int i = 0; i < noOfParams; i++)
                {
                    Byte paramNo = arrData[iLength++];

                    //czas
                    if (paramNo == 0)
                    {
                        UInt32 iUnixTime = BitConverter.ToUInt32(arrData, iLength);
                        dtUTC    = ConvertFromUnixTimestamp(iUnixTime);
                        iLength += 4;

                        if ((DateTime.UtcNow - dtUTC).TotalSeconds > 1800)
                        {
                            AddStatus("Historic", true);
                        }
                        else
                        {
                            AddStatus("Historic", false);
                        }

                        continue;
                    }

                    //wejscia i wyjscia
                    if (paramNo == 1)
                    {
                        uint parVal = BitConverter.ToUInt32(arrData, iLength);

                        AddStatus("Ignition", (parVal & (1 << 0)) != 0);
                        AddStatus("In1", (parVal & (1 << 1)) != 0);
                        AddStatus("In2", (parVal & (1 << 2)) != 0);
                        AddStatus("In3", (parVal & (1 << 3)) != 0);
                        AddStatus("In4", (parVal & (1 << 4)) != 0);
                        AddStatus("In5", (parVal & (1 << 5)) != 0);
                        AddStatus("In6", (parVal & (1 << 6)) != 0);
                        AddStatus("In7", (parVal & (1 << 7)) != 0);

                        AddStatus("Out1", (parVal & (1 << 8)) != 0);
                        AddStatus("Out2", (parVal & (1 << 9)) != 0);
                        AddStatus("Out3", (parVal & (1 << 10)) != 0);
                        AddStatus("Out4", (parVal & (1 << 11)) != 0);
                        AddStatus("Out5", (parVal & (1 << 12)) != 0);
                        AddStatus("Out6", (parVal & (1 << 13)) != 0);
                        AddStatus("Out7", (parVal & (1 << 14)) != 0);
                        AddStatus("Out8", (parVal & (1 << 15)) != 0);

                        AddStatus("Alarm0", (parVal & (1 << 16)) != 0);
                        AddStatus("Alarm1", (parVal & (1 << 17)) != 0);
                        AddStatus("Alarm2", (parVal & (1 << 18)) != 0);
                        AddStatus("Alarm3", (parVal & (1 << 19)) != 0);

                        AddStatus("HarshAcceleration", (parVal & (1 << 20)) != 0);
                        AddStatus("HarshBraking", (parVal & (1 << 21)) != 0);
                        AddStatus("HarshCornering", (parVal & (1 << 22)) != 0);
                        AddStatus("Accident", (parVal & (1 << 23)) != 0);

                        SpeedHRMode = (parVal & (1 << 31)) != 0;

                        iLength += 3;

                        AddStatus("Csq", (double)(((byte)arrData[iLength++]) & 0x7F));

                        continue;
                    }

                    //pozycja
                    if (paramNo == 192)
                    {
                        Latitude = BitConverter.ToSingle(arrData, iLength);
                        iLength += 4;

                        Longitude = BitConverter.ToSingle(arrData, iLength);
                        iLength  += 4;

                        continue;
                    }

                    //wysokosc, predkosc, hdop itp
                    if (paramNo == 193)
                    {
                        Altitude = BitConverter.ToInt16(arrData, iLength);
                        iLength += 2;

                        Speed    = (double)BitConverter.ToUInt16(arrData, iLength);
                        iLength += 2;

                        Heading  = BitConverter.ToUInt16(arrData, iLength);
                        iLength += 2;

                        Byte SU   = arrData[iLength];
                        bool iFix = false;
                        if ((SU & 0x80) == 0x80)
                        {
                            iFix   = true;
                            bValid = true;
                            SU    -= 0x80;
                        }

                        iLength++;

                        double HDOP = arrData[iLength] / 10d;

                        iLength++;

                        AddStatus("HDOP", HDOP);
                        AddStatus("Fix", iFix);
                        AddStatus("SatelliteCount", (double)SU);

                        continue;
                    }

                    if (paramNo == 11)
                    {
                        int parVal = BitConverter.ToInt32(arrData, iLength);
                        AddStatus("OdometerAcc", Convert.ToDouble(parVal) * 1000);
                    }

                    if (paramNo >= 2 && paramNo <= 63)
                    {
                        int parVal = BitConverter.ToInt32(arrData, iLength);
                        iLength += 4;

                        AddStatus("Parameter" + paramNo, Convert.ToDouble(parVal));

                        continue;
                    }

                    if (paramNo == 70)
                    {
                        TerminalStatus = Convert.ToInt32((SByte)arrData[iLength]);
                    }

                    if (paramNo >= 64 && paramNo <= 127)
                    {
                        SByte parVal = (SByte)arrData[iLength];
                        iLength++;

                        AddStatus("Parameter" + paramNo, Convert.ToDouble(parVal));

                        continue;
                    }

                    if (paramNo >= 128 && paramNo <= 191)
                    {
                        Int16 parVal = BitConverter.ToInt16(arrData, iLength);
                        iLength += 2;

                        AddStatus("Parameter" + paramNo, Convert.ToDouble(parVal));

                        continue;
                    }

                    if (paramNo >= 194 && paramNo <= 239)
                    {
                        Int64 parVal = BitConverter.ToInt64(arrData, iLength);
                        iLength += 8;

                        AddStatus("Parameter" + paramNo, Convert.ToDouble(parVal));

                        continue;
                    }

                    if (paramNo >= 240 && paramNo <= 255)
                    {
                        uint parLen = arrData[iLength];
                        iLength++;

                        byte[] data = new byte[parLen];
                        Array.Copy(arrData, iLength, data, 0, parLen);
                        iLength += (int)parLen;

                        AddStatus("Parameter" + paramNo, UTF8Encoding.UTF8.GetString(data));

                        continue;
                    }
                }

                if (SpeedHRMode)
                {
                    Speed /= 100.0;
                }

                if (Longitude < -180.0 || Longitude > 180 || Latitude < -90 || Latitude > 90)
                {
                    Longitude = 0;
                    Latitude  = 0;
                    bValid    = false;
                }

                SetTrackPoint(new TrackPoint(
                                  new Position(Longitude, Latitude, Altitude),
                                  new Velocity(Speed, Heading, SpeedUnits.Kph),
                                  dtUTC, bValid));


                SetLength(iLength);
            }
            catch (Exception ex)
            {
                parseError = true;
                m_nlog.ErrorException(string.Format("Could not parse {0}", GLPParser.LogBinary(null, arrData)), ex);
                //throw new FormatException(string.Format("Could not parse {0}", GLPParser.LogBinary(null, arrData)), ex);
            }
        }
Пример #3
0
        protected override void Parse(byte[] arrData)
        {
            try
            {
                parseError = false;

                int iLength = 0;

                // Info byte
                int iInfo = arrData[0];
                switch (iInfo)
                {
                case 32:        //terminal state
                    iLength       += 1;
                    TerminalStatus = Convert.ToInt32((SByte)arrData[iLength]);
                    iLength       += 1;
                    //if (arrData[1] == 0)
                    //    TerminalData = Encoding.UTF8.GetBytes("Terminal odłączony");
                    //if (arrData[1] == 1)
                    //    TerminalData = Encoding.UTF8.GetBytes("Terminal podłączony");
                    break;

                case 33:        //message
                    iLength++;

                    UInt64 iMessageID = BitConverter.ToUInt64(arrData, iLength);
                    iLength += 8;

                    UInt16 iMessageSize = BitConverter.ToUInt16(arrData, iLength);
                    iLength += 2;

                    TerminalData = new byte[iMessageSize];
                    Array.Copy(arrData, iLength, TerminalData, 0, iMessageSize);
                    iLength += iMessageSize;

                    if (TerminalData[0] == (byte)'S' && TerminalData[1] == (byte)'T' && TerminalData[2] == (byte)'O' && TerminalData[3] == (byte)'P' && TerminalData[4] == (byte)':')
                    {
                        byte[] TerminalTemp = new byte[10];
                        int    dwukropek = 4, rownosc = 0;
                        rownosc = Array.IndexOf(TerminalData, (byte)'=');
                        if (rownosc > 0)
                        {
                            Array.Copy(TerminalData, dwukropek + 1, TerminalTemp, 0, rownosc - dwukropek - 1);
                            int.TryParse(Encoding.ASCII.GetString(TerminalData, dwukropek + 1, rownosc - dwukropek - 1), out JobID);
                            int.TryParse(Encoding.ASCII.GetString(TerminalData, rownosc + 1, 3), out JobStatus);
                        }
                    }
                    break;

                case 34:        //message received acknowledgement
                    iLength += 13;
                    break;

                case 40:        //destination accepted
                    iLength++;
                    UInt64 iStopID = BitConverter.ToUInt64(arrData, iLength);
                    iLength += 8;
                    UInt32 iUnixTime = BitConverter.ToUInt32(arrData, iLength);
                    iLength += 4;
                    UInt32 iDistance = BitConverter.ToUInt32(arrData, iLength);
                    iLength     += 4;
                    TerminalData = Encoding.UTF8.GetBytes("Cel " + iStopID + " zaakceptowany o " + iUnixTime + ". Odległość do celu: " + iDistance);
                    break;

                case 41:        //destination point reached
                    iLength++;

                    iStopID      = BitConverter.ToUInt64(arrData, iLength);
                    iLength     += 8;
                    iUnixTime    = BitConverter.ToUInt32(arrData, iLength);
                    iLength     += 4;
                    TerminalData = Encoding.UTF8.GetBytes("Cel " + iStopID + " osiągnięty o " + iUnixTime);
                    break;

                default:
                    break;
                }

                SetLength(iLength);
            }
            catch (Exception ex)
            {
                parseError = true;
                m_nlog.ErrorException(string.Format("Could not parse {0}", GLPParser.LogBinary(null, arrData)), ex);
                //throw new FormatException(string.Format("Could not parse {0}", GLPParser.LogBinary(null, arrData)), ex);
            }
        }
Пример #4
0
        protected override void Parse(byte[] arrData)
        {
            if (arrData == null)
            {
                throw new ArgumentNullException("arrData");
            }
            try
            {
                parseError = false;

                int iLength = 0;

                // Info byte
                int iInfo = arrData[0];
                switch (iInfo)
                {
                case 1:
                    iLength     += 1;
                    CameraStatus = 1;
                    break;

                case 2:
                    iLength     += 1;
                    CameraStatus = 2;
                    break;

                case 3:
                    iLength     += 1;
                    CameraStatus = 3;

                    UInt32 iUnixTime = BitConverter.ToUInt32(arrData, iLength);
                    this.m_dtRTC = ConvertFromUnixTimestamp(iUnixTime);
                    iLength     += 4;

                    this.m_bPackageIndex = BitConverter.ToInt16(arrData, iLength);
                    iLength += 2;
                    this.m_bTotalPackages = BitConverter.ToInt16(arrData, iLength);
                    iLength += 2;
                    this.m_iPictureDataSize = BitConverter.ToInt16(arrData, iLength);
                    iLength += 2;
                    if (arrData.Length - 13 != this.m_iPictureDataSize)
                    {
                        throw new InvalidOperationException("Incomplete GLP picture data packet.");
                    }
                    this.m_arrPictureData = new byte[this.m_iPictureDataSize];
                    Buffer.BlockCopy(arrData, 11, this.m_arrPictureData, 0, this.m_iPictureDataSize);
                    iLength += this.m_iPictureDataSize;

                    break;

                default:
                    break;
                }

                SetLength(iLength);
            }
            catch (Exception ex)
            {
                parseError = true;
                m_nlog.ErrorException(string.Format("Could not parse {0}", GLPParser.LogBinary(null, arrData)), ex);
                //throw new FormatException(string.Format("Could not parse {0}", GLPParser.LogBinary(null, arrData)), ex);
            }
        }