From 0865b43363ce59f860ed3b996e7fb7421b00915e Mon Sep 17 00:00:00 2001 From: Rafael Lillo Date: Wed, 12 Feb 2020 08:26:11 +0000 Subject: [PATCH 1/2] Fixes error to integrate with gateway --- sample/Multi/Startup.cs | 2 +- sample/Multi/Things/ExampleDimmableLight.cs | 2 +- sample/Multi/appsettings.json | 2 +- sample/Single/Startup.cs | 2 +- sample/Single/appsettings.json | 2 +- .../Activator/ThingActivator.cs | 5 +++++ src/Mozilla.IoT.WebThing/Property.cs | 14 +------------- src/Mozilla.IoT.WebThing/ServiceRouteBuilder.cs | 2 +- .../ThingEndpointRouteBuilderExtensions.cs | 4 +--- 9 files changed, 13 insertions(+), 22 deletions(-) diff --git a/sample/Multi/Startup.cs b/sample/Multi/Startup.cs index 9ee9548..21b738f 100644 --- a/sample/Multi/Startup.cs +++ b/sample/Multi/Startup.cs @@ -31,7 +31,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) app.UseDeveloperExceptionPage(); } - app.UseHttpsRedirection(); + //app.UseHttpsRedirection(); app.UseRouting(); diff --git a/sample/Multi/Things/ExampleDimmableLight.cs b/sample/Multi/Things/ExampleDimmableLight.cs index 588825b..5e699b0 100644 --- a/sample/Multi/Things/ExampleDimmableLight.cs +++ b/sample/Multi/Things/ExampleDimmableLight.cs @@ -108,7 +108,7 @@ public class FadeAction : Action protected override async ValueTask ExecuteAsync(CancellationToken cancellation) { - int value = Input["input"] as int? ?? 0; + int value = Input?["input"] as int? ?? 0; await Task.Delay(value, cancellation); var property = Thing.Properties.FirstOrDefault(x => x.Name == "brightness"); diff --git a/sample/Multi/appsettings.json b/sample/Multi/appsettings.json index def9159..8929653 100644 --- a/sample/Multi/appsettings.json +++ b/sample/Multi/appsettings.json @@ -1,7 +1,7 @@ { "Logging": { "LogLevel": { - "Default": "Warning" + "Default": "Debug" } }, "AllowedHosts": "*" diff --git a/sample/Single/Startup.cs b/sample/Single/Startup.cs index ddf5037..ca77d01 100644 --- a/sample/Single/Startup.cs +++ b/sample/Single/Startup.cs @@ -33,7 +33,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) app.UseDeveloperExceptionPage(); } - app.UseHttpsRedirection(); + //app.UseHttpsRedirection(); app.UseRouting(); diff --git a/sample/Single/appsettings.json b/sample/Single/appsettings.json index def9159..8929653 100644 --- a/sample/Single/appsettings.json +++ b/sample/Single/appsettings.json @@ -1,7 +1,7 @@ { "Logging": { "LogLevel": { - "Default": "Warning" + "Default": "Debug" } }, "AllowedHosts": "*" diff --git a/src/Mozilla.IoT.WebThing/Activator/ThingActivator.cs b/src/Mozilla.IoT.WebThing/Activator/ThingActivator.cs index a26777b..24674de 100644 --- a/src/Mozilla.IoT.WebThing/Activator/ThingActivator.cs +++ b/src/Mozilla.IoT.WebThing/Activator/ThingActivator.cs @@ -38,6 +38,10 @@ public void Register(IServiceProvider service) where T : Thing { string name = typeof(T).Name; Register(service, name.Replace("Thing", "")); + + var thing = CreateInstance(service, name.Replace("Thing", "")); + + _thingType.TryAdd(thing.Name, typeof(T)); } public void Register(IServiceProvider service, string thing) @@ -53,6 +57,7 @@ public void Register(IServiceProvider service, T thing) { _thingType.TryAdd(thing.Name, typeof(T)); _typeActivatorCache.TryAdd(typeof(T), thing); + BindingThingNotify(thing, service, thing.Name); } diff --git a/src/Mozilla.IoT.WebThing/Property.cs b/src/Mozilla.IoT.WebThing/Property.cs index 60e8ddc..bdedf87 100644 --- a/src/Mozilla.IoT.WebThing/Property.cs +++ b/src/Mozilla.IoT.WebThing/Property.cs @@ -20,19 +20,7 @@ public Property(string name, object value, IDictionary metadata) { } - public new virtual T Value - { - get - { - if (base.Value == null) - { - return default; - } - - return (T)base.Value; - } - set => base.Value = value; - } + public new virtual T Value { get; set; } internal override Type Type => typeof(T); diff --git a/src/Mozilla.IoT.WebThing/ServiceRouteBuilder.cs b/src/Mozilla.IoT.WebThing/ServiceRouteBuilder.cs index d7155cd..ba46825 100644 --- a/src/Mozilla.IoT.WebThing/ServiceRouteBuilder.cs +++ b/src/Mozilla.IoT.WebThing/ServiceRouteBuilder.cs @@ -27,7 +27,7 @@ internal IEnumerable Build(IEndpointRouteBuilder end if (!_option.IsSingleThing) { - prefix = "/{{thing}}"; + prefix = "/things/{thing}"; result.AddLast(endpointRouteBuilder.MapGet("/", GetThings.Invoke)); result.AddLast(endpointRouteBuilder.MapGet(prefix, GetThing.Invoke)); diff --git a/src/Mozilla.IoT.WebThing/ThingEndpointRouteBuilderExtensions.cs b/src/Mozilla.IoT.WebThing/ThingEndpointRouteBuilderExtensions.cs index 3ed665e..35f41e6 100644 --- a/src/Mozilla.IoT.WebThing/ThingEndpointRouteBuilderExtensions.cs +++ b/src/Mozilla.IoT.WebThing/ThingEndpointRouteBuilderExtensions.cs @@ -70,9 +70,7 @@ public static ThingEndpointConventionBuilder MapThing(this IEndpointRouteBuil var activator = builder.ServiceProvider.GetService(); activator.Register(builder.ServiceProvider, thing); - - - + var serviceRouteBuilder = builder.ServiceProvider.GetRequiredService(); var endpointConventionBuilders = serviceRouteBuilder.Build(builder); From e2b35a92ff5d2ee6c36e3535b51bb5625ac61be6 Mon Sep 17 00:00:00 2001 From: Rafael Lillo Date: Wed, 12 Feb 2020 12:37:08 +0000 Subject: [PATCH 2/2] Fixes test --- .../Activator/ThingActivator.cs | 9 ++++++--- src/Mozilla.IoT.WebThing/Property.cs | 14 ++++++++++++-- .../Activator/ThingActivatorTest.cs | 3 +-- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/Mozilla.IoT.WebThing/Activator/ThingActivator.cs b/src/Mozilla.IoT.WebThing/Activator/ThingActivator.cs index 24674de..b205bc4 100644 --- a/src/Mozilla.IoT.WebThing/Activator/ThingActivator.cs +++ b/src/Mozilla.IoT.WebThing/Activator/ThingActivator.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; @@ -40,8 +40,11 @@ public void Register(IServiceProvider service) where T : Thing Register(service, name.Replace("Thing", "")); var thing = CreateInstance(service, name.Replace("Thing", "")); - - _thingType.TryAdd(thing.Name, typeof(T)); + + if (thing.Name != null) + { + _thingType.TryAdd(thing.Name, typeof(T)); + } } public void Register(IServiceProvider service, string thing) diff --git a/src/Mozilla.IoT.WebThing/Property.cs b/src/Mozilla.IoT.WebThing/Property.cs index bdedf87..5505ed6 100644 --- a/src/Mozilla.IoT.WebThing/Property.cs +++ b/src/Mozilla.IoT.WebThing/Property.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Diagnostics; @@ -20,7 +20,17 @@ public Property(string name, object value, IDictionary metadata) { } - public new virtual T Value { get; set; } + private T _value; + + public new virtual T Value + { + get => _value; + set + { + _value = value; + OnValueChanged(); + } + } internal override Type Type => typeof(T); diff --git a/test/Mozilla.IoT.WebThing.Test/Activator/ThingActivatorTest.cs b/test/Mozilla.IoT.WebThing.Test/Activator/ThingActivatorTest.cs index e128b37..45f352f 100644 --- a/test/Mozilla.IoT.WebThing.Test/Activator/ThingActivatorTest.cs +++ b/test/Mozilla.IoT.WebThing.Test/Activator/ThingActivatorTest.cs @@ -1,4 +1,4 @@ -using Mozilla.IoT.WebThing.Activator; +using Mozilla.IoT.WebThing.Activator; #if DEBUG using System.Linq; using System; @@ -295,7 +295,6 @@ public void GetEnumerator_Should_ReturnAllInstance() private class SampleThing : Thing { - } } }