gpt4 book ai didi

javascript - Ditto 中没有可用的消息映射处理器

转载 作者:行者123 更新时间:2023-12-02 10:03:42 28 4
gpt4 key购买 nike

尽管我遵循同上示例中的指南,但我遇到了陌生人错误。

Octopus 可以将消息发布到 MQTT。我可以看到他们使用 MQTT 客户端。WebApp 显示连接已建立并且发送发送事件有效。我可以通过“my.test.octopus”面板更改值。但是当我使用 API 查询它时,我只能从 webapp 获取值,而从未从 octopus 获取值。

我检查了连接日志,似乎是映射问题...我在创建连接时使用了以下内容来创建映射:

"incomingScript": "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);
}"

感谢帮助

更新

错误显示在以下日志行中:

查看以下日志语句:

"The message mapper configuration failed due to: unterminated regular expression literal (incomingScript#1) - in line/column #1/472," -- ""incomingScript": "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); }"

最佳答案

您的映射脚本似乎工作正常。我使用 payload mapping testing from 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();
}

incomingScript.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);
}

expectedAdaptable.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();

您能否以某种方式验证您的 Octopus 发送的字节有效负载是否正确? Octopus 真的发送字节有效负载还是文本有效负载(application/json)?

更新

根据Bob Su的评论 Octopus 正在发送文本有效负载。为了映射此有效负载,您实际上必须使用文本有效负载而不是字节有效负载。在下面您将看到更新后的传入脚本。

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();
}

关于javascript - Ditto 中没有可用的消息映射处理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55454096/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com