Beispiel #1
0
        /// <summary>
        /// Execute the command and wait for completion.
        /// </summary>
        /// <param name="self"></param>
        /// <returns></returns>
        public static StatusWithUuidAndLog ExecuteAndWait(this ICommandWithResponse <StatusWithUuid> self)
        {
            self.Execute();
            if (self.Response?.Status != "ok")
            {
                return(new StatusWithUuidAndLog()
                {
                    Status = self.Response?.Status,
                    Uuid = self.Response?.Uuid ?? Guid.Empty
                });
            }

            var ret = new StatusWithUuidAndLog()
            {
                Status = "running",
                Uuid   = self.Response.Uuid
            };

            var progressCommand = new Commands.Core.Firmware.GetUpgradeProgress()
            {
                Config = self.Config,
                Logger = self.Logger
            };

            while (true)
            {
                progressCommand.Execute();

                if (progressCommand.Response is null)
                {
                    throw new NetOpnApiInvalidResponseException(ErrorCode.MissingResponse, "Failed to determine status of running command.");
                }

                switch (progressCommand.Response.Status)
                {
                case "done":
                case "reboot":
                    ret.Status = progressCommand.Response.Status;
                    ret.Log    = progressCommand.Response.Log;
                    return(ret);

                case "error":
                    ret.Status = "error";
                    ret.Log    = "There is no command running.";
                    return(ret);

                case "running":
                    Thread.Sleep(250);
                    break;

                default:
                    throw new NetOpnApiInvalidResponseException(ErrorCode.InvalidStatus, $"The status \"{progressCommand.Response.Status}\" is not valid.");
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Try to executed the command.
        /// </summary>
        /// <param name="self"></param>
        /// <param name="errorCode">Returns the error code if command execution fails.</param>
        /// <returns></returns>
        public static bool TryExecute <TResponse>(this ICommandWithResponse <TResponse> self, out ErrorCode errorCode)
        {
            errorCode = ErrorCode.NoError;

            try
            {
                self.Execute();
                return(true);
            }
            catch (NetOpnApiException e)
            {
                errorCode = e.Code;
                return(false);
            }
        }