public void Handle(string json)
        {
            Console.WriteLine("Fall detection handler invoked...");

            var fall = JsonConvert.DeserializeObject <dynamic>(json);

            Console.WriteLine(fall);

            if (fall.category.ToString() == "FALL_DETECTION")
            {
                Console.WriteLine("FALL detected!");

                var path = "/api/v1/user/" + fall.content["user"]["id"] + "/";
                Console.WriteLine(path);

                var lang = storeAPI.GetLang(path);

                //Console.WriteLine(path);

                InformCaregivers(path, "steps", "high",
                                 Loc.Get(lang, Loc.MSG, Loc.FALL_DETECTED, Loc.CAREGVR),
                                 Loc.Get(lang, Loc.DES, Loc.FALL_DETECTED, Loc.CAREGVR)

                                 );
            }
        }
Example #2
0
        private void AnalyzeStepCount(string userURIPath)
        {
            Console.WriteLine("Analize step count invoked!");

            var LANG = storeAPI.GetLang(userURIPath);

            var now     = DateTime.UtcNow;
            var startTs = (long)ChangeTime(now, 0, 0).Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
            var endTs   = (long)now.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;;

            int userStepCount = storeAPI.GetUserStepCount(userURIPath, startTs, endTs);

            Console.WriteLine("Analize steps count " + userStepCount);

            if (userStepCount < 1000)
            {
                Console.WriteLine("Case < 1000");
                var caregiverMsg = string.Format(Loc.Get(LANG, Loc.MSG, Loc.STEPS_LESS_1000, Loc.CAREGVR), userStepCount);
                InformCaregivers(userURIPath, "steps", "high", caregiverMsg, Loc.Get(LANG, Loc.DES, Loc.STEPS_LESS_1000, Loc.CAREGVR));
            }
            else if (userStepCount < 2000)
            {
                Console.WriteLine("Case > 1000 && < 2000");
                var endUserMsg = string.Format(Loc.Get(LANG, Loc.MSG, Loc.STEPS_BETWEEN_1000_2000, Loc.USR), userStepCount);
                InformUser(userURIPath, "steps", "medium", endUserMsg, Loc.Get(LANG, Loc.DES, Loc.STEPS_BETWEEN_1000_2000, Loc.USR));
            }
            else if (userStepCount > 6000)
            {
                try{
                    Console.WriteLine("Case > 6000");
                    var endUserMsg = string.Format(Loc.Get(LANG, Loc.MSG, Loc.STEPS_BIGGER_6000, Loc.USR), userStepCount);
                    InformUser(userURIPath, "steps", "low", endUserMsg, string.Format(Loc.Get(LANG, Loc.DES, Loc.STEPS_BIGGER_6000, Loc.USR), userStepCount));
                }
                catch (Exception e) {
                    Console.WriteLine("ERROR: " + e.ToString());
                }
            }
        }
        private void SendBPMeasurementNotification(string userURIPath)
        {
            var LANG = storeAPI.GetLang(userURIPath);

            string enduser_msg  = Loc.Get(LANG, Loc.MSG, Loc.REMINDER_SENT, Loc.USR);
            string enduser_desc = Loc.Get(LANG, Loc.DES, Loc.REMINDER_SENT, Loc.USR);

            string caregiver_msg  = Loc.Get(LANG, Loc.MSG, Loc.REMINDER_SENT, Loc.CAREGVR);
            string caregiver_desc = Loc.Get(LANG, Loc.DES, Loc.REMINDER_SENT, Loc.CAREGVR);

            string notification_type = "appointment";

            // get the user data to retrieve caregiver uri as well
            var userData = storeAPI.GetUserData(userURIPath);

            if (userData != null)
            {
                // send notification for end user
                int          enduserID           = GetIdFromURI(userURIPath);
                JournalEntry endUserJournalEntry = storeAPI.PushJournalEntry(userURIPath, notification_type, "low", enduser_msg, enduser_desc);
                insertionAPI.InsertPushNotification(JsonConvert.SerializeObject(new DSS.RMQ.INS.PushNotification()
                {
                    message = enduser_msg, user_id = enduserID
                }));

                // send notification to all caregivers
                List <int> caregiverJournalEntryIDs = new List <int>();
                var        profile = userData["enduser_profile"];
                if (profile["caregivers"] != null)
                {
                    foreach (var caregiverURIPath in profile["caregivers"])
                    {
                        int caregiverID = GetIdFromURI((string)caregiverURIPath);

                        var caregiverJournalEntry = storeAPI.PushJournalEntry((string)caregiverURIPath, notification_type, "low", caregiver_msg, caregiver_desc);
                        insertionAPI.InsertPushNotification(JsonConvert.SerializeObject(new DSS.RMQ.INS.PushNotification()
                        {
                            message = caregiver_msg, user_id = caregiverID
                        }));

                        caregiverJournalEntryIDs.Add(caregiverJournalEntry.id);
                    }
                }

                // generate reminder event
                var reminderEvent = new Event()
                {
                    category = "USER_NOTIFICATIONS",
                    content  = new Content()
                    {
                        uuid       = Guid.NewGuid().ToString(),
                        name       = "reminder_sent",
                        value_type = "complex",
                        val        = new Dictionary <string, dynamic>()
                        {
                            { "user", new Dictionary <string, int>()
                              {
                                  { "id", enduserID }
                              } },
                            { "journal", new Dictionary <string, dynamic>()
                              {
                                  { "id_enduser", endUserJournalEntry.id },
                                  { "id_caregivers", caregiverJournalEntryIDs.ToArray <int>() }
                              } },
                        }
                    },
                    annotations = new Annotations()
                    {
                        timestamp = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds,
                        source    = "DSS"
                    }
                };

                Console.WriteLine("[MotionEventHandler] Inserting new reminderEvent: " + reminderEvent);
                insertionAPI.InsertEvent(JsonConvert.SerializeObject(reminderEvent));
            }
            else
            {
                Console.WriteLine("[MotionEventHandler] Error - no information available on user with uri: " + userURIPath + ". Cannot send notification to end user and caregivers.");
            }
        }
        public void Handle(string json)
        {
            Console.WriteLine("Reminder invoked...");

            var reminder = JsonConvert.DeserializeObject <dynamic>(json);

            if (reminder.category.ToString().ToLower() == "user_notifications")
            {
                var userIdStr   = reminder.content.value.user.id.ToString();
                var userURIPath = "/api/v1/user/" + userIdStr + "/";
                var key         = userURIPath;

                var LANG = storeAPI.GetLang(userURIPath);

                if (reminder.content.name == "exercise_started")
                {
                    key = userURIPath + "_exercise";
                    Console.WriteLine("[ReminderHandler] Exercise started");
                    Console.WriteLine("[ReminderHandler] userActiveExcercise map key: " + key);

                    userActiveExerciseMap[key] = reminder;
                    string exerciseType = reminder.content.value.exercise_type.ToString();

                    InformCaregivers(userURIPath, "exercise", "none", Loc.Get(LANG, Loc.MSG, Loc.EXERCISE_STARTED, Loc.CAREGVR), string.Format(Loc.Get(LANG, Loc.DES, Loc.EXERCISE_STARTED, Loc.CAREGVR), exerciseType));
                    return;
                }
                else if (reminder.content.name == "exercise_ended")
                {
                    key = userURIPath + "_exercise";
                    Console.WriteLine("[ReminderHandler] Exercise ended");
                    Console.WriteLine("[ReminderHandler] userActiveExcercise map key: " + key);

                    if (userActiveExerciseMap.ContainsKey(key) && reminder.content.value.session_uuid == userActiveExerciseMap[key].content.value.session_uuid)
                    {
                        Console.WriteLine("Exercise ended is in the active map");

                        float  score        = float.Parse(reminder.content.value.score.ToString());
                        string exerciseType = reminder.content.value.exercise_type.ToString();

                        InformCaregivers(userURIPath,
                                         "exercise",
                                         "none",
                                         Loc.Get(LANG, Loc.MSG, Loc.EXERCISE_ENDED, Loc.CAREGVR),
                                         string.Format(Loc.Get(LANG, Loc.DES, Loc.EXERCISE_ENDED, Loc.CAREGVR), exerciseType, score));



                        var desc = Loc.EXERCISE_ENDED_HIGH;

                        if (score <= 30)
                        {
                            desc = Loc.EXERCISE_ENDED_LOW;
                        }
                        else if (score > 30 && score <= 60)
                        {
                            desc = Loc.EXERCISE_ENDED_MID;
                        }


                        InformUser(userURIPath, "exercise", "low", Loc.Get(LANG, Loc.MSG, Loc.EXERCISE_ENDED_LOW, Loc.USR), Loc.Get(LANG, Loc.DES, desc, Loc.USR));
                        userActiveExerciseMap.Remove(key);
                    }
                    return;
                }

                //key = userURIPath + reminder.type;

                if (reminder.content.name == "reminder_sent")
                {
                    Console.WriteLine("[reminder_handler] reminder sent entered");
                    var journalId    = reminder.content.value.journal.id_enduser.ToString();
                    var journalEntry = storeAPI.GetJournalEntryById(journalId);

                    Console.WriteLine("[reminder_handler] Sent journal entry message for reminder: " + journalEntry.message.ToString());

                    Console.WriteLine("Type: " + journalEntry.type.ToString());

                    var msg = new List <string>(journalEntry.message.ToString().Split(' '));
                    var des = new List <string>(journalEntry.description.ToString().Split(' '));

                    msg.AddRange(des);

                    Console.WriteLine("Contains blood or presure " + (msg.Contains("blood") || msg.Contains("pressure")));
                    Console.WriteLine("Contains weight: " + msg.Contains("weight"));

                    // set the key
                    if (msg.Contains("blood") || msg.Contains("pressure"))
                    {
                        key = userURIPath + "_blood_pressure";
                    }
                    else if (msg.Contains("weight"))
                    {
                        key = userURIPath + "_weight";
                    }
                    Console.WriteLine("[ReminderHandler] userReminder map key: " + key);


                    if (userReminderMap.ContainsKey(key))
                    {
                        userReminderMap.Remove(key);
                    }

                    userReminderMap.Add(key, reminder);

                    var aTimer = new System.Timers.Timer(WAIT_MS);
                    aTimer.AutoReset = false;
                    aTimer.Start();
                    aTimer.Elapsed += (x, y) =>
                    {
                        if (userReminderMap.ContainsKey(key))
                        {
                            if (msg.Contains("blood") || msg.Contains("pressure"))
                            {
                                Console.WriteLine("Blood pressure measured wasn't ack");

                                InformCaregivers(userURIPath, "heart", "high",
                                                 "The person under your care ignored the blood pressure measurements reminder.",
                                                 "Please take action and call to remind them of the measurement.");

                                userReminderMap.Remove(key);
                            }
                            else if (msg.Contains("weight"))
                            {
                                InformCaregivers(userURIPath, "weight", "high",
                                                 "The person under your care ignored the weight measurements reminder.",
                                                 "Please take action and call to remind them of the measurement.");

                                userReminderMap.Remove(key);
                            }
                        }
                    };
                }
                else if (reminder.content.name == "reminder_acknowledged")
                {
                    var ack          = reminder.content.value.ack.ToString() == "ok" ? true : false;
                    var journalId    = reminder.content.value.journal.id.ToString();
                    var journalEntry = storeAPI.GetJournalEntryById(journalId);

                    Console.WriteLine(journalId);
                    storeAPI.PatchJournalEntry(journalId, ack);

                    var msg = new List <string>(journalEntry.message.ToString().Split(' '));
                    var des = new List <string>(journalEntry.description.ToString().Split(' '));

                    msg.AddRange(des);

                    Console.WriteLine("Contains blood or presure " + (msg.Contains("blood") || msg.Contains("pressure")));
                    Console.WriteLine("Contains weight: " + msg.Contains("weight"));

                    if (ack)
                    {
                        MetricsPublisher.Current.Increment("cami.event.reminder.ack", 1);

                        Console.WriteLine("Reminder acknowledged");

                        //Do this just in case it's possible to check if user did something for example
                        //there is a new value in weight measurements and ignore if it's not possible
                        //for example medication

                        Console.WriteLine("Type: " + journalEntry.type.ToString());
                        Console.WriteLine("[reminder_handler] Acknowledged journal entry message: " + journalEntry.message.ToString());

                        //var expectedMessage = Loc.Get(LANG, Loc.MSG, Loc.REMINDER_SENT, Loc.USR);

                        if (msg.Contains("blood") || msg.Contains("pressure"))
                        {
                            Console.WriteLine("ACK blood pressure measurement!");

                            // set key
                            key = userURIPath + "_blood_pressure";

                            var aTimer = new System.Timers.Timer(WAIT_MS);
                            aTimer.AutoReset = false;
                            aTimer.Start();
                            aTimer.Elapsed += (x, y) =>
                            {
                                Console.WriteLine("Blood pressure check invoked");

                                if (!storeAPI.CheckForMeasuremntInLastNMinutes("blood_pressure", 6, int.Parse(userIdStr)))
                                {
                                    Console.WriteLine("Blood pressure wasn't measured");

                                    InformCaregivers(userURIPath, "heart", "high",
                                                     Loc.Get(LANG, Loc.MSG, Loc.MEASUREMENT_IGNORED, Loc.CAREGVR),
                                                     Loc.Get(LANG, Loc.DES, Loc.MEASUREMENT_IGNORED, Loc.CAREGVR));
                                }
                            };
                        }
                        else if (msg.Contains("weight"))
                        {
                            Console.WriteLine("ACK Weight measurement!");

                            // set key
                            key = userURIPath + "_weight";

                            var aTimer = new System.Timers.Timer(WAIT_MS);
                            aTimer.AutoReset = false;
                            aTimer.Start();
                            aTimer.Elapsed += (x, y) =>
                            {
                                if (!storeAPI.CheckForMeasuremntInLastNMinutes("weight", 6, int.Parse(userIdStr)))
                                {
                                    Console.WriteLine("Weight wasn't measured");

                                    InformCaregivers(userURIPath, "weight", "high",
                                                     Loc.Get(LANG, Loc.MSG, Loc.MEASUREMENT_IGNORED_WEIGHT, Loc.CAREGVR),
                                                     Loc.Get(LANG, Loc.DES, Loc.MEASUREMENT_IGNORED_WEIGHT, Loc.CAREGVR));
                                }
                            };
                        }

                        Console.WriteLine("[ReminderHandler] userReminder map key: " + key);
                        userReminderMap.Remove(key);
                    }
                    else
                    {
                        Console.WriteLine("Reminder snoozed");

                        // send the BP snoozed notification only if it is relating to a BP reminder journal entry

                        if (msg.Contains("blood") || msg.Contains("pressure"))
                        {
                            // set key
                            key = userURIPath + "_blood_pressure";

                            Console.WriteLine("Snoozed blood pressure measurement!");
                            InformCaregivers(userURIPath, "heart", "medium", Loc.Get(LANG, Loc.MSG, Loc.MEASUREMENT_POSTPONED, Loc.CAREGVR), Loc.Get(LANG, Loc.DES, Loc.MEASUREMENT_POSTPONED, Loc.CAREGVR));
                        }
                        else if (msg.Contains("weight"))
                        {
                            // set key
                            key = userURIPath + "_weight";

                            Console.WriteLine("Snoozed weight measurement!");
                            InformCaregivers(userURIPath, "weight", "medium", Loc.Get(LANG, Loc.MSG, Loc.MEASUREMENT_POSTPONED_WEIGHT, Loc.CAREGVR),
                                             Loc.Get(LANG, Loc.DES, Loc.MEASUREMENT_POSTPONED_WEIGHT, Loc.CAREGVR));
                        }

                        Console.WriteLine("[ReminderHandler] userReminder map key: " + key);
                        userReminderMap.Remove(key);
                    }
                }
            }
        }