This repository has been archived by the owner on Jul 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
122 lines (101 loc) · 3.3 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const MQTT = require('mqtt');
const SSH = require('simple-ssh');
const READ_INTERVAL = 30000;
const WRITE_INTERVAL = 2000;
const COLOR_PATH = '/proc/ubnt_ledbar/custom_color';
const BRIGHTNESS_PATH = '/proc/ubnt_ledbar/brightness';
const LED_PATTERN_PATH = '/proc/gpio/led_pattern';
const readConfig = function () {
const fs = require('fs');
return JSON.parse(fs.readFileSync('config.json'));
}
const mqttPublish = function (client, name, type, value) {
client.publish('unifi/' + name + '/' + type, value);
}
const createSSHFor = function (nameAP, config) {
return new SSH({
host: config.ap[nameAP].host,
user: config.ap[nameAP].username,
pass: config.ap[nameAP].password
});
}
const processMessage = function (topic, message, config) {
const parts = topic.split('/', 3);
const nameAP = parts[1];
const command = parts[2];
const ssh = createSSHFor(nameAP, config);
let file;
switch (command) {
case 'set_color':
file = COLOR_PATH;
break;
case 'set_brightness':
file = BRIGHTNESS_PATH;
break;
case 'set_power':
file = LED_PATTERN_PATH;
break;
}
console.debug('Processing command: ' + command + ' to AP ' + nameAP + ' in file: ' + file);
let msg = message.toString();
ssh
.exec('cat > ' + file, {
in: msg
})
.start();
}
const readLoop = function (mqttClient, config) {
for (let nameAP in config.ap) {
const ssh = createSSHFor(nameAP, config);
ssh
.exec('cat ' + COLOR_PATH, {
out: function (stdout) {
console.debug('AP: ' + nameAP + ' Color: ' + stdout);
mqttPublish(mqttClient, nameAP, 'color', stdout);
}
})
.exec('cat ' + BRIGHTNESS_PATH, {
out: function (stdout) {
console.debug('AP: ' + nameAP + ' Brightness: ' + stdout);
mqttPublish(mqttClient, nameAP, 'brightness', stdout);
}
})
.exec('cat ' + LED_PATTERN_PATH, {
out: function (stdout) {
console.debug('AP: ' + nameAP + ' LED Pattern: ' + stdout);
mqttPublish(mqttClient, nameAP, 'power', stdout);
}
})
.start();
}
}
const subscribe = function (mqttClient, config) {
for (let nameAP in config.ap) {
mqttClient.subscribe('unifi/' + nameAP + '/set_color');
mqttClient.subscribe('unifi/' + nameAP + '/set_brightness');
mqttClient.subscribe('unifi/' + nameAP + '/set_power');
}
}
const config = readConfig();
const mqttClient = MQTT.connect(config.mqtt);
const commandQueue = [];
mqttClient.on('connect', function () {
subscribe(mqttClient, config);
});
mqttClient.on('message', function (topic, message) {
commandQueue.push({
topic,
message
});
});
setInterval(function () {
console.debug('Reading states from devices..');
readLoop(mqttClient, config);
}, READ_INTERVAL);
setInterval(function () {
if (commandQueue.length > 0) {
const command = commandQueue.pop();
commandQueue.length = 0;
processMessage(command.topic, command.message, config);
}
}, WRITE_INTERVAL);