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 3d2a9ee
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ class MultiAdminClientFragment : Fragment() {
ADMINISTRATOR_COMMISSIONING_CLUSTER_ENDPOINT_ID,
AdministratorCommissioning.ID,
AdministratorCommissioning.Command.RevokeCommissioning.id,
tlvWriter.getEncoded(),
null
tlvWriter.getEncoded()
)

deviceController.invoke(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,7 @@ class OnOffClientFragment : Fragment() {
addressUpdateFragment.endpointId,
LevelControl.ID,
LevelControl.Command.MoveToLevel.id,
tlvWriter.getEncoded(),
null
tlvWriter.getEncoded()
)

deviceController.invoke(
Expand Down Expand Up @@ -293,8 +292,7 @@ class OnOffClientFragment : Fragment() {
addressUpdateFragment.endpointId,
OnOff.ID,
commandId.id,
tlvWriter.getEncoded(),
null
tlvWriter.getEncoded()
)

deviceController.invoke(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,7 @@ class OpCredClientFragment : Fragment() {
addressUpdateFragment.endpointId,
OperationalCredentials.ID,
OperationalCredentials.Command.RemoveFabric.id,
tlvWriter.getEncoded(),
null
tlvWriter.getEncoded()
)

deviceController.invoke(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ class WildcardFragment : Fragment() {
}
tlvWriter.endStructure()
val invokeElement =
InvokeElement.newInstance(endpointId, clusterId, commandId, tlvWriter.getEncoded(), null)
InvokeElement.newInstance(endpointId, clusterId, commandId, tlvWriter.getEncoded())
deviceController.invoke(
invokeCallback,
ChipClient.getConnectedDevicePointer(requireContext(), addressUpdateFragment.deviceId),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ class PairOnNetworkLongImInvokeCommand(
/* endpointId= */ 0,
CLUSTER_ID_IDENTIFY,
IDENTIFY_COMMAND,
tlvWriter.getEncoded(),
null
tlvWriter.getEncoded()
)

currentCommissioner()
Expand Down
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,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 @@ -93,7 +133,7 @@ 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,73 @@ 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) {
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,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 Expand Up @@ -112,23 +119,43 @@ public static InvokeElement newInstance(
ChipPathId endpointId,
ChipPathId clusterId,
ChipPathId commandId,
@Nullable byte[] tlv,
@Nullable String jsonString) {
return new InvokeElement(endpointId, clusterId, commandId, tlv, jsonString);
byte[] tlv) {
return new InvokeElement(endpointId, clusterId, commandId, tlv, null);
}

/** Create a new {@link InvokeElement} with only concrete ids. */
public static InvokeElement newInstance(
public static InvokeElement newInstance(
int endpointId,
long clusterId,
long commandId,
@Nullable byte[] tlv,
@Nullable String jsonString) {
byte[] tlv) {
return new InvokeElement(
ChipPathId.forId(endpointId),
ChipPathId.forId(clusterId),
ChipPathId.forId(commandId),
tlv,
null);
}

public static InvokeElement newInstance(
ChipPathId endpointId,
ChipPathId clusterId,
ChipPathId commandId,
String jsonString) {
return new InvokeElement(endpointId, clusterId, commandId, null, jsonString);
}

/** Create a new {@link InvokeElement} with only concrete ids. */
public static InvokeElement newInstance(
int endpointId,
long clusterId,
long commandId,
String jsonString) {
return new InvokeElement(
ChipPathId.forId(endpointId),
ChipPathId.forId(clusterId),
ChipPathId.forId(commandId),
null,
jsonString);
}
}

0 comments on commit 3d2a9ee

Please sign in to comment.