コード例 #1
0
ファイル: Core.cs プロジェクト: Prodject/HomeAssistant
        public static async Task OnExit()
        {
            Logger.Log("Shutting down...");
            if (ModuleLoader != null)
            {
                await ModuleLoader.ExecuteAsyncEvent(Enums.AsyncModuleContext.AssistantShutdown).ConfigureAwait(false);
            }

            if (TaskManager != null)
            {
                TaskManager.OnCoreShutdownRequested();
            }

            if (Update != null)
            {
                Update.StopUpdateTimer();
                Logger.Log("Update timer disposed!", Enums.LogLevels.Trace);
            }

            if (RefreshConsoleTitleTimer != null)
            {
                RefreshConsoleTitleTimer.Dispose();
                Logger.Log("Console title refresh timer disposed!", Enums.LogLevels.Trace);
            }

            if (ConfigWatcher != null && ConfigWatcher.ConfigWatcherOnline)
            {
                ConfigWatcher.StopConfigWatcher();
            }

            if (ModuleWatcher != null && ModuleWatcher.ModuleWatcherOnline)
            {
                ModuleWatcher.StopModuleWatcher();
            }

            if (AssistantStatus != null)
            {
                AssistantStatus.Dispose();
            }

            if (KestrelServer.IsServerOnline)
            {
                await KestrelServer.Stop().ConfigureAwait(false);
            }

            if (ModuleLoader != null)
            {
                ModuleLoader.OnCoreShutdown();
            }

            if (Controller != null)
            {
                Controller.InitGpioShutdownTasks();
            }

            if (Config != null)
            {
                Config.ProgramLastShutdown = DateTime.Now;
                Config.SaveConfig(Config);
            }

            Logger.Log("Finished on exit tasks.", Enums.LogLevels.Trace);
        }
コード例 #2
0
ファイル: Core.cs プロジェクト: Prodject/HomeAssistant
        /// <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);
        }