Search in sources :

Example 56 with MqttMessage

use of org.eclipse.paho.client.mqttv3.MqttMessage in project mist by snuspl.

the class ContinuousStreamTest method testMqttSink.

/**
 * Test for Mqtt sink.
 */
@Test
public void testMqttSink() throws InjectionException {
    final MISTStream<MqttMessage> sink = filteredMappedStream.mqttOutput(TestParameters.HOST, TestParameters.TOPIC);
    final Map<String, String> conf = sink.getConfiguration();
    Assert.assertEquals(TestParameters.HOST, conf.get(ConfKeys.MqttSink.MQTT_SINK_BROKER_URI.name()));
    Assert.assertEquals(TestParameters.TOPIC, conf.get(ConfKeys.MqttSink.MQTT_SINK_TOPIC.name()));
    // Check src -> sink
    final DAG<MISTStream, MISTEdge> dag = queryBuilder.build().getDAG();
    final Map<MISTStream, MISTEdge> neighbors = dag.getEdges(filteredMappedStream);
    Assert.assertEquals(1, neighbors.size());
    Assert.assertEquals(new MISTEdge(Direction.LEFT), neighbors.get(sink));
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) MISTEdge(edu.snu.mist.common.graph.MISTEdge) Test(org.junit.Test)

Example 57 with MqttMessage

use of org.eclipse.paho.client.mqttv3.MqttMessage in project mist by snuspl.

the class CepUtils method cepActionTranslator.

/**
 * Add output of cep action to cep qualifier.
 * @param qualifierFilterStream
 * @param cepAction             cep action
 */
private static <T> void cepActionTranslator(final ContinuousStream<Map<String, List<T>>> qualifierFilterStream, final CepAction cepAction) {
    final CepSink sink = cepAction.getCepSink();
    final String separator = sink.getSeparator();
    final List<Object> params = cepAction.getParams();
    final StringBuilder outputBuilder = new StringBuilder();
    for (final Object iterParam : params) {
        outputBuilder.append(iterParam.toString());
        outputBuilder.append(separator);
    }
    if (outputBuilder.length() == 0) {
        throw new NullPointerException("No Parameters for cep sink!");
    }
    outputBuilder.delete(outputBuilder.length() - separator.length(), outputBuilder.length());
    switch(sink.getCepSinkType()) {
        case TEXT_SOCKET_OUTPUT:
            {
                qualifierFilterStream.map(s -> outputBuilder.toString()).textSocketOutput((String) sink.getSinkConfigs().get("SOCKET_SINK_ADDRESS"), (int) sink.getSinkConfigs().get("SOCKET_SINK_PORT"));
                break;
            }
        case MQTT_OUTPUT:
            {
                qualifierFilterStream.map(s -> new MqttMessage(outputBuilder.toString().getBytes())).mqttOutput((String) sink.getSinkConfigs().get("MQTT_SINK_BROKER_URI"), (String) sink.getSinkConfigs().get("MQTT_SINK_TOPIC"));
            }
        default:
            throw new NotImplementedException("TEXT_SOCKET_OUTPUT and MQTT_OUTPUT are supported now!: " + sink.getCepSinkType().toString());
    }
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) NotImplementedException(org.apache.commons.lang.NotImplementedException)

Example 58 with MqttMessage

use of org.eclipse.paho.client.mqttv3.MqttMessage in project mist by snuspl.

the class ContinuousStreamImpl method mqttOutput.

@Override
public MISTStream<MqttMessage> mqttOutput(final String brokerURI, final String topic) {
    final Map<String, String> confMap = new HashMap<>();
    confMap.put(ConfKeys.SinkConf.SINK_TYPE.name(), ConfValues.SinkType.MQTT.name());
    confMap.put(ConfKeys.MqttSink.MQTT_SINK_BROKER_URI.name(), brokerURI);
    confMap.put(ConfKeys.MqttSink.MQTT_SINK_TOPIC.name(), topic);
    final MISTStream<MqttMessage> sink = new MISTStreamImpl<>(dag, confMap);
    dag.addVertex(sink);
    dag.addEdge(this, sink, new MISTEdge(Direction.LEFT));
    return sink;
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) MISTEdge(edu.snu.mist.common.graph.MISTEdge)

Example 59 with MqttMessage

use of org.eclipse.paho.client.mqttv3.MqttMessage in project mist by snuspl.

the class MQTTNoiseSensing method submitQuery.

/**
 * Submit a query fetching data from a MQTT source.
 * The query reads strings from a MQTT topic and send them to a sink.
 * @return result of the submission
 * @throws IOException
 * @throws InjectionException
 */
public static APIQueryControlResult submitQuery(final Configuration configuration) throws IOException, InjectionException, URISyntaxException {
    final String brokerURI = Tang.Factory.getTang().newInjector(configuration).getNamedInstance(TestMQTTBrokerURI.class);
    final SourceConfiguration localMQTTSourceConf = MISTExampleUtils.getMQTTSourceConf("MISTExampleSub", brokerURI);
    final MISTQueryBuilder queryBuilder = new MISTQueryBuilder();
    final ContinuousStream<Integer> sensedData = queryBuilder.mqttStream(localMQTTSourceConf).map((mqttMessage) -> Integer.parseInt(new String(mqttMessage.getPayload())));
    final ContinuousStream<Integer> noisy = sensedData.filter((value) -> value < 200);
    final ContinuousStream<Integer> calm = sensedData.filter((value) -> value >= 200);
    // Text socket output
    noisy.map((loudValue) -> new StringBuilder().append("It's noisy! The value was ").append(loudValue).toString()).textSocketOutput(MISTExampleUtils.SINK_HOSTNAME, MISTExampleUtils.SINK_PORT);
    // MQTT output
    noisy.map((value) -> new MqttMessage("ON".getBytes())).mqttOutput(brokerURI, "MISTExamplePub");
    calm.map((value) -> new MqttMessage("OFF".getBytes())).mqttOutput(brokerURI, "MISTExamplePub");
    return MISTExampleUtils.submit(queryBuilder, configuration);
}
Also used : MISTQueryBuilder(edu.snu.mist.client.MISTQueryBuilder) Tang(org.apache.reef.tang.Tang) MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) SourceConfiguration(edu.snu.mist.client.datastreams.configurations.SourceConfiguration) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) CommandLine(org.apache.reef.tang.formats.CommandLine) ContinuousStream(edu.snu.mist.client.datastreams.ContinuousStream) APIQueryControlResult(edu.snu.mist.client.APIQueryControlResult) TestMQTTBrokerURI(edu.snu.mist.examples.parameters.TestMQTTBrokerURI) MISTQueryBuilder(edu.snu.mist.client.MISTQueryBuilder) Configuration(org.apache.reef.tang.Configuration) JavaConfigurationBuilder(org.apache.reef.tang.JavaConfigurationBuilder) InjectionException(org.apache.reef.tang.exceptions.InjectionException) MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) SourceConfiguration(edu.snu.mist.client.datastreams.configurations.SourceConfiguration)

Example 60 with MqttMessage

use of org.eclipse.paho.client.mqttv3.MqttMessage in project thingsboard by thingsboard.

the class AbstractMqttTelemetryIntegrationTest method testPushMqttRpcData.

@Test
public void testPushMqttRpcData() throws Exception {
    String clientId = MqttAsyncClient.generateClientId();
    MqttAsyncClient client = new MqttAsyncClient(MQTT_URL, clientId);
    MqttConnectOptions options = new MqttConnectOptions();
    options.setUserName(accessToken);
    client.connect(options);
    Thread.sleep(3000);
    MqttMessage message = new MqttMessage();
    message.setPayload("{\"key1\":\"value1\", \"key2\":true, \"key3\": 3.0, \"key4\": 4}".getBytes());
    client.publish("v1/devices/me/telemetry", message);
    String deviceId = savedDevice.getId().getId().toString();
    Thread.sleep(1000);
    List<String> actualKeys = doGetAsync("/api/plugins/telemetry/DEVICE/" + deviceId + "/keys/timeseries", List.class);
    Set<String> actualKeySet = new HashSet<>(actualKeys);
    List<String> expectedKeys = Arrays.asList("key1", "key2", "key3", "key4");
    Set<String> expectedKeySet = new HashSet<>(expectedKeys);
    assertEquals(expectedKeySet, actualKeySet);
    String getTelemetryValuesUrl = "/api/plugins/telemetry/DEVICE/" + deviceId + "/values/timeseries?keys=" + String.join(",", actualKeySet);
    Map<String, List<Map<String, String>>> values = doGetAsync(getTelemetryValuesUrl, Map.class);
    assertEquals("value1", values.get("key1").get(0).get("value"));
    assertEquals("true", values.get("key2").get(0).get("value"));
    assertEquals("3.0", values.get("key3").get(0).get("value"));
    assertEquals("4", values.get("key4").get(0).get("value"));
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) MqttConnectOptions(org.eclipse.paho.client.mqttv3.MqttConnectOptions) MqttAsyncClient(org.eclipse.paho.client.mqttv3.MqttAsyncClient) AbstractControllerTest(org.thingsboard.server.controller.AbstractControllerTest) Test(org.junit.Test) DaoNoSqlTest(org.thingsboard.server.dao.service.DaoNoSqlTest)

Aggregations

MqttMessage (org.eclipse.paho.client.mqttv3.MqttMessage)64 MqttException (org.eclipse.paho.client.mqttv3.MqttException)28 MqttClient (org.eclipse.paho.client.mqttv3.MqttClient)19 IMqttDeliveryToken (org.eclipse.paho.client.mqttv3.IMqttDeliveryToken)16 MemoryPersistence (org.eclipse.paho.client.mqttv3.persist.MemoryPersistence)16 MqttConnectOptions (org.eclipse.paho.client.mqttv3.MqttConnectOptions)15 Test (org.junit.Test)14 MqttCallback (org.eclipse.paho.client.mqttv3.MqttCallback)8 CountDownLatch (java.util.concurrent.CountDownLatch)4 JsonParser (com.google.gson.JsonParser)3 IOException (java.io.IOException)3 MqttAsyncClient (org.eclipse.paho.client.mqttv3.MqttAsyncClient)3 JSONObject (org.json.JSONObject)3 Test (org.testng.annotations.Test)3 JsonArray (com.google.gson.JsonArray)2 JsonObject (com.google.gson.JsonObject)2 MISTEdge (edu.snu.mist.common.graph.MISTEdge)2 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 CamelContext (org.apache.camel.CamelContext)2