Exemplo n.º 1
0
        /// <summary>
        /// This method starts up the assistant core.
        /// After registration of very important events on Program.cs, control moves to here.
        /// This is basically the entry point of the application.
        /// </summary>
        /// <param name="args">The startup arguments. (if any)</param>
        /// <returns></returns>
        public static async Task <bool> InitCore(string[] args)
        {
            if (File.Exists(Constants.TraceLogPath))
            {
                File.Delete(Constants.TraceLogPath);
            }

            Helpers.CheckMultipleProcess();
            StartupTime = DateTime.Now;

            if (Helpers.CheckForInternetConnection())
            {
                IsNetworkAvailable = true;
            }
            else
            {
                Logger.Log("No internet connection.", Enums.LogLevels.Warn);
                Logger.Log($"Starting {AssistantName} in offline mode...");
                IsNetworkAvailable = false;
            }

            Config                    = Config.LoadConfig();
            RunningPlatform           = Helpers.GetOsPlatform();
            AssistantName             = Config.AssistantDisplayName;
            Logger.LogIdentifier      = AssistantName;
            Config.ProgramLastStartup = StartupTime;
            Constants.LocalIP         = Helpers.GetLocalIpAddress();

            SecureLine = new SecureLineServer(IPAddress.Any, 7777);
            SecureLine.StartSecureLine();

            SendLocalIp(!Helpers.IsNullOrEmpty(Constants.LocalIP));

            Helpers.SetFileSeperator();

            Helpers.GenerateAsciiFromText(Config.AssistantDisplayName);
            Constants.ExternelIP = Helpers.GetExternalIp();

            File.WriteAllText("version.txt", Constants.Version.ToString());

            if (Helpers.IsNullOrEmpty(Constants.ExternelIP))
            {
                Constants.ExternelIP = "Failed.";
            }

            Helpers.InBackgroundThread(SetConsoleTitle, "Console Title Updater", true);
            Logger.Log($"X---------------- Starting {AssistantName} v{Constants.Version} ----------------X", Enums.LogLevels.Ascii);

            ConfigWatcher.InitConfigWatcher();
            ParseStartupArguments(args);

            if (!Helpers.IsNullOrEmpty(Config.PushBulletApiKey))
            {
                PushBulletService = new PushBulletService(Config.PushBulletApiKey);
                (bool status, UserDeviceListResponse currentPushDevices) = PushBulletService.InitPushService();

                if (status)
                {
                    Logger.Log("Push bullet service started.", Enums.LogLevels.Trace);
                }
            }

            if (!Helpers.IsRaspberryEnvironment())
            {
                DisablePiMethods = true;
                IsUnknownOs      = true;
            }

            if (Helpers.GetOsPlatform().Equals(OSPlatform.Windows))
            {
                AssistantStatus = new ProcessStatus();
            }
            else
            {
                Logger.Log("Could not start performence counters as it is not supported on this platform.", Enums.LogLevels.Trace);
            }

            await Update.CheckAndUpdateAsync(true).ConfigureAwait(false);

            if (Config.KestrelServer && !Helpers.IsNullOrEmpty(Config.KestrelServerUrl))
            {
                await KestrelServer.Start().ConfigureAwait(false);
            }

            ModuleLoader = new ModuleInitializer();

            ModuleLoader.LoadAndStartModulesOfType <IModuleBase>();

            ModuleWatcher.InitModuleWatcher();
            Controller = new PiController(true);
            CoreInitiationCompleted = true;

            await PostInitTasks().ConfigureAwait(false);

            return(true);
        }