2024-09-03 08:30:34 +00:00
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.Text ;
using System.Threading.Tasks ;
using System.Windows.Media ;
using System.Windows ;
using System.Windows.Threading ;
using Brush = System . Windows . Media . Brush ;
namespace JiangsuEarthquake.ViewModels
{
public class SystemMonitorDataViewModel : 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 systemMonitorDataMsg ;
public string SystemMonitorDataMsg
{
get { return systemMonitorDataMsg ; }
set { systemMonitorDataMsg = value ; this . DoNotify ( ) ; }
}
#endregion
private ObservableCollection < BaseStationMonitorModel > systemMonitorDataList = new ObservableCollection < BaseStationMonitorModel > ( ) ;
public ObservableCollection < BaseStationMonitorModel > SystemMonitorDataList
{
get { return systemMonitorDataList ; }
set { systemMonitorDataList = value ; this . DoNotify ( ) ; }
}
private ObservableCollection < BaseStationMonitorModel > totalSystemMonitorDataList = new ObservableCollection < BaseStationMonitorModel > ( ) ;
public ObservableCollection < BaseStationMonitorModel > TotalSystemMonitorDataList
{
get { return totalSystemMonitorDataList ; }
set { totalSystemMonitorDataList = value ; this . DoNotify ( ) ; }
}
#region Data Filtering
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 ;
}
2024-09-12 01:48:49 +00:00
timeSearch = " and RecordTime BETWEEN '" + StartDateTime + "' and '" + EndDateTime + "' " ;
2024-09-03 08:30:34 +00:00
}
else
{
timeSearch = "" ;
}
//设置当前页为1
PageIndex = 1 ;
TotalSystemMonitorDataList . Clear ( ) ;
SystemMonitorDataList . Clear ( ) ;
string sql = String . Format ( "select * from juncbox_monitor 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 ( ) )
{
BaseStationMonitorModel baseStationMonitorModel = new BaseStationMonitorModel ( ) ;
baseStationMonitorModel . Index = index + + ;
baseStationMonitorModel . RecordTime = Convert . ToDateTime ( dataReader [ "RecordTime" ] ) ;
baseStationMonitorModel . Seis1_Voltage = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Seis1_Voltage" ] . ToString ( ) ) ? "0" : dataReader [ "Seis1_Voltage" ] ) ;
baseStationMonitorModel . Seis1_Current = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Seis1_Current" ] . ToString ( ) ) ? "0" : dataReader [ "Seis1_Current" ] ) ;
baseStationMonitorModel . Seis2_Voltage = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Seis2_Voltage" ] . ToString ( ) ) ? "0" : dataReader [ "Seis2_Voltage" ] ) ;
baseStationMonitorModel . Seis2_Current = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Seis2_Current" ] . ToString ( ) ) ? "0" : dataReader [ "Seis2_Current" ] ) ;
baseStationMonitorModel . Elect_Current = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Elect_Current" ] . ToString ( ) ) ? "0" : dataReader [ "Elect_Current" ] ) ;
baseStationMonitorModel . Out_Voltage12_Reserved1 = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Out_Voltage12_Reserved1" ] . ToString ( ) ) ? "0" : dataReader [ "Out_Voltage12_Reserved1" ] ) ;
baseStationMonitorModel . Out_Voltage12_Reserved2 = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Out_Voltage12_Reserved2" ] . ToString ( ) ) ? "0" : dataReader [ "Out_Voltage12_Reserved2" ] ) ;
baseStationMonitorModel . Reserved = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Reserved" ] . ToString ( ) ) ? "0" : dataReader [ "Reserved" ] ) ;
TotalSystemMonitorDataList . Add ( baseStationMonitorModel ) ;
}
dataReader . Dispose ( ) ;
RecordCount = index - 1 ;
if ( RecordCount < = 10 )
{
TotalPage = 1 ;
2024-09-12 01:48:49 +00:00
SystemMonitorDataList = new ObservableCollection < BaseStationMonitorModel > ( ) ;
2024-09-03 08:30:34 +00:00
SystemMonitorDataList = TotalSystemMonitorDataList ;
}
else
{
TotalPage = ( int ) Math . Ceiling ( ( double ) RecordCount / 10 ) ;
2024-09-12 01:48:49 +00:00
SystemMonitorDataList = new ObservableCollection < BaseStationMonitorModel > ( ) ;
2024-09-03 08:30:34 +00:00
for ( int i = 0 ; i < 10 ; i + + )
{
SystemMonitorDataList . Add ( TotalSystemMonitorDataList [ i ] ) ;
}
}
SystemMonitorDataMsg = string . Format ( "共计{0}页,当前第{1}页" , TotalPage , PageIndex ) ;
string record = "查询接驳盒监测数据历史数据,查询时间范围为:" + StartDateTime + "至" + EndDateTime + ",共查询到" + RecordCount + "条历史数据" ;
sql = $"insert into log_record(StationID,RecordTime,Device_Name,Operation_Type,Record) values('{station_id}','{DateTime.Now}','海底地震监测基站-接驳盒','数据查询','{record}');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
}
}
#endregion
#region CommandBase Define
public CommandBase ForwordSystemMonitorDataCommand { get ; set ; } //读取日志信息
public CommandBase NextSystemMonitorDataCommand { get ; set ; } //读取日志信息
public CommandBase RefreshDataCommand { get ; set ; }
public CommandBase DownloadDataCommand { get ; set ; }
#endregion
int station_id = 1 ;
string timeSearch = "" ;
public DispatcherTimer timerDownloadDataMsgHidden = new DispatcherTimer ( ) ;
public SystemMonitorDataViewModel ( 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 . ForwordSystemMonitorDataCommand = new CommandBase ( ) ;
this . ForwordSystemMonitorDataCommand . DoExcute = new Action < object > ( ForwordSystemMonitorData ) ;
this . ForwordSystemMonitorDataCommand . DoCanExcute = new Func < object , bool > ( ( o ) = > true ) ;
this . NextSystemMonitorDataCommand = new CommandBase ( ) ;
this . NextSystemMonitorDataCommand . DoExcute = new Action < object > ( NextSystemMonitorData ) ;
this . NextSystemMonitorDataCommand . DoCanExcute = new Func < object , bool > ( ( o ) = > true ) ;
this . RefreshDataCommand = new CommandBase ( ) ;
this . RefreshDataCommand . DoExcute = new Action < object > ( RefreshData ) ;
this . RefreshDataCommand . DoCanExcute = new Func < object , bool > ( ( o ) = > true ) ;
this . DownloadDataCommand = new CommandBase ( ) ;
this . DownloadDataCommand . DoExcute = new Action < object > ( DownloadData ) ;
this . DownloadDataCommand . DoCanExcute = new Func < object , bool > ( ( o ) = > true ) ;
#endregion
timerDownloadDataMsgHidden . Interval = TimeSpan . FromSeconds ( 2 ) ;
timerDownloadDataMsgHidden . Tick + = TimerDownloadDataMsgHidden_Tick ;
}
#region RefreshData
public void RefreshData ( object o )
{
if ( IsChecked )
{
2024-09-12 01:48:49 +00:00
timeSearch = " and RecordTime BETWEEN '" + StartDateTime + "' and '" + EndDateTime + "' " ;
2024-09-03 08:30:34 +00:00
}
else
{
timeSearch = "" ;
}
//设置当前页为1
PageIndex = 1 ;
TotalSystemMonitorDataList . Clear ( ) ;
SystemMonitorDataList . Clear ( ) ;
string sql = String . Format ( "select * from juncbox_monitor 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 ( ) )
{
BaseStationMonitorModel baseStationMonitorModel = new BaseStationMonitorModel ( ) ;
baseStationMonitorModel . Index = index + + ;
baseStationMonitorModel . RecordTime = Convert . ToDateTime ( dataReader [ "RecordTime" ] ) ;
baseStationMonitorModel . Seis1_Voltage = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Seis1_Voltage" ] . ToString ( ) ) ? "0" : dataReader [ "Seis1_Voltage" ] ) ;
baseStationMonitorModel . Seis1_Current = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Seis1_Current" ] . ToString ( ) ) ? "0" : dataReader [ "Seis1_Current" ] ) ;
baseStationMonitorModel . Seis2_Voltage = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Seis2_Voltage" ] . ToString ( ) ) ? "0" : dataReader [ "Seis2_Voltage" ] ) ;
baseStationMonitorModel . Seis2_Current = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Seis2_Current" ] . ToString ( ) ) ? "0" : dataReader [ "Seis2_Current" ] ) ;
baseStationMonitorModel . Elect_Current = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Elect_Current" ] . ToString ( ) ) ? "0" : dataReader [ "Elect_Current" ] ) ;
baseStationMonitorModel . Out_Voltage12_Reserved1 = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Out_Voltage12_Reserved1" ] . ToString ( ) ) ? "0" : dataReader [ "Out_Voltage12_Reserved1" ] ) ;
baseStationMonitorModel . Out_Voltage12_Reserved2 = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Out_Voltage12_Reserved2" ] . ToString ( ) ) ? "0" : dataReader [ "Out_Voltage12_Reserved2" ] ) ;
baseStationMonitorModel . Reserved = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Reserved" ] . ToString ( ) ) ? "0" : dataReader [ "Reserved" ] ) ;
TotalSystemMonitorDataList . Add ( baseStationMonitorModel ) ;
}
dataReader . Dispose ( ) ;
RecordCount = index - 1 ;
if ( RecordCount < = 10 )
{
TotalPage = 1 ;
2024-09-12 01:48:49 +00:00
SystemMonitorDataList = new ObservableCollection < BaseStationMonitorModel > ( ) ;
2024-09-03 08:30:34 +00:00
SystemMonitorDataList = TotalSystemMonitorDataList ;
}
else
{
TotalPage = ( int ) Math . Ceiling ( ( double ) RecordCount / 10 ) ;
2024-09-12 01:48:49 +00:00
SystemMonitorDataList = new ObservableCollection < BaseStationMonitorModel > ( ) ;
2024-09-03 08:30:34 +00:00
for ( int i = 0 ; i < 10 ; i + + )
{
SystemMonitorDataList . Add ( TotalSystemMonitorDataList [ i ] ) ;
}
}
SystemMonitorDataMsg = string . Format ( "共计{0}页,当前第{1}页" , TotalPage , PageIndex ) ;
sql = $"insert into log_record(StationID,RecordTime,Device_Name,Operation_Type,Record) values('{station_id}','{DateTime.Now}','海底地震监测基站-接驳盒','数据刷新','刷新接驳盒监测数据');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
}
#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 ;
string baseStationMonitorFolder = Tools . GetAppSetting ( "SystemMonitorDataFolder" ) ;
string savePath = CSVDownload . CreateFile ( baseStationMonitorFolder , "SystemMonitorData_" + DateTime . Now . ToString ( "yyyyMMddhhMMss" ) , "csv" ) ;
bool result = CSVDownload . SaveBaseStationMonitorDataToCSVFile ( TotalSystemMonitorDataList , savePath ) ;
if ( result )
{
DownloadDataMsg = "下载数据成功!" ;
DownloadDataMsgVisibility = Visibility . Visible ;
DownloadDataMsgForeground = new SolidColorBrush ( Colors . Green ) ;
}
else
{
DownloadDataMsg = "下载数据失败!" ;
DownloadDataMsgVisibility = Visibility . Visible ;
DownloadDataMsgForeground = new SolidColorBrush ( Colors . Red ) ;
}
timerDownloadDataMsgHidden . Start ( ) ;
DownloadDataBtnIsEnabled = true ;
string record = "下载接驳盒监测数据," + DownloadDataMsg ;
string sql = $"insert into log_record(StationID,RecordTime,Device_Name,Operation_Type,Record) values('{station_id}','{DateTime.Now}','海底地震监测基站-地震仪','数据下载','{record}');" ;
DBHelper . ExecuteNonQuery ( sql , 1 ) ;
}
#endregion
#region Page Switching
public void ForwordSystemMonitorData ( object o )
{
if ( PageIndex = = 1 )
return ;
try
{
PageIndex - = 1 ;
SystemMonitorDataList . Clear ( ) ;
for ( int i = 0 ; i < 10 ; i + + )
{
SystemMonitorDataList . Add ( TotalSystemMonitorDataList [ i + ( PageIndex - 1 ) * 10 ] ) ;
}
SystemMonitorDataMsg = string . Format ( "共计{0}页,当前第{1}页" , TotalPage , PageIndex ) ;
}
catch
{
TotalSystemMonitorDataList . Clear ( ) ;
SystemMonitorDataList . Clear ( ) ;
}
}
public void NextSystemMonitorData ( object o )
{
if ( PageIndex = = TotalPage )
return ;
try
{
PageIndex + = 1 ;
SystemMonitorDataList . Clear ( ) ;
for ( int i = 0 ; i < ( PageIndex = = TotalPage ? ( RecordCount - ( PageIndex - 1 ) * 10 ) : 10 ) ; i + + )
{
SystemMonitorDataList . Add ( TotalSystemMonitorDataList [ i + ( PageIndex - 1 ) * 10 ] ) ;
}
SystemMonitorDataMsg = string . Format ( "共计{0}页,当前第{1}页" , TotalPage , PageIndex ) ;
}
catch
{
TotalSystemMonitorDataList . Clear ( ) ;
SystemMonitorDataList . Clear ( ) ;
}
}
#endregion
}
}