ConnectionViewModel.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls.Primitives;
  7. using System.Windows.Data;
  8. using System.Windows.Input;
  9. using Aitex.Core.RT.DataCenter;
  10. using Aitex.Core.UI.MVVM;
  11. using MECF.Framework.Common.Communications;
  12. using MECF.Framework.Common.DataCenter;
  13. using MECF.Framework.Common.OperationCenter;
  14. using MECF.Framework.UI.Client.ClientBase;
  15. using OpenSEMI.ClientBase;
  16. namespace MECF.Framework.UI.Client.CenterViews.Maitenances.Connections
  17. {
  18. public class ConnectionViewModel : UiViewModelBase, ISupportMultipleSystem
  19. {
  20. public string SystemName { get; set; }
  21. protected override void OnInitialize()
  22. {
  23. base.OnInitialize();
  24. }
  25. protected override void OnActivate()
  26. {
  27. base.OnActivate();
  28. }
  29. protected override void OnDeactivate(bool close)
  30. {
  31. base.OnDeactivate(close);
  32. }
  33. public ObservableCollection<NotifiableConnectionItem> ListConnections { get; set; }
  34. public ICommand ConnectCommand { get; set; }
  35. public ICommand DisconnectCommand { get; set; }
  36. public ConnectionViewModel()
  37. {
  38. ListConnections = new ObservableCollection<NotifiableConnectionItem>();
  39. ConnectCommand = new DelegateCommand<string>(DoConnect);
  40. DisconnectCommand = new DelegateCommand<string>(DoDisconnect);
  41. }
  42. private void DoConnect(string obj)
  43. {
  44. if (DialogBox.Confirm($"Are you sure you want to connect {obj}?"))
  45. {
  46. InvokeClient.Instance.Service.DoOperation($"Connection.Connect", obj);
  47. }
  48. }
  49. private void DoDisconnect(string obj)
  50. {
  51. if (DialogBox.Confirm($"Are you sure you want to disconnect {obj}?"))
  52. {
  53. InvokeClient.Instance.Service.DoOperation($"Connection.Disconnect", obj);
  54. }
  55. }
  56. protected override void Poll()
  57. {
  58. Application.Current.Dispatcher.Invoke(new Action(() =>
  59. {
  60. var connections = QueryDataClient.Instance.Service.GetData("Connection.List");
  61. if (connections == null)
  62. return;
  63. List<NotifiableConnectionItem> items = connections as List<NotifiableConnectionItem>;
  64. foreach (var item in items)
  65. {
  66. if (ListConnections.Count == 0 || (ListConnections.FirstOrDefault(x => x.Name == item.Name) == null))
  67. {
  68. ListConnections.Add(item);
  69. }
  70. else
  71. {
  72. var find = ListConnections.First(x => x.Name == item.Name);
  73. find.IsConnected = find.IsConnected;
  74. find.InvokePropertyChanged("IsConnected");
  75. }
  76. }
  77. InvokePropertyChanged("ListConnections");
  78. }));
  79. }
  80. }
  81. public class IoButton : ToggleButton
  82. {
  83. public static readonly DependencyProperty ONProperty;
  84. static IoButton()
  85. {
  86. ONProperty = DependencyProperty.Register("ON", typeof(bool), typeof(IoButton));
  87. }
  88. public bool ON
  89. {
  90. get { return (bool)GetValue(ONProperty); }
  91. set { SetValue(ONProperty, value); }
  92. }
  93. }
  94. public class BoolBackgroundConverter : IValueConverter
  95. {
  96. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  97. {
  98. bool? ret = (bool?)value;
  99. return ret.HasValue && ret.Value ? "LightBlue" : "Transparent";
  100. }
  101. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  102. {
  103. return null;
  104. }
  105. }
  106. }