【问题标题】:Orion CB doesn't handle IoT agent repliesOrion CB 不处理 IoT 代理回复
【发布时间】:2018-01-03 13:58:52
【问题描述】:

我正在开发一个系统,其中我使用 Orion CB 作为主事件总线,同时使用 iotagent 节点库,以便与第三方 API 建立接口。

现在发生的情况是,我在 iotagent 上注册了一个新设备,只有惰性属性,其中一个应该是 json 字符串。

覆盖查询处理程序及其子函数,我尝试将这个 json 字符串字符串化并将其保留为 json 格式,这就是我得到的:

1) 将 json 字符串化:
当我查询 Orion 以检索实体时,它回复该实体不存在,状态码为 404。查看 iotagent 调试日志,我发现它的响应正是我所期望的(所有属性都格式化)并且它以“ statusCode":{"code": 200, "reasonPhrase": "OK"} 所以看来 iotagent 正在用状态码 200 回复 Orion(成功!)。

2) 保持 json 原样:
当我查询 Orion 以检索它以状态码 200 回复的实体时,所有属性都正常,但带有 json 的属性为空。同样,查看 iotagent 调试日志,我看到响应是我希望看到的对 Orion 的查询,它以“statusCode”:{“code”:200,“reasonPhrase”:“OK”} 结尾。

这里发生了什么?第一种情况可能是什么问题?似乎出于某种原因 Orion 不喜欢 iotagent 响应,它说该实体不存在。而在第二种情况下,我认为问题可能出在不允许的数据结构中(json 作为属性值)。

我错过了什么吗?如果有人能帮我解决这个问题,我会很高兴,如果您需要更多信息,请告诉我。

编辑:

这就是我配置设备的方式:

{
    "devices": [
        {
            "device_id": "taxi_list",
            "entity_name": "taxi_list",
            "entity_type": "taxilist",
            "lazy": [
                {
                    "name": "taxiPosition",
                    "type": "StructuredValue"
                },
                {
                    "name": "error",
                    "type": "string"
                },
                {
                    "name": "status",
                    "type": "string"
                },
                {
                    "name": "corsaRuntime",
                    "type": "string"
                }
            ]
        }
    ]
}

我需要json的属性是“taxiPosition”,所以它的类型是“StructuredValue”。创建响应我只是在“设备”检索到的有效负载上使用 JSON.parse() 并将此值分配给如下属性:

responses.push({
                name: 'taxiPositions',
                type: 'StructuredValue',
                value: [{...}, {...}]
        });

其中的值是一个对象数组,具有简单的单级键值对,格式如下:

{ "idTaxi": "100", "idStato": "1", "lat": "90.843227100000000", "lng": "64.228254600000000", "nome": "Name", "cognome": "Surname", "licenza": "24XX", "cellulare": "+39320662332XX", "email": "xxxxxx@xxxx.eu", "targa": "XX000XX", "modelloAuto": "Focus SW", "marcaAuto": "Ford", "annoImmatricolazione": "2020", "passeggeri": "4", "dataCreazione": "2014-05-05", "rank": "4.2", "n_corse": "6", "os": "A", "dataOraInizioTurno": "05-01-2018 07:51", "dataOraFineTurno": "05-01-2018 11:50", "radio": "1", "pos": "0", "nome_radio": null, "iban": "" }

当我尝试将值作为字符串传递时,代码是相同的,但属性的类型是“字符串”,并且该值是通过在先前解析的 json 上使用 JSON.stringify() 获得的。

要从 Orion 查询实体,我只需调用 /v2/entities/taxi_list

【问题讨论】:

  • 请指定您在这两种情况下使用的代码。我的猜测是您的问题在于您传递给 Orion 的 json 结构。
  • 感谢您的回复,我已经添加了更多信息。我认为问题也出在结构上。如果我正确理解了 Orion 和 iotagent 如何一起工作,他们会使用 NGSIv1 格式(并且 Orion 将 NGSIv2 请求转换为 NGSIv1 以便做到这一点)。该版本是否可能不支持 json 值?但无论如何它并没有解释为什么传递一个字符串而不是 json Orion 回复一个 404 状态码。
  • 或尝试使用如下列表:"value": ["first", "second", "third"]。我的意思是,没有“价值观”的名称。并根据位置定位具体值。
  • 根据您发布的链接上报告的内容,我正在使用的 json 结构应该与 NSGIv1 和 NGSIv2 完全兼容。无论如何,我尝试使用您建议的结构来简化结构,但总是遇到同样的问题。

标签: fiware fiware-orion


【解决方案1】:

我测试了一些格式,最后两种是最好的选择: 这是一个验证 json:

POST /v2/entities

{
            "device_id": "taxi_list",
            "entity_name": "taxi_list",
            "entity_type": "taxilist",
            "lazy": [
                {
                    "name": "taxiPosition",
                    "type": "StructuredValue"
                },
                {
                    "name": "error",
                    "type": "string"
                },
                {
                    "name": "status",
                    "type": "string"
                },
                {
                    "name": "corsaRuntime",
                    "type": "string"
                }
            ]
}

答案:

{
    "error": "BadRequest",
    "description": "entity id length: 0, min length supported: 1"
}

POST /v2/entities

{
            "id": "taxi_list",
            "name": "taxi_list",
            "type": "taxilist",
            "lazy": [
                {
                    "name": "taxiPosition",
                    "type": "StructuredValue"
                },
                {
                    "name": "error",
                    "type": "string"
                },
                {
                    "name": "status",
                    "type": "string"
                },
                {
                    "name": "corsaRuntime",
                    "type": "string"
                }
            ]
}

答案:

{
    "error": "BadRequest",
    "description": "attribute must be a JSON object, unless keyValues option is used"
}

POST /v2/entities

{
            "id": "taxi_list",
            "name": "taxi_list",
            "type": "taxilist",
            "taxiPosition":{
              "type": "StructuredValue",
                            "value":
                            { "idTaxi": "100", "idStato": "1", "lat": "90.843227100000000", "lng": "64.228254600000000", "nome": "Name", "cognome": "Surname", "licenza": "24XX", "cellulare": "+39320662332XX", "email": "xxxxxx@xxxx.eu", "targa": "XX000XX", "modelloAuto": "Focus SW", "marcaAuto": "Ford", "annoImmatricolazione": "2020", "passeggeri": "4", "dataCreazione": "2014-05-05", "rank": "4.2", "n_corse": "6", "os": "A", "dataOraInizioTurno": "05-01-2018 07:51", "dataOraFineTurno": "05-01-2018 11:50", "radio": "1", "pos": "0", "nome_radio": null, "iban": "" }

                }
}

答案:

{
    "error": "BadRequest",
    "description": "attribute must be a JSON object, unless keyValues option is used"
}

最好的例子

POST v2/entities?options=keyValues

{
                "id": "taxi_list",
                "name": "taxi_list",
                "type": "taxilist",
                "taxiPosition":
                                { "idTaxi": "100", "idStato": "1", "lat": "90.843227100000000", "lng": "64.228254600000000", "nome": "Name", "cognome": "Surname", "licenza": "24XX", "cellulare": "+39320662332XX", "email": "xxxxxx@xxxx.eu", "targa": "XX000XX", "modelloAuto": "Focus SW", "marcaAuto": "Ford", "annoImmatricolazione": "2020", "passeggeri": "4", "dataCreazione": "2014-05-05", "rank": "4.2", "n_corse": "6", "os": "A", "dataOraInizioTurno": "05-01-2018 07:51", "dataOraFineTurno": "05-01-2018 11:50", "radio": "1", "pos": "0", "nome_radio": null, "iban": "" }


    }

答案:

NONE

HTTP 答案:

HTTP/1.1 201 Created

GET /v2/entities

[
    {
        "id": "taxi_list",
        "type": "taxilist",
        "name": {
            "type": "Text",
            "value": "taxi_list",
            "metadata": {}
        },
        "taxiPosition": {
            "type": "StructuredValue",
            "value": {
                "idTaxi": "100",
                "idStato": "1",
                "lat": "90.843227100000000",
                "lng": "64.228254600000000",
                "nome": "Name",
                "cognome": "Surname",
                "licenza": "24XX",
                "cellulare": "+39320662332XX",
                "email": "xxxxxx@xxxx.eu",
                "targa": "XX000XX",
                "modelloAuto": "Focus SW",
                "marcaAuto": "Ford",
                "annoImmatricolazione": "2020",
                "passeggeri": "4",
                "dataCreazione": "2014-05-05",
                "rank": "4.2",
                "n_corse": "6",
                "os": "A",
                "dataOraInizioTurno": "05-01-2018 07:51",
                "dataOraFineTurno": "05-01-2018 11:50",
                "radio": "1",
                "pos": "0",
                "nome_radio": null,
                "iban": ""
            },
            "metadata": {}
        }
    }
]

其他格式:

POST /v2/entities?options=keyValues

{
            "id": "taxi_list3",
            "name": "taxi_list",
            "type": "taxilist",
            "lazy": 
                        [
                            {
                                "name":"taxiPosition",
                                 "type": "StructuredValue",
                                "value":
                                                { "idTaxi": "100", "idStato": "1", "lat": "90.843227100000000", "lng": "64.228254600000000", "nome": "Name", "cognome": "Surname", "licenza": "24XX", "cellulare": "+39320662332XX", "email": "xxxxxx@xxxx.eu", "targa": "XX000XX", "modelloAuto": "Focus SW", "marcaAuto": "Ford", "annoImmatricolazione": "2020", "passeggeri": "4", "dataCreazione": "2014-05-05", "rank": "4.2", "n_corse": "6", "os": "A", "dataOraInizioTurno": "05-01-2018 07:51", "dataOraFineTurno": "05-01-2018 11:50", "radio": "1", "pos": "0", "nome_radio": null, "iban": "" }
                         },
                         {
                    "name": "error",
                    "type": "string"
                },
                {
                    "name": "status",
                    "type": "string"
                },
                {
                    "name": "corsaRuntime",
                    "type": "string"
                }

                         ]             
}

GET /v2/entities/taxi_list3

{
    "id": "taxi_list3",
    "type": "taxilist",
    "lazy": {
        "type": "StructuredValue",
        "value": [
            {
                "name": "taxiPosition",
                "type": "StructuredValue",
                "value": {
                    "idTaxi": "100",
                    "idStato": "1",
                    "lat": "90.843227100000000",
                    "lng": "64.228254600000000",
                    "nome": "Name",
                    "cognome": "Surname",
                    "licenza": "24XX",
                    "cellulare": "+39320662332XX",
                    "email": "xxxxxx@xxxx.eu",
                    "targa": "XX000XX",
                    "modelloAuto": "Focus SW",
                    "marcaAuto": "Ford",
                    "annoImmatricolazione": "2020",
                    "passeggeri": "4",
                    "dataCreazione": "2014-05-05",
                    "rank": "4.2",
                    "n_corse": "6",
                    "os": "A",
                    "dataOraInizioTurno": "05-01-2018 07:51",
                    "dataOraFineTurno": "05-01-2018 11:50",
                    "radio": "1",
                    "pos": "0",
                    "nome_radio": null,
                    "iban": ""
                }
            },
            {
                "name": "error",
                "type": "string"
            },
            {
                "name": "status",
                "type": "string"
            },
            {
                "name": "corsaRuntime",
                "type": "string"
            }
        ],
        "metadata": {}
    },
    "name": {
        "type": "Text",
        "value": "taxi_list",
        "metadata": {}
    }
}

然后,你可以使用:

?options=keyValues

这个例子中的实体“id”:“taxi_list”,更有趣。因为 Orion 创建结构化格式,Orion 会进行验证。在 tax_list3 的负载中,values 中的字符串将代表值,无需 Orion 验证。

【讨论】:

    【解决方案2】:

    阿拉戈纳](https://stackoverflow.com/users/8362361/f-aragona)

    您使用的 Fiware Orion 版本是什么?对于 IotAgent,您使用的是最新的吗?

    另一方面,强烈建议使用 docker-compose 来部署您正在使用的所有服务,除非 Mosquitto

    version : "2"
    
    services:
      mongo:
        image: mongo:3.2
        command: --nojournal
        ports:
          - "27017:27017"
        expose:
          - "27017"
      orion:
        image: fiware/orion
        links:
          - mongo
        ports:
          - "1026:1026"
        command: -dbhost mongo
        expose:
          - "1026"
    

    对于 Mosquitto 和 IotAgent,您必须从源安装它。

    git clone https://github.com/telefonicaid/iotagent-json.git
    

    克隆并安装后,您必须根据您的 IP 容器设置config.js。附上我给你一些脚本,以便创建实体和设备。为了将device 分配给entity,请务必按部就班地进行操作

    脚本

    1.创建服务

        #!/bin/bash
    
        curl -X POST -H "Fiware-Service: myHome" -H "Fiware-ServicePath: /environment" -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
    "services": [
        {
              "resource": "/",
              "apikey": "1234",
              "type": "multiSensor",
              "cbroker":"172.18.0.3:1026"
        }
        ]
    }' 'http://localhost:4041/iot/services'
    

    注意:

    • "apikey": "1234" 字段必须在config.js IotAgent JSON 文件中定义。
    • "cbroker":"172.18.0.3" 字段是 ORION IP 容器

    2 创建设备

        #!/bin/bash
    
        curl -X POST -H "Fiware-Service: myHome" -H "Fiware-ServicePath: /environment" -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{ 
        "devices": [ 
            { 
                "device_id": "sensor00", 
                "entity_name": "R2-D2", 
                "entity_type": "robot",
                "attributes": [
                  {
                    "name": "temperature",
                    "type": "celsius"
                  },
                  {
                    "name": "Position",
                    "type": "string"
                  }
                ]
            }
        ]
    }
    
    ' 'http://localhost:4041/iot/devices'
    

    3.向 OCB 发送测量值(使用 IotAgent-JSON 导致有效负载)

    mosquitto_pub -t /1234/sensor00/attrs -m '{"temperature": 40.2,"Position": "left"}'
    

    4.查询 Orion 以查看结果

    curl -X POST -H "Content-Type: application/json" -H "Accept: application/json" -H "Fiware-Service: myHome" -H "Fiware-ServicePath: /environment" -d '{
        "entities": [
            {
                "isPattern": "false",
                "id": "R2-D2",
                "type": "robot"
            }
        ]
    }' 'http://172.18.0.3:1026/NGSI10/queryContext'
    

    如果你解决了请告诉我。

    fernando.mendez@atos.net

    【讨论】:

    • F.Aragona你试过了吗?有消息吗?请告诉我。谢谢,费尔南多
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-01
    • 1970-01-01
    相关资源
    最近更新 更多