/// <summary> /// Save the route to the catalogue /// </summary> /// <param name="route"> /// Travel route /// </param> /// <returns> /// The saved route's information /// </returns> private RouteInfo SaveRoute(StorageRoute route) { var logger = AppContext.Logger; int max = 0; using (var fs = File.Open(this.CatalogFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) { var formatter = new TolerantBinaryFormatter(logger); try { while (fs.Position != fs.Length) { var existRoute = formatter.Deserialize(fs) as RouteInfo; if (existRoute != null) { if (existRoute.Id > max) { max = existRoute.Id; } if (existRoute.Route.Equals(route)) { return existRoute; } } } } catch (Exception ex) { logger.Error("Failed to read route information: " + ex.Message); fs.SetLength(0); fs.Position = 0; } var newItem = new RouteInfo(++max, route); formatter.Serialize(fs, newItem); return new RouteInfo(max, route); } }
/// <summary> /// The load env. /// </summary> /// <param name="filePath"> /// The file path. /// </param> /// <returns> /// The <see cref="MonitorEnvironment"/>. /// </returns> public MonitorEnvironment LoadEnv(string filePath) { try { if (!File.Exists(filePath)) { return null; } var configs = new Dictionary<Type, ConfigInfo>(); byte[][] rawData; var formatter = new TolerantBinaryFormatter(this.Logger); var typeResolver = new TypeResolver(this.Logger); using (var stream = File.Open(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) { using (var reader = new BinaryReader(stream)) { var productName = reader.ReadString(); var productVersion = reader.ReadString(); this.Logger.DebugFormat("Loading configuration for {0} {1}...", productName, productVersion); rawData = formatter.Deserialize(stream) as byte[][]; } } if (rawData != null) { foreach (var data in rawData) { try { using (var ms = new MemoryStream(data)) { var configData = formatter.Deserialize(ms) as KeyValue<Type, ConfigInfo>; if (configData != null && configData.Key != null) { configs.Add(configData.Key, configData.Value); } } } catch (Exception ex) { this.Logger.Warn("Cannot read configuration: " + ex.Message); } } } if (configs.Count > 0) { IArchiveManager archiveManager = null; IFareDataProvider fareDataProvider = null; ISyncableDatabase syncDb = null; IFareDatabase fareDatabase = null; ICurrencyProvider currencyProvider = null; BackgroundServiceManager backgroundServices = new BackgroundServiceManager(this.Logger); foreach (var pair in configs) { Type type = pair.Key; ConfigInfo info = pair.Value; if (type == typeof(IArchiveManager)) { if (info.ConfiguredType != null) { archiveManager = typeResolver.CreateInstance<IArchiveManager>(info.ConfiguredType); if (archiveManager != null) { archiveManager.Configuration = info.TypeConfiguration ?? archiveManager.DefaultConfig; } } } else if (type == typeof(IFareDataProvider)) { if (info.ConfiguredType != null) { fareDataProvider = typeResolver.CreateInstance<IFareDataProvider>(info.ConfiguredType); if (fareDataProvider != null) { fareDataProvider.Configuration = info.TypeConfiguration ?? fareDataProvider.DefaultConfig; } if (archiveManager != null) { archiveManager.FareDataProvider = fareDataProvider; } } } else if (type == typeof(IFareDatabase)) { if (info.ConfiguredType != null) { fareDatabase = typeResolver.CreateInstance<IFareDatabase>(info.ConfiguredType); if (fareDatabase != null) { fareDatabase.Configuration = info.TypeConfiguration ?? fareDatabase.DefaultConfig; } syncDb = fareDatabase as ISyncableDatabase; } } else if (type == typeof(IDatabaseSyncer<>)) { if (syncDb != null) { if (info.ConfiguredType != null) { var dbSyncer = typeResolver.CreateInstance<IPlugin>(info.ConfiguredType); if (dbSyncer != null) { var dataSyncer = dbSyncer as IDataSyncer; dbSyncer.Configuration = info.TypeConfiguration ?? dbSyncer.DefaultConfig; syncDb.DataSynchronizer = dataSyncer; syncDb.PackageSynchronizer = dbSyncer as IPackageSyncer<TravelRoute>; } } } } else if (typeof(IHelperService).IsAssignableFrom(type)) { if (info.ConfiguredType != null) { var newService = typeResolver.CreateInstance<IHelperService>(info.ConfiguredType); if (newService != null) { newService.Configuration = info.TypeConfiguration ?? newService.DefaultConfig; backgroundServices.Add(newService, false); var currencyService = newService as ICurrencyProvider; if (currencyService != null) { currencyProvider = currencyService; } } } } } return new MonitorEnvironment( this, this._pluginResolver, fareDataProvider, fareDatabase, archiveManager, currencyProvider, backgroundServices, this.Logger); } } catch (Exception ex) { this.Logger.ErrorFormat("Failed to load environment data: {0}", ex); } return null; }
/// <summary> /// The get routes info. /// </summary> /// <returns> /// The <see cref="List"/>. /// </returns> public List<RouteInfo> GetRoutesInfo() { var result = new List<RouteInfo>(); var logger = AppContext.Logger; if (File.Exists(this.CatalogFile)) { using (var fs = File.Open(this.CatalogFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) { try { var formatter = new TolerantBinaryFormatter(logger); while (fs.Position != fs.Length) { var existRoute = formatter.Deserialize(fs) as RouteInfo; if (existRoute != null) { result.Add(existRoute); } } } catch (Exception ex) { logger.Error("Failed to read route information: " + ex.Message); fs.SetLength(0); fs.Position = 0; } } } return result; }