public void AfterStartingService(HostStartedContext context) { //CLI for when running the service as a normal console program, runned in its own thread //because Topshelf reserves everything for itself on the main thread. new Thread(() => { if (Environment.UserInteractive) { while (true) { string input = Console.ReadLine(); if (string.IsNullOrEmpty(input)) { continue; } var cmdIsInt = int.TryParse(input, out var cmd); if (cmdIsInt) { client.Command(cmd); } else if (input.StartsWith("0x")) { client.CommandAsHex(input); } else { client.Command(input); } } } }).Start(); }
public static void Command(this LibCECClient client, int cmd) { if (Enum.IsDefined(typeof(CecOpcode), cmd)) { client.Command((CecOpcode)cmd); } }
public static void CommandAsHex(this LibCECClient client, string cmd) { bool succeded = true; if (cmd.StartsWith("0x")) { cmd = cmd.Remove(0, 2); var hexValues = cmd.Split(':'); if (hexValues.Length == 0) { succeded = false; } byte[] hexParsed = new byte[hexValues.Length]; for (var i = 0; i < hexValues.Length; i++) { var hexVal = hexValues[i]; bool parsed = Byte.TryParse(hexVal, NumberStyles.HexNumber, null, out hexParsed[i]); succeded = parsed; } client.Command(hexParsed); } if (!succeded) { Console.WriteLine($"Tried to parse CMD \"{cmd}\" as hex values, but failed."); } }
public static void CommandAsServiceCommand(this LibCECClient client, int cmd) { var cmdNames = Enum.GetNames(typeof(CecOpcode)); var index = cmd - 128; if (index < 0 || index > cmdNames.Length) { Console.WriteLine($"Received Command: {cmd} " + "Tried to interpret as service command, but couldn\'t. " + "Value has to be between 128 and 255!"); return; } client.Command(cmdNames[index]); }
/// <summary> /// Send command via a string representation of a <see cref="CecOpcode"/>. /// <para /> /// Note: Command is Case Insensitive /// <para /> /// <example> /// Ex: /// "Standby", "TextViewOn", "standby", "textviewon" /// </example> /// </summary> /// <param name="client"></param> /// <param name="cmd"></param> public static void Command(this LibCECClient client, string cmd) { cmd = cmd.ToLower(); if (cmd == "on") { client.Lib.PowerOnDevices(CecLogicalAddress.Tv); return; } string[] strings = Enum.GetNames(typeof(CecOpcode)); if (!strings.Contains(cmd, StringComparer.CurrentCultureIgnoreCase)) { Console.WriteLine($"{cmd} is not a CEC command!"); return; } CecOpcode cecCmd = (CecOpcode)Enum.Parse(typeof(CecOpcode), cmd, true); client.Command(cecCmd); }