概述
物联网平台支持广播通信,即向指定产品下的全量在线设备发送消息。设备无需订阅广播Topic,即可收到服务器发送的广播消息;也可以根据需求订阅带有:identifer自定义字符串的Topic,实现设备端广播消息接收。
Step By Step
1、Device Code
import com.alibaba.taro.AliyunIoTSignUtil;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import java.util.HashMap;
import java.util.Map;
public class BroadCastDemo {
// 设备三元组信息
public static String productKey = "a1o9N******";
public static String deviceName = "device1";
public static String deviceSecret = "7Ukl6Ka80nIEXhUwCmw7XrUC********";
public static String regionId = "cn-shanghai";
// 自定义topic,在产品Topic列表位置定义
private static String subTopic = "/broadcast/a1o9N******/group2"; ///broadcast/a1o9N******/${identifer} 广播topic,identifer用户自定义字符串
private static MqttClient mqttClient;
public static void main(String[] args) {
initAliyunIoTClient();
try {
mqttClient.subscribe(subTopic); // 订阅Topic
} catch (MqttException e) {
System.out.println("error:" + e.getMessage());
e.printStackTrace();
}
// 设置订阅监听
mqttClient.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable throwable) {
System.out.println("connection Lost");
}
@Override
public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
System.out.println("Sub message");
System.out.println("Topic : " + s);
System.out.println(new String(mqttMessage.getPayload())); //打印输出消息payLoad
}
@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
}
});
}
/**
* 初始化 Client 对象
*/
private static void initAliyunIoTClient() {
try {
// 构造连接需要的参数
String clientId = "java" + System.currentTimeMillis();
Map<String, String> params = new HashMap<>(16);
params.put("productKey", productKey);
params.put("deviceName", deviceName);
params.put("clientId", clientId);
String timestamp = String.valueOf(System.currentTimeMillis());
params.put("timestamp", timestamp);
// cn-shanghai
String targetServer = "tcp://" + productKey + ".iot-as-mqtt." + regionId + ".aliyuncs.com:1883";
String mqttclientId = clientId + "|securemode=3,signmethod=hmacsha1,timestamp=" + timestamp + "|";
String mqttUsername = deviceName + "&" + productKey;
String mqttPassword = AliyunIoTSignUtil.sign(params, deviceSecret, "hmacsha1");
connectMqtt(targetServer, mqttclientId, mqttUsername, mqttPassword);
} catch (Exception e) {
System.out.println("initAliyunIoTClient error " + e.getMessage());
}
}
public static void connectMqtt(String url, String clientId, String mqttUsername, String mqttPassword) throws Exception {
MemoryPersistence persistence = new MemoryPersistence();
mqttClient = new MqttClient(url, clientId, persistence);
MqttConnectOptions connOpts = new MqttConnectOptions();
// MQTT 3.1.1
connOpts.setMqttVersion(4);
connOpts.setAutomaticReconnect(false);
connOpts.setConnectionTimeout(10);
// connOpts.setCleanSession(true);
connOpts.setCleanSession(false);
connOpts.setUserName(mqttUsername);
connOpts.setPassword(mqttPassword.toCharArray());
connOpts.setKeepAliveInterval(60);
mqttClient.connect(connOpts);
}
}
2、 PubBroadcast 接口调用
3、设备端测试情况
4、PubBroadcast API 参数:TopicFullName 解释
可选参数:
- 不赋值,表示推送消息到指定ProductKey的全量在线设备。设备端收到的广播Topic格式为/sys/${productKey}/${deviceName}/broadcast/request/${MessageId},其中MessageId由物联网平台生成。
- 赋值,表示推送消息到指定ProductKey的已订阅广播Topic的在线设备。传入要接收广播消息的Topic全称,格式为:/broadcast/${productKey}/自定义字段。其中,${productKey}是要接收广播消息的具体产品ProductKey;自定义字段中您可以指定任意字段
注意:
- 广播Topic是在设备开发时编码定义的,无需控制台创建。
- 一个广播Topic最多可被1,000个设备订阅。如果您的设备超过数量限制,您可以对设备进行分组。例如,如果您有5,000个设备,您可以将设备按每组1,000个,而分成5组。您需要分5次调用广播Topic,自定义字段分别设置为group1/2/3/4/5,然后让每组设备分别订阅各自分组的广播Topic。