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.
Changes for new message formats, while maintaining compatibility with…
… staging, #1
- Loading branch information
Showing
2 changed files
with
72 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,78 @@ | ||
'use strict'; | ||
(function() { | ||
'use strict'; | ||
|
||
const mqtt = require('mqtt'); | ||
const util = require('util'); | ||
const EventEmitter = require('events'); | ||
const mqtt = require('mqtt'); | ||
const util = require('util'); | ||
const EventEmitter = require('events'); | ||
|
||
const Client = class Client extends EventEmitter { | ||
constructor(broker, appEUI, appAccessKey) { | ||
super(); | ||
this.appEUI = appEUI; | ||
const Client = class Client extends EventEmitter { | ||
constructor(broker, appEUI, appAccessKey) { | ||
super(); | ||
this.appEUI = appEUI; | ||
this.legacy = (broken === 'staging.thethingsnetwork.org'); | ||
this.client = mqtt.connect(util.format('mqtt://%s', broker), { | ||
username: appEUI, | ||
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)); | ||
} | ||
|
||
this.client = mqtt.connect(util.format('mqtt://%s', broker), { | ||
username: appEUI, | ||
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() { | ||
this.client.end(); | ||
} | ||
downlink(devEUI, payload, ttl, port) { | ||
var topic = util.format('%s/devices/%s/down', this.appEUI, devEUI); | ||
var payload_raw = payload.toString('base64'); | ||
var message = JSON.stringify(this.legacy ? { | ||
payload: payload_raw, | ||
ttl: ttl || '1h', | ||
port: port || 1 | ||
} : { | ||
payload_raw: payload_raw, | ||
port: port || 1 | ||
}); | ||
this.client.publish(topic, message); | ||
} | ||
|
||
downlink(devEUI, payload, ttl, port) { | ||
var topic = util.format('%s/devices/%s/down', this.appEUI, devEUI); | ||
var message = JSON.stringify({ | ||
payload: payload.toString('base64'), | ||
ttl: ttl || '1h', | ||
port: port || 1 | ||
}); | ||
this.client.publish(topic, message); | ||
} | ||
_connected() { | ||
super.emit('connect'); | ||
this.client.subscribe(['+/devices/+/activations', '+/devices/+/up']); | ||
} | ||
|
||
_connected() { | ||
super.emit('connect'); | ||
this.client.subscribe(['+/devices/+/activations', '+/devices/+/up']); | ||
} | ||
_handleMessage(topic, message) { | ||
var parts = topic.split('/'); | ||
var devEUI = parts[2]; | ||
var payload = JSON.parse(message.toString()); | ||
switch (parts[3]) { | ||
case 'activations': | ||
super.emit('activation', this.legacy ? { | ||
devEUI: devEUI | ||
} : payload); | ||
break; | ||
case 'up': | ||
super.emit('uplink', this.legacy ? { | ||
devEUI: devEUI, | ||
fields: payload.fields || { | ||
raw: payload.payload | ||
}, | ||
counter: payload.counter, | ||
port: payload.port, | ||
metadata: payload.metadata[0] | ||
} : Object.assign({ | ||
dev_eui: devEUI | ||
}, payload)); | ||
break; | ||
} | ||
} | ||
|
||
_handleMessage(topic, message) { | ||
var parts = topic.split('/'); | ||
var payload = JSON.parse(message.toString()); | ||
switch (parts[3]) { | ||
case 'activations': | ||
super.emit('activation', { | ||
devEUI: parts[2] | ||
}); | ||
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] | ||
}); | ||
break; | ||
_error(err) { | ||
super.emit('error', err); | ||
} | ||
} | ||
}; | ||
|
||
_error(err) { | ||
super.emit('error', err); | ||
} | ||
} | ||
module.exports = Client; | ||
|
||
module.exports = Client; | ||
})(); |