AdapterLib.MockLightingServiceHandler.TransitionLampStateAsync C# (CSharp) Method

TransitionLampStateAsync() private method

private TransitionLampStateAsync ( ulong Timestamp, BridgeRT NewState, uint TransitionPeriod ) : void
Timestamp ulong
NewState BridgeRT
TransitionPeriod uint
return void
        private async void TransitionLampStateAsync(ulong Timestamp, BridgeRT.State NewState, uint TransitionPeriod)
        {
            if (Timestamp > 0)
                await Task.Delay((int)Timestamp);
            System.Threading.CancellationToken token = System.Threading.CancellationToken.None;
            lock (transitionLock)
            {
                if (transitionCancellation != null)
                {
                    //Cancel any ongoing transitions
                    transitionCancellation.Cancel();
                }
                transitionCancellation = new System.Threading.CancellationTokenSource();
                token = transitionCancellation.Token;
            }
            var steps = Math.Floor(30d / (1000d / TransitionPeriod));
            var stepdelay = TransitionPeriod / steps;
            var startHue = LampState_Hue;
            var startBrightness = LampState_Brightness;
            var startSaturation = LampState_Saturation;
            var startTemp = LampState_ColorTemp;
            if (NewState.IsOn.HasValue && NewState.IsOn.Value)
                LampState_OnOff = true;
            if(NewState.ColorTemp.HasValue && LampDetails_VariableColorTemp)
            {
                //Clip to supported temperatures
                if (NewState.ColorTemp.Value > kelvinToUInt(LampDetails_MaxTemperature))
                    NewState.ColorTemp = kelvinToUInt(LampDetails_MaxTemperature);
                else if (NewState.ColorTemp.Value < kelvinToUInt(LampDetails_MinTemperature))
                    NewState.ColorTemp = kelvinToUInt(LampDetails_MinTemperature);
            }
            for (int i = 1; i < steps; i++)
            {
                if(NewState.Hue.HasValue && LampDetails_Color)
                {
                    var inc = (NewState.Hue.Value - (double)startHue) / steps;
                    this.hue = (uint)(startHue + inc * i);
                    OnPropertyChanged(nameof(LampState_Hue));
                }
                if (NewState.Brightness.HasValue && LampDetails_Dimmable)
                {
                    var inc = (NewState.Brightness.Value - (double)startBrightness) / steps;
                    this.brightness = (uint)(startBrightness + inc * i);
                    OnPropertyChanged(nameof(LampState_Brightness));
                }
                if (NewState.Saturation.HasValue && LampDetails_Color)
                {
                    var inc = (NewState.Saturation.Value - (double)startSaturation) / steps;
                    this.saturation = (uint)(startSaturation + inc * i);
                    OnPropertyChanged(nameof(LampState_Saturation));
                }
                if (NewState.ColorTemp.HasValue && LampDetails_VariableColorTemp)
                {
                    var inc = (NewState.ColorTemp.Value - (double)startTemp) / steps;
                    this.colorTemp = (uint)(startTemp + inc * i);
                    OnPropertyChanged(nameof(LampState_ColorTemp));
                }
                OnPropertyChanged(nameof(Color));
                OnPropertyChanged(nameof(ColorFullBrightness));
                await Task.Delay((int)stepdelay);
                if (token.IsCancellationRequested)
                    return;
            }
            if (NewState.Hue.HasValue && LampDetails_Color)
            {
                this.hue = NewState.Hue.Value;
                OnPropertyChanged(nameof(LampState_Hue));
            }
            if (NewState.Brightness.HasValue && LampDetails_Dimmable)
            {
                this.brightness = NewState.Brightness.Value;
                OnPropertyChanged(nameof(LampState_Brightness));
            }
            if (NewState.Saturation.HasValue && LampDetails_Color)
            {
                this.saturation = NewState.Saturation.Value;
                OnPropertyChanged(nameof(LampState_Saturation));
            }
            if (NewState.ColorTemp.HasValue && LampDetails_VariableColorTemp)
            {
                this.colorTemp = NewState.ColorTemp.Value;
                OnPropertyChanged(nameof(LampState_ColorTemp));
            }
            OnPropertyChanged(nameof(Color));
            OnPropertyChanged(nameof(ColorFullBrightness));
            if (NewState.IsOn.HasValue && !NewState.IsOn.Value)
                LampState_OnOff = false;
            LampStateChanged?.Invoke(this, EventArgs.Empty);
        }