本文整理汇总了Java中org.eclipse.paho.client.mqttv3.MqttMessage.getPayload方法的典型用法代码示例。如果您正苦于以下问题:Java MqttMessage.getPayload方法的具体用法?Java MqttMessage.getPayload怎么用?Java MqttMessage.getPayload使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.eclipse.paho.client.mqttv3.MqttMessage
的用法示例。
在下文中一共展示了MqttMessage.getPayload方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: messageArrived
import org.eclipse.paho.client.mqttv3.MqttMessage; //导入方法依赖的package包/类
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
String payload = new String(message.getPayload());
System.out.println("Red'c command: " + payload);
JsonObject jsonObject = parser.parse(payload).getAsJsonObject();
String cmd = extractCommandData(jsonObject, CMD_KEY);
switch (cmd) {
case "ping":
sendResponse(pingResponse(jsonObject));
break;
case "randnum":
sendResponse(randResponse(jsonObject));
break;
default:
sendResponse(payload);
}
msgRecd = true;
}
示例2: convert
import org.eclipse.paho.client.mqttv3.MqttMessage; //导入方法依赖的package包/类
@Override
public List<DeviceData> convert(String topic, MqttMessage msg) throws Exception {
String data = new String(msg.getPayload(), StandardCharsets.UTF_8);
log.trace("Parsing json message: {}", data);
if (!filterExpression.isEmpty()) {
try {
log.debug("Data before filtering {}", data);
DocumentContext document = JsonPath.parse(data);
document = JsonPath.parse((Object) document.read(filterExpression));
data = document.jsonString();
log.debug("Data after filtering {}", data);
} catch (RuntimeException e) {
log.debug("Failed to apply filter expression: {}", filterExpression);
throw new RuntimeException("Failed to apply filter expression " + filterExpression);
}
}
JsonNode node = mapper.readTree(data);
List<String> srcList;
if (node.isArray()) {
srcList = new ArrayList<>(node.size());
for (int i = 0; i < node.size(); i++) {
srcList.add(mapper.writeValueAsString(node.get(i)));
}
} else {
srcList = Collections.singletonList(data);
}
return parse(topic, srcList);
}
示例3: convert
import org.eclipse.paho.client.mqttv3.MqttMessage; //导入方法依赖的package包/类
public AttributeRequest convert(String topic, MqttMessage msg) {
deviceNameTopicPattern = checkAndCompile(deviceNameTopicPattern, deviceNameTopicExpression);
attributeKeyTopicPattern = checkAndCompile(attributeKeyTopicPattern, attributeKeyTopicExpression);
requestIdTopicPattern = checkAndCompile(requestIdTopicPattern, requestIdTopicExpression);
String data = new String(msg.getPayload(), StandardCharsets.UTF_8);
DocumentContext document = JsonPath.parse(data);
AttributeRequest.AttributeRequestBuilder builder = AttributeRequest.builder();
builder.deviceName(eval(topic, deviceNameTopicPattern, document, deviceNameJsonExpression));
builder.attributeKey(eval(topic, attributeKeyTopicPattern, document, attributeKeyJsonExpression));
builder.requestId(Integer.parseInt(eval(topic, requestIdTopicPattern, document, requestIdJsonExpression)));
builder.clientScope(this.isClientScope());
builder.topicExpression(this.getResponseTopicExpression());
builder.valueExpression(this.getValueExpression());
return builder.build();
}
示例4: convert
import org.eclipse.paho.client.mqttv3.MqttMessage; //导入方法依赖的package包/类
public AttributeRequest convert(String topic, MqttMessage msg) {
deviceNameTopicPattern = checkAndCompile(deviceNameTopicPattern, deviceNameTopicExpression);
attributeKeyTopicPattern = checkAndCompile(attributeKeyTopicPattern, attributeKeyTopicExpression);
requestIdTopicPattern = checkAndCompile(requestIdTopicPattern, requestIdTopicExpression);
String data = new String(msg.getPayload(), StandardCharsets.UTF_8);
DocumentContext document = JsonPath.parse(data);
AttributeRequest.AttributeRequestBuilder builder = AttributeRequest.builder();
builder.deviceName(eval(topic, deviceNameTopicPattern, document, deviceNameJsonExpression));
builder.attributeKey(eval(topic, attributeKeyTopicPattern, document, attributeKeyJsonExpression));
builder.requestId(Integer.parseInt(eval(topic, requestIdTopicPattern, document, requestIdJsonExpression)));
builder.clientScope(this.isClientScope());
builder.topicExpression(this.getResponseTopicExpression());
builder.valueExpression(this.getValueExpression());
return builder.build();
}
示例5: messageArrived
import org.eclipse.paho.client.mqttv3.MqttMessage; //导入方法依赖的package包/类
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
String body = new String(message.getPayload());
log.info("Topic: {}, Message: {}", topic, body);
try {
String eventName = topic.substring(topic.lastIndexOf('/') + 1);
Class<?> cls = epService.getEPAdministrator()
.getConfiguration()
.getEventType(eventName)
.getUnderlyingType();
Object event = mapper.readValue(body, cls);
epService.getEPRuntime().sendEvent(event);
} catch (Exception e) {
log.error("Can not resolve Mqtt message", e);
}
}
示例6: messageArrived
import org.eclipse.paho.client.mqttv3.MqttMessage; //导入方法依赖的package包/类
@Override
public void messageArrived(String s, MqttMessage mqttMessage) throws BadPayloadException {
String payload = new String(mqttMessage.getPayload());
try {
JSONObject obj = new JSONObject(payload);
String id = null;
if (obj.has("id")) id = obj.getString("id");
else
throw new BadPayloadException("Missing id"); //Extract node id from the JSON message (/!\ Id = id registered in the targeted nodes' index)
long time = -1;
if (obj.has("time")) time = obj.getLong("time");
else throw new BadPayloadException("Missing id"); //Extract time from the JSON message
if (obj.has("values")) {
JSONObject values = obj.getJSONObject("values");
long finalTime = time;
String finalId = id;
values.keySet().forEach(attributeName -> { // For each attribute stored in the JSON message, update the node's attributes
JSONObject attribute = values.getJSONObject(attributeName);
newTask().travelInTime(Long.toString(finalTime))
.readIndex(lookupIndex, finalId)
.then(CoreActions.forceAttribute(attributeName, Type.typeFromName(attribute.getString("type")), attribute.getString("value")))
.execute(super.getGraph(), null);
});
super.getGraph().save(null);
} else throw new BadPayloadException("Missing values");
} catch (JSONException jsonException) {
jsonException.printStackTrace();
}
}
示例7: messageArrived
import org.eclipse.paho.client.mqttv3.MqttMessage; //导入方法依赖的package包/类
@Override
public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
String content = new String(mqttMessage.getPayload());
String[] elements = content.split(";");
if (elements.length != 3)
throw new RuntimeException("Unparsable message received: " + content);
String id = null;
long time = -1L;
double value = 0;
try {
id = elements[0];
value = Double.parseDouble(elements[1]);
time = Long.parseLong(elements[2]);
} catch (NumberFormatException e) {
throw new RuntimeException("Unparsable message received: " + content);
}
newTask().travelInTime(Long.toString(time))
.readIndex(lookupIndex, id)
.then(CoreActions.forceAttribute("value", Type.typeFromName("DOUBLE"), Double.toString(value)))
.execute(super.getGraph(), null);
super.getGraph().save(null);
}
示例8: EmqMessage
import org.eclipse.paho.client.mqttv3.MqttMessage; //导入方法依赖的package包/类
public EmqMessage(String topic, MqttMessage mqttMessage) {
this.topic = topic;
this.mqttMessage = mqttMessage;
this.updateTime = StringUtil.formatNow();
if (mqttMessage != null) {
this.dup = mqttMessage.isDuplicate();
this.payload = new String(mqttMessage.getPayload());
this.qos = mqttMessage.getQos();
this.messageId = mqttMessage.getId();
this.retained = mqttMessage.isRetained();
}
}
示例9: processMqttMessage
import org.eclipse.paho.client.mqttv3.MqttMessage; //导入方法依赖的package包/类
@Override
public boolean processMqttMessage(String mqttTopic, String mqttTopicSubscribe, MqttMessage mqttMessage) throws Exception {
serialMessage = "";
/*
* Get rid of subscribed topic prefix
*/
// Handle possible topic wildcards
String mqttTopicPrefix = mqttTopicSubscribe.replaceAll("/#", "");
mqttTopicPrefix = mqttTopicPrefix.replaceAll("/+", "");
mqttTopic = mqttTopic.substring(mqttTopicPrefix.length());
if (mqttTopic.startsWith("/")) {
mqttTopic = mqttTopic.substring(1);
}
mqttTopic = mqttTopic.replaceAll("/", ";");
serialMessage = mqttTopic;
// Append payload if there is any
if (mqttMessage.getPayload().length > 0) {
serialMessage += ";" + new String(mqttMessage.getPayload());
}
return true;
}
示例10: MqttData
import org.eclipse.paho.client.mqttv3.MqttMessage; //导入方法依赖的package包/类
public MqttData ( final MqttMessage msg )
{
super ( msg.getPayload (), null );
this.message = msg;
}
示例11: messageArrived
import org.eclipse.paho.client.mqttv3.MqttMessage; //导入方法依赖的package包/类
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
// A message has arrived from the MQTT broker
// The MQTT broker doesn't send back
// an acknowledgment to the server until
// this method returns cleanly
if (!topic.equals("commands/leds/led01")) {
return;
}
String messageText =
new String(message.getPayload(), "UTF-8");
System.out.println(
String.format("%s received %s: %s",
"led01",
topic,
messageText));
// String[] keyValue =
// messageText.split(COMMAND_SEPARATOR);
// if (keyValue.length != 3) {
// return;
// }
// if (keyValue[0].equals(COMMAND_KEY) &&
// keyValue[1].equals(
// GET_ALTITUDE_COMMAND_KEY) &&
// keyValue[2].equals(name)) {
// // Process the get altitude command
// int altitudeInFeet = ThreadLocalRandom
// .current().nextInt(1, 6001);
// System.out.println(
// String.format("%s altitude: %d feet",
// name,
// altitudeInFeet));
// }
}
开发者ID:PacktPublishing,项目名称:MQTT-Essentials-A-Lightweight-IoT-Protocol,代码行数:35,代码来源:SubscriberCallback.java
示例12: messageArrived
import org.eclipse.paho.client.mqttv3.MqttMessage; //导入方法依赖的package包/类
@Override
public void messageArrived(String topic, MqttMessage msg) throws Exception {
try {
if (!StringUtils.isEmpty(mapping.getDeviceNameTopicExpression())) {
deviceNameConsumer.accept(eval(topic));
} else {
String data = new String(msg.getPayload(), StandardCharsets.UTF_8);
DocumentContext document = JsonPath.parse(data);
deviceNameConsumer.accept(eval(document, mapping.getDeviceNameJsonExpression()));
}
} catch (Exception e) {
log.error("Failed to convert msg", e);
}
}
示例13: convert
import org.eclipse.paho.client.mqttv3.MqttMessage; //导入方法依赖的package包/类
@Override
public List<DeviceData> convert(String topic, MqttMessage msg) throws Exception {
String data = new String(msg.getPayload(), StandardCharsets.UTF_8);
log.trace("Parsing json message: {}", data);
if (!filterExpression.isEmpty()) {
try {
log.debug("Data before filtering {}", data);
DocumentContext document = JsonPath.parse(data);
document = JsonPath.parse((Object) document.read(filterExpression));
data = document.jsonString();
log.debug("Data after filtering {}", data);
} catch (RuntimeException e) {
log.debug("Failed to apply filter expression: {}", filterExpression);
throw new RuntimeException("Failed to apply filter expression " + filterExpression);
}
}
JsonNode node = mapper.readTree(data);
List<String> srcList;
if (node.isArray()) {
srcList = new ArrayList<>(node.size());
for (int i = 0; i < node.size(); i++) {
srcList.add(mapper.writeValueAsString(node.get(i)));
}
} else {
srcList = Collections.singletonList(data);
}
return parse(topic, srcList);
}
示例14: processMessage
import org.eclipse.paho.client.mqttv3.MqttMessage; //导入方法依赖的package包/类
/*********************************************************************************************************************************************************************
*
*/
public void processMessage(String topic, MqttMessage message) {
if (!serialMqttBridge.isInitialized()) {
return;
}
/*
* If defined: Log message content
*/
if (serialMqttBridge.getConfigHandler().logMqttInbound()) {
logger.info("MQTT/IN: " + topic + " | " + new String(message.getPayload()));
}
String serialMessage = "";
/*
* If defined: Do serial message preprocessing before asking SerialHandler to send message
*/
SerialSendPreprocessingPlugin serialSendPreprocessor = serialMqttBridge.getSerialSendPreprocessor();
if (serialSendPreprocessor != null) {
try {
serialSendPreprocessor.processMqttMessage(topic, serialMqttBridge.getConfigHandler().getMqttTopicSubscribe(), message);
serialMessage = serialSendPreprocessor.getSerialMessage();
}
catch (Exception e) {
logger.error("Exception", e);
}
}
/*
* Otherwise process serial message directly
*/
else {
serialMessage = new String(message.getPayload());
}
/*
* Ask serial handler to send message
*/
serialMqttBridge.getSerialHandler().sendMessage(serialMessage);
}
示例15: messageArrived
import org.eclipse.paho.client.mqttv3.MqttMessage; //导入方法依赖的package包/类
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
String messageText = new String(message.getPayload(), encoding);
System.out.println(
String.format("Topic: %s. Payload: %s",
topic,
messageText));
// A message has arrived from the MQTT broker
// The MQTT broker doesn't send back
// an acknowledgment to the server until
// this method returns cleanly
if (!topic.startsWith(boardCommandsTopic)) {
// The topic for the arrived message doesn't start with boardTopic
return;
}
final boolean isTurnOnMessage = messageText.equals("TURN ON");
final boolean isTurnOffMessage = messageText.equals("TURN OFF");
boolean isInvalidCommand = false;
boolean isInvalidTopic = false;
// Extract the sensor name from the topic
String sensorName = topic.replaceFirst(boardCommandsTopic, "").replaceFirst(TOPIC_SEPARATOR, "");
switch (sensorName) {
case SENSOR_SUNLIGHT:
if (isTurnOnMessage) {
isSunlightSensorTurnedOn = true;
} else if (isTurnOffMessage) {
isSunlightSensorTurnedOn = false;
} else {
isInvalidCommand = true;
}
break;
case SENSOR_EARTH_HUMIDITY:
if (isTurnOnMessage) {
isEarthHumiditySensorTurnedOn = true;
} else if (isTurnOffMessage) {
isEarthHumiditySensorTurnedOn = false;
} else {
isInvalidCommand = true;
}
break;
default:
isInvalidTopic = true;
}
if (!isInvalidCommand && !isInvalidTopic) {
publishProcessedCommandMessage(sensorName, messageText);
}
}
开发者ID:PacktPublishing,项目名称:MQTT-Essentials-A-Lightweight-IoT-Protocol,代码行数:48,代码来源:SensorsManager.java