Example #1
0
        /// <summary>
        /// Save TNS to iso storage
        /// </summary>
        /// <param name="tns"></param>
        public static void SaveTNS(TransitNetworkSearch tns)
        {
            if (tns == null)
            {
                return;
            }

            if (tns.Routes != null)
            {
                foreach (string id in tns.Routes.Keys)
                {
                    AppSettings.KnownRoutes.Value[id] = tns.Routes[id];
                }
                AppSettings.KnownRoutes.Save();
            }
            if (tns.Stops != null)
            {
                foreach (string id in tns.Stops.Keys)
                {
                    AppSettings.KnownStops.Value[id] = tns.Stops[id];
                }
                AppSettings.KnownStops.Save();
            }
            if (tns.Thresholds != null)
            {
                foreach (string id in tns.Thresholds.Keys)
                {
                    AppSettings.AlarmThresholds.Value[id] = tns.Thresholds[id];
                }
                AppSettings.AlarmThresholds.Save();
            }
        }
Example #2
0
        /// <summary>
        /// Backup all known routes and stops to skydrive
        /// </summary>
        /// <returns></returns>
        public static async Task BackupToSkydrive()
        {
            // Group by agency
            var knownTNS = new TransitNetworkSearch(
                AppSettings.KnownRoutes.Value, 
                AppSettings.KnownStops.Value,
                AppSettings.AlarmThresholds.Value);
            Dictionary<string, TransitNetworkSearch> agencies = TransitNetworkSearch.GroupByAgency(knownTNS);
            //if (agencies.Count == 0) return;

            // Get folder
            List<FolderFileData> folders = await CloudStorage.GetFiles(CloudStorage.ROOT_FOLDER);
            string folder_id;
            FolderFileData ffd = folders.FirstOrDefault(f => f.Name == CloudStorage.BA_FolderName);
            if (ffd == null)
            {
                folder_id = await CreateFolder(CloudStorage.ROOT_FOLDER, CloudStorage.BA_FolderName);
            }
            else
            {
                folder_id = ffd.Id;
            }

            // Upload agencies
            foreach (var keyval in agencies)
            {
                await CloudStorage.UploadTNS(folder_id, keyval.Key, keyval.Value);
            }
        }
Example #3
0
        /// <summary>
        /// Backup all known routes and stops to skydrive
        /// </summary>
        /// <returns></returns>
        public static async Task BackupToSkydrive()
        {
            // Group by agency
            var knownTNS = new TransitNetworkSearch(
                AppSettings.KnownRoutes.Value,
                AppSettings.KnownStops.Value,
                AppSettings.AlarmThresholds.Value);
            Dictionary <string, TransitNetworkSearch> agencies = TransitNetworkSearch.GroupByAgency(knownTNS);
            //if (agencies.Count == 0) return;

            // Get folder
            List <FolderFileData> folders = await CloudStorage.GetFiles(CloudStorage.ROOT_FOLDER);

            string         folder_id;
            FolderFileData ffd = folders.FirstOrDefault(f => f.Name == CloudStorage.BA_FolderName);

            if (ffd == null)
            {
                folder_id = await CreateFolder(CloudStorage.ROOT_FOLDER, CloudStorage.BA_FolderName);
            }
            else
            {
                folder_id = ffd.Id;
            }

            // Upload agencies
            foreach (var keyval in agencies)
            {
                await CloudStorage.UploadTNS(folder_id, keyval.Key, keyval.Value);
            }
        }
Example #4
0
        /// <summary>
        /// Download transit network
        /// </summary>
        /// <param name="lat"></param>
        /// <param name="lon"></param>
        /// <returns></returns>
        public static async Task <TransitNetworkSearch> GetTransitNetwork(double lat, double lon)
        {
            try
            {
                var response = await DownloadString(rNetworkSearch(lat, lon).ToUri());

                TransitNetworkSearch tns = JsonConvert.DeserializeObject <TransitNetworkSearch>(response);
                TransitNetworkSearch.SaveTNS(tns);
                return(tns);
            }
            catch { return(null); }
        }
Example #5
0
        /// <summary>
        /// Download TNS from path in Skydrive and update known routes/stops
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static async Task DownloadTNS(string path)
        {
            try
            {
                LiveDownloadOperationResult ldor = await CloudStorage.Client.DownloadAsync(path + "/content");

                //TransitNetworkSearch tns = JsonConvert.DeserializeObject<TransitNetworkSearch>("hi");
                TransitNetworkSearch tns;
                using (StreamReader sr = new StreamReader(ldor.Stream))
                {
                    string content = sr.ReadToEnd();
                    tns = JsonConvert.DeserializeObject <TransitNetworkSearch>(content);
                }
                TransitNetworkSearch.SaveTNS(tns);
            }
            catch
            {
            }
        }
Example #6
0
        /// <summary>
        /// Return a list of Transit Networks grouped by Agency name
        /// </summary>
        /// <param name="rootTNS">TNS supplying all routes and stops</param>
        /// <returns></returns>
        public static Dictionary <string, TransitNetworkSearch> GroupByAgency(TransitNetworkSearch rootTNS)
        {
            Dictionary <string, TransitNetworkSearch> retval = new Dictionary <string, TransitNetworkSearch>();

            if (rootTNS == null ||
                rootTNS.Routes == null ||
                rootTNS.Stops == null ||
                rootTNS.Thresholds == null)
            {
                return(retval);
            }

            var routeGroups = rootTNS.Routes.Values.GroupBy(rt => rt.Agency);

            foreach (var rg in routeGroups)
            {
                TransitNetworkSearch tns = new TransitNetworkSearch()
                {
                    Routes     = new Dictionary <string, BusRoute>(),
                    Stops      = new Dictionary <string, BusStop>(),
                    Thresholds = new Dictionary <string, double>()
                };
                foreach (BusRoute route in rg)
                {
                    tns.Routes.Add(route.Id, route);
                    foreach (string stop_id in route.Stop_Ids)
                    {
                        if (rootTNS.Stops.ContainsKey(stop_id) && !tns.Stops.ContainsKey(stop_id))
                        {
                            tns.Stops.Add(stop_id, rootTNS.Stops[stop_id]);
                            if (rootTNS.Thresholds.ContainsKey(stop_id) && !tns.Thresholds.ContainsKey(stop_id))
                            {
                                tns.Thresholds.Add(stop_id, rootTNS.Thresholds[stop_id]);
                            }
                        }
                    }
                }
                retval.Add(rg.Key, tns);
            }

            return(retval);
        }
Example #7
0
 /// <summary>
 /// Upload Transit Network Search obj
 /// </summary>
 /// <param name="folder_id"></param>
 /// <param name="agency"></param>
 /// <param name="tns"></param>
 /// <returns></returns>
 public static async Task UploadTNS(
     string folder_id,
     string agency,
     TransitNetworkSearch tns)
 {
     agency = agency.Trim();
     agency = string.IsNullOrWhiteSpace(agency) ? CloudStorage.defaultAgency : agency;
     try
     {
         await CloudStorage.Client.UploadAsync(
             folder_id,
             agency + CloudStorage.FILE_EXTENSION,
             JsonConvert.SerializeObject(tns).ToStream(),
             OverwriteOption.Overwrite);
     }
     catch
     {
         throw;
     }
 }
Example #8
0
 /// <summary>
 /// Upload Transit Network Search obj
 /// </summary>
 /// <param name="folder_id"></param>
 /// <param name="agency"></param>
 /// <param name="tns"></param>
 /// <returns></returns>
 public static async Task UploadTNS(
     string folder_id, 
     string agency, 
     TransitNetworkSearch tns)
 {
     agency = agency.Trim();
     agency = string.IsNullOrWhiteSpace(agency) ? CloudStorage.defaultAgency : agency;
     try
     {
         await CloudStorage.Client.UploadAsync(
             folder_id,
             agency + CloudStorage.FILE_EXTENSION,
             JsonConvert.SerializeObject(tns).ToStream(),
             OverwriteOption.Overwrite);
     }
     catch
     {
         throw;
     }
 }
Example #9
0
        /// <summary>
        /// Save TNS to iso storage
        /// </summary>
        /// <param name="tns"></param>
        public static void SaveTNS(TransitNetworkSearch tns)
        {
            if (tns == null) return;

            if (tns.Routes != null)
            {
                foreach (string id in tns.Routes.Keys)
                {
                    AppSettings.KnownRoutes.Value[id] = tns.Routes[id];
                }
                AppSettings.KnownRoutes.Save();
            }
            if (tns.Stops != null)
            {
                foreach (string id in tns.Stops.Keys)
                {
                    AppSettings.KnownStops.Value[id] = tns.Stops[id];
                }
                AppSettings.KnownStops.Save();
            }
            if (tns.Thresholds != null)
            {
                foreach (string id in tns.Thresholds.Keys)
                {
                    AppSettings.AlarmThresholds.Value[id] = tns.Thresholds[id];
                }
                AppSettings.AlarmThresholds.Save();
            }
        }
Example #10
0
        /// <summary>
        /// Return a list of Transit Networks grouped by Agency name
        /// </summary>
        /// <param name="rootTNS">TNS supplying all routes and stops</param>
        /// <returns></returns>
        public static Dictionary<string, TransitNetworkSearch> GroupByAgency(TransitNetworkSearch rootTNS)
        {
            Dictionary<string, TransitNetworkSearch> retval = new Dictionary<string, TransitNetworkSearch>();
            if (rootTNS == null ||
                rootTNS.Routes == null ||
                rootTNS.Stops == null ||
                rootTNS.Thresholds == null) return retval;

            var routeGroups = rootTNS.Routes.Values.GroupBy(rt => rt.Agency);
            foreach (var rg in routeGroups)
            {
                TransitNetworkSearch tns = new TransitNetworkSearch()
                {
                    Routes = new Dictionary<string, BusRoute>(),
                    Stops = new Dictionary<string, BusStop>(),
                    Thresholds = new Dictionary<string, double>()
                };
                foreach (BusRoute route in rg)
                {
                    tns.Routes.Add(route.Id, route);
                    foreach (string stop_id in route.Stop_Ids)
                    {
                        if (rootTNS.Stops.ContainsKey(stop_id) && !tns.Stops.ContainsKey(stop_id))
                        {
                            tns.Stops.Add(stop_id, rootTNS.Stops[stop_id]);
                            if (rootTNS.Thresholds.ContainsKey(stop_id) && !tns.Thresholds.ContainsKey(stop_id))
                            {
                                tns.Thresholds.Add(stop_id, rootTNS.Thresholds[stop_id]);
                            }
                        }
                    }
                }
                retval.Add(rg.Key, tns);
            }

            return retval;
        }