Ejemplo n.º 1
0
        /// <summary>
        /// Hier wird überprüft, ob alle notwendigen Felder vorhanden sind
        /// </summary>
        private static void Validate(XmlUarcConfig config)
        {
            foreach (XmlOpcUaServer c_OpcUaServer in config.XmlOpcUaServers)
            {
                //Überprüfen, ob Serverbezeichnung vorhanden ist
                if (c_OpcUaServer.XmlServerlabel == null)
                {
                    string e = "Ein OPC UA Server enthält keinen \"Serverbezeichnung\"";
                    throw new IOException(e);
                }

                //Überprüfen, ob OpcURL vorhanden ist
                if (c_OpcUaServer.XmlOpcUrl == null)
                {
                    string e = "Der Server: " + c_OpcUaServer.XmlServerlabel + " besitzt keine \"URL\"";
                    throw new IOException(e);
                }

                //Überprüfen der OPCSubscription Konfiguration
                foreach (XmlOpcUaSubscription c_OpcUaSubscription in c_OpcUaServer.XmlOpcUaSubscriptions)
                {
                    if (c_OpcUaSubscription.XmlPublishingInterval == null)
                    {
                        string e = "Eine Subscription vom " + c_OpcUaServer.XmlServerlabel + " besitzt kein \"PublishingInterval\"";
                        throw new IOException(e);
                    }

                    //Überprüfen der OPCVariablen Konfiguration
                    foreach (XmlOpcUaVariable c_OpcUaVariable in c_OpcUaSubscription.XmlOpcUaVariablen)
                    {
                        //Überprüfen, ob ein Name vorhanden ist
                        if (c_OpcUaVariable.XmlVarLabel == null)
                        {
                            string e = "Eine Variable vom " + c_OpcUaServer.XmlServerlabel + " besitzt keinen \"Name\"";
                            throw new IOException(e);
                        }

                        //Überprüfen, ob eine NodeID vorhanden ist. Wenn nur die Daten der S7 vorhanden sind, wird die zugehörige NodeID erstellt
                        if (c_OpcUaVariable.XmlNodeId == null && c_OpcUaVariable.XmlS7db != null && c_OpcUaVariable.XmlS7var != null)
                        {
                            c_OpcUaVariable.XmlNodeId = ConvertS7DataToNodeID(c_OpcUaVariable.XmlS7db, c_OpcUaVariable.XmlS7var);
                        }
                        else if (c_OpcUaVariable.XmlNodeId != null && c_OpcUaVariable.XmlS7db == null && c_OpcUaVariable.XmlS7var == null)
                        {
                        }
                        else
                        {
                            string e = "Die Variable " + c_OpcUaVariable.XmlVarLabel + " vom " + c_OpcUaServer.XmlServerlabel + " ist nicht ausreichend definiert";
                            throw new IOException(e);
                        }

                        //Überprüfen, ob ein Samling Intervall eingestellt wurde, falls nicht wird 1 Sekunde eingestellt
                        if (c_OpcUaVariable.XmlSamplingInterval == "")
                        {
                            c_OpcUaVariable.XmlSamplingInterval = "100";
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Erstellt aus der XML Konfiguration Clients
        /// </summary>
        /// <param name="filePath"></param>
        public void CreateUarcClientsAndDbWithXml(string filePath)
        {
            // Erstellen der XmlUarcConfig.
            XmlUarcConfig opcXmlConfig = XmlConfigFileRead(filePath);

            // Erstellen der Clients und der Datenbank.
            CreateUarcClientsAndDbWithConfig(opcXmlConfig);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Hier wird eine OpcXML_Konfigurations Datei in das Filesystem geschrieben.
        /// </summary>
        /// <param name="path"></param>
        /// <param name="config"></param>
        public static void OpcUaXmlFileWrite(string path, XmlUarcConfig config)
        {
            XmlSerializer serialzer = new XmlSerializer(typeof(XmlUarcConfig));
            TextWriter    writer    = File.CreateText(path);

            serialzer.Serialize(writer, config);
            writer.Close();
        }
Ejemplo n.º 4
0
        /// <summary>
        /// XML Beispiel Konfigurations Datei schreiben
        /// </summary>
        /// <param name="path"></param>
        public static void OpcUaXmlExampleFileWrite(string path)
        {
            //Initialisierung der Beipsiel Konfiguration als Beispiel-Objekt
            XmlOpcUaExampleConfigFile BeispielConfig = new XmlOpcUaExampleConfigFile();
            XmlUarcConfig             Config         = BeispielConfig.OpcUaXmlExampleFileCreate();

            //Erstellen der XML-Datei aus dem Beispiel-Objekt
            UarcParser.OpcUaXmlFileWrite(path, Config);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Mit dieser Methode wird die XML Konfigurationsdatei eingelesen und als Objekt ausgegeben.
        /// </summary>
        /// <param name="Pfad">Enthält den Ablageort der Konfigurations-Datei</param>
        private XmlUarcConfig XmlConfigFileRead(string Pfad)
        {
            XmlUarcConfig opcXmlConfig = null;

            // Einlesen der XML-Datei.
            opcXmlConfig = OpcUaXmlConfigIO.OpcUaXmlConfigFileRead(Pfad);

            // Überprüfen der Daten.
            OpcUaXmlConfigChecker.FormatAndValidate(opcXmlConfig);

            return(opcXmlConfig);
        }
        public IActionResult Check(string path)
        {
            XmlConfigViewModel configModel = new XmlConfigViewModel();
            XmlUarcConfig      config      = OpcUaXmlConfigIO.OpcUaXmlConfigFileRead(path);

            try
            {
                OpcUaXmlConfigChecker.FormatAndValidate(config);
            }
            catch
            {
                throw new Exception("Konfigurationsdatei ist nicht korrekt");
            }

            OpcUaXmlConfigIO.OpcUaXmlConfigFileWrite(path, config);;

            configModel.Path = path;

            return(View(configModel));
        }
Ejemplo n.º 7
0
        public void CreateUarcClientsAndDbWithConfig(XmlUarcConfig opcXmlConfig)
        {
            // Erstellen der Datenbank.
            try
            {
                CreateDatabase(opcXmlConfig.XmlSQLConfig.XmlSqlConnectionString);
            }
            catch (Exception e)
            {
                if (e.Message == null)
                {
                    e = new Exception(String.Format("Die SQL Datenbank konnte nicht erstellt werden"));
                    throw e;
                }
                else
                {
                    throw e;
                }
            }

            // Erstellen der Clients.
            if (opcXmlConfig != null)
            {
                // Das opcXmlConfig Object wird in ein Array umgewandelt, um es mit einer foreach Schleife zu durchlaufen.
                XmlOpcUaServer[] C_OPCUAServers = opcXmlConfig.XmlOpcUaServers.ToArray();

                // In dieser foreach Schleife werden jeweils die Instanzen von MyClient für jeden Server erzeugt und im Dictionary abgelegt.
                foreach (XmlOpcUaServer c_opcuaserver in C_OPCUAServers)
                {
                    UarcOpcUaClient Client = new UarcOpcUaClient(m_SqlDbOptions, c_opcuaserver.XmlOpcUrl, c_opcuaserver.XmlServerlabel, c_opcuaserver.XmlUser, c_opcuaserver.XmlPassword, serverId, c_opcuaserver.XmlOpcUaSubscriptions);

                    //Ablegen des Objects im Dictionary
                    m_OpcUaVerbindungen.Add(Convert.ToString(serverId) + ": " + c_opcuaserver.XmlServerlabel, Client);
                    serverId = serverId + 1;
                }
            }
            else
            {
                throw new Exception("Es sind keine Konfigurationsdaten verfügbar. Bitte lesen Sie eine gültige Konfigurationsdatei ein");
            }
        }
        public async Task <FileResult> Export()
        {
            try
            {
                // Erstellen der XmlUarcConfig aus dem Dicitionary vom UarcColletor.
                XmlUarcConfig xmlUarcConfig = await Program.UarcCollector.ExportXmlConfigAsync();

                // Erstellen des Strings mit Datum und Uhrzeit.
                string dateAndTime = (Convert.ToString(System.DateTime.Now)).Replace(':', '-');

                // XML-Dokumentname.
                string backupFileName = "UARC_Collector Backup, vom: " + dateAndTime + ".xml";

                // XML-Ablageort.
                string filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "XML Dateien", "Backups", backupFileName);

                // Erzeugen der XML Konfiguration.
                OpcUaXmlConfigIO.OpcUaXmlConfigFileWrite(filePath, xmlUarcConfig);

                // Ausgeben der Beispiel Konfiguration.
                byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);
                return(File(fileBytes, "application/x-msdownload", backupFileName));
            }
            catch (Exception e)
            {
                if (e.Message == null)
                {
                    e = new Exception("Konfiguration konnte nicht exportiert werden.");
                    throw e;
                }
                else
                {
                    throw e;
                }
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Hier werden alle Clients beendet und neu erstellt.
        /// </summary>
        /// <returns></returns>
        public async Task RecreateAllUarcClientsAsync()
        {
            // Die Konfiguration wird gesichert.
            XmlUarcConfig config = await ExportXmlConfigAsync();

            // Alle Sessions werden geschlossen.
            foreach (KeyValuePair <string, UarcOpcUaClient> client in m_OpcUaVerbindungen)
            {
                await client.Value.SaveAndCloseClientAsync();
            }

            // Interne Parameter werden zurückgesetzt.
            m_SqlDbOptions        = null;
            m_SqlConnectionString = null;
            m_OpcUaVerbindungen.Clear();
            serverId = 0;


            // Alle Clients und Datenbanken werden neu erstellt.
            CreateUarcClientsAndDbWithConfig(config);

            // Alle Clients werden initiert.
            await InitAllUarcClientsAsync();
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Mit dieser Methode wird die Konfiguration wieder aus dem Dictionary exportiert
        /// </summary>
        /// <returns>XmlUarcConfig</returns>
        public async Task <XmlUarcConfig> ExportXmlConfigAsync()
        {
            // XmlUarcConfig Objekt wird erzeugt.
            XmlUarcConfig opcXmlConfig = new XmlUarcConfig();

            // XmlUarcConfig Objekt wird mit den Daten aus dem Dicitionary befüllt.
            foreach (UarcOpcUaClient client in m_OpcUaVerbindungen.Values)
            {
                // Das Server Abbild wird erstellt.
                XmlOpcUaServer xmlOpcUaServer = new XmlOpcUaServer
                {
                    XmlUser        = client.Username,
                    XmlPassword    = client.Password,
                    XmlServerlabel = client.ServerLabel,
                    XmlOpcUrl      = client.EndpointUrl
                };

                // Alle Subscriptions werden aus der Session gelesen und im Client Obj gesichert.
                await client.SaveAllSubscriptionsAsync();

                // Für alle Subscription wird ein Abbild erstellt.
                foreach (Subscription sub in client.Subscriptions)
                {
                    XmlOpcUaSubscription xmlOpcUaSubscription = new XmlOpcUaSubscription
                    {
                        XmlPublishingInterval = Convert.ToString(sub.PublishingInterval)
                    };

                    foreach (MonitoredItem item in sub.MonitoredItems)
                    {
                        XmlOpcUaVariable xmlOpcUaVariable = new XmlOpcUaVariable
                        {
                            XmlVarLabel         = item.DisplayName,
                            XmlNodeId           = Convert.ToString(item.ResolvedNodeId),
                            XmlSamplingInterval = Convert.ToString(item.SamplingInterval)
                        };

                        xmlOpcUaSubscription.XmlOpcUaVariablen.Add(xmlOpcUaVariable);
                    }
                    xmlOpcUaServer.XmlOpcUaSubscriptions.Add(xmlOpcUaSubscription);
                }

                // Alle gesicherten Subscriptions werden entfernt.
                client.Subscriptions.ToList().Clear();

                // Jede Client Konfiguration wird dem KonfigurationsObjekt hinzugefügt
                opcXmlConfig.XmlOpcUaServers.Add(xmlOpcUaServer);
            }

            XmlSql SqlConfig = new XmlSql
            {
                XmlSqlConnectionString = m_SqlConnectionString,
            };

            // Der Connectionstring wird ermittelt.
            opcXmlConfig.XmlSQLConfig = SqlConfig;

            // Das XmlUarcConfig Objekt wird überprüft.
            OpcUaXmlConfigChecker.FormatAndValidate(opcXmlConfig);

            // XmlUarcConfig Objekt wird zurückgegeben.
            return(opcXmlConfig);
        }
Ejemplo n.º 11
0
        // Erzeugen der Xml Beispiel Konfiguration.
        public XmlUarcConfig OpcUaXmlExampleFileCreate()
        {
            XmlUarcConfig config = new XmlUarcConfig();

            XmlOpcUaServer StandardOpcUaServer = new XmlOpcUaServer
            {
                XmlUser        = "******",
                XmlPassword    = "******",
                XmlServerlabel = "OPC UA Server - OPC Foundation",
                XmlOpcUrl      = "opc.tcp://*****:*****@"Server=localhost\SQLEXPRESS;Database=OpcUaDataDb;Trusted_Connection=True;",
            };

            XmlOpcUaSubscription StandardSubscription = new XmlOpcUaSubscription
            {
                XmlPublishingInterval = "1",
            };

            XmlOpcUaSubscription BeispielSubscription = new XmlOpcUaSubscription
            {
                XmlPublishingInterval = "10",
            };

            StandardSubscription.XmlOpcUaVariablen.Add(StandardOpcUaVar);
            BeispielSubscription.XmlOpcUaVariablen.Add(S7OpcUaVar);

            StandardOpcUaServer.XmlOpcUaSubscriptions.Add(StandardSubscription);
            S7OpcUaServer.XmlOpcUaSubscriptions.Add(BeispielSubscription);

            config.XmlOpcUaServers.Add(StandardOpcUaServer);
            config.XmlOpcUaServers.Add(S7OpcUaServer);

            config.XmlSQLConfig = SqlConfig;

            return(config);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Hier werden alle Felder die keine Inforamtion erhalten auf "null" gestzt, um einen einfacherne Umgang mit der Konfiguration zu ermöglichen
        /// </summary>
        private static void Format(XmlUarcConfig config)
        {
            /// <summary>
            /// Hier wird die Konfigurationsdatei bearabeitet
            /// </summary>
            foreach (XmlOpcUaServer c_OpcUaServer in config.XmlOpcUaServers)
            {
                if (c_OpcUaServer.XmlServerlabel == "")
                {
                    c_OpcUaServer.XmlServerlabel = null;
                }

                if (c_OpcUaServer.XmlOpcUrl == "")
                {
                    c_OpcUaServer.XmlOpcUrl = null;
                }

                if (c_OpcUaServer.XmlUser == "")
                {
                    c_OpcUaServer.XmlUser = null;
                }

                if (c_OpcUaServer.XmlPassword == "")
                {
                    c_OpcUaServer.XmlPassword = null;
                }

                foreach (XmlOpcUaSubscription c_OpcUaSubscription in c_OpcUaServer.XmlOpcUaSubscriptions)
                {
                    if (c_OpcUaSubscription.XmlPublishingInterval == "")
                    {
                        c_OpcUaSubscription.XmlPublishingInterval = null;
                    }

                    foreach (XmlOpcUaVariable c_OpcUaVariable in c_OpcUaSubscription.XmlOpcUaVariablen)
                    {
                        if (c_OpcUaVariable.XmlVarLabel == "")
                        {
                            c_OpcUaVariable.XmlVarLabel = null;
                        }

                        if (c_OpcUaVariable.XmlNodeId == "")
                        {
                            c_OpcUaVariable.XmlNodeId = null;
                        }

                        if (c_OpcUaVariable.XmlS7db == "")
                        {
                            c_OpcUaVariable.XmlS7db = null;
                        }

                        if (c_OpcUaVariable.XmlS7var == "")
                        {
                            c_OpcUaVariable.XmlS7var = null;
                        }

                        if (c_OpcUaVariable.XmlSamplingInterval == "")
                        {
                            c_OpcUaVariable.XmlSamplingInterval = null;
                        }
                    }
                }
            }
        }
Ejemplo n.º 13
0
 /// <summary>
 /// Hier wird das Objekt einer Konfigurationsdatei übergeben, danach formatiert, überprüft und zurückgegeben
 /// </summary>
 /// <param name="config"></param>
 public static void FormatAndValidate(XmlUarcConfig config)
 {
     Format(config);
     Validate(config);
 }
Ejemplo n.º 14
0
 /// <summary>
 /// XML Konfigurations Datei schreiben
 /// </summary>
 /// <param name="path"></param>
 /// <param name="config"></param>
 public static void OpcUaXmlConfigFileWrite(string path, XmlUarcConfig config)
 {
     UarcParser.OpcUaXmlFileWrite(path, config);
 }
Ejemplo n.º 15
0
        /// <summary>
        /// XML Konfigurations Datei einlesen
        /// </summary>
        /// <param name="path"></param>
        /// <returns>XML-Konfigurations Objekt</returns>
        public static XmlUarcConfig OpcUaXmlConfigFileRead(string path)
        {
            XmlUarcConfig config = UarcParser.OpcUaXmlFileRead(path);

            return(config);
        }