Exemple #1
0
        /// <summary>
        /// derived class must implement to handle GET request
        /// </summary>
        protected override HttpServiceResponse ProcessGetRequest(HttpServiceContext context)
        {
            HttpListenerRequest  request  = context.Context.Request;
            HttpListenerResponse response = context.Context.Response;

            string dataName = _defaultDataName;

            if (context.UnmatchedSegments?.Count > 0)
            {
                dataName = context.UnmatchedSegments[0];
            }
            IotData data = _service.GetData <IotData>(dataName);

            if (data != null)
            {
                string responseString = JsonConvert.SerializeObject(data, DefaultJsonSerializerSettings);
                response.ContentType = "application/json";
                return(CreateSuccessResponse(context, Name, 200, responseString));
            }
            return(CreateResponseForBadRequest(context, Name, $"InvalidDataName: {dataName}"));
        }
Exemple #2
0
        public IotData GetData(string mac)
        {
            return(App.Cache.GetOrAdd($"iot_data_{mac}",
                                      key =>
            {
                var data = new IotData
                {
                    Device = GetDevice(mac)
                };

                // set param default with null
                foreach (var param in data.Device.Params.Keys)
                {
                    data.Values[param] = null;
                }

                return data;
            }
                                      )
                   .Do(d => d.Device = GetDevice(mac)));
        }