kopia lustrzana https://github.com/bertrik/ttnhabbridge
Simplify/reduce/rename configuration items a bit.
rodzic
2f4b7960fc
commit
14ca831b95
|
@ -8,10 +8,8 @@ public interface ITtnHabBridgeConfig {
|
|||
String getHabitatUrl();
|
||||
int getHabitatTimeout();
|
||||
|
||||
String getMqttServerUrl();
|
||||
String getMqttClientId();
|
||||
String getMqttUserName();
|
||||
String getMqttPassword();
|
||||
String getMqttTopic();
|
||||
String getTtnMqttUrl();
|
||||
String getTtnAppId();
|
||||
String getTtnAppKey();
|
||||
|
||||
}
|
||||
|
|
|
@ -59,8 +59,8 @@ public final class TtnHabBridge {
|
|||
* @param config the application configuration
|
||||
*/
|
||||
private TtnHabBridge(ITtnHabBridgeConfig config) {
|
||||
this.ttnListener = new TtnListener(this::handleTTNMessage, config.getMqttServerUrl(), config.getMqttUserName(),
|
||||
config.getMqttPassword(), config.getMqttTopic());
|
||||
this.ttnListener = new TtnListener(this::handleTTNMessage,
|
||||
config.getTtnMqttUrl(), config.getTtnAppId(), config.getTtnAppKey());
|
||||
final IHabitatRestApi restApi =
|
||||
HabitatUploader.newRestClient(config.getHabitatUrl(), config.getHabitatTimeout());
|
||||
this.habUploader = new HabitatUploader(restApi);
|
||||
|
|
|
@ -23,11 +23,9 @@ public final class TtnHabBridgeConfig implements ITtnHabBridgeConfig {
|
|||
HABITAT_URL("habitat.url", "http://habitat.habhub.org", "URL of the habitat server"),
|
||||
HABITAT_TIMEOUT("habitat.timeout", "3000", "Timeout in milliseconds"),
|
||||
|
||||
MQTT_SERVER_URL("mqtt.serverurl", "tcp://eu.thethings.network", "URL of the TTN MQTT server"),
|
||||
MQTT_CLIENT_ID("mqtt.clientid", "ttnhabbridge", "MQTT client id"),
|
||||
MQTT_USER_NAME("mqtt.username", "ttnmapper", "TTN application name used as MQTT user name"),
|
||||
MQTT_USER_PASS("mqtt.password", "ttn-account-v2.Xc8BFRKeBK5nUhc9ikDcR-sbelgSMdHKnOQKMAiwpgI", "TTN application password"),
|
||||
MQTT_TOPIC("mqtt.topic", "ttnmapper/devices/+/up", "MQTT topic to subscribe to")
|
||||
TTN_MQTT_URL("ttn.mqtt.url", "tcp://eu.thethings.network", "URL of the TTN MQTT server"),
|
||||
TTN_APP_ID("ttn.app.id", "habhub", "TTN Application Id (e.g. habhub, ttnmapper, etc.)"),
|
||||
TTN_APP_KEY("ttn.app.key", "ttn-account-v2.Sh49WL90oQz-ZuxoDrS6yKuACL_jtAA0agdDfO_eVj4", "TTN Application key")
|
||||
;
|
||||
|
||||
private String key;
|
||||
|
@ -101,28 +99,18 @@ public final class TtnHabBridgeConfig implements ITtnHabBridgeConfig {
|
|||
}
|
||||
|
||||
@Override
|
||||
public String getMqttClientId() {
|
||||
return props.get(EConfigItem.MQTT_CLIENT_ID);
|
||||
public String getTtnMqttUrl() {
|
||||
return props.get(EConfigItem.TTN_MQTT_URL);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMqttUserName() {
|
||||
return props.get(EConfigItem.MQTT_USER_NAME);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMqttPassword() {
|
||||
return props.get(EConfigItem.MQTT_USER_PASS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMqttServerUrl() {
|
||||
return props.get(EConfigItem.MQTT_SERVER_URL);
|
||||
@Override
|
||||
public String getTtnAppId() {
|
||||
return props.get(EConfigItem.TTN_APP_ID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMqttTopic() {
|
||||
return props.get(EConfigItem.MQTT_TOPIC);
|
||||
public String getTtnAppKey() {
|
||||
return props.get(EConfigItem.TTN_APP_KEY);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -3,8 +3,6 @@ package nl.sikken.bertrik.hab.ttn;
|
|||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
|
||||
import org.eclipse.paho.client.mqttv3.MqttCallback;
|
||||
import org.eclipse.paho.client.mqttv3.MqttClient;
|
||||
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
||||
import org.eclipse.paho.client.mqttv3.MqttException;
|
||||
|
@ -23,8 +21,8 @@ public final class TtnListener {
|
|||
private final String clientId;
|
||||
private final IMessageReceived callback;
|
||||
private final String url;
|
||||
private final String userName;
|
||||
private final String password;
|
||||
private final String appId;
|
||||
private final String appKey;
|
||||
private final String topic;
|
||||
|
||||
private MqttClient mqttClient;
|
||||
|
@ -34,17 +32,16 @@ public final class TtnListener {
|
|||
*
|
||||
* @param receiveCallback the interface for indicating a received message.
|
||||
* @param url the URL of the MQTT server
|
||||
* @param userName the user name
|
||||
* @param password the password
|
||||
* @param topic the MQTT topic
|
||||
* @param appId the user name
|
||||
* @param appKey the password
|
||||
*/
|
||||
public TtnListener(IMessageReceived receiveCallback, String url, String userName, String password, String topic) {
|
||||
public TtnListener(IMessageReceived receiveCallback, String url, String appId, String appKey) {
|
||||
this.callback = receiveCallback;
|
||||
this.url = url;
|
||||
this.clientId = UUID.randomUUID().toString();
|
||||
this.userName = userName;
|
||||
this.password = password;
|
||||
this.topic = topic;
|
||||
this.appId = appId;
|
||||
this.appKey = appKey;
|
||||
this.topic = appId + "/devices/+/up";
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -56,39 +53,35 @@ public final class TtnListener {
|
|||
LOG.info("Starting TTN listener");
|
||||
|
||||
// connect
|
||||
LOG.info("Connecting as user '{}' to MQTT server {}", userName, url);
|
||||
LOG.info("Connecting as user '{}' to MQTT server {}", appId, url);
|
||||
this.mqttClient = new MqttClient(url, clientId);
|
||||
final MqttConnectOptions options = new MqttConnectOptions();
|
||||
options.setUserName(userName);
|
||||
options.setPassword(password.toCharArray());
|
||||
options.setUserName(appId);
|
||||
options.setPassword(appKey.toCharArray());
|
||||
options.setAutomaticReconnect(true);
|
||||
mqttClient.connect(options);
|
||||
|
||||
// subscribe
|
||||
LOG.info("Subscribing to topic '{}'", topic);
|
||||
mqttClient.setCallback(new MqttCallback() {
|
||||
@Override
|
||||
public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
|
||||
final String message = new String(mqttMessage.getPayload(), StandardCharsets.US_ASCII);
|
||||
LOG.info("Message arrived on topic {}: {}", topic, message);
|
||||
callback.messageReceived(topic, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deliveryComplete(IMqttDeliveryToken token) {
|
||||
// we don't care
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connectionLost(Throwable cause) {
|
||||
LOG.info("connectionLost: {}", cause.getMessage());
|
||||
}
|
||||
});
|
||||
mqttClient.subscribe(topic);
|
||||
mqttClient.subscribe(topic, this::messageArrived);
|
||||
|
||||
LOG.info("Started TTN listener");
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles an incoming message.
|
||||
*
|
||||
* @param topic the topic
|
||||
* @param mqttMessage the message
|
||||
* @throws Exception who knows?
|
||||
*/
|
||||
private void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
|
||||
final String message = new String(mqttMessage.getPayload(), StandardCharsets.US_ASCII);
|
||||
LOG.info("Message arrived on topic '{}': {}", topic, message);
|
||||
// forward it to our user
|
||||
callback.messageReceived(topic, message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops this module.
|
||||
*/
|
||||
|
@ -96,10 +89,16 @@ public final class TtnListener {
|
|||
LOG.info("Stopping TTN listener");
|
||||
try {
|
||||
mqttClient.disconnect(DISCONNECT_TIMEOUT_MS);
|
||||
mqttClient.close();
|
||||
} catch (MqttException e) {
|
||||
// don't care, just log
|
||||
LOG.warn("Caught exception while shutting down", e.getMessage());
|
||||
LOG.warn("Caught exception on disconnect: {}", e.getMessage());
|
||||
} finally {
|
||||
try {
|
||||
mqttClient.close();
|
||||
} catch (MqttException e) {
|
||||
// don't care, just log
|
||||
LOG.warn("Caught exception on close: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
LOG.info("Stopped TTN listener");
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package nl.sikken.bertrik;
|
|||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
@ -27,5 +28,18 @@ public final class TtnHabBridgeConfigTest {
|
|||
config.save(file);
|
||||
config.load(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that every getter returns a valid value.
|
||||
*/
|
||||
@Test
|
||||
public void testAllProps() {
|
||||
final TtnHabBridgeConfig config = new TtnHabBridgeConfig();
|
||||
Assert.assertNotNull(config.getHabitatUrl());
|
||||
Assert.assertNotNull(config.getHabitatTimeout());
|
||||
Assert.assertNotNull(config.getTtnMqttUrl());
|
||||
Assert.assertNotNull(config.getTtnAppId());
|
||||
Assert.assertNotNull(config.getTtnAppKey());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -5,17 +5,10 @@ habitat.url=http://habitat.habhub.org
|
|||
habitat.timeout=3000
|
||||
|
||||
# URL of the TTN MQTT server
|
||||
mqtt.serverurl=tcp://eu.thethings.network
|
||||
ttn.mqtt.url=tcp://eu.thethings.network
|
||||
|
||||
# MQTT client id
|
||||
mqtt.clientid=ttnhabbridge
|
||||
|
||||
# TTN application name used as MQTT user name
|
||||
mqtt.username=ttnmapper
|
||||
# TTN application id (used as MQTT user name)
|
||||
ttn.app.id=habhub
|
||||
|
||||
# TTN application password
|
||||
mqtt.password=ttn-account-v2.Xc8BFRKeBK5nUhc9ikDcR-sbelgSMdHKnOQKMAiwpgI
|
||||
|
||||
# MQTT topic to subscribe to
|
||||
mqtt.topic=ttnmapper/devices/+/up
|
||||
|
||||
ttn.app.key=ttn-account-v2.Sh49WL90oQz-ZuxoDrS6yKuACL_jtAA0agdDfO_eVj4
|
||||
|
|
Ładowanie…
Reference in New Issue