コード例 #1
0
        public async Task RegisterAdapterSettingOptionAddedTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

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

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

            dbAdapter.Settings.Add(adapterSetting);
            var option1 = new AdapterSettingOption
            {
                Name = "Option 1",
            };
            var option2 = new AdapterSettingOption
            {
                Name = "Option 2",
            };

            adapterSetting.Options.Add(option1);


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

            adapterSetting.Options.Add(option2);

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

            AdapterSetting a;

            using (var context = new ZvsContext(dbConnection))
            {
                a = await context.AdapterSettings
                    .Include(o => o.Options)
                    .FirstOrDefaultAsync(o => o.Id == adapterSetting.Id);
            }

            //assert
            Console.WriteLine(result.Message);
            Assert.IsFalse(result.HasError);
            Assert.IsTrue(a.Options.Count == 2, "Expected 2 adapter setting options");
            Assert.IsTrue(a.Options[1].Name == option2.Name, "Adapter option name mismatch");
        }
コード例 #2
0
        public override async Task OnSettingsCreating(AdapterSettingBuilder settingBuilder)
        {
            var testSetting = new AdapterSetting
            {
                Name        = "Test setting",
                Value       = (360).ToString(CultureInfo.InvariantCulture),
                ValueType   = DataType.STRING,
                Description = "Unit testing only"
            };

            await settingBuilder.Adapter(this).RegisterAdapterSettingAsync(testSetting, o => o.TestSetting);
        }
コード例 #3
0
        public async Task LoadAdaptersAsyncTest()
        {
            //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 unitTestingAdapter = new StubUnitTestAdapter
            {
                AdapterGuidGet = () => Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                NameGet        = () => "Unit Testing Adapter",
                DescriptionGet = () => "",
                OnDeviceTypesCreatingDeviceTypeBuilder = s => Task.FromResult(0)
            };

            unitTestingAdapter.OnSettingsCreatingAdapterSettingBuilder = async settingBuilder =>
            {
                var testSetting = new AdapterSetting
                {
                    Name        = "Test setting",
                    Value       = 360.ToString(CultureInfo.InvariantCulture),
                    ValueType   = DataType.INTEGER,
                    Description = "Unit testing only"
                };

                await
                settingBuilder.Adapter(unitTestingAdapter)
                .RegisterAdapterSettingAsync(testSetting, o => o.PropertyTest);
            };

            var adapterManager = new AdapterManager(new List <ZvsAdapter> {
                unitTestingAdapter
            }, dbConnection, log);

            //act
            await adapterManager.StartAsync(CancellationToken.None);

            //assert
            Assert.IsTrue(logEntries.Count(o => o.Level == LogEntryLevel.Error) == 0, "Expected 0 errors");
            Assert.IsTrue(unitTestingAdapter.PropertyTest == 360, "Expected TestSetting property to be 360");
        }
コード例 #4
0
        public async Task RegisterAdapterSettingValueDonestTriggerChangedTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

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

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

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

            adapterSetting.Value = "New value!";

            //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].Value == "Hello World", "Adapter value changed when it shouldn't!");
        }
コード例 #5
0
ファイル: Adapter.cs プロジェクト: lulzzz/zVirtualScenes
        public override async Task OnSettingsCreating(AdapterSettingBuilder settingBuilder)
        {
            var hostSetting = new AdapterSetting
            {
                Name        = "Host",
                Value       = "127.0.0.1",
                ValueType   = DataType.STRING,
                Description = "The host machine which your MQTT server is installed."
            };
            var portSetting = new AdapterSetting
            {
                Name        = "Port",
                Value       = "1883",
                ValueType   = DataType.INTEGER,
                Description =
                    "The port on host machine which your MQTT server is installed. (1883 - default, 8883 - SSL)"
            };

            await settingBuilder.Adapter(this).RegisterAdapterSettingAsync(hostSetting, o => o.HostSetting);

            await settingBuilder.Adapter(this).RegisterAdapterSettingAsync(portSetting, o => o.PortSetting);
        }
コード例 #6
0
        public async Task LoadAdaptersAsyncAutoStartTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

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

            var adapter = new Adapter()
            {
                AdapterGuid = Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                IsEnabled   = true
            };

            using (var context = new ZvsContext(dbConnection))
            {
                context.Adapters.Add(adapter);
                await context.SaveChangesAsync(CancellationToken.None);
            }
            var isStarted          = false;
            var unitTestingAdapter = new StubUnitTestAdapter
            {
                AdapterGuidGet = () => Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                NameGet        = () => "Unit Testing Adapter",
                DescriptionGet = () => "",
                OnDeviceTypesCreatingDeviceTypeBuilder = s => Task.FromResult(0),
                StartAsync01 = () =>
                {
                    isStarted = true;
                    return(Task.FromResult(0));
                }
            };

            unitTestingAdapter.OnSettingsCreatingAdapterSettingBuilder = async settingBuilder =>
            {
                var testSetting = new AdapterSetting
                {
                    Name        = "Test setting",
                    Value       = 360.ToString(CultureInfo.InvariantCulture),
                    ValueType   = DataType.INTEGER,
                    Description = "Unit testing only"
                };

                await
                settingBuilder.Adapter(unitTestingAdapter)
                .RegisterAdapterSettingAsync(testSetting, o => o.PropertyTest);
            };

            var adapterManager = new AdapterManager(new List <ZvsAdapter> {
                unitTestingAdapter
            }, dbConnection, log);

            //act
            await adapterManager.StartAsync(CancellationToken.None);

            //assert
            Assert.IsTrue(isStarted, "Adapter not started!");
        }
コード例 #7
0
        public async Task LoadAdaptersAsyncBadPropertyTest()
        {
            //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 badSettings = new AdapterSetting
            {
                Name             = "Test setting2",
                Value            = 360.ToString(CultureInfo.InvariantCulture),
                ValueType        = DataType.STRING,
                Description      = "Unit testing only",
                UniqueIdentifier = "NotExistantPropertyName"
            };

            var adapter = new Adapter()
            {
                AdapterGuid = Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
            };

            adapter.Settings.Add(badSettings);
            using (var context = new ZvsContext(dbConnection))
            {
                context.Adapters.Add(adapter);
                await context.SaveChangesAsync(CancellationToken.None);
            }

            var unitTestingAdapter = new StubUnitTestAdapter
            {
                AdapterGuidGet = () => Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                NameGet        = () => "Unit Testing Adapter",
                DescriptionGet = () => "",
                OnDeviceTypesCreatingDeviceTypeBuilder = s => Task.FromResult(0)
            };

            unitTestingAdapter.OnSettingsCreatingAdapterSettingBuilder = async settingBuilder =>
            {
                var testSetting = new AdapterSetting
                {
                    Name        = "Test setting",
                    Value       = 360.ToString(CultureInfo.InvariantCulture),
                    ValueType   = DataType.INTEGER,
                    Description = "Unit testing only"
                };

                await
                settingBuilder.Adapter(unitTestingAdapter)
                .RegisterAdapterSettingAsync(testSetting, o => o.PropertyTest);
            };

            var adapterManager = new AdapterManager(new List <ZvsAdapter> {
                unitTestingAdapter
            }, dbConnection, log);

            //act
            await adapterManager.StartAsync(CancellationToken.None);

            //assert
            Assert.IsTrue(unitTestingAdapter.PropertyTest == 360, "Expected TestSetting property to be 360");
            Assert.IsTrue(logEntries.Count(o => o.Level == LogEntryLevel.Error && o.Message.Contains("Cannot find property")) == 1, "Expected 1 Cannot find property error");
        }
コード例 #8
0
            public async Task <Result> RegisterAdapterSettingAsync(AdapterSetting adapterSetting, Expression <Func <T, Object> > property)
            {
                MemberExpression memberExpression = (property.Body.NodeType == ExpressionType.Convert) ? (MemberExpression)((UnaryExpression)property.Body).Operand : property.Body as MemberExpression;

                if (memberExpression != null)
                {
                    var propertyInfo = memberExpression.Member as PropertyInfo;
                    if (propertyInfo == null)
                    {
                        throw new ArgumentException("The lambda expression 'property' should point to a valid Property");
                    }

                    adapterSetting.UniqueIdentifier = propertyInfo.Name;
                }

                using (var context = new ZvsContext(AdapterSettingBuilder.EntityContextConnection))
                {
                    var adapter =
                        await
                        context.Adapters.FirstOrDefaultAsync(p => p.AdapterGuid == Adapter.AdapterGuid,
                                                             AdapterSettingBuilder.CancellationToken);

                    if (adapter == null)
                    {
                        return(Result.ReportError("Invalid Adapter"));
                    }

                    var existingAdapter = await context.AdapterSettings.FirstOrDefaultAsync(
                        o => o.Adapter.Id == adapter.Id && o.UniqueIdentifier == adapterSetting.UniqueIdentifier,
                        AdapterSettingBuilder.CancellationToken);

                    var changed = false;
                    if (existingAdapter == null)
                    {
                        adapter.Settings.Add(adapterSetting);
                        changed = true;
                    }
                    else
                    {
                        PropertyChangedEventHandler handler = (s, a) => changed = true;
                        existingAdapter.PropertyChanged += handler;

                        existingAdapter.Description = adapterSetting.Description;
                        existingAdapter.Name        = adapterSetting.Name;
                        existingAdapter.ValueType   = adapterSetting.ValueType;

                        existingAdapter.PropertyChanged -= handler;

                        var added = adapterSetting.Options.Where(
                            option => existingAdapter.Options.All(o => o.Name != option.Name)).ToList();
                        foreach (var option in added)
                        {
                            existingAdapter.Options.Add(option);
                            changed = true;
                        }

                        var removed = existingAdapter.Options.Where(
                            option => adapterSetting.Options.All(o => o.Name != option.Name)).ToList();
                        foreach (var option in removed)
                        {
                            context.AdapterSettingOptions.Local.Remove(option);
                            changed = true;
                        }
                    }

                    if (changed)
                    {
                        return(await context.TrySaveChangesAsync(AdapterSettingBuilder.CancellationToken));
                    }
                }
                return(Result.ReportSuccess("Nothing to update"));
            }
コード例 #9
0
        public override async Task OnSettingsCreating(AdapterSettingBuilder settingBuilder)
        {
            var wifi1IpSetting = new AdapterSetting
            {
                Name        = "WiFi Controller 1 IP Address",
                Value       = "",
                ValueType   = DataType.STRING,
                Description = "The IP Address of the first WiFi Controller."
            };

            var wifi1SettingResult =
                await settingBuilder.Adapter(this).RegisterAdapterSettingAsync(wifi1IpSetting, o => o.WiFi1Setting);

            if (wifi1SettingResult.HasError)
            {
                await
                Log.ReportErrorFormatAsync(CancellationToken,
                                           "An error occured when registering the wifi controller 1 setting. {0}",
                                           wifi1SettingResult.Message);
            }



            var wifi2IpSetting = new AdapterSetting
            {
                Name        = "WiFi Controller 2 IP Address",
                Value       = "",
                ValueType   = DataType.STRING,
                Description = "The IP Address of the second WiFi Controller."
            };

            var wifi2SettingResult =
                await settingBuilder.Adapter(this).RegisterAdapterSettingAsync(wifi2IpSetting, o => o.WiFi2Setting);

            if (wifi2SettingResult.HasError)
            {
                await
                Log.ReportErrorFormatAsync(CancellationToken,
                                           "An error occured when registering the wifi controller 2 setting. {0}",
                                           wifi2SettingResult.Message);
            }

            var wifi3IpSetting = new AdapterSetting
            {
                Name        = "WiFi Controller 3 IP Address",
                Value       = "",
                ValueType   = DataType.STRING,
                Description = "The IP Address of the third WiFi Controller."
            };

            var wifi3SettingResult =
                await settingBuilder.Adapter(this).RegisterAdapterSettingAsync(wifi3IpSetting, o => o.WiFi3Setting);

            if (wifi3SettingResult.HasError)
            {
                await
                Log.ReportErrorFormatAsync(CancellationToken,
                                           "An error occured when registering the wifi controller 1 setting. {0}",
                                           wifi3SettingResult.Message);
            }

            var wifi4IpSetting = new AdapterSetting
            {
                Name        = "WiFi Controller 4 IP Address",
                Value       = "",
                ValueType   = DataType.STRING,
                Description = "The IP Address of the fourth WiFi Controller."
            };

            var wifi4SettingResult =
                await settingBuilder.Adapter(this).RegisterAdapterSettingAsync(wifi4IpSetting, o => o.WiFi4Setting);

            if (wifi4SettingResult.HasError)
            {
                await
                Log.ReportErrorFormatAsync(CancellationToken,
                                           "An error occured when registering the wifi controller 4 setting. {0}",
                                           wifi4SettingResult.Message);
            }



            await base.OnSettingsCreating(settingBuilder);
        }