-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrgbw-pm-double-triple-press
78 lines (71 loc) · 2.37 KB
/
rgbw-pm-double-triple-press
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
//colors can be overrided in KVS.colors
//transitionDuration can be overrided in KVS.transitionDuration
let colors = {
"purple": [255,0,230],
"blue": [0,0,255],
"cyan": [0,255,255],
"green": [0,255,0],
"yellow": [255,255,0],
"red": [255,0,0]
};
let transitionDuration = 1;
let userData = null;
let logStr = "";
function onEvent(eventData) {
//print("Event called: ", JSON.stringify(eventData));
//eventData.info.event can be "btn_down", "btn_up", "double_push", "triple_push"
switch(eventData.info.event) {
case "double_push":
Shelly.call("RGBW.GetStatus", {id: 0}, function(result) {
print("GetStatus: ", JSON.stringify(result));
switchColor(result.rgb);
});
break;
case "triple_push":
Shelly.call("RGBW.GetStatus", {id: 0}, function(result) {
switchMode(result);
});
break;
}
}
function switchColor(currentColor) {
const nextColor = findNextColor(currentColor);
Shelly.call("RGBW.Set", {id: 0, rgb:nextColor, white: 0, on: true, transition_duration: transitionDuration});
}
function findNextColor(currentColor) {
const firstColorName = Object.keys(colors)[0];
let result = colors[firstColorName];
let colorFound = false;
for (var colorName in colors) {
if (colorFound) {
result = colors[colorName];
break;
}
colorFound = JSON.stringify(currentColor) == JSON.stringify(colors[colorName]);
}
return result;
}
function switchMode(status) {
//print("Status: ", JSON.stringify(status));
if (status.white) {
Shelly.call("RGBW.Set", {id: 0, rgb:[255,255,255], white: 0, on: true, transition_duration: transitionDuration});
} else if (JSON.stringify(status.rgb) == JSON.stringify([255,255,255])) {
switchColor(status.rgb);
} else {
Shelly.call("RGBW.Set", {id: 0, rgb:[0,0,0], white: 170, on: true, transition_duration: transitionDuration});
}
}
function log(str) {
logStr += str + "\n";
}
Shelly.addEventHandler(onEvent, userData);
Shelly.call("KVS.Get", {"key": "colors"}, function(result) {
if (!result) return;
colors = JSON.parse(result.value);
});
Shelly.call("KVS.Get", {"key": "transitionDuration"}, function(result) {
if (!result) return;
transitionDuration = JSON.parse(result.value);
});
//ensure button_doublepush is disabled in config
Shelly.call("RGBW.SetConfig", {id: 0, config:{"button_presets":{"button_doublepush":null}}});