Atos-Research-and-Innovation / IoTagent-LoRaWAN

FIWARE IoT Agent for LoRaWAN protocol (with CayenneLpp and CBOR data models)
https://fiware-lorawan.readthedocs.io/en/latest/
Other
34 stars 40 forks source link

Issue jexl expressions #167

Open arturohu opened 1 year ago

arturohu commented 1 year ago

After running a group provisioning, with jexl expressions, and reporting as dummy device, the jexl expression doesn't seem to work properly.

Deploy stack:

cd examples/dummy-devices/
docker-compose up

Step 1:

curl --location --request POST 'localhost:4041/iot/services' \
--header 'fiware-service: smartgondor' \
--header 'fiware-servicePath: /environment' \
--header 'Content-Type: application/json' \
--data-raw '{
    "services": [{
        "entity_type": "WeatherObserved",
        "apikey": "",
        "resource": "70B3D57ED00006B2",
        "expressionLanguage": "jexl",
        "attributes": [{
                "object_id": "temperature_1",
                "name": "temperature",
                "type": "Number",
                "expression": "temperature_1 + 21.5"

            },
            {
                "object_id": "barometric_pressure_0",
                "name": "pressure",
                "type": "Number"
            },
            {
                "object_id": "relative_humidity_2",
                "name": "relative_humidity",
                "type": "Number",
                "expression": "${@relative_humidity_2 + 21.5}"
            }
        ],
        "internal_attributes": {
            "lorawan": {
                "application_server": {
                    "host": "mqtt",
                    "username": "admin",
                    "password": "password",
                    "provider": "chirpstack"
                },
                "app_eui": "70B3D57ED00006B2",
                "application_id": "demoChirpstack",
                "application_key": "BE6996EEE2B2D6AFFD951383C1F3C3BD",
                "data_model": "cayennelpp"
            }
        }
    }]
}' | jq

Step 2:

docker exec -ti dummy-devices_mqtt_1 mosquitto_pub -h mqtt -u admin -P password -t application/demoChirpstack/device/test_device/event/up -m '{
  "applicationID": "demoChirpstack",
  "applicationName": "test",
  "deviceName": "test_device",
  "devEUI": "test_device",
  "fPort": 5,
  "data": "AHMnSwFnARYCaFADAGQEAQAFAdc="
}'

Step 3:

curl --location --request GET 'http://localhost:4041/iot/devices' \
--header 'fiware-service: smartgondor' \
--header 'fiware-servicepath: /environment' | jq

Step 4:

curl --location --request GET 'http://localhost:1026/v2/entities/urn:WeatherObserved:test_device' \
--header 'fiware-service: smartgondor' \
--header 'fiware-servicepath: /environment' | jq

Result:

{
  "id": "urn:WeatherObserved:test_device",
  "type": "WeatherObserved",
...
  "relative_humidity": {
    "type": "Number",
    "value": 61.5,
    "metadata": {
      "TimeInstant": {
        "type": "DateTime",
        "value": "2022-09-20T11:23:17.444Z"
      }
    }
  },
  "temperature": {
    "type": "Number",
    "value": "temperature_1 + 21.5",
    "metadata": {
      "TimeInstant": {
        "type": "DateTime",
        "value": "2022-09-20T11:23:17.444Z"
      }
    }
  }
}

Jexl expression it isn't working in spite of the "expressionLanguage" attribute defined on group provisioning. Moreover, the legacy expression ("expression": "${@relative_humidity_2 + 21.5}") is working.

Maybe some bad configuration. I would like to set some ternary operator with jexl expressions.

chicco785 commented 1 year ago

@arturohu I am not sure this has something to do with the lorawan agent. I tend to think this more related with the https://github.com/telefonicaid/iotagent-node-lib.

  1. did you check the logs for the error
  2. have you tried setting jexl as default language for the agent (and just use jexl, you should not combine the two agents in a single device / group configuration)
arturohu commented 1 year ago

I have set the environment variable:

IOTA_DEFAULT_EXPRESSION_LANGUAGE=jexl

with this definition of attributes:

        "attributes": [{
                "object_id": "temperature_1",
                "name": "temperature",
                "type": "Number",
                "expression": "temperature_1 + 21.5"

            },
            {
                "object_id": "barometric_pressure_0",
                "name": "pressure",
                "type": "Number"
            },
            {
                "object_id": "relative_humidity_2",
                "name": "relative_humidity",
                "type": "Number",
                "expression": "relative_humidity_2 + 21.5"
            }
        ]

The output entity:

{
  "id": "urn:WeatherObserved:test_device",
  "type": "WeatherObserved",
...
  "relative_humidity": {
    "type": "Number",
    "value": 61.5,
    "metadata": {
      "TimeInstant": {
        "type": "DateTime",
        "value": "2022-09-20T12:54:24.693Z"
      }
    }
  },
  "temperature": {
    "type": "Number",
    "value": 49.3,
    "metadata": {
      "TimeInstant": {
        "type": "DateTime",
        "value": "2022-09-20T12:54:24.693Z"
      }
    }
  }
}

It is working! Thank you.

I think the "expressionLanguage" definition provisioned is being ignored.

chicco785 commented 1 year ago

assuming expressionLanguage is the correct parameter to pass. on my side, i can check that there is no point in side lorawan code where this parameter is eventually dropped (it should not be the case, but you never know).

chicco785 commented 1 year ago

https://github.com/Atos-Research-and-Innovation/IoTagent-LoRaWAN/blob/master/lib/iotagent-lora.js#L601 the issue may be here