Skip to content

Commit

Permalink
respect both scales
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanSST committed Sep 25, 2023
1 parent e69a577 commit 0136eb3
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 9 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ 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

* Story: respect both boxes in Schedule
* Story: Integration test with whole process (testing)
* Story: Check if Slack is better than Threema
* Story: using Lombok to log
* Story: Check if Slack is better than Threema
* Story: switch to feign client
* Story: fix disabled test
* Story: Integration test with whole process (testing)

* Story: ENV VAR for "enabled" and Schedule "fixedRate"
* Story: (done) respect both boxes in Schedule
* Story: (done) ENV VAR for "enabled" and Schedule "fixedRate"
* Story: (done) write tests and mock for scale service (testing)
* Story: (done) get current weight and calculate the "state" (chicken, egg)
* Story: (done) env values via application.yaml
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/ch/stephan/chickenfarm/scale/ScaleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class ScaleService {
private static final Logger log = LoggerFactory.getLogger(ScaleService.class);

private IPConnection ipConnection;
private List<Discovery> discoveryResult = new ArrayList<>();
private List<Discovery> discoveryResult;

public ScaleService() {
super();
Expand Down Expand Up @@ -69,8 +69,23 @@ public String calibrate(String uid) {
return "successfully calibrated";
}

public String tare(String uid) {
try {
BrickletLoadCellV2 loadCell = new BrickletLoadCellV2(uid, ipConnection);
loadCell.tare();
log.info("Scale {} has been tared.", uid);

} catch (TinkerforgeException ex) {
ex.printStackTrace();
}

return "successfully tared";
}

public List<Discovery> discovery() {
try {
discoveryResult = new ArrayList<>();

ipConnection.enumerate();
log.info("Broadcast sent to all connected components");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@

@RestController
public class MeasureController {
private static final String MEASURE = "Weight of box %s (%s) is %s.";
private static final String POINT = ".";
private static final String COMMA = ", ";
private static final String EMPTY_STRING = "";
private static final String MEASURE = "Weight of box %s (%s) is %s";
private static final String CALIBRATE = "Calibrated box %s, result: %s.";
private static final String TARE = "Tared box %s, result: %s.";

private final AtomicLong counter = new AtomicLong();

Expand All @@ -36,7 +40,7 @@ public Message measure() {
int weight = scaleService.measureWeight(b.id());
return String.format(MEASURE, b.id(), b.description(), weight);
})//
.collect(Collectors.joining(", "));
.collect(Collectors.joining(COMMA, EMPTY_STRING, POINT));
return new Message(counter.incrementAndGet(), message);
}

Expand All @@ -45,6 +49,11 @@ public Message calibrate(@RequestParam(value = "uid") String uid) {
return new Message(counter.incrementAndGet(), String.format(CALIBRATE, uid, scaleService.calibrate(uid)));
}

@GetMapping("/tare")
public Message tare(@RequestParam(value = "uid") String uid) {
return new Message(counter.incrementAndGet(), String.format(TARE, uid, scaleService.tare(uid)));
}

@GetMapping("/send")
public Message send(@RequestParam(value = "text") String text) {
String result = messengerService.sendNotification(text);
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/ch/stephan/chickenfarm/timer/ScaleObserver.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.stereotype.Component;

import ch.stephan.chickenfarm.messenger.MessengerService;
import ch.stephan.chickenfarm.registry.BoxService;
import ch.stephan.chickenfarm.scale.ScaleService;

@Component
Expand All @@ -20,6 +21,9 @@ public class ScaleObserver {

private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

@Autowired
private BoxService boxService;

@Autowired
private ScaleService scaleService;

Expand All @@ -29,7 +33,11 @@ public class ScaleObserver {
@Scheduled(fixedRateString = "${schedulerservice.fixedRate}")

public void measureWeights() {
String uid = "23yp";
boxService.getBoxes().stream()//
.forEach(b -> measureWeightOfScale(b.id()));
}

private void measureWeightOfScale(String uid) {
int weight = scaleService.measureWeight(uid);

if (weight > 1000) { // chicken in the box
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void testMeasure() throws Exception {
Message message = objectMapper.readValue(mockMvcResult, new TypeReference<>() {});

assertNotNull(message);
assertThat(message.content()).isEqualTo("Weight of box 23yp (hinten) is 666., Weight of box ZUw (vorne) is 667.");
assertThat(message.content()).isEqualTo("Weight of box 23yp (hinten) is 666, Weight of box ZUw (vorne) is 667.");
}

@Test
Expand Down

0 comments on commit 0136eb3

Please sign in to comment.