Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android]add missing json member for AttributeWriteRequest #28712

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
yunhanw-google marked this conversation as resolved.
Show resolved Hide resolved

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