public async Task <IActionResult> DoCode() { string code; using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8)) { code = await reader.ReadToEndAsync(); } try { using CommandConnection connection = await BuildConnection(); _logger.LogInformation($"[{nameof(DoCode)}] Executing code '{code}'"); string result = await connection.PerformSimpleCode(code, CodeChannel.HTTP); return(Content(result)); } catch (AggregateException ae) when(ae.InnerException is IncompatibleVersionException) { _logger.LogError($"[{nameof(DoCode)}] Incompatible DCS version"); return(StatusCode(502, ae.InnerException.Message)); } catch (AggregateException ae) when(ae.InnerException is SocketException) { _logger.LogError($"[{nameof(DoCode)}] DCS is unavailable"); return(StatusCode(503, ae.InnerException.Message)); } catch (Exception e) { _logger.LogWarning(e, $"[{nameof(DoCode)}] Failed to execute code {code}"); return(StatusCode(500, e.Message)); } }
static void Main(string[] args) { // Connect to DCS CommandConnection connection = new CommandConnection(); if (args.Length == 0) { connection.Connect().Wait(); } else { connection.Connect(args[0]).Wait(); } Console.WriteLine("Connected!"); // Start reading lines from stdin and send them to DCS as simple codes. // When the code has finished, the result is printed to stdout string input = Console.ReadLine(); while (connection.IsConnected && input != null && input != "exit" && input != "quit") { try { string output = connection.PerformSimpleCode(input).Result; Console.Write(output); } catch (AggregateException ae) { Console.WriteLine(ae.InnerException.Message); } input = Console.ReadLine(); } }
public async Task <IActionResult> DoCode() { string code; using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8)) { code = await reader.ReadToEndAsync(); } try { using CommandConnection connection = await BuildConnection(); _logger.LogInformation($"[{nameof(DoCode)}] Executing code '{code}'"); string result = await connection.PerformSimpleCode(code, CodeChannel.HTTP); return(Content(result)); } catch (Exception e) { if (e is AggregateException ae) { e = ae.InnerException; } if (e is IncompatibleVersionException) { _logger.LogError($"[{nameof(DoCode)}] Incompatible DCS version"); return(StatusCode(502, "Incompatible DCS version")); } if (e is SocketException) { _logger.LogError($"[{nameof(DoCode)}] DCS is not started"); return(StatusCode(503, "Failed to connect to Duet, please check your connection (DCS is not started)")); } _logger.LogWarning(e, $"[{nameof(DoCode)}] Failed to perform code"); return(StatusCode(500, e.Message)); } }
public static async Task Main(string[] args) { // Parse the command line arguments string lastArg = null, codeToExecute = null, socketPath = Defaults.FullSocketPath; bool quiet = false; foreach (string arg in args) { if (lastArg == "-s" || lastArg == "--socket") { socketPath = arg; } else if (lastArg == "-c" || lastArg == "-c") { codeToExecute = arg; } else if (arg == "-q" || arg == "--quiet") { quiet = true; } else if (arg == "-h" || arg == "--help") { Console.WriteLine("Available command line arguments:"); Console.WriteLine("-s, --socket <socket>: UNIX socket to connect to"); Console.WriteLine("-c, --code <code>: Execute the given code(s), wait for the result and exit"); Console.WriteLine("-q, --quiet: Do not display when a connection has been established (only applicable in interactive mode)"); Console.WriteLine("-h, --help: Display this help text"); return; } lastArg = arg; } // Create a new connection and connect to DuetControlServer using CommandConnection connection = new CommandConnection(); await connection.Connect(socketPath); // Check if this is an interactive session if (codeToExecute == null) { if (!quiet) { // Notify the user that a connection has been established Console.WriteLine("Connected!"); } // Register an (interactive) user session int sessionId = await connection.AddUserSession(DuetAPI.Machine.AccessLevel.ReadWrite, DuetAPI.Machine.SessionType.Local, "console"); // Start reading lines from stdin and send them to DCS as simple codes. // When the code has finished, the result is printed to stdout string input = Console.ReadLine(); while (input != null && input != "exit" && input != "quit") { try { string output = await connection.PerformSimpleCode(input); if (output.EndsWith(Environment.NewLine)) { Console.Write(output); } else { Console.WriteLine(output); } } catch (SocketException) { Console.WriteLine("Server has closed the connection"); break; } catch (Exception e) { if (e is AggregateException ae) { e = ae.InnerException; } Console.WriteLine(e.Message); } input = Console.ReadLine(); } // Unregister this session again if (connection.IsConnected) { await connection.RemoveUserSession(sessionId); } } else { // Execute only the given code(s) and quit string output = await connection.PerformSimpleCode(codeToExecute); if (output.EndsWith('\n')) { Console.Write(output); } else { Console.WriteLine(output); } } }