Skip to content

Commit 905dc36

Browse files
mthiescrestyled-commitslazarkov
authored
Serialize ContentApp SupportedCluster set (#34144)
* Serialize ContentApp SupportedCluster set * Restyled by google-java-format * Update EndpointsDataStore.java * Restyled by google-java-format --------- Co-authored-by: Restyled.io <commits@restyled.io> Co-authored-by: Lazar Kovacic <lkovacic@amazon.com>
1 parent 89a6f5e commit 905dc36

File tree

2 files changed

+113
-17
lines changed

2 files changed

+113
-17
lines changed

examples/tv-app/android/App/platform-app/src/main/java/com/matter/tv/server/model/ContentApp.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public ContentApp(
2525
this.vendorId = vendorId;
2626
this.productId = productId;
2727
this.version = version;
28+
this.supportedClusters = Collections.EMPTY_SET;
2829
}
2930

3031
public ContentApp(
@@ -67,9 +68,7 @@ public void setEndpointId(int endpoint) {
6768
}
6869

6970
public Set<SupportedCluster> getSupportedClusters() {
70-
return supportedClusters != null
71-
? Collections.unmodifiableSet(supportedClusters)
72-
: Collections.EMPTY_SET;
71+
return Collections.unmodifiableSet(supportedClusters);
7372
}
7473

7574
public String getVersion() {

examples/tv-app/android/App/platform-app/src/main/java/com/matter/tv/server/utils/EndpointsDataStore.java

+111-14
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44
import android.content.SharedPreferences;
55
import android.util.JsonReader;
66
import android.util.JsonWriter;
7+
import com.matter.tv.app.api.SupportedCluster;
78
import com.matter.tv.server.model.ContentApp;
89
import java.io.IOException;
910
import java.io.StringReader;
1011
import java.io.StringWriter;
12+
import java.util.ArrayList;
1113
import java.util.Collections;
1214
import java.util.HashMap;
15+
import java.util.HashSet;
16+
import java.util.List;
1317
import java.util.Map;
18+
import java.util.Set;
1419

1520
public class EndpointsDataStore {
1621

@@ -21,6 +26,11 @@ public class EndpointsDataStore {
2126
private static final String KEY_PRODUCTID = "PID";
2227
private static final String KEY_VERSION = "VER";
2328
private static final String KEY_ENDPOINTID = "EPID";
29+
private static final String KEY_SUPPORTED_CLUSTERS = "supportedClusters";
30+
private static final String KEY_CLUSTER_IDENTIFIER = "clusterIdentifier";
31+
private static final String KEY_FEATURES = "features";
32+
private static final String KEY_OPTIONAL_COMMAND_IDENTIFIERS = "optionalCommandIdentifiers";
33+
private static final String KEY_OPTIONAL_ATTRIBUTES_IDENTIFIERS = "optionalAttributesIdentifiers";
2434
private static EndpointsDataStore instance;
2535
private final SharedPreferences discoveredEndpoints;
2636
Map<String, ContentApp> persistedContentApps = new HashMap<>();
@@ -57,19 +67,20 @@ private String serializeContentApp(ContentApp app) {
5767
StringWriter stringWriter = new StringWriter();
5868
JsonWriter jsonWriter = new JsonWriter(stringWriter);
5969
try {
60-
jsonWriter
61-
.beginObject()
62-
.name(KEY_VENDORID)
63-
.value(app.getVendorId())
64-
.name(KEY_VENDORNAME)
65-
.value(app.getVendorName())
66-
.name(KEY_PRODUCTID)
67-
.value(app.getProductId())
68-
.name(KEY_VERSION)
69-
.value(app.getVersion())
70-
.name(KEY_ENDPOINTID)
71-
.value(app.getEndpointId())
72-
.endObject();
70+
jsonWriter.beginObject();
71+
jsonWriter.name(KEY_VENDORID);
72+
jsonWriter.value(app.getVendorId());
73+
jsonWriter.name(KEY_VENDORNAME);
74+
jsonWriter.value(app.getVendorName());
75+
jsonWriter.name(KEY_PRODUCTID);
76+
jsonWriter.value(app.getProductId());
77+
jsonWriter.name(KEY_VERSION);
78+
jsonWriter.value(app.getVersion());
79+
jsonWriter.name(KEY_ENDPOINTID);
80+
jsonWriter.value(app.getEndpointId());
81+
jsonWriter.name(KEY_SUPPORTED_CLUSTERS);
82+
serializeSupportedClusters(jsonWriter, app.getSupportedClusters());
83+
jsonWriter.endObject();
7384
jsonWriter.flush();
7485
jsonWriter.close();
7586
} catch (IOException e) {
@@ -88,6 +99,7 @@ private ContentApp deserializeContentApp(String appName, String appMetadata) {
8899
int vendorId = 0;
89100
int productId = 0;
90101
int endpoint = ContentApp.INVALID_ENDPOINTID;
102+
Set<SupportedCluster> supportedClusters = new HashSet<>();
91103
while (jsonReader.hasNext()) {
92104
String name = jsonReader.nextName();
93105
switch (name) {
@@ -106,14 +118,99 @@ private ContentApp deserializeContentApp(String appName, String appMetadata) {
106118
case KEY_ENDPOINTID:
107119
endpoint = jsonReader.nextInt();
108120
break;
121+
case KEY_SUPPORTED_CLUSTERS:
122+
supportedClusters = deserializeSupportedClusters(jsonReader);
123+
break;
109124
}
110125
}
111-
app = new ContentApp(appName, vendorName, vendorId, productId, version);
126+
app = new ContentApp(appName, vendorName, vendorId, productId, version, supportedClusters);
112127
jsonReader.endObject();
113128
jsonReader.close();
114129
} catch (IOException e) {
115130
// Cannot happen
116131
}
117132
return app;
118133
}
134+
135+
private void serializeSupportedClusters(
136+
JsonWriter jsonWriter, Set<SupportedCluster> supportedClusters) throws IOException {
137+
if (supportedClusters != null) {
138+
jsonWriter.beginArray();
139+
for (SupportedCluster supportedCluster : supportedClusters) {
140+
if (supportedCluster != null) {
141+
serializeSupportedCluster(jsonWriter, supportedCluster);
142+
}
143+
}
144+
jsonWriter.endArray();
145+
}
146+
}
147+
148+
private Set<SupportedCluster> deserializeSupportedClusters(JsonReader jsonReader)
149+
throws IOException {
150+
Set<SupportedCluster> supportedClusters = new HashSet<>();
151+
jsonReader.beginArray();
152+
while (jsonReader.hasNext()) {
153+
supportedClusters.add(deserializeSupportedCluster(jsonReader));
154+
}
155+
jsonReader.endArray();
156+
return supportedClusters;
157+
}
158+
159+
private void serializeSupportedCluster(JsonWriter jsonWriter, SupportedCluster supportedCluster)
160+
throws IOException {
161+
jsonWriter.beginObject();
162+
jsonWriter.name(KEY_CLUSTER_IDENTIFIER);
163+
jsonWriter.value(supportedCluster.clusterIdentifier);
164+
jsonWriter.name(KEY_FEATURES);
165+
jsonWriter.value(supportedCluster.features);
166+
jsonWriter.name(KEY_OPTIONAL_COMMAND_IDENTIFIERS);
167+
serializeIntArray(jsonWriter, supportedCluster.optionalCommandIdentifiers);
168+
jsonWriter.name(KEY_OPTIONAL_ATTRIBUTES_IDENTIFIERS);
169+
serializeIntArray(jsonWriter, supportedCluster.optionalAttributesIdentifiers);
170+
jsonWriter.endObject();
171+
}
172+
173+
private SupportedCluster deserializeSupportedCluster(JsonReader jsonReader) throws IOException {
174+
SupportedCluster supportedCluster = new SupportedCluster();
175+
jsonReader.beginObject();
176+
while (jsonReader.hasNext()) {
177+
String name = jsonReader.nextName();
178+
switch (name) {
179+
case KEY_CLUSTER_IDENTIFIER:
180+
supportedCluster.clusterIdentifier = jsonReader.nextInt();
181+
break;
182+
case KEY_FEATURES:
183+
supportedCluster.features = jsonReader.nextInt();
184+
break;
185+
case KEY_OPTIONAL_COMMAND_IDENTIFIERS:
186+
supportedCluster.optionalCommandIdentifiers = deserializeIntArray(jsonReader);
187+
break;
188+
case KEY_OPTIONAL_ATTRIBUTES_IDENTIFIERS:
189+
supportedCluster.optionalAttributesIdentifiers = deserializeIntArray(jsonReader);
190+
break;
191+
}
192+
}
193+
jsonReader.endObject();
194+
return supportedCluster;
195+
}
196+
197+
private void serializeIntArray(JsonWriter jsonWriter, int[] array) throws IOException {
198+
jsonWriter.beginArray();
199+
if (array != null) {
200+
for (int value : array) {
201+
jsonWriter.value(value);
202+
}
203+
}
204+
jsonWriter.endArray();
205+
}
206+
207+
private int[] deserializeIntArray(JsonReader jsonReader) throws IOException {
208+
List<Integer> dynamicArray = new ArrayList<>();
209+
jsonReader.beginArray();
210+
while (jsonReader.hasNext()) {
211+
dynamicArray.add(jsonReader.nextInt());
212+
}
213+
jsonReader.endArray();
214+
return dynamicArray.stream().mapToInt(Integer::intValue).toArray();
215+
}
119216
}

0 commit comments

Comments
 (0)