using PamhagenSysCtrl.Dialogs; 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; private readonly DispatcherTimer AutoUpdateTimer = new() { Interval = TimeSpan.FromHours(1) }; 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()?.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); if (Config.UpdateUrl != null && Config.UpdateAuto) { AutoUpdateTimer.Tick += async (_, _) => { try { await CheckForUpdates(); } catch { } }; AutoUpdateTimer.Start(); await Dispatcher.BeginInvoke(async () => { await Task.Delay(1500); try { await CheckForUpdates(); } catch { } }); } } public static async Task CheckForUpdates(bool showResult = false) { if (Config.UpdateUrl == null) return; try { var latest = await UpdateService.GetLatestInstallerUrl(Config.UpdateUrl); if (latest.HasValue && new Version(latest.Value.Version) > Version) { var dialog = new UpdateDialog(latest.Value.Version, latest.Value.Url, latest.Value.Size) { Owner = Current.MainWindow, }; if (dialog.ShowDialog() == true) { Current.Shutdown(); } } else if (showResult) { MessageBox.Show($"Die Anlagensteuerung ist auf dem aktuellen Stand.\n(Version {Version})", "Nach Updates suchen", MessageBoxButton.OK, MessageBoxImage.Information); } } catch (OperationCanceledException) { if (showResult) { MessageBox.Show("Zeitüberschreitung beim Abrufen der Update-Informationen.", "Nach Updates suchen", MessageBoxButton.OK, MessageBoxImage.Error); } else { throw; } } catch (Exception exc) { if (showResult) { MessageBox.Show($"Die Update-Informationen konnten nicht abgerufen werden:\n\n{exc.Message}", "Nach Updates suchen", MessageBoxButton.OK, MessageBoxImage.Error); } else { throw; } } } protected async void Application_Exit(object sender, ExitEventArgs evt) { Plant?.Dispose(); } } }