private bool HandleEventS88(IItem item, ListEntry listEntry)
        {
            S88 e = item as S88;

            if (e == null)
            {
                return(false);
            }

            // 1101 0000 1101 0110
            //                   0
            // 16

            string hex = listEntry.Arguments[0].Parameter[0];

            var itemS88 = GetObjectBy(e.ObjectId) as S88;

            if (itemS88 != null)
            {
                itemS88.StateOriginal = hex;
                itemS88.UpdateTitle();
                itemS88.UpdateSubTitle();
            }

            return(true);
        }
        private bool AddS88(IBlock block)
        {
            if (block == null)
            {
                return(false);
            }

            foreach (var e in block.ListEntries)
            {
                if (e == null)
                {
                    continue;
                }

                string sid = $"{e.ObjectId}";

                if (sid.StartsWith("10", StringComparison.OrdinalIgnoreCase))
                {
                    int n = _objects.Count(x => x is S88);

                    var s88 = GetObjectBy(e.ObjectId) as S88;
                    if (s88 == null)
                    {
                        s88 = new S88 {
                            ObjectId = e.ObjectId, Index = n
                        };
                        s88.Parse(e.Arguments);
                        s88.CommandsReady += CommandsReady;
                        _objects.Add(s88);
                        DataChanged?.Invoke(this);
                        s88.EnableView();
                    }
                    else
                    {
                        s88.Parse(e.Arguments);
                        s88.CommandsReady -= CommandsReady;
                        s88.CommandsReady += CommandsReady;
                        if (!s88.HasView)
                        {
                            s88.EnableView();
                        }
                        DataChanged?.Invoke(this);
                    }
                }
            }

            return(true);
        }
Example #3
0
        public void SetRoute(Analyze.Route route, bool state)
        {
            if (route == null)
            {
                return;
            }

            var n = route.Count;

            var trackEntity = Ctx.TrackEntity;

            for (int idx = 1; idx < n - 1; ++idx)
            {
                var r = route[idx];
                if (r == null)
                {
                    continue;
                }

                if (trackEntity?.Viewer != null)
                {
                    string themeIcon = null;

                    var trackInfo = trackEntity.Track.Get(r.X, r.Y);
                    if (trackInfo != null)
                    {
                        var themeInfo = Theme?.Get(trackInfo.ThemeId);
                        if (themeInfo != null)
                        {
                            TrackCheckResult     checkResult = null;
                            TrackInformation.S88 s88item     = null;

                            var w        = Ctx.Dispatcher.Weaver;
                            var objItems = w.GetObject(trackInfo);
                            if (objItems != null && objItems.Count > 0)
                            {
                                s88item = objItems[0] as S88;
                                var checkFnc = w.GetCheckFnc(s88item, trackInfo);
                                if (checkFnc != null)
                                {
                                    checkResult = checkFnc();
                                }
                            }

                            if (checkResult == null)
                            {
                                if (state)
                                {
                                    if (Route.HasPoint(route, trackInfo.X, trackInfo.Y))
                                    {
                                        themeIcon = themeInfo.Off.Route;
                                    }
                                    else
                                    {
                                        themeIcon = themeInfo.Off.Default;
                                    }
                                }
                                else
                                {
                                    themeIcon = themeInfo.Off.Default;
                                }
                            }
                            else
                            {
                                bool rS88 = checkResult?.State != null && checkResult.State.Value;

                                if (rS88 && s88item != null)
                                {
                                    if (s88item.IsRouted)
                                    {
                                        themeIcon = themeInfo.Active.Route;
                                    }
                                    else
                                    {
                                        themeIcon = themeInfo.Active.Occ;
                                    }
                                }
                                else
                                {
                                    if (s88item != null && s88item.IsRouted)
                                    {
                                        themeIcon = themeInfo.Off.Route;
                                    }
                                    else
                                    {
                                        if (state && Route.HasPoint(route, trackInfo.X, trackInfo.Y))
                                        {
                                            themeIcon = themeInfo.Off.Route;
                                        }
                                        else
                                        {
                                            themeIcon = themeInfo.Off.Default;
                                        }
                                    }
                                }
                            }
                        }
                    }

                    if (!string.IsNullOrEmpty(themeIcon))
                    {
                        var x           = trackInfo.X;
                        var y           = trackInfo.Y;
                        var themeId     = trackInfo.ThemeId;
                        var orientation = trackInfo.Orientation;
                        var symbol      = themeIcon;

                        var isSwitch = RailwayEssentialCore.Globals.SwitchIds.Contains(themeId);

                        if (r.HasTurn && isSwitch)
                        {
                            var parts = symbol.Split(new[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
                            if (parts.Length == 2)
                            {
                                symbol = parts[0] + "-t-" + parts[1];
                            }
                            else if (parts.Length == 1)
                            {
                                symbol = parts[0] + "-t";
                            }
                        }

                        Ctx.ExecuteJs($"changeSymbol({x}, {y}, {themeId}, \"{orientation}\", \"{symbol}\");");
                    }
                }
            }
        }
        private bool HandleEvent(IBlock block)
        {
            if (block == null)
            {
                return(false);
            }

            RegisterS88.S88Info s88Info;
            if (RegisterS88.IsRegisterS88(block, out s88Info))
            {
                if (s88Info != null && !s88Info.Appended)
                {
                    RemoveObjectWithId((uint)s88Info.ObjectId);

                    return(true);
                }
                else if (s88Info != null)
                {
                    if (DoesObjectIdExist((uint)s88Info.ObjectId))
                    {
                        RemoveObjectWithId((uint)s88Info.ObjectId);
                    }

                    int n = _objects.Count(x => x is S88);

                    if (n + 1 != s88Info.NewSize)
                    {
                        Trace.WriteLine("<Error> S88 BusSize Mismatch");

                        return(false);
                    }

                    var s88 = new S88 {
                        ObjectId = s88Info.ObjectId, Index = n
                    };
                    s88.CommandsReady += CommandsReady;
                    _objects.Add(s88);
                    DataChanged?.Invoke(this);
                    s88.EnableView();

                    return(true);
                }
            }

            foreach (var e in block.ListEntries)
            {
                if (e == null)
                {
                    continue;
                }

                IItem item = GetObjectBy(e.ObjectId);

                if (HandleEventS88(item, e))
                {
                    continue;
                }

                if (HandleEventSwitch(item, e))
                {
                    continue;
                }

                if (HandleEventLocomotive(item, e))
                {
                    continue;
                }
            }

            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);
            }
        }