Telemetry (Dữ liệu cảm biến)

Thu thập, lưu trữ và truy vấn dữ liệu time-series từ thiết bị IoT.

Telemetry (Dữ liệu cảm biến)

Telemetry là dữ liệu time-series được thiết bị gửi lên theo thời gian: nhiệt độ, độ ẩm, điện áp, GPS... VieLang IoT lưu trữ toàn bộ lịch sử và cho phép truy vấn, tổng hợp theo nhiều cách.

Định dạng dữ liệu

Không có timestamp (server tự gán)

{"temperature": 25.5, "humidity": 68, "voltage": 3.7}

Có timestamp (milliseconds)

{ "ts": 1735000000000, "values": { "temperature": 25.5, "humidity": 68 } }

Mảng nhiều điểm cùng lúc

[ {"ts": 1735000000000, "values": {"temperature": 25.5}}, {"ts": 1735000005000, "values": {"temperature": 25.8}}, {"ts": 1735000010000, "values": {"temperature": 26.1}} ]

Lưu trữ

VieLang IoT hỗ trợ nhiều backend lưu trữ:

BackendKhuyến nghị dùng khi
PostgreSQL< 5,000 điểm/giây, đơn giản
Cassandra> 5,000 điểm/giây, phân tán
TimescaleDBCần SQL + time-series tốc độ cao

Cấu hình trong vielang.toml:

[timeseries] backend = "postgres" # "postgres" | "cassandra" | "timescale"

Data Retention (Giữ dữ liệu)

Cấu hình TTL (time-to-live) ở nhiều cấp độ (ưu tiên từ cao xuống thấp):

  1. Rule node — TTL ghi trong message metadata
  2. Tenant attributeTTL attribute của tenant
  3. Tenant profile — Cấu hình trong tenant profile
  4. System config — Cấu hình global trong vielang.toml
[timeseries] default_ttl_days = 365 # 1 năm

REST API

Lấy keys hiện có

GET /api/plugins/telemetry/DEVICE/{deviceId}/keys/timeseries Authorization: Bearer {JWT} # Response: ["temperature", "humidity", "voltage", "battery"]

Lấy giá trị mới nhất

GET /api/plugins/telemetry/DEVICE/{deviceId}/values/timeseries ?keys=temperature,humidity Authorization: Bearer {JWT} # Response: { "temperature": [{"ts": 1735000000000, "value": "25.5"}], "humidity": [{"ts": 1735000000000, "value": "68"}] }

Lấy lịch sử (time range)

GET /api/plugins/telemetry/DEVICE/{deviceId}/values/timeseries ?keys=temperature &startTs=1700000000000 &endTs=1735000000000 &interval=3600000 &agg=AVG &limit=720 Authorization: Bearer {JWT}

Các tham số:

Tham sốMô tảVí dụ
keysDanh sách keys, cách nhau bởi ,temperature,humidity
startTsThời gian bắt đầu (ms)1700000000000
endTsThời gian kết thúc (ms)1735000000000
intervalBucket size (ms)3600000 (1 giờ)
aggHàm tổng hợpAVG, MIN, MAX, SUM, COUNT, NONE
limitSố điểm tối đa1000
orderByThứ tựASC, DESC

Hàm tổng hợp (Aggregation)

HàmMô tả
NONERaw data, không tổng hợp
AVGTrung bình cộng
MINGiá trị nhỏ nhất
MAXGiá trị lớn nhất
SUMTổng
COUNTĐếm số điểm

WebSocket — Dữ liệu real-time

Kết nối WebSocket để nhận cập nhật ngay khi thiết bị gửi dữ liệu:

const ws = new WebSocket("ws://localhost:8080/api/ws/plugins/telemetry") // Authenticate sau khi kết nối ws.onopen = () => { ws.send(JSON.stringify({ authCmd: { cmdId: 0, token: "YOUR_JWT_TOKEN" } })) } // Subscribe telemetry real-time ws.onmessage = (event) => { const msg = JSON.parse(event.data) // Sau khi auth thành công, gửi subscription if (msg.authCmd) { ws.send(JSON.stringify({ tsSubCmds: [{ entityType: "DEVICE", entityId: "DEVICE_UUID", scope: "LATEST_TELEMETRY", cmdId: 1 }] })) } else { // Nhận data update console.log("Telemetry update:", msg.data) } }

Xóa telemetry

# Xóa keys cụ thể (giữ lại dữ liệu cũ) DELETE /api/plugins/telemetry/DEVICE/{deviceId}/timeseries/delete ?keys=temperature,humidity &startTs=1700000000000 &endTs=1710000000000 Authorization: Bearer {JWT} # Xóa toàn bộ lịch sử của key DELETE /api/plugins/telemetry/DEVICE/{deviceId}/timeseries/delete ?keys=temperature &deleteAllDataForKeys=true Authorization: Bearer {JWT}