forked from thomasloven/plejd2mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
157 lines (136 loc) · 5.2 KB
/
main.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const api = require('./api');
const mqtt = require('./mqtt');
const fs = require('fs');
const PlejdService = require('./ble.bluez');
const SceneManager = require('./scene.manager');
const version = "0.4.7";
var configPi;
const path = "/etc/nibepi"
if (!fs.existsSync(path)) {
exec(`sudo mount -o remount,rw / && sudo mkdir ${path} && sudo chown ${process.env.USER}:${process.env.USER} ${path}`, function(error, stdout, stderr) {
if(error) {
exec(`mkdir ${path} && chown ${process.env.USER}:${process.env.USER} ${path}`, function(error, stdout, stderr) {
if(error) {console.log('Error creating config directory')} else { console.log('Created config directory')}
});
} else {
console.log(`Configuration directory created ${path}`);
}
});
}
function requireF(modulePath){ // force require
try {
return require(modulePath);
}
catch (e) {
console.log('Config file not found, loading default.');
let conf = require(__dirname+'/default.json')
fs.writeFile(path+'/config.json', JSON.stringify(conf,null,2), function(err) {
if(err) console.log(err)
});
return conf;
}
}
configPi = requireF(path+'/config.json');
async function main() {
console.log('starting Plejd add-on v. ' + version);
const config = {
site: configPi.plejd.cloud_name || "Default Site",
username: configPi.plejd.cloud_user || "",
password: configPi.plejd.cloud_pass || "",
mqttBroker: `mqtt://${configPi.plejd.host}:${configPi.plejd.port}` || "mqtt://localhost",
mqttUsername: configPi.plejd.user || "",
mqttPassword: configPi.plejd.pass || "",
includeRoomsAsLights: process.env.PLEJD_INCLUDE_ROOMS_AS_LIGHTS || false,
connectionTimeout: process.env.BLUETOOTH_TIMEOUT || 4,
writeQueueWaitTime: process.env.BLUETOOTH_WAIT || 600,
}
if (!config.connectionTimeout) {
config.connectionTimeout = 2;
}
const plejdApi = new api.PlejdApi(config.site, config.username, config.password);
const client = new mqtt.MqttClient(config.mqttBroker, config.mqttUsername, config.mqttPassword);
['SIGINT', 'SIGHUP', 'SIGTERM'].forEach(signal => {
process.on(signal, () => {
console.log(`Received ${signal}. Cleaning up.`);
client.disconnect(() => process.exit(0));
});
});
plejdApi.login().then(() => {
// load all sites and find the one that we want (from config)
plejdApi.getSites().then((site) => {
// load the site and retrieve the crypto key
plejdApi.getSite(site.site.siteId).then((cryptoKey) => {
// parse all devices from the API
const devices = plejdApi.getDevices();
client.on('connected', () => {
console.log('plejd-mqtt: connected to mqtt.');
client.discover(devices);
});
client.init();
// init the BLE interface
const sceneManager = new SceneManager(plejdApi.site, devices);
const plejd = new PlejdService(cryptoKey, devices, sceneManager, config.connectionTimeout, config.writeQueueWaitTime, true);
plejd.on('connectFailed', () => {
console.log('plejd-ble: were unable to connect, will retry connection in 10 seconds.');
setTimeout(() => {
plejd.init();
}, 10000);
});
plejd.init();
plejd.on('authenticated', () => {
console.log('plejd: connected via bluetooth.');
});
// subscribe to changes from Plejd
plejd.on('stateChanged', (deviceId, command) => {
client.updateState(deviceId, command);
});
plejd.on('sceneTriggered', (deviceId, scene) => {
client.sceneTriggered(scene);
});
// subscribe to changes from HA
client.on('stateChanged', (device, command) => {
const deviceId = device.id;
if (device.typeName === 'Scene') {
// we're triggering a scene, lets do that and jump out.
// since scenes aren't "real" devices.
plejd.triggerScene(device.id);
return;
}
let state = 'OFF';
let commandObj = {};
if (typeof command === 'string') {
// switch command
state = command;
commandObj = {
state: state
};
// since the switch doesn't get any updates on whether it's on or not,
// we fake this by directly send the updateState back to HA in order for
// it to change state.
client.updateState(deviceId, {
state: state === 'ON' ? 1 : 0
});
} else {
state = command.state;
commandObj = command;
}
if (state === 'ON') {
plejd.turnOn(deviceId, commandObj);
} else {
plejd.turnOff(deviceId, commandObj);
}
});
client.on('settingsChanged', (settings) => {
if (settings.module === 'mqtt') {
client.updateSettings(settings);
} else if (settings.module === 'ble') {
plejd.updateSettings(settings);
} else if (settings.module === 'api') {
plejdApi.updateSettings(settings);
}
});
});
});
});
}
main();