Esempio n. 1
0
        /// <summary>
        /// Main entry point
        /// </summary>
        /// <param name="args">The arguments.</param>
        private static void Main(string[] args)
        {
            using (var relayBoard = new RelayController(s => s.Trace(), (e) => e.Error()))
            {
                var          isOpen     = false;
                var          password   = RelayController.DefaultPassword;
                const string serialPort = "COM6";

                while (isOpen == false)
                {
                    try
                    {
                        $"Opening port {serialPort}".Info();

                        relayBoard.Open(serialPort, password);
                        isOpen = true;
                    }
                    catch (Exception ex)
                    {
                        ex.Log(nameof(Main));
                        password = PromptForPassword();
                    }
                }

                while (true)
                {
                    var selectedOption = Terminal.ReadPrompt("Select an option", ActionOptions, "Exit this program");
                    if (selectedOption.Key == ConsoleKey.Q)
                    {
                        $"Board: Model: {relayBoard.BoardModel}, Version: {relayBoard.BoardVersion}, FW: {relayBoard.FirmwareVersion}, Channels: {relayBoard.RelayChannelCount}, Mode: {relayBoard.RelayOperatingMode}".Info();
                    }
                    else if (selectedOption.Key == ConsoleKey.W)
                    {
                        DumpRelaysState(relayBoard);
                    }
                    else if (selectedOption.Key == ConsoleKey.E)
                    {
                        var newOpModeKey = Terminal.ReadKey("Enter Operating Mode (L for Latching, anything else, Momentary): ", false);
                        relayBoard.RelayOperatingMode = newOpModeKey.Key == ConsoleKey.L ? RelayOperatingMode.Latching : RelayOperatingMode.Momentary;

                        $"Relay Operating Mode is now: {relayBoard.RelayOperatingMode}".Info();
                    }
                    else if (selectedOption.Key == ConsoleKey.R)
                    {
                        $"Relay Board Working Voltage: {relayBoard.WorkingVoltage}".Info();
                        $"Relay Board Temperature    : {relayBoard.Temperature}".Info();
                    }
                    else if (selectedOption.Key == ConsoleKey.T)
                    {
                        relayBoard.ChangePassword(PromptForPassword());
                    }
                    else if (selectedOption.Key == ConsoleKey.A)
                    {
                        relayBoard.SetRelayStateAll(true);
                    }
                    else if (selectedOption.Key == ConsoleKey.S)
                    {
                        relayBoard.SetRelayStateAll(false);
                    }
                    else if (selectedOption.Key == ConsoleKey.D)
                    {
                        var relayNumber = (RelayNumber)Terminal.ReadNumber("Enter a relay number from 1 to 8", 1);
                        if ((int)relayNumber >= 1 && (int)relayNumber <= 8)
                        {
                            relayBoard[relayNumber] = true;
                        }
                        else
                        {
                            "Bad relay number. Argument must be between 1 and 8".Error();
                        }
                    }
                    else if (selectedOption.Key == ConsoleKey.F)
                    {
                        var relayNumber = (RelayNumber)Terminal.ReadNumber("Enter a relay number from 1 to 8", 1);
                        if ((int)relayNumber >= 1 && (int)relayNumber <= 8)
                        {
                            relayBoard[relayNumber] = false;
                        }
                        else
                        {
                            "Bad relay number. Argument must be between 1 and 8".Error();
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }

            Terminal.ReadKey("Press any key to continue . . .");
        }