2024-09-12 02:04:55 +00:00
using FujianEarthquake.Common ;
using FujianEarthquake.DataAccess ;
using FujianEarthquake.Views.UserControls ;
using FujianEarthquake.Models ;
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 ;
using System.Windows.Media ;
using System.Windows.Threading ;
namespace FujianEarthquake.ViewModels
{
public class JunctionBoxEnvironDataViewModel : 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 junctionBoxEnvironMsg ;
public string JunctionBoxEnvironMsg
{
get { return junctionBoxEnvironMsg ; }
set { junctionBoxEnvironMsg = value ; this . DoNotify ( ) ; }
}
private ObservableCollection < JunctionBoxEnvironModel > junctionBoxEnvironDataList = new ObservableCollection < JunctionBoxEnvironModel > ( ) ;
public ObservableCollection < JunctionBoxEnvironModel > JunctionBoxEnvironDataList
{
get { return junctionBoxEnvironDataList ; }
set { junctionBoxEnvironDataList = value ; this . DoNotify ( ) ; }
}
private ObservableCollection < JunctionBoxEnvironModel > totalJunctionBoxEnvironDataList = new ObservableCollection < JunctionBoxEnvironModel > ( ) ;
public ObservableCollection < JunctionBoxEnvironModel > TotalJunctionBoxEnvironDataList
{
get { return totalJunctionBoxEnvironDataList ; }
set { totalJunctionBoxEnvironDataList = value ; this . DoNotify ( ) ; }
}
#endregion
#region Command Define
public CommandBase ForwordJunctionBoxEnvironCommand { get ; set ; }
public CommandBase NextJunctionBoxEnvironCommand { get ; set ; }
//public CommandBase AskDataBtnCommand { get; set; }
public CommandBase RefreshDataCommand { get ; set ; }
public CommandBase DownloadDataCommand { get ; set ; }
public CommandBase FTPSettingCommand { get ; set ; }
public CommandBase RefreshFileCommand { get ; set ; }
#endregion
int station_id = 1 ;
public DispatcherTimer timerDownloadDataMsgHidden = new DispatcherTimer ( ) ;
//public FTPSettingView fTPSettingView1 = new FTPSettingView(1);
//public FTPSettingView fTPSettingView2 = new FTPSettingView(2);
//public FTPLIST ftpList { get; set; } = new FTPLIST();
//public FTPConnectModel ftpcon { get; set; } = new FTPConnectModel();
#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 ;
return ;
}
timeSearch = " and DataTime BETWEEN '" + StartDateTime + "' and '" + EndDateTime + "' " ;
}
else
{
timeSearch = "" ;
}
//设置当前页为1
PageIndex = 1 ;
TotalJunctionBoxEnvironDataList . Clear ( ) ;
JunctionBoxEnvironDataList . Clear ( ) ;
string sql = String . Format ( "select * from underjuncbox_env where StationID = {0} {1} ORDER by id desc limit 100" , station_id , timeSearch ) ;
MySqlDataReader dataReader = DBHelper . ExecuteReader ( sql , 1 ) ;
string sqlSub = String . Format ( "select * from underjuncbox_cavity_state where StationID = {0} {1} ORDER by id desc limit 100" , station_id , timeSearch ) ;
MySqlDataReader dataReaderSub = DBHelper . ExecuteReader ( sqlSub , 1 ) ;
int index = 1 ;
while ( dataReader . Read ( ) )
{
JunctionBoxEnvironModel JunctionBoxEnvironModel = new JunctionBoxEnvironModel ( ) ;
JunctionBoxEnvironModel . Index = index + + ;
JunctionBoxEnvironModel . DataTime = Convert . ToDateTime ( dataReader [ "DataTime" ] ) ;
JunctionBoxEnvironModel . RecordTime = Convert . ToDateTime ( dataReader [ "RecordTime" ] ) ;
JunctionBoxEnvironModel . Temperature = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Temperature" ] . ToString ( ) ) ? "0" : dataReader [ "Temperature" ] ) ;
JunctionBoxEnvironModel . Humidity = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Humidity" ] . ToString ( ) ) ? "0" : dataReader [ "Humidity" ] ) ;
JunctionBoxEnvironModel . AttitudeX = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "AttitudeX" ] . ToString ( ) ) ? "0" : dataReader [ "AttitudeX" ] ) ;
JunctionBoxEnvironModel . AttitudeY = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "AttitudeY" ] . ToString ( ) ) ? "0" : dataReader [ "AttitudeY" ] ) ;
JunctionBoxEnvironModel . AttitudeZ = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "AttitudeZ" ] . ToString ( ) ) ? "0" : dataReader [ "AttitudeZ" ] ) ;
if ( dataReaderSub . Read ( ) )
{
int leakState = Convert . ToInt32 ( string . IsNullOrEmpty ( dataReaderSub [ "Leakage" ] . ToString ( ) ) ? "2" : dataReaderSub [ "Leakage" ] ) ;
if ( leakState = = 1 )
{
JunctionBoxEnvironModel . Leakage = ( ImageSource ) Application . Current . FindResource ( "CycleRed" ) ;
}
else if ( leakState = = 0 )
{
JunctionBoxEnvironModel . Leakage = ( ImageSource ) Application . Current . FindResource ( "CycleGreen" ) ;
}
else
{
JunctionBoxEnvironModel . Leakage = ( ImageSource ) Application . Current . FindResource ( "CycleGray" ) ;
}
}
else
{
JunctionBoxEnvironModel . Leakage = ( ImageSource ) Application . Current . FindResource ( "CycleGray" ) ;
}
TotalJunctionBoxEnvironDataList . Add ( JunctionBoxEnvironModel ) ;
}
dataReader . Dispose ( ) ;
dataReaderSub . Dispose ( ) ;
RecordCount = index - 1 ;
if ( RecordCount < = 10 )
{
TotalPage = 1 ;
2024-09-19 10:17:46 +00:00
JunctionBoxEnvironDataList = new ObservableCollection < JunctionBoxEnvironModel > ( ) ;
2024-09-12 02:04:55 +00:00
JunctionBoxEnvironDataList = TotalJunctionBoxEnvironDataList ;
}
else
{
TotalPage = ( int ) Math . Ceiling ( ( double ) RecordCount / 10 ) ;
2024-09-19 10:17:46 +00:00
JunctionBoxEnvironDataList = new ObservableCollection < JunctionBoxEnvironModel > ( ) ;
2024-09-12 02:04:55 +00:00
for ( int i = 0 ; i < 10 ; i + + )
{
JunctionBoxEnvironDataList . Add ( TotalJunctionBoxEnvironDataList [ i ] ) ;
}
}
JunctionBoxEnvironMsg = 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
public JunctionBoxEnvironDataViewModel ( 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 . ForwordJunctionBoxEnvironCommand = new CommandBase ( ) ;
this . ForwordJunctionBoxEnvironCommand . DoExcute = new Action < object > ( ForwordJunctionBoxEnviron ) ;
this . ForwordJunctionBoxEnvironCommand . DoCanExcute = new Func < object , bool > ( ( o ) = > true ) ;
this . NextJunctionBoxEnvironCommand = new CommandBase ( ) ;
this . NextJunctionBoxEnvironCommand . DoExcute = new Action < object > ( NextJunctionBoxEnviron ) ;
this . NextJunctionBoxEnvironCommand . DoCanExcute = new Func < object , bool > ( ( o ) = > true ) ;
//this.AskDataBtnCommand = new CommandBase();
//this.AskDataBtnCommand.DoExcute = new Action<object>(AskData);
//this.AskDataBtnCommand.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 ) ;
this . FTPSettingCommand = new CommandBase ( ) ;
this . FTPSettingCommand . DoExcute = new Action < object > ( FTPSetting ) ;
this . FTPSettingCommand . DoCanExcute = new Func < object , bool > ( ( o ) = > true ) ;
this . RefreshFileCommand = new CommandBase ( ) ;
this . RefreshFileCommand . DoExcute = new Action < object > ( RefreshFile ) ;
this . RefreshFileCommand . DoCanExcute = new Func < object , bool > ( ( o ) = > true ) ;
#endregion
#region DispatcherTimer Set
timerDownloadDataMsgHidden . Interval = TimeSpan . FromSeconds ( 2 ) ;
timerDownloadDataMsgHidden . Tick + = TimerDownloadDataMsgHidden_Tick ;
#endregion
//if (station_id == 1)
//{
// ftpcon.IP = Tools.GetAppSetting("FTPIP1");
// ftpcon.Port = int.Parse(Tools.GetAppSetting("FTPPort1"));
// ftpcon.UserName = Tools.GetAppSetting("FTPUserName1");
// ftpcon.PassWord = Tools.GetAppSetting("FTPPassword1");
//}
//else
//{
// ftpcon.IP = Tools.GetAppSetting("FTPIP2");
// ftpcon.Port = int.Parse(Tools.GetAppSetting("FTPPort2"));
// ftpcon.UserName = Tools.GetAppSetting("FTPUserName2");
// ftpcon.PassWord = Tools.GetAppSetting("FTPPassword2");
//}
}
/// <summary>
/// 更新FTP文件目录
/// </summary>
/// <param name="FTPPath"></param>
public void RefreshFTPList ( string FTPPath )
{
// try
// {
// string ftpURL = ftpcon.getFtpHelper(ftpcon.IP, ftpcon.Port, FTPPath, ftpcon.UserName, ftpcon.PassWord, "");
// List<FTPModel> list = ftpcon.ListDirectories(); //该目录下的文件夹
// List<FTPModel> File_list = ftpcon.ListFiles(); //该目录下的文件
// if (File_list != null)
// {
// for (int i = 0; i < File_list.Count; i++)
// {
// list.Add(File_list[i]);
// }
// }
// ftpList.FTPList = list;
// }
// catch (Exception ex)
// {
// }
}
public void RefreshFile ( object o )
{
// if (Tools.PingIp(ftpcon.IP))
// RefreshFTPList("FTP_Test");
}
#region Page Switching
public void ForwordJunctionBoxEnviron ( object o )
{
if ( PageIndex = = 1 )
return ;
try
{
PageIndex - = 1 ;
JunctionBoxEnvironDataList . Clear ( ) ;
for ( int i = 0 ; i < 10 ; i + + )
{
JunctionBoxEnvironDataList . Add ( TotalJunctionBoxEnvironDataList [ i + ( PageIndex - 1 ) * 10 ] ) ;
}
JunctionBoxEnvironMsg = string . Format ( "共计{0}页,当前第{1}页" , TotalPage , PageIndex ) ;
}
catch
{
TotalJunctionBoxEnvironDataList . Clear ( ) ;
JunctionBoxEnvironDataList . Clear ( ) ;
}
}
public void NextJunctionBoxEnviron ( object o )
{
if ( PageIndex = = TotalPage )
return ;
try
{
PageIndex + = 1 ;
JunctionBoxEnvironDataList . Clear ( ) ;
for ( int i = 0 ; i < ( PageIndex = = TotalPage ? ( RecordCount - ( PageIndex - 1 ) * 10 ) : 10 ) ; i + + )
{
JunctionBoxEnvironDataList . Add ( TotalJunctionBoxEnvironDataList [ i + ( PageIndex - 1 ) * 10 ] ) ;
}
JunctionBoxEnvironMsg = string . Format ( "共计{0}页,当前第{1}页" , TotalPage , PageIndex ) ;
}
catch
{
TotalJunctionBoxEnvironDataList . Clear ( ) ;
JunctionBoxEnvironDataList . Clear ( ) ;
}
}
#endregion
#region RefreshData
public void RefreshData ( object o )
{
if ( IsChecked )
{
timeSearch = " and DataTime BETWEEN '" + StartDateTime + "' and '" + EndDateTime + "' " ;
}
else
{
timeSearch = "" ;
}
//设置当前页为1
PageIndex = 1 ;
TotalJunctionBoxEnvironDataList . Clear ( ) ;
JunctionBoxEnvironDataList . Clear ( ) ;
string sql = String . Format ( "select * from underjuncbox_env where StationID = {0} {1} ORDER by id desc limit 100" , station_id , timeSearch ) ;
MySqlDataReader dataReader = DBHelper . ExecuteReader ( sql , 1 ) ;
string sqlSub = String . Format ( "select * from underjuncbox_cavity_state where StationID = {0} {1} ORDER by id desc limit 100" , station_id , timeSearch ) ;
MySqlDataReader dataReaderSub = DBHelper . ExecuteReader ( sqlSub , 1 ) ;
int index = 1 ;
while ( dataReader . Read ( ) )
{
JunctionBoxEnvironModel junctionBoxEnvironModel = new JunctionBoxEnvironModel ( ) ;
junctionBoxEnvironModel . Index = index + + ;
junctionBoxEnvironModel . DataTime = Convert . ToDateTime ( dataReader [ "DataTime" ] ) ;
junctionBoxEnvironModel . RecordTime = Convert . ToDateTime ( dataReader [ "RecordTime" ] ) ;
junctionBoxEnvironModel . Temperature = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Temperature" ] . ToString ( ) ) ? "0" : dataReader [ "Temperature" ] ) ;
junctionBoxEnvironModel . Humidity = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Humidity" ] . ToString ( ) ) ? "0" : dataReader [ "Humidity" ] ) ;
junctionBoxEnvironModel . AttitudeX = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "AttitudeX" ] . ToString ( ) ) ? "0" : dataReader [ "AttitudeX" ] ) ;
junctionBoxEnvironModel . AttitudeY = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "AttitudeY" ] . ToString ( ) ) ? "0" : dataReader [ "AttitudeY" ] ) ;
junctionBoxEnvironModel . AttitudeZ = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "AttitudeZ" ] . ToString ( ) ) ? "0" : dataReader [ "AttitudeZ" ] ) ;
if ( dataReaderSub . Read ( ) )
{
int leakState = Convert . ToInt32 ( string . IsNullOrEmpty ( dataReaderSub [ "Leakage" ] . ToString ( ) ) ? "2" : dataReaderSub [ "Leakage" ] ) ;
if ( leakState = = 1 )
{
junctionBoxEnvironModel . Leakage = ( ImageSource ) Application . Current . FindResource ( "CycleRed" ) ;
}
else if ( leakState = = 0 )
{
junctionBoxEnvironModel . Leakage = ( ImageSource ) Application . Current . FindResource ( "CycleGreen" ) ;
}
else
{
junctionBoxEnvironModel . Leakage = ( ImageSource ) Application . Current . FindResource ( "CycleGray" ) ;
}
}
else
{
junctionBoxEnvironModel . Leakage = ( ImageSource ) Application . Current . FindResource ( "CycleGray" ) ;
}
TotalJunctionBoxEnvironDataList . Add ( junctionBoxEnvironModel ) ;
}
dataReader . Dispose ( ) ;
dataReaderSub . Dispose ( ) ;
RecordCount = index - 1 ;
if ( RecordCount < = 10 )
{
TotalPage = 1 ;
2024-09-19 10:17:46 +00:00
JunctionBoxEnvironDataList = new ObservableCollection < JunctionBoxEnvironModel > ( ) ;
2024-09-12 02:04:55 +00:00
JunctionBoxEnvironDataList = TotalJunctionBoxEnvironDataList ;
}
else
{
TotalPage = ( int ) Math . Ceiling ( ( double ) RecordCount / 10 ) ;
2024-09-19 10:17:46 +00:00
JunctionBoxEnvironDataList = new ObservableCollection < JunctionBoxEnvironModel > ( ) ;
2024-09-12 02:04:55 +00:00
for ( int i = 0 ; i < 10 ; i + + )
{
JunctionBoxEnvironDataList . Add ( TotalJunctionBoxEnvironDataList [ i ] ) ;
}
}
JunctionBoxEnvironMsg = 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 junctionBoxEnvironFolder = Tools . GetAppSetting ( "JunctionBoxFolder" ) ;
string savePath = CSVDownload . CreateFile ( junctionBoxEnvironFolder , "JunctionBoxEnvironData_" + DateTime . Now . ToString ( "yyyyMMddhhMMss" ) , "csv" ) ;
bool result = CSVDownload . SaveJunctionBoxEnvironDataToCSVFile ( TotalJunctionBoxEnvironDataList , 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 FTPSetting
public void FTPSetting ( object o )
{
//if (station_id == 1)
// HandyControl.Controls.Dialog.Show(fTPSettingView1);
//else
// HandyControl.Controls.Dialog.Show(fTPSettingView2);
}
#endregion
}
}