2018-04-08 23:03:12 +02:00
## Prerequisites
Before proceeding, install the following prerequisites:
- [Docker ](https://docs.docker.com/install/ )
- [Docker compose ](https://docs.docker.com/compose/install/ )
- [jsonpp ](https://jmhodges.github.io/jsonpp/ ) (optional)
Once everything is installed, execute the following commands from project root:
```bash
2018-04-18 16:49:10 +02:00
docker-compose -f docker/docker-compose.yml up -d
2018-04-08 23:03:12 +02:00
```
## User management
### Account creation
2018-04-11 14:30:37 +02:00
Use the Mainflux API to create user account:
```
2018-04-24 13:42:36 +02:00
curl -s -S -i --cacert docker/ssl/certs/mainflux-server.crt --insecure -X POST -H "Content-Type: application/json" https://localhost/users -d '{"email":"john.doe@email .com", "password":"123"}'
2018-04-11 14:30:37 +02:00
```
2018-04-18 22:36:24 +02:00
Note that when using official `docker-compose` , all services are behind `nginx`
2018-04-11 14:30:37 +02:00
proxy and all traffic is `TLS` encrypted.
2018-04-08 23:03:12 +02:00
### Obtaining an authorization key
2018-04-11 14:30:37 +02:00
In order for this user to be able to authenticate to the system, you will have
to create an authorization token for him:
```
2018-04-24 13:42:36 +02:00
curl -s -S -i --cacert docker/ssl/certs/mainflux-server.crt --insecure -X POST -H "Content-Type: application/json" https://localhost/tokens -d '{"email":"john.doe@email .com", "password":"123"}'
2018-04-11 14:30:37 +02:00
```
Response should look like this:
```
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MjMzODg0NzcsImlhdCI6MTUyMzM1MjQ3NywiaXNzIjoibWFpbmZsdXgiLCJzdWIiOiJqb2huLmRvZUBlbWFpbC5jb20ifQ.cygz9zoqD7Rd8f88hpQNilTCAS1DrLLgLg4PRcH-iAI"
}
```
2018-04-08 23:03:12 +02:00
## System provisioning
Before proceeding, make sure that you have created a new account, and obtained
an authorization key.
### Provisioning devices
2018-04-18 22:36:24 +02:00
Devices are provisioned by executing request `POST /clients` , with a
`"type":"device"` specified in JSON payload. Note that you will also need
`user_auth_token` in order to provision clients (both devices and application)
2018-04-11 14:30:37 +02:00
that belong to this particular user.
```
2018-04-24 13:42:36 +02:00
curl -s -S -i --cacert docker/ssl/certs/mainflux-server.crt --insecure -X POST -H "Content-Type: application/json" -H "Authorization: < user_auth_token > " https://localhost/clients -d '{"type":"device", "name":"weio"}'
2018-04-11 14:30:37 +02:00
```
2018-04-18 22:36:24 +02:00
Response will contain `Location` header whose value represents path to newly
2018-04-11 14:30:37 +02:00
created client:
```
HTTP/1.1 201 Created
2018-04-24 13:42:36 +02:00
Content-Type: application/json
2018-04-11 14:30:37 +02:00
Location: /clients/81380742-7116-4f6f-9800-14fe464f6773
Date: Tue, 10 Apr 2018 10:02:59 GMT
Content-Length: 0
```
2018-04-08 23:03:12 +02:00
### Provisioning applications
2018-04-18 22:36:24 +02:00
Applications are provisioned by executing HTTP request `POST /clients` , with
2018-04-11 14:30:37 +02:00
`"type":"app"` specified in JSON payload.
```
2018-04-24 13:42:36 +02:00
curl -s -S -i --cacert docker/ssl/certs/mainflux-server.crt --insecure -X POST -H "Content-Type: application/json" -H "Authorization: < user_auth_token > " https://localhost/clients -d '{"type":"app", "name":"myapp"}'
2018-04-11 14:30:37 +02:00
```
2018-04-18 22:36:24 +02:00
Response will contain `Location` header whose value represents path to newly
2018-04-11 14:30:37 +02:00
created client (same as for devices):
```
HTTP/1.1 201 Created
2018-04-24 13:42:36 +02:00
Content-Type: application/json
2018-04-11 14:30:37 +02:00
Location: /clients/cb63f852-2d48-44f0-a0cf-e450496c6c92
Date: Tue, 10 Apr 2018 10:33:17 GMT
Content-Length: 0
```
2018-04-08 23:03:12 +02:00
### Retrieving provisioned clients
2018-04-18 22:36:24 +02:00
In order to retrieve data of provisioned clients that is written in database, you
2018-04-11 14:30:37 +02:00
can send following request:
```
curl -s -S -i --cacert docker/ssl/certs/mainflux-server.crt --insecure -H "Authorization: < user_auth_token > " https://localhost/clients
```
2018-04-18 22:36:24 +02:00
Notice that you will receive only those clients that were provisioned by
2018-04-11 14:30:37 +02:00
`user_auth_token` owner.
```
HTTP/1.1 200 OK
2018-04-24 13:42:36 +02:00
Content-Type: application/json
2018-04-11 14:30:37 +02:00
Date: Tue, 10 Apr 2018 10:50:12 GMT
Content-Length: 1105
{
"clients": [
{
"id": "81380742-7116-4f6f-9800-14fe464f6773",
"type": "device",
"name": "weio",
"key": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MjMzNTQ1NzksImlzcyI6Im1haW5mbHV4Iiwic3ViIjoiODEzODA3NDItNzExNi00ZjZmLTk4MDAtMTRmZTQ2NGY2NzczIn0.5s8s1hlK-l30kQAyHxEZO_M2NIQw53MQuy7b3Wf3OOE"
},
{
"id": "cb63f852-2d48-44f0-a0cf-e450496c6c92",
"type": "app",
"name": "myapp",
"key": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MjMzNTYzOTcsImlzcyI6Im1haW5mbHV4Iiwic3ViIjoiY2I2M2Y4NTItMmQ0OC00NGYwLWEwY2YtZTQ1MDQ5NmM2YzkyIn0.FE6DWB3yJmBb8uojpQJaKUEbD0Elrjx0HhJA28bVzkU"
}
]
}
```
2018-04-08 23:03:12 +02:00
2018-04-18 23:06:11 +02:00
You can specify `offset` and `limit` parameters in order to fetch specific
group of clients. In that case, your request should look like:
2018-04-18 22:36:24 +02:00
```
2018-04-18 23:06:11 +02:00
curl -s -S -i --cacert docker/ssl/certs/mainflux-server.crt --insecure -H "Authorization: < user_auth_token > " https://localhost/clients?offset=0& limit=5
2018-04-18 22:36:24 +02:00
```
2018-04-18 23:06:11 +02:00
If you don't provide them, default values will be used instead: 0 for `offset` ,
and 10 for `limit` . Note that `limit` cannot be set to values greater than 100. Providing
invalid values will be considered malformed request.
2018-04-18 22:36:24 +02:00
2018-04-08 23:03:12 +02:00
### Removing clients
2018-04-11 14:30:37 +02:00
In order to remove you own client you can send following request:
```
curl -s -S -i --cacert docker/ssl/certs/mainflux-server.crt --insecure -X DELETE -H "Authorization: < user_auth_token > " https://localhost/clients/< client_id >
```
2018-04-08 23:03:12 +02:00
### Provisioning channels
2018-04-11 14:30:37 +02:00
Channels are provisioned by executing request `POST /channels` :
```
2018-04-24 13:42:36 +02:00
curl -s -S -i --cacert docker/ssl/certs/mainflux-server.crt --insecure -X POST -H "Content-Type: application/json" -H "Authorization: < user_auth_token > " https://localhost/channels -d '{"name":"mychan"}'
2018-04-11 14:30:37 +02:00
```
2018-04-18 22:36:24 +02:00
After sending request you should receive response with `Location` header that
2018-04-11 14:30:37 +02:00
contains path to newly created channel:
```
HTTP/1.1 201 Created
2018-04-24 13:42:36 +02:00
Content-Type: application/json
2018-04-11 14:30:37 +02:00
Location: /channels/19daa7a8-a489-4571-8714-ef1a214ed914
Date: Tue, 10 Apr 2018 11:30:07 GMT
Content-Length: 0
```
2018-04-08 23:03:12 +02:00
### Retrieving provisioned channels
2018-04-11 14:30:37 +02:00
To retreve provisioned channels you should send request to `/channels` with
authorization token in `Authorization` header:
```
curl -s -S -i --cacert docker/ssl/certs/mainflux-server.crt --insecure -H "Authorization: < user_auth_token > " https://localhost/channels
```
2018-04-18 22:36:24 +02:00
Note that you will receive only those channels that were created by authorization
token's owner.
2018-04-11 14:30:37 +02:00
```
HTTP/1.1 200 OK
2018-04-24 13:42:36 +02:00
Content-Type: application/json
2018-04-11 14:30:37 +02:00
Date: Tue, 10 Apr 2018 11:38:06 GMT
Content-Length: 139
{
"channels": [
{
"id": "19daa7a8-a489-4571-8714-ef1a214ed914",
"name": "mychan"
}
]
}
```
2018-04-18 23:06:11 +02:00
You can specify `offset` and `limit` parameters in order to fetch specific
group of channels. In that case, your request should look like:
2018-04-18 22:36:24 +02:00
```
curl -s -S -i --cacert docker/ssl/certs/mainflux-server.crt --insecure -H "Authorization: < user_auth_token > " https://localhost/channels?offset=0& limit=5
```
2018-04-18 23:06:11 +02:00
If you don't provide them, default values will be used instead: 0 for `offset` ,
and 10 for `limit` . Note that `limit` cannot be set to values greater than 100. Providing
invalid values will be considered malformed request.
2018-04-08 23:03:12 +02:00
### Removing channels
2018-04-11 14:30:37 +02:00
In order to remove specific channel you should send following request:
```
curl -s -S -i --cacert docker/ssl/certs/mainflux-server.crt --insecure -X DELETE -H "Authorization: < user_auth_token > " https://localhost/channels/< channel_id >
```
2018-04-08 23:03:12 +02:00
## Access control
2018-04-11 14:30:37 +02:00
Channel can be observed as a communication group of clients. Only clients that
are connected to the channel can send and receive messages from other clients
in this channel. Clients that are not connected to this channel are not allowed
to communicate over it.
2018-04-18 22:36:24 +02:00
Only user, who is the owner of a channel and of the clients, can connect the
clients to the channel (which is equivalent of giving permissions to these clients
2018-04-11 14:30:37 +02:00
to communicate over given communication group).
To connect client to the channel you should send following request:
```
curl -s -S -i --cacert docker/ssl/certs/mainflux-server.crt --insecure -X PUT -H "Authorization: < user_auth_token > " https://localhost/channels/< channel_id > /clients/< client_id >
```
You can observe which clients are connected to specific channel:
```
curl -s -S -i --cacert docker/ssl/certs/mainflux-server.crt --insecure -H "Authorization: < user_auth_token > " https://localhost/channels/< channel_id >
```
You should receive response with the lists of connected clients in `connected` field
similar to this one:
```
{
"id": "19daa7a8-a489-4571-8714-ef1a214ed914",
"name": "mychan",
"connected": [
{
"id": "81380742-7116-4f6f-9800-14fe464f6773",
"type": "device",
"name": "weio",
"key": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MjMzNTQ1NzksImlzcyI6Im1haW5mbHV4Iiwic3ViIjoiODEzODA3NDItNzExNi00ZjZmLTk4MDAtMTRmZTQ2NGY2NzczIn0.5s8s1hlK-l30kQAyHxEZO_M2NIQw53MQuy7b3Wf3OOE"
}
]
}
```
If you want to disconnect your device from the channel, send following request:
```
curl -s -S -i --cacert docker/ssl/certs/mainflux-server.crt --insecure -X DELETE -H "Authorization: < user_auth_token > " https://localhost/channels/< channel_id > /clients/< client_id >
```
2018-04-08 23:03:12 +02:00
## Sending messages
Once a channel is provisioned and client is connected to it, it can start to
publish messages on the channel. The following sections will provide an example
of message publishing for each of the supported protocols.
### HTTP
2018-04-11 14:30:37 +02:00
To publish message over channel, client should send following request:
```
2018-05-14 13:11:29 +02:00
curl -s -S -i --cacert docker/ssl/certs/mainflux-server.crt --insecure -X POST -H "Content-Type: application/senml+json" -H "Authorization: < client_token > " https://localhost/http/channels/< channel_id > /messages -d '[{"bn":"some-base-name:","bt":1.276020076001e+09, "bu":"A","bver":5, "n":"voltage","u":"V","v":120.1}, {"n":"current","t":-5,"v":1.2}, {"n":"current","t":-4,"v":1.3}]'
2018-04-11 14:30:37 +02:00
```
2018-04-18 13:09:01 +02:00
Note that you should always send array of messages in senML format.
### WebSocket
2018-04-18 22:36:24 +02:00
To publish and receive messages over channel using web socket, you should first
2018-04-18 13:09:01 +02:00
send handshake request to `/channels/<channel_id>/messages` path. Don't forget
2018-04-18 22:36:24 +02:00
to send `Authorization` header with client authorization token.
2018-04-18 13:09:01 +02:00
2018-04-18 22:36:24 +02:00
If you are not able to send custom headers in your handshake request, send it as
query parameter `authorization` . Then your path should look like this
2018-04-18 13:09:01 +02:00
`/channels/<channel_id>/messages?authorization=<client_auth_key>` .
2018-05-09 12:56:20 +02:00
### CoAP
These are two useful tools for sending CoAP messages:
- [Copper ](https://github.com/mkovatsc/Copper )
- [coap-cli ](https://github.com/mcollina/coap-cli )
To publish message over channel, client should send following confirmable message:
```
coap post coap://localhost/channels/< channel_id > /messages?key=< client_token >
```
Client token must be present in `Uri-Query` option. Otherwise, message will be considered unauthorized.
To subscribe to channel, client should send following confirmable message:
```
coap get coap://localhost/channels/< channel_id > /messages?key=< client_token > -o
```
where `-o` in indicates that value of `Observe` option is 0.
2018-05-14 13:11:29 +02:00
To unsubscribe from channel, client should send same message with value of `Observe` option set to 1, ignore confirmable message received from service (by default, server will attempt to send confirmable message multiple times, as described in [RFC 7252 ](https://tools.ietf.org/html/rfc7252#page-28 ) or send `Reset` message with appropriate token.