diff --git a/pom.xml b/pom.xml index 19e3ec8..2d69852 100644 --- a/pom.xml +++ b/pom.xml @@ -89,6 +89,15 @@ lombok-mapstruct-binding 0.2.0 + + org.springframework.integration + spring-integration-mqtt + + + org.eclipse.paho + org.eclipse.paho.client.mqttv3 + 1.2.5 + diff --git a/src/main/java/de/exodus_hn/excalibur_tour/configurations/MqttConfig.java b/src/main/java/de/exodus_hn/excalibur_tour/configurations/MqttConfig.java new file mode 100644 index 0000000..f10bde2 --- /dev/null +++ b/src/main/java/de/exodus_hn/excalibur_tour/configurations/MqttConfig.java @@ -0,0 +1,75 @@ +package de.exodus_hn.excalibur_tour.configurations; + +import de.exodus_hn.excalibur_tour.services.SwordStateService; +import org.eclipse.paho.client.mqttv3.MqttConnectOptions; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.integration.annotation.ServiceActivator; +import org.springframework.integration.channel.DirectChannel; +import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory; +import org.springframework.integration.mqtt.core.MqttPahoClientFactory; +import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter; +import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler; +import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter; +import org.springframework.messaging.MessageChannel; +import org.springframework.messaging.MessageHandler; + +@Configuration +public class MqttConfig { + + private static final String BROKER_URL = "ssl://excaliburs-erben.de:8883"; + private static final String MQTT_USER = "exodususer"; + private static final String MQTT_PASSWORD = "awesomeexodusoutdooradventures"; + + @Bean + public MqttPahoClientFactory mqttClientFactory() { + DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory(); + MqttConnectOptions options = new MqttConnectOptions(); + options.setServerURIs(new String[] { BROKER_URL }); + options.setUserName(MQTT_USER); + options.setPassword(MQTT_PASSWORD.toCharArray()); + options.setCleanSession(true); + factory.setConnectionOptions(options); + return factory; + } + + @Bean + public MessageChannel mqttOutboundChannel() { + return new DirectChannel(); + } + + @Bean + @ServiceActivator(inputChannel = "mqttOutboundChannel") + public MessageHandler mqttOutbound() { + MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler("springBootServerPublisher", mqttClientFactory()); + messageHandler.setAsync(true); + messageHandler.setDefaultTopic("staging/outdoor/king-arthur/sword/set"); + return messageHandler; + } + + @Bean + public MessageChannel mqttInputChannel() { + return new DirectChannel(); + } + + @Bean + public MqttPahoMessageDrivenChannelAdapter inbound() { + MqttPahoMessageDrivenChannelAdapter adapter = + new MqttPahoMessageDrivenChannelAdapter("springBootServerSubscriber", mqttClientFactory(), "staging/outdoor/king-arthur/sword/stat"); + adapter.setCompletionTimeout(5000); + adapter.setConverter(new DefaultPahoMessageConverter()); + adapter.setQos(2); + adapter.setOutputChannel(mqttInputChannel()); + return adapter; + } + + @Bean + @ServiceActivator(inputChannel = "mqttInputChannel") + public MessageHandler inboundHandler(SwordStateService swordStateService) { + return message -> { + String payload = (String) message.getPayload(); + System.out.println("Received MQTT payload: " + payload); + swordStateService.updateState(payload); + }; + } +} \ No newline at end of file diff --git a/src/main/java/de/exodus_hn/excalibur_tour/controller/SwordController.java b/src/main/java/de/exodus_hn/excalibur_tour/controller/SwordController.java new file mode 100644 index 0000000..5edead1 --- /dev/null +++ b/src/main/java/de/exodus_hn/excalibur_tour/controller/SwordController.java @@ -0,0 +1,38 @@ +package de.exodus_hn.excalibur_tour.controller; + +import de.exodus_hn.excalibur_tour.services.SwordStateService; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.integration.support.MessageBuilder; +import org.springframework.integration.mqtt.support.MqttHeaders; +import org.springframework.messaging.Message; +import org.springframework.messaging.MessageChannel; +import org.springframework.web.bind.annotation.*; + +@CrossOrigin +@RestController +@RequestMapping("/sword") +@RequiredArgsConstructor +public class SwordController { + + @Qualifier("mqttOutboundChannel") + private final MessageChannel mqttOutboundChannel; + + private final SwordStateService swordStateService; + + @GetMapping + public String getSwordState() { + return swordStateService.getCurrentState(); + } + + @PostMapping("/command/{commandType}") + public String sendCommand(@PathVariable String commandType) { + // Build and send the MQTT message directly to the channel[cite: 7] + Message message = MessageBuilder.withPayload(commandType) + .setHeader(MqttHeaders.TOPIC, "staging/outdoor/king-arthur/sword/set") + .build(); + + mqttOutboundChannel.send(message); + return "Command sent to MQTT: " + commandType; + } +} \ No newline at end of file diff --git a/src/main/java/de/exodus_hn/excalibur_tour/services/SwordStateService.java b/src/main/java/de/exodus_hn/excalibur_tour/services/SwordStateService.java new file mode 100644 index 0000000..5e20a60 --- /dev/null +++ b/src/main/java/de/exodus_hn/excalibur_tour/services/SwordStateService.java @@ -0,0 +1,17 @@ +package de.exodus_hn.excalibur_tour.services; + +import org.springframework.stereotype.Service; +import java.util.concurrent.atomic.AtomicReference; + +@Service +public class SwordStateService { + private final AtomicReference currentState = new AtomicReference<>("No state received yet"); + + public void updateState(String state) { + currentState.set(state); + } + + public String getCurrentState() { + return currentState.get(); + } +}