use of org.eclipse.paho.client.mqttv3.MqttClient in project camel by apache.
the class PahoEndpoint method doStart.
@Override
protected void doStart() throws Exception {
super.doStart();
client = new MqttClient(getBrokerUrl(), getClientId(), resolvePersistence());
client.connect(resolveMqttConnectOptions());
}
use of org.eclipse.paho.client.mqttv3.MqttClient in project camel by apache.
the class PahoProducer method process.
@Override
public void process(Exchange exchange) throws Exception {
MqttClient client = getEndpoint().getClient();
String topic = getEndpoint().getTopic();
int qos = exchange.getIn().getHeader(PahoConstants.CAMEL_PAHO_MSG_QOS, getEndpoint().getQos(), Integer.class);
boolean retained = exchange.getIn().getHeader(PahoConstants.CAMEL_PAHO_MSG_RETAINED, getEndpoint().isRetained(), Boolean.class);
byte[] payload = exchange.getIn().getBody(byte[].class);
MqttMessage message = new MqttMessage(payload);
message.setQos(qos);
message.setRetained(retained);
client.publish(topic, message);
}
use of org.eclipse.paho.client.mqttv3.MqttClient in project SEPA by arces-wot.
the class MQTTWebThing method start.
public boolean start() {
try {
mqttClient = new MqttClient(serverURI, clientID);
} catch (MqttException e) {
logger.fatal("Failed to create MQTT client " + e.getMessage());
return created;
}
try {
MqttConnectOptions options = new MqttConnectOptions();
if (sslEnabled) {
SSLSecurityManager sm;
try {
sm = new SSLSecurityManager("TLSv1", "sepa.jks", "sepa2017", "sepa2017");
} catch (UnrecoverableKeyException | KeyManagementException | KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e) {
logger.error(e.getMessage());
return false;
}
try {
options.setSocketFactory(sm.getSSLContext().getSocketFactory());
} catch (KeyManagementException | NoSuchAlgorithmException e) {
logger.error(e.getMessage());
return false;
}
}
mqttClient.connect(options);
} catch (MqttException e) {
logger.fatal(e.getMessage());
return created;
}
mqttClient.setCallback(this);
try {
mqttClient.subscribe(topicsFilter);
} catch (MqttException e) {
logger.fatal("Failed to subscribe " + e.getMessage());
return created;
}
String topics = "";
for (int i = 0; i < topicsFilter.length; i++) topics += "\"" + topicsFilter[i] + "\" ";
logger.info("MQTT client " + clientID + " subscribed to " + serverURI + " Topic filter " + topics);
created = true;
return created;
}
use of org.eclipse.paho.client.mqttv3.MqttClient in project SEPA by arces-wot.
the class MQTTSmartifier method start.
public boolean start(boolean simulate) {
// Subscribe to observation-topic mapping
Response ret = subscribe(null);
if (ret.isError()) {
logger.fatal("Failed to subscribe: " + ret);
return false;
}
SubscribeResponse results = (SubscribeResponse) ret;
onAddedResults(results.getBindingsResults());
if (simulate)
simulator();
else {
// MQTT: begin
JsonObject mqtt = getApplicationProfile().getExtendedData().get("mqtt").getAsJsonObject();
String url = mqtt.get("url").getAsString();
int port = mqtt.get("port").getAsInt();
JsonArray topics = mqtt.get("topics").getAsJsonArray();
topicsFilter = new String[topics.size()];
int i = 0;
for (JsonElement topic : topics) {
topicsFilter[i] = topic.getAsString();
i++;
}
boolean sslEnabled = false;
if (mqtt.get("ssl") != null)
sslEnabled = mqtt.get("ssl").getAsBoolean();
String serverURI = null;
if (sslEnabled) {
serverURI = "ssl://" + url + ":" + String.format("%d", port);
} else {
serverURI = "tcp://" + url + ":" + String.format("%d", port);
}
// Create client
logger.info("Creating MQTT client...");
String clientID = MqttClient.generateClientId();
logger.info("Client ID: " + clientID);
logger.info("Server URI: " + serverURI);
try {
mqttClient = new MqttClient(serverURI, clientID);
} catch (MqttException e) {
logger.error(e.getMessage());
return false;
}
// Connect
logger.info("Connecting...");
MqttConnectOptions options = new MqttConnectOptions();
if (sslEnabled) {
SSLSecurityManager sm;
try {
sm = new SSLSecurityManager("TLSv1", "sepa.jks", "sepa2017", "sepa2017");
} catch (UnrecoverableKeyException | KeyManagementException | KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e) {
logger.error(e.getMessage());
return false;
}
logger.info("Set SSL security");
try {
options.setSocketFactory(sm.getSSLContext().getSocketFactory());
} catch (KeyManagementException | NoSuchAlgorithmException e) {
logger.error(e.getMessage());
return false;
}
}
try {
mqttClient.connect(options);
} catch (MqttException e) {
logger.error(e.getMessage());
}
// Subscribe
mqttClient.setCallback(this);
logger.info("Subscribing...");
try {
mqttClient.subscribe(topicsFilter);
} catch (MqttException e) {
logger.error(e.getMessage());
return false;
}
for (String topic : topicsFilter) logger.info("MQTT client " + clientID + " subscribed to " + serverURI + " Topic filter " + topic);
// MQTT: end
}
return true;
}
use of org.eclipse.paho.client.mqttv3.MqttClient in project SEPA by arces-wot.
the class MQTTAdapter method start.
public boolean start() {
// MQTT
JsonObject mqtt = getApplicationProfile().getExtendedData().get("mqtt").getAsJsonObject();
String url = mqtt.get("url").getAsString();
int port = mqtt.get("port").getAsInt();
JsonArray topics = mqtt.get("topics").getAsJsonArray();
topicsFilter = new String[topics.size()];
int i = 0;
for (JsonElement topic : topics) {
topicsFilter[i] = topic.getAsString();
i++;
}
boolean sslEnabled = false;
if (mqtt.get("ssl") != null)
sslEnabled = mqtt.get("ssl").getAsBoolean();
if (sslEnabled) {
serverURI = "ssl://" + url + ":" + String.format("%d", port);
} else {
serverURI = "tcp://" + url + ":" + String.format("%d", port);
}
// Create client
logger.info("Creating MQTT client...");
String clientID = MqttClient.generateClientId();
logger.info("Client ID: " + clientID);
logger.info("Server URI: " + serverURI);
try {
mqttClient = new MqttClient(serverURI, clientID);
} catch (MqttException e) {
logger.error(e.getMessage());
return false;
}
// Connect
logger.info("Connecting...");
MqttConnectOptions options = new MqttConnectOptions();
if (sslEnabled) {
SSLSecurityManager sm;
try {
sm = new SSLSecurityManager("TLSv1", "sepa.jks", "sepa2017", "sepa2017");
} catch (UnrecoverableKeyException | KeyManagementException | KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e) {
logger.error(e.getMessage());
return false;
}
logger.info("Set SSL security");
try {
options.setSocketFactory(sm.getSSLContext().getSocketFactory());
} catch (KeyManagementException | NoSuchAlgorithmException e) {
logger.error(e.getMessage());
return false;
}
}
try {
mqttClient.connect(options);
} catch (MqttException e) {
logger.error(e.getMessage());
}
// Subscribe
mqttClient.setCallback(this);
logger.info("Subscribing...");
try {
mqttClient.subscribe(topicsFilter);
} catch (MqttException e) {
logger.error(e.getMessage());
return false;
}
String printTopics = "Topic filter ";
for (String s : topicsFilter) {
printTopics += s + " ";
}
logger.info("MQTT client " + clientID + " subscribed to " + serverURI + printTopics);
return true;
}
Aggregations