28
28
import org .eclipse .jdt .annotation .Nullable ;
29
29
import org .openhab .binding .hue .internal .api .dto .clip2 .enums .ActionType ;
30
30
import org .openhab .binding .hue .internal .api .dto .clip2 .enums .ButtonEventType ;
31
+ import org .openhab .binding .hue .internal .api .dto .clip2 .enums .CategoryType ;
31
32
import org .openhab .binding .hue .internal .api .dto .clip2 .enums .ContactStateType ;
33
+ import org .openhab .binding .hue .internal .api .dto .clip2 .enums .ContentType ;
32
34
import org .openhab .binding .hue .internal .api .dto .clip2 .enums .EffectType ;
33
35
import org .openhab .binding .hue .internal .api .dto .clip2 .enums .ResourceType ;
34
36
import org .openhab .binding .hue .internal .api .dto .clip2 .enums .SceneRecallAction ;
55
57
56
58
import com .google .gson .JsonElement ;
57
59
import com .google .gson .JsonObject ;
60
+ import com .google .gson .JsonPrimitive ;
58
61
import com .google .gson .annotations .SerializedName ;
59
62
60
63
/**
@@ -74,8 +77,16 @@ public class Resource {
74
77
* values have changed. A sparse resource does not contain the full state of the resource. And the absence of any
75
78
* field from such a resource does not indicate that the field value is UNDEF, but rather that the value is the same
76
79
* as what it was previously set to by the last non-sparse resource.
80
+ * <p>
81
+ * The following content types are defined:
82
+ *
83
+ * <li><b>ADD</b> resource being added; contains (assumed) all fields</li>
84
+ * <li><b>DELETE</b> resource being deleted; contains id and type only</li>
85
+ * <li><b>UPDATE</b> resource being updated; contains id, type and changed fields</li>
86
+ * <li><b>ERROR</b> resource with error; contents unknown</li>
87
+ * <li><b>FULL_STATE</b> existing resource being downloaded; contains all fields</li>
77
88
*/
78
- private transient boolean hasSparseData ;
89
+ private transient ContentType contentType ;
79
90
80
91
private @ Nullable String type ;
81
92
private @ Nullable String id ;
@@ -107,14 +118,23 @@ public class Resource {
107
118
private @ Nullable Dynamics dynamics ;
108
119
private @ Nullable @ SerializedName ("contact_report" ) ContactReport contactReport ;
109
120
private @ Nullable @ SerializedName ("tamper_reports" ) List <TamperReport > tamperReports ;
110
- private @ Nullable String state ;
121
+ private @ Nullable JsonElement state ;
122
+ private @ Nullable @ SerializedName ("script_id" ) String scriptId ;
123
+
124
+ /**
125
+ * Constructor
126
+ */
127
+ public Resource () {
128
+ contentType = ContentType .FULL_STATE ;
129
+ }
111
130
112
131
/**
113
132
* Constructor
114
133
*
115
134
* @param resourceType
116
135
*/
117
136
public Resource (@ Nullable ResourceType resourceType ) {
137
+ this ();
118
138
if (Objects .nonNull (resourceType )) {
119
139
setType (resourceType );
120
140
}
@@ -343,6 +363,14 @@ public State getColorTemperaturePercentState() {
343
363
return color ;
344
364
}
345
365
366
+ /**
367
+ * Return the resource's metadata category.
368
+ */
369
+ public CategoryType getCategory () {
370
+ MetaData metaData = getMetaData ();
371
+ return Objects .nonNull (metaData ) ? metaData .getCategory () : CategoryType .NULL ;
372
+ }
373
+
346
374
/**
347
375
* Return an HSB where the HS part is derived from the color xy JSON element (only), so the B part is 100%
348
376
*
@@ -375,6 +403,10 @@ public State getContactState() {
375
403
: OpenClosedType .OPEN ;
376
404
}
377
405
406
+ public ContentType getContentType () {
407
+ return contentType ;
408
+ }
409
+
378
410
public int getControlId () {
379
411
MetaData metadata = this .metadata ;
380
412
return Objects .nonNull (metadata ) ? metadata .getControlId () : 0 ;
@@ -648,6 +680,13 @@ public Optional<Boolean> getSceneActive() {
648
680
return Optional .empty ();
649
681
}
650
682
683
+ /**
684
+ * Return the scriptId if any.
685
+ */
686
+ public @ Nullable String getScriptId () {
687
+ return scriptId ;
688
+ }
689
+
651
690
/**
652
691
* If the getSceneActive() optional result is empty return 'UnDefType.NULL'. Otherwise if the optional result is
653
692
* present and 'true' (i.e. the scene is active) return the scene name. Or finally (the optional result is present
@@ -661,13 +700,14 @@ public State getSceneState() {
661
700
662
701
/**
663
702
* Check if the smart scene resource contains a 'state' element. If such an element is present, returns a Boolean
664
- * Optional whose value depends on the value of that element, or an empty Optional if it is not.
703
+ * Optional whose value depends on the value of that element, or an empty Optional if it is not. Note that in some
704
+ * resource types the 'state' element is not a String primitive.
665
705
*
666
706
* @return true, false, or empty.
667
707
*/
668
708
public Optional <Boolean > getSmartSceneActive () {
669
- if (ResourceType .SMART_SCENE == getType ()) {
670
- String state = this . state ;
709
+ if (ResourceType .SMART_SCENE == getType () && ( state instanceof JsonPrimitive statePrimitive ) ) {
710
+ String state = statePrimitive . getAsString () ;
671
711
if (Objects .nonNull (state )) {
672
712
return Optional .of (SmartSceneState .ACTIVE == SmartSceneState .of (state ));
673
713
}
@@ -785,17 +825,12 @@ public State getZigbeeState() {
785
825
}
786
826
787
827
public boolean hasFullState () {
788
- return ! hasSparseData ;
828
+ return ContentType . FULL_STATE == contentType ;
789
829
}
790
830
791
- /**
792
- * Mark that the resource has sparse data.
793
- *
794
- * @return this instance.
795
- */
796
- public Resource markAsSparse () {
797
- hasSparseData = true ;
798
- return this ;
831
+ public boolean hasName () {
832
+ MetaData metaData = getMetaData ();
833
+ return Objects .nonNull (metaData ) && Objects .nonNull (metaData .getName ());
799
834
}
800
835
801
836
public Resource setAlerts (Alerts alert ) {
@@ -818,6 +853,11 @@ public Resource setContactReport(ContactReport contactReport) {
818
853
return this ;
819
854
}
820
855
856
+ public Resource setContentType (ContentType contentType ) {
857
+ this .contentType = contentType ;
858
+ return this ;
859
+ }
860
+
821
861
public Resource setDimming (@ Nullable Dimming dimming ) {
822
862
this .dimming = dimming ;
823
863
return this ;
0 commit comments