-
Notifications
You must be signed in to change notification settings - Fork 1
/
color.js
133 lines (114 loc) · 3.5 KB
/
color.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
// Color MQTT<->Websocket proxy
const yargs = require('yargs');
const mqtt = require('mqtt');
const WebSocket = require('ws');
const argv = yargs
.option('mqttHost', {
description: 'Hostname of MQTT broker',
alias: 'mqtt',
type: 'string'
})
.option('wsURL', {
description: 'URL for WebSocket server. Full URL include wss:https://',
alias: 'ws',
type: 'string'
})
.option('mqttLightName', {
description: 'Name of the light that will precede / commands',
type: 'string'
})
.option('wsKey', {
description: 'In websocket message, the key the light represents. Ex: {"$key": { power: true|false } }',
type: 'string'
})
.option('wsOnAction', {
description: 'In websocket message, the key that will be sent as the action. ex: {"action": "$wsOnAction"}',
type: 'string'
})
.option('wsColorAction', {
description: 'In websocket message, the key that will be sent as the action. ex: {"action": "$wsColorAction"}',
type: 'string'
})
.help()
.alias('help', 'h')
.argv;
// CONFIG
const mqqtHost = (argv.mqttHost) ? argv.mqttHost : 'o.xrho.com';
const mqttLightName = (argv.mqttLightName) ? argv.mqttLightName : 'rgbLight';
const wsURL = (argv.wsURL) ? argv.wsURL : 'wss:https://cloud.xrho.com:443';
// key the light represents. {"$key": { power: true|false } }
const wsKey = (argv.wsKey) ? argv.wsKey : 'normalLight';
// {"action": $wsXXXXAction}
// this can and probably should be abstracted more
// the rest of the json payload is still hardcoded and specific
const wsOnAction = (argv.wsOnAction) ? argv.wsOnAction : 'on';
const wsColorAction = (argv.wsColorAction) ? argv.wsColorAction : 'color';
console.log('color led proxy starting');
const client = mqtt.connect(`mqtt:https://${mqqtHost}`);
const ws = new WebSocket(`${wsURL}`);
client.on('connect', () => {
console.log('mqtt connected');
client.publish(`${mqttLightName}/connected`, 'true');
client.subscribe(`${mqttLightName}/setOn`);
client.subscribe(`${mqttLightName}/setRGB`);
});
client.on('message', (topic, message) => {
console.log(`${topic.toString()} :: ${message.toString()}`);
if (topic === `${mqttLightName}/setOn`) {
console.log(`mqtt setOn: ${message.toString()}`);
if (message.toString() === 'true') {
ws.send(JSON.stringify({
'action': wsOnAction
}));
} else {
ws.send(JSON.stringify({
'action': 'off'
}));
}
} else if (topic === `${mqttLightName}/setRGB`) {
console.log(`mqtt setRGB: ${message.toString()}`);
// color comes down as a comma separated list
let color = message.toString().split(',');
if (color.length > 2) {
console.log(JSON.stringify({
'action': wsColorAction,
'color': {
'rgb': {
'r': color[0],
'g': color[1],
'b': color[2]
}
}
}));
ws.send(JSON.stringify({
'action': wsColorAction,
'color': {
'rgb': {
'r': parseInt(color[0]),
'g': parseInt(color[1]),
'b': parseInt(color[2])
}
}
}));
}
}
});
ws.onopen = () => {
console.log('webocket connection opened');
};
ws.onerror = (error) => {
console.error(`websocket error: ${error}`);
};
ws.onclose = (event) => {
console.log('websocket connection closed');
process.exit();
};
ws.onmessage = (event) => {
let json = JSON.parse(event.data);
console.log(`ws power: ${json[wsKey]['power'].toString()}`);
// publish the light status
client.publish(`${mqttLightName}/getOn`, json[wsKey]['power'].toString());
console.log(`ws color: ${json[wsKey]['color']['rgb']}`);
// publish the color
client.publish(`${mqttLightName}/getRGB`, json[wsKey]['color']['rgb'].join(','));
};