In the eclipse-ditto mappingcontext I placed following incomingScript:
function mapToDittoProtocolMsg(headers, textPayload, bytePayload, contentType) {
switch (contentType) {
case "application/json":
var jsonData = JSON.parse(textPayload);
var temperature = jsonData.temp;
var humidity = jsonData.hum;
var path;
var value;
if (temperature != null && humidity != null) {
path = "/features";
value = {
temperature: {
properties: {
value: temperature
}
},
humidity: {
properties: {
value: humidity
}
}
};
} else if (temperature != null) {
path = "/features/temperature/properties/value";
value = temperature;
} else if (humidity != null) {
path = "/features/humidity/properties/value";
value = humidity;
}
if (!path || !value) {
return null;
}
return Ditto.buildDittoProtocolMsg("tenant_aloxy", headers["device_id"], "things", "twin", "commands", "modify", path, headers, value);
break;
case "application/octet-stream":
let byteBuf = Ditto.asByteBuffer(bytePayload);
var path = "/features/alp/properties/value";
var value = 21;
return Ditto.buildDittoProtocolMsg("tenant_aloxy", headers["device_id"], "things", "twin", "commands", "modify", path, headers, value);
default:
return null;
}
}
When I send in binary data, I'm hitting the second case of the switch as expected. However, when it tries to convert the incoming data as a bytebuffer (Ditto.asByteBuffer(bytePayload);) it throws following exception:
ReferenceError: "dcodeIO" is not defined.
That helper function in the "Ditto" scope requires the "ByteBuffer.js" library as described in the documentation: https://www.eclipse.org/ditto/connectivity-mapping.html#bytebufferjs (dcodeIO
was used as scope for that library).
That means you have simply to enable that this library is loaded in the configuration of your mapping: https://www.eclipse.org/ditto/connectivity-mapping.html#configuration-options
{
"incomingScript": "...",
"outgoingScript": "...",
"loadBytebufferJS": true,
"loadLongJS": true
}
After that you should be able to use Ditto.asByteBuffer()