From 9ecad6aa79da7d53947e36af7d9280ec765c8866 Mon Sep 17 00:00:00 2001
From: Lorenz Stechauner <lorenz.stechauner@necronda.net>
Date: Sat, 10 Feb 2024 18:43:45 +0100
Subject: [PATCH] Weighing: Add ICommandScale and IEventScale

---
 Elwig/Helpers/Weighing/ICommandScale.cs   | 35 +++++++++++++++++++++++
 Elwig/Helpers/Weighing/IEventScale.cs     |  8 ++++++
 Elwig/Helpers/Weighing/IScale.cs          | 30 +------------------
 Elwig/Helpers/Weighing/InvalidScale.cs    | 29 ++-----------------
 Elwig/Helpers/Weighing/SchemberScale.cs   | 21 +-------------
 Elwig/Helpers/Weighing/SystecScale.cs     |  2 +-
 Elwig/Windows/DeliveryAdminWindow.xaml.cs |  8 ++++--
 Elwig/Windows/TestWindow.xaml.cs          |  7 +++--
 8 files changed, 58 insertions(+), 82 deletions(-)
 create mode 100644 Elwig/Helpers/Weighing/ICommandScale.cs
 create mode 100644 Elwig/Helpers/Weighing/IEventScale.cs

diff --git a/Elwig/Helpers/Weighing/ICommandScale.cs b/Elwig/Helpers/Weighing/ICommandScale.cs
new file mode 100644
index 0000000..789997a
--- /dev/null
+++ b/Elwig/Helpers/Weighing/ICommandScale.cs
@@ -0,0 +1,35 @@
+using System.Threading.Tasks;
+
+namespace Elwig.Helpers.Weighing {
+    /// <summary>
+    /// Interface for controlling a a scale which responds to commands sent to it
+    /// </summary>
+    public interface ICommandScale : IScale {
+        /// <summary>
+        /// Get the current weight on the scale without performing a weighing process
+        /// </summary>
+        /// <returns>Result of the weighing process (probably without a weighing id)</returns>
+        Task<WeighingResult> GetCurrentWeight();
+
+        /// <summary>
+        /// Perform a weighing process
+        /// </summary>
+        /// <returns>Result of the weighing process (including a weighing id)</returns>
+        Task<WeighingResult> Weigh();
+
+        /// <summary>
+        /// Empty the scale container or grant clearance to do so
+        /// </summary>
+        Task Empty();
+
+        /// <summary>
+        /// Grant clearance to fill the scale container
+        /// </summary>
+        Task GrantFillingClearance();
+
+        /// <summary>
+        /// Revoke clearance to fill the scale container
+        /// </summary>
+        Task RevokeFillingClearance();
+    }
+}
diff --git a/Elwig/Helpers/Weighing/IEventScale.cs b/Elwig/Helpers/Weighing/IEventScale.cs
new file mode 100644
index 0000000..5c7da71
--- /dev/null
+++ b/Elwig/Helpers/Weighing/IEventScale.cs
@@ -0,0 +1,8 @@
+namespace Elwig.Helpers.Weighing {
+    /// <summary>
+    /// Interface for controlling a a scale which automatically sends weighing updates
+    /// </summary>
+    public interface IEventScale : IScale {
+
+    }
+}
diff --git a/Elwig/Helpers/Weighing/IScale.cs b/Elwig/Helpers/Weighing/IScale.cs
index a91f243..9e5efda 100644
--- a/Elwig/Helpers/Weighing/IScale.cs
+++ b/Elwig/Helpers/Weighing/IScale.cs
@@ -1,9 +1,8 @@
 using System;
-using System.Threading.Tasks;
 
 namespace Elwig.Helpers.Weighing {
     /// <summary>
-    /// Interface for controlling a industrial scale
+    /// Interface for controlling a industrial scale (industrial terminal, "IT")
     /// </summary>
     public interface IScale : IDisposable {
         /// <summary>
@@ -45,32 +44,5 @@ namespace Elwig.Helpers.Weighing {
         /// Where to log the requests and responses from the scale to
         /// </summary>
         string? LogPath { get; }
-
-        /// <summary>
-        /// Get the current weight on the scale without performing a weighing process
-        /// </summary>
-        /// <returns>Result of the weighing process (probably without a weighing id)</returns>
-        Task<WeighingResult> GetCurrentWeight();
-
-        /// <summary>
-        /// Perform a weighing process
-        /// </summary>
-        /// <returns>Result of the weighing process (including a weighing id)</returns>
-        Task<WeighingResult> Weigh();
-
-        /// <summary>
-        /// Empty the scale container or grant clearance to do so
-        /// </summary>
-        Task Empty();
-
-        /// <summary>
-        /// Grant clearance to fill the scale container
-        /// </summary>
-        Task GrantFillingClearance();
-
-        /// <summary>
-        /// Revoke clearance to fill the scale container
-        /// </summary>
-        Task RevokeFillingClearance();
     }
 }
diff --git a/Elwig/Helpers/Weighing/InvalidScale.cs b/Elwig/Helpers/Weighing/InvalidScale.cs
index eea7459..7db66b7 100644
--- a/Elwig/Helpers/Weighing/InvalidScale.cs
+++ b/Elwig/Helpers/Weighing/InvalidScale.cs
@@ -1,44 +1,19 @@
 using System;
-using System.Threading.Tasks;
 
 namespace Elwig.Helpers.Weighing {
-    public class InvalidScale : IScale {
+    public class InvalidScale(string id) : IScale {
 
         public string Manufacturer => "NONE";
         public string Model => "NONE";
-        public string ScaleId { get; private set; }
+        public string ScaleId => id;
         public int InternalScaleNr => 0;
         public bool IsReady => false;
         public bool HasFillingClearance => false;
         public int? WeightLimit => null;
         public string? LogPath => null;
 
-        public InvalidScale(string id) {
-            ScaleId = id;
-        }
-
         public void Dispose() {
             GC.SuppressFinalize(this);
         }
-
-        public Task<WeighingResult> Weigh() {
-            throw new NotImplementedException();
-        }
-
-        public Task<WeighingResult> GetCurrentWeight() {
-            throw new NotImplementedException();
-        }
-
-        public Task Empty() {
-            throw new NotImplementedException();
-        }
-
-        public Task GrantFillingClearance() {
-            throw new NotImplementedException();
-        }
-
-        public Task RevokeFillingClearance() {
-            throw new NotImplementedException();
-        }
     }
 }
diff --git a/Elwig/Helpers/Weighing/SchemberScale.cs b/Elwig/Helpers/Weighing/SchemberScale.cs
index 80237c2..52b1ce1 100644
--- a/Elwig/Helpers/Weighing/SchemberScale.cs
+++ b/Elwig/Helpers/Weighing/SchemberScale.cs
@@ -1,9 +1,7 @@
 using System;
-using System.Threading.Tasks;
 
 namespace Elwig.Helpers.Weighing {
-    // TODO implement SchemberScale
-    public class SchemberScale : IScale {
+    public class SchemberScale : IEventScale {
 
         public string Manufacturer => "Schember";
         public string Model => throw new NotImplementedException();
@@ -18,24 +16,7 @@ namespace Elwig.Helpers.Weighing {
             throw new NotImplementedException();
         }
 
-        public Task Empty() {
-            throw new NotImplementedException();
-        }
 
-        public Task<WeighingResult> GetCurrentWeight() {
-            throw new NotImplementedException();
-        }
 
-        public Task GrantFillingClearance() {
-            throw new NotImplementedException();
-        }
-
-        public Task RevokeFillingClearance() {
-            throw new NotImplementedException();
-        }
-
-        public Task<WeighingResult> Weigh() {
-            throw new NotImplementedException();
-        }
     }
 }
diff --git a/Elwig/Helpers/Weighing/SystecScale.cs b/Elwig/Helpers/Weighing/SystecScale.cs
index bf022a8..22dc411 100644
--- a/Elwig/Helpers/Weighing/SystecScale.cs
+++ b/Elwig/Helpers/Weighing/SystecScale.cs
@@ -6,7 +6,7 @@ using System.Text;
 using System.Threading.Tasks;
 
 namespace Elwig.Helpers.Weighing {
-    public class SystecScale : IScale {
+    public class SystecScale : ICommandScale {
 
         protected enum Output { RTS, DTR, OUT1, OUT2 };
 
diff --git a/Elwig/Windows/DeliveryAdminWindow.xaml.cs b/Elwig/Windows/DeliveryAdminWindow.xaml.cs
index 3351827..09d1815 100644
--- a/Elwig/Windows/DeliveryAdminWindow.xaml.cs
+++ b/Elwig/Windows/DeliveryAdminWindow.xaml.cs
@@ -1,6 +1,7 @@
 using Elwig.Documents;
 using Elwig.Helpers;
 using Elwig.Helpers.Export;
+using Elwig.Helpers.Weighing;
 using Elwig.Models.Entities;
 using LinqKit;
 using Microsoft.EntityFrameworkCore;
@@ -968,7 +969,8 @@ namespace Elwig.Windows {
             CancelCreatingButton.IsEnabled = false;
             try {
                 var s = App.Scales[index];
-                var res = await s.Weigh();
+                if (s is not ICommandScale cs) return;
+                var res = await cs.Weigh();
                 if ((res.Weight ?? 0) > 0 && res.FullWeighingId != null) {
                     WeightInput.Text = $"{res.Weight:N0}";
                     ScaleId = s.ScaleId;
@@ -1080,8 +1082,8 @@ namespace Elwig.Windows {
 
         private void EmptyScale() {
             var scale = App.Scales.Where(s => s.ScaleId == ScaleId).FirstOrDefault();
-            if (scale == null) return;
-            scale.Empty();
+            if (scale is not ICommandScale cs) return;
+            cs.Empty();
         }
 
         private async void NewDeliveryPartButton_Click(object sender, RoutedEventArgs evt) {
diff --git a/Elwig/Windows/TestWindow.xaml.cs b/Elwig/Windows/TestWindow.xaml.cs
index c1d34b4..ffc7e0d 100644
--- a/Elwig/Windows/TestWindow.xaml.cs
+++ b/Elwig/Windows/TestWindow.xaml.cs
@@ -2,6 +2,7 @@ using Elwig.Documents;
 using Elwig.Helpers;
 using Elwig.Helpers.Billing;
 using Elwig.Helpers.Export;
+using Elwig.Helpers.Weighing;
 using Elwig.Models.Dtos;
 using Microsoft.EntityFrameworkCore;
 using System;
@@ -27,7 +28,8 @@ namespace Elwig.Windows {
 
         private async void WeighingButton1_Click(object sender, RoutedEventArgs evt) {
             try {
-                var res = await App.Scales[0].GetCurrentWeight();
+                if (App.Scales[0] is not ICommandScale cs) return;
+                var res = await cs.GetCurrentWeight();
                 Output.Text = res.ToString();
             } catch (Exception e) {
                 MessageBox.Show($"Beim Wiegen ist ein Fehler aufgetreten:\n\n{e.Message}", "Waagenfehler",
@@ -37,7 +39,8 @@ namespace Elwig.Windows {
 
         private async void WeighingButton2_Click(object sender, RoutedEventArgs evt) {
             try {
-                var res = await App.Scales[0].Weigh();
+                if (App.Scales[0] is not ICommandScale cs) return;
+                var res = await cs.Weigh();
                 Output.Text = res.ToString();
             } catch (Exception e) {
                 MessageBox.Show($"Beim Wiegen ist ein Fehler aufgetreten:\n\n{e.Message}", "Waagenfehler",