Ejemplo n.º 1
0
 bool ProcessBinaryPacket(byte[] buffer, ref int from)
 {
     if (m_IMEI == 0)
     {
         m_IMEI = BIConverter.ToInt64(buffer, from + 2);
         if (!IMEIIsValid())
         {
             return(false);
         }
     }
     m_SeqID = BIConverter.ToUInt16(buffer, from + 10);
     from   += 12;
     return(true);
 }
Ejemplo n.º 2
0
    ////////////////////////////////////////////////////////////////////////
    bool ProcessRecord(byte[] buffer, ref int from)
    {
        if (buffer[from++] != 0x78 || buffer[from++] != 0x78)
        {
            return(false);
        }

        int len = buffer[from++];

        if (from + len + 2 > buffer.Length)
        {
            return(false);
        }

        if (buffer[from + len] != 13 || buffer[from + len + 1] != 10)
        {
            return(false);
        }

        int crc = GetCrc16(buffer, from - 1, len - 1);

        if (crc != BIConverter.ToUInt16(buffer, from + len - 2))
        {
            return(false);
        }

        byte pn = buffer[from++];
        int  sn = BitConverter.ToUInt16(buffer, from + len - 5);

        switch (pn)
        {
        case 1:    //login
        {
            m_IMEI = GetNumberFromHex(buffer, from, 8);
            if (!IMEIIsValid())
            {
                return(false);
            }

            byte[] reply = new byte[10] {
                0x78, 0x78, 0x05, pn, (byte)(sn >> 8), (byte)sn, 0x00, 0x00, 0x0D, 0x0A
            };
            crc = GetCrc16(reply, 2, reply.Length - 6);
            reply[reply.Length - 4] = (byte)(crc >> 8);
            reply[reply.Length - 3] = (byte)crc;
            Send(reply, reply.Length);

            break;
        }

        case 0x10:    //GPS
        {
            TrackerPacket packet = new TrackerPacket(m_IMEI);
            if (buffer[from] > 0)
            {
                packet.m_Time = CTime.GetTime(new DateTime(2000 + buffer[from], buffer[from + 1], buffer[from + 2], buffer[from + 3], buffer[from + 4], buffer[from + 5]));
            }
            packet.m_SatteliteCount = (byte)(((buffer[from + 16] & 16) == 0) ? 0 : buffer[from + 6] & 0x0F);
            packet.m_fLat           = (float)(BIConverter.ToUInt32(buffer, from + 7) / 1800000.0 * (((buffer[from + 16] & 4) == 0) ? -1 : 1));
            packet.m_fLng           = (float)(BIConverter.ToUInt32(buffer, from + 11) / 1800000.0 * (((buffer[from + 16] & 8) == 0) ? 1 : -1));
            packet.m_Speed          = buffer[from + 15];
            packet.m_Direction      = (ushort)(BIConverter.ToUInt16(buffer, from + 16) & 0x3FF);
            if (Math.Abs(CTime.GetTime(DateTime.UtcNow) - packet.m_Time) < 600)
            {
                packet.SetInput("ACC", m_Volt);
            }
            if (!PushPacket(packet))
            {
                return(false);
            }
            break;
        }

        case 0x12:    //GPS
        {
            TrackerPacket packet = new TrackerPacket(m_IMEI);
            if (buffer[from] > 0)
            {
                packet.m_Time = CTime.GetTime(new DateTime(2000 + buffer[from], buffer[from + 1], buffer[from + 2], buffer[from + 3], buffer[from + 4], buffer[from + 5]));
            }
            packet.m_SatteliteCount = (byte)(((buffer[from + 16] & 16) == 0) ? 0 : buffer[from + 6] & 0x0F);
            packet.m_fLat           = (float)(BIConverter.ToUInt32(buffer, from + 7) / 1800000.0 * (((buffer[from + 16] & 4) == 0) ? -1 : 1));
            packet.m_fLng           = (float)(BIConverter.ToUInt32(buffer, from + 11) / 1800000.0 * (((buffer[from + 16] & 8) == 0) ? 1 : -1));
            packet.m_Speed          = buffer[from + 15];
            packet.m_Direction      = (ushort)(BIConverter.ToUInt16(buffer, from + 16) & 0x3FF);
            if (Math.Abs(CTime.GetTime(DateTime.UtcNow) - packet.m_Time) < 600)
            {
                packet.SetInput("ACC", m_Volt);
            }
            if (!PushPacket(packet))
            {
                return(false);
            }
            break;
        }

        case 0x13:    //Status package
        {
            switch (buffer[from + 1])
            {
            case 1: m_Volt = 5; break;

            case 2: m_Volt = 10; break;

            case 3: m_Volt = 30; break;

            case 4: m_Volt = 50; break;

            case 5: m_Volt = 75; break;

            case 6: m_Volt = 100; break;

            default: m_Volt = 0; break;
            }

            byte[] reply = new byte[10] {
                0x78, 0x78, 0x05, pn, (byte)(sn >> 8), (byte)sn, 0x00, 0x00, 0x0D, 0x0A
            };
            crc = GetCrc16(reply, 2, reply.Length - 6);
            reply[reply.Length - 4] = (byte)(crc >> 8);
            reply[reply.Length - 3] = (byte)crc;
            Send(reply, reply.Length);
            break;
        }

        case 0x16:    //GPS LBS status combined package
        {
            byte[] reply = new byte[15] {
                0x78, 0x78, 11, pn, 0, 0x00, 0x00, 0x00, 0x01, (byte)(sn >> 8), (byte)sn, 0, 0, 0x0D, 0x0A
            };
            crc = GetCrc16(reply, 2, reply.Length - 6);
            reply[reply.Length - 4] = (byte)(crc >> 8);
            reply[reply.Length - 3] = (byte)crc;
            Send(reply, reply.Length);

            m_Logger.Push(LogLevel.DEBUG, 0, "GPS LBS status combined package");
            break;
        }

        case 0x17:    //LBS telephone number address searching package
        {
            byte[] reply = new byte[15] {
                0x78, 0x78, 11, pn, 0, 0x00, 0x00, 0x00, 0x01, (byte)(sn >> 8), (byte)sn, 0, 0, 0x0D, 0x0A
            };
            crc = GetCrc16(reply, 2, reply.Length - 6);
            reply[reply.Length - 4] = (byte)(crc >> 8);
            reply[reply.Length - 3] = (byte)crc;
            Send(reply, reply.Length);
            m_Logger.Push(LogLevel.DEBUG, 0, "LBS telephone number address searching package");
            break;
        }

        case 0x18:    //LBS extension package
        {
            TrackerPacket packet = new TrackerPacket(m_IMEI);
            if (buffer[from] > 0)
            {
                packet.m_Time = CTime.GetTime(new DateTime(2000 + buffer[from], buffer[from + 1], buffer[from + 2], buffer[from + 3], buffer[from + 4], buffer[from + 5]));
            }
            packet.SetGSMInfo(BIConverter.ToUInt16(buffer, from + 6), buffer[from + 8], BIConverter.ToUInt16(buffer, from + 9), BIConverter.ToUInt16(buffer, from + 12));
            if (Math.Abs(CTime.GetTime(DateTime.UtcNow) - packet.m_Time) < 600)
            {
                packet.SetInput("ACC", m_Volt);
            }
            if (!PushPacket(packet))
            {
                return(false);
            }
            break;
        }

        case 0x19:    //LBS status combined package
        {
            byte[] reply = new byte[15] {
                0x78, 0x78, 11, pn, 0, 0x00, 0x00, 0x00, 0x01, (byte)(sn >> 8), (byte)sn, 0, 0, 0x0D, 0x0A
            };
            crc = GetCrc16(reply, 2, reply.Length - 6);
            reply[reply.Length - 4] = (byte)(crc >> 8);
            reply[reply.Length - 3] = (byte)crc;
            Send(reply, reply.Length);
            m_Logger.Push(LogLevel.DEBUG, 0, "LBS status combined package");
            break;
        }

        case 0x1A:    //GPS telephone number address searching package
        {
            byte[] reply = new byte[15] {
                0x78, 0x78, 11, pn, 0, 0x00, 0x00, 0x00, 0x01, (byte)(sn >> 8), (byte)sn, 0, 0, 0x0D, 0x0A
            };
            crc = GetCrc16(reply, 2, reply.Length - 6);
            reply[reply.Length - 4] = (byte)(crc >> 8);
            reply[reply.Length - 3] = (byte)crc;
            Send(reply, reply.Length);
            m_Logger.Push(LogLevel.DEBUG, 0, "GPS telephone number address searching package");
            break;
        }

        case 0x1F:    //Time Synchronization packets
        {
            long   time  = CTime.GetTime(DateTime.UtcNow);
            byte[] reply = new byte[14] {
                0x78, 0x78, 10, pn, (byte)(time >> 24), (byte)(time >> 16), (byte)(time >> 8), (byte)(time), (byte)(sn >> 8), (byte)sn, 0, 0, 0x0D, 0x0A
            };
            crc = GetCrc16(reply, 2, reply.Length - 6);
            reply[reply.Length - 4] = (byte)(crc >> 8);
            reply[reply.Length - 3] = (byte)crc;
            Send(reply, reply.Length);
            m_Logger.Push(LogLevel.DEBUG, 0, "Time Synchronization package ");
            break;
        }

        case 0x80:    //Command Package
        case 0x81:    //
        {
            /*byte[] reply = new byte[] { 0x78, 0x78, 0x1A, pn, 0x12, 0x00, 0x46, 0x04, 0x46, 0x50, 0x41, 0x52, 0x41, 0x4D, 0x3D, 0x53, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x21, 0x00, 0x02, 0x00, 0x6D, 0x72, 0x7D, 0x0D, 0x0A };
             * crc = GetCrc16(reply, 2, reply.Length - 6);
             * reply[reply.Length - 4] = (byte)(crc >> 8);
             * reply[reply.Length - 3] = (byte)crc;
             * Send(reply, reply.Length);*/
            break;
        }

        default:
        {
            m_Logger.Push(LogLevel.DEBUG, 0, "unknown package " + pn);
            break;
        }
        }
        from += len - 1; //13 10
        from += 2;       //13 10
        return(true);
    }
Ejemplo n.º 3
0
    ////////////////////////////////////////////////////////////////////////
    bool ProcessRecord(byte[] buffer, ref int from)
    {
        bool len2bytes = buffer[from] == 0x79;

        if ((buffer[from] != 0x78 && buffer[from] != 0x79) || buffer[from + 1] != buffer[from])
        {
            return(false);
        }
        from += 2;

        int len = len2bytes ? (BIConverter.ToUInt16(buffer, from)) : buffer[from];

        from += len2bytes ? 2 : 1;

        if (from + len + 2 > buffer.Length)
        {
            return(false);
        }

        if (buffer[from + len] != 13 || buffer[from + len + 1] != 10)
        {
            return(false);
        }

        int crc = GetCrc16(buffer, from - (len2bytes ? 2 : 1), len - 2 + (len2bytes ? 2 : 1));

        if (crc != BIConverter.ToUInt16(buffer, from + len - 2))
        {
            return(false);
        }

        byte pn = buffer[from++];
        int  sn = BIConverter.ToUInt16(buffer, from + len - 5);

        switch (pn)
        {
        case 1:    //login
        {
            m_IMEI = GetNumberFromHex(buffer, from, 8);
            if (!IMEIIsValid())
            {
                return(false);
            }

            byte[] reply = new byte[10] {
                0x78, 0x78, 0x05, pn, (byte)(sn >> 8), (byte)sn, 0x00, 0x00, 0x0D, 0x0A
            };
            crc = GetCrc16(reply, 2, reply.Length - 6);
            reply[reply.Length - 4] = (byte)(crc >> 8);
            reply[reply.Length - 3] = (byte)crc;
            Send(reply, reply.Length);
            break;
        }

        case 0x10:    //GPS
        {
            TrackerPacket packet = new TrackerPacket(m_IMEI);
            if (buffer[from] > 0)
            {
                packet.m_Time = CTime.GetTime(new DateTime(2000 + buffer[from], buffer[from + 1], buffer[from + 2], buffer[from + 3], buffer[from + 4], buffer[from + 5]));
            }
            packet.m_SatteliteCount = (byte)(((buffer[from + 16] & 16) == 0) ? 0 : buffer[from + 6] & 0x0F);
            packet.m_fLat           = (float)(BIConverter.ToUInt32(buffer, from + 7) / 1800000.0 * (((buffer[from + 16] & 4) == 0) ? -1 : 1));
            packet.m_fLng           = (float)(BIConverter.ToUInt32(buffer, from + 11) / 1800000.0 * (((buffer[from + 16] & 8) == 0) ? 1 : -1));
            packet.m_Speed          = buffer[from + 15];
            ushort dir = BIConverter.ToUInt16(buffer, from + 16);
            packet.m_Direction = (ushort)(dir & 0x3FF);

            PutExtData2Packet(packet);

            if (!PushPacket(packet))
            {
                return(false);
            }
            break;
        }

        case 0x12:    //location data packet
        {
            TrackerPacket packet = new TrackerPacket(m_IMEI);
            if (buffer[from] > 0)
            {
                packet.m_Time = CTime.GetTime(new DateTime(2000 + buffer[from], buffer[from + 1], buffer[from + 2], buffer[from + 3], buffer[from + 4], buffer[from + 5]));
            }
            packet.m_SatteliteCount = (byte)(((buffer[from + 16] & 16) == 0) ? 0 : buffer[from + 6] & 0x0F);
            packet.m_fLat           = (float)(BIConverter.ToUInt32(buffer, from + 7) / 1800000.0 * (((buffer[from + 16] & 4) == 0) ? -1 : 1));
            packet.m_fLng           = (float)(BIConverter.ToUInt32(buffer, from + 11) / 1800000.0 * (((buffer[from + 16] & 8) == 0) ? 1 : -1));
            packet.m_Speed          = buffer[from + 15];
            ushort dir = BIConverter.ToUInt16(buffer, from + 16);
            packet.m_Direction = (ushort)(dir & 0x3FF);

            PutExtData2Packet(packet);

            if (!PushPacket(packet))
            {
                return(false);
            }
            break;
        }

        case 0x13:    //Status package
        {
            m_LastStatusByte = buffer[from];
            switch (buffer[from + 1])
            {
            case 1: m_fAcc = 5; break;

            case 2: m_fAcc = 10; break;

            case 3: m_fAcc = 30; break;

            case 4: m_fAcc = 50; break;

            case 5: m_fAcc = 75; break;

            case 6: m_fAcc = 100; break;

            default: m_fAcc = 0; break;
            }

            byte[] reply = new byte[10] {
                0x78, 0x78, 0x05, pn, (byte)(sn >> 8), (byte)sn, 0x00, 0x00, 0x0D, 0x0A
            };
            crc = GetCrc16(reply, 2, reply.Length - 6);
            reply[reply.Length - 4] = (byte)(crc >> 8);
            reply[reply.Length - 3] = (byte)crc;
            Send(reply, reply.Length);
            break;
        }

        case 0x15:    //command reply
        {
            string str = Encoding.ASCII.GetString(buffer, from + 5, buffer[from] - 4);
            break;
        }

        case 0x16:    //GPS LBS status combined package
        {
            byte[] reply = new byte[15] {
                0x78, 0x78, 11, pn, 0, 0x00, 0x00, 0x00, 0x01, (byte)(sn >> 8), (byte)sn, 0, 0, 0x0D, 0x0A
            };
            crc = GetCrc16(reply, 2, reply.Length - 6);
            reply[reply.Length - 4] = (byte)(crc >> 8);
            reply[reply.Length - 3] = (byte)crc;
            Send(reply, reply.Length);

            TrackerPacket packet = new TrackerPacket(m_IMEI);
            if (buffer[from] > 0)
            {
                packet.m_Time = CTime.GetTime(new DateTime(2000 + buffer[from], buffer[from + 1], buffer[from + 2], buffer[from + 3], buffer[from + 4], buffer[from + 5]));
            }
            packet.m_SatteliteCount = (byte)(((buffer[from + 16] & 16) == 0) ? 0 : buffer[from + 6] & 0x0F);
            packet.m_fLat           = (float)(BIConverter.ToUInt32(buffer, from + 7) / 1800000.0 * (((buffer[from + 16] & 4) == 0) ? -1 : 1));
            packet.m_fLng           = (float)(BIConverter.ToUInt32(buffer, from + 11) / 1800000.0 * (((buffer[from + 16] & 8) == 0) ? 1 : -1));
            packet.m_Speed          = buffer[from + 15];
            ushort dir = BIConverter.ToUInt16(buffer, from + 16);
            packet.m_Direction = (ushort)(dir & 0x3FF);

            byte alarm = buffer[from + 30];
            if (alarm == 1)    //SOS
            {
                m_iAlarm = 1;
            }
            PutExtData2Packet(packet);
            m_iAlarm = 0;

            if (!PushPacket(packet))
            {
                return(false);
            }
            break;
        }

        case 0x17:    //LBS telephone number address searching package
        {
            byte[] reply = new byte[15] {
                0x78, 0x78, 11, pn, 0, 0x00, 0x00, 0x00, 0x01, (byte)(sn >> 8), (byte)sn, 0, 0, 0x0D, 0x0A
            };
            crc = GetCrc16(reply, 2, reply.Length - 6);
            reply[reply.Length - 4] = (byte)(crc >> 8);
            reply[reply.Length - 3] = (byte)crc;
            Send(reply, reply.Length);
            m_Logger.Push(LogLevel.DEBUG, 0, "LBS telephone number address searching package");
            break;
        }

        case 0x18:    //LBS extension package
        {
            break;
        }

        case 0x19:    //LBS status combined package
        {
            //StoreEvent(CEvent.EventType.ALARM, CTime.GetTime(DateTime.UtcNow), "");

            byte[] reply = new byte[15] {
                0x78, 0x78, 11, pn, 0, 0x00, 0x00, 0x00, 0x01, (byte)(sn >> 8), (byte)sn, 0, 0, 0x0D, 0x0A
            };
            crc = GetCrc16(reply, 2, reply.Length - 6);
            reply[reply.Length - 4] = (byte)(crc >> 8);
            reply[reply.Length - 3] = (byte)crc;
            Send(reply, reply.Length);
            m_Logger.Push(LogLevel.DEBUG, 0, "LBS status combined package");
            break;
        }

        case 0x1A:    //GPS telephone number address searching package
        {
            byte[] reply = new byte[15] {
                0x78, 0x78, 11, pn, 0, 0x00, 0x00, 0x00, 0x01, (byte)(sn >> 8), (byte)sn, 0, 0, 0x0D, 0x0A
            };
            crc = GetCrc16(reply, 2, reply.Length - 6);
            reply[reply.Length - 4] = (byte)(crc >> 8);
            reply[reply.Length - 3] = (byte)crc;
            Send(reply, reply.Length);
            m_Logger.Push(LogLevel.DEBUG, 0, "GPS telephone number address searching package");
            break;
        }

        case 0x1F:    //Time Synchronization packets
        {
            long   time  = CTime.GetTime(DateTime.UtcNow);
            byte[] reply = new byte[14] {
                0x78, 0x78, 10, pn, (byte)(time >> 24), (byte)(time >> 16), (byte)(time >> 8), (byte)(time), (byte)(sn >> 8), (byte)sn, 0, 0, 0x0D, 0x0A
            };
            crc = GetCrc16(reply, 2, reply.Length - 6);
            reply[reply.Length - 4] = (byte)(crc >> 8);
            reply[reply.Length - 3] = (byte)crc;
            Send(reply, reply.Length);
            m_Logger.Push(LogLevel.DEBUG, 0, "Time Synchronization package ");
            break;
        }

        case 0x80:    //Command Package
        case 0x81:    //
        {
            //need reply
            break;
        }

        case 0x8a:    //?????
        {
            break;
        }

        case 0x94:    //?????
        {
            byte id = buffer[from];
            int  k  = from + 1;
            switch (id)
            {
            case 0x00:        //power
            {
                m_fPwr = (float)(BIConverter.ToUInt16(buffer, k) / 100.0);
                k     += 2;
                break;
            }

            case 0x04:        //ascii content ALM1=75;ALM2=D5;ALM3=5F;STA1=40;DYD=01;SOS=+917226938881,+919904038880,+917226038882;CENTER=;FENCE=Fence,OFF,0,0.000000,0.000000,300,IN or OUT,1;
            {
                string str = Encoding.ASCII.GetString(buffer, k, len - 6);
                LogEvent(LogLevel.DEBUG, "0x94:     " + str);

                string[] parts = str.Split(new char[] { ';' });
                for (int i = 0; i < parts.Length; i++)
                {
                    string[] s = parts[i].Split(new char[] { '=' });
                    if (s.Length == 2)
                    {
                        switch (s[0])
                        {
                        /*case "ALM1":
                         *  m_iAlarm = m_iAlarm & 0xFFFF00;
                         *  m_iAlarm = m_iAlarm | int.Parse(s[1], System.Globalization.NumberStyles.HexNumber, null);
                         *  break;
                         * case "ALM2":
                         *  m_iAlarm = m_iAlarm & 0xFF00FF;
                         *  m_iAlarm = m_iAlarm | (int.Parse(s[1], System.Globalization.NumberStyles.HexNumber, null) << 8);
                         *  break;
                         * case "ALM3":
                         *  m_iAlarm = m_iAlarm & 0x00FFFF;
                         *  m_iAlarm = m_iAlarm | (int.Parse(s[1], System.Globalization.NumberStyles.HexNumber, null) << 16);
                         *  break;*/
                        case "STA1":
                            m_iStatus = m_iStatus & 0xFFFF00;
                            m_iStatus = m_iStatus | int.Parse(s[1], System.Globalization.NumberStyles.HexNumber, null);
                            break;

                        case "DYD":
                            m_iStatus = m_iStatus & 0xFF00FF;
                            m_iStatus = m_iStatus | (int.Parse(s[1], System.Globalization.NumberStyles.HexNumber, null) << 8);
                            break;
                        }
                    }
                }


                k += len - 6;
                break;
            }

            case 0x05:        //door status
            {
                m_fDoor = (float)buffer[k];
                k      += 1;
                break;
            }

            default:
                break;
            }
            break;
        }

        default:
        {
            m_Logger.Push(LogLevel.DEBUG, 0, "unknown package " + pn);
            break;
        }
        }
        from += len - 1; //13 10
        from += 2;       //13 10
        return(true);
    }