static void Main(string[] args) { string dev = "/dev/serial/by-id/usb-Unknown_radino_CC1101-if00"; if (args.Length > 0 && args[0].StartsWith("/")) { dev = args[0]; } SomfyRtsController controller = SomfyRtsController.CreateFromFile(); controller.SignalDuinoAddress = dev; do { var key = Console.ReadKey(); switch (key.Key) { case ConsoleKey.A: controller.AddDevice(cLight, 180004); break; case ConsoleKey.P: controller.SendCommand(cLight, SomfyRtsButton.Prog); break; case ConsoleKey.U: controller.SendCommand(cLight, SomfyRtsButton.Up); break; case ConsoleKey.D: controller.SendCommand(cLight, SomfyRtsButton.Down); break; case ConsoleKey.F: controller.SendCommand(cLight, SomfyRtsButton.My); break; case ConsoleKey.C: controller.Close(); break; case ConsoleKey.O: controller.Open(); break; } controller.Save(); } while (true); }
public static async Task HandleRequests() { bool runServer = true; sController = SomfyRtsController.CreateFromFile(); string result = "OK"; while (runServer) { // Will wait here until we hear from a connection HttpListenerContext ctx = await sHttp.GetContextAsync(); // Peel out the requests and response objects HttpListenerRequest req = ctx.Request; HttpListenerResponse resp = ctx.Response; // Print out some info about the request Console.WriteLine(req.Url.ToString()); Console.WriteLine(req.HttpMethod); Console.WriteLine(req.UserHostName); Console.WriteLine(req.UserAgent); Console.WriteLine(); //Handle URLs if (req.HttpMethod == "GET") { if (req.Url.AbsolutePath == "/shutdown") { Console.WriteLine("Shutdown requested"); runServer = false; } if (req.Url.AbsolutePath.StartsWith(cControllerName)) { var path = req.Url.AbsolutePath.Replace(cControllerName, ""); var token = path.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries); if (token.Length > 0 && token[0] == "list") { StringBuilder sb = new StringBuilder(); foreach (var dev in sController.Devices) { sb.AppendLine($"Device: {dev.Name} RC: {dev.RollingCode} EK: {dev.EncryptionKey} Address: {dev.Address}"); } result = sb.ToString(); } else if (token.Length > 0 && token[0] == "help") { StringBuilder sb = new StringBuilder(); foreach (var cmd in sButtonMapping) { sb.AppendLine($"Cmd: '{cmd.Key}' => {cmd.Value}"); } result = sb.ToString(); } else if (token.Length > 0 && token[0] == "version") { result = $"{Assembly.GetExecutingAssembly().GetName().Name} {Assembly.GetExecutingAssembly().GetName().Version}"; } else if (token.Length > 0) { string dev = token[0]; //asume device if (sController.DeviceAvailable(dev)) { if (token.Length > 1) { var cmd = token[1]; SomfyRtsButton?button = null; if (sButtonMapping.ContainsKey(cmd)) { button = sButtonMapping[cmd]; if (cmd == "dimm") { token = new string[] { token[0], token[1], "20" }; } } if (null != button) { if (token.Length > 2 && Int32.TryParse(token[2], out int rep)) { sController.SendCommand(dev, button.Value, rep); } else { sController.SendCommand(dev, button.Value); } sController.Save(); result = "OK"; } else { result = $"Command '{cmd}' not found! Call help to list available cmds."; } } else { result = $"No command specified!"; } } else { result = $"Device '{token[0]}' not found."; } } } } // Write the response info byte[] data = Encoding.UTF8.GetBytes(result); resp.ContentType = "text/plain"; resp.ContentEncoding = Encoding.UTF8; resp.ContentLength64 = data.LongLength; // Write out to the response stream (asynchronously), then close it await resp.OutputStream.WriteAsync(data, 0, data.Length); resp.Close(); } }