首页 > 解决方案 > Fiware JSON IoT 代理是否期望设备提供答案?

问题描述

我正在本地运行Fiware IoT Agent 示例。计划是将其连接到某种设备并进行演示。我正在使用请求箱来检查从 IoT 代理发送的请求。

启动环境后,./services start我调用这个 shell 脚本在代理中注册一个响铃设备,然后触发“ring”命令。

curl -iX POST \
  'http://localhost:4041/iot/devices' \
  -H 'Content-Type: application/json' \
  -H 'fiware-service: openiot' \
  -H 'fiware-servicepath: /' \
  -d '{
  "devices": [
    {
      "device_id": "bell001",
      "entity_name": "urn:ngsi-ld:Bell:001",
      "entity_type": "Bell",
      "protocol": "PDI-IoTA-UltraLight",
      "transport": "HTTP",
      "endpoint": "https://requestbin.fullcontact.com/zhygotzh/iot/bell001",
      "commands": [
        { "name": "ring", "type": "command" }
       ],
       "static_attributes": []
    }
  ]
}
'

curl -iX POST \
  'http://localhost:4041/v1/updateContext' \
  -H 'Content-Type: application/json' \
  -H 'fiware-service: openiot' \
  -H 'fiware-servicepath: /' \
  -d '{
    "contextElements": [
        {
            "type": "Bell",
            "isPattern": "false",
            "id": "urn:ngsi-ld:Bell:001",
            "attributes": [
                { "name": "ring", "type": "command", "value": "" }
            ],
            "static_attributes": []
        }
    ],
    "updateAction": "UPDATE"
}'

这很好用,我收到 200 响应,我可以在请求箱中看到请求。

$ ./setup-ul.sh
HTTP/1.1 201 Created
X-Powered-By: Express
Fiware-Correlator: 8298b65a-8550-4b6e-8a4d-21bc32abdf8a
Content-Type: application/json; charset=utf-8
Content-Length: 2
ETag: W/"2-vyGp6PvFo4RvsFtPoIWeCReyIC8"
Date: Fri, 29 Mar 2019 11:34:05 GMT
Connection: keep-alive

{}HTTP/1.1 200 OK
X-Powered-By: Express
Fiware-Correlator: c79a1146-05d8-4eb1-8e1c-bf19661cb403
Content-Type: application/json; charset=utf-8
Content-Length: 208
ETag: W/"d0-6+Ce6hwRVmP90ZI667iON6zHtdA"
Date: Fri, 29 Mar 2019 11:34:07 GMT
Connection: keep-alive

{"contextResponses":[{"contextElement":{"attributes":[{"name":"ring","type":"command","value":""}],"id":"urn:ngsi-ld:Bell:001","isPattern":false,"type":"Bell"},"statusCode":{"code":200,"reasonPhrase":"OK"}}]}

但是,请求采用 Ultra Light 格式。我更喜欢 JSON 格式。所以我想我只是替换 docker compose 文件中的图像。

  iot-agent:
    image: fiware/iotagent-ul:1.8.0
    hostname: iot-agent

更改为

  iot-agent:
    image: fiware/iotagent-json
    hostname: iot-agent

docker-compose.yml.

现在,当我使用更新的 docker-compose 文件尝试相同的操作时,我得到以下结果:

$ ./setup.sh
HTTP/1.1 201 Created
X-Powered-By: Express
Fiware-Correlator: 69d6a6ad-a44a-4e57-87dd-4e512d499fee
Content-Type: application/json; charset=utf-8
Content-Length: 2
ETag: W/"2-vyGp6PvFo4RvsFtPoIWeCReyIC8"
Date: Fri, 29 Mar 2019 11:44:36 GMT
Connection: keep-alive

{}HTTP/1.1 400 Bad Request
X-Powered-By: Express
Fiware-Correlator: 6c9a28b5-0505-456c-bcdb-841df2bc6f62
Content-Type: application/json; charset=utf-8
Content-Length: 388
ETag: W/"184-RqfzTJc6iD9nXX3kAbxZqwruJC0"
Date: Fri, 29 Mar 2019 11:44:37 GMT
Connection: keep-alive

{"contextResponses":[{"contextElement":{"contextElements":[{"type":"Bell","isPattern":"false","id":"urn:ngsi-ld:Bell:001","attributes":[{"name":"ring","type":"command","value":""}],"static_attributes":[]}],"updateAction":"UPDATE"},"statusCode":{"code":400,"reasonPhrase":"HTTP_COMMAND_RESPONSE_ERROR","details":"There was an error in the response of a device to a command [400 ]:null"}}]}

这似乎是相关部分:There was an error in the response of a device to a command [400 ]:null. 这是否意味着物联网代理期待来自“设备”的更具体的响应?

该请求可以在请求箱中看到,所以它工作得那么远。但是为什么代理会认为有问题呢?它是否需要一些特定的响应格式?

我也尝试将设备注册步骤中的“协议”链接到“PDI-IoTA-JSON”。那没有帮助。

标签: fiware

解决方案


问题在于 IoT 代理假定设备将使用 JSON 进行响应。Requestbin 的默认答案是字符串“ok”。这导致代理崩溃。

如果设备返回 ,则请求成功{},如果返回有关命令结果的一些信息,则更成功。例如

{
  "ring": "successfully rung"
}

推荐阅读