Step By Step
1、创建产品和设备
2、使用SDK模拟设备上报消息
3、添加孪生体
4、添加孪生体子节点
5、添加数字映射
6、查看效果
一、创建产品和设备
- 1.1 控制台购买企业版实例(目前只有企业版实例支持试用数字孪生功能)
- 1.2 实例下面创建产品
- 1.3 为产品定义物模型
- 1.4 添加设备
二、使用SDK模拟设备上报消息
- 2.1 pom.xml
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.1.0</version>
</dependency>
- 2.2 AliyunIoTSignUtil.java
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Arrays;
import java.util.Map;
/**
* AliyunIoTSignUtil
*/
public class AliyunIoTSignUtil {
public static String sign(Map<String, String> params, String deviceSecret, String signMethod) {
//将参数Key按字典顺序排序
String[] sortedKeys = params.keySet().toArray(new String[] {});
Arrays.sort(sortedKeys);
//生成规范化请求字符串
StringBuilder canonicalizedQueryString = new StringBuilder();
for (String key : sortedKeys) {
if ("sign".equalsIgnoreCase(key)) {
continue;
}
canonicalizedQueryString.append(key).append(params.get(key));
}
try {
String key = deviceSecret;
return encryptHMAC(signMethod,canonicalizedQueryString.toString(), key);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* HMACSHA1加密
*
*/
public static String encryptHMAC(String signMethod,String content, String key) throws Exception {
SecretKey secretKey = new SecretKeySpec(key.getBytes("utf-8"), signMethod);
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
mac.init(secretKey);
byte[] data = mac.doFinal(content.getBytes("utf-8"));
return bytesToHexString(data);
}
public static final String bytesToHexString(byte[] bArray) {
StringBuffer sb = new StringBuffer(bArray.length);
String sTemp;
for (int i = 0; i < bArray.length; i++) {
sTemp = Integer.toHexString(0xFF & bArray[i]);
if (sTemp.length() < 2) {
sb.append(0);
}
sb.append(sTemp.toUpperCase());
}
return sb.toString();
}
}
- 2.3 DeviceCode Sample
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import java.util.HashMap;
import java.util.Map;
public class Device1 {
// 设备三元组信息
public static String productKey = "gqu********";
public static String deviceName = "device1";
public static String deviceSecret = "26b39f328********";
public static String regionId = "cn-shanghai";
// 实例id
public static String instanceId = "iot-06******";
// 物模型-属性上报topic
private static String pubTopic = "/sys/" + productKey + "/" + deviceName + "/thing/event/property/post";
// 物模型-属性响应topic
private static String subTopic = "/sys/" + productKey + "/" + deviceName + "/thing/event/property/post_reply";
private static MqttClient mqttClient;
public static void main(String [] args) throws InterruptedException {
// 初始化Client
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) {
}
});
// 属性上报
for (int i = 0; i < 10000; i++) {
postDeviceProperties();
Thread.sleep(1000);
System.out.println("-------------------" + i);
}
Thread.sleep(10000*60);
}
/**
* 初始化 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);
String targetServer = "ssl://"+instanceId+".mqtt.iothub.aliyuncs.com:443";
String mqttclientId = clientId + "|securemode=2,signmethod=hmacsha256,timestamp=" + timestamp + "|";
String mqttUsername = deviceName + "&" + productKey;
String mqttPassword = AliyunIoTSignUtil.sign(params, deviceSecret, "hmacsha256");
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);
}
/**
* 汇报属性
*/
private static void postDeviceProperties() {
try {
//上报数据
//高级版 物模型-属性上报payload
System.out.println("上报属性值");
String payloadJson = "{\"params\":{\"temperature\":21}}";
MqttMessage message = new MqttMessage(payloadJson.getBytes("utf-8"));
message.setQos(1);
mqttClient.publish(pubTopic, message);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
- 2.4 运行结果查看
三、添加孪生体
四、添加孪生体子节点
- 4.1 编辑孪生体
- 4.2 添加子节点
- 4.3 编辑子节点
- 4.4 保存模板
- 4.5 拖拽模板添加子节点(也可以手动重新添加一个)
- 4.6 父节点编辑物模型
- 4.7 编辑父节点孪生规则
五、添加数字映射
- 5.1 输入脚本
//获取设备上报的消息内容。
var payload = payload("json");
//定义Map类型数据,存储键值对数据。
var data = {};
//获取设备名称。
var deviceName = topic(2);
//获取设备上报的温度数据。
var t = payload.items.temperature.value;
//存储不同设备上报的数据。
if (deviceName == "device1") {
data["temp1"] = t;
}if (deviceName == "device2") {
data["temp2"] = t;
}
return data;
输入脚本实际是通过规则引擎流转数据的,所以在提取物模型数据的时候,针对自定义topic和系统topic,
//获取设备上报的温度数据。
var t = payload.items.temperature.value;
是会有差异的,具体提取方式可以参考链接
- 5.2 输出脚本
[
{
"key": "temp1",
"iotId": "<替换为自己的子节点id>",
"identifier": "temperature"
},
{
"key": "temp2",
"iotId": "<替换为自己的子节点id>",
"identifier": "temperature"
}
]
六、查看效果
- 6.1 平台物模型数据显示
- 6.2 孪生体显示
- 6.3 日志查看(常见问题排查位置)