private void PreInit() { PinController = new PinController(this); EventManager = new EventManager(this); MorseTranslator = new MorseRelayTranslator(this); BluetoothController = new BluetoothController(this); SoundController = new SoundController(this); ConfigManager = new PinConfigManager(this); }
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(); } }