private void PrintAll() { if (Interpreter.CommandsCount <= 0) { ShellIO.Error("No commands exist."); return; } ShellIO.Info("--------------------------------------- Shell Commands ---------------------------------------"); foreach (KeyValuePair <string, IShellCommand> cmd in Interpreter.Commands) { if (string.IsNullOrEmpty(cmd.Key) || cmd.Value == null) { continue; } cmd.Value.OnHelpExec(false); } ShellIO.Info("----------------------------------------------------------------------------------------------"); }
public async Task ExecuteAsync(Parameter parameter) { if (!IsInitSuccess) { return; } if (!Program.CoreInstance.IsBaseInitiationCompleted) { ShellIO.Error("Cannot execute as the core hasn't been successfully started yet."); return; } if (parameter.Parameters.Length > MaxParameterCount) { ShellIO.Error("Too many arguments."); return; } await Sync.WaitAsync().ConfigureAwait(false); try { switch (parameter.ParameterCount) { case 0: default: ShellIO.Info("After this process, you wont be able to execute shell commands in assistant..."); Interpreter.ExitShell(); ShellIO.Info("Shutdown process started!"); return; } } catch (Exception e) { ShellIO.Exception(e); return; } finally { Sync.Release(); } }
public async Task ExecuteAsync(Parameter parameter) { if (!IsInitSuccess) { return; } if (parameter.Parameters.Length > MaxParameterCount) { ShellIO.Error("Too many arguments."); return; } await Sync.WaitAsync().ConfigureAwait(false); try { if (OnExecuteFunc != null) { if (OnExecuteFunc.Invoke(parameter)) { return; } } string?apiKey; if (string.IsNullOrEmpty(Program.CoreInstance.GetCoreConfig().ApiKeys.OpenWeatherApiKey)) { ShellIO.Error("Weather API key isn't set."); apiKey = ShellIO.GetString("Open Weather Api Key"); if (string.IsNullOrEmpty(apiKey)) { ShellIO.Error("Api key is invalid or not set properly."); return; } } apiKey = Program.CoreInstance.GetCoreConfig().ApiKeys.OpenWeatherApiKey; int pinCode; OpenWeatherApiSharp.WeatherResponse?weather; switch (parameter.ParameterCount) { case 0: ShellIO.Error("Pin code is invalid or not set."); return; case 1 when !string.IsNullOrEmpty(parameter.Parameters[0]): if (!int.TryParse(parameter.Parameters[0], out pinCode)) { ShellIO.Error("Failed to parse pin code. Entered pin code is invalid."); return; } using (OpenWeatherMapClient client = new OpenWeatherMapClient(apiKey)) { weather = await client.GetWeatherAsync(pinCode, "in").ConfigureAwait(false); } if (weather == null || weather.Location == null || weather.Wind == null || weather.Data == null) { ShellIO.Error("Weather request failed."); return; } ShellIO.Info($"---------- Weather Data | {weather.LocationName} | {weather.Location.Latitude}:{weather.Location.Longitude} ----------"); ShellIO.Info($"Wind Speed: {weather.Wind.Speed}"); ShellIO.Info($"Humidity: {weather.Data.Humidity}"); ShellIO.Info($"Pressure: {weather.Data.Pressure}"); ShellIO.Info($"Sea Level: {weather.Data.SeaLevel}"); ShellIO.Info($"Temperature: {KelvinToCelsius(weather.Data.Temperature)} C"); return; case 2 when !string.IsNullOrEmpty(parameter.Parameters[0]) && !string.IsNullOrEmpty(parameter.Parameters[1]): if (!int.TryParse(parameter.Parameters[0], out pinCode)) { ShellIO.Error("Pin code is invalid."); return; } if (parameter.Parameters[1].Length > 3) { ShellIO.Error("Country code is invalid."); return; } using (OpenWeatherMapClient client = new OpenWeatherMapClient(apiKey)) { weather = await client.GetWeatherAsync(pinCode, "in").ConfigureAwait(false); } if (weather == null || weather.Location == null || weather.Wind == null || weather.Data == null) { ShellIO.Error("Weather request failed."); return; } ShellIO.Info($"---------- Weather Data | {weather.LocationName} | {weather.Location.Latitude}:{weather.Location.Longitude} ----------"); ShellIO.Info($"Wind Speed: {weather.Wind.Speed}"); ShellIO.Info($"Humidity: {weather.Data.Humidity}"); ShellIO.Info($"Pressure: {weather.Data.Pressure}"); ShellIO.Info($"Sea Level: {weather.Data.SeaLevel}"); ShellIO.Info($"Temperature: {KelvinToCelsius(weather.Data.Temperature)} C"); return; case 3 when !string.IsNullOrEmpty(parameter.Parameters[0]) && !string.IsNullOrEmpty(parameter.Parameters[1]) && !string.IsNullOrEmpty(parameter.Parameters[2]): if (!int.TryParse(parameter.Parameters[0], out pinCode)) { ShellIO.Error("Pin code is invalid."); return; } if (parameter.Parameters[1].Length > 3) { ShellIO.Error("Country code is invalid."); return; } bool tts = parameter.Parameters[2].AsBool(); using (OpenWeatherMapClient client = new OpenWeatherMapClient(apiKey)) { weather = await client.GetWeatherAsync(pinCode, "in").ConfigureAwait(false); } if (weather == null || weather.Location == null || weather.Wind == null || weather.Data == null) { ShellIO.Error("Weather request failed."); return; } if (tts) { Helpers.InBackground(() => { using (TTS tts = new TTS(false, false)) { tts.Speak($"Weather Data for {weather.LocationName}. " + $"Wind Speed is {weather.Wind.Speed}. " + $"Humidity level is {weather.Data.Humidity}. Pressure level {weather.Data.Pressure}. " + $"Sea Level is {weather.Data.SeaLevel}. Temperature is {weather.Data.Temperature}." ); } }); } ShellIO.Info($"---------- Weather Data | {weather.LocationName} | {weather.Location.Latitude}:{weather.Location.Longitude} ----------"); ShellIO.Info($"Wind Speed: {weather.Wind.Speed}"); ShellIO.Info($"Humidity: {weather.Data.Humidity}"); ShellIO.Info($"Pressure: {weather.Data.Pressure}"); ShellIO.Info($"Sea Level: {weather.Data.SeaLevel}"); ShellIO.Info($"Temperature: {KelvinToCelsius(weather.Data.Temperature)} C"); return; default: ShellIO.Error("Command seems to be in incorrect syntax."); return; } } catch (Exception e) { ShellIO.Exception(e); return; } finally { Sync.Release(); } }
public async Task ExecuteAsync(Parameter parameter) { if (!IsInitSuccess) { return; } if (parameter.Parameters.Length > MaxParameterCount) { ShellIO.Error("Too many arguments."); return; } await Sync.WaitAsync().ConfigureAwait(false); try { if (OnExecuteFunc != null) { if (OnExecuteFunc.Invoke(parameter)) { return; } } int exitCode = 0; switch (parameter.ParameterCount) { case 0: ShellIO.Info("Exiting assistant in 5 seconds..."); Helpers.ScheduleTask(() => Program.CoreInstance.ExitEnvironment(0), TimeSpan.FromSeconds(5)); return; case 1 when !string.IsNullOrEmpty(parameter.Parameters[0]): if (!int.TryParse(parameter.Parameters[0], out exitCode)) { ShellIO.Error("Couldn't parse exit code argument."); return; } ShellIO.Info($"Exiting assistant with '{exitCode}' exit code in 5 seconds..."); Helpers.ScheduleTask(() => Program.CoreInstance.ExitEnvironment(exitCode), TimeSpan.FromSeconds(5)); return; case 2 when !string.IsNullOrEmpty(parameter.Parameters[0]) && !string.IsNullOrEmpty(parameter.Parameters[1]): if (!int.TryParse(parameter.Parameters[0], out exitCode)) { ShellIO.Error("Couldn't parse exit code argument."); return; } if (!int.TryParse(parameter.Parameters[1], out int delay)) { ShellIO.Error("Couldn't parse delay argument."); return; } ShellIO.Info($"Exiting assistant with '{exitCode}' exit code in '{delay}' seconds..."); Helpers.ScheduleTask(() => Program.CoreInstance.ExitEnvironment(exitCode), TimeSpan.FromSeconds(delay)); return; default: ShellIO.Error("Command seems to be in incorrect syntax."); return; } } catch (Exception e) { ShellIO.Exception(e); return; } finally { Sync.Release(); } }
public async Task ExecuteAsync(Parameter parameter) { if (!IsInitSuccess) { return; } if (parameter.Parameters.Length > MaxParameterCount) { ShellIO.Error("Too many arguments."); return; } await Sync.WaitAsync().ConfigureAwait(false); try { if (OnExecuteFunc != null) { if (OnExecuteFunc.Invoke(parameter)) { return; } } switch (parameter.ParameterCount) { case 0: foreach (KeyValuePair <string, IShellCommand> cmd in Interpreter.Commands) { if (string.IsNullOrEmpty(cmd.Value.CommandKey) || string.IsNullOrEmpty(cmd.Value.CommandName)) { continue; } cmd.Value.OnHelpExec(true); } return; case 1 when !string.IsNullOrEmpty(parameter.Parameters[0]) && parameter.Parameters[0].Equals("all", StringComparison.OrdinalIgnoreCase): PrintAll(); return; case 1 when !string.IsNullOrEmpty(parameter.Parameters[0]): IShellCommand shellCmd = await Interpreter.Init.GetCommandWithKeyAsync <IShellCommand>(parameter.Parameters[0]).ConfigureAwait(false); if (shellCmd == null) { ShellIO.Error("Command doesn't exist. use ' help -all ' to check all available commands!"); return; } shellCmd.OnHelpExec(false); return; default: ShellIO.Error("Command seems to be in incorrect syntax."); return; } } catch (Exception e) { ShellIO.Exception(e); return; } finally { Sync.Release(); } }
public async Task ExecuteAsync(Parameter parameter) { if (!IsInitSuccess) { return; } if (parameter.Parameters.Length > MaxParameterCount) { ShellIO.Error("Too many arguments."); return; } await Sync.WaitAsync().ConfigureAwait(false); MorseCore morseCore = MorseRelayTranslator.GetCore(); string morse = string.Empty; try { if (OnExecuteFunc != null) { if (OnExecuteFunc.Invoke(parameter)) { return; } } switch (parameter.ParameterCount) { case 1 when !string.IsNullOrEmpty(parameter.Parameters[0]): morse = morseCore.ConvertToMorseCode(parameter.Parameters[0]); if (string.IsNullOrEmpty(morse) || !morseCore.IsValidMorse(morse)) { ShellIO.Error("Failed to verify generated morse code."); return; } ShellIO.Info(">>> " + morse); return; case 2 when !string.IsNullOrEmpty(parameter.Parameters[0]) && !string.IsNullOrEmpty(parameter.Parameters[1]): morse = morseCore.ConvertToMorseCode(parameter.Parameters[0]); if (string.IsNullOrEmpty(morse) || !morseCore.IsValidMorse(morse)) { ShellIO.Error("Failed to verify generated morse code."); return; } ShellIO.Info(">>> " + morse); int relayNumber; try { if (!int.TryParse(parameter.Parameters[1], out relayNumber)) { ShellIO.Error("Relay number argument is invalid."); return; } } catch (IndexOutOfRangeException) { ShellIO.Error("The specified relay number is invalid is greater than all the available relay numbers."); return; } if (!PinController.IsValidPin(Program.CoreInstance.GetGpioCore().GetAvailablePins().OutputPins[relayNumber])) { ShellIO.Error("The specified pin is invalid."); return; } await Program.CoreInstance.GetGpioCore().GetMorseTranslator().RelayMorseCycle(morse, Program.CoreInstance.GetGpioCore().GetAvailablePins().OutputPins[relayNumber]).ConfigureAwait(false); ShellIO.Info("Completed!"); return; default: ShellIO.Error("Command seems to be in incorrect syntax."); return; } } catch (Exception e) { ShellIO.Exception(e); return; } finally { Sync.Release(); } }
public async Task ExecuteAsync(Parameter parameter) { if (!IsInitSuccess) { return; } if (!Program.CoreInstance.IsBaseInitiationCompleted) { ShellIO.Error("Cannot execute as the core hasn't been successfully started yet."); return; } if (parameter.Parameters.Length > MaxParameterCount) { ShellIO.Error("Too many arguments."); return; } if (!GpioCore.IsAllowedToExecute) { ShellIO.Error("Gpio functions are not allowed to execute."); ShellIO.Info("Gpio pin controlling functions are only available on raspberry pi with an OS such as Raspbian."); return; } await Sync.WaitAsync().ConfigureAwait(false); try { if (OnExecuteFunc != null) { if (OnExecuteFunc.Invoke(parameter)) { return; } } if (parameter.Parameters == null || parameter.Parameters.Length == 0) { ShellIO.Error("Gpio pin, Pin state, pin mode values are not specified."); return; } if (string.IsNullOrEmpty(parameter.Parameters[0])) { ShellIO.Error("Gpio pin is invalid or not specified."); return; } int pin; GpioPinMode pinMode; GpioPinState pinState; bool isSet; GpioControllerDriver?driver = PinController.GetDriver(); if (driver == null || !driver.IsDriverInitialized) { ShellIO.Error("Internal error occurred with the gpio driver. Please restart the assistant."); return; } switch (parameter.ParameterCount) { case 1 when !string.IsNullOrEmpty(parameter.Parameters[0]): ShellIO.Info("Note: as only 1 argument is specified, the default value will be set for the specified pin."); if (!int.TryParse(parameter.Parameters[0], out pin)) { ShellIO.Error("Failed to parse gpio pin value."); return; } ShellIO.Info($"{pin} will be set to Output mode and configured in On state."); if (!Constants.BcmGpioPins.Contains(pin) || !PinController.IsValidPin(pin) || !Program.CoreInstance.GetCoreConfig().GpioConfiguration.OutputModePins.Contains(pin)) { ShellIO.Error("Specified gpio pin is an invalid."); return; } isSet = driver.TogglePinState(pin); if (!isSet) { ShellIO.Error($"Failed to configure {pin} gpio pin. Please validate the pin argument."); return; } ShellIO.Info($"Successfully configured {pin} gpio pin."); return; case 2 when !string.IsNullOrEmpty(parameter.Parameters[0]) && !string.IsNullOrEmpty(parameter.Parameters[1]) && parameter.Parameters[0].Equals("relay", StringComparison.OrdinalIgnoreCase): if (!int.TryParse(parameter.Parameters[1], out int relayNum)) { ShellIO.Error("Failed to parse relay number value."); return; } if (!PinController.IsValidPin(Program.CoreInstance.GetGpioCore().GetAvailablePins().OutputPins[relayNum])) { ShellIO.Error($"The pin ' {Program.CoreInstance.GetGpioCore().GetAvailablePins().OutputPins[relayNum]} ' is invalid."); return; } isSet = driver.TogglePinState(Program.CoreInstance.GetGpioCore().GetAvailablePins().OutputPins[relayNum]); if (!isSet) { ShellIO.Error($"Failed to configure {Program.CoreInstance.GetGpioCore().GetAvailablePins().OutputPins[relayNum]} gpio pin. Please validate the pin argument."); return; } ShellIO.Info($"Successfully configured {Program.CoreInstance.GetGpioCore().GetAvailablePins().OutputPins[relayNum]} gpio pin."); return; case 2 when !string.IsNullOrEmpty(parameter.Parameters[0]) && !string.IsNullOrEmpty(parameter.Parameters[1]): if (!int.TryParse(parameter.Parameters[0], out pin)) { ShellIO.Error("Failed to parse gpio pin value."); return; } if (!int.TryParse(parameter.Parameters[1], out int modeVal)) { ShellIO.Error("Failed to parse gpio pin mode value."); return; } pinMode = (GpioPinMode)modeVal; ShellIO.Info($"{pin} will be set to {pinMode.ToString()} mode and configured in On state."); if (!Constants.BcmGpioPins.Contains(pin) || !PinController.IsValidPin(pin) || !Program.CoreInstance.GetCoreConfig().GpioConfiguration.OutputModePins.Contains(pin)) { ShellIO.Error("Specified gpio pin is an invalid."); return; } isSet = driver.SetGpioValue(pin, pinMode, GpioPinState.On); if (!isSet) { ShellIO.Error($"Failed to configure {pin} gpio pin. Please validate the pin argument."); return; } ShellIO.Info($"Successfully configured {pin} gpio pin."); return; case 3 when !string.IsNullOrEmpty(parameter.Parameters[0]) && !string.IsNullOrEmpty(parameter.Parameters[1]) && !string.IsNullOrEmpty(parameter.Parameters[2]): if (!int.TryParse(parameter.Parameters[0], out pin)) { ShellIO.Error("Failed to parse gpio pin value."); return; } if (!int.TryParse(parameter.Parameters[1], out int pinModeVal)) { ShellIO.Error("Failed to parse gpio pin mode value."); return; } if (!int.TryParse(parameter.Parameters[2], out int stateVal)) { ShellIO.Error("Failed to parse gpio pin state value."); return; } pinMode = (GpioPinMode)pinModeVal; pinState = (GpioPinState)stateVal; ShellIO.Info($"{pin} will be set to {pinMode.ToString()} mode and configured in {pinState} state."); if (!Constants.BcmGpioPins.Contains(pin) || !PinController.IsValidPin(pin) || !Program.CoreInstance.GetCoreConfig().GpioConfiguration.OutputModePins.Contains(pin)) { ShellIO.Error("Specified gpio pin is an invalid."); return; } isSet = driver.SetGpioValue(pin, pinMode, pinState); if (!isSet) { ShellIO.Error($"Failed to configure {pin} gpio pin. Please validate the pin argument."); return; } ShellIO.Info($"Successfully configured {pin} gpio pin."); return; case 4 when !string.IsNullOrEmpty(parameter.Parameters[0]) && !string.IsNullOrEmpty(parameter.Parameters[1]) && !string.IsNullOrEmpty(parameter.Parameters[2]) && !string.IsNullOrEmpty(parameter.Parameters[3]): if (!int.TryParse(parameter.Parameters[0], out pin)) { ShellIO.Error("Failed to parse gpio pin value."); return; } if (!int.TryParse(parameter.Parameters[1], out int modeValue)) { ShellIO.Error("Failed to parse gpio pin mode value."); return; } if (!int.TryParse(parameter.Parameters[2], out int stateValue)) { ShellIO.Error("Failed to parse gpio pin state value."); return; } if (!int.TryParse(parameter.Parameters[3], out int delayValue)) { ShellIO.Error("Failed to parse gpio pin state value."); return; } pinMode = (GpioPinMode)modeValue; pinState = (GpioPinState)stateValue; ShellIO.Info($"{pin} will be set to {pinMode.ToString()} mode and configured in {pinState} state and set back by a delay of {delayValue} minutes."); if (!Constants.BcmGpioPins.Contains(pin) || !PinController.IsValidPin(pin) || !Program.CoreInstance.GetCoreConfig().GpioConfiguration.OutputModePins.Contains(pin)) { ShellIO.Error("Specified gpio pin is an invalid."); return; } isSet = driver.SetGpioValue(pin, pinMode, pinState, TimeSpan.FromMinutes(delayValue), false); if (!isSet) { ShellIO.Error($"Failed to configure {pin} gpio pin. Please validate the pin argument."); return; } ShellIO.Info($"Successfully configured {pin} gpio pin."); return; default: ShellIO.Error("Command seems to be in incorrect syntax."); return; } } catch (Exception e) { ShellIO.Exception(e); return; } finally { Sync.Release(); } }