Ejemplo n.º 1
0
        public void UpdateHeartRateMessage(HeartRateMessage message)
        {
            if (message == null)
            {
                return;
            }

            _latestHeartRateMessage = message;
            SendMessageToHub(_latestHeartRateMessage);
        }
Ejemplo n.º 2
0
 private void InitializeMessages()
 {
     _latestContextMessage = new ContextMessage
     {
         GeoFilter  = GeoFilter.All,
         TimeFilter = DateTime.Today
     };
     _latestSummaryMessage = new SummaryMessage()
     {
         IsDefault = true
     };
     _latestTemperatureMessage = new TemperatureMessage()
     {
         IsDefault = true
     };
     _latestHeartRateMessage = new HeartRateMessage()
     {
         IsDefault = true
     };
     _latestOxygenMessage = new OxygenMessage()
     {
         IsDefault = true
     };
     _latestBostonMessage = new BostonMessage()
     {
         IsDefault = true
     };
     _latestChicagoMessage = new ChicagoMessage()
     {
         IsDefault = true
     };
     _latestNewYorkMessage = new NewYorkMessage()
     {
         IsDefault = true
     };
     _latestGlucoseMessage = new GlucoseMessage()
     {
         IsDefault = true
     };
     _sendToGroup = new Dictionary <string, Action>()
     {
         { GetMessageMetadata <ContextMessage>().Group, () => SendMessageToHub(LatestContextMessage) },
         { GetMessageMetadata <SummaryMessage>().Group, () => SendMessageToHub(LatestSummaryMessage) },
         { GetMessageMetadata <TemperatureMessage>().Group, () => SendMessageToHub(LatestTemperatureMessage) },
         { GetMessageMetadata <OxygenMessage>().Group, () => SendMessageToHub(LatestOxygenMessage) },
         { GetMessageMetadata <GlucoseMessage>().Group, () => SendMessageToHub(LatestGlucoseMessage) },
         { GetMessageMetadata <HeartRateMessage>().Group, () => SendMessageToHub(LatestHeartRateMessage) },
         { GetMessageMetadata <BostonMessage>().Group, () => SendMessageToHub(LatestBostonMessage) },
         { GetMessageMetadata <ChicagoMessage>().Group, () => SendMessageToHub(LatestChicagoMessage) },
         { GetMessageMetadata <NewYorkMessage>().Group, () => SendMessageToHub(LatestNewYorkMessage) }
     };
 }
Ejemplo n.º 3
0
        public async Task <JsonResult> SendData(int heartRate)
        {
            try
            {
                using (var httpClient1 = new HttpClient())
                {
                    HeartRateMessage hmMessage = null;
                    dynamic          jsonData  = new ExpandoObject();
                    jsonData.n       = 1;
                    jsonData.timeout = 60000;
                    jsonData.wait    = 0;
                    jsonData.delete  = false;
                    HttpRequestMessage request = new HttpRequestMessage
                    {
                        Content    = new StringContent(JsonConvert.SerializeObject(jsonData), Encoding.UTF8, "application/json"),
                        Method     = HttpMethod.Post,
                        RequestUri = new Uri("https://mq-aws-eu-west-1-1.iron.io/3/projects/57dbdea41e0aa6000858dbae/queues/messages/reservations?oauth=pqYbPAL1MN3mf5PyBu2S")
                    };
                    //reserve message in iron message queue for 1 minute
                    var response = await httpClient1.SendAsync(request);

                    if (response.IsSuccessStatusCode)
                    {
                        IList <HeartRateMessage> messagesLst = null;
                        var ironResponse = await response.Content.ReadAsStringAsync();

                        var ironMessage = JsonConvert.DeserializeObject <IDictionary <string, object> >(ironResponse);
                        if (ironMessage.Count > 0 && ironMessage.ContainsKey("messages"))
                        {
                            messagesLst = JsonConvert.DeserializeObject <IList <HeartRateMessage> >(ironMessage["messages"].ToString());
                        }

                        if (messagesLst != null && messagesLst.LastOrDefault() != null)
                        {
                            hmMessage = messagesLst.LastOrDefault();
                        }

                        Parallel.Invoke(() =>
                        {
                            if (hmMessage != null)
                            {
                                //delete iron queue message after reading
                                DeleteIronMessageByID(hmMessage.id, hmMessage.reservation_id);
                            }
                        }, async() =>
                        {
                            if (hmMessage == null || (hmMessage != null && !string.IsNullOrEmpty(hmMessage.body) && !hmMessage.body.ToUpper().Contains("DONE")))
                            {
                                HeartRateMonitor hrMonitor = new HeartRateMonitor();
                                hrMonitor.deviceID         = mDeviceID;
                                hrMonitor.heartRate        = heartRate;
                                using (var httpClient2 = new HttpClient())
                                {
                                    httpClient2.DefaultRequestHeaders.Accept.Clear();
                                    httpClient2.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                                    httpClient2.DefaultRequestHeaders.Add("Authorization", "Bearer iguZgpnIwr8N60cD7cYgSMbYQm9QZXEY9AaqmeD6f4d2DoyvZNEhcQdzGSSFivDylWcXR5ShTu1AfMSCJi9sAj");
                                    request = new HttpRequestMessage
                                    {
                                        Content    = new StringContent(JsonConvert.SerializeObject(hrMonitor), Encoding.UTF8, "application/json"),
                                        Method     = HttpMethod.Post,
                                        RequestUri = new Uri("https://ingestion-xcdvudaz0dz3.us3.sfdcnow.com/streams/heart_rate_monito001/heart_rate_monito001/event")
                                    };
                                    response = await httpClient2.SendAsync(request);
                                }
                            }
                        });
                    }

                    if (hmMessage == null)
                    {
                        return(Json(null));
                    }
                    else
                    {
                        return(Json(hmMessage.body));
                    }
                }
            }
            catch (Exception ex)
            {
                return(Json(null));
            }
        }
Ejemplo n.º 4
0
        public async Task <JsonResult> SendData(int heartRate)
        {
            try
            {
                HeartRateMessage hmMessage = null;
                using (var httpClient1 = new HttpClient())
                {
                    HeartRateMonitor hrMonitor = new HeartRateMonitor();
                    hrMonitor.deviceID  = ConfigVars.Instance.DeviceID;
                    hrMonitor.heartRate = heartRate;
                    httpClient1.DefaultRequestHeaders.Accept.Clear();
                    httpClient1.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    httpClient1.DefaultRequestHeaders.Add("Authorization", ConfigVars.Instance.DeviceToken);
                    HttpRequestMessage request = new HttpRequestMessage
                    {
                        Content    = new StringContent(JsonConvert.SerializeObject(hrMonitor), Encoding.UTF8, "application/json"),
                        Method     = HttpMethod.Post,
                        RequestUri = new Uri(ConfigVars.Instance.EnpointUrl)
                    };
                    var response = await httpClient1.SendAsync(request);

                    if (response.IsSuccessStatusCode)
                    {
                        using (var httpClient2 = new HttpClient())
                        {
                            dynamic jsonData = new ExpandoObject();
                            jsonData.n       = 1;
                            jsonData.timeout = 60000;
                            jsonData.wait    = 0;
                            jsonData.delete  = false;
                            request          = new HttpRequestMessage
                            {
                                Content    = new StringContent(JsonConvert.SerializeObject(jsonData), Encoding.UTF8, "application/json"),
                                Method     = HttpMethod.Post,
                                RequestUri = new Uri(string.Format("{0}/reservations?oauth={1}", ConfigVars.Instance.IronMQUrl, ConfigVars.Instance.IronMQToken))
                            };

                            response = await httpClient2.SendAsync(request);

                            if (response.IsSuccessStatusCode)
                            {
                                IList <HeartRateMessage> messagesLst = null;
                                var ironResponse = await response.Content.ReadAsStringAsync();

                                var ironMessage = JsonConvert.DeserializeObject <IDictionary <string, object> >(ironResponse);
                                if (ironMessage.Count > 0 && ironMessage.ContainsKey("messages"))
                                {
                                    messagesLst = JsonConvert.DeserializeObject <IList <HeartRateMessage> >(ironMessage["messages"].ToString());
                                }
                                if (messagesLst != null && messagesLst.Where(p => !string.IsNullOrEmpty(p.body) && p.body.Contains(ConfigVars.Instance.DeviceID)).Count() > 0)
                                {
                                    hmMessage = messagesLst.Where(p => p.body.Contains(ConfigVars.Instance.DeviceID)).LastOrDefault();
                                }

                                if (hmMessage != null)
                                {
                                    Parallel.Invoke(() =>
                                    {
                                        //delete iron queue message after reading
                                        DeleteIronMessageByID(hmMessage.id, hmMessage.reservation_id);
                                    });
                                }
                            }
                        }
                    }
                }

                if (hmMessage == null)
                {
                    return(Json(null));
                }
                else
                {
                    return(Json(hmMessage.body));
                }
            }
            catch (Exception ex)
            {
                return(Json(null));
            }
        }