Skip to content

Commit

Permalink
using a Discovery record instead of string
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanSST committed Jul 27, 2023
1 parent 99a463b commit 3b90b15
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 31 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@ This code will run on a Raspberry PI in a chicken barn. There are scales under t
* Story: (done) using Github Actions to automatic build and create an image
* Story: (done) install current Docker image on Raspberry PI and start it initially
* Story: (done) create structure with timer service pulling dummy scale service and show data via GET service
* Story: (done) give calibrate webservice operation for load cell
* Story: (done) create scale service that delivers current weight

* Story: create scale service that delivers current weight
* Story: change discovery webserivce to use DO instead of strings
* Story: write tests and mock for scale service
* Story: respect box/UID of load cell

* Story: get current weight and calculate the "state" (chicken, egg)
* Story: Integration test with whole process
* Story: integrate Whatsapp and send messages
* Story: integrate Prometheus
* Story: manage chickens
* Story: read data from scale and show by webservice (GET)




6 changes: 0 additions & 6 deletions src/main/java/ch/stephan/chickenfarm/Discovery.java

This file was deleted.

6 changes: 2 additions & 4 deletions src/main/java/ch/stephan/chickenfarm/DiscoveryController.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package ch.stephan.chickenfarm;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import ch.stephan.chickenfarm.dto.Discovery;
import ch.stephan.chickenfarm.scale.ScaleService;

@RestController
Expand All @@ -17,9 +17,7 @@ public class DiscoveryController {

@GetMapping("/discovery")
public List<Discovery> discovery() {
return scaleService.discovery().entrySet().stream()//
.map(e -> new Discovery(e.getKey(), e.getValue()))//
.collect(Collectors.toList());
return scaleService.discovery();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import ch.stephan.chickenfarm.dto.Greeting;
import ch.stephan.chickenfarm.scale.ScaleService;

@RestController
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/ch/stephan/chickenfarm/dto/Discovery.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package ch.stephan.chickenfarm.dto;

public record Discovery(String uid, String connectedUid, int position, String hardwareVersion, String firmwareVersion,
int deviceIdentifier, int enumerationType) {
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package ch.stephan.chickenfarm;
package ch.stephan.chickenfarm.dto;

public record Greeting(long id, String content) { }
39 changes: 21 additions & 18 deletions src/main/java/ch/stephan/chickenfarm/scale/ScaleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

Expand All @@ -15,6 +13,7 @@
import com.tinkerforge.NotConnectedException;
import com.tinkerforge.TinkerforgeException;

import ch.stephan.chickenfarm.dto.Discovery;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;

Expand All @@ -26,7 +25,7 @@ public class ScaleService {
private static final String UID = "ZUw";

private IPConnection ipConnection;
private Map<String, List<String>> discoveryResult = new HashMap<>();
private List<Discovery> discoveryResult = new ArrayList<>();

public int measureWeight(int box) {
int weight = -1;
Expand All @@ -42,7 +41,22 @@ public int measureWeight(int box) {
return weight;
}

public Map<String, List<String>> discovery() {
public String calibrate(int box) {
try {
BrickletLoadCellV2 loadCell = new BrickletLoadCellV2(UID, ipConnection);
int before = loadCell.getWeight();
loadCell.calibrate(before);
int after = loadCell.getWeight();
System.out.println("Scale was recalibrated. Before: " + before + " g - after: " + after + " g");

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

return "successfully calibrated";
}

public List<Discovery> discovery() {
try {
ipConnection.enumerate();
System.out.println("Broadcast sent to all connected components");
Expand Down Expand Up @@ -89,20 +103,9 @@ private final class EnumerateListenerImpl implements IPConnection.EnumerateListe
public void enumerate(String uid, String connectedUid, char position, short[] hardwareVersion,
short[] firmwareVersion, int deviceIdentifier, short enumerationType) {

List<String> result = new ArrayList<String>();
result.add("UID: " + uid);
result.add("Enumeration Type: " + enumerationType);

if (enumerationType != IPConnection.ENUMERATION_TYPE_DISCONNECTED) {
result.add("Connected UID: " + connectedUid);
result.add("Position: " + position);
result.add("Hardware Version: " + asString(hardwareVersion));
result.add("Firmware Version: " + asString(firmwareVersion));
result.add("Device Identifier: " + deviceIdentifier);
}
result.add("");

discoveryResult.put(uid, result);
Discovery discovery = new Discovery(uid, connectedUid, position, asString(hardwareVersion),
asString(firmwareVersion), deviceIdentifier, enumerationType);
discoveryResult.add(discovery);
}

private String asString(short[] version) {
Expand Down

0 comments on commit 3b90b15

Please sign in to comment.