1 准备工作
设备端可以使用C,C++,Android等语言开发,后台服务使用Java SpringBoot构建;消息服务使用阿里云物联网平台;下面主要介绍企业服务器和物联网平台的通信。
步骤1:通过HTTP/2协议订阅设备端消息
步骤2:通过提供的SDK api接口发布消息,给设备端订阅。
![](https://img.haomeiwen.com/i18756356/1fdec06ae9f72130.png)
首先申请阿里云账户,在阿里云平台新建产品和设备获取三元组信息
1.1 新建产品和设备
![](https://img.haomeiwen.com/i18756356/4fe855519ffc9f50.png)
1.2设备三元组
![](https://img.haomeiwen.com/i18756356/8f45b57f3887d9b3.png)
1.3开启订阅服务
选择产品中的服务端订阅菜单,点击设置
![](https://img.haomeiwen.com/i18756356/83252580fad47ae3.png)
有三个选型,可以根据需要进行选择
![](https://img.haomeiwen.com/i18756356/61d31d01cc38ce88.png)
2 服务器端订阅
2.1 接入HTTP/2 SDK
请确保JDK版本为8
<dependency>
<groupId>com.aliyun.openservices</groupId>
<artifactId>iot-client-message</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>3.7.1</version>
</dependency>
2.2 消息订阅
通过阿里云账号AccessKey进行身份认证,建立SDK与物联网平台的连接,并订阅设备topic,接手设备端消息
logger.info("==========init AioT properties===========");
// endPoint: https://${uid}.iot-as-http2.${region}.aliyuncs.com
// uid是阿里云账户id egionId是物联网平台服务所在地域代码
String endPoint = "https://" + aiotConfig.getUserUid() + ".iot-as-http2." + aiotConfig.getRegionId() + ".aliyuncs.com";
// 连接配置
Profile profile = Profile.getAccessKeyProfile(endPoint, aiotConfig.getRegionId(), aiotConfig.getAccessKey(), aiotConfig.getAccessSecret());
// 构造客户端
MessageClient client = MessageClientFactory.messageClient(profile);
// 数据接收
client.connect(messageToken -> {
Message m = messageToken.getMessage();
return MessageCallback.Action.CommitSuccess;
});
MessageCallback messageCallback = new MessageCallback() {
@Override
public Action consume(MessageToken messageToken) {
Message message = messageToken.getMessage();
logger.info("receive : " + new String(message.getPayload()));
//订阅消息处理服务 payload数据体
aiotPublishService.pub(message);
return MessageCallback.Action.CommitSuccess;
}
};
//设备端消息订阅 topic
client.setMessageListener("/ProductKey/DeviceName/user/update ",messageCallback);
2.3 消息发布
物联网平台提供的云端SDK,通过提供的API发布消息
2.3.1 maven依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-iot</artifactId>
<version>6.11.0</version>
</dependency>
<!-- 公共依赖包,订阅消息也需要引入 -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>3.5.1</version>
</dependency>
2.3.1 服务端消息发布
/**
* 服务的消息发布
* @param mesg
* @throws IOException
*/
public static void pub(String mesg) {
MessagePack msgpack = new MessagePack();
byte[] values = msgpack.write(mesg);
PubRequest request = new PubRequest();
request.setProductKey("a12zcpFg9L2");
request.setMessageContent(Base64.encodeBase64String(values));
//消息发布topic
request.setTopicFullName("/a12zcpFg9L2/TestFace01/user/get");
request.setQos(0); //目前支持QoS0和QoS1
try {
//创建连接
DefaultAcsClient client = conn();
PubResponse response = client.getAcsResponse(request);
System.out.println(response.getSuccess());
System.out.println(response.getErrorMessage());
}catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
public static DefaultAcsClient conn() throws ClientException{
//通过阿里云accessKey管理获取
String accessKey = "accessKey";
String accessSecret = "accessSecret";
DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Iot", "iot.cn-shanghai.aliyuncs.com");
IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKey, accessSecret);
DefaultAcsClient client = new DefaultAcsClient(profile); //初始化SDK客户端
return client;
}
总结
阿里云物联网平台提供产品和设备管理,提供Java SDK方便开发。通过设备三元组(ProductKey,DeviceName,DeviceSecret),实现设备端和服务端的认证,通过消息订阅与发布,实现通信,协议使用MQTT和HTTP/2 ; 数据传输协议使用MessgePack,比Json更小,效率高,节约成本。
网友评论