Taking a Go at Gateway and Sub-device Development

  • The gateway adds the network topology relationship for the sub-device.
  • The sub-device reuses the gateway MQTT connection channel to implement the connection.
  • The gateway reports the sub-device data to the cloud.
  • The gateway receives the directive and forwards it to the sub-device.
  • The gateway reports the disconnection of the sub-device.
  • The gateway deletes the network topology relationship for the sub-device.

Create a Network Product

To create a network product, you’ll need to select a node type-which, for this tutorial, you’ll want it to be gateway-this will serve as the direct connection means that can mount sub-devices. The gateway can manage sub-devices, maintain the topological relationships that exists with sub-devices, and synchronize these topological relationships to the cloud.

Connect the Gateway

You can set the following parameters to connect the gateway device.

LinkKitInitParams params = new LinkKitInitParams();DeviceInfo gatewayInfo = new DeviceInfo();
gatewayInfo.productKey = gateway.productKey;
gatewayInfo.deviceName = gateway.deviceName;
gatewayInfo.deviceSecret = gateway.deviceSecret;
params.deviceInfo = gatewayInfo;
LinkKit.getInstance().init(params, ILinkKitConnectListener)

Add the Network Topology Relationship

You can set the following parameters to add the network topology relationship.

DeviceInfo deviceInfo = new DeviceInfo();
deviceInfo.productKey = productKey;
deviceInfo.deviceName = deviceName;
deviceInfo.deviceSecret = deviceSecret;
LinkKit.getInstance().getGateway().gatewayAddSubDevice(
deviceInfo, // the ID of the sub-device
SubDeviceConnectListener)

Connect the Sub-device

You can set the following parameters to connect the sub-device.

DeviceInfo deviceInfo = new DeviceInfo();
deviceInfo.productKey = productKey;
deviceInfo.deviceName = deviceName;
deviceInfo.deviceSecret = deviceSecret;
LinkKit.getInstance().getGateway().gatewaySubDeviceLogin(
deviceInfo, // the ID of the sub-device
ISubDeviceActionListener)

Report the Sub-device Data

You can set the following parameters to report the data of the sub-device.

DeviceInfo deviceInfo = new DeviceInfo();
deviceInfo.productKey = productKey;
deviceInfo.deviceName = deviceName;
deviceInfo.deviceSecret = deviceSecret;
LinkKit.getInstance().getGateway().gatewaySubDevicePublish(
topic, // sub-device topic
data, // the data
deviceInfo, // the ID of the sub-device
ISubDeviceActionListener)

Make the Sub-device Subscribe to Topics

You can set the following parameters to make the sub-device subscribe to topics.

DeviceInfo deviceInfo = new DeviceInfo();
deviceInfo.productKey = productKey;
deviceInfo.deviceName = deviceName;
deviceInfo.deviceSecret = deviceSecret;
LinkKit.getInstance().getGateway().gatewaySubDeviceSubscribe(
topic, // sub-device subscription topic
deviceInfo, // the ID of the sub-device
ISubDeviceActionListener)

Disconnect the Sub-device

DeviceInfo deviceInfo = new DeviceInfo();
deviceInfo.productKey = productKey;
deviceInfo.deviceName = deviceName;
deviceInfo.deviceSecret = deviceSecret;
LinkKit.getInstance().getGateway().gatewaySubDeviceLogout(
deviceInfo, // the ID of the sub-device
ISubDeviceActionListener)

Delete the Network Topology Relationship for the Sub-device

DeviceInfo deviceInfo = new DeviceInfo();
deviceInfo.productKey = productKey;
deviceInfo.deviceName = deviceName;
deviceInfo.deviceSecret = deviceSecret;
LinkKit.getInstance().getGateway().gatewayDeleteSubDevice(
deviceInfo, // the ID of the sub-device
ISubDeviceRemoveListener)

Original Source

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com