Ejemplo n.º 1
0
        public GoogleLocationHistoryItems ReadJsonAndWriteToCache(String jsonPath, String userName, bool returnGoogleLocationHistoryItems)
        {
            GoogleLocationHistoryDatabaseCache googleLocationDatabaseCache = new GoogleLocationHistoryDatabaseCache(dbTools);

            googleLocationDatabaseCache.WriteLocationHistorySource(userName, jsonPath);

            GoogleLocationHistoryItems googleLocationHistory = null;

            if (returnGoogleLocationHistoryItems)
            {
                googleLocationHistory = new GoogleLocationHistoryItems(userName);
            }


            int locationsFoundCount = 0;
            var sqlTransactionBatch = googleLocationDatabaseCache.TransactionBegin();

            var dinamic = new DinamicStreamJsonParser()
            {
                PropertyToType = new Dictionary <string, Type>()
                {
                    { "locations", Type.GetType(Type.GetType("GoogleLocationHistory.GoogleJsonLocations").AssemblyQualifiedName) },
                    { "activity", Type.GetType(Type.GetType("GoogleLocationHistory.GoogleJsonActivity").AssemblyQualifiedName) },
                },
                ObjectFoundCallback = (object obj, long readPosition, long fileLength) =>
                {
                    if (obj.GetType() == typeof(GoogleJsonLocations))
                    {
                        locationsFoundCount++;
                        if (locationsFoundCount % 5000 == 0)
                        {
                            googleLocationDatabaseCache.TransactionCommit(sqlTransactionBatch);
                            sqlTransactionBatch = googleLocationDatabaseCache.TransactionBegin();
                        }
                        if (returnGoogleLocationHistoryItems)
                        {
                            googleLocationHistory.Add((GoogleJsonLocations)obj);
                        }

                        googleLocationDatabaseCache.WriteLocationHistory(userName, (GoogleJsonLocations)obj);

                        LocationFound?.Invoke(this, EventArgs.Empty);
                        LocationFoundParam?.Invoke(this, locationsFoundCount, readPosition, fileLength);
                    }
                },
                NewTypeFoundCallback = (object obj, string name) => {
                    Logger.Debug($"Found object {obj} with name {name}");
                },
                StreamReader = new StreamReader(jsonPath, System.Text.Encoding.UTF8, true, 1000),
            };

            dinamic.Parse();

            googleLocationDatabaseCache.TransactionCommit(sqlTransactionBatch);

            return(googleLocationHistory);
        }
Ejemplo n.º 2
0
        private void ExtractPlacemarks(Feature feature)
        {
            // Is the passed in value a Placemark?
            if (feature is Placemark placemark)
            {
                SharpKml.Dom.GX.Track  track       = placemark.Geometry as SharpKml.Dom.GX.Track;
                SharpKml.Base.Vector[] vector      = track.Coordinates.ToArray();
                DateTime[]             whenElement = track.When.ToArray();

                var sqlTransactionBatch = googleLocationDatabaseCache.TransactionBegin();
                for (int i = 0; i < vector.Length; i++)
                {
                    if (i % 5000 == 0)
                    {
                        googleLocationDatabaseCache.TransactionCommit(sqlTransactionBatch);
                        sqlTransactionBatch = googleLocationDatabaseCache.TransactionBegin();
                    }
                    LocationFoundParam?.Invoke(this, i, i, vector.Length);

                    GoogleJsonLocations googleJsonLocations = new GoogleJsonLocations();
                    googleJsonLocations.Accuracy  = 0;
                    googleJsonLocations.Altitude  = vector[i].Altitude == null ? 0 : (float)vector[i].Altitude;
                    googleJsonLocations.Latitude  = (float)vector[i].Latitude;
                    googleJsonLocations.Longitude = (float)vector[i].Longitude;
                    googleJsonLocations.Timestamp = whenElement[i];
                    googleLocationDatabaseCache.WriteLocationHistory(username, googleJsonLocations);
                }
                googleLocationDatabaseCache.TransactionCommit(sqlTransactionBatch);
            }
            else
            {
                // Is it a Container, as the Container might have a child Placemark?
                if (feature is Container container)
                {
                    // Check each Feature to see if it's a Placemark or another Container
                    foreach (Feature f in container.Features)
                    {
                        ExtractPlacemarks(f);
                    }
                }
            }
        }