-
Notifications
You must be signed in to change notification settings - Fork 0
/
DualLed.js
164 lines (153 loc) · 3.78 KB
/
DualLed.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
158
159
160
161
162
163
164
// Konstanten
const temp_warm = 2700;
const temp_cold = 6500;
const brightMin = [6,8];
const brightMax = [40,70];
// globale Variablen
const mqttPrefix = Shelly.getComponentConfig( "mqtt" ).topic_prefix;
let output = false;
let temp = [4500,4500];
let brightness = [100,100];
function showOutput()
{
MQTT.publish( mqttPrefix+"/status/boolean:200", "{\"value\":"+output+"}", 1, false );
}
function setLights()
{
try
{
const warm0 = brightness[0]*(temp_cold-temp[0]) / (temp_cold-temp_warm);
const cold0 = brightness[0] - warm0;
const warm1 = brightness[1]*(temp_cold-temp[1]) / (temp_cold-temp_warm);
const cold1 = brightness[1] - warm1;
//print(warm0,cold0,warm1,cold1)
Shelly.call( "Light.Set", { id:0, on:output, brightness:Math.round(warm0) }, function(result,error_code,error_message,userdata)
{
try
{
Shelly.call( "Light.Set", { id:1, on:output, brightness:Math.round(cold0) }, function(result,error_code,error_message,userdata)
{
try
{
Shelly.call( "Light.Set", { id:2, on:output, brightness:Math.round(warm1) }, function(result,error_code,error_message,userdata)
{
try
{
Shelly.call( "Light.Set", { id:3, on:output, brightness:Math.round(cold1) }, function(result,error_code,error_message,userdata)
{
try
{
showOutput();
}
catch( error )
{
print( error );
}
} )
}
catch( error )
{
print( error );
}
} )
}
catch( error )
{
print( error );
}
} )
}
catch( error )
{
print( error );
}
} );
}
catch( error )
{
print( error );
}
}
function setOutput(value)
{
//print("setOutput",value);
if( value === "toggle" )
{
output = ! output;
}
else if( value === true || value === "on" || value == 1 )
{
output = true;
}
else if( value === false || value === "off" || value == 0 )
{
output = false;
}
setLights();
}
function setBrightness(id,value)
{
brightness[id] = brightMin[id] + value * (brightMax[id]-brightMin[id]) / 100;
//print("setBrightness",id,brightness[id]);
}
function setTemp(id,value)
{
//print("setTemp",id,value);
temp[id] = value;
}
function rpcCallback(topic,message,ud)
{
try
{
message = JSON.parse( message );
if( message.method === "Boolean.Set" && message.params.id == 200 )
{
setOutput( message.params.value );
}
const answer = {
id: message.id,
src: mqttPrefix,
dst: message.src,
result: {}
};
MQTT.publish( message.src+"/rpc", JSON.stringify( answer ), 1, false );
}
catch( error )
{
print( error );
}
}
function mqttCallback(topic,message,ud)
{
try
{
message = JSON.parse( message );
if( message.brightness !== undefined )
{
setBrightness( 0, message.brightness );
setBrightness( 1, message.brightness );
}
if( message.temp !== undefined )
{
setTemp( 0, message.temp );
setTemp( 1, message.temp );
}
setLights();
}
catch( error )
{
print( error );
}
}
Timer.set( 60*1000, true,
function(ud)
{
showOutput();
},
null
);
// Main
showOutput();
MQTT.subscribe( mqttPrefix+"/rpc", rpcCallback, null );
MQTT.subscribe( "Shelly2/Data/TunableWhite", mqttCallback, null );
print( "started" );