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

First steps in fixing #758 #767

Merged
merged 6 commits into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
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
8 changes: 6 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions packages/binding-coap/src/coap-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export default class CoapServer implements ProtocolServer {
for (const propertyName in thing.properties) {
const href = base + "/" + this.PROPERTY_DIR + "/" + encodeURIComponent(propertyName);
const form = new TD.Form(href, type);
ProtocolHelpers.updatePropertyFormWithTemplate(form, tdTemplate, propertyName);
ProtocolHelpers.updatePropertyFormWithTemplate(form, thing.properties[propertyName]);
if (thing.properties[propertyName].readOnly) {
form.op = ["readproperty"];
} else if (thing.properties[propertyName].writeOnly) {
Expand All @@ -160,7 +160,7 @@ export default class CoapServer implements ProtocolServer {
for (const actionName in thing.actions) {
const href = base + "/" + this.ACTION_DIR + "/" + encodeURIComponent(actionName);
const form = new TD.Form(href, type);
ProtocolHelpers.updateActionFormWithTemplate(form, tdTemplate, actionName);
ProtocolHelpers.updateActionFormWithTemplate(form, thing.actions[actionName]);
form.op = "invokeaction";
thing.actions[actionName].forms.push(form);
console.debug(
Expand All @@ -172,7 +172,7 @@ export default class CoapServer implements ProtocolServer {
for (const eventName in thing.events) {
const href = base + "/" + this.EVENT_DIR + "/" + encodeURIComponent(eventName);
const form = new TD.Form(href, type);
ProtocolHelpers.updateEventFormWithTemplate(form, tdTemplate, eventName);
ProtocolHelpers.updateEventFormWithTemplate(form, thing.events[eventName]);
form.op = ["subscribeevent", "unsubscribeevent"];
thing.events[eventName].forms.push(form);
console.debug(
Expand Down
8 changes: 8 additions & 0 deletions packages/binding-coap/test/coap-server-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class CoapServerTest {

const test: DataSchemaValue = "testValue";
testThing.setPropertyReadHandler("test", (_) => Promise.resolve(test));
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
testThing.properties.test.forms = [];

await coapServer.expose(testThing);
Expand Down Expand Up @@ -89,6 +91,8 @@ class CoapServerTest {
testThing.setPropertyWriteHandler("test", async (value) => {
test = await value.value();
});
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
testThing.properties.test.forms = [];

await coapServer.expose(testThing);
Expand Down Expand Up @@ -126,6 +130,8 @@ class CoapServerTest {
resolve("TEST");
});
});
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
testThing.actions.try.forms = [];

await coapServer.expose(testThing);
Expand Down Expand Up @@ -231,6 +237,8 @@ class CoapServerTest {
resolve(test);
});
});
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
testThing.properties.test.forms = [];

await coapServer.expose(testThing);
Expand Down
3 changes: 2 additions & 1 deletion packages/binding-http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
"node-fetch": "^2.6.7",
"query-string": "^7.1.1",
"rxjs": "5.5.11",
"slugify": "^1.4.5"
"slugify": "^1.4.5",
"wot-thing-description-types": "^1.1.0-09-February-2022"
},
"scripts": {
"build": "tsc -b",
Expand Down
11 changes: 6 additions & 5 deletions packages/binding-http/src/http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { OAuth2SecurityScheme } from "@node-wot/td-tools";
import slugify from "slugify";
import { ThingDescription } from "wot-typescript-definitions";
import * as acceptLanguageParser from "accept-language-parser";
import { PropertyElement } from "wot-thing-description-types";

export default class HttpServer implements ProtocolServer {
public readonly scheme: "http" | "https";
Expand Down Expand Up @@ -206,7 +207,7 @@ export default class HttpServer implements ProtocolServer {

private updateInteractionNameWithUriVariablePattern(
interactionName: string,
uriVariables: { [key: string]: TD.DataSchema }
uriVariables: PropertyElement["uriVariables"]
): string {
if (uriVariables && Object.keys(uriVariables).length > 0) {
let pattern = "{?";
Expand Down Expand Up @@ -321,7 +322,7 @@ export default class HttpServer implements ProtocolServer {
);
const href = base + "/" + this.PROPERTY_DIR + "/" + propertyNamePattern;
const form = new TD.Form(href, type);
ProtocolHelpers.updatePropertyFormWithTemplate(form, tdTemplate, propertyName);
ProtocolHelpers.updatePropertyFormWithTemplate(form, thing.properties[propertyName]);
if (thing.properties[propertyName].readOnly) {
form.op = ["readproperty"];
const hform: HttpForm = form;
Expand Down Expand Up @@ -372,7 +373,7 @@ export default class HttpServer implements ProtocolServer {
);
const href = base + "/" + this.ACTION_DIR + "/" + actionNamePattern;
const form = new TD.Form(href, type);
ProtocolHelpers.updateActionFormWithTemplate(form, tdTemplate, actionName);
ProtocolHelpers.updateActionFormWithTemplate(form, thing.actions[actionName]);
form.op = ["invokeaction"];
const hform: HttpForm = form;
if (hform["htv:methodName"] === undefined) {
Expand All @@ -392,7 +393,7 @@ export default class HttpServer implements ProtocolServer {
);
const href = base + "/" + this.EVENT_DIR + "/" + eventNamePattern;
const form = new TD.Form(href, type);
ProtocolHelpers.updateEventFormWithTemplate(form, tdTemplate, eventName);
ProtocolHelpers.updateEventFormWithTemplate(form, thing.events[eventName]);
form.subprotocol = "longpoll";
form.op = ["subscribeevent", "unsubscribeevent"];
thing.events[eventName].forms.push(form);
Expand Down Expand Up @@ -791,7 +792,7 @@ export default class HttpServer implements ProtocolServer {
}
} else if (segments[2] === this.ACTION_DIR) {
// sub-path -> select Action
const action: TD.ThingAction = thing.actions[segments[3]];
const action = thing.actions[segments[3]];
if (action) {
if (req.method === "POST") {
const options: WoT.InteractionOptions & { formIndex: number } = {
Expand Down
2 changes: 2 additions & 0 deletions packages/binding-http/test/http-server-oauth-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class OAuthServerTests {
},
},
});
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
testThing.properties.test.forms = [];

await this.server.expose(testThing);
Expand Down
16 changes: 16 additions & 0 deletions packages/binding-http/test/http-server-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class HttpServerTest {
testThing
);
// testThing.extendInteractions();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
testThing.properties.test.forms = [];

await httpServer.expose(testThing);
Expand Down Expand Up @@ -198,13 +200,17 @@ class HttpServerTest {
expect(options.uriVariables).to.deep.equal({ id: "testId" });
test = await value.value();
});
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
testThing.properties.test.forms = [];
testThing.setActionHandler("try", (input: WoT.InteractionOutput, params: InteractionOptions) => {
return new Promise<string>((resolve, reject) => {
expect(params.uriVariables).to.deep.equal({ step: 5 });
resolve("TEST");
});
});
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
testThing.actions.try.forms = [];

await httpServer.expose(testThing);
Expand Down Expand Up @@ -244,12 +250,16 @@ class HttpServerTest {
testThing.setPropertyWriteHandler("test", async (value) => {
test = await value.value();
});
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
testThing.properties.test.forms = [];
testThing.setActionHandler("try", (input: WoT.InteractionOutput) => {
return new Promise<string>((resolve, reject) => {
resolve("TEST");
});
});
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
testThing.actions.try.forms = [];

await httpServer.expose(testThing);
Expand Down Expand Up @@ -430,7 +440,11 @@ class HttpServerTest {
},
},
});
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
testThing.properties.maintenanceNeeded.forms = [];
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
testThing.actions.makeDrink.forms = [];

await httpServer.expose(testThing);
Expand Down Expand Up @@ -481,6 +495,8 @@ class HttpServerTest {
resolve(test);
});
});
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
testThing.properties.test.forms = [];

await httpServer.expose(testThing);
Expand Down
3 changes: 2 additions & 1 deletion packages/binding-mqtt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"@node-wot/td-tools": "0.8.1",
"aedes": "^0.46.2",
"mqtt": "^4.2.8",
"rxjs": "5.5.11"
"rxjs": "5.5.11",
"wot-thing-description-types": "^1.1.0-09-February-2022"
},
"scripts": {
"build": "tsc -b",
Expand Down
5 changes: 3 additions & 2 deletions packages/binding-mqtt/src/mqtt-broker-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import * as TD from "@node-wot/td-tools";
import { MqttBrokerServerConfig } from "./mqtt";
import { ProtocolServer, Servient, ExposedThing, ContentSerdes, ProtocolHelpers, Content } from "@node-wot/core";
import { InteractionOptions } from "wot-typescript-definitions";
import { ActionElement, PropertyElement } from "wot-thing-description-types";

export default class MqttBrokerServer implements ProtocolServer {
readonly scheme: string = "mqtt";
Expand Down Expand Up @@ -255,7 +256,7 @@ export default class MqttBrokerServer implements ProtocolServer {
}

private handleAction(
action: TD.ThingAction,
action: ActionElement,
packet: IPublishPacket,
payload: Buffer,
segments: string[],
Expand Down Expand Up @@ -312,7 +313,7 @@ export default class MqttBrokerServer implements ProtocolServer {
}

private handlePropertyWrite(
property: TD.ThingProperty,
property: PropertyElement,
packet: IPublishPacket,
payload: Buffer,
segments: string[],
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/codecs/base64-codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default class Base64Codec implements ContentCodec {
valueToBytes(value: unknown, schema: DataSchema, parameters?: { [key: string]: string }): Buffer {
let body = "";
if (value !== undefined) {
body = value.toString();
body = JSON.stringify(value);
}
return Buffer.from(body, "base64");
}
Expand Down
20 changes: 12 additions & 8 deletions packages/core/src/codecs/octetstream-codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,15 @@ export default class OctetstreamCodec implements ContentCodec {
return "application/octet-stream";
}

bytesToValue(bytes: Buffer, schema: DataSchema, parameters?: { [key: string]: string }): DataSchemaValue {
bytesToValue(
bytes: Buffer,
schema: DataSchema,
parameters: { [key: string]: string | undefined } = {}
): DataSchemaValue {
// console.debug(`OctetstreamCodec parsing '${bytes.toString()}'`);

const bigendian = parameters.byteorder ? parameters.byteorder === "bigendian" : true;
let signed = parameters.signed ? parameters.signed === "true" : false;
const bigendian = parameters.byteorder !== "littleendian"; // default to big endian
let signed = parameters.signed !== "false"; // default to signed

// check length if specified
if (parameters.length && parseInt(parameters.length) !== bytes.length) {
Expand Down Expand Up @@ -160,15 +164,15 @@ export default class OctetstreamCodec implements ContentCodec {
}
}

valueToBytes(value: unknown, schema: DataSchema, parameters?: { [key: string]: string }): Buffer {
valueToBytes(value: unknown, schema: DataSchema, parameters: { [key: string]: string | undefined } = {}): Buffer {
// console.debug(`OctetstreamCodec serializing '${value}'`);

if (!parameters.length) {
console.warn("[core/octetstream-codec]", "Missing 'length' parameter necessary for write. I'll do my best");
}

const bigendian = parameters.byteorder ? parameters.byteorder === "bigendian" : true;
let signed = parameters.signed ? parameters.signed === "true" : true; // default is signed
const bigendian = parameters.byteorder !== "littleendian"; // default to bigendian
let signed = parameters.signed !== "false"; // if signed is undefined -> true (default)
let length = parameters.length ? parseInt(parameters.length) : undefined;
let buf: Buffer;

Expand All @@ -192,7 +196,7 @@ export default class OctetstreamCodec implements ContentCodec {

switch (dataType) {
case "boolean":
return Buffer.alloc(length, value ? 255 : 0);
return Buffer.alloc(length ?? 1, value ? 255 : 0);
case "byte":
case "short":
case "int":
Expand Down Expand Up @@ -302,7 +306,7 @@ export default class OctetstreamCodec implements ContentCodec {
throw new Error("Unable to handle object type " + dataType);

case "null":
return null;
return Buffer.alloc(0);
default:
throw new Error("Unable to handle object type " + dataType);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/codecs/text-codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ export default class TextCodec implements ContentCodec {
// console.debug(`TextCodec serializing '${value}'`);
let body = "";
if (value !== undefined) {
body = value.toString();
body = JSON.stringify(value);
}

// type BufferEncoding = "ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "latin1" | "binary" | "hex";
let be: BufferEncoding;
let be: BufferEncoding | undefined;
if (parameters && parameters.charset) {
switch (parameters.charset) {
case "ascii":
Expand Down
Loading