This repository was archived by the owner on Sep 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpubsub.js
More file actions
101 lines (82 loc) · 3.16 KB
/
pubsub.js
File metadata and controls
101 lines (82 loc) · 3.16 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var env = require('./env');
var mosca = require('mosca');
var moment = require('moment');
var Datastore = require('nedb');
var request = require('request');
var data = require('./data');
var devices = require('./devices');
var mqtt_regex = require('mqtt-regex');
var settings = {
port: env.PUBSUB_PORT,
http: {
port: env.PUBSUB_WS_PORT,
bundle: true,
static: './'
}
};
var server = new mosca.Server(settings);
server.on('ready', setup);
function setup() {
console.log('Mosca server is up and running on ' + env.PUBSUB_PORT);
}
server.on('clientConnected', function(client) {
console.log('client connected', client.id);
});
server.on('subscribed', function(topic, client) {
console.log('Client: ' + client.id + ' subscribed to: ' + topic);
});
server.on('published', function(packet, client) {
var sender = (client) ? client.id : 'Broker';
console.log('');
console.log('New message published');
console.log('Topic: ' + packet.topic);
console.log('From: ' + sender);
console.log('Payload: ', packet.payload.toString());
if (!client) return false;
Object.keys(messageHandlers).forEach(function(handler) {
var pattern = mqtt_regex(handler);
var results = pattern.regex.exec(packet.topic);
if(pattern.exec(packet.topic)) {
var fn = messageHandlers[handler];
fn(packet, client, pattern.getParams(results));
}
});
});
messageHandlers = {
'+device/ping': function(packet, client) {
devices.update({ _id: client.id }, { $set: { last_ping: moment().unix() } });
},
'+device/register': function(packet, client) {
var payload = JSON.parse(packet.payload.toString());
devices.update({ _id: client.id }, { $set: { label: payload.label, last_ping: moment().unix() } }, { upsert: true });
},
'+device/register/output': function(packet, client) {
var payload = JSON.parse(packet.payload.toString());
devices.update({ _id: client.id }, { $set: { output: payload.devices } });
},
'+device/register/input': function(packet, client) {
var payload = JSON.parse(packet.payload.toString());
devices.update({ _id: client.id }, { $set: { input: payload.devices } });
},
'+device/input/+sensor': function(packet, client, params) {
var payload = JSON.parse(packet.payload.toString());
var now = moment().unix();
data.insert({ sensor: params.sensor, value: payload.value, date: moment().unix(), device: client.id });
},
'+device/input/temp': function(packet, client, params) {
var payload = JSON.parse(packet.payload.toString());
console.log(payload);
var red = Math.floor(0 + (255 * (payload.value / 30)));
var green = Math.floor(255 - (255 * (payload.value / 50)));
var blue = Math.floor(0 + (255 * (payload.value / 300)));
var topic = params.device + '/output/strip/set';
server.publish({ topic: topic, payload: JSON.stringify({ r: 255, g: green, b: 0 }) });
},
'+device/output/+id/status': function(packet, client, params) {
var path = 'output.' + params.id;
var payload = JSON.parse(packet.payload.toString());
var value = parseInt(payload.value);
devices.update({ _id: client.id }, { $set: { [path]: { value: value } } });
},
};
module.exports = server;