private async Task ControlDimmer(double destinationLevel) { int powerOnTime = 0; var dest = TimeForPercentage(destinationLevel); var current = TimeForPercentage(_PowerLevel.Value); if (destinationLevel > _PowerLevel) { // If last time dimmer was increasing its value we have to change direction by short time power on if (_CurrentValue > _PreviousCurrentValue && _CurrentValue > 0) { // We add time that we consume on direction change powerOnTime += await ChangeDimmerDirection(); } powerOnTime += (int)(dest - current); Logger.LogInformation($"Set dimmer to {destinationLevel} by waiting {powerOnTime}ms"); } else { // If last time dimmer was decreasing its value we have to change direction by short time power on if (_PreviousCurrentValue > _CurrentValue && _CurrentValue > 0) { powerOnTime += await ChangeDimmerDirection(); } powerOnTime += (int)(current - dest); } ForwardToPowerAdapter(TurnOnCommand.Create(powerOnTime)); }
private async Task CalibrationMinimumLight(IContext context) { if (context.Message is StopCommand stopCommand) { // Ignore previous commands if we receive any if (stopCommand[MessageProperties.Context] == "MAX") { return; } Become(StandardMode); ForwardToPowerAdapter(TurnOffCommand.Default); ResetStateValues(); TryCalculateSpectrum(); Logger.LogInformation($"Calibration of {Uid} : calibration finished with MIN: {_Minimum}, MAX: {_Maximum}, RANGE: {_Range}"); await Task.Delay(WAIT_AFTER_CHANGE).ConfigureAwait(false); ForwardToPowerAdapter(TurnOnCommand.Create(CHANGE_POWER_STATE_TIME)); } else if (context.Message is PropertyChangedEvent minimumState) { await MessageBroker.SendAfterDelay(ActorMessageContext.Create(Self, StopCommand.Create("MIN")), TimeSpan.FromMilliseconds(1500)).ConfigureAwait(false); _Minimum = minimumState.AsDouble(MessageProperties.NewValue); } else { await StandardMode(context).ConfigureAwait(false); } }
private async Task <int> ChangeDimmerDirection() { ForwardToPowerAdapter(TurnOnCommand.Create(SWITCH_CHANGE_DIRECTION)); await Task.Delay(WAIT_AFTER_CHANGE).ConfigureAwait(false); return(SWITCH_CHANGE_DIRECTION); }
private async Task ChangePowerState() { ForwardToPowerAdapter(TurnOnCommand.Create(CHANGE_POWER_STATE_TIME)); await Task.Delay(WAIT_AFTER_CHANGE); }