Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(binding-mqtt): fix sending plain value instead of content object #987

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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