场景:办公室中已经布点的温湿度计设备上报数据到钉钉群机器人。
思路:温湿度计通过MQTT协议连接到IoT套件,规则引擎针对数据上报Topic配置转发到函数计算(FunctionComputer)中编写好的函数pushData2DingTalk,函数Nodejs脚本处理数据,post到钉钉群机器人的Webhook,配置了温湿度机器人的钉钉群组即可收到消息。
$ node thermometer.js
1.3 规则引擎SQL
rules-engine-fc.sql
payload :{temperature: 18,humidity: 63}
1.4 函数计算脚本
fc-post2Dingtalk.js
阿里云IoT物联网套件控制台
创建高级版产品,基本信息
高级版产品,定义物模型的 属性,服务,事件
产品的消息通信-Topic类列表
//属性上报的Topic
const topic = `/sys/${options.productKey}/${options.deviceName}/thing/event/property/post`;
setInterval(function() {
//发布数据到topic
client.publish(topic, getPostData());
}, 5 * 1000);
//服务订阅的Topic
const serviceTopic = `/sys/${options.productKey}/${options.deviceName}/thing/service/setTemperature`;
client.subscribe(serviceTopic)
client.on('message', function(topic, message) {
if (serviceTopic === topic) {
console.log("setTemperature " + JSON.parse(message).params.temperature)
}
})
//事件上报的Topic
const eventTopic = `/sys/${options.productKey}/${options.deviceName}/thing/event/alarmCleaning/post`;
setInterval(function() {
//事件上报
client.publish(eventTopic, getEventData());
}, 20 * 1000);
设备的运行状态