20240301_JSEQ_upperpc/JiangsuEarthquake_test/JiangsuEarthquake/ViewModels/SystemEnvironDataViewModel.cs

587 lines
23 KiB
C#
Raw Permalink Normal View History

using GalaSoft.MvvmLight.CommandWpf;
using JiangsuEarthquake.Common;
using JiangsuEarthquake.DataAccess;
using JiangsuEarthquake.Models;
using JiangsuEarthquake.Models.FTP;
using JiangsuEarthquake.Views.UserControls;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using static MaterialDesignThemes.Wpf.Theme.ToolBar;
using Application = System.Windows.Application;
using Brush = System.Windows.Media.Brush;
namespace JiangsuEarthquake.ViewModels
{
public class SystemEnvironDataViewModel : 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<BaseStationEnvirModel> systemStateDataList = new ObservableCollection<BaseStationEnvirModel>();
public ObservableCollection<BaseStationEnvirModel> SystemStateDataList
{
get { return systemStateDataList; }
set { systemStateDataList = value; this.DoNotify(); }
}
private ObservableCollection<BaseStationEnvirModel> totalSystemStateDataList = new ObservableCollection<BaseStationEnvirModel>();
public ObservableCollection<BaseStationEnvirModel> 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; }
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;
//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 juncbox_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 juncbox_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())
{
BaseStationEnvirModel baseStationEnvirModel = new BaseStationEnvirModel();
baseStationEnvirModel.Index = index++;
baseStationEnvirModel.DataTime = Convert.ToDateTime(dataReader["DataTime"]);
baseStationEnvirModel.RecordTime = Convert.ToDateTime(dataReader["RecordTime"]);
baseStationEnvirModel.Temperature = Convert.ToSingle(string.IsNullOrEmpty(dataReader["Temperature"].ToString()) ? "0" : dataReader["Temperature"]);
baseStationEnvirModel.Humidity = Convert.ToSingle(string.IsNullOrEmpty(dataReader["Humidity"].ToString()) ? "0" : dataReader["Humidity"]);
baseStationEnvirModel.AttitudeX = Convert.ToSingle(string.IsNullOrEmpty(dataReader["AttitudeX"].ToString()) ? "0" : dataReader["AttitudeX"]);
baseStationEnvirModel.AttitudeY = Convert.ToSingle(string.IsNullOrEmpty(dataReader["AttitudeY"].ToString()) ? "0" : dataReader["AttitudeY"]);
baseStationEnvirModel.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)
{
baseStationEnvirModel.Leakage = (ImageSource)Application.Current.FindResource("CycleRed");
}
else if (leakState == 0)
{
baseStationEnvirModel.Leakage = (ImageSource)Application.Current.FindResource("CycleGreen");
}
else
{
baseStationEnvirModel.Leakage = (ImageSource)Application.Current.FindResource("CycleGray");
}
//int hatchState = Convert.ToInt32(string.IsNullOrEmpty(dataReaderSub["Hatch_State"].ToString()) ? "2" : dataReaderSub["Hatch_State"]);
//if (hatchState == 1)
//{
// baseStationEnvirModel.Hatch_State = (ImageSource)Application.Current.FindResource("CycleRed");
//}
//else if (hatchState == 0)
//{
// baseStationEnvirModel.Hatch_State = (ImageSource)Application.Current.FindResource("CycleGreen");
//}
//else
//{
// baseStationEnvirModel.Hatch_State = (ImageSource)Application.Current.FindResource("CycleGray");
//}
}
else
{
baseStationEnvirModel.Leakage = (ImageSource)Application.Current.FindResource("CycleGray");
//baseStationEnvirModel.Hatch_State = (ImageSource)Application.Current.FindResource("CycleGray");
}
TotalSystemStateDataList.Add(baseStationEnvirModel);
}
dataReader.Dispose();
dataReaderSub.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);
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 SystemEnvironDataViewModel(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<object>(ForwordSystemState);
this.ForwordSystemStateCommand.DoCanExcute = new Func<object, bool>((o) => true);
this.NextSystemStateCommand = new CommandBase();
this.NextSystemStateCommand.DoExcute = new Action<object>(NextSystemState);
this.NextSystemStateCommand.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 Ask Data
//public void AskData(object o)
//{
// if (station_id == 1)
// {
// if (MainWindow.mainViewModel.serverModel1.IsOpened)
// MainWindow.mainViewModel.serverModel1.SendMessage(new byte[4] { 0x00, 0x01, 0x02, 0x04 });
// }
// else
// {
// if (MainWindow.mainViewModel.serverModel2.IsOpened)
// MainWindow.mainViewModel.serverModel2.SendMessage(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 juncbox_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 juncbox_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())
{
BaseStationEnvirModel baseStationEnvirModel = new BaseStationEnvirModel();
baseStationEnvirModel.Index = index++;
baseStationEnvirModel.DataTime = Convert.ToDateTime(dataReader["DataTime"]);
baseStationEnvirModel.RecordTime = Convert.ToDateTime(dataReader["RecordTime"]);
baseStationEnvirModel.Temperature = Convert.ToSingle(string.IsNullOrEmpty(dataReader["Temperature"].ToString()) ? "0" : dataReader["Temperature"]);
baseStationEnvirModel.Humidity = Convert.ToSingle(string.IsNullOrEmpty(dataReader["Humidity"].ToString()) ? "0" : dataReader["Humidity"]);
baseStationEnvirModel.AttitudeX = Convert.ToSingle(string.IsNullOrEmpty(dataReader["AttitudeX"].ToString()) ? "0" : dataReader["AttitudeX"]);
baseStationEnvirModel.AttitudeY = Convert.ToSingle(string.IsNullOrEmpty(dataReader["AttitudeY"].ToString()) ? "0" : dataReader["AttitudeY"]);
baseStationEnvirModel.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)
{
baseStationEnvirModel.Leakage = (ImageSource)Application.Current.FindResource("CycleRed");
}
else if (leakState == 0)
{
baseStationEnvirModel.Leakage = (ImageSource)Application.Current.FindResource("CycleGreen");
}
else
{
baseStationEnvirModel.Leakage = (ImageSource)Application.Current.FindResource("CycleGray");
}
//int hatchState = Convert.ToInt32(string.IsNullOrEmpty(dataReaderSub["Hatch_State"].ToString()) ? "2" : dataReaderSub["Hatch_State"]);
//if (hatchState == 1)
//{
// baseStationEnvirModel.Hatch_State = (ImageSource)Application.Current.FindResource("CycleRed");
//}
//else if (hatchState == 0)
//{
// baseStationEnvirModel.Hatch_State = (ImageSource)Application.Current.FindResource("CycleGreen");
//}
//else
//{
// baseStationEnvirModel.Hatch_State = (ImageSource)Application.Current.FindResource("CycleGray");
//}
}
else
{
baseStationEnvirModel.Leakage = (ImageSource)Application.Current.FindResource("CycleGray");
//baseStationEnvirModel.Hatch_State = (ImageSource)Application.Current.FindResource("CycleGray");
}
TotalSystemStateDataList.Add(baseStationEnvirModel);
}
dataReader.Dispose();
dataReaderSub.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);
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 baseStationFolder = Tools.GetAppSetting("BaseStationFolder");
string savePath = CSVDownload.CreateFile(baseStationFolder, "SystemStateData_" + DateTime.Now.ToString("yyyyMMddhhMMss"), "csv");
bool result = CSVDownload.SaveBaseStationEnvironDataToCSVFile(TotalSystemStateDataList, 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
}
}