FIWARE / tutorials.IoT-Agent

:orange_book: FIWARE 202: Provisioning the Ultralight IoT Agent
https://fiware-iotagent-ul.rtfd.io
MIT License
13 stars 19 forks source link

Can’t[Provisioning a Smart Lamp] #32

Open hsakamoto123 opened 1 year ago

hsakamoto123 commented 1 year ago

Execution of the tutorial command results in an error.

$ curl -iX POST \
>   'http://localhost:4041/iot/devices' \
>   -H 'Content-Type: application/json' \
>   -H 'fiware-service: openiot' \
>   -H 'fiware-servicepath: /' \
>   -d '{
>   "devices": [
>     {
>       "device_id": "lamp001",
>       "entity_name": "urn:ngsi-ld:Lamp:001",
>       "entity_type": "Lamp",
>       "protocol": "PDI-IoTA-UltraLight",
>       "transport": "HTTP",
>       "endpoint": "http://iot-sensors:3001/iot/lamp001",
>       "commands": [
>         {"name": "on","type": "command"},
>         {"name": "off","type": "command"}
>        ],
>        "attributes": [
>         {"object_id": "s", "name": "state", "type":"Text"},
>         {"object_id": "l", "name": "luminosity", "type":"Integer"}
>        ],
>        "static_attributes": [
>          {"name":"refStore", "type": "Relationship","value": "urn:ngsi-ld:Store:001"}
>       ]
>     }
>   ]
> }
> '
HTTP/1.1 409 Conflict
X-Powered-By: Express
Fiware-Correlator: e2dfb1c9-f8fb-458c-8f53-ebc569e4fa87
Content-Type: application/json; charset=utf-8
Content-Length: 108
ETag: W/"6c-MXV0ASHmZcVgSm+giF5KNzT8oKg"
Date: Thu, 29 Dec 2022 13:41:59 GMT
Connection: keep-alive
Keep-Alive: timeout=5

{"name":"DUPLICATE_DEVICE_ID","message":"A device with the same pair (Service, DeviceId) was found:lamp001"}

The details of lamp001 at this time are as follows.

$ curl -iX GET \
>   'http://localhost:4041/iot/devices/lamp001' \
>   -H 'Content-Type: application/json' \
>   -H 'fiware-service: openiot' \
>   -H 'fiware-servicepath: /'
HTTP/1.1 200 OK
X-Powered-By: Express
Fiware-Correlator: 9137758e-ffec-4183-b2cb-64764bac4582
Content-Type: application/json; charset=utf-8
Content-Length: 170
ETag: W/"aa-DtkbAvlQAzPkTzVDNEWEFDN2uS0"
Date: Thu, 29 Dec 2022 14:23:32 GMT
Connection: keep-alive
Keep-Alive: timeout=5

{"device_id":"lamp001","service":"openiot","service_path":"/","entity_name":"Thing:lamp001","entity_type":"Thing","transport":"HTTP","commands":[],"static_attributes":[]}

I deleted lamp001 and tried again but got the same error.

$ curl -iX DELETE \
>   'http://localhost:4041/iot/devices/lamp001' \
>   -H 'fiware-service: openiot' \
>   -H 'fiware-servicepath: /'
HTTP/1.1 204 No Content
X-Powered-By: Express
Fiware-Correlator: 13850b76-210f-4fea-b43f-cd6922988d06
Date: Thu, 29 Dec 2022 14:21:27 GMT
Connection: keep-alive
Keep-Alive: timeout=5
-----
$ curl -iX GET \
>   'http://localhost:4041/iot/devices/lamp001' \
>   -H 'Content-Type: application/json' \
>   -H 'fiware-service: openiot' \
>   -H 'fiware-servicepath: /'
HTTP/1.1 404 Not Found
X-Powered-By: Express
Fiware-Correlator: 7969f9a8-8126-42c3-adf9-4e6ab03075c7
Content-Type: application/json; charset=utf-8
Content-Length: 75
ETag: W/"4b-K4b3wKcdA3xs//EOZUi47GwR6k0"
Date: Thu, 29 Dec 2022 14:21:52 GMT
Connection: keep-alive
Keep-Alive: timeout=5

{"name":"DEVICE_NOT_FOUND","message":"No device was found with id:lamp001"}

Please let me know how I can avoid this.

Added 2023/1/4: I don't know if it's directly related, but the container log has the following error.

{"log":"time=2023-01-04T14:15:51.229Z | lvl=ERROR | corr=ebf236d8-e24d-4dbd-9cfc-20fd509c4213 | trans=ebf236d8-e24d-4dbd-9cfc-20fd509c4213 | op=IOTAUL.AMQP.Binding | from=n/a | srv=n/a | subsrv=n/a | msg=connect ECONNREFUSED 127.0.0.1:5672 | comp=IoTAgent\n","stream":"stdout","time":"2023-01-04T14:15:51.234099317Z"}
hsakamoto123 commented 1 year ago

I was able to avoid it in the same way as the issue answer below. However, if you follow the steps in the tutorial in order, this event will occur. Therefore, I would like to change the order of the tutorials or add a note of caution.

https://github.com/FIWARE/tutorials.IoT-Agent-JSON/issues/7

DUPLICATE_DEVICE_ID would indicate that the device has already been correctly provisioned. Deleting it will remove it as you can see the count is reduced. What I suspect is happened here, is that the Lamp was ON and streaming data, once it was deleted the next request from the lamp (which had a known API Key) recreated the lamp as an [anonymous device](https://iotagent-node-lib.readthedocs.io/en/latest/getting-started.html#receiving-a-measure-from-an-anonymous-device). IIRC if the tutorial is running and any actuation has occurred dummy measures are sent periodically once a minute.

The key takeaway here is not to send measures to the IoT Agent until all devices are fully provisioned.