我正在开发一个系统,我使用Orion CB作为主事件总线,同时使用iotagent节点lib,以便与第三方API建立接口。
现在发生的事情是我在iotagent上注册了一个只有懒惰属性的新设备,其中一个应该是一个json字符串。
覆盖查询处理程序及其子函数我尝试将这个json字符串字符串化并将其保留为json格式,这就是我得到的结果:
1)对json进行字符串化:
当我查询Orion以检索实体时,它回复该实体不存在,状态代码为404.在iotagent调试日志上查看我看到它的响应正是我所期望的(所有属性格式良好),并以“ statusCode“:{”code“:200,”reasonPhrase“:”OK“}所以似乎iotagent正在回复Orion,状态代码为200(成功!)。
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”。创建响应我只是在'device'检索的有效负载上使用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": "" }
当我尝试将值作为字符串传递时,代码是相同的,但属性的类型是“string”,并且通过在先前解析的json上使用JSON.stringify()来获取值。
要从Orion查询实体,我只需调用/ v2 / entities / taxi_list
答案 0 :(得分:0)
我测试了一些格式,最后两个是最好的选择: 这是一个验证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": {}
}
}
然后,您可以使用:
?选项=键值
此示例中的实体" id":" taxi_list",更有趣。因为Orion创建了结构化格式,Orion将验证。在tax_list3的有效负载中,值中的字符串将表示值,而不进行Orion验证。
答案 1 :(得分:0)
阿拉戈纳](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"
IotAgent JSON文件中定义config.js
字段。"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