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 removing a device #165

Closed arturohu closed 2 years ago

arturohu commented 2 years ago

After running a group provisioning, and reporting a device that automatically creates a device in IoTAgent-LoRaWAN, an error occurs when trying to delete it.

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",
            "attributes": [
                {
                    "object_id": "temperature_1",
                    "name": "temperature",
                    "type": "Number"
                },
                {
                    "object_id": "barometric_pressure_0",
                    "name": "pressure",
                    "type": "Number"
                },
                {
                    "object_id": "relative_humidity_2",
                    "name": "relative_humidity",
                    "type": "Number"
                }
            ],
            "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

Step 5:

curl --location --request DELETE 'http://localhost:4041/iot/devices/test_device' \
--header 'fiware-service: smartgondor' \
--header 'fiware-servicepath: /environment'

IoTAgent-LoRaWAN log:

time=2022-08-02T07:04:35.487Z | lvl=FATAL | corr=n/a | trans=n/a | op=IoTAgentNGSI.Global | from=n/a | srv=n/a | subsrv=n/a | msg=An unexpected exception has been raised. Ignoring: TypeError: Cannot read property 'app_eui' of undefined
    at removeDevice (/opt/iotagent-lora/lib/iotagent-lora.js:511:57)
    at applyRemoveDeviceHandler (/opt/iotagent-lora/node_modules/iotagent-node-lib/lib/services/northBound/deviceProvisioningServer.js:287:13)
    at nextTask (/opt/iotagent-lora/node_modules/async/dist/async.js:5327:14)
    at next (/opt/iotagent-lora/node_modules/async/dist/async.js:5334:9)
    at /opt/iotagent-lora/node_modules/async/dist/async.js:972:16
    at Function.<anonymous> (/opt/iotagent-lora/node_modules/iotagent-node-lib/lib/services/northBound/deviceProvisioningServer.js:278:17)
    at /opt/iotagent-lora/node_modules/iotagent-node-lib/lib/services/common/alarmManagement.js:105:29
    at /opt/iotagent-lora/node_modules/iotagent-node-lib/lib/services/devices/deviceRegistryMongoDB.js:241:13
    at handleGet (/opt/iotagent-lora/node_modules/iotagent-node-lib/lib/services/devices/deviceRegistryMongoDB.js:202:13)
    at /opt/iotagent-lora/node_modules/mongoose/lib/model.js:4609:16
    at /opt/iotagent-lora/node_modules/mongoose/lib/utils.js:276:16
    at /opt/iotagent-lora/node_modules/mongoose/lib/query.js:4323:11
    at /opt/iotagent-lora/node_modules/kareem/index.js:135:16
    at processTicksAndRejections (internal/process/task_queues.js:79:11) | comp=IoTAgent

I think it is because of the line:

https://github.com/Atos-Research-and-Innovation/IoTagent-LoRaWAN/blob/master/lib/iotagent-lora.js#L508

device.internalAttributes is an array. However, during provisioning _internalattributes is a dict on the examples.

chicco785 commented 2 years ago

good catch, when updating the agent, we had to deal with the new expected internalAttributes format of the iot agent lib, will ensuring backward compatibility. while update and register device where updated correctly, delete was not.

chicco785 commented 2 years ago

@arturohu please confirm that the issue is solved :) thx

arturohu commented 2 years ago

It seems to be working on master docker image!!!

Thank you!