enableiot / iotkit-api

External API service for enableiot.com
https://dashboard.us.enableiot.com
BSD 2-Clause "Simplified" License
21 stars 7 forks source link

Adding a device to a component does not work, or the endpoint + params are incorrect in the docs #2

Closed edgarsilva closed 9 years ago

edgarsilva commented 9 years ago

Hi,

I'm trying to add a component to a device as specified in this section of the wiki:

https://github.com/enableiot/iotkit-api/wiki/Device-Management#add-a-component-to-a-device

But I always get this error back when passing params:

{
"code": 401,
"message": "Not Authorized"
}

If I change the endpoint to GET /accounts/account_id/devices/<device_id> I can see the device info, also this endpoint returns a list of components /accounts/<account_id>/cmpcatalog?full=true

Validation seems to work, this is what I get with no params passed:

{
"code": 400,
"message": "Invalid request",
"errors": [
"cid is missing and it is required",
"name is missing and it is required",
"type is missing and it is required"
]
}

This is what I'm passing to the request as POST JSON:

{ 
    "cid": "dist.v1.0",
    "name": "dist",
    "type": "sensor"
}

One other thing the docs seem to be incorrect, since they specify to pass these params:

{ 
    "cid": "436e7e74-6771-4898-9057-26932f5eb7e1",// is this the component id (dist.v1.0)?
    "name": "temp",// is this a random name or the component dimension?
    "type": "temperature.v1.0" // should this be the component type (sensor or actuator)?
}

as you can see the cid value does not match any type of component id in the returned list of components. Also type should be sensor or actuator right? Lastly should the name be the dimension param specified in the component (name in the UI) or just any name for the relationship between the device and the component?

Any help would be appreciated, thanks.

mtomczew commented 9 years ago

You receive "Not Authorized", because you probably used user token, not device one (which is acquired during device activation process and cannot be retrieved again).

When you add component to a device, its type must be already defined in account catalog. In POST /accounts/{accountId}/devices/{deviceId}/components you should pass unique id (we suggest using UUID), custom name and type of component from available catalog.

Default component types are for example: temperature.v1.0, humidity.v1.0 and powerswitch.v1.0 which is an actuator (you can send messages back to device). You can retrieve list of catalog items using: https://github.com/enableiot/iotkit-api/wiki/Component-Types-Catalog#list-all-component-types-for-an-account

edgarsilva commented 9 years ago

@mtomczew thanks for the quick response, should I then create a new device and activate it to retrieve the device token? then add all components right away (since cannot be retrieved again) is this the correct way to do it?

Regarding the component types, that is what I'm using to list them. I'll try again activating a device and getting that device's token, I'll update here afterwards.

One last question, are you planning on having an endpoint (or other way) to retrieve the deviceToken for already activated devices? or is this a security measure? not being able to retrieve it again that is.

edgarsilva commented 9 years ago

Nice this seems to be working, I'm closing the issue.

As you mentioned I was using the user token instead of the device token, getting the device token fixed it.

Thanks for the help.

bbaltz505 commented 9 years ago

@edgarsilva, glad you got it working. Yes, normally, you would normally set your device-id and activate your device to get the device token. The device token is stored in data/device.json (or certs/token.json in older agents). You need to keep it as you currently can't retrieve or regenerate it. You use the device token to register components (e.g., 'temp') since they are device-specific. The device token is also required when submitting data. The user token is used in almost all other cases (e.g., getting device info, component info and executing data queries).

edgarsilva commented 9 years ago

mmm... Ok, thanks. Would be good to include this information somewhere in the docs, at least an explanation of device_token vs user_token, since I could not find anything regarding the difference and how to use those two; either in the wiki or the intel IoT site.

The exact same info in this issue would've saved me a couple of hours of testing and trial and error. So this info should be useful to other developers.

bbaltz505 commented 9 years ago

Thanks for the feedback. The token types are described in the API intro but it may help to link references to the tokens back to this page: https://github.com/enableiot/iotkit-api/wiki/Api-Home#authorization-tokens Actually, it sounds like you're not using the agent software on the device and doing everything using REST. In this case, you need to save the device token yourself after you get it by activating.

edgarsilva commented 9 years ago

You are correct, I'm using REST, and yeap already taking into account saving the device's token.

Yeah it can come in handy to cross reference with that page.

Thanks for the help and quick response, I will let you know if anything comes up.