public bool TryGetValue(object key, out ISmartLight value)
 {
     if (key is short)
     {
         Lyte lyte;
         bool result = lights.TryGetValue((short)key, out lyte);
         value = (ISmartLight)lyte;
         return result;
     }
     else
     {
         throw new Exception("Key needs to be a short for this controller");
     }
 }
Exemple #2
0
 public void UpdateLight(ISmartLight light)
 {
     UpdateLight(light.Id, light.State);
 }
 public void Add(object key, ISmartLight value)
 {
     if (key is short && value is Lyte)
     {
         ((Lyte)value).OnCommandReady += Lyte_OnCommandReady;
         lights.Add((short)key, (Lyte)value);
     }
     else
     {
         throw new Exception("Key needs to be a short and value needs to be a Lyte for this controller");
     }
 }
Exemple #4
0
        private void UpdateLoop()
        {
            Dictionary <string, ISmartSensor> prevSensorState = GetSensorState();

            IList <ISmartLight> prevLightState = _smartHub.PollLights();
            IList <ISmartLight> currLightState = prevLightState;

            HomeLogger.WriteLine("Pulled initial sensor and light states");

            while (_active)
            {
                Dictionary <string, ISmartSensor> currSensorState = GetSensorState();

                if (_lightRefreshCounter > 5)
                {
                    currLightState = _smartHub.PollLights();

                    bool externalChangeDetected = false;

                    for (var i = 0; i < currLightState.Count; i++)
                    {
                        ISmartLight currLight = currLightState[i];
                        ISmartLight prevLight = prevLightState[i];

                        // Light has changed outside of orchestration
                        if (currLight.State.On != prevLight.State.On ||
                            Math.Abs(currLight.State.Brightness - prevLight.State.Brightness) > 10)
                        {
                            _externalInputMap[_lightRoomMap[currLight.Id]] = DateTime.Now;
                            externalChangeDetected = true;
                        }
                    }

                    if (externalChangeDetected)
                    {
                        HomeLogger.WriteLine("External change detected", ConsoleColor.Yellow);
                    }

                    _lightRefreshCounter = 0;
                }

                int currentTime = DateTime.Now.Hour;

                foreach (Rule rule in _rules)
                {
                    int satisfiedRules = 0;

                    // Iterate over each condition and check if they are satisifed
                    foreach (RuleCondition condition in rule.Conditions)
                    {
                        switch (condition.Type)
                        {
                        case RuleConditionType.TimeRange:
                            if (condition.TimeRange.Start <= currentTime &&
                                condition.TimeRange.End >= currentTime)
                            {
                                satisfiedRules++;
                            }
                            break;

                        case RuleConditionType.MotionDetection:
                            if (!prevSensorState[condition.RoomId].DetectMotion &&
                                currSensorState[condition.RoomId].DetectMotion)
                            {
                                satisfiedRules++;
                            }
                            break;

                        case RuleConditionType.NoMotionDetection:
                            TimeSpan timeSinceActivity = DateTime.Now - _activityMap[condition.RoomId];

                            if (timeSinceActivity.TotalSeconds > condition.DurationInSeconds)
                            {
                                satisfiedRules++;
                            }
                            break;

                        case RuleConditionType.LightState:
                            foreach (ISmartLight light in currLightState)
                            {
                                if (_lightRoomMap[light.Id] == condition.RoomId &&
                                    light.State.On == condition.LightState.On)
                                {
                                    satisfiedRules++;
                                    break;
                                }
                            }
                            break;

                        case RuleConditionType.NoExternalInput:

                            break;
                        }
                    }

                    // All rules were satisfied, run the actions
                    if (satisfiedRules == rule.Conditions.Length)
                    {
                        foreach (RuleAction ruleAction in rule.Actions)
                        {
                            HomeLogger.WriteLine("Executing rule " + rule.Name,
                                                 ruleAction.LightState.On ? ConsoleColor.Green : ConsoleColor.Red);

                            if (ruleAction.RoomIds != null)
                            {
                                foreach (string roomId in ruleAction.RoomIds)
                                {
                                    _smartHub.UpdateLightsInRooms(new[] { roomId }, ruleAction.LightState);
                                }
                            }
                        }

                        // Force an update to light states
                        currLightState = _smartHub.PollLights();
                    }
                }

                _lightRefreshCounter++;

                prevSensorState = currSensorState;
                prevLightState  = currLightState;

                Thread.Sleep(500);
            }
        }