215 lines
8.1 KiB
C#
215 lines
8.1 KiB
C#
|
|
using Microsoft.Extensions.Configuration;
|
|
using Newtonsoft.Json;
|
|
using SafeBagDE70.Admin;
|
|
using SafeBagDE70.Model;
|
|
using SafeBagDE70.StateMachine;
|
|
using Serilog;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Data;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Forms;
|
|
using VirtualKeyboard.Wpf;
|
|
|
|
namespace SafeBagDE70
|
|
{
|
|
/// <summary>
|
|
/// Interaction logic for App.xaml
|
|
/// </summary>
|
|
public partial class App : System.Windows.Application
|
|
{
|
|
public static StateMachineConfig StateMachineConfig { get; private set; }
|
|
private static Mutex mutex;
|
|
public App()
|
|
{
|
|
}
|
|
protected override void OnStartup(StartupEventArgs e)
|
|
{
|
|
base.OnStartup(e);
|
|
|
|
CheckInstance();
|
|
LoadConfig();
|
|
LoadDualScreens();
|
|
}
|
|
private static void CheckInstance()
|
|
{
|
|
bool isNewInstance;
|
|
string mutexName = "9f5b55c9-c6eb-443a-8e28-027defa46fb5";
|
|
|
|
mutex = new Mutex(true, mutexName, out isNewInstance);
|
|
|
|
if (!isNewInstance)
|
|
{
|
|
// If an instance is already running, exit the application
|
|
System.Windows.MessageBox.Show("Ứng dụng đã khởi chạy");
|
|
System.Windows.Application.Current.Shutdown();
|
|
return;
|
|
}
|
|
}
|
|
private static void LoadConfig()
|
|
{
|
|
try
|
|
{
|
|
AppData.CurrentVersion = Assembly.GetExecutingAssembly().GetName().Version;
|
|
VKeyboard.Listen<System.Windows.Controls.TextBox>(e => e.Text);
|
|
var configuration = new ConfigurationBuilder()
|
|
.AddJsonFile("appsettings.json")
|
|
.Build();
|
|
Log.Logger = new LoggerConfiguration()
|
|
.ReadFrom.Configuration(configuration)
|
|
.WriteTo.Sink(new RealTimeLogSink())
|
|
.CreateLogger();
|
|
AppData.CDMPort = configuration["SerialPort:CDMPort"];
|
|
AppData.SensorPort = configuration["SerialPort:SensorPort"];
|
|
AppData.BaseURL = configuration["Device:BaseURL"];
|
|
AppData.TenantId = int.Parse("0" + configuration["Device:TenantId"]);
|
|
var maxNotes = int.Parse("0"+configuration["CDM:MaxNotes"]);
|
|
if(maxNotes > 0)
|
|
AppData.MaxNotes = maxNotes;
|
|
|
|
StateMachineConfig = new StateMachineConfig();
|
|
configuration.GetSection("StateMachine").Bind(StateMachineConfig);
|
|
Log.Information("START APP version: "+AppData.CurrentVersion);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
System.Windows.MessageBox.Show(ex.Message);
|
|
}
|
|
}
|
|
private static void LoadConfig2()
|
|
{
|
|
try
|
|
{
|
|
AppData.CurrentVersion = Assembly.GetExecutingAssembly().GetName().Version;
|
|
VKeyboard.Listen<System.Windows.Controls.TextBox>(e => e.Text);
|
|
var configuration = new ConfigurationBuilder()
|
|
.AddJsonFile("appsettings.json")
|
|
.Build();
|
|
InitLogger(configuration);
|
|
AppData.CDMPort = configuration["SerialPort:CDMPort"];
|
|
AppData.SensorPort = configuration["SerialPort:SensorPort"];
|
|
AppData.BaseURL = configuration["Device:BaseURL"];
|
|
AppData.TenantId = int.Parse("0" + configuration["Device:TenantId"]);
|
|
var maxNotes = int.Parse("0" + configuration["CDM:MaxNotes"]);
|
|
if (maxNotes > 0)
|
|
AppData.MaxNotes = maxNotes;
|
|
|
|
StateMachineConfig = new StateMachineConfig();
|
|
configuration.GetSection("StateMachine").Bind(StateMachineConfig);
|
|
Log.Information("START APP");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
System.Windows.MessageBox.Show(ex.Message);
|
|
}
|
|
}
|
|
private static void InitLogger(IConfigurationRoot configuration)
|
|
{
|
|
var logPath = configuration["Serilog:WriteTo:0:Args:path"];
|
|
var rollingIntervalSetting = configuration["Serilog:WriteTo:0:Args:rollingInterval"];
|
|
|
|
// Ensure the path is relative to the application's directory
|
|
var resolvedLogPath = Path.IsPathRooted(logPath)
|
|
? logPath
|
|
: Path.Combine(AppDomain.CurrentDomain.BaseDirectory, logPath);
|
|
|
|
// Parse the rollingInterval from string to enum
|
|
RollingInterval rollingInterval = Enum.TryParse(rollingIntervalSetting, out RollingInterval interval)
|
|
? interval
|
|
: RollingInterval.Day; // Default to 'Day' if parsing fails
|
|
|
|
// Ensure the directory exists
|
|
var logDirectory = Path.GetDirectoryName(resolvedLogPath);
|
|
if (!Directory.Exists(logDirectory))
|
|
{
|
|
Directory.CreateDirectory(logDirectory);
|
|
}
|
|
|
|
// Configure Serilog to write logs to the resolved log path
|
|
Log.Logger = new LoggerConfiguration()
|
|
.WriteTo.File(
|
|
path: resolvedLogPath,
|
|
rollingInterval: rollingInterval
|
|
)
|
|
.WriteTo.Sink(new RealTimeLogSink())
|
|
.CreateLogger();
|
|
|
|
try
|
|
{
|
|
Log.Information("Application Starting...");
|
|
// Your application code here
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Fatal(ex, "Application failed to start.");
|
|
}
|
|
finally
|
|
{
|
|
Log.CloseAndFlush();
|
|
}
|
|
}
|
|
private void LoadDualScreens()
|
|
{
|
|
try
|
|
{
|
|
// Create instances of the windows you want to open
|
|
var mainWindow = new MainWindow();
|
|
|
|
// Show both windows
|
|
mainWindow.Show();
|
|
|
|
// Get all available screens
|
|
var screens = Screen.AllScreens;
|
|
|
|
// Open the Main Window on the primary screen
|
|
var primaryScreen = screens.FirstOrDefault(s => s.Primary) ?? screens.First();
|
|
PositionWindowOnScreen(mainWindow, primaryScreen);
|
|
mainWindow.Show();
|
|
|
|
// Open the Secondary Window on the secondary screen, if available
|
|
if (screens.Length > 1)
|
|
{
|
|
var adminWindow = new AdminWindow(); // Replace this with your actual window class
|
|
adminWindow.Show();
|
|
var secondaryScreen = screens.First(s => !s.Primary);
|
|
PositionWindowOnScreen(adminWindow, secondaryScreen);
|
|
adminWindow.Show();
|
|
}
|
|
}catch (Exception ex)
|
|
{
|
|
Log.Fatal(ex,"START APP");
|
|
}
|
|
|
|
}
|
|
|
|
private void PositionWindowOnScreen(Window window, Screen screen)
|
|
{
|
|
// Convert screen bounds to WPF units (96 DPI)
|
|
var screenLeft = screen.WorkingArea.Left / (double)Screen.PrimaryScreen.WorkingArea.Width * SystemParameters.PrimaryScreenWidth;
|
|
var screenTop = screen.WorkingArea.Top / (double)Screen.PrimaryScreen.WorkingArea.Height * SystemParameters.PrimaryScreenHeight;
|
|
|
|
// Set window position and state
|
|
window.WindowStartupLocation = WindowStartupLocation.Manual;
|
|
window.Left = screenLeft;
|
|
window.Top = screenTop;
|
|
window.Width = screen.WorkingArea.Width;
|
|
window.Height = screen.WorkingArea.Height;
|
|
}
|
|
protected override void OnExit(ExitEventArgs e)
|
|
{
|
|
// Release the mutex when the application is closed
|
|
AppData.UpdateDeviceStatusToFile();
|
|
mutex?.ReleaseMutex();
|
|
base.OnExit(e);
|
|
}
|
|
}
|
|
}
|