Weighing: Add Gassner scale and update tests
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
using System.Net.Sockets;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
|
||||
namespace Tests.WeighingTests {
|
||||
public abstract class MockScale : IDisposable {
|
||||
@ -63,9 +64,46 @@ namespace Tests.WeighingTests {
|
||||
}
|
||||
|
||||
public class EventMockScale : MockScale {
|
||||
|
||||
private readonly Func<int, string?, int, (string, bool)> Handler;
|
||||
private readonly Thread ServerThread;
|
||||
private TcpClient? Client;
|
||||
private bool IsRunning = true;
|
||||
|
||||
public EventMockScale(int port, Func<int, string?, int, (string, bool)> handler) :
|
||||
base(port) {
|
||||
// TODO
|
||||
Handler = handler;
|
||||
ServerThread = new Thread(new ParameterizedThreadStart(Serve));
|
||||
ServerThread.Start();
|
||||
}
|
||||
|
||||
private async void Serve(object? parameters) {
|
||||
while (IsRunning) {
|
||||
try {
|
||||
Client = await Server.AcceptTcpClientAsync();
|
||||
} catch (Exception) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task Weigh(int weight) {
|
||||
Weight = weight;
|
||||
await Weigh();
|
||||
}
|
||||
|
||||
public async Task Weigh() {
|
||||
var (res, inc) = Handler(Weight, Error, IdentNr + 1);
|
||||
if (inc) IdentNr++;
|
||||
await Client!.GetStream().WriteAsync(Encoding.ASCII.GetBytes(res));
|
||||
}
|
||||
|
||||
public new void Dispose() {
|
||||
Client?.Dispose();
|
||||
IsRunning = false;
|
||||
ServerThread.Interrupt();
|
||||
ServerThread.Join();
|
||||
base.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user