Example #1
0
        private void CopyDatabase()
        {
            string extension      = Path.GetExtension(fullDBPath);
            var    dbResourcePath = NSBundle.MainBundle.PathForResource(DBName.Replace(extension, null), extension.Remove(0, 1));

            File.Copy(dbResourcePath, fullDBPath);
        }
Example #2
0
        static void Main(string[] args)
        {
            // check if an instance is already running
            Process[] proc = Process.GetProcessesByName("FDACore");
            if (proc.Length > 1)
            {
                Console.WriteLine("An existing FDA instance was detected, closing");
                OperationalMessageServer.WriteLine("An existing FDA instance was detected, closing");
                return;
            }

            // check arguments for console mode
            Globals.ConsoleMode = args.Contains("-console");
            string message = "Starting the FDA in background mode";

            if (Globals.ConsoleMode)
            {
                message = "Starting the FDA in console mode";
            }
            Console.WriteLine(message);

            // generate a new execution ID
            ExecutionID = Guid.NewGuid();

            // record the time of execution
            Globals.ExecutionTime = Globals.FDANow();

            // get the FDA version string
            Globals.FDAVersion = Assembly.GetEntryAssembly().GetName().Version.ToString();

            // load the configuration from appsettings.json file
            string FDAID;
            string DBInstance;
            string DBName;
            string userName;
            string userPass;
            IConfigurationSection appConfig = null;

            try
            {
                configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json", false, true).Build();
                appConfig     = configuration.GetSection(nameof(AppSettings));
                FDAID         = appConfig["FDAID"];
                DBInstance    = appConfig["DatabaseInstance"];
                DBType        = appConfig["DatabaseType"];
                DBName        = appConfig["SystemDBName"];
                userName      = appConfig["SystemLogin"];
                userPass      = appConfig["SystemDBPass"];

                if (DBInstance.Contains("(default"))
                {
                    DBInstance = DBInstance.Replace("(default)", "");
                    LogEvent("DatabaseInstance setting not found, using the default value");
                }

                if (DBName.Contains("(default"))
                {
                    DBName = DBName.Replace("(default)", "");
                    LogEvent("SystemDBName setting not found, using the default value");
                }

                if (userName.Contains("(default"))
                {
                    userName = userName.Replace("(default)", "");
                    LogEvent("SystemLogin setting not found, using the default value");
                }

                if (userPass.Contains("(default"))
                {
                    userPass = userPass.Replace("(default)", "");
                    LogEvent("SystemLogin setting not found, using the default value ");
                }


                if (FDAID.Contains("(default"))
                {
                    FDAID = FDAID.Replace("(default)", "");
                    LogEvent("FDAID setting not found, using the default");
                }
            }
            catch (Exception ex)
            {
                OperationalMessageServer.WriteLine("Error while reading appsettings.json \"" + ex.Message + "\"\nFDA Exiting");
                Console.WriteLine("Error while reading appsettings.json \"" + ex.Message + "\"");
                Console.WriteLine("FDA Exiting");
                Console.Read();
                return;
            }

            // start basic services server
            Console.WriteLine("Starting the basic services control port server");
            _FDAControlServer = TCPServer.NewTCPServer(9572);
            if (_FDAControlServer != null)
            {
                _FDAControlServer.DataAvailable      += TCPServer_DataAvailable;
                _FDAControlServer.ClientDisconnected += TCPServer_ClientDisconnected;
                _FDAControlServer.ClientConnected    += TCPServer_ClientConnected;
                _FDAControlServer.Start();
            }
            else
            {
                Globals.SystemManager.LogApplicationError(Globals.FDANow(), TCPServer.LastError, "Error occurred while initializing the Basic Services Control Port Server");
            }

            // start operational messages server
            Console.WriteLine("Starting the operational messages streaming server");
            OperationalMessageServer.Start();

            // give clients a chance to connect to the server(s) before continuing
            Console.WriteLine("Waiting five seconds to allow clients to connect to the BSCP or OMSP ports");
            Thread.Sleep(5000);

            // if running in background mode: hide the console, disable the x button, and disable quick edit mode (windows only)
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                // hide the console window if the -show command line option is not specified
                if (!args.Contains("-console"))
                {
                    var handle = NativeMethods.GetConsoleWindow();
                    NativeMethods.ShowWindow(handle, NativeMethods.SW_HIDE);
                }
                else
                {
                    // the console window is shown, we need to change some settings

                    IntPtr conHandle = NativeMethods.GetConsoleWindow();

                    //disable the window close button(X)
                    NativeMethods.DeleteMenu(NativeMethods.GetSystemMenu(conHandle, false), NativeMethods.SC_CLOSE, NativeMethods.MF_BYCOMMAND);

                    // disable quick edit mode (this causes the app to pause when the user clicks in the console window)
                    IntPtr stdHandle = NativeMethods.GetStdHandle(NativeMethods.STD_INPUT_HANDLE);
                    if (!NativeMethods.GetConsoleMode(stdHandle, out int mode))
                    {
                        // error getting the console mode
                        Console.WriteLine("Error retrieving console mode");
                        OperationalMessageServer.WriteLine("Error retrieving the console mode");
                    }
                    mode &= ~(NativeMethods.QuickEditMode | NativeMethods.ExtendedFlags);
                    if (!NativeMethods.SetConsoleMode(stdHandle, mode))
                    {
                        // error setting console mode.
                        Console.WriteLine("Error setting console mode");
                        OperationalMessageServer.WriteLine("Error while settting the console mode");
                    }
                }
            }


            // initialize and start data acquisition
            try // general error catching
            {
                Globals.FDAIsElevated = false;
                Globals.FDAIsElevated = MQTTUtils.ThisProcessIsAdmin();



                // start the FDASystemManager
                FDASystemManager systemManager;
                switch (DBType.ToUpper())
                {
                case "POSTGRESQL": systemManager = new FDASystemManagerPG(DBInstance, DBName, userName, userPass, Globals.FDAVersion, ExecutionID); break;

                case "SQLSERVER": systemManager = new FDASystemManagerSQL(DBInstance, DBName, userName, userPass, Globals.FDAVersion, ExecutionID); break;

                default:
                    LogEvent("Unrecognized database server type '" + DBType + "'. Should be 'POSTGRESQL' or 'SQLSERVER'");
                    return;
                }



                Dictionary <string, FDAConfig> options = systemManager.GetAppConfig();

                systemManager.LogStartup(ExecutionID, Globals.FDANow(), Globals.FDAVersion);

                // set the "detailed messaging" flag
                if (options.ContainsKey("DetailedMessaging"))
                {
                    Globals.DetailedMessaging = (systemManager.GetAppConfig()["DetailedMessaging"].OptionValue == "on");
                }

                // connect to the MQTT broker
                Globals.MQTTEnabled = false;
                if (options.ContainsKey("MQTTEnabled"))
                {
                    Globals.MQTTEnabled = (options["MQTTEnabled"].OptionValue == "1");
                }

                if (Globals.MQTTEnabled)
                {
                    MQTTConnect("localhost");
                    Globals.MQTT?.Publish("FDA/DBType", Encoding.UTF8.GetBytes(DBType.ToUpper()), 0, true);
                }
                else
                {
                    Console.WriteLine("MQTT is disabled in AppConfig table, skipping MQTT connection");
                }

                Globals.FDAStatus = Globals.AppState.Starting;

                // create a DBManager of the selected type
                string    FDADBConnString = systemManager.GetAppDBConnectionString();
                DBManager dbManager;
                Console.WriteLine("Connecting to the " + DBType + " database");
                switch (DBType.ToUpper())
                {
                case "SQLSERVER": dbManager = new DBManagerSQL(FDADBConnString); break;

                case "POSTGRESQL": dbManager = new DBManagerPG(FDADBConnString); break;

                default:
                    Globals.SystemManager.LogApplicationEvent("FDA App", "", "unrecognized database server type '" + DBType + "', unable to continue");
                    return;
                }

                Console.WriteLine("Starting the Data Acquisition Manager");
                // start the DataAcqManager
                _dataAquisitionManager = new DataAcqManager(FDAID, dbManager, ExecutionID);

                // watch for changes to the MQTTEnabled option
                _dataAquisitionManager.MQTTEnableStatusChanged += DataAquisitionManager_MQTTEnableStatusChanged;

                if (_dataAquisitionManager.TestDBConnection())
                {
                    _dataAquisitionManager.Start();
                    Globals.FDAStatus = Globals.AppState.Normal;
                }
                else
                {
                    Console.WriteLine("Failed to connect to database, exiting");
                    return;
                }
            }
            catch (Exception ex)
            {
                if (Globals.SystemManager != null)
                {
                    Globals.SystemManager.LogApplicationError(Globals.FDANow(), ex, "FDA: error in application initilization");
                }
                else
                {
                    Console.WriteLine("FDA: error in application initilization: " + ex.Message + " Stacktrace: " + ex.StackTrace);
                }

                return;
            }

            while (!ShutdownComplete)
            {
                Thread.Sleep(1000);
            }
        }