public async Task ExecuteBuiltinCommandAsyncRepollAllTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var repollDeviceIdRequestSentToAdapter = new List <int>();
            var adapterManager = new StubIAdapterManager
            {
                FindZvsAdapterGuid = adapterGuid => new StubZvsAdapter
                {
                    IsEnabled         = true,
                    RepollAsyncDevice = async d => repollDeviceIdRequestSentToAdapter.Add(d.Id)
                }
            };
            var log = new StubIFeedback <LogEntry>();
            var cts = new CancellationTokenSource();
            var commmandProcessor = new CommandProcessor(adapterManager, dbConnection, log);

            var device  = UnitTesting.CreateFakeDevice();
            var device2 = UnitTesting.CreateFakeDevice();

            using (var context = new ZvsContext(dbConnection))
            {
                context.Devices.Add(device);
                context.Devices.Add(device2);
                var builtinCommand = new BuiltinCommand
                {
                    Name             = "Repoll all",
                    UniqueIdentifier = "REPOLL_ALL"
                };
                context.Commands.Add(builtinCommand);
                await context.SaveChangesAsync(new CancellationToken());

                //Act
                var result = await commmandProcessor.ExecuteBuiltinCommandAsync(builtinCommand, device.Id.ToString(CultureInfo.InvariantCulture), "", cts.Token);

                Console.WriteLine(result.Message);

                //Assert
                Assert.IsFalse(result.HasError);
                Assert.IsTrue(repollDeviceIdRequestSentToAdapter.Count == 2, "Process did not run the correct amount of commands.");
                Assert.IsTrue(repollDeviceIdRequestSentToAdapter[0] == device.Id, "Wrong command processed");
                Assert.IsTrue(repollDeviceIdRequestSentToAdapter[1] == device2.Id, "Wrong command processed");
            }
        }
        public async Task RegisterAsyncUpdatedDeviceValueTest()
        {
            //arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var dvb = new DeviceValueBuilder(dbConnection);

            var device = UnitTesting.CreateFakeDevice();

            using (var context = new ZvsContext(dbConnection))
            {
                context.Devices.Add(device);
                var deviceValue = new DeviceValue
                {
                    UniqueIdentifier = "UNIT_TESTING_VALUE1",
                    CommandClass     = "Command Class 1",
                    CustomData1      = "Custom Data 1",
                    CustomData2      = "Custom Data 2",
                    Description      = "Testing Value Description Here",
                    Name             = "Test Value",
                    ValueType        = DataType.BOOL,
                    Value            = true.ToString(),
                    Genre            = "Genre",
                    Index            = "Index",
                    IsReadOnly       = true
                };
                device.Values.Add(deviceValue);

                await context.SaveChangesAsync();

                deviceValue.Value = false.ToString();

                //act
                var result = await dvb.RegisterAsync(deviceValue, device, CancellationToken.None);

                var dv = await context.DeviceValues.FirstOrDefaultAsync(o => o.Name == deviceValue.Name);


                //assert
                Assert.IsFalse(result.HasError, result.Message);
                Assert.IsNotNull(dv, "Registered device value count not be found in database.");
                Assert.AreEqual(dv.Value, false.ToString(), "Device value not updated properly");
                Console.WriteLine(result.Message);
            }
        }
        public async Task ValueChangeTest()
        {
            //arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var logEntries = new List <LogEntry>();
            var log        = new StubIFeedback <LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e.ToString());
                    logEntries.Add(e);
                    return(Task.FromResult(0));
                }
            };
            var cts = new CancellationTokenSource();
            var tm  = new ChangeListener(log, dbConnection);

            //Act
            await tm.StartAsync(cts.Token);

            var device = UnitTesting.CreateFakeDevice();

            device.Values.Add(new DeviceValue
            {
                UniqueIdentifier = "DeviceTestValue1",
                Value            = "0"
            });

            using (var context = new ZvsContext(dbConnection))
            {
                context.Devices.Add(device);
                await context.SaveChangesAsync(CancellationToken.None);

                device.Values.First().Value = "1";
                await context.SaveChangesAsync(CancellationToken.None);
            }
            await Task.Delay(100, CancellationToken.None);

            await tm.StopAsync(cts.Token);

            //Assert
            Assert.IsTrue(logEntries.Count == 3, "Unexpected number of log entries.");
            Assert.IsTrue(logEntries[1].Message.Contains("changed"), "Log message didn't contain changed.");
        }
Beispiel #4
0
        public async Task RegisterAsyncUpdatedCommandDeviceTypeTest()
        {
            //arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var dtb     = new DeviceTypeBuilder(dbConnection);
            var adapter = UnitTesting.CreateFakeAdapter();
            var dt      = new DeviceType
            {
                AdapterId        = adapter.Id,
                UniqueIdentifier = "UNIT_TEST_DEVICE_TYPE1",
                Name             = "Unit Test Device Type"
            };
            var dtc = new DeviceTypeCommand
            {
                UniqueIdentifier = "DTC1",
                Name             = "Test Device Type Command"
            };

            dt.Commands.Add(dtc);
            adapter.DeviceTypes.Add(dt);
            using (var context = new ZvsContext(dbConnection))
            {
                context.Adapters.Add(adapter);
                await context.SaveChangesAsync();
            }

            dtc.Name = "New DTC Test Name";

            //act
            var result = await dtb.RegisterAsync(adapter.AdapterGuid, dt, CancellationToken.None);

            using (var context = new ZvsContext(dbConnection))
            {
                var dtc1 = context.DeviceTypeCommands.First();

                //assert
                Console.WriteLine(result.Message);
                Assert.IsFalse(result.HasError);
                Assert.IsTrue(dtc1.Name == dtc.Name, "Command did not update");
            }
        }
Beispiel #5
0
        public async Task RegisterAdapterSettingAdapterTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());
            var adapterBuilder = new AdapterSettingBuilder(dbConnection, CancellationToken.None);
            var dbAdapter      = UnitTesting.CreateFakeAdapter();

            using (var context = new ZvsContext(dbConnection))
            {
                context.Adapters.Add(dbAdapter);
                await context.SaveChangesAsync();
            }
            var adapter = new StubUnitTestAdapter
            {
                AdapterGuidGet = () => dbAdapter.AdapterGuid
            };

            var adapterSetting = new AdapterSetting
            {
                Name      = "Adapter Setting 1",
                ValueType = DataType.STRING,
                Value     = "Hello World"
            };

            //act
            var result = await adapterBuilder.Adapter(adapter).RegisterAdapterSettingAsync(adapterSetting, o => o.PropertyTest);

            Adapter a;

            using (var context = new ZvsContext(dbConnection))
            {
                a = await context.Adapters
                    .Include(o => o.Settings)
                    .FirstOrDefaultAsync(o => o.AdapterGuid == dbAdapter.AdapterGuid);
            }

            //assert
            Console.WriteLine(result.Message);
            Assert.IsFalse(result.HasError);
            Assert.IsTrue(a.Settings.Count == 1, "Expected 1 adapter setting");
            Assert.IsTrue(a.Settings[0].Name == adapterSetting.Name, "Adapter setting name mismatch");
        }
        public async Task ExecuteDeviceTypeCommandAsyncOkTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var commandsSendToAdapter = new List <int>();
            var adapterManager        = new StubIAdapterManager
            {
                FindZvsAdapterGuid = adapterGuid => new StubZvsAdapter
                {
                    IsEnabled = true,
                    ProcessDeviceTypeCommandAsyncDeviceTypeDeviceDeviceTypeCommandString = async(adapterDevice, command, argument, argument2) => commandsSendToAdapter.Add(command.Id)
                }
            };

            var log = new StubIFeedback <LogEntry>();
            var cts = new CancellationTokenSource();
            var commmandProcessor = new CommandProcessor(adapterManager, dbConnection, log);

            var device = UnitTesting.CreateFakeDevice();

            using (var context = new ZvsContext(dbConnection))
            {
                var deviceTypeCommand = new DeviceTypeCommand
                {
                    Name = "Turn On"
                };
                device.Type.Commands.Add(deviceTypeCommand);
                context.Devices.Add(device);
                await context.SaveChangesAsync(CancellationToken.None);

                //Act
                var result = await commmandProcessor.ExecuteDeviceTypeCommandAsync(deviceTypeCommand, "1", device.Id.ToString(CultureInfo.InvariantCulture), cts.Token);

                Console.WriteLine(result.Message);

                //Assert
                Assert.IsFalse(result.HasError);
                Assert.IsTrue(commandsSendToAdapter.Count == 1, "Process did not run the correct amount of commands.");
                Assert.IsTrue(commandsSendToAdapter[0] == deviceTypeCommand.Id, "Wrong command processed");
            }
        }
        public async Task RegisterAsyncUpdateCommandTest()
        {
            //arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());
            var dvb = new DeviceCommandBuilder(dbConnection);

            var device        = UnitTesting.CreateFakeDevice();
            var deviceCommand = new DeviceCommand
            {
                Name = "Unit Testing Command"
            };

            using (var context = new ZvsContext(dbConnection))
            {
                device.Commands.Add(deviceCommand);
                context.Devices.Add(device);
                await context.SaveChangesAsync();
            }

            deviceCommand.Name = "New Testing Command";

            //act
            var result = await dvb.RegisterAsync(device.Id, deviceCommand, CancellationToken.None);

            Console.WriteLine(result.Message);

            Device d;

            using (var context = new ZvsContext(dbConnection))
            {
                d = await context.Devices
                    .Include(o => o.Commands)
                    .FirstOrDefaultAsync(o => o.Id == device.Id);
            }

            //assert
            Assert.IsFalse(result.HasError, result.Message);
            Assert.IsNotNull(d, "device not found!");
            Assert.IsTrue(d.Commands.Count == 1, "Device has an unexpected number of commands");
            Assert.IsTrue(d.Commands[0].Name == deviceCommand.Name, "Device command did not save correctly");
        }
        public async Task ExecuteDeviceTypeCommandAsyncAdapterNotEnabledTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var adapterManager = new StubIAdapterManager
            {
                FindZvsAdapterGuid = adapterGuid => new StubZvsAdapter
                {
                    IsEnabled = false,
                }
            };
            var ranstoredCommands = new List <int>();
            var log = new StubIFeedback <LogEntry>();
            var cts = new CancellationTokenSource();
            var commmandProcessor = new CommandProcessor(adapterManager, dbConnection, log);

            var device = UnitTesting.CreateFakeDevice();

            using (var context = new ZvsContext(dbConnection))
            {
                var deviceTypeCommand = new DeviceTypeCommand
                {
                    Name = "Turn On"
                };
                device.Type.Commands.Add(deviceTypeCommand);
                context.Devices.Add(device);
                await context.SaveChangesAsync(CancellationToken.None);

                //Act
                var result = await commmandProcessor.ExecuteDeviceTypeCommandAsync(deviceTypeCommand, "", device.Id.ToString(CultureInfo.InvariantCulture), cts.Token);

                Console.WriteLine(result.Message);

                //Assert
                Assert.IsTrue(result.HasError);
                Assert.IsTrue(ranstoredCommands.Count == 0, "Process did not run the correct amount of commands.");
                Assert.IsTrue(result.Message.Contains("adapter is disabled"), "Expect error message to contain 'adapter is disabled'");
            }
        }
Beispiel #9
0
        public async Task RegisterUpdatedDeviceTypeSettingTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var adapterBuilder    = new AdapterSettingBuilder(dbConnection, CancellationToken.None);
            var deviceType        = UnitTesting.CreateFakeDeviceType();
            var deviceTypeSetting = new DeviceTypeSetting
            {
                Name       = "Device Type Setting 1",
                ValueType  = DataType.STRING,
                Value      = "Hello World",
                DeviceType = deviceType
            };

            using (var context = new ZvsContext(dbConnection))
            {
                context.DeviceTypeSettings.Add(deviceTypeSetting);
                await context.SaveChangesAsync();
            }

            deviceTypeSetting.Value = "New value!";

            //act
            var result = await adapterBuilder.RegisterDeviceTypeSettingAsync(deviceTypeSetting);

            DeviceTypeSetting setting;

            using (var context = new ZvsContext(dbConnection))
            {
                setting = await context.DeviceTypeSettings.FirstOrDefaultAsync();
            }

            //assert
            Console.WriteLine(result.Message);
            Assert.IsFalse(result.HasError);
            Assert.IsNotNull(setting, "Setting not found");
            Assert.IsTrue(setting.Value == "New value!", "Device type setting name mismatch");
        }
        public async Task RunCommandAsyncDeviceCommand()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var adapterManager = new StubIAdapterManager
            {
                FindZvsAdapterGuid = adapterGuid => new StubZvsAdapter
                {
                    IsEnabled = true,
                    ProcessDeviceCommandAsyncDeviceDeviceCommandStringString = (adapterDevice, command, argument, argument2) => Task.FromResult(0)
                }
            };
            var log = new StubIFeedback <LogEntry>();

            var cts = new CancellationTokenSource();
            var commmandProcessor = new CommandProcessor(adapterManager, dbConnection, log);

            var device = UnitTesting.CreateFakeDevice();

            using (var context = new ZvsContext(dbConnection))
            {
                var deviceCommand = new DeviceCommand
                {
                    Name = "Turn On"
                };
                device.Commands.Add(deviceCommand);
                context.Devices.Add(device);
                await context.SaveChangesAsync(CancellationToken.None);

                //Act
                var result = await commmandProcessor.RunCommandAsync(deviceCommand.Id, "", "", cts.Token);

                Console.WriteLine(result.Message);

                //Assert
                Assert.IsFalse(result.HasError);
            }
        }
        public async Task ExecuteBuiltinCommandAsyncRepollDisabledAdapterMeTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var adapterManager = new StubIAdapterManager
            {
                FindZvsAdapterGuid = adapterGuid => new StubZvsAdapter
                {
                    IsEnabled = false,
                }
            };
            var log = new StubIFeedback <LogEntry>();
            var cts = new CancellationTokenSource();
            var commmandProcessor = new CommandProcessor(adapterManager, dbConnection, log);

            var device = UnitTesting.CreateFakeDevice();

            using (var context = new ZvsContext(dbConnection))
            {
                context.Devices.Add(device);
                var builtinCommand = new BuiltinCommand
                {
                    UniqueIdentifier = "REPOLL_ME"
                };
                context.Commands.Add(builtinCommand);
                await context.SaveChangesAsync(new CancellationToken());

                //Act
                var result = await commmandProcessor.ExecuteBuiltinCommandAsync(builtinCommand, device.Id.ToString(CultureInfo.InvariantCulture), "", cts.Token);

                Console.WriteLine(result.Message);

                //Assert
                Assert.IsTrue(result.HasError);
                Assert.IsTrue(result.Message.Contains("adapter is disabled"), "Expect error message to contain 'adapter is disabled'");
            }
        }
        public async Task RegisterAsyncNewDeviceValueTest()
        {
            //arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var dvb = new DeviceValueBuilder(dbConnection);

            var device = UnitTesting.CreateFakeDevice();

            using (var context = new ZvsContext(dbConnection))
            {
                context.Devices.Add(device);
                await context.SaveChangesAsync();

                var deviceValue = new DeviceValue
                {
                    Description = "Testing Value Description Here",
                    Name        = "Test Value",
                    ValueType   = DataType.BOOL,
                    Value       = true.ToString(),
                    DeviceId    = device.Id
                };

                //act
                var result = await dvb.RegisterAsync(deviceValue, device, CancellationToken.None);

                var dv = await context.DeviceValues.FirstOrDefaultAsync(o => o.Name == deviceValue.Name);


                //assert
                Assert.IsFalse(result.HasError, result.Message);
                Assert.IsNotNull(dv, "Registered device value count not be found in database.");
                Console.WriteLine(result.Message);
            }
        }
        public async Task ExecuteBuiltinCommandAsyncSceneTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var deviceCommandIds = new List <int>();
            var adapterManager   = new StubIAdapterManager
            {
                FindZvsAdapterGuid = adapterGuid => new StubZvsAdapter
                {
                    IsEnabled = true,
                    ProcessDeviceCommandAsyncDeviceDeviceCommandStringString = async(adapterDevice, command, argument, argument2) => deviceCommandIds.Add(command.Id)
                }
            };
            var log = new StubIFeedback <LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e);
                    return(Task.FromResult(0));
                }
            };

            var cts = new CancellationTokenSource();
            var commmandProcessor = new CommandProcessor(adapterManager, dbConnection, log);

            var device = UnitTesting.CreateFakeDevice();

            using (var context = new ZvsContext(dbConnection))
            {
                var deviceCommand = new DeviceCommand
                {
                    Name = "Turn On"
                };
                device.Commands.Add(deviceCommand);
                context.Devices.Add(device);

                var scene = new Scene
                {
                    Name = "Test Scene"
                };
                scene.Commands.Add(new SceneStoredCommand
                {
                    Command  = deviceCommand,
                    Argument = "0"
                });
                context.Scenes.Add(scene);

                var builtinCommand = new BuiltinCommand
                {
                    Name             = "Activate Scene",
                    UniqueIdentifier = "RUN_SCENE"
                };
                context.Commands.Add(builtinCommand);
                await context.SaveChangesAsync(new CancellationToken());

                //Act
                var result = await commmandProcessor.ExecuteBuiltinCommandAsync(builtinCommand, scene.Id.ToString(CultureInfo.InvariantCulture), "", cts.Token);

                Console.WriteLine(result.Message);

                //Assert
                Assert.IsFalse(result.HasError);
                Assert.IsTrue(deviceCommandIds.Count == 1, "Process did not run the correct amount of commands.");
                Assert.IsTrue(deviceCommand.Id == deviceCommandIds[0], "Ran the wrong scene!");
            }
        }
        public async Task RegisterAsyncAddedCommandOptionsTest()
        {
            //arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());
            var dvb = new DeviceCommandBuilder(dbConnection);

            var device        = UnitTesting.CreateFakeDevice();
            var deviceCommand = new DeviceCommand
            {
                Name = "Unit Testing Command",
            };
            var option1 = new CommandOption
            {
                Name = "Option 1"
            };
            var option2 = new CommandOption
            {
                Name = "Option 2"
            };

            deviceCommand.Options.Add(option1);
            deviceCommand.Options.Add(option2);

            using (var context = new ZvsContext(dbConnection))
            {
                device.Commands.Add(deviceCommand);
                context.Devices.Add(device);
                await context.SaveChangesAsync();
            }

            var option3 = new CommandOption
            {
                Name = "Option 3"
            };

            deviceCommand.Options.Add(option3);

            //act
            var result = await dvb.RegisterAsync(device.Id, deviceCommand, CancellationToken.None);

            Console.WriteLine(result.Message);

            List <DeviceCommand> deviceCommands;

            using (var context = new ZvsContext(dbConnection))
            {
                deviceCommands = await context.DeviceCommands
                                 .Include(o => o.Options)
                                 .Include(o => o.Device)
                                 .Where(o => o.Id == deviceCommand.Id)
                                 .ToListAsync();
            }

            //assert
            Assert.IsFalse(result.HasError, result.Message);
            Assert.IsTrue(deviceCommands.Count == 1, "Device has an unexpected number of commands");
            Assert.IsTrue(deviceCommands[0].Options.Count == 3, "Option not removed as expected");
            Assert.IsTrue(deviceCommands[0].Options[2].Name == option3.Name, "Option mismatch");
        }
Beispiel #15
0
        public async Task ContraLessThanTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var logEntries        = new List <LogEntry>();
            var ranstoredCommands = new List <int>();
            var log = new StubIFeedback <LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e.ToString());
                    logEntries.Add(e);
                    return(Task.FromResult(0));
                }
            };

            var commandProcessor = new StubICommandProcessor
            {
                RunCommandAsyncNullableOfInt32StringStringCancellationToken = (commandId, argument, argument2, cancellationToken) =>
                {
                    if (commandId.HasValue)
                    {
                        ranstoredCommands.Add(commandId.Value);
                    }
                    return(Task.FromResult(Result.ReportSuccess()));
                }
            };


            Database.SetInitializer(new CreateFreshDbInitializer());

            var cts            = new CancellationTokenSource();
            var triggerManager = new TriggerRunner(log, commandProcessor, dbConnection);
            await triggerManager.StartAsync(cts.Token);

            var cmd = new Command();
            var dv  = new DeviceValue
            {
                Value     = "first value",
                ValueType = DataType.STRING,
                Triggers  = new ObservableCollection <DeviceValueTrigger> {
                    new DeviceValueTrigger
                    {
                        Name      = "trigger1",
                        IsEnabled = true,
                        Operator  = TriggerOperator.LessThan,
                        Value     = "1",
                        Command   = cmd
                    }
                }
            };

            var device = UnitTesting.CreateFakeDevice();

            device.Values.Add(dv);
            using (var context = new ZvsContext(dbConnection))
            {
                context.Devices.Add(device);
                var r = await context.TrySaveChangesAsync(cts.Token);

                Assert.IsFalse(r.HasError, r.Message);
                dv.Value = "3";

                //Act
                var r2 = await context.TrySaveChangesAsync(cts.Token);

                Assert.IsFalse(r2.HasError, r2.Message);
            }
            await Task.Delay(700, cts.Token);

            await triggerManager.StopAsync(cts.Token);

            //Assert
            Assert.IsTrue(logEntries.All(o => o.Level != LogEntryLevel.Error), "Expected no error log entries");
            Assert.IsTrue(ranstoredCommands.Count == 0, "Trigger runner did not run the correct amount of commands.");
        }
Beispiel #16
0
        public async Task QuickFireTriggerTest()
        {
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var logEntries        = new List <LogEntry>();
            var ranstoredCommands = new List <int>();

            //Arrange
            var log = new StubIFeedback <LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e.ToString());
                    logEntries.Add(e);
                    return(Task.FromResult(0));
                }
            };

            var commandProcessor = new StubICommandProcessor
            {
                RunCommandAsyncNullableOfInt32StringStringCancellationToken = (commandId, argument, argument2, cancellationToken) =>
                {
                    if (commandId.HasValue)
                    {
                        ranstoredCommands.Add(commandId.Value);
                    }
                    return(Task.FromResult(Result.ReportSuccess()));
                }
            };

            var cts            = new CancellationTokenSource();
            var triggerManager = new TriggerRunner(log, commandProcessor, dbConnection);
            await triggerManager.StartAsync(cts.Token);

            var cmd = new Command();
            var dv  = new DeviceValue
            {
                Value     = "first value",
                ValueType = DataType.STRING,
                Triggers  = new ObservableCollection <DeviceValueTrigger> {
                    new DeviceValueTrigger
                    {
                        Name      = "trigger1",
                        IsEnabled = true,
                        Operator  = TriggerOperator.EqualTo,
                        Value     = "some unique value",
                        Command   = cmd
                    }
                }
            };

            var device = UnitTesting.CreateFakeDevice();

            device.Values.Add(dv);

            //Act
            using (var context = new ZvsContext(dbConnection))
            {
                context.Devices.Add(device);
                await context.TrySaveChangesAsync(cts.Token);

                dv.Value = "Not It!";
                await context.TrySaveChangesAsync(cts.Token);

                dv.Value = "not this one";
                await context.TrySaveChangesAsync(cts.Token);

                dv.Value = "some unique value";
                await context.TrySaveChangesAsync(cts.Token);

                dv.Value = "not it";
                await context.TrySaveChangesAsync(cts.Token);

                dv.Value = "some unique value";
                await context.TrySaveChangesAsync(cts.Token);

                Console.WriteLine(context.DeviceValueTriggers.Count());
            }

            await Task.Delay(700, cts.Token);

            await triggerManager.StopAsync(cts.Token);

            //Assert
            Assert.IsTrue(logEntries.All(o => o.Level == LogEntryLevel.Info), "Expected only info type log entries");
            Assert.IsTrue(ranstoredCommands.Count == 2, "Trigger runner did not run the correct amount of commands.");
            Assert.IsTrue(ranstoredCommands.All(o => o == cmd.Id), "Scheduled task runner did not run the correct command.");
        }
        public async Task RunCommandTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            int?commandIdRan = 0;
            var arg1Ran      = "";
            var arg2Ran      = "";

            var commandProcessor = new StubICommandProcessor
            {
                RunCommandAsyncNullableOfInt32StringStringCancellationToken = (commandId, argument, argument2, cancellationToken) =>
                {
                    commandIdRan = commandId;
                    arg1Ran      = argument;
                    arg2Ran      = argument2;
                    return(Task.FromResult(Result.ReportSuccess()));
                }
            };
            var log = new StubIFeedback <LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e);
                    return(Task.FromResult(0));
                }
            };

            var device0 = UnitTesting.CreateFakeDevice();
            var device  = UnitTesting.CreateFakeDevice();

            using (var context = new ZvsContext(dbConnection))
            {
                var deviceCommand = new DeviceCommand
                {
                    Name = "Turn On"
                };
                device.Commands.Add(deviceCommand);
                context.Devices.Add(device0);
                context.Devices.Add(device);
                await context.SaveChangesAsync(CancellationToken.None);

                var cts    = new CancellationTokenSource();
                var runner = new JavaScriptRunner(log, commandProcessor, dbConnection);
                var script =
                    $@"
function f1() {{ 
       var result = runCommand({deviceCommand.Id
                        },'98', '0'); 
       logInfo(result.Message);
}};
f1();";

                //Act
                var result = await runner.ExecuteScriptAsync(script, cts.Token);

                //Assert
                Assert.IsFalse(result.HasError, result.Message);
                Assert.IsTrue(deviceCommand.Id == commandIdRan, "Wrong command ran!");
                Assert.IsTrue("98" == arg1Ran, "command argument1 not passed in correctly.");
                Assert.IsTrue("0" == arg2Ran, "command argument2 not passed in correctly.");
            }
        }