Skip to content

Commit

Permalink
Update Android CHIPTool to use cluster commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
austinh0 committed Jun 18, 2021
1 parent 6437ffc commit 34105ea
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,37 @@ import android.view.ViewGroup
import android.widget.SeekBar
import android.widget.Toast
import androidx.fragment.app.Fragment
import chip.devicecontroller.ChipCommandType
import chip.devicecontroller.ChipClusters
import chip.devicecontroller.ChipClusters.OnOffCluster
import chip.devicecontroller.ChipDeviceController
import chip.devicecontroller.ChipDeviceControllerException
import com.google.chip.chiptool.ChipClient
import com.google.chip.chiptool.GenericChipDeviceListener
import com.google.chip.chiptool.R
import com.google.chip.chiptool.util.DeviceIdUtil
import kotlinx.android.synthetic.main.on_off_client_fragment.*
import kotlinx.android.synthetic.main.on_off_client_fragment.view.*
import kotlinx.android.synthetic.main.on_off_client_fragment.commandStatusTv
import kotlinx.android.synthetic.main.on_off_client_fragment.deviceIdEd
import kotlinx.android.synthetic.main.on_off_client_fragment.fabricIdEd
import kotlinx.android.synthetic.main.on_off_client_fragment.levelBar
import kotlinx.android.synthetic.main.on_off_client_fragment.view.levelBar
import kotlinx.android.synthetic.main.on_off_client_fragment.view.offBtn
import kotlinx.android.synthetic.main.on_off_client_fragment.view.onBtn
import kotlinx.android.synthetic.main.on_off_client_fragment.view.toggleBtn
import kotlinx.android.synthetic.main.on_off_client_fragment.view.updateAddressBtn

class OnOffClientFragment : Fragment() {
private val deviceController: ChipDeviceController
get() = ChipClient.getDeviceController()

private var commandType: ChipCommandType? = null
private var levelValue: Int? = null

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return inflater.inflate(R.layout.on_off_client_fragment, container, false).apply {
deviceController.setCompletionListener(ChipControllerCallback())

updateAddressBtn.setOnClickListener{ updateAddressClick() }
updateAddressBtn.setOnClickListener { updateAddressClick() }
onBtn.setOnClickListener { sendOnCommandClick() }
offBtn.setOnClickListener { sendOffCommandClick() }
toggleBtn.setOnClickListener { sendToggleCommandClick() }
Expand All @@ -50,12 +55,12 @@ class OnOffClientFragment : Fragment() {
}

override fun onStopTrackingTouch(seekBar: SeekBar?) {
Toast.makeText(requireContext(),
"Level is: " + levelBar.progress,
Toast.LENGTH_SHORT).show()
commandType = ChipCommandType.LEVEL
levelValue = levelBar.progress
sendCommand()
Toast.makeText(
requireContext(),
"Level is: " + levelBar.progress,
Toast.LENGTH_SHORT
).show()
sendLevelCommandClick()
}
})
}
Expand All @@ -70,9 +75,7 @@ class OnOffClientFragment : Fragment() {
}

inner class ChipControllerCallback : GenericChipDeviceListener() {
override fun onConnectDeviceComplete() {
sendCommand()
}
override fun onConnectDeviceComplete() {}

override fun onSendMessageComplete(message: String?) {
commandStatusTv.text = requireContext().getString(R.string.echo_status_response, message)
Expand All @@ -93,7 +96,10 @@ class OnOffClientFragment : Fragment() {

private fun updateAddressClick() {
val serviceInfo = NsdServiceInfo().apply {
serviceName = "%016X-%016X".format(fabricIdEd.text.toString().toLong(), deviceIdEd.text.toString().toLong())
serviceName = "%016X-%016X".format(
fabricIdEd.text.toString().toLong(),
deviceIdEd.text.toString().toLong()
)
serviceType = "_chip._tcp"
}

Expand Down Expand Up @@ -125,43 +131,69 @@ class OnOffClientFragment : Fragment() {
}
}

private fun sendLevelCommandClick() {
val cluster = ChipClusters.LevelControlCluster(
ChipClient.getDeviceController()
.getDevicePointer(deviceIdEd.text.toString().toLong()), 1
)
cluster.moveToLevel(object : ChipClusters.DefaultClusterCallback {
override fun onSuccess() {
showMessage("MoveToLevel command success")
}

override fun onError(ex: Exception) {
showMessage("MoveToLevel command failure $ex")
Log.e(TAG, "MoveToLevel command failure", ex)
}

}, levelBar.progress, 0, 0, 0)
}

private fun sendOnCommandClick() {
commandType = ChipCommandType.ON
levelValue = 0
sendCommand()
getOnOffClusterForDevice().on(object : ChipClusters.DefaultClusterCallback {
override fun onSuccess() {
showMessage("ON command success")
}

override fun onError(ex: Exception) {
showMessage("ON command failure $ex")
Log.e(TAG, "ON command failure", ex)
}

})
}

private fun sendOffCommandClick() {
commandType = ChipCommandType.OFF
levelValue = 0
sendCommand()
}
getOnOffClusterForDevice().off(object : ChipClusters.DefaultClusterCallback {
override fun onSuccess() {
showMessage("OFF command success")
}

private fun sendToggleCommandClick() {
commandType = ChipCommandType.TOGGLE
levelValue = 0
sendCommand()
override fun onError(ex: Exception) {
showMessage("OFF command failure $ex")
Log.e(TAG, "OFF command failure", ex)
}
})
}

private fun sendCommand() {
val chipCommandType = commandType ?: run {
Log.e(TAG, "No ChipCommandType specified.")
return
}
private fun sendToggleCommandClick() {
getOnOffClusterForDevice().toggle(object : ChipClusters.DefaultClusterCallback {
override fun onSuccess() {
showMessage("TOGGLE command success")
}

commandStatusTv.text = requireContext()
.getString(R.string.send_command_type_label_text, chipCommandType.name, levelValue)
override fun onError(ex: Exception) {
showMessage("TOGGLE command failure $ex")
Log.e(TAG, "TOGGLE command failure", ex)
}
})
}

try {
// mask levelValue from integer to uint8_t and if null use 0
deviceController.sendCommand(
DeviceIdUtil.getLastDeviceId(requireContext()),
commandType,
( 0xff and (levelValue ?: 0))
)
} catch (e: ChipDeviceControllerException) {
showMessage(e.toString())
}
private fun getOnOffClusterForDevice(): OnOffCluster {
return OnOffCluster(
ChipClient.getDeviceController()
.getDevicePointer(deviceIdEd.text.toString().toLong()), 1
)
}

private fun showMessage(msg: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ class DeviceProvisioningFragment : Fragment() {

private val networkType: ProvisionNetworkType
get() = requireNotNull(
ProvisionNetworkType.fromName(arguments?.getString(ARG_PROVISION_NETWORK_TYPE))
ProvisionNetworkType.fromName(arguments?.getString(ARG_PROVISION_NETWORK_TYPE))
)

private val scope = CoroutineScope(Dispatchers.Main + Job())

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
deviceInfo = checkNotNull(requireArguments().getParcelable(ARG_DEVICE_INFO))
return inflater.inflate(R.layout.single_fragment_container, container, false).apply {
Expand All @@ -83,17 +83,17 @@ class DeviceProvisioningFragment : Fragment() {
val bluetoothManager = BluetoothManager()

showMessage(
R.string.rendezvous_over_ble_scanning_text,
deviceInfo.discriminator.toString()
R.string.rendezvous_over_ble_scanning_text,
deviceInfo.discriminator.toString()
)
val device = bluetoothManager.getBluetoothDevice(deviceInfo.discriminator) ?: run {
showMessage(R.string.rendezvous_over_ble_scanning_failed_text)
return@launch
}

showMessage(
R.string.rendezvous_over_ble_connecting_text,
device.name ?: device.address.toString()
R.string.rendezvous_over_ble_connecting_text,
device.name ?: device.address.toString()
)
gatt = bluetoothManager.connect(requireContext(), device)

Expand All @@ -110,7 +110,7 @@ class DeviceProvisioningFragment : Fragment() {
requireActivity().runOnUiThread {
val context = requireContext()
Toast.makeText(context, context.getString(msgResId, stringArgs), Toast.LENGTH_SHORT)
.show()
.show()
}
}

Expand All @@ -120,13 +120,13 @@ class DeviceProvisioningFragment : Fragment() {
}

override fun onStatusUpdate(status: Int) {
Log.d(TAG, "Pairing status update: $status");
Log.d(TAG, "Pairing status update: $status")
}

override fun onPairingComplete(code: Int) {
Log.d(TAG, "onPairingComplete: $code")

if (code == 0) {
if (code == STATUS_PAIRING_SUCCESS) {
childFragmentManager.beginTransaction()
.add(R.id.fragment_container, EnterNetworkFragment.newInstance(networkType))
.commit()
Expand Down Expand Up @@ -171,11 +171,11 @@ class DeviceProvisioningFragment : Fragment() {
private const val TAG = "DeviceProvisioningFragment"
private const val ARG_DEVICE_INFO = "device_info"
private const val ARG_PROVISION_NETWORK_TYPE = "provision_network_type"
private const val STATUS_NETWORK_PROVISIONING_SUCCESS = 2
private const val STATUS_PAIRING_SUCCESS = 0

fun newInstance(
deviceInfo: CHIPDeviceInfo,
networkType: ProvisionNetworkType
deviceInfo: CHIPDeviceInfo,
networkType: ProvisionNetworkType
): DeviceProvisioningFragment {
return DeviceProvisioningFragment().apply {
arguments = Bundle(2).apply {
Expand Down
Loading

0 comments on commit 34105ea

Please sign in to comment.