Skip to content

Commit

Permalink
add missing json member for AttributeWriteRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
yunhanw-google committed Aug 15, 2023
1 parent 39511c2 commit b24b178
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@
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,
Expand All @@ -38,6 +45,37 @@ private AttributeWriteRequest(
this.attributeId = attributeId;
this.tlv = tlv.clone();
this.dataVersion = dataVersion;
this.json = null;
}

private AttributeWriteRequest(
ChipPathId endpointId,
ChipPathId clusterId,
ChipPathId attributeId,
@Nullable byte[] tlv,
@Nullable String jsonString,
Optional<Integer> dataVersion) {
this.endpointId = endpointId;
this.clusterId = clusterId;
this.attributeId = attributeId;

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;
}

public ChipPathId getEndpointId() {
Expand All @@ -60,8 +98,24 @@ 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 @@ -125,4 +179,58 @@ public static AttributeWriteRequest newInstance(
tlv,
dataVersion);
}

public static AttributeWriteRequest newInstance(
ChipPathId endpointId,
ChipPathId clusterId,
ChipPathId attributeId,
@Nullable byte[] tlv,
@Nullable String jsonString,
Optional<Integer> dataVersion) {
return new AttributeWriteRequest(
endpointId, clusterId, attributeId, tlv, jsonString, dataVersion);
}

/** Create a new {@link AttributeWriteRequest} with only concrete ids. */
public static AttributeWriteRequest newInstance(
int endpointId,
long clusterId,
long attributeId,
@Nullable byte[] tlv,
@Nullable String jsonString,
Optional<Integer> dataVersion) {
return new AttributeWriteRequest(
ChipPathId.forId(endpointId),
ChipPathId.forId(clusterId),
ChipPathId.forId(attributeId),
tlv,
jsonString,
dataVersion);
}

public static AttributeWriteRequest newInstance(
ChipPathId endpointId,
ChipPathId clusterId,
ChipPathId attributeId,
@Nullable byte[] tlv,
@Nullable String jsonString) {
return new AttributeWriteRequest(
endpointId, clusterId, attributeId, tlv, jsonString, Optional.empty());
}

/** Create a new {@link AttributeWriteRequest} with only concrete ids. */
public static AttributeWriteRequest newInstance(
int endpointId,
long clusterId,
long attributeId,
@Nullable byte[] tlv,
@Nullable String jsonString) {
return new AttributeWriteRequest(
ChipPathId.forId(endpointId),
ChipPathId.forId(clusterId),
ChipPathId.forId(attributeId),
tlv,
jsonString,
Optional.empty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,17 @@ 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 b24b178

Please sign in to comment.