问题
I am using the IoT-agent for ultra light and I'd like to send a command (including a specific value) back to my IoT-Device. If I follow the example for a smart door here I can send something like:
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 publishes the payload "urn:ngsi-ld:pysense:002@color|" to my mqtt-broker. But I would expect the value of "100" to be passed as well?
I also found 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 ?? Is that the up-to-date way to issue commands?
I am using Orion-ld and the iotagent-ul:1.8.0.
Can somebody help me getting the value of "color" into the mqtt-message as well?
Thank you!
EDIT1:
I just tried orion:2.2.0 and reregistered the command like this:
{
"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
}
}
If i try to send the command orion-logs go crazy (keep scrolling) with the following text:
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
...
And I get the following error as a reply in Postman:
{
"error": "NotFound",
"description": "The requested entity has not been found. Check type and id"
}
When orion registers the command by itself it looks like this with the iot-agent as provider instead of 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"
}
With that I can actually send a command but it still misses the value. :(
EDIT2: Something I found in the extensive debug logs of 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)
After that the IoT-Agent updates the context but without a value.
回答1:
There are two fixes to that problem. First thing that differs from the tutorial is the provider you need to register with a command. Instead of orion:1026/v1 register the IoT-Agent. It should look something like this with the IOTA_PROVIDER_URL from your docker-compose.yml as provider:
{
"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"
}
Now you can send a command in two ways. The first one is directly to the north port of the IoT Agent with a POST command to orion/v1/updateContext. The second one is to update the command in the context by sending a PATCH command to the entities attributes, providing the type of the entity to the request.
1: POST to 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 adding the type of the entity
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"
}
}
Thanks to fgalan for the help with that!
来源:https://stackoverflow.com/questions/56427851/fiware-iot-agent-ul-send-command-value-via-mqtt-to-device