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

Add CoAP observe #72

Merged
merged 2 commits into from Mar 5, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 42 additions & 0 deletions examples/scripts/coap-observe-client.js
@@ -0,0 +1,42 @@
/********************************************************************************
* Copyright (c) 2018 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the W3C Software Notice and
* Document License (2015-05-13) which is available at
* https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document.
*
* SPDX-License-Identifier: EPL-2.0 OR W3C-20150513
********************************************************************************/

//Tested with https://github.com/mcollina/node-coap/blob/master/examples/observe_server.js

let td =
`{
"name": "CoAP date server",
"id": "urn:dev:wot:coap:date",
"properties" : {
"mydate": {
"forms": [
{"href": "coap://localhost:5683"}
]
}
}
}`;

let thing = WoT.consume(td);

thing.properties.mydate.subscribe(
x => {
console.info("onNext: ", x);
},
e => console.log("onError: %s", e),
() => {
console.log("onCompleted");
}
);
console.info("Subscribed");
27 changes: 24 additions & 3 deletions packages/binding-coap/src/coap-client.ts
Expand Up @@ -134,8 +134,28 @@ export default class CoapClient implements ProtocolClient {
}

public subscribeResource(form: CoapForm, next: ((value: any) => void), error?: (error: any) => void, complete?: () => void): any {
error(new Error(`CoapClient does not implement subscribe`));
return null;
let req = this.generateRequest(form, "GET", true);

console.log(`CoapClient sending ${req.statusCode} to ${form.href}`);

req.on("response", (res: any) => {
console.log(`CoapClient received ${res.code} from ${form.href}`);
console.debug(`CoapClient received Content-Format: ${res.headers["Content-Format"]}`);

// FIXME does not work with blockwise because of node-coap
let contentType = res.headers["Content-Format"];
if (!contentType) contentType = form.contentType;

res.on('data', (data: any) => {
next({ type: contentType, body: res.payload });
});
});

req.on("error", (err: any) => error(err));

req.end();

return new Subscription( () => {} );
}

public start(): boolean {
Expand Down Expand Up @@ -166,7 +186,7 @@ export default class CoapClient implements ProtocolClient {
return options;
}

private generateRequest(form: CoapForm, dflt: string): any {
private generateRequest(form: CoapForm, dflt: string, observable: boolean = false): any {

let options: CoapRequestConfig = this.uriToOptions(form.href);

Expand All @@ -182,6 +202,7 @@ export default class CoapClient implements ProtocolClient {
default: console.warn("CoapClient got invalid 'methodCode', using default", options.method);
}
}
options.observe = observable;

let req = this.agent.request(options);

Expand Down