using JiangsuEarthquake.Common; using JiangsuEarthquake.DataAccess; using JiangsuEarthquake.Models; using JiangsuEarthquake.Views.UserControls; using MySql.Data.MySqlClient; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Media; using System.Windows.Threading; namespace JiangsuEarthquake.ViewModels { public class SystemStateDataViewModel : NotifyBase { #region Search Data private DateTime _startDateTime; public DateTime StartDateTime { get { return _startDateTime; } set { Set(ref _startDateTime, value); } } private DateTime _endDateTime; public DateTime EndDateTime { get { return _endDateTime; } set { Set(ref _endDateTime, value); } } int PageIndex = 0; private int recordCount; public int RecordCount { get { return recordCount; } set { recordCount = value; this.DoNotify(); } } private int totalPage; public int TotalPage { get { return totalPage; } set { totalPage = value; this.DoNotify(); } } private string systemStateMsg; public string SystemStateMsg { get { return systemStateMsg; } set { systemStateMsg = value; this.DoNotify(); } } private ObservableCollection systemStateDataList = new ObservableCollection(); public ObservableCollection SystemStateDataList { get { return systemStateDataList; } set { systemStateDataList = value; this.DoNotify(); } } private ObservableCollection totalSystemStateDataList = new ObservableCollection(); public ObservableCollection TotalSystemStateDataList { get { return totalSystemStateDataList; } set { totalSystemStateDataList = value; this.DoNotify(); } } #endregion #region Command Define public CommandBase ForwordSystemStateCommand { get; set; } public CommandBase NextSystemStateCommand { get; set; } public CommandBase AskDataBtnCommand { get; set; } public CommandBase RefreshDataCommand { get; set; } public CommandBase DownloadDataCommand { get; set; } #endregion int station_id = 1; public DispatcherTimer timerDownloadDataMsgHidden = new DispatcherTimer(); #region Data Filtering string timeSearch = ""; public static DialogViewModel vm; private bool _isChecked; public bool IsChecked { get { return _isChecked; } set { _isChecked = value; this.DoNotify(); if (IsChecked) { if (string.IsNullOrEmpty(EndDateTime.ToString()) || string.IsNullOrEmpty(StartDateTime.ToString())) { IsChecked = false; return; } if (EndDateTime < StartDateTime) { HandyControl.Controls.Dialog.Show(new TextDialog("起始时间大于结束时间,\n请重新输入!")); IsChecked = false; //this.EndDateTime = DateTime.Now; //this.StartDateTime = DateTime.Now.AddDays(-1); return; } timeSearch = " and DataTime BETWEEN '" + StartDateTime + "' and '" + EndDateTime + "' "; } else { timeSearch = ""; } //设置当前页为1 PageIndex = 1; TotalSystemStateDataList.Clear(); SystemStateDataList.Clear(); string sql = String.Format("select * from conboxenvpara where StationID = {0} {1} ORDER by id desc limit 100", station_id, timeSearch); MySqlDataReader dataReader = DBHelper.ExecuteReader(sql, 1); int index = 1; while (dataReader.Read()) { BaseStationStatusModel baseStationStatusModel = new BaseStationStatusModel(); baseStationStatusModel.Index = index++; baseStationStatusModel.DataTime = Convert.ToDateTime(dataReader["DataTime"]); baseStationStatusModel.In_Vol = Convert.ToSingle(string.IsNullOrEmpty(dataReader["In_Vol"].ToString()) ? "0" : dataReader["In_Vol"]); baseStationStatusModel.In_Cur = Convert.ToSingle(string.IsNullOrEmpty(dataReader["In_Cur"].ToString()) ? "0" : dataReader["In_Cur"]); baseStationStatusModel.JBH_Tem = Convert.ToSingle(string.IsNullOrEmpty(dataReader["JBH_Tem"].ToString()) ? "0" : dataReader["JBH_Tem"]); baseStationStatusModel.JBH_Hum = Convert.ToSingle(string.IsNullOrEmpty(dataReader["JBH_Hum"].ToString()) ? "0" : dataReader["JBH_Hum"]); int leakState = Convert.ToInt32(string.IsNullOrEmpty(dataReader["JBH_Leak"].ToString()) ? "1" : dataReader["JBH_Leak"]); if (leakState == 1) { baseStationStatusModel.JBH_Leak = (ImageSource)Application.Current.FindResource("CycleRed"); } else { baseStationStatusModel.JBH_Leak = (ImageSource)Application.Current.FindResource("CycleGreen"); } baseStationStatusModel.JBH_Attitude_x = Convert.ToSingle(string.IsNullOrEmpty(dataReader["JBH_Attitude_x"].ToString()) ? "0" : dataReader["JBH_Attitude_x"]); baseStationStatusModel.JBH_Attitude_y = Convert.ToSingle(string.IsNullOrEmpty(dataReader["JBH_Attitude_y"].ToString()) ? "0" : dataReader["JBH_Attitude_y"]); baseStationStatusModel.JBH_Attitude_z = Convert.ToSingle(string.IsNullOrEmpty(dataReader["JBH_Attitude_z"].ToString()) ? "0" : dataReader["JBH_Attitude_z"]); TotalSystemStateDataList.Add(baseStationStatusModel); } dataReader.Dispose(); RecordCount = index - 1; if (RecordCount <= 10) { TotalPage = 1; SystemStateDataList = TotalSystemStateDataList; } else { TotalPage = (int)Math.Ceiling((double)RecordCount / 10); for (int i = 0; i < 10; i++) { SystemStateDataList.Add(TotalSystemStateDataList[i]); } } SystemStateMsg = string.Format("共计{0}页,当前第{1}页", TotalPage, PageIndex); } } #endregion public SystemStateDataViewModel(int id) { // 默认查询1天内的日志 this.EndDateTime = DateTime.Now; this.StartDateTime = DateTime.Now.AddDays(-1); PageIndex = 1; station_id = id; vm = new DialogViewModel { Content = "" }; #region Command Set this.ForwordSystemStateCommand = new CommandBase(); this.ForwordSystemStateCommand.DoExcute = new Action(ForwordSystemState); this.ForwordSystemStateCommand.DoCanExcute = new Func((o) => true); this.NextSystemStateCommand = new CommandBase(); this.NextSystemStateCommand.DoExcute = new Action(NextSystemState); this.NextSystemStateCommand.DoCanExcute = new Func((o) => true); this.AskDataBtnCommand = new CommandBase(); this.AskDataBtnCommand.DoExcute = new Action(AskData); this.AskDataBtnCommand.DoCanExcute = new Func((o) => true); this.RefreshDataCommand = new CommandBase(); this.RefreshDataCommand.DoExcute = new Action(RefreshData); this.RefreshDataCommand.DoCanExcute = new Func((o) => true); this.DownloadDataCommand = new CommandBase(); this.DownloadDataCommand.DoExcute = new Action(DownloadData); this.DownloadDataCommand.DoCanExcute = new Func((o) => true); #endregion timerDownloadDataMsgHidden.Interval = TimeSpan.FromSeconds(2); timerDownloadDataMsgHidden.Tick += TimerDownloadDataMsgHidden_Tick; } #region Ask Data public void AskData(object o) { if (station_id == 1) { if (MainWindow.mainViewModel.serverModel1.IsOpened) MainWindow.mainViewModel.serverModel1.SendMess(new byte[4] { 0x00, 0x01, 0x02, 0x04 }); } else { if (MainWindow.mainViewModel.serverModel2.IsOpened) MainWindow.mainViewModel.serverModel2.SendMess(new byte[4] { 0x00, 0x01, 0x02, 0x03 }); } } #endregion #region Page Switching public void ForwordSystemState(object o) { if (PageIndex == 1) return; PageIndex -= 1; SystemStateDataList.Clear(); for (int i = 0; i < 10; i++) { SystemStateDataList.Add(TotalSystemStateDataList[i + (PageIndex - 1) * 10]); } SystemStateMsg = string.Format("共计{0}页,当前第{1}页", TotalPage, PageIndex); } public void NextSystemState(object o) { if (PageIndex == TotalPage) return; PageIndex += 1; SystemStateDataList.Clear(); for (int i = 0; i < (PageIndex == TotalPage ? (RecordCount - (PageIndex - 1) * 10) : 10); i++) { SystemStateDataList.Add(TotalSystemStateDataList[i + (PageIndex - 1) * 10]); } SystemStateMsg = string.Format("共计{0}页,当前第{1}页", TotalPage, PageIndex); } #endregion #region RefreshData public void RefreshData(object o) { if (IsChecked) { timeSearch = " and DataTime BETWEEN '" + StartDateTime + "' and '" + EndDateTime + "' "; } else { timeSearch = ""; } //设置当前页为1 PageIndex = 1; TotalSystemStateDataList.Clear(); SystemStateDataList.Clear(); string sql = String.Format("select * from conboxenvpara where StationID = {0} {1} ORDER by id desc limit 100", station_id, timeSearch); MySqlDataReader dataReader = DBHelper.ExecuteReader(sql, 1); int index = 1; while (dataReader.Read()) { BaseStationStatusModel baseStationStatusModel = new BaseStationStatusModel(); baseStationStatusModel.Index = index++; baseStationStatusModel.DataTime = Convert.ToDateTime(dataReader["DataTime"]); baseStationStatusModel.In_Vol = Convert.ToSingle(string.IsNullOrEmpty(dataReader["In_Vol"].ToString()) ? "0" : dataReader["In_Vol"]); baseStationStatusModel.In_Cur = Convert.ToSingle(string.IsNullOrEmpty(dataReader["In_Cur"].ToString()) ? "0" : dataReader["In_Cur"]); baseStationStatusModel.JBH_Tem = Convert.ToSingle(string.IsNullOrEmpty(dataReader["JBH_Tem"].ToString()) ? "0" : dataReader["JBH_Tem"]); baseStationStatusModel.JBH_Hum = Convert.ToSingle(string.IsNullOrEmpty(dataReader["JBH_Hum"].ToString()) ? "0" : dataReader["JBH_Hum"]); int leakState = Convert.ToInt32(string.IsNullOrEmpty(dataReader["JBH_Leak"].ToString()) ? "1" : dataReader["JBH_Leak"]); if (leakState == 1) { baseStationStatusModel.JBH_Leak = (ImageSource)Application.Current.FindResource("CycleRed"); } else { baseStationStatusModel.JBH_Leak = (ImageSource)Application.Current.FindResource("CycleGreen"); } baseStationStatusModel.JBH_Attitude_x = Convert.ToSingle(string.IsNullOrEmpty(dataReader["JBH_Attitude_x"].ToString()) ? "0" : dataReader["JBH_Attitude_x"]); baseStationStatusModel.JBH_Attitude_y = Convert.ToSingle(string.IsNullOrEmpty(dataReader["JBH_Attitude_y"].ToString()) ? "0" : dataReader["JBH_Attitude_y"]); baseStationStatusModel.JBH_Attitude_z = Convert.ToSingle(string.IsNullOrEmpty(dataReader["JBH_Attitude_z"].ToString()) ? "0" : dataReader["JBH_Attitude_z"]); TotalSystemStateDataList.Add(baseStationStatusModel); } dataReader.Dispose(); RecordCount = index - 1; if (RecordCount <= 10) { TotalPage = 1; SystemStateDataList = TotalSystemStateDataList; } else { TotalPage = (int)Math.Ceiling((double)RecordCount / 10); for (int i = 0; i < 10; i++) { SystemStateDataList.Add(TotalSystemStateDataList[i]); } } SystemStateMsg = string.Format("共计{0}页,当前第{1}页", TotalPage, PageIndex); } #endregion #region DownloadData private void TimerDownloadDataMsgHidden_Tick(object sender, EventArgs e) { DownloadDataMsgVisibility = Visibility.Hidden; // 停止定时器 (sender as DispatcherTimer).Stop(); } private string downloadDataMsg; public string DownloadDataMsg { get { return downloadDataMsg; } set { downloadDataMsg = value; this.DoNotify(); } } private Brush downloadDataMsgForeground; public Brush DownloadDataMsgForeground { get { return downloadDataMsgForeground; } set { downloadDataMsgForeground = value; this.DoNotify(); } } private bool downloadDataBtnIsEnabled = true; public bool DownloadDataBtnIsEnabled { get { return downloadDataBtnIsEnabled; } set { downloadDataBtnIsEnabled = value; this.DoNotify(); } } private Visibility downloadDataMsgVisibility = Visibility.Visible; public Visibility DownloadDataMsgVisibility { get { return downloadDataMsgVisibility; } set { downloadDataMsgVisibility = value; this.DoNotify(); } } public void DownloadData(object o) { DownloadDataBtnIsEnabled = false; DownloadDataMsgVisibility = Visibility.Visible; string baseStationFolder = Tools.GetAppSetting("BaseStationFolder"); string savePath = CSVDownload.CreateFile(baseStationFolder, "SystemStateData_" + DateTime.Now.ToString("yyyyMMddhhMMss"), "csv"); bool result = CSVDownload.SaveBaseStationDataToCSVFile(TotalSystemStateDataList, savePath); if (result) { DownloadDataMsg = "下载数据成功!"; DownloadDataMsgForeground = new SolidColorBrush(Colors.Green); } else { DownloadDataMsg = "下载数据失败!"; DownloadDataMsgForeground = new SolidColorBrush(Colors.Red); } timerDownloadDataMsgHidden.Start(); DownloadDataBtnIsEnabled = true; } #endregion } }