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

feat: add initial CBOR codec support #958

Merged
merged 2 commits into from
Apr 26, 2023
Merged

Conversation

JKRhb
Copy link
Member

@JKRhb JKRhb commented Apr 23, 2023

Corresponding with #953, this PR adds initial support for CBOR encoding and decoding. For now, the Codec is added to the core package, but as discussed in the issue, it should probably be moved to its own codec-cbor package, either in this PR or a follow-up.

Future additions to the codec should include support for custom tag encoding/decoding1, but for now, at least all basic JavaScript data types should be supported (via the cbor package).

Footnotes

  1. See RFC 8949, section 3.4 for more information on CBOR tags (sorry for confusing the purpose of tags a bit at the community meetup on Thursday 😅).

package.json Outdated Show resolved Hide resolved
@egekorkan
Copy link
Member

I have tested and it indeed works. I have used the following script

const { Servient, Helpers } = require("@node-wot/core");
const { CoapClientFactory } = require("@node-wot/binding-coap");
const { HttpClientFactory } = require("@node-wot/binding-http");

// create Servient and add CoAP binding
const servient = new Servient();
servient.addClientFactory(new CoapClientFactory());
servient.addClientFactory(new HttpClientFactory());

const td = {
    "@context": "https://www.w3.org/2022/wot/td/v1.1",
    title: "CborTestThing",
    securityDefinitions: {
        nosec_sc: { scheme: "nosec" },
    },
    security: "nosec_sc",
    properties: {
        test1: {
            forms: [
                {
                    href: "coap://californium.eclipseprojects.io/multi-format",
                    contentType: "application/cbor",
                    "cov:accept": 60,
                },
            ],
        },
        test2: {
            forms: [
                {
                    href: "coap://californium.eclipseprojects.io/multi-format",
                    contentType: "application/json",
                    "cov:accept": 50,
                },
            ],
        },
        test3: {
            forms: [
                {
                    href: "http://plugfest.thingweb.io:8083/testthing/properties/bool",
                    contentType: "application/json",
                },
            ],
        },
    },
};

const wotHelper = new Helpers(servient);

async function main() {
    const WoT = await servient.start();
    const thing = await WoT.consume(td);

    // read property
    const read1 = await thing.readProperty("test1");
    console.log("value1 is: ", await read1.value());

    const read2 = await thing.readProperty("test2");
    console.log("value2 is: ", await read2.value());

    const read3 = await thing.readProperty("test3");
    console.log("value3 is: ", await read3.value());
}

main()

I get:

value1 is: { mid: 54248, code: 'GET', type: 'CON', accept: 60 }
value2 is: { type: 'CON', code: 'GET', mid: 37042, accept: 50 }
value3 is: false

@egekorkan
Copy link
Member

@JKRhb could you also change the readme at https://github.com/eclipse/thingweb.node-wot/blob/master/README.md?plain=1#L79

@JKRhb
Copy link
Member Author

JKRhb commented Apr 24, 2023

Thank you for testing! :)

@JKRhb could you also change the readme at https://github.com/eclipse/thingweb.node-wot/blob/master/README.md?plain=1#L79

Done in 1e3e895 :)

Copy link
Member

@danielpeintner danielpeintner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@relu91 relu91 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! 🥳

@relu91 relu91 merged commit fa1cd43 into eclipse-thingweb:master Apr 26, 2023
7 checks passed
@JKRhb JKRhb deleted the cbor branch April 26, 2023 08:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants