This repository has been archived by the owner on Oct 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from TheThingsNetwork/refactor
Refactor for Dashboard V2, closes #1
- Loading branch information
Showing
6 changed files
with
175 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"node": true, | ||
"esnext": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); |