using Aurora; using Aurora.Api; using Aurora.View; using Hermes; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Remoting.Messaging; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; namespace AuroraLight.View { public class KsLightModel : NotifyPropertyChangedBase { #region Binding Properties private bool _IsActive1; public bool IsActive1 { get { return _IsActive1; } set { SetProperty(ref _IsActive1, value); if (_IsActive1) { _IsActiveiconsC1 = Visibility.Visible; _IsActiveiconsD1 = Visibility.Hidden; } else { _IsActiveiconsC1 = Visibility.Hidden; _IsActiveiconsD1 = Visibility.Visible; } } } private Visibility _IsActiveiconsC1 = Visibility.Hidden; public Visibility IsActiveiconsC1 { get { return _IsActiveiconsC1; } set { SetProperty(ref _IsActiveiconsC1, value); } } private Visibility _IsActiveiconsD1 = Visibility.Hidden; public Visibility IsActiveiconsD1 { get { return _IsActiveiconsD1; } set { SetProperty(ref _IsActiveiconsD1, value); } } private string _SliderName1; public string SliderName1 { get => _SliderName1; set { SetProperty(ref _SliderName1, value); } } private bool _IsActive2; public bool IsActive2 { get { return _IsActive2; } set { SetProperty(ref _IsActive2, value); if (_IsActive2) { _IsActiveiconsC2 = Visibility.Visible; _IsActiveiconsD2 = Visibility.Hidden; } else { _IsActiveiconsC2 = Visibility.Hidden; _IsActiveiconsD2 = Visibility.Visible; } } } private bool _IsActiveALL; public bool IsActiveALL { get { return _IsActiveALL; } set { SetProperty(ref _IsActiveALL, value); } } private Visibility _IsActiveiconsC2 = Visibility.Hidden; public Visibility IsActiveiconsC2 { get { return _IsActiveiconsC2; } set { SetProperty(ref _IsActiveiconsC2, value); } } private Visibility _IsActiveiconsD2 = Visibility.Hidden; public Visibility IsActiveiconsD2 { get { return _IsActiveiconsD2; } set { SetProperty(ref _IsActiveiconsD2, value); } } private string _SliderName2; public string SliderName2 { get => _SliderName2; set { SetProperty(ref _SliderName2, value); } } private int _nSliderValue1; public int nSliderValue1 { get => _nSliderValue1; set { try { if (SetProperty(ref _nSliderValue1, value)) { if (_LightDevice1 != null && _LightDevice1._light != null) _LightDevice1._light.SetLightValueInternal(cbChannel1, value); var config = _aurora?.DeviceManager?.GetDeviceConfig(_deviceId1) as LightConfig; if (config != null) { config.SetChannelLevel(cbChannel1, value); } } } catch (Exception ex) { } } } private int _nSliderValue2; public int nSliderValue2 { get => _nSliderValue2; set { try { if (SetProperty(ref _nSliderValue2, value)) { if (_LightDevice2 != null && _LightDevice2._light != null) _LightDevice2._light.SetLightValueInternal(cbChannel2, value); var config = _aurora?.DeviceManager?.GetDeviceConfig(_deviceId2) as LightConfig; if (config != null) { config.SetChannelLevel(cbChannel2, 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 _buttonOnOffToggle1; public bool buttonOnOffToggle1 { get => _buttonOnOffToggle1; set => SetProperty(ref _buttonOnOffToggle1, value); } private bool _buttonOnOffToggle2; public bool buttonOnOffToggle2 { get => _buttonOnOffToggle2; set => SetProperty(ref _buttonOnOffToggle2, value); } private string _strButtonOnOff1; public string strButtonOnOff1 { get => _strButtonOnOff1; set => SetProperty(ref _strButtonOnOff1, value); } private string _strButtonOnOff2; public string strButtonOnOff2 { get => _strButtonOnOff2; set => SetProperty(ref _strButtonOnOff2, value); } private List _lsChannel1; public List lsChannel1 { get => _lsChannel1; set => SetProperty(ref _lsChannel1, value); } private List _lsChannel2; public List lsChannel2 { get => _lsChannel2; set => SetProperty(ref _lsChannel2, value); } private int _cbChannel1; public int cbChannel1 { get => _cbChannel1; set { if (SetProperty(ref _cbChannel1, value)) { var config = _aurora?.DeviceManager?.GetDeviceConfig(_deviceId1) as LightConfig; if (config != null) { int chLevel = config.GetChannelLevel(value); if (_nSliderValue1 != chLevel) { _nSliderValue1 = chLevel; NotifyPropertyChanged(nameof(nSliderValue1)); } } } } } private int _cbChannel2; public int cbChannel2 { get => _cbChannel2; set { if (SetProperty(ref _cbChannel2, value)) { var config = _aurora?.DeviceManager?.GetDeviceConfig(_deviceId2) as LightConfig; if (config != null) { int chLevel = config.GetChannelLevel(value); if (_nSliderValue2 != chLevel) { _nSliderValue2 = chLevel; NotifyPropertyChanged(nameof(nSliderValue2)); } } } } } private bool _buttonOnOffToggleALL; public bool buttonOnOffToggleALL { get => _buttonOnOffToggleALL; set => SetProperty(ref _buttonOnOffToggleALL, value); } private int _nSliderValueALL; public int nSliderValueALL { get => _nSliderValueALL; set { try { if (SetProperty(ref _nSliderValueALL, value)) { if (_LightDevice1 != null && _LightDevice1._light != null) { _LightDevice1._light.SetLightValueInternal(cbChannelALL, value); } if (_LightDevice2 != null && _LightDevice2._light != null) { _LightDevice2._light.SetLightValueInternal(cbChannelALL, value); } var config1 = _aurora?.DeviceManager?.GetDeviceConfig(_deviceId1) as LightConfig; if (config1 != null) { config1.SetChannelLevel(cbChannelALL, value); } var config2 = _aurora?.DeviceManager?.GetDeviceConfig(_deviceId2) as LightConfig; if (config2 != null) { config2.SetChannelLevel(cbChannelALL, value); } nSliderValue1 = value; nSliderValue2 = value; } } catch (Exception ex) { } } } private List _lsChannelALL; public List lsChannelALL { get => _lsChannelALL; set { SetProperty(ref _lsChannelALL, value); } } private int _cbChannelALL; public int cbChannelALL { get => _cbChannelALL; set { SetProperty(ref _cbChannelALL, value); cbChannel1 = value; cbChannel2 = value; } } private string _strButtonOnOffALL; public string strButtonOnOffALL { get => _strButtonOnOffALL; set => SetProperty(ref _strButtonOnOffALL, value); } #endregion #region EventCommands public ICommand CMD_LightOnOff1 { get; } public ICommand CMD_LightOnOff2 { get; } public ICommand CMD_LightOnOffALL { get; } public ICommand CMD_Reload { get; } public ICommand CMD_Save { get; } public ICommand CMD_GetStat { get; } #endregion private IAuroraEnvironment _aurora; private string _deviceId1; private string _deviceId2; private LightDevice _LightDevice1; private LightDevice _LightDevice2; public KsLightModel() { } public KsLightModel(IAuroraEnvironment aurora, string deviceId1, string deviceId2) { _aurora = aurora; _deviceId1 = deviceId1; _deviceId2 = deviceId2; _LightDevice1 = aurora.DeviceManager[deviceId1] as LightDevice; _LightDevice2 = aurora.DeviceManager[deviceId2] as LightDevice; IsActive1 = true; IsActive2 = true; IsActiveALL = true; SliderName1 = _LightDevice1 != null ? _LightDevice1.DeviceId : deviceId1; SliderName2 = _LightDevice2 != null ? _LightDevice2.DeviceId : deviceId2; #region Initialize Binding Properties nSliderMax = 100; _nSliderValue1 = 0; _nSliderValue2 = 0; _nSliderValueALL = 0; if ((_LightDevice1 != null && _LightDevice1._lightType == eLight.ERI) && (_LightDevice2 != null && _LightDevice2._lightType == eLight.ERI)) { nChannel = 4; nSliderMax = 255; } else nChannel = 8; if (lsChannel1 != null) lsChannel1.Clear(); else lsChannel1 = new List(); int nChannelMax1 = nChannel; // default setting for (int i = 0; i < nChannelMax1; i++) lsChannel1.Add(i); if (lsChannel2 != null) lsChannel2.Clear(); else lsChannel2 = new List(); int nChannelMax2 = nChannel; // default setting for (int i = 0; i < nChannelMax2; i++) lsChannel2.Add(i); lsChannelALL = new List(); int nChannelMaxALL = nChannel; // default setting for (int i = 0; i < nChannelMaxALL; i++) lsChannelALL.Add(i); // Read saved values from LightConfig var config1 = _aurora?.DeviceManager?.GetDeviceConfig(_deviceId1) as LightConfig; var config2 = _aurora?.DeviceManager?.GetDeviceConfig(_deviceId2) as LightConfig; _nSliderValue1 = config1 != null ? config1.GetChannelLevel(cbChannel1) : 200; _nSliderValue2 = config2 != null ? config2.GetChannelLevel(cbChannel2) : 200; _nSliderValueALL = _nSliderValue1; NotifyPropertyChanged(nameof(nSliderValue1)); NotifyPropertyChanged(nameof(nSliderValue2)); NotifyPropertyChanged(nameof(nSliderValueALL)); // Turn on lights with configured brightness values if (_LightDevice1 != null && _LightDevice1._light != null) { _LightDevice1._light.SetLightValueInternal(cbChannel1, _nSliderValue1); } if (_LightDevice2 != null && _LightDevice2._light != null) { _LightDevice2._light.SetLightValueInternal(cbChannel2, _nSliderValue2); } buttonOnOffToggle1 = false; strButtonOnOff1 = "OFF"; buttonOnOffToggle2 = false; strButtonOnOff2 = "OFF"; buttonOnOffToggleALL = false; strButtonOnOffALL = "OFF"; #endregion #region DelegateCommands CMD_LightOnOff1 = new DelegateCommand(LightOnOff1); CMD_LightOnOff2 = new DelegateCommand(LightOnOff2); CMD_LightOnOffALL = new DelegateCommand(LightOnOffALL); CMD_Reload = new DelegateCommand(ReLoad); CMD_Save = new DelegateCommand(Save); CMD_GetStat = new DelegateCommand(GetStat); #endregion } private void GetStat(object obj) { MessageUtil.ShowErrorToast($"Unexpected Error : Duplicated Header : {IsActive1}"); } private void ReLoad(object obj) { if (_LightDevice1 != null && _LightDevice1.IsActivated) { IsActive1 = true; } else { IsActive1 = false; } if (_LightDevice2 != null && _LightDevice2.IsActivated) { IsActive2 = true; } else { IsActive2 = false; } if(IsActive1 && IsActive2) { IsActiveALL = true; } else { IsActiveALL = false; } if (_aurora != null && _aurora.DeviceManager != null) { var config1 = _aurora.DeviceManager.GetDeviceConfig(_deviceId1) as LightConfig; if (config1 != null) { _nSliderValue1 = config1.GetChannelLevel(cbChannel1); NotifyPropertyChanged(nameof(nSliderValue1)); } var config2 = _aurora.DeviceManager.GetDeviceConfig(_deviceId2) as LightConfig; if (config2 != null) { _nSliderValue2 = config2.GetChannelLevel(cbChannel2); NotifyPropertyChanged(nameof(nSliderValue2)); } } } private void Save(object obj) { try { if (_aurora != null && _aurora.DeviceManager != null) { var config1 = _aurora.DeviceManager.GetDeviceConfig(_deviceId1) as LightConfig; if (config1 != null) { config1.SetChannelLevel(cbChannel1, nSliderValue1); _aurora.DeviceManager.SaveDeviceConfig(_deviceId1); } var config2 = _aurora.DeviceManager.GetDeviceConfig(_deviceId2) as LightConfig; if (config2 != null) { config2.SetChannelLevel(cbChannel2, nSliderValue2); _aurora.DeviceManager.SaveDeviceConfig(_deviceId2); } MessageUtil.ShowSuccessToast("Light configuration saved."); } } catch (Exception ex) { MessageUtil.ShowErrorToast($"Failed to save light config: {ex.Message}"); } } private void LightOnOff1(object obj) { if (_LightDevice1 != null && _LightDevice1._light != null) { if (_buttonOnOffToggle1 == true) { // light on , button text on -> off toggle _LightDevice1._light.SetLightValueInternal(cbChannel1, nSliderValue1); _buttonOnOffToggle1 = false; strButtonOnOff1 = "OFF"; } else { // light off , button text off -> on toggle _LightDevice1._light.OffLightInternal(cbChannel1); _buttonOnOffToggle1 = true; strButtonOnOff1 = "ON"; } } } private void LightOnOff2(object obj) { if (_LightDevice2 != null && _LightDevice2._light != null) { if (_buttonOnOffToggle2 == true) { // light on , button text on -> off toggle _LightDevice2._light.SetLightValueInternal(cbChannel2, nSliderValue2); buttonOnOffToggle2 = false; strButtonOnOff2 = "OFF"; } else { // light off , button text off -> on toggle _LightDevice2._light.OffLightInternal(cbChannel2); buttonOnOffToggle2 = true; strButtonOnOff2 = "ON"; } } } private void LightOnOffALL(object obj) { if (_LightDevice1.IsActivated && _LightDevice2.IsActivated) { if (_buttonOnOffToggleALL == true) { _LightDevice1._light.SetLightValueInternal(cbChannel1, nSliderValue1); _LightDevice2._light.SetLightValueInternal(cbChannel2, nSliderValue2); buttonOnOffToggle1 = false; buttonOnOffToggle2 = false; buttonOnOffToggleALL = false; strButtonOnOff1 = "OFF"; strButtonOnOff2 = "OFF"; strButtonOnOffALL = "OFF"; } else { _LightDevice1._light.OffLightInternal(cbChannel1); _LightDevice2._light.OffLightInternal(cbChannel2); buttonOnOffToggle1 = true; buttonOnOffToggle2 = true; buttonOnOffToggleALL = true; strButtonOnOff1 = "ON"; strButtonOnOff2 = "ON"; strButtonOnOffALL = "ON"; } } else { MessageUtil.ShowErrorToast($"Deivce Light Not Active: Device 1[{_LightDevice1.IsActivated}] / Device 2[{_LightDevice2.IsActivated}]"); } } //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; //} } }