/** * Create a stream of JsonObject tuples from received JSON WebSocket text messages. * @return the stream */ public TStream<JsonObject> receive() { return receiveText(JsonFunctions.fromString()); }
/** * Send a stream's JsonObject tuples as JSON in a WebSocket text message. * @param stream the stream * @return sink */ public TSink<JsonObject> send(TStream<JsonObject> stream) { Objects.requireNonNull(stream, "stream"); return sendText(stream, JsonFunctions.asString()); }
private TStream<JsonObject> allCommands() { if (commandStream == null) { String topicFilter = commandTopic(null); commandStream = connector.subscribe(topicFilter, commandQoS, (topic, payload) -> { JsonObject jo = new JsonObject(); jo.addProperty(CMD_DEVICE, deviceId); jo.addProperty(CMD_ID, extractCmd(topic)); jo.addProperty(CMD_TS, System.currentTimeMillis()); String fmt = extractCmdFmt(topic); jo.addProperty(CMD_FORMAT, fmt); if ("json".equals(fmt)) { jo.add(CMD_PAYLOAD, JsonFunctions.fromBytes().apply(payload)); } else { jo.addProperty(CMD_PAYLOAD, new String(payload, StandardCharsets.UTF_8)); } return jo; }) .tag("allDeviceCmds"); } return commandStream; }
@Test public void testStrings() { JsonObject jo1 = newTestObject(); Function<JsonObject,String> asString = JsonFunctions.asString(); Function<String,JsonObject> fromString = JsonFunctions.fromString(); String s1 = asString.apply(jo1); JsonObject jo2 = fromString.apply(s1); assertEquals(jo2, jo1); }
/** * {@inheritDoc} * * <p>The event is published to the configured MQTT {@code mqttDevice.event.topic.pattern}, * as described in the above class documentation, substituting the value returned * by the {@code eventId} function for "{EVENTID}" in the pattern. * The MQTT message's payload is the JSON representation * of the JsonObject stream tuple. */ @Override public TSink<JsonObject> events(TStream<JsonObject> stream, Function<JsonObject, String> eventId, UnaryOperator<JsonObject> payload, Function<JsonObject, Integer> qos) { Function<JsonObject, String> topic = jo -> eventTopic(eventId.apply(jo)); Function<JsonObject,byte[]> payloadFn = jo -> JsonFunctions.asBytes().apply(payload.apply(jo)); return connector.publish(stream, topic, payloadFn, qos, jo -> retainEvents); }
@Test public void testValueOfBoolean() { JsonObject joTrue = JsonFunctions.valueOfBoolean("propName").apply(true); assertEquals(true, joTrue.get("propName").getAsBoolean()); JsonObject joFalse = JsonFunctions.valueOfBoolean("propName").apply(false); assertEquals(false, joFalse.get("propName").getAsBoolean()); }
@Test public void testValueOfString() { JsonObject jo = JsonFunctions.valueOfString("propName").apply("str1"); assertEquals("str1", jo.get("propName").getAsString()); }
@Test public void testUnpartitioned() { Function<JsonObject,JsonElement> unpartitionedFn = JsonFunctions.unpartitioned(); assertEquals(0, unpartitionedFn.apply(new JsonObject()).getAsInt()); }
@Test public void testValueOfNumber() { JsonObject joShort = JsonFunctions.valueOfNumber("propName").apply(Short.MAX_VALUE); assertEquals(Short.MAX_VALUE, joShort.get("propName").getAsShort()); JsonObject joInt = JsonFunctions.valueOfNumber("propName").apply(Integer.MAX_VALUE); assertEquals(Integer.MAX_VALUE, joInt.get("propName").getAsInt()); JsonObject joLong = JsonFunctions.valueOfNumber("propName").apply(Long.MAX_VALUE); assertEquals(Long.MAX_VALUE, joLong.get("propName").getAsLong()); JsonObject joFloat = JsonFunctions.valueOfNumber("propName").apply(Float.MAX_VALUE); assertEquals(Float.MAX_VALUE, joFloat.get("propName").getAsFloat(), 0.0f); JsonObject joDouble = JsonFunctions.valueOfNumber("propName").apply(Double.MAX_VALUE); assertEquals(Double.MAX_VALUE, joDouble.get("propName").getAsDouble(), 0.0d); }
@Test public void testValueOfCharacter() { JsonObject jo = JsonFunctions.valueOfCharacter("propName").apply('c'); assertEquals('c', jo.get("propName").getAsCharacter()); } }
@Test public void testJson() throws Exception { Topology t = newTopology("testJson"); System.out.println("===== "+t.getName()); startEchoer(); // before getConfig() so it gets the port Properties config = getConfig(); WebSocketClient wsClient = new Jsr356WebSocketClient(t, config); String[] expected = new String[] { "{\"id\":\"" + getStr1() + "\",\"value\":27}", "{\"id\":\"" + getStr2() + "\",\"value\":13}" }; TStream<JsonObject> s = t.strings(expected) .map(JsonFunctions.fromString()); s = PlumbingStreams.blockingOneShotDelay(s, 2, TimeUnit.SECONDS); wsClient.send(s); TStream<String> rcvd = wsClient.receive() .map(JsonFunctions.asString()); completeAndValidate("", t, rcvd, SEC_TMO, expected); }
val -> config.setUserName(val)); setConfig(p, "mqtt.will", val -> { JsonObject jo = JsonFunctions.fromString().apply(val); String topic = jo.get("topic").getAsString(); byte[] payload = jo.get("payload").getAsString().getBytes(StandardCharsets.UTF_8);