Commit 334af4f0 authored by p's avatar p
Browse files

added endpoint for alarm

parent 60ff50b6
......@@ -21,6 +21,12 @@
<dependencies>
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
......
package com.tuke.fei.atvi2.api.config;
import org.eclipse.paho.client.mqttv3.IMqttClient;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.UUID;
@Configuration
public class MqttConfig {
// private final String BROKER = "tcp://broker.mqttdashboard.com";
private final String BROKER = "tcp://openlab.kpi.fei.tuke.sk";
private final String CLIENT_ID = "TUKE-IoT2-" + UUID.randomUUID().toString();
@Bean
public IMqttClient mqttClient() throws MqttException {
IMqttClient publisher = new MqttClient(BROKER, CLIENT_ID, new MemoryPersistence());
MqttConnectOptions options = new MqttConnectOptions();
options.setAutomaticReconnect(true);
options.setCleanSession(true);
options.setConnectionTimeout(10);
publisher.connect(options);
return publisher;
}
}
package com.tuke.fei.atvi2.api.endpoint;
import org.eclipse.paho.client.mqttv3.IMqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("alarm")
public class AlarmEndpoint {
@Autowired
IMqttClient mqttClient;
@GetMapping(value = "/")
public boolean alarm() throws MqttException {
MqttMessage message = new MqttMessage("{\"play\": \"https://nf1f8200-a.akamaihd.net/downloads/ringtones/files/mp3/danger-alarm-23793.mp3\" }".getBytes());
message.setQos(2);
mqttClient.publish("openlab/audio", message);
return true;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment