public void Should_TurnOn_IfMotionDetected()
        {
            var testController = new TestController();
            var adapter        = new TestMotionDetectorAdapter();
            var motionDetector = new MotionDetector(
                "Test",
                adapter,
                testController.GetInstance <ISchedulerService>(),
                testController.GetInstance <ISettingsService>());

            var automation = new TurnOnAndOffAutomation(
                "Test",
                testController.GetInstance <IDateTimeService>(),
                testController.GetInstance <ISchedulerService>(),
                testController.GetInstance <ISettingsService>(),
                testController.GetInstance <IDaylightService>());

            var output = new Lamp("Test", new TestLampAdapter());

            Assert.AreEqual(true, output.GetState().Has(PowerState.Off));

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

            adapter.Invoke();

            Assert.AreEqual(true, output.GetState().Has(PowerState.On));
        }
        public void Should_NotTurnOn_IfMotionDetected_AndTimeRangeConditionIs_NotFulfilled()
        {
            var testController = new TestController();

            testController.SetTime(TimeSpan.Parse("18:00:00"));
            var adapter        = new TestMotionDetectorAdapter();
            var motionDetector = new MotionDetector(
                "Test",
                adapter,
                testController.GetInstance <ISchedulerService>(),
                testController.GetInstance <ISettingsService>());

            var automation = new TurnOnAndOffAutomation(
                "Test",
                testController.GetInstance <IDateTimeService>(),
                testController.GetInstance <ISchedulerService>(),
                testController.GetInstance <ISettingsService>(),
                testController.GetInstance <IDaylightService>());

            var output = new Lamp("Test", new TestLampAdapter());

            Assert.AreEqual(true, output.GetState().Has(PowerState.Off));

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

            adapter.Invoke();

            Assert.AreEqual(true, output.GetState().Has(PowerState.Off));
        }
        public void CombinedComponent_TurnOnAndOff()
        {
            var lamp1 = new Lamp("Lamp1", new TestLampAdapter());
            var lamp2 = new Lamp("Lamp2", new TestLampAdapter());
            var lamp3 = new Lamp("Lamp3", new TestLampAdapter());

            var logicalComponent = new LogicalComponent("Test");

            logicalComponent.WithComponent(lamp1);
            logicalComponent.WithComponent(lamp2);
            logicalComponent.WithComponent(lamp3);

            logicalComponent.ExecuteCommand(new TurnOffCommand());
            Assert.IsTrue(logicalComponent.GetState().Has(PowerState.Off));
            Assert.IsTrue(lamp1.GetState().Has(PowerState.Off));
            Assert.IsTrue(lamp2.GetState().Has(PowerState.Off));
            Assert.IsTrue(lamp3.GetState().Has(PowerState.Off));

            logicalComponent.ExecuteCommand(new TurnOnCommand());
            Assert.IsTrue(logicalComponent.GetState().Has(PowerState.On));
            Assert.IsTrue(lamp1.GetState().Has(PowerState.On));
            Assert.IsTrue(lamp2.GetState().Has(PowerState.On));
            Assert.IsTrue(lamp3.GetState().Has(PowerState.On));

            logicalComponent.ExecuteCommand(new TurnOffCommand());
            Assert.IsTrue(logicalComponent.GetState().Has(PowerState.Off));
            Assert.IsTrue(lamp1.GetState().Has(PowerState.Off));
            Assert.IsTrue(lamp2.GetState().Has(PowerState.Off));
            Assert.IsTrue(lamp3.GetState().Has(PowerState.Off));
        }
        public void Should_TurnOn_IfButtonPressed_EvenIfTimeRangeConditionIs_NotFulfilled()
        {
            var testController = new TestController();

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

            var automation = new TurnOnAndOffAutomation(
                "Test",
                testController.GetInstance <IDateTimeService>(),
                testController.GetInstance <ISchedulerService>(),
                testController.GetInstance <ISettingsService>(),
                testController.GetInstance <IDaylightService>());

            var buttonAdapter = new TestButtonAdapter();
            var button        = new Button("Test", buttonAdapter, testController.GetInstance <ITimerService>(), testController.GetInstance <ISettingsService>());
            var output        = new Lamp("Test", new TestLampAdapter());

            Assert.AreEqual(true, output.GetState().Has(PowerState.Off));

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

            buttonAdapter.Touch();

            Assert.AreEqual(true, output.GetState().Has(PowerState.On));
        }
        public void Should_TurnOn_IfButtonPressedShort()
        {
            var testController = new TestController();

            var automation = new TurnOnAndOffAutomation(
                "Test",
                testController.GetInstance <IDateTimeService>(),
                testController.GetInstance <ISchedulerService>(),
                testController.GetInstance <ISettingsService>(),
                testController.GetInstance <IDaylightService>(),
                testController.GetInstance <IMessageBrokerService>());

            var buttonAdapter = new TestButtonAdapter();
            var button        = new Button("Test", buttonAdapter, testController.GetInstance <ITimerService>(), testController.GetInstance <ISettingsService>(), testController.GetInstance <IMessageBrokerService>(), testController.GetInstance <ILogService>());
            var output        = new Lamp("Test", new TestLampAdapter());

            Assert.AreEqual(true, output.GetState().Has(PowerState.Off));

            automation.WithTrigger(button.CreatePressedShortTrigger(testController.GetInstance <IMessageBrokerService>()));
            automation.WithTarget(output);

            buttonAdapter.Touch();

            Assert.AreEqual(true, output.GetState().Has(PowerState.On));
        }
예제 #6
0
        public void Trigger_AttachAction()
        {
            var c = new TestController();

            var buttonAdapter = new TestButtonAdapter();
            var button        = new Button("Test", buttonAdapter, c.GetInstance <ITimerService>(), c.GetInstance <ISettingsService>(), c.GetInstance <IMessageBrokerService>(), c.GetInstance <ILogService>());
            var lamp          = new Lamp("Test", new TestLampAdapter());

            button.CreatePressedShortTrigger(c.GetInstance <IMessageBrokerService>()).Attach(() => lamp.TryTogglePowerState());

            lamp.GetState().Has(PowerState.Off);
            buttonAdapter.Touch();
            lamp.GetState().Has(PowerState.On);
            buttonAdapter.Touch();
            lamp.GetState().Has(PowerState.Off);
        }
예제 #7
0
        public void StateAlias_Lamp()
        {
            var endpoint = new TestBinaryStateEndpoint();
            var lamp     = new Lamp(ComponentIdFactory.EmptyId, endpoint);

            lamp.SetStateIdAlias(BinaryStateId.On, LevelStateId.Level1);

            endpoint.TurnOnCalledCount.ShouldBeEquivalentTo(0);
            endpoint.TurnOffCalledCount.ShouldBeEquivalentTo(1);
            lamp.GetState().ShouldBeEquivalentTo(BinaryStateId.Off);

            lamp.SetState(LevelStateId.Level1);
            endpoint.TurnOnCalledCount.ShouldBeEquivalentTo(1);
            endpoint.TurnOffCalledCount.ShouldBeEquivalentTo(1);
            lamp.GetState().ShouldBeEquivalentTo(BinaryStateId.On);
        }
예제 #8
0
        public void Lamp_TurnOn()
        {
            var adapter = new TestLampAdapter();
            var lamp    = new Lamp("Test", adapter);

            lamp.ResetState();

            Assert.AreEqual(0, adapter.TurnOnCalledCount);
            Assert.AreEqual(1, adapter.TurnOffCalledCount);
            Assert.AreEqual(true, lamp.GetState().Has(PowerState.Off));

            lamp.TryTurnOn();

            Assert.AreEqual(1, adapter.TurnOnCalledCount);
            Assert.AreEqual(1, adapter.TurnOffCalledCount);
            Assert.AreEqual(true, lamp.GetState().Has(PowerState.On));
        }
        public void Should_NotTurnOn_IfMotionDetected_AndSkipConditionIs_Fulfilled()
        {
            var testController = new TestController();

            testController.SetTime(TimeSpan.Parse("14:00:00"));
            var adapter        = new TestMotionDetectorAdapter();
            var motionDetector = new MotionDetector(
                "Test",
                adapter,
                testController.GetInstance <ISchedulerService>(),
                testController.GetInstance <ISettingsService>(),
                testController.GetInstance <IMessageBrokerService>());

            var automation = new TurnOnAndOffAutomation(
                "Test",
                testController.GetInstance <IDateTimeService>(),
                testController.GetInstance <ISchedulerService>(),
                testController.GetInstance <ISettingsService>(),
                testController.GetInstance <IDaylightService>(),
                testController.GetInstance <IMessageBrokerService>());

            var output = new Lamp("Test", new TestLampAdapter());

            Assert.AreEqual(true, output.GetState().Has(PowerState.Off));

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

            var other2 = new Lamp("Test", new TestLampAdapter());

            other2.TryTurnOn();

            IComponent[] otherActuators =
            {
                new Lamp("Test", new TestLampAdapter()),
                other2
            };

            automation.WithSkipIfAnyIsAlreadyOn(otherActuators);

            adapter.Invoke();

            Assert.AreEqual(true, output.GetState().Has(PowerState.Off));
        }
예제 #10
0
        public void Toggle_Lamp()
        {
            var endpoint = new TestBinaryStateEndpoint();
            var lamp     = new Lamp(ComponentIdFactory.EmptyId, endpoint);

            endpoint.TurnOnCalledCount.ShouldBeEquivalentTo(0);
            endpoint.TurnOffCalledCount.ShouldBeEquivalentTo(1);
            lamp.GetState().ShouldBeEquivalentTo(BinaryStateId.Off);

            lamp.SetNextState();
            endpoint.TurnOnCalledCount.ShouldBeEquivalentTo(1);
            endpoint.TurnOffCalledCount.ShouldBeEquivalentTo(1);
            lamp.GetState().ShouldBeEquivalentTo(BinaryStateId.On);

            lamp.SetNextState();
            endpoint.TurnOnCalledCount.ShouldBeEquivalentTo(1);
            endpoint.TurnOffCalledCount.ShouldBeEquivalentTo(2);
            lamp.GetState().ShouldBeEquivalentTo(BinaryStateId.Off);
        }
        public void Empty_ConditionalOnAutomation()
        {
            var c = new TestController();

            var automation = new ConditionalOnAutomation("Test",
                                                         c.GetInstance <ISchedulerService>(),
                                                         c.GetInstance <IDateTimeService>(),
                                                         c.GetInstance <IDaylightService>());

            var buttonAdapter = new TestButtonAdapter();
            var button        = new Button("Test", buttonAdapter, c.GetInstance <ITimerService>(), c.GetInstance <ISettingsService>(), c.GetInstance <IMessageBrokerService>(), c.GetInstance <ILogService>());
            var testOutput    = new Lamp("Test", new TestLampAdapter());

            automation.WithTrigger(button.CreatePressedShortTrigger(c.GetInstance <IMessageBrokerService>()));
            automation.WithComponent(testOutput);

            Assert.IsTrue(testOutput.GetState().Has(PowerState.Off));
            buttonAdapter.Touch();
            Assert.IsTrue(testOutput.GetState().Has(PowerState.On));
        }
예제 #12
0
        public void Automation_Toggle()
        {
            var testController = new TestController();

            var buttonAdapter = new TestButtonAdapter();
            var button        = new Button("Test", buttonAdapter, testController.GetInstance <ITimerService>(), testController.GetInstance <ISettingsService>());
            var testOutput    = new Lamp("Test", new TestLampAdapter());

            new Automation("Test")
            .WithTrigger(button.PressedShortTrigger)
            .WithActionIfConditionsFulfilled(() => testOutput.TryTogglePowerState());

            Assert.IsTrue(testOutput.GetState().Has(PowerState.Off));
            buttonAdapter.Touch();
            Assert.IsTrue(testOutput.GetState().Has(PowerState.On));
            buttonAdapter.Touch();
            Assert.IsTrue(testOutput.GetState().Has(PowerState.Off));
            buttonAdapter.Touch();
            Assert.IsTrue(testOutput.GetState().Has(PowerState.On));
        }
        public void Should_TurnOff_IfButtonPressed_WhileTargetIsAlreadyOn()
        {
            var testController = new TestController();

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

            var automation = new TurnOnAndOffAutomation(
                "Test",
                testController.GetInstance <IDateTimeService>(),
                testController.GetInstance <ISchedulerService>(),
                testController.GetInstance <ISettingsService>(),
                testController.GetInstance <IDaylightService>(),
                testController.GetInstance <IMessageBrokerService>());

            var buttonAdapter = new TestButtonAdapter();
            var button        = new Button("Test", buttonAdapter, testController.GetInstance <ITimerService>(), testController.GetInstance <ISettingsService>(), testController.GetInstance <IMessageBrokerService>(), testController.GetInstance <ILogService>());
            var output        = new Lamp("Test", new TestLampAdapter());

            Assert.AreEqual(true, output.GetState().Has(PowerState.Off));

            automation.WithTrigger(button.CreatePressedShortTrigger(testController.GetInstance <IMessageBrokerService>()));
            automation.WithTarget(output);

            IComponent[] otherActuators =
            {
                new Lamp("Test", new TestLampAdapter()),
                new Lamp("Test", new TestLampAdapter())
            };

            automation.WithSkipIfAnyIsAlreadyOn(otherActuators);

            buttonAdapter.Touch();
            Assert.AreEqual(true, output.GetState().Has(PowerState.On));

            buttonAdapter.Touch();
            Assert.AreEqual(true, output.GetState().Has(PowerState.On));

            automation.WithTurnOffIfButtonPressedWhileAlreadyOn();
            buttonAdapter.Touch();
            Assert.AreEqual(true, output.GetState().Has(PowerState.Off));
        }
예제 #14
0
        public void API_InvokeCommand()
        {
            var testController = new TestController();
            var socket         = new Lamp("Test", new TestLampAdapter());

            socket.ResetState();
            testController.AddComponent(socket);

            Assert.IsTrue(socket.GetState().Has(PowerState.Off));

            var parameter = new JObject
            {
                ["ComponentId"] = "Test",
                ["CommandType"] = "TurnOnCommand"
            };

            var apiContext = testController.InvokeApi("Service/IComponentRegistryService/ExecuteCommand", parameter);

            Assert.AreEqual(ApiResultCode.Success, apiContext.ResultCode);
            Assert.IsTrue(socket.GetState().Has(PowerState.On));
        }
예제 #15
0
        public void Automation_WithCondition()
        {
            var testController = new TestController();

            var buttonAdapter = new TestButtonAdapter();
            var button        = new Button("Test", buttonAdapter, testController.GetInstance <ITimerService>(), testController.GetInstance <ISettingsService>());
            var testOutput    = new Lamp("Test", new TestLampAdapter());

            new Automation("Test")
            .WithTrigger(button.PressedShortTrigger)
            .WithCondition(ConditionRelation.And, new TimeRangeCondition(testController.GetInstance <IDateTimeService>()).WithStart(TimeSpan.FromHours(1)).WithEnd(TimeSpan.FromHours(2)))
            .WithActionIfConditionsFulfilled(() => testOutput.TryTogglePowerState());

            Assert.IsTrue(testOutput.GetState().Has(PowerState.Off));
            testController.SetTime(TimeSpan.FromHours(0));
            buttonAdapter.Touch();
            Assert.IsTrue(testOutput.GetState().Has(PowerState.Off));

            testController.SetTime(TimeSpan.FromHours(1.5));
            buttonAdapter.Touch();
            Assert.IsTrue(testOutput.GetState().Has(PowerState.On));
        }