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 CommandAsHex(this LibCECClient client, int cmd) { string hex = cmd.ToString("X"); int insertIndex = 1; for (var i = 0; i < hex.Length - insertIndex; i++) { if (i % 2 == 1) { hex = hex.Insert(i + insertIndex, ":"); insertIndex++; } } client.CommandAsHex($"0x{hex}"); }