From 482ce90ecf8f4cc9fdb9deeb37ad1d50d9f5caa2 Mon Sep 17 00:00:00 2001 From: stephan Date: Sun, 24 Mar 2024 11:42:09 +0100 Subject: [PATCH] bug: tare always when weight < 50 --- README.md | 5 +- .../timer/ContinuousScaleTaring.java | 2 +- .../timer/ContinuousScaleTaringTest.java | 68 +++++++++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 src/test/java/ch/stephan/chickenfarm/timer/ContinuousScaleTaringTest.java diff --git a/README.md b/README.md index fd1835e..d515021 100644 --- a/README.md +++ b/README.md @@ -8,13 +8,16 @@ This code will run on a Raspberry PI in a chicken barn. There are scales under t ### Epic v1: Chicken scale recognizes weight change and sends message about state (which chicken, an egg) via Messenger -* understand mqtt, qos, retain and create tasks to adopt +* BUG: Observer muss regelmässiger laufen, abstimmen mit Mqtt Message +* BUG: wenn Ei gelegt und erkannt, tare-it oder wenn nicht, die letzten Hühner in Message packen +* BUG: Konstellation "Ei drin, neues Huhn rein" löst keine Message und keinen neuen State aus * correct shutdown of mqtt user * implement calibrate * write tests and mocks for mqtt client * ping message to check scale * endpoint to display state and messages * endpoint to set current weight, box-state and chicken +* understand mqtt, qos, retain and create tasks to adopt ## TODO List Arduino diff --git a/src/main/java/ch/stephan/chickenfarm/timer/ContinuousScaleTaring.java b/src/main/java/ch/stephan/chickenfarm/timer/ContinuousScaleTaring.java index 4db1a6d..fceb0d7 100644 --- a/src/main/java/ch/stephan/chickenfarm/timer/ContinuousScaleTaring.java +++ b/src/main/java/ch/stephan/chickenfarm/timer/ContinuousScaleTaring.java @@ -30,7 +30,7 @@ public void tareAllBoxes() { private void tareIfNeeded(String id) { Box box = boxService.getBox(id); - if (Math.abs(box.getWeight()) > 5 && Math.abs(box.getWeight()) < 50) { + if (box.getWeight() < 50) { scaleService.tare(id); } else { log.info("No need to tare Scale {}, weight was {}.", id, box.getWeight()); diff --git a/src/test/java/ch/stephan/chickenfarm/timer/ContinuousScaleTaringTest.java b/src/test/java/ch/stephan/chickenfarm/timer/ContinuousScaleTaringTest.java new file mode 100644 index 0000000..e24754a --- /dev/null +++ b/src/test/java/ch/stephan/chickenfarm/timer/ContinuousScaleTaringTest.java @@ -0,0 +1,68 @@ +package ch.stephan.chickenfarm.timer; + +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.times; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +import ch.stephan.chickenfarm.registry.BoxService; +import ch.stephan.chickenfarm.scale.ScaleService; + +@ExtendWith(MockitoExtension.class) +class ContinuousScaleTaringTest { + + private ContinuousScaleTaring continuousScaleTaring; + + @InjectMocks + private BoxService boxService; + + @Mock + private ScaleService mockedScaleService; + + @BeforeEach + void setUp() throws Exception { + boxService.initBoxes(); + continuousScaleTaring = new ContinuousScaleTaring(boxService, mockedScaleService); + } + + @Test + void test_WhenWeight0_tareIsCalled() { + setAllWeightsTo(0); + callAndCheckTimesCalled(2); + } + + @Test + void test_WhenWeightMinus165_tareIsCalled() { + setAllWeightsTo(-165); + callAndCheckTimesCalled(2); + } + + @Test + void test_WhenWeight15_tareIsCalled() { + setAllWeightsTo(15); + callAndCheckTimesCalled(2); + } + + @Test + void test_WhenWeight65_tareIsCalled() { + setAllWeightsTo(65); + callAndCheckTimesCalled(0); + } + + private void setAllWeightsTo(int weight) { + boxService.getBoxes().stream()// + .forEach(b -> b.setWeight(weight)); + } + + private void callAndCheckTimesCalled(int times) { + continuousScaleTaring.tareAllBoxes(); + Mockito.verify(mockedScaleService, times(times)).tare(anyString()); + } + +}