add libraries server project

This commit is contained in:
Loredana Crusoveanu
2018-07-31 22:39:22 +03:00
parent 113ebc2725
commit 4aa8633c43
31 changed files with 186 additions and 7 deletions
@@ -0,0 +1,49 @@
package com.baeldung.mqtt;
import java.util.Random;
import java.util.concurrent.Callable;
import org.eclipse.paho.client.mqttv3.IMqttClient;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class EngineTemperatureSensor implements Callable<Void> {
private static final Logger log = LoggerFactory.getLogger(EngineTemperatureSensor.class);
public static final String TOPIC = "engine/temperature";
private IMqttClient client;
private Random rnd = new Random();
public EngineTemperatureSensor(IMqttClient client) {
this.client = client;
}
@Override
public Void call() throws Exception {
if ( !client.isConnected()) {
log.info("[I31] Client not connected.");
return null;
}
MqttMessage msg = readEngineTemp();
msg.setQos(0);
msg.setRetained(true);
client.publish(TOPIC,msg);
return null;
}
/**
* This method simulates reading the engine temperature
* @return
*/
private MqttMessage readEngineTemp() {
double temp = 80 + rnd.nextDouble() * 20.0;
byte[] payload = String.format("T:%04.2f",temp).getBytes();
MqttMessage msg = new MqttMessage(payload);
return msg;
}
}