首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ditto没有可用的MessageMappingProcessor

Ditto没有可用的MessageMappingProcessor
EN

Stack Overflow用户
提问于 2019-04-01 11:30:33
回答 1查看 197关注 0票数 0

我犯了一个奇怪的错误,尽管我遵循了同样的准则。

章鱼可以向MQTT发布消息。我可以看到他们使用MQTT客户端。WebApp显示已建立的连接和发送事件工作。我可以通过"my.test.octopus"面板改变值。但是当我使用API查询它时,我只能从webapp中获得值,从章鱼那里得不到值。

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

代码语言:javascript
复制
"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);
}"

谢谢你的帮助

更新

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

请参见以下日志语句:

代码语言:javascript
复制
"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); }"
EN

回答 1

Stack Overflow用户

发布于 2019-04-01 12:26:31

您的映射脚本似乎工作正常。我使用有效载荷映射测试的例子为它创建了一个单元测试。

此测试如下所示:

代码语言:javascript
复制
 @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

代码语言:javascript
复制
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

代码语言:javascript
复制
{
  "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:

代码语言:javascript
复制
final byte[] bytePayload = "{\"thingId\":\"my.test.thing\",\"temp\":25.6,\"alt\":11}".getBytes();

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

更新

根据苏鲍伯的评论,章鱼正在发送文本有效载荷。为了映射此有效负载,实际上必须使用文本有效负载而不是字节有效负载。在下面的文章中,您将看到更新的incomingScript。

incomingScript.js

代码语言:javascript
复制
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);
}

该测试可适用于:

代码语言:javascript
复制
@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();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55454096

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档