예제 #1
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));
        }
예제 #2
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));
        }
예제 #3
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));
        }