Compare commits

...

7 Commits

11 changed files with 284 additions and 18 deletions

View File

@ -7,7 +7,7 @@
<UseWPF>true</UseWPF>
<PreserveCompilationContext>true</PreserveCompilationContext>
<ApplicationIcon>Resources\Images\Elwig.ico</ApplicationIcon>
<Version>0.6.5</Version>
<Version>0.6.6</Version>
<SatelliteResourceLanguages>de-AT</SatelliteResourceLanguages>
</PropertyGroup>

View File

@ -160,16 +160,16 @@ namespace Elwig.Helpers {
}
public async Task<int> NextMgNr() {
int c = await Members.Select(m => m.MgNr).MinAsync();
int c = 0;
(await Members.OrderBy(m => m.MgNr).Select(m => m.MgNr).ToListAsync())
.ForEach(a => { if (a <= c + 1000) c = a; });
return c + 1;
}
public async Task<int> NextFbNr() {
int c = await AreaCommitments.Select(ac => ac.FbNr).MinAsync();
int c = 0;
(await AreaCommitments.OrderBy(ac => ac.FbNr).Select(ac => ac.FbNr).ToListAsync())
.ForEach(a => { if (a <= c + 1000) c = a; });
.ForEach(a => { if (a <= c + 10000) c = a; });
return c + 1;
}

View File

@ -13,10 +13,7 @@ namespace Elwig.Helpers.Weighing {
public string? LogPath => throw new NotImplementedException();
public void Dispose() {
throw new NotImplementedException();
GC.SuppressFinalize(this);
}
}
}

View File

@ -44,6 +44,8 @@ namespace Elwig.Helpers.Weighing {
} else {
throw new ArgumentException("Unsupported scheme");
}
Stream.WriteTimeout = 250;
Stream.ReadTimeout = 11000;
if (empty != null) {
var parts = empty.Split(':');
@ -105,7 +107,7 @@ namespace Elwig.Helpers.Weighing {
line = await reader.ReadLineAsync();
if (LogPath != null) await File.AppendAllTextAsync(LogPath, $"{line}\r\n");
}
if (line == null || line.Length < 4 || !line.StartsWith("<") || !line.EndsWith(">")) {
if (line == null || line.Length < 4 || !line.StartsWith('<') || !line.EndsWith('>')) {
throw new IOException("Invalid response from scale");
}
@ -162,7 +164,7 @@ namespace Elwig.Helpers.Weighing {
var crc16 = line[52..60].Trim();
if (Utils.CalcCrc16Modbus(record[..54]) != ushort.Parse(crc16)) {
throw new IOException($"Invalid response from scale: Invalid CRC16 checksum ({crc16} != {Utils.CalcCrc16Modbus(record[..54]).ToString()})");
throw new IOException($"Invalid response from scale: Invalid CRC16 checksum ({crc16} != {Utils.CalcCrc16Modbus(record[..54])})");
} else if (unit != "kg") {
throw new IOException($"Unsupported unit in weighing response: '{unit}'");
}

View File

@ -4,36 +4,38 @@ namespace Elwig.Helpers.Weighing {
/// <summary>
/// Result of a weighing process on an industrial scale
/// </summary>
public class WeighingResult {
public struct WeighingResult {
/// <summary>
/// Measured net weight in kg
/// </summary>
public int? Weight = null;
public int? Weight;
/// <summary>
/// Weighing id (or IdentNr) provided by the scale
/// </summary>
public string? WeighingId = null;
public string? WeighingId;
/// <summary>
/// Wheighing id (or IdentNr) provided by the scale optionally combined with the current date
/// </summary>
public string? FullWeighingId = null;
public string? FullWeighingId;
/// <summary>
/// Date string provided by the scale
/// </summary>
public DateOnly? Date = null;
public DateOnly? Date;
/// <summary>
/// Time string provided by the scale
/// </summary>
public TimeOnly? Time = null;
public TimeOnly? Time;
/// <returns>&lt;Weight/WeighingId/Date/Time&gt;</returns>
override public string ToString() {
public override readonly string ToString() {
var w = Weight != null ? $"{Weight}kg" : "";
return $"<{w}/{WeighingId}/{Date:yyyy-MM-dd}/{Time:HH:mm}>";
}
}
}

View File

@ -863,7 +863,7 @@ namespace Elwig.Windows {
var originalMemberKgNr = d?.Member?.DefaultKgNr;
if (d == null) {
d = Context.CreateProxy<Delivery>();
year = Utils.CurrentNextSeason;
year = Utils.CurrentYear;
did = await Context.NextDId(year);
} else {
year = d.Year;

View File

@ -100,5 +100,40 @@ namespace Tests.HelperTests {
Assert.That(Utils.SplitName("ABC GesbR", "Bauer"), Is.EqualTo(((string, string?))("ABC GesbR", null)));
});
}
[Test]
public void Test_CalcCrc16Modbus() {
Assert.Multiple(() => {
Assert.That(Utils.CalcCrc16Modbus(""), Is.EqualTo(0xFFFF));
Assert.That(Utils.CalcCrc16Modbus("abcd"), Is.EqualTo(0x1D97));
Assert.That(Utils.CalcCrc16Modbus("ABCD"), Is.EqualTo(0x0F85));
// Matzen (SysTec IT3000A)
Assert.That(Utils.CalcCrc16Modbus("000011.08.2319:03 01 0 0 0kg 1"), Is.EqualTo(43166));
Assert.That(Utils.CalcCrc16Modbus("000011.08.2319:04 01 0 0 0kg 1"), Is.EqualTo(21615));
Assert.That(Utils.CalcCrc16Modbus("000011.08.2319:05 01 0 0 0kg 1"), Is.EqualTo(40446));
Assert.That(Utils.CalcCrc16Modbus("000011.08.2319:06 01 0 0 0kg 1"), Is.EqualTo(34638));
Assert.That(Utils.CalcCrc16Modbus("000011.08.2319:07 01 0 0 0kg 1"), Is.EqualTo(20191));
Assert.That(Utils.CalcCrc16Modbus("000011.08.2319:08 01 0 0 0kg 1"), Is.EqualTo(16047));
Assert.That(Utils.CalcCrc16Modbus("000011.08.2319:09 01 0 0 0kg 1"), Is.EqualTo(63294));
Assert.That(Utils.CalcCrc16Modbus("000011.08.2319:10 01 0 0 0kg 1"), Is.EqualTo(7718));
Assert.That(Utils.CalcCrc16Modbus("000011.08.2319:12 01 0 0 0kg 1"), Is.EqualTo(52487));
// Wolkersdorf, Waage 1 (SysTec IT6000E)
Assert.That(Utils.CalcCrc16Modbus("000004.09.2315:50 51 0 0 0kg A"), Is.EqualTo(10187));
Assert.That(Utils.CalcCrc16Modbus("000004.09.2315:50 61 0 0 0kg A"), Is.EqualTo(20683));
Assert.That(Utils.CalcCrc16Modbus("000004.09.2315:50 71 0 0 0kg A"), Is.EqualTo(48586));
Assert.That(Utils.CalcCrc16Modbus("000004.09.2315:50 81 0 0 0kg A"), Is.EqualTo(22217));
Assert.That(Utils.CalcCrc16Modbus("000004.09.2315:50 91 0 0 0kg A"), Is.EqualTo(48072));
Assert.That(Utils.CalcCrc16Modbus("000004.09.2315:51 101 0 0 0kg A"), Is.EqualTo(30119));
Assert.That(Utils.CalcCrc16Modbus("000004.09.2315:51 111 0 0 0kg A"), Is.EqualTo(39078));
// Wolkersdorf, Waage 2 (SysTec IT6000E)
Assert.That(Utils.CalcCrc16Modbus("000004.09.2315:51 41 0 0 0kg B"), Is.EqualTo(539));
Assert.That(Utils.CalcCrc16Modbus("000004.09.2315:51 51 0 0 0kg B"), Is.EqualTo(61210));
Assert.That(Utils.CalcCrc16Modbus("000004.09.2315:51 61 0 0 0kg B"), Is.EqualTo(38938));
Assert.That(Utils.CalcCrc16Modbus("000004.09.2315:51 71 0 0 0kg B"), Is.EqualTo(29979));
Assert.That(Utils.CalcCrc16Modbus("000004.09.2315:51 81 0 0 0kg B"), Is.EqualTo(40472));
Assert.That(Utils.CalcCrc16Modbus("000004.09.2315:51 91 0 0 0kg B"), Is.EqualTo(29465));
Assert.That(Utils.CalcCrc16Modbus("000004.09.2315:51 101 0 0 0kg B"), Is.EqualTo(29927));
});
}
}
}

View File

@ -0,0 +1,71 @@
using System.Net.Sockets;
using System.Net;
using System.Text;
namespace Tests.WeighingTests {
public abstract class MockScale : IDisposable {
protected readonly TcpListener Server;
public int IdentNr { get; set; } = 0;
public int Weight { get; set; } = 0;
public string? Error { get; set; } = null;
protected MockScale(int port) {
Server = new TcpListener(IPAddress.Loopback, port);
Server.Start(4);
}
public void Dispose() {
Server.Dispose();
GC.SuppressFinalize(this);
}
}
public class CommandMockScale : MockScale {
private readonly Func<string, int, string?, int, (string, bool)> Handler;
private readonly Thread ServerThread;
private bool IsRunning = true;
public CommandMockScale(int port, Func<string, int, string?, int, (string, bool)> handler) :
base(port) {
Handler = handler;
ServerThread = new Thread(new ParameterizedThreadStart(Serve));
ServerThread.Start();
}
private async void Serve(object? parameters) {
byte[] buffer = new byte[16];
while (IsRunning) {
try {
using var client = await Server.AcceptTcpClientAsync();
if (client == null) continue;
using var stream = client.GetStream();
while (true) {
int read = await stream.ReadAsync(buffer);
var (res, inc) = Handler(Encoding.ASCII.GetString(buffer, 0, read), Weight, Error, IdentNr + 1);
if (inc) IdentNr++;
await stream.WriteAsync(Encoding.ASCII.GetBytes(res));
}
} catch (Exception) {
// ignore
}
}
}
public new void Dispose() {
IsRunning = false;
ServerThread.Interrupt();
ServerThread.Join();
base.Dispose();
}
}
public class EventMockScale : MockScale {
public EventMockScale(int port, Func<int, string?, int, (string, bool)> handler) :
base(port) {
// TODO
}
}
}

View File

@ -0,0 +1,147 @@
using Elwig.Helpers.Weighing;
namespace Tests.WeighingTests {
[TestFixture]
class ScaleTestMatzen {
private MockScale? Mock;
private SystecScale? Scale;
private static (string, bool) ScaleHandler(string req, int weight, string? error, int identNr) {
var modes = error?.Split(';') ?? [];
var overloaded = modes.Contains("overloaded");
var moving = modes.Contains("moving");
var invalid = modes.Contains("invalid");
var crc = modes.Contains("crc");
var unit = modes.Contains("unit");
if (invalid) {
return ("abcd\r\n", false);
} else if (!req.StartsWith('<') || !req.EndsWith('>')) {
return ("<31>\r\n", false);
}
req = req[1..^1];
bool incr;
if (req.Length > 3) {
return ("<32>\r\n", false);
} else if (req.StartsWith("RN")) {
incr = true;
} else if (req.StartsWith("RM")) {
incr = false;
} else {
return ("<32>\r\n", false);
}
if (overloaded) {
return ("<12>\r\n", false);
} else if (weight == 0) {
incr = false;
}
if (moving && incr)
return ("<13>\r\n", false);
string data = $"00{(moving ? 1 : 0)}0{DateTime.Now:dd.MM.yyHH:mm}{(incr ? identNr : 0),4}1" +
$"{weight,8}{0,8}{weight,8}{(unit ? "lb" : "kg")} {1,3}";
ushort checksum = Elwig.Helpers.Utils.CalcCrc16Modbus(data);
if (crc) checksum += 10;
return ($"<{data}{checksum,8}>\r\n", incr);
}
[OneTimeSetUp]
public void SetupScale() {
Mock = new CommandMockScale(12345, ScaleHandler);
Scale = new("1", "IT3000A", "tcp://127.0.0.1:12345");
}
[OneTimeTearDown]
public void TeardownScale() {
Mock?.Dispose();
Scale?.Dispose();
}
[SetUp]
public void ResetScale() {
Mock!.IdentNr = 0;
Mock!.Weight = 0;
Mock!.Error = null;
}
[Test]
public async Task Test_01_CurrentWeight() {
Mock!.Weight = 1234;
Assert.That(await Scale!.GetCurrentWeight(), Is.EqualTo(new WeighingResult {
Weight = 1234, Date = Utils.Today, Time = Utils.Time,
}));
Mock!.Weight = 1235;
Assert.That(await Scale!.GetCurrentWeight(), Is.EqualTo(new WeighingResult {
Weight = 1235, Date = Utils.Today, Time = Utils.Time,
}));
Mock!.Weight = 1236;
Assert.That(await Scale!.GetCurrentWeight(), Is.EqualTo(new WeighingResult {
Weight = 1236, Date = Utils.Today, Time = Utils.Time,
}));
}
[Test]
public async Task Test_02_Normal() {
Mock!.Weight = 1234;
Assert.That(await Scale!.Weigh(), Is.EqualTo(new WeighingResult {
Weight = 1234, WeighingId = "1",
FullWeighingId = $"{DateTime.Today:yyyy-MM-dd}/1",
Date = Utils.Today, Time = Utils.Time,
}));
Mock!.Weight = 3333;
Assert.That(await Scale!.Weigh(), Is.EqualTo(new WeighingResult {
Weight = 3333, WeighingId = "2",
FullWeighingId = $"{DateTime.Today:yyyy-MM-dd}/2",
Date = Utils.Today, Time = Utils.Time,
}));
Mock!.Weight = 4321;
Assert.That(await Scale!.Weigh(), Is.EqualTo(new WeighingResult {
Weight = 4321, WeighingId = "3",
FullWeighingId = $"{DateTime.Today:yyyy-MM-dd}/3",
Date = Utils.Today, Time = Utils.Time,
}));
}
[Test]
public void Test_03_Moving() {
Mock!.Weight = 1_000;
Mock!.Error = "moving";
IOException ex = Assert.ThrowsAsync<IOException>(async () => await Scale!.Weigh());
Assert.That(ex.Message, Contains.Substring("Waage in Bewegung"));
}
[Test]
public void Test_04_Overloaded() {
Mock!.Weight = 10_000;
Mock!.Error = "overloaded";
IOException ex = Assert.ThrowsAsync<IOException>(async () => await Scale!.Weigh());
Assert.That(ex.Message, Contains.Substring("Waage in Überlast"));
}
[Test]
public void Test_05_InvalidResponse() {
Mock!.Weight = 1_000;
Mock!.Error = "invalid";
Assert.ThrowsAsync<IOException>(async () => await Scale!.Weigh());
}
[Test]
public void Test_06_InvalidCrc() {
Mock!.Weight = 1_000;
Mock!.Error = "crc";
IOException ex = Assert.ThrowsAsync<IOException>(async () => await Scale!.Weigh());
Assert.That(ex.Message, Contains.Substring("Invalid CRC16 checksum"));
}
[Test]
public void Test_07_InvalidUnit() {
Mock!.Weight = 1_000;
Mock!.Error = "unit";
IOException ex = Assert.ThrowsAsync<IOException>(async () => await Scale!.Weigh());
}
}
}

View File

@ -0,0 +1,6 @@
namespace Tests.WeighingTests {
[TestFixture]
public class ScaleTestWolkersdorf {
// TODO
}
}

View File

@ -0,0 +1,6 @@
namespace Tests.WeighingTests {
public static class Utils {
public static DateOnly Today => DateOnly.FromDateTime(DateTime.Now);
public static TimeOnly Time => new(DateTime.Now.Hour, DateTime.Now.Minute);
}
}