-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_helper.js
More file actions
56 lines (47 loc) · 1.39 KB
/
node_helper.js
File metadata and controls
56 lines (47 loc) · 1.39 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
'use strict';
/* Magic Mirror
* Module: MMM-mqtt_command
*
* By Paul Langdon
* MIT Licensed.
*/
const NodeHelper = require('node_helper');
var mqtt = require('mqtt');
module.exports = NodeHelper.create({
start: function() {
console.log('MMM-mqtt_command started ...');
},
getMqtt: function(payload) {
var self = this;
var client = mqtt.connect(payload.mqttServer);
client.on('connect', function() {
client.subscribe(payload.topic);
});
client.on('error', function(error) {
console.log('*** MQTT JS ERROR ***: ' + error);
self.sendSocketNotification('ERROR', {
type: 'notification',
title: 'MQTT Error',
message: 'The MQTT Client has generated an error: ' + error
});
});
client.on('offline', function() {
console.log('*** MQTT Client Offline ***');
self.sendSocketNotification('ERROR', {
type: 'notification',
title: 'MQTT Offline',
message: 'MQTT Server is offline.'
});
client.end();
});
client.on('message', function(topic, message) {
self.sendSocketNotification('MQTT_DATA', {'topic':topic, 'data':message.toString()});
// client.end();
});
},
socketNotificationReceived: function(notification, payload) {
if (notification === 'MQTT_SERVER') {
this.getMqtt(payload);
}
}
});