Device Group

Last Updated on : 2024-05-11 10:43:30download

Overview

The device group service enables you to:

  • Get the list of devices to group.
  • Create a group.
  • Edit a group.
  • Delete a group.

Implementation

Prerequisites

Only when the user has the role of admin or higher and the device supports grouping, the device group service can work.

Check support for grouping

  // Check support for grouping
  DeviceBean deviceBean = ThingGroupCoreKit.INSTANCE.getDeviceBean(deviceId);
  boolean supportGroup = deviceBean.isSupportGroup();

Check user role

Check if the role of the current user is an admin or higher. Integrate with the Home Member Management BizBundle SDK and check user permissions with the following code.

  // Check user role
  var mMemberUseCase : IMemberUseCase = FamilyManagerCoreKit.getMemberUseCase()
  mMemberUseCase.getMemberInfo(11,111,object :IFamilyMemberDataCallback<MemberBean>{
      override fun onSuccess(result: MemberBean?) {
        /**
         * 0: Home owner MemberRole.ROLE_OWNER
         * 1: Home admin MemberRole.ROLE_ADMIN
         * 2: Common member MemberRole.ROLE_MEMBER
         */
        result?.role
      }

      override fun onError(errorCode: String?, errorMessage: String?) {

      }
    })

Step 1: Initialize management class

With the prerequisites met, perform the initialization with the device ID or group ID and get the group management class ThingGroupBizManager. This class handles all API calls for the group service.

  // If a group already exists, perform the initialization with the groupId.
  val initBuilder = GroupInitBuilder.Builder()
                      .setGroupId(groupId)
                      .build()

  // If no group exists, perform the initialization with the devcieId.
  val initBuilder = GroupInitBuilder.Builder()
                      .setDevId(deviceId)
                      .build()
  val thingGroupBizManager = ThingGroupBizKit.getGroupBizManager(homeId,initBuilder)
  • During the initialization, pass in the ID of the home associated with the current device or group.
  • Management classes obtained through different initialization methods are not interchangeable. For example, after initializing a management class with the device ID, you create a group using fetchDeviceList() and createGroup(). To edit or delete the group, first initialize a management class with the groupId. Then, use updateGroup() and dismissGroup() to make changes to the group. Otherwise, an error might occur.

Step 2: Get device list

With the ThingGroupBizManager, you can call fetchDeviceList() to get the list of devices that can be grouped.

    val operateBuilder = GroupOperateBuilder.Builder()
        .setQueryDeviceCallback(object : QueryDeviceCallback {
            override fun result(list: List<GroupInfo>) {
                /**
                 *
                 * Get the device list by group ID. The list includes devices already in the group and those that can be added.
                 * The checked field in GroupInfo identifies whether a device is already in the group.
                 *
                 * Get the device list by device ID. The list includes the device itself and any that can be grouped with it.
                 *
                 */
            }

        })
        .setFailureCallback(object : FailureCallback {
            override fun onError(
                errorCode: String?,
                errorMessage: String?,
                groupId: Long,
                failDevices: List<GroupResult>?
            ) {

            }

        })
        .build()
    thingGroupBizManager?.fetchDeviceList(operateBuilder)

Step 3: Create group

After fetchDeviceList() returns the list of devices for grouping, you can guide users to add a device to a group with an intuitive UI design. Then, users need to name the group. Finally, call createGroup() to create a group.

    val operateBuilder = GroupOperateBuilder.Builder()
        .setGroupName("Group 1")
        .setAllSelectDeviceList(selectedDevice)
        .setSuccessCallback(object : SuccessCallback {
            override fun result(groupId: Long, failDevices: List<GroupResult>?) {
              // A group is created. You get the group ID.

            }

        })
        .setProcessCallback(object : ProcessCallback {
            override fun result(process: Int, size: Int) {

            }
        })
        .setFailureCallback(object : FailureCallback {
            override fun onError(
                errorCode: String?,
                errorMessage: String?,
                groupId: Long,
                failDevices: List<GroupResult>?
            ) {

            }

        })
        .build()
    thingGroupBizManager?.createGroup(operateBuilder)

Step 4: Edit group

After a group is created and assigned a group ID, it can be edited. You need to initialize a management class with the group ID and get the new ThingGroupBizManager object to add or remove a device from the group.

  // If a group already exists, perform the initialization with the groupId.
    val initBuilder = GroupInitBuilder.Builder()
                      .setGroupId(groupId)
                      .build()
    val thingGroupBizManager = ThingGroupBizKit.getGroupBizManager(homeId,initBuilder)

    // Editing a device in the group only requires the list of selected device IDs, without needing the group name.
    val operateBuilder = GroupOperateBuilder.Builder()
        .setAllSelectDeviceList(selectedDevice)
        .setSuccessCallback(object : SuccessCallback {
            override fun result(groupId: Long, failDevices: List<GroupResult>?) {
              // Edited successfully.

            }

        })
        .setProcessCallback(object : ProcessCallback {
            override fun result(process: Int, size: Int) {

            }
        })
        .setFailureCallback(object : FailureCallback {
            override fun onError(
                errorCode: String?,
                errorMessage: String?,
                groupId: Long,
                failDevices: List<GroupResult>?
            ) {

            }

        })
        .build()
    thingGroupBizManager?.updateGroup(operateBuilder)

Step 5: Delete group

You need to initialize a management class with the group ID and get the new ThingGroupBizManager object to delete the group. You can use the same management class for editing and deleting groups.

  // If a group already exists, perform the initialization with the groupId even if it has been passed during initialization.
    val initBuilder = GroupInitBuilder.Builder()
                      .setGroupId(groupId)
                      .build()
    val thingGroupBizManager = ThingGroupBizKit.getGroupBizManager(homeId,initBuilder)

    GroupOperateBuilder operateBuilder = new GroupOperateBuilder.Builder()
            .setGroupId(groupId)
            .setSuccessCallback((groupId, failDevices) -> {
              // Deleted successfully.

            })
            .setFailureCallback((errorCode, errorMessage, groupId, failDevices) -> {

            })
            .build();
    thingGroupBizManager?.dismissGroup(operateBuilder)

Demo

This topic describes the service implementation in the sequence of read, create, update, and delete operations. In real-world projects, you can implement them as needed. For more information, see the Group Management Module in the BizBundle SDK Demo.