using HMI.Base;
using HMI.Core.Util;
using HMI.Util;
using Microsoft.Web.WebView2.Core;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace Plugin.DataBoard
{
///
/// UserControl1.xaml 的交互逻辑
///
public partial class DataBoard : PluginControl
{
public DataBoard()
{
DataContext = this;
InitializeComponent();
webview2.DefaultBackgroundColor = Color.Transparent;//Color.FromArgb(243, 249, 255);
}
private ObservableCollection _options = new ObservableCollection {
new OptionData { Name = LangUtil.Get("bd.today"), Value = "today" },
//new OptionData { Name = LangUtil.Get("bd.today"), Value = "today" },
new OptionData { Name = LangUtil.Get("bd.week"), Value = "week" },
new OptionData { Name = LangUtil.Get("bd.month"), Value = "month" }
};
public ObservableCollection Options
{
get { return _options; }
set { Set(ref _options, value); }
}
private void PluginControl_Loaded(object sender, RoutedEventArgs e)
{
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var btn = sender as Button;
DateTime start, end;
switch (btn.Name)
{
case "btn0":
start = dtp00.SelectedDateTime ?? DateTime.Now;
end = dtp01.SelectedDateTime ?? DateTime.Now;
RefreshChart0(start, end);
break;
case "btn1":
start = dtp10.SelectedDateTime ?? DateTime.Now;
end = dtp11.SelectedDateTime ?? DateTime.Now;
RefreshChart1(start, end);
break;
case "btn2":
start = dtp20.SelectedDateTime ?? DateTime.Now;
end = dtp21.SelectedDateTime ?? DateTime.Now;
RefreshChart2(start, end);
break;
case "btn3":
start = dtp30.SelectedDateTime ?? DateTime.Now;
end = dtp31.SelectedDateTime ?? DateTime.Now;
RefreshChart3(start, end);
break;
default:
break;
}
}
private List SelectSerialDataByTime(DateTime start, DateTime end)
{
string[] files = DbFileHelper.GetDbFiles(BaseFolder, start, end);
if (files == null || files.Length <= 0)
{
return new List();
}
string sql = $"SELECT * FROM serialData WHERE time BETWEEN '{start:yyyy-MM-dd HH:mm:ss}' AND '{end:yyyy-MM-dd HH:mm:ss}';";
DataTable dt = new DataTable();
foreach (string file in files)
{
using (SqliteHelper helper = new SqliteHelper(file))
{
dt.Merge(helper.GetDataTable(sql));
}
}
return BeanUtil.ReadBeanFromDataTable(dt);
}
private void webview2_Loaded(object sender, RoutedEventArgs e)
{
string path = Path.Combine(PathHelper.AppFolder, "Libs", "Static", "test.html");
webview2.Source = new Uri(path);
}
private void Webview2_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
{
cb0.SelectedIndex = 0;
cb1.SelectedIndex = 0;
cb2.SelectedIndex = 0;
cb3.SelectedIndex = 0;
}
private void webview2_SizeChanged(object sender, SizeChangedEventArgs e)
{
var width = this.webview2.ActualWidth - 100;
var height = this.webview2.ActualHeight;
webview2.CoreWebView2?.ExecuteScriptAsync($"resize({width},{height})");
}
private void RefreshChart0(DateTime start, DateTime end)
{
dtp00.SelectedDateTime = start;
dtp01.SelectedDateTime = end;
List datas = SelectSerialDataByTime(start, end);
datas.OrderByDescending(e => e.time);
int all = 0, output = 0;
HashSet snCache = new HashSet();
foreach (var data in datas)
{
if (snCache.Contains(data.sn)) continue;
snCache.Add(data.sn);
all++;
if (data.status == "Passed") output++;
}
var fields = $"['{this.lang("bd.input")}','{this.lang("bd.output")}']";
webview2.CoreWebView2?.ExecuteScriptAsync($"drawChart0({fields}, [{all},{output}])");
}
private void RefreshChart1(DateTime start, DateTime end)
{
dtp10.SelectedDateTime = start;
dtp11.SelectedDateTime = end;
List datas = SelectSerialDataByTime(start, end);
datas = datas.OrderByDescending(e => e.time).ToList();
Dictionary pieData = new Dictionary {
{ "Passed",0},
{ "Failed",0},
{ "Action",0},
{ "Wait",0},
};
HashSet snCache = new HashSet();
foreach (var data in datas)
{
if (snCache.Contains(data.sn)) continue;
snCache.Add(data.sn);
if (pieData.ContainsKey(data.status))
{
pieData[data.status]++;
}
}
string res = "[";
foreach (var key in pieData.Keys)
{
res += $"{{name:'{key}',value:{pieData[key]}}},";
}
res += "]";
webview2.CoreWebView2?.ExecuteScriptAsync($"drawChart1({res})");
}
private void RefreshChart2(DateTime start, DateTime end)
{
dtp20.SelectedDateTime = start;
dtp21.SelectedDateTime = end;
List datas = SelectSerialDataByTime(start, end);
var groups =
datas.GroupBy(d => d.time.ToString("yyyy-MM-dd"));
Dictionary res = new Dictionary();
foreach (var group in groups)
{
Dictionary snStatusCache = new Dictionary();
double all = 0, oneTimePass = 0, passed = 0;
var groupDatas = group.ToList();
groupDatas = groupDatas.OrderBy(e => e.time).ToList();
foreach (var data in groupDatas)
{
if (snStatusCache.ContainsKey(data.sn))
{
if (snStatusCache[data.sn] != "Passed" && data.status == "Passed")
{
passed++;
}
else if (snStatusCache[data.sn] == "Passed" && data.status == "Passed")
{
if (oneTimePass > 0) oneTimePass--;
}
}
else
{
all++;
if (data.status == "Passed")
{
passed++;
oneTimePass++;
}
snStatusCache.Add(data.sn, data.status);
}
}
res.Add(group.Key, new PassRateData
{
time = group.Key,
onePassRate = oneTimePass / all * 100,
passRate = passed / all * 100,
});
}
string fields = "[", vals0 = "[", vals1 = "[";
foreach (var key in res.Keys)
{
fields += $"'{key}',";
vals0 += $"{res[key].onePassRate},";
vals1 += $"{res[key].passRate},";
}
fields += "]";
vals0 += "]";
vals1 += "]";
var names = $"['{this.lang("bd.onePassRate")}','{this.lang("bd.passRate")}']";
webview2.CoreWebView2?.ExecuteScriptAsync($"drawChart2({fields},{names},{vals0},{vals1})");
}
private void RefreshChart3(DateTime start, DateTime end)
{
dtp30.SelectedDateTime = start;
dtp31.SelectedDateTime = end;
List datas = SelectSerialDataByTime(start, end);
datas = datas.OrderByDescending(e => e.time).ToList();
Dictionary pieData = new Dictionary();
HashSet snCache = new HashSet();
foreach (var data in datas)
{
//if (snCache.Contains(data.sn)) continue;
if (data.status != "Failed") continue;
snCache.Add(data.sn);
if (pieData.ContainsKey(data.step))
{
pieData[data.step]++;
}
else
{
pieData[data.step] = 1;
}
}
string names = string.Empty, vals = string.Empty;
foreach (var key in pieData.Keys)
{
names += $"'{key}',";
vals += $"{pieData[key]},";
}
webview2.CoreWebView2?.ExecuteScriptAsync($"drawChart3([{names}],[{vals}])");
}
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox cb = sender as ComboBox;
string val = (e.AddedItems[0] as OptionData)?.Value?.ToString();
DateTime start, end;
if (val == "today")
{
DateUtil.GetDayTime(out start, out end);
}
else if (val == "week")
{
DateUtil.GetWeekTime(out start, out end);
}
else
{
DateUtil.GetMonthTime(out start, out end);
}
switch (cb.Name)
{
case "cb0":
RefreshChart0(start, end);
break;
case "cb1":
RefreshChart1(start, end);
break;
case "cb2":
RefreshChart2(start, end);
break;
case "cb3":
RefreshChart3(start, end);
break;
default:
break;
}
}
}
}