-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
107 lines (99 loc) · 3.14 KB
/
index.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
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
102
103
104
105
106
107
'use strict';
var mqtt = require('mqtt');
var request = require('request');
function MQTThook (brokerUrl, options = {}) {
var client = mqtt.connect(brokerUrl, options.connection);
this._mqttClient = client;
this._triggers = {};
this._promise = new Promise(resolve => {
client.on('connect', () => { resolve(); });
});
client.on('message', (topic, message) => {
var triggers = this._triggers;
try {
var data = JSON.parse(message.toString());
data = typeof data === 'object' ? data : {};
if (triggers[topic] &&
((typeof triggers[topic].if === 'function' && triggers[topic].if(data)) ||
(triggers[topic].if === undefined)) &&
typeof triggers[topic].trigger === 'function') {
triggers[topic].trigger(data);
}
} catch (e) {
console.error(e.message);
}
});
}
MQTThook.prototype = {
_promise: null,
_mqttClient: null,
_triggers: null, // { if: function() {}, trigger: function() {} }
hook: function(mqttTopic) {
var that = this;
this._promise = this._promise.then(() => {
return new Promise(resolve => {
this._mqttClient.subscribe(mqttTopic);
resolve(mqttTopic);
});
});
return {
if: that._if.bind(that),
trigger: that.trigger.bind(that),
};
},
trigger: function(callback, data) {
var that = this;
var triggers = this._triggers;
this._promise = this._promise.then((mqttTopic) => {
return new Promise(resolve => {
if (typeof callback === 'function') {
if (mqttTopic) {
triggers[mqttTopic] = triggers[mqttTopic] || {};
triggers[mqttTopic].trigger = callback;
} else {
callback();
}
} else if (typeof callback === 'string') {
// Trigger a Webhook.
triggers[mqttTopic] = triggers[mqttTopic] || {};
if (callback.startsWith('https://') || callback.startsWith('http://')) {
triggers[mqttTopic].trigger = mqttTopic ? triggerWebhook : triggerWebhook(data);
} else { // Trigger a MQTThook in the same MQTT broker.
triggers[mqttTopic].trigger = mqttTopic ? triggerMQTThook : triggerMQTThook(data);
}
function triggerWebhook(data) {
request({ url: callback, qs: data }, error => {
error && console.error(error.message);
});
}
function triggerMQTThook(data) {
that._mqttClient.publish(callback, JSON.stringify(data), null, error => {
error && console.error(error.message);
});
}
}
resolve();
});
});
return {
hook: that.hook.bind(that),
};
},
_if: function(callback) {
var that = this;
var triggers = this._triggers;
this._promise = this._promise.then((mqttTopic) => {
return new Promise(resolve => {
if (typeof callback === 'function') {
triggers[mqttTopic] = triggers[mqttTopic] || {};
triggers[mqttTopic].if = callback;
}
resolve(mqttTopic);
});
});
return {
trigger: that.trigger.bind(that),
};
},
};
module.exports = MQTThook;