WorkAreaViewModel.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using AlarmInfoServerSim.Services;
  2. using CommunityToolkit.Mvvm.ComponentModel;
  3. using CommunityToolkit.Mvvm.Input;
  4. using HardwareData;
  5. using RealtimeData;
  6. using RTCommunicatorBase;
  7. using System.Collections.Concurrent;
  8. using System.Collections.ObjectModel;
  9. using System.Text;
  10. namespace AlarmInfoServerSim.ViewModels;
  11. public partial class WorkAreaViewModel : ObservableObject, IDisposable
  12. {
  13. private readonly ILogService _logService;
  14. private readonly IInfoSendingService _infoSendingService;
  15. private readonly Hardwares _hardwares;
  16. private bool disposedValue;
  17. [ObservableProperty]
  18. private ConcurrentDictionary<byte, Mini8Data> _mini8;
  19. [ObservableProperty]
  20. private int _selectedMini8Index = -1;
  21. [ObservableProperty]
  22. private ConcurrentDictionary<byte, ChannelData> _mini8Channel;
  23. [ObservableProperty]
  24. private int _selectedChannelIndex = -1;
  25. [ObservableProperty]
  26. private AlarmType[] _alarmTypes;
  27. [ObservableProperty]
  28. private int _selectedAlarmTypeIndex = -1;
  29. [ObservableProperty]
  30. private float _pV;
  31. [ObservableProperty]
  32. private float _caps;
  33. [ObservableProperty]
  34. private float _floor;
  35. [ObservableProperty]
  36. private ObservableCollection<string> _receivedTlvData;
  37. [ObservableProperty]
  38. [NotifyCanExecuteChangedFor(nameof(SendInfoCommand))]
  39. private bool _hasConnection;
  40. public WorkAreaViewModel(ILogService logService, ISharedConfig sharedConfig ,IInfoSendingService infoSendingService)
  41. {
  42. if(sharedConfig.Hardwares is null)
  43. {
  44. throw new ArgumentNullException(nameof(sharedConfig.Hardwares));
  45. }
  46. _hardwares = sharedConfig.Hardwares;
  47. _mini8 = _hardwares.Mini8s;
  48. _mini8Channel = [];
  49. _alarmTypes = Enum.GetValues<AlarmType>();
  50. _receivedTlvData = [];
  51. _logService= logService;
  52. _infoSendingService = infoSendingService;
  53. _infoSendingService.ConnectionChanged += OnConnectionChanged;
  54. _infoSendingService.DataReceived += OnDataReceived;
  55. }
  56. partial void OnSelectedMini8IndexChanged(int value)
  57. {
  58. SelectedChannelIndex = -1;
  59. SelectedAlarmTypeIndex = -1;
  60. Mini8Channel = _hardwares.Mini8Channels[Mini8.ElementAt(value).Key];
  61. }
  62. partial void OnSelectedChannelIndexChanged(int value)
  63. {
  64. SelectedAlarmTypeIndex = -1;
  65. }
  66. [RelayCommand(CanExecute = nameof(HasConnection))]
  67. private void SendInfo()
  68. {
  69. if (SelectedMini8Index == -1 || SelectedChannelIndex == -1 || SelectedAlarmTypeIndex == -1)
  70. {
  71. _logService.Log("Failed to send information, please select every ComboBox");
  72. return;
  73. }
  74. // ChannelAlarmNotify = 1
  75. _infoSendingService.Send(1, new ST_ALARM()
  76. {
  77. Mini8Index = Mini8.ElementAt(SelectedMini8Index).Key,
  78. ChannelIndex = Mini8Channel.ElementAt(SelectedChannelIndex).Key,
  79. PV = PV,
  80. Caps = Caps,
  81. Floor = Floor,
  82. AlarmType = (AlarmType)SelectedAlarmTypeIndex
  83. });
  84. }
  85. private void OnConnectionChanged(object? sender, (bool, UniversalNetFrame451.IO.TcpConnection) e)
  86. {
  87. App.Current.Dispatcher.Invoke(() =>
  88. {
  89. HasConnection = e.Item1;
  90. });
  91. }
  92. private void OnDataReceived(object? sender, (ushort, TLVProtocal.TlvData) e)
  93. {
  94. string fileName = Encoding.UTF8.GetString(e.Item2.RawData);
  95. App.Current.Dispatcher.Invoke(() =>
  96. {
  97. if (ReceivedTlvData.Count >= 50)
  98. {
  99. ReceivedTlvData.RemoveAt(ReceivedTlvData.Count - 1);
  100. }
  101. ReceivedTlvData.Insert(0, e.Item2.DateTime.ToString() + $" {fileName}");
  102. });
  103. }
  104. protected virtual void Dispose(bool disposing)
  105. {
  106. if (!disposedValue)
  107. {
  108. if (disposing)
  109. {
  110. // TODO: dispose managed state (managed objects)
  111. _infoSendingService.ConnectionChanged -= OnConnectionChanged;
  112. _infoSendingService.DataReceived -= OnDataReceived;
  113. }
  114. // TODO: free unmanaged resources (unmanaged objects) and override finalizer
  115. // TODO: set large fields to null
  116. disposedValue = true;
  117. }
  118. }
  119. // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
  120. // ~WorkAreaViewModel()
  121. // {
  122. // // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  123. // Dispose(disposing: false);
  124. // }
  125. public void Dispose()
  126. {
  127. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  128. Dispose(disposing: true);
  129. GC.SuppressFinalize(this);
  130. }
  131. }