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) { bool incr = false; if (!req.StartsWith('<') || !req.EndsWith('>')) return ("<31>\r\n", incr); req = req[1..^1]; if (req.Length > 3) { return ("<32>\r\n", incr); } else if (req.StartsWith("RN")) { incr = true; } else if (req.StartsWith("RM")) { incr = false; } else { return ("<32>\r\n", incr); } var modes = error?.Split(';') ?? []; if (weight == 0) { incr = false; } bool moving = modes.Contains("moving"); string data = $"00{(moving ? 1 : 0)}0{DateTime.Now:dd.MM.yyHH:mm}{(incr ? identNr : 0),4}1{weight,8}{0,8}{weight,8}kg {1,3}"; return ($"<{data}{Elwig.Helpers.Utils.CalcCrc16Modbus(data),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, })); } } }