Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kotlin templates for CHIP commands, ClusterHelper #159

Open
jonsmirl opened this issue Aug 15, 2023 · 1 comment
Open

Kotlin templates for CHIP commands, ClusterHelper #159

jonsmirl opened this issue Aug 15, 2023 · 1 comment

Comments

@jonsmirl
Copy link

It should be possible in Kotlin to template all of this and just pass in the command: getAdministratorCommissioningClusterForDevice.
From the command the template should be able to extract the parent class and the parameters. I'm sure a Google Kotlin expert can write this even more efficiently that I can think of. Then you'd just list the commands you need and the templates would expand everything.

/** Singleton to facilitate access to Clusters functionality. */
@Singleton
class ClustersHelper @Inject constructor(private val chipClient: ChipClient) {

    // -----------------------------------------------------------------------------------------------
    // Administrator Commissioning Cluster (11.19)

    suspend fun openCommissioningWindowAdministratorCommissioningCluster(
        deviceId: Long,
        endpoint: Int,
        timeoutSeconds: Int,
        pakeVerifier: ByteArray,
        discriminator: Int,
        iterations: Long,
        salt: ByteArray,
        timedInvokeTimeoutMs: Int
    ) {
        Timber.d("openCommissioningWindowAdministratorCommissioningCluster())")
        val connectedDevicePtr =
            try {
                chipClient.getConnectedDevicePointer(deviceId)
            } catch (e: IllegalStateException) {
                Timber.e(e, "Can't get connectedDevicePointer.")
                return
            }

        /*
        ChipClusters.DefaultClusterCallback var1, Integer var2, byte[] var3, Integer var4, Long var5, byte[] var6, int var7
         */
        return suspendCoroutine { continuation ->
            getAdministratorCommissioningClusterForDevice(connectedDevicePtr, endpoint)
                .openCommissioningWindow(
                    object : ChipClusters.DefaultClusterCallback {
                        override fun onSuccess() {
                            continuation.resume(Unit)
                        }

                        override fun onError(ex: java.lang.Exception?) {
                            Timber.e(
                                ex,
                                "getAdministratorCommissioningClusterForDevice.openCommissioningWindow command failure"
                            )
                            continuation.resumeWithException(ex!!)
                        }
                    },
                    timeoutSeconds,
                    pakeVerifier,
                    discriminator,
                    iterations,
                    salt,
                    timedInvokeTimeoutMs
                )
        }
    }

    private fun getAdministratorCommissioningClusterForDevice(
        devicePtr: Long,
        endpoint: Int
    ): ChipClusters.AdministratorCommissioningCluster {
        return ChipClusters.AdministratorCommissioningCluster(devicePtr, endpoint)
    }
}

@jonsmirl
Copy link
Author

jonsmirl commented Aug 15, 2023

Does this work? It is from the Kotlin documentation.

inline suspend fun <T> vx(crossinline callback: (Handler<AsyncResult<T>>) -> Unit) = 
    suspendCoroutine<T> { cont ->
        callback(Handler { result: AsyncResult<T> ->
            if (result.succeeded()) {
                cont.resume(result.result())
            } else {
                cont.resumeWithException(result.cause())
            }
        })
    }

Using this helper function, an arbitrary asynchronous vert.x function async.foo(params, handler) can be invoked from a coroutine with vx { async.foo(params, it) }.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant