Skip to content
This repository has been archived by the owner on Oct 21, 2020. It is now read-only.

Commit

Permalink
Merge pull request #4 from TheThingsNetwork/refactor
Browse files Browse the repository at this point in the history
Refactor for Dashboard V2, closes #1
  • Loading branch information
johanstokking committed Sep 7, 2016
2 parents 38e4110 + ace6fac commit 3414e84
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 42 deletions.
4 changes: 4 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"node": true,
"esnext": true
}
109 changes: 107 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,115 @@ This is the Node.js client for [The Things Network](https://www.thethingsnetwork
npm install --save ttn
```

## Documentation

A Quick Start and full API Reference can be found in [The Things Network Documentation](https://www.thethingsnetwork.org/docs/node-js/).

## Example

An [example](src/example.js) is included and can be run directly in the browser via [Tonic](https://tonicdev.com/npm/ttn).

## Documentation
### Monitor

A Quick Start and full API Reference can be found in [The Things Network Documentation](https://www.thethingsnetwork.org/docs/node-js/).
For a quick example run [bin/monitor](bin/monitor) and pass region, application ID and access key:

```bash
npm run monitor eu HELLO-WORLD 4rw/vLixroZHch8WNZVBAnmP9GEvuJ1QEB5fI2czlfo=

> ttn@2.0.0 monitor ~/node-ttn
> node bin/monitor "eu" "HELLO-WORLD" "4rw/vLixroZHch8WNZVBAnmP9GEvuJ1QEB5fI2czlfo="

[DEBUG] Region: eu
[DEBUG] Application ID: HELLO-WORLD
[DEBUG] Application Access Key: 4rw/vLixroZHch8WNZVBAnmP9GEvuJ1QEB5fI2czlfo=
[DEBUG] Connect: Packet {
cmd: 'connack',
retain: false,
qos: 0,
dup: false,
length: 2,
topic: null,
payload: null,
sessionPresent: false,
returnCode: 0 }
[INFO] Message: {
"dev_id": "MY-UNO",
"port": 1,
"counter": 63,
"payload_raw": "SGVsbG8=",
"payload_fields": {
"message": "Hello"
},
"metadata": {
"time": "2016-09-06T13:39:51.11186125Z",
"frequency": 868.1,
"modulation": "LORA",
"data_rate": "SF7BW125",
"coding_rate": "4/5",
"gateways": [
{
"eui": "B827EBFGFE87BD21",
"timestamp": 3746387779,
"time": "2016-09-06T13:39:51.077691Z",
"rssi": -76,
"snr": 7.2,
"rf_chain": 1
}
]
}
}
```
## Release Policies
### Pre-releases
If you'd like to do a pre-release this is how it works.
1. Bump package [version](https://docs.npmjs.com/cli/version) and add git tag:
- For the first pre-release of a version:
```bash
npm version pre[patch|minor|major]
```
- For consecutive pre-releases of the same version:
```bash
npm version prerelease
```
2. [Publish](https://docs.npmjs.com/cli/publish) to a pre-release stream (aka npm tag), e.g. `refactor`
```bash
npm publish --tag refactor
```
3. [Push](https://git-scm.com/docs/git-push) commits, including tags:
```bash
git push --follow-tags
```
### Releases
1. Bump package [version](https://docs.npmjs.com/cli/version) and add git tag:
```bash
npm version [patch|minor|major]
```
> **NOTE:** If the current version is a pre-release all of the above will simply remove the pre-release identifier. For example, if the current version is `2.0.0-3` then `npm version patch` will result in `2.0.0` and not `2.0.1`.
2. [Publish](https://docs.npmjs.com/cli/publish) package:
```bash
npm publish
```
3. [Push](https://git-scm.com/docs/git-push) commits, including tags:
```bash
git push --follow-tags
```
29 changes: 29 additions & 0 deletions bin/monitor
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env node

var ttn = require('../');

var region = process.argv[2];
var appId = process.argv[3];
var accessKey = process.argv[4];

console.log('[DEBUG]', 'Region:', region);
console.log('[DEBUG]', 'Application ID:', appId);
console.log('[DEBUG]', 'Application Access Key:', accessKey);

var client = new ttn.Client(region, appId, accessKey);

client.on('connect', function(connack) {
console.log('[DEBUG]', 'Connect:', connack);
});

client.on('error', function(err) {
console.error('[ERROR]', err.message);
});

client.on('activation', function(e) {
console.log('[INFO] ', 'Activation:', e.devEUI);
});

client.on('message', function(msg) {
console.info('[INFO] ', 'Message:', JSON.stringify(msg, null, 2));
});
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"main": "dist/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"monitor": "node bin/monitor",
"compile": "babel src --presets babel-preset-es2015 --out-dir dist",
"prepublish": "npm run compile"
},
Expand All @@ -20,10 +21,10 @@
"homepage": "https://github.com/TheThingsNetwork/node-ttn#readme",
"tonicExampleFilename": "dist/example.js",
"dependencies": {
"mqtt": "^1.10.0"
"mqtt": "^1.14.1"
},
"devDependencies": {
"babel-cli": "^6.9.0",
"babel-cli": "^6.14.0",
"babel-preset-es2015": "^6.14.0"
}
}
45 changes: 19 additions & 26 deletions src/client.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,59 @@
'use strict';

const mqtt = require('mqtt');
const util = require('util');
const EventEmitter = require('events');

const Client = class Client extends EventEmitter {
constructor(broker, appEUI, appAccessKey) {
constructor(region, appId, appAccessKey) {
super();
this.appEUI = appEUI;

var broker = (region.indexOf('.') !== -1) ? region : region + '.thethings.network';
this.appId = appId;
this.client = mqtt.connect(util.format('mqtt://%s', broker), {
username: appEUI,
username: appId,
password: appAccessKey
});
this.client.on('connect', this._connected.bind(this));
this.client.on('message', this._handleMessage.bind(this));
this.client.on('error', this._error.bind(this));
}

end() {
this.client.end();
end(...args) {
this.client.end(...args);
}

downlink(devEUI, payload, ttl, port) {
var topic = util.format('%s/devices/%s/down', this.appEUI, devEUI);
send(devId, payload, port) {
var topic = util.format('%s/devices/%s/down', this.appId, devId);
var payload_raw = payload.toString('base64');
var message = JSON.stringify({
payload: payload.toString('base64'),
ttl: ttl || '1h',
payload_raw: payload_raw,
port: port || 1
});
this.client.publish(topic, message);
}

_connected() {
super.emit('connect');
_connected(connack) {
super.emit('connect', connack);
this.client.subscribe(['+/devices/+/activations', '+/devices/+/up']);
}

_handleMessage(topic, message) {
var parts = topic.split('/');
var devId = parts[2];
var payload = JSON.parse(message.toString());
switch (parts[3]) {
case 'activations':
super.emit('activation', {
devEUI: parts[2]
});
super.emit('activation', payload);
break;
case 'up':
super.emit('uplink', {
devEUI: parts[2],
fields: payload.fields || { raw: payload.payload },
counter: payload.counter,
port: payload.port,
metadata: payload.metadata[0]
});
super.emit('message', Object.assign({
dev_id: devId
}, payload));
break;
}
}

_error(err) {
super.emit('error', err);
}
}
};

module.exports = Client;
module.exports = Client;
25 changes: 13 additions & 12 deletions src/example.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
var ttn = require('ttn');

var appEUI = '';
var region = 'eu';
var appId = '';
var accessKey = '';

var client = new ttn.Client('staging.thethingsnetwork.org', appEUI, accessKey);
var client = new ttn.Client(region, appId, accessKey);

client.on('connect', function() {
console.log('[DEBUG]', 'Connected');
client.on('connect', function(connack) {
console.log('[DEBUG]', 'Connect:', connack);
});

client.on('error', function(err) {
console.error('[ERROR]', err.message);
});

client.on('activation', function(e) {
console.log('[INFO] ', 'Activated: ', e.devEUI);
client.on('activation', function(data) {
console.log('[INFO] ', 'Activation:', data);
});

client.on('uplink', function(msg) {
console.info('[INFO] ', 'Uplink: ' + JSON.stringify(msg, null, 2));
client.on('message', function(data) {
console.info('[INFO] ', 'Message:', JSON.stringify(data, null, 2));
});

client.on('uplink', function(msg) {
client.on('message', function(data) {

// respond to every third message
if (msg.counter % 3 === 0) {
console.log('[DEBUG]', 'Downlink');
if (data.counter % 3 === 0) {
console.log('[DEBUG]', 'Sending');

var payload = new Buffer('4869', 'hex');
client.downlink(msg.devEUI, payload);
client.send(data.dev_id, payload, data.port);
}
});

0 comments on commit 3414e84

Please sign in to comment.