using Aurora; using Aurora.View; using System; using System.Collections.Generic; using System.Windows.Input; namespace AuroraLight.View { public class UCLightViewModel : NotifyPropertyChangedBase { #region Binding Properties private int _nSliderValue; public int nSliderValue { get => _nSliderValue; set { try { if (SetProperty(ref _nSliderValue, value)) { if (_LightDevice != null && _LightDevice._light != null) _LightDevice._light.SetLightValueInternal(cbChannel, value); var config = _aurora?.DeviceManager?.GetDeviceConfig(_deviceId) as LightConfig; if (config != null) { config.SetChannelLevel(cbChannel, value); } } } catch (Exception ex) { } } } private int _nSliderMax; public int nSliderMax { get => _nSliderMax; set => SetProperty(ref _nSliderMax, value); } private int _nChannel; public int nChannel { get => _nChannel; set => SetProperty(ref _nChannel, value); } private bool _buttonOnOffToggle; public bool buttonOnOffToggle { get => _buttonOnOffToggle; set => SetProperty(ref _buttonOnOffToggle, value); } private string _strButtonOnOff; public string strButtonOnOff { get => _strButtonOnOff; set => SetProperty(ref _strButtonOnOff, value); } private List _lsChannel; public List lsChannel { get => _lsChannel; set => SetProperty(ref _lsChannel, value); } private int _cbChannel; public int cbChannel { get => _cbChannel; set { if (SetProperty(ref _cbChannel, value)) { var config = _aurora?.DeviceManager?.GetDeviceConfig(_deviceId) as LightConfig; if (config != null) { int chLevel = config.GetChannelLevel(value); if (_nSliderValue != chLevel) { _nSliderValue = chLevel; NotifyPropertyChanged(nameof(nSliderValue)); } } } } } #endregion #region EventCommands public ICommand CMD_LightOnOff { get; } public ICommand CMD_Save { get; } #endregion private IAuroraEnvironment _aurora; private string _deviceId; private LightDevice _LightDevice; public UCLightViewModel(IAuroraEnvironment aurora, string deviceId) { _aurora = aurora; _deviceId = deviceId; _LightDevice = aurora.DeviceManager[deviceId] as LightDevice; #region Initialize Binding Properties nSliderMax = 100; _nSliderValue = 0; if (_LightDevice != null && _LightDevice._lightType == eLight.ERI) { nChannel = 4; nSliderMax = 255; } else nChannel = 8; if (lsChannel != null) lsChannel.Clear(); else lsChannel = new List(); int nChannelMax = nChannel; // default setting for (int i = 0; i < nChannelMax; i++) lsChannel.Add(i); var config = _aurora?.DeviceManager?.GetDeviceConfig(_deviceId) as LightConfig; _nSliderValue = config != null ? config.GetChannelLevel(cbChannel) : 200; NotifyPropertyChanged(nameof(nSliderValue)); if (_LightDevice != null && _LightDevice._light != null) { _LightDevice._light.SetLightValueInternal(cbChannel, _nSliderValue); } buttonOnOffToggle = false; strButtonOnOff = "OFF"; #endregion #region DelegateCommands CMD_LightOnOff = new DelegateCommand(LightOnOff); CMD_Save = new DelegateCommand(Save); #endregion } private void Save(object obj) { try { if (_aurora != null && _aurora.DeviceManager != null) { var config = _aurora.DeviceManager.GetDeviceConfig(_deviceId) as LightConfig; if (config != null) { config.SetChannelLevel(cbChannel, nSliderValue); _aurora.DeviceManager.SaveDeviceConfig(_deviceId); } MessageUtil.ShowSuccessToast("Light configuration saved."); } } catch (Exception ex) { MessageUtil.ShowErrorToast($"Failed to save light config: {ex.Message}"); } } private void LightOnOff(object obj) { if (_LightDevice != null && _LightDevice._light != null) { if (buttonOnOffToggle == true) { // light on , button text on -> off toggle _LightDevice._light.SetLightValueInternal(cbChannel, nSliderValue); buttonOnOffToggle = false; strButtonOnOff = "OFF"; } else { // light off , button text off -> on toggle _LightDevice._light.OffLightInternal(cbChannel); buttonOnOffToggle = true; strButtonOnOff = "ON"; } } } private void SetChannelMax(int nChannelMax) { lsChannel.Clear(); for (int i = 0; i < nChannelMax; i++) lsChannel.Add(i); } private void SetLightDevice(ILight LightDevice) { _LightDevice._light = LightDevice; } } }