Beispiel #1
0
        private async Task HandleDeviceEventAsync(MyState state, dynamic deviceEvent)
        {
            _ = state ??
                throw new ArgumentNullException(nameof(state));
            _ = deviceEvent ??
                throw new ArgumentNullException(nameof(deviceEvent));
            _ = deviceEvent.subscriptionName ??
                throw new ArgumentException($"deviceEvent.subscriptionName is null!",
                                            nameof(deviceEvent));

            var subscriptionName = deviceEvent.subscriptionName.Value;

            if (subscriptionName.StartsWith("MySwitches", StringComparison.Ordinal))
            {
                if (state.LightSwitches == null)
                {
                    Logger.LogDebug("No light switches configured, ignoring event...");
                }
                else
                {
                    Logger.LogDebug($"Checking light switch: {deviceEvent}...");

                    var lightSwitch =
                        state.LightSwitches.SingleOrDefault(ls =>
                                                            ls.Id == deviceEvent.deviceId.Value);

                    _ = lightSwitch ??
                        throw new InvalidOperationException($"Could not find configured lightSwitch with id: {deviceEvent.deviceId.Value}");

                    lightSwitch.CurrentState =
                        LightSwitch.SwitchStateFromDynamic(deviceEvent.value);

                    Logger.LogDebug($"Updated state for light switch: {lightSwitch.ToJson()}");

                    await stateManager.StoreStateAsync(state.InstalledAppId, state).ConfigureAwait(false);
                }
            }
            else
            {
                throw new InvalidOperationException($"Unexpected subscriptionName: {subscriptionName}!");
            }
        }