예제 #1
0
 public ObixError(Type @Type, int ErrorCode)
 {
     this.Type            = Type;
     this.ErrorCode       = ErrorCode;
     this.AuxErrorMessage = ObixResult.Message(ErrorCode);
     this.ErrorDate       = DateTime.UtcNow;
 }
예제 #2
0
        /// <summary>
        /// Asynchronously invokes an obix:op(eration) on the oBIX server at endpoint <paramref name="uri"/>, optionally
        /// with the data specified by <paramref name="data"/>.
        /// </summary>
        /// <param name="uri">The URI of the obix:op endpoint to invoke</param>
        /// <param name="data">(optional) The data to send as parameters to the obix:op</param>
        /// <returns>kObixClientResultSuccess on success with the response byte array on success,
        /// another value otherwise.</returns>
        public async Task <ObixResult <byte[]> > InvokeUriAsync(Uri uri, byte[] data)
        {
            HttpResponseMessage responseMessage = null;
            HttpRequestMessage  requestMessage  = null;

            byte[] response = null;

            if (WebClient == null || connected == false)
            {
                return(ErrorStack.PushWithObject(GetType(), ObixResult.kObixClientNotConnectedError, (byte[])null));
            }

            try {
                requestMessage = new HttpRequestMessage(HttpMethod.Post, uri);
                requestMessage.Headers.ExpectContinue = false;
                requestMessage.Content = new ByteArrayContent(data);
                requestMessage.Content.Headers.ContentLength = data.Length;

                responseMessage = await WebClient.SendAsync(requestMessage);
            } catch (Exception ex) {
                return(ErrorStack.PushWithObject(GetType(), ex, (byte[])null));
            }

            if (responseMessage.IsSuccessStatusCode == false)
            {
                return(ErrorStack.PushWithObject <byte[]>(GetType(), ObixResult.kObixClientSocketError, responseMessage.ReasonPhrase));
            }

            response = await responseMessage.Content.ReadAsByteArrayAsync();

            return(ObixResult.FromResult(ObixResult.kObixClientSuccess, response));
        }
예제 #3
0
        /// <summary>
        /// Writes the data provided by <paramref name="data"/> to the endpoint specified by
        /// <paramref name="uri"/>.
        /// </summary>
        /// <param name="uri">The oBIX endpoint to send data to</param>
        /// <param name="data">A byte array of the data to send to the oBIX server</param>
        /// <returns>kObixClientResultSuccess on success with the response byte array from the server,
        /// another value otherwise.</returns>
        /// <remarks>
        /// This method should be regarded as raw data, see the derived implementations of ReadUriAsync
        /// for high-level oBIX reads.
        /// </remarks>
        public ObixResult <byte[]> WriteUri(Uri uri, byte[] data)
        {
            HttpResponseMessage response;

            byte[] responseContent = null;

            if (WebClient == null || connected == false)
            {
                return(ErrorStack.PushWithObject(GetType(), ObixResult.kObixClientNotConnectedError, (byte[])null));
            }

            if (data == null || data.Length == 0)
            {
                return(ErrorStack.PushWithObject(GetType(), ObixResult.kObixClientInputError,
                                                 "WriteUri called with no data to write to the oBIX endpoint.", (byte[])null));
            }

            try {
                response = WebClient.PutAsync(uri, new ByteArrayContent(data)).Result;
            } catch (Exception ex) {
                return(ErrorStack.PushWithObject(GetType(), ex, (byte[])null));
            }

            if (response.IsSuccessStatusCode == false)
            {
                return(ErrorStack.PushWithObject <byte[]>(GetType(), ObixResult.kObixClientSocketError, response.ReasonPhrase));
            }

            responseContent = response.Content.ReadAsByteArrayAsync().Result;

            return(ObixResult.FromResult(ObixResult.kObixClientSuccess, responseContent));
        }
예제 #4
0
        /// <summary>
        /// Retrieves an oBIX client error message.
        /// </summary>
        /// <returns>The obix result message.</returns>
        /// <param name="result">Result.</param>
        public static string Message(int result)
        {
            ObixResult obixResult = ObixResult.kObixClientUnknownError;

            obixResult = (ObixResult)result;

            if (itemDict.ContainsKey(obixResult) == false)
            {
                return("Unknown error: " + result);
            }

            return(itemDict[obixResult]);
        }
예제 #5
0
        /// <summary>
        /// Asynchronously gets raw data from the oBIX server specified by <paramref name="uri"/>.
        /// </summary>
        /// <param name="uri">The URI of the oBIX data point to read</param>
        /// <returns>kObixClientResultSuccess with the data array on success, another value otherwise.</returns>
        /// <remarks>
        /// This method should be regarded as raw data, see the derived implementations of ReadUriAsync
        /// for high-level oBIX reads.
        /// </remarks>
        public async Task <ObixResult <byte[]> > ReadUriAsync(Uri uri)
        {
            byte[] data = null;

            if (WebClient == null)
            {
                return(ErrorStack.PushWithObject <byte[]>(this.GetType(), ObixResult.kObixClientNotConnectedError, (byte[])null));
            }

            try {
                data = await WebClient.GetByteArrayAsync(uri.ToString());
            } catch (Exception ex) {
                return(ErrorStack.PushWithObject <byte[]>(this.GetType(), ex, null));
            }

            return(ObixResult.FromResult(ObixResult.kObixClientSuccess, data));
        }
예제 #6
0
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            if (this.Exception == null)
            {
                sb.AppendFormat("[{3} - {0}] Error {1}: {2}", this.ErrorDate.ToLocalTime(), this.ErrorCode, ObixResult.Message(this.ErrorCode), this.Type.ToString());
                if (string.IsNullOrEmpty(this.AuxErrorMessage) == false)
                {
                    sb.AppendFormat(" ({0}: {1})", this.AuxErrorCode, this.AuxErrorMessage);
                }
            }
            else
            {
                sb.AppendFormat("[{3} - {0}] Exception {1}: {2}", this.ErrorDate, this.ErrorCode, this.Exception.GetType().ToString(), this.Exception.Message);
            }
            return(sb.ToString());
        }
예제 #7
0
 public ObixResult <TObject> PushWithObject <TObject>(Type @Type, ObixResult Result, string AuxMessage, TObject obj = default(TObject))
 {
     return(new ObixResult <TObject>(Push(new ObixError(@Type, (int)Result, 0, AuxMessage)), obj));
 }
예제 #8
0
 public ObixResult <TObject> PushWithObject <TObject>(Type @Type, ObixResult Result, TObject obj = default(TObject))
 {
     return(new ObixResult <TObject>(Push(Type, Result), obj));
 }
예제 #9
0
 public ObixResult Push(Type @Type, ObixResult Result, string AuxMessage)
 {
     return(Push(new ObixError(@Type, (int)Result, 0, AuxMessage)));
 }
예제 #10
0
 public ObixResult Push(Type @Type, ObixResult Result)
 {
     return(Push(new ObixError(@Type, (int)Result)));
 }
예제 #11
0
 public static ObixResult <TResult> FromResult <TResult>(ObixResult result, TResult obj)
 {
     return(new ObixResult <TResult>(result, obj));
 }