예제 #1
0
            /// <summary>
            /// Process the result
            /// </summary>
            protected override void OnQueryObjects(Reply reply)
            {
                if (!reply.IsSucceeded)
                {
                    throw new ProtocolException(reply.ErrorMessage);
                }
                var railway      = package.Railway;
                var existingLocs = railway.GetLocs().ToList();

                foreach (var row in reply.Rows)
                {
                    var loc      = new RawLoc(Client, row.Id);
                    var name     = loc.GetName();
                    var address  = loc.GetAddress();
                    var protocol = loc.GetProtocol();

                    var existingLoc = existingLocs.FirstOrDefault(x => x.Description == name);
                    if (existingLoc != null)
                    {
                        continue;
                    }

                    int addressNr;
                    if (!int.TryParse(address, out addressNr))
                    {
                        continue;
                    }

                    existingLoc = existingLocs.FirstOrDefault(x => x.Address.ValueAsInt == addressNr);
                    if (existingLoc != null)
                    {
                        continue;
                    }

                    AddressType addressType;
                    if (!EcosUtility.TryGetAddressType(protocol, out addressType))
                    {
                        log("Unsupported protocol: " + protocol);
                        continue;
                    }

                    // Add the new loc
                    var newLoc = package.AddNewLoc();
                    newLoc.Description = name;
                    newLoc.Address     = new Address(addressType, null, addressNr);
                    package.Railway.Locs.Add(newLoc);

                    log("Imported new loc: " + name);
                }
            }
예제 #2
0
        /// <summary>
        /// Import all locs
        /// </summary>
        protected void ImportLocs(IPackage package, IRailway railway)
        {
            var lclist = Root.Element("lclist");

            if (lclist == null)
            {
                return;
            }

            foreach (var lc in lclist.Elements("lc"))
            {
                var id = lc.GetAttributeValue("id", string.Empty);
                if (string.IsNullOrEmpty(id))
                {
                    Message("Loc found without an id");
                    continue;
                }
                if (locs.ContainsKey(id.ToLower()))
                {
                    Message("Duplicate loc found ({0})", id);
                    continue;
                }

                var addr = lc.GetIntAttributeValue("addr");

                var entity = package.AddNewLoc();
                railway.Locs.Add(entity);
                entity.Description = id;

                var addrType = (lc.GetAttributeValue("prot") == "M") ? AddressType.Motorola : AddressType.Dcc;
                entity.Address = new Address(addrType, null, addr);

                // Record
                locs[id.ToLower()] = entity;
            }
        }