コード例 #1
0
        public void Handle(string json)
        {
            Console.WriteLine("MOTION event handler invoked ...");
            try
            {
                var eventObj = JsonConvert.DeserializeObject <Event>(json);
                Console.WriteLine("[MotionEventHandler] Handling eventObj: " + eventObj);

                if (eventObj.category.ToLower() == "user_environment")
                {
                    // if we are dealing with a presence sensor
                    if (eventObj.content.name == "presence")
                    {
                        // see if it is a sensor activation
                        if (eventObj.content.val["alarm_motion"] != null && (bool)eventObj.content.val["alarm_motion"])
                        {
                            // retrieve the gateway and sensor URI from the source annotations
                            var gatewayURIPath = (string)eventObj.annotations.source["gateway"];
                            var deviceURIPath  = (string)eventObj.annotations.source["sensor"];

                            // make a call to the store API to get the user from the gateway
                            var userURIPath = storeAPI.GetUserOfGateway(gatewayURIPath);

                            if (userURIPath != null)
                            {
                                int userID = GetIdFromURI(userURIPath);
                                Tuple <string, string> userLocales = storeAPI.GetUserLocale(userURIPath, userID);

                                if (userLocales != null)
                                {
                                    // retrieve timestamp from annotations
                                    long timestamp = eventObj.annotations.timestamp;

                                    // get localized datetime
                                    TimeZoneInfo localTz     = TimeZoneInfo.FindSystemTimeZoneById(userLocales.Item2);
                                    DateTime     dtime       = UnixTimeStampToDateTime(timestamp, localTz);
                                    DateTime     currentTime = UnixTimeStampToDateTime((long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds, localTz);

                                    // get morning limits
                                    Tuple <DateTime, DateTime> morningLimits = getMorningLimits(localTz);

                                    // check if current dtime is within limits
                                    if (dtime >= morningLimits.Item1 && currentTime >= morningLimits.Item1 && dtime <= morningLimits.Item2 && currentTime <= morningLimits.Item2)
                                    //if (dtime >= morningLimits.Item1 && dtime <= morningLimits.Item2)
                                    {
                                        if (lastActivationMap.ContainsKey(userURIPath))
                                        {
                                            long     lastTs = lastActivationMap[userURIPath];
                                            DateTime lastDt = UnixTimeStampToDateTime(lastTs, localTz);

                                            // if the lastDt is outside of the morning limits
                                            if (lastDt < morningLimits.Item1)
                                            {
                                                Console.WriteLine("[MotionEventHandler] Identified first activation of motion sensor in morning at : " + dtime.ToString());
                                                SendBPMeasurementNotification(userURIPath);
                                            }

                                            lastActivationMap[userURIPath] = timestamp;
                                        }
                                        else
                                        {
                                            // if this is the first activation ever within morning limits
                                            Console.WriteLine("[MotionEventHandler] First ever activation of motion sensor in morning at : " + dtime.ToString());
                                            SendBPMeasurementNotification(userURIPath);

                                            lastActivationMap[userURIPath] = timestamp;
                                        }
                                    }
                                    else
                                    {
                                        // just mark as latest activation
                                        Console.WriteLine("[MotionEventHandler] Motion event not within morning limits " + dtime.ToString());
                                        lastActivationMap[userURIPath] = timestamp;
                                    }
                                }
                                else
                                {
                                    Console.WriteLine("[MotionEventHandler] Insufficient locales information in enduser profile endpoint for user: "******"[MotionEventHandler] Skipping motion event handling since no user can be retrieved for gateway: " + gatewayURIPath);
                            }
                        }
                        else
                        {
                            Console.WriteLine("[MotionEventHandler] The motion sensor has not been triggered: " + eventObj.content.val["alarm_motion"]);
                        }
                    }
                }
            }
            catch (JsonException ex)
            {
                Console.WriteLine(ex);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }