Skip to content

Commit

Permalink
[Android] Implement Event path command (#20198)
Browse files Browse the repository at this point in the history
* Implement android event path command

* Restyled by whitespace

* Restyled by google-java-format

* Restyled by clang-format

* Update zap generate

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Aug 6, 2022
1 parent 4653d06 commit 4365604
Show file tree
Hide file tree
Showing 30 changed files with 6,793 additions and 590 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 @@ -13,8 +13,11 @@ import androidx.lifecycle.lifecycleScope
import chip.devicecontroller.ChipDeviceController
import chip.devicecontroller.ChipIdLookup
import chip.devicecontroller.ReportCallback
import chip.devicecontroller.ReportEventCallback
import chip.devicecontroller.ResubscriptionAttemptCallback
import chip.devicecontroller.SubscriptionEstablishedCallback
import chip.devicecontroller.model.ChipAttributePath
import chip.devicecontroller.model.ChipEventPath
import chip.devicecontroller.model.ChipPathId
import chip.devicecontroller.model.NodeState
import com.google.chip.chiptool.ChipClient
Expand All @@ -23,9 +26,12 @@ 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
import kotlinx.android.synthetic.main.wildcard_fragment.eventIdEd
import kotlinx.android.synthetic.main.wildcard_fragment.outputTv
import kotlinx.android.synthetic.main.wildcard_fragment.view.readBtn
import kotlinx.android.synthetic.main.wildcard_fragment.view.readEventBtn
import kotlinx.android.synthetic.main.wildcard_fragment.view.subscribeBtn
import kotlinx.android.synthetic.main.wildcard_fragment.view.subscribeEventBtn
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

Expand All @@ -49,6 +55,28 @@ class WildcardFragment : Fragment() {
Log.i(TAG, debugString)
requireActivity().runOnUiThread { outputTv.text = debugString }
}

override fun onDone() {
Log.i(TAG, "wildcard report Done")
}
}

private val reportEventCallback = object : ReportEventCallback {
override fun onError(eventPath: ChipEventPath, ex: Exception) {
Log.e(TAG, "Report error for $eventPath: $ex")
}

override fun onReport(nodeState: NodeState) {
Log.i(TAG, "Received wildcard report")

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

override fun onDone() {
Log.i(TAG, "wildcard report Done")
}
}

override fun onCreateView(
Expand All @@ -58,8 +86,10 @@ class WildcardFragment : Fragment() {
): View {
scope = viewLifecycleOwner.lifecycleScope
return inflater.inflate(R.layout.wildcard_fragment, container, false).apply {
subscribeBtn.setOnClickListener { scope.launch { showSubscribeDialog() } }
readBtn.setOnClickListener { scope.launch { read() } }
subscribeBtn.setOnClickListener { scope.launch { showSubscribeDialog(ATTRIBUTE) } }
readBtn.setOnClickListener { scope.launch { read(ATTRIBUTE) } }
subscribeEventBtn.setOnClickListener { scope.launch { showSubscribeDialog(EVENT) } }
readEventBtn.setOnClickListener { scope.launch { read(EVENT) } }

addressUpdateFragment =
childFragmentManager.findFragmentById(R.id.addressUpdateFragment) as AddressUpdateFragment
Expand All @@ -76,44 +106,74 @@ class WildcardFragment : Fragment() {
val attributeName = ChipIdLookup.attributeIdToName(clusterId, attributeId)
stringBuilder.append("\t\t$attributeName: ${attributeState.value}\n")
}
clusterState.eventStates.forEach { (eventId, eventState) ->
val eventName = ChipIdLookup.eventIdToName(clusterId, eventId)
stringBuilder.append("\t\t$eventName: ${eventState.value}\n")
}
stringBuilder.append("\t}\n")
}
stringBuilder.append("}\n")
}
return stringBuilder.toString()
}

private suspend fun subscribe(minInterval: Int, maxInterval: Int) {
private suspend fun subscribe(type: Int, minInterval: Int, maxInterval: Int) {
val subscriptionEstablishedCallback =
SubscriptionEstablishedCallback { Log.i(TAG, "Subscription to device established") }

val resubscriptionAttemptCallback =
ResubscriptionAttemptCallback { terminationCause, nextResubscribeIntervalMsec
-> Log.i(TAG, "ResubscriptionAttempt terminationCause:$terminationCause, nextResubscribeIntervalMsec:$nextResubscribeIntervalMsec") }

val endpointId = getChipPathIdForText(endpointIdEd.text.toString())
val clusterId = getChipPathIdForText(clusterIdEd.text.toString())
val attributeId = getChipPathIdForText(attributeIdEd.text.toString())
val attributePath = ChipAttributePath.newInstance(endpointId, clusterId, attributeId)

deviceController.subscribeToPath(subscriptionEstablishedCallback,
reportCallback,
ChipClient.getConnectedDevicePointer(requireContext(),
addressUpdateFragment.deviceId),
listOf(attributePath),
minInterval,
maxInterval)
val eventId = getChipPathIdForText(eventIdEd.text.toString())

if (type == ATTRIBUTE) {
val attributePath = ChipAttributePath.newInstance(endpointId, clusterId, attributeId)
deviceController.subscribeToPath(subscriptionEstablishedCallback,
reportCallback,
ChipClient.getConnectedDevicePointer(requireContext(),
addressUpdateFragment.deviceId),
listOf(attributePath),
minInterval,
maxInterval)
} else if (type == EVENT) {
val eventPath = ChipEventPath.newInstance(endpointId, clusterId, eventId)
deviceController.subscribeToEventPath(subscriptionEstablishedCallback,
resubscriptionAttemptCallback,
reportEventCallback,
ChipClient.getConnectedDevicePointer(requireContext(),
addressUpdateFragment.deviceId),
listOf(eventPath),
minInterval,
maxInterval)
}
}

private suspend fun read() {
private suspend fun read(type: Int) {
val endpointId = getChipPathIdForText(endpointIdEd.text.toString())
val clusterId = getChipPathIdForText(clusterIdEd.text.toString())
val attributeId = getChipPathIdForText(attributeIdEd.text.toString())
val attributePath = ChipAttributePath.newInstance(endpointId, clusterId, attributeId)

deviceController.readPath(reportCallback,
ChipClient.getConnectedDevicePointer(requireContext(),
addressUpdateFragment.deviceId),
listOf(attributePath))
val eventId = getChipPathIdForText(eventIdEd.text.toString())

if (type == ATTRIBUTE) {
val attributePath = ChipAttributePath.newInstance(endpointId, clusterId, attributeId)
deviceController.readPath(reportCallback,
ChipClient.getConnectedDevicePointer(requireContext(),
addressUpdateFragment.deviceId),
listOf(attributePath))
} else if (type == EVENT) {
val eventPath = ChipEventPath.newInstance(endpointId, clusterId, eventId)
deviceController.readEventPath(reportEventCallback,
ChipClient.getConnectedDevicePointer(requireContext(),
addressUpdateFragment.deviceId),
listOf(eventPath))
}
}

private fun showSubscribeDialog() {
private fun showSubscribeDialog(type: Int) {
val dialogView = requireActivity().layoutInflater.inflate(R.layout.subscribe_dialog, null)
val dialog = AlertDialog.Builder(requireContext()).apply {
setView(dialogView)
Expand All @@ -123,7 +183,7 @@ class WildcardFragment : Fragment() {
val maxIntervalEd = dialogView.findViewById<EditText>(R.id.maxIntervalEd)
dialogView.findViewById<Button>(R.id.subscribeBtn).setOnClickListener {
scope.launch {
subscribe(minIntervalEd.text.toString().toInt(), maxIntervalEd.text.toString().toInt())
subscribe(type, minIntervalEd.text.toString().toInt(), maxIntervalEd.text.toString().toInt())
requireActivity().runOnUiThread { dialog.dismiss() }
}
}
Expand All @@ -136,7 +196,8 @@ class WildcardFragment : Fragment() {

companion object {
private const val TAG = "WildcardFragment"

private const val ATTRIBUTE = 1
private const val EVENT = 2
fun newInstance(): WildcardFragment = WildcardFragment()
}
}
73 changes: 58 additions & 15 deletions src/android/CHIPTool/app/src/main/res/layout/wildcard_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:constraint_referenced_ids="endpointIdLabel,endpointIdEd,clusterIdLabel,clusterIdEd,attributeIdLabel,attributeIdEd"
app:constraint_referenced_ids="endpointIdLabel,endpointIdEd,clusterIdLabel,clusterIdEd,attributeIdLabel,attributeIdEd,eventIdLabel,eventIdEd"
app:flow_horizontalBias="0.0"
app:flow_horizontalGap="8dp"
app:flow_horizontalStyle="packed"
Expand Down Expand Up @@ -76,6 +76,21 @@
android:inputType="number"
android:hint="@string/wildcard_help_label"/>

<TextView
android:id="@+id/eventIdLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:textSize="16sp"
android:text="@string/event_id_label" />

<EditText
android:id="@+id/eventIdEd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:inputType="number"
android:hint="@string/wildcard_help_label"/>
<Button
android:id="@+id/readBtn"
android:layout_width="wrap_content"
Expand All @@ -90,27 +105,55 @@
app:layout_constraintEnd_toStartOf="@id/subscribeBtn"
app:layout_constraintTop_toBottomOf="@id/flow"/>

<Button
android:id="@+id/subscribeBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"
android:layout_gravity="center"
android:text="@string/wildcard_subscribe_btn_text"
android:textSize="16sp"
app:layout_constraintStart_toEndOf="@id/readBtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/flow"/>
<Button
android:id="@+id/subscribeBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"
android:layout_gravity="center"
android:text="@string/wildcard_subscribe_btn_text"
android:textSize="16sp"
app:layout_constraintStart_toEndOf="@id/readBtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/flow"/>

<Button
android:id="@+id/readEventBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"
android:layout_gravity="center"
android:text="@string/wildcard_read_event_btn_text"
android:textSize="16sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/subscribeEventBtn"
app:layout_constraintTop_toBottomOf="@id/readBtn"/>

<Button
android:id="@+id/subscribeEventBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"
android:layout_gravity="center"
android:text="@string/wildcard_subscribe_event_btn_text"
android:textSize="16sp"
app:layout_constraintStart_toEndOf="@id/readEventBtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/subscribeBtn"/>

<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:fadeScrollbars="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/subscribeBtn">
app:layout_constraintTop_toBottomOf="@id/subscribeEventBtn">
<TextView
android:id="@+id/outputTv"
android:layout_width="match_parent"
Expand Down
3 changes: 3 additions & 0 deletions src/android/CHIPTool/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,12 @@
<string name="endpoint_id_label">Endpoint ID</string>
<string name="cluster_id_label">Cluster ID</string>
<string name="attribute_id_label">Attribute ID</string>
<string name="event_id_label">Event ID</string>
<string name="wildcard_help_label">Leave blank for wildcard</string>
<string name="wildcard_subscribe_btn_text">Subscribe</string>
<string name="wildcard_read_btn_text">Read</string>
<string name="wildcard_subscribe_event_btn_text">Subscribe Event</string>
<string name="wildcard_read_event_btn_text">Read Event</string>

<string name="provision_custom_flow_btn_text">Provision CHIP device with Custom Flow</string>
<string name="chip_device_info_commissioning_flow_label">Commissioning Flow:</string>
Expand Down
16 changes: 16 additions & 0 deletions src/controller/java/AndroidCallbacks-JNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,19 @@ JNI_METHOD(void, ReportCallbackJni, deleteCallback)(JNIEnv * env, jobject self,
VerifyOrReturn(reportCallback != nullptr, ChipLogError(Controller, "ReportCallback handle is nullptr"));
delete reportCallback;
}

JNI_METHOD(jlong, ReportEventCallbackJni, newCallback)
(JNIEnv * env, jobject self, jobject subscriptionEstablishedCallbackJava, jobject reportCallbackJava,
jobject resubscriptionAttemptCallbackJava)
{
ReportEventCallback * reportCallback = chip::Platform::New<ReportEventCallback>(
self, subscriptionEstablishedCallbackJava, reportCallbackJava, resubscriptionAttemptCallbackJava);
return reinterpret_cast<jlong>(reportCallback);
}

JNI_METHOD(void, ReportEventCallbackJni, deleteCallback)(JNIEnv * env, jobject self, jlong callbackHandle)
{
ReportEventCallback * reportCallback = reinterpret_cast<ReportEventCallback *>(callbackHandle);
VerifyOrReturn(reportCallback != nullptr, ChipLogError(Controller, "ReportCallback handle is nullptr"));
delete reportCallback;
}
Loading

0 comments on commit 4365604

Please sign in to comment.