using CK.SCP.Controller; using CK.SCP.Models; using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Windows.Threading; namespace UniApiWpf { /// /// MainWindow.xaml 的交互逻辑 /// public partial class MainWindow : Window { private DispatcherTimer PutTimer; private DispatcherTimer GetTimer; private readonly BackgroundWorker _bgwGet = new BackgroundWorker(); private readonly BackgroundWorker _bgwPut = new BackgroundWorker(); //private TimerStateEnum TimeState = TimerStateEnum.Off; private int _refreshPutInterval = 30; private int _refreshGetInterval = 30; public MainWindow() { InitializeComponent(); this.DataContext = this; InitParam(); } public LogModel SelecteGetLog { get; set; } public LogModel SelectePutLog { get; set; } private List getLogItemsSource = new List(); public List GetLogItemsSource { get { return getLogItemsSource; } set { getLogItemsSource = value; OnPropertyChanged("GetLogItemsSource"); } } private static List putLogItemsSource = new List(); public List PutLogItemsSource { get { return putLogItemsSource; } set { putLogItemsSource = value; OnPropertyChanged("PutLogItemsSource"); } } void InitParam() { List list = new List() { "TES_CODE", "TES_DOMAIN" }; //var liststr = JsonHelper.ListToJson(list); //GlobalVar.ExchangeCenterService.Timeout = GlobalConfig.Setting.ServiceTimeout;//20分钟超时。 //var resultStr = GlobalVar.ExchangeCenterService.GetSessionId(GlobalConfig.Setting.DataCenterSystemName, EncryptHelper.Encrypt(GlobalConfig.Setting.DefaultPassword), NetHelper.GetIpAddress()); //_refreshPutInterval = GlobalConfig.Setting.RefreshInterval; //_refreshGetInterval = GlobalConfig.Setting.RefreshInterval; //GlobalConfig.Setting.SessionId = JsonHelper.JsonToEntity(resultStr).Data; _bgwGet.DoWork += BgwGetDoWork; _bgwGet.RunWorkerCompleted += BgwGetRunWorkerCompleted; _bgwPut.DoWork += BgwPutDoWork; _bgwPut.RunWorkerCompleted += _bgwPut_RunWorkerCompleted; var putlogData = new List() { new LogModel() { LogTime = DateTime.Now.ToString(), LogMessage = "推送载入" } }; CreateLogDataList(putlogData, WriteLogEnum.Put); var getlogData = new List() { new LogModel() { LogTime = DateTime.Now.ToString(), LogMessage = "获取载入" } }; CreateLogDataList(getlogData, WriteLogEnum.Get); } private void _bgwPut_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { } private void BtnPutStart_Click(object sender, RoutedEventArgs e) { PutTimer = new DispatcherTimer(); PutTimer.Tick += new EventHandler(PutTimer_Tick); PutTimer.Interval = TimeSpan.FromSeconds(1); //设置刷新的间隔时间 PutTimer.Start(); BtnPutStart.IsEnabled = false; BtnPutStop.IsEnabled = true; } private void PutTimer_Tick(object sender, EventArgs e) { if (_refreshPutInterval > 0) { _refreshPutInterval--; } else { _refreshPutInterval = int.Parse(ConfigurationSettings.AppSettings["WriteTimeSpan"])*60; PutData(); } BtnPutStart.Content = $"开始按钮:{_refreshPutInterval}"; } private void GetTimer_Tick(object sender, EventArgs e) { if (_refreshGetInterval > 0) { _refreshGetInterval--; } else { _refreshGetInterval = int.Parse(ConfigurationSettings.AppSettings["ReadTimeSpan"]) * 60; GetData(); } BtnGetStart.Content = $"开始按钮:{_refreshGetInterval}"; } private void BtnPutStop_Click(object sender, RoutedEventArgs e) { BtnPutStart.IsEnabled = true; BtnPutStop.IsEnabled = false; PutTimer.Stop(); } private void BtnGetStart_Click(object sender, RoutedEventArgs e) { GetTimer = new DispatcherTimer(); GetTimer.Tick += new EventHandler(GetTimer_Tick); GetTimer.Interval = TimeSpan.FromSeconds(1); //设置刷新的间隔时间 GetTimer.Start(); BtnGetStart.IsEnabled = false; BtnGetStop.IsEnabled = true; } private void BtnGetStop_Click(object sender, RoutedEventArgs e) { BtnGetStart.IsEnabled = true; BtnGetStop.IsEnabled = false; GetTimer.Stop(); } /// /// 推送数据中心变更任务,同步到转换中心 /// private void BtnPutManual_Click(object sender, RoutedEventArgs e) { var logData = new List() { new LogModel() { LogTime= DateTime.Now.ToString(),LogMessage= "推送交换中心变更任务手动执行" } }; PutData(); } /// /// 读取转换中心变更任务,同步给数据中心 /// private void BtnGetManual_Click(object sender, RoutedEventArgs e) { var logData = new List() { new LogModel() { LogTime= DateTime.Now.ToString(),LogMessage= "读取交换中心变更任务手动执行" } }; GetData(); } private void BgwPutDoWork(object sender, DoWorkEventArgs e) { TimeSpan nowDt = DateTime.Now.TimeOfDay; TimeSpan workstartDT = DateTime.Parse("1:00").TimeOfDay; TimeSpan workendDT = DateTime.Parse("2:00").TimeOfDay; if (nowDt < workstartDT || nowDt > workendDT) { SCP_DC_UNI_CONTROLLER.WriteService((rs) => { if (rs.State == ReturnStatus.Succeed) { CreateLogDataList(rs.Result, WriteLogEnum.Put); } else { CreateLogDataList(rs.Result, WriteLogEnum.Put); } }); } } private void BgwGetRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { // Logger.Write(EnumApiType.Get, EnumLogType.End, $"{GetType().Name}"); } private void BgwGetDoWork(object sender, DoWorkEventArgs e) { try { TimeSpan nowDt = DateTime.Now.TimeOfDay; TimeSpan workstartDT = DateTime.Parse("1:00").TimeOfDay; TimeSpan workendDT = DateTime.Parse("2:00").TimeOfDay; if (nowDt < workstartDT || nowDt > workendDT) { #region SCP_DC_UNI_CONTROLLER.ReadService((rs) => { List _list = new List(); if (rs.State == ReturnStatus.Succeed) { rs.MessageList.ForEach(p => { _list.Add(new LogModel() { LogMessage = string.Format("{0}任务:{1}", p, rs.Message.ToString()), LogTime = DateTime.Now.ToShortDateString(), TableName = "", TaskId = rs.Message.ToString(), }); }); CreateLogDataList(_list, WriteLogEnum.Get); } else { rs.ErrorList.ForEach(p => { _list.Add(new LogModel() { LogMessage = string.Format("{0}任务:{1}", p.Message, rs.Message.ToString()), LogTime = DateTime.Now.ToShortDateString(), TableName = "", TaskId = rs.Message.ToString(), }); }); CreateLogDataList(_list, WriteLogEnum.Get); } }); #endregion } } catch (Exception ex) { //Logger.WriteEx(EnumApiType.Get, $"{GetType().Name}", ex); } } public void GetData() { try { if (_bgwGet.IsBusy) return; //Logger.Write(EnumApiType.Get, EnumLogType.Begin, $"{GetType().Name}"); _bgwGet.RunWorkerAsync(); } catch (Exception ex) { //Logger.WriteEx(EnumApiType.Get, $"{GetType().Name}", ex); } } /// /// 将数据中心表变更任务,获取后同步给转换中心 /// public void PutData() { try { if (_bgwPut.IsBusy) return; //Logger.Write(EnumApiType.Put, EnumLogType.Begin, $"{GetType().Name}"); _bgwPut.RunWorkerAsync(); } catch (Exception ex) { //Logger.WriteEx(EnumApiType.Put, $"{GetType().Name}", ex); } } public void CreateLogDataList(List list, WriteLogEnum writeLogEnum) { List _listTemp = new List(); if (writeLogEnum == WriteLogEnum.Put) { if (getLogItemsSource.Count >500) { getLogItemsSource = new List(); } getLogItemsSource.AddRange(list); _listTemp.AddRange(getLogItemsSource); this.Dispatcher.BeginInvoke(new Action(() => { PutDataGrid.ItemsSource =_listTemp; })); //GetLogItemsSource = _listTemp; } else { if (putLogItemsSource.Count > 500) { putLogItemsSource = new List(); } putLogItemsSource.AddRange(list); _listTemp.AddRange(putLogItemsSource); this.Dispatcher.BeginInvoke(new Action(() => { GetDataGrid.ItemsSource = _listTemp; })); } } private void Userdatagrid_OnMouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e) { } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } public enum WriteLogEnum { /// /// 读取转换中心变更任务,同步给数据中心 /// Get, /// /// 推送数据中心变更任务,同步到转换中心 /// Put, } public enum TimerStateEnum { Open, Off, } } }