From f993e40e929d72dc3a1e5582e109986d19171e07 Mon Sep 17 00:00:00 2001 From: Hasan Eroglu Date: Tue, 9 May 2023 20:30:44 +0200 Subject: [PATCH 1/5] fix(binding-mqtt): fix sending plain value instead of content object Closes #979 Signed-off-by: Hasan Eroglu --- packages/binding-mqtt/src/mqtt-broker-server.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/binding-mqtt/src/mqtt-broker-server.ts b/packages/binding-mqtt/src/mqtt-broker-server.ts index 1bff40b62..77e9ae832 100644 --- a/packages/binding-mqtt/src/mqtt-broker-server.ts +++ b/packages/binding-mqtt/src/mqtt-broker-server.ts @@ -36,6 +36,7 @@ import { } from "@node-wot/core"; import { InteractionOptions } from "wot-typescript-definitions"; import { ActionElement, PropertyElement } from "wot-thing-description-types"; +import { Readable } from "stream"; const { info, debug, error, warn } = createLoggers("binding-mqtt", "mqtt-broker-server"); @@ -235,6 +236,7 @@ export default class MqttBrokerServer implements ProtocolServer { * For further discussion see https://github.com/eclipse/thingweb.node-wot/pull/253 */ let value; + if ("properties" in packet && "contentType" in packet.properties) { try { value = ContentSerdes.get().contentToValue( @@ -266,7 +268,11 @@ export default class MqttBrokerServer implements ProtocolServer { }; thing - .handleInvokeAction(segments[3], value, options) + .handleInvokeAction( + segments[3], + new Content(ContentSerdes.DEFAULT, Readable.from(value)), + options + ) .then((output: unknown) => { if (output) { warn(`MqttBrokerServer at ${this.brokerURI} cannot return output '${segments[3]}'`); @@ -300,7 +306,11 @@ export default class MqttBrokerServer implements ProtocolServer { }; try { - thing.handleWriteProperty(segments[3], JSON.parse(payload.toString()), options); + thing.handleWriteProperty( + segments[3], + new Content(ContentSerdes.DEFAULT, Readable.from(payload.toString())), + options + ); } catch (err) { error( `MqttBrokerServer at ${this.brokerURI} got error on writing to property '${segments[3]}': ${err.message}` From 7000c5232d91691c24fecbd5b6abf80aeb3d355c Mon Sep 17 00:00:00 2001 From: Hasan Eroglu Date: Wed, 10 May 2023 08:22:14 +0200 Subject: [PATCH 2/5] fix: run prettier Signed-off-by: Hasan Eroglu --- packages/binding-mqtt/src/mqtt-broker-server.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/binding-mqtt/src/mqtt-broker-server.ts b/packages/binding-mqtt/src/mqtt-broker-server.ts index 77e9ae832..993044810 100644 --- a/packages/binding-mqtt/src/mqtt-broker-server.ts +++ b/packages/binding-mqtt/src/mqtt-broker-server.ts @@ -268,11 +268,7 @@ export default class MqttBrokerServer implements ProtocolServer { }; thing - .handleInvokeAction( - segments[3], - new Content(ContentSerdes.DEFAULT, Readable.from(value)), - options - ) + .handleInvokeAction(segments[3], new Content(ContentSerdes.DEFAULT, Readable.from(value)), options) .then((output: unknown) => { if (output) { warn(`MqttBrokerServer at ${this.brokerURI} cannot return output '${segments[3]}'`); From a8c725815f8278c2b63340816bfff81f58273e9d Mon Sep 17 00:00:00 2001 From: Hasan Eroglu Date: Wed, 10 May 2023 08:27:03 +0200 Subject: [PATCH 3/5] fix: add json parse Signed-off-by: Hasan Eroglu --- packages/binding-mqtt/src/mqtt-broker-server.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/binding-mqtt/src/mqtt-broker-server.ts b/packages/binding-mqtt/src/mqtt-broker-server.ts index 993044810..c6a76b924 100644 --- a/packages/binding-mqtt/src/mqtt-broker-server.ts +++ b/packages/binding-mqtt/src/mqtt-broker-server.ts @@ -304,7 +304,7 @@ export default class MqttBrokerServer implements ProtocolServer { try { thing.handleWriteProperty( segments[3], - new Content(ContentSerdes.DEFAULT, Readable.from(payload.toString())), + new Content(ContentSerdes.DEFAULT, Readable.from(JSON.parse(payload.toString()))), options ); } catch (err) { From 9fb57490bdca7ef9f888651880aed5a707161591 Mon Sep 17 00:00:00 2001 From: Hasan Eroglu Date: Wed, 10 May 2023 08:50:13 +0200 Subject: [PATCH 4/5] fix: remove json parse Signed-off-by: Hasan Eroglu --- packages/binding-mqtt/src/mqtt-broker-server.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/binding-mqtt/src/mqtt-broker-server.ts b/packages/binding-mqtt/src/mqtt-broker-server.ts index c6a76b924..993044810 100644 --- a/packages/binding-mqtt/src/mqtt-broker-server.ts +++ b/packages/binding-mqtt/src/mqtt-broker-server.ts @@ -304,7 +304,7 @@ export default class MqttBrokerServer implements ProtocolServer { try { thing.handleWriteProperty( segments[3], - new Content(ContentSerdes.DEFAULT, Readable.from(JSON.parse(payload.toString()))), + new Content(ContentSerdes.DEFAULT, Readable.from(payload.toString())), options ); } catch (err) { From c32b712a0009913f077994bba17ede76bac7017e Mon Sep 17 00:00:00 2001 From: Hasan eroglu Date: Thu, 11 May 2023 18:12:47 +0200 Subject: [PATCH 5/5] fix: get contentType from forms Signed-off-by: Hasan eroglu --- packages/binding-mqtt/src/mqtt-broker-server.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/binding-mqtt/src/mqtt-broker-server.ts b/packages/binding-mqtt/src/mqtt-broker-server.ts index 993044810..19eb24aab 100644 --- a/packages/binding-mqtt/src/mqtt-broker-server.ts +++ b/packages/binding-mqtt/src/mqtt-broker-server.ts @@ -267,8 +267,11 @@ export default class MqttBrokerServer implements ProtocolServer { ), }; + const contentType = action.forms[options.formIndex].contentType ?? ContentSerdes.DEFAULT; + const inputContent = new Content(contentType, Readable.from(value)); + thing - .handleInvokeAction(segments[3], new Content(ContentSerdes.DEFAULT, Readable.from(value)), options) + .handleInvokeAction(segments[3], inputContent, options) .then((output: unknown) => { if (output) { warn(`MqttBrokerServer at ${this.brokerURI} cannot return output '${segments[3]}'`); @@ -301,10 +304,13 @@ export default class MqttBrokerServer implements ProtocolServer { ), }; + const formContentType = property.forms[options.formIndex].contentType ?? ContentSerdes.DEFAULT; + const inputContent = new Content(formContentType, Readable.from(payload.toString())); + try { thing.handleWriteProperty( segments[3], - new Content(ContentSerdes.DEFAULT, Readable.from(payload.toString())), + inputContent, options ); } catch (err) {