网关和子设备实战开发(java)在很多物联网场景下,终端设备本身不具备联网能力,那么如何上传数据到云端?物联网平台支持MQTT直连设备,支持的设备挂载在网关上。网关作为网关的子设备,代表网关接入物联网平台。此时网关设备不仅作为物联网网关设备(带有身份三元组)与物联网平台建立MQTT连接,收发数据,还负责子设备的管理,包括:添加子设备-设备到网关,网络拓扑关系,子设备复用网关mqtt连接通道上线。网关将子设备数据上报给云网关接收指令转发给子设备。网关上报子设备下线。网关删除子设备网络拓扑关系。网关与子设备的通信协议由本地网络决定,可以是http、mqtt、ZigBee、Modbus、BLE、OPC-UA等,这部分逻辑由网关实现,以及IoTSDK不包含这部分功能。1、创建网关产品创建网关产品时,需要选择节点类型:网关,即直连设备,可以挂载子设备。网关可以对子设备进行管理,维护与子设备的拓扑关系,并将拓扑关系同步到云端。网关与子设备的拓扑关系如下图所示:2.网关设备在线LinkKitInitParamsparams=newLinkKitInitParams();DeviceInfogatewayInfo=newDeviceInfo();gatewayInfo.productKey=gateway.productKey;gatewayInfo。deviceName=gateway.deviceName;gatewayInfo.deviceSecret=gateway.deviceSecret;params.deviceInfo=gatewayInfo;LinkKit.getInstance().init(params,ILinkKitConnectListener)在控制台可以看到网关在线,设备状态在线3.添加网络拓扑DeviceInfodeviceInfo=newDeviceInfo();deviceInfo.productKey=productKey;deviceInfo.deviceName=deviceName;deviceInfo.deviceSecret=deviceSecret;LinkKit.getInstance().getGateway().gatewayAddSubDevice(deviceInfo,//subdeviceidentitySubDeviceConnectListener)4.子设备在线DeviceInfodeviceInfo=newDeviceInfo();deviceInfo.productKey=productKey;deviceInfo.deviceName=deviceName;deviceInfo.deviceSecret=deviceSecret;LinkKit.getInstance().getGateway().gatewaySubDeviceLogin(deviceInfo,//子设备标识ISubDeviceActionListener)子设备访问官网5.子设备上报数据DeviceInfodeviceInfo=newDeviceInfo();deviceInfo.productKey=productKey;deviceInfo.deviceName=deviceName;deviceInfo.deviceSecret=deviceSecret;LinkKit.getInstance().getGateway().gatewaySubDevicePublish(topic,//子设备topicdata,//datadeviceInfo,//subDeviceidentityISubDeviceActionListener)6.子设备订阅topicDeviceInfodeviceInfo=newDeviceInfo();deviceInfo.productKey=productKey;deviceInfo.deviceName=deviceName;deviceInfo.deviceSecret=deviceSecret;LinkKit.getInstance().getGateway().gatewaySubDeviceSubscri,//子设备订阅TopicdeviceInfo,//子设备标识ISubDeviceActionListener)7.子设备离线DeviceInfodeviceInfo=newDeviceInfo();deviceInfo.productKey=productKey;deviceInfo.deviceName=deviceName;deviceInfo.deviceSecret=deviceSecret;LinkKit.getInstance().getGateway().gatewaySubDeviceLogout(deviceInfo,//子设备标识ISubDeviceActionListener)8.子设备网络拓扑删除DeviceInfodeviceInfo=newDeviceInfo();deviceInfo.productKey=productKey;deviceInfo.deviceName=deviceName;设备信息.deviceSecret=deviceSecret;LinkKit.getInstance().getGateway().gatewayDeleteSubDevice(deviceInfo,//子设备标识ISubDeviceRemoveListener)物联网平台产品介绍详情:https://www.aliyun.com/produc...阿里云物联网平台客户交流群
