コード例 #1
0
        /// <summary>
        /// Handle Http GET requests for state endpoint.
        /// </summary>
        /// <param name="requestContext">
        /// Context containing HTTP GET request data, and which will also contain associated
        /// response upon return.
        /// </param>
        /// <returns>
        /// Await-able task.
        /// </returns>
        private async Task HandleGetStateRequest(HttpListenerContext requestContext)
        {
            // Don't cache results of any endpoint requests
            AddNoCacheHeaders(requestContext.Response);

            var responseProperties = new Dictionary <string, object>();

            foreach (var mapEntry in this.streamHandlerMap)
            {
                var handlerStatus = mapEntry.Value.GetState(mapEntry.Key);
                responseProperties.Add(mapEntry.Key, handlerStatus);
            }

            requestContext.Response.ContentType = JsonContentType;
            await responseProperties.DictionaryToJsonAsync(requestContext.Response.OutputStream);

            CloseResponse(requestContext, HttpStatusCode.OK);
        }
コード例 #2
0
        /// <summary>
        /// Handle Http POST requests for state endpoint.
        /// </summary>
        /// <param name="requestContext">
        /// Context containing HTTP POST request data, and which will also contain associated
        /// response upon return.
        /// </param>
        /// <returns>
        /// Await-able task.
        /// </returns>
        private async Task HandlePostStateRequest(HttpListenerContext requestContext)
        {
            // Don't cache results of any endpoint requests
            AddNoCacheHeaders(requestContext.Response);

            Dictionary <string, object> requestProperties;

            try
            {
                requestProperties = await requestContext.Request.InputStream.DictionaryFromJsonAsync();
            }
            catch (SerializationException)
            {
                requestProperties = null;
            }

            if (requestProperties == null)
            {
                CloseResponse(requestContext, HttpStatusCode.BadRequest);
                return;
            }

            var responseProperties = new Dictionary <string, object>();
            var errorStreamNames   = new List <string>();

            foreach (var requestEntry in requestProperties)
            {
                ISensorStreamHandler handler;
                if (!this.streamHandlerMap.TryGetValue(requestEntry.Key, out handler))
                {
                    // Don't process unrecognized handlers
                    responseProperties.Add(requestEntry.Key, Properties.Resources.StreamNameUnrecognized);
                    errorStreamNames.Add(requestEntry.Key);
                    continue;
                }

                var propertiesToSet = requestEntry.Value as IDictionary <string, object>;
                if (propertiesToSet == null)
                {
                    continue;
                }

                var propertyErrors = new Dictionary <string, object>();
                var success        = handler.SetState(requestEntry.Key, new ReadOnlyDictionary <string, object>(propertiesToSet), propertyErrors);
                if (!success)
                {
                    responseProperties.Add(requestEntry.Key, propertyErrors);
                    errorStreamNames.Add(requestEntry.Key);
                }
            }

            if (errorStreamNames.Count == 0)
            {
                responseProperties.Add(SuccessPropertyName, true);
            }
            else
            {
                // The only properties returned other than the "success" property are to indicate error,
                // so if there are other properties present it means that we've encountered at least
                // one error while trying to change state of the streams.
                responseProperties.Add(SuccessPropertyName, false);
                responseProperties.Add(ErrorsPropertyName, errorStreamNames.ToArray());
            }

            requestContext.Response.ContentType = JsonContentType;
            await responseProperties.DictionaryToJsonAsync(requestContext.Response.OutputStream);

            CloseResponse(requestContext, HttpStatusCode.OK);
        }
コード例 #3
0
        /// <summary>
        /// Handle Http POST requests for state endpoint.
        /// </summary>
        /// <param name="requestContext">
        /// Context containing HTTP POST request data, and which will also contain associated
        /// response upon return.
        /// </param>
        /// <returns>
        /// Await-able task.
        /// </returns>
        private async Task HandlePostStateRequest(HttpListenerContext requestContext)
        {
            // Don't cache results of any endpoint requests
            AddNoCacheHeaders(requestContext.Response);
            
            Dictionary<string, object> requestProperties;

            try
            {
                requestProperties = await requestContext.Request.InputStream.DictionaryFromJsonAsync();
            }
            catch (SerializationException)
            {
                requestProperties = null;
            }

            if (requestProperties == null)
            {
                CloseResponse(requestContext, HttpStatusCode.BadRequest);
                return;
            }

            var responseProperties = new Dictionary<string, object>();
            var errorStreamNames = new List<string>();

            foreach (var requestEntry in requestProperties)
            {
                ISensorStreamHandler handler;
                if (!this.streamHandlerMap.TryGetValue(requestEntry.Key, out handler))
                {
                    // Don't process unrecognized handlers
                    responseProperties.Add(requestEntry.Key, Properties.Resources.StreamNameUnrecognized);
                    errorStreamNames.Add(requestEntry.Key);
                    continue;
                }

                var propertiesToSet = requestEntry.Value as IDictionary<string, object>;
                if (propertiesToSet == null)
                {
                    continue;
                }

                var propertyErrors = new Dictionary<string, object>();
                var success = handler.SetState(requestEntry.Key, new ReadOnlyDictionary<string, object>(propertiesToSet), propertyErrors);
                if (!success)
                {
                    responseProperties.Add(requestEntry.Key, propertyErrors);
                    errorStreamNames.Add(requestEntry.Key);
                }
            }

            if (errorStreamNames.Count == 0)
            {
                responseProperties.Add(SuccessPropertyName, true);
            }
            else
            {
                // The only properties returned other than the "success" property are to indicate error,
                // so if there are other properties present it means that we've encountered at least
                // one error while trying to change state of the streams.
                responseProperties.Add(SuccessPropertyName, false);
                responseProperties.Add(ErrorsPropertyName, errorStreamNames.ToArray());
            }

            requestContext.Response.ContentType = JsonContentType;
            await responseProperties.DictionaryToJsonAsync(requestContext.Response.OutputStream);
            CloseResponse(requestContext, HttpStatusCode.OK);
        }
コード例 #4
0
        /// <summary>
        /// Handle Http GET requests for state endpoint.
        /// </summary>
        /// <param name="requestContext">
        /// Context containing HTTP GET request data, and which will also contain associated
        /// response upon return.
        /// </param>
        /// <returns>
        /// Await-able task.
        /// </returns>
        private async Task HandleGetStateRequest(HttpListenerContext requestContext)
        {
            // Don't cache results of any endpoint requests
            AddNoCacheHeaders(requestContext.Response);
            
            var responseProperties = new Dictionary<string, object>();
            foreach (var mapEntry in this.streamHandlerMap)
            {
                var handlerStatus = mapEntry.Value.GetState(mapEntry.Key);
                responseProperties.Add(mapEntry.Key, handlerStatus);
            }

            requestContext.Response.ContentType = JsonContentType;
            await responseProperties.DictionaryToJsonAsync(requestContext.Response.OutputStream);
            CloseResponse(requestContext, HttpStatusCode.OK);
        }