Weighing: Add SchemberEventScale

This commit is contained in:
2024-02-21 18:33:36 +01:00
parent d289a5d4bf
commit 10b78dfb72
6 changed files with 130 additions and 36 deletions

View File

@ -53,6 +53,8 @@ namespace Elwig {
public static string? BranchFaxNr { get; private set; } public static string? BranchFaxNr { get; private set; }
public static string? BranchMobileNr { get; private set; } public static string? BranchMobileNr { get; private set; }
public static IList<IScale> Scales { get; private set; } public static IList<IScale> Scales { get; private set; }
public static IList<ICommandScale> CommandScales => Scales.Where(s => s is ICommandScale).Cast<ICommandScale>().ToList();
public static IList<IEventScale> EventScales => Scales.Where(s => s is IEventScale).Cast<IEventScale>().ToList();
public static ClientParameters Client { get; set; } public static ClientParameters Client { get; set; }
public static bool IsPrintingReady => Html.IsReady && Pdf.IsReady; public static bool IsPrintingReady => Html.IsReady && Pdf.IsReady;

View File

@ -4,5 +4,8 @@
/// </summary> /// </summary>
public interface IEventScale : IScale { public interface IEventScale : IScale {
public event EventHandler<WeighingEventArgs> WeighingEvent;
delegate void EventHandler<WeighingEventArgs>(object sender, WeighingEventArgs args);
} }
} }

View File

@ -24,7 +24,7 @@ namespace Elwig.Helpers.Weighing {
int? limit = config.Limit != null ? int.Parse(config.Limit) : null; int? limit = config.Limit != null ? int.Parse(config.Limit) : null;
if (config.Type == "SysTec-IT") { if (config.Type == "SysTec-IT") {
return new SysTecITScale(config.Id, config.Model!, config.Connection!, config.Empty, config.Filling, limit, config.Log); return new SysTecITScale(config.Id, config.Model!, config.Connection!, config.Empty, config.Filling, limit, config.Log);
} else if (config.Type == "Schember-evt") { } else if (config.Type == "Schember-Evt") {
return new SchemberEventScale(config.Id, config.Model!, config.Connection!, config.Empty, config.Filling, limit, config.Log); return new SchemberEventScale(config.Id, config.Model!, config.Connection!, config.Empty, config.Filling, limit, config.Log);
} else { } else {
throw new ArgumentException($"Invalid scale type: \"{config.Type}\""); throw new ArgumentException($"Invalid scale type: \"{config.Type}\"");
@ -39,7 +39,7 @@ namespace Elwig.Helpers.Weighing {
Tcp = Utils.OpenTcpConnection(cnx); Tcp = Utils.OpenTcpConnection(cnx);
Stream = Tcp.GetStream(); Stream = Tcp.GetStream();
} else { } else {
throw new ArgumentException("Unsupported scheme"); throw new ArgumentException($"Unsupported scheme: \"{cnx.Split(':')[0]}\"");
} }
LogPath = log; LogPath = log;

View File

@ -1,5 +1,12 @@
namespace Elwig.Helpers.Weighing { using System;
public class SchemberEventScale : Scale, IEventScale { using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
namespace Elwig.Helpers.Weighing {
public class SchemberEventScale : Scale, IEventScale, IDisposable {
public string Manufacturer => "Schember"; public string Manufacturer => "Schember";
public int InternalScaleNr => 1; public int InternalScaleNr => 1;
@ -8,15 +15,74 @@
public bool IsReady { get; private set; } public bool IsReady { get; private set; }
public bool HasFillingClearance { get; private set; } public bool HasFillingClearance { get; private set; }
public event IEventScale.EventHandler<WeighingEventArgs> WeighingEvent;
private bool IsRunning = true;
private readonly Thread BackgroundThread;
public SchemberEventScale(string id, string model, string cnx, string? empty = null, string? filling = null, int? limit = null, string? log = null) : public SchemberEventScale(string id, string model, string cnx, string? empty = null, string? filling = null, int? limit = null, string? log = null) :
base(cnx, empty, filling, limit, log) { base(cnx, empty, filling, limit, log) {
ScaleId = id; ScaleId = id;
Model = model; Model = model;
IsReady = true; IsReady = true;
HasFillingClearance = false; HasFillingClearance = false;
Stream.WriteTimeout = 250; BackgroundThread = new Thread(new ParameterizedThreadStart(BackgroundLoop));
Stream.ReadTimeout = 6000; BackgroundThread.Start();
} }
protected virtual void RaiseWeighingEvent(WeighingEventArgs evt) {
App.MainDispatcher.BeginInvoke(() => WeighingEvent?.Invoke(this, evt));
}
public new void Dispose() {
IsRunning = false;
BackgroundThread.Interrupt();
BackgroundThread.Join();
base.Dispose();
GC.SuppressFinalize(this);
}
protected async void BackgroundLoop(object? parameters) {
while (IsRunning) {
try {
var data = await Receive();
RaiseWeighingEvent(new WeighingEventArgs(data));
} catch (Exception ex) {
MessageBox.Show($"Beim Wiegen ist ein Fehler Aufgetreten:\n\n{ex.Message}", "Waagenfehler",
MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
protected async Task<WeighingResult> Receive() {
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 != 32 || line[0] != ' ' || line[9] != ' ' || line[15] != ' ' || line[20] != ' ') {
throw new IOException($"Invalid event from scale: '{line}'");
}
var date = line[ 1.. 9];
var time = line[10..15];
var identNr = line[16..20].Trim();
var netto = line[21..30].Trim();
var unit = line[30..32];
if (unit != "kg") {
throw new IOException($"Unsupported unit in weighing event: '{unit}'");
}
identNr = identNr.Length > 0 && identNr != "0" ? identNr : null;
var parsedDate = DateOnly.Parse(date);
return new() {
Weight = int.Parse(netto),
WeighingId = identNr,
FullWeighingId = identNr != null ? $"{parsedDate:yyyy-MM-dd}/{identNr}" : null,
Date = parsedDate,
Time = TimeOnly.Parse(time),
};
}
} }
} }

View File

@ -0,0 +1,12 @@
using System;
namespace Elwig.Helpers.Weighing {
public class WeighingEventArgs : EventArgs {
public WeighingResult Result { get; set; }
public WeighingEventArgs(WeighingResult result) {
Result = result;
}
}
}

View File

@ -76,17 +76,20 @@ namespace Elwig.Windows {
if (IsReceipt) { if (IsReceipt) {
Title = $"Übernahme - {App.BranchName} - Elwig"; Title = $"Übernahme - {App.BranchName} - Elwig";
TodayOnlyInput.IsChecked = true; TodayOnlyInput.IsChecked = true;
var n = App.Scales.Count; var n = App.CommandScales.Count;
if (n < 1) WeighingAButton.Visibility = Visibility.Hidden; if (n < 1) WeighingAButton.Visibility = Visibility.Hidden;
if (n < 2) WeighingBButton.Visibility = Visibility.Hidden; if (n < 2) WeighingBButton.Visibility = Visibility.Hidden;
if (n < 3) WeighingCButton.Visibility = Visibility.Hidden; if (n < 3) WeighingCButton.Visibility = Visibility.Hidden;
if (n < 4) WeighingDButton.Visibility = Visibility.Hidden; if (n < 4) WeighingDButton.Visibility = Visibility.Hidden;
if (n == 1) WeighingAButton.Content = "Wiegen"; if (n == 1) WeighingAButton.Content = "Wiegen";
if (n > 1) WeighingAButton.Content = $"Wiegen {App.Scales[0].ScaleId}"; if (n > 1) WeighingAButton.Content = $"Wiegen {App.CommandScales[0].ScaleId}";
if (n >= 2) WeighingBButton.Content = $"Wiegen {App.Scales[1].ScaleId}"; if (n >= 2) WeighingBButton.Content = $"Wiegen {App.CommandScales[1].ScaleId}";
if (n >= 3) WeighingCButton.Content = $"Wiegen {App.Scales[2].ScaleId}"; if (n >= 3) WeighingCButton.Content = $"Wiegen {App.CommandScales[2].ScaleId}";
if (n >= 4) WeighingDButton.Content = $"Wiegen {App.Scales[3].ScaleId}"; if (n >= 4) WeighingDButton.Content = $"Wiegen {App.CommandScales[3].ScaleId}";
WeighingManualButton.Margin = new Thickness(10, 10 + n * 32, 10, 10); WeighingManualButton.Margin = new Thickness(10, 10 + n * 32, 10, 10);
foreach (var s in App.EventScales) {
s.WeighingEvent += Scale_Weighing;
}
} else { } else {
WeighingManualButton.Visibility = Visibility.Hidden; WeighingManualButton.Visibility = Visibility.Hidden;
WeighingAButton.Visibility = Visibility.Hidden; WeighingAButton.Visibility = Visibility.Hidden;
@ -967,35 +970,43 @@ namespace Elwig.Windows {
FinishButton.IsEnabled = false; FinishButton.IsEnabled = false;
NewDeliveryPartButton.IsEnabled = false; NewDeliveryPartButton.IsEnabled = false;
CancelCreatingButton.IsEnabled = false; CancelCreatingButton.IsEnabled = false;
var s = App.CommandScales[index];
try { try {
var s = App.Scales[index]; var res = await s.Weigh();
if (s is not ICommandScale cs) return; OnWeighingResult(s, res);
var res = await cs.Weigh(); } catch (Exception ex) {
if ((res.Weight ?? 0) > 0 && res.FullWeighingId != null) { LastScaleError = ex.Message.Split(": ")[^1];
WeightInput.Text = $"{res.Weight:N0}"; OnWeighingResult(s, new() { Weight = 0 });
ScaleId = s.ScaleId; MessageBox.Show($"Beim Wiegen ist ein Fehler aufgetreten:\n\n{ex.Message}", "Waagenfehler",
WeighingId = res.FullWeighingId;
} else {
WeightInput.Text = "";
ScaleId = null;
WeighingId = null;
}
LastScaleError = null;
} catch (Exception e) {
LastScaleError = e.Message.Split(": ")[^1];
WeightInput.Text = "";
ScaleId = null;
WeighingId = null;
MessageBox.Show($"Beim Wiegen ist ein Fehler aufgetreten:\n\n{e.Message}", "Waagenfehler",
MessageBoxButton.OK, MessageBoxImage.Error); MessageBoxButton.OK, MessageBoxImage.Error);
} }
ManualWeighingReason = null; ManualWeighingReason = null;
ManualWeighingInput.IsChecked = false; ManualWeighingInput.IsChecked = false;
base.TextBox_TextChanged(WeightInput, null);
EnableWeighingButtons(); EnableWeighingButtons();
}
private void OnWeighingResult(IScale scale, WeighingResult res) {
if ((res.Weight ?? 0) > 0 && res.FullWeighingId != null) {
WeightInput.Text = $"{res.Weight:N0}";
ScaleId = scale.ScaleId;
WeighingId = res.FullWeighingId;
ManualWeighingReason = null;
ManualWeighingInput.IsChecked = false;
} else {
WeightInput.Text = "";
ScaleId = null;
WeighingId = null;
}
LastScaleError = null;
TextBox_TextChanged(WeightInput, null);
UpdateButtons(); UpdateButtons();
} }
private void Scale_Weighing(object sender, WeighingEventArgs evt) {
if (sender is not IScale scale) return;
OnWeighingResult(scale, evt.Result);
}
private async void SearchInput_TextChanged(object sender, RoutedEventArgs evt) { private async void SearchInput_TextChanged(object sender, RoutedEventArgs evt) {
TextFilter = SearchInput.Text.ToLower().Split(" ").ToList().FindAll(e => e.Length > 0); TextFilter = SearchInput.Text.ToLower().Split(" ").ToList().FindAll(e => e.Length > 0);
await RefreshDeliveryListQuery(true); await RefreshDeliveryListQuery(true);
@ -1511,11 +1522,11 @@ namespace Elwig.Windows {
private void EnableWeighingButtons() { private void EnableWeighingButtons() {
WeighingManualButton.IsEnabled = true; WeighingManualButton.IsEnabled = true;
var n = App.Scales.Count; var n = App.CommandScales.Count;
WeighingAButton.IsEnabled = n > 0 && App.Scales[0].IsReady; WeighingAButton.IsEnabled = n > 0 && App.CommandScales[0].IsReady;
WeighingBButton.IsEnabled = n > 1 && App.Scales[1].IsReady; WeighingBButton.IsEnabled = n > 1 && App.CommandScales[1].IsReady;
WeighingCButton.IsEnabled = n > 2 && App.Scales[2].IsReady; WeighingCButton.IsEnabled = n > 2 && App.CommandScales[2].IsReady;
WeighingDButton.IsEnabled = n > 3 && App.Scales[3].IsReady; WeighingDButton.IsEnabled = n > 3 && App.CommandScales[3].IsReady;
} }
private async Task UpdateLsNr() { private async Task UpdateLsNr() {