Example #1
0
        async void CloudConnectionStatusChangedHandler(string deviceId,
                                                       CloudConnectionStatus connectionStatus)
        {
            Preconditions.CheckNonWhiteSpace(deviceId, nameof(deviceId));
            Events.HandlingConnectionStatusChangedHandler(deviceId, connectionStatus);
            if (!this.devices.TryGetValue(deviceId, out ConnectedDevice device))
            {
                throw new InvalidOperationException($"Device {deviceId} not found in the list of connected devices");
            }

            switch (connectionStatus)
            {
            case CloudConnectionStatus.TokenNearExpiry:
                Events.ProcessingTokenNearExpiryEvent(device.Identity);
                Option <IClientCredentials> clientCredentials = await this.credentialsCache.Get(device.Identity);

                if (clientCredentials.HasValue)
                {
                    await clientCredentials.ForEachAsync(
                        async cc =>
                    {
                        if (cc is ITokenCredentials tokenCredentials && tokenCredentials.IsUpdatable)
                        {
                            Try <ICloudConnection> cloudConnectionTry = await device.CreateOrUpdateCloudConnection(c => this.CreateOrUpdateCloudConnection(c, tokenCredentials));
                            if (!cloudConnectionTry.Success)
                            {
                                await this.RemoveDeviceConnection(device, true);
                                this.CloudConnectionLost?.Invoke(this, device.Identity);
                            }
                        }
                        else
                        {
                            await this.RemoveDeviceConnection(device, false);
                        }
                    });
Example #2
0
        async void CloudConnectionStatusChangedHandler(string deviceId,
                                                       CloudConnectionStatus connectionStatus)
        {
            Preconditions.CheckNonWhiteSpace(deviceId, nameof(deviceId));
            if (!this.devices.TryGetValue(deviceId, out ConnectedDevice device))
            {
                throw new InvalidOperationException($"Device {deviceId} not found in the list of connected devices");
            }

            switch (connectionStatus)
            {
            case CloudConnectionStatus.TokenNearExpiry:

                Option <IDeviceProxy> deviceProxy = device.DeviceConnection.Map(d => d.DeviceProxy).Filter(d => d.IsActive);
                if (deviceProxy.HasValue)
                {
                    Option <IClientCredentials> token = await deviceProxy.Map(d => d.GetUpdatedIdentity())
                                                        .GetOrElse(Task.FromResult(Option.None <IClientCredentials>()));

                    if (token.HasValue)
                    {
                        await token.ForEachAsync(async t =>
                        {
                            Try <ICloudConnection> cloudConnectionTry = await device.CreateOrUpdateCloudConnection(c => this.CreateOrUpdateCloudConnection(c, t));
                            if (!cloudConnectionTry.Success)
                            {
                                await this.RemoveDeviceConnection(device, true);
                                this.CloudConnectionLost?.Invoke(this, device.Identity);
                            }
                        });
                    }
                    else
                    {
                        await this.RemoveDeviceConnection(device, false);
                    }
                }
                else
                {
                    await this.RemoveDeviceConnection(device, true);
                }

                break;

            case CloudConnectionStatus.DisconnectedTokenExpired:
                await this.RemoveDeviceConnection(device, true);

                this.CloudConnectionLost?.Invoke(this, device.Identity);
                break;

            case CloudConnectionStatus.Disconnected:
                this.CloudConnectionLost?.Invoke(this, device.Identity);
                break;

            case CloudConnectionStatus.ConnectionEstablished:
                this.CloudConnectionEstablished?.Invoke(this, device.Identity);
                break;
            }
        }
        void ForwardConnectivityEvent(CloudConnectionStatus status)
        {
            switch (status)
            {
            case CloudConnectionStatus.ConnectionEstablished:
                this.DeviceConnected?.Invoke(this, EventArgs.Empty);
                break;

            case CloudConnectionStatus.Disconnected:
                this.DeviceDisconnected?.Invoke(this, EventArgs.Empty);
                break;
            }
        }
Example #4
0
        async void CloudConnectionStatusChangedHandler(string deviceId,
                                                       CloudConnectionStatus connectionStatus)
        {
            Preconditions.CheckNonWhiteSpace(deviceId, nameof(deviceId));
            if (!this.devices.TryGetValue(deviceId, out ConnectedDevice device))
            {
                throw new InvalidOperationException($"Device {deviceId} not found in the list of connected devices");
            }

            switch (connectionStatus)
            {
            case CloudConnectionStatus.TokenNearExpiry:
                Events.ProcessingTokenNearExpiryEvent(device.Identity);
                Option <IClientCredentials> token = await this.credentialsCache.Get(device.Identity);

                if (token.HasValue)
                {
                    await token.ForEachAsync(async t =>
                    {
                        await device.CreateOrUpdateCloudConnection(c => this.CreateOrUpdateCloudConnection(c, t));
                    });
                }
                else
                {
                    await this.RemoveDeviceConnection(device, false);
                }
                break;

            case CloudConnectionStatus.DisconnectedTokenExpired:
                await this.RemoveDeviceConnection(device, true);

                Events.InvokingCloudConnectionLostEvent(device.Identity);
                this.CloudConnectionLost?.Invoke(this, device.Identity);
                break;

            case CloudConnectionStatus.Disconnected:
                Events.InvokingCloudConnectionLostEvent(device.Identity);
                this.CloudConnectionLost?.Invoke(this, device.Identity);
                break;

            case CloudConnectionStatus.ConnectionEstablished:
                Events.InvokingCloudConnectionEstablishedEvent(device.Identity);
                this.CloudConnectionEstablished?.Invoke(this, device.Identity);
                break;
            }
        }
Example #5
0
 public static void HandlingConnectionStatusChangedHandler(string deviceId, CloudConnectionStatus connectionStatus)
 {
     Log.LogInformation((int)EventIds.HandlingConnectionStatusChangedHandler, Invariant($"Connection status for {deviceId} changed to {connectionStatus}"));
 }
Example #6
0
 void ConnectionChangedEventHandler(CloudConnectionStatus cloudConnectionStatus)
 {
     this.connectionStatusChangedHandler(this.identity.Id, cloudConnectionStatus);
 }