-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (49 loc) · 1.48 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
const util = require('./util.js')
const { Board } = require('johnny-five');
const debug = require('debug')('main');
const CustomIO = require('./CustomIO.js');
const MQTTManager = require('./MQTTManager.js');
const ONE_WIRE_PIN = 12;
const mqtt = require('mqtt');
// point d'entrée
main();
function main() {
try {
debug("Retreiving MQTT HA config...");
util.getMQTTConfig((mqttConfig) => {
debug("MQTT config:");
//console.log(mqttConfig);
debug("Connexion au broker MQTT...");
/** @type {mqtt.MqttClient} */
const mqttClient = mqtt.connect(`mqtt://${mqttConfig.host}`, {
username: mqttConfig.username,
password: mqttConfig.password
});
mqttClient.on('connect', () => {
console.log('Connected to MQTT broker');
onMQTTConnected(new MQTTManager(mqttClient, mqttConfig));
});
mqttClient.on('error', (error) => {
util.handleError(error);
});
mqttClient.on('reconnect', () => {
console.log('Reconnecting to MQTT broker');
});
mqttClient.on('close', () => {
console.log('Disconnected from MQTT broker');
});
});
} catch (error) {
util.handleError(error);
}
}
function onMQTTConnected(mqttManager) {
debug("MQTT", "Connexion ok");
debug("main", "Retreiving HA user custom config...");
//call getconf
util.getAddonConfig((addonConfig) => {
const board = new Board({
io: new CustomIO(addonConfig, mqttManager),
});
});
}