예제 #1
0
 public abstract Task HandleReceivedMessage(OicReceivedMessage message);
예제 #2
0
        public override async Task HandleReceivedMessage(OicReceivedMessage received)
        {
            var isDiscoverResponse = false;

            lock (_discoverRequests)
                isDiscoverResponse = _discoverRequests.Contains(received.Message.RequestId);

            if (!isDiscoverResponse)
            {
                _logger?.LogTrace($"Request ({received.Message.RequestId}) was not intended for {nameof(OicResourceDiscoverClient)}");

                if (Handler != null)
                {
                    await Handler.HandleReceivedMessage(received);
                }
                return;
            }

            var response = received.Message as OicResponse;

            if (response.ResposeCode != OicResponseCode.Content)
            {
                _logger?.LogInformation($"Response to discover request resulted in {response.ResposeCode:G}");
                _logger?.LogInformation(_configuration.Serialiser.Prettify(response.Content, response.ContentType));
                return;
            }

            var resources = _configuration.Serialiser.Deserialise(response.Content, response.ContentType) as OicResourceList
                            ?? throw new OicException($"Incorrect response for /oic/res request");

            //Todo: Review Resource Directory (OIC Core v1.1.1: Section 11.3.6.1.2 Resource directory)
            foreach (var resource in resources)
            {
                var newDevice = false;

                if (!(resource is OicResourceDirectory directory))
                {
                    _logger?.LogError($"{nameof(resource)} ({resource.GetType()}) is not of type {typeof(OicResourceList)}. Skipping");
                    continue;
                }

                OicRemoteDevice device = null;
                lock (Devices)
                    device = Devices.FirstOrDefault(d => d.DeviceId == directory.DeviceId);

                if (device == null)
                {
                    _logger?.LogDebug($"Discovered new device ({directory.DeviceId})");
                    device = new OicRemoteDevice(received.Endpoint)
                    {
                        DeviceId = directory.DeviceId
                    };
                    lock (Devices)
                        Devices.Add(device);
                    newDevice = true;
                }
                else
                {
                    _logger?.LogDebug($"Updating existing device ({directory.DeviceId})");
                }

                // Update device name
                device.Name = device.Name ?? directory.Name;

                // Add or update device resources
                // TODO: Do we want to clear out old resources?
                var newResources = directory.Links.Select(l => l.CreateResource(_configuration.Resolver));
                foreach (var newResource in newResources)
                {
                    var oldResource = device.Resources.SingleOrDefault(r => r.RelativeUri == newResource.RelativeUri);
                    if (oldResource == null)
                    {
                        device.Resources.Add(newResource);
                    }
                    else
                    {
                        oldResource.UpdateFields(newResource);
                    }
                }

                if (newDevice)
                {
                    NewDevice?.Invoke(this, new OicNewDeviceEventArgs {
                        Device = device, Endpoint = received.Endpoint
                    });
                }
            }
        }