Search code examples
entityfiwarefiware-cygnus

Fiware Entities and STH


I am using the Orion Context Broker, an IoT Agent and Cygnus to handle and persist the data of several devices into a MongoDB. It's working, but I don't know if I'm doing it in the Fiware way, because after reading the documentation I am confused yet about some things:

  1. I don't completely understand the difference between an Entity and an IoT Entity (or device?). My guess is that is a matter of how they provide context data and the nature of the entity modelled, but I would be grateful if someone could clarify it. I am especially confused because the creation of each entity type is different (it seems that I can't initialize an IoT entity at creation time, which I can when dealing with a regular Entity).
  2. I can only persist the data of IoT Entities. Is it possible to have a Short Term History of a regular Entity?
  3. I don't understand why the STH data is repeating attributes that have not changed. If I have an IoT Entity with two attributes, 'a' and 'b', and I modify both of them, a STH entry is created for each one, which is fine. However, if then I change the value of attribute 'b', two more registers are created: one for 'a' (which hasn't changed and is reflecting the same value that it already had) and one for 'b'. Could someone explain to me this behavior?

Solution

  • 1. Entities vs IoT Entities

    I assume that what you mean by an IoT entity is the entry made by the IoT Agent upon receiving a sensor reading from a provisioned device.

    Logically there is no difference between an entity created and maintained by an IoT Agent and an entity created and maintained by any other service making NGSI request to the context broker.

    Your so-called IoT Entity is merely a construct where an IoT agent does all the heavy lifting for you and converts the data coming from a device in a propitiatory format into the NGSI standard.

    2. Short Term History of a regular Entity

    To create Short Term History you will need a separate Generic Enabler such as STH-Comet or QuantumLeap. Both of these enablers receive updates from Orion using the subscriptions mechanism. If you set up your IoT data using one fiware-service header and set up your non-IoT data using another fiware-service you can easily set up a subscription to differentiate between the two.

    e.g. the following subscription:

    curl -iX POST \
      'http://localhost:1026/v2/subscriptions/' \
      -H 'Content-Type: application/json' \
      -H 'fiware-service: iotdata' \
      -H 'fiware-servicepath: /' \
      -d '<body>'
    

    Will only apply to entities with the iotdata service path, which would be created when you provision your IoT service.

    3. Repeating attributes that have not changed.

    The <body> of the subscription can be used to narrow down the conditions under which the historical data is persisted.

    The entities, conditions and the attrs are the important part of the subject

    subject": {
        "entities": [
          {
            "idPattern": "Motion.*"
          }
        ],
        "condition": {
          "attrs": [
            "count"
          ]
        }
      },
      "notification": {
        "http": {
          "url": "http://quantumleap:8668/v2/notify"
        },
        "attrs": [
          "count"
        ],
        "metadata": ["dateCreated", "dateModified"]
      },
      "throttling": 1
    }'
    

    The subscription defined above will only fire if the count attribute is changed and only persist the count attribute. If you do not limit your attrs then multiple lines will be persisted to the database. Similarly if you do not limit the condition then multiple entries of count will be persisted when other attributes are updated.