Skip to content

Commit

Permalink
Merge pull request #159 from eclipse/wiresio-patch-2
Browse files Browse the repository at this point in the history
CoAP and MQTT enhancements
  • Loading branch information
danielpeintner authored Jan 23, 2020
2 parents f039b0f + 32f7879 commit 386da07
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 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
11 changes: 10 additions & 1 deletion packages/binding-mqtt/src/mqtt-broker-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,16 @@ export default class MqttBrokerServer implements ProtocolServer {
if (action) {
thing.invokeAction(segments[3],
// action.invoke(
JSON.parse(payload))
function() {
let value;
try {
value = ContentSerdes.get().contentToValue({ type: packet.properties.contentType, body: Buffer.from(payload) }, action.input);
return value;
} catch(err) {
console.warn(`MqttBrokerServer at ${this.brokerURI} cannot process received message for '${segments[3]}': ${err.message}`);
return;
}
})
.then((output) => {
// MQTT cannot return results
if (output) {
Expand Down

0 comments on commit 386da07

Please sign in to comment.