public async Task Start(string inputMetamodelPath, string outputRoutePath) { Started?.Invoke(this); Progress?.Invoke(this, new RouteAnalyzerState { Message = "Started" }); if (string.IsNullOrEmpty(inputMetamodelPath)) { Ctx?.SendDebugMessage($"Input metamodel file is not set."); return; } if (!File.Exists(inputMetamodelPath)) { Ctx?.SendDebugMessage($"Input metamodel file does not exist: {inputMetamodelPath}"); return; } var fname = System.IO.Path.GetFileName(inputMetamodelPath); await Task.Run(() => { try { var field = LoadPlanFieldFile(inputMetamodelPath); if (field != null) { Ctx?.SendDebugMessage($"Field is loaded, items: {field.Count}"); } var analyzer = new Analyze(field); var analyzerResult = analyzer.Execute((step, maxSteps) => { var m = GetProgressMessage(step, maxSteps, $"Analyzing {fname}"); Progress?.Invoke(this, new RouteAnalyzerState { Message = $"{m}" }); }); Ctx?.SendDebugMessage($"Found {analyzerResult.NumberOfRoutes} routes."); var json = analyzerResult.ToJson(); if (string.IsNullOrEmpty(json)) { Failed?.Invoke(this, $"Result of the analyze call is empty."); } Ctx?.SendDebugMessage($"Apply recent disabling states."); json = ApplyRouteDisableStates(outputRoutePath, json); json.FixBomIfNeeded(); StringUtilities.WriteAllTextNoBom(outputRoutePath, json, out _); Finished?.Invoke(this); } catch (Exception ex) { FailedEx?.Invoke(this, ex); } }); }
public async Task Start() { Started?.Invoke(this); Progress?.Invoke(this, new InitializeSystemState { Message = "Started" }); await Task.Run(() => { try { // inititalize accessories var planField = Ctx?.GetPlanField(); if (planField == null) { throw new Exception("Planfield instance is missing."); } var relevantItems = new Dictionary <string, PlanItem>(); foreach (var it in planField) { var k = it.Key; var item = it.Value; if (item == null) { continue; } if (!item.IsSignal && !item.IsSwitch) { continue; } relevantItems.Add(k, item); } Progress?.Invoke(this, new InitializeSystemState { Message = $"Found {relevantItems.Count} accessories." }); var dp = Ctx._sniffer.GetDataProvider(); if (dp == null) { Failed?.Invoke(this, "No datatype provider."); return; } foreach (var it in relevantItems) { var parts = it.Key.Split('x'); if (parts.Length != 2) { continue; } var x = int.Parse(parts[0]); var y = int.Parse(parts[1]); Utilities.GetEcosAddress(planField, x, y, out var ecosAddr1, out var ecosAddr2, out _, out _); ecoslib.Entities.Accessory item0 = null; if (ecosAddr1 != null) { item0 = dp.GetAccessoryByAddress(ecosAddr1.Value) as ecoslib.Entities.Accessory; } ecoslib.Entities.Accessory item1 = null; if (ecosAddr2 != null) { item1 = dp.GetAccessoryByAddress(ecosAddr2.Value) as ecoslib.Entities.Accessory; } if (item0 != null) { Progress?.Invoke(this, new InitializeSystemState { Message = $"Switch {item0.Caption}" }); if (Ctx._sniffer != null && Ctx._sniffer.IsSimulationMode) { item0.SwitchSimulation(0); Ctx._sniffer?.TriggerDataProviderModifiedForSimulation(); item0.SwitchSimulation(1); Ctx._sniffer?.TriggerDataProviderModifiedForSimulation(); } else { item0.Switch(0); SendAndWait(); item0.Switch(1); SendAndWait(); } } if (item1 != null) { Progress?.Invoke(this, new InitializeSystemState { Message = $"Switch {item1.Caption}" }); if (Ctx._sniffer != null && Ctx._sniffer.IsSimulationMode) { item1.SwitchSimulation(0); Ctx._sniffer?.TriggerDataProviderModifiedForSimulation(); item1.SwitchSimulation(1); Ctx._sniffer?.TriggerDataProviderModifiedForSimulation(); } else { item1.Switch(0); SendAndWait(); item1.Switch(1); SendAndWait(); } } } Finished?.Invoke(this); Progress?.Invoke(this, new InitializeSystemState { Message = "Finished" }); } catch (Exception ex) { FailedEx?.Invoke(this, ex); } }); }