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

423 lines
16 KiB
C#
Raw Normal View History

2024-11-01 07:54:08 +00:00
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 int systemStateTotalPage;
public int SystemStateTotalPage
{
get { return systemStateTotalPage; }
set { systemStateTotalPage = value; this.DoNotify(); }
}
private int systemStateNowPage;
public int SystemStateNowPage
{
get { return systemStateNowPage; }
set { systemStateNowPage = 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 RefreshDataCommand { get; set; }
public CommandBase DownloadDataCommand { get; set; }
public CommandBase SearchEnvironDataCommand { get; set; }
public CommandBase ResetSearchCommand { get; set; }
#endregion
int station_id = 1;
public static DialogViewModel vm;
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.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.SearchEnvironDataCommand = new CommandBase();
this.SearchEnvironDataCommand.DoExcute = new Action<object>(SearchEnvironData);
this.SearchEnvironDataCommand.DoCanExcute = new Func<object, bool>((o) => true);
this.ResetSearchCommand = new CommandBase();
this.ResetSearchCommand.DoExcute = new Action<object>(ResetSearch);
this.ResetSearchCommand.DoCanExcute = new Func<object, bool>((o) => true);
#endregion
}
#region Page Switching
public void ForwordSystemState(object o)
{
if (PageIndex == 1)
return;
try
{
PageIndex -= 1;
SystemStateDataList.Clear();
for (int i = 0; i < 10; i++)
{
SystemStateDataList.Add(TotalSystemStateDataList[i + (PageIndex - 1) * 10]);
}
SystemStateTotalPage = TotalPage;
SystemStateNowPage = PageIndex;
}
catch
{
TotalSystemStateDataList.Clear();
SystemStateDataList.Clear();
}
}
public void NextSystemState(object o)
{
if (PageIndex == TotalPage)
return;
try
{
PageIndex += 1;
SystemStateDataList.Clear();
for (int i = 0; i < (PageIndex == TotalPage ? (RecordCount - (PageIndex - 1) * 10) : 10); i++)
{
SystemStateDataList.Add(TotalSystemStateDataList[i + (PageIndex - 1) * 10]);
}
SystemStateTotalPage = TotalPage;
SystemStateNowPage = PageIndex;
}
catch
{
TotalSystemStateDataList.Clear();
SystemStateDataList.Clear();
}
}
#endregion
#region RefreshData
public void RefreshData(object o)
{
this.EndDateTime = DateTime.Now;
this.StartDateTime = DateTime.Now.AddDays(-1);
//设置当前页为1
PageIndex = 1;
TotalSystemStateDataList.Clear();
SystemStateDataList.Clear();
string sql = String.Format("select * from juncbox_env where StationID = {0} ORDER by id desc limit 100", station_id);
MySqlDataReader dataReader = DBHelper.ExecuteReader(sql, 1);
string sqlSub = String.Format("select * from juncbox_cavity_state where StationID = {0} ORDER by id desc limit 100", station_id);
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 = "漏水";
}
else if (leakState == 0)
{
baseStationEnvirModel.Leakage = "未漏水";
}
else
{
baseStationEnvirModel.Leakage = "未知";
}
}
else
{
baseStationEnvirModel.Leakage = "未知";
}
TotalSystemStateDataList.Add(baseStationEnvirModel);
}
dataReader.Dispose();
dataReaderSub.Dispose();
RecordCount = index - 1;
if (RecordCount <= 10)
{
TotalPage = 1;
SystemStateDataList = new ObservableCollection<BaseStationEnvirModel>();
SystemStateDataList = TotalSystemStateDataList;
}
else
{
TotalPage = (int)Math.Ceiling((double)RecordCount / 10);
SystemStateDataList = new ObservableCollection<BaseStationEnvirModel>();
for (int i = 0; i < 10; i++)
{
SystemStateDataList.Add(TotalSystemStateDataList[i]);
}
}
SystemStateTotalPage = TotalPage;
SystemStateNowPage = 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
public void DownloadData(object o)
{
string baseStationFolder = Tools.GetAppSetting("BaseStationFolder");
string savePath = CSVDownload.CreateFile(baseStationFolder, "SystemStateData_" + DateTime.Now.ToString("yyyyMMddhhMMss"), "csv");
string DownloadDataMsg = "";
bool result = CSVDownload.SaveBaseStationEnvironDataToCSVFile(TotalSystemStateDataList, savePath);
if (result)
{
DownloadDataMsg = "下载数据成功!";
}
else
{
DownloadDataMsg = "下载数据失败!";
}
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 SerachData
public void SearchEnvironData(object o)
{
if (string.IsNullOrEmpty(EndDateTime.ToString()) || string.IsNullOrEmpty(StartDateTime.ToString()))
{
HandyControl.Controls.Dialog.Show(new TextDialog("请选择起始时间和结束时间!"));
return;
}
if (EndDateTime < StartDateTime)
{
HandyControl.Controls.Dialog.Show(new TextDialog("起始时间大于结束时间,\n请重新输入"));
return;
}
string timeSearch = " and RecordTime BETWEEN '" + StartDateTime + "' and '" + EndDateTime + "' ";
//设置当前页为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 = "漏水";
}
else if (leakState == 0)
{
baseStationEnvirModel.Leakage = "未漏水";
}
else
{
baseStationEnvirModel.Leakage = "未知";
}
}
else
{
baseStationEnvirModel.Leakage = "未知";
}
TotalSystemStateDataList.Add(baseStationEnvirModel);
}
dataReader.Dispose();
dataReaderSub.Dispose();
RecordCount = index - 1;
if (RecordCount <= 10)
{
TotalPage = 1;
SystemStateDataList = new ObservableCollection<BaseStationEnvirModel>();
SystemStateDataList = TotalSystemStateDataList;
}
else
{
TotalPage = (int)Math.Ceiling((double)RecordCount / 10);
SystemStateDataList = new ObservableCollection<BaseStationEnvirModel>();
for (int i = 0; i < 10; i++)
{
SystemStateDataList.Add(TotalSystemStateDataList[i]);
}
}
SystemStateTotalPage = TotalPage;
SystemStateNowPage = 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);
}
public void ResetSearch(object o)
{
RefreshData(null);
}
#endregion
}
}