示例#1
0
 /// <summary>
 /// Execute the command on the device.
 /// </summary>
 /// <exception cref="ArgumentNullException">Config is null. -or- ParameterSet is null.</exception>
 /// <exception cref="ArgumentException">Config is invalid. -or- ParameterSet is invalid.</exception>
 /// <exception cref="NetOpnApiHttpException">An HTTP error is raised or an invalid HTTP status code is returned.</exception>
 /// <exception cref="NetOpnApiNotAuthorizedException">The configured API key does not have sufficient access.</exception>
 /// <exception cref="NetOpnApiNotImplementedException">The command is not implemented on the device.</exception>
 /// <exception cref="NetOpnApiInvalidResponseException">The command returns an invalid response.</exception>
 public static void Execute <TResponse>(this ICommandWithResponse <TResponse> self)
 => ExecuteCommand(
     self,
     self.CreateRequest,
     self.SetEmptyResponse,
     self.SetResponse
     );
示例#2
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.");
                }
            }
        }
示例#3
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);
            }
        }
示例#4
0
        private static void SetResponse <T>(this ICommandWithResponse <T> self, JsonElement value)
        {
            if (value.ValueKind == JsonValueKind.Null)
            {
                self.SetEmptyResponse();
                return;
            }

            var t = typeof(T);

            try
            {
                self.Logger?.LogDebug($"Deserializing {t} from JSON element.");
                self.Response = JsonSerializer.Deserialize <T>(value.ToString(), new JsonSerializerOptions());
            }
            catch (JsonException)
            {
                throw new ArgumentException($"Failed to decode {t} from JSON.");
            }
        }
示例#5
0
 public R RunCommand <R>(ICommandWithResponse <R> command, params IEvent[] expectedEvents)
 {
     return(command.Run(this, expectedEvents));
 }
示例#6
0
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="TResponse"></typeparam>
 /// <param name="cmd"></param>
 /// <param name="msgId"></param>
 /// <param name="message"></param>
 /// <returns></returns>
 public static TResponse ResponseFailure <TResponse>(this ICommandWithResponse <TResponse> cmd, BluffinMessageId msgId, string message) where TResponse : IResponse
 {
     return(cmd.Response(false, msgId, message));
 }
示例#7
0
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="TResponse"></typeparam>
 /// <param name="cmd"></param>
 /// <returns></returns>
 public static TResponse ResponseSuccess <TResponse>(this ICommandWithResponse <TResponse> cmd) where TResponse : IResponse
 {
     return(cmd.Response(true, BluffinMessageId.None, String.Empty));
 }
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="TResponse"></typeparam>
 /// <param name="cmd"></param>
 /// <param name="msgId"></param>
 /// <param name="message"></param>
 /// <returns></returns>
 public static TResponse ResponseSuccess <TResponse>(this ICommandWithResponse <TResponse> cmd, TaluvaMessageId msgId, string message) where TResponse : IResponse
 {
     return(cmd.Response(true, msgId, message));
 }
示例#9
0
 private static void SetEmptyResponse <T>(this ICommandWithResponse <T> self)
 {
     self.Response = default(T);
 }