Skip to content

Commit

Permalink
Generate read attribute (#11650)
Browse files Browse the repository at this point in the history
* try to find a state where m5stack doesn't bootloop

* add back class description

* select different cluster, command will remove previous displayed parameter

* add responseValueInfo class instead of string split

* remove unused variable

* Restyled by whitespace

* Restyled by google-java-format

* Restyled by gn

* fix parameter response ui alignment issue

* resolve comments

* fix format

* regenerate clusterInfoMapping.java

* resolve comments

* merge with master to get pass the check

* finish read attribute code generation, need to handle displaying objects on the ui next

* able to display complex data on the ui

* skip merge with master

* need outer loop variable to store the deserialized string representation of the object

* Update ClusterInteractionFragment.kt

Co-authored-by: Restyled.io <commits@restyled.io>
  • Loading branch information
2 people authored and pull[bot] committed Dec 13, 2022
1 parent 2b1b40a commit 2566232
Show file tree
Hide file tree
Showing 10 changed files with 12,877 additions and 5,426 deletions.
12 changes: 0 additions & 12 deletions src/android/CHIPTool/.idea/runConfigurations.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import android.widget.LinearLayout
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.forEach
import androidx.fragment.app.Fragment
Expand Down Expand Up @@ -67,9 +68,15 @@ class ClusterDetailFragment : Fragment() {
return inflater.inflate(R.layout.cluster_detail_fragment, container, false).apply {
deviceController.setCompletionListener(GenericChipDeviceListener())
commandAutoCompleteTv.visibility = View.GONE
clusterAutoCompleteSetup(clusterAutoCompleteTv, commandAutoCompleteTv, parameterList)
clusterAutoCompleteSetup(
clusterAutoCompleteTv,
commandAutoCompleteTv,
parameterList,
callbackList
)
commandAutoCompleteSetup(commandAutoCompleteTv, inflater, parameterList, callbackList)
invokeCommand.setOnClickListener {
callbackList.removeAllViews()
val commandArguments = HashMap<String, Any>()
parameterList.forEach {
val type =
Expand All @@ -89,7 +96,7 @@ class ClusterDetailFragment : Fragment() {
Int::class.java -> data.toInt()
String::class.java -> data
Boolean::class.java -> data.toBoolean()
else -> null
else -> data
}
}

Expand All @@ -102,7 +109,8 @@ class ClusterDetailFragment : Fragment() {
private fun clusterAutoCompleteSetup(
clusterAutoComplete: AutoCompleteTextView,
commandAutoComplete: AutoCompleteTextView,
parameterList: LinearLayout
parameterList: LinearLayout,
callbackList: LinearLayout
) {
val clusterNameList = constructHint(clusterMap)
val clusterAdapter =
Expand All @@ -113,6 +121,7 @@ class ClusterDetailFragment : Fragment() {
// when new cluster is selected, clear the command text and possible parameterList
commandAutoComplete.setText("", false)
parameterList.removeAllViews()
callbackList.removeAllViews()
// populate all the commands that belong to the selected cluster
val selectedCluster: String = clusterAutoComplete.adapter.getItem(position).toString()
val commandAdapter = getCommandOptions(selectedCluster)
Expand All @@ -127,8 +136,9 @@ class ClusterDetailFragment : Fragment() {
callbackList: LinearLayout
) {
commandAutoComplete.setOnItemClickListener { parent, view, position, id ->
// when new command is selected, clear all the parameterList
// when new command is selected, clear all the parameterList and callbackList
parameterList.removeAllViews()
callbackList.removeAllViews()
selectedCluster = selectedClusterInfo.createClusterFunction.create(devicePtr, endpointId)
val selectedCommand: String = commandAutoComplete.adapter.getItem(position).toString()
selectedCommandInfo = selectedClusterInfo.commands[selectedCommand]!!
Expand Down Expand Up @@ -171,12 +181,39 @@ class ClusterDetailFragment : Fragment() {
callbackList: LinearLayout
) {
responseValues.forEach { (variableNameType, response) ->
val callback =
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)
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)
}
}
} else {
val callback =
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)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import com.google.chip.chiptool.ChipClient
import com.google.chip.chiptool.GenericChipDeviceListener
import com.google.chip.chiptool.R
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import com.google.chip.chiptool.clusterclient.AddressUpdateFragment
import kotlinx.android.synthetic.main.cluster_interaction_fragment.view.endpointList
Expand Down Expand Up @@ -94,4 +92,4 @@ class ClusterInteractionFragment : Fragment() {
showFragment(ClusterDetailFragment.newInstance(devicePtr, position))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@

<TextView
android:id="@+id/clusterCallbackNameTv"
android:layout_width="80dp"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:padding="16dp"
android:minWidth="48dp"
android:padding="8dp"
android:minWidth="90dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/clusterCallbackDataTv"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />


<TextView
android:id="@+id/clusterCallbackDataTv"
android:layout_width="0dp"
Expand All @@ -26,8 +27,6 @@
app:layout_constraintEnd_toStartOf="@id/clusterCallbackTypeTv"
app:layout_constraintStart_toEndOf="@id/clusterCallbackNameTv"
app:layout_constraintTop_toTopOf="parent" />


<TextView
android:id="@+id/clusterCallbackTypeTv"
android:textStyle="bold"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@
android:layout_marginBottom="8dp"
android:layout_below="@id/commandAutoCompleteTv"
android:orientation="vertical" />

<LinearLayout
android:id="@+id/callbackList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_below="@+id/invokeCommand"
android:orientation="vertical" />

<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/callbackList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_below="@+id/invokeCommand"
android:orientation="vertical" />
</ScrollView>
</LinearLayout>
4 changes: 4 additions & 0 deletions src/controller/java/src/chip/clusterinfo/ClusterInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public Map<String, CommandInfo> getCommands() {
return commands;
}

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

/**
* The functional interface provides a uniform way to create cluster through create function. In
* ClusterInfoMapping, each ClusterConstructor was generated using the intended function. Using
Expand Down
39 changes: 39 additions & 0 deletions src/controller/java/templates/ChipClusters-java.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,45 @@ public class ChipClusters {
{{/if}}
{{/chip_attribute_list_entryTypes}}
}

@Override
public String toString() {
StringBuilder output = new StringBuilder("");
{{#chip_attribute_list_entryTypes}}
{{#if isOptional}}
{{! TODO: Add support for optional types here }}
{{else if isNullable}}
{{! TODO: Add support for nullable types here }}
{{else if isArray}}
{{! TODO: Add support for lists here }}
{{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");
}
}
{{else if (isCharString type)}}
output.append("String {{asLowerCamelCase name}}: ");
output.append(this.{{asLowerCamelCase name}});
output.append("\n");
{{else}}
output.append("{{asJavaBasicType label type}} {{asLowerCamelCase name}}: ");
output.append(this.{{asLowerCamelCase name}});
output.append("\n");
{{/if}}

{{/chip_attribute_list_entryTypes}}
return output.toString();
}
}
{{/if}}

Expand Down
Loading

0 comments on commit 2566232

Please sign in to comment.