Skip to content

Commit c63c9d7

Browse files
author
Fabrizio Mirabito
authored
Merge pull request #238 from arduino/feature/refactor
Version 0.6.0
2 parents 0807826 + 29c98c3 commit c63c9d7

34 files changed

+8178
-6724
lines changed

.babelrc

-19
This file was deleted.

.editorconfig

-12
This file was deleted.

.eslintrc.js

+26-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
module.exports = {
2-
"extends": "airbnb-base",
3-
"env": {
4-
"browser": true,
5-
"jest": true
6-
},
7-
"rules": {
8-
"no-await-in-loop": 0
9-
}
2+
env: {
3+
es6: true,
4+
},
5+
ignorePatterns: ['node_modules/*', 'lib/*', 'es/*'],
6+
parser: '@typescript-eslint/parser',
7+
extends: [
8+
'plugin:@typescript-eslint/recommended',
9+
'prettier/@typescript-eslint',
10+
'plugin:prettier/recommended',
11+
],
12+
parserOptions: {
13+
sourceType: 'module',
14+
project: './tsconfig.json',
15+
ecmaVersion: 2018,
16+
},
17+
rules: {
18+
'@typescript-eslint/explicit-function-return-type': 0,
19+
'@typescript-eslint/interface-name-prefix': 'off',
20+
'@typescript-eslint/no-use-before-define': 'warn',
21+
'@typescript-eslint/camelcase': 'warn',
22+
'@typescript-eslint/no-empty-interface': 'warn',
23+
'@typescript-eslint/no-empty-function': 'warn',
24+
'@typescript-eslint/no-namespace': 'warn',
25+
'prefer-rest-params': 'warn',
26+
'no-else-return': 'error',
27+
},
1028
};

.github/workflows/test.yaml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: test
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- master
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Disable EOL conversions
14+
run: git config --global core.autocrlf false
15+
16+
- name: Checkout
17+
uses: actions/checkout@master
18+
19+
- name: Integration tests
20+
uses: actions/setup-node@v1
21+
with:
22+
node-version: '12'
23+
- run: npm install
24+
- run: npm run lint
25+
- run: npm run test
26+
env:
27+
CLIENT_ID: ${{ secrets.CLIENT_ID }}
28+
CLIENT_SECRET: ${{ secrets.CLIENT_SECRET }}

.prettierrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"printWidth": 120,
3+
"singleQuote": true,
4+
"trailingComma": "es5"
5+
}

README.md

+142-66
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,164 @@
1+
12
[![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)
23
[![npm version](https://badge.fury.io/js/arduino-iot-js.svg)](https://badge.fury.io/js/arduino-iot-js)
34

45
# arduino-iot-js
6+
## Introduction
7+
This NPM module provides interaction with the Arduino IoT Cloud MQTT broker. It can be used both from the browser and node.js
58

6-
JS module providing interaction with Arduino Cloud
9+
The main features of this module are:
10+
- Connection/disconnection to Arduino IoT Cloud Broker using WebSocket
11+
- Send IoT Cloud *property* updates
12+
- Listen for IoT Cloud *property* updates made by other clients and/or devices
713

8-
## Installation
14+
If you are looking for a way to create, read, update, delete resources like
15+
- Devices
16+
- Things
17+
- Properties
18+
- Data Timeseries
919

10-
```bash
11-
$ npm install arduino-iot-js
12-
```
20+
please check the official [Javascript Rest API client](https://www.npmjs.com/package/@arduino/arduino-iot-client).
1321

14-
## How to use
15-
```javascript
16-
import ArduinoCloud from 'arduino-iot-js';
17-
18-
// connect establishes a connection with mqtt, using token as the password
19-
// options = {
20-
// host: 'BROKER_URL', // Default is wss.iot.arduino.cc
21-
// port: BROKER_PORT, // Default is 8443
22-
// ssl: true/false, // Default is true
23-
// token: 'YOUR_BEARER_TOKEN' // Required!
24-
// apiUrl: 'AUTH SERVER URL', // Default is https://api2.arduino.cc
25-
// onDisconnect: message => { /* Disconnection callback */ },
26-
// useCloudProtocolV2: true/false, // Default is false
27-
// }
28-
ArduinoCloud.connect(options).then(() => {
29-
// Connected
30-
});
22+
If you want to learn more about Arduino IoT Cloud architecture, check the official [getting started documentation](https://www.arduino.cc/en/IoT/HomePage).
3123

32-
ArduinoCloud.disconnect().then(() => {
33-
// Disconnected
34-
});
3524

36-
ArduinoCloud.subscribe(topic, cb).then(topic => {
37-
// Subscribed to topic, messaged fired in the cb
38-
});
3925

40-
ArduinoCloud.unsubscribe(topic).then(topic => {
41-
// Unsubscribed to topic
42-
});
26+
## Installation
4327

44-
ArduinoCloud.sendMessage(topic, message).then(() => {
45-
// Message sent
46-
});
28+
```bash
29+
$ npm install arduino-iot-js
30+
```
4731

48-
ArduinoCloud.openCloudMonitor(deviceId, cb).then(topic => {
49-
// Cloud monitor messages fired to cb
50-
});
32+
## How to use
33+
The MQTT connection over Websocket relies on Username / Password authentication. Under the hood, this module uses your user ID (plus a timestamp) as *Username* and needs a valid JWT Token as *Password*. You can use either a valid JWT token or just your API Credentials (*clientId* and *clientSecret*).
5134

52-
ArduinoCloud.writeCloudMonitor(deviceId, message).then(() => {
53-
// Message sent to cloud monitor
54-
});
35+
### How to import arduino-iot-js in your project
36+
Using a web application in the browser
37+
```javascript
38+
import { ArduinoIoTCloud } from 'arduino-iot-js'
39+
```
40+
Using nodejs
41+
```javascript
42+
const { ArduinoIoTCloud } = require('arduino-iot-js');
43+
```
5544

56-
ArduinoCloud.closeCloudMonitor(deviceId).then(topic => {
57-
// Close cloud monitor
58-
});
45+
### How to connect to Arduino IoT Cloud broker using API Credentials
46+
```javascript
47+
const { ArduinoIoTCloud } = require('arduino-iot-js');
48+
49+
const options = {
50+
clientId: "YOUR_CLIENT_ID",
51+
clientSecret: "YOUR_CLIENT_SECRET",
52+
onDisconnect: message => {
53+
console.error(message);
54+
}
55+
}
56+
57+
ArduinoIoTCloud.connect(options)
58+
.then(() => console.log("Connected to Arduino IoT Cloud broker"))
59+
.catch(error => console.error(error));
60+
```
5961

60-
// Send a property value to a device
61-
// - value can be a string, a boolean or a number
62-
// - timestamp is a unix timestamp, not required
63-
ArduinoCloud.sendProperty(thingId, name, value, timestamp).then(() => {
64-
// Property value sent
65-
});
62+
### How to listen for property value updates
63+
After a successful connection, you can listen for property updates.
64+
To do this you need:
65+
- The ID of the *Thing* the *property* belongs to. You can list all your things and properties using the [Javascript Rest API client](https://www.npmjs.com/package/@arduino/arduino-iot-client), calling the [GET Things endpoint](https://www.arduino.cc/reference/en/iot/api/index.html#api-ThingsV2-thingsV2List)
66+
- The *variable name* of the property you want to listen
6667

67-
// Register a callback on a property value change
68-
//
69-
ArduinoCloud.onPropertyValue(thingId, propertyName, updateCb).then(() => {
70-
// updateCb(message) will be called every time a new value is available. Value can be string, number, or a boolean depending on the property type
71-
});
68+
```javascript
69+
const { ArduinoIoTCloud } = require('arduino-iot-js');
70+
const thingId = "THING_ID"
71+
const variableName = "PROPERTY_NAME"
72+
73+
const options = {
74+
clientId: "YOUR_CLIENT_ID",
75+
clientSecret: "YOUR_CLIENT_SECRET",
76+
onDisconnect: message => {
77+
console.error(message);
78+
}
79+
}
80+
81+
ArduinoIoTCloud.connect(options)
82+
.then(() => {
83+
console.log("Connected to Arduino IoT Cloud broker");
84+
return ArduinoIoTCloud.onPropertyValue(thingId, variableName, showUpdates = value => console.log(value));
85+
})
86+
.then(() => console.log("Callback registered"))
87+
.catch(error => console.log(error));
88+
```
89+
Each time a new value is sent from the Device, the `counterUpdates` callback will be called.
7290

73-
// Re-connect with a new authentication token, keeping the subscriptions
74-
// to the Things topics
75-
ArduinoCloud.updateToken(newToken).then(() => {
76-
// Successful reconnection with the provided new token
91+
### How to disconnect from Arduino IoT Cloud Broker
92+
```javascript
93+
ArduinoCloud.disconnect()
94+
.then(() => console.log("Successfully disconnected"));
95+
```
96+
### How to send property values to the device
97+
To do this you need:
98+
- The ID of the *Thing* the *property* belongs to. You can list all your things and properties using the [Javascript Rest API client](https://www.npmjs.com/package/@arduino/arduino-iot-client), calling the [GET Things endpoint](https://www.arduino.cc/reference/en/iot/api/index.html#api-ThingsV2-thingsV2List)
99+
- The *variable name* of the property you want to set
100+
- Value can be either a string, a boolean or a number
101+
```javascript
102+
const { ArduinoIoTCloud } = require('arduino-iot-js');
103+
const thingId = "THING_ID"
104+
const variableName = "PROPERTY_NAME"
105+
106+
const options = {
107+
clientId: "YOUR_CLIENT_ID",
108+
clientSecret: "YOUR_CLIENT_SECRET",
109+
onDisconnect: message => {
110+
console.error(message);
111+
}
112+
}
113+
114+
ArduinoIoTCloud.connect(options).then(() => {
115+
console.log("Connected to Arduino IoT Cloud broker");
116+
ArduinoCloud.sendProperty(thingId, variableName, value).then(() => {
117+
console.log("Property value correctly sent");
118+
});
77119
});
78120

79121
```
80-
81-
## Run tests
82-
First of all you need a valid Hydra Arduino token, you can get it from [Arduino Create IoT Cloud](https://create.arduino.cc/cloud/)
83-
84-
Then you can use this token to run tests
85-
86-
```bash
87-
$ TOKEN=YOUR_HYDRA_TOKEN_HERE npm run test
122+
### How to listen to every user properties updates
123+
```javascript
124+
const { ArduinoIoTCloud } = require('arduino-iot-js');
125+
const ArduinoIoTApi = require('@arduino/arduino-iot-client');
126+
127+
const options = {
128+
clientId: "YOUR_CLIENT_ID",
129+
clientSecret: "YOUR_CLIENT_SECRET",
130+
onDisconnect: message => {
131+
console.error(message);
132+
}
133+
}
134+
135+
// Connect to Arduino IoT Cloud MQTT Broker
136+
ArduinoIoTCloud.connect(options)
137+
.then(() => {
138+
console.log("Connected to Arduino IoT Cloud MQTT broker");
139+
140+
// Init Arduino API Client
141+
const ArduinoIoTClient = ArduinoIoTApi.ApiClient.instance;
142+
ArduinoIoTClient.authentications['oauth2'].accessToken = ArduinoIoTCloud.getToken();
143+
144+
const thingsApi = new ArduinoIoTAPI.ThingsV2Api(ArduinoIoTClient);
145+
const propertiesAPI = new ArduinoIoTApi.PropertiesV2Api(ArduinoIoTClient);
146+
147+
return thingsApi.thingsV2List()
148+
.then(things => {
149+
things.forEach(thing => {
150+
propertiesAPI.propertiesV2List(thing.id)
151+
.then(properties => {
152+
properties.forEach(property => {
153+
ArduinoIoTCloud.onPropertyValue(thing.id, property.variable_name,
154+
showUpdates = value => console.log(property.variable_name + ": " + value))
155+
.then(() => console.log("Callback registered for " + property.variable_name))
156+
.catch(error => console.error(error));
157+
});
158+
})
159+
.catch(error => console.error(error));
160+
});
161+
});
162+
})
163+
.catch(error => console.error(error));
88164
```

examples/listenProperties/index.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const { ArduinoIoTCloud } = require('arduino-iot-js');
2+
const ArduinoIoTAPI = require('@arduino/arduino-iot-client');
3+
4+
options = {
5+
clientId: "YOUR_CLIENT_ID",
6+
clientSecret: "YOUR_CLIENT_SECRET",
7+
onDisconnect: message => {
8+
console.error(message);
9+
}
10+
}
11+
12+
// Connect to Arduino IoT Cloud MQTT Broker
13+
ArduinoIoTCloud.connect(options)
14+
.then(() => {
15+
console.log("Connected to Arduino IoT Cloud MQTT broker");
16+
17+
// Init Arduino API Client
18+
const ArduinoIoTClient = ArduinoIoTAPI.ApiClient.instance;
19+
ArduinoIoTClient.authentications['oauth2'].accessToken = ArduinoIoTCloud.getToken();
20+
21+
const thingsAPI = new ArduinoIoTAPI.ThingsV2Api(ArduinoIoTClient);
22+
const propertiesAPI = new ArduinoIoTAPI.PropertiesV2Api(ArduinoIoTClient);
23+
24+
thingsAPI.thingsV2List()
25+
.then(things => {
26+
things.forEach(thing => {
27+
propertiesAPI.propertiesV2List(thing.id)
28+
.then(properties => {
29+
properties.forEach(property => {
30+
ArduinoIoTCloud.onPropertyValue(thing.id, property.variable_name,
31+
showUpdates = update = value => console.log(property.variable_name+": "+value))
32+
.then(() => console.log("Callback registered for "+property.variable_name))
33+
.catch(error => console.error(error));
34+
});
35+
})
36+
.catch(error => console.error(error));
37+
});
38+
})
39+
.catch(error => console.error(error));
40+
})
41+
.catch(error => console.error(error));

0 commit comments

Comments
 (0)