public async Task DiscoverAsync(bool clearCached = false)
        {
            if (clearCached)
            {
                _logger?.LogDebug($"Clearing device cache");
                lock (Devices)
                    Devices.Clear();
            }

            // TODO: Cancel previous discovery requests

            // Create a discover request message
            var payload = new OicRequest
            {
                Operation = OicRequestOperation.Get,
                ToUri     = new Uri("/oic/res", UriKind.Relative),
            };

            using (var handle = _client.GetHandle(payload))
            {
                lock (_discoverRequests)
                    _discoverRequests.Add(handle.RequestId);

                // Get the handle first and store it before broadcasting request. Reponses may be lost if we don't know our requesting Id.
                _logger?.LogDebug($"Creating discovery request with RequestId {handle.RequestId}");
                await _client.BroadcastAsync(payload);
            }
        }
        public virtual async Task <OicResponse> RetrieveAsync(OicRequest request)
        {
            if (Device == null)
            {
                throw new NullReferenceException($"{GetType().FullName}.{nameof(Device)} cannot be null null");
            }

            var endoint = Device.Endpoint;

            using (var requestiHandle = await _client.SendAsync(request, Device.Endpoint))
            {
                var response = await requestiHandle.GetReponseAsync() as OicResponse;

                return(new OicResourceResponse(_client.Configuration, _client.Configuration.Serialiser.Deserialise(response.Content, response.ContentType)));
            }

            //using (var results = Device.Configuration.Serialiser.Deserialise(response.Content, response.ContentType)
            //    .GetEnumerator())
            //{
            //    results.MoveNext();
            //    var result = results.Current;

            //    resource.UpdateFields(result);

            //    // We should not have more than one result in a response to a Retreive.
            //    if (results.MoveNext())
            //        throw new InvalidOperationException($"Received multiple objects during {nameof(RetrieveAsync)}");
            //}

            //return Task.FromResult(response);
        }
 public virtual Task <OicResponse> DeleteAsync(OicRequest request)
 {
     throw new NotSupportedException();
 }
 public virtual Task <OicResponse> CreateOrUpdateAsync(OicRequest request, IOicResource resource)
 {
     throw new NotSupportedException();
 }