public void TestSaveDevice_IMEI_13845257385757011()
        {
            string imei = "13845257385757011";
            string deviceSn = "000010052";

            var newGeoPointDto = new GeoPointDto
            {
                Id = Guid.NewGuid().ToString(),
                Imei = imei,
                DeviceSn = deviceSn,
                Latitude = 13.7253520,
                Longitude = 100.5794770,
                HeaderTime = DateTime.UtcNow,
                CreateDate = DateTime.Now,
            };

            var locations = reader.GetLocationsByImei(imei, new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 1), new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59));
            var count1 = locations.ToList().Count;
            writer.SaveGeoPointDto(newGeoPointDto);
            locations = reader.GetLocationsByImei(imei, new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 1), new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59));
            var count2 = locations.ToList().Count;

            var ge = count2 > count1;

            ge.Should().Equals(true);
        }
        public int SaveGeoPointDto(GeoPointDto geopoint)
        {
            var database = CreateSpatialDatabase();
            using (Mongo.RequestStart(database))
            {
                var geolocations = database.GetCollection<Geolocation>(Geolocations);
                var geolocation = geopoint.ToGeolocation();
                geolocations.Save(geolocation);

                geolocations.EnsureIndex(IndexKeys.GeoSpatial("loc"));
            }
            return 1;
        }
        public static IList<GeoPointDto> ToGeoPointDto(this IList<Geolocation> geolocationList)
        {
            List<GeoPointDto> result = new List<GeoPointDto>();

            geolocationList.ToList().ForEach(geoPointMsg =>
            {
                var loc = geoPointMsg.LocationJson;
                var locDoc = loc.Replace("0E-19", "0");
                dynamic locJson = JsonConvert.DeserializeObject(locDoc);
                double lat = locJson.lat;
                double lon = locJson.lon;

                var geoPointDto = new GeoPointDto
                {
                    Id = geoPointMsg.Id,

                    HeaderTime = geoPointMsg.HeaderTime,
                    DeviceSn = geoPointMsg.DeviceSN,
                    UniqueJournyId = (ushort)geoPointMsg.JourneyId,
                    Seq = geoPointMsg.Seq,

                    Latitude = lat,
                    Longitude = lon,

                    Altitude = geoPointMsg.Altitude,
                    Groundspeed = geoPointMsg.Groundspeed,
                    NumberOfSatellitesUsed = (byte)geoPointMsg.NumberOfSatellitesUsed,
                    Heading = geoPointMsg.Heading,
                    CreateDate = geoPointMsg.CreateDate,

                    UtcTime = geoPointMsg.UtcTime,
                    FromDate = geoPointMsg.UtcTime.ToLocalTime(),
                    ToDate = geoPointMsg.UtcTime.ToLocalTime(),

                    FromMessage = geoPointMsg.Message,
                    FromMessageJson = geoPointMsg.MessageJson,
                    PacketId = geoPointMsg.PacketId,

                    MapId = geoPointMsg.MapId,

                    Imei = geoPointMsg.Imei,

                };

                result.Add(geoPointDto);
            });

            for (int i = 1; i < result.Count; i++)
            {
                var g1 = result[i - 1];
                var g2 = result[i];

                Vector2D l = new Vector2D
                {
                    X = (double)g1.Latitude,
                    Y = (double)g1.Longitude,
                };

                Vector2D r = new Vector2D
                {
                    X = (double)g2.Latitude,
                    Y = (double)g2.Longitude,
                };

                double distance = 0.0;
                distance = HarversineHelper.Distance(l, r);

                g2.HavDistanceMeters = distance;
            }

            return result;
        }
        public static GeoPointDto ToGeoPointDto(this SqlDataReader rdr)
        {
            var geoPointDto = new GeoPointDto();

            geoPointDto.Id = rdr.GetString(0);
            if (rdr["HeaderTime"] != DBNull.Value)
            {
                geoPointDto.HeaderTime = Convert.ToDateTime(rdr["HeaderTime"]);
            }
            if (rdr["DeviceSn"] != DBNull.Value)
            {
                geoPointDto.DeviceSn = Convert.ToString(rdr["DeviceSn"]);
            }
            if (rdr["JourneyId"] != DBNull.Value)
            {
                geoPointDto.UniqueJournyId = Convert.ToUInt16(rdr["JourneyId"]);
            }
            if (rdr["Seq"] != DBNull.Value)
            {
                geoPointDto.Seq = Convert.ToInt32(rdr["Seq"]);
            }

            if (rdr["Latitude"] != DBNull.Value)
            {
                geoPointDto.Latitude = Convert.ToDouble(rdr["Latitude"]);
            }
            if (rdr["Longitude"] != DBNull.Value)
            {
                geoPointDto.Longitude = Convert.ToDouble(rdr["Longitude"]);
            }

            if (rdr["Altitude"] != DBNull.Value)
            {
                geoPointDto.Altitude = Convert.ToInt32(rdr["Altitude"]);
            }
            if (rdr["Groundspeed"] != DBNull.Value)
            {
                geoPointDto.Groundspeed = Convert.ToInt32(rdr["Groundspeed"]);
            }
            if (rdr["NumberOfSatellitesUsed"] != DBNull.Value)
            {
                geoPointDto.NumberOfSatellitesUsed = Convert.ToByte(rdr["NumberOfSatellitesUsed"]);
            }
            if (rdr["Heading"] != DBNull.Value)
            {
                geoPointDto.Heading = Convert.ToInt32(rdr["Heading"]);
            }
            if (rdr["CreateDate"] != DBNull.Value)
            {
                geoPointDto.CreateDate = Convert.ToDateTime(rdr["CreateDate"]);
            }

            if (rdr["UtcTime"] != DBNull.Value)
            {
                geoPointDto.UtcTime = Convert.ToDateTime(rdr["UtcTime"]);
            }

            geoPointDto.FromDate = geoPointDto.UtcTime.ToLocalTime();
            geoPointDto.ToDate = geoPointDto.UtcTime.ToLocalTime();

            if (rdr["Message"] != DBNull.Value)
            {
                geoPointDto.FromMessage = Convert.ToString(rdr["Message"]);
            }
            if (rdr["MessageJson"] != DBNull.Value)
            {
                geoPointDto.FromMessageJson = Convert.ToString(rdr["MessageJson"]);
            }
            if (rdr["PacketId"] != DBNull.Value)
            {
                geoPointDto.PacketId = Convert.ToString(rdr["PacketId"]);
            }

            if (rdr["MapId"] != DBNull.Value)
            {
                geoPointDto.MapId = Convert.ToInt32(rdr["MapId"]);
            }

            if (rdr["Imei"] != DBNull.Value)
            {
                geoPointDto.Imei = Convert.ToString(rdr["Imei"]);
            }

            return geoPointDto;
        }