关于传感器:IoT-存量设备-零改造泛化SDK实现整体业务迁移上云实践类

10次阅读

共计 4359 个字符,预计需要花费 11 分钟才能阅读完成。

在物联网理论我的项目中,有些设施采纳公有协定接入了本地设施管理系统,有些 NB-IoT 设施被迫接入了电信 AEP 平台,有些设施接入了挪动 OneNET 平台。但甲方客户的整体业务都部署在阿里云上,咱们如何实现整体业务上云呢?

阿里云 IoT 物联网平台提供了泛化协定 SDK 接入的计划,在 IoT 设施零革新的前提下,帮忙企业疾速构建云上桥接服务,通过网桥实现 IoT 终端设备与阿里云 IoT 物联网平台的双向数据通信。

技术架构

计划 1:泛化 SDK

泛化协定 SDK 是协定自适应的框架,用以构建与阿里云物联网平台进行高效双向通信的桥接服务。

实用场景

泛化协定 SDK 面向的指标场景包含:

  • 设施无 Internet 联网能力。
  • 设施采纳公有协定。
  • 存量设施不批改固件逻辑

实用场景

泛化协定 SDK 使得网桥 Server 具备与物联网平台进行通信的能力。

  • 提供基于配置文件的动态配置管理能力。
  • 提供设施连贯治理能力。
  • 提供上行通信能力。
  • 提供上行通信能力。

接入流程  **

应用泛化协定 SDK,桥接设备与物联网平台的整体流程图,如下:

   开发实战  **

泛化 SDK 依赖
泛化协定仅反对 Java 开发语言,增加泛化协定 SDK 的我的项目 Maven 依赖,如下:

<dependency>
  <groupId>com.aliyun.openservices</groupId>
  <artifactId>iot-as-bridge-sdk-core</artifactId>
  <version>2.1.3</version>
</dependency>

初始化 SDK 
您须要创立一个 BridgeBootstrap 对象实例,并调用 bootstrap 办法。泛化协定 SDK 初始化工作实现后,读取网桥信息,并向云端发动网桥设施上线申请等。
此外,能够在调用 bootstrap 办法的同时,向泛化协定 SDK 注册一个 DownlinkChannelHandler 回调,用于接管云端上行音讯。

BridgeBootstrap bridgeBootstrap = new BridgeBootstrap();
bridgeBootstrap.bootstrap(new DownlinkChannelHandler() {
    @Override
    public boolean pushToDevice(Session session, String topic, byte[] payload) {
        // 云端上行控制指令
        String content = new String(bytes);
        log.info("Get DownLink message, session:{}, {}, {}", session, topic, content);
        return true;
    }
    @Override
    public boolean broadcast(String topic, byte[] payload) {return false;}
});

配置泛化网桥身份  
创立网桥产品

注册网桥设施

网桥配置默认应用配置文件形式。默认从 Java 工程默认资源文件门路(个别是 src/main/resources/)下的 application.conf 中读取配置文件。

# Server endpoint
http2Endpoint = "https:// 你的 ProductKey.iot-as-http2.cn-shanghai.aliyuncs.com:443"
authEndpoint = "https://iot-auth.cn-shanghai.aliyuncs.com/auth/bridge"
# Gateway device info, productKey & deviceName & deviceSecret
productKey = ${bridge-ProductKey}
deviceName = ${bridge-DeviceName}
deviceSecret = ${bridge-DeviceSecret}

# subDeviceConnectMode = 3,即子设施在线状态和网关是否在线无关
subDeviceConnectMode = 3

配置网桥下设施身份  
创立子设施产品

注册设施,获取身份三元组

配置设施原始身份标识符和设施证书信息的映射关系。默认应用配置文件形式,默认从 Java 工程的默认资源文件门路(个别是 src/main/resources/)下的 devices.conf 中读取配置文件

${device-original-Identity} {productKey : ${device-ProductKey}
  deviceName : ${device-DeviceName}
  deviceSecret : ${device-DeviceSceret}
  
}

设施上线  
设施上线时,须要传 Session。上行音讯回调时,会把 Session 回调给网桥。Session 中蕴含设施的原始身份标识符字段,以便网桥判断音讯属于哪个设施。

UplinkChannelHandler uplinkHandler = new UplinkChannelHandler();
// 创立 Session
Object channel = new Object();
Session session = Session.newInstance(originalIdentity, channel);
// 设施上线
boolean success = uplinkHandler.doOnline(session, originalIdentity);
if (success) {// 设施上线胜利,网桥承受后续设施通信申请。} else {// 设施上线失败,网桥能够回绝后续设施通信申请,如断开连接。}

设施通过网桥上报数据  
网桥应用泛化协定 SDK 代理设施上报音讯,代码如下:

// originalIdentity 设施上报数据
DeviceIdentity deviceIdentity = ConfigFactory.getDeviceConfigManager().getDeviceIdentity(originalIdentity);
ProtocolMessage protocolMessage = new ProtocolMessage();
protocolMessage.setPayload(payload);
protocolMessage.setQos(0);
protocolMessage.setTopic(String.format(TOPIC_TEMPLATE_USER_DEFINE, deviceIdentity.getProductKey(), deviceIdentity.getDeviceName()));
// 网桥代理上报
uplinkChannelHandler.doPublishAsync(originalIdentity, protocolMessage);

设施接管云端指令
云端能够调用 Pub API 给设施下发控制指令,网桥监听和解决云端音讯的代码如下:

private static ExecutorService executorService  = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(),
    Runtime.getRuntime().availableProcessors() * 2,
    60, TimeUnit.SECONDS,
    new LinkedBlockingQueue<>(1000),
    new ThreadFactoryBuilder().setDaemon(true).setNameFormat("bridge-downlink-handle-%d").build(),
    new ThreadPoolExecutor.AbortPolicy());
public static void main(String args[]) {
    //Use application.conf & devices.conf by default
    bridgeBootstrap = new BridgeBootstrap();
    bridgeBootstrap.bootstrap(new DownlinkChannelHandler() {
        @Override
        public boolean pushToDevice(Session session, String topic, byte[] payload) {
            //get message from cloud
            //get downlink message from cloud
            executorService.submit(() -> handleDownLinkMessage(session, topic, payload));
            return true;
        }
        @Override
        public boolean broadcast(String s, byte[] bytes) {return false;}
    });
}
private static void handleDownLinkMessage(Session session, String topic, byte[] payload) {String content = new String(payload);
    log.info("Get DownLink message, session:{}, topic:{}, content:{}", session, topic, content);
    Object channel = session.getChannel();
    String originalIdentity = session.getOriginalIdentity();
    //for example, you can send the message to device via channel, it depends on you specific server implementation
}

设施下线
当设施从网桥断开后,能够调用下线接口,告知云端:

upLinkHandler.doOffline(originalIdentity);

【往期回顾】
1、39 张 IoT 传感器工作原理 GIF 图汇总
[2、IoT 设施发送 MQTT 申请的波折经验
](https://mp.weixin.qq.com/s?sp…)
[3、20 元体 Arduino 环境监测仪开发
](https://mp.weixin.qq.com/s?sp…)
[4、智能手持测温枪开发实际
](https://mp.weixin.qq.com/s?sp…)
[5、JMeter 压测 MQTT 服务性能实战
](https://mp.weixin.qq.com/s?sp…)

物联网平台产品介绍详情:https://www.aliyun.com/produc…

             阿里云物联网平台客户交换群
正文完
 0