From 6031813143aa52fd889dee11060afa5adb43cf1d Mon Sep 17 00:00:00 2001 From: yunhanw-google Date: Wed, 28 Sep 2022 05:11:01 -0700 Subject: [PATCH] event should be stored in array for java/android (#22909) --- .../clusterclient/WildcardFragment.kt | 17 ++++++----- .../devicecontroller/model/ClusterState.java | 30 +++++++++++-------- .../devicecontroller/model/NodeState.java | 7 +++-- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/WildcardFragment.kt b/src/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/WildcardFragment.kt index 9a29d0d598d549..dd2f366e28f469 100644 --- a/src/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/WildcardFragment.kt +++ b/src/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/WildcardFragment.kt @@ -95,13 +95,16 @@ class WildcardFragment : Fragment() { val attributeName = ChipIdLookup.attributeIdToName(clusterId, attributeId) stringBuilder.append("\t\t$attributeName: ${attributeState.value}\n") } - clusterState.eventStates.forEach { (eventId, eventState) -> - stringBuilder.append("\t\teventNumber: ${eventState.eventNumber}\n") - stringBuilder.append("\t\tpriorityLevel: ${eventState.priorityLevel}\n") - stringBuilder.append("\t\tsystemTimeStamp: ${eventState.systemTimeStamp}\n") - - val eventName = ChipIdLookup.eventIdToName(clusterId, eventId) - stringBuilder.append("\t\t$eventName: ${eventState.value}\n") + clusterState.eventStates.forEach { (eventId, events) -> + for (event in events) + { + stringBuilder.append("\t\teventNumber: ${event.eventNumber}\n") + stringBuilder.append("\t\tpriorityLevel: ${event.priorityLevel}\n") + stringBuilder.append("\t\tsystemTimeStamp: ${event.systemTimeStamp}\n") + + val eventName = ChipIdLookup.eventIdToName(clusterId, eventId) + stringBuilder.append("\t\t$eventName: ${event.value}\n") + } } stringBuilder.append("\t}\n") } diff --git a/src/controller/java/src/chip/devicecontroller/model/ClusterState.java b/src/controller/java/src/chip/devicecontroller/model/ClusterState.java index e90d0e76acec54..4ce1c1e62e1824 100644 --- a/src/controller/java/src/chip/devicecontroller/model/ClusterState.java +++ b/src/controller/java/src/chip/devicecontroller/model/ClusterState.java @@ -18,16 +18,18 @@ package chip.devicecontroller.model; import androidx.annotation.Nullable; +import java.util.ArrayList; import java.util.Map; import java.util.Optional; /** Class for tracking CHIP cluster state in a hierarchical manner. */ public final class ClusterState { private Map attributes; - private Map events; + private Map> events; private Optional dataVersion; - public ClusterState(Map attributes, Map events) { + public ClusterState( + Map attributes, Map> events) { this.attributes = attributes; this.events = events; this.dataVersion = Optional.empty(); @@ -37,7 +39,7 @@ public Map getAttributeStates() { return attributes; } - public Map getEventStates() { + public Map> getEventStates() { return events; } @@ -60,12 +62,12 @@ public AttributeState getAttributeState(long attributeId) { } /** - * Convenience utility for getting an {@code EventState}. + * Convenience utility for getting an {@code ArrayList }. * - * @return the {@code EventState} for the specified id, or null if not found. + * @return the {@code ArrayList} for the specified id, or null if not found. */ @Nullable - public EventState getEventState(long eventId) { + public ArrayList getEventState(long eventId) { return events.get(eventId); } @@ -82,12 +84,16 @@ public String toString() { builder.append("\n"); }); events.forEach( - (eventId, eventState) -> { - builder.append("Event "); - builder.append(eventId); - builder.append(": "); - builder.append(eventState.getValue() == null ? "null" : eventState.getValue().toString()); - builder.append("\n"); + (eventId, eventStates) -> { + eventStates.forEach( + (eventState) -> { + builder.append("Event "); + builder.append(eventId); + builder.append(": "); + builder.append( + eventState.getValue() == null ? "null" : eventState.getValue().toString()); + builder.append("\n"); + }); }); return builder.toString(); } diff --git a/src/controller/java/src/chip/devicecontroller/model/NodeState.java b/src/controller/java/src/chip/devicecontroller/model/NodeState.java index 652dc3265ffab1..1dab3ebc0ea3b6 100644 --- a/src/controller/java/src/chip/devicecontroller/model/NodeState.java +++ b/src/controller/java/src/chip/devicecontroller/model/NodeState.java @@ -18,6 +18,7 @@ package chip.devicecontroller.model; import androidx.annotation.Nullable; +import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -75,8 +76,10 @@ private void addEvent(int endpointId, long clusterId, long eventId, EventState e endpointState.getClusterStates().put(clusterId, clusterState); } - // This will overwrite previous events. - clusterState.getEventStates().put(eventId, eventStateToAdd); + if (!clusterState.getEventStates().containsKey(eventId)) { + clusterState.getEventStates().put(eventId, new ArrayList()); + } + clusterState.getEventStates().get(eventId).add(eventStateToAdd); } @Override