From b11dcd273df55204597eac1604c694e90d10c405 Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Fri, 11 Aug 2023 22:11:44 +0200 Subject: [PATCH] Fixes in Scales --- Elwig/App.xaml.cs | 5 +++-- Elwig/Helpers/Config.cs | 18 +++++++++++++---- Elwig/Helpers/Weighing/GassnerScale.cs | 1 + Elwig/Helpers/Weighing/IScale.cs | 5 +++++ Elwig/Helpers/Weighing/SchemberScale.cs | 1 + Elwig/Helpers/Weighing/SystecScale.cs | 27 +++++++++++++------------ Setup/Files/config.ini | 1 + 7 files changed, 39 insertions(+), 19 deletions(-) diff --git a/Elwig/App.xaml.cs b/Elwig/App.xaml.cs index ad4b03a..301449a 100644 --- a/Elwig/App.xaml.cs +++ b/Elwig/App.xaml.cs @@ -74,14 +74,15 @@ namespace Elwig { foreach (var s in Config.Scales) { try { var id = s[0]; - var type = s[1].ToLower(); + var type = s[1]?.ToLower(); var model = s[2]; var cnx = s[3]; var empty = s[4]; var filling = s[5]; int? limit = s[6] == null ? null : int.Parse(s[6]); + var log = s[7]; if (type == "systec") { - list.Add(new SystecScale(id, model, cnx, empty, filling, limit)); + list.Add(new SystecScale(id, model, cnx, empty, filling, limit, log)); } else { throw new ArgumentException($"Invalid scale type: \"{type}\""); } diff --git a/Elwig/Helpers/Config.cs b/Elwig/Helpers/Config.cs index 5d84f2d..5052076 100644 --- a/Elwig/Helpers/Config.cs +++ b/Elwig/Helpers/Config.cs @@ -12,8 +12,8 @@ namespace Elwig.Helpers { public string DatabaseFile = App.DataPath + "database.sqlite3"; public string? DatabaseLog = null; public string? Branch = null; - public IEnumerable Scales; - private readonly LinkedList ScaleList = new(); + public IList Scales; + private readonly List ScaleList = new(); public Config(string filename) { FileName = filename; @@ -54,9 +54,16 @@ namespace Elwig.Helpers { Scales = ScaleList; if (ini != null) { foreach (var s in ini.Sections.Where(s => s.SectionName.StartsWith("scale."))) { - ScaleList.AddLast(new string[] { + string? scaleLog = null; + if (s.Keys["log"] != null) { + scaleLog = s.Keys["log"]; + if (scaleLog.Length <= 1 || (scaleLog[1] != ':' && scaleLog[0] != '/' && scaleLog[0] != '\\')) { + scaleLog = App.DataPath + scaleLog; + } + } + ScaleList.Add(new string?[] { s.SectionName[6..], s.Keys["type"], s.Keys["model"], s.Keys["connection"], - s.Keys["empty"], s.Keys["filling"], s.Keys["limit"] + s.Keys["empty"], s.Keys["filling"], s.Keys["limit"], scaleLog }); } } @@ -64,6 +71,8 @@ namespace Elwig.Helpers { public void Write() { using var file = new StreamWriter(FileName, false, Encoding.UTF8); + file.Write($"\r\n[general]\r\n"); + if (Branch != null) file.Write($"branch = {Branch}\r\n"); file.Write($"\r\n[database]\r\nfile = {DatabaseFile}\r\n"); if (DatabaseLog != null) file.Write($"log = {DatabaseLog}\r\n"); foreach (var s in ScaleList) { @@ -71,6 +80,7 @@ namespace Elwig.Helpers { if (s[4] != null) file.Write($"empty = {s[4]}\r\n"); if (s[5] != null) file.Write($"filling = {s[5]}\r\n"); if (s[6] != null) file.Write($"limit = {s[6]}\r\n"); + if (s[7] != null) file.Write($"log = {s[7]}\r\n"); } } } diff --git a/Elwig/Helpers/Weighing/GassnerScale.cs b/Elwig/Helpers/Weighing/GassnerScale.cs index e8d6c35..95b1961 100644 --- a/Elwig/Helpers/Weighing/GassnerScale.cs +++ b/Elwig/Helpers/Weighing/GassnerScale.cs @@ -18,6 +18,7 @@ namespace Elwig.Helpers.Weighing { public bool IsReady { get; private set; } public bool HasFillingClearance { get; private set; } public int? WeightLimit { get; private set; } + public string? LogPath { get; private set; } public GassnerScale(string id, string model, string connection) { ScaleId = id; diff --git a/Elwig/Helpers/Weighing/IScale.cs b/Elwig/Helpers/Weighing/IScale.cs index ffb9d1e..a91f243 100644 --- a/Elwig/Helpers/Weighing/IScale.cs +++ b/Elwig/Helpers/Weighing/IScale.cs @@ -41,6 +41,11 @@ namespace Elwig.Helpers.Weighing { /// int? WeightLimit { get; } + /// + /// Where to log the requests and responses from the scale to + /// + string? LogPath { get; } + /// /// Get the current weight on the scale without performing a weighing process /// diff --git a/Elwig/Helpers/Weighing/SchemberScale.cs b/Elwig/Helpers/Weighing/SchemberScale.cs index b69fd2e..80237c2 100644 --- a/Elwig/Helpers/Weighing/SchemberScale.cs +++ b/Elwig/Helpers/Weighing/SchemberScale.cs @@ -12,6 +12,7 @@ namespace Elwig.Helpers.Weighing { public bool IsReady => throw new NotImplementedException(); public bool HasFillingClearance => throw new NotImplementedException(); public int? WeightLimit => throw new NotImplementedException(); + public string? LogPath => throw new NotImplementedException(); public void Dispose() { throw new NotImplementedException(); diff --git a/Elwig/Helpers/Weighing/SystecScale.cs b/Elwig/Helpers/Weighing/SystecScale.cs index c0d7dc4..1d20036 100644 --- a/Elwig/Helpers/Weighing/SystecScale.cs +++ b/Elwig/Helpers/Weighing/SystecScale.cs @@ -4,6 +4,7 @@ using System.IO.Ports; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; +using System.Windows.Shapes; namespace Elwig.Helpers.Weighing { public class SystecScale : IScale { @@ -25,12 +26,14 @@ namespace Elwig.Helpers.Weighing { public bool IsReady { get; private set; } public bool HasFillingClearance { get; private set; } public int? WeightLimit { get; private set; } + public string? LogPath { get; private set; } - public SystecScale(string id, string model, string connection, string? empty = null, string? fill = null, int? limit = null) { + public SystecScale(string id, string model, string connection, string? empty = null, string? fill = null, int? limit = null, string? log = null) { ScaleId = id; Model = model; IsReady = true; HasFillingClearance = false; + LogPath = log; if (connection.StartsWith("serial:")) { Serial = Utils.OpenSerialConnection(connection); @@ -61,29 +64,27 @@ namespace Elwig.Helpers.Weighing { } protected static Output? ConvertOutput(string? value) { - if (value == null) return null; - value = value.ToUpper(); - if (value == "RTS") { - return Output.RTS; - } else if (value == "DTR") { - return Output.DTR; - } else if (value == "OUT1") { - return Output.OUT1; - } else if (value == "OUT2") { - return Output.OUT2; - } - throw new ArgumentException("Invalid value for argument empty"); + return value switch { + null => null, + "RTS" => Output.RTS, + "DTR" => Output.DTR, + "OUT1" => Output.OUT1, + "OUT2" => Output.OUT2, + _ => throw new ArgumentException($"Invalid value for argument: '{value}'"), + }; } protected async Task SendCommand(string command) { byte[] bytes = Encoding.ASCII.GetBytes($"<{command}>"); await Stream.WriteAsync(bytes); + if (LogPath != null) await File.AppendAllTextAsync(LogPath, $"<{command}>"); } protected async Task ReceiveResponse() { string? line = null; using (var reader = new StreamReader(Stream, Encoding.ASCII, false, -1, true)) { line = await reader.ReadLineAsync(); + if (LogPath != null) await File.AppendAllTextAsync(LogPath, $"{line}\r\n"); } if (line == null || line.Length < 4 || !line.StartsWith("<") || !line.EndsWith(">")) { throw new IOException("Invalid response from scale"); diff --git a/Setup/Files/config.ini b/Setup/Files/config.ini index fbf48c6..5e99e26 100644 --- a/Setup/Files/config.ini +++ b/Setup/Files/config.ini @@ -15,6 +15,7 @@ file = database.sqlite3 ;connection = serial://COM1:9600,8,N,1 ;empty = RTS:1000 ;limit = 4000 +;log = waage.log ;[scale.B] ;type = Type