Пример #1
0
            /// <summary>
            /// Загрузить шаблон устройства
            /// </summary>
            public bool LoadTemplate(string fileName, out string errMsg)
            {
                try
                {
                    // очистка списков групп элементов и команд
                    ElemGroups.Clear();
                    Cmds.Clear();

                    // загрузка шаблона устройства
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(fileName);

                    // загрузка групп элементов
                    XmlNode elemGroupsNode = xmlDoc.DocumentElement.SelectSingleNode("ElemGroups");

                    if (elemGroupsNode != null)
                    {
                        int kpTagInd = 0;

                        foreach (XmlElement elemGroupElem in elemGroupsNode.ChildNodes)
                        {
                            TableTypes tableType = (TableTypes)(Enum.Parse(typeof(TableTypes), 
                                elemGroupElem.GetAttribute("tableType"), true));
                            ElemGroup elemGroup = new ElemGroup(tableType);
                            elemGroup.Address = ushort.Parse(elemGroupElem.GetAttribute("address"));
                            elemGroup.Name = elemGroupElem.GetAttribute("name");
                            elemGroup.StartKPTagInd = kpTagInd;
                            elemGroup.StartSignal = kpTagInd + 1;

                            XmlNodeList elemNodes = elemGroupElem.SelectNodes("Elem");
                            foreach (XmlElement elemElem in elemNodes)
                            {
                                Elem elem = new Elem();
                                elem.Name = elemElem.GetAttribute("name");
                                string elemTypeStr = elemElem.GetAttribute("type");
                                elem.ElemType = elemTypeStr == "" ? elemGroup.DefElemType : 
                                    (ElemTypes)(Enum.Parse(typeof(ElemTypes), elemTypeStr, true));
                                elem.InitByteOrder(elemElem.GetAttribute("byteOrder"));
                                elemGroup.Elems.Add(elem);
                            }

                            if (0 < elemGroup.Elems.Count && elemGroup.Elems.Count <= ElemGroup.GetMaxElemCnt(tableType))
                            {
                                ElemGroups.Add(elemGroup);
                                kpTagInd += elemGroup.Elems.Count;
                            }
                        }
                    }

                    // загрузка команд
                    XmlNode cmdsNode = xmlDoc.DocumentElement.SelectSingleNode("Cmds");

                    if (cmdsNode != null)
                    {
                        foreach (XmlElement cmdElem in cmdsNode.ChildNodes)
                        {
                            TableTypes tableType = (TableTypes)(Enum.Parse(typeof(TableTypes),
                                cmdElem.GetAttribute("tableType"), true));
                            string multiple = cmdElem.GetAttribute("multiple");
                            string elemCnt = cmdElem.GetAttribute("elemCnt");
                            Cmd cmd = multiple == "" || elemCnt == "" ? 
                                new Cmd(tableType) : 
                                new Cmd(tableType, bool.Parse(multiple), int.Parse(elemCnt));
                            cmd.Address = ushort.Parse(cmdElem.GetAttribute("address"));
                            cmd.Name = cmdElem.GetAttribute("name");
                            cmd.CmdNum = int.Parse(cmdElem.GetAttribute("cmdNum"));

                            if (0 < cmd.CmdNum && cmd.CmdNum <= ushort.MaxValue)
                                Cmds.Add(cmd);
                        }
                    }

                    errMsg = "";
                    return true;
                }
                catch (Exception ex)
                {
                    errMsg = Phrases.LoadTemplateError + ":" + Environment.NewLine + ex.Message;
                    return false;
                }
            }