private void ConstructPresubscriptionArray(string id) { // get the union of all the local subscriptions var presubs = new HashSet <ResourceValuesFilter>(); ResourceValueObservers.ForEach(s => { presubs = presubs.Union(s.ResourceValueSubscriptions).ToHashSet(); }); AllLocalSubscriptions = presubs; if (ConnectApi != null) { // get the union of the local subscriptions and the online subscriptions var mappedSubs = presubs.Select(p => ResourceValuesFilter.Map(p)).ToArray(); var serverSubs = ConnectApi.ListPresubscriptions(); var merged = MergeLocalAndServerLists(mappedSubs, serverSubs); ConnectApi.UpdatePresubscriptions(merged); if (Immediacy == FirstValueImmediacy.OnValueUpdate) { var connectedDevices = ConnectApi.ListConnectedDevices(); ResourceValueObservers.Where(v => v.Id == id).ToList().ForEach(v => v.ResourceValueSubscriptions.ToList().ForEach(s => { // add resource subscriptions for all matching resources on connected devices connectedDevices .Where(d => s.DeviceId.MatchWithWildcard(d.Id)) .ToList() .ForEach(m => { m.ListResources() .ToList() .ForEach(async r => { if (!s.ResourcePaths.Any() || s.ResourcePaths.Any(p => p.MatchWithWildcard(r.Path))) { await ConnectApi.AddResourceSubscriptionAsync(r.DeviceId, r.Path); } }); }); })); } } }
/// <summary> /// Subscribe to this resource. /// </summary> /// <returns>Async consumer with string</returns> public async Task <Resource> Subscribe() { return(await api.AddResourceSubscriptionAsync(DeviceId, Path)); }