网关与子设施开发实战(java)
在很多物联网场景中,终端设备自身没有连贯互联网能力,那么数据如何上云呢?
IoT物联网平台反对设施MQTT直连,也反对的设施挂载到网关上,作为网关的子设施,由网关代理接入IoT物联网平台。
这时候网关设施除了本身作为IoT网关设施(领有身份三元组)与IoT物联网平台建设MQTT连贯,收发数据,还要负责子设施的治理,包含:
- 网关增加子设施网络拓扑关系
- 子设施复用网关mqtt连贯通道上线
- 网关把子设施数据上报到云端
- 网关接管指令,并转发给子设施
- 网关上报子设施下线
- 网关删除子设施网络拓扑关系
网关和子设施通信的协定由本地网络决定,能够是http,mqtt,ZigBee,Modbus,BLE,OPC-UA等,这部分逻辑由网关实现,IoT SDK不蕴含这部分性能。
1.创立网关产品
创立网关产品时,须要抉择节点类型:网关,即指能够挂载子设施的直连设施。网关能够治理子设施、能够维持与子设施的拓扑关系,并将该拓扑关系同步到云端。
网关与子设施的拓扑关系如下图所示:
2.网关设施上线
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)
网关上线能够在控制台看到,设施状态是 在线
3.增加网络拓扑关系
DeviceInfo deviceInfo = new DeviceInfo();deviceInfo.productKey = productKey;deviceInfo.deviceName = deviceName;deviceInfo.deviceSecret = deviceSecret;LinkKit.getInstance().getGateway().gatewayAddSubDevice( deviceInfo, //子设施身份 SubDeviceConnectListener)
4.子设施上线
DeviceInfo deviceInfo = new DeviceInfo();deviceInfo.productKey = productKey;deviceInfo.deviceName = deviceName;deviceInfo.deviceSecret = deviceSecret;LinkKit.getInstance().getGateway().gatewaySubDeviceLogin( deviceInfo, //子设施身份 ISubDeviceActionListener)
子设施查看到接入官网的信息
5.子设施上报数据
DeviceInfo deviceInfo = new DeviceInfo();deviceInfo.productKey = productKey;deviceInfo.deviceName = deviceName;deviceInfo.deviceSecret = deviceSecret;LinkKit.getInstance().getGateway().gatewaySubDevicePublish( topic, //子设施topic data, //数据 deviceInfo, //子设施身份 ISubDeviceActionListener)
6.子设施订阅主题
DeviceInfo deviceInfo = new DeviceInfo();deviceInfo.productKey = productKey;deviceInfo.deviceName = deviceName;deviceInfo.deviceSecret = deviceSecret;LinkKit.getInstance().getGateway().gatewaySubDeviceSubscribe( topic, //子设施订阅Topic deviceInfo, //子设施身份 ISubDeviceActionListener)
7.子设施下线
DeviceInfo deviceInfo = new DeviceInfo();deviceInfo.productKey = productKey;deviceInfo.deviceName = deviceName;deviceInfo.deviceSecret = deviceSecret;LinkKit.getInstance().getGateway().gatewaySubDeviceLogout( deviceInfo, //子设施身份 ISubDeviceActionListener)
8.子设施网络拓扑删除
DeviceInfo deviceInfo = new DeviceInfo();deviceInfo.productKey = productKey;deviceInfo.deviceName = deviceName;deviceInfo.deviceSecret = deviceSecret;LinkKit.getInstance().getGateway().gatewayDeleteSubDevice( deviceInfo, //子设施身份 ISubDeviceRemoveListener)
物联网平台产品介绍详情:https://www.aliyun.com/produc...
阿里云物联网平台客户交换群