-
Notifications
You must be signed in to change notification settings - Fork 0
/
WmcUtilsDemo_1_0_0.groovy
294 lines (270 loc) · 8.93 KB
/
WmcUtilsDemo_1_0_0.groovy
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// ---------------------------------------------------------------------------------
// Demo-Utils - Demo functions from the lUtils library.
//
// Copyright (C) 2023-Present Wesley M. Conner
//
// LICENSE
// Licensed under the Apache License, Version 2.0 (aka Apache-2.0, the
// "License"), see http://www.apache.org/licenses/LICENSE-2.0. You may
// not use this file except in compliance with the License. Unless
// required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied.
// ---------------------------------------------------------------------------------
// The Groovy Linter generates NglParseError on Hubitat #include !!!
#include Wmc.WmcUtilsLib_1_0_0
import com.hubitat.app.ChildDeviceWrapper as ChildDevW
import com.hubitat.app.DeviceWrapper as DevW
import com.hubitat.app.InstalledAppWrapper as InstAppW
import com.hubitat.hub.domain.Event as Event
import groovy.json.JsonOutput as JsonOutput
import groovy.json.JsonSlurper as JsonSlurper
import groovy.transform.Field
import java.lang.Math as Math
import java.lang.Object as Object
import java.util.concurrent.ConcurrentHashMap
definition (
name: 'WmcUtilsDemo_1_0_0',
namespace: 'Wmc',
author: 'Wesley M. Conner',
description: 'Demo lUtils methods',
singleInstance: true,
iconUrl: '',
iconX2Url: ''
)
preferences {
page(
name: 'WmcUtilsDemo_1.0.0',
title: h1("WmcUtilsDemo_1.0.0 (${app.id})"),
install: true,
uninstall: true
) {
//---------------------------------------------------------------------------------
// Per https://community.hubitat.com/t/issues-with-deselection-of-settings/36054/42
//-> app.removeSetting('..')
//-> atomicState.remove('..')
//---------------------------------------------------------------------------------
section {
String s = sArg ?: app?.getLabel() ?: app?.getName()?.toString() ?: device?.getDeviceNetworkId()
Long i = iArg ?: app?.id ?: (device?.id as Long)
String key = "${s}◼${i}"
String ss = (s && s.length() > 15 ) ? "…${s.substring(s.length() - 15, s.length())}" : s
paragraph([
"sArg: ${sArg}",
"app?.getLabel(): ${app?.getLabel()}",
"app?.getName()?.toString(): ${app?.getName()?.toString()}",
"device?.getDeviceNetworkId(): ${device?.getDeviceNetworkId()}",
"iArg: ${iArg}",
"app?.id: ${app?.id}",
"device?.id as Long: ${device?.id as Long}",
"s: ${s}",
"i: ${i}",
"key: ${key}",
"ss: ${ss}"
].join('<br/>'))
paragraph([
h1('Header Level 1'),
h2('Header Level 2'),
h3('Header Level 3')
].join('<br/>'))
demoBullets()
demoTextEmphasis()
demoMapToTable()
demoParseInt()
demoCleanStrings()
demoModeNames()
demoColorBars()
demoAlert()
demoTdBordered()
demoColorTable()
exerciseHuedCache()
testsRunWhenDoneIsClicked()
}
}
}
void demoBullets() {
paragraph([
h1('Demo Bullets'),
bullet1('bullet1()'),
bullet2('bullet2()'),
bullet3('bullet3()'),
square1('square1()'),
square2('square2()'),
square3('square3()')
].join('<br/>'))
}
void demoTextEmphasis() {
ArrayList x = ['one', 'two', 'three', 'four']
Map y = [a: 'apple', b: 'banana', c: 'cantelope']
paragraph([
h1('Demo Text Emphasis'),
"b(..): ${b('This is bold')}",
"i(..): ${i('This is italic')}",
"bi(..): ${bi('This is bold & italic')}",
"List x: ${x}",
"bList(x): ${bList(x)}",
"Map y: ${y}",
"bMap(y): ${bMap(y)}"
].join('<br/>'))
}
void demoMapToTable() {
Map y = [a: 'apple', b: 'banana', c: 'cantelope']
paragraph([
h1('Demo mapToTable()'),
b('With Borders - mapToTable(y, true)'),
mapToTable(y, true),
b('Without Borders - mapToTable(y, false)'),
mapToTable(y, false)
].join('<br/>'))
}
void demoParseInt() {
paragraph([
h1('Demo safeParseInt()'),
"safeParseInt(): ${safeParseInt()}",
"safeParseInt(''): ${safeParseInt('')}",
"safeParseInt('0'): ${safeParseInt('0')}",
"safeParseInt('-51'): ${safeParseInt('-51')}",
"safeParseInt('22'): ${safeParseInt('22')}"
].join('<br/>'))
}
void demoCleanStrings() {
ArrayList ex1 = [null, 'a', 'a', 'b', 'c', null, 'a']
ArrayList ex2 = [null, 'a', 'd', 'b', 'c', null, 'a']
ArrayList ex3 = [null, 'a', 'c', null, 'd', 'b ', 'a']
paragraph([
h1('Demo cleanStrings()'),
"cleanStrings(${ex1}) -> ${cleanStrings(ex1)}",
"cleanStrings(${ex2}) -> ${cleanStrings(ex2)}",
"cleanStrings(${ex3}) -> ${cleanStrings(ex3)}"
].join('<br/>'))
}
void demoModeNames() {
paragraph([
h1('Demo modeNames()'),
"modeNames() -> ${modeNames()}"
].join('<br/>'))
}
void demoColorBars() {
paragraph([
h1('Demo Color Bars'),
h2('blackBar()'),
blackBar(),
h2('greenBar()'),
greenBar(),
h2('redBar()'),
redBar(),
].join('<br/>'))
}
void demoAlert() {
paragraph([
h1('Demo Alert'),
alert('This is a sample alert!')
].join('<br/>'))
}
void demoTdBordered() {
paragraph([
h1('Demo Individually-Bordered Table Cell'),
'<table><tr><td>one</td><td>two</td><td>three</td></tr>',
"<tr><td>four</td>${tdBordered('five')}<td>six</td></tr>",
"<tr><td>seven</td><td>eight</td>${tdBordered('nine')}</tr>",
'</table>'
].join())
}
void demoColorTable() {
paragraph([
h1('Color Hued() Colors Table'),
getFgBgTable()
].join('<br/>'))
}
void exerciseHuedCache() {
paragraph([
h1('Exercise Hued Cache'),
*addTestDataToCache(),
*removeTestDataFromCache()
].join('<br/>'))
}
ArrayList addTestDataToCache() {
return [
h2('CREATING SAMPLE ENTRIES IN HUED_CACHE:'),
"hued('Apple', 15) -> ${hued('Apple', 15)}",
"hued('Banana', 1015) -> ${hued('Banana', 1015)}",
"hued('Carrot', 2015) -> ${hued('Carrot', 2015)}",
"hued('Donut', 3015) -> ${hued('Donut', 3015)}",
"hued('Egg', 4015) -> ${hued('Egg', 4015)}",
"hued('FrenchFries', 5015) -> ${hued('FrenchFries', 5015)}",
"hued('Guava', 6015) -> ${hued('Guava', 6015)}",
"hued('HotDog', 7015) -> ${hued('HotDog', 7015)}",
*getHuedCacheContents()
]
}
ArrayList removeTestDataFromCache() {
// Iterate an in-memory snapshot of the ConcurrentHashMap to
// direct ConcurrentHashMap key removals.
ArrayList results = [h2('REMOVING SAMPLE ENTRIES FROM HUED_CACHE')]
HUED_CACHE.findAll{ k1, v1 -> (k1) } each { k, v ->
if (['Apple_15', 'Banana_1015', 'Carrot_2015', 'Donut_3015',
'Egg_4015', 'FrenchFries_5015', 'Guava_6015',
'HotDog_7015'].contains(k)) {
results << "Removing key: ${k}"
HUED_CACHE.remove(k)
}
}
return [*results, *getHuedCacheContents()]
}
void testsRunWhenDoneIsClicked() {
paragraph([
h1('Tests that Run When "Done" is clicked ...'),
bullet2('The log functions in this library:'),
bullet3('Can reduce log data sent to Hubitat.'),
bullet3('The method "setLogLevel()" adjusts what is sent.'),
bullet3("${b('setLogLevel("TRACE")')} sends everything."),
bullet3("${b('setLogLevel("DEBUG")')} suppresses TRACE."),
bullet3("${b('setLogLevel("INFO")')} suppresses TRACE & DEBUG."),
bullet3("${b('setLogLevel("WARN")')} suppresses TRACE, DEBUG & INFO."),
bullet3("${b('setLogLevel("ERROR")')} suppresses TRACE, DEBUG, INFO & WARN."),
bullet3('ERROR is never suppressed.'),
h2("${b('demoLogLevels()')} is called MULTIPLE TIMES to issue:"),
bullet2("A sample ${b('logTrace()')} call"),
bullet2("A sample ${b('logDebug()')} call"),
bullet2("A sample ${b('logInfo()')} call"),
bullet2("A sample ${b('logWarn()')} call"),
bullet2("A Sample ${b('logError()')} call"),
h2("Each time ${b('demoLogLevels()')} is called, the log level is reduced."),
bullet2("In loop #1 all five samples are sent."),
bullet2("In loop #2 ${b('logTrace()')} is suppressed."),
bullet2("In loop #3 ${b('logDebug()')} is also suppressed"),
bullet2("In loop #4 ${b('logInfo()')} is also suppressed"),
bullet2("In loop #5 ${b('logWarn()')} is also suppressed")
].join('<br/>'))
}
void installed() {
// Called when a bare device is first constructed.
demoLogFiltering()
}
void updated() {
// Called when a human uses the Hubitat GUI's Device drilldown page to edit
// preferences (aka settings) AND presses 'Save Preferences'.
demoLogFiltering()
}
void demoLogFiltering() {
['TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR'].each { level ->
log.info("Calling demoLogLevels() with setLogLevel(${b(level)}):")
setLogLevel(level)
demoLogLevels()
}
}
void demoLogLevels() {
logTrace('demoLogLevels', 'logTrace(..) test only')
logDebug('demoLogLevels', 'logDebug(..) test only')
logInfo('demoLogLevels', 'logInfo(..) test only')
logWarn('demoLogLevels', 'logWarn(..) test only')
logError('demoLogLevels', 'logError(..) test only')
}
// UNUSED / UNSUPPORTED
//-> void uninstalled() {
//-> // Called on device tear down.
//-> }
//-> void initialize() {
//-> // Called on hub startup (per capability "Initialize").
//-> }