Exemplo n.º 1
0
        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. Alternative codes: startUpdate (Set DSF to updating), endUpdate (End DSF updating state)");
                    Console.WriteLine("-q, --quiet: Do not output any messages (not applicable for code replies 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.ObjectModel.AccessLevel.ReadWrite, DuetAPI.ObjectModel.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
                    {
                        if (input.Equals("startUpdate", StringComparison.InvariantCultureIgnoreCase))
                        {
                            await connection.SetUpdateStatus(true);

                            Console.WriteLine("DSF is now in update mode");
                        }
                        else if (input.Equals("endUpdate", StringComparison.InvariantCultureIgnoreCase))
                        {
                            await connection.SetUpdateStatus(false);

                            Console.WriteLine("DSF is no longer in update mode");
                        }
                        else if (input.StartsWith("eval ", StringComparison.InvariantCultureIgnoreCase))
                        {
                            JsonElement result = await connection.EvaluateExpression <JsonElement>(input[5..].Trim());