telefonicaid / lightweightm2m-iotagent

IoT Agent accepting COAP requests. Designed to be a bridge between the OMA Lightweight M2M protocol and the NGSI interface.
https://fiware-iotagent-lwm2m.rtfd.io/
GNU Affero General Public License v3.0
24 stars 31 forks source link

Problem with virtual IoT devices #173

Open DiEVeXx opened 5 years ago

DiEVeXx commented 5 years ago

I'm trying to follow Robot's example: https://fiware-iotagent-lwm2m.readthedocs.io/en/latest/deviceProvisioning/

Using dynamic provisioning in the latest stable version of Fiware/Orion. Virtual Device data: (following this: https://github.com/telefonicaid/lwm2m-node-lib)

create /7392/0
set /7392/0 1 79
set /7392/0 2 "First robot here"
set /7392/0 3 "[0, 0]"
connect 127.0.0.1 5683 robot1 /
list

Here I show the Dockerfile:

volumes:
  mongodb:
services:
  iotagent:
    image: fiware/lightweightm2m-iotagent
    depends_on:
        - mongodb
    ports:
        - "4041:4041"
        - "5683:5683/udp"
    environment:
        - "IOTA_CB_HOST=orion"
        - "IOTA_CB_PORT=1026"
        - "IOTA_NORTH_PORT=4041"
        - "IOTA_REGISTRY_TYPE=mongodb"
        - "IOTA_MONGO_HOST=mongodb"
        - "IOTA_MONGO_PORT=27017"
        - "IOTA_MONGO_DB=iotagent-lwm2m"
    volumes:
        - "./config.js:/opt/iota-lwm2m/config.js"
        - "./omaInverseRegistry.json:/opt/iota-lwm2m/omaInverseRegistry.json"
        - "./omaRegistry.json:/opt/iota-lwm2m/omaRegistry.json"

  mongodb:
    image: mongo:3.6
    ports:
        - "27017:27017"
    command: --bind_ip_all --smallfiles
    volumes:
        - mongodb:/data

  orion:
    image: fiware/orion
    depends_on:
        - mongodb
    expose:
        - "1026"
    ports:
        - "1026:1026"
    command: -dbhost mongodb

Here I show the config.js file:

var config = {};

config.lwm2m = {
    logLevel: 'DEBUG',
    port: '5683',
    defaultType: 'Robot',
    ipProtocol: 'udp4',
    serverProtocol: 'udp4',
    delayedObservationTimeout: 50,
    formats: [
        {
            name: 'application-vnd-oma-lwm2m/tlv',
            value: 11542
        },
        {
            name: 'application-vnd-oma-lwm2m/json',
            value: 11543
        },
        {
            name: 'application-vnd-oma-lwm2m/opaque',
            value: 11544
        }
    ],
    writeFormat: 'application-vnd-oma-lwm2m/opaque',
    types: []
};

config.ngsi = {
    logLevel: 'DEBUG',
    contextBroker: {
    host: 'orion',
        port: '1026'
    },
    server: {port: '4041'},
    deviceRegistry: {type: 'mongo'},
    mongodb: {
        host: 'mongo',
        port: '27017',
        db: 'mongo-iotagent'
    },
    types: {},
    service: 'Default',
    subservice: '/default',
    providerUrl: 'http://iotagent:4041',
    deviceRegistrationDuration: 'P1M'
};
module.exports = config;

retrieve devices provisioning returns:

    "count": 1,
    "devices": [
        {
            "device_id": "robot1",
            "service": "Factory",
            "service_path": "/robots",
            "entity_name": "robot1",
            "entity_type": "Robot",
            "attributes": [
                {
                    "object_id": "Battery",
                    "name": "Battery",
                    "type": "number"
                }
            ],
            "lazy": [
                {
                    "object_id": "Message",
                    "name": "Message",
                    "type": "string"
                }
            ],
            "commands": [
                {
                    "object_id": "Position",
                    "name": "Position",
                    "type": "location"
                }
            ],
            "static_attributes": [],
            "internal_attributes": {
                "lwm2mResourceMapping": {
                    "Battery": {
                        "objectType": 7392,
                        "objectInstance": 0,
                        "objectResource": 1
                    },
                    "Message": {
                        "objectType": 7392,
                        "objectInstance": 0,
                        "objectResource": 2
                    },
                    "Position": {
                        "objectType": 7392,
                        "objectInstance": 0,
                        "objectResource": 3
                    }
                }
            }
        }
    ]
}

retrieve entities v2:

  'http://192.168.1.32:1026/v2/entities?limit=50' \
  -H 'Postman-Token: 4195a240-4eab-4698-b9f2-200490f59514' \
  -H 'cache-control: no-cache' \
  -H 'fiware-service: Factory' \
  -H 'fiware-servicepath: /robots'

returns me:

[
    {
        "id": "robot1",
        "type": "Robot",
        "Battery": {
            "type": "number",
            "value": "79",
            "metadata": {}
        },
        "Position_info": {
            "type": "commandResult",
            "value": " ",
            "metadata": {}
        },
        "Position_status": {
            "type": "commandStatus",
            "value": "UNKNOWN",
            "metadata": {}
        }
    }
]

Well, now i cant access to lazy values or modify them

curl -X POST \
  http://192.168.1.32:1026/v1/queryContext \
  -H 'Accept: application/json' \
  -H 'Content-Type: application/json' \
  -H 'Postman-Token: 5ea5ae61-6367-46d0-864c-027e153d2d28' \
  -H 'cache-control: no-cache' \
  -H 'fiware-service: Factory' \
  -H 'fiware-servicepath: /robots' \
  -d '{
    "entities": [
        {
            "type": "Robot",
            "isPattern": "false",
            "id": "robot1"
        }
    ],
    "attributes": [
        "Message"
    ]
}'

returns:

{
    "errorCode": {
        "code": "404",
        "reasonPhrase": "No context element found",
        "details": "No device was found with id:robot1"
    }
}

Server log when i try to get a lazy value :

iotagent_1  | time=2019-02-07T15:45:24.412Z | lvl=DEBUG | corr=622b203e-2aef-11e9-9039-0242ac130004 | trans=6f5738c9-ef56-4a40-ac1b-92c4a1592987 | op=IoTAgentNGSI.GenericMiddlewares | srv=factory | subsrv=/robots | msg=Request for path [//queryContext] from [iotagent:4041] | comp=IoTAgent
iotagent_1  | time=2019-02-07T15:45:24.412Z | lvl=DEBUG | corr=622b203e-2aef-11e9-9039-0242ac130004 | trans=6f5738c9-ef56-4a40-ac1b-92c4a1592987 | op=IoTAgentNGSI.GenericMiddlewares | srv=factory | subsrv=/robots | msg=Body:
iotagent_1  | {
iotagent_1  |     "entities": [
iotagent_1  |         {
iotagent_1  |             "type": "Robot",
iotagent_1  |             "isPattern": "false",
iotagent_1  |             "id": "robot1"
iotagent_1  |         }
iotagent_1  |     ],
iotagent_1  |     "attributes": [
iotagent_1  |         "Message"
iotagent_1  |     ]
iotagent_1  | }
iotagent_1  |  | comp=IoTAgent
iotagent_1  | time=2019-02-07T15:45:24.412Z | lvl=DEBUG | corr=622b203e-2aef-11e9-9039-0242ac130004 | trans=6f5738c9-ef56-4a40-ac1b-92c4a1592987 | op=IoTAgentNGSI.ContextServer | srv=factory | subsrv=/robots | msg=Handling query from [iotagent:4041] | comp=IoTAgent
iotagent_1  | time=2019-02-07T15:45:24.413Z | lvl=DEBUG | corr=622b203e-2aef-11e9-9039-0242ac130004 | trans=6f5738c9-ef56-4a40-ac1b-92c4a1592987 | op=IoTAgentNGSI.MongoDBDeviceRegister | srv=factory | subsrv=/robots | msg=Looking for device with name [robot1]. | comp=IoTAgent
iotagent_1  | time=2019-02-07T15:45:24.415Z | lvl=DEBUG | corr=4fcfe24e-6c43-470d-864d-beb1b52cf383 | trans=4fcfe24e-6c43-470d-864d-beb1b52cf383 | op=IoTAgentNGSI.MongoDBDeviceRegister | srv=n/a | subsrv=n/a | msg=Device [robot1] not found. | comp=IoTAgent
iotagent_1  | time=2019-02-07T15:45:24.415Z | lvl=DEBUG | corr=4fcfe24e-6c43-470d-864d-beb1b52cf383 | trans=4fcfe24e-6c43-470d-864d-beb1b52cf383 | op=IoTAgentNGSI.ContextServer | srv=n/a | subsrv=n/a | msg=There was an error handling the query: [object Object]. | comp=IoTAgent
iotagent_1  | time=2019-02-07T15:45:24.415Z | lvl=DEBUG | corr=4fcfe24e-6c43-470d-864d-beb1b52cf383 | trans=4fcfe24e-6c43-470d-864d-beb1b52cf383 | op=IoTAgentNGSI.ContextServer | srv=n/a | subsrv=n/a | msg=Query error [DEVICE_NOT_FOUND] handling request: No device was found with id:robot1 | comp=IoTAgent
orion_1     | time=Thursday 07 Feb 15:45:24 2019.416Z | lvl=WARN | corr=622b203e-2aef-11e9-9039-0242ac130004 | trans=1549550994-444-00000000096 | from=192.168.1.32 | srv=factory | subsrv=/robots | comp=Orion | op=httpRequestSend.cpp[625]:httpRequestSendWithCurl | msg=Notification response NOT OK, http code: 404
iotagent_1  | time=2019-02-07T15:45:24.416Z | lvl=DEBUG | corr=622b203e-2aef-11e9-9039-0242ac130004 | trans=6f5738c9-ef56-4a40-ac1b-92c4a1592987 | op=IoTAgentNGSI.DomainControl | srv=factory | subsrv=/robots | msg=response-time: 5 | comp=IoTAgent

I could also show the log when I try to modify the value of the 'Message' lazy attribute. but this issue seems long enough.

It works flawless using this docker images: iotagent: hopu/lwm2m-iotagent:observe-and-read orion: fiware/orion:2.0.0

dcalvoalonso commented 5 years ago

As you can see in https://fiware-iotagent-lwm2m.readthedocs.io/en/latest/deviceProvisioning/, you should use fiware-service in the headers of your requests in lowercase. So instead of Factory, please use factory.

Also confirm the name of the entity in the Context Broker. In my tests, instead of robot1 is Robot:robot1.

DiEVeXx commented 5 years ago

Hi, after the changes I'm having new problems. I just deleted all my DB and deleted my service provisioning. I just changed the fiware-service to factory and removed the line in the dynamic device provisioning "entity_name": "robot1", to make the system concatenate the entity_type:device_id for the id.

Orion returns in retrieve entities:

[
    {
        "id": "Robot:robot1",
        "type": "Robot",
        "Battery": {
            "type": "number",
            "value": " ",
            "metadata": {}
        },
        "Position_info": {
            "type": "commandResult",
            "value": " ",
            "metadata": {}
        },
        "Position_status": {
            "type": "commandStatus",
            "value": "UNKNOWN",
            "metadata": {}
        },
        "Message": {
            "type": "string",
            "value": "First robot here",
            "metadata": {}
        },
        "Position": {
            "type": "string",
            "value": "[0, 0]",
            "metadata": {}
        }
    }
]

First question: Why is IoTAgent uploading lazy attributes to Orion? So now, my queryContext request for a lazy attribute is this:

curl -X POST \
  http://192.168.1.32:1026/v1/queryContext \
  -H 'Accept: application/json' \
  -H 'Content-Type: application/json' \
  -H 'Postman-Token: 00efb112-2917-404a-8fc1-da6de93716a5' \
  -H 'cache-control: no-cache' \
  -H 'fiware-service: factory' \
  -H 'fiware-servicepath: /robots' \
  -d '{
    "entities": [
        {
            "type": "Robot",
            "isPattern": "false",
            "id": "Robot:robot1"
        }
    ],
    "attributes": [
        "Message"
    ]
}'

it returns:

{
    "contextResponses": [
        {
            "contextElement": {
                "type": "Robot",
                "isPattern": "false",
                "id": "Robot:robot1",
                "attributes": [
                    {
                        "name": "Message",
                        "type": "string",
                        "value": "First robot here"
                    }
                ]
            },
            "statusCode": {
                "code": "200",
                "reasonPhrase": "OK"
            }
        }
    ]
}

But when I try to update the lazy 'Message' value using:

curl -X POST \
  http://192.168.1.32:1026/v1/updatecontext \
  -H 'Accept: application/json' \
  -H 'Content-Type: application/json' \
  -H 'Postman-Token: 1ccc1d90-9172-4a85-93aa-5a2893a4fb61' \
  -H 'cache-control: no-cache' \
  -H 'fiware-service: factory' \
  -H 'fiware-servicepath: /robots' \
  -d '{
    "contextElements": [
        {
            "type": "Robot",
            "isPattern": "false",
            "id": "Robot:robot1",
            "attributes": [
                {
                    "name": "Message",
                    "type": "string",
                    "value": "SOS!"
                }
            ]
        }
    ],
    "updateAction": "UPDATE"
}'

I get:

{
    "errorCode": {
        "code": "404",
        "reasonPhrase": "No context element found",
        "details": "error forwarding update"
    }
}

this is the log:

iotagent_1  | time=2019-02-08T08:08:49.250Z | lvl=DEBUG | corr=c3caf7e0-2b78-11e9-a143-0242ac130004 | trans=7e0537ee-3c70-4692-bef3-a3d673bdb436 | op=IoTAgentNGSI.GenericMiddlewares | srv=factory | subsrv=/robots | msg=Request for path [//updateContext] from [iotagent:4041] | comp=IoTAgent
iotagent_1  | time=2019-02-08T08:08:49.250Z | lvl=DEBUG | corr=c3caf7e0-2b78-11e9-a143-0242ac130004 | trans=7e0537ee-3c70-4692-bef3-a3d673bdb436 | op=IoTAgentNGSI.GenericMiddlewares | srv=factory | subsrv=/robots | msg=Body:
iotagent_1  | {
iotagent_1  |     "contextElements": [
iotagent_1  |         {
iotagent_1  |             "type": "Robot",
iotagent_1  |             "isPattern": "false",
iotagent_1  |             "id": "Robot:robot1",
iotagent_1  |             "attributes": [
iotagent_1  |                 {
iotagent_1  |                     "name": "Message",
iotagent_1  |                     "type": "string",
iotagent_1  |                     "value": "SOS!"
iotagent_1  |                 }
iotagent_1  |             ]
iotagent_1  |         }
iotagent_1  |     ],
iotagent_1  |     "updateAction": "UPDATE"
iotagent_1  | }
iotagent_1  |  | comp=IoTAgent
iotagent_1  | time=2019-02-08T08:08:49.251Z | lvl=DEBUG | corr=c3caf7e0-2b78-11e9-a143-0242ac130004 | trans=7e0537ee-3c70-4692-bef3-a3d673bdb436 | op=IoTAgentNGSI.ContextServer | srv=factory | subsrv=/robots | msg=Handling update from [iotagent:4041] | comp=IoTAgent
iotagent_1  | time=2019-02-08T08:08:49.251Z | lvl=DEBUG | corr=c3caf7e0-2b78-11e9-a143-0242ac130004 | trans=7e0537ee-3c70-4692-bef3-a3d673bdb436 | op=IoTAgentNGSI.ContextServer | srv=factory | subsrv=/robots | msg=[object Object] | comp=IoTAgent
iotagent_1  | time=2019-02-08T08:08:49.251Z | lvl=DEBUG | corr=c3caf7e0-2b78-11e9-a143-0242ac130004 | trans=7e0537ee-3c70-4692-bef3-a3d673bdb436 | op=IoTAgentNGSI.MongoDBDeviceRegister | srv=factory | subsrv=/robots | msg=Looking for device with name [Robot:robot1]. | comp=IoTAgent
iotagent_1  | time=2019-02-08T08:08:49.255Z | lvl=DEBUG | corr=31f6217e-704c-48ce-a59a-47a1ff244ee4 | trans=31f6217e-704c-48ce-a59a-47a1ff244ee4 | op=IoTAgentNGSI.MongoDBGroupRegister | srv=n/a | subsrv=n/a | msg=Looking for group params ["service","subservice","type"] with queryObj {"service":"factory","subservice":"/robots","type":"Robot"} | comp=IoTAgent
iotagent_1  | time=2019-02-08T08:08:49.256Z | lvl=DEBUG | corr=31f6217e-704c-48ce-a59a-47a1ff244ee4 | trans=31f6217e-704c-48ce-a59a-47a1ff244ee4 | op=IoTAgentNGSI.MongoDBGroupRegister | srv=n/a | subsrv=n/a | msg=Device group for fields [["service","subservice","type"]] not found: [{"service":"factory","subservice":"/robots","type":"Robot"}] | comp=IoTAgent
iotagent_1  | time=2019-02-08T08:08:49.257Z | lvl=ERROR | corr=31f6217e-704c-48ce-a59a-47a1ff244ee4 | trans=31f6217e-704c-48ce-a59a-47a1ff244ee4 | op=IoTAgentNGSI.Alarms | srv=n/a | subsrv=n/a | msg=Raising [MONGO-ALARM]: {"name":"DEVICE_GROUP_NOT_FOUND","message":"Couldn\t find device group","code":404} | comp=IoTAgent
iotagent_1  | time=2019-02-08T08:08:49.257Z | lvl=DEBUG | corr=31f6217e-704c-48ce-a59a-47a1ff244ee4 | trans=31f6217e-704c-48ce-a59a-47a1ff244ee4 | op=IoTAgentNGSI.DeviceService | srv=n/a | subsrv=n/a | msg=deviceData: {"lazy":[{"name":"Message","type":"string","object_id":"Message"}],"active":[{"name":"Battery","type":"number","object_id":"Battery"}],"commands":[{"name":"Position","type":"location","object_id":"Position"}],"staticAttributes":[],"subscriptions":[],"_id":"5c5d373b99391b001423dff5","creationDate":"2019-02-08T08:00:59.203Z","id":"robot1","type":"Robot","name":"Robot:robot1","service":"factory","subservice":"/robots","registrationId":"5c5d373be5fdde598ad1b046","internalId":null,"internalAttributes":{"lwm2mResourceMapping":{"Battery":{"objectType":7392,"objectInstance":0,"objectResource":1},"Message":{"objectType":7392,"objectInstance":0,"objectResource":2},"Position":{"objectType":7392,"objectInstance":0,"objectResource":3}}}} | comp=IoTAgent
iotagent_1  | time=2019-02-08T08:08:49.257Z | lvl=DEBUG | corr=31f6217e-704c-48ce-a59a-47a1ff244ee4 | trans=31f6217e-704c-48ce-a59a-47a1ff244ee4 | op=IoTAgentNGSI.DeviceService | srv=n/a | subsrv=n/a | msg=deviceData2: {"lazy":[{"name":"Message","type":"string","object_id":"Message"}],"active":[{"name":"Battery","type":"number","object_id":"Battery"}],"commands":[{"name":"Position","type":"location","object_id":"Position"}],"staticAttributes":[],"subscriptions":[],"_id":"5c5d373b99391b001423dff5","creationDate":"2019-02-08T08:00:59.203Z","id":"robot1","type":"Robot","name":"Robot:robot1","service":"factory","subservice":"/robots","registrationId":"5c5d373be5fdde598ad1b046","internalId":null,"internalAttributes":[{"lwm2mResourceMapping":{"Battery":{"objectType":7392,"objectInstance":0,"objectResource":1},"Message":{"objectType":7392,"objectInstance":0,"objectResource":2},"Position":{"objectType":7392,"objectInstance":0,"objectResource":3}}}]} | comp=IoTAgent
iotagent_1  | time=2019-02-08T08:08:49.257Z | lvl=DEBUG | corr=31f6217e-704c-48ce-a59a-47a1ff244ee4 | trans=31f6217e-704c-48ce-a59a-47a1ff244ee4 | op=IOTAgent.NGSIHandlers | srv=n/a | subsrv=n/a | msg=Handling device data update from the north port for device [Robot:robot1] of type [Robot] | comp=IoTAgent
iotagent_1  | time=2019-02-08T08:08:49.258Z | lvl=DEBUG | corr=31f6217e-704c-48ce-a59a-47a1ff244ee4 | trans=31f6217e-704c-48ce-a59a-47a1ff244ee4 | op=IOTAgent.NGSIHandlers | srv=n/a | subsrv=n/a | msg=New attributes;
iotagent_1  | [object Object] | comp=IoTAgent
iotagent_1  | time=2019-02-08T08:08:49.258Z | lvl=DEBUG | corr=31f6217e-704c-48ce-a59a-47a1ff244ee4 | trans=31f6217e-704c-48ce-a59a-47a1ff244ee4 | op=IoTAgentNGSI.MongoDBDeviceRegister | srv=n/a | subsrv=n/a | msg=Looking for device with name [Robot:robot1]. | comp=IoTAgent
iotagent_1  | time=2019-02-08T08:08:49.260Z | lvl=ERROR | corr=31f6217e-704c-48ce-a59a-47a1ff244ee4 | trans=31f6217e-704c-48ce-a59a-47a1ff244ee4 | op=IoTAgentNGSI.Alarms | srv=n/a | subsrv=n/a | msg=Releasing [MONGO-ALARM] | comp=IoTAgent
iotagent_1  | time=2019-02-08T08:08:49.260Z | lvl=DEBUG | corr=31f6217e-704c-48ce-a59a-47a1ff244ee4 | trans=31f6217e-704c-48ce-a59a-47a1ff244ee4 | op=IoTAgentNGSI.MongoDBDeviceRegister | srv=n/a | subsrv=n/a | msg=Looking for device with name [Robot:robot1]. | comp=IoTAgent
iotagent_1  | time=2019-02-08T08:08:49.262Z | lvl=DEBUG | corr=31f6217e-704c-48ce-a59a-47a1ff244ee4 | trans=31f6217e-704c-48ce-a59a-47a1ff244ee4 | op=IoTAgentNGSI.MongoDBGroupRegister | srv=n/a | subsrv=n/a | msg=Selecting ID from the stored device data | comp=IoTAgent
iotagent_1  | time=2019-02-08T08:08:49.262Z | lvl=DEBUG | corr=31f6217e-704c-48ce-a59a-47a1ff244ee4 | trans=31f6217e-704c-48ce-a59a-47a1ff244ee4 | op=LWM2MLib.DeviceManagement | srv=n/a | subsrv=n/a | msg=Writting a new value [SOS!] on resource /7392/0/2 in device [2] | comp=IoTAgent
orion_1     | time=Friday 08 Feb 08:08:54 2019.246Z | lvl=ERROR | corr=c3caf7e0-2b78-11e9-a143-0242ac130004 | trans=1549612847-482-00000000028 | from=192.168.1.32 | srv=factory | subsrv=/robots | comp=Orion | op=postUpdateContext.cpp[190]:updateForward | msg=Runtime Error (error 'Timeout was reached' forwarding 'Update' to providing application)

Thank you in advance.