Weighing: Differentiate between scale and terminal
This commit is contained in:
@@ -9,9 +9,8 @@ namespace Elwig.Helpers.Weighing {
|
||||
public class AveryEventScale : Scale, IEventScale, IDisposable {
|
||||
|
||||
public string Manufacturer => "Avery";
|
||||
public int InternalScaleNr => 1;
|
||||
public string Model { get; private set; }
|
||||
public string ScaleId { get; private set; }
|
||||
public string ScaleTerminalId { get; private set; }
|
||||
public bool IsReady { get; private set; }
|
||||
public bool HasFillingClearance { get; private set; }
|
||||
|
||||
@@ -19,13 +18,11 @@ namespace Elwig.Helpers.Weighing {
|
||||
|
||||
private bool IsRunning = true;
|
||||
private readonly Thread BackgroundThread;
|
||||
private readonly string Connection;
|
||||
|
||||
public AveryEventScale(string id, string model, string cnx, string? log = null, bool required = true) :
|
||||
base(cnx, null, null, null, log, true, !required) {
|
||||
ScaleId = id;
|
||||
ScaleTerminalId = id;
|
||||
Model = model;
|
||||
Connection = cnx;
|
||||
IsReady = true;
|
||||
HasFillingClearance = false;
|
||||
Stream.WriteTimeout = -1;
|
||||
|
||||
+17
-17
@@ -5,18 +5,17 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Elwig.Helpers.Weighing {
|
||||
public class GassnerScale : Scale, ICommandScale {
|
||||
public class GassnerEnqScale : Scale, ICommandScale {
|
||||
|
||||
public string Manufacturer => "Gassner";
|
||||
public int InternalScaleNr => 1;
|
||||
public string Model { get; private set; }
|
||||
public string ScaleId { get; private set; }
|
||||
public string ScaleTerminalId { get; private set; }
|
||||
public bool IsReady { get; private set; }
|
||||
public bool HasFillingClearance { get; private set; }
|
||||
|
||||
public GassnerScale(string id, string model, string cnx, string? empty = null, string? filling = null, int? limit = null, string? log = null) :
|
||||
public GassnerEnqScale(string id, string model, string cnx, string? empty = null, string? filling = null, int? limit = null, string? log = null) :
|
||||
base(cnx, empty, filling, limit, log) {
|
||||
ScaleId = id;
|
||||
ScaleTerminalId = id;
|
||||
Model = model;
|
||||
IsReady = true;
|
||||
HasFillingClearance = false;
|
||||
@@ -24,10 +23,9 @@ namespace Elwig.Helpers.Weighing {
|
||||
Stream.ReadTimeout = 11000;
|
||||
}
|
||||
|
||||
protected Task SendCommand(char command) => SendCommand(Convert.ToByte(command));
|
||||
protected Task SendCommand(string command) => SendCommand(Encoding.ASCII.GetBytes(command));
|
||||
|
||||
protected async Task SendCommand(byte command) {
|
||||
byte[] cmd = [command];
|
||||
protected async Task SendCommand(byte[] cmd) {
|
||||
await Stream.WriteAsync(cmd);
|
||||
if (LogPath != null) await File.AppendAllTextAsync(LogPath, Encoding.ASCII.GetString(cmd));
|
||||
}
|
||||
@@ -53,15 +51,15 @@ namespace Elwig.Helpers.Weighing {
|
||||
return line[1..^1];
|
||||
}
|
||||
|
||||
protected async Task<WeighingResult> Weigh(bool incIdentNr, bool retry = true) {
|
||||
protected async Task<WeighingResult> Weigh(int? internalScaleNr, bool incIdentNr, bool retry = true) {
|
||||
string record;
|
||||
try {
|
||||
await SendCommand(incIdentNr ? '\x05' : '?');
|
||||
await SendCommand(incIdentNr ? "\x05" : "?");
|
||||
record = await ReceiveResponse();
|
||||
} catch (IOException) {
|
||||
if (!retry || Tcp == null) throw;
|
||||
ReconnectTcp();
|
||||
return await Weigh(incIdentNr, false);
|
||||
return await Weigh(internalScaleNr, incIdentNr, false);
|
||||
}
|
||||
if (record.Length != 45)
|
||||
throw new FormatException("Invalid response from scale: Received record has invalid size");
|
||||
@@ -80,6 +78,7 @@ namespace Elwig.Helpers.Weighing {
|
||||
GrossWeight = int.Parse(brutto),
|
||||
TareWeight = int.Parse(tara),
|
||||
NetWeight = int.Parse(netto),
|
||||
ScaleId = scaleNr,
|
||||
WeighingId = identNr,
|
||||
FullWeighingId = identNr,
|
||||
Date = DateOnly.TryParseExact(date, "yyyyMMdd", out var d) ? d : null,
|
||||
@@ -87,12 +86,12 @@ namespace Elwig.Helpers.Weighing {
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<WeighingResult> Weigh() {
|
||||
return await Weigh(true);
|
||||
public async Task<WeighingResult> Weigh(int? scaleNr = null) {
|
||||
return await Weigh(scaleNr, true);
|
||||
}
|
||||
|
||||
public async Task<WeighingResult> GetCurrentWeight() {
|
||||
return await Weigh(false);
|
||||
public async Task<WeighingResult> GetCurrentWeight(int? scaleNr = null) {
|
||||
return await Weigh(scaleNr, false);
|
||||
}
|
||||
|
||||
public async Task Empty() {
|
||||
@@ -126,8 +125,9 @@ namespace Elwig.Helpers.Weighing {
|
||||
await SetFillingClearance(false);
|
||||
}
|
||||
|
||||
public Task SetDateAndTime(DateTime dateTime) {
|
||||
throw new NotImplementedException("Für Waagen vom Typ 'Gassner' ist diese Funktion noch nicht implementiert");
|
||||
public async Task SetDateAndTime(DateTime dateTime) {
|
||||
await SendCommand($"C{dateTime:yyyyMMddHHmmss}\r");
|
||||
await ReceiveResponse();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,13 +10,13 @@ namespace Elwig.Helpers.Weighing {
|
||||
/// 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();
|
||||
Task<WeighingResult> GetCurrentWeight(int? scaleNr = null);
|
||||
|
||||
/// <summary>
|
||||
/// Perform a weighing process
|
||||
/// </summary>
|
||||
/// <returns>Result of the weighing process (including a weighing id)</returns>
|
||||
Task<WeighingResult> Weigh();
|
||||
Task<WeighingResult> Weigh(int? scaleNr = null);
|
||||
|
||||
/// <summary>
|
||||
/// Empty the scale container or grant clearance to do so
|
||||
|
||||
@@ -18,12 +18,7 @@ namespace Elwig.Helpers.Weighing {
|
||||
/// <summary>
|
||||
/// Unique identificator of the scale
|
||||
/// </summary>
|
||||
string ScaleId { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Internal identifying number of the scale in its system
|
||||
/// </summary>
|
||||
int InternalScaleNr { get; }
|
||||
string ScaleTerminalId { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Indicates if the scale is currently processing a request or not
|
||||
|
||||
@@ -5,8 +5,7 @@ namespace Elwig.Helpers.Weighing {
|
||||
|
||||
public string Manufacturer => "NONE";
|
||||
public string Model => "NONE";
|
||||
public string ScaleId => id;
|
||||
public int InternalScaleNr => 0;
|
||||
public string ScaleTerminalId => id;
|
||||
public bool IsReady => false;
|
||||
public bool HasFillingClearance => false;
|
||||
public int? WeightLimit => null;
|
||||
|
||||
@@ -28,10 +28,10 @@ namespace Elwig.Helpers.Weighing {
|
||||
int? limit = config.Limit != null ? int.Parse(config.Limit) : null;
|
||||
if (config.Type == "SysTec-IT") {
|
||||
return new SysTecITScale(config.Id, config.Model!, config.Connection!, config.Empty, config.Filling, limit, config.Log);
|
||||
} else if (config.Type == "Gassner-Enq" || config.Type == "Gassner") {
|
||||
return new GassnerEnqScale(config.Id, config.Model!, config.Connection!, config.Empty, config.Filling, limit, config.Log);
|
||||
} else if (config.Type == "Avery-Async") {
|
||||
return new AveryEventScale(config.Id, config.Model!, config.Connection!, config.Log, config.Required);
|
||||
} else if (config.Type == "Gassner") {
|
||||
return new GassnerScale(config.Id, config.Model!, config.Connection!, config.Empty, config.Filling, limit, config.Log);
|
||||
} else {
|
||||
throw new ArgumentException($"Invalid scale type: \"{config.Type}\"");
|
||||
}
|
||||
|
||||
@@ -8,15 +8,14 @@ namespace Elwig.Helpers.Weighing {
|
||||
public class SysTecITScale : Scale, ICommandScale {
|
||||
|
||||
public string Manufacturer => "SysTec";
|
||||
public int InternalScaleNr => 1;
|
||||
public string Model { get; private set; }
|
||||
public string ScaleId { get; private set; }
|
||||
public string ScaleTerminalId { get; private set; }
|
||||
public bool IsReady { get; private set; }
|
||||
public bool HasFillingClearance { get; private set; }
|
||||
|
||||
public SysTecITScale(string id, string model, string cnx, string? empty = null, string? filling = null, int? limit = null, string? log = null) :
|
||||
base(cnx, empty, filling, limit, log) {
|
||||
ScaleId = id;
|
||||
ScaleTerminalId = id;
|
||||
Model = model;
|
||||
IsReady = true;
|
||||
HasFillingClearance = false;
|
||||
@@ -74,15 +73,15 @@ namespace Elwig.Helpers.Weighing {
|
||||
return line[1..^3];
|
||||
}
|
||||
|
||||
protected async Task<WeighingResult> Weigh(bool incIdentNr, bool retry = true) {
|
||||
protected async Task<WeighingResult> Weigh(int internalScalNr, bool incIdentNr, bool retry = true) {
|
||||
string record;
|
||||
try {
|
||||
await SendCommand(incIdentNr ? $"RN{InternalScaleNr}" : $"RM{InternalScaleNr}");
|
||||
await SendCommand(incIdentNr ? $"RN{internalScalNr}" : $"RM{internalScalNr}");
|
||||
record = await ReceiveResponse();
|
||||
} catch (IOException) {
|
||||
if (!retry || Tcp == null) throw;
|
||||
ReconnectTcp();
|
||||
return await Weigh(incIdentNr, false);
|
||||
return await Weigh(internalScalNr, incIdentNr, false);
|
||||
}
|
||||
if (record.Length != 62)
|
||||
throw new FormatException("Invalid response from scale: Received record has invalid size");
|
||||
@@ -114,6 +113,7 @@ namespace Elwig.Helpers.Weighing {
|
||||
GrossWeight = int.Parse(brutto),
|
||||
TareWeight = int.Parse(tara),
|
||||
NetWeight = int.Parse(netto),
|
||||
ScaleId = scaleNr,
|
||||
WeighingId = identNr,
|
||||
FullWeighingId = identNr != null ? $"{parsedDate:yyyy-MM-dd}/{identNr}" : null,
|
||||
Date = parsedDate,
|
||||
@@ -121,12 +121,12 @@ namespace Elwig.Helpers.Weighing {
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<WeighingResult> Weigh() {
|
||||
return await Weigh(true);
|
||||
public async Task<WeighingResult> Weigh(int? scaleNr = 1) {
|
||||
return await Weigh(scaleNr ?? 1, true);
|
||||
}
|
||||
|
||||
public async Task<WeighingResult> GetCurrentWeight() {
|
||||
return await Weigh(false);
|
||||
public async Task<WeighingResult> GetCurrentWeight(int? scaleNr = 1) {
|
||||
return await Weigh(scaleNr ?? 1, false);
|
||||
}
|
||||
|
||||
public async Task Empty() {
|
||||
|
||||
@@ -21,6 +21,11 @@ namespace Elwig.Helpers.Weighing {
|
||||
/// </summary>
|
||||
public int? NetWeight;
|
||||
|
||||
/// <summary>
|
||||
/// Reported id of the scale
|
||||
/// </summary>
|
||||
public string? ScaleId;
|
||||
|
||||
/// <summary>
|
||||
/// Weighing id (or IdentNr) provided by the scale
|
||||
/// </summary>
|
||||
@@ -49,6 +54,7 @@ namespace Elwig.Helpers.Weighing {
|
||||
|
||||
public readonly JsonObject ToJson() {
|
||||
var obj = new JsonObject();
|
||||
if (ScaleId != null) obj["scale"] = ScaleId;
|
||||
if (FullWeighingId != null) obj["id"] = FullWeighingId;
|
||||
if (WeighingId != null) obj["nr"] = int.Parse(WeighingId);
|
||||
if (Date != null) obj["date"] = $"{Date:yyyy-MM-dd}";
|
||||
|
||||
Reference in New Issue
Block a user