public async Task AddDeviceConnection(IIdentity identity, IDeviceProxy deviceProxy) { Preconditions.CheckNotNull(identity, nameof(identity)); Preconditions.CheckNotNull(deviceProxy, nameof(deviceProxy)); ConnectedDevice device = this.GetOrCreateConnectedDevice(identity); Option <DeviceConnection> currentDeviceConnection = device.AddDeviceConnection(deviceProxy); Events.NewDeviceConnection(identity); await currentDeviceConnection .Filter(dc => dc.IsActive) .ForEachAsync(dc => dc.CloseAsync(new MultipleConnectionsException($"Multiple connections detected for device {identity.Id}"))); this.DeviceConnected?.Invoke(this, identity); }
public async Task AddDeviceConnection(IIdentity identity, IDeviceProxy deviceProxy) { Preconditions.CheckNotNull(identity, nameof(identity)); Preconditions.CheckNotNull(deviceProxy, nameof(deviceProxy)); ConnectedDevice device = this.GetOrCreateConnectedDevice(identity); Option <DeviceConnection> currentDeviceConnection = device.AddDeviceConnection(deviceProxy); currentDeviceConnection.ForEach(async c => { // If we add a device connection that already has subscriptions, we will remake the cloud connection. // Otherwise the cloud connection won't get established until some other operation does it (i.e. telemetry) if (c.Subscriptions.Count > 0) { Events.GettingCloudConnectionForDeviceSubscriptions(); await this.TryGetCloudConnection(identity.Id); } }); Events.NewDeviceConnection(identity); await currentDeviceConnection .Filter(dc => dc.IsActive) .ForEachAsync(dc => dc.CloseAsync(new MultipleConnectionsException($"Multiple connections detected for device {identity.Id}"))); this.DeviceConnected?.Invoke(this, identity); }