1 Star 1 Fork 1

ThingsGateway/Docs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
TDengineDBProducer.mdx 9.74 KB
一键复制 编辑 原始数据 按行查看 历史
Diego 提交于 2025-07-08 11:40 +08:00 . 更新taos文档
---
id: 305
title: TDengineDBProducer
---
import useBaseUrl from "@docusaurus/useBaseUrl";
import Tag from "@site/src/components/Tag.js";
import Highlight from '@site/src/components/Highlight.js';
:::tip `提示`
taos数据库插件支持原生连接和websocket连接,[官方文档](https://docs.taosdata.com/develop/connect/#websocket-%E8%BF%9E%E6%8E%A5)
:::
## 一、说明
TDengineDBProducer适配TDengineDB时序数据库,可以定时/变化存储变量数据
通道只支持 Other
## 二、插件属性配置项
<img src={require("@site/static/img/docs/TDengineDBProducer1.png").default} />
| 属性 | 说明 | 备注|
| ---------------| --------------------------| ---|
| 链接字符串 | 链接字符串 ||
| 表名称 | 超级表名称,子表名称为 [超级表名称]_变量名称 | |
| 分组上传 | 启用后,无论是定时还是变化模式,始终会上传**变量分组属性**为key分组的全部变量 。在变化模式时,每次变量变化都会触发一次组上传 | False |
| 是否选择全部变量 | 是否选择全部变量,true时不需要单个变量添加业务属性 | |
| 上传模式 | 间隔/变化/变化和间隔同时生效 | |
| 定时上传间隔 | 间隔执行时间 | |
| 启用缓存 | 是否启用缓存 | |
| 缓存文件最大长度(mb) | 缓存文件最大长度 | |
| 上传每页条数 | 每一次上传的列表最大数量 | |
| 内存队列最大数量 | 内存队列的最大数量,超出或失败时转入文件缓存,根据数据量设定适当值 | |
| 历史库动态脚本 | 历史库动态脚本 | |
## 三、脚本与实体
#### 3.1、脚本接口
```
public abstract class DynamicSQLBase
{
public TouchSocket.Core.ILog Logger { get; set; }
/// <summary>
/// 建库建表
/// </summary>
/// <returns></returns>
public virtual Task DBInit(ISqlSugarClient db, CancellationToken cancellationToken)
{
throw new NotSupportedException();
}
/// <summary>
/// 自定义上传
/// </summary>
/// <returns></returns>
public virtual Task DBInsertable(ISqlSugarClient db, IEnumerable<object> datas, CancellationToken cancellationToken)
{
throw new NotSupportedException();
}
}
```
##### 完全自定义上传
在``DBInsertable``方法中,自主实现上传逻辑,自定义Sql语句或通过orm执行
##### Demo
```
using ThingsGateway.Plugin.DB;
using ThingsGateway.SqlSugar;
using System.Text;
using ThingsGateway.Gateway.Application;
using TouchSocket.Core;
public class Taos : DynamicSQLBase
{
public override async Task DBInsertable(ISqlSugarClient db, IEnumerable<object> datas, CancellationToken cancellationToken)
{
StringBuilder stringBuilder = new();
stringBuilder.Append($"INSERT INTO");
var dbInserts = datas.Cast<VariableBasicData>();
//(`id`,`createtime`,`collecttime`,`isonline`,`value`)
foreach (var deviceGroup in dbInserts.GroupBy(a => a.DeviceName))
{
foreach (var variableGroup in deviceGroup.GroupBy(a => a.Name))
{
stringBuilder.Append($"""
`tablename1_{deviceGroup.Key}_{variableGroup.Key}`
USING `tablename1` TAGS ("{deviceGroup.Key}", "{variableGroup.Key}")
VALUES
""");
foreach (var item in variableGroup)
{
stringBuilder.Append($"""(NOW,"{item.CollectTime.ToString("yyyy-MM-dd HH:mm:ss.fff")}",{item.Id},{item.IsOnline},"{item.Value}"),""");
}
stringBuilder.Remove(stringBuilder.Length - 1, 1);
}
}
stringBuilder.Append(';');
stringBuilder.AppendLine();
await db.Ado.ExecuteCommandAsync(stringBuilder.ToString(), default, cancellationToken: cancellationToken).ConfigureAwait(false);
LogMessage?.Trace($"Count {datas.Count()}");
}
public override async Task DBInit(ISqlSugarClient db, CancellationToken cancellationToken)
{
var sql = $"""
CREATE STABLE IF NOT EXISTS `tablename1`(
`createtime` TIMESTAMP ,
`collecttime` TIMESTAMP ,
`id` BIGINT ,
`isonline` BOOL ,
`value` VARCHAR(255) ) TAGS(`devicename` VARCHAR(100) ,`name` VARCHAR(100))
""";
await db.Ado.ExecuteCommandAsync(sql, default, cancellationToken: cancellationToken).ConfigureAwait(false);
}
}
```
#### 3.2、脚本方法传入参数实体类
```
/// <summary>
/// 变量业务变化数据
/// </summary>
public class VariableBasicData
{
/// <inheritdoc cref="PrimaryIdEntity.Id"/>
public long Id { get; set; }
/// <inheritdoc cref="Variable.Name"/>
public string Name { get; set; }
/// <inheritdoc cref="Variable.CollectGroup"/>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull)]
public string CollectGroup { get; set; }
/// <inheritdoc cref="Variable.BusinessGroup"/>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull)]
public string BusinessGroup { get; set; }
/// <inheritdoc cref="Variable.BusinessGroupUpdateTrigger"/>
public bool BusinessGroupUpdateTrigger { get; set; }
/// <inheritdoc cref="VariableRuntime.DeviceName"/>
public string DeviceName { get; set; }
/// <inheritdoc cref="VariableRuntime.RuntimeType"/>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull)]
public string RuntimeType { get; set; }
[JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
public bool IsNumber => Value?.GetType()?.IsNumber() == true;
/// <inheritdoc cref="VariableRuntime.Value"/>
public object Value { get; set; }
/// <inheritdoc cref="VariableRuntime.RawValue"/>
public object RawValue { get; set; }
/// <inheritdoc cref="VariableRuntime.LastSetValue"/>
public object LastSetValue { get; set; }
/// <inheritdoc cref="VariableRuntime.ChangeTime"/>
public DateTime ChangeTime { get; set; }
/// <inheritdoc cref="VariableRuntime.CollectTime"/>
public DateTime CollectTime { get; set; }
/// <inheritdoc cref="VariableRuntime.IsOnline"/>
public bool IsOnline { get; set; }
///// <inheritdoc cref="VariableRuntime.DeviceRuntime"/>
//[System.Text.Json.Serialization.JsonIgnore]
//[Newtonsoft.Json.JsonIgnore]
//public DeviceBasicData DeviceRuntime { get; set; }
/// <inheritdoc cref="Variable.DeviceId"/>
public long DeviceId { get; set; }
/// <inheritdoc cref="VariableRuntime.LastErrorMessage"/>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull)]
public string? LastErrorMessage { get; set; }
/// <inheritdoc cref="Variable.RegisterAddress"/>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull)]
public string? RegisterAddress { get; set; }
/// <inheritdoc cref="Variable.Unit"/>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull)]
public string? Unit { get; set; }
/// <inheritdoc cref="Variable.Description"/>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull)]
public string? Description { get; set; }
/// <inheritdoc cref="Variable.ProtectType"/>
public ProtectTypeEnum ProtectType { get; set; }
/// <inheritdoc cref="Variable.DataType"/>
public DataTypeEnum DataType { get; set; }
/// <inheritdoc cref="Device.Remark1"/>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull)]
public string Remark1 { get; set; }
/// <inheritdoc cref="Device.Remark2"/>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull)]
public string Remark2 { get; set; }
/// <inheritdoc cref="Device.Remark3"/>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull)]
public string Remark3 { get; set; }
/// <inheritdoc cref="Device.Remark4"/>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull)]
public string Remark4 { get; set; }
/// <inheritdoc cref="Device.Remark5"/>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull)]
public string Remark5 { get; set; }
}
```
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ThingsGateway/Docs.git
git@gitee.com:ThingsGateway/Docs.git
ThingsGateway
Docs
Docs
master

搜索帮助