OA0 = Omni AI 0
OA0 是一个探索 AI 的论坛
现在注册
已注册用户请  登录
OA0  ›  技能包  ›  thingsboard-skill:管理 ThingsBoard 设备、仪表板与遥测数据数据

thingsboard-skill:管理 ThingsBoard 设备、仪表板与遥测数据数据

 
  oa0 ·  2026-02-25 08:19:27 · 2 次点击  · 0 条评论  

名称: thingsboard
描述: 通过 ThingsBoard REST API 管理设备、仪表板、遥测数据和用户。
主页: https://thingsboard.io
元数据: {"clawdbot":{"emoji":"📊","requires":{"bins":["jq","curl"],"env":["TB_URL","TB_USERNAME","TB_PASSWORD"]}}}


ThingsBoard 技能

管理 ThingsBoard IoT 平台资源,包括设备、仪表板、遥测数据和用户。

环境配置

  1. credentials.json 中配置你的 ThingsBoard 服务器信息:
    json [ { "name": "Server Thingsboard", "url": "http://localhost:8080", "account": [ { "sysadmin": { "email": "sysadmin@thingsboard.org", "password": "sysadmin" } }, { "tenant": { "email": "tenant@thingsboard.org", "password": "tenant" } } ] } ]

  2. 设置环境变量:
    bash export TB_URL="http://localhost:8080" export TB_USERNAME="tenant@thingsboard.org" export TB_PASSWORD="tenant"

  3. 获取认证令牌:
    bash export TB_TOKEN=$(curl -s -X POST "$TB_URL/api/auth/login" \ -H "Content-Type: application/json" \ -d "{\"username\":\"$TB_USERNAME\",\"password\":\"$TB_PASSWORD\"}" | jq -r '.token')

使用说明

所有命令均使用 curl 与 ThingsBoard REST API 进行交互。

认证

登录并获取令牌:

curl -s -X POST "$TB_URL/api/auth/login" \
  -H "Content-Type: application/json" \
  -d "{\"username\":\"$TB_USERNAME\",\"password\":\"$TB_PASSWORD\"}" | jq -r '.token'

刷新令牌(当令牌过期时):

curl -s -X POST "$TB_URL/api/auth/token" \
  -H "Content-Type: application/json" \
  -d "{\"refreshToken\":\"$TB_REFRESH_TOKEN\"}" | jq -r '.token'

获取当前用户信息:

curl -s "$TB_URL/api/auth/user" \
  -H "X-Authorization: Bearer $TB_TOKEN" | jq

设备管理

列出所有租户设备:

curl -s "$TB_URL/api/tenant/devices?pageSize=100&page=0" \
  -H "X-Authorization: Bearer $TB_TOKEN" | jq '.data[] | {name, id: .id.id, type}'

根据 ID 获取设备信息:

curl -s "$TB_URL/api/device/{deviceId}" \
  -H "X-Authorization: Bearer $TB_TOKEN" | jq

获取设备凭证:

curl -s "$TB_URL/api/device/{deviceId}/credentials" \
  -H "X-Authorization: Bearer $TB_TOKEN" | jq

遥测数据与属性

获取遥测数据键:

curl -s "$TB_URL/api/plugins/telemetry/DEVICE/{deviceId}/keys/timeseries" \
  -H "X-Authorization: Bearer $TB_TOKEN" | jq

获取最新遥测数据:

curl -s "$TB_URL/api/plugins/telemetry/DEVICE/{deviceId}/values/timeseries?keys=temperature,humidity" \
  -H "X-Authorization: Bearer $TB_TOKEN" | jq

获取指定时间范围内的时序数据:

START_TS=$(($(date +%s)*1000 - 3600000))  # 1 小时前
END_TS=$(($(date +%s)*1000))              # 当前时间
curl -s "$TB_URL/api/plugins/telemetry/DEVICE/{deviceId}/values/timeseries?keys=temperature&startTs=$START_TS&endTs=$END_TS&limit=100" \
  -H "X-Authorization: Bearer $TB_TOKEN" | jq

获取属性键:

# 客户端作用域
curl -s "$TB_URL/api/plugins/telemetry/DEVICE/{deviceId}/keys/attributes/CLIENT_SCOPE" \
  -H "X-Authorization: Bearer $TB_TOKEN" | jq

# 共享作用域
curl -s "$TB_URL/api/plugins/telemetry/DEVICE/{deviceId}/keys/attributes/SHARED_SCOPE" \
  -H "X-Authorization: Bearer $TB_TOKEN" | jq

# 服务器作用域
curl -s "$TB_URL/api/plugins/telemetry/DEVICE/{deviceId}/keys/attributes/SERVER_SCOPE" \
  -H "X-Authorization: Bearer $TB_TOKEN" | jq

按作用域获取属性值:

curl -s "$TB_URL/api/plugins/telemetry/DEVICE/{deviceId}/values/attributes/CLIENT_SCOPE?keys=attribute1,attribute2" \
  -H "X-Authorization: Bearer $TB_TOKEN" | jq

保存设备属性:

curl -s -X POST "$TB_URL/api/plugins/telemetry/DEVICE/{deviceId}/attributes/SERVER_SCOPE" \
  -H "X-Authorization: Bearer $TB_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"attribute1":"value1","attribute2":"value2"}' | jq

删除时序数据键:

curl -s -X DELETE "$TB_URL/api/plugins/telemetry/DEVICE/{deviceId}/timeseries/delete?keys=oldKey1,oldKey2&deleteAllDataForKeys=true" \
  -H "X-Authorization: Bearer $TB_TOKEN"

仪表板管理

列出所有仪表板:

curl -s "$TB_URL/api/tenant/dashboards?pageSize=100&page=0" \
  -H "X-Authorization: Bearer $TB_TOKEN" | jq '.data[] | {name, id: .id.id}'

获取仪表板信息:

curl -s "$TB_URL/api/dashboard/{dashboardId}" \
  -H "X-Authorization: Bearer $TB_TOKEN" | jq

将仪表板设为公开:

curl -s -X POST "$TB_URL/api/customer/public/dashboard/{dashboardId}" \
  -H "X-Authorization: Bearer $TB_TOKEN" | jq

获取公开仪表板信息(无需认证):

curl -s "$TB_URL/api/dashboard/info/{publicDashboardId}" | jq

移除公开访问权限:

curl -s -X DELETE "$TB_URL/api/customer/public/dashboard/{dashboardId}" \
  -H "X-Authorization: Bearer $TB_TOKEN"

用户管理

列出租户用户:

curl -s "$TB_URL/api/tenant/users?pageSize=100&page=0" \
  -H "X-Authorization: Bearer $TB_TOKEN" | jq '.data[] | {email, firstName, lastName, id: .id.id}'

列出客户:

curl -s "$TB_URL/api/customers?pageSize=100&page=0" \
  -H "X-Authorization: Bearer $TB_TOKEN" | jq '.data[] | {title, id: .id.id}'

获取客户用户:

curl -s "$TB_URL/api/customer/{customerId}/users?pageSize=100&page=0" \
  -H "X-Authorization: Bearer $TB_TOKEN" | jq '.data[]'

资产管理

列出所有资产:

curl -s "$TB_URL/api/tenant/assets?pageSize=100&page=0" \
  -H "X-Authorization: Bearer $TB_TOKEN" | jq '.data[] | {name, type, id: .id.id}'

根据 ID 获取资产信息:

curl -s "$TB_URL/api/asset/{assetId}" \
  -H "X-Authorization: Bearer $TB_TOKEN" | jq

重要说明

  • 认证:JWT 令牌在配置的时间后过期(默认 2 小时)。收到 401 错误时请重新认证。
  • 设备/仪表板 ID:实体 ID 格式为 {entityType: "DEVICE", id: "uuid"}。在 API 调用中使用 id 字段。
  • 分页:大多数列表接口支持 pageSizepage 参数(默认每页 100 项,最多 1000 项)。
  • 属性作用域
  • CLIENT_SCOPE:客户端属性(由设备设置)
  • SHARED_SCOPE:服务器与设备共享
  • SERVER_SCOPE:仅服务器端(对设备不可见)
  • 时间戳startTsendTs 参数使用自纪元以来的毫秒数。
  • 速率限制:请检查你的 ThingsBoard 服务器配置以了解 API 速率限制。
  • HTTPS:生产环境请使用 HTTPS URL(例如 https://demo.thingsboard.io)。

完整示例

# 完整工作流:登录、列出设备、获取遥测数据
export TB_URL="http://localhost:8080"
export TB_USERNAME="tenant@thingsboard.org"
export TB_PASSWORD="tenant"

# 获取令牌
export TB_TOKEN=$(curl -s -X POST "$TB_URL/api/auth/login" \
  -H "Content-Type: application/json" \
  -d "{\"username\":\"$TB_USERNAME\",\"password\":\"$TB_PASSWORD\"}" | jq -r '.token')

# 列出所有设备
curl -s "$TB_URL/api/tenant/devices?pageSize=10&page=0" \
  -H "X-Authorization: Bearer $TB_TOKEN" | jq '.data[] | {name, type, id: .id.id}'

# 获取第一个设备的 ID
DEVICE_ID=$(curl -s "$TB_URL/api/tenant/devices?pageSize=1&page=0" \
  -H "X-Authorization: Bearer $TB_TOKEN" | jq -r '.data[0].id.id')

# 获取设备的遥测数据键
curl -s "$TB_URL/api/plugins/telemetry/DEVICE/$DEVICE_ID/keys/timeseries" \
  -H "X-Authorization: Bearer $TB_TOKEN" | jq

# 获取最新的遥测值
curl -s "$TB_URL/api/plugins/telemetry/DEVICE/$DEVICE_ID/values/timeseries?keys=temperature,humidity" \
  -H "X-Authorization: Bearer $TB_TOKEN" | jq

# 获取历史数据(最近一小时)
START_TS=$(($(date +%s)*1000 - 3600000))
END_TS=$(($(date +%s)*1000))
curl -s "$TB_URL/api/plugins/telemetry/DEVICE/$DEVICE_ID/values/timeseries?keys=temperature&startTs=$START_TS&endTs=$END_TS&limit=100" \
  -H "X-Authorization: Bearer $TB_TOKEN" | jq

# 列出仪表板并将第一个设为公开
DASHBOARD_ID=$(curl -s "$TB_URL/api/tenant/dashboards?pageSize=1&page=0" \
  -H "X-Authorization: Bearer $TB_TOKEN" | jq -r '.data[0].id.id')

curl -s -X POST "$TB_URL/api/customer/public/dashboard/$DASHBOARD_ID" \
  -H "X-Authorization: Bearer $TB_TOKEN" | jq
2 次点击  ∙  0 人收藏  
登录后收藏  
目前尚无回复
0 条回复
About   ·   Help   ·    
OA0 - Omni AI 0 一个探索 AI 的社区
沪ICP备2024103595号-2
Developed with Cursor