thingsboard 实体及数据存储研究 · 物联网平台-威尼斯人最新

haiiiiiyun · 2020年04月27日 · 最后由 回复于 2022年08月24日 · 735 次阅读
本帖已被设为精华帖!

原文链接见

1. 概述

本文主要研究 thingsboard 各种实体在关系型数据库 postgres 中的存储。

2. 安装与配置使用 postgresql 数据库

从源码编译安装参考 :

编译运行后,需创建 thingsboard 数据库:

psql -u postgres -d postgres -h 127.0.0.1 -w
create database thingsboard;
\q

postgresql 的基本使用,见

thingsboard v2.4 中,默认使用的数据库用户名和密码都是 postgres, 见 /application/src/main/resources/thingsboard.yml:

datasource:
    driverclassname: "${spring_driver_class_name:org.postgresql.driver}"
    url: "${spring_datasource_url:jdbc:postgresql://localhost:5432/thingsboard}"
    username: "${spring_datasource_username:postgres}"
    password: "${spring_datasource_password:postgres}"

创建 schema 并导入测试数据:

cd ~/workspace/thingsboard/application/target/bin/install
chmod  x install_dev_db.sh
./install_dev_db.sh

登录测试

用户名 tenant@thingsboard.org 密码 tenant

实体

tenants 租户

是一个独立的商业实体:比如个人或组织,可以拥有若产生多个设备或资产。一个租户可有多个租户管理员用户及多个客户。

可将它理解为代理商,中间商,如中国移动是一个 tenant, 承包了某地区的所有项目,信息保存在 tenant 表中。

customers 客户

也是一个独立的商业实体:如何个人或组织,客户从租户那购买设备和资产。一个客户中可有多个用户。

可将它理解为甲方客户,如某单位或企业,从租户中国移动那购买服务。

信息保存在 customer 表中, 通过 tenant_id 关联相应的 tenant。

users 用户

用户登录系统,查看信息并管理实体的账号。系统中创建的用户,保存于 tb_user 中。

通过 customer_idtenant_id 关联相应的租户和客户,用户权限由 authority 字段指定,值见:

// thingsboard/common/data/src/main/java/org/thingsboard/server/common/data/security/authority.java
public enum authority {
    sys_admin(0),
    tenant_admin(1),
    customer_user(2),
    refresh_token(10);
}

customer_id 和 tenant_id 有一个默认的值 1b21dd2138140008080808080808080,该值应该为系统中的一个默认 tenant 和 customer 值。

例如系统管理员账户 "sysadmin@thingsboard.org" 对应的 customer_id 和 tenant_id 即为该值。

devices 设备

设备可上传遥测数据或执行 rpc 命令。

保存于 device 表中,通过 customer_idtenant_id 关联相应的租户和客户。设备类型由 type 字符串字段指定,默认为 default, 可自由创建新值。

新建的设备,其默认 customer_id 为 1b21dd2138140008080808080808080,该值应该为系统中的一个默认 customer 值。

通过设备管理界面可以分配给指定客户。

设备的访问凭证信息保存在表 device_credentials 中,类型由 credentials_type 字段指定,类型值见:

// thingsboard/common/data/src/main/java/org/thingsboard/server/common/data/security/devicecredentialstype.java
public enum devicecredentialstype {
    access_token,
    x509_certificate
}

assets 资产

一种用于关联其它设备和资产的抽象实体,如区域、建筑物、单位等,是一个容器概念。 保存于表 asset 中。

alarms 警报

与各种实体关联的警报事件。

保存于表 alarm,由 originator_idoriginator_type 关联实体,状态由 status 字段指定,值见:

// thingsboard/common/data/src/main/java/org/thingsboard/server/common/data/alarm/alarmsearchstatus.java
public enum alarmsearchstatus {
    any, active, cleared, ack, unack
}

dashboards

可视化面板,可可视化数据,并操控设备。

保存于表 dashboard 中,dashboard 由 tenant 所有,通过 tenant_id 字段关联。并可以分配给多人 customer, 分配信息保存在 assigned_customers 字段中,如:

[{"customerid":{"entitytype":"customer","id":"5c3c4ff0-8763-11ea-b734-6151c7bf4d3f"},"title":"customer b","public":false},{"customerid":{"entitytype":"customer","id":"5c360e60-8763-11ea-b734-6151c7bf4d3f"},"title":"customer a","public":false}]

rule node 规则结点

处理结点,对上报的信息及实体事件进行处理。

保存在表 rule_node 中,通过 rule_chain_id 关联到某个规则链。

系统已创建了多种结点类型,当前结点类型在 type 字段中指定,值例如(有很多):

  • "org.thingsboard.rule.engine.telemetry.tbmsgtimeseriesnode"
  • "org.thingsboard.rule.engine.telemetry.tbmsgattributesnode"
  • "org.thingsboard.rule.engine.filter.tbmsgtypeswitchnode"
  • "org.thingsboard.rule.engine.action.tblognode"
  • "org.thingsboard.rule.engine.rpc.tbsendrpcrequestnode"

见 thingsboard/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/telemetry/tbmsgtimeseriesnode.java, thingsboard/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/action/tblognode.java 等文件中的定义。

配置信息保存在字段 configuration 中,是 json 字串,如 {"timeoutinseconds":60}

additional_info 字段中保存当前结点在规则链配置界面中的坐标,是 json 字串,如 {"layoutx":824,"layouty":138}

rule chain 规则链

关联的多个规则结点的一个逻辑单元。

保存于表 rule_chain 中,首个结点通过 first_rule_node_id 指定,通过 root boolean 指定是否为根链,通过 tenant_id 关联 tenant。描述信息等以 json 字串形式保存在 additional_info 字段中。

所有实体支持属性、遥测数据和关联: attributes, telemetry date, relations.

attributes 属性

属性值是键值对, 保存于表 attribute_kv 中。实体由 entity_typeentity_id 字段关联,属性类型由 attribute_type 指定,值见:

// thingsboard/common/data/src/main/java/org/thingsboard/server/common/data/dataconstants.java
public class dataconstants {
    public static final string client_scope = "client_scope";
    public static final string server_scope = "server_scope";
    public static final string shared_scope = "shared_scope";
    public static final string[] allscopes() {
        return new string[]{client_scope, shared_scope, server_scope};
    }

kv 值中的 key 在 attribute_key 中保存,value 在 bool_v, str_v, long_v, dbl_v 字段其中一个中保存,分别表示为 bool, string, long, double 值。

telemetry data 遥测数据

时序数据。

历史值在 ts_kv 表中保存, 最新数据在 ts_kv_latest 表中保存。

实体由 entity_typeentity_id 字段关联,key 在 key 字段,时间在 ts 字段,值在 bool_v, str_v, long_v, dbl_v

relations 关联

实体间的有向连接关系信息。

保存于表 relation 中,关联是两个实体间的一种关系,由 from_id, from_type 字段指定 from 端的实体,实体类型值见:

// thingsboard/common/data/src/main/java/org/thingsboard/server/common/data/entitytype.java
public enum entitytype {
    tenant, customer, user, dashboard, asset, device, alarm, rule_chain, rule_node, entity_view, widgets_bundle, widget_type
}

to_id, to_type 字段指定 to 端的实体。

关联关系由 relation_type_grouprelation_type 字段指定。

audit logs 审计日志

审计/操作记录信息保存在表 audit_log 中,操作者通过 tenant_id, customer_id, user_id, user_name 字段指定,操作对象通过 entity_id, entity_type, entity_name 字段指定, 操作类型由 action_type 字段指定,值见:

// thingsboard/common/data/src/main/java/org/thingsboard/server/common/data/audit/actiontype.java
public enum actiontype {
    added(false), // log entity
    deleted(false), // log string id
    updated(false), // log entity
    attributes_updated(false), // log attributes/values
    attributes_deleted(false), // log attributes
    timeseries_deleted(false), // log timeseries
    rpc_call(false), // log method and params
    credentials_updated(false), // log new credentials
    assigned_to_customer(false), // log customer name
    unassigned_from_customer(false), // log customer name
    activated(false), // log string id
    suspended(false), // log string id
    credentials_read(true), // log device id
    attributes_read(true), // log attributes
    relation_add_or_update(false),
    relation_deleted(false),
    relations_deleted(false),
    alarm_ack(false),
    alarm_clear(false),
    login(false),
    logout(false),
    lockout(false);
    private final boolean isread;
    actiontype(boolean isread) {
        this.isread = isread;
    }
}

操作数据以 json 字串形式保存在 action_data 中。

操作状态在 action_status 字段指定,值有 successfailure,如果失败,则失败信息在 action_failure_details 字段中保存,如 prc error: timeout

资源

楼主幸苦了!!!谢谢

thingskit 将本帖设为了精华贴 04月28日 16:36

我在本地运行的 tb-v2.5 项目,配置的是 cassandra 数据库,为什么上传设备客户端属性和遥测数据,在数据库里查找不到?debug 看是接收到了消息的,也进入了 success 方法,好几天了实在不解!还需要安装或者配置什么东西吗?

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册
网站地图