Skip to content

Commit

Permalink
resolve comments from #11650 (#11751)
Browse files Browse the repository at this point in the history
* resolve comments

* Restyled by google-java-format

* Restyled by gn

* update zap generated code

* fix comments

* merge with master

* fix unwanted format changes

* modify clusterinfo mapping to resolve comment

* final fix for comments

* Delete misc.xml

Co-authored-by: Restyled.io <commits@restyled.io>
  • Loading branch information
2 people authored and pull[bot] committed Jan 4, 2023
1 parent eb31427 commit d74e7b9
Show file tree
Hide file tree
Showing 9 changed files with 3,496 additions and 3,309 deletions.
12 changes: 12 additions & 0 deletions src/android/CHIPTool/.idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import androidx.core.view.forEach
import androidx.fragment.app.Fragment
import chip.clusterinfo.ClusterCommandCallback
import chip.clusterinfo.ClusterInfo
import chip.clusterinfo.CommandInfo
import chip.clusterinfo.InteractionInfo
import chip.clusterinfo.CommandResponseInfo
import chip.clusterinfo.DelegatedClusterCallback
import chip.devicecontroller.ChipClusters
Expand Down Expand Up @@ -53,7 +53,7 @@ class ClusterDetailFragment : Fragment() {
private lateinit var selectedClusterInfo: ClusterInfo
private lateinit var selectedCluster: ChipClusters.BaseChipCluster
private lateinit var selectedCommandCallback: DelegatedClusterCallback
private lateinit var selectedCommandInfo: CommandInfo
private lateinit var selectedInteractionInfo: InteractionInfo
private var devicePtr = 0L
private var endpointId = 0

Expand All @@ -80,12 +80,12 @@ class ClusterDetailFragment : Fragment() {
val commandArguments = HashMap<String, Any>()
parameterList.forEach {
val type =
selectedCommandInfo.commandParameters[it.clusterParameterNameTv.text.toString()]!!.type
selectedInteractionInfo.commandParameters[it.clusterParameterNameTv.text.toString()]!!.type
val data = castStringToType(it.clusterParameterData.text.toString(), type)!!

commandArguments[it.clusterParameterNameTv.text.toString()] = data
}
selectedCommandInfo.getCommandFunction()
selectedInteractionInfo.getCommandFunction()
.invokeCommand(selectedCluster, selectedCommandCallback, commandArguments)
}
}
Expand All @@ -94,7 +94,6 @@ class ClusterDetailFragment : Fragment() {
private fun castStringToType(data: String, type: Class<*>): Any? {
return when (type) {
Int::class.java -> data.toInt()
String::class.java -> data
Boolean::class.java -> data.toBoolean()
else -> data
}
Expand Down Expand Up @@ -141,8 +140,8 @@ class ClusterDetailFragment : Fragment() {
callbackList.removeAllViews()
selectedCluster = selectedClusterInfo.createClusterFunction.create(devicePtr, endpointId)
val selectedCommand: String = commandAutoComplete.adapter.getItem(position).toString()
selectedCommandInfo = selectedClusterInfo.commands[selectedCommand]!!
selectedCommandCallback = selectedCommandInfo.commandCallbackSupplier.get()
selectedInteractionInfo = selectedClusterInfo.commands[selectedCommand]!!
selectedCommandCallback = selectedInteractionInfo.commandCallbackSupplier.get()
populateCommandParameter(inflater, parameterList)
selectedCommandCallback.setCallbackDelegate(object : ClusterCommandCallback {
override fun onSuccess(responseValues: Map<CommandResponseInfo, Any>) {
Expand All @@ -167,7 +166,7 @@ class ClusterDetailFragment : Fragment() {
}

private fun populateCommandParameter(inflater: LayoutInflater, parameterList: LinearLayout) {
selectedCommandInfo.commandParameters.forEach { (paramName, paramInfo) ->
selectedInteractionInfo.commandParameters.forEach { (paramName, paramInfo) ->
val param = inflater.inflate(R.layout.cluster_parameter_item, null, false) as ConstraintLayout
param.clusterParameterNameTv.text = "${paramName}"
param.clusterParameterTypeTv.text = "${paramInfo.type}"
Expand All @@ -182,37 +181,55 @@ class ClusterDetailFragment : Fragment() {
) {
responseValues.forEach { (variableNameType, response) ->
if (response is List<*>) {
if (response.size == 0) {
val emptyCallback =
inflater.inflate(R.layout.cluster_callback_item, null, false) as ConstraintLayout
emptyCallback.clusterCallbackNameTv.text = "Result is empty"
callbackList.addView(emptyCallback)
} else {
response.forEachIndexed { index, it ->
val objectCallback =
inflater.inflate(R.layout.cluster_callback_item, null, false) as ConstraintLayout
objectCallback.clusterCallbackNameTv.text = variableNameType.name + "[$index]"
val objectDeserializationString = it.toString()
val callbackClassName = it!!.javaClass.toString().split('$').last()
objectCallback.clusterCallbackDataTv.text = callbackClassName
objectCallback.clusterCallbackDataTv.setOnClickListener {
AlertDialog.Builder(requireContext())
.setTitle(callbackClassName)
.setMessage(objectDeserializationString)
.create()
.show()
}
objectCallback.clusterCallbackTypeTv.text = "List<$callbackClassName>"
callbackList.addView(objectCallback)
}
}
createListReadAttributeView(response, inflater, callbackList, variableNameType)
} else {
val callback =
createBasicReadAttributeView(response, inflater, callbackList, variableNameType)
}
}
}

private fun createBasicReadAttributeView(
response: Any,
inflater: LayoutInflater,
callbackList: LinearLayout,
variableNameType: CommandResponseInfo
) {
val callbackItem =
inflater.inflate(R.layout.cluster_callback_item, null, false) as ConstraintLayout
callbackItem.clusterCallbackNameTv.text = variableNameType.name
callbackItem.clusterCallbackDataTv.text = response.toString()
callbackItem.clusterCallbackTypeTv.text = variableNameType.type
callbackList.addView(callbackItem)
}

private fun createListReadAttributeView(
response: List<*>,
inflater: LayoutInflater,
callbackList: LinearLayout,
variableNameType: CommandResponseInfo
) {
if (response.isEmpty()) {
val emptyCallback =
inflater.inflate(R.layout.cluster_callback_item, null, false) as ConstraintLayout
emptyCallback.clusterCallbackNameTv.text = "Result is empty"
callbackList.addView(emptyCallback)
} else {
response.forEachIndexed { index, it ->
val readAttributeCallbackItem =
inflater.inflate(R.layout.cluster_callback_item, null, false) as ConstraintLayout
callback.clusterCallbackNameTv.text = variableNameType.name
callback.clusterCallbackDataTv.text = response.toString()
callback.clusterCallbackTypeTv.text = variableNameType.type
callbackList.addView(callback)
readAttributeCallbackItem.clusterCallbackNameTv.text = variableNameType.name + "[$index]"
val objectString = it.toString()
val callbackClassName = it!!.javaClass.toString().split('$').last()
readAttributeCallbackItem.clusterCallbackDataTv.text = callbackClassName
readAttributeCallbackItem.clusterCallbackDataTv.setOnClickListener {
AlertDialog.Builder(requireContext())
.setTitle(callbackClassName)
.setMessage(objectString)
.create()
.show()
}
readAttributeCallbackItem.clusterCallbackTypeTv.text = "List<$callbackClassName>"
callbackList.addView(readAttributeCallbackItem)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/controller/java/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ android_library("java") {
sources = [
"src/chip/clusterinfo/ClusterCommandCallback.java",
"src/chip/clusterinfo/ClusterInfo.java",
"src/chip/clusterinfo/CommandInfo.java",
"src/chip/clusterinfo/CommandParameterInfo.java",
"src/chip/clusterinfo/CommandResponseInfo.java",
"src/chip/clusterinfo/DelegatedClusterCallback.java",
"src/chip/clusterinfo/InteractionInfo.java",
"src/chip/devicecontroller/ChipClusterException.java",
"src/chip/devicecontroller/ChipCommandType.java",
"src/chip/devicecontroller/ChipDeviceController.java",
Expand Down
9 changes: 5 additions & 4 deletions src/controller/java/src/chip/clusterinfo/ClusterInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
/** ClusterInfo maps commands and provides a constructor function for a cluster. */
public class ClusterInfo {
private final ClusterConstructor createClusterFunction;
private final Map<String, CommandInfo> commands;
private final Map<String, InteractionInfo> commands;

public ClusterInfo(ClusterConstructor createClusterFunction, Map<String, CommandInfo> commands) {
public ClusterInfo(
ClusterConstructor createClusterFunction, Map<String, InteractionInfo> commands) {
this.createClusterFunction = createClusterFunction;
this.commands = commands;
}
Expand All @@ -17,11 +18,11 @@ public ClusterConstructor getCreateClusterFunction() {
return createClusterFunction;
}

public Map<String, CommandInfo> getCommands() {
public Map<String, InteractionInfo> getCommands() {
return commands;
}

public void combineCommands(Map<String, CommandInfo> newCommands) {
public void combineCommands(Map<String, InteractionInfo> newCommands) {
this.commands.putAll(newCommands);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
import java.util.function.Supplier;

/**
* CommandInfo has a functional interface to invoke arbitrary commands based on cluster, callback
* and a map of arguments, a Supplier that provides {@link DelegatedClusterCallback}, and maps the
* parameter and commandParametersInfo.
* InteractionInfo has a functional interface to invoke arbitrary interaction based on cluster,
* callback and a map of arguments, a Supplier that provides {@link DelegatedClusterCallback}, and
* maps the parameter and commandParametersInfo.
*/
public class CommandInfo {
public class InteractionInfo {
public ClusterCommandFunction commandFunction;
private Supplier<DelegatedClusterCallback> commandCallbackSupplier;
private Map<String, CommandParameterInfo> commandParameters;

public CommandInfo(
public InteractionInfo(
ClusterCommandFunction commandFunction,
Supplier<DelegatedClusterCallback> commandCallbackSupplier,
Map<String, CommandParameterInfo> commandParameters) {
Expand Down
16 changes: 4 additions & 12 deletions src/controller/java/templates/ChipClusters-java.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package chip.devicecontroller;

import androidx.annotation.Nullable;
import java.util.List;
import java.util.Arrays;
import java.util.Optional;

public class ChipClusters {
Expand Down Expand Up @@ -179,18 +180,9 @@ public class ChipClusters {
{{else if isStruct}}
{{! TODO: Add support for structs here }}
{{else if (isOctetString type)}}
output.append("byte[] {{asLowerCamelCase name}}: [");
for (int i = 0; i < {{asLowerCamelCase name}}.length; i++) {
if (i != {{asLowerCamelCase name}}.length - 1) {
output.append({{asLowerCamelCase name}}[i]);
output.append(", ");
}
else {
output.append({{asLowerCamelCase name}}[i]);
output.append("]");
output.append("\n");
}
}
output.append("byte[] ");
output.append(Arrays.toString({{asLowerCamelCase name}}));
output.append("\n");
{{else if (isCharString type)}}
output.append("String {{asLowerCamelCase name}}: ");
output.append(this.{{asLowerCamelCase name}});
Expand Down
56 changes: 36 additions & 20 deletions src/controller/java/templates/ClusterInfo-java.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import java.util.Map;
import java.util.List;
import java.util.HashMap;
import chip.clusterinfo.ClusterInfo;
import chip.clusterinfo.CommandInfo;
import chip.clusterinfo.InteractionInfo;
import chip.clusterinfo.CommandParameterInfo;
import chip.clusterinfo.DelegatedClusterCallback;
import chip.clusterinfo.ClusterCommandCallback;
Expand Down Expand Up @@ -248,15 +248,34 @@ public class ClusterInfoMapping {
{{/chip_client_clusters}}

public Map<String, ClusterInfo> getClusterMap() {
Map<String, ClusterInfo> clusterMap = initializeClusterMap();
Map<String, Map<String, InteractionInfo>> commandMap = getCommandMap();
combineCommand(clusterMap, commandMap);
Map<String, Map<String, InteractionInfo>> readAttributeMap = getReadAttributeMap();
combineCommand(clusterMap, readAttributeMap);
return clusterMap;
}

public Map<String, ClusterInfo> initializeClusterMap() {
Map<String, ClusterInfo> clusterMap = new HashMap<>();
getCommandMap(clusterMap);
getReadAttributeMap(clusterMap);
{{#chip_client_clusters}}
ClusterInfo {{asLowerCamelCase name}}ClusterInfo = new ClusterInfo(
(ptr, endpointId) -> new ChipClusters.{{asUpperCamelCase name}}Cluster(ptr, endpointId), new HashMap<>());
clusterMap.put("{{asLowerCamelCase name}}", {{asLowerCamelCase name}}ClusterInfo);
{{/chip_client_clusters}}
return clusterMap;
}

public Map<String, ClusterInfo> getCommandMap(Map<String, ClusterInfo> clusterMap) {
public void combineCommand(Map<String, ClusterInfo> destination, Map<String, Map<String, InteractionInfo>> source) {
{{#chip_client_clusters}}
destination.get("{{asLowerCamelCase name}}").combineCommands(source.get("{{asLowerCamelCase name}}"));
{{/chip_client_clusters}}
}

public Map<String, Map<String, InteractionInfo>> getCommandMap() {
Map<String, Map<String, InteractionInfo>> commandMap = new HashMap<>();
{{#chip_client_clusters}}
Map<String, CommandInfo> {{asLowerCamelCase name}}ClusterCommandInfoMap = new LinkedHashMap<>();
Map<String, InteractionInfo> {{asLowerCamelCase name}}ClusterInteractionInfoMap = new LinkedHashMap<>();
{{#chip_cluster_commands}}
Map<String, CommandParameterInfo> {{asLowerCamelCase ../name}}{{asLowerCamelCase name}}CommandParams = new LinkedHashMap<String, CommandParameterInfo>();
{{! TODO: fill out parameter types }}
Expand All @@ -270,7 +289,7 @@ public class ClusterInfoMapping {
{{/if}}
// Populate commands
{{#if hasSpecificResponse}}
CommandInfo {{asLowerCamelCase ../name}}{{asLowerCamelCase name}}CommandInfo = new CommandInfo(
InteractionInfo {{asLowerCamelCase ../name}}{{asLowerCamelCase name}}InteractionInfo = new InteractionInfo(
(cluster, callback, commandArguments) -> {
((ChipClusters.{{asUpperCamelCase ../name}}Cluster) cluster)
.{{asLowerCamelCase name}}((ChipClusters.{{asUpperCamelCase ../name}}Cluster.{{asUpperCamelCase responseName}}Callback) callback
Expand All @@ -284,7 +303,7 @@ public class ClusterInfoMapping {
{{asLowerCamelCase ../name}}{{asLowerCamelCase name}}CommandParams
);
{{else}}
CommandInfo {{asLowerCamelCase ../name}}{{asLowerCamelCase name}}CommandInfo = new CommandInfo(
InteractionInfo {{asLowerCamelCase ../name}}{{asLowerCamelCase name}}InteractionInfo = new InteractionInfo(
(cluster, callback, commandArguments) -> {
((ChipClusters.{{asUpperCamelCase ../name}}Cluster) cluster)
.{{asLowerCamelCase name}}((DefaultClusterCallback) callback
Expand All @@ -298,23 +317,21 @@ public class ClusterInfoMapping {
{{asLowerCamelCase ../name}}{{asLowerCamelCase name}}CommandParams
);
{{/if}}
{{asLowerCamelCase ../name}}ClusterCommandInfoMap.put("{{asLowerCamelCase name}}", {{asLowerCamelCase ../name}}{{asLowerCamelCase name}}CommandInfo);
{{asLowerCamelCase ../name}}ClusterInteractionInfoMap.put("{{asLowerCamelCase name}}", {{asLowerCamelCase ../name}}{{asLowerCamelCase name}}InteractionInfo);
{{/chip_cluster_commands}}
// Populate cluster
ClusterInfo {{asLowerCamelCase name}}ClusterInfo = new ClusterInfo(
(ptr, endpointId) -> new ChipClusters.{{asUpperCamelCase name}}Cluster(ptr, endpointId), {{asLowerCamelCase name}}ClusterCommandInfoMap);
clusterMap.put("{{asLowerCamelCase name}}", {{asLowerCamelCase name}}ClusterInfo);
commandMap.put("{{asLowerCamelCase name}}", {{asLowerCamelCase name}}ClusterInteractionInfoMap);
{{/chip_client_clusters}}
return clusterMap;
return commandMap;
}

public Map<String, ClusterInfo> getReadAttributeMap(Map<String, ClusterInfo> clusterMap) {
public Map<String, Map<String, InteractionInfo>> getReadAttributeMap() {
Map<String, Map<String, InteractionInfo>> readAttributeMap = new HashMap<>();
{{#chip_client_clusters}}
Map<String, CommandInfo> read{{asUpperCamelCase name}}CommandInfo = new LinkedHashMap<>();
Map<String, InteractionInfo> read{{asUpperCamelCase name}}InteractionInfo = new LinkedHashMap<>();
// read attribute
{{#chip_server_cluster_attributes}}
Map<String, CommandParameterInfo> read{{asUpperCamelCase ../name}}{{asUpperCamelCase name}}CommandParams = new LinkedHashMap<String, CommandParameterInfo>();
CommandInfo read{{asUpperCamelCase ../name}}{{asUpperCamelCase name}}AttributeCommandInfo = new CommandInfo(
InteractionInfo read{{asUpperCamelCase ../name}}{{asUpperCamelCase name}}AttributeInteractionInfo = new InteractionInfo(
(cluster, callback, commandArguments) -> {
((ChipClusters.{{asUpperCamelCase ../name}}Cluster) cluster).read{{asUpperCamelCase name}}Attribute(
{{#if isList}}
Expand All @@ -331,12 +348,11 @@ public class ClusterInfoMapping {
{{/if}}
read{{asUpperCamelCase ../name}}{{asUpperCamelCase name}}CommandParams
);
read{{asUpperCamelCase ../name}}CommandInfo.put("read{{asUpperCamelCase name}}Attribute", read{{asUpperCamelCase ../name}}{{asUpperCamelCase name}}AttributeCommandInfo);
read{{asUpperCamelCase ../name}}InteractionInfo.put("read{{asUpperCamelCase name}}Attribute", read{{asUpperCamelCase ../name}}{{asUpperCamelCase name}}AttributeInteractionInfo);
{{/chip_server_cluster_attributes}}
// combine the read Attribute into the original commands
clusterMap.get("{{asLowerCamelCase name}}").combineCommands(read{{asUpperCamelCase name}}CommandInfo);
readAttributeMap.put("{{asLowerCamelCase name}}", read{{asUpperCamelCase name}}InteractionInfo);
{{/chip_client_clusters}}
return clusterMap;
return readAttributeMap;
}
}

Expand Down
Loading

0 comments on commit d74e7b9

Please sign in to comment.