public void CanParseData_ContinuousLocation2()
        {
            byte[] payloadByte = {
                                     0x78, 0x78, 0x11, 0x01, 0x01, 0x23
                                 };

            byte[] payloadByteSecond = {
                                           0x45, 0x67, 0x89, 0x01, 0x23, 0x45, 0x20, 0x10, 0x32, 0x00, 0x00, 0x01, 0xA1, 0x21, 0x0D, 0x0,
                                     0x78, 0x78, 0x0A, 0x13, 0x01, 0x00, 0x03, 0x00, 0x02, 0x00, 0x05, 0xE4, 0x73, 0x0D, 0x0A,
                                     0x78, 0x78, 0x22, 0x22, 0x10, 0x02, 0x18, 0x07, 0x10, 0x0C, 0xC4, 0x01, 0x2E, 0xC3, 0x30, 0x08, 0x3F, 0xC6, 0xE0, 0x00, 0x15, 0x41, 0x01, 0x94, 0x5E, 0x00, 0x4A, 0x00, 0x67, 0x14, 0x00, 0x08, 0x01, 0x00, 0x02, 0xB2, 0x92, 0x0D, 0x0A,
                                     0x78, 0x78, 0x22, 0x22, 0x10, 0x02, 0x18, 0x07, 0x24, 0x29, 0xC4, 0x01, 0x2E, 0xCA, 0x00, 0x08, 0x3F, 0xC0, 0xB0, 0x00, 0x14, 0x00, 0x01, 0x94, 0x5E, 0x00, 0x4A, 0x00, 0x67, 0x14, 0x00, 0x08, 0x01, 0x00, 0x02, 0x41, 0x39, 0x0D, 0x0A,
                                     0x78, 0x78, 0x22, 0x22, 0x10, 0x02, 0x18, 0x07, 0x38, 0x2E, 0xC4, 0x01, 0x2E, 0xB3, 0x9C, 0x08, 0x3F, 0xC6, 0x60, 0x00, 0x15, 0x5F, 0x01, 0x94, 0x5E, 0x00, 0x4A, 0x00, 0x67, 0x14, 0x00, 0x08, 0x01, 0x00, 0x02, 0x3E, 0x7B, 0x0D, 0x0A,
                                     0x78, 0x78, 0x22, 0x22, 0x10, 0x02, 0x18, 0x08, 0x24, 0x12, 0xC4, 0x01, 0x2E, 0xC5, 0xF2, 0x08, 0x3F, 0xC3, 0x80, 0x01, 0x14, 0xBE, 0x01, 0x94, 0x5E, 0x00, 0x4A, 0x00, 0x67, 0x14, 0x00, 0x08, 0x01, 0x00, 0x00, 0x2C, 0x92, 0x0D, 0x0A,
                                     0x78, 0x78, 0x22, 0x22, 0x10, 0x02, 0x18, 0x08, 0x39, 0x24, 0xC4, 0x01, 0x2E, 0xCA, 0x26, 0x08, 0x3F, 0xC6, 0x00, 0x00, 0x14, 0x00, 0x01, 0x94, 0x5E, 0x00, 0x4A, 0x00, 0x67, 0x14, 0x00, 0x08, 0x01, 0x00, 0x00, 0x4D, 0x1B, 0x0D, 0x0A,
                                     0x78, 0x78, 0x22, 0x22, 0x10, 0x02, 0x18, 0x09, 0x12, 0x24, 0xC4, 0x01, 0x2E, 0xCF, 0xE8, 0x08, 0x3F, 0xC1, 0xF0, 0x00, 0x15, 0x5A, 0x01, 0x94, 0x5E, 0x00, 0x4A, 0x00, 0x67, 0x14, 0x00, 0x08, 0x01, 0x00, 0x02, 0x45, 0x7E, 0x0D, 0x0A,
                                     0x78, 0x78, 0x22, 0x22, 0x10, 0x02, 0x18, 0x09, 0x25, 0x28, 0xC4, 0x01, 0x2E, 0xC2, 0x6A, 0x08, 0x3F, 0xC4, 0x50, 0x00, 0x14, 0xB3, 0x01, 0x94, 0x5E, 0x00, 0x4A, 0x00, 0x67, 0x14, 0x00, 0x08, 0x01, 0x00, 0x00, 0x20, 0x85, 0x0D, 0x0A,
                                     0x78, 0x78, 0x22, 0x22, 0x10, 0x02, 0x18, 0x09, 0x3A, 0x29, 0xC4, 0x01, 0x2E, 0x89, 0x42, 0x08, 0x3F, 0xC1, 0x40, 0x00, 0x14, 0xAF, 0x01, 0x94, 0x5E, 0x00, 0x4A, 0x00, 0x67, 0x14, 0x00, 0x08, 0x01, 0x00, 0x00, 0xE7, 0x28, 0x0D, 0x0A,
                                     0x78, 0x78, 0x22, 0x22, 0x10, 0x02, 0x18, 0x0A, 0x13, 0x1E, 0xC4, 0x01, 0x2E, 0xB2, 0x34, 0x08, 0x3F, 0xC4, 0x40, 0x00, 0x14, 0x04, 0x01, 0x94, 0x5E, 0x00, 0x4A, 0x00, 0x67, 0x14, 0x00, 0x08, 0x01, 0x00, 0x00, 0xF9, 0x4C, 0x0D, 0x0A,
                                     0x78, 0x78, 0x22, 0x22, 0x10, 0x02, 0x18, 0x0A, 0x27, 0x27, 0xC4, 0x01, 0x2E, 0xC4, 0x52, 0x08, 0x3F, 0xC4, 0xE0, 0x00, 0x14, 0xCF, 0x01, 0x94, 0x5E, 0x00, 0x4A, 0x00, 0x67, 0x14, 0x00, 0x08, 0x01, 0x00, 0x02, 0x48, 0x2F, 0x0D, 0x0A
                                 };

            Tracker.Protocol.Protocol protocolParser = new Tracker.Protocol.Gt100Protocol();
            var di = new DeviceInfo() { RawData = payloadByte };
            Protocol.Protocol.Process(protocolParser, di);

            di.RawData = payloadByteSecond;

            Protocol.Protocol.Process(protocolParser, di);
        }
        public void CanNotParseDataJunk_ContinuousLocation()
        {
            byte[] payloadByte = {
                                     0x27, 0x27, 0xC4, 0x01, 0x2E, 0xC4, 0x52, 0x08, 0x3F, 0xC4, 0xE0, 0x00, 0x14, 0xCF, 0x01, 0x94, 0x5E, 0x00, 0x4A, 0x00, 0x67, 0x14, 0x00, 0x08, 0x01, 0x00, 0x02, 0x48, 0x2F, 0x0D, 0x0A
                                 };

            Tracker.Protocol.Protocol protocolParser = new Tracker.Protocol.Gt100Protocol();
            var di = new DeviceInfo() { RawData = payloadByte };
            Protocol.Protocol.Process(protocolParser, di);
        }
Exemple #3
0
        public override DeviceInfo Parse(DeviceInfo deviceInfo)
        {
            try
            {
                deviceInfo.Payload += BitConverter.ToString(deviceInfo.RawData).Replace("-", string.Empty);
                deviceInfo.RawData = new byte[0];
                var tPayload = deviceInfo.Payload;
                Console.WriteLine(tPayload);
                log.DebugFormat("Device IP: {0}, payLoad: {1}", deviceInfo.TrackerIp, deviceInfo.Payload);

                if (deviceInfo.Payload.Length < REQ_MIN_Length())
                {
                    deviceInfo.ParserStatus = ProtocolParserStatus.InSufficientData;
                    return deviceInfo;
                }

                if (ParserHelper.TSubstring(ref deviceInfo.Payload, 0, 4) == "7878")
                {
                    int packetLength = GetNumber(ParserHelper.TSubstring(ref deviceInfo.Payload, 0, 2));
                    string packetComparer = ParserHelper.TSubstring(ref deviceInfo.Payload, 0, 2);

                    if (packetComparer == ProtocolNumber.LoginMessage)
                    {
                        if (tPayload.Length >= (PayloadLength.LoginMessage * 2))
                        {
                            // Login message
                            deviceInfo.DeviceId = ParserHelper.TSubstring(ref deviceInfo.Payload, 0, 16);
                            deviceInfo.CommandType = DeviceCommandType.LoginData;
                            byte[] forCheckSumData = { 0x05,
                                0x01,
                                (byte)(Convert.ToInt32(ParserHelper.TSubstring(ref deviceInfo.Payload, 0, 2), 16)),
                                (byte)(Convert.ToInt32(ParserHelper.TSubstring(ref deviceInfo.Payload, 0, 2), 16)),
                            };
                            var checkSum = CRC16.ComputeChecksumBytes(forCheckSumData);
                            byte[] payloadByte = { 0x78, 0x78 };
                            payloadByte = payloadByte.Concat(forCheckSumData).ToArray();
                            payloadByte = payloadByte.Concat(checkSum).ToArray();
                            byte[] endValue = { 0x0D, 0x0A };
                            payloadByte = payloadByte.Concat(endValue).ToArray();
                            deviceInfo.ToSendRawData = payloadByte;
                            ParserHelper.TSkip(ref tPayload, ref deviceInfo.Payload, PayloadLength.LoginMessage * 2);
                            log.DebugFormat("{0}/Parse data: {1}", "GT03AProtocol", payloadByte);
                            deviceInfo = SetDataParsedTime(deviceInfo);
                            deviceInfo.ParserStatus = ProtocolParserStatus.Parsed;

                        }
                    }
                    else if (packetComparer == ProtocolNumber.GPSInformationdata)
                    {
                        if (tPayload.Length >= (PayloadLength.GPSInformation * 2))
                        {
                            deviceInfo.CommandType = DeviceCommandType.LocationData;

                            var datetime = DateTime.UtcNow.Year.ToString().Substring(0, 2) + GetNumberString(ParserHelper.TSubstring(ref deviceInfo.Payload, 0, 12));

                            deviceInfo.DeviceDataTime = new DateTime(Convert.ToInt32(ParserHelper.TSubstring(ref datetime, 0, 4)), Convert.ToInt32(ParserHelper.TSubstring(ref datetime, 0, 2)), Convert.ToInt32(ParserHelper.TSubstring(ref datetime, 0, 2)),
                                Convert.ToInt32(ParserHelper.TSubstring(ref datetime, 0, 2)), Convert.ToInt32(ParserHelper.TSubstring(ref datetime, 0, 2)), Convert.ToInt32(ParserHelper.TSubstring(ref datetime, 0, 2)));

                            // Parse Sinal
                            var gsQty = ParserHelper.TSubstring(ref deviceInfo.Payload, 0, 2);
                            // Parse lat
                            //0x02 0x6B 0x3F 0x3E
                            // To 40582974(Decimal)= 26B3F3E(Hexadecimal)
                            //22º32.7658‟=(22X60+32.7658)X30000=40582974

                            var latMin = (Double)Convert.ToInt32(ParserHelper.TSubstring(ref deviceInfo.Payload, 0, 8), 16);
                            var Latitude = (latMin / Double.Parse("60") / Double.Parse("30000")).ToString("0.000000000").Replace("-", ""); // To device Minute value

                            var longMin = (Double)Convert.ToInt32(ParserHelper.TSubstring(ref deviceInfo.Payload, 0, 8), 16);
                            var Longitude = (longMin / Double.Parse("60") / Double.Parse("30000")).ToString("0.000000000").Replace("-", ""); // To device Minute value

                            var Speed = GetNumberString(ParserHelper.TSubstring(ref deviceInfo.Payload, 0, 2)).ToString();

                            deviceInfo.Odometer = //(deviceInfo.Odometer ?? 0) +
                                Convert.ToInt32(GetDistance(deviceInfo.Latitude, deviceInfo.Longitude, Latitude, Longitude, deviceInfo.Speed.ToNullable<int>(), Speed.ToNullable<int>()));

                            deviceInfo.Speed = Speed;
                            deviceInfo.Latitude = Latitude;
                            deviceInfo.Longitude = Longitude;

                            // Lat Long East / Nort calc
                            int courseS = GetNumber(ParserHelper.TSubstring(ref deviceInfo.Payload, 0, 4));
                            if ((8192 & courseS) != 0)
                            {
                                //real time position
                            }
                            else
                            {
                                // differential position
                            }

                            if ((4096 & courseS) != 0)
                            {
                                //GPS positioned
                            }
                            else
                            {
                                //GPS not positioned
                            }

                            if ((2048 & courseS) != 0)
                            {
                                //West Longitude
                                // Add (-) to Longitude
                                deviceInfo.Longitude = "-" + deviceInfo.Longitude;
                            }
                            else
                            {
                                //East Longitude
                            }

                            if ((1024 & courseS) != 0)
                            {
                                //North Latitude
                            }
                            else
                            {
                                //South Latitude
                                deviceInfo.Latitude = "-" + deviceInfo.Latitude;
                            }

                            var serialNo = ParserHelper.TSubstring(ref deviceInfo.Payload, 0, 4);

                            ParserHelper.TSkip(ref tPayload, ref deviceInfo.Payload, PayloadLength.GPSInformation * 2);

                            deviceInfo = SetDataParsedTime(deviceInfo);
                            deviceInfo.ParserStatus = ProtocolParserStatus.Parsed;
                        }
                    }
                    else if (packetComparer == ProtocolNumber.StatusInformation)
                    {
                        if (tPayload.Length >= (PayloadLength.StatusInformation * 2))
                        {
                            deviceInfo.CommandType = DeviceCommandType.StatusInformation;
                            string statusInfo = ParserHelper.TSubstring(ref deviceInfo.Payload, 0, 10);
                            int tic = GetNumber(ParserHelper.TSubstring(ref statusInfo, 0, 2));

                            //128:	1000 0000-> true: oil & electricity disconnected, else working
                            deviceInfo.OilNElectricConected = (128 & tic) != 0 ? 0 : 1; // it is reverse when compared to below

                            //64:	0100 0000-> true: GPS on, GPS off
                            deviceInfo.OnGps = (64 & tic) != 0 ? 1 : 0;

                            //0000 0000
                            //32:		0010 0000-> true: SOS alarm
                            deviceInfo.OnSOS = (32 & tic) != 0 ? 1 : 0;
                            //24:		0001 1000->low battery alarm
                            deviceInfo.OnLowBattery = (24 & tic) != 0 ? 1 : 0;
                            //16:		0001 0000->Power cut alarm
                            deviceInfo.OnPowerCut = (16 & tic) != 0 ? 1 : 0;
                            //8:		0000 1000->shock alarm
                            deviceInfo.OnShock = (8 & tic) != 0 ? 1 : 0;
                            //0:		0000 0000->normal

                            //4:	0000 0100 true charge is on, else off
                            deviceInfo.OnCharge = (4 & tic) != 0 ? 1 : 0;
                            //2:	0000 0010 true ACC high, else low
                            deviceInfo.OnAcc = (2 & tic) != 0 ? 1 : 0;
                            //1:	0000 0001 true defense activated
                            deviceInfo.OnDefence = (1 & tic) != 0 ? 1 : 0;

                            deviceInfo.VoltageLevel = GetNumber(ParserHelper.TSubstring(ref statusInfo, 0, 2));

                            deviceInfo.SignalStrengthLevel = GetNumber(ParserHelper.TSubstring(ref statusInfo, 0, 2));

                            string alarmLanguage = ParserHelper.TSubstring(ref statusInfo, 0, 2);

                            deviceInfo.AlarmType = DeviceAlarmType.NormalAlarm;
                            if (alarmLanguage == "01")
                                deviceInfo.AlarmType = DeviceAlarmType.SOSAlarm;
                            if (alarmLanguage == "02")
                                deviceInfo.AlarmType = DeviceAlarmType.PowerCutAlarm;
                            if (alarmLanguage == "03")
                                deviceInfo.AlarmType = DeviceAlarmType.VibrationAlarm;
                            if (alarmLanguage == "04")
                                deviceInfo.AlarmType = DeviceAlarmType.FenceInAlarm;
                            if (alarmLanguage == "05")
                                deviceInfo.AlarmType = DeviceAlarmType.FenceOutAlarm;
                            if (alarmLanguage == "06")
                                deviceInfo.AlarmType = DeviceAlarmType.SpeedAlarm;
                            if (alarmLanguage == "09")
                                deviceInfo.AlarmType = DeviceAlarmType.MovingAlarm;

                            //Skipp latter byte
                            ParserHelper.TSubstring(ref statusInfo, 0, 2);

                            byte[] forCheckSumData = { 0x05,
                                0x13,
                                (byte)(Convert.ToInt32(ParserHelper.TSubstring(ref deviceInfo.Payload, 0, 2), 16)),
                                (byte)(Convert.ToInt32(ParserHelper.TSubstring(ref deviceInfo.Payload, 0, 2), 16)),
                            };

                            var checkSum = CRC16.ComputeChecksumBytes(forCheckSumData);

                            byte[] payloadByte = { 0x78, 0x78 };
                            payloadByte = payloadByte.Concat(forCheckSumData).ToArray();
                            payloadByte = payloadByte.Concat(checkSum).ToArray();
                            byte[] endValue = { 0x0D, 0x0A };
                            payloadByte = payloadByte.Concat(endValue).ToArray();

                            deviceInfo.ToSendRawData = payloadByte;

                            ParserHelper.TSkip(ref tPayload, ref deviceInfo.Payload, PayloadLength.StatusInformation * 2);

                            deviceInfo = SetDataParsedTime(deviceInfo);
                            deviceInfo.ParserStatus = ProtocolParserStatus.Parsed;
                        }
                    }
                    else if (packetComparer == ProtocolNumber.LBSInformationdata)
                    {
                        if (tPayload.Length >= (PayloadLength.LBSInformation * 2))
                        {
                            ParserHelper.TSkip(ref tPayload, ref deviceInfo.Payload, PayloadLength.LBSInformation * 2);

                            deviceInfo = SetDataParsedTime(deviceInfo);
                            deviceInfo.ParserStatus = ProtocolParserStatus.Parsed;
                        }
                    }
                    else if (packetComparer == ProtocolNumber.StatusInformation)
                    {
                        if (tPayload.Length >= (PayloadLength.StatusInformation * 2))
                        {
                            deviceInfo.CommandType = DeviceCommandType.StatusInformation;
                            string statusInfo = ParserHelper.TSubstring(ref deviceInfo.Payload, 0, 10);
                            int tic = GetNumber(ParserHelper.TSubstring(ref statusInfo, 0, 2));

                            //128:	1000 0000-> true: oil & electricity disconnected, else working
                            deviceInfo.OilNElectricConected = (128 & tic) != 0 ? 0 : 1; // it is reverse when compared to below

                            //64:	0100 0000-> true: GPS on, GPS off
                            deviceInfo.OnGps = (64 & tic) != 0 ? 1 : 0;

                            //0000 0000
                            //32:		0010 0000-> true: SOS alarm
                            deviceInfo.OnSOS = (32 & tic) != 0 ? 1 : 0;
                            //24:		0001 1000->low battery alarm
                            deviceInfo.OnLowBattery = (24 & tic) != 0 ? 1 : 0;
                            //16:		0001 0000->Power cut alarm
                            deviceInfo.OnPowerCut = (16 & tic) != 0 ? 1 : 0;
                            //8:		0000 1000->shock alarm
                            deviceInfo.OnShock = (8 & tic) != 0 ? 1 : 0;
                            //0:		0000 0000->normal

                            //4:	0000 0100 true charge is on, else off
                            deviceInfo.OnCharge = (4 & tic) != 0 ? 1 : 0;
                            //2:	0000 0010 true ACC high, else low
                            deviceInfo.OnAcc = (2 & tic) != 0 ? 1 : 0;
                            //1:	0000 0001 true defense activated
                            deviceInfo.OnDefence = (1 & tic) != 0 ? 1 : 0;

                            deviceInfo.VoltageLevel = GetNumber(ParserHelper.TSubstring(ref statusInfo, 0, 2));

                            deviceInfo.SignalStrengthLevel = GetNumber(ParserHelper.TSubstring(ref statusInfo, 0, 2));

                            string alarmLanguage = ParserHelper.TSubstring(ref statusInfo, 0, 2);

                            deviceInfo.AlarmType = DeviceAlarmType.NormalAlarm;
                            if (alarmLanguage == "01")
                                deviceInfo.AlarmType = DeviceAlarmType.SOSAlarm;
                            if (alarmLanguage == "02")
                                deviceInfo.AlarmType = DeviceAlarmType.PowerCutAlarm;
                            if (alarmLanguage == "03")
                                deviceInfo.AlarmType = DeviceAlarmType.VibrationAlarm;
                            if (alarmLanguage == "04")
                                deviceInfo.AlarmType = DeviceAlarmType.FenceInAlarm;
                            if (alarmLanguage == "05")
                                deviceInfo.AlarmType = DeviceAlarmType.FenceOutAlarm;
                            if (alarmLanguage == "06")
                                deviceInfo.AlarmType = DeviceAlarmType.SpeedAlarm;
                            if (alarmLanguage == "09")
                                deviceInfo.AlarmType = DeviceAlarmType.MovingAlarm;

                            //Skipp latter byte
                            ParserHelper.TSubstring(ref statusInfo, 0, 2);

                            byte[] forCheckSumData = { 0x05,
                                0x13,
                                (byte)(Convert.ToInt32(ParserHelper.TSubstring(ref deviceInfo.Payload, 0, 2), 16)),
                                (byte)(Convert.ToInt32(ParserHelper.TSubstring(ref deviceInfo.Payload, 0, 2), 16)),
                            };

                            var checkSum = CRC16.ComputeChecksumBytes(forCheckSumData);

                            byte[] payloadByte = { 0x78, 0x78 };
                            payloadByte = payloadByte.Concat(forCheckSumData).ToArray();
                            payloadByte = payloadByte.Concat(checkSum).ToArray();
                            byte[] endValue = { 0x0D, 0x0A };
                            payloadByte = payloadByte.Concat(endValue).ToArray();

                            deviceInfo.ToSendRawData = payloadByte;

                            ParserHelper.TSkip(ref tPayload, ref deviceInfo.Payload, PayloadLength.StatusInformation * 2);

                            deviceInfo = SetDataParsedTime(deviceInfo);
                            deviceInfo.ParserStatus = ProtocolParserStatus.Parsed;
                        }
                    }
                    else
                    {
                        deviceInfo.CommandType = DeviceCommandType.UnknownData;
                        // TODO parse Status info
                        deviceInfo.Payload = deviceInfo.Payload.Substring(deviceInfo.Payload.IndexOf("0D0A") + 4);

                        deviceInfo = SetDataParsedTime(deviceInfo);
                        deviceInfo.ParserStatus = ProtocolParserStatus.Parsed;
                    }

                    log.DebugFormat("Device IP: {0}, payLoad parsed", deviceInfo.TrackerIp);

                }
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Parse Error: {0}", ex);
            }

            return deviceInfo;
        }
Exemple #4
0
        public DeviceInfo Parse(DeviceInfo deviceInfo)
        {
            deviceInfo.Payload += (Encoding.ASCII.GetString(deviceInfo.RawData, 0, deviceInfo.RawData.Length));

            Console.WriteLine("Tk103Protocol, Payload: {0}, PayloadLength: {1}", deviceInfo.Payload, deviceInfo.Payload.Length);
            //deviceInfo.PayLoadMinLength = 94;
            if (deviceInfo.Payload.IndexOf('(') < deviceInfo.Payload.IndexOf(')'))
            {

                //deviceInfo.PayLoad = "(014114564660BP05000014114564660160122A1058.2023N07737.1963E000.016260558.63000000178L04527024)";
                deviceInfo.Payload = deviceInfo.Payload.Substring(1); //Eliminating (

                deviceInfo.DeviceId = deviceInfo.Payload.Substring(0, 12); // 014114564660 BP05000014114564660160122A1058.2023N07737.1963E000.016260558.63000000178L04527024)";
                deviceInfo.Payload = deviceInfo.Payload.Substring(12);

                deviceInfo.CommandType = DeviceCommandType.LocationData;// deviceInfo.PayLoad.Substring(0, 4); //BP05 000014114564660160122A1058.2023N07737.1963E000.016260558.63000000178L04527024)";
                deviceInfo.Payload = deviceInfo.Payload.Substring(4);

                deviceInfo.DeviceId = deviceInfo.Payload.Substring(0, 15); //000014114564660 160122A1058.2023N07737.1963E000.016260558.63000000178L04527024)";
                deviceInfo.Payload = deviceInfo.Payload.Substring(15);

                // Parse body
                // Datetime in SQL understan format '2015-01-23 12:23:23'
                 string dt = DateTime.UtcNow.Date.Year.ToString().Substring(0, 2) + deviceInfo.Payload.Substring(0, 6); //160122 A1058.2023N07737.1963E000.016260558.63000000178L04527024)";
                // di.TrackerDataActionTime 20160122
                dt = dt.Insert(6, "-");
                dt = dt.Insert(4, "-") + " ";
                deviceInfo.Payload = deviceInfo.Payload.Substring(6);

                deviceInfo.ValidData = deviceInfo.Payload.Substring(0, 1); //A 1058.2023N07737.1963E000.016260558.63000000178L04527024)";
                deviceInfo.Payload = deviceInfo.Payload.Substring(1);

                //TODO Lat
                deviceInfo.Latitude = deviceInfo.Payload.Substring(0, deviceInfo.Payload.IndexOf('.') + 1); //1058. 2023N07737.1963E000.016260558.63000000178L04527024)";
                deviceInfo.Latitude += deviceInfo.Payload.Substring(deviceInfo.Latitude.Length, 5); //1058. 2023N 07737.1963E000.016260558.63000000178L04527024)";
                deviceInfo.Payload = deviceInfo.Payload.Substring(deviceInfo.Latitude.Length);

                //TODO Long
                deviceInfo.Longitude = deviceInfo.Payload.Substring(0, deviceInfo.Payload.IndexOf('.') + 1); //07737. 1963E000.016260558.63000000178L04527024)";
                deviceInfo.Longitude += deviceInfo.Payload.Substring(deviceInfo.Longitude.Length, 5); //07737. 1963E 000.016260558.63000000178L04527024)";
                deviceInfo.Payload = deviceInfo.Payload.Substring(deviceInfo.Longitude.Length);

                deviceInfo.Speed = deviceInfo.Payload.Substring(0, deviceInfo.Payload.IndexOf('.') + 1);
                deviceInfo.Speed += deviceInfo.Payload.Substring(deviceInfo.Speed.Length, 1); //000. 0 16260558.63000000178L04527024)";
                deviceInfo.Payload = deviceInfo.Payload.Substring(deviceInfo.Speed.Length);

                // TODO Proper datetime onvertion
                // 2015-01-23 12:23:23 already cotaind 2016-01-22
                dt += deviceInfo.Payload.Substring(0, 6); //162605 58.63000000178L04527024)";
                dt = dt.Insert(13, ":");
                dt = dt.Insert(16, ":");
                deviceInfo.TrackerDataActionTime = DateTime.Parse(dt);
                deviceInfo.Payload = deviceInfo.Payload.Substring(6);

                deviceInfo.Direction = deviceInfo.Payload.Substring(0, deviceInfo.Payload.IndexOf('.') + 1); //58. 63000000178L04527024)";
                deviceInfo.Direction += deviceInfo.Payload.Substring(deviceInfo.Direction.Length, 2); //63 000000178L04527024)";
                deviceInfo.Payload = deviceInfo.Payload.Substring(deviceInfo.Direction.Length);

                deviceInfo.OnBattery = int.Parse(deviceInfo.Payload.Substring(0, 1)); //0 00000178L04527024)";
                deviceInfo.Payload = deviceInfo.Payload.Substring(1);

                deviceInfo.OnIgnition = int.Parse(deviceInfo.Payload.Substring(0, 1));//0 0000178L04527024)";
                deviceInfo.Payload = deviceInfo.Payload.Substring(1);

                deviceInfo.OnAc = int.Parse(deviceInfo.Payload.Substring(0, 1));//0 000178L04527024)";
                deviceInfo.Payload = deviceInfo.Payload.Substring(1);

                deviceInfo.UnKnown = deviceInfo.Payload.Substring(0, 1);//0 00178L04527024)";
                deviceInfo.Payload = deviceInfo.Payload.Substring(1);

                deviceInfo.OnGps = int.Parse(deviceInfo.Payload.Substring(0, 1));//0 0178L04527024)";
                deviceInfo.Payload = deviceInfo.Payload.Substring(1);

                //deviceInfo.PayLoad = deviceInfo.PayLoad.Substring(1);//0 178L04527024)";
                //deviceInfo.PayLoad = deviceInfo.PayLoad.Substring(1);//1 78L04527024)";
                //deviceInfo.PayLoad = deviceInfo.PayLoad.Substring(1);//7 8L04527024)";
                //deviceInfo.PayLoad = deviceInfo.PayLoad.Substring(1);//8 L04527024)";
                deviceInfo.Payload = deviceInfo.Payload.Substring(deviceInfo.Payload.IndexOf('L') + 1);
                //TODO: Hexcode onvertions
                deviceInfo.Mileage = deviceInfo.Payload.Substring(0, deviceInfo.Payload.IndexOf(')'));//L 04527024)";
                deviceInfo.Payload = deviceInfo.Payload.Substring(deviceInfo.Payload.IndexOf(')') + 1);

                //deviceInfo.ReplyMessage = "ACO0";
            }
            else
            {
            }
            deviceInfo = SetDataParsedTime(deviceInfo);
            deviceInfo.ParserStatus = ProtocolParserStatus.Parsed;
            return deviceInfo;
        }
Exemple #5
0
 public DeviceInfo SetDataParsedTime(DeviceInfo di)
 {
     di.TrackerDataParsedTime = DateTime.UtcNow;
     return di;
 }
Exemple #6
0
 public static void UpdateDevice(DeviceInfo deviceInfo)
 {
     devicePoolMutex.WaitOne();
     {
         int index = DevicePool.FindIndex(d => d.DeviceId == deviceInfo.DeviceId);
         if (index < 0)
         {
             DevicePool.Add(deviceInfo);
         }
         else
         {
             DevicePool[index] = deviceInfo;
         }
     }
     devicePoolMutex.ReleaseMutex();
 }
Exemple #7
0
        public static bool SaveData(DeviceInfo deviceInfo)
        {
            try
            {
                log.DebugFormat("{0}/SaveData:", _fileNm);

                var dbParams = new SqlParameter[]{
                    new SqlParameter("DeviceId", deviceInfo.DeviceId),
                    new SqlParameter("IMEI", deviceInfo.IMEI),
                    new SqlParameter("CommandType", deviceInfo.CommandType),
                    new SqlParameter("StatusCode", deviceInfo.StatusCode),
                    new SqlParameter("Latitude", deviceInfo.Latitude),
                    new SqlParameter("Longitude", deviceInfo.Longitude),
                    new SqlParameter("Speed", deviceInfo.Speed),
                    new SqlParameter("Direction", deviceInfo.Direction),
                    new SqlParameter("Altitude", deviceInfo.Altitude),
                    new SqlParameter("Mileage", deviceInfo.Mileage),
                    new SqlParameter("ValidData", deviceInfo.ValidData),
                    new SqlParameter("FullAddress", deviceInfo.FullAddress),
                    new SqlParameter("PayLoad", deviceInfo.Payload),
                    new SqlParameter("UnParsedPayload", deviceInfo.UnParsedPayload),
                    new SqlParameter("Distance", deviceInfo.Distance),
                    new SqlParameter("Odometer", deviceInfo.Odometer),
                    new SqlParameter("OnBattery", deviceInfo.OnLowBattery),
                    new SqlParameter("OnIgnition", deviceInfo.OnIgnition),
                    new SqlParameter("OnAc", deviceInfo.OnAc),
                    new SqlParameter("OnGps", deviceInfo.OnGps),
                    new SqlParameter("UnKnown", deviceInfo.UnKnown),
                    new SqlParameter("GeozoneIndex", deviceInfo.GeozoneIndex),
                    new SqlParameter("GeozoneID", deviceInfo.GeozoneID),
                    new SqlParameter("TrackerIp", deviceInfo.TrackerIp),
                    new SqlParameter("DeviceDataTime", deviceInfo.DeviceDataTime),
                    new SqlParameter("TrackerConnectedTime", deviceInfo.TrackerConnectedTime),
                    new SqlParameter("TrackerDataActionTime", deviceInfo.TrackerDataActionTime),
                    new SqlParameter("TrackerDataParsedTime", deviceInfo.TrackerDataParsedTime),

                    new SqlParameter("OnAcc", deviceInfo.OnAcc),
                    new SqlParameter("OilNElectricConected", deviceInfo.OilNElectricConected),
                    new SqlParameter("OnSOS", deviceInfo.OnSOS),
                    new SqlParameter("OnLowBattery", deviceInfo.OnLowBattery),
                    new SqlParameter("OnPowerCut", deviceInfo.OnPowerCut),
                    new SqlParameter("OnShock", deviceInfo.OnShock),
                    new SqlParameter("OnCharge", deviceInfo.OnCharge),
                    new SqlParameter("OnDefence", deviceInfo.OnDefence),
                    new SqlParameter("VoltageLevel", deviceInfo.VoltageLevel),
                    new SqlParameter("SignalStrengthLevel", deviceInfo.SignalStrengthLevel),
                    new SqlParameter("AlarmType", deviceInfo.AlarmType)
                };

                _DAL.Data.StoreData_ExecuteNonQuery(_DAL.DataBase.TcpServer, System.Data.CommandType.StoredProcedure, "T_StoreDeviceData", dbParams);

                log.InfoFormat("{0}/SaveData: Finished", _fileNm);
            }
            catch (Exception ex)
            {
                log.ErrorFormat("{0}/SaveData: {1}", _fileNm, ex);
            }
            return true;
        }
Exemple #8
0
        public static DeviceInfo GetDeviceById(string deviceId)
        {
            DeviceInfo di = null;
            devicePoolMutex.WaitOne();
            {
                di = DevicePool.Where(d => d.DeviceId == deviceId).FirstOrDefault();
                if (di == null)
                {
                    di = new DeviceInfo()
                    {
                        DeviceId = deviceId
                    };
                    //DevicePool.Add(di);
                }

                di.ToSendRawData = new byte[0];
            }
            devicePoolMutex.ReleaseMutex();
            return di;
        }
Exemple #9
0
        public static DeviceInfo Dispose(DeviceInfo deviceInfo)
        {
            deviceInfo.DeviceId = null;
            deviceInfo.IMEI = null;
            deviceInfo.CommandType = DeviceCommandType.None;
            deviceInfo.StatusCode = null;
            deviceInfo.Latitude = null;
            deviceInfo.Longitude = null;
            deviceInfo.Speed = null;
            deviceInfo.Direction = null;
            deviceInfo.Altitude = null;
            deviceInfo.Mileage = null;
            deviceInfo.ValidData = null;
            deviceInfo.FullAddress = null;
            deviceInfo.Payload = null;
            deviceInfo.UnParsedPayload = null;
            deviceInfo.Distance = null;
            deviceInfo.Odometer = null;
            deviceInfo.OnLowBattery = null;
            deviceInfo.OnIgnition = null;
            deviceInfo.OnAc = null;
            deviceInfo.OnGps = null;
            deviceInfo.UnKnown = null;
            deviceInfo.DeviceDataTime = null;
            deviceInfo.GeozoneIndex = null;
            deviceInfo.GeozoneID = null;
            deviceInfo.TrackerIp = null;
            deviceInfo.TrackerConnectedTime = null;
            deviceInfo.TrackerDataActionTime = null;
            deviceInfo.TrackerDataParsedTime = null;

            deviceInfo.ReplyMessage = null;

            deviceInfo.RawData = new byte[0];
            deviceInfo.ToSendRawData = null;

            deviceInfo.ParserStatus = ProtocolParserStatus.Initialized;
            return deviceInfo;
        }
Exemple #10
0
 public static DeviceInfo CopyLastDataOfDevice(DeviceInfo deviceInfo)
 {
     DeviceInfo di = GetDeviceById(deviceInfo.DeviceId);
     di.CommandType = deviceInfo.CommandType;
     di.ToSendRawData = deviceInfo.ToSendRawData;
     di.TrackerDataParsedTime = deviceInfo.TrackerDataParsedTime;
     di.ParserStatus = deviceInfo.ParserStatus;
     di.TrackerIp = deviceInfo.TrackerIp;
     di.DeviceDataTime = deviceInfo.DeviceDataTime;
     di.TrackerConnectedTime = deviceInfo.TrackerConnectedTime;
     di.TrackerDataActionTime = deviceInfo.TrackerDataActionTime;
     di.Payload = string.Empty;
     return di;
 }
Exemple #11
0
        public static DeviceInfo ClearProcessedData(DeviceInfo deviceInfo)
        {
            log.DebugFormat("{0}/SaveData:", _fileNm);

            deviceInfo.CommandType = DeviceCommandType.None;
            //deviceInfo.StatusCode = string.Empty;
            //deviceInfo.Latitude = string.Empty;
            //deviceInfo.Longitude = string.Empty;
            //deviceInfo.Speed = string.Empty;
            //deviceInfo.Direction = string.Empty;
            //deviceInfo.Altitude = string.Empty;
            //deviceInfo.Mileage = string.Empty;
            //deviceInfo.ValidData = string.Empty;
            //deviceInfo.FullAddress = string.Empty;
            //deviceInfo.UnParsedPayload = string.Empty;
            //deviceInfo.Distance = string.Empty;
            deviceInfo.Odometer = 0;
            //deviceInfo.OnBattery = null;
            //deviceInfo.OnIgnition = null;
            //deviceInfo.OnAc = null;
            //deviceInfo.OnGps = null;
            //deviceInfo.UnKnown = string.Empty;
            //deviceInfo.GeozoneIndex = string.Empty;
            //deviceInfo.GeozoneID = string.Empty;
            //deviceInfo.DeviceDataTime = null;
            //deviceInfo.GeozoneIndex = null;
            //deviceInfo.GeozoneID = null;
            //deviceInfo.TrackerIp = null;
            //deviceInfo.TrackerDataActionTime = null;
            //deviceInfo.TrackerDataParsedTime = null;

            deviceInfo.ReplyMessage = null;

            deviceInfo.RawData = new byte[0];
            //deviceInfo.ToSendRawData = null;

            deviceInfo.ParserStatus = ProtocolParserStatus.Initialized;

            return deviceInfo;
        }
Exemple #12
0
 public override DeviceInfo Parse(DeviceInfo deviceInfo)
 {
     deviceInfo = SetDataParsedTime(deviceInfo);
     deviceInfo.ParserStatus = ProtocolParserStatus.Parsed;
     return deviceInfo;
 }
Exemple #13
0
        public DeviceInfo Parse(DeviceInfo deviceInfo)
        {
            deviceInfo.Payload += BitConverter.ToString(deviceInfo.RawData).Replace("-", string.Empty);

            log.DebugFormat("Device IP: {0}, payLoad: {1}", deviceInfo.TrackerIp, deviceInfo.Payload);

            if (deviceInfo.Payload.Length < REQ_MIN_Length())
            {
                deviceInfo.ParserStatus = ProtocolParserStatus.InSufficientData;
                return deviceInfo;
            }

            if (deviceInfo.Payload.Substring(0, 4) == "7878")
            {
                deviceInfo.Payload = deviceInfo.Payload.Substring(4);

                int packetLength = GetNumber(deviceInfo.Payload.Substring(0, 2));
                deviceInfo.Payload = deviceInfo.Payload.Substring(2);

                if (deviceInfo.Payload.Substring(0, 2) == "01")
                {
                    // Login message

                    deviceInfo.Payload = deviceInfo.Payload.Substring(2);

                    deviceInfo.DeviceId = deviceInfo.Payload.Substring(0, 16);
                    deviceInfo.Payload = deviceInfo.Payload.Substring(16);

                    byte[] payloadByte = { 0x78, 0x78, 0x05, 0x01, 0x00, 0x01, 0xD9, 0xDC, 0x0D, 0x0A };
                    deviceInfo.ToSendRawData = payloadByte;
                }
                else if (deviceInfo.Payload.Substring(0, 2) == "12")
                {
                    deviceInfo.Payload = deviceInfo.Payload.Substring(2);

                    var datetime = DateTime.UtcNow.Year.ToString().Substring(0, 2);
                    datetime += GetNumberString(deviceInfo.Payload.Substring(0, 12));
                    datetime = datetime.Insert(6, "-");
                    datetime = datetime.Insert(4, "-") + " ";

                    deviceInfo.Payload = deviceInfo.Payload.Substring(12);

                    // Parse Sinal

                    var gsQty = deviceInfo.Payload = deviceInfo.Payload.Substring(0, 2);
                    deviceInfo.Payload = deviceInfo.Payload.Substring(2);

                    // Parse lat
                    //0x02 0x6B 0x3F 0x3E
                    // To 40582974(Decimal)= 26B3F3E(Hexadecimal)
                    //22º32.7658‟=(22X60+32.7658)X30000=40582974

                    var latDec = Decimal.Parse(GetNumberString(deviceInfo.Payload.Substring(0, 8)));
                    decimal latDecN = latDec / decimal.Parse("30000");
                    decimal latDegT = latDecN / decimal.Parse("60");
                    decimal latDeg = Math.Truncate(latDegT);
                    string lat = latDeg.ToString() + ((latDegT - latDeg) * decimal.Parse("60")).ToString("00.0000");

                    //To convert Seconds to decimals, divide by 3600.
                    //Decimal value = Seconds/3600
                    //As an example, a latitude of 122 degrees 45 minutes 45 seconds North is equal to 122.7625 degrees North.

                    //So the complete formula looks similar the following:
                    //Decimal value = Degrees + (Minutes/60) + (Seconds/3600)

                    deviceInfo.Latitude = lat;
                    deviceInfo.Payload = deviceInfo.Payload.Substring(8);

                    var lanDec = Decimal.Parse(GetNumberString(deviceInfo.Payload.Substring(0, 8)));
                    decimal lanDecN = latDec / decimal.Parse("30000");
                    decimal lanDegT = latDecN / decimal.Parse("60");
                    decimal lanDeg = Math.Truncate(latDegT);
                    string longt = latDeg.ToString() + ((latDegT - latDeg) * decimal.Parse("60")).ToString("00.0000");

                    deviceInfo.Longitude = longt;
                    //var lan = deviceInfo.PayLoad = deviceInfo.PayLoad.Substring(0, 8);
                    deviceInfo.Payload = deviceInfo.Payload.Substring(8);

                    var speed = deviceInfo.Payload = deviceInfo.Payload.Substring(0, 2);
                    deviceInfo.Payload = deviceInfo.Payload.Substring(2);

                    var cStatus = deviceInfo.Payload = deviceInfo.Payload.Substring(0, 4);
                    deviceInfo.Payload = deviceInfo.Payload.Substring(4);

                    var mcc = deviceInfo.Payload = deviceInfo.Payload.Substring(0, 4);
                    deviceInfo.Payload = deviceInfo.Payload.Substring(4);

                    var mnc = deviceInfo.Payload = deviceInfo.Payload.Substring(0, 2);
                    deviceInfo.Payload = deviceInfo.Payload.Substring(2);

                    var lac = deviceInfo.Payload = deviceInfo.Payload.Substring(0, 4);
                    deviceInfo.Payload = deviceInfo.Payload.Substring(4);

                    var cellId = deviceInfo.Payload = deviceInfo.Payload.Substring(0, 6);
                    deviceInfo.Payload = deviceInfo.Payload.Substring(6);

                    var serialNo = deviceInfo.Payload = deviceInfo.Payload.Substring(0, 4);
                    deviceInfo.Payload = deviceInfo.Payload.Substring(4);

                    //byte[] payloadByte = { 0x78, 0x78, 0x05, 0x01, 0x00, 0x01, 0xD9, 0xDC, 0x0D, 0x0A };
                    //deviceInfo.TrackerSendPayLoadBytes = payloadByte;
                }

            }
            else
            {
                // Invalid message
            }

            deviceInfo = SetDataParsedTime(deviceInfo);
            deviceInfo.ParserStatus = ProtocolParserStatus.Parsed;
            return deviceInfo;
        }
Exemple #14
0
 protected virtual DeviceInfo SetDataParsedTime(DeviceInfo di)
 {
     di.TrackerDataParsedTime = DateTime.UtcNow;
     return di;
 }
Exemple #15
0
 // Note: This method needs to handles the following:
 // Payload bytes may be only half due to connectivity issue
 // Payload bytes may contains 2 continuous string
 public abstract DeviceInfo Parse(DeviceInfo deviceInfo);
Exemple #16
0
        public static DeviceInfo Process(Tracker.Parser.Protocol protocolParser, DeviceInfo deviceInfo)
        {
            log.InfoFormat("{0}/Process:", _fileNm);

            bool IsSequenceFirst = true;

            log.DebugFormat("{0}/Process: IsSequenceFirst: {1}", _fileNm, IsSequenceFirst);

            Console.WriteLine("{0} >> {1}", deviceInfo.TrackerIp, BitConverter.ToString(deviceInfo.RawData));

            while ((IsSequenceFirst == true) ||
                (deviceInfo.Payload != null && deviceInfo.Payload.Length >= protocolParser.REQ_MIN_Length()))
            {
                log.DebugFormat("{0}/Process: IsSequenceFirst: {1}, deviceInfo.Payload: {2}, REQ_MIN_Length: {3}",
                    _fileNm, IsSequenceFirst, deviceInfo.Payload, protocolParser.REQ_MIN_Length());

                IsSequenceFirst = false;

                deviceInfo.ParserStatus = ProtocolParserStatus.Initialized;

                bool IsDeviceNeedToIdentify = false;
                if (string.IsNullOrWhiteSpace(deviceInfo.DeviceId))
                {
                    // new connection, first request
                    IsDeviceNeedToIdentify = true;
                }

                deviceInfo = protocolParser.Parse(deviceInfo);

                if (IsDeviceNeedToIdentify)
                {
                    deviceInfo = DeviceData.CopyLastDataOfDevice(deviceInfo);
                }

                log.DebugFormat("{0}/Process: deviceInfo.ParserStatus: {1}", _fileNm, deviceInfo.ParserStatus);

                if (deviceInfo.ParserStatus == ProtocolParserStatus.Parsed)
                {
                    if (DeviceData.IsActiveDevice(deviceInfo.DeviceId))
                    {
                        DeviceData.AddActiveDevice(deviceInfo.DeviceId);
                        DeviceData.SaveData(deviceInfo);
                    }
                    else
                    {
                        DeviceData.AddUnknownDevice(deviceInfo.DeviceId);
                    }

                    deviceInfo.ParserStatus = ProtocolParserStatus.Saved;

                    log.DebugFormat("{0}/Process: deviceInfo.ParserStatus: {1}", _fileNm, deviceInfo.ParserStatus);

                    deviceInfo = DeviceData.ClearProcessedData(deviceInfo);
                    deviceInfo.ParserStatus = ProtocolParserStatus.Finished;

                    log.DebugFormat("{0}/Process: deviceInfo.ParserStatus: {1}", _fileNm, deviceInfo.ParserStatus);
                }
                else
                {
                    break;
                }
            }
            log.InfoFormat("{0}/Process: Finished", _fileNm);
            return deviceInfo;
        }