-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
70 lines (59 loc) · 1.47 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
import mqtt from 'mqtt'
import { EventEmitter } from 'events'
export default class Adapter extends EventEmitter {
constructor(client) {
super()
this.client = client
this.options = client.options
}
connect(cb) {
let { brokerUrl } = this.options
this.mqttClient = mqtt.connect(brokerUrl, this.options)
let client = this.client
this.mqttClient.on('reconnect', () => {
client.emit('reconnect')
})
this.mqttClient.on('error', (err) => {
client.emit('error', err)
})
this.mqttClient.on('message', (topic, message) => {
client.emit('message', topic, message)
})
cb && cb()
}
end(cb) {
this.mqttClient.end(cb)
}
/**
*
* @see https://github.com/mqttjs/MQTT.js#publish
* @param {*} topic
* @param {*} message
* @param {*} options
* @param {*} cb
*/
publish(topic, message, options, cb) {
this.mqttClient.publish(topic, message, options, cb)
}
/**
* topic = 'single_topic' | ['topic_1', 'topic_2'] | {'test1': {qos: 0}, 'test2': {qos: 1}}
*
* @see https://github.com/mqttjs/MQTT.js#subscribe
* @param {*} topic
* @param {*} message
* @param {*} options
* @param {*} cb
*/
subscribe(topic, options, cb) {
this.mqttClient.subscribe(topic, options, cb)
}
/**
*
* @see @see https://github.com/mqttjs/MQTT.js#unsubscribe
* @param {*} topic
* @param {*} cb
*/
unsubscribe(topic, cb) {
this.mqttClient.unsubscribe(topic, cb)
}
}