private void doPublish( String name, Object val, String src, String dpt, String textual, long updateTime, long lastChange) { JsonObject jso = new JsonObject(); jso.add("ts", updateTime).add("lc", lastChange).add("knx_src_addr", src).add("knx_dpt", dpt); if (textual != null) jso.add("knx_textual", textual); if (val instanceof Integer) jso.add("val", ((Integer) val).intValue()); else if (val instanceof Number) jso.add("val", ((Number) val).doubleValue()); else jso.add("val", val.toString()); String txtmsg = jso.toString(); MqttMessage msg = new MqttMessage(jso.toString().getBytes(StandardCharsets.UTF_8)); msg.setQos(0); msg.setRetained(true); try { String fullTopic = topicPrefix + "status/" + name; mqttc.publish(fullTopic, msg); L.finer("Published " + txtmsg + " to " + fullTopic); } catch (MqttException e) { L.log(Level.WARNING, "Error when publishing message " + txtmsg, e); } }
public void publishMQTT(String topic, byte[] data) { MqttMessage message = new MqttMessage(data); message.setQos(2); try { this.mqttClient.publish(topic, message); } catch (MqttException e) { getLogger().warn("Publish MQTT Topic", e); } }
private void testMessageWithOnlyFixedHeader(MqttMessageType messageType) throws Exception { MqttMessage message = createMessageWithFixedHeader(messageType); ByteBuf byteBuf = MqttEncoder.doEncode(ALLOCATOR, message); final List<Object> out = new LinkedList<>(); mqttDecoder.decode(ctx, byteBuf, out); assertEquals("Expected one object bout got " + out.size(), 1, out.size()); final MqttMessage decodedMessage = (MqttMessage) out.get(0); validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader()); }
/* (non-Javadoc) * @see org.eclipse.paho.client.mqttv3.IMqttAsyncClient#publish(java.lang.String, byte[], int, boolean, java.lang.Object, org.eclipse.paho.client.mqttv3.IMqttActionListener) */ public IMqttDeliveryToken publish( String topic, byte[] payload, int qos, boolean retained, Object userContext, IMqttActionListener callback) throws MqttException, MqttPersistenceException { MqttMessage message = new MqttMessage(payload); message.setQos(qos); message.setRetained(retained); return this.publish(topic, message, userContext, callback); }
@Override public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception { try { String msgText = mqttMessage.toString(); if (log.isDebugEnabled()) { log.debug(msgText); } PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId); if (log.isDebugEnabled()) { log.debug("Event received in MQTT Event Adapter - " + msgText); } if (contentValidator != null && contentTransformer != null) { ContentInfo contentInfo; Map<String, Object> dynamicProperties = new HashMap<>(); dynamicProperties.put(MQTTEventAdapterConstants.TOPIC, topic); msgText = (String) contentTransformer.transform(msgText, dynamicProperties); contentInfo = contentValidator.validate(msgText, dynamicProperties); if (contentInfo != null && contentInfo.isValidContent()) { eventAdapterListener.onEvent(contentInfo.getMessage()); } } else { eventAdapterListener.onEvent(msgText); } } finally { PrivilegedCarbonContext.endTenantFlow(); } }
@Override public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception { if (topic.equals("Likaci/MqttMap")) { String msg = new String(mqttMessage.getPayload()); JSONObject json = new JSONObject(msg); if (!json.getString("id").equals(id)) { Point p = (Point) GeometryEngine.project( new Point(json.getDouble("x"), json.getDouble("y")), SpatialReference.create(4326), SpatialReference.create(3857)); layer.removeAll(); SimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbol( Color.parseColor("#763382"), 15, SimpleMarkerSymbol.STYLE.DIAMOND); layer.addGraphic(new Graphic(p, markerSymbol)); TextSymbol textSymbol = new TextSymbol( 15, json.getString("id"), Color.parseColor("#763382"), TextSymbol.HorizontalAlignment.CENTER, TextSymbol.VerticalAlignment.MIDDLE); textSymbol.setOffsetY(-15); layer.addGraphic(new Graphic(p, textSymbol)); } } }
private void processSetGet(String namePart, MqttMessage msg, boolean set) { if (msg.isRetained()) { L.finer("Ignoring retained message " + msg + " to " + namePart); return; } // Now translate the topic into a group address GroupAddressInfo gai = GroupAddressManager.getGAInfoForName(namePart); if (gai == null) { L.warning( "Unable to translate name " + namePart + " into a group address, ignoring message " + msg); return; } L.fine("Name " + namePart + " translates to GA " + gai.address); String data = new String(msg.getPayload(), StandardCharsets.UTF_8); if (set) KNXConnector.doGroupWrite(gai.address, data, gai); else KNXConnector.doGroupRead(gai.address, data, gai); }
@Override public void messageArrived(String arg0, MqttMessage arg1) throws Exception { log.info("MQTT Message received: " + arg1.toString()); }