Skip to content

Commit

Permalink
Update Cloudv2 docs
Browse files Browse the repository at this point in the history
Signed-off-by: hubmartin <[email protected]>
  • Loading branch information
hubmartin committed May 31, 2024
1 parent 685fe66 commit 0147e8d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 120 deletions.
77 changes: 19 additions & 58 deletions cloud/cloud-v2/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,81 +6,42 @@ import Image from '@theme/IdealImage';

# API

For Cloud v2 API please see the [**API Swagger documentation**](https://api.prod.hardwario.cloud/v2/documentation/index.html).
For Cloud v2 API please see the [**API Swagger documentation**](https://api.hardwario.cloud/v2/documentation/).

For real-time delivery of devices' messages we strongly recommend using HTTP callback, please see the [**Connectors**](connectors.md).

## Python API examples
## API Examples

Currently, Cloud v2 API does not support fixed API Keys, so you have to log in using your credentials and space name. See the examples below.
### List devices in space with cURL

### Log-in and list devices
Please create an API key and fill it in `<api-key>`, also set correct `<space-id>`.

This example logs in to the Cloud v2 production portal (`api.prod.hardwario.cloud`). It gets Space ID, then it get list of all devices in this space.
```
curl -X GET "https://api.hardwario.cloud/v2/spaces/<space-id>/devices" -H 'accept: application/json' -H 'X-API-Key: <api-key>' -H 'Content-Type: application/json'
```

### List devices in space with Python

```python
#!/usr/bin/env python3

import requests
import json

email = ''
password = ''
space_id = ''

base_url = 'https://api.prod.hardwario.cloud'

# Login
resp = requests.post(base_url + '/v2/auth/login',
json={"email": email, "password": password})

if resp.status_code != 200:
raise Exception('Login failed')
api_key = ...
space_id = ...
base_url = 'https://api.hardwario.cloud'

authorization = 'Bearer ' + resp.json()['access_token']

print(authorization)
def get_devices():
data = requests.get(f'{base_url}/v2/spaces/{space_id}/devices?limit=500',
headers={'X-API-Key': api_key}).json()

resp = requests.get(f'{base_url}/v2/space/{space_id}/devices',
headers={'Authorization': authorization})
for item in data:
print(item["id"], item["name"])

print(resp.status_code)

if resp.status_code != 200:
raise Exception('Device read failed:' + resp.text)

print(resp.json())
```

### Create devices

```python
if __name__ == "__main__":
get_devices()

# Copy the log-in from the first example code

# Create device from list in space

devices = [
{
"name": "dev-001",
"serial_number": "2112345678",
"token": "hie1koh5toh0ga6iedoJ1hahshohmoh7"
},
{
"name": "dev-002",
"serial_number": "2112345679",
"token": "hie1koh5toh0ga6iedoJ1hahshohmoh7",
"tags": [{"id": "5f6b5b4e-3b4c-4c4c-8c8c-8c8c8c8c8c8c"}],
"external_id": "1234567890"
}
]

for device in devices:
resp = requests.post(f'{base_url}/v2/space/{space_id}/devices', json=device,
headers={'Authorization': authorization})

if resp.status_code != 201:
raise Exception('Device create failed:' + resp.text)

print(resp.json())
```
11 changes: 8 additions & 3 deletions cloud/cloud-v2/connectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,22 @@ In the connector preview tab, you can select one of the latest messages and in r

![](connector-preview.png)

In the **Advanced** tab in the **Connectors**, you might change how many times the **HARDWARIO Cloud** tries to call your callback and in which intervals.

## JavaScript

Every message passing through the Cloud is handled by a JavaScript. Users can add further logic or completely reformat JSON which is sent. In script, you can also access all the information from the device like name, tags and labels. You can change HTTP request headers, change URL or completely stop the callback.

```js
function main(job) {
let body = job.message.body
return {
"url": "https://best.app.ever/new-message",
"method": "POST",
"body": JSON.stringify(job),
"header": { "Content-Type": "application/json" }
"url": "https://...",
"header": {
"Content-Type": "application/json"
},
"data": body
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion cloud/cloud-v2/devices.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Tags are used to connect a group of devices to the connector. So also create at

### QR Code Scan

You can also use **SCAN DEVICE** button in the bottom of the **CREATE NEW DEVICE** dialog and use the phone or computer camera to scan the device QR Code. The HSN and Claim token is filled from the code.
You can use **SCAN DEVICE** button at the bottom of the **CREATE NEW DEVICE** dialog and use the phone or computer camera to scan the device QR Code. The HSN and Claim token is filled from the code.

### Manually

Expand Down
56 changes: 0 additions & 56 deletions cloud/cloud-v2/downlink.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,59 +33,3 @@ In the messages or device detail, you can click on the **shell** icon and the sh
In the console, you can enter **single or multiple commands** that will be executed next time **CHESTER** will boot, send data or poll to the cloud. After that you will receive the **response of every message** and the response appears in the console. You don't need to keep the console window open. You can schedule commands and return later or the next day to see the results.

![](shell-console.png)

## API Examples

You can send downlink commands and configuration also over API. Please create an API key first in the HARDWARIO Cloud **Keys** section.

### Data

```bash
#/bin/bash

# Run with
# API_KEY="xxx" DEVICE_ID="xxx" ./codec/control.sh

set -x

curl -X 'POST' \
'https://api.prod.hardwario.cloud/v2/messages' \
-H 'accept: application/json' \
-H "Authorization: ApiKey $API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"body": {
"output_1_state": 0,
"output_2_state": 0,
"output_3_state": 0,
"output_4_state": 0
},
"device_id": "'"$DEVICE_ID"'",
"type": "data"
}'
```

### Config

```bash
#/bin/bash

# Run with
# API_KEY="xxx" DEVICE_ID="xxx" ./codec/config.sh

set -x

curl -X 'POST' \
'https://api.prod.hardwario.cloud/v2/messages' \
-H 'accept: application/json' \
-H "Authorization: ApiKey $API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"body": [
"app config analog-interval-sample 60",
"app config analog-interval-aggreg 300",
],
"device_id": "'"$DEVICE_ID"'",
"type": "config"
}'
```
8 changes: 6 additions & 2 deletions cloud/cloud-v2/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Image from '@theme/IdealImage';

Thanks to this, even low-power battery devices can be controlled and configured remotely with:
- [**Web interface**](https://prod.hardwario.cloud/)
- [**API**](https://api.prod.hardwario.cloud/v2/documentation/index.html)
- [**API**](api.md)

**Cloud v2** improves the device's codecs. In Cloud v1 codecs had to be set manually, in v2 codecs are directly in the device's firmware and they are uploaded automatically.

Expand All @@ -33,7 +33,11 @@ You can name your device so it can fill in for example its address or a specific

### Tags

Tags are useful for organizing similar devices together, or you can create a callback and connect it to the tag. So tagged devices will trigger your callback when sending uplink messages. You can have multiple tags assigned to a device.
Tags are useful for organizing similar devices together, or you can create a callback and connect it to the tag. So tagged devices will trigger your callback when sending uplink messages.

Tags might also help you filter and categorize different types of devices.

You can have multiple tags assigned to a device.

### Labels

Expand Down

0 comments on commit 0147e8d

Please sign in to comment.