Ditto.asByteBuffer(bytePayload) 抛出异常
Ditto.asByteBuffer(bytePayload) throws exception
在 eclipse-ditto 映射上下文中,我放置了以下 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;
}
}
当我发送二进制数据时,我按预期点击了开关的第二种情况。但是,当它尝试将传入数据转换为字节缓冲区 (Ditto.asByteBuffer(bytePayload);) 时,它会抛出以下异常:
ReferenceError: "dcodeIO" is not defined.
"Ditto" 范围内的辅助函数需要 "ByteBuffer.js" 库,如文档中所述:https://www.eclipse.org/ditto/connectivity-mapping.html#bytebufferjs(dcodeIO
用作该库的范围)。
这意味着您只需在映射配置中启用此库即可加载:https://www.eclipse.org/ditto/connectivity-mapping.html#configuration-options
{
"incomingScript": "...",
"outgoingScript": "...",
"loadBytebufferJS": true,
"loadLongJS": true
}
之后你应该可以使用 Ditto.asByteBuffer()
在 eclipse-ditto 映射上下文中,我放置了以下 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;
}
}
当我发送二进制数据时,我按预期点击了开关的第二种情况。但是,当它尝试将传入数据转换为字节缓冲区 (Ditto.asByteBuffer(bytePayload);) 时,它会抛出以下异常:
ReferenceError: "dcodeIO" is not defined.
"Ditto" 范围内的辅助函数需要 "ByteBuffer.js" 库,如文档中所述:https://www.eclipse.org/ditto/connectivity-mapping.html#bytebufferjs(dcodeIO
用作该库的范围)。
这意味着您只需在映射配置中启用此库即可加载:https://www.eclipse.org/ditto/connectivity-mapping.html#configuration-options
{
"incomingScript": "...",
"outgoingScript": "...",
"loadBytebufferJS": true,
"loadLongJS": true
}
之后你应该可以使用 Ditto.asByteBuffer()