12/5/2023
消息规则 在 ThingsCloud 中发挥着重要的作用,它可以按照设定的条件和操作,对设备和云平台之间传递的消息进行各种计算、处理、转发等操作。
在消息规则中,可编程的云函数扮演着重要的作用。
通俗的说,云函数是运行在云平台上的一段由您编写的函数代码,可以解析和生成设备协议,以及实现设备联动,总之,云函数无所不能。
现在,云函数已支持在线测试,您可以选择模拟的设备,并自定义云函数的输入参数,一键测试运行云函数,立即获得云函数的返回结果。
云函数在线测试功能有助于:
接下来我们通过几个例子,来帮助您了解云函数在线测试的使用方法。
温度湿度指数(Temperature-Humidity Index, THI)通常用于以下领域:
通过以下的属性上报预处理规则的云函数,便可以计算生成 THI,如下图:
module.exports = function (report_attributes) { /** * report_attributes: 上报的属性对象,同时作为函数返回值。函数中可更新属性对象。 */ if (report_attributes.temperature !== undefined && report_attributes.humidity !== undefined) { var t = report_attributes.temperature; var h = report_attributes.humidity; report_attributes.THI = (1.8*t+32)-(0.55-0.55*h*0.01)*(1.8*t-26); } return report_attributes;}
为了验证云函数的准确性,我们模拟设备上报的温度和湿度属性值,运行测试,在右侧可以看到经过预处理后的上报属性,多了一个计算生成的 THI 属性。
该规则示例的详细介绍请浏览 通过温湿度自动生成温热指数(THI)
设备通过模拟量采集,得到电流信号(4-20mA),利用厂家提供的信号计算规则,在 ThingsCloud 属性上报预处理规则的云函数中,即可计算流速,以及最大流速和最小流速。如下图:
module.exports = function (report_attributes) { /** * report_attributes: 上报的属性对象,同时作为函数返回值。函数中可更新属性对象。 */ if (report_attributes.analog_value !== undefined) { // 得到电流信号 mA var analog_ma = report_attributes.analog_value / 1000; report_attributes.flow_speed = 14 + (analog_ma - 4) / (20 - 4) * (140 - 14); // 调用 ThingsCloud 内置函数,读取设备当前属性,也就是上次上报后的数据 const attrs = Cloud.getCurrentAttributes(); // 记录本次上报的年月日 report_attributes.report_date = Cloud.Utils.dateFormat("YYYYMMDD"); if (report_attributes.report_date !== attrs.report_date) { // 如果是新的一天首次上报 report_attributes.min_speed = report_attributes.flow_speed; report_attributes.max_speed = report_attributes.flow_speed; } else { report_attributes.min_speed = Math.min(attrs.min_speed, report_attributes.flow_speed); report_attributes.max_speed = Math.max(attrs.max_speed, report_attributes.flow_speed); } } return report_attributes;}
接下来运行在线测试,我们模拟输入一个电流信号的属性上报,运行后在右侧可以看到生成的当前流速、最大值、最小值,以及在云函数中追加的一个当前日期。
客户的星纵 LoRaWAN 设备 EM500 CO2 传感器接入 ThingsCloud,通过网关转发已上报原始的 base64
编码数据,通过以下规则云函数,对数据进行解码。
module.exports = function (report_attributes) { /** * report_attributes: 上报的属性对象,同时作为函数返回值。函数中可更新属性对象。 */ if (report_attributes.data !== undefined) { // 利用内置函数,将 LoRaWAN 设备上报的 base64 格式的 data 转为二进制数据。 var buffer = Cloud.Utils.base64ToBuffer(report_attributes.data); // 转成 hex 格式,仅做调试记录 report_attributes.data_hex = buffer.toString('hex'); // 根据设备协议解码 let i = 0; if (buffer[i] == 0x01 && buffer[i+1] == 0x75) { report_attributes.battery = buffer.readInt8(i+2); i += 3; } if (buffer[i] == 0x03 && buffer[i+1] == 0x67) { report_attributes.temperature = buffer.readInt16LE(i+2) / 10; i += 4; } if (buffer[i] == 0x04 && buffer[i+1] == 0x68) { report_attributes.humidity = buffer.readInt8(i+2) / 2; i += 3; } if (buffer[i] == 0x05 && buffer[i+1] == 0x7d) { report_attributes.co2 = buffer.readUInt16LE(i+2); i += 4; } if (buffer[i] == 0x06 && buffer[i+1] == 0x73) { report_attributes.pressure = buffer.readInt16LE(i+2) / 10; i += 4; } } return report_attributes;}
通过在线测试云函数,对原始数据进行解码测试,右侧成功获得解码后的上报属性 JSON。
该规则示例的详细介绍请浏览 星纵 LoRaWAN EM500 CO2 传感器上报数据解析
在某客户的仓库自动化场景中,利用行程开关采集多处限位状态,通过限位状态的开关量上报,来自动下发继电器通断状态。
通过以下的云函数示例,几行非常简单的逻辑代码,便可以实现当开关量 di1
上报状态时,云平台对继电器 relay1
下发同样的状态指令。
利用云函数测试,左侧我们模拟上报的 di1
,在右侧即可看到平台下发的 relay1
。
客户使用某 4G 模块自带继电器,需要通过 AT 指令来控制继电器通断。
通过属性下发规则的云函数,自动生成 AT 指令的自定义数据,下发到设备。这样一来,可以在控制台、可视化看板、用户 App 中,通过拨动开关来直接控制继电器。
规则云函数如下图:
利用云函数测试功能,可以轻松验证生成的 AT 指令是否符合预期,如下图:
该规则示例的详细介绍请浏览 将开关量属性下发自动转化为模块 AT 指令
另一个示例来自 ESPHome 这个基于 ESP32 的开源固件接入 ThingsCloud,通过平台的属性下发规则云函数,可以将开关量下发,转变为 ESPHome 支持的指令内容,如下图:
module.exports = function (push_attributes) { /** * push_attributes: 下发的属性对象,作为函数参数传入。 * data: 构造下发的自定义数据对象,作为函数返回值,下发到硬件。 */ if (push_attributes.living_room_fan !== undefined) { var data = { params: { type: "switch", name: "living_room_fan" }, type: "text", msg: push_attributes.living_room_fan ? "ON" : "OFF" } } return data;}
运行云函数测试,右侧生成的下发自定义数据,结果符合预期。
实际应用中,对于设备 Modbus 协议解析,可以使用 ThingsCloud 内置的专用方式,参考 [保姆级教程] RS485/Modbus 设备通过 DTU 透传接入 ThingsCloud。
这里通过规则云函数解析 Modbus,仅用于演示云函数的在线测试。
在示例中,DTU 设备连接了多个 RS485 从机传感器,在自定义数据上报规则中,通过代码对 Modbus RTU 的 HEX 消息进行逐个字节的解析,生成相应的属性。
module.exports = function (topic, data) { /** * topic: 上报的自定义 topic * data: 上报的自定义数据,二进制 Buffer格式 * attributes: 自定义上报数据解析生成的设备属性,作为函数返回值。 */ var attributes = {}; if (data[0] == 0x01) { attributes.temperature_sq = data.readUInt16BE(3) / 10; attributes.temperature = data.readUInt16BE(5) / 10; attributes.humidity = data.readUInt16BE(7) / 10; attributes.temperature_ld = data.readUInt16BE(9) / 10; } else if (data[0] == 0x02) { attributes.co2 = data.readUInt16BE(3); } else if (data[0] == 0x03) { attributes.light = data.readUInt16BE(5); } return attributes;}
在云函数测试时,我们在左侧模拟设备上报的 Modbus RTU 消息,右侧立即生成解析后的属性上报 JSON,解析成功。
客户在项目中需要用到车载位置数据采集模块,使用银尔达 Air780EG 模块上报 GPS/LBS 位置数据,通过 ThingsCloud 消息规则,可以快速将原始数据解析生成设备位置属性,同时利用消息规则完成 GPS 位置偏移校准,利用设备地图轻松管理设备。
消息规则云函数如下图:
module.exports = function (identifier, data) { /** * 参数: * identifier: 上报的自定义数据流标识符 * data: 上报的自定义数据,二进制 Buffer格式 * 返回值: * attributes: 生成设备属性 */ var attributes = {}; const text = data.toString(); if (text.substr(0, 4) == 'LBS=') { const tmp = text.split('='); if (tmp.length == 2) { const loc = tmp[1].split('_'); if (loc.length == 2) { attributes.LBS = { lng: loc[0] /1, lat: loc[1] /1, } } } } // GPS=E:117.1390,N:34.66553 if (text.substr(0, 4) == 'GPS=') { const tmp = text.split('='); if (tmp.length == 2) { const loc = tmp[1].split(','); if (loc.length == 2) { const lng = loc[0].substr(2) /1; const lat = loc[1].substr(2) /1; attributes.GPS_loc = { lng: lng, lat: lat, } } } } return attributes;}
在设备调试消息中,我们可以复制设备实际上报的原始消息,作为测试的模拟输入,来观察规则云函数的运行结果,一切符合预期。
该规则示例的详细介绍请浏览 银尔达 Air780EG 模块上报 GPS/LBS 位置数据解析
关于规则云函数的在线测试功能,您掌握了吗?期待您的探索和反馈。
如果您对任何功能有疑问,或者希望了解如何将它们融入您的数字化项目中,请随时联系我们。
ThingsCloud 是新一代物联网设备统一接入平台,帮助企业在极短的时间内搭建个性化的物联网平台和应用,并适应不断变化的发展需求。目前广泛应用于制造、电力、能源、环境、农业、楼宇、家居、教育、交通、物流、自动化等领域。
ThingsCloud 可接入各类网关,传感器、执行器、控制器、通信模组、智能硬件等,实现数据采集、远程控制,数据分析、告警通知、智能联动。还可以零代码生成项目应用 SaaS 和用户应用 App,并开放 API 和实时消息,便于业务系统集成和扩展开发。
通过使用 ThingsCloud,企业可以大大缩短搭建物联网系统的时间,节省软件开发费用,降低定制开发的风险,快速落地数字化和智能化项目。我们的客户遍布各行业,包括中国石化、中国铁塔、中国燃气、吉林大学、北控水务、ACE、中国民航大学、西安交通大学、精量电子、大秦铁路、宁波水利局等。
5000+ 企业在使用 ThingsCloud