trial mqtt
This commit is contained in:
1 parent
b7561ca55c
commit
515f00b46f
4 files changed
+139
No files matched your search
@@ -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);
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -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<String> message = MessageBuilder.withPayload(commandType)
|
||||
.setHeader(MqttHeaders.TOPIC, "staging/outdoor/king-arthur/sword/set")
|
||||
.build();
|
||||
|
||||
mqttOutboundChannel.send(message);
|
||||
return "Command sent to MQTT: " + commandType;
|
||||
}
|
||||
}
|
||||
@@ -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<String> currentState = new AtomicReference<>("No state received yet");
|
||||
|
||||
public void updateState(String state) {
|
||||
currentState.set(state);
|
||||
}
|
||||
|
||||
public String getCurrentState() {
|
||||
return currentState.get();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user