Skip to content

Commit

Permalink
Implement observable property
Browse files Browse the repository at this point in the history
Please test. Not sure whether cancelling of observation works properly.
  • Loading branch information
wiresio authored Jan 8, 2020
1 parent f039b0f commit f775403
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions packages/binding-coap/src/coap-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,26 @@ export default class CoapServer implements ProtocolServer {
});
// observeproperty
} else {
// TODO
console.log(`CoapServer on port ${this.getPort()} rejects observe for '${segments[3]}' from ${Helpers.toUriLiteral(req.rsinfo.address)}:${req.rsinfo.port}`);

// work-around to reply with error code and without Observe option
throw new Error("observeproperty not implemented yet");
var oInterval = setInterval(function() {
thing.readProperty(segments[3])
// property.read() periodically
.then((value) => {
let content = ContentSerdes.get().valueToContent(value, <any>property);
res.setOption("Content-Format", content.type);
res.code = "2.05";
res.write(content.body);

res.on('finish', function(err: Error) {
clearInterval(oInterval);
res.end();
});
})
.catch(err => {
console.error(`CoapServer on port ${this.getPort()} got internal error on read '${requestUri.pathname}': ${err.message}`);
res.code = "5.00";
res.end(err.message);
});
}, 100);
}
// writeproperty
} else if (req.method === "PUT") {
Expand Down

0 comments on commit f775403

Please sign in to comment.