Files
youlai-boot/docs/sql/device_heartbeat.sql
tongtongstudio 4500f92b15 feat(docs): 添加设备心跳表SQL文档
新增TDengine超级表device_heartbeat的建表语句及EMQX规则引擎配置示例。
2026-08-11 21:25:13 +08:00

32 lines
1.3 KiB
SQL
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- 1. 创建数据库 mqtt如果不存在
-- 建议指定时间精度为 ms毫秒与 EMQX 规则中的 now_timestamp('millisecond') 匹配
CREATE DATABASE IF NOT EXISTS mqtt PRECISION 'ms' KEEP 3650;
-- 2. 切换到 mqtt 数据库
USE mqtt;
-- 3. 创建超级表 device_heartbeat
CREATE STABLE IF NOT EXISTS device_heartbeat (
ts TIMESTAMP, -- 必填,时间戳 (毫秒级)8字节
online BOOL, -- 设备状态true=online, false=offline1字节
ip VARCHAR(15), -- IPv4 地址最多15字符1字节/字符 + 1字节长度
rssi SMALLINT, -- 信号强度 (-128 到 127/32767)2字节
screen_on BOOL -- 屏幕开启状态 (true / false) 1字节
) TAGS (
sn VARCHAR(32) -- 设备SN码可变长字符串比NCHAR省一半以上空间
);
-- 4.emqx rules
SELECT
payload.sn AS sn,
payload.status AS status,
payload.ip AS ip,
payload.rssi AS rssi,
payload.screenOn AS screen_on,
now_timestamp('millisecond') AS ts
FROM "ttstd/device/status"
WHERE is_not_null(payload.sn)
-- 5.emqx action 没有数据打开 "未定义变量作为 NULL" 开关
INSERT INTO mqtt.`hb_${sn}` USING mqtt.device_heartbeat TAGS ('${sn}')
VALUES (${ts}, '${status}', '${ip}', ${rssi}, ${screen_on})