-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysensordioder.rules
130 lines (102 loc) · 3.09 KB
/
mysensordioder.rules
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
import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import java.lang.*
import java.util.concurrent.locks.ReentrantLock
var java.util.concurrent.locks.ReentrantLock lockDioder = new java.util.concurrent.locks.ReentrantLock()
rule "MySensorID6RGBState updated"
when
Item MySensorID6RGBState received update
then
lockDioder.lock()
try {
logInfo("rules","MySensorID6RGBState received an update.")
var int r = Integer::parseInt(MySensorID6RGBState.state.toString.substring(0,2),16)
var int g = Integer::parseInt(MySensorID6RGBState.state.toString.substring(2,4),16)
var int b = Integer::parseInt(MySensorID6RGBState.state.toString.substring(4,6),16)
var double hue
var double bright
var double saturation
if(r!=0 || g!=0 || b!=0)
{
var double r_d = r
var double g_d = g
var double b_d = b
r_d = r_d / 255
g_d = g_d / 255
b_d = b_d / 255
var double min
var double max
var int channel
if(r_d >= g_d)
{
min = g_d
max = r_d
channel = 0
}
else
{
min = r_d
max = g_d
channel = 1
}
if(b_d > max)
{
max = b_d
channel = 2
}
if(b_d < min)
min = b_d
if(channel == 0)
hue = 60*((g_d-b_d)/(max-min))
if(channel == 1)
hue = 60*(2 + ((b_d-r_d)/(max-min)))
if(channel == 2)
hue = 60*(4 + ((r_d-g_d)/(max-min)))
if (hue < 0)
hue = hue + 360
bright = (max+min)/2
if(min==max)
saturation = 0
else
{
if(saturation < 0.5)
saturation = (max-min)/(max+min)
else
saturation = (max-min)/(2-max-min)
}
bright = (bright*100).intValue
saturation = (saturation*100).intValue
logInfo("rules","no zero RGB received converted to hue. Send the hue to the Bedroom Dioder strip.")
var HSBType scene = new HSBType(hue+","+saturation+","+bright)
postUpdate(Color_Bedroom_Dioder, scene)
}
} finally
{
// release the lock - we are ready to process the next event
lockDioder.unlock()
}
end
rule "Color Bedroom Dioder update"
when
Item Color_Bedroom_Dioder received command
then
logInfo("rules","Color Bedroom Dioder received a command.")
var HSBType currentState = Color_Bedroom_Dioder.state
var DecimalType hue = currentState.getHue()
var PercentType red = currentState.getRed()
var PercentType green = currentState.getGreen()
var PercentType blue = currentState.getBlue()
var PercentType sat = currentState.getSaturation()
var PercentType bright = currentState.getBrightness()
var int redI = (red.intValue * 255) / 100
var int greenI = (green.intValue * 255) / 100
var int blueI = (blue.intValue * 255) / 100
var String redS = if (redI < 16) "0"+Integer::toHexString(redI) else Integer::toHexString(redI)
var String greenS = if (greenI < 16) "0"+Integer::toHexString(greenI) else Integer::toHexString(greenI)
var String blueS = if (blueI < 16) "0"+Integer::toHexString(blueI) else Integer::toHexString(blueI)
logInfo("rules","Send RGB in hex to MySensorID6RGB.")
sendCommand(MySensorID6RGBCommand,redS + greenS + blueS)
logInfo("rules","Send Dimmer value to MySensorID6Dimmer.")
sendCommand(MySensorID6DimmerCommand,bright.toString)
end