public bool LoadServers(string file) { XmlDocument XmlDoc = new XmlDocument(); try { XmlDoc.Load(System.IO.Path.GetFullPath(file)); } catch { return(false); } if (XmlDoc.DocumentElement == null) { return(false); } if (XmlDoc.DocumentElement.Name == "servergroups") { foreach (XmlNode XmlGroup in XmlDoc.SelectNodes("servergroups/servergroup")) { ServerGroup servergroup = AddServerGroup(); foreach (XmlNode XmlServer in XmlGroup.SelectNodes("servers/server")) { string Address = XmlServer.SelectSingleNode("address").InnerText; int Port = int.Parse(XmlServer.SelectSingleNode("port").InnerText); int Connections = int.Parse(XmlServer.SelectSingleNode("connections").InnerText); bool RequiresLogin = XmlServer.SelectSingleNode("login").HasChildNodes; string Username = ""; string Password = ""; if (RequiresLogin) { Username = XmlServer.SelectSingleNode("login/username").InnerText; Password = XmlServer.SelectSingleNode("login/password").InnerText; } bool NeedsGroup = true; if (XmlServer.SelectSingleNode("needsgroup") != null) { NeedsGroup = (XmlServer.SelectSingleNode("needsgroup").InnerText.ToLower() == "true"); } Server server = servergroup.AddServer(new Server(Address, Port, Connections, RequiresLogin, Username, Password, NeedsGroup)); if (XmlServer.Attributes.GetNamedItem("enabled") != null) { if (!bool.Parse(XmlServer.Attributes.GetNamedItem("enabled").InnerText)) { DisableServer(server); } } } } } return(true); }
/// <summary> /// Reading all readings from an OPC server. /// </summary> private void ProcessOPCServer(OOPCDAServer opcDaServer) { if (opcDaServer.AutomaticPollingEnabled == 0) return; XmlServer srv = null; try { srv = new XmlServer(opcDaServer.ObjectName); List<OPoint> points = TablesLogic.tPoint.LoadList( TablesLogic.tPoint.OPCDAServerID == opcDaServer.ObjectID); if (points.Count == 0) return; List<ReadRequestItem> readRequestItems = new List<ReadRequestItem>(); Hashtable hash = new Hashtable(); foreach (OPoint point in points) { readRequestItems.Add(NewReadRequestItem(point.OPCItemName)); hash[point.OPCItemName] = point; } ReadRequestItemList readItemList = new ReadRequestItemList(); readItemList.Items = readRequestItems.ToArray(); ReplyBase reply; RequestOptions options = new RequestOptions(); options.ReturnErrorText = true; options.ReturnItemName = true; ReplyItemList rslt; OPCError[] err; reply = srv.Read(options, readItemList, out rslt, out err); if (rslt == null) throw new Exception(err[0].Text); else { int count = 0; foreach (ItemValue iv in rslt.Items) { if (iv.ResultID == null) { try { using (Connection c = new Connection()) { // Create the readings. // OReading reading = TablesLogic.tReading.Create(); OPoint point = hash[iv.ItemName] as OPoint; if (point != null) { reading.PointID = point.ObjectID; reading.LocationID = point.LocationID; //reading.LocationTypeParameterID = point.LocationTypeParameterID; reading.EquipmentID = point.EquipmentID; //reading.EquipmentTypeParameterID = point.EquipmentTypeParameterID; reading.DateOfReading = DateTime.Now; if (iv.Value is bool) reading.Reading = ((bool)iv.Value) ? 1 : 0; else reading.Reading = Convert.ToDecimal(iv.Value.ToString()); reading.Source = ReadingSource.OPCServer; reading.CheckForBreachOfReading(point); reading.Save(); count++; } c.Commit(); } } catch (Exception ex) { LogEvent(ex.Message + "\n\n" + ex.StackTrace, BackgroundLogMessageType.Error); } } else LogEvent("Error reading '" + iv.ItemName + "': " + iv.ResultID.Name, BackgroundLogMessageType.Error); } LogEvent(count + " points out of " + readItemList.Items.Length + " successfully read from server '" + opcDaServer.ObjectName + "'."); } } catch (Exception ex) { LogEvent(ex.Message + "\n\n" + ex.StackTrace, BackgroundLogMessageType.Error); } finally { if (srv != null) srv.Dispose(); } }