简体   繁体   中英

Orion CB doesn't handle IoT agent replies

I'm developing a system in which I use Orion CB as main event bus making use also of the iotagent node lib in order to have an interface with third party APIs.

Now what happens is that I register a new device on the iotagent with only lazy attributes, one of which should be a json string.

Overriding the query handler and its subfunctions I tried both to stringify this json string and leaving it in json format and this is what i get:

1) Stringify the json:
When I query Orion to retrieve the entity it replies that that entity does not exist with status code 404. Looking on the iotagent debug log I see that its response is exactly what I was expecting (with all attributes well formatted) and it ends with "statusCode":{"code": 200, "reasonPhrase": "OK"} so it seems that the iotagent is replying to Orion with status code 200 (success!).

2) Leave the json as it is:
When I query Orion to retrieve the entity it replies with status code 200, all attributes are ok but the one with the json, that is empty. Again, looking at the iotagent debug log I see that the response is what I'd like to see querying to Orion and it ends with "statusCode":{"code": 200, "reasonPhrase": "OK"}.

What is happening here? What could be the problem in the first case? It seems that for some reason Orion doesn't like the iotagent response and it says that the entity doesn't exist. While in the second case i think the problem could be in the data structure that is not allowed (json as attribute value).

Am I missing something? I'd be glad if someone can help me with this trouble, let me know if you require more information.

Edit:

This is how I'm provisioning the device:

{
    "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"
                }
            ]
        }
    ]
}

The attribute in which I need to have the json is "taxiPosition", so its type is "StructuredValue". Creating the response I just use JSON.parse() on the payload retrieved by the 'device' and assign this value to the attribute like:

responses.push({
                name: 'taxiPositions',
                type: 'StructuredValue',
                value: [{...}, {...}]
        });

Where the value is an array of objects with simple one-level key-value pairs formatted like this:

{ "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": "" }

When I attempt to do it passing the value as a string the code is the same but the type for the attribute is "string" and the value is obtained by using JSON.stringify() on the previously parsed json.

To query the entity from Orion I simply make a call to /v2/entities/taxi_list

I tested some formats, and the two last are the best option: Here is an validate 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"
                }
            ]
}

Answer:

{
    "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"
                }
            ]
}

Answer:

{
    "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": "" }

                }
}

Answer:

{
    "error": "BadRequest",
    "description": "attribute must be a JSON object, unless keyValues option is used"
}

Best example

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": "" }


    }

Answer:

NONE

HTTP Answer:

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": {}
        }
    }
]

Other format:

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": {}
    }
}

Then, you can use:

?options=keyValues

The entity in this example "id": "taxi_list", is the more interesting. Because the Orion create the structured format, and Orion will validate. In the payload of tax_list3, the string in values will represent values, whithout an Orion validation.

Aragona]( https://stackoverflow.com/users/8362361/f-aragona )

What is your Fiware Orion version you use? For the IotAgent are you using the latest one?

By other hand it's highly recommend to use docker-compose to deploy all the servicies you are using unless 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"

For Mosquitto and for the IotAgent, you must install it from sources.

git clone https://github.com/telefonicaid/iotagent-json.git

Once cloned and installed it, you must set up config.js according your IP containers. Attached I give you some scripts in order to create an entity and a device. It's important to follow step by step in order to assign the device to the entity

Scripts

1.Create Service

    #!/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'

Note:

  • The "apikey": "1234" field must be define in config.js IotAgent JSON file.
  • The "cbroker":"172.18.0.3" field is the ORION IP container

2 Create Device

    #!/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.Send meassures to OCB (Using IotAgent-JSON cause payload)

mosquitto_pub -t /1234/sensor00/attrs -m '{"temperature": 40.2,"Position": "left"}'

4.Query to Orion to see results

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'

Please let me know if you solve it.

fernando.mendez@atos.net

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM