37 Star 391 Fork 118

联犀/物联网iot模块

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
schemaRepo.go 2.44 KB
一键复制 编辑 原始数据 按行查看 历史
杨磊 提交于 2024-11-02 14:49 . feat: 初步完善提交
package schemaDataRepo
import (
"context"
"fmt"
"gitee.com/unitedrhino/share/clients"
"gitee.com/unitedrhino/share/conf"
schema "gitee.com/unitedrhino/share/domain/schema"
"gitee.com/unitedrhino/share/errors"
"gitee.com/unitedrhino/share/stores"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stores/kv"
"os"
)
const (
PropertyType = "property_type"
)
type DeviceDataRepo struct {
t *clients.Td
getProductSchemaModel schema.GetSchemaModel
getDeviceSchemaModel schema.GetSchemaModel
SchemaStore
kv kv.Store
}
func NewDeviceDataRepo(dataSource conf.TSDB, getProductSchemaModel schema.GetSchemaModel, getDeviceSchemaModel schema.GetSchemaModel, kv kv.Store) *DeviceDataRepo {
td, err := clients.NewTDengine(dataSource)
if err != nil {
logx.Error("NewTDengine err", err)
os.Exit(-1)
}
return &DeviceDataRepo{t: td, getProductSchemaModel: getProductSchemaModel, getDeviceSchemaModel: getDeviceSchemaModel, kv: kv}
}
func (d *DeviceDataRepo) Init(ctx context.Context) error {
{
sql := fmt.Sprintf("CREATE STABLE IF NOT EXISTS %s "+
"(`ts` timestamp,`event_id` BINARY(50),`event_type` BINARY(20), `param` BINARY(5000)) "+
"TAGS (`product_id` BINARY(50),`device_name` BINARY(50));",
d.GetEventStableName())
if _, err := d.t.ExecContext(ctx, sql); err != nil {
return errors.Database.AddDetail(err)
}
}
genDeviceStable := func(tb string, def schema.Define) error {
sql := fmt.Sprintf("CREATE STABLE IF NOT EXISTS %s (`ts` timestamp,`param` %s)"+
" TAGS (`product_id` BINARY(50),`device_name` BINARY(50),`"+PropertyType+"` BINARY(50));",
tb, stores.GetTdType(def))
if _, err := d.t.ExecContext(ctx, sql); err != nil {
return errors.Database.AddDetail(err)
}
return nil
}
execDeviceTables := []struct {
tb string
def schema.Define
}{
{tb: d.GetDeviceStableBoolName(), def: schema.Define{Type: schema.DataTypeBool}},
{tb: d.GetDeviceStableIntName(), def: schema.Define{Type: schema.DataTypeInt}},
{tb: d.GetDeviceStableStringName(), def: schema.Define{Type: schema.DataTypeString}},
{tb: d.GetDeviceStableTimestampName(), def: schema.Define{Type: schema.DataTypeTimestamp}},
{tb: d.GetDeviceStableEnumName(), def: schema.Define{Type: schema.DataTypeEnum}},
{tb: d.GetDeviceStableFloatName(), def: schema.Define{Type: schema.DataTypeFloat}},
}
for _, e := range execDeviceTables {
err := genDeviceStable(e.tb, e.def)
if err != nil {
return err
}
}
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/unitedrhino/things.git
git@gitee.com:unitedrhino/things.git
unitedrhino
things
物联网iot模块
v1.2.0

搜索帮助