StatisticsViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Windows;
  7. using Aitex.Core.RT.Log;
  8. using Aitex.Core.Util;
  9. using MECF.Framework.Common.CommonData;
  10. using MECF.Framework.Common.DataCenter;
  11. using MECF.Framework.Common.OperationCenter;
  12. using MECF.Framework.UI.Client.ClientBase;
  13. using OpenSEMI.ClientBase;
  14. using SciChart.Core.Extensions;
  15. namespace EfemDualUI.Views.History.Statistics
  16. {
  17. public class StatsDataListItem : NotifiableItem
  18. {
  19. public string Name { get; set; }
  20. public string Value { get; set; }
  21. public string Description { get; set; }
  22. public string Total { get; set; }
  23. public string LastUpdateTime { get; set; }
  24. public string LastResetTime { get; set; }
  25. public string LastResetTotalTime { get; set; }
  26. public string WarningValue { get; set; }
  27. public string WarningValueSetPoint { get; set; }
  28. public bool WarningEnable { get; set; }
  29. public string AlarmValue { get; set; }
  30. public string AlarmValueSetPoint { get; set; }
  31. public bool AlarmEnable { get; set; }
  32. public bool AlarmTextSaved { get; set; }
  33. public bool WarningTextSaved { get; set; }
  34. public bool IsVisible { get; set; }
  35. }
  36. public class StatsDataListItemRFAndPump : NotifiableItem
  37. {
  38. public string Name { get; set; }
  39. public string Description { get; set; }
  40. public string LastPMTime { get; set; }
  41. public string fromLastPM { get; set; }
  42. public string Total { get; set; }
  43. public string PMInterval { get; set; }
  44. public string PMIntervalSetPoint { get; set; }
  45. public bool AlarmEnable { get; set; }
  46. public bool PMIntervalTextSaved { get; set; }
  47. }
  48. public class StatisticsViewModel : UiViewModelBase
  49. {
  50. private int MenuPermission;
  51. public ObservableCollection<StatsDataListItem> StatData
  52. {
  53. get;
  54. set;
  55. }
  56. public ObservableCollection<StatsDataListItemRFAndPump> StatDataRFAndPump
  57. {
  58. get;
  59. set;
  60. }
  61. protected override void OnInitialize()
  62. {
  63. StatData = new ObservableCollection<StatsDataListItem>();
  64. StatDataRFAndPump = new ObservableCollection<StatsDataListItemRFAndPump>();
  65. base.OnInitialize();
  66. }
  67. protected override void OnActivate()
  68. {
  69. base.OnActivate();
  70. }
  71. protected override void OnDeactivate(bool close)
  72. {
  73. base.OnDeactivate(close);
  74. }
  75. protected override void Poll()
  76. {
  77. try
  78. {
  79. MenuPermission = ClientApp.Instance.GetPermission("Statistics");
  80. string sql = $"SELECT * FROM \"stats_data\" order by \"name\" ASC;";
  81. DataTable dbData = QueryDataClient.Instance.Service.QueryData(sql);
  82. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  83. {
  84. if (dbData == null || dbData.Rows.Count == 0)
  85. return;
  86. string[] clearedNameList = Array.ConvertAll<StatsDataListItem, string>(StatData.ToArray(), x => x.Name);
  87. List<string> removableList = new List<string>();
  88. if (clearedNameList.Length > 0)
  89. removableList = clearedNameList.ToList();
  90. for (int i = 0; i < dbData.Rows.Count; i++)
  91. {
  92. if (!dbData.Rows[i]["is_visible"].Equals(DBNull.Value) && !Convert.ToBoolean(dbData.Rows[i]["is_visible"].ToString()))
  93. continue;
  94. string name = dbData.Rows[i]["name"].ToString();
  95. removableList.RemoveIfContains(name);
  96. StatsDataListItem item = StatData.FirstOrDefault(x => x.Name == name);
  97. if (item == null)
  98. {
  99. item = new StatsDataListItem();
  100. item.Name = dbData.Rows[i]["name"].ToString();
  101. item.AlarmEnable = false;
  102. item.WarningEnable = false;
  103. item.IsVisible = true;
  104. item.AlarmTextSaved = true;
  105. item.WarningTextSaved = true;
  106. item.AlarmValueSetPoint = dbData.Rows[i]["alarm_value"].ToString();
  107. item.WarningValueSetPoint = dbData.Rows[i]["warning_value"].ToString();
  108. StatData.Add(item);
  109. }
  110. item.Description = dbData.Rows[i]["description"].ToString();
  111. item.Value = dbData.Rows[i]["value"].ToString();
  112. item.Total = dbData.Rows[i]["total"].ToString();
  113. if (!dbData.Rows[i]["enable_warning"].Equals(DBNull.Value))
  114. item.WarningEnable = Convert.ToBoolean(dbData.Rows[i]["enable_warning"].ToString());
  115. item.WarningValue = dbData.Rows[i]["warning_value"].ToString();
  116. if (!dbData.Rows[i]["enable_alarm"].Equals(DBNull.Value))
  117. item.AlarmEnable = Convert.ToBoolean(dbData.Rows[i]["enable_alarm"].ToString());
  118. item.AlarmValue = dbData.Rows[i]["alarm_value"].ToString();
  119. if (!dbData.Rows[i]["last_update_time"].Equals(DBNull.Value))
  120. item.LastUpdateTime = ((DateTime)dbData.Rows[i]["last_update_time"]).ToString("yyyy-MM-dd HH:mm:ss.fff");
  121. if (!dbData.Rows[i]["last_reset_time"].Equals(DBNull.Value))
  122. item.LastResetTime = ((DateTime)dbData.Rows[i]["last_reset_time"]).ToString("yyyy-MM-dd HH:mm:ss.fff");
  123. if (!dbData.Rows[i]["last_total_reset_time"].Equals(DBNull.Value))
  124. item.LastResetTotalTime = ((DateTime)dbData.Rows[i]["last_total_reset_time"]).ToString("yyyy-MM-dd HH:mm:ss.fff");
  125. item.InvokePropertyChanged(nameof(item.AlarmValue));
  126. item.InvokePropertyChanged(nameof(item.WarningValue));
  127. item.InvokePropertyChanged(nameof(item.Value));
  128. item.InvokePropertyChanged(nameof(item.Total));
  129. item.InvokePropertyChanged(nameof(item.LastUpdateTime));
  130. item.InvokePropertyChanged(nameof(item.LastResetTime));
  131. item.InvokePropertyChanged(nameof(item.LastResetTotalTime));
  132. }
  133. foreach (var name in removableList)
  134. {
  135. StatsDataListItem item = StatData.FirstOrDefault(x => x.Name == name);
  136. if (item != null)
  137. StatData.Remove(item);
  138. }
  139. }));
  140. string sqlrfpump = $"SELECT * FROM \"stats_data_rf_pump\" order by \"name\" ASC;";
  141. DataTable dbDataRFPump = QueryDataClient.Instance.Service.QueryData(sqlrfpump);
  142. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  143. {
  144. if (dbDataRFPump == null || dbDataRFPump.Rows.Count == 0)
  145. return;
  146. string[] clearedNameList = Array.ConvertAll<StatsDataListItemRFAndPump, string>(StatDataRFAndPump.ToArray(), x => x.Name);
  147. List<string> removableList = new List<string>();
  148. if (clearedNameList.Length > 0)
  149. removableList = clearedNameList.ToList();
  150. for (int i = 0; i < dbDataRFPump.Rows.Count; i++)
  151. {
  152. string name = dbDataRFPump.Rows[i]["name"].ToString();
  153. removableList.RemoveIfContains(name);
  154. StatsDataListItemRFAndPump item = StatDataRFAndPump.FirstOrDefault(x => x.Name == name);
  155. if (item == null)
  156. {
  157. item = new StatsDataListItemRFAndPump();
  158. item.Name = dbDataRFPump.Rows[i]["name"].ToString();
  159. item.AlarmEnable = false;
  160. item.PMIntervalTextSaved = true;
  161. item.PMIntervalSetPoint = dbDataRFPump.Rows[i]["pm_interval"].ToString();
  162. StatDataRFAndPump.Add(item);
  163. }
  164. item.Description = dbDataRFPump.Rows[i]["description"].ToString();
  165. if (!dbDataRFPump.Rows[i]["enable_alarm"].Equals(DBNull.Value))
  166. item.AlarmEnable = Convert.ToBoolean(dbDataRFPump.Rows[i]["enable_alarm"].ToString());
  167. item.fromLastPM = dbDataRFPump.Rows[i]["from_last_pm"].ToString();
  168. item.PMInterval = dbDataRFPump.Rows[i]["pm_interval"].ToString();
  169. item.Total = dbDataRFPump.Rows[i]["total"].ToString();
  170. if (!dbDataRFPump.Rows[i]["last_pm_time"].Equals(DBNull.Value))
  171. item.LastPMTime = ((DateTime)dbDataRFPump.Rows[i]["last_pm_time"]).ToString("yyyy-MM-dd HH:mm:ss.fff");
  172. item.InvokePropertyChanged(nameof(item.Name));
  173. item.InvokePropertyChanged(nameof(item.AlarmEnable));
  174. item.InvokePropertyChanged(nameof(item.Description));
  175. item.InvokePropertyChanged(nameof(item.Total));
  176. item.InvokePropertyChanged(nameof(item.fromLastPM));
  177. item.InvokePropertyChanged(nameof(item.PMInterval));
  178. item.InvokePropertyChanged(nameof(item.LastPMTime));
  179. }
  180. foreach (var name in removableList)
  181. {
  182. StatsDataListItemRFAndPump item = StatDataRFAndPump.FirstOrDefault(x => x.Name == name);
  183. if (item != null)
  184. StatDataRFAndPump.Remove(item);
  185. }
  186. }));
  187. }
  188. catch (Exception e)
  189. {
  190. LOG.Write(e);
  191. }
  192. }
  193. public void ResetValue(StatsDataListItem item)
  194. {
  195. if (MenuPermission != 3) return;
  196. InvokeClient.Instance.Service.DoOperation("System.Stats.ResetValue", item.Name);
  197. }
  198. public void ResetEnableAlarm(StatsDataListItem item)
  199. {
  200. if (MenuPermission != 3) return;
  201. InvokeClient.Instance.Service.DoOperation("System.Stats.EnableAlarm", item.Name, item.AlarmEnable);
  202. }
  203. public void ResetEnableWarning(StatsDataListItem item)
  204. {
  205. if (MenuPermission != 3) return;
  206. InvokeClient.Instance.Service.DoOperation("System.Stats.EnableWarning", item.Name, item.WarningEnable);
  207. }
  208. public void SetAlarmValue(StatsDataListItem item)
  209. {
  210. if (MenuPermission != 3) return;
  211. if (string.IsNullOrEmpty(item.AlarmValueSetPoint) ||
  212. !int.TryParse(item.AlarmValueSetPoint, out int setValue))
  213. {
  214. DialogBox.ShowWarning("Alarm value not valid");
  215. return;
  216. }
  217. InvokeClient.Instance.Service.DoOperation("System.Stats.SetAlarmValue", item.Name, setValue);
  218. item.AlarmTextSaved = true;
  219. item.InvokePropertyChanged(nameof(item.AlarmTextSaved));
  220. }
  221. public void SetWarningValue(StatsDataListItem item)
  222. {
  223. if (MenuPermission != 3) return;
  224. if (string.IsNullOrEmpty(item.WarningValueSetPoint) ||
  225. !int.TryParse(item.WarningValueSetPoint, out int setValue))
  226. {
  227. DialogBox.ShowWarning("Warning value not valid");
  228. return;
  229. }
  230. InvokeClient.Instance.Service.DoOperation("System.Stats.SetWarningValue", item.Name, setValue);
  231. item.WarningTextSaved = true;
  232. item.InvokePropertyChanged(nameof(item.WarningTextSaved));
  233. }
  234. public void ResetTotalValue(StatsDataListItem item)
  235. {
  236. if (MenuPermission != 3) return;
  237. InvokeClient.Instance.Service.DoOperation("System.Stats.ResetTotalValue", item.Name);
  238. }
  239. public void ResetTimeValue(StatsDataListItemRFAndPump item)
  240. {
  241. if (MenuPermission != 3) return;
  242. InvokeClient.Instance.Service.DoOperation("System.StatsRFAndPump.ResetValue", item.Name);
  243. }
  244. public void ResetRFEnableAlarm(StatsDataListItemRFAndPump item)
  245. {
  246. if (MenuPermission != 3) return;
  247. InvokeClient.Instance.Service.DoOperation("System.StatsRFAndPump.EnableAlarm", item.Name, item.AlarmEnable);
  248. }
  249. public void SetPMIntervalValue(StatsDataListItemRFAndPump item)
  250. {
  251. if (MenuPermission != 3) return;
  252. if (string.IsNullOrEmpty(item.PMIntervalSetPoint) ||
  253. !int.TryParse(item.PMIntervalSetPoint, out int setValue))
  254. {
  255. DialogBox.ShowWarning("PM Interval value not valid");
  256. return;
  257. }
  258. InvokeClient.Instance.Service.DoOperation("System.StatsRFAndPump.SetPMIntervalValue", item.Name, setValue);
  259. item.PMIntervalTextSaved = true;
  260. item.InvokePropertyChanged(nameof(item.PMIntervalTextSaved));
  261. }
  262. }
  263. }