概述
IoT Studio服务开发是一个物联网业务逻辑的开发工具,通过编排服务节点的方式快速完成简单的物联网业务逻辑的设计。本文主要使用:定时触发、设备和钉钉机器人节点实现对灯泡的定时控制,并将控制后的结果发送给钉钉机器人。本文以官方文档:定时关灯为基础,针对文档中缺少设备属性上报,钉钉机器人配置参考较老等问题,逐一介绍整个链路的完整实现。
Step By Step
1、创建产品和设备,使用Code完成属性上报
- 1.1 创建产品和设备
- 1.2 使用开源Java MQTT SDK上报属性,参考链接:基于开源JAVA MQTT Client连接阿里云IoT
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 IoTDemoPubSubDemo {
public static String productKey = "a1Agp******";
public static String deviceName = "LightDevice1";
public static String deviceSecret = "RLHDiBljxC7YQE7opM**************";
public static String regionId = "cn-shanghai";
// 物模型-属性上报topic
private static String pubTopic = "/sys/" + productKey + "/" + deviceName + "/thing/event/property/post";
// 物模型-属性订阅topic
private static String subTopic = "/sys/" + productKey + "/" + deviceName + "/thing/service/property/set";
private static MqttClient mqttClient;
public static void main(String [] args){
initAliyunIoTClient();
postDeviceProperties();
try {
mqttClient.unsubscribe(pubTopic); //取消订阅
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
// 订阅到消息后即刻汇报
System.out.println("收到云端下发指令后向平台上行消息");
mqttClient.publish(pubTopic, new MqttMessage((new String(mqttMessage.getPayload())).getBytes("utf-8")));
}
@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(true);
// 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\":{\"LightSwitch\":1}}";
MqttMessage message = new MqttMessage(payloadJson.getBytes("utf-8"));
message.setQos(0);
mqttClient.publish(pubTopic, message);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
1.3 设备状态查看
2、IoT Studio中进行产品和设备绑定,完整服务开发模块搭建
- 2.1 绑定产品和设备
2.2 创建服务开发
2.3 钉钉机器人Webhook获取
- 2.4 部署启动定时任务
3 测试效果
- 3.1 钉钉群消息
- 3.2 程序运行日志
Sub message
Topic : /sys/a1Agp******/LightDevice1/thing/service/property/set
{"method":"thing.service.property.set","id":"50505571","params":{"LightSwitch":0},"version":"1.0.0"}
收到云端下发指令后向平台上行消息