Skip to content

Commit

Permalink
Merge pull request #987 from hasanheroglu/mqtt-interactions-content
Browse files Browse the repository at this point in the history
fix(binding-mqtt): fix sending plain value instead of content object
  • Loading branch information
relu91 authored May 11, 2023
2 parents fb2c2aa + b810918 commit 5145fc7
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions packages/binding-mqtt/src/mqtt-broker-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -246,6 +247,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(
Expand Down Expand Up @@ -278,8 +280,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[this.INTERACTION_NAME_SEGMENT_INDEX], value, options)
.handleInvokeAction(segments[this.INTERACTION_NAME_SEGMENT_INDEX], inputContent, options)
.then((output: unknown) => {
if (output) {
warn(
Expand Down Expand Up @@ -320,12 +325,11 @@ 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[this.INTERACTION_NAME_SEGMENT_INDEX],
JSON.parse(payload.toString()),
options
);
thing.handleWriteProperty(segments[this.INTERACTION_NAME_SEGMENT_INDEX], inputContent, options);
} catch (err) {
error(
`MqttBrokerServer at ${this.brokerURI} got error on writing to property '${
Expand Down

0 comments on commit 5145fc7

Please sign in to comment.