using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Data; using System.Linq; using System.Windows; using Aitex.Core.RT.Log; using Aitex.Core.Util; using MECF.Framework.Common.CommonData; using MECF.Framework.Common.DataCenter; using MECF.Framework.Common.OperationCenter; using MECF.Framework.UI.Client.ClientBase; using OpenSEMI.ClientBase; using SciChart.Core.Extensions; namespace EfemDualUI.Views.History.Statistics { public class StatsDataListItem : NotifiableItem { public string Name { get; set; } public string Value { get; set; } public string Description { get; set; } public string Total { get; set; } public string LastUpdateTime { get; set; } public string LastResetTime { get; set; } public string LastResetTotalTime { get; set; } public string WarningValue { get; set; } public string WarningValueSetPoint { get; set; } public bool WarningEnable { get; set; } public string AlarmValue { get; set; } public string AlarmValueSetPoint { get; set; } public bool AlarmEnable { get; set; } public bool AlarmTextSaved { get; set; } public bool WarningTextSaved { get; set; } public bool IsVisible { get; set; } } public class StatsDataListItemRFAndPump : NotifiableItem { public string Name { get; set; } public string Description { get; set; } public string LastPMTime { get; set; } public string fromLastPM { get; set; } public string Total { get; set; } public string PMInterval { get; set; } public string PMIntervalSetPoint { get; set; } public bool AlarmEnable { get; set; } public bool PMIntervalTextSaved { get; set; } } public class StatisticsViewModel : UiViewModelBase { private int MenuPermission; public ObservableCollection StatData { get; set; } public ObservableCollection StatDataRFAndPump { get; set; } protected override void OnInitialize() { StatData = new ObservableCollection(); StatDataRFAndPump = new ObservableCollection(); base.OnInitialize(); } protected override void OnActivate() { base.OnActivate(); } protected override void OnDeactivate(bool close) { base.OnDeactivate(close); } protected override void Poll() { try { MenuPermission = ClientApp.Instance.GetPermission("Statistics"); string sql = $"SELECT * FROM \"stats_data\" order by \"name\" ASC;"; DataTable dbData = QueryDataClient.Instance.Service.QueryData(sql); Application.Current.Dispatcher.BeginInvoke(new Action(() => { if (dbData == null || dbData.Rows.Count == 0) return; string[] clearedNameList = Array.ConvertAll(StatData.ToArray(), x => x.Name); List removableList = new List(); if (clearedNameList.Length > 0) removableList = clearedNameList.ToList(); for (int i = 0; i < dbData.Rows.Count; i++) { if (!dbData.Rows[i]["is_visible"].Equals(DBNull.Value) && !Convert.ToBoolean(dbData.Rows[i]["is_visible"].ToString())) continue; string name = dbData.Rows[i]["name"].ToString(); removableList.RemoveIfContains(name); StatsDataListItem item = StatData.FirstOrDefault(x => x.Name == name); if (item == null) { item = new StatsDataListItem(); item.Name = dbData.Rows[i]["name"].ToString(); item.AlarmEnable = false; item.WarningEnable = false; item.IsVisible = true; item.AlarmTextSaved = true; item.WarningTextSaved = true; item.AlarmValueSetPoint = dbData.Rows[i]["alarm_value"].ToString(); item.WarningValueSetPoint = dbData.Rows[i]["warning_value"].ToString(); StatData.Add(item); } item.Description = dbData.Rows[i]["description"].ToString(); item.Value = dbData.Rows[i]["value"].ToString(); item.Total = dbData.Rows[i]["total"].ToString(); if (!dbData.Rows[i]["enable_warning"].Equals(DBNull.Value)) item.WarningEnable = Convert.ToBoolean(dbData.Rows[i]["enable_warning"].ToString()); item.WarningValue = dbData.Rows[i]["warning_value"].ToString(); if (!dbData.Rows[i]["enable_alarm"].Equals(DBNull.Value)) item.AlarmEnable = Convert.ToBoolean(dbData.Rows[i]["enable_alarm"].ToString()); item.AlarmValue = dbData.Rows[i]["alarm_value"].ToString(); if (!dbData.Rows[i]["last_update_time"].Equals(DBNull.Value)) item.LastUpdateTime = ((DateTime)dbData.Rows[i]["last_update_time"]).ToString("yyyy-MM-dd HH:mm:ss.fff"); if (!dbData.Rows[i]["last_reset_time"].Equals(DBNull.Value)) item.LastResetTime = ((DateTime)dbData.Rows[i]["last_reset_time"]).ToString("yyyy-MM-dd HH:mm:ss.fff"); if (!dbData.Rows[i]["last_total_reset_time"].Equals(DBNull.Value)) item.LastResetTotalTime = ((DateTime)dbData.Rows[i]["last_total_reset_time"]).ToString("yyyy-MM-dd HH:mm:ss.fff"); item.InvokePropertyChanged(nameof(item.AlarmValue)); item.InvokePropertyChanged(nameof(item.WarningValue)); item.InvokePropertyChanged(nameof(item.Value)); item.InvokePropertyChanged(nameof(item.Total)); item.InvokePropertyChanged(nameof(item.LastUpdateTime)); item.InvokePropertyChanged(nameof(item.LastResetTime)); item.InvokePropertyChanged(nameof(item.LastResetTotalTime)); } foreach (var name in removableList) { StatsDataListItem item = StatData.FirstOrDefault(x => x.Name == name); if (item != null) StatData.Remove(item); } })); string sqlrfpump = $"SELECT * FROM \"stats_data_rf_pump\" order by \"name\" ASC;"; DataTable dbDataRFPump = QueryDataClient.Instance.Service.QueryData(sqlrfpump); Application.Current.Dispatcher.BeginInvoke(new Action(() => { if (dbDataRFPump == null || dbDataRFPump.Rows.Count == 0) return; string[] clearedNameList = Array.ConvertAll(StatDataRFAndPump.ToArray(), x => x.Name); List removableList = new List(); if (clearedNameList.Length > 0) removableList = clearedNameList.ToList(); for (int i = 0; i < dbDataRFPump.Rows.Count; i++) { string name = dbDataRFPump.Rows[i]["name"].ToString(); removableList.RemoveIfContains(name); StatsDataListItemRFAndPump item = StatDataRFAndPump.FirstOrDefault(x => x.Name == name); if (item == null) { item = new StatsDataListItemRFAndPump(); item.Name = dbDataRFPump.Rows[i]["name"].ToString(); item.AlarmEnable = false; item.PMIntervalTextSaved = true; item.PMIntervalSetPoint = dbDataRFPump.Rows[i]["pm_interval"].ToString(); StatDataRFAndPump.Add(item); } item.Description = dbDataRFPump.Rows[i]["description"].ToString(); if (!dbDataRFPump.Rows[i]["enable_alarm"].Equals(DBNull.Value)) item.AlarmEnable = Convert.ToBoolean(dbDataRFPump.Rows[i]["enable_alarm"].ToString()); item.fromLastPM = dbDataRFPump.Rows[i]["from_last_pm"].ToString(); item.PMInterval = dbDataRFPump.Rows[i]["pm_interval"].ToString(); item.Total = dbDataRFPump.Rows[i]["total"].ToString(); if (!dbDataRFPump.Rows[i]["last_pm_time"].Equals(DBNull.Value)) item.LastPMTime = ((DateTime)dbDataRFPump.Rows[i]["last_pm_time"]).ToString("yyyy-MM-dd HH:mm:ss.fff"); item.InvokePropertyChanged(nameof(item.Name)); item.InvokePropertyChanged(nameof(item.AlarmEnable)); item.InvokePropertyChanged(nameof(item.Description)); item.InvokePropertyChanged(nameof(item.Total)); item.InvokePropertyChanged(nameof(item.fromLastPM)); item.InvokePropertyChanged(nameof(item.PMInterval)); item.InvokePropertyChanged(nameof(item.LastPMTime)); } foreach (var name in removableList) { StatsDataListItemRFAndPump item = StatDataRFAndPump.FirstOrDefault(x => x.Name == name); if (item != null) StatDataRFAndPump.Remove(item); } })); } catch (Exception e) { LOG.Write(e); } } public void ResetValue(StatsDataListItem item) { if (MenuPermission != 3) return; InvokeClient.Instance.Service.DoOperation("System.Stats.ResetValue", item.Name); } public void ResetEnableAlarm(StatsDataListItem item) { if (MenuPermission != 3) return; InvokeClient.Instance.Service.DoOperation("System.Stats.EnableAlarm", item.Name, item.AlarmEnable); } public void ResetEnableWarning(StatsDataListItem item) { if (MenuPermission != 3) return; InvokeClient.Instance.Service.DoOperation("System.Stats.EnableWarning", item.Name, item.WarningEnable); } public void SetAlarmValue(StatsDataListItem item) { if (MenuPermission != 3) return; if (string.IsNullOrEmpty(item.AlarmValueSetPoint) || !int.TryParse(item.AlarmValueSetPoint, out int setValue)) { DialogBox.ShowWarning("Alarm value not valid"); return; } InvokeClient.Instance.Service.DoOperation("System.Stats.SetAlarmValue", item.Name, setValue); item.AlarmTextSaved = true; item.InvokePropertyChanged(nameof(item.AlarmTextSaved)); } public void SetWarningValue(StatsDataListItem item) { if (MenuPermission != 3) return; if (string.IsNullOrEmpty(item.WarningValueSetPoint) || !int.TryParse(item.WarningValueSetPoint, out int setValue)) { DialogBox.ShowWarning("Warning value not valid"); return; } InvokeClient.Instance.Service.DoOperation("System.Stats.SetWarningValue", item.Name, setValue); item.WarningTextSaved = true; item.InvokePropertyChanged(nameof(item.WarningTextSaved)); } public void ResetTotalValue(StatsDataListItem item) { if (MenuPermission != 3) return; InvokeClient.Instance.Service.DoOperation("System.Stats.ResetTotalValue", item.Name); } public void ResetTimeValue(StatsDataListItemRFAndPump item) { if (MenuPermission != 3) return; InvokeClient.Instance.Service.DoOperation("System.StatsRFAndPump.ResetValue", item.Name); } public void ResetRFEnableAlarm(StatsDataListItemRFAndPump item) { if (MenuPermission != 3) return; InvokeClient.Instance.Service.DoOperation("System.StatsRFAndPump.EnableAlarm", item.Name, item.AlarmEnable); } public void SetPMIntervalValue(StatsDataListItemRFAndPump item) { if (MenuPermission != 3) return; if (string.IsNullOrEmpty(item.PMIntervalSetPoint) || !int.TryParse(item.PMIntervalSetPoint, out int setValue)) { DialogBox.ShowWarning("PM Interval value not valid"); return; } InvokeClient.Instance.Service.DoOperation("System.StatsRFAndPump.SetPMIntervalValue", item.Name, setValue); item.PMIntervalTextSaved = true; item.InvokePropertyChanged(nameof(item.PMIntervalTextSaved)); } } }