WorkAreaViewModel.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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
  12. {
  13. private readonly ILogService _logService;
  14. private readonly IInfoSendingService _infoSendingService;
  15. private readonly Hardwares _hardwares;
  16. [ObservableProperty]
  17. private ConcurrentDictionary<byte, Mini8Data> _mini8;
  18. [ObservableProperty]
  19. private int _selectedMini8Index = -1;
  20. [ObservableProperty]
  21. private ConcurrentDictionary<byte, ChannelData> _mini8Channel;
  22. [ObservableProperty]
  23. private int _selectedChannelIndex = -1;
  24. [ObservableProperty]
  25. private AlarmType[] _alarmTypes;
  26. [ObservableProperty]
  27. private int _selectedAlarmTypeIndex = -1;
  28. [ObservableProperty]
  29. private float _pV;
  30. [ObservableProperty]
  31. private float _caps;
  32. [ObservableProperty]
  33. private float _floor;
  34. [ObservableProperty]
  35. private ObservableCollection<string> _receivedTlvData;
  36. [ObservableProperty]
  37. [NotifyCanExecuteChangedFor(nameof(SendInfoCommand))]
  38. private bool _hasConnection;
  39. public WorkAreaViewModel(ILogService logService, ISharedConfig sharedConfig ,IInfoSendingService infoSendingService)
  40. {
  41. if(sharedConfig.Hardwares is null)
  42. {
  43. throw new ArgumentNullException(nameof(sharedConfig.Hardwares));
  44. }
  45. _hardwares = sharedConfig.Hardwares;
  46. _mini8 = _hardwares.Mini8s;
  47. _mini8Channel = [];
  48. _alarmTypes = Enum.GetValues<AlarmType>();
  49. _receivedTlvData = [];
  50. _logService= logService;
  51. _infoSendingService = infoSendingService;
  52. _infoSendingService.ConnectionChanged += OnConnectionChanged;
  53. _infoSendingService.DataReceived += OnDataReceived;
  54. }
  55. partial void OnSelectedMini8IndexChanged(int value)
  56. {
  57. SelectedChannelIndex = -1;
  58. SelectedAlarmTypeIndex = -1;
  59. Mini8Channel = _hardwares.Mini8Channels[Mini8.ElementAt(value).Key];
  60. }
  61. partial void OnSelectedChannelIndexChanged(int value)
  62. {
  63. SelectedAlarmTypeIndex = -1;
  64. }
  65. [RelayCommand(CanExecute = nameof(HasConnection))]
  66. private void SendInfo()
  67. {
  68. if (SelectedMini8Index == -1 || SelectedChannelIndex == -1 || SelectedAlarmTypeIndex == -1)
  69. {
  70. _logService.Log("Failed to send information, please select every ComboBox");
  71. return;
  72. }
  73. // ChannelAlarmNotify = 1
  74. _infoSendingService.Send(1, new ST_ALARM()
  75. {
  76. Mini8Index = Mini8.ElementAt(SelectedMini8Index).Key,
  77. ChannelIndex = Mini8Channel.ElementAt(SelectedChannelIndex).Key,
  78. PV = PV,
  79. Caps = Caps,
  80. Floor = Floor,
  81. AlarmType = (AlarmType)SelectedAlarmTypeIndex
  82. });
  83. }
  84. private void OnConnectionChanged(object? sender, (bool, UniversalNetFrame451.IO.TcpConnection) e)
  85. {
  86. App.Current.Dispatcher.Invoke(() =>
  87. {
  88. HasConnection = e.Item1;
  89. });
  90. }
  91. private void OnDataReceived(object? sender, (ushort, TLVProtocal.TlvData) e)
  92. {
  93. string fileName = Encoding.UTF8.GetString(e.Item2.RawData);
  94. App.Current.Dispatcher.Invoke(() =>
  95. {
  96. if (ReceivedTlvData.Count >= 50)
  97. {
  98. ReceivedTlvData.RemoveAt(ReceivedTlvData.Count - 1);
  99. }
  100. ReceivedTlvData.Insert(0, e.Item2.DateTime.ToString() + $" {fileName}");
  101. });
  102. }
  103. }