using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Threading; using System.Windows; using System.Windows.Forms; using System.Windows.Input; using GummingCommon; using GummingEntity; using GummingSupport; using GummingControl; using System.Text; using GummingBusiness; using Newtonsoft.Json; using GummingLine; namespace Gumming { public class RealtimeWarningViewModule : ViewModelBase { #region base parameters private int rows = 100; private int pagenumber = 1; public RealtimeWarningView View { get; set; } private bool IsConfirm; private bool isLoading; private AppEnum.PathType _CurrentPathType; public AppEnum.PathType CurrentPathType { get { return _CurrentPathType; } set { _CurrentPathType = value; OnPropertyChanged("CurrentPathType"); } } private System.Timers.Timer timer; private bool forceRefresh = false; #endregion #region constructor public RealtimeWarningViewModule() { View = new RealtimeWarningView(); View.DataContext = this; InitializeCommands(); InitializeParameters(); } private void InitializeCommands() { ConfirmAlamCommand = new DelegateCommand(OnConfirmAlamCommand); } public override void InitializeParameters(object content = null) { IsConfirm = false; WindowTitle = ""; SysRealtimeWarnings = new ObservableCollection(); PagerControlViewer = new PagerControlViewModule(); PagerControlViewer.OnPager += new PagerControlViewModule.PagerHandler(PagerControlView_OnPager); var thread = new Thread(new ParameterizedThreadStart(StartLoadTestItem)) { IsBackground = true }; thread.Start(); if (timer == null) { timer = new System.Timers.Timer(); timer.Interval = 1000; timer.Elapsed += timer_Elapsed; } timer.Start(); } ~RealtimeWarningViewModule() { } public void ForceRefresh() { forceRefresh = true; } #endregion #region Binding Properties public string WindowTitle { get; private set; } private string _TotalRecords; public string TotalRecords { get { return _TotalRecords; } set { _TotalRecords = value; OnPropertyChanged("TotalRecords"); } } private ObservableCollection _SysRealtimeWarnings; public ObservableCollection SysRealtimeWarnings { get { return _SysRealtimeWarnings; } set { _SysRealtimeWarnings = value; OnPropertyChanged("SysRealtimeWarnings"); } } private PagerControlViewModule _PagerControlViewer; public PagerControlViewModule PagerControlViewer { get { return _PagerControlViewer; } set { _PagerControlViewer = value; OnPropertyChanged("PagerControlViewer"); } } #endregion #region Binding Commands private ICommand _ConfirmAlamCommand; public ICommand ConfirmAlamCommand { get { return _ConfirmAlamCommand; } set { _ConfirmAlamCommand = value; OnPropertyChanged("ConfirmAlamCommand"); } } #endregion #region Private Methods private void PagerControlView_OnPager(int currentpagenumber, int currentrows) { pagenumber = currentpagenumber; rows = currentrows; if (!isLoading) { Thread t = new Thread(StartLoadTestItem); t.IsBackground = true; t.Start(); } } private void StartLoadTestItem(object key) { try { isLoading = true; List ids = SysRealtimeWarnings.Select(q => q.RecId).ToList(); var record = SysWarningDA.Load(new PagerEntity() { PageIndex = pagenumber, Rows = rows, Conditions = " and createtime > date('now') and EndTime is null " }); List logs = (List)record.rows; if (logs.Any(q => !ids.Contains(q.RecId)) || logs?.Count != SysRealtimeWarnings?.Count || forceRefresh) { forceRefresh = false; View.Dispatcher.Invoke((Action)(() => { SysRealtimeWarnings.Clear(); foreach (var s in logs) { SysRealtimeWarnings.Add(s); } OnPropertyChanged("HartLog"); TotalRecords = string.Format("设置记录数: {0}", record.records);//.ToString("D5")); PagerControlViewer.SetPager(rows, record.page, record.total, record.Count, record.records); isLoading = false; })); } } catch (Exception ex) { LogService.WriteErrorLog(ex); ShowErrorBox("Error:" + ex.Message); return; } } private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { if (this.IsActivePage) { var thread = new Thread(new ParameterizedThreadStart(StartLoadTestItem)) { IsBackground = true }; thread.Start(); } } private void OnConfirmAlamCommand(Object sender) { SysRealtimeWarnings.Clear(); //解除暂停 foreach (var station in Global.Stations) { Global.ClearPauseState(station?.StationCode); Global.ClearAbortState(station?.StationCode); Global.ClearErrorState(station.StationCode);//清空初始化红色告警 } OutPort.SetBeepIO(false); if (Global.RobotSpaceError) { Thread tr = new Thread(new ThreadStart((Action)(() => { Robot.ClearError(); Global.RobotSpaceError = false; }))); tr.IsBackground = true; tr.Start(); Global.RobotWaitHome = false; } if (Global.RobotWaitError) { Thread tr = new Thread(new ThreadStart((Action)(() => { Global.RobotWaitError = false; Robot.Initialize(); }))); tr.IsBackground = true; tr.Start(); Global.RobotWaitHome = false; } //ECAT重连 string warningKey = "SYS_100"; if (SysWarningDA.WarningPool.ContainsKey(warningKey)) { ShowProgressView("开始重新初始化ECAT..."); SysWarningDA.WarningPool.Remove(warningKey); string recid = SysWarningDA.LoadLastWarning(warningKey); if (!string.IsNullOrWhiteSpace(recid)) { SysWarningDA.RefreshEndTime(recid); } Thread tr = new Thread(new ThreadStart((Action)(() => { DeviceControl.OpenCard(); DeviceControl.FindSlave(); HardPortsEntity port = OutPort.GetLightIO(); if (port != null) { //默认灯亮 ControlCheck.PMCInvoke(false, "SetPortStatus", null, () => DeviceControl.SetPortStatus(port.NodeId, port.SlotId, port.PortIndex, 1)); } OutPort.SetYellowIO(true); CloseProgressView(); }))); tr.IsBackground = true; tr.Start(); } //清理轴异常告警 for (int i = 0; i < Global.Axises.Count; i++) { var axis = Global.Axises[i]; if (ExecuteHelper.CheckAxisWarning(axis.NodeId, axis.SlotId)) { DeviceControl.ResetRalm(axis.NodeId, axis.SlotId); } } OutPort.SetBeepIO(false); Global.IsAlarming = false;//解除报警 Global.IsException = false; Global.IsAlarmingConfirmed = true;//解除报警 SysWarningDA.RefreshAllEndTime();//恢复所有报警状态 SysWarningDA.RefreshAlarming();//刷新告警状态 PortTimer.ClearWarningPool(); DTPTimer.ClearWarningPool(); GlobalTimer.ClearWarningPool(); HPTimer.ClearWarningPool(); YeweiTimer.ClearWarningPool(); ExecuteHelper.ClearWarningPool(); ExecuteRobotHelper.ClearWarningPool(); Global.RobotWaitError = false; Global.Rs484WaitError = false; Global.DTMWaitError = false; var thread = new Thread(new ParameterizedThreadStart(StartLoadTestItem)) { IsBackground = true }; thread.Start(); } #endregion } }