Exemplo n.º 1
0
        private static async Task DisplayConsoleCommandMenu()
        {
            Logger.Log("Displaying console command window", Enums.LogLevels.Trace);
            Logger.Log($"------------------------- COMMAND WINDOW -------------------------", Enums.LogLevels.UserInput);
            Logger.Log($"{Constants.ConsoleQuickShutdownKey} - Quick shutdown the assistant.", Enums.LogLevels.UserInput);
            Logger.Log($"{Constants.ConsoleDelayedShutdownKey} - Shutdown assistant in 5 seconds.", Enums.LogLevels.UserInput);

            if (!DisablePiMethods)
            {
                Logger.Log($"{Constants.ConsoleRelayCommandMenuKey} - Display relay pin control menu.", Enums.LogLevels.UserInput);
                Logger.Log($"{Constants.ConsoleRelayCycleMenuKey} - Display relay cycle control menu.", Enums.LogLevels.UserInput);
            }

            Logger.Log($"{Constants.ConsoleTestMethodExecutionKey} - Run pre-configured test methods or tasks.", Enums.LogLevels.UserInput);

            if (Config.EnableModules)
            {
                Logger.Log($"{Constants.ConsoleModuleShutdownKey} - Invoke shutdown method on all currently running modules.", Enums.LogLevels.UserInput);
            }

            if (MorseCode != null)
            {
                Logger.Log($"{Constants.ConsoleMorseCodeKey} - Morse code generator", Enums.LogLevels.UserInput);
            }

            if (WeatherApi != null)
            {
                Logger.Log($"{Constants.ConsoleWheatherInfoKey} - Get weather info", Enums.LogLevels.UserInput);
            }

            Logger.Log($"-------------------------------------------------------------------", Enums.LogLevels.UserInput);
            Logger.Log("Awaiting user input: \n", Enums.LogLevels.UserInput);

            int failedTriesCount = 0;
            int maxTries         = 3;

            while (true)
            {
                if (failedTriesCount > maxTries)
                {
                    Logger.Log($"Multiple wrong inputs. please start the command menu again  by pressing {Constants.ConsoleCommandMenuKey} key.", Enums.LogLevels.Warn);
                    return;
                }

                char pressedKey = Console.ReadKey().KeyChar;

                switch (pressedKey)
                {
                case Constants.ConsoleQuickShutdownKey: {
                    Logger.Log("Force quitting assistant...", Enums.LogLevels.Warn);
                    Exit(true);
                }
                    return;

                case Constants.ConsoleDelayedShutdownKey: {
                    Logger.Log("Gracefully shutting down assistant...", Enums.LogLevels.Warn);
                    GracefullModuleShutdown = true;
                    await Task.Delay(5000).ConfigureAwait(false);
                    await Exit(0).ConfigureAwait(false);
                }
                    return;

                case Constants.ConsoleRelayCommandMenuKey when !DisablePiMethods: {
                    Logger.Log("Displaying relay command menu...", Enums.LogLevels.Warn);
                    DisplayRelayCommandMenu();
                }
                    return;

                case Constants.ConsoleRelayCycleMenuKey when !DisablePiMethods: {
                    Logger.Log("Displaying relay cycle menu...", Enums.LogLevels.Warn);
                    await DisplayRelayCycleMenu().ConfigureAwait(false);
                }
                    return;

                case Constants.ConsoleRelayCommandMenuKey when DisablePiMethods: {
                    Logger.Log("Assistant is running in an Operating system/Device which doesnt support GPIO pin controlling functionality.", Enums.LogLevels.Warn);
                }
                    return;

                case Constants.ConsoleRelayCycleMenuKey when DisablePiMethods: {
                    Logger.Log("Assistant is running in an Operating system/Device which doesnt support GPIO pin controlling functionality.", Enums.LogLevels.Warn);
                }
                    return;

                case Constants.ConsoleMorseCodeKey: {
                    Logger.Log("Enter text to convert to morse: ");
                    string morseCycle = Console.ReadLine();
                    if (Controller.MorseTranslator.IsTranslatorOnline)
                    {
                        await Controller.MorseTranslator.RelayMorseCycle(morseCycle, Config.RelayPins[0]).ConfigureAwait(false);
                    }
                    else
                    {
                        Logger.Log("Could not convert due to an unknown error.", Enums.LogLevels.Warn);
                    }
                }
                    return;

                case Constants.ConsoleWheatherInfoKey: {
                    Logger.Log("Please enter the pin code of the location: ");
                    int counter = 0;

                    int pinCode;
                    while (true)
                    {
                        if (counter > 4)
                        {
                            Logger.Log("Failed multiple times. aborting...");
                            return;
                        }

                        try {
                            pinCode = Convert.ToInt32(Console.ReadLine());
                            break;
                        }
                        catch {
                            counter++;
                            Logger.Log("Please try again!", Enums.LogLevels.Warn);
                            continue;
                        }
                    }

                    if (WeatherApi != null)
                    {
                        (bool status, WeatherData response) = WeatherApi.GetWeatherInfo(Config.OpenWeatherApiKey, pinCode, "in");
                        if (status)
                        {
                            Logger.Log($"------------ Weather information for {pinCode}/{response.LocationName} ------------", Enums.LogLevels.Success);
                            Logger.Log($"Temperature: {response.Temperature}", Enums.LogLevels.Success);
                            Logger.Log($"Humidity: {response.Temperature}", Enums.LogLevels.Success);
                            Logger.Log($"Latitude: {response.Latitude}", Enums.LogLevels.Success);
                            Logger.Log($"Longitude: {response.Logitude}", Enums.LogLevels.Success);
                            Logger.Log($"Location name: {response.LocationName}", Enums.LogLevels.Success);
                            Logger.Log($"Preasure: {response.Pressure}", Enums.LogLevels.Success);
                            Logger.Log($"Wind speed: {response.WindDegree}", Enums.LogLevels.Success);
                        }
                        else
                        {
                            Logger.Log("Failed to fetch wheather information, try again later!");
                        }
                    }
                }
                    return;

                case Constants.ConsoleTestMethodExecutionKey: {
                    Logger.Log("Executing test methods/tasks", Enums.LogLevels.Warn);
                    Logger.Log("Test method execution finished successfully!", Enums.LogLevels.Success);
                }
                    return;

                case Constants.ConsoleModuleShutdownKey when !ModuleLoader.ModulesCollection.IsModulesEmpty && Config.EnableModules: {
                    Logger.Log("Shutting down all modules...", Enums.LogLevels.Warn);
                    ModuleLoader.OnCoreShutdown();
                }
                    return;

                case Constants.ConsoleModuleShutdownKey when ModuleLoader.ModulesCollection.IsModulesEmpty: {
                    Logger.Log("There are no modules to shutdown...");
                }
                    return;

                default: {
                    if (failedTriesCount > maxTries)
                    {
                        Logger.Log($"Unknown key was pressed. ({maxTries - failedTriesCount} tries left)", Enums.LogLevels.Warn);
                    }

                    failedTriesCount++;
                    continue;
                }
                }
            }
        }