private async Task <WTT> LoadTimeTable() { string WTTFile = null; JObject TaskConfig; try { TaskConfig = JObject.Parse(this._JSON); WTT SourceWTT = new WTT(); WTTFile = TaskConfig["sourceWTT"].ToString(); await Task.Run(() => { SourceWTT.LoadFromWTT(WTTFile); }).ConfigureAwait(false); return(SourceWTT); } catch (Exception Ex) { throw new Exception(ExceptionHelper.GetStaticException("QueuerLoadWTTFromFileError", new object[] { WTTFile }), Ex); } }
public async Task <QueuerResponseStatus> Execute() { bool DebugMode = Convert.ToBoolean(this._Config["debugMode"]); //Add process started Response this._Responses.Add(new QueuerResponse(QueuerResponseStatus.Running, "processStarted", null)); //Check user was authenticated at time of queue if (this._Authenticated == false) { this._Responses.Add(new QueuerResponse(QueuerResponseStatus.CompletedWithWarning, "processUserNotAuthenticatedAtQueue", null)); return(QueuerResponseStatus.CompletedWithWarning); } try { //Initialise simulation this._Simulation = new Simulation(this.Config["simName"].ToString(), this.Config["simDescription"] == null ? string.Empty: this.Config["simDescription"].ToString(), this.Config["simWikiLink"] == null ? string.Empty : this.Config["simWikiLink"].ToString(), this.Config["simSimSigCode"].ToString(), this._SQLConnector); //Declare the individual async tasks Task <WTT> SourceWTT = this.LoadTimeTable(); Task <bool> SimulationExists = this.CheckSimulationExists(); //Declare list of all the async tasks List <Task> allTasks = new List <Task> { SourceWTT, SimulationExists }; while (allTasks.Any()) { Task finished = await Task.WhenAny(allTasks).ConfigureAwait(false); if (finished == SourceWTT) { if (DebugMode) { this._Responses.Add(new QueuerResponse(QueuerResponseStatus.DebugMesssage, "Completed build the WTT object from the source WTT file", null)); } #if DEBUG Console.WriteLine($"{DateTime.UtcNow.ToLongTimeString()}:- Completed reading WTT"); #endif this._TimeTable = SourceWTT.Result; } else if (finished == SimulationExists) { if (DebugMode) { this._Responses.Add(new QueuerResponse(QueuerResponseStatus.DebugMesssage, "Completed checking if the simulation already exists inthe GroundFrame.SQL database", null)); } #if DEBUG Console.WriteLine($"{DateTime.UtcNow.ToLongTimeString()}:- Completed checking whether the sim Exists"); #endif } allTasks.Remove(finished); } //Check is simulation already exists in the data. If it does save the simulation to the GroundFrame.SQL database if (SimulationExists.Result == true) { this._Responses.Add(new QueuerResponse(QueuerResponseStatus.CompletedWithWarning, "processSimulationAlreadyExists", null)); #if DEBUG Console.WriteLine($"{DateTime.UtcNow.ToLongTimeString()}:- The simulation already exists so the process is stopped"); #endif return(QueuerResponseStatus.CompletedWithWarning); } else { try { this._Simulation.SaveToSQLDB(); //Now load the Extension version so we can keep track of the nodes this._SimExt = new SimulationExtension(this._Simulation.ID, this._SQLConnector); if (DebugMode) { this._Responses.Add(new QueuerResponse(QueuerResponseStatus.DebugMesssage, $"Simulation saved to the GroundFrame.SQL database. ID = {this._Simulation.ID}.", null)); } #if DEBUG Console.WriteLine($"{DateTime.UtcNow.ToLongTimeString()}:- Simulation saved to GroundFrame.SQL database"); #endif } catch (Exception Ex) { ResourceManager ExceptionMessageResources = new ResourceManager("GroundFrame.Core.Resources.ExceptionResources", Assembly.GetExecutingAssembly()); string ExceptionMessage = ExceptionMessageResources.GetString("QueuerErrorSavingSimulation", Globals.UserSettings.GetCultureInfo()); throw new Exception(ExceptionMessage, Ex); } } //Build the next stage of the task by setting the individual lasts Task <List <MapperLocation> > GetLocationMapperTask = this.GetLocationMapperFromWTT(); //Gets the location mapper list from the WTT Task <List <MapperLocationNode> > GetLocationNodeMapperTask = this.GetLocationNodeMapperFromWTT(); //Gets the location node mapper list from the WTT Task <SimSig.Version> SimVersionTask = this.GetSimSigVersion(); //Gets SimSig version requested from the GroundFrame.SQL database Task <SimSig.SimulationEra> TemplateSimEraTask = this.GetSimTemplateEra(); //Gets the era template for the simulation //Build All List task allTasks = new List <Task> { GetLocationMapperTask, GetLocationNodeMapperTask, SimVersionTask, TemplateSimEraTask }; //Wait for the tasks to finish while (allTasks.Any()) { Task finished = await Task.WhenAny(allTasks).ConfigureAwait(false); if (finished == GetLocationMapperTask) { if (DebugMode) { this._Responses.Add(new QueuerResponse(QueuerResponseStatus.DebugMesssage, "Completed getting the Location Mapper from the source WTT", null)); } #if DEBUG Console.WriteLine($"{DateTime.UtcNow.ToLongTimeString()}:- Completed getting Location Mapper"); #endif _LocationMapper = GetLocationMapperTask.Result; } else if (finished == GetLocationNodeMapperTask) { if (DebugMode) { this._Responses.Add(new QueuerResponse(QueuerResponseStatus.DebugMesssage, "Completed getting the Location Node Mapper from the source WTT", null)); } #if DEBUG Console.WriteLine($"{DateTime.UtcNow.ToLongTimeString()}:- Completed getting Location Node Mapper"); #endif _LocationNodeMapper = GetLocationNodeMapperTask.Result; } else if (finished == SimVersionTask) { if (DebugMode) { this._Responses.Add(new QueuerResponse(QueuerResponseStatus.DebugMesssage, "Completed getting the Simulation version from the GroundFrame.SQL database", null)); } #if DEBUG Console.WriteLine($"{DateTime.UtcNow.ToLongTimeString()}:- Completed getting SimSig version"); #endif _Version = SimVersionTask.Result; } else if (finished == TemplateSimEraTask) { if (DebugMode) { this._Responses.Add(new QueuerResponse(QueuerResponseStatus.DebugMesssage, "Completed getting the Simulation template era from the GroundFrame.SQL database", null)); } #if DEBUG Console.WriteLine($"{DateTime.UtcNow.ToLongTimeString()}:- Completed getting the template era"); #endif _TemplateSimEra = TemplateSimEraTask.Result; } allTasks.Remove(finished); } //Next create the locations in the GroundFrame.SQL database await CreateLocationsFromMap().ConfigureAwait(false); if (DebugMode) { this._Responses.Add(new QueuerResponse(QueuerResponseStatus.DebugMesssage, "Completed creating the locations in the GroundFrame.SQL database", null)); } #if DEBUG using SimulationExtension SimExtentionLocations = new SimulationExtension(this._Simulation.ID, this._SQLConnector); Console.WriteLine($"{DateTime.UtcNow.ToLongTimeString()}:- The Simulation contains {SimExtentionLocations.Locations.Count} location(s)."); #endif //Next create the location nodes in the GroundFrame.SQL database await CreateLocationNodesFromMap(this._SimExt).ConfigureAwait(false); if (DebugMode) { this._Responses.Add(new QueuerResponse(QueuerResponseStatus.DebugMesssage, "Completed creating the location nodes in the GroundFrame.SQL database", null)); } #if DEBUG Console.WriteLine($"{DateTime.UtcNow.ToLongTimeString()}:- The Simulation contains {this._SimExt.LocationNodes.Count} location nodes(s)."); #endif //Next create the path edges in the GroundFrame.SQL database await CreatePathEdgesFromMap(this._SimExt).ConfigureAwait(false); if (DebugMode) { this._Responses.Add(new QueuerResponse(QueuerResponseStatus.DebugMesssage, "Completed creating the path edges in the GroundFrame.SQL database", null)); } #if DEBUG Console.WriteLine($"{DateTime.UtcNow.ToLongTimeString()}:- The Simulation contains {this._SimExt.LocationNodes.Sum(x => x.PathEdges.Count)} path edge(s)."); #endif this._Responses.Add(new QueuerResponse(QueuerResponseStatus.Success, "processSuccess", null)); return(QueuerResponseStatus.Success); } catch (AggregateException Ex) { ResourceManager ExceptionMessageResources = new ResourceManager("GroundFrame.Core.Resources.ExceptionResources", Assembly.GetExecutingAssembly()); string ExceptionMessage = ExceptionMessageResources.GetString("QueuerGenericFailureMessage", Globals.UserSettings.GetCultureInfo()); foreach (Exception Inner in Ex.InnerExceptions) { this._Responses.Add(new QueuerResponse(QueuerResponseStatus.Failed, ExceptionMessage, Inner)); } return(QueuerResponseStatus.Failed); } catch (Exception Ex) { ResourceManager ExceptionMessageResources = new ResourceManager("GroundFrame.Core.Resources.ExceptionResources", Assembly.GetExecutingAssembly()); string ExceptionMessage = ExceptionMessageResources.GetString("QueuerGenericFailureMessage", Globals.UserSettings.GetCultureInfo()); this._Responses.Add(new QueuerResponse(QueuerResponseStatus.Failed, ExceptionMessage, Ex)); return(QueuerResponseStatus.Failed); } }