Skip to content

Commit

Permalink
add missing json member for AttributeWriteRequest (project-chip#28712)
Browse files Browse the repository at this point in the history
  • Loading branch information
yunhanw-google authored Aug 16, 2023
1 parent bd771be commit 07fa5bf
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> 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<Integer> 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;
}

Expand All @@ -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.
Expand Down Expand Up @@ -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(
Expand All @@ -102,7 +142,7 @@ public static AttributeWriteRequest newInstance(
ChipPathId attributeId,
byte[] tlv,
Optional<Integer> 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. */
Expand All @@ -113,16 +153,63 @@ 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<Integer> dataVersion) {
return new AttributeWriteRequest(
ChipPathId.forId(endpointId),
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<Integer> 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<Integer> dataVersion) {
return new AttributeWriteRequest(
ChipPathId.forId(endpointId),
ChipPathId.forId(clusterId),
ChipPathId.forId(attributeId),
null,
jsonString,
dataVersion);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 07fa5bf

Please sign in to comment.