LightManualViewModel.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. using Prism.Commands;
  2. using Prism.Events;
  3. using Prism.Ioc;
  4. using Prism.Mvvm;
  5. using Prism.Regions;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Linq;
  10. using System.Threading.Tasks;
  11. using TeamAAS_VP.Core;
  12. using TeamAAS_VP.Data;
  13. using TeamAAS_VP.Events;
  14. using TeamAAS_VP.Interfaces;
  15. using TeamAAS_VP.Models.Lights;
  16. using TeamAAS_VP.Resources.Languages;
  17. using TeamAAS_VP.Core.Lights;
  18. namespace TeamAAS_VP.ViewModels.DebugMod
  19. {
  20. public class LightManualViewModel : BindableBase, INavigationAware
  21. {
  22. IContainerProvider _container;
  23. IEventAggregator _eventAggregator;
  24. ILightManagerService _light_manager_service; // fallback
  25. ILightManagerService _lightManagerService;
  26. IConfigService _configService;
  27. ISystemDatabaseService _systemDatabaseService;
  28. #region 属性
  29. private ObservableCollection<ChannelItem> _Channels = new ObservableCollection<ChannelItem>();
  30. public ObservableCollection<ChannelItem> Channels
  31. {
  32. get => _Channels; set => SetProperty(ref _Channels, value);
  33. }
  34. #endregion
  35. #region 命令
  36. private DelegateCommand _LoadedCommand;
  37. public DelegateCommand LoadedCommand =>
  38. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  39. #endregion
  40. #region 事件
  41. #endregion
  42. public LightManualViewModel(IContainerProvider container, IEventAggregator ea, ILightManagerService lightManagerService, IConfigService configService, ISystemDatabaseService systemDatabaseService)
  43. {
  44. _container = container;
  45. _eventAggregator = ea;
  46. _light_manager_service = lightManagerService;
  47. _lightManagerService = lightManagerService;
  48. _configService = configService;
  49. _systemDatabaseService = systemDatabaseService;
  50. //订阅权限登录事件
  51. _eventAggregator.GetEvent<UserLoginNotification>().Subscribe(LoginChange);
  52. }
  53. #region 方法
  54. void ExecuteLoadedCommand()
  55. {
  56. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  57. {
  58. IsAllowEdit = false;
  59. }
  60. else
  61. {
  62. IsAllowEdit = true;
  63. }
  64. }
  65. #endregion
  66. #region 继承
  67. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  68. {
  69. continuationCallback(true);
  70. }
  71. public async void OnNavigatedTo(NavigationContext navigationContext)
  72. {
  73. // load channels from light manager service
  74. Channels.Clear();
  75. try
  76. {
  77. var globals = _lightManagerService.GlobalChannels.ToList();
  78. foreach (var kv in globals)
  79. {
  80. var item = new ChannelItem(kv.Key, (ILightChannel)kv.Value, _lightManagerService, _configService, _eventAggregator);
  81. Channels.Add(item);
  82. await item.InitializeAsync();
  83. }
  84. }
  85. catch (Exception ex)
  86. {
  87. // ignore or notify
  88. }
  89. }
  90. public bool IsNavigationTarget(NavigationContext navigationContext)
  91. {
  92. return true;
  93. }
  94. public void OnNavigatedFrom(NavigationContext navigationContext)
  95. {
  96. // nothing
  97. }
  98. #endregion
  99. private bool _IsAllowEdit = false;
  100. public bool IsAllowEdit
  101. {
  102. get { return _IsAllowEdit; }
  103. set { SetProperty(ref _IsAllowEdit, value); }
  104. }
  105. private void LoginChange(Models.User user)
  106. {
  107. if (user.userPart == Enums.UserPart.Operator)
  108. {
  109. IsAllowEdit = false;
  110. }
  111. else
  112. {
  113. IsAllowEdit = true;
  114. }
  115. }
  116. // Nested ChannelItem class used as item VM
  117. public class ChannelItem : BindableBase
  118. {
  119. private readonly int _globalId;
  120. private readonly ILightChannel _channel;
  121. private readonly ILightManagerService _lightManagerService;
  122. private readonly IConfigService _configService;
  123. private readonly IEventAggregator _eventAggregator;
  124. public int GlobalId => _globalId;
  125. private string _Name;
  126. public string Name { get => _Name; set => SetProperty(ref _Name, value); }
  127. private int _PendingBrightness;
  128. public int PendingBrightness { get => _PendingBrightness; set => SetProperty(ref _PendingBrightness, value); }
  129. private int _CurrentBrightness;
  130. public int CurrentBrightness { get => _CurrentBrightness; set => SetProperty(ref _CurrentBrightness, value); }
  131. private bool _IsOn;
  132. public bool IsOn { get => _IsOn; set => SetProperty(ref _IsOn, value); }
  133. public DelegateCommand SaveCommand { get; }
  134. public DelegateCommand TurnOnCommand { get; }
  135. public DelegateCommand TurnOffCommand { get; }
  136. public ChannelItem(int globalId, ILightChannel channel, ILightManagerService lightManagerService, IConfigService configService, IEventAggregator eventAggregator)
  137. {
  138. _globalId = globalId;
  139. _channel = channel;
  140. _lightManagerService = lightManagerService;
  141. _configService = configService;
  142. _eventAggregator = eventAggregator;
  143. Name = channel?.ChannelName ?? $"Channel {_globalId}";
  144. PendingBrightness = 0;
  145. CurrentBrightness = 0;
  146. IsOn = false;
  147. SaveCommand = new DelegateCommand(async () => await SaveAsync());
  148. TurnOnCommand = new DelegateCommand(async () => await TurnOnAsync());
  149. TurnOffCommand = new DelegateCommand(async () => await TurnOffAsync());
  150. }
  151. public async Task InitializeAsync()
  152. {
  153. try
  154. {
  155. if (_channel != null)
  156. {
  157. var b = await _channel.GetBrightnessAsync();
  158. CurrentBrightness = b;
  159. PendingBrightness = b;
  160. IsOn = await _channel.GetStatusAsync();
  161. }
  162. }
  163. catch
  164. {
  165. }
  166. }
  167. private async Task SaveAsync()
  168. {
  169. try
  170. {
  171. var success = await _lightManagerService.SetGlobalChannelBrightnessAsync(_globalId, PendingBrightness);
  172. if (success)
  173. {
  174. CurrentBrightness = PendingBrightness;
  175. IsOn = PendingBrightness > 0;
  176. // also save as default in config
  177. _configService.UpdateChannelDefaultBrightness(_globalId, PendingBrightness);
  178. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"保存完成: {Name} -> {PendingBrightness}", Duration = 1 });
  179. }
  180. else
  181. {
  182. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"保存失败: {Name}", Duration = 1 });
  183. }
  184. }
  185. catch (Exception ex)
  186. {
  187. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"保存异常: {ex.Message}", Duration = 1 });
  188. }
  189. }
  190. private async Task TurnOnAsync()
  191. {
  192. try
  193. {
  194. var success = await _lightManagerService.TurnOnGlobalChannelAsync(_globalId);
  195. if (success)
  196. {
  197. IsOn = true;
  198. // refresh brightness
  199. var b = await _channel.GetBrightnessAsync();
  200. PendingBrightness = b;
  201. CurrentBrightness = b;
  202. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"打开通道: {Name}", Duration = 1 });
  203. }
  204. }
  205. catch (Exception ex)
  206. {
  207. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"打开异常: {ex.Message}", Duration = 1 });
  208. }
  209. }
  210. private async Task TurnOffAsync()
  211. {
  212. try
  213. {
  214. var success = await _lightManagerService.TurnOffGlobalChannelAsync(_globalId);
  215. if (success)
  216. {
  217. IsOn = false;
  218. PendingBrightness = 0;
  219. CurrentBrightness = 0;
  220. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"关闭通道: {Name}", Duration = 1 });
  221. }
  222. }
  223. catch (Exception ex)
  224. {
  225. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"关闭异常: {ex.Message}", Duration = 1 });
  226. }
  227. }
  228. }
  229. }
  230. }