diff --git a/src/controller/java/src/chip/devicecontroller/model/AttributeWriteRequest.java b/src/controller/java/src/chip/devicecontroller/model/AttributeWriteRequest.java index 0b74693480f5ba..792b7577efc15d 100644 --- a/src/controller/java/src/chip/devicecontroller/model/AttributeWriteRequest.java +++ b/src/controller/java/src/chip/devicecontroller/model/AttributeWriteRequest.java @@ -20,23 +20,47 @@ import java.util.Locale; import java.util.Objects; import java.util.Optional; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.annotation.Nullable; +import org.json.JSONException; +import org.json.JSONObject; /** An attribute write request that should be used for interaction model write interaction. */ public final class AttributeWriteRequest { + private static final Logger logger = Logger.getLogger(AttributeWriteRequest.class.getName()); private final ChipPathId endpointId, clusterId, attributeId; private final Optional dataVersion; - private final byte[] tlv; + @Nullable private final byte[] tlv; + @Nullable private final JSONObject json; private AttributeWriteRequest( ChipPathId endpointId, ChipPathId clusterId, ChipPathId attributeId, - byte[] tlv, + @Nullable byte[] tlv, + @Nullable String jsonString, Optional dataVersion) { this.endpointId = endpointId; this.clusterId = clusterId; this.attributeId = attributeId; - this.tlv = tlv.clone(); + + if (tlv != null) { + this.tlv = tlv.clone(); + } else { + this.tlv = null; + } + + JSONObject jsonObject = null; + if (jsonString != null) { + try { + jsonObject = new JSONObject(jsonString); + } catch (JSONException ex) { + logger.log(Level.SEVERE, "Error parsing JSON string", ex); + } + } + + this.json = jsonObject; this.dataVersion = dataVersion; } @@ -60,8 +84,23 @@ public boolean hasDataVersion() { return dataVersion.isPresent(); } + @Nullable public byte[] getTlvByteArray() { - return tlv.clone(); + if (tlv != null) { + return tlv.clone(); + } + return null; + } + + @Nullable + public JSONObject getJson() { + return json; + } + + @Nullable + public String getJsonString() { + if (json == null) return null; + return json.toString(); } // check whether the current AttributeWriteRequest has same path as others. @@ -93,7 +132,8 @@ public String toString() { public static AttributeWriteRequest newInstance( ChipPathId endpointId, ChipPathId clusterId, ChipPathId attributeId, byte[] tlv) { - return new AttributeWriteRequest(endpointId, clusterId, attributeId, tlv, Optional.empty()); + return new AttributeWriteRequest( + endpointId, clusterId, attributeId, tlv, null, Optional.empty()); } public static AttributeWriteRequest newInstance( @@ -102,7 +142,7 @@ public static AttributeWriteRequest newInstance( ChipPathId attributeId, byte[] tlv, Optional dataVersion) { - return new AttributeWriteRequest(endpointId, clusterId, attributeId, tlv, dataVersion); + return new AttributeWriteRequest(endpointId, clusterId, attributeId, tlv, null, dataVersion); } /** Create a new {@link AttributeWriteRequest} with only concrete ids. */ @@ -113,9 +153,11 @@ public static AttributeWriteRequest newInstance( ChipPathId.forId(clusterId), ChipPathId.forId(attributeId), tlv, + null, Optional.empty()); } + /** Create a new {@link AttributeWriteRequest} with only concrete ids. */ public static AttributeWriteRequest newInstance( int endpointId, long clusterId, long attributeId, byte[] tlv, Optional dataVersion) { return new AttributeWriteRequest( @@ -123,6 +165,51 @@ public static AttributeWriteRequest newInstance( ChipPathId.forId(clusterId), ChipPathId.forId(attributeId), tlv, + null, + dataVersion); + } + + public static AttributeWriteRequest newInstance( + ChipPathId endpointId, ChipPathId clusterId, ChipPathId attributeId, String jsonString) { + return new AttributeWriteRequest( + endpointId, clusterId, attributeId, null, jsonString, Optional.empty()); + } + + /** Create a new {@link AttributeWriteRequest} with only concrete ids. */ + public static AttributeWriteRequest newInstance( + int endpointId, long clusterId, long attributeId, String jsonString) { + return new AttributeWriteRequest( + ChipPathId.forId(endpointId), + ChipPathId.forId(clusterId), + ChipPathId.forId(attributeId), + null, + jsonString, + Optional.empty()); + } + + public static AttributeWriteRequest newInstance( + ChipPathId endpointId, + ChipPathId clusterId, + ChipPathId attributeId, + String jsonString, + Optional dataVersion) { + return new AttributeWriteRequest( + endpointId, clusterId, attributeId, null, jsonString, dataVersion); + } + + /** Create a new {@link AttributeWriteRequest} with only concrete ids. */ + public static AttributeWriteRequest newInstance( + int endpointId, + long clusterId, + long attributeId, + String jsonString, + Optional dataVersion) { + return new AttributeWriteRequest( + ChipPathId.forId(endpointId), + ChipPathId.forId(clusterId), + ChipPathId.forId(attributeId), + null, + jsonString, dataVersion); } } diff --git a/src/controller/java/src/chip/devicecontroller/model/InvokeElement.java b/src/controller/java/src/chip/devicecontroller/model/InvokeElement.java index 2ae4b55ff423e6..1a97bf64bc012a 100644 --- a/src/controller/java/src/chip/devicecontroller/model/InvokeElement.java +++ b/src/controller/java/src/chip/devicecontroller/model/InvokeElement.java @@ -81,10 +81,16 @@ public byte[] getTlvByteArray() { } @Nullable - public JSONObject getJson() { + public JSONObject getJsonObject() { return json; } + @Nullable + public String getJsonString() { + if (json == null) return null; + return json.toString(); + } + // check whether the current InvokeElement has same path as others. @Override public boolean equals(Object object) {