Esempio n. 1
0
        public static OPCConnection ReadOPCMGR()
        {
            OPCConnection connection = new OPCConnection();
            XmlReader     reader     = XmlReader.Create(opcmgrPath);

            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                case XmlNodeType.Element:
                    if (string.Equals(reader.Name, "connection", StringComparison.OrdinalIgnoreCase))
                    {
                        connection        = new OPCConnection();
                        connection.server = reader.GetAttribute("server");
                        connection.name   = reader.GetAttribute("name");
                    }
                    if (string.Equals(reader.Name, "group", StringComparison.OrdinalIgnoreCase))
                    {
                        OPCGroup group = new OPCGroup();
                        group.name           = reader.GetAttribute("name");
                        group.active         = !string.Equals(reader.GetAttribute("active"), "0");
                        group.write_together = string.Equals(reader.GetAttribute("write_together"), "yes", StringComparison.OrdinalIgnoreCase);
                        group.update_rate    = reader.GetAttribute("update_rate") ?? "0";
                        group.trace_mask     = reader.GetAttribute("trace_mask");
                        connection.Groups.Add(group);
                    }
                    if (string.Equals(reader.Name, "item", StringComparison.OrdinalIgnoreCase))
                    {
                        OPCItem item = new OPCItem();
                        item.access_path = reader.GetAttribute("access_path");
                        item.id          = reader.GetAttribute("id");
                        item.name        = reader.GetAttribute("name");
                        item.type        = reader.GetAttribute("type") ?? "default";
                        item.access      = reader.GetAttribute("access") ?? "modify";
                        item.code        = reader.GetAttribute("code") ?? "0";
                        item.active      = !string.Equals(reader.GetAttribute("active"), "0");
                        connection.Groups[connection.Groups.Count - 1].Items.Add(item);
                    }
                    break;
                }
            }

            return(connection);
        }
Esempio n. 2
0
        public static void SaveOPCMGR(OPCConnection c)
        {
            XmlWriterSettings settings = new XmlWriterSettings()
            {
                Indent      = true,
                IndentChars = "   ",
                //ConformanceLevel = ConformanceLevel.Fragment,
                OmitXmlDeclaration = true
            };
            XmlWriter writer = XmlWriter.Create(opcmgrPath, settings);

            writer.WriteStartElement("OPCMgr");
            writer.WriteStartElement("connection");
            writer.WriteAttributeString("server", c.server);
            writer.WriteAttributeString("name", c.name);
            foreach (OPCGroup g in c.Groups)
            {
                writer.WriteStartElement("group");
                writer.WriteAttributeString("name", g.name);
                writer.WriteAttributeString("write_together", g.write_together ? "yes" : "no");
                writer.WriteAttributeString("update_rate", g.update_rate);
                writer.WriteAttributeString("trace_mask", g.trace_mask);
                writer.WriteAttributeString("active", g.active ? "1" : "0");
                foreach (OPCItem i in g.Items)
                {
                    writer.WriteStartElement("item");
                    writer.WriteAttributeString("access_path", i.access_path);
                    writer.WriteAttributeString("id", i.id);
                    writer.WriteAttributeString("name", i.name);
                    writer.WriteAttributeString("type", i.type);
                    writer.WriteAttributeString("access", i.access);
                    writer.WriteAttributeString("code", i.code);
                    writer.WriteAttributeString("active", i.active ? "1" : "0");
                    writer.WriteEndElement();
                }
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
            writer.WriteEndElement();

            writer.Close();
        }
        public void InitializeBaseData()
        {
            // Initialize

            #region Headers and Combo Values
            GroupHeaders = new List <string>()
            {
                "Name", "Write Together", "Update Rate", "Trace Mask", "Active"
            };
            GroupDescriptions = new List <string>()
            {
                "The name of the group. Must be unique per OPC server.",

                "\"yes\" indicates that when any item in the group changes," + Environment.NewLine +
                "all items in the group are to be written." + Environment.NewLine +
                "(optional; default is \"no\", i.e., each item is independent)",

                "The rate at which the items are polled, in ms." + Environment.NewLine +
                "(optional; default is \"0\", i.e., the fastest reasonable rate)",

                "log (halwr) reads, writes, and changes. (Ex: trace_mask=\"RWC\")",

                "The active state of the group. Active items are updated" + Environment.NewLine +
                "by the OPC server automatically, and we are notified of their" + Environment.NewLine +
                "changes. (optional; default is \"1\")"
            };

            ItemHeaders = new List <string>()
            {
                "Group", "Access Path", "ID", "Name", "Type", "Access", "Code", "Active"
            };
            ItemDescriptions = new List <string>()
            {
                "The group this item belongs to (see above)",

                "The path that the OPC server uses to find the physical device.",

                "The address on that device that this item refers to.",

                "Only used for logging; has no meaning to OPC.",

                "Used to determine if byte swapping is necessary; has no meaning to OPC." + Environment.NewLine +
                "(optional; must be \"default\" or \"string\".",

                "Used to restrict writing OPC items if they are inputs. Some OPC items" + Environment.NewLine +
                "(e.g., Opto inputs) can be written to without error, but then the GLOBAL" + Environment.NewLine +
                "representation no longer matches the device inputs. Value means read only" + Environment.NewLine +
                "(read), write only (write) or read and write (modify) permissions. (optional;" + Environment.NewLine +
                "must be \"read\", \"write\", or \"modify\". Default is \"modify\".",

                "Used to generate a mapping between OPC items and claimed/released mux in" + Environment.NewLine +
                "the system databases; has no meaning to OPC. Should be a decimal or hex" + Environment.NewLine +
                "value (if hex, prefix with \"0x\"). Optional.",

                "The active state of the item. This is only considered" + Environment.NewLine +
                "when the active state of the group is \"1\", and may be used to" + Environment.NewLine +
                "designate inactive items in an active group. (optional;" + Environment.NewLine +
                "default is \"1\")"
            };

            TypeValues   = new string[] { "default", "string" };
            AccessValues = new string[] { "read", "write", "modify" };

            #endregion

            GroupNames = new ObservableCollection <string>();
            ItemIDs    = new ObservableCollection <string>();
            ItemIDs.Add("NONE");

            OPCConnection = MainDataModel.ReadOPCMGR();

            Groups = new ObservableCollectionWithPropertyChanged <OPCGroup>(OPCConnection.Groups);
            foreach (OPCGroup g in Groups)
            {
                GroupNames.Add(g.name);
            }

            Groups.CollectionChanged += (s, e) =>
            {
                if (e.Action == NotifyCollectionChangedAction.Remove)
                {
                    GroupNames.RemoveAt(e.OldStartingIndex);
                }
                else if (e.Action == NotifyCollectionChangedAction.Add)
                {
                    GroupNames.Insert(e.NewStartingIndex, Groups[e.NewStartingIndex].name);
                }
                else // Property Changed
                {
                    int index = ((NotifyCollectionChangedEventArgs_WithPropertyChanged)e).Index;
                    GroupNames[index] = Groups[index].name;
                }
            };

            Items = new ObservableCollectionWithPropertyChanged <OPCItem>();
            foreach (OPCGroup g in Groups)
            {
                foreach (OPCItem i in g.Items)
                {
                    i.group = g.name;
                    Items.Add(i);
                    ItemIDs.Add(i.id);
                }
            }

            Items.CollectionChanged += (s, e) =>
            {
                if (e.Action == NotifyCollectionChangedAction.Remove)
                {
                    ItemIDs.RemoveAt(e.OldStartingIndex + 1);
                }
                else if (e.Action == NotifyCollectionChangedAction.Add)
                {
                    ItemIDs.Insert(e.NewStartingIndex + 1, Items[e.NewStartingIndex + 1].id);
                }
                else // Property Changed
                {
                    int index = ((NotifyCollectionChangedEventArgs_WithPropertyChanged)e).Index;
                    ItemIDs[index + 1] = Items[index].id;
                }
            };
        }