LightManualViewModel.cs 11 KB

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