Sub-Device Control Through Zigbee Gateway

Last Updated on : 2023-06-01 06:18:29download

This topic describes how to control sub-devices that are connected through a Zigbee gateway. The gateway is also a device that can be controlled. For more information, see Device Management.

Integrate with the class

Class name Description
IThingGateway The gateway class provides operations related to a Zigbee gateway, including the capabilities to control, query, and monitor the status of its sub-devices.

Initialize a gateway object

API description

ThingHomeSdk.newGatewayInstance(String devId)

Parameters

Parameter Description
devId The device ID of the gateway.

Query a list of sub-devices

API description

void getSubDevList(IThingDataCallback<List<DeviceBean>> callback)

Parameters

The parameters are included in the asynchronous callback. The following code block shows this callback:

public interface IThingDataCallback<List<DeviceBean>> {

    void onSuccess(List<DeviceBean> result);

    void onError(String errorCode, String errorMessage);

}

Example

ThingHomeSdk.newGatewayInstance(devId).getSubDevList(new IThingDataCallback<List<DeviceBean>>() {
    @Override
    public void onSuccess(List<DeviceBean> list) {
    }

    @Override
    public void onError(String errorCode, String errorMessage) {

    }
});

Register a sub-device status listener

API description

void registerSubDevListener(ISubDevListener listener);

Parameters

The following code block shows the parameters for the listener:

public interface ISubDevListener {

    /**
     * The notification of the change in device DP status.
     *
     * @param nodeId The value of `nodeId` in the `DeviceBean` field for the sub-device.
     * @param dpStr The changed DP data of the sub-device.
     */
    void onSubDevDpUpdate(String nodeId, String dpStr);

    /**
     * The notification of device removal.
     */
    void onSubDevRemoved(String devId);

    /**
     * The notification of an added device.
     */
    void onSubDevAdded(String devId);

    /**
     * The notification that is sent when a sub-device is renamed.
     */
    void onSubDevInfoUpdate(String devId);

    /**
     * The notification that is sent when a sub-device gets online or offline.
     */
    void onSubDevStatusChanged(List<String> onlineNodeIds, List<String> offlineNodeIds);
}

Example

ThingHomeSdk.newGatewayInstance(devId).registerSubDevListener(new ISubDevListener() {
    @Override
    public void onSubDevDpUpdate(String nodeId, String dpStr) {

    }

    @Override
    public void onSubDevRemoved(String devId) {

    }

    @Override
    public void onSubDevAdded(String devId) {

    }

    @Override
    public void onSubDevInfoUpdate(String devId) {

    }

    @Override
    public void onSubDevStatusChanged(List<String> onlines, List<String> offlines) {

    }
});

Unregister a sub-device status listener

API description

void unRegisterSubDevListener();

Example

ThingHomeSdk.newGatewayInstance(devId).unRegisterSubDevListener();

Control a specified sub-device

API description

void publishDps(String nodeId, String dps, IResultCallback callback)

Parameters

Parameter Description
nodeId The node ID of the target sub-device. You can get the value from DeviceBean of the sub-device.
dps The list of target DPs in the JSON format to implement device control.
callback The success or failure callback.

Example

ThingHomeSdk.newGatewayInstance(devId).publishDps(subDeviceBean.getNodeId(), "{\"101\": true}", new IResultCallback() {
    @Override
    public void onError(String code, String error) {

    }

    @Override
    public void onSuccess() {

    }
});

Control a specified sub-device group

API description

Controls all devices that belong to the same group as a sub-device.

void multicastDps(String nodeId, String dps, IResultCallback callback)

Parameters

Parameter Description
nodeId The node ID of the target sub-device. You can get the value from DeviceBean of the sub-device.
dps The list of target DPs in the JSON format to implement device control.
callback The success or failure callback.

Example

ThingHomeSdk.newGatewayInstance(devId).multicastDps(subDeviceBean.getNodeId(), "{\"101\": true}", new IResultCallback() {
    @Override
    public void onError(String code, String error) {

    }

    @Override
    public void onSuccess() {

    }

});

Control all sub-devices of a gateway

API description

void broadcastDps(String dps, IResultCallback callback)

Parameters

Parameter Description
dps The list of target DPs in the JSON format to implement device control.
callback The success or failure callback.

Example

ThingHomeSdk.newGatewayInstance(devId).broadcastDps("{\"101\": true}", new IResultCallback() {
    @Override
    public void onError(String code, String error) {

    }

    @Override
    public void onSuccess() {

    }

});