forked from gonzalocasas/ttn-microclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
56 lines (44 loc) · 1.38 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var fs = require('fs');
var ttn = require('ttn');
// Run the webserver on port 8080
server.listen(8080);
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
// Add Socket.io clients
io.on('connection', function (socket) {
socket.on('start', function (data) {
var appId = data.appId;
console.log('Connecting to AppId: ' + appId);
// Start the TTN Client
var client = new ttn.Client(data.region, appId, data.accessKey);
// Forward connection ok
client.on('connect', function (msg) {
console.log('Connected to TTN MQTT');
socket.emit('ttn-connect', {})
});
// Forward uplink
client.on('message', function (deviceId, data) {
console.log('Uplink from ' + deviceId + ': ' + data)
socket.emit('uplink', data)
});
// Forward activations
client.on('activation', function (evt) {
console.log('Activated Device: ' + evt.devEUI)
socket.emit('activation', evt)
});
// Forward errors
client.on('error', function (err) {
console.log('Error: ' + err);
socket.emit('ttn-error', { message: err.message });
});
// Close the TTN client on exit
socket.on('disconnect', function () {
client.end()
console.log('Disconnected');
});
});
});