예제 #1
0
파일: Program.cs 프로젝트: NeoTim/VlixOPC
        static void Main(string[] args)
        {
            Global.Product = new ProductDetails("VlixOPC", typeof(OPCBackEndConfig), "Vlix\\VlixOPC");
            bool SendResult = (new EmbeddedResourceFileSender()).SendFiles("VlixOPC", "SendToProgramDataDirectory", Global.Product.ProgramDataDirectory, "*", false, Assembly.GetExecutingAssembly());

            if (!SendResult)
            {
                throw new CustomException("FATAL ERROR: Unable to access Directory '" + Global.Product.ProgramDataDirectory);
            }
            OPCBackEnd oPCBackEnd = new OPCBackEnd();

            Console.ReadKey();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
예제 #2
0
        public Task <Res> TrySaveSettings(OPCBackEndConfig OPCBackEndConfig)
        {
            try
            {
                Logger.Log("Saving and applying OPC Service Back End Configuration...");
                bool             ChangeOccured           = false;
                OPCBackEndConfig CurrentOPCBackEndConfig = (OPCBackEndConfig)Global.Product.Config;

                //HTTP Settings
                if (CurrentOPCBackEndConfig.Enable_WebAPI_Http != OPCBackEndConfig.Enable_WebAPI_Http ||
                    CurrentOPCBackEndConfig.Http_Port != OPCBackEndConfig.Http_Port)
                {
                    ChangeOccured = true;
                    if (OPCBackEndConfig.Enable_WebAPI_Http)
                    {
                        OPCBackEnd.WCFHost_Http?.Stop(); OPCBackEnd.EnableHttp();
                    }
                    else
                    {
                        OPCBackEnd.WCFHost_Http?.Stop();
                    }
                }

                //HTTPS Settings
                if (CurrentOPCBackEndConfig.Enable_WebAPI_Https != OPCBackEndConfig.Enable_WebAPI_Https ||
                    CurrentOPCBackEndConfig.Https_Port != OPCBackEndConfig.Https_Port ||
                    CurrentOPCBackEndConfig.Enable_WebAPI_BasicAuthentication != OPCBackEndConfig.Enable_WebAPI_BasicAuthentication ||
                    CurrentOPCBackEndConfig.WebAPI_Username != OPCBackEndConfig.WebAPI_Username ||
                    CurrentOPCBackEndConfig.WebAPI_Password != OPCBackEndConfig.WebAPI_Password ||
                    CurrentOPCBackEndConfig.SubjectAlternativeNames_ForHTTPSCert != OPCBackEndConfig.SubjectAlternativeNames_ForHTTPSCert)
                {
                    ChangeOccured = true;
                    if (OPCBackEndConfig.Enable_WebAPI_Https)
                    {
                        OPCBackEnd.WCFHost_Https?.Stop(); OPCBackEnd.EnableHttps();
                    }
                    else
                    {
                        OPCBackEnd.WCFHost_Https?.Stop();
                    }
                }

                //Tcp Settings
                if (CurrentOPCBackEndConfig.Enable_Tcp_Authentication != OPCBackEndConfig.Enable_Tcp_Authentication ||
                    CurrentOPCBackEndConfig.Tcp_Port != OPCBackEndConfig.Tcp_Port ||
                    CurrentOPCBackEndConfig.Tcp_Username != OPCBackEndConfig.Tcp_Username ||
                    CurrentOPCBackEndConfig.Tcp_Password != OPCBackEndConfig.Tcp_Password)
                {
                    ChangeOccured = true;
                    OPCBackEnd.WCFHost_Tcp?.Stop();
                    OPCBackEnd.WCFHost_Tcp.ListeningPort            = OPCBackEndConfig.Tcp_Port;
                    OPCBackEnd.WCFHost_Tcp.EnableUserAuthentication = OPCBackEndConfig.Enable_Tcp_Authentication && !(OPCBackEndConfig.Tcp_Username.IsNullOrEmpty() && OPCBackEndConfig.Tcp_Password.IsNullOrEmpty());

                    OPCBackEnd.WCFHost_Tcp.Start();
                }

                //OPC Settings
                if (CurrentOPCBackEndConfig.OPCClassic_SubscriptionGroupSizeLimit != OPCBackEndConfig.OPCClassic_SubscriptionGroupSizeLimit)
                {
                    ChangeOccured = true;
                    OPCBackEnd.OPCClassicBrowserEngine.OPCGroupSizeLimit = OPCBackEndConfig.OPCClassic_SubscriptionGroupSizeLimit.ToInt(40);
                }
                if (CurrentOPCBackEndConfig.OPCCUA_SubscriptionGroupSizeLimit != OPCBackEndConfig.OPCCUA_SubscriptionGroupSizeLimit)
                {
                    ChangeOccured = true;
                    OPCBackEnd.OPCUABrowserEngine.OPCGroupSizeLimit = OPCBackEndConfig.OPCCUA_SubscriptionGroupSizeLimit.ToInt(40);
                }
                if (ChangeOccured)
                {
                    OPCBackEnd.Config     = OPCBackEndConfig;
                    Global.Product.Config = OPCBackEndConfig;
                    Global.Product.Config.TrySaveConfigFile(out string ExStrC);
                    Logger.Log("Successfully saved OPC Service Back End Configuration at '" + Global.Product.ConfigFilePath + "'");
                }
                else
                {
                    Logger.Log("Save did not execute as there were no changes made!");
                }
                return(Task.FromResult(new Res()));
            }
            catch (Exception ex)
            {
                return(Task.FromResult(new Res(ex.ToString())));;
            }
        }
예제 #3
0
        /// <summary>
        /// THe main Class for the OPC Back End
        /// </summary>
        /// <param name="LocalPipeName">Use for OPC Admin Communication</param>
        public OPCBackEnd(string LocalPipeName = null)
        {
            Logger.ClearLogs();
            if (!Global.Product.TryLoadConfigFile())
            {
                throw new CustomException("FATAL ERROR: Unable to load Config File '" + Global.Product.ConfigFilePath + "'");
            }
            OPCBackEnd.Config = (OPCBackEndConfig)Global.Product.Config;
            Logger.EnableLogger();
            Thread.Sleep(100);
            Logger.Log("***************************************************");
            Thread.Sleep(100);
            Logger.Log("VLIX OPC (BACK END) STARTED");
            Thread.Sleep(100);
            Logger.Log("***************************************************");
            Thread.Sleep(100);
            Logger.Log("Log Path = '" + Global.Product.LogFilePath + "'");
            OPCBackEnd.OPCClassicBrowserEngine = new OPCClassicBrowserEngine();
            OPCBackEnd.OPCUABrowserEngine      = new OPCUABrowserEngine();


            //*************************************************
            //   REPORT NUMBER OF CALLS PER MINUTE
            //*************************************************
            (new Thread(async() =>
            {
                while (true)
                {
                    DateTime DTGate = DateTime.UtcNow.AddMinutes(-1);
                    int TotalCallsLastMinute = TotalAPICalls.Count(C => C > DTGate);
                    Logger.Log("Total API Calls Last Minute = " + TotalCallsLastMinute);
                    try { TotalAPICalls.RemoveWhere(C => C < DTGate); } catch { }
                    await Task.Delay(120000);
                }
            })).Start();


            //*************************************************
            //   ENABLE NAMED PIPE AND NET TCP FOR CONFIGURATION FRONT END
            //*************************************************
            if (LocalPipeName.IsNullOrWhiteSpace())
            {
                LocalPipeName = "VlixOPC";
            }
            //string LocalPipeName = GlobalWCF.GetLocalPipeName();
            WCFHost_Pipe = new WCFHost <OPCServiceContract, iOPCServiceContract>(LocalPipeName, "");
            WCFHost_Pipe.Start();
            WCFHost_Tcp = new WCFHost <OPCServiceContract, iOPCServiceContract>(OPCBackEnd.Config.Tcp_Port, HostProtocolType.NetTCPSecure, OPCBackEnd.Config.Enable_Tcp_Authentication)
            {
                OnValidatingUsernamePassword = (string UN, string PW, out UserBase user) =>
                {
                    user = null;
                    return(string.Equals(UN, OPCBackEnd.Config.Tcp_Username, StringComparison.OrdinalIgnoreCase) && PW == OPCBackEnd.Config.Tcp_Password);
                }
            };
            WCFHost_Tcp.Start();


            //****************************************
            //   ENABLE HTTP
            //****************************************
            if (OPCBackEnd.Config.Enable_WebAPI_Http)
            {
                OPCBackEnd.EnableHttp();
            }
            else
            {
                Logger.Log("Http API not Enabled");
            }


            //****************************************
            //   ENABLE HTTPS
            //****************************************
            if (OPCBackEnd.Config.Enable_WebAPI_Https)
            {
                OPCBackEnd.EnableHttps();
            }
            else
            {
                Logger.Log("Https Secure API not Enabled");
            }
        }
예제 #4
0
        public OPCBackEnd()
        {
            Logger.ClearLogs();
            if (!Global.Product.TryLoadConfigFile())
            {
                throw new CustomException("FATAL ERROR: Unable to load Config File '" + Global.Product.ConfigFilePath + "'");
            }
            OPCBackEnd.Config = (OPCBackEndConfig)Global.Product.Config;
            Logger.EnableLogger();
            Thread.Sleep(100);
            Logger.Log("***************************************************");
            Thread.Sleep(100);
            Logger.Log("VLIX OPC (BACK END) STARTED");
            Thread.Sleep(100);
            Logger.Log("***************************************************");
            Thread.Sleep(100);
            Logger.Log("Log Path = '" + Global.Product.LogFilePath + "'");
            OPCBackEnd.OPCClassicBrowserEngine = new OPCClassicBrowserEngine();
            OPCBackEnd.OPCUABrowserEngine      = new OPCUABrowserEngine();


            //*************************************************
            //   REPORT NUMBER OF CALLS PER MINUTE
            //*************************************************
            (new Thread(async() =>
            {
                while (true)
                {
                    DateTime DTGate = DateTime.UtcNow.AddMinutes(-1);
                    int TotalCallsLastMinute = TotalAPICalls.Count(C => C > DTGate);
                    Logger.Log("Total API Calls Last Minute = " + TotalCallsLastMinute);
                    try { TotalAPICalls.RemoveWhere(C => C < DTGate); } catch { }
                    await Task.Delay(30000);
                }
            })).Start();


            //*************************************************
            //   ENABLE NAMED PIPE FOR CONFIGURATION FRONT END
            //*************************************************
            string LocalPipeName = Global.GetLocalPipeName();

            WCFHost_Pipe = new WCFHost <VlixOPCContract, iVlixOPCContract>(LocalPipeName, "");
            WCFHost_Pipe.Start();



            //****************************************
            //   ENABLE HTTP
            //****************************************
            if (OPCBackEnd.Config.EnableAPI_Http)
            {
                OPCBackEnd.EnableHttp();
            }
            else
            {
                Logger.Log("Http API not Enabled");
            }


            //****************************************
            //   ENABLE HTTPS
            //****************************************
            if (OPCBackEnd.Config.EnableAPI_Https)
            {
                OPCBackEnd.EnableHttps();
            }
            else
            {
                Logger.Log("Https Secure API not Enabled");
            }
        }
예제 #5
0
        public bool TrySaveSettings(OPCBackEndConfig OPCBackEndConfig)
        {
            try
            {
                Logger.Log("Saving and applying Vlix OPC New Back End Configuration...");
                bool             ChangeOccured           = false;
                OPCBackEndConfig CurrentOPCBackEndConfig = (OPCBackEndConfig)Global.Product.Config;

                //HTTP Settings
                if (CurrentOPCBackEndConfig.EnableAPI_Http != OPCBackEndConfig.EnableAPI_Http ||
                    CurrentOPCBackEndConfig.Http_Port != OPCBackEndConfig.Http_Port)
                {
                    ChangeOccured = true;
                    if (OPCBackEndConfig.EnableAPI_Http)
                    {
                        OPCBackEnd.WCFHost_Http?.Stop(); OPCBackEnd.EnableHttp();
                    }
                    else
                    {
                        OPCBackEnd.WCFHost_Http?.Stop();
                    }
                }

                //HTTPS Settings
                if (CurrentOPCBackEndConfig.EnableAPI_Https != OPCBackEndConfig.EnableAPI_Https ||
                    CurrentOPCBackEndConfig.Https_Port != OPCBackEndConfig.Https_Port ||
                    CurrentOPCBackEndConfig.RequireAPIBasicAuthentication != OPCBackEndConfig.RequireAPIBasicAuthentication ||
                    CurrentOPCBackEndConfig.Username_ForAPIBasicAuthentication != OPCBackEndConfig.Username_ForAPIBasicAuthentication ||
                    CurrentOPCBackEndConfig.Password_ForAPIBasicAuthentication != OPCBackEndConfig.Password_ForAPIBasicAuthentication ||
                    CurrentOPCBackEndConfig.SubjectAlternativeNames_ForHTTPSCert != OPCBackEndConfig.SubjectAlternativeNames_ForHTTPSCert)
                {
                    ChangeOccured = true;
                    if (OPCBackEndConfig.EnableAPI_Https)
                    {
                        OPCBackEnd.WCFHost_Https?.Stop(); OPCBackEnd.EnableHttps();
                    }
                    else
                    {
                        OPCBackEnd.WCFHost_Https?.Stop();
                    }
                }

                //OPC Settings
                if (CurrentOPCBackEndConfig.OPCClassic_SubscriptionGroupSizeLimit != OPCBackEndConfig.OPCClassic_SubscriptionGroupSizeLimit)
                {
                    ChangeOccured = true;
                    OPCBackEnd.OPCClassicBrowserEngine.OPCGroupSizeLimit = OPCBackEndConfig.OPCClassic_SubscriptionGroupSizeLimit.ToInt(40);
                }
                if (CurrentOPCBackEndConfig.OPCCUA_SubscriptionGroupSizeLimit != OPCBackEndConfig.OPCCUA_SubscriptionGroupSizeLimit)
                {
                    ChangeOccured = true;
                    OPCBackEnd.OPCUABrowserEngine.OPCGroupSizeLimit = OPCBackEndConfig.OPCCUA_SubscriptionGroupSizeLimit.ToInt(40);
                }
                if (ChangeOccured)
                {
                    OPCBackEnd.Config     = OPCBackEndConfig;
                    Global.Product.Config = OPCBackEndConfig;
                    Global.Product.Config.TrySaveConfigFile(out string ExStrC);
                    Logger.Log("Successfully saved Vlix OPC Back End Configuration at '" + Global.Product.ConfigFilePath + "'");
                }
                else
                {
                    Logger.Log("Save did not execute as there were no changes made!");
                }
                return(true);
            }
            catch
            {
                return(false);
            }
        }