From 2ff3758596cc9a2540907036da2954bc9b538de7 Mon Sep 17 00:00:00 2001 From: iTob Date: Sun, 19 Jan 2025 10:03:41 +0100 Subject: [PATCH] wip --- src/CamBooth/CamBooth.App/App.xaml.cs | 8 +- src/CamBooth/CamBooth.App/CamBooth.App.csproj | 11 + .../Core/AppSettings/AppSettingsService.cs | 30 +++ .../Core/AppSettings/app.settings.dev.json | 10 + .../Core/AppSettings/app.settings.json | 9 + .../CamBooth.App/Core/Logging/Logger.cs | 18 +- .../DebugConsole/DebugConsolePage.xaml | 11 +- .../DebugConsole/DebugConsolePage.xaml.cs | 10 +- .../CamBooth.App/LiveView/LiveViewPage.xaml | 7 +- .../LiveView/LiveViewPage.xaml.cs | 239 ++++++++++++------ src/CamBooth/CamBooth.App/MainWindow.xaml | 14 +- src/CamBooth/CamBooth.App/MainWindow.xaml.cs | 34 ++- 12 files changed, 291 insertions(+), 110 deletions(-) create mode 100644 src/CamBooth/CamBooth.App/Core/AppSettings/AppSettingsService.cs create mode 100644 src/CamBooth/CamBooth.App/Core/AppSettings/app.settings.dev.json create mode 100644 src/CamBooth/CamBooth.App/Core/AppSettings/app.settings.json diff --git a/src/CamBooth/CamBooth.App/App.xaml.cs b/src/CamBooth/CamBooth.App/App.xaml.cs index 3b3fb3e..2e49d9d 100644 --- a/src/CamBooth/CamBooth.App/App.xaml.cs +++ b/src/CamBooth/CamBooth.App/App.xaml.cs @@ -1,8 +1,11 @@ -using System.Configuration; +using System.ComponentModel; +using System.Configuration; using System.Data; using System.Windows; +using CamBooth.App.Core.AppSettings; using CamBooth.App.Core.Logging; +using CamBooth.App.DebugConsole; using CamBooth.App.LiveView; using Microsoft.Extensions.DependencyInjection; @@ -29,11 +32,12 @@ public partial class App : Application mainWindow.Show(); } + private void ConfigureServices(IServiceCollection services) { // Register your services and view models here services.AddTransient(); - services.AddTransient(); services.AddSingleton(); + services.AddSingleton(); } } \ No newline at end of file diff --git a/src/CamBooth/CamBooth.App/CamBooth.App.csproj b/src/CamBooth/CamBooth.App/CamBooth.App.csproj index e18bb52..664bf85 100644 --- a/src/CamBooth/CamBooth.App/CamBooth.App.csproj +++ b/src/CamBooth/CamBooth.App/CamBooth.App.csproj @@ -21,6 +21,8 @@ + + @@ -29,4 +31,13 @@ + + + Always + + + Always + + + diff --git a/src/CamBooth/CamBooth.App/Core/AppSettings/AppSettingsService.cs b/src/CamBooth/CamBooth.App/Core/AppSettings/AppSettingsService.cs new file mode 100644 index 0000000..830384b --- /dev/null +++ b/src/CamBooth/CamBooth.App/Core/AppSettings/AppSettingsService.cs @@ -0,0 +1,30 @@ +namespace CamBooth.App.Core.AppSettings; + +using Microsoft.Extensions.Configuration; + +public class AppSettingsService +{ + IConfigurationRoot configuration; + + public AppSettingsService() + { + this.Initialize(); + } + + + private void Initialize() + { + var configFile = "Core/AppSettings/app.settings.json"; +#if DEBUG + configFile = "Core/AppSettings/app.settings.dev.json"; +#endif + configuration = new ConfigurationBuilder() + .SetBasePath(AppContext.BaseDirectory) + .AddJsonFile(configFile, optional: false, reloadOnChange: true) + .Build(); + } + + public string? AppName => configuration["AppSettings:AppName"]; + public string? PictureLocation => configuration["AppSettings:PictureLocation"]; + public string? ConnectionString => configuration.GetConnectionString("DefaultConnection"); +} \ No newline at end of file diff --git a/src/CamBooth/CamBooth.App/Core/AppSettings/app.settings.dev.json b/src/CamBooth/CamBooth.App/Core/AppSettings/app.settings.dev.json new file mode 100644 index 0000000..96c84d2 --- /dev/null +++ b/src/CamBooth/CamBooth.App/Core/AppSettings/app.settings.dev.json @@ -0,0 +1,10 @@ +{ + "AppSettings": { + "AppName": "Meine Anwendung", + "Version": "1.0.0", + "PictureLocation": "C:\\tmp\\cambooth" + }, + "ConnectionStrings": { + "DefaultConnection": "Server=myServer;Database=myDB;User Id=myUser;Password=myPassword;" + } +} diff --git a/src/CamBooth/CamBooth.App/Core/AppSettings/app.settings.json b/src/CamBooth/CamBooth.App/Core/AppSettings/app.settings.json new file mode 100644 index 0000000..0107a5e --- /dev/null +++ b/src/CamBooth/CamBooth.App/Core/AppSettings/app.settings.json @@ -0,0 +1,9 @@ +{ + "AppSettings": { + "AppName": "Meine Anwendung", + "Version": "1.0.0" + }, + "ConnectionStrings": { + "DefaultConnection": "Server=myServer;Database=myDB;User Id=myUser;Password=myPassword;" + } +} diff --git a/src/CamBooth/CamBooth.App/Core/Logging/Logger.cs b/src/CamBooth/CamBooth.App/Core/Logging/Logger.cs index c58d2ef..699b9a9 100644 --- a/src/CamBooth/CamBooth.App/Core/Logging/Logger.cs +++ b/src/CamBooth/CamBooth.App/Core/Logging/Logger.cs @@ -1,9 +1,21 @@ -namespace CamBooth.App.Core.Logging; +using System.Globalization; +using System.Windows; + +namespace CamBooth.App.Core.Logging; public class Logger { - public void LogInfo(string message) + public event LoggingEventHandler? InfoLog; + public delegate void LoggingEventHandler(string text); + + public void Info(string message) { - Console.WriteLine(message); + Application.Current.Dispatcher.Invoke(() => + { + message = DateTime.Now.ToString("dd.MM.yyyy HH:MM:ss", CultureInfo.InvariantCulture) + ": " + message; + InfoLog?.Invoke(message); + Console.WriteLine(message); + }); + } } \ No newline at end of file diff --git a/src/CamBooth/CamBooth.App/DebugConsole/DebugConsolePage.xaml b/src/CamBooth/CamBooth.App/DebugConsole/DebugConsolePage.xaml index 02cbb96..6197be4 100644 --- a/src/CamBooth/CamBooth.App/DebugConsole/DebugConsolePage.xaml +++ b/src/CamBooth/CamBooth.App/DebugConsole/DebugConsolePage.xaml @@ -5,8 +5,11 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:CamBooth.App.DebugConsole" mc:Ignorable="d" - Title="DebugConsolePage" Height="100" Width="800"> - + Title="DebugConsolePage" Width="1600" Height="170" HorizontalAlignment="Stretch"> + Debug Console - - + + + + + \ No newline at end of file diff --git a/src/CamBooth/CamBooth.App/DebugConsole/DebugConsolePage.xaml.cs b/src/CamBooth/CamBooth.App/DebugConsole/DebugConsolePage.xaml.cs index ff8db54..83c58db 100644 --- a/src/CamBooth/CamBooth.App/DebugConsole/DebugConsolePage.xaml.cs +++ b/src/CamBooth/CamBooth.App/DebugConsole/DebugConsolePage.xaml.cs @@ -1,11 +1,19 @@ using System.Windows.Controls; +using CamBooth.App.Core.Logging; + namespace CamBooth.App.DebugConsole; public partial class DebugConsolePage : Page { - public DebugConsolePage() + public DebugConsolePage(Logger logger) { + logger.InfoLog += Logger_OnInfoLog; InitializeComponent(); } + + private void Logger_OnInfoLog(string text) + { + this.tbDebugOutput.Text = this.tbDebugOutput.Text.Insert(0, text + "\n"); + } } \ No newline at end of file diff --git a/src/CamBooth/CamBooth.App/LiveView/LiveViewPage.xaml b/src/CamBooth/CamBooth.App/LiveView/LiveViewPage.xaml index 905a072..37f6e03 100644 --- a/src/CamBooth/CamBooth.App/LiveView/LiveViewPage.xaml +++ b/src/CamBooth/CamBooth.App/LiveView/LiveViewPage.xaml @@ -5,10 +5,9 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:CamBooth.App.LiveView" mc:Ignorable="d" - Title="LiveViewPage" Height="550" Width="780" + Title="LiveViewPage" Width="1600" Height="800" Background="PaleVioletRed"> - - LiveView CamBooth - + + diff --git a/src/CamBooth/CamBooth.App/LiveView/LiveViewPage.xaml.cs b/src/CamBooth/CamBooth.App/LiveView/LiveViewPage.xaml.cs index 9478369..9f1bf13 100644 --- a/src/CamBooth/CamBooth.App/LiveView/LiveViewPage.xaml.cs +++ b/src/CamBooth/CamBooth.App/LiveView/LiveViewPage.xaml.cs @@ -1,9 +1,14 @@ -using System.IO; +using System.ComponentModel; +using System.Configuration; +using System.IO; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Imaging; +using CamBooth.App.Core.AppSettings; +using CamBooth.App.Core.Logging; + using EOSDigital.API; using EOSDigital.SDK; @@ -11,6 +16,10 @@ namespace CamBooth.App.LiveView; public partial class LiveViewPage : Page { + private readonly Logger _logger; + + private readonly AppSettingsService _appSettings; + CanonAPI APIHandler; Camera MainCamera; @@ -36,19 +45,31 @@ public partial class LiveViewPage : Page object ErrLock = new object(); - public LiveViewPage() + public LiveViewPage(Logger logger, AppSettingsService appSettings) { + this._logger = logger; + this._appSettings = appSettings; try { InitializeComponent(); APIHandler = new CanonAPI(); - APIHandler.CameraAdded += APIHandler_CameraAdded; ErrorHandler.SevereErrorHappened += ErrorHandler_SevereErrorHappened; ErrorHandler.NonSevereErrorHappened += ErrorHandler_NonSevereErrorHappened; + //SavePathTextBox.Text = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "RemotePhoto"); SetImageAction = (BitmapImage img) => { bgbrush.ImageSource = img; }; RefreshCamera(); IsInit = true; + List cameraList = this.APIHandler.GetCameraList(); + if (cameraList.Any()) + { + this.OpenSession(); + this.StarLiveView(); + this.SetCameraSaveToComputer(); + } + + string cameraDeviceNames = string.Join(", ", cameraList.Select(cam => cam.DeviceName)); + logger.Info(cameraDeviceNames == string.Empty ? "No cameras / devices found" : cameraDeviceNames); } catch (DllNotFoundException) { @@ -59,87 +80,107 @@ public partial class LiveViewPage : Page ReportError(ex.Message, true); } } + + private void SetCameraSaveToComputer() + { + MainCamera.SetSetting(PropertyID.SaveTo, (int)SaveTo.Host); + MainCamera.SetCapacity(4096, int.MaxValue); + } + + public void CloseSession() + { + MainCamera.CloseSession(); + + // AvCoBox.Items.Clear(); + // TvCoBox.Items.Clear(); + // ISOCoBox.Items.Clear(); + // SettingsGroupBox.IsEnabled = false; + // LiveViewGroupBox.IsEnabled = false; + // SessionButton.Content = "Open Session"; + // SessionLabel.Content = "No open session"; + // StarLVButton.Content = "Start LV"; + } - private void CloseSession() - { - MainCamera.CloseSession(); - // AvCoBox.Items.Clear(); - // TvCoBox.Items.Clear(); - // ISOCoBox.Items.Clear(); - // SettingsGroupBox.IsEnabled = false; - // LiveViewGroupBox.IsEnabled = false; - // SessionButton.Content = "Open Session"; - // SessionLabel.Content = "No open session"; - // StarLVButton.Content = "Start LV"; - } + private void RefreshCamera() + { + // CameraListBox.Items.Clear(); + CamList = APIHandler.GetCameraList(); - private void RefreshCamera() - { - // CameraListBox.Items.Clear(); - CamList = APIHandler.GetCameraList(); - // foreach (Camera cam in CamList) CameraListBox.Items.Add(cam.DeviceName); - // if (MainCamera?.SessionOpen == true) CameraListBox.SelectedIndex = CamList.FindIndex(t => t.ID == MainCamera.ID); - // else if (CamList.Count > 0) CameraListBox.SelectedIndex = 0; - } + // foreach (Camera cam in CamList) CameraListBox.Items.Add(cam.DeviceName); + // if (MainCamera?.SessionOpen == true) CameraListBox.SelectedIndex = CamList.FindIndex(t => t.ID == MainCamera.ID); + // else if (CamList.Count > 0) CameraListBox.SelectedIndex = 0; + } - private void OpenSession() - { - MainCamera = CamList[0]; - MainCamera.OpenSession(); - MainCamera.LiveViewUpdated += MainCamera_LiveViewUpdated; - MainCamera.ProgressChanged += MainCamera_ProgressChanged; - MainCamera.StateChanged += MainCamera_StateChanged; - MainCamera.DownloadReady += MainCamera_DownloadReady; + private void OpenSession() + { + MainCamera = CamList[0]; + MainCamera.OpenSession(); + MainCamera.LiveViewUpdated += MainCamera_LiveViewUpdated; + //MainCamera.ProgressChanged += MainCamera_ProgressChanged; + MainCamera.StateChanged += MainCamera_StateChanged; + MainCamera.DownloadReady += MainCamera_DownloadReady; - - //SessionLabel.Content = MainCamera.DeviceName; - AvList = MainCamera.GetSettingsList(PropertyID.Av); - TvList = MainCamera.GetSettingsList(PropertyID.Tv); - ISOList = MainCamera.GetSettingsList(PropertyID.ISO); - // foreach (var Av in AvList) AvCoBox.Items.Add(Av.StringValue); - // foreach (var Tv in TvList) TvCoBox.Items.Add(Tv.StringValue); - // foreach (var ISO in ISOList) ISOCoBox.Items.Add(ISO.StringValue); - // AvCoBox.SelectedIndex = AvCoBox.Items.IndexOf(AvValues.GetValue(MainCamera.GetInt32Setting(PropertyID.Av)).StringValue); - // TvCoBox.SelectedIndex = TvCoBox.Items.IndexOf(TvValues.GetValue(MainCamera.GetInt32Setting(PropertyID.Tv)).StringValue); - // ISOCoBox.SelectedIndex = ISOCoBox.Items.IndexOf(ISOValues.GetValue(MainCamera.GetInt32Setting(PropertyID.ISO)).StringValue); - // SettingsGroupBox.IsEnabled = true; - // LiveViewGroupBox.IsEnabled = true; - - } + //SessionLabel.Content = MainCamera.DeviceName; + AvList = MainCamera.GetSettingsList(PropertyID.Av); + TvList = MainCamera.GetSettingsList(PropertyID.Tv); + ISOList = MainCamera.GetSettingsList(PropertyID.ISO); - private void ReportError(string message, bool lockdown) - { - int errc; - lock (ErrLock) { errc = ++ErrCount; } + // foreach (var Av in AvList) AvCoBox.Items.Add(Av.StringValue); + // foreach (var Tv in TvList) TvCoBox.Items.Add(Tv.StringValue); + // foreach (var ISO in ISOList) ISOCoBox.Items.Add(ISO.StringValue); + // AvCoBox.SelectedIndex = AvCoBox.Items.IndexOf(AvValues.GetValue(MainCamera.GetInt32Setting(PropertyID.Av)).StringValue); + // TvCoBox.SelectedIndex = TvCoBox.Items.IndexOf(TvValues.GetValue(MainCamera.GetInt32Setting(PropertyID.Tv)).StringValue); + // ISOCoBox.SelectedIndex = ISOCoBox.Items.IndexOf(ISOValues.GetValue(MainCamera.GetInt32Setting(PropertyID.ISO)).StringValue); + // SettingsGroupBox.IsEnabled = true; + // LiveViewGroupBox.IsEnabled = true; + } - if (lockdown) EnableUI(false); - if (errc < 4) MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); - else if (errc == 4) MessageBox.Show("Many errors happened!", "Error", MessageBoxButton.OK, MessageBoxImage.Error); + private void ReportError(string message, bool lockdown) + { + int errc; + lock (ErrLock) + { + errc = ++ErrCount; + } - lock (ErrLock) { ErrCount--; } - } + if (lockdown) + { + //this.EnableUI(false); + } - private void EnableUI(bool enable) - { - if (!Dispatcher.CheckAccess()) Dispatcher.Invoke((Action)delegate { EnableUI(enable); }); - else - { - // SettingsGroupBox.IsEnabled = enable; - // InitGroupBox.IsEnabled = enable; - // LiveViewGroupBox.IsEnabled = enable; - } - } + switch (errc) + { + case < 4: + this._logger.Info(message); + break; + case 4: + this._logger.Info("Many errors happened!"); + break; + } - #region API Events + lock (ErrLock) + { + ErrCount--; + } + } - private void APIHandler_CameraAdded(CanonAPI sender) + private void StarLiveView() { try { - + if (!MainCamera.IsLiveViewOn) + { + LVCanvas.Background = bgbrush; + MainCamera.StartLiveView(); + } + else + { + MainCamera.StopLiveView(); + LVCanvas.Background = Brushes.LightGray; + } } catch (Exception ex) { @@ -147,6 +188,19 @@ public partial class LiveViewPage : Page } } + #region API Events + + // private void APIHandler_CameraAdded(CanonAPI sender) + // { + // try + // { + // } + // catch (Exception ex) + // { + // ReportError(ex.Message, false); + // } + // } + private void MainCamera_StateChanged(Camera sender, StateEventID eventID, int parameter) { @@ -164,17 +218,17 @@ public partial class LiveViewPage : Page } - private void MainCamera_ProgressChanged(object sender, int progress) - { - try - { - //MainProgressBar.Dispatcher.Invoke((Action)delegate { MainProgressBar.Value = progress; }); - } - catch (Exception ex) - { - ReportError(ex.Message, false); - } - } + // private void MainCamera_ProgressChanged(object sender, int progress) + // { + // try + // { + // //MainProgressBar.Dispatcher.Invoke((Action)delegate { MainProgressBar.Value = progress; }); + // } + // catch (Exception ex) + // { + // ReportError(ex.Message, false); + // } + // } private void MainCamera_LiveViewUpdated(Camera sender, Stream img) @@ -202,11 +256,12 @@ public partial class LiveViewPage : Page private void MainCamera_DownloadReady(Camera sender, DownloadInfo Info) { + this._logger.Info("MainCamera_DownloadReady called"); try { - string dir = null; - //SavePathTextBox.Dispatcher.Invoke((Action)delegate { dir = SavePathTextBox.Text; }); + string dir = this._appSettings.PictureLocation; sender.DownloadFile(Info, dir); + this._logger.Info("Download complete: " + Path.Combine(dir, Info.FileName)); //MainProgressBar.Dispatcher.Invoke((Action)delegate { MainProgressBar.Value = 0; }); } catch (Exception ex) @@ -215,7 +270,6 @@ public partial class LiveViewPage : Page } } - private void ErrorHandler_NonSevereErrorHappened(object sender, ErrorCode ex) { ReportError($"SDK Error code: {ex} ({((int)ex).ToString("X")})", false); @@ -228,4 +282,25 @@ public partial class LiveViewPage : Page } #endregion + + + public void TakePhoto() + { + try + { + MainCamera.TakePhotoAsync(); + } + catch (Exception ex) + { + ReportError(ex.Message, false); + } + } + + public void Dispose() + { + this.CloseSession(); + IsInit = false; + MainCamera?.Dispose(); + APIHandler?.Dispose(); + } } \ No newline at end of file diff --git a/src/CamBooth/CamBooth.App/MainWindow.xaml b/src/CamBooth/CamBooth.App/MainWindow.xaml index 14b3e6c..30b3815 100644 --- a/src/CamBooth/CamBooth.App/MainWindow.xaml +++ b/src/CamBooth/CamBooth.App/MainWindow.xaml @@ -5,18 +5,19 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:CamBooth.App" mc:Ignorable="d" - Title="MainWindow" Height="650" Width="800"> + Title="MainWindow" Width="1600" Height="900" HorizontalAlignment="Stretch"> - - + + - +