Пример #1
0
        public async Task <HttpResponseMessage> Post(HttpRequestMessage requestMessage)
        {
            var    deviceId = Request.GetDeviceId();
            var    json     = string.Empty;
            JArray events   = null;

            try
            {
                json = await requestMessage.Content.ReadAsStringAsync();

                dynamic request = JObject.Parse(json);
                events = (JArray)request.events;

                _log.TraceEvent("ParseEvents",
                                new Facet("deviceId", deviceId),
                                new Facet("eventCount", events.Count));
            }
            catch (Exception ex)
            {
                var errorId = _log.ErrorEvent("ParseEvents", ex,
                                              new Facet("json", json));

                var error     = new { errorId = errorId };
                var errorJson = JsonConvert.SerializeObject(error);

                return(new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    Content = new StringContent(errorJson)
                });
            }

            var sendToEventHubs = Config.Parse <bool>("DataBrain.DeviceEvents.SendToEventHubs");

            if (sendToEventHubs && events.Any())
            {
                try
                {
                    await _sender.SendEventsAsync(events, deviceId);

                    return(new HttpResponseMessage(HttpStatusCode.Created));
                }
                catch (Exception ex)
                {
                    var errorId   = _log.ErrorEvent("SendEvents", ex);
                    var error     = new { errorId = errorId };
                    var errorJson = JsonConvert.SerializeObject(error);

                    return(new HttpResponseMessage(HttpStatusCode.InternalServerError)
                    {
                        Content = new StringContent(errorJson)
                    });
                }
            }

            return(new HttpResponseMessage(HttpStatusCode.OK));
        }
Пример #2
0
        //The return will only contain the return message (based on if the event has been successfully processed)
        //The request will contain an array of events, but the schema is not fixed (typical BigData).
        //So we wont be working with Model (instead only with HttpResponse and HttpRequest Message.
        public async Task <HttpResponseMessage> Post(HttpRequestMessage requestMessage)
        {
            var    json     = string.Empty;
            var    deviceId = Request.GetDeviceId(); //The device id is read from the header because it is same for all the events in the post request
            JArray events;

            try
            {
                json = await requestMessage.Content.ReadAsStringAsync();

                dynamic request = JObject.Parse(json); //dynamic JSON has been used becasue we are not fixing the JSON schema
                events = (JArray)request.events;

                _log.TraceEvent("ParseEvent",   //Disable Trace in higher environment
                                new Facet("deviceId", deviceId),
                                new Facet("eventCount", events.Count.ToString()));
            }
            catch (Exception error)
            {
                _log.ErrorEvent("ParseEvent", error, new Facet("json", json));
                throw;
            }

            if (Boolean.Parse(ConfigurationManager.AppSettings["Telemetry.DeviceEvents.SendToEventhub"]))
            {
                try
                {
                    await _sender.SendEventsAsync(events, deviceId);    //Wait for the sending to be over, becasue based on the result we might have to change the response

                    return(new HttpResponseMessage(HttpStatusCode.OK));
                }
                catch (Exception ex)
                {
                    var errorId   = _log.ErrorEvent("SendEvents", ex);
                    var error     = new { errorId = errorId };
                    var errorJson = JsonConvert.SerializeObject(error);

                    return(new HttpResponseMessage(HttpStatusCode.InternalServerError)
                    {
                        Content = new StringContent(errorJson)
                    });
                }
            }
            return(new HttpResponseMessage(HttpStatusCode.OK));
        }