-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathhubigraph time graph
1850 lines (1481 loc) · 88.9 KB
/
hubigraph time graph
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import groovy.json.*;
/**
* Hubigraph Line Graph Child App
*
* Copyright 2020, but let's behonest, you'll copy it
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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. See the License
* for the specific language governing permissions and limitations under the License.
*
*/
// Hubigraph Line Graph Changelog
// *****ALPHA BUILD
// v0.1 Initial release
// v0.2 My son added webpage efficiencies, reduced load on hubitat by 75%.
// v0.3 Loading Update; Removed ALL processing from Hub, uses websocket endpoint
// v0.5 Multiple line support
// v0.51 Select ANY device
// v0.60 Select AXIS to graph on
// v0.70 A lot more options
// v0.80 Added Horizontal Axis Formatting
// ****BETA BUILD
// v0.1 Added Hubigraph Tile support with Auto-add Dashboard Tile
// v0.2 Added Custom Device/Attribute Labels
// v0.3 Added waiting screen for initial graph loading & sped up load times
// v0.32 Bug Fixes
// V 1.0 Released (not Beta) Cleanup and Preview Enabled
// v 1.2 Complete UI Refactor
// V 1.5 Ordering, Color and Common API Update
// V 1.8 Smoother sliders, bug fixes
// V 2.0 New Version to Support Combo Graphs. Support for Line Graphs is ended.
// V 2.1 Long Term Storage Enabled
// V 4.6 Added finer control for timespan, resize graph sizes, bug fixes
// Credit to Alden Howard for optimizing the code.
def ignoredEvents() { return [ 'lastReceive' , 'reachable' ,
'buttonReleased' , 'buttonPressed', 'lastCheckinDate', 'lastCheckin', 'buttonHeld' ] }
def version() { return "v1.0" }
definition(
name: "Hubigraph Time Graph",
namespace: "tchoward",
author: "Thomas Howard",
description: "Hubigraph Time Graph",
category: "",
parent: "tchoward:Hubigraphs",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/[email protected]",
iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/[email protected]",
)
preferences {
section ("test"){
page(name: "mainPage", install: true, uninstall: true)
page(name: "deviceSelectionPage", nextPage: "graphSetupPage")
page(name: "graphSetupPage", nextPage: "mainPage")
page(name: "longTermStoragePage", nextPage: "mainPage");
page(name: "enableAPIPage")
page(name: "disableAPIPage")
}
mappings {
path("/graph/") {
action: [
GET: "getGraph"
]
}
path("/getData/") {
action: [
GET: "getData"
]
}
path("/getOptions/") {
action: [
GET: "getOptions"
]
}
path("/getSubscriptions/") {
action: [
GET: "getSubscriptions"
]
}
}
}
def call(Closure code) {
code.setResolveStrategy(Closure.DELEGATE_ONLY);
code.setDelegate(this);
code.call();
}
/********************************************************************************************************************************
*********************************************************************************************************************************
****************************************** PAGES ********************************************************************************
*********************************************************************************************************************************
*********************************************************************************************************************************/
def getEvents(sensor, attribute, num){
def resp = [:]
def today = new Date();
def then = new Date();
use (groovy.time.TimeCategory) {
then -= 2.days;
}
def respEvents = [];
respEvents << sensor.statesSince(attribute, then, [max: 200]){ it.value };
respEvents = respEvents.flatten();
respEvents = respEvents.unique();
return respEvents;
}
def graphSetupPage(){
def fontEnum = [["1":"1"], ["2":"2"], ["3":"3"], ["4":"4"], ["5":"5"], ["6":"6"], ["7":"7"], ["8":"8"], ["9":"9"], ["10":"10"],
["11":"11"], ["12":"12"], ["13":"13"], ["14":"14"], ["15":"15"], ["16":"16"], ["17":"17"], ["18":"18"], ["19":"19"], ["20":"20"]];
def updateEnum = [["-1":"Never"], ["0":"Real Time"], ["10":"10 Milliseconds"], ["1000":"1 Second"], ["5000":"5 Seconds"], ["60000":"1 Minute"],
["300000":"5 Minutes"], ["600000":"10 Minutes"], ["1200000":"20 Minutes"], ["1800000":"Half Hour"], ["3600000":"1 Hour"], ["6400000":"2 Hours"], ["19200000":"6 Hours"],
["43200000":"12 Hours"], ["86400000":"1 Day"]];
def timespanEnum = [["60000":"1 Minute"],
["3600000":"1 Hour"],
["43200000":"12 Hours"],
["86400000":"1 Day"],
["259200000":"3 Days"],
["604800000":"1 Week"],
["2419200000":"2 Weeks"],
["1814400000":"3 Weeks"],
["2419200000":"1 Month"],
["custom": "Custom Timespan"]];
def timespanEnum2 = [["10":"10 Milliseconds"], ["1000":"1 Second"], ["5000":"5 Seconds"], ["30000":"30 Seconds"], ["60000":"1 Minute"], ["120000":"2 Minutes"], ["300000":"5 Minutes"], ["600000":"10 Minutes"],
["2400000":"30 minutes"], ["3600000":"1 Hour"], ["43200000":"12 Hours"], ["86400000":"1 Day"], ["259200000":"3 Days"], ["604800000":"1 Week"]];
def updateRateEnum = [["-1":"Never"], ["0":"Real Time"], ["1000":"1 Second"], ["60000":"1 Minute"], ["300000":"5 Minutes"], ["600000":"10 Minutes"], ["1200000":"20 Minutes"], ["1800000":"Half Hour"], ["3600000":"1 Hour"]];
dynamicPage(name: "graphSetupPage") {
parent.hubiForm_section(this,"General Options", 1)
{
input( type: "enum", name: "graph_update_rate", title: "<b>Integration Time</b><br><small>(The amount of time each data point covers)</small>",
multiple: false, required: true, options: timespanEnum2, defaultValue: "300000", submitOnChange: true)
input( type: "enum", name: "graph_refresh_rate", title: "<b>Graph Update Rate</b></br><small>(For panel viewing; the refresh rate of the graph)",
multiple: false, required: true, options: updateRateEnum, defaultValue: "300000")
container = [];
container << parent.hubiForm_sub_section(this, "Graph Time Span<br><small>Amount of time the graph covers</small>");
if (graph_timespan_weeks == null){
app.updateSetting("graph_timespan_weeks", 0);
app.updateSetting("graph_timespan_days", 1);
app.updateSetting("graph_timespan_hours", 0);
app.updateSetting("graph_timespan_minutes", 0);
settings["graph_timespan_weeks"] = 0;
settings["graph_timespan_days"] = 1;
settings["graph_timespan_hours"] = 0;
settings["graph_timespan_minutes"] = 0;
}
container << parent.hubiForm_slider (this, title: "<b>Weeks</b>", name: "graph_timespan_weeks",
default: 0, min: 0, max: 104, units: " weeks", submit_on_change: true);
container << parent.hubiForm_slider (this, title: "<b>Days</b>", name: "graph_timespan_days",
default: 0, min: 0, max: 30, units: " days", submit_on_change: true);
container << parent.hubiForm_slider (this, title: "<b>Hours</b>", name: "graph_timespan_hours",
default: 0, min: 0, max: 24, units: " hours", submit_on_change: true);
container << parent.hubiForm_slider (this, title: "<b>Minutes</b>", name: "graph_timespan_minutes",
default: 0, min: 0, max: 60, units: " seconds", submit_on_change: true);
if (graph_timespan_weeks==null){
secs = 86400000;
} else {
secs = (long)((double)(graph_timespan_weeks)*604800000+
(double)(graph_timespan_days)*86400000+
(double)(graph_timespan_hours)*3600000+
(double)(graph_timespan_minutes)*60000);
}
app.updateSetting("graph_timespan", [type: "number", value: secs]);
points = graph_update_rate ? (long)(secs/Double.parseDouble(graph_update_rate)) : 280;
if (points > 2000) {
container << parent.hubiForm_text (this, """<span style="color: red; font-weight: bold;">WARNING:</span> <b>${(points)} Points </b>will be generated per Attribute per Graph<br><small>Too many points will cause Hubigraphs to hang or take a long time to generate</small>""");
} else {
container << parent.hubiForm_text (this, "NOTE: <b>${(points)} Points </b>will be generated per Attribute per Graph");
}
container << parent.hubiForm_sub_section(this, "Other Options");
container << parent.hubiForm_color (this, "Graph Background", "graph_background", "#FFFFFF", false)
container << parent.hubiForm_switch(this, title: "<b>Smooth Graph Points</b><br><small>(Enable Google Graph Smoothing)</small>", name: "graph_smoothing", default: false);
container << parent.hubiForm_switch(this, title: "<b>Flip Graph to Vertical?</b><br><small>(Rotate 90 degrees)</small>", name: "graph_y_orientation", default: false);
container << parent.hubiForm_switch(this, title: "<b>Reverse Data Order?</b><br><small> (Flip data left to Right)</small>", name: "graph_z_orientation", default: false)
parent.hubiForm_container(this, container, 1);
}
parent.hubiForm_section(this,"Graph Title", 1)
{
container = [];
container << parent.hubiForm_switch(this, title: "<b>Show Title on Graph</b>", name: "graph_show_title", default: false, submit_on_change: true);
if (graph_show_title==true) {
container << parent.hubiForm_text_input (this, "<b>Graph Title</b>", "graph_title", "Graph Title", false);
container << parent.hubiForm_font_size (this, title: "Title", name: "graph_title", default: 9, min: 2, max: 20);
container << parent.hubiForm_color (this, "Title", "graph_title", "#000000", false);
container << parent.hubiForm_switch (this, title: "Graph Title Inside Graph?", name: "graph_title_inside", default: false);
}
parent.hubiForm_container(this, container, 1);
}
parent.hubiForm_section(this, "Graph Size", 1){
container = [];
container << parent.hubiForm_switch (this, title: "<b>Set Fill % of Graph?</b><br><small>(False = Default (80%) Fill)</small>",
name: "graph_percent_fill", default: false, submit_on_change: true);
if (graph_percent_fill==true){
container << parent.hubiForm_slider (this, title: "Horizontal fill % of the graph", name: "graph_h_fill",
default: 80, min: 1, max: 100, units: "%", submit_on_change: false);
container << parent.hubiForm_slider (this, title: "Vertical fill % of the graph", name: "graph_v_fill",
default: 80, min: 1, max: 100, units: "%", submit_on_change: false);
}
container << parent.hubiForm_switch (this, title: "<b>Set size of Graph?</b><br><small>(False = Fill Window)</small>",
name: "graph_static_size", default: false, submit_on_change: true);
if (graph_static_size==true){
container << parent.hubiForm_slider (this, title: "Horizontal dimension of the graph", name: "graph_h_size",
default: 800, min: 100, max: 3000, units: " pixels", submit_on_change: false);
container << parent.hubiForm_slider (this, title: "Vertical dimension of the graph", name: "graph_v_size",
default: 600, min: 100, max: 3000, units: " pixels", submit_on_change: false);
}
parent.hubiForm_container(this, container, 1);
}
parent.hubiForm_section(this,"Horizontal Axis", 1)
{
//Axis
container = [];
container << parent.hubiForm_font_size (this, title: "Horizontal Axis", name: "graph_haxis", default: 9, min: 2, max: 20);
container << parent.hubiForm_color (this, "Horizonal Header", "graph_hh", "#C0C0C0", false);
container << parent.hubiForm_color (this, "Horizonal Axis", "graph_ha", "#C0C0C0", false);
container << parent.hubiForm_text_input (this, "<b>Num Horizontal Gridlines</b><small> (Blank for auto)</small>", "graph_h_num_grid", "", false);
container << parent.hubiForm_text_input (this, "<b>Horizontal Axis Format<b>", "graph_h_format", "", "true");
if (graph_h_format){
today = new Date();
container << parent.hubiForm_text(this, """<i><small><b>Horizontal Axis Sample:</b> ${today.format(graph_h_format)}</small></i>""");
}
container << parent.hubiForm_switch (this, title: "Show String Formatting Help", name: "dummy", default: false, submit_on_change: true);
if (dummy == true){
rows = [];
header = ["<small>Name", "Format", "Result"];
rows << ["Year", "Y", "2020"];
rows << ["Month Number", "M", "12"];
rows << ["Month Name ", "MMM", "Feb"];
rows << ["Month Full Name", "MMMM", "February"];
rows << ["Day of Month", "d", "February"];
rows << ["Day of Week", "EEE", "Mon"];
rows << ["Day of Week", "EEEE", "Monday"];
rows << ["Period", "a", "AM/PM"];
rows << ["Hour (12)", "h", "1..12"];
rows << ["Hour (12)", "hh", "01..12"];
rows << ["Hour (24)", "H", "1..23"];
rows << ["Hour (24)", "HH", "01..23"];
rows << ["Minute", "m", "1..59"];
rows << ["Minute", "mm", "01..59"];
rows << ["Seconds", "s", "1..59"];
rows << ["Seconds", "ss", "01..59 <small>"];
container << parent.hubiForm_table(this, header: header, rows: rows);
container << parent.hubiForm_text(this, """<b><small>Example: "EEEE, MMM d, Y hh:mm:ss a" <br>= "Monday, June 2, 2020 08:21:33 AM</small></b>""")
}
parent.hubiForm_container(this, container, 1);
}
//Vertical Axis
parent.hubiForm_section(this,"Vertical Axis", 1)
{
container = [];
container << parent.hubiForm_font_size (this, title: "Vertical Axis", name: "graph_vaxis", default: 9, min: 2, max: 20);
container << parent.hubiForm_color (this, "Vertical Header", "graph_vh", "#000000", false);
container << parent.hubiForm_color (this, "Vertical Axis", "graph_va", "#C0C0C0", false);
parent.hubiForm_container(this, container, 1);
}
//Left Axis
def formatEnum = [["": "No Formatting ::: 12345"], ["decimal":"Decimal ::: 12,345"], ["short": "Short ::: 12K"], ["scientific": "Scientific ::: 1e5"], ["percent": "Percent ::: 1234500%"], ["long": "Long ::: 12 Thousand"]];
parent.hubiForm_section(this,"Left Axis", 1, "arrow_back"){
input( type: "enum", name: "graph_vaxis_1_format", title: "<b>Number Format</b>", multiple: false, required: true, options: formatEnum, defaultValue: "")
container = [];
container << parent.hubiForm_text_input(this, "<b>Minimum for left axis</b><small> (Blank for auto)</small>", "graph_vaxis_1_min", "", false);
container << parent.hubiForm_text_input(this, "<b>Maximum for left axis</b><small> (Blank for auto)</small>", "graph_vaxis_1_max", "", false);
container << parent.hubiForm_text_input (this, "<b>Num Vertical Gridlines</b><small> (Blank for auto)</small>", "graph_vaxis_1_num_lines", "", false);
container << parent.hubiForm_switch (this, title: "<b>Show Left Axis Label on Graph</b>", name: "graph_show_left_label", default: false, submit_on_change: true);
if (graph_show_left_label==true){
container << parent.hubiForm_text_input (this, "<b>Input Left Axis Label</b>", "graph_left_label", "Left Axis Label", false);
container << parent.hubiForm_font_size (this, title: "Left Axis", name: "graph_left", default: 9, min: 2, max: 20);
container << parent.hubiForm_color (this, "Left Axis", "graph_left", "#FFFFFF", false);
}
parent.hubiForm_container(this, container, 1);
}
//Right Axis
parent.hubiForm_section(this,"Right Axis", 1, "arrow_forward"){
input( type: "enum", name: "graph_vaxis_2_format", title: "<b>Number Format</b>", multiple: false, required: true, options: formatEnum, defaultValue: "")
container = [];
container << parent.hubiForm_text_input(this, "<b>Minimum for right axis</b><small> (Blank for auto)</small>", "graph_vaxis_2_min", "", false);
container << parent.hubiForm_text_input(this, "<b>Maximum for right axis</b><small> (Blank for auto)</small>", "graph_vaxis_2_max", "", false);
container << parent.hubiForm_text_input (this, "<b>Num Vertical Gridlines</b><small> (Blank for auto)</small>", "graph_vaxis_2_num_lines", "", false);
container << parent.hubiForm_switch (this, title: "<b>Show Right Axis Label on Graph</b>", name: "graph_show_right_label", default: false, submit_on_change: true);
if (graph_show_right_label==true){
container << parent.hubiForm_text_input (this, "<b>Input right Axis Label</b>", "graph_right_label", "Right Axis Label", false);
container << parent.hubiForm_font_size (this, title: "Right Axis", name: "graph_right", default: 9, min: 2, max: 20);
container << parent.hubiForm_color (this, "Right Axis", "graph_right", "#FFFFFF", false);
}
parent.hubiForm_container(this, container, 1);
}
//Legend
parent.hubiForm_section(this,"Legend", 1){
container = [];
def legendPosition = [["top": "Top"], ["bottom":"Bottom"], ["in": "Inside Top"]];
def insidePosition = [["start": "Left"], ["center": "Center"], ["end": "Right"]];
container << parent.hubiForm_switch(this, title: "<b>Show Legend on Graph</b>", name: "graph_show_legend", default: false, submit_on_change: true);
if (graph_show_legend==true){
container << parent.hubiForm_font_size (this, title: "Legend", name: "graph_legend", default: 9, min: 2, max: 20);
container << parent.hubiForm_color (this, "Legend", "graph_legend", "#000000", false);
parent.hubiForm_container(this, container, 1);
input( type: "enum", name: "graph_legend_position", title: "<b>Legend Position</b>", defaultValue: "Bottom", options: legendPosition);
input( type: "enum", name: "graph_legend_inside_position", title: "<b>Legend Justification</b>", defaultValue: "center", options: insidePosition);
} else {
parent.hubiForm_container(this, container, 1);
}
}
parent.hubiForm_section(this, "Current Value Overlay", 1){
def horizonalAlignmentEnum = ["Left", "Middle", "Right"];
def veticalAlignmentEnum = ["Top", "Middle", "Bottom"];
container = [];
container << parent.hubiForm_switch (this, title: "<b>Show Current Values on Graph?</b>", name: "show_overlay", default: false, submit_on_change: true);
if (show_overlay == true){
container << parent.hubiForm_color (this, "Background", "overlay_background", "#000000", false);
container << parent.hubiForm_slider (this, title: "Background Opacity",
name: "overlay_background_opacity",
default: 90,
min: 0,
max: 100,
units: "%",
submit_on_change: false);
container << parent.hubiForm_font_size (this, title: "Device", name: "overlay", default: 12, min: 2, max: 40);
container << parent.hubiForm_color (this, "Device Text", "overlay_text", "#FFFFFF", false);
container << parent.hubiForm_enum (this, title: "Horizontal Placement",
name: "overlay_horizontal_placement",
list: horizonalAlignmentEnum,
default: "Right");
container << parent.hubiForm_enum (this, title: "Vertical Placement",
name: "overlay_vertical_placement",
list: veticalAlignmentEnum,
default: "Top");
container << parent.hubiForm_sub_section(this, "Display Order");
parent.hubiForm_container(this, container, 1);
container = [];
parent.hubiForm_list_reorder(this, "overlay_order", "background");
}
parent.hubiForm_container(this, container, 1);
}
state.num_devices = 0;
sensors.each { sensor ->
settings["attributes_${sensor.id}"].each { attribute ->
state.num_devices++;
}
}
def availableAxis = [["0" : "Left Axis"], ["1": "Right Axis"]];
if (state.num_devices == 1) {
availableAxis = [["0" : "Left Axis"], ["1": "Right Axis"], ["2": "Both Axes"]];
}
//Line
cnt = 1;
def bar_size_shown = false;
//Deal with Global-Specific Settings (i.e bar spacing and plot-point size)
def show_tile = false;
def show_bar = false;
def show_scatter = false;
sensors.each { sensor ->
settings["attributes_${sensor.id}"].each { attribute ->
switch (settings["graph_type_${sensor.id}_${attribute}"]){
case "Bar" : show_title = true; show_bar = true; break;
}
}
}
if (show_title){
parent.hubiForm_section(this,"Overall Settings for Graph Types", 1){
container = [];
if (show_bar) {
container << parent.hubiForm_slider (this, title: "Bar Graphs:: Relative Width for Bars",
name: "graph_bar_width",
default: 90,
min: 0,
max: 100,
units: "%",
submit_on_change: false);
}
parent.hubiForm_container(this, container, 1);
}
}
sensors.each { sensor ->
settings["attributes_${sensor.id}"].each { attribute ->
parent.hubiForm_section(this,"${sensor.displayName} - ${attribute}", 1){
container = [];
if (parent.ltsAvailable(sensor.id, attribute)){
container << parent.hubiForm_sub_section(this, "Long Term Storage");
container << parent.hubiForm_switch(this, title: "<b>Long Term Storage Available, Use it?</b>", name: "var_${sensor.id}_${attribute}_lts", default: false, submit_on_change: false);
} else {
app.updateSetting ("var_${sensor.id}_${attribute}_lts", [type: "bool", value: "false"]);
}
container << parent.hubiForm_sub_section(this, "Plot Options");
container << parent.hubiForm_enum (this, title: "Plot Type",
name: "graph_type_${sensor.id}_${attribute}",
list: ["Line", "Area", "Scatter", "Bar", "Stepped"],
default: "Line",
submit_on_change: true);
container << parent.hubiForm_enum (this, title: "Time Integration Function",
name: "var_${sensor.id}_${attribute}_function",
list: ["Average", "Min", "Max", "Mid", "Sum"],
default: "Average");
container << parent.hubiForm_enum (this, title: "Axis Side",
name: "graph_axis_number_${sensor.id}_${attribute}",
list: ["Left", "Right"],
default: "Left");
def colorText = "";
def fillText = "";
def graphType = settings["graph_type_${sensor.id}_${attribute}"];
switch (graphType){
case "Line":
colorText = "Line";
break;
case "Area":
colorText = "Area Line";
fillText = "Fill";
break;
case "Bar":
colorText = "Bar Border";
fillText = "Fill";
break;
case "Scatter":
colorText = "Border";
fillText = "Fill";
break;
case "Stepped":
colorText = "Line";
fillText = "Fill";
break;
}
container << parent.hubiForm_sub_section(this, colorText+" Options");
container << parent.hubiForm_color(this, colorText,
"var_${sensor.id}_${attribute}_stroke",
parent.hubiTools_rotating_colors(cnt),
false);
container << parent.hubiForm_slider (this, title: colorText+" Opacity",
name: "var_${sensor.id}_${attribute}_stroke_opacity",
default: 90,
min: 0,
max: 100,
units: "%",
submit_on_change: false);
container << parent.hubiForm_line_size (this, title: colorText,
name: "var_${sensor.id}_${attribute}_stroke",
default: 2, min: 1, max: 20);
if (graphType == "Bar" || graphType == "Area" || graphType == "Stepped") {
container << parent.hubiForm_sub_section(this, graphType+" "+fillText+" Options");
container << parent.hubiForm_color(this, fillText,
"var_${sensor.id}_${attribute}_fill",
parent.hubiTools_rotating_colors(cnt),
false);
container << parent.hubiForm_slider (this, title: fillText+" Opacity",
name: "var_${sensor.id}_${attribute}_fill_opacity",
default: 90,
min: 0,
max: 100,
units: "%",
submit_on_change: false);
}
if (graphType == "Scatter" || graphType == "Line" || graphType == "Area"){
container << parent.hubiForm_sub_section(this, "Data Points");
if (graphType == "Line" || graphType == "Area"){
container << parent.hubiForm_switch(this, title: "<b>Display Data Points on Line?</b>", name: "var_${sensor.id}_${attribute}_line_plot_points", default: false, submit_on_change: true);
}
if (settings["var_${sensor.id}_${attribute}_line_plot_points"] || graphType == "Scatter"){
container << parent.hubiForm_enum (this,
title: "Point Type",
name: "var_${sensor.id}_${attribute}_point_type",
list: [ "Circle", "Triangle", "Square", "Diamond", "Star", "Polygon"],
default: "Circle");
container << parent.hubiForm_slider (this, title: "Point Size",
name: "var_${sensor.id}_${attribute}_point_size",
default: 5,
min: 0,
max: 60,
units: " points",
submit_on_change: false);
if (graphType == "Area") {
container << parent.hubiForm_text (this, "<b>*Note, Area Plots use the same fill setting for Points and Area (Above)");
} else {
container << parent.hubiForm_color(this, "Point Fill",
"var_${sensor.id}_${attribute}_fill",
parent.hubiTools_rotating_colors(cnt),
false);
container << parent.hubiForm_slider (this, title: "Point Fill Opacity",
name: "var_${sensor.id}_${attribute}_fill_opacity",
default: 90,
min: 0,
max: 100,
units: "%",
submit_on_change: false);
}
} else {
app.updateSetting ("var_${sensor.id}_${attribute}_point_size", 0);
}
}
currentAttribute = null;
enumType = false;
sensor.getSupportedAttributes().each{attrib->
if (attrib.name == attribute){
currentAttribute = attrib;
if (attrib.dataType == "ENUM") enumType = true;
}
}
if (enumType == true){
possible_values = currentAttribute.getValues();
def count_ = 0;
container << parent.hubiForm_sub_section(this, """Numerical values for "$attribute" states""");
possible_values.each{value->
container << parent.hubiForm_text_input(this, "Value for <mark>$value</mark>",
"attribute_${sensor.id}_${attribute}_${value}",
"100",
false);
count_++;
}
app.updateSetting ("attribute_${sensor.id}_${attribute}_states", possible_values);
}
if (enumType == false){
container << parent.hubiForm_sub_section(this, """Custom State Values for "$attribute" """ );
if (settings["attribute_${sensor.id}_${attribute}_custom_states"] == null){
app.updateSetting("attribute_${sensor.id}_${attribute}_custom_states", [type: "bool", value: "false"]);
}
container << parent.hubiForm_switch(this, title: "<b>Set Custom State Values?</b><br><small>(For custom drivers w/ non-numeric values)</small>",
name: "attribute_${sensor.id}_${attribute}_custom_states",
default: false,
submit_on_change: true);
if (settings["attribute_${sensor.id}_${attribute}_custom_states"] == true){
if (!settings["attribute_${sensor.id}_${attribute}_num_custom_states"]){
}
container << parent.hubiForm_text_input(this,"<b>Number of Custom States</b>",
"attribute_${sensor.id}_${attribute}_num_custom_states",
"2", "true");
def numStates = Integer.parseInt(settings["attribute_${sensor.id}_${attribute}_num_custom_states"]);
for (def i=0; i<numStates; i++){
subcontainer = [];
subcontainer << parent.hubiForm_text_input(this, "<b>State #"+(i)+"</b>",
"attribute_${sensor.id}_${attribute}_custom_state_${i}",
"",
"true");
if (settings["attribute_${sensor.id}_${attribute}_custom_state_${i}"]){
subcontainer << parent.hubiForm_text_input(this, '<b>Value for "<mark>'+settings["attribute_${sensor.id}_${attribute}_custom_state_${i}"]+'</mark></b>"',
"attribute_${sensor.id}_${attribute}_custom_state_${i}_value",
"0",
"true");
}
container << parent.hubiForm_subcontainer(this, objects: subcontainer, breakdown: [0.5, 0.5]);
}
//Update Settings
possible_values = [];
for (i=0; i<Integer.parseInt(settings["attribute_${sensor.id}_${attribute}_num_custom_states"]); i++){
if (settings["attribute_${sensor.id}_${attribute}_custom_state_${i}"] &&
settings["attribute_${sensor.id}_${attribute}_custom_state_${i}_value"]){
val = settings["attribute_${sensor.id}_${attribute}_custom_state_${i}"].replaceAll("\\s","");
possible_values << val;
app.updateSetting ("attribute_${sensor.id}_${attribute}_${val}", settings["attribute_${sensor.id}_${attribute}_custom_state_${i}_value"]);
}
}
if (possible_values != []) app.updateSetting ("attribute_${sensor.id}_${attribute}_states", possible_values);
} else {
if (settings["attribute_${sensor.id}_${attribute}_states"]){
possible_values = settings["attribute_${sensor.id}_${attribute}_states"];
possible_values.each{val->
app.updateSetting ("attribute_${sensor.id}_${attribute}_${val}", 0);
}
app.updateSetting ("attribute_${sensor.id}_${attribute}_states", 0);
}
}
}
//Line and Area Graphs can be "Drop-line"
if ((graphType == "Line" || graphType == "Area" || graphType == "Stepped") && enumType==false && settings["attribute_${sensor.id}_${attribute}_custom_states"] == false) {
container << parent.hubiForm_sub_section(this, "Handle Missing Values");
container << parent.hubiForm_switch(this, title: "<b>Display Missing Data as a Drop Line?</b>", name: "attribute_${sensor.id}_${attribute}_drop_line", default: false, submit_on_change: true);
if (settings["attribute_${sensor.id}_${attribute}_drop_line"]==true){
container << parent.hubiForm_text_input(this,"<b>Value of Missing Data</b>",
"attribute_${sensor.id}_${attribute}_drop_value",
"0", false);
}
container << parent.hubiForm_switch(this, title: "<b>Extend Left Value?</b><br><small>When values are unavailable, extend value to left</small>",
name: "attribute_${sensor.id}_${attribute}_extend_left", default: false, submit_on_change: false);
container << parent.hubiForm_switch(this, title: "<b>Extend Right Value?</b><br><small>When values are unavailable, extend value to right</small>",
name: "attribute_${sensor.id}_${attribute}_extend_right", default: false, submit_on_change: false);
}
container << parent.hubiForm_sub_section(this, "Restrict Displayed Values");
container << parent.hubiForm_switch(this, title: "<b>Restrict Displaying Bad Values?</b>", name: "attribute_${sensor.id}_${attribute}_bad_value", default: false, submit_on_change: true);
if (settings["attribute_${sensor.id}_${attribute}_bad_value"]==true){
container << parent.hubiForm_text_input(this,"<b>Min Value to Exclude</b><br><small>If the recorded sensor value is <b>below</b> this value it will be dropped</small>",
"attribute_${sensor.id}_${attribute}_min_value",
"0", false);
container << parent.hubiForm_text_input(this,"<b>Max Value to Exclude</b><br><small>If the recorded sensor value is <b>above</b> this value it will be dropped</small>",
"attribute_${sensor.id}_${attribute}_max_value",
"100", false);
}
container << parent.hubiForm_sub_section(this, "Override Display Name on Graph");
container << parent.hubiForm_text_input(this, "<small></i>Use %deviceName% for DEVICE and %attributeName% for ATTRIBUTE</i></small>",
"graph_name_override_${sensor.id}_${attribute}",
"%deviceName%: %attributeName%", false);
container << parent.hubiForm_text_input(this, "<b>Units for Pretty Display</b>",
"units_${sensor.id}_${attribute}",
"", false);
parent.hubiForm_container(this, container, 1);
cnt += 1;
}//parent.hubiForm
}//attribute
}//sensor
}//page
}//function
def deviceSelectionPage() {
def final_attrs;
dynamicPage(name: "deviceSelectionPage") {
parent.hubiForm_section(this,"Device Selection", 1){
input "sensors", "capability.*", title: "Sensors", multiple: true, required: true, submitOnChange: true
if (sensors){
sensors.each {
attributes_ = it.getSupportedAttributes();
final_attrs = [];
attributes_.each{ attribute_->
name = attribute_.getName();
if (it.currentState(name)){
final_attrs << ["$name" : "$name ::: [${it.currentState(name).getValue()}]"];
}
}
final_attrs = final_attrs.unique(false);
container = [];
container << parent.hubiForm_sub_section(this, it.displayName);
parent.hubiForm_container(this, container, 1);
input( type: "enum", name: "attributes_${it.id}", title: "Attributes to graph", required: true, multiple: true, options: final_attrs, defaultValue: "1")
}
}
}
}
}
def disableAPIPage() {
dynamicPage(name: "disableAPIPage") {
section() {
if (state.endpoint) {
revokeAccessToken();
state.endpoint = null
}
paragraph "Token revoked. Click done to continue."
}
}
}
def enableAPIPage() {
dynamicPage(name: "enableAPIPage", title: "") {
section() {
if(!state.endpoint) initializeAppEndpoint();
paragraph "Token created. Click done to continue."
}
}
}
def mainPage() {
unschedule();
dynamicPage(name: "mainPage") {
def container = [];
if (!state.endpoint) {
parent.hubiForm_section(this, "Please set up OAuth API", 1, "report"){
href name: "enableAPIPageLink", title: "Enable API", description: "", page: "enableAPIPage"
}
} else {
parent.hubiForm_section(this, "Graph Options", 1, "tune"){
container = [];
container << parent.hubiForm_page_button(this, "Select Device/Data", "deviceSelectionPage", "100%", "vibration");
container << parent.hubiForm_page_button(this, "Configure Graph", "graphSetupPage", "100%", "poll");
parent.hubiForm_container(this, container, 1);
}
parent.hubiForm_section(this, "Local Graph URL", 1, "link"){
container = [];
container << parent.hubiForm_text(this, "${state.localEndpointURL}graph/?access_token=${state.endpointSecret}");
parent.hubiForm_container(this, container, 1);
}
if (graph_timespan){
parent.hubiForm_section(this, "Preview", 10, "show_chart"){
container = [];
container << parent.hubiForm_graph_preview(this)
parent.hubiForm_container(this, container, 1);
} //graph_timespan
parent.hubiForm_section(this, "Hubigraph Tile Installation", 2, "apps"){
container = [];
container << parent.hubiForm_switch(this, title: "Install Hubigraph Tile Device?", name: "install_device", default: false, submit_on_change: true);
if (install_device==true){
container << parent.hubiForm_text_input(this, "Name for HubiGraph Tile Device", "device_name", "Hubigraph Tile", "false");
}
parent.hubiForm_container(this, container, 1);
}
}
if (state.endpoint){
parent.hubiForm_section(this, "Hubigraph Application", 1, "settings"){
container = [];
container << parent.hubiForm_sub_section(this, "Application Name");
container << parent.hubiForm_text_input(this, "Rename the Application?", "app_name", "Hubigraph Time Graph", "false");
container << parent.hubiForm_sub_section(this, "Debugging");
container << parent.hubiForm_switch(this, title: "Enable Debug Logging?", name: "debug", default: false);
container << parent.hubiForm_sub_section(this, "Disable Oauth Authorization");
container << parent.hubiForm_page_button(this, "Disable API", "disableAPIPage", "100%", "cancel");
parent.hubiForm_container(this, container, 1);
}
}
} //else
} //dynamicPage
}
/********************************************************************************************************************************
*********************************************************************************************************************************
****************************************** END PAGES ********************************************************************************
*********************************************************************************************************************************
*********************************************************************************************************************************/
def getDays(str){
switch (str){
case "1 Day": return 1; break;
case "2 Days": return 2; break;
case "3 Days": return 3; break;
case "4 Days": return 4; break;
case "5 Days": return 5; break;
case "6 Days": return 6; break;
case "1 Week": return 7; break;
case "2 Weeks": return 14; break;
case "3 Weeks": return 21; break;
case "1 Month": return 30; break;
case "2 Months": return 60; break;
case "Indefinite": return 0; break;
}
}
def installed() {
initialize()
}
def uninstalled() {
if (state.endpoint) {
try {
revokeAccessToken()
}
catch (e) {
log.warn "Unable to revoke API access token: $e"
}
}
removeChildDevices(getChildDevices());
}
private removeChildDevices(delete) {
delete.each {deleteChildDevice(it.deviceNetworkId)}
}
def updated() {
app.updateLabel(app_name);
if (install_device == true){
parent.hubiTool_create_tile(this);
}
}
def initialize() {
updated();
}
private getValue(id, attr, val){
def reg = ~/[a-z,A-Z]+/;
orig = val;
val = "${val}"
val = val.replaceAll("\\s","");
if (settings["attribute_${id}_${attr}_${val}"]!=null){
ret = Double.parseDouble(settings["attribute_${id}_${attr}_${val}"]);
} else {
try {
ret = Double.parseDouble(val - reg);
} catch (e) {
log.debug ("Bad value in Parse: "+orig);
ret = null;
}
}
return ret;
}
private cleanupData(data){
def then = new Date();
use (groovy.time.TimeCategory) {
then -= getDays(lts_time);
}
then_milliseconds = then.getTime();
return data.findAll{ it.date >= then_milliseconds };
}
private buildData() {
def resp = [:]
def graph_time;
def then = new Date();
use (groovy.time.TimeCategory) {
val = Double.parseDouble("${graph_timespan}")/1000.0;
then -= ((int)val).seconds;
graph_time = then.getTime();
}
if(sensors) {
sensors.each { sensor ->
resp[sensor.id] = [:];
settings["attributes_${sensor.id}"].each {attribute ->
start = new Date();
data = parent.getData(sensor, attribute, settings["var_${sensor.id}_${attribute}_lts"], then);
data = data.collect{[date: it.date.getTime(), value: getValue(sensor.id, attribute, it.value)]}
resp[sensor.id][attribute] = data.findAll{ it.date > graph_time};
//Restrict "bad" values
if (settings["attribute_${sensor.id}_${attribute}_bad_value"]==true){
min = Float.valueOf(settings["attribute_${sensor.id}_${attribute}_min_value"]);
max = Float.valueOf(settings["attribute_${sensor.id}_${attribute}_max_value"]);
resp[sensor.id][attribute] = resp[sensor.id][attribute].findAll{ it.value > min && it.value < max};
}
atomicState["history_${sensor.id}_${attribute}"] = null;
end = new Date();
}
}
}
return resp;
}
def getChartOptions(){
/*Setup Series*/
def series = ["series" : [:]];
def options = [
"graphReduction": graph_max_points,
"graphTimespan": Long.parseLong("${graph_timespan}"),