-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathopenHASP_demo.rules
184 lines (168 loc) · 11.1 KB
/
openHASP_demo.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import java.lang.Integer
import java.lang.NumberFormatException
val Jaffa_Sunrise = newArrayList("#FFAC00","#DC5C05","#C7BAA7","#978B7D","#0A0006")
rule "HASP_DEMO Setup"
when
Item HASP_demo_Plate_Refresh received command ON or
Item HASP_demo_Plate_LWT received update ON
then
var object_Array = newArrayList(
"page=0;obj=btn;id=1;x=5;y=257;w=73;h=58;bg_color=" + Jaffa_Sunrise.get(4) + ";border_width=0;text=\\uE04D;text_color=" + Jaffa_Sunrise.get(3) + ";text_font=28;action=prev",
"page=0;obj=btn;id=2;x=83;y=257;w=73;h=58;bg_color=" + Jaffa_Sunrise.get(4) + ";border_width=0;text=\\uE2DC;text_color=" + Jaffa_Sunrise.get(3) + ";text_font=28;action=back",
"page=0;obj=btn;id=3;x=161;y=257;w=73;h=58;bg_color=" + Jaffa_Sunrise.get(4) + ";border_width=0;text=\\uE054;text_color=" + Jaffa_Sunrise.get(3) + ";text_font=28;action=next",
"page=1;obj=btn;id=1;x=5;y=5;w=230;h=58;bg_color=" + Jaffa_Sunrise.get(4) + ";border_color=" + Jaffa_Sunrise.get(0) + ";border_width=2;radius=10;text=Lights On;value_ofs_x=-85;value_font=28;value_str=\\uE6E8;value_color=" + Jaffa_Sunrise.get(2) + ";text_color=" + Jaffa_Sunrise.get(2) + ";text_font=28",
"page=1;obj=btn;id=2;x=5;y=68;w=230;h=58;bg_color=" + Jaffa_Sunrise.get(4) + ";border_color=" + Jaffa_Sunrise.get(0) + ";border_width=2;radius=10;text=Daylight;value_ofs_x=-85;value_font=28;value_str=\\uE599;value_color=" + Jaffa_Sunrise.get(2) + ";text_color=" + Jaffa_Sunrise.get(2) + ";text_font=28",
"page=1;obj=btn;id=3;x=5;y=131;w=230;h=58;bg_color=" + Jaffa_Sunrise.get(4) + ";border_color=" + Jaffa_Sunrise.get(0) + ";border_width=2;radius=10;text=Night;value_ofs_x=-85;value_font=28;value_str=\\uE594;value_color=" + Jaffa_Sunrise.get(2) + ";text_color=" + Jaffa_Sunrise.get(2) + ";text_font=28",
"page=1;obj=btn;id=4;x=5;y=194;w=230;h=58;bg_color=" + Jaffa_Sunrise.get(4) + ";border_color=" + Jaffa_Sunrise.get(0) + ";border_width=2;radius=10;text=Lights Off;value_ofs_x=-85;value_font=28;value_str=\\uE335;value_color=" + Jaffa_Sunrise.get(2) + ";text_color=" + Jaffa_Sunrise.get(2) + ";text_font=28",
"page=1;id=0;prev=3",
"page=2;obj=label;id=1;x=5;y=5;w=230;h=30;text=Kitchen Dimmer;text_color=" + Jaffa_Sunrise.get(2) + ";text_font=28",
"page=2;obj=label;id=2;x=5;y=80;w=230;h=30;text=Dining Dimmer;text_color=" + Jaffa_Sunrise.get(2) + ";text_font=28",
"page=2;obj=label;id=3;x=5;y=165;w=230;h=30;text=Front Blinds;text_color=" + Jaffa_Sunrise.get(2) + ";text_font=28",
"page=2;obj=slider;id=4;x=20;y=40;w=200;h=30;bg_color=" + Jaffa_Sunrise.get(2) + ";radius=15;radius1=15;radius2=20;text_font=1;val=80;bg_color1=" + Jaffa_Sunrise.get(0) + ";bg_color2=" + Jaffa_Sunrise.get(1),
"page=2;obj=slider;id=5;x=20;y=120;w=200;h=30;bg_color=" + Jaffa_Sunrise.get(2) + ";radius=15;radius1=15;radius2=20;text_font=1;val=65;bg_color1=" + Jaffa_Sunrise.get(0) + ";bg_color2=" + Jaffa_Sunrise.get(1),
"page=2;obj=slider;id=6;x=20;y=205;w=200;h=30;bg_color=" + Jaffa_Sunrise.get(2) + ";radius=15;radius1=15;radius2=20;text_font=1;val=25;bg_color1=" + Jaffa_Sunrise.get(0) + ";bg_color2=" + Jaffa_Sunrise.get(1),
"page=3;obj=label;id=1;x=42;y=10;w=236;h=30;text=Gold;text_color=" + Jaffa_Sunrise.get(2) + ";text_font=28",
"page=3;obj=label;id=2;x=42;y=60;mode=loop;w=236;h=30;text=Chet Faker;text_color=" + Jaffa_Sunrise.get(2) + ";text_font=28",
"page=3;obj=btn;id=3;x=2;y=140;w=76;h=61;bg_color=" + Jaffa_Sunrise.get(4) + ";border_color=" + Jaffa_Sunrise.get(0) + ";border_width=2;radius=10;text=\\uE4AE;text_color=" + Jaffa_Sunrise.get(2) + ";text_font=28",
"page=3;obj=btn;id=4;x=82;y=140;w=76;h=61;bg_color=" + Jaffa_Sunrise.get(4) + ";border_color=" + Jaffa_Sunrise.get(0) + ";border_width=2;radius=10;text=\\uE3E4;text_color=" + Jaffa_Sunrise.get(2) + ";text_font=28",
"page=3;obj=btn;id=5;x=162;y=140;w=76;h=61;bg_color=" + Jaffa_Sunrise.get(4) + ";border_color=" + Jaffa_Sunrise.get(0) + ";border_width=2;radius=10;text=\\uE4AD;text_color=" + Jaffa_Sunrise.get(2) + ";text_font=28",
"page=3;obj=bar;id=6;x=2;y=105;w=236;h=20;bg_color=" + Jaffa_Sunrise.get(2) + ";radius=15;radius1=15;radius2=15;text_font=1;val=65;bg_color1=" + Jaffa_Sunrise.get(0),
"page=3;obj=slider;id=7;x=35;y=220;w=170;h=30;bg_color=" + Jaffa_Sunrise.get(2) + ";radius=15;radius1=15;radius2=20;text_font=1;val=30;bg_color1=" + Jaffa_Sunrise.get(0) + ";bg_color2=" + Jaffa_Sunrise.get(1),
"page=3;obj=label;id=8;x=2;y=2;w=40;h=61;text=\\uE75A;text_color=" + Jaffa_Sunrise.get(2) + ";text_font=22",
"page=3;obj=label;id=9;x=10;y=51;w=36;h=61;text=\\uE004;text_color=" + Jaffa_Sunrise.get(2) + ";text_font=22",
"page=3;obj=label;id=10;x=5;y=224;w=25;h=40;text=\\uE75F;text_color=" + Jaffa_Sunrise.get(2) + ";text_font=22",
"page=3;obj=label;id=11;x=210;y=224;w=25;h=40;text=\\uE57E;text_color=" + Jaffa_Sunrise.get(2) + ";text_font=22",
"page=3;id=0;next=1"
)
val Number LimitDeviceJsonl = 500
val Number LimitObjectJsonl = 230
var Number StartSizeObjectJsonl
var Number currentID
var Number currentPage
var String jsonlString = ""
var String objectAttribute
var z = -1
var y = -1
var object_Count = (object_Array.size as Number)
var Number attributeCount = 0
var Number pageToClear = 999
val String bg_color = Jaffa_Sunrise.get(4)
logWarn("openHASP", "Objects=" + object_Count)
while ((z=z+1) <= object_Count -1) {
attributeCount = (object_Array.get(z).split(";").length as Number)
logWarn("openHASP", "Attributes=" + attributeCount)
StartSizeObjectJsonl = jsonlString.length()
jsonlString += "{"
y = -1
while ((y=y+1) <= attributeCount -1) {
objectAttribute = object_Array.get(z).split(";").get(y)
logWarn("openHASP", "Item=" + (y + 1) + "/" + attributeCount + " value=[" + objectAttribute + "]")
if(objectAttribute.split("=").get(0) == 'page') { currentPage = Integer.parseInt(objectAttribute.split("=").get(1)) }
if(objectAttribute.split("=").get(0) == 'id') { currentID = Integer.parseInt(objectAttribute.split("=").get(1)) }
// Send Clearpage and set bg color when refreshing the lcd
if (pageToClear != currentPage) {
HASP_demo_Plate_Command_CLEARPAGE.sendCommand(currentPage)
HASP_demo_Plate_Command_JSON.sendCommand("['p[" + currentPage + "].b[0].bg_color=" + bg_color + "']")
logWarn("openHASP", "ClearPage=" + currentPage + "]")
pageToClear = currentPage
}
jsonlString += "\"" + objectAttribute.split("=").get(0) + "\":"
try {
val int num = Integer.parseInt(objectAttribute.split("=").get(1))
jsonlString += num.toString
} catch (NumberFormatException e) {
jsonlString += "\"" + objectAttribute.split("=").get(1) + "\""
}
if (LimitObjectJsonl < (jsonlString.length() - StartSizeObjectJsonl))
{
jsonlString += "}\n"
StartSizeObjectJsonl = jsonlString.length()
jsonlString += "{\"page\":" + currentPage + ",\"id\":" + currentID
}
if (y < attributeCount -1) {
jsonlString += ","
}
}
jsonlString += "}\n"
logWarn("openHASP", "JSONL Size=" + jsonlString.length().toString)
if (LimitDeviceJsonl < jsonlString.length()) {
logWarn("openHASP", "JSONL=" + jsonlString)
HASP_demo_Plate_Command_JSONL.sendCommand(jsonlString)
jsonlString = ""
}
}
if(jsonlString.length() > 0) {
logWarn("openHASP", "JSONL=" + jsonlString)
HASP_demo_Plate_Command_JSONL.sendCommand(jsonlString)
}
end
/**/
rule "HASP_DEMO CHANNEL TRIGGERED"
when
Channel "mqtt:broker:openHASPBroker:HASP_demo_Plate_State_Event" triggered
then
logInfo("HASP_channel.rules", " Channel triggered: {} ", receivedEvent)
var topic = receivedEvent.toString.split("#").get(0)
var payload = receivedEvent.substring(topic.length+1,receivedEvent.length)
var topicvalue = topic.split("/").get(topic.split("/").size -1)
var jsonkey = transform("JS","json_demo_key.js",payload)
var jsonvalue = transform("JSONPATH", "$." + jsonkey, payload).toString.toUpperCase
logInfo("HASP_channel.rules", " Topic: {} [{}] Payload: {}", topic, topicvalue , payload)
switch (topicvalue)
{
case 'idle':
{
logInfo("HASP_channel.rules", " Idle event: {}", payload)
}
case 'statusupdate':
{
logInfo("HASP_channel.rules", " Statusupdate: {}", payload)
}
case 'dim':
{
logInfo("HASP_channel.rules", " Dim: {}", payload)
}
case 'page':
{
logInfo("HASP_channel.rules", " Page: {}", payload)
postUpdate(HASP_demo_Plate_Page_Current, payload)
}
case 'input':
{
logInfo("HASP_channel.rules", " Input: {}", payload)
}
default:
{
var eventpage = topicvalue.split("b").get(0).split("p").get(1)
var eventbutton = topicvalue.split("b").get(1)
logInfo("HASP_channel.rules", " Page: {} Button: {} Key: {} Value: {}", eventpage, eventbutton, jsonkey, jsonvalue)
switch(eventpage)
{
case '1':
{
switch(eventbutton)
{
case '1':
{
if(jsonkey == "event" && jsonvalue == "UP")
{
var String stat
var String icon
if (HASP_demo_Plate_P1B1.state == null || HASP_demo_Plate_P1B1.state == OFF){
HASP_demo_Plate_P1B1.postUpdate(ON)
stat = "On"
icon = "\\uE6E8"
} else {
HASP_demo_Plate_P1B1.postUpdate(OFF)
stat = "Off"
icon = "\\uE335"
}
HASP_demo_Plate_Command_JSON.sendCommand("['p1b1.text=Lights " + stat + "','p1b1.value_str=" + icon + "']")
}
}
}
}
}
}
}
end