Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evgeniy d/memory monitor #56

Merged
merged 5 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.png filter=lfs diff=lfs merge=lfs -text
*.jpg filter=lfs diff=lfs merge=lfs -text
*.svg filter=lfs diff=lfs merge=lfs -text
*.ttf filter=lfs diff=lfs merge=lfs -text
*.ico filter=lfs diff=lfs merge=lfs -text
198 changes: 102 additions & 96 deletions PrinterApp/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,119 +1,126 @@
using System;
using System;
using System.IO;
using Serilog;
using System.Reflection;
using System.Threading;
using System.Windows;

namespace PrinterApp
namespace PrinterApp;

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
private readonly AutoUpdater _autoUpdater = new();
private readonly ConfigFile _configFile = new();
private readonly MemoryMonitor _memoryMonitor = new();
private PrinterModel _printerModel;
private MainWindow _mainWindow;

private readonly string _assemblyVersion =
Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? string.Empty;

private App()
{
private readonly AutoUpdater _autoUpdater = new();
private readonly ConfigFile _configFile = new();
private PrinterModel _printerModel;
private MainWindow _mainWindow;
Thread.CurrentThread.CurrentCulture =
System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
_memoryMonitor.StartTimer();

private readonly string _assemblyVersion =
Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? string.Empty;
var fileName = GetType().Namespace!;
ConfigureLogger(fileName);
_configFile.LoadConfig(fileName);
if (!Directory.Exists(_configFile.TempSavePath))
Directory.CreateDirectory(_configFile.TempSavePath);

private App()
{
var fileName = GetType().Namespace!;
ConfigureLogger(fileName);
_configFile.LoadConfig(fileName);
if (!Directory.Exists(_configFile.TempSavePath))
Directory.CreateDirectory(_configFile.TempSavePath);

var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
var str = Environment.ProcessPath ?? string.Empty;
key?.SetValue(GetType().Namespace, _configFile.StartWithWindows ? str : string.Empty);

if (_configFile.AutoUpdate) _autoUpdater.StartTimer();

_printerModel = new PrinterModel(_configFile, _autoUpdater);
_printerModel.Reboot += RebootHandler;
_mainWindow = new MainWindow(_printerModel);
_mainWindow.Title = $"{_mainWindow.Title} {_assemblyVersion}";
_mainWindow.Closing += MainWindowClosing;
_mainWindow.Closed += MainWindowClosed;

Marketing.LoadProgram();
}
var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
var str = Environment.ProcessPath ?? string.Empty;
key?.SetValue(GetType().Namespace, _configFile.StartWithWindows ? str : string.Empty);

private void RebootHandler()
{
_printerModel.PrinterViewModel.DownloadNotInProgress = false;
_printerModel.PrinterViewModel.CodeTextBoxText = "REBOOT";
_mainWindow.Close();
}
if (_configFile.AutoUpdate) _autoUpdater.StartTimer();

private void MainWindowClosed(object? sender, EventArgs e)
{
if (_printerModel.PrinterViewModel.CodeTextBoxText != "REBOOT") return;
_printerModel = new PrinterModel(_configFile, _autoUpdater);
_printerModel.Reboot += RebootHandler;
_mainWindow = new MainWindow(_printerModel);
_mainWindow.Title = $"{_mainWindow.Title} {_assemblyVersion}";
_mainWindow.Closing += MainWindowClosing;
_mainWindow.Closed += MainWindowClosed;
_mainWindow.Show();
}
_printerModel = new PrinterModel(_configFile, _autoUpdater);
_printerModel.Reboot += RebootHandler;
_mainWindow = new MainWindow(_printerModel);
_mainWindow.Title = $"{_mainWindow.Title} {_assemblyVersion}";
_mainWindow.Closing += MainWindowClosing;
_mainWindow.Closed += MainWindowClosed;

private void MainWindowClosing(object? sender, System.ComponentModel.CancelEventArgs e)
Marketing.LoadProgram();
}

private void RebootHandler()
{
_printerModel.PrinterViewModel.DownloadNotInProgress = false;
_printerModel.PrinterViewModel.CodeTextBoxText = "REBOOT";
_mainWindow.Close();
}

private void MainWindowClosed(object? sender, EventArgs e)
{
if (_printerModel.PrinterViewModel.CodeTextBoxText != "REBOOT") return;
_printerModel = new PrinterModel(_configFile, _autoUpdater);
_printerModel.Reboot += RebootHandler;
_mainWindow = new MainWindow(_printerModel);
_mainWindow.Title = $"{_mainWindow.Title} {_assemblyVersion}";
_mainWindow.Closing += MainWindowClosing;
_mainWindow.Closed += MainWindowClosed;
_mainWindow.Show();
}

private void MainWindowClosing(object? sender, System.ComponentModel.CancelEventArgs e)
{
switch (_printerModel.PrinterViewModel.CodeTextBoxText)
{
switch (_printerModel.PrinterViewModel.CodeTextBoxText)
{
case "UPDATE":
_printerModel.PrinterViewModel.CodeTextBoxText = "";
_autoUpdater.ManualUpdate();
e.Cancel = true;
return;
case "REBOOT":
_printerModel.SocketsClose();
Marketing.ManualReboot();
return;
}

if (_printerModel.WrongExitCode())
{
Marketing.CloseWithoutAccessProgram();
Log.Information(
$"{GetType().Name} {MethodBase.GetCurrentMethod()?.Name}: Attempt to close without access");
case "UPDATE":
_printerModel.PrinterViewModel.CodeTextBoxText = "";
_autoUpdater.ManualUpdate();
e.Cancel = true;
return;
}

Marketing.ManualShutdown();
_printerModel.SocketsClose();
_autoUpdater.StopTimer();
Current.Shutdown();
case "REBOOT":
_printerModel.SocketsClose();
Marketing.ManualReboot();
return;
}

private void App_OnStartup(object sender, StartupEventArgs e)
if (_printerModel.WrongExitCode())
{
_mainWindow.Show();
Marketing.CloseWithoutAccessProgram();
Log.Information(
$"{GetType().Name} {MethodBase.GetCurrentMethod()?.Name}: Attempt to close without access");
e.Cancel = true;
return;
}

private void App_OnExit(object sender, ExitEventArgs e)
{
Log.Debug("App_OnExit");
Log.CloseAndFlush();
}
Marketing.ManualShutdown();
_printerModel.SocketsClose();
_autoUpdater.StopTimer();
_memoryMonitor.StopTimer();
Current.Shutdown();
}

private static void ConfigureLogger(string fileName)
{
private void App_OnStartup(object sender, StartupEventArgs e)
{
_mainWindow.Show();
}

private void App_OnExit(object sender, ExitEventArgs e)
{
Log.Debug("App_OnExit");
Log.CloseAndFlush();
}

private static void ConfigureLogger(string fileName)
{
#if DEBUG
var log = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Debug()
.WriteTo.File(
$"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}{Path.DirectorySeparatorChar}.printerAppLogs/{fileName}.txt",
rollingInterval: RollingInterval.Day)
.CreateLogger();
var log = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Debug()
.WriteTo.File(
$"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}{Path.DirectorySeparatorChar}.printerAppLogs/{fileName}.txt",
rollingInterval: RollingInterval.Day)
.CreateLogger();
#else
var log = new LoggerConfiguration()
.MinimumLevel.Information()
Expand All @@ -123,7 +130,6 @@ private static void ConfigureLogger(string fileName)
rollingInterval: RollingInterval.Day)
.CreateLogger();
#endif
Log.Logger = log;
}
Log.Logger = log;
}
}
19 changes: 12 additions & 7 deletions PrinterApp/AutoUpdater.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
Expand All @@ -14,16 +14,16 @@ namespace PrinterApp;
public class AutoUpdater
{
private readonly DispatcherTimer _dispatcherTimer;

private readonly HttpClient _httpClient = new()
{
BaseAddress = new Uri("https://github.com/profcomff/print-winapp/releases/latest/"),
};

private readonly HttpClient _httpClient;
private readonly Regex _regex = new(@"v\d+\..+");
mrerro marked this conversation as resolved.
Show resolved Hide resolved

public AutoUpdater()
{
_httpClient = new HttpClient
{
BaseAddress = new Uri("https://github.com/profcomff/print-winapp/releases/latest/"),
};

_dispatcherTimer = new DispatcherTimer
{
Interval = TimeSpan.FromHours(1)
Expand All @@ -35,6 +35,11 @@ public AutoUpdater()
CreateUpdateBat();
}

~AutoUpdater()
{
_httpClient.Dispose();
}

public void StartTimer()
{
if (_dispatcherTimer.IsEnabled) return;
Expand Down
Loading