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));
}
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());
}
}
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;
}
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);
}
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"));
}
Aggregations