ViewBViewModel.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using CommunityToolkit.Mvvm.ComponentModel;
  2. using CommunityToolkit.Mvvm.Input;
  3. using Serilog;
  4. using YZWater.Core.Models;
  5. using YZWater.Core.Services;
  6. namespace YZWater.Core.ViewModels;
  7. /// <summary>
  8. /// 鍙傛暟璁剧疆瑙嗗浘 ViewModel
  9. /// </summary>
  10. public partial class ViewBViewModel : ObservableObject
  11. {
  12. [ObservableProperty]
  13. private string _plcIp = "192.168.0.150";
  14. [ObservableProperty]
  15. private int _plcPort = 5000;
  16. [ObservableProperty]
  17. private bool _autoConnect = true;
  18. [ObservableProperty]
  19. private float _levelHighAlarm = 80f;
  20. [ObservableProperty]
  21. private float _levelLowAlarm = 20f;
  22. [ObservableProperty]
  23. private float _flowHighAlarm = 100f;
  24. [ObservableProperty]
  25. private float _pumpFrequency = 50f;
  26. [ObservableProperty]
  27. private string _connectionStatus = "鏈繛鎺";
  28. [ObservableProperty]
  29. private bool _isConnecting;
  30. // 鈹鈹鈹 缈昏瘧鏂囧瓧 鈹鈹鈹
  31. private readonly LanguageService _lang = LanguageService.Instance;
  32. [ObservableProperty] private string _titleText;
  33. [ObservableProperty] private string _subtitleText;
  34. [ObservableProperty] private string _plcConnectionText;
  35. [ObservableProperty] private string _ipAddressText;
  36. [ObservableProperty] private string _portText;
  37. [ObservableProperty] private string _autoConnectText;
  38. [ObservableProperty] private string _statusText;
  39. [ObservableProperty] private string _testText;
  40. [ObservableProperty] private string _alarmThresholdsText;
  41. [ObservableProperty] private string _levelHighText;
  42. [ObservableProperty] private string _levelLowText;
  43. [ObservableProperty] private string _flowHighText;
  44. [ObservableProperty] private string _pumpSettingsText;
  45. [ObservableProperty] private string _frequencyText;
  46. [ObservableProperty] private string _saveConfigText;
  47. [ObservableProperty] private string _resetDefaultText;
  48. [ObservableProperty] private string _connectedText;
  49. [ObservableProperty] private string _disconnectedText;
  50. [ObservableProperty] private string _connectingText;
  51. public ViewBViewModel()
  52. {
  53. UpdateTexts();
  54. _lang.LanguageChanged += UpdateTexts;
  55. LoadConfig();
  56. }
  57. private void UpdateTexts()
  58. {
  59. TitleText = _lang.Get("Parameters");
  60. SubtitleText = _lang.Get("SysConfig");
  61. PlcConnectionText = _lang.Get("PlcConnection");
  62. IpAddressText = _lang.Get("IpAddress");
  63. PortText = _lang.Get("Port");
  64. AutoConnectText = _lang.Get("AutoConnect");
  65. StatusText = _lang.Get("Status");
  66. TestText = _lang.Get("Test");
  67. AlarmThresholdsText = _lang.Get("AlarmThresholds");
  68. LevelHighText = _lang.Get("LevelHigh");
  69. LevelLowText = _lang.Get("LevelLow");
  70. FlowHighText = _lang.Get("FlowHigh");
  71. PumpSettingsText = _lang.Get("PumpSettings");
  72. FrequencyText = _lang.Get("Frequency");
  73. SaveConfigText = _lang.Get("SaveConfig");
  74. ResetDefaultText = _lang.Get("ResetDefault");
  75. ConnectedText = _lang.Get("Connected");
  76. DisconnectedText = _lang.Get("Disconnected");
  77. ConnectingText = _lang.Get("Connecting");
  78. }
  79. /// <summary>
  80. /// 鍔犺浇閰嶇疆
  81. /// </summary>
  82. [RelayCommand]
  83. private void LoadConfig()
  84. {
  85. var config = ConfigService.GetConfig();
  86. if (config != null)
  87. {
  88. PlcIp = config.PlcIp;
  89. PlcPort = config.PlcPort;
  90. AutoConnect = config.AutoConnect;
  91. LevelHighAlarm = config.LevelHighAlarm;
  92. LevelLowAlarm = config.LevelLowAlarm;
  93. FlowHighAlarm = config.FlowHighAlarm;
  94. PumpFrequency = config.PumpFrequency;
  95. }
  96. Log.Debug("閰嶇疆宸插姞杞");
  97. }
  98. /// <summary>
  99. /// 淇濆瓨閰嶇疆
  100. /// </summary>
  101. [RelayCommand]
  102. private void SaveConfig()
  103. {
  104. var config = new SystemConfig
  105. {
  106. PlcIp = PlcIp,
  107. PlcPort = PlcPort,
  108. AutoConnect = AutoConnect,
  109. LevelHighAlarm = LevelHighAlarm,
  110. LevelLowAlarm = LevelLowAlarm,
  111. FlowHighAlarm = FlowHighAlarm,
  112. PumpFrequency = PumpFrequency
  113. };
  114. ConfigService.UpdateConfig(config);
  115. Log.Information("閰嶇疆宸蹭繚瀛");
  116. }
  117. /// <summary>
  118. /// 娴嬭瘯杩炴帴
  119. /// </summary>
  120. [RelayCommand]
  121. private async Task TestConnectionAsync()
  122. {
  123. IsConnecting = true;
  124. ConnectionStatus = "杩炴帴涓...";
  125. try
  126. {
  127. // 鏇存柊 PLC IP
  128. PlcService.Initialize();
  129. var success = await PlcService.ConnectAsync();
  130. ConnectionStatus = success ? "杩炴帴鎴愬姛" : "杩炴帴澶辫触";
  131. if (success)
  132. {
  133. Log.Information("PLC 杩炴帴娴嬭瘯鎴愬姛");
  134. }
  135. else
  136. {
  137. Log.Warning("PLC 杩炴帴娴嬭瘯澶辫触");
  138. }
  139. }
  140. catch (Exception ex)
  141. {
  142. ConnectionStatus = "杩炴帴寮傚父";
  143. Log.Error(ex, "PLC 杩炴帴娴嬭瘯寮傚父");
  144. }
  145. finally
  146. {
  147. IsConnecting = false;
  148. }
  149. }
  150. /// <summary>
  151. /// 鎭㈠榛樿璁剧疆
  152. /// </summary>
  153. [RelayCommand]
  154. private void ResetToDefault()
  155. {
  156. PlcIp = "192.168.0.150";
  157. PlcPort = 5000;
  158. AutoConnect = true;
  159. LevelHighAlarm = 80f;
  160. LevelLowAlarm = 20f;
  161. FlowHighAlarm = 100f;
  162. PumpFrequency = 50f;
  163. Log.Information("宸叉仮澶嶉粯璁よ缃");
  164. }
  165. }