Files
pamhagen-sysctrl/PamhagenSysCtrl/App.xaml.cs
T

55 lines
2.3 KiB
C#

using PamhagenSysCtrl.Helpers;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Threading;
namespace PamhagenSysCtrl {
public partial class App : Application {
public static PamhagenPlant? Plant;
public static Dispatcher? MainDispatcher;
public static readonly string DataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Anlagensteuerung Pamhagen");
public static readonly string ConfigPath = Path.Combine(DataPath, "config.ini");
public static readonly string InstallPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "Anlagensteuerung Pamhagen");
public static readonly string TempPath = Path.Combine(Path.GetTempPath(), "Anlagensteuerung Pamhagen");
public static Config Config { get; private set; } = new(ConfigPath);
public static Version Version { get; private set; } = new();
public App() :
base() {
Directory.CreateDirectory(TempPath);
Directory.CreateDirectory(DataPath);
MainDispatcher = Dispatcher;
var args = Environment.GetCommandLineArgs();
if (args.Length >= 2) {
Config = new(Path.GetFullPath(args[1]));
}
}
protected override async void OnStartup(StartupEventArgs evt) {
Version = new Version(typeof(App).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion.Split('+')[0] ?? "0.0.0");
try {
Plant = new(Config.PlcPort);
await Plant.StartThread();
} catch (Exception exc) {
Plant?.Dispose();
Plant = null;
var str = $"Beim Aufbauen einer Verbindung zur SPS ist ein Fehler aufgetreten:\n\n{exc.GetType().Name}: {exc.Message}";
if (exc.InnerException != null) str += $"\n\n{exc.InnerException.GetType().Name}: {exc.InnerException.Message}";
MessageBox.Show(str, "SPS-Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
}
base.OnStartup(evt);
}
protected async void Application_Exit(object sender, ExitEventArgs evt) {
Plant?.Dispose();
}
}
}