public async override Task<Result> ImportAsync(string fileName, CancellationToken cancellationToken)
        {
            var result = await ReadAsXmlFromDiskAsync<List<GroupBackup>>(fileName);

            if (result.HasError)
                return Result.ReportError(result.Message);

            var skippedCount = 0;
            var newGroups = new List<Group>();

            using (var context = new ZvsContext(EntityContextConnection))
            {
                var existingGroups = await context.Groups.ToListAsync(cancellationToken);
                var existingDevice = await context.Devices.ToListAsync(cancellationToken);

                foreach (var backupGroup in result.Data)
                {
                    if (existingGroups.Any(o => o.Name == backupGroup.Name))
                    {
                        skippedCount++;
                        continue;
                    }

                    var group = new Group();
                    group.Name = backupGroup.Name;
                    var devices = existingDevice.Where(o => backupGroup.NodeNumbers.Contains(o.NodeNumber));

                    foreach (var device in devices)
                        group.Devices.Add(device);

                    newGroups.Add(group);
                }

                context.Groups.AddRange(newGroups);

                if (newGroups.Count > 0)
                {
                    var saveResult = await context.TrySaveChangesAsync(cancellationToken);
                    if (saveResult.HasError)
                        return Result.ReportError(saveResult.Message);
                }
            }
            return Result.ReportSuccess(
                $"Imported {newGroups.Count} groups, skipped {skippedCount} from {Path.GetFileName(fileName)}");
        }
 public override Task DeactivateGroupAsync(Group group)
 {
     throw new NotImplementedException();
 }
Esempio n. 3
0
 public override Task DeactivateGroupAsync(Group @group, ZvsContext context)
 {
     //throw new NotImplementedException();
     return null;
 }
 public override Task ActivateGroupAsync(Group group)
 {
     throw new System.NotImplementedException();
 }
Esempio n. 5
0
 public override Task DeactivateGroupAsync(Group @group)
 {
     return Task.FromResult(0);
 }
        public override async Task DeactivateGroupAsync(Group group)
        {
            using (var context = new ZvsContext(EntityContextConnection))
            {
                var devices = await context.Devices
                    .Where(o => o.Type.Adapter.AdapterGuid == AdapterGuid)
                    .Where(o => o.Groups.Any(g => g.Id == group.Id))
                    .ToListAsync();

                foreach (var device in devices)
                {
                    var nodeNumber = Convert.ToByte(device.NodeNumber);
                    if (!IsNodeReady(nodeNumber))
                    {
                        await
                            Log.ReportInfoFormatAsync(CancellationToken,
                                "Failed to deactivate group member {0}, node {1}. Node not ready.", device.Name,
                                nodeNumber);
                        continue;
                    }

                    MManager.SetNodeOff(MHomeId, nodeNumber);
                }
            }
        }
        public override async Task ActivateGroupAsync(Group group)
        {
            using (var context = new ZvsContext(EntityContextConnection))
            {
                var devices = await context.Devices
                    .Include(d => d.Type)
                    .Where(o => o.Type.Adapter.AdapterGuid == AdapterGuid)
                    .Where(o => o.Groups.Any(g => g.Id == group.Id))
                    .ToListAsync();

                foreach (var device in devices)
                {
                    var nodeNumber = Convert.ToByte(device.NodeNumber);
                    if (!IsNodeReady(nodeNumber))
                    {
                        await
                            Log.ReportInfoFormatAsync(CancellationToken,
                                "Failed to activate group member {0}, node {1}. Node not ready.", device.Name,
                                nodeNumber);
                        continue;
                    }

                    if (device.Type.UniqueIdentifier == OpenzWaveDeviceTypes.Dimmer.ToString())
                    {
                        var value =
                            await
                                device.GetDeviceTypeValueAsync(
                                    OpenzWaveDeviceTypeSettings.DefaultDimmerOnLevel.ToString(), context);

                        if (value != null)
                        {
                            byte bValue = byte.TryParse(value, out bValue) ? bValue : (byte)99;
                            MManager.SetNodeLevel(MHomeId, (byte)device.NodeNumber, bValue);
                            continue;
                        }
                    }

                    MManager.SetNodeOn(MHomeId, nodeNumber);
                }
            }
        }
        public async Task ExecuteBuiltinCommandAsyncGroupOffTest()
        {
            //Arrange 
            var dbConnection = new UnitTestDbConnection();
            Database.SetInitializer(new CreateFreshDbInitializer());

            var groupOnIdsRequestSentToAdapter = new List<Group>();
            var adapterManager = new StubIAdapterManager
            {
                FindZvsAdapterGuid = adapterGuid => new StubZvsAdapter
                {
                    IsEnabled = true,
                    DeactivateGroupAsyncGroup = async g => groupOnIdsRequestSentToAdapter.Add(g)
                }
            };
            var log = new StubIFeedback<LogEntry>();
            var cts = new CancellationTokenSource();
            var commmandProcessor = new CommandProcessor(adapterManager, dbConnection, log);

            var device = UnitTesting.CreateFakeDevice();
            var device2 = UnitTesting.CreateFakeDevice();
            var device3 = UnitTesting.CreateFakeDevice();
            using (var context = new ZvsContext(dbConnection))
            {
                context.Devices.Add(device);
                context.Devices.Add(device2);
                context.Devices.Add(device3);

                var group = new Group
                {
                    Name = "Test Group"
                };
                group.Devices.Add(device);
                group.Devices.Add(device2);
                context.Groups.Add(group);

                var builtinCommand = new BuiltinCommand
                {
                    Name = "Turn off Group",
                    UniqueIdentifier = "GROUP_OFF"
                };
                context.Commands.Add(builtinCommand);
                await context.SaveChangesAsync(new CancellationToken());

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

                //Assert
                Assert.IsFalse(result.HasError);
                Assert.IsTrue(groupOnIdsRequestSentToAdapter.Count == 2, "Process did not run the correct amount of commands.");
                Assert.IsTrue(group.Id == groupOnIdsRequestSentToAdapter[0].Id, "Ran the wrong group!");
            }
        }
        public async Task ExecuteBuiltinCommandAsyncGroupOnNoDevicesTest()
        {
            //Arrange 
            var dbConnection = new UnitTestDbConnection();
            Database.SetInitializer(new CreateFreshDbInitializer());

            var groupOnIdsRequestSentToAdapter = new List<Group>();
            var adapterManager = new StubIAdapterManager
            {
                FindZvsAdapterGuid = adapterGuid => new StubZvsAdapter
                {
                    IsEnabled = true,
                    ActivateGroupAsyncGroup = async g => groupOnIdsRequestSentToAdapter.Add(g)
                }
            };
            var log = new StubIFeedback<LogEntry>();
            var cts = new CancellationTokenSource();
            var commmandProcessor = new CommandProcessor(adapterManager, dbConnection, log);

            using (var context = new ZvsContext(dbConnection))
            {
                var group = new Group
                {
                    Name = "Test Group"
                };
                context.Groups.Add(group);

                var builtinCommand = new BuiltinCommand
                {
                    Name = "Turn on a Group",
                    UniqueIdentifier = "GROUP_ON"
                };
                context.Commands.Add(builtinCommand);
                await context.SaveChangesAsync(new CancellationToken());

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

                //Assert
                Assert.IsTrue(result.HasError);
                Assert.IsTrue(result.Message.Contains("no devices found "), "Expected to see 'no devices found' in log when executed group with no devices");
            }
        }
Esempio n. 10
0
 public abstract Task DeactivateGroupAsync(Group group);