-
Notifications
You must be signed in to change notification settings - Fork 1
/
daylight-rgbw.js
139 lines (104 loc) · 5.57 KB
/
daylight-rgbw.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
/*
Daylight RGBW Color control Node for Node-RED
-------------------------------------------------------------------------------------------------------------------
Copyright (c) 2018 Raimond Brookman
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
module.exports = function(RED) {
"use strict";
var ct = require('color-temperature');
var SunCalc = require('suncalc');
function ScaleRGBLevelToPercent(rgbLevel) {
return rgbLevel * 100.0 / 255.0;
}
function KelvinToMired(kelvin)
{
return 1000000 / kelvin;
}
// ------------------------------------------------------------------------------------------
function DaylightRGBWNode(n) {
//
// DaylightRGBW Input Node
//
// Create a RED node
RED.nodes.createNode(this,n);
this.topic = n.topic;
this.command = n.command;
this.minTemp = Number(n.minColorTemp);
this.maxTemp = Number(n.maxColorTemp);
this.whiteLevel = Number(n.whiteLevel);
this.latitude = Number(n.latitude)||0.0;
this.longitude = Number(n.longitude)||0.0;
this.itemState = this.context().get("itemState") || "OFF";
this.colorTemp = this.minTemp;
this.colorTempUnit = n.colorTempUnit || "K";
this.log("Configured colorTempUnit:" + this.colorTempUnit );
var node = this;
// This will be executed on every input message
this.on('input', function (msg) {
this.log("Received Topic:" + msg.topic);
if(msg.topic == "date-time")
{
this.dateTime = msg.payload;
this.log("Received dt:" + this.dateTime);
var positionResult = SunCalc.getPosition(this.dateTime, this.latitude, this.longitude);
this.log("Sun position:" + positionResult.altitude);
var fraction = positionResult.altitude * 2.0 / Math.PI;
this.log("Sun position fraction:" + fraction);
this.colorTemp = (this.minTemp * 1.0) + (Math.max(fraction,0) * (this.maxTemp-this.minTemp));
} else if(msg.topic == "color-temp")
{
this.colorTemp = Number(msg.payload) * 1.0;
}
else if(msg.topic == "item-switch" || msg.event == "StateEvent" )
{
this.itemState = msg.payload;
this.context().set("itemSate", this.itemState);
}
else if(msg.topic == "white-level")
{
var currentWhiteLevel = this.whiteLevel;
var newWhiteLevel = Number(msg.payload) * 1.0;
if(newWhiteLevel >= 0 || newWhiteLevel <= 100)
{
this.whiteLevel = newWhiteLevel;
}
}
else
{
this.status({fill:"red",shape:"dot",text:"unknown topic:" + msg.topic});
return;
}
this.log("Color-temp:" + this.colorTemp);
this.status({fill:"yellow",shape:"ring",text:"calculating for:" + this.colorTemp.toFixed(1)});
var colorTempOut = (this.colorTempUnit == "K") ? this.colorTemp : KelvinToMired(this.colorTemp);
if(this.itemState == "ON")
{
var rgb = ct.colorTemperature2rgb(this.colorTemp);
// Convert values to percentage
var red = ScaleRGBLevelToPercent(rgb.red);
var green = ScaleRGBLevelToPercent(rgb.green);
var blue = ScaleRGBLevelToPercent(rgb.blue);
var white = Number(this.whiteLevel) * 1.0;
var msgRed = { topic: this.topic, payload: red};
var msgGreen = { topic: this.topic, payload: green};
var msgBlue = { topic: this.topic, payload: blue};
var msgWhite = { topic: this.topic, payload: white};
var msgColorTemp = { topic: this.topic, payload: colorTempOut };
this.send([msgRed, msgGreen, msgBlue, msgWhite, msgColorTemp]);
this.status({fill:"green",shape:"ring",text:"R:" + red.toFixed(1) +
",G:" + green.toFixed(1) + ",B:" + blue.toFixed(1) +
",W:" + white.toFixed(1) + ",colorTemp:" + colorTempOut.toFixed(1)});
}
else
{
this.status({fill:"red",shape:"ring",text:"OFF, colortemp:" + colorTempOut.toFixed(1)});
}
});
this.on("close", function() {
});
}
RED.nodes.registerType("daylight-rgbw",DaylightRGBWNode);
}