Skip to content

Commit

Permalink
bug: tare always when weight < 50
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanSST committed Mar 24, 2024
1 parent 541a677 commit 482ce90
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}

}

0 comments on commit 482ce90

Please sign in to comment.