All Products
Search
Document Center

IoT Platform:Use the advanced features

Last Updated:Oct 20, 2023

This topic describes how to configure IoT as Bridge SDK to use the advanced features. You can specify configuration file paths and dynamically register bridges. You can also call the operations that are encapsulated in IoT as Bridge SDK to submit Thing Specification Language (TSL) data, submit properties or events, set properties, and call services.

Specify configuration file paths

By default, the configuration file of a bridge is application.conf, and the configuration file of the device certificate mapping is devices.conf.

IoT as Bridge SDK allows you to specify custom file paths. Before you call the bootstrap() method, you can call the ConfigFactory.init() method to specify the path of a configuration file. You can also create an instance and implement the methods based on your business requirements.

Sample code:

private static DeviceConfigManager selfDefineDeviceConfigManager = new DeviceConfigManager() {
    @Override
    public DeviceIdentity getDeviceIdentity(String originalIdentity) {
        return devicesMap.get(originalIdentity);
    }

    @Override
    public String getOriginalIdentity(String productKey, String deviceName) {
        return null;
    }
};
BridgeBootstrap bridgeBootstrap = new BridgeBootstrap();
ConfigFactory.init(ConfigFactory.getBridgeConfigManager("application-self-define.conf"),selfDefineDeviceConfigManager);
bridgeBootstrap.bootstrap();

Dynamically register bridges

When you deploy bridges on a large number of servers, the deployment process is complex if you need to specify different bridges for different bridge servers. You can configure the application.conf bridge configuration file to dynamically register bridges.

You must specify the productKey and popClientProfile parameters of a bridge in the configuration file. Then, IoT as Bridge SDK calls an API operation of IoT Platform to register the bridge and uses the MAC address of the bridge server as the DeviceName of the bridge.

Note
  • To dynamically register bridges, you need to only configure the bridge configuration file. For information about the sample code, see Use the basic features.

  • All fields in the popClientprofile parameter must be specified. If a MAC address has been used as the DeviceName of an existing device, the device is used as a bridge.

  • The deviceName and deviceSecret parameters must be left empty. If you have specified the information about a bridge, the bridge cannot be dynamically registered.

  • We recommend that you use dedicated test devices for debugging. Do not debug programs on local machines to prevent possible impacts on the production environment.

    If you debug the programs on multiple local machines, the MAC addresses of the machines are registered as bridge names. The bridges are associated with all devices specified in the devices.conf file.

Table 1. Parameters

Parameter

Required

Description

productKey

Yes

The ProductKey of the product to which the bridge belongs.

subDeviceConnectMode

No

The type of the bridge.

  • If this parameter is set to 3, a large-sized bridge is created. Up to 500,000 devices can be connected to the bridge.

  • If this parameter is not specified, a small-sized bridge is created. Up to 1,500 devices can be connected to the bridge.

Large-sized and small-sized bridges use different policies to disconnect devices. For more information, see the Disconnect a device from IoT Platform section of the "Use the basic features" topic.

http2Endpoint

Yes

The endpoint of the HTTP/2 gateway. The endpoint is used to establish a persistent connection between the bridge and IoT Platform over the HTTP/2 protocol.

Endpoint format:

  • Enterprise Edition instances: https://${IotInstanceId}.http2.iothub.aliyuncs.com:443.

    Replace ${IotInstanceId} with the ID of the instance that you purchased.

    For example, if the instance ID is iot-cn-g06kwb****, the endpoint is https://iot-cn-g06kwb****.http2.iothub.aliyuncs.com:443.

  • Public instances: The endpoint format for public instances of the new version is different from the endpoint format for public instances of the old version.

    • Public instances of the new version use the same endpoint format as Enterprise Edition instances.

    • Public instances of the old version: https://${productKey}.iot-as-http2.${RegionId}.aliyuncs.com:443.

      Replace ${productKey} with the ProductKey of the product to which your bridge belongs.

      Replace ${RegionId} with the ID of the region where the IoT Platform service is deployed. For information about region IDs, see Regions and Zones.

      For example, if the ProductKey of a bridge is a1abcab**** and the public instance resides in the China (Shanghai) region, the endpoint is https://a1abcab****.iot-as-http2.cn-shanghai.aliyuncs.com:443.

    For more information about IoT Platform instances, see Overview.

authEndpoint

Yes

The endpoint of the device verification service.

Endpoint format:

  • Enterprise Edition instances: https://${IotInstanceId}.auth.iothub.aliyuncs.com/auth/bridge.

    Replace ${IotInstanceId} with the ID of the instance that you purchased.

    For example, if the ID of an instance is iot-cn-g06kwb****, the endpoint of the instance is https://iot-cn-g06kwb****.auth.iothub.aliyuncs.com/auth/bridge.

  • Public instances: The endpoint format for public instances of the new version is different from the endpoint format for public instances of the old version.

    • Public instances of the new version use the same endpoint format as Enterprise Edition instances.

    • Public instances of the old version: https://iot-auth.${RegionId}.aliyuncs.com/auth/bridge.

      Replace ${RegionId} with the ID of the region where the IoT Platform service is deployed. For information about region IDs, see Regions and Zones.

      For example, if an instance resides in the China (Shanghai) region, the endpoint of the instance is https://iot-auth.cn-shanghai.aliyuncs.com/auth/bridge.

popClientProfile

Yes

If you specify this parameter, IoT as Bridge SDK automatically calls an operation of IoT Platform to create a bridge.

The following table describes the fields of popClientProfile.

Table 2. popClientProfile

Field

Required

Description

accessKey

Yes

The AccessKey ID of your Alibaba Cloud account.

Log on to the IoT Platform console, move the pointer over your profile picture, and then click AccessKey Management. On the AccessKey Pair page, you can create or view AccessKey pairs.

accessSecret

Yes

The AccessKey secret of your Alibaba Cloud account.

name

Yes

The ID of the region to which the bridge belongs.

For more information about region IDs, see Regions and Zones.

region

Yes

product

Yes

The name of the product. Set the value to Iot.

endpoint

Yes

The endpoint of the API in the specified region. The value must be specified in the iot.${RegionId}.aliyuncs.com format.

Replace ${RegionId} with the ID of the region where the IoT Platform service is deployed. For information about region IDs, see Regions and Zones.

For example, if your IoT Platform service resides in the China (Shanghai) region, the endpoint is iot.cn-shanghai.aliyuncs.com.

The following sample code shows how to configure a small-sized bridge. In this example, an Enterprise Edition instance is used.

// The sendpoint of services.
http2Endpoint = "https://${IotInstanceId}.http2.iothub.aliyuncs.com:443"
authEndpoint = "https://${IotInstanceId}.auth.iothub.aliyuncs.com/auth/bridge"

// The parameters of the bridge.
productKey = ${YourProductKey}

popClientProfile = {
    accessKey = ${YourAliyunAccessKey}
    accessSecret = ${YourAliyunAccessSecret}
    name = cn-shanghai
    region = cn-shanghai
    product = Iot
    endpoint = iot.cn-shanghai.aliyuncs.com
}

Call operations to submit TSL data

IoT as Bridge SDK encapsulates the operations to submit data. You can call the reportProperty operation to submit properties, call the fireEvent operation to submit events, and call the updateDeviceTag operation to update tags. A device can use these operations to report properties, report events, and update device tags.

Note
  • Before you call the reportProperty and fireEvent operations, you must define properties and events in the IoT Platform console. Log on to the IoT Platform console and go to the details page of a product. Then, you can define properties and events on the Define Feature tab. For more information, see Add a TSL feature.

  • If a tag already exists when you call the updateDeviceTag operation, the tag value is updated. To view existing tags, go to the details page of a device in the IoT Platform console. If a tag does not exist, IoT Platform automatically creates the tag.

Sample code:

TslUplinkHandler tslUplinkHandler = new TslUplinkHandler();
// Submit a property. 
// The testProp property is defined. 
String requestId = String.valueOf(random.nextInt(1000));
// The timestamp is not included in the submitted property data. 
tslUplinkHandler.reportProperty(requestId, originalIdentity, "testProp", random.nextInt(100));
// The timestamp is included in the submitted property data. 
//tslUplinkHandler.reportProperty(requestId, originalIdentity, "testProp", random.nextInt(100), System.currentTimeMillis());

// Submit an event. 
// The testEvent event is defined. 
requestId = String.valueOf(random.nextInt(1000));
HashMap<String, Object> params = new HashMap<String, Object>();
params.put("testEventParam", 123);
// The timestamp is not included in the submitted event data. 
tslUplinkHandler.fireEvent(originalIdentity, "testEvent", ThingEventTypes.INFO, params);
// The timestamp is included in the submitted event data.
//tslUplinkHandler.fireEvent(originalIdentity, "testEvent", ThingEventTypes.INFO, params, System.currentTimeMillis());

// Update a device tag. 
// The key of the tag is set to testDeviceTag. 
requestId = String.valueOf(random.nextInt(1000));
tslUplinkHandler.updateDeviceTag(requestId, originalIdentity, "testDeviceTag", String.valueOf(random.nextInt(1000)));

The following table describes the parameters.

Parameter

Description

requestId

The ID of the request.

originalIdentity

The original identifier of the device.

testProp

The identifier of the property. In this example, the identifier of the defined property is testProp. The testProp property is submitted to IoT Platform.

random.nextInt(100)

The value of the property. When you define a property, you can set a value range. In this example, random.nextInt(100) indicates a random integer that is less than 100.

testEvent

The identifier of the event. In this example, the identifier of the defined event is testEvent. The testEvent event is submitted to IoT Platform.

ThingEventTypes.INFO

The type of the event. ThingEventTypes specifies the event type. A value of INFO indicates that the event type is INFO.

In this example, the event type is set to INFO when the testEvent event is defined in the IoT Platform console.

If the event type is ERROR, set this parameter to ThingEventTypes.ERROR.

params

The output parameters of the event. The identifiers, data types, and value ranges of the output parameters are defined in the IoT Platform console. In this example, the identifier of the output parameter is testEventParam, and the value is 123.

testDeviceTag

The tag key. The data type is string. In this example, the key is testDeviceTag. Set the tag key based on the configuration rules for tag keys and your business requirements. For more information, see Tags.

String.valueOf(random.nextInt(1000))

The tag value. The data type is string. In this example, String.valueOf(random.nextInt(1000)) indicates a random integer that is less than 1,000. Set the tag value based on the configuration rules for tag values and your business requirements. For more information, see Tags.

System.currentTimeMillis()

The timestamp of the system. Specify a UNIX timestamp representing the number of milliseconds that have elapsed since January 1, 1970, 00:00:00 Coordinated Universal Time (UTC).

Call operations to submit multiple properties and events at a time

IoT as Bridge SDK encapsulates the operations to submit multiple properties and events at a time. You must create a BatchPostEventPropertyMessage object and call the addProperty() and addEvent() methods to add property and event data. Then, create a TslUplinkHandler object and call the BatchPostEventPropertyMessage() method to submit data.

Note

Before you call the reportProperty and fireEvent operations, you must define properties and events in the IoT Platform console. Log on to the IoT Platform console and go to the details page of a product. Then, you can define properties and events on the Define Feature tab. For more information, see Add a TSL feature.

Sample code:

TslUplinkHandler tslUplinkHandler = new TslUplinkHandler();
// Submit multiple properties and events at a time.
String requestId = String.valueOf(random.nextInt(1000));
long startTime = System.currentTimeMillis() - 3000;

// Create a message to submit data.
BatchPostEventPropertyMessage batchPostEventPropertyMessage = new BatchPostEventPropertyMessage();
Map<String, Object> aiEventParams = new HashMap<>();
aiEventParams.put("EventContent", "hello world");
batchPostEventPropertyMessage
    .addProperty("PowerConsumption", 1000, startTime)
    .addProperty("PowerConsumption", 123, startTime + 1000)
    .addProperty("LightAdjustLevel", 23, startTime)
    .addProperty("LightAdjustLevel", 44, startTime + 1000)
    .addProperty("LightAdjustLevel", 47, startTime + 2000)
    .addEvent("AIEvent", aiEventParams, startTime);
batchPostEventPropertyMessage.setId(requestId);

// Submit the data.
tslUplinkHandler.batchPostEventPropertyMessage(originalIdentity, batchPostEventPropertyMessage);

The following table describes the parameters.

Parameter

Description

requestId

The ID of the request.

startTime

The timestamps of the properties and events to be submitted. Specify a UNIX timestamp representing the number of milliseconds that have elapsed since January 1, 1970, 00:00:00 UTC. You can specify a custom value for this parameter based on your business requirements.

aiEventParams

The information about the event.

PowerConsumption

The identifiers of the properties. In this example, the identifiers of the defined properties include PowerConsumption and LightAdjustLevel. The values of the two properties at different points in time are submitted.

LightAdjustLevel

AIEvent

The identifier of the event. In this example, the identifier of the defined event is AIEvent. The AIEvent event is submitted to IoT Platform.

originalIdentity

The original identifier of the device.

Call operations to set properties and call services

IoT as Bridge SDK encapsulates the PropertySetHandler operation to set properties and the ServiceInvokeHandler operation to call services. A device can use these operations to receive commands from IoT Platform to update data.

Sample code:

BridgeBootstrap bridgeBootstrap = new BridgeBootstrap();
// Set properties.
bridgeBootstrap.setPropertySetHandler(new PropertySetHandler() {
    @Override
    public void onPropertySet(PropertySetMessage msg) {
        log.info("on property set, {}", msg.getParams());
        // If you call the replySuccess() method, the SDK sends the /property/set_reply message to IoT Platform. The response code is 200.
        msg.replySuccess();
        // If you call the replyFail() method, the SDK sends the /property/set_reply message to IoT Platform. You can specify the response code based on your business requirements.
        //msg.replyFail(400);
    }
});

// Call services.
bridgeBootstrap.setServiceInvokeHandler(new ServiceInvokeHandler() {
    @Override
    public void onServiceInvoke(ServiceInvokeMessage message) {
        log.info("on service invoke, {}", message.getParams());
        // If you call the replySuccess() method, the SDK sends the /service/{service.identifier}_reply message to IoT Platform. The response code is 200.
        message.replySuccess();
        // If you call the replyFail() method, the SDK sends the /service/{service.identifier}_reply message to IoT Platform. You can specify the response code based on your business requirements.
        //msg.replyFail(400);
    }
});