Skip to content

Commit

Permalink
using env vars to configure
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanSST committed Aug 1, 2023
1 parent 7dcd498 commit 7733978
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ 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 Whatsapp

* Story: env values via applicatoin.yaml
* Story: get current weight and calculate the "state" (chicken, egg)

* Story: using Lombok to log
* Story: write tests and mock for scale service (testing)
* Story: Integration test with whole process (testing)

* Story: (done) env values via application.yaml
* Story: (done) integrate Threema and send messages
* Story: (done) read data from scale and show by webservice (GET)
* Story: (done) change discovery webservice to use DO instead of strings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
Expand All @@ -18,12 +19,18 @@ public class MessengerService {
private static final Logger log = LoggerFactory.getLogger(MessengerService.class);

private static final String URL = "https://msgapi.threema.ch/send_simple?to={to}&text={text}&from={from}&secret={secret}";
private static final String FROM = System.getenv("THREEMA_FROM");
private static final String TO = System.getenv("THREEMA_TO");
private static final String SECRET = System.getenv("THREEMA_SECRET");

private final RestTemplate restTemplate;

@Value("${messengerservice.from}")
private String from;

@Value("${messengerservice.to}")
private String to;

@Value("${messengerservice.secret}")
private String secret;

public MessengerService(RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = restTemplateBuilder.build();
}
Expand All @@ -35,10 +42,10 @@ public String sendNotification(String message) {
HttpEntity<?> httpEntity = new HttpEntity<>(headers);

Map<String, String> params = new HashMap<>();
params.put("to", TO);
params.put("to", to);
params.put("text", message);
params.put("from", FROM);
params.put("secret", SECRET);
params.put("from", from);
params.put("secret", secret);

String messageId = restTemplate.postForObject(URL, httpEntity, String.class, params);

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/application-dev.yaml
Empty file.
8 changes: 8 additions & 0 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
spring:
application:
name: chicken-farm

messengerservice:
from: ${THREEMA_FROM}
to: ${THREEMA_TO}
secret: ${THREEMA_SECRET}

0 comments on commit 7733978

Please sign in to comment.