0

尽管我遵循同上示例中的指南,但我遇到了一个奇怪的错误。

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

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

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

1 回答 1

1

您的映射脚本似乎工作正常。我使用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();
}
于 2019-04-01T12:26:31.117 回答