Ejemplo n.º 1
0
        private TLFuelMilstoneEvent ProceedTNCANFilters(CreateTeltonikaGps data)
        {
            var fuelLevel = default(UInt32?);
            var milestone = default(UInt32?);
            var fuelUsed  = default(UInt32?);

            if (data.AllIoElements != null &&
                data.AllIoElements.ContainsKey(TNIoProperty.High_resolution_total_vehicle_distance_X))
            {
                milestone = Convert.ToUInt32(data.AllIoElements[TNIoProperty.High_resolution_total_vehicle_distance_X]);
            }

            if (data.AllIoElements != null && data.AllIoElements.ContainsKey(TNIoProperty.Engine_total_fuel_used))
            {
                fuelUsed = Convert.ToUInt32(data.AllIoElements[TNIoProperty.Engine_total_fuel_used]);
            }

            if (data.AllIoElements != null && data.AllIoElements.ContainsKey(TNIoProperty.Fuel_level_1_X))
            {
                fuelLevel = Convert.ToUInt32(data.AllIoElements[TNIoProperty.Fuel_level_1_X]);
            }
            // ReSharper disable once ComplexConditionExpression
            if (fuelLevel != default(UInt32) && fuelLevel > 0 && fuelUsed > 0)
            {
                return new TLFuelMilstoneEvent
                       {
                           FuelConsumption = Convert.ToInt32(fuelUsed),
                           Milestone       = Convert.ToInt32(milestone),
                           DateTimeUtc     = data.DateTimeUtc,
                           FuelLevel       = Convert.ToInt32(fuelLevel)
                       }
            }
            ;
            return(default(TLFuelMilstoneEvent));
        }
Ejemplo n.º 2
0
        public PositionViewModel(CreateTeltonikaGps tk103Gps, Vehicle vehicle, GeofenceHelper.Position lasPosition)
        {
            double dir = 0;

            if (Math.Abs(lasPosition.Latitude - tk103Gps.Lat) > 0.0)
            {
                if (Math.Abs(lasPosition.Longitude - tk103Gps.Long) > 0.0)
                {
                    dir = GetDirection(tk103Gps, lasPosition);
                }
            }

            Latitude  = tk103Gps.Lat;
            Longitude = tk103Gps.Long;
            Address   = tk103Gps.Address;
            IMEI      = tk103Gps.Imei;
            // SerialNumber = tk103Gps.s;
            //Direction = tk103Gps.Address
            Speed        = tk103Gps.Speed;
            VehicleName  = vehicle.VehicleName;
            VehicleId    = vehicle.Id.ToString();
            CustomerName = vehicle.Customer?.Name;
            TimeStampUtc = tk103Gps.Timestamp;
            SetVehicleImage(vehicle, dir);
        }
Ejemplo n.º 3
0
 private async Task <Box> Getbox(CreateTeltonikaGps context)
 {
     using (var contextFScope = DbContextScopeFactory.Create())
     {
         _db = contextFScope.DbContexts.Get <SmartFleetObjectContext>();
         return(await _db.Boxes.Include(x => x.Vehicle).SingleOrDefaultAsync(b => b.Imei == context.Imei));
     }
 }
Ejemplo n.º 4
0
        private static double GetDirection(CreateTeltonikaGps tk103Gps, GeofenceHelper.Position lastPos)
        {
            var dir = !lastPos.Equals(default(GeofenceHelper.Position))
                ? GeofenceHelper.DegreeBearing(lastPos.Latitude, lastPos.Longitude, tk103Gps.Lat, tk103Gps.Long)
                : 0;

            return(dir);
        }
Ejemplo n.º 5
0
        public List <TlIdentifierEvent> ProceedDriverCardDetection(CreateTeltonikaGps data, Guid customerId)
        {
            var identifiers  = new List <TlIdentifierEvent>();
            var driverStrBld = new StringBuilder();

            if (data.AllIoElements != null &&
                data.AllIoElements.ContainsKey(TNIoProperty.KLN_Driver_1_Identification_part1) &&
                data.AllIoElements.ContainsKey(TNIoProperty.KLN_Driver_1_Identification_part2))
            {
                driverStrBld.Append(Encoding.ASCII
                                    .GetString(BitConverter
                                               .GetBytes(Convert.ToUInt64(data.AllIoElements[TNIoProperty.KLN_Driver_1_Identification_part1]))
                                               .Reverse().ToArray()).TrimEnd((Char)0));
                driverStrBld.Append(Encoding.ASCII
                                    .GetString(BitConverter
                                               .GetBytes(Convert.ToUInt64(data.AllIoElements[TNIoProperty.KLN_Driver_1_Identification_part2]))
                                               .Reverse().ToArray()).TrimEnd((Char)0));

                if (driverStrBld.Length == 16)
                {
                    identifiers.Add(new TlIdentifierEvent
                    {
                        CustomerId       = customerId,
                        IdentifierNumber = driverStrBld.ToString()
                    });
                }
            }

            if (data.AllIoElements == null ||
                !data.AllIoElements.ContainsKey(TNIoProperty.KLN_Driver_2_Identification_part1) ||
                !data.AllIoElements.ContainsKey(TNIoProperty.KLN_Driver_2_Identification_part2))
            {
                return(identifiers);
            }

            driverStrBld = new StringBuilder();
            driverStrBld.Append(Encoding.ASCII
                                .GetString(BitConverter
                                           .GetBytes(Convert.ToUInt64(data.AllIoElements[TNIoProperty.KLN_Driver_2_Identification_part1]))
                                           .Reverse().ToArray()).TrimEnd((Char)0));
            driverStrBld.Append(Encoding.ASCII
                                .GetString(BitConverter
                                           .GetBytes(Convert.ToUInt64(data.AllIoElements[TNIoProperty.KLN_Driver_2_Identification_part2]))
                                           .Reverse().ToArray()).TrimEnd((Char)0));

            if (driverStrBld.Length == 16)
            {
                identifiers.Add(new TlIdentifierEvent
                {
                    CustomerId       = customerId,
                    IdentifierNumber = driverStrBld.ToString()
                });
            }

            return(identifiers);
        }
Ejemplo n.º 6
0
        private async Task <Box> GetBoxAsync(CreateTeltonikaGps context)
        {
            using (var contextFScope = DbContextScopeFactory.Create())
            {
                _db = contextFScope.DbContexts.Get <SmartFleetObjectContext>();
                var box = await _db.Boxes.Include(x => x.Vehicle).FirstOrDefaultAsync(b => b.Imei == context.Imei)
                          .ConfigureAwait(false);

                return(box);
            }
        }
Ejemplo n.º 7
0
 private TLExcessSpeedEvent ProceedTLSpeedingAlert(CreateTeltonikaGps data, Guid vehicleId, Guid?customerId)
 {
     return(new TLExcessSpeedEvent
     {
         Id = Guid.NewGuid(),
         CustomerId = customerId,
         VehicleId = vehicleId,
         VehicleEventType = VehicleEvent.EXCESS_SPEED,
         EventUtc = data.DateTimeUtc,
         Latitude = (float?)data.Lat,
         Longitude = (float?)data.Long,
         Address = data.Address,
         Speed = data.Speed
     });
 }
Ejemplo n.º 8
0
 public PositionViewModel(CreateTeltonikaGps tk103Gps, Vehicle vehicle, SmartFleetObjectContext db, Guid boxId)
 {
     //  var dir = GetDirection(tk103Gps, db, boxId);
     Latitude  = tk103Gps.Lat;
     Longitude = tk103Gps.Long;
     Address   = tk103Gps.Address;
     IMEI      = tk103Gps.Imei;
     // SerialNumber = tk103Gps.s;
     //Direction = tk103Gps.Address
     Speed        = tk103Gps.Speed;
     VehicleName  = vehicle.VehicleName;
     VehicleId    = vehicle.Id.ToString();
     CustomerName = vehicle.Customer?.Name;
     TimeStampUtc = tk103Gps.Timestamp;
     //SetVehicleImage(vehicle ,dir);
 }
Ejemplo n.º 9
0
 private static void InitAllIoElements(CreateTeltonikaGps context)
 {
     context.AllIoElements = new Dictionary <TNIoProperty, long>();
     foreach (var ioElement in context.IoElements_1B)
     {
         context.AllIoElements.Add((TNIoProperty)ioElement.Key, ioElement.Value);
     }
     foreach (var ioElement in context.IoElements_2B)
     {
         context.AllIoElements.Add((TNIoProperty)ioElement.Key, ioElement.Value);
     }
     foreach (var ioElement in context.IoElements_4B)
     {
         context.AllIoElements.Add((TNIoProperty)ioElement.Key, ioElement.Value);
     }
     foreach (var ioElement in context.IoElements_8B)
     {
         context.AllIoElements.Add((TNIoProperty)ioElement.Key, ioElement.Value);
     }
 }
Ejemplo n.º 10
0
 private Task <Box> GetboxAsync(CreateTeltonikaGps command)
 {
     return(_dbContext.Boxes.Include(x => x.Vehicle).FirstOrDefaultAsync(b => b.Imei == command.Imei));
 }
Ejemplo n.º 11
0
        private TLEcoDriverAlertEvent ProceedEcoDriverEvents(CreateTeltonikaGps data, Guid vehicleId, Guid?customerId)
        {
            var @event = default(TLEcoDriverAlertEvent);

            if (data.DataEventIO == (int)TNIoProperty.Engine_speed_X)
            {
                @event = new TLEcoDriverAlertEvent
                {
                    Id               = Guid.NewGuid(),
                    CustomerId       = customerId,
                    VehicleId        = vehicleId,
                    VehicleEventType = VehicleEvent.EXCESS_ENGINE_SPEED,
                    EventUtc         = data.DateTimeUtc,
                    Latitude         = (float?)data.Lat,
                    Longitude        = (float?)data.Long,
                    Address          = data.Address
                                       //Speed = data.Speed
                }
            }
            ;
            else if (data.AllIoElements.ContainsKey(TNIoProperty.ECO_driving_type))
            {
                switch (Convert.ToByte(data.AllIoElements[TNIoProperty.ECO_driving_type]))
                {
                case 1:
                    if (Convert.ToByte(data.AllIoElements[TNIoProperty.ECO_driving_value]) > 31)
                    {
                        @event = new TLEcoDriverAlertEvent
                        {
                            Id               = Guid.NewGuid(),
                            CustomerId       = customerId,
                            VehicleId        = vehicleId,
                            VehicleEventType = VehicleEvent.EXCESS_ACCELERATION,
                            EventUtc         = data.DateTimeUtc,
                            Latitude         = (float?)data.Lat,
                            Longitude        = (float?)data.Long,
                            Address          = data.Address
                                               //Speed = data.Speed
                        }
                    }
                    ;
                    break;

                case 2:
                    if (Convert.ToByte(data.AllIoElements[TNIoProperty.ECO_driving_value]) > 38)
                    {
                        @event = new TLEcoDriverAlertEvent
                        {
                            Id               = Guid.NewGuid(),
                            CustomerId       = customerId,
                            VehicleId        = vehicleId,
                            VehicleEventType = VehicleEvent.SUDDEN_BRAKING,
                            EventUtc         = data.DateTimeUtc,
                            Latitude         = (float?)data.Lat,
                            Longitude        = (float?)data.Long,
                            Address          = data.Address
                                               //Speed = data.Speed
                        }
                    }
                    ;
                    break;

                case 3:
                    if (Convert.ToByte(data.AllIoElements[TNIoProperty.ECO_driving_value]) > 45)
                    {
                        @event = new TLEcoDriverAlertEvent
                        {
                            Id               = Guid.NewGuid(),
                            CustomerId       = customerId,
                            VehicleId        = vehicleId,
                            VehicleEventType = VehicleEvent.FAST_CORNER,
                            EventUtc         = data.DateTimeUtc,
                            Latitude         = (float?)data.Lat,
                            Longitude        = (float?)data.Long,
                            Address          = data.Address
                                               //Speed = data.Speed
                        }
                    }
                    ;
                    break;

                default:
                    break;
                }
            }
            return(@event);
        }
Ejemplo n.º 12
0
        public List <CreateTeltonikaGps> DecodeAvl(List <byte> receiveBytes, string imei)
        {
            receiveBytes.Skip(4).Take(4).ToList().ForEach(delegate(byte b)
            {
            });
            int numberOfData = Convert.ToInt32(receiveBytes.Skip(9).Take(1).ToList()[0]);

            var results      = new List <CreateTeltonikaGps>();
            int tokenAddress = 10;

            for (int n = 0; n < numberOfData; n++)
            {
                CreateTeltonikaGps gpsData      = new CreateTeltonikaGps();
                string             hexTimeStamp = string.Empty;
                receiveBytes.Skip(tokenAddress).Take(8).ToList().ForEach(delegate(byte b)
                {
                    hexTimeStamp += String.Format("{0:X2}", b);
                });
                long timeSt = Convert.ToInt64(hexTimeStamp, 16);

                DateTime origin    = new DateTime(1970, 1, 1, 0, 0, 0, 0);
                DateTime timestamp = origin.AddMilliseconds(Convert.ToDouble(timeSt));


                int priority = Convert.ToInt32(receiveBytes.Skip(tokenAddress + 8).Take(1).ToList()[0]);

                string longt = string.Empty;
                receiveBytes.Skip(tokenAddress + 9).Take(4).ToList().ForEach(delegate(byte b)
                {
                    longt += String.Format("{0:X2}", b);
                });
                double longtitude = ((double)Convert.ToInt32(longt, 16)) / 10000000;

                string lat = string.Empty;
                receiveBytes.Skip(tokenAddress + 13).Take(4).ToList().ForEach(delegate(byte b)
                {
                    lat += String.Format("{0:X2}", b);
                });
                double latitude = ((double)Convert.ToInt32(lat, 16)) / 10000000;

                string alt = string.Empty;
                receiveBytes.Skip(tokenAddress + 17).Take(2).ToList().ForEach(delegate(byte b)
                {
                    alt += String.Format("{0:X2}", b);
                });
                int altitude = Convert.ToInt32(alt, 16);

                string ang = string.Empty;
                receiveBytes.Skip(tokenAddress + 19).Take(2).ToList().ForEach(delegate(byte b)
                {
                    ang += String.Format("{0:X2}", b);
                });
                int angle      = Convert.ToInt32(ang, 16);
                int satellites = Convert.ToInt32(receiveBytes.Skip(tokenAddress + 21).Take(1).ToList()[0]);

                string sp = string.Empty;
                receiveBytes.Skip(tokenAddress + 22).Take(2).ToList().ForEach(delegate(byte b)
                {
                    sp += String.Format("{0:X2}", b);
                });
                int speed = Convert.ToInt32(sp, 16);

                byte eventIoElementId =
                    (byte)Convert.ToInt32(receiveBytes.Skip(tokenAddress + 24).Take(1).ToList()[0]);
                gpsData.EventIoElementId = eventIoElementId;

                int ioElementInRecord = Convert.ToInt32(receiveBytes.Skip(tokenAddress + 25).Take(1).ToList()[0]);


                if (ioElementInRecord != 0)
                {
                    int currentCursor = tokenAddress + 26;

                    int ioElements_1BQuantity = Convert.ToInt32(receiveBytes.Skip(currentCursor).Take(1).ToList()[0]);


                    for (int io1 = 0; io1 < ioElements_1BQuantity; io1++)
                    {
                        var parameterId =
                            (byte)Convert.ToInt32(receiveBytes.Skip(currentCursor + 1 + io1 * 2).Take(1).ToList()[0]);
                        var ioElement_1B =
                            (byte)Convert.ToInt32(receiveBytes.Skip(currentCursor + 2 + io1 * 2).Take(1).ToList()[0]);
                        gpsData.IoElements_1B.Add(parameterId, ioElement_1B);
                    }
                    currentCursor += ioElements_1BQuantity * 2 + 1;

                    int ioElements_2BQuantity = Convert.ToInt32(receiveBytes.Skip(currentCursor).Take(1).ToList()[0]);

                    for (int io2 = 0; io2 < ioElements_2BQuantity; io2++)
                    {
                        var parameterId =
                            (byte)Convert.ToInt32(receiveBytes.Skip(currentCursor + 1 + io2 * 3).Take(1).ToList()[0]);
                        string value = string.Empty;
                        receiveBytes.Skip(currentCursor + 2 + io2 * 3).Take(2).ToList().ForEach(delegate(byte b)
                        {
                            value += String.Format("{0:X2}", b);
                        });
                        var ioElement_2B = Convert.ToInt16(value, 16);
                        gpsData.IoElements_2B.Add(parameterId, ioElement_2B);
                    }
                    currentCursor += ioElements_2BQuantity * 3 + 1;

                    int ioElements_4BQuantity = Convert.ToInt32(receiveBytes.Skip(currentCursor).Take(1).ToList()[0]);

                    for (int io4 = 0; io4 < ioElements_4BQuantity; io4++)
                    {
                        var parameterId =
                            (byte)Convert.ToInt32(receiveBytes.Skip(currentCursor + 1 + io4 * 5).Take(1).ToList()[0]);
                        string value = string.Empty;
                        receiveBytes.Skip(currentCursor + 2 + io4 * 5).Take(4).ToList().ForEach(delegate(byte b)
                        {
                            value += String.Format("{0:X2}", b);
                        });
                        var ioElement_4B = Convert.ToInt32(value, 16);
                        gpsData.IoElements_4B.Add(parameterId, ioElement_4B);
                    }
                    currentCursor += ioElements_4BQuantity * 5 + 1;

                    int ioElements_8BQuantity = Convert.ToInt32(receiveBytes.Skip(currentCursor).Take(1).ToList()[0]);

                    for (int io8 = 0; io8 < ioElements_8BQuantity; io8++)
                    {
                        var parameterId =
                            (byte)Convert.ToInt32(receiveBytes.Skip(currentCursor + 1 + io8 * 9).Take(1).ToList()[0]);
                        string value = string.Empty;
                        receiveBytes.Skip(currentCursor + 2 + io8 * 9).Take(8).ToList().ForEach(delegate(byte b)
                        {
                            value += String.Format("{0:X2}", b);
                        });
                        var ioElement_8B = Convert.ToInt64(value, 16);
                        gpsData.IoElements_8B.Add(parameterId, ioElement_8B);
                    }

                    tokenAddress += 30 + ioElements_1BQuantity * 2 +
                                    ioElements_2BQuantity * 3 + ioElements_4BQuantity * 5
                                    + ioElements_8BQuantity * 9;
                }
                else
                {
                    tokenAddress += 30;
                }

                //  Data dt = new Data();

                gpsData.Altitude  = (short)altitude;
                gpsData.Direction = (short)angle;
                gpsData.Lat       = latitude;
                gpsData.Long      = longtitude;
                gpsData.Priority  = (byte)priority;
                gpsData.Satellite = (byte)satellites;
                gpsData.Speed     = speed;
                gpsData.Timestamp = timestamp;
                gpsData.Imei      = imei.Substring(0, 15);
                results.Add(gpsData);
                //  dt.SaveGPSPositionFMXXXX(gpsData);
            }
            //CRC for check of data correction and request again data from device if it not correct
            return(results);
        }
Ejemplo n.º 13
0
 public EcoDriveService(CreateTeltonikaGps data)
 {
     _data = data;
 }
Ejemplo n.º 14
0
        private static List <CreateTeltonikaGps> ParsePositions(Byte[] byteBuffer)
        {
            var index = 0;

            index += 7;
            uint dataSize = byteBuffer[index];

            index++;
            uint codecID = byteBuffer[index];

            if (codecID == CODEC_FMXXX)
            {
                index++;
                uint numberOfData = byteBuffer[index];

                Trace.WriteLine(string.Format("{0} {1} {2} ", codecID, numberOfData, dataSize));

                List <CreateTeltonikaGps> result = new List <CreateTeltonikaGps>();

                index++;
                for (int i = 0; i < numberOfData; i++)
                {
                    CreateTeltonikaGps position = new CreateTeltonikaGps();
                    position.Imei = _IMEI;
                    Trace.WriteLine(_IMEI);
                    var timestamp = Int64.Parse(Parsebytes(byteBuffer, index, 8), System.Globalization.NumberStyles.HexNumber);
                    index += 8;
                    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);

                    position.Timestamp = origin.AddMilliseconds(timestamp);

                    var Preority = byte.Parse(Parsebytes(byteBuffer, index, 1), System.Globalization.NumberStyles.HexNumber);
                    position.Priority = Preority;
                    index++;

                    position.Long = Int32.Parse(Parsebytes(byteBuffer, index, 4), System.Globalization.NumberStyles.HexNumber) / 10000000.0;
                    index        += 4;

                    position.Lat = Int32.Parse(Parsebytes(byteBuffer, index, 4), System.Globalization.NumberStyles.HexNumber) / 10000000.0;
                    index       += 4;

                    var Altitude = Int16.Parse(Parsebytes(byteBuffer, index, 2), System.Globalization.NumberStyles.HexNumber);
                    index            += 2;
                    position.Altitude = Altitude;
                    var dir = Int16.Parse(Parsebytes(byteBuffer, index, 2), System.Globalization.NumberStyles.HexNumber);

                    if (dir < 90)
                    {
                        position.Direction = 1;           // east
                    }
                    else if (dir == 90)
                    {
                        position.Direction = 2;                //east
                    }
                    else if (dir < 180)
                    {
                        position.Direction = 3;                // east
                    }
                    else if (dir == 180)
                    {
                        position.Direction = 4;                 // ??
                    }
                    else if (dir < 270)
                    {
                        position.Direction = 5;                //west
                    }
                    else if (dir == 270)
                    {
                        position.Direction = 6;                 // west
                    }
                    else if (dir > 270)
                    {
                        position.Direction = 7;                //west
                    }
                    index += 2;

                    var satellite = byte.Parse(Parsebytes(byteBuffer, index, 1), System.Globalization.NumberStyles.HexNumber);
                    index++;

                    if (satellite >= 3)
                    {
                        position.Status = "A";
                    }
                    else
                    {
                        position.Status = "L";
                    }
                    var  speed = Parsebytes(byteBuffer, index, 2).ToString(CultureInfo.InvariantCulture);
                    byte x     = Convert.ToByte(speed, 16);
                    position.Speed = Convert.ToDouble(x);
                    index         += 2;

                    int ioEvent = byte.Parse(Parsebytes(byteBuffer, index, 1), System.Globalization.NumberStyles.HexNumber);
                    index++;
                    int ioCount = byte.Parse(Parsebytes(byteBuffer, index, 1), System.Globalization.NumberStyles.HexNumber);
                    index++;
                    //read 1 byte
                    {
                        int cnt = byte.Parse(Parsebytes(byteBuffer, index, 1), System.Globalization.NumberStyles.HexNumber);
                        index++;
                        for (int j = 0; j < cnt; j++)
                        {
                            int id = byte.Parse(Parsebytes(byteBuffer, index, 1), System.Globalization.NumberStyles.HexNumber);
                            index++;
                            //Add output status
                            switch (id)
                            {
                            case ACC:
                            {
                                var value = byte.Parse(Parsebytes(byteBuffer, index, 1), System.Globalization.NumberStyles.HexNumber);
                                position.Status += value == 1 ? ",ACC off" : ",ACC on";
                                index++;
                                break;
                            }

                            case DOOR:
                            {
                                var value = byte.Parse(Parsebytes(byteBuffer, index, 1), System.Globalization.NumberStyles.HexNumber);
                                position.Status += value == 1 ? ",door close" : ",door open";
                                index++;
                                break;
                            }

                            case GSM:
                            {
                                var value = byte.Parse(Parsebytes(byteBuffer, index, 1), System.Globalization.NumberStyles.HexNumber);
                                position.Status += string.Format(",GSM {0}", value);
                                index++;
                                break;
                            }

                            case STOP:
                            {
                                var value = byte.Parse(Parsebytes(byteBuffer, index, 1), System.Globalization.NumberStyles.HexNumber);
                                position.StopFlag = value == 1;
                                position.IsStop   = value == 1;

                                index++;
                                break;
                            }

                            case IMMOBILIZER:
                            {
                                var value = byte.Parse(Parsebytes(byteBuffer, index, 1), System.Globalization.NumberStyles.HexNumber);
                                position.Alarm = value == 0 ? "Activate Anti-carjacking success" : "Emergency release success";
                                index++;
                                break;
                            }

                            case GREEDRIVING:
                            {
                                var value = byte.Parse(Parsebytes(byteBuffer, index, 1), System.Globalization.NumberStyles.HexNumber);
                                switch (value)
                                {
                                case 1:
                                {
                                    position.Alarm = "Acceleration intense !!";
                                    break;
                                }

                                case 2:
                                {
                                    position.Alarm = "Freinage brusque !!";
                                    break;
                                }

                                case 3:
                                {
                                    position.Alarm = "Virage serré !!";
                                    break;
                                }

                                default:
                                    break;
                                }
                                index++;
                                break;
                            }

                            default:
                            {
                                index++;
                                break;
                            }
                            }
                        }
                    }

                    //read 2 byte
                    {
                        int cnt = byte.Parse(Parsebytes(byteBuffer, index, 1), System.Globalization.NumberStyles.HexNumber);
                        index++;
                        for (int j = 0; j < cnt; j++)
                        {
                            int id = byte.Parse(Parsebytes(byteBuffer, index, 1), System.Globalization.NumberStyles.HexNumber);
                            index++;



                            switch (id)
                            {
                            case Analog:
                            {
                                var value = Int16.Parse(Parsebytes(byteBuffer, index, 2), System.Globalization.NumberStyles.HexNumber);
                                if (value < 12)
                                {
                                    position.Alarm += string.Format("Low voltage", value);
                                }
                                index += 2;
                                break;
                            }

                            case SPEED:
                            {
                                var value = Int16.Parse(Parsebytes(byteBuffer, index, 2), System.Globalization.NumberStyles.HexNumber);
                                position.Alarm += string.Format("Speed", value);
                                index          += 2;
                                break;
                            }

                            default:
                            {
                                index += 2;
                                break;
                            }
                            }
                        }
                    }

                    //read 4 byte
                    {
                        int cnt = byte.Parse(Parsebytes(byteBuffer, index, 1), System.Globalization.NumberStyles.HexNumber);
                        index++;
                        for (int j = 0; j < cnt; j++)
                        {
                            int id = byte.Parse(Parsebytes(byteBuffer, index, 1), System.Globalization.NumberStyles.HexNumber);
                            index++;

                            switch (id)
                            {
                            case TEMPERATURE:
                            {
                                var value = Int32.Parse(Parsebytes(byteBuffer, index, 4), System.Globalization.NumberStyles.HexNumber);
                                position.Temprature = value;
                                position.Alarm     += string.Format("Temperature {0}", value);
                                index += 4;
                                break;
                            }

                            case ODOMETER:
                            {
                                var value = Int32.Parse(Parsebytes(byteBuffer, index, 4), System.Globalization.NumberStyles.HexNumber);
                                position.Mileage = value;
                                index           += 4;
                                break;
                            }

                            default:
                            {
                                index += 4;
                                break;
                            }
                            }
                        }
                    }

                    //read 8 byte
                    {
                        int cnt = byte.Parse(Parsebytes(byteBuffer, index, 1), System.Globalization.NumberStyles.HexNumber);
                        index++;
                        for (int j = 0; j < cnt; j++)
                        {
                            int id = byte.Parse(Parsebytes(byteBuffer, index, 1), System.Globalization.NumberStyles.HexNumber);
                            index++;

                            var io = Int64.Parse(Parsebytes(byteBuffer, index, 8), System.Globalization.NumberStyles.HexNumber);
                            position.Status += string.Format(",{0} {1}", id, io);
                            index           += 8;
                        }
                    }

                    result.Add(position);
                    // Console.WriteLine(position.ToString());
                }

                return(result);
            }
            return(null);
        }
Ejemplo n.º 15
0
        public List <CreateTeltonikaGps> DecodeAvl(List <byte> receiveBytes, string imei)
        {
            string hexDataLength = string.Empty;

            receiveBytes.Skip(4).Take(4).ToList().ForEach(delegate(byte b)
            {
                hexDataLength += String.Format("{0:X2}", b);
            });
            int numberOfData = Convert.ToInt32(receiveBytes.Skip(9).Take(1).ToList()[0]);

            int nextPacketStartAddress = 10;
            //  Data dt = new Data();
            List <CreateTeltonikaGps> data = new List <CreateTeltonikaGps>();

            for (int n = 0; n < numberOfData; n++)
            {
                string hexTimeStamp = string.Empty;
                receiveBytes.Skip(nextPacketStartAddress).Take(4).ToList().ForEach(delegate(byte b)
                {
                    hexTimeStamp += String.Format("{0:X2}", b);
                });

                //ShowDiagnosticInfo(bit_30_timestamp);
                var  result = Convert.ToInt64(hexTimeStamp, 16) & 0x3FFFFFFF;
                long timeSt = Convert.ToInt64(result);
                // long timeSt = Convert.ToInt64(Convert.ToString(Convert.ToInt32(hexTimeStamp, 16), 2).Substring(2, 30), 2);
                //long timeSt = Convert.ToInt64(hexTimeStamp.Substring(2, 30), 16);

                // For GH3000 time is seconds from 2007.01.01 00:00:00
                DateTime origin    = new DateTime(2007, 1, 1, 0, 0, 0, 0);
                DateTime timestamp = origin.AddSeconds(Convert.ToDouble(timeSt));

                //DateTime timestamp = DateTime.FromBinary(timeSt);

                int priority =
                    (Convert.ToByte(hexTimeStamp.Substring(0, 2), 16) & 0xC0) /
                    64; //Convert.ToInt32(receiveBytes.Skip(nextPacketStartAddress + 8).Take(1));

                // If ALARM send SMS
                // if (priority == 2)
                //     SMSsender.SendSms(dt.GetAlarmNumberFromModemId(IMEI), "5555555", "Alarm button pressed", 3, true);

                GlobalMask globalMask = (GlobalMask)receiveBytes.Skip(nextPacketStartAddress + 4).Take(1).First();
                GpSmask    gpsMask    = (GpSmask)receiveBytes.Skip(nextPacketStartAddress + 5).Take(1).First();

                CreateTeltonikaGps gpsData = new CreateTeltonikaGps();
                gpsData.Priority    = (byte)priority;
                gpsData.DateTimeUtc = timestamp;
                int gpsElementDataAddress = 0;
                if ((globalMask & GlobalMask.GpSelement) != 0)
                {
                    if ((gpsMask & GpSmask.LatAndLong) != 0)
                    {
                        gpsElementDataAddress = 6;
                        string longt = string.Empty;
                        receiveBytes.Skip(nextPacketStartAddress + gpsElementDataAddress + 4).Take(4).ToList()
                        .ForEach(delegate(byte b) { longt += String.Format("{0:X2}", b); });
                        float longtitude = GetFloatIee754(receiveBytes
                                                          .Skip(nextPacketStartAddress + gpsElementDataAddress + 4).Take(4).ToArray());

                        string lat = string.Empty;
                        receiveBytes.Skip(nextPacketStartAddress + gpsElementDataAddress).Take(4).ToList()
                        .ForEach(delegate(byte b) { lat += String.Format("{0:X2}", b); });
                        float latitude = GetFloatIee754(receiveBytes
                                                        .Skip(nextPacketStartAddress + gpsElementDataAddress).Take(4).ToArray());
                        gpsElementDataAddress += 8;
                        gpsData.Lat            = latitude;
                        gpsData.Long           = longtitude;
                    }

                    if ((gpsMask & GpSmask.Altitude) != 0)
                    {
                        string alt = string.Empty;
                        receiveBytes.Skip(nextPacketStartAddress + gpsElementDataAddress).Take(2).ToList()
                        .ForEach(delegate(byte b) { alt += String.Format("{0:X2}", b); });
                        int altitude = Convert.ToInt32(alt, 16);
                        gpsElementDataAddress += 2;
                        gpsData.Altitude       = (short)altitude;
                    }

                    if ((gpsMask & GpSmask.Angle) != 0)
                    {
                        string ang = string.Empty;
                        receiveBytes.Skip(nextPacketStartAddress + gpsElementDataAddress).Take(1).ToList()
                        .ForEach(delegate(byte b) { ang += String.Format("{0:X2}", b); });
                        int angle = Convert.ToInt32(ang, 16);
                        angle = Convert.ToInt32(angle * 360.0 / 256.0);
                        gpsElementDataAddress += 1;
                        gpsData.Direction      = (short)angle;
                    }

                    if ((gpsMask & GpSmask.Speed) != 0)
                    {
                        string sp = string.Empty;
                        receiveBytes.Skip(nextPacketStartAddress + gpsElementDataAddress).Take(1).ToList()
                        .ForEach(delegate(byte b) { sp += String.Format("{0:X2}", b); });
                        int speed = Convert.ToInt32(sp, 16);
                        gpsElementDataAddress += 1;
                        gpsData.Speed          = (short)speed;
                    }

                    if ((gpsMask & GpSmask.Sattelites) != 0)
                    {
                        int satellites = Convert.ToInt32(receiveBytes
                                                         .Skip(nextPacketStartAddress + gpsElementDataAddress).Take(1).ToList()[0]);
                        gpsElementDataAddress += 1;
                        gpsData.Satellite      = (byte)satellites;
                    }

                    if ((gpsMask & GpSmask.LocalAreaCodeAndCellId) != 0)
                    {
                        string localArea = string.Empty;
                        receiveBytes.Skip(nextPacketStartAddress + gpsElementDataAddress).Take(2).ToList()
                        .ForEach(delegate(byte b) { localArea += String.Format("{0:X2}", b); });
                        int localAreaCode = Convert.ToInt32(localArea, 16);
                        gpsElementDataAddress += 2;
                        gpsData.LocalAreaCode  = (short)localAreaCode;

                        string cell_ID = string.Empty;
                        receiveBytes.Skip(nextPacketStartAddress + gpsElementDataAddress).Take(2).ToList()
                        .ForEach(delegate(byte b) { cell_ID += String.Format("{0:X2}", b); });
                        int cellID = Convert.ToInt32(cell_ID, 16);
                        gpsElementDataAddress += 2;
                        gpsData.CellID         = (short)cellID;
                    }

                    if ((gpsMask & GpSmask.SignalQuality) != 0)
                    {
                        string gsmQua = string.Empty;
                        receiveBytes.Skip(nextPacketStartAddress + gpsElementDataAddress).Take(1).ToList()
                        .ForEach(delegate(byte b) { gsmQua += String.Format("{0:X2}", b); });
                        int gsmSignalQuality = Convert.ToInt32(gsmQua, 16);
                        gpsElementDataAddress   += 1;
                        gpsData.GsmSignalQuality = (byte)gsmSignalQuality;
                    }

                    if ((gpsMask & GpSmask.OperatorCode) != 0)
                    {
                        string opCode = string.Empty;
                        receiveBytes.Skip(nextPacketStartAddress + gpsElementDataAddress).Take(4).ToList()
                        .ForEach(delegate(byte b) { opCode += String.Format("{0:X2}", b); });
                        int operatorCode = Convert.ToInt32(opCode, 16);
                        gpsElementDataAddress += 4;
                        gpsData.OperatorCode   = operatorCode;
                    }
                }

                nextPacketStartAddress += gpsElementDataAddress;
                if ((globalMask & GlobalMask.IO_Element_1B) != 0)
                {
                    byte quantityOfIOelementData = receiveBytes.Skip(nextPacketStartAddress).Take(1).First();
                    nextPacketStartAddress += 1;
                    for (int i = 0; i < quantityOfIOelementData; i++)
                    {
                        byte parameterID    = receiveBytes.Skip(nextPacketStartAddress).Take(1).First();
                        byte parameterValue = receiveBytes.Skip(nextPacketStartAddress + 1).Take(1).First();
                        gpsData.IoElements_1B.Add(parameterID, parameterValue);
                        nextPacketStartAddress += 2;

                        //------------------- end
                    }
                }

                if ((globalMask & GlobalMask.IO_Element_2B) != 0)
                {
                    byte quantityOfIOelementData = receiveBytes.Skip(nextPacketStartAddress).Take(1).First();
                    nextPacketStartAddress += 1;
                    for (int i = 0; i < quantityOfIOelementData; i++)
                    {
                        byte   parameterID = receiveBytes.Skip(nextPacketStartAddress).Take(1).First();
                        string value       = string.Empty;
                        receiveBytes.Skip(nextPacketStartAddress + 1).Take(2).ToList().ForEach(delegate(byte b)
                        {
                            value += String.Format("{0:X2}", b);
                        });
                        short parameterValue = (short)Convert.ToInt32(value, 16);
                        gpsData.IoElements_2B.Add(parameterID, parameterValue);
                        nextPacketStartAddress += 3;
                    }
                }

                if ((globalMask & GlobalMask.IO_Element_4B) != 0)
                {
                    byte quantityOfIOelementData = receiveBytes.Skip(nextPacketStartAddress).Take(1).First();
                    nextPacketStartAddress += 1;
                    for (int i = 0; i < quantityOfIOelementData; i++)
                    {
                        byte   parameterID = receiveBytes.Skip(nextPacketStartAddress).Take(1).First();
                        string value       = string.Empty;
                        receiveBytes.Skip(nextPacketStartAddress + 1).Take(4).ToList().ForEach(delegate(byte b)
                        {
                            value += String.Format("{0:X2}", b);
                        });
                        int parameterValue = Convert.ToInt32(value, 16);
                        gpsData.IoElements_4B.Add(parameterID, parameterValue);
                        nextPacketStartAddress += 5;
                    }
                }

                gpsData.Imei = imei.Substring(0, 15);
                //dt.SaveGPSPositionGH3000(gpsData);
                data.Add(gpsData);
            }

            return(data);
        }