Fiware 物联网代理 UL:通过 mqtt 发送 command_value 到设备

Fiware IoT Agent UL: Send command_value via mqtt to device

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

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-broker。但我希望“100”的值也被传递?

我也找到了this documentation on Git regarding mqtt-commands. But that one does not specify where to send that and how? I suppose it uses the the same URL as this request: http://localhost:4041/v1/updateContext ??这是发布命令的最新方式吗?

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

有人可以帮我把 "color" 的值也放到 mqtt-message 中吗?

谢谢!

编辑 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"
    }

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

编辑2: 我在 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 更新上下文但没有值。

该问题有两个修复方法。与本教程不同的第一件事是您需要使用命令注册的提供者。而不是 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"
    }

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

1: POST 到 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 的帮助!