首页 > 解决方案 > Fiware IoT Agent UL:通过 mqtt 向设备发送 command_value

问题描述

我正在使用超轻型物联网代理,我想将命令(包括特定值)发送回我的物联网设备。如果我按照此处智能门的示例,我可以发送如下内容:

curl -iX PATCH \
  'http://localhost:1026/v2/entities/urn:ngsi-ld:pysense:002/attrs' \
  -H 'Content-Type: application/json' \
  -H 'fiware-service: openiot' \
  -H 'fiware-servicepath: /' \
  -d '{
    "color": {
    "type" : "command",
    "value" : "100"
    }
  }'

Fiware 发布有效载荷“urn:ngsi-ld:pysense:002@color|” 给我的 mqtt 经纪人。但我希望“100”的值也会被传递?

我还在 Git 上找到有关 mqtt-commands 的文档。但是那个没有指定发送到哪里以及如何发送?我想它使用与此请求相同的 URL : http://localhost:4041/v1/updateContext ?? 这是发出命令的最新方式吗?

我正在使用 Orion-ld 和 iotagent-ul:1.8.0。

有人可以帮助我将“颜色”的值也纳入 mqtt 消息吗?

谢谢!

编辑1:

我刚刚尝试了 orion:2.2.0 并重新注册了这样的命令:

{
  "description": "Setting LED",
  "dataProvided": {
    "entities": [
      {
        "id": "urn:ngsi-ld:pysense:002", "type": "pysense"
      }
    ],
    "attrs": ["color", "brightness"]
  },
  "provider": {
    "http": {"url": "http://orion:1026/v2"},
    "legacyForwarding": true
  }
}

如果我尝试使用以下文本发送命令 orion-logs go crazy(继续滚动):

fiware-orion   | INFO@06:42:33  logMsg.h[1844]: Starting transaction from 172.18.1.1:35800/v2/entities/urn:ngsi-ld:pysense:002/attrs
fiware-orion   | INFO@06:42:33  rest.cpp[885]: Service Path 0: '/dresden'
fiware-orion   | INFO@06:42:33  connectionOperations.cpp[239]: Database Operation Successful (count: { _id.id: "urn:ngsi-ld:pysense:002", _id.servicePath: { $in: [ /^/dresden$/ ] } })
fiware-orion   | INFO@06:42:33  connectionOperations.cpp[94]: Database Operation Successful (query: { _id.id: "urn:ngsi-ld:pysense:002", _id.servicePath: { $in: [ /^/dresden$/ ] } })
fiware-orion   | INFO@06:42:33  connectionOperations.cpp[94]: Database Operation Successful (query: { $or: [ { entities.id: "urn:ngsi-ld:pysense:002", $or: [ { entities.type: "pysense" }, { entities.type: { $exists: false } } ], entities.isPattern: "false", entities.isTypePattern: { $ne: true }, expiration: { $gt: 1559716953 }, status: { $ne: "inactive" }, servicePath: { $in: [ /^$|^/#$|^/dresden/#$|^/dresden$/, null ] } }, { entities.isPattern: "true", entities.isTypePattern: { $ne: true }, expiration: { $gt: 1559716953 }, status: { $ne: "inactive" }, servicePath: { $in: [ /^$|^/#$|^/dresden/#$|^/dresden$/, null ] }, $where: function(){for (var i=0; i < this.entities.length; i++) {if (this.enti... }, { entities.isPattern: "false", entities.isTypePattern: true, expiration: { $gt: 1559716953 }, status: { $ne: "inactive" }, servicePath: { $in: [ /^$|^/#$|^/dresden/#$|^/dresden$/, null ] }, $where: function(){for (var i=0; i < this.entities.length; i++) {if (this.enti... }, { entities.isPattern: "true", entities.isTypePattern: true, expiration: { $gt: 1559716953 }, status: { $ne: "inactive" }, servicePath: { $in: [ /^$|^/#$|^/dresden/#$|^/dresden$/, null ] }, $where: function(){for (var i=0; i < this.entities.length; i++) {if (this.enti... } ] })
fiware-orion   | INFO@06:42:33  connectionOperations.cpp[177]: Database Operation Successful (query: { query: { $or: [ { contextRegistration.entities: { $in: [ { id: "urn:ngsi-ld:pysense:002", type: "pysense" }, { type: "pysense", id: "urn:ngsi-ld:pysense:002" } ] } }, { contextRegistration.entities.id: { $in: [] } } ], expiration: { $gt: 1559716953 }, contextRegistration.attrs.name: { $in: [ "color" ] }, servicePath: { $in: [ /^/dresden$/ ] } }, orderby: { _id: 1 } })
fiware-orion   | ERROR@06:42:33  postUpdateContext.cpp[553]: Internal Error (attribute 'color' not found)
fiware-orion   | INFO@06:42:33  logMsg.h[1844]: Starting transaction to http://orion:1026/v1/updateContext
fiware-orion   | INFO@06:42:33  httpRequestSend.cpp[592]: Sending message 8 to HTTP server: sending message of 433 bytes to HTTP server
fiware-orion   | INFO@06:42:33  logMsg.h[1844]: Starting transaction from 172.18.1.7:46738/v1/updateContext
fiware-orion   | INFO@06:42:33  rest.cpp[885]: Service Path 0: '/dresden'
fiware-orion   | INFO@06:42:33  connectionOperations.cpp[94]: Database Operation Successful (query: { _id.id: "urn:ngsi-ld:pysense:002", _id.type: "pysense", _id.servicePath: { $in: [ /^/dresden$/ ] } })
fiware-orion   | INFO@06:42:34  connectionOperations.cpp[94]: Database Operation Successful (query: { $or: [ { entities.id: "urn:ngsi-ld:pysense:002", $or: [ { entities.type: "pysense" }, { entities.type: { $exists: false } } ], entities.isPattern: "false", entities.isTypePattern: { $ne: true }, expiration: { $gt: 1559716953 }, status: { $ne: "inactive" }, servicePath: { $in: [ /^$|^/#$|^/dresden/#$|^/dresden$/, null ] } }, { entities.isPattern: "true", entities.isTypePattern: { $ne: true }, expiration: { $gt: 1559716953 }, status: { $ne: "inactive" }, servicePath: { $in: [ /^$|^/#$|^/dresden/#$|^/dresden$/, null ] }, $where: function(){for (var i=0; i < this.entities.length; i++) {if (this.enti... }, { entities.isPattern: "false", entities.isTypePattern: true, expiration: { $gt: 1559716953 }, status: { $ne: "inactive" }, servicePath: { $in: [ /^$|^/#$|^/dresden/#$|^/dresden$/, null ] }, $where: function(){for (var i=0; i < this.entities.length; i++) {if (this.enti... }, { entities.isPattern: "true", entities.isTypePattern: true, expiration: { $gt: 1559716953 }, status: { $ne: "inactive" }, servicePath: { $in: [ /^$|^/#$|^/dresden/#$|^/dresden$/, null ] }, $where: function(){for (var i=0; i < this.entities.length; i++) {if (this.enti... } ] })
fiware-orion   | INFO@06:42:34  connectionOperations.cpp[177]: Database Operation Successful (query: { query: { $or: [ { contextRegistration.entities: { $in: [ { id: "urn:ngsi-ld:pysense:002", type: "pysense" }, { type: "pysense", id: "urn:ngsi-ld:pysense:002" } ] } }, { contextRegistration.entities.id: { $in: [] } } ], expiration: { $gt: 1559716954 }, contextRegistration.attrs.name: { $in: [ "color" ] }, servicePath: { $in: [ /^/dresden$/ ] } }, orderby: { _id: 1 } })
fiware-orion   | INFO@06:42:34  logMsg.h[1844]: Starting transaction to http://orion:1026/v1/updateContext
...

我收到以下错误作为 Postman 的回复:

{
    "error": "NotFound",
    "description": "The requested entity has not been found. Check type and id"
}

当 orion 自己注册命令时,它看起来像这样,使用 iot-agent 作为提供者而不是 orion/v1:

    {
        "id": "5cf677b7c7ecc4737e1e0ec9",
        "expires": "2020-06-04T13:52:55.00Z",
        "dataProvided": {
            "entities": [
                {
                    "id": "urn:ngsi-ld:pysense:002",
                    "type": "pysense"
                }
            ],
            "attrs": [
                "color",
                "brightness"
            ]
        },
        "provider": {
            "http": {
                "url": "http://iot-agent:4041"
            },
            "supportedForwardingMode": "all",
            "legacyForwarding": true
        },
        "status": "active"
    }

有了它,我实际上可以发送一个命令,但它仍然错过了这个值。:(

EDIT2: 我在 orion 的大量调试日志中发现的东西:

fiware-orion   | INFO@07:35:09  connectionOperations.cpp[177]: Database Operation Successful (query: { query: { $or: [ { contextRegistration.entities: { $in: [ { id: "urn:ngsi-ld:pysense:002", type: "pysense" }, { type: "pysense", id: "urn:ngsi-ld:pysense:002" } ] } }, { contextRegistration.entities.id: { $in: [] } } ], expiration: { $gt: 1559720109 }, contextRegistration.attrs.name: { $in: [ "color" ] }, servicePath: { $in: [ /^/dresden$/ ] } }, orderby: { _id: 1 } })
fiware-orion   | ERROR@07:35:09  postUpdateContext.cpp[553]: Internal Error (attribute 'color' not found)

之后,IoT-Agent 更新上下文但没有值。

标签: fiwarefiware-orion

解决方案


该问题有两个修复方法。与教程不同的第一件事是您需要使用命令注册的提供程序。代替 orion:1026/v1 注册 IoT-Agent。使用 docker-compose.yml 中的 IOTA_PROVIDER_URL 作为提供者,它应该看起来像这样:

{
        "description": "Setting LED",
        "dataProvided": {
            "entities": [
                {
                    "id": "urn:ngsi-ld:pysense:002",
                    "type": "pysense"
                }
            ],
            "attrs": [
                "color",
                "brightness"
            ]
        },
        "provider": {
            "http": {
                "url": "http://iot-agent:4041"
            },
            "supportedForwardingMode": "all",
            "legacyForwarding": true
        },
        "status": "active"
    }

现在您可以通过两种方式发送命令。第一个是直接到 IoT 代理的北端口,使用 POST 命令到 orion/v1/updateContext。第二个是通过向实体属性发送 PATCH 命令来更新上下文中的命令,向请求提供实体的类型。

1:发布到 updateContext

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

2:PATCH添加实体的类型

curl -iX PATCH \
  'http://localhost:1026/v2/entities/urn_ngsi-ld:pysense:002/attrs?type=pysense' \
  -H 'Content-Type: application/json' \
  -H 'fiware-service: openiot' \
  -H 'fiware-servicepath: /' \
  -d '{
  "color": {
      "type" : "command",
      "value" : "90"
  }
}

感谢 fgalan 的帮助!


推荐阅读