From f67a05043a6871f1622eadc083e323868967373c Mon Sep 17 00:00:00 2001 From: stephan Date: Tue, 19 Mar 2024 17:02:51 +0100 Subject: [PATCH] save log file --- .gitignore | 1 + README.md | 2 +- .../stephan/chickenfarm/mqtt/MqttConfig.java | 7 +- .../chickenfarm/mqtt/MqttConfiguration.java | 7 +- ...itional-spring-configuration-metadata.json | 86 ++++++++++--------- src/main/resources/application-maven.yaml | 5 +- src/main/resources/application.yaml | 12 ++- .../chickenfarm/mqtt/MockedMqttConfig.java | 2 +- .../mqtt/MockedMqttConfiguration.java | 2 +- 9 files changed, 74 insertions(+), 50 deletions(-) diff --git a/.gitignore b/.gitignore index 3a032e0..d93555b 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ buildNumber.properties .mvn /logs/ /RasPiProducer-tcphuehnerstall1883/ +/EclipseProducer-tcphuehnerstall1883/ diff --git a/README.md b/README.md index ea9716b..c517f19 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,9 @@ 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 * check logs; observer and service log twice -* log messures to file or Prometheus * implement calibrate * write tests and mocks for mqtt client +* correct shutdown of mqtt user ## TODO List Arduino diff --git a/src/main/java/ch/stephan/chickenfarm/mqtt/MqttConfig.java b/src/main/java/ch/stephan/chickenfarm/mqtt/MqttConfig.java index 140d754..154bd33 100644 --- a/src/main/java/ch/stephan/chickenfarm/mqtt/MqttConfig.java +++ b/src/main/java/ch/stephan/chickenfarm/mqtt/MqttConfig.java @@ -22,11 +22,12 @@ @Slf4j @RequiredArgsConstructor -@Profile("default") +@Profile({ "default", "dev" }) @Configuration public class MqttConfig { - private static final String MQTT_CLIENT_ID = "RasPiConsumer"; + @Value("${mqtt.clientId.consumer}") + private String mqttClientId; @Autowired private BoxService boxService; @@ -55,7 +56,7 @@ MqttPahoClientFactory mqttClientFactory() { @Bean MessageProducer inbound() { - MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter(MQTT_CLIENT_ID, + MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter(mqttClientId, mqttClientFactory(), "/chicken-farm/replies"); adapter.setCompletionTimeout(5000); diff --git a/src/main/java/ch/stephan/chickenfarm/mqtt/MqttConfiguration.java b/src/main/java/ch/stephan/chickenfarm/mqtt/MqttConfiguration.java index 3cb8938..9b1a83b 100644 --- a/src/main/java/ch/stephan/chickenfarm/mqtt/MqttConfiguration.java +++ b/src/main/java/ch/stephan/chickenfarm/mqtt/MqttConfiguration.java @@ -12,11 +12,12 @@ import lombok.extern.slf4j.Slf4j; @Slf4j -@Profile("default") +@Profile({ "default", "dev" }) @Configuration public class MqttConfiguration { - private static final String MQTT_CLIENT_ID = "RasPiProducer"; + @Value("${mqtt.clientId.producer}") + private String mqttClientId; @Value("${mqtt.user}") private String user; @@ -34,7 +35,7 @@ IMqttClient mqttClient() throws MqttException { options.setCleanSession(true); options.setConnectionTimeout(10); - IMqttClient mqttClient = new MqttClient("tcp://huehnerstall:1883", MQTT_CLIENT_ID); + IMqttClient mqttClient = new MqttClient("tcp://huehnerstall:1883", mqttClientId); mqttClient.connect(options); log.info("successfully connected to MQTT server"); return mqttClient; diff --git a/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 11f5e70..3b8fbf6 100644 --- a/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -1,39 +1,47 @@ -{ - "properties": [ - { - "name": "messengerservice.enabled", - "type": "java.lang.String", - "description": "Send messages by messenger or not." - }, - { - "name": "messengerservice.channel", - "type": "java.lang.String", - "description": "Name of the Slack channel to publish the messages" - }, - { - "name": "messengerservice.token", - "type": "java.lang.String", - "description": "The security token to use Slack" - }, - { - "name": "schedulerservice.observer.cron", - "type": "java.lang.String", - "description": "Cron expression for scale observer" - }, - { - "name": "schedulerservice.tare.cron", - "type": "java.lang.String", - "description": "Cron expression when to tare the scale" - }, - { - "name": "mqtt.user", - "type": "java.lang.String", - "description": "User id of the mqtt service" - }, - { - "name": "mqtt.password", - "type": "java.lang.String", - "description": "Password of the mqtt service" - } - ] -} \ No newline at end of file +{"properties": [ + { + "name": "messengerservice.enabled", + "type": "java.lang.String", + "description": "Send messages by messenger or not." + }, + { + "name": "messengerservice.channel", + "type": "java.lang.String", + "description": "Name of the Slack channel to publish the messages" + }, + { + "name": "messengerservice.token", + "type": "java.lang.String", + "description": "The security token to use Slack" + }, + { + "name": "schedulerservice.observer.cron", + "type": "java.lang.String", + "description": "Cron expression for scale observer" + }, + { + "name": "schedulerservice.tare.cron", + "type": "java.lang.String", + "description": "Cron expression when to tare the scale" + }, + { + "name": "mqtt.user", + "type": "java.lang.String", + "description": "User id of the mqtt service" + }, + { + "name": "mqtt.password", + "type": "java.lang.String", + "description": "Password of the mqtt service" + }, + { + "name": "mqtt.clientId.producer", + "type": "java.lang.String", + "description": "Client id of the producer of mqtt events" + }, + { + "name": "mqtt.clientId.consumer", + "type": "java.lang.String", + "description": "Client id of the consumer of mqtt events" + } +]} \ No newline at end of file diff --git a/src/main/resources/application-maven.yaml b/src/main/resources/application-maven.yaml index bc28dd4..6846241 100644 --- a/src/main/resources/application-maven.yaml +++ b/src/main/resources/application-maven.yaml @@ -15,4 +15,7 @@ schedulerservice: mqtt: user: dummy - password: dummy \ No newline at end of file + password: dummy + clientId: + producer: MavenProducer + consumer: MavenConsumer diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 0573b38..e40541e 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -15,4 +15,14 @@ schedulerservice: mqtt: user: ${MQTT_USER} - password: ${MQTT_PASSWORD} \ No newline at end of file + password: ${MQTT_PASSWORD} + clientId: + producer: RasPiProducer + consumer: RasPiConsumer + +logging: + file: + name: /logs/application-debug.log + logback: + rollingpolicy: + max-history: 14 diff --git a/src/test/java/ch/stephan/chickenfarm/mqtt/MockedMqttConfig.java b/src/test/java/ch/stephan/chickenfarm/mqtt/MockedMqttConfig.java index a541024..4a610b0 100644 --- a/src/test/java/ch/stephan/chickenfarm/mqtt/MockedMqttConfig.java +++ b/src/test/java/ch/stephan/chickenfarm/mqtt/MockedMqttConfig.java @@ -15,7 +15,7 @@ @RequiredArgsConstructor @Configuration -@Profile({ "maven", "dev" }) +@Profile("maven") public class MockedMqttConfig { @MockBean diff --git a/src/test/java/ch/stephan/chickenfarm/mqtt/MockedMqttConfiguration.java b/src/test/java/ch/stephan/chickenfarm/mqtt/MockedMqttConfiguration.java index ca0e2f9..d2047b9 100644 --- a/src/test/java/ch/stephan/chickenfarm/mqtt/MockedMqttConfiguration.java +++ b/src/test/java/ch/stephan/chickenfarm/mqtt/MockedMqttConfiguration.java @@ -7,7 +7,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; -@Profile({ "maven", "dev" }) +@Profile("maven") @Configuration public class MockedMqttConfiguration {