using HMI.Base;
using HMI.Core.Util;
using HMI.UI.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
namespace HMI.LogViewer
{
///
/// UserControl1.xaml 的交互逻辑
///
public partial class LogViewer : PluginControl
{
private string logDir;
private string todayLogFile;
private bool autoRfh;
public bool AutoRfh
{
get { return autoRfh; }
set
{
Set(ref autoRfh, value);
if (autoRfhTimer != null) autoRfhTimer.Enabled = value;
}
}
private Timer autoRfhTimer;
public LogViewer()
{
string logDir = Path.Combine(PathHelper.AppFolder, "Log");
todayLogFile = Path.Combine(logDir, $"log{DateTime.Now.ToString("yyyy-MM-dd")}.db");
if (!Directory.Exists(logDir))
{
Directory.CreateDirectory(logDir);
}
this.logDir = logDir;
DataContext = this;
InitializeComponent();
}
public override void InitFinish()
{
base.InitFinish();
string baseFile = Path.Combine(logDir, "log.db");
try
{
if (!File.Exists(baseFile))
{
SqliteHelper.CreateDatabase(baseFile);
}
SqliteHelper sqliteHelper = new SqliteHelper(baseFile);
sqliteHelper.ExecuteSql(@"CREATE TABLE IF NOT EXISTS log (
id INTEGER PRIMARY KEY AUTOINCREMENT,
type TEXT NOT NULL,
text TEXT NOT NULL,
source TEXT,
time TIME
);");
}
catch (Exception ex)
{
log.Error("Create Log File Failed.", ex);
}
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
autoRfhTimer = new Timer
{
Enabled = false,
Interval = 1000
};
autoRfhTimer.Elapsed += AutoRfhTimer_Elapsed;
Loaded -= UserControl_Loaded;
}
private void AutoRfhTimer_Elapsed(object sender, ElapsedEventArgs e)
{
Refresh100items();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
switch (btn.Name)
{
case "queryBtn":
QueryLog();
break;
case "resetBtn":
QueryReset();
break;
case "exportBtn":
break;
default:
break;
}
}
private void QueryLog()
{
try
{
queryBtn.IsEnabled = false;
autoRfhInput.IsEnabled = false;
Filtrate();
queryBtn.IsEnabled = true;
autoRfhInput.IsEnabled = true;
}
catch (Exception ex)
{
log.Error("Log Query Failed.", ex);
this.Error($"查询失败!{ex.Message}");
}
}
private void QueryReset()
{
levelInput.Text = null;
startTimeInput.SelectedDateTime = null;
endTimeInput.SelectedDateTime = null;
startTimeInput.Text = null;
table.ItemsSource = null;
}
private void Filtrate()
{
string str = LoadSql();
DataTable dt = new DataTable();
var files = Directory.GetFiles(logDir, "*.db");
foreach (string file in files)
{
SqliteHelper sql = new SqliteHelper(file);
dt.Merge(sql.GetDataTable(str));
sql.Close();
}
dt.DefaultView.Sort = "time Desc";
table.ItemsSource = dt.DefaultView;
}
///
/// 加载界面判断数据库语句
///
/// str数据库的语句
private string LoadSql()
{
string sql = " select type,time,source,text from log";
List datas = new List();
if (!string.IsNullOrEmpty(levelInput.Text))
{
datas.Add($"type='{levelInput.Text}'");
}
if (!string.IsNullOrEmpty(startTimeInput.Text))
{
datas.Add($"time>='{startTimeInput.Text}'");
}
if (!string.IsNullOrEmpty(endTimeInput.Text))
{
datas.Add($"time<='{endTimeInput.Text}'");
}
if (!string.IsNullOrEmpty(sourceInput.Text))
{
datas.Add($"source='{sourceInput.Text}'");
}
string condition = string.Join(" AND ", datas);
condition = string.IsNullOrEmpty(condition) ? string.Empty : $" WHERE {condition}";
sql = $"{sql}{condition} order by time desc";
return sql;
}
///
/// 刷新100条数据
///
private void Refresh100items()
{
try
{
if (!File.Exists(todayLogFile)) return;
string str = "select type,time,source,text from log order by time desc limit 100";
SqliteHelper sql = new SqliteHelper(todayLogFile);
var dt = sql.GetDataTable(str);
this.Dispatcher.BeginInvoke((Action)delegate ()
{
table.ItemsSource = dt.DefaultView;
});
}
catch (Exception ex)
{
log.Error("Log Auto Refresh Failed.", ex);
this.Error($"自动刷新失败!{ex.Message}");
}
}
}
}