using SCADA.CommonCtrl.WpfControl;
using SCADA.CommonLib;
using SCADA.CommonLib.Service;
using SCADA_DAQ.Plugin.Core.AutoUpdater;
using SCADA_DAQ.Plugin.CoreUI;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
namespace SCADA_DAQ
{
///
/// App.xaml 的交互逻辑
///
public partial class App : Application, IApp
{
private static SystemControl systemControl;
private string _sessionid;
///
/// 会话ID
///
public string SessionId => _sessionid ?? (_sessionid = Guid.NewGuid().ToString());
internal Task updateCheckTask;
///
/// 产品ID
///
public string ProductId { get; } = System.Windows.Forms.Application.ProductName;
///
/// 版本ID
///
public string UidVersion { get; set; }
///
/// 启动时间
///
public DateTime StartTime { get; }
///
///
///
public IServiceHost ServiceHost { get; set; }
private App()
{
StartTime = DateTime.Now;
Console.WriteLine($"App Start {DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}");
updateCheckTask = Task.Factory.StartNew(() =>
{
try
{
var _checkUpdate = true;
if (File.Exists("UserConfig.xml"))
{
var xmlFile = XDocument.Load("UserConfig.xml");
var node = xmlFile.Descendants("App.AutoUpdate").FirstOrDefault();
var val = node?.Value;
bool.TryParse(val, out _checkUpdate);
}
if (_checkUpdate)
{
var task = UpdateHelper.CheckUpdate();
task.Wait();
}
}
catch (Exception)
{
}
});
try
{
var appServer = Env.WebServerAddress.Value;
Trace.WriteLine($"Server:{appServer}");
var task = Task.Factory.StartNew(Env.ParameterInit);
var win = new MainWindow();
MainWindow = win;
win.FrmClosingWait += Win_FrmClosingWait;
systemControl = SystemControl.Create(this);
var iconPath = "Content/Img/favicon.ico";
if (File.Exists(iconPath))
{
MainWindow.Icon = new BitmapImage(new Uri(iconPath, UriKind.RelativeOrAbsolute));
}
MainWindow.Show();
task.Wait();
systemControl.SystemInit();
}
catch (Exception ex)
{
updateCheckTask?.Wait(10 * 1000);
Trace.TraceError(ex.ToString());
}
}
private void Win_FrmClosingWait(object sender, EventArgs e)
{
SystemControl.GetInstance().ApplicationDispose(0);
}
private void Application_Startup(object sender, StartupEventArgs e)
{
}
private void Application_SessionEnding(object sender, SessionEndingCancelEventArgs e)
{
systemControl.ApplicationSessionEnding(this, e);
}
private void Application_Exit(object sender, ExitEventArgs e)
{
systemControl.ApplicationExit(e.ApplicationExitCode);
}
///
///
///
///
public bool RestartHost()
{
return systemControl.RestartHost();
}
///
///
///
///
public void Restart(bool runAs = false)
{
MainWindow.Close();
Exit += (s, e) =>
{
new Thread(() =>
{
Thread.Sleep(1000);
string strAppFileName = Process.GetCurrentProcess().MainModule.FileName;
Process myNewProcess = new Process();
myNewProcess.StartInfo.FileName = strAppFileName;
myNewProcess.StartInfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
if (runAs == true)
{
myNewProcess.StartInfo.Verb = "runas";
}
myNewProcess.Start();
}).Start();
};
}
///
///
///
public void Restart()
{
Dispatcher.Invoke(new Action(() =>
{
Restart(false);
}));
}
///
///
///
public void ReLoadLicense()
{
systemControl.LoadLicense();
}
///
/// 显示toast
///
/// toast内容
///
/// toast 时间 短:2500 长:3500
public void ShowToast(object message, ControlStyle toastType = ControlStyle.Default, int showTime = 2000)
{
Dispatcher.BeginInvoke(new Action(() =>
{
var toastItem = new ToastItem() { Message = message, ControlStyle = toastType };
var showPannel = SCADA.CommonCtrl.WpfHelper.VisualHelper.GetChildObject(MainWindow, "PART_ToastLayer");
toastItem.Show(showPannel, showTime);
}));
}
///
/// 显示短toast
///
/// toast内容
///
public void ShowShortToast(object message, ControlStyle toastType)
{
ShowToast(message, toastType, 2000);
}
///
/// 显示长toast
///
/// toast内容
///
public void ShowLongToast(object message, ControlStyle toastType)
{
ShowToast(message, toastType, 3500);
}
}
}