Skip to content

Commit

Permalink
[Android] Add utility for translating cluster/attribute ids to names (#…
Browse files Browse the repository at this point in the history
…15749)

* Add utility for translating cluster/attribute ids to names

* Restyled by whitespace

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Sep 13, 2023
1 parent da8fe89 commit 3684696
Show file tree
Hide file tree
Showing 6 changed files with 2,762 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import chip.devicecontroller.ChipDeviceController
import chip.devicecontroller.ChipIdLookup
import com.google.chip.chiptool.ChipClient
import com.google.chip.chiptool.R
import com.google.chip.chiptool.util.DeviceIdUtil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import android.widget.EditText
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import chip.devicecontroller.ChipDeviceController
import chip.devicecontroller.ChipIdLookup
import chip.devicecontroller.ReportCallback
import chip.devicecontroller.SubscriptionEstablishedCallback
import chip.devicecontroller.model.ChipAttributePath
import chip.devicecontroller.model.ChipPathId
import chip.devicecontroller.model.NodeState
import com.google.chip.chiptool.ChipClient
import com.google.chip.chiptool.R
import java.lang.StringBuilder
import kotlinx.android.synthetic.main.wildcard_fragment.attributeIdEd
import kotlinx.android.synthetic.main.wildcard_fragment.clusterIdEd
import kotlinx.android.synthetic.main.wildcard_fragment.endpointIdEd
Expand All @@ -40,10 +42,12 @@ class WildcardFragment : Fragment() {
Log.e(TAG, "Report error for $attributePath: $ex")
}

override fun onReport(nodeData: NodeState) {
override fun onReport(nodeState: NodeState) {
Log.i(TAG, "Received wildcard report")
Log.i(TAG, nodeData.toString())
requireActivity().runOnUiThread { outputTv.text = nodeData.toString() }

val debugString = nodeStateToDebugString(nodeState)
Log.i(TAG, debugString)
requireActivity().runOnUiThread { outputTv.text = debugString }
}
}

Expand All @@ -62,6 +66,23 @@ class WildcardFragment : Fragment() {
}
}

private fun nodeStateToDebugString(nodeState: NodeState): String {
val stringBuilder = StringBuilder()
nodeState.endpointStates.forEach { (endpointId, endpointState) ->
stringBuilder.append("Endpoint $endpointId: {\n")
endpointState.clusterStates.forEach { (clusterId, clusterState) ->
stringBuilder.append("\t${ChipIdLookup.clusterIdToName(clusterId)}Cluster: {\n")
clusterState.attributeStates.forEach { (attributeId, attributeState) ->
val attributeName = ChipIdLookup.attributeIdToName(clusterId, attributeId)
stringBuilder.append("\t\t$attributeName: ${attributeState.value}\n")
}
stringBuilder.append("\t}\n")
}
stringBuilder.append("}\n")
}
return stringBuilder.toString()
}

private suspend fun subscribe(minInterval: Int, maxInterval: Int) {
val subscriptionEstablishedCallback =
SubscriptionEstablishedCallback { Log.i(TAG, "Subscription to device established") }
Expand Down
1 change: 1 addition & 0 deletions src/controller/java/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ android_library("java") {
"src/chip/devicecontroller/model/EndpointState.java",
"src/chip/devicecontroller/model/NodeState.java",
"zap-generated/chip/devicecontroller/ChipClusters.java",
"zap-generated/chip/devicecontroller/ChipIdLookup.java",
"zap-generated/chip/devicecontroller/ChipStructs.java",
"zap-generated/chip/devicecontroller/ClusterInfoMapping.java",
"zap-generated/chip/devicecontroller/ClusterReadMapping.java",
Expand Down
38 changes: 38 additions & 0 deletions src/controller/java/templates/ChipIdLookup-java.zapt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{{> header}}
{{#if (chip_has_client_clusters)}}

package chip.devicecontroller;

public final class ChipIdLookup {
/**
* Translates cluster ID to a cluster name in upper camel case. If no matching
* ID is found, returns an empty string.
*/
public static String clusterIdToName(long clusterId) {
{{#chip_client_clusters}}
if (clusterId == {{code}}L) {
return "{{asUpperCamelCase name}}";
}
{{/chip_client_clusters}}
return "";
}

/**
* Translates cluster ID and attribute ID to an attribute name in upper camel case.
* If no matching IDs are found, returns an empty string.
*/
public static String attributeIdToName(long clusterId, long attributeId) {
{{#chip_client_clusters}}
if (clusterId == {{code}}L) {
{{#chip_server_cluster_attributes}}
if (attributeId == {{code}}L) {
return "{{asUpperCamelCase name}}";
}
{{/chip_server_cluster_attributes}}
return "";
}
{{/chip_client_clusters}}
return "";
}
}
{{/if}}
5 changes: 5 additions & 0 deletions src/controller/java/templates/templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@
"name": "CHIP ZCL API for Java",
"output": "src/controller/java/zap-generated/chip/devicecontroller/ChipClusters.java"
},
{
"path": "ChipIdLookup-java.zapt",
"name": "Generate utilities for looking up cluster/attribute IDs",
"output": "src/controller/java/zap-generated/chip/devicecontroller/ChipIdLookup.java"
},
{
"path": "ClusterInfo-java.zapt",
"name": "Cluster information mapping for Java",
Expand Down
Loading

0 comments on commit 3684696

Please sign in to comment.