private bool AddSwitchOrRoute(IBlock block) { if (block == null) { return(false); } foreach (var e in block.ListEntries) { if (e == null) { continue; } string sid = $"{e.ObjectId}"; if (sid.StartsWith("20", StringComparison.OrdinalIgnoreCase)) { var sw = GetObjectBy(e.ObjectId); if (sw == null) { sw = new Switch { ObjectId = e.ObjectId }; sw.Parse(e.Arguments); sw.CommandsReady += CommandsReady; _objects.Add(sw); DataChanged?.Invoke(this); sw.EnableView(); } else { sw.Parse(e.Arguments); sw.CommandsReady -= CommandsReady; sw.CommandsReady += CommandsReady; DataChanged?.Invoke(this); if (!sw.HasView) { sw.EnableView(); } } } else if (sid.StartsWith("30", StringComparison.OrdinalIgnoreCase)) { var r = GetObjectBy(e.ObjectId); if (r == null) { r = new Route { ObjectId = e.ObjectId }; r.Parse(e.Arguments); r.CommandsReady += CommandsReady; _objects.Add(r); DataChanged?.Invoke(this); r.EnableView(); } else { r.Parse(e.Arguments); r.CommandsReady -= CommandsReady; r.CommandsReady += CommandsReady; if (!r.HasView) { r.EnableView(); } DataChanged?.Invoke(this); } } } return(true); }
public bool LoadObjects(string targetPath) { try { if (!File.Exists(targetPath)) { return(false); } var cnt = File.ReadAllText(targetPath, Encoding.UTF8); JObject o = JObject.Parse(cnt); if (o == null) { return(false); } if (o["locomotives"] != null) { JArray ar = o["locomotives"] as JArray; if (ar != null) { foreach (var arItem in ar) { if (arItem == null) { continue; } var e = new Locomotive(); e.ParseJson(arItem as JObject); e.CommandsReady += CommandsReady; _objects.Add(e); DataChanged?.Invoke(this); } } } if (o["switches"] != null) { JArray ar = o["switches"] as JArray; if (ar != null) { foreach (var arItem in ar) { if (arItem == null) { continue; } var e = new Switch(); e.ParseJson(arItem as JObject); e.CommandsReady += CommandsReady; _objects.Add(e); DataChanged?.Invoke(this); } } } if (o["routes"] != null) { JArray ar = o["routes"] as JArray; if (ar != null) { foreach (var arItem in ar) { if (arItem == null) { continue; } var e = new Route(); e.ParseJson(arItem as JObject); e.CommandsReady += CommandsReady; _objects.Add(e); DataChanged?.Invoke(this); } } } if (o["s88"] != null) { JArray ar = o["s88"] as JArray; if (ar != null) { foreach (var arItem in ar) { if (arItem == null) { continue; } var e = new S88(); e.ParseJson(arItem as JObject); e.CommandsReady += CommandsReady; _objects.Add(e); DataChanged?.Invoke(this); } } } return(true); } catch (Exception ex) { Trace.WriteLine("<DataProvider> " + ex.Message); return(false); } }