Exemple #1
0
        public void Should_TurnOff_IfButtonPressed_WhileTargetIsAlreadyOn()
        {
            var timer = new TestHomeAutomationTimer();

            timer.SetTime(TimeSpan.Parse("14:00:00"));

            var buttonFactory       = new TestButtonFactory(timer);
            var stateMachineFactory = new TestStateMachineFactory();

            var automation = new TurnOnAndOffAutomation(AutomationIdFactory.EmptyId, timer);
            var button     = buttonFactory.CreateTestButton();

            var output = stateMachineFactory.CreateTestStateMachineWithOnOffStates();

            output.GetState().ShouldBeEquivalentTo(BinaryStateId.Off);

            automation.WithTrigger(button.GetPressedShortlyTrigger());
            automation.WithTarget(output);

            IStateMachine[] otherActuators =
            {
                stateMachineFactory.CreateTestStateMachineWithOnOffStates(),
                stateMachineFactory.CreateTestStateMachineWithOnOffStates()
            };

            automation.WithSkipIfAnyActuatorIsAlreadyOn(otherActuators);

            button.PressShortly();
            output.GetState().ShouldBeEquivalentTo(BinaryStateId.On);

            button.PressShortly();
            output.GetState().ShouldBeEquivalentTo(BinaryStateId.On);

            automation.WithTurnOffIfButtonPressedWhileAlreadyOn();
            button.PressShortly();
            output.GetState().ShouldBeEquivalentTo(BinaryStateId.Off);
        }
        public void Associate_TriggerWithActuatorAction()
        {
            var buttonFactory       = new TestButtonFactory(new TestHomeAutomationTimer());
            var stateMachineFactory = new TestStateMachineFactory();

            var testButton = buttonFactory.CreateTestButton();
            var testOutput = stateMachineFactory.CreateTestStateMachineWithOnOffStates();

            testButton.GetPressedShortlyTrigger().Attach(testOutput.GetSetNextStateAction());

            testOutput.GetState().ShouldBeEquivalentTo(BinaryStateId.Off);
            testButton.PressShortly();
            testOutput.GetState().ShouldBeEquivalentTo(BinaryStateId.On);
            testButton.PressShortly();
            testOutput.GetState().ShouldBeEquivalentTo(BinaryStateId.Off);
        }
Exemple #3
0
        public void Empty_ConditionalOnAutomation()
        {
            var testController = new TestController();
            var automation     = new ConditionalOnAutomation(AutomationIdFactory.EmptyId, testController.Timer);

            var testButtonFactory       = new TestButtonFactory(testController.Timer);
            var testStateMachineFactory = new TestStateMachineFactory();

            var testButton = testButtonFactory.CreateTestButton();
            var testOutput = testStateMachineFactory.CreateTestStateMachineWithOnOffStates();

            automation.WithTrigger(testButton.GetPressedShortlyTrigger());
            automation.WithActuator(testOutput);

            testOutput.GetState().ShouldBeEquivalentTo(BinaryStateId.Off);
            testButton.PressShortly();
            testOutput.GetState().ShouldBeEquivalentTo(BinaryStateId.On);
        }
Exemple #4
0
        public void Should_TurnOn_IfButtonPressedShort()
        {
            var timer               = new TestTimerService();
            var buttonFactory       = new TestButtonFactory(timer, new SettingsService(new BackupService(), new StorageService()));
            var stateMachineFactory = new TestStateMachineFactory();

            var automation = new TurnOnAndOffAutomation(AutomationIdGenerator.EmptyId, new TestDateTimeService(), new SchedulerService(new TestTimerService(), new DateTimeService()), new SettingsService(new BackupService(), new StorageService()), new TestDaylightService());
            var button     = buttonFactory.CreateTestButton();
            var output     = stateMachineFactory.CreateTestStateMachineWithOnOffStates();

            output.GetState().ShouldBeEquivalentTo(BinaryStateId.Off);

            automation.WithTrigger(button.GetPressedShortlyTrigger());
            automation.WithTarget(output);

            button.PressShortly();

            output.GetState().ShouldBeEquivalentTo(BinaryStateId.On);
        }
Exemple #5
0
        public void Should_TurnOn_IfMotionDetected()
        {
            var schedulerService      = new SchedulerService(new TestTimerService(), new DateTimeService());
            var motionDetectorFactory = new TestMotionDetectorFactory(schedulerService, new SettingsService(new BackupService(), new StorageService()));
            var stateMachineFactory   = new TestStateMachineFactory();

            var automation     = new TurnOnAndOffAutomation(AutomationIdGenerator.EmptyId, new TestDateTimeService(), schedulerService, new SettingsService(new BackupService(), new StorageService()), new TestDaylightService());
            var motionDetector = motionDetectorFactory.CreateTestMotionDetector();
            var output         = stateMachineFactory.CreateTestStateMachineWithOnOffStates();

            output.GetState().ShouldBeEquivalentTo(BinaryStateId.Off);

            automation.WithTrigger(motionDetector);
            automation.WithTarget(output);

            motionDetector.TriggerMotionDetection();

            output.GetState().ShouldBeEquivalentTo(BinaryStateId.On);
        }
Exemple #6
0
        public void Should_TurnOn_IfButtonPressedShort()
        {
            var timer               = new TestHomeAutomationTimer();
            var buttonFactory       = new TestButtonFactory(timer);
            var stateMachineFactory = new TestStateMachineFactory();

            var automation = new TurnOnAndOffAutomation(AutomationIdFactory.EmptyId, new TestHomeAutomationTimer());
            var button     = buttonFactory.CreateTestButton();
            var output     = stateMachineFactory.CreateTestStateMachineWithOnOffStates();

            output.GetState().ShouldBeEquivalentTo(BinaryStateId.Off);

            automation.WithTrigger(button.GetPressedShortlyTrigger());
            automation.WithTarget(output);

            button.PressShortly();

            output.GetState().ShouldBeEquivalentTo(BinaryStateId.On);
        }
Exemple #7
0
        public void Should_TurnOn_IfMotionDetected()
        {
            var timer = new TestHomeAutomationTimer();
            var motionDetectorFactory = new TestMotionDetectorFactory(timer);
            var stateMachineFactory   = new TestStateMachineFactory();

            var automation     = new TurnOnAndOffAutomation(AutomationIdFactory.EmptyId, timer);
            var motionDetector = motionDetectorFactory.CreateTestMotionDetector();
            var output         = stateMachineFactory.CreateTestStateMachineWithOnOffStates();

            output.GetState().ShouldBeEquivalentTo(BinaryStateId.Off);

            automation.WithTrigger(motionDetector);
            automation.WithTarget(output);

            motionDetector.TriggerMotionDetection();

            output.GetState().ShouldBeEquivalentTo(BinaryStateId.On);
        }
Exemple #8
0
        public void Automation_Toggle()
        {
            var timer                   = new TestHomeAutomationTimer();
            var testButtonFactory       = new TestButtonFactory(timer);
            var testStateMachineFactory = new TestStateMachineFactory();

            var testButton = testButtonFactory.CreateTestButton();
            var testOutput = testStateMachineFactory.CreateTestStateMachineWithOnOffStates();

            CreateAutomation()
            .WithTrigger(testButton.GetPressedShortlyTrigger())
            .WithActionIfConditionsFulfilled(testOutput.GetSetNextStateAction());

            testOutput.GetState().ShouldBeEquivalentTo(BinaryStateId.Off);
            testButton.PressShortly();
            testOutput.GetState().ShouldBeEquivalentTo(BinaryStateId.On);
            testButton.PressShortly();
            testOutput.GetState().ShouldBeEquivalentTo(BinaryStateId.Off);
            testButton.PressShortly();
            testOutput.GetState().ShouldBeEquivalentTo(BinaryStateId.On);
        }
        public void Empty_ConditionalOnAutomation()
        {
            var testController = new TestController();
            var automation     = new ConditionalOnAutomation(AutomationIdGenerator.EmptyId,
                                                             testController.SchedulerService,
                                                             testController.DateTimeService,
                                                             testController.DaylightService);

            var testButtonFactory       = new TestButtonFactory(testController.TimerService, new SettingsService(new BackupService(), new StorageService()));
            var testStateMachineFactory = new TestStateMachineFactory();

            var testButton = testButtonFactory.CreateTestButton();
            var testOutput = testStateMachineFactory.CreateTestStateMachineWithOnOffStates();

            automation.WithTrigger(testButton.GetPressedShortlyTrigger());
            automation.WithActuator(testOutput);

            testOutput.GetState().ShouldBeEquivalentTo(BinaryStateId.Off);
            testButton.PressShortly();
            testOutput.GetState().ShouldBeEquivalentTo(BinaryStateId.On);
        }
Exemple #10
0
        public void Should_TurnOn_IfButtonPressed_EvenIfTimeRangeConditionIs_NotFulfilled()
        {
            var timer = new TestHomeAutomationTimer();

            timer.SetTime(TimeSpan.Parse("18:00:00"));

            var buttonFactory       = new TestButtonFactory(timer);
            var stateMachineFactory = new TestStateMachineFactory();

            var automation = new TurnOnAndOffAutomation(AutomationIdFactory.EmptyId, timer);
            var button     = buttonFactory.CreateTestButton();
            var output     = stateMachineFactory.CreateTestStateMachineWithOnOffStates();

            output.GetState().ShouldBeEquivalentTo(BinaryStateId.Off);

            automation.WithTurnOnWithinTimeRange(() => TimeSpan.Parse("10:00:00"), () => TimeSpan.Parse("15:00:00"));
            automation.WithTrigger(button.GetPressedShortlyTrigger());
            automation.WithTarget(output);

            button.PressShortly();

            output.GetState().ShouldBeEquivalentTo(BinaryStateId.On);
        }
Exemple #11
0
        public void Should_NotTurnOn_IfMotionDetected_AndTimeRangeConditionIs_NotFulfilled()
        {
            var timer = new TestHomeAutomationTimer();

            timer.SetTime(TimeSpan.Parse("18:00:00"));

            var motionDetectorFactory = new TestMotionDetectorFactory(timer);
            var stateMachineFactory   = new TestStateMachineFactory();

            var automation     = new TurnOnAndOffAutomation(AutomationIdFactory.EmptyId, timer);
            var motionDetector = motionDetectorFactory.CreateTestMotionDetector();
            var output         = stateMachineFactory.CreateTestStateMachineWithOnOffStates();

            output.GetState().ShouldBeEquivalentTo(BinaryStateId.Off);

            automation.WithTurnOnWithinTimeRange(() => TimeSpan.Parse("10:00:00"), () => TimeSpan.Parse("15:00:00"));
            automation.WithTrigger(motionDetector);
            automation.WithTarget(output);

            motionDetector.TriggerMotionDetection();

            output.GetState().ShouldBeEquivalentTo(BinaryStateId.Off);
        }
Exemple #12
0
        public void Should_TurnOn_IfButtonPressed_EvenIfTimeRangeConditionIs_NotFulfilled()
        {
            var timer           = new TestTimerService();
            var dateTimeService = new TestDateTimeService();

            dateTimeService.SetTime(TimeSpan.Parse("18:00:00"));

            var buttonFactory       = new TestButtonFactory(timer, new SettingsService(new BackupService(), new StorageService()));
            var stateMachineFactory = new TestStateMachineFactory();

            var automation = new TurnOnAndOffAutomation(AutomationIdGenerator.EmptyId, dateTimeService, new SchedulerService(timer, dateTimeService), new SettingsService(new BackupService(), new StorageService()), new TestDaylightService());
            var button     = buttonFactory.CreateTestButton();
            var output     = stateMachineFactory.CreateTestStateMachineWithOnOffStates();

            output.GetState().ShouldBeEquivalentTo(BinaryStateId.Off);

            automation.WithTurnOnWithinTimeRange(() => TimeSpan.Parse("10:00:00"), () => TimeSpan.Parse("15:00:00"));
            automation.WithTrigger(button.GetPressedShortlyTrigger());
            automation.WithTarget(output);

            button.PressShortly();

            output.GetState().ShouldBeEquivalentTo(BinaryStateId.On);
        }
Exemple #13
0
        public void Should_NotTurnOn_IfMotionDetected_AndTimeRangeConditionIs_NotFulfilled()
        {
            var timer           = new TestTimerService();
            var dateTimeService = new TestDateTimeService();

            dateTimeService.SetTime(TimeSpan.Parse("18:00:00"));

            var motionDetectorFactory = new TestMotionDetectorFactory(new SchedulerService(timer, dateTimeService), new SettingsService(new BackupService(), new StorageService()));
            var stateMachineFactory   = new TestStateMachineFactory();

            var automation     = new TurnOnAndOffAutomation(AutomationIdGenerator.EmptyId, dateTimeService, new SchedulerService(timer, dateTimeService), new SettingsService(new BackupService(), new StorageService()), new TestDaylightService());
            var motionDetector = motionDetectorFactory.CreateTestMotionDetector();
            var output         = stateMachineFactory.CreateTestStateMachineWithOnOffStates();

            output.GetState().ShouldBeEquivalentTo(BinaryStateId.Off);

            automation.WithTurnOnWithinTimeRange(() => TimeSpan.Parse("10:00:00"), () => TimeSpan.Parse("15:00:00"));
            automation.WithTrigger(motionDetector);
            automation.WithTarget(output);

            motionDetector.TriggerMotionDetection();

            output.GetState().ShouldBeEquivalentTo(BinaryStateId.Off);
        }
Exemple #14
0
        public void Automation_WithCondition()
        {
            var testController = new TestController();

            var testButtonFactory       = new TestButtonFactory(testController.TimerService, new SettingsService(new BackupService(), new StorageService()));
            var testStateMachineFactory = new TestStateMachineFactory();

            var testButton = testButtonFactory.CreateTestButton();
            var testOutput = testStateMachineFactory.CreateTestStateMachineWithOnOffStates();

            new Automation(AutomationIdGenerator.EmptyId)
            .WithTrigger(testButton.GetPressedShortlyTrigger())
            .WithCondition(ConditionRelation.And, new TimeRangeCondition(testController.DateTimeService).WithStart(TimeSpan.FromHours(1)).WithEnd(TimeSpan.FromHours(2)))
            .WithActionIfConditionsFulfilled(testOutput.GetSetNextStateAction());

            testOutput.GetState().ShouldBeEquivalentTo(BinaryStateId.Off);
            testController.SetTime(TimeSpan.FromHours(0));
            testButton.PressShortly();
            testOutput.GetState().ShouldBeEquivalentTo(BinaryStateId.Off);

            testController.SetTime(TimeSpan.FromHours(1.5));
            testButton.PressShortly();
            testOutput.GetState().ShouldBeEquivalentTo(BinaryStateId.On);
        }