Ejemplo n.º 1
0
        public async Task RegisterAsyncRemvoedCommandOptionDeviceTypeTest()
        {
            //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"
            };
            var option1 = new CommandOption
            {
                Name = "Option 1"
            };
            var option2 = new CommandOption
            {
                Name = "Option 2"
            };

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

            dtc.Options.Remove(option1);

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

            using (var context = new ZvsContext(dbConnection))
            {
                var dtc1 = context.DeviceTypeCommands
                           .Include(o => o.Options)
                           .First();

                //assert
                Console.WriteLine(result.Message);
                Assert.IsFalse(result.HasError);
                Assert.IsTrue(dtc1.Options.Count == 1, "Expected 2 options");
            }
        }
Ejemplo n.º 2
0
        public override async Task ProcessDeviceTypeCommandAsync(DeviceType deviceType, Device device,
                                                                 DeviceTypeCommand command, string argument)
        {
            var     miCommand = command.CustomData1;
            var     zone      = command.CustomData2;
            var     ip        = (from d in device.Values where d.Name == "IPAddress" select d.Value).FirstOrDefault();
            decimal level;

            decimal.TryParse(argument, out level);
            await _controller.Send(ip, miCommand, zone, level);

            await Log.ReportInfoAsync($"{Name} Command Sent Command:{miCommand}, Zone:{zone}, IP:{ip}, Level:{level}", CancellationToken);
        }
Ejemplo n.º 3
0
        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");
            }
        }
Ejemplo n.º 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");
            }
        }
Ejemplo n.º 5
0
        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'");
            }
        }
Ejemplo n.º 6
0
        public async Task RunCommandAsyncDeviceTypeCommand()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var adapterManager = new StubIAdapterManager
            {
                FindZvsAdapterGuid = adapterGuid => new StubZvsAdapter
                {
                    IsEnabled = true,
                    ProcessDeviceTypeCommandAsyncDeviceTypeDeviceDeviceTypeCommandString = (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 deviceTypeCommand = new DeviceTypeCommand
                {
                    Name = "Turn On"
                };
                context.Commands.Add(deviceTypeCommand);
                context.Devices.Add(device);
                await context.SaveChangesAsync(CancellationToken.None);

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

                Console.WriteLine(result.Message);

                //Assert
                Assert.IsFalse(result.HasError);
            }
        }
Ejemplo n.º 7
0
        private Task Publish(DeviceType deviceType, Device device, DeviceTypeCommand deviceTypeCommand,
                             DeviceCommand deviceCommand, string argument)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            var o =
                new
            {
                DeviceName             = device.Name,
                DeviceCommandName      = (deviceCommand == null ? null : deviceCommand.Name),
                DeviceTypeCommandName  = (deviceTypeCommand == null ? null : deviceTypeCommand.Name),
                DeviceTypeCommandValue = (deviceTypeCommand == null ? null : deviceTypeCommand.Value),
                Argument       = argument,
                DeviceTypeName = (device == null || device.Type == null ? null : device.Type.Name),
                device.CurrentLevelInt,
                device.CurrentLevelText
            };
            var str = js.Serialize(o);

            client.Publish(SystemTopic, Encoding.UTF8.GetBytes(str));
            return(Task.FromResult(0));
        }
Ejemplo n.º 8
0
        internal async Task <Result> ExecuteDeviceTypeCommandAsync(DeviceTypeCommand command, string argument, string argument2, CancellationToken cancellationToken)
        {
            using (var context = new ZvsContext(EntityContextConnection))
            {
                int dId = int.TryParse(argument2, out dId) ? dId : 0;

                var device = await context.Devices
                             .Include(o => o.Type)
                             .Include(o => o.Type.Adapter)
                             .FirstOrDefaultAsync(o => o.Id == dId, cancellationToken);

                if (device == null)
                {
                    return(Result.ReportErrorFormat("Cannot find device with id of {0}", dId));
                }

                var commandAction =
                    $"{command.Name}{(string.IsNullOrEmpty(argument) ? "" : " " + argument)} {device.Name}";

                var aGuid   = device.Type.Adapter.AdapterGuid;
                var adapter = AdapterManager.FindZvsAdapter(aGuid);
                if (adapter == null)
                {
                    return(Result.ReportErrorFormat("{0} failed, device adapter is not loaded!",
                                                    commandAction));
                }

                if (!adapter.IsEnabled)
                {
                    return(Result.ReportErrorFormat("{0} failed because the {1} adapter is {2}",
                                                    commandAction,
                                                    device.Type.Adapter.Name,
                                                    adapter.IsEnabled ? "not ready" : "disabled"));
                }

                await adapter.ProcessDeviceTypeCommandAsync(device.Type, device, command, argument);

                return(Result.ReportSuccessFormat("{0} complete", commandAction));
            }
        }
Ejemplo n.º 9
0
 public override Task ProcessDeviceTypeCommandAsync(DeviceType deviceType, Device device, DeviceTypeCommand command, string argument)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 10
0
 public override Task ProcessDeviceTypeCommandAsync(DeviceType deviceType, Device device, DeviceTypeCommand command, string argument)
 {
     return(Publish(deviceType, device, command, null, argument));
 }
Ejemplo n.º 11
0
 public abstract Task ProcessDeviceTypeCommandAsync(DeviceType deviceType, Device device, DeviceTypeCommand command, string argument);