public void AddDevice(OicDevice oicDevice) { if (!_resources.ContainsKey("/oic/d")) { // TODO: Decouple OicDevice to OicDeviceResource mapping var deviceResource = new OicDeviceResource { Name = oicDevice.Name, SpecVersions = "core.1.1.1", // TODO: Reference this from somewhere (make it a fixed default value?) PlatformId = Guid.NewGuid(), DeviceId = oicDevice.DeviceId, ResourceTypes = oicDevice.DeviceTypes.ToList(), ServerVersion = System.Diagnostics.FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion, }; _resources.Add("/oic/d", deviceResource); _discoverableOicResources.Add(OicResourceLink.FromResource(deviceResource)); } var discoveredResources = oicDevice.GetType() .GetProperties() .Where(p => p.GetCustomAttribute <OicResourceAttribute>() != null) .Select(p => Tuple.Create((IOicResource)p.GetMethod.Invoke(oicDevice, null), p.GetCustomAttribute <OicResourceAttribute>(false))); var deviceResourceDirectory = new OicResourceDirectory { DeviceId = oicDevice.DeviceId, Name = oicDevice.Name, }; _resourceDirectory.Add(deviceResourceDirectory); foreach (var resource in discoveredResources) { _resources.Add(resource.Item1.RelativeUri, resource.Item1); if (resource.Item2.Policies.HasFlag(OicResourcePolicies.Discoverable)) { deviceResourceDirectory.Links.Add(OicResourceLink.FromResource( resource.Item1, resource.Item2.Policies.HasFlag(OicResourcePolicies.Secure) ? new OicResourceLink.LinkPolicies { Policies = LinkPolicyFlags.Discoverable, IsSecure = true } : null)); } } }
public static IApplicationBuilder UseOicResources(this IApplicationBuilder app, OicDevice device) { if (app == null) { throw new ArgumentNullException(nameof(app)); } var host = app.ApplicationServices.GetService <OicHostDevice>(); host.AddDevice(device); return(app.UseResourceRepository(options => { options.ResourceRepository = host; })); }
public async Task TestDiscoverDevice() { //Arange bool newDeviceCallbackInvoked = false; OicDevice actualDevice = null; // Act using (var client = new OicClient()) { var service = new OicResourceDiscoverClient(client); client.AddTransport(_mockTransport.Object); service.NewDevice += (s, e) => { newDeviceCallbackInvoked = true; actualDevice = e.Device; }; await service.DiscoverAsync(); await Task.Delay(100); } // Assert Assert.IsTrue(newDeviceCallbackInvoked, $"{typeof(OicResourceDiscoverClient)}.{nameof(OicResourceDiscoverClient.NewDevice)} was not invoked"); var expectedDevice = new OicDevice() { DeviceId = Guid.Parse("0685B960-736F-46F7-BEC0-9E6CBD61ADC1"), Resources = { new CoreResources.OicDeviceResource { RelativeUri = "/oid/d", ResourceTypes ={ "oic.d.light", "oic.wk.d" }, Interfaces = OicResourceInterface.ReadOnly | OicResourceInterface.Baseline, }, new CoreResources.OicPlatformResource { RelativeUri = "/oid/p", ResourceTypes ={ "oic.wk.p" }, Interfaces = OicResourceInterface.ReadOnly | OicResourceInterface.Baseline, }, new ResourceTypes.SwitchBinary { RelativeUri = "/switch", ResourceTypes ={ "oic.r.switch.binary" }, Interfaces = OicResourceInterface.Actuator | OicResourceInterface.Baseline, }, new ResourceTypes.LightBrightness { RelativeUri = "/brightness", ResourceTypes ={ "oic.r.light.brightness" }, Interfaces = OicResourceInterface.Actuator | OicResourceInterface.Baseline, } } }; Assert.AreEqual(expectedDevice.DeviceId, actualDevice.DeviceId); Assert.AreEqual(expectedDevice.Resources, actualDevice.Resources); }