您的映射脚本似乎工作正常。我使用ditto-examples 中的有效负载映射测试为其创建了一个单元测试。
此测试如下所示:
@Test
public void incomingBytePayloadMapping() throws IOException {
final Resource incomingMappingFunction = new Resource("incomingScript.js");
final PayloadMappingFunction underTest = PayloadMappingFunction.fromJavaScript(incomingMappingFunction.getContent());
final Map<String, String> headers = new HashMap<>();
headers.put("content-type", ContentTypes.APPLICATION_OCTET_STREAM.toString());
headers.put("device_id", "the-thing-id");
final byte[] bytePayload = "{\"thingId\":\"my.test.thing\",\"temp\":25.6,\"alt\":11}".getBytes();
final ExternalMessage message = ExternalMessageFactory.newExternalMessageBuilder(headers)
.withBytes(bytePayload)
.build();
final Resource expectedAdaptableJsonResource = new Resource("expectedAdaptable.json");
final JsonObject expectedAdaptableJson = JsonFactory.newObject(expectedAdaptableJsonResource.getContent());
final Adaptable expectedAdaptable = ProtocolFactory
.jsonifiableAdaptableFromJson(expectedAdaptableJson)
.setDittoHeaders(DittoHeaders.of(headers));
PayloadMappingTestCase.assertThat(message)
.mappedByJavascriptPayloadMappingFunction(underTest)
.isEqualTo(expectedAdaptable)
.verify();
}
传入脚本.js
function mapToDittoProtocolMsg(
headers,
textPayload,
bytePayload,
contentType) {
const jsonString = String.fromCharCode.apply(null, new Uint8Array(bytePayload));
const jsonData = JSON.parse(jsonString);
const thingId = jsonData.thingId;
const value = {
temp_sensor: {
properties: {
value: jsonData.temp
}
},
altitude: {
properties: {
value: jsonData.alt
}
}
};
return Ditto.buildDittoProtocolMsg('my.test', thingId, 'things', 'twin', 'commands', 'modify', '/features', headers,
value);
}
预期的Adaptable.json
{
"topic": "my.test/my.test.thing/things/twin/commands/modify",
"headers": {},
"path": "/features",
"value": {
"temp_sensor": {
"properties": {
"value": 25.6
}
},
"altitude": {
"properties": {
"value": 11
}
}
}
}
到目前为止,这似乎可行,但在这个测试中,我假设以下传入的 bytePayload:
final byte[] bytePayload = "{\"thingId\":\"my.test.thing\",\"temp\":25.6,\"alt\":11}".getBytes();
你能以某种方式验证你的八达通发送的字节有效载荷看起来正确吗?章鱼真的发送字节有效载荷还是文本有效载荷(应用程序/json)?
更新
根据Bob Su的评论,章鱼正在发送文本有效载荷。为了映射此有效负载,您实际上必须使用文本有效负载而不是字节有效负载。在下文中,您将看到更新的incomingScript。
传入脚本.js
function mapToDittoProtocolMsg(
headers,
textPayload,
bytePayload,
contentType) {
var jsonData = JSON.parse(textPayload);
const thingId = jsonData.thingId;
const value = {
temp_sensor: {
properties: {
value: jsonData.temp
}
},
altitude: {
properties: {
value: jsonData.alt
}
}
};
return Ditto.buildDittoProtocolMsg('my.test', thingId, 'things', 'twin', 'commands', 'modify', '/features', headers,
value);
}
该测试可适用于:
@Test
public void incomingTextPayloadMapping() throws IOException {
final Resource incomingMappingFunction = new Resource("incomingScript.js");
final PayloadMappingFunction underTest = PayloadMappingFunction.fromJavaScript(incomingMappingFunction.getContent());
final Map<String, String> headers = new HashMap<>();
headers.put("content-type", ContentTypes.APPLICATION_JSON.toString());
headers.put("device_id", "the-thing-id");
final ExternalMessage message = ExternalMessageFactory.newExternalMessageBuilder(headers)
.withText("{\"thingId\":\"my.test.thing\",\"temp\":25.6,\"alt\":11}")
.build();
final Resource expectedAdaptableJsonResource = new Resource("expectedAdaptable.json");
final JsonObject expectedAdaptableJson = JsonFactory.newObject(expectedAdaptableJsonResource.getContent());
final Adaptable expectedAdaptable = ProtocolFactory
.jsonifiableAdaptableFromJson(expectedAdaptableJson)
.setDittoHeaders(DittoHeaders.of(headers));
PayloadMappingTestCase.assertThat(message)
.mappedByJavascriptPayloadMappingFunction(underTest)
.isEqualTo(expectedAdaptable)
.verify();
}