-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-mqtt-adapter.js
116 lines (96 loc) · 2.88 KB
/
simple-mqtt-adapter.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
108
109
110
111
112
113
114
115
116
'use strict';
const {
Adapter,
Device,
Event,
} = require('gateway-addon');
const MQTT = require('async-mqtt');
const config = require('./config');
const manifest = require('./manifest.json');
const DEVICE_ID = 'simple-mqtt-virtual';
const DEVICE_NAME = 'MQTT Broker';
class MQTTVirtualDevice extends Device {
constructor(adapter, conf) {
super(adapter, DEVICE_ID);
this.title = DEVICE_NAME;
this.description = DEVICE_NAME;
this.mqttClient = adapter.mqttClient;
this.conf = conf;
this.createActions(this.conf.actions);
this.createEvents(this.conf.events);
this.mqttClient.on('message', (topic, message) => {
const event = this.conf.events.find((eventConf) => eventConf.topic === topic);
if (event) {
this.eventNotify(new Event(this, event.name));
}
});
}
async performAction(action) {
action.start();
const actionConfig = this.conf.actions.find((actionConf) => actionConf.name === action.name);
if (this.mqttClient.connected && actionConfig) {
console.log('Performing action...', actionConfig);
await this.mqttClient.publish(actionConfig.topic, actionConfig.payload);
}
action.finish();
}
createActions(actions = []) {
for (const action of actions) {
this.addAction(action.name, {
title: action.title,
});
}
}
createEvents(events = []) {
for (const event of events) {
this.addEvent(event.name, {
title: event.title,
});
try {
this.mqttClient.subscribe(event.topic);
} catch (error) {
console.error(`Failed to subscribe to ${event.topic}!`);
}
}
}
}
class SimpleMQTTAdapter extends Adapter {
constructor(addonManager) {
super(addonManager, 'SimpleMQTTAdapter', manifest.id);
addonManager.addAdapter(this);
this.create(manifest.id);
}
async create(manifest) {
const conf = await config.load(manifest);
try {
await this.connectMQTT(conf.mqttHost, conf.mqttUsername, conf.mqttPassword);
await this.createVirtualDevice(conf);
} catch (error) {
console.error(error);
}
}
async connectMQTT(host, username, password) {
if (!host) {
throw new Error('Please set the MQTT Host in the addon config!');
}
let options = {};
if (username && password) {
options.username = username;
options.password = password;
}
console.log('Connecting to MQTT Broker..', host);
this.mqttClient = await MQTT.connectAsync(host, options);
console.log('Connected to MQTT Broker!');
}
async createVirtualDevice(conf) {
console.log('Creating virtual device..');
this._device = new MQTTVirtualDevice(this, conf);
this.handleDeviceAdded(this._device);
}
startPairing() {
if (this._device && !this.devices.hasOwnProperty(this._device.id)) {
this.handleDeviceAdded(this._device);
}
}
}
module.exports = SimpleMQTTAdapter;