WorkAreaViewModel.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using CommunityToolkit.Mvvm.ComponentModel;
  2. using CommunityToolkit.Mvvm.Input;
  3. using PLCIOPointTool.Services;
  4. using TwinCAT;
  5. using TwinCAT.Ads;
  6. namespace PLCIOPointTool.ViewModels;
  7. public enum DataType
  8. {
  9. Bool,
  10. INT16, //short
  11. UINT16,//unsigned short
  12. INT32,
  13. FLOAT,
  14. DOUBLE
  15. }
  16. public partial class WorkAreaViewModel : ObservableObject, IDisposable
  17. {
  18. private readonly ILogService _logService;
  19. private readonly IClientService _clientService;
  20. private bool disposedValue;
  21. #region ObservableProperty
  22. [ObservableProperty]
  23. private bool _isConnected = false;
  24. [ObservableProperty]
  25. private string _readSymbolName = string.Empty;
  26. [ObservableProperty]
  27. private string _readValue = string.Empty;
  28. [ObservableProperty]
  29. private string _readValueType = string.Empty;
  30. [ObservableProperty]
  31. private string _writeSymbolName = string.Empty;
  32. [ObservableProperty]
  33. private string _writeValue = string.Empty;
  34. [ObservableProperty]
  35. private DataType[] _dataTypes = Enum.GetValues<DataType>();
  36. [ObservableProperty]
  37. private int _dataTypeId = -1;
  38. #endregion
  39. public WorkAreaViewModel(ILogService logService, IClientService clientService)
  40. {
  41. _logService = logService;
  42. _clientService = clientService;
  43. _clientService.ConnectionStateChanged += OnConnectionStateChanged;
  44. }
  45. [RelayCommand]
  46. private void Read()
  47. {
  48. Task.Run(async () =>
  49. {
  50. var value = await _clientService.ReadValue(ReadSymbolName);
  51. if (value != null)
  52. {
  53. ReadValue = value.ToString()!;
  54. ReadValueType = value.GetType().Name;
  55. }
  56. });
  57. }
  58. [RelayCommand]
  59. private void Write()
  60. {
  61. Task.Run(async () =>
  62. {
  63. try
  64. {
  65. if (DataTypeId < 0 || string.IsNullOrWhiteSpace(WriteValue))
  66. {
  67. _logService.Warning($"Input parameters before writing");
  68. return;
  69. }
  70. var dataType = (DataType)DataTypeId;
  71. object value = dataType switch
  72. {
  73. DataType.Bool => bool.Parse(WriteValue),
  74. DataType.INT16 => short.Parse(WriteValue),
  75. DataType.UINT16 => ushort.Parse(WriteValue),
  76. DataType.INT32 => int.Parse(WriteValue),
  77. DataType.FLOAT => float.Parse(WriteValue),
  78. DataType.DOUBLE => double.Parse(WriteValue),
  79. _ => throw new NotImplementedException()
  80. };
  81. await _clientService.WriteValue(WriteSymbolName, value);
  82. }
  83. catch (Exception ex)
  84. {
  85. _logService.Error($"Got an exception {ex}");
  86. }
  87. });
  88. }
  89. private void OnConnectionStateChanged(object? sender, ConnectionState e)
  90. {
  91. IsConnected = e switch
  92. {
  93. ConnectionState.Connected => true,
  94. ConnectionState.Disconnected => false,
  95. _ => throw new NotImplementedException()
  96. };
  97. }
  98. #region Dispose
  99. protected virtual void Dispose(bool disposing)
  100. {
  101. if (!disposedValue)
  102. {
  103. if (disposing)
  104. {
  105. // TODO: dispose managed state (managed objects)
  106. }
  107. // TODO: free unmanaged resources (unmanaged objects) and override finalizer
  108. // TODO: set large fields to null
  109. disposedValue = true;
  110. }
  111. }
  112. // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
  113. // ~WorkAreaViewModel()
  114. // {
  115. // // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  116. // Dispose(disposing: false);
  117. // }
  118. public void Dispose()
  119. {
  120. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  121. Dispose(disposing: true);
  122. GC.SuppressFinalize(this);
  123. }
  124. #endregion
  125. }