设备历史

This commit is contained in:
XM-GO
2023-09-08 17:41:54 +08:00
parent 3c645e7529
commit da67865a7e
8 changed files with 529 additions and 512 deletions

View File

@@ -86,7 +86,7 @@ func (p *DeviceApi) GetDeviceStatus(rc *restfulx.ReqCtx) {
Define: tel.Define,
Time: rs["ts"],
}
if v, ok := rs[tel.Key]; ok {
if v, ok := rs[strings.ToLower(tel.Key)]; ok {
sdv.Value = v
} else {
sdv.Value = tel.Define["default_value"]
@@ -97,6 +97,20 @@ func (p *DeviceApi) GetDeviceStatus(rc *restfulx.ReqCtx) {
rc.ResData = res
}
// GetDeviceTelemetryHistory 获取Device属性的遥测历史
func (p *DeviceApi) GetDeviceTelemetryHistory(rc *restfulx.ReqCtx) {
id := restfulx.PathParam(rc, "id")
key := restfulx.QueryParam(rc, "key")
startTime := restfulx.QueryParam(rc, "startTime")
endTime := restfulx.QueryParam(rc, "endTime")
limit := restfulx.QueryInt(rc, "limit", 1000)
device := p.DeviceApp.FindOne(id)
sql := `select ts,? from ? where ts > '?' and ts < '?' and ? is not null ORDER BY ts DESC LIMIT ? `
rs, err := global.TdDb.GetAll(sql, key, fmt.Sprintf("%s_telemetry", device.Name), startTime, endTime, key, limit)
biz.ErrIsNilAppendErr(err, "查询设备属性的遥测历史失败")
rc.ResData = rs
}
// 下发设备属性
func (p *DeviceApi) DownAttribute(rc *restfulx.ReqCtx) {
//id := restfulx.PathParam(rc, "id")
@@ -124,8 +138,6 @@ func (p *DeviceApi) InsertDevice(rc *restfulx.ReqCtx) {
data.LinkStatus = global.INACTIVE
data.LastAt = time.Now()
p.DeviceApp.Insert(data)
// 创建超级表
createDeviceTable(data.Pid, data.Name)
}
// UpdateDevice 修改Device
@@ -140,11 +152,6 @@ func (p *DeviceApi) UpdateDevice(rc *restfulx.ReqCtx) {
func (p *DeviceApi) DeleteDevice(rc *restfulx.ReqCtx) {
id := restfulx.PathParam(rc, "id")
ids := strings.Split(id, ",")
for _, id := range ids {
list := p.DeviceApp.FindOne(id)
// 删除表
deleteDeviceTable(list.Name)
}
p.DeviceApp.Delete(ids)
}
@@ -194,17 +201,3 @@ func (p *DeviceApi) ScreenTwinData(rc *restfulx.ReqCtx) {
rc.ResData = vt
}
}
func createDeviceTable(productId, device string) {
err := global.TdDb.CreateTable(productId+"_"+entity.ATTRIBUTES_TSL, device+"_"+entity.ATTRIBUTES_TSL)
biz.ErrIsNil(err, "创建时序属性表失败")
err = global.TdDb.CreateTable(productId+"_"+entity.TELEMETRY_TSL, device+"_"+entity.TELEMETRY_TSL)
biz.ErrIsNil(err, "创建时序遥测表失败")
}
func deleteDeviceTable(device string) {
err := global.TdDb.DropTable(device + "_" + entity.ATTRIBUTES_TSL)
biz.ErrIsNil(err, "删除时序属性表失败")
err = global.TdDb.DropTable(device + "_" + entity.TELEMETRY_TSL)
biz.ErrIsNil(err, "删除时序遥测表失败")
}

View File

@@ -11,7 +11,6 @@ import (
"github.com/PandaXGO/PandaKit/model"
"github.com/PandaXGO/PandaKit/restfulx"
"github.com/kakuilan/kgo"
"pandax/pkg/global"
"strings"
"pandax/apps/device/entity"
@@ -70,8 +69,6 @@ func (p *ProductApi) InsertProduct(rc *restfulx.ReqCtx) {
data.Id = kgo.KStr.Uniqid("p_")
data.Owner = rc.LoginAccount.UserName
p.ProductApp.Insert(data)
// 创建taos数据库超级表
createDeviceStable(data.Id)
}
// UpdateProduct 修改Product
@@ -96,21 +93,5 @@ func (p *ProductApi) DeleteProduct(rc *restfulx.ReqCtx) {
// 删除所有模型,固件
p.TemplateApp.Delete([]string{id})
p.OtaAPP.Delete([]string{id})
// 删除超级表
deleteDeviceStable(id)
}
}
func createDeviceStable(productId string) {
err := global.TdDb.CreateStable(productId + "_" + entity.ATTRIBUTES_TSL)
biz.ErrIsNil(err, "创建时序属性超级表失败")
err = global.TdDb.CreateStable(productId + "_" + entity.TELEMETRY_TSL)
biz.ErrIsNil(err, "创建时序遥测超级表失败")
}
func deleteDeviceStable(productId string) {
err := global.TdDb.DropStable(productId + "_" + entity.ATTRIBUTES_TSL)
biz.ErrIsNil(err, "删除时序属性超级表失败")
err = global.TdDb.DropStable(productId + "_" + entity.TELEMETRY_TSL)
biz.ErrIsNil(err, "删除时序遥测超级表失败")
}

View File

@@ -71,6 +71,19 @@ func InitDeviceRouter(container *restful.Container) {
Returns(200, "OK", []entity.DeviceStatusVo{}).
Returns(404, "Not Found", nil))
ws.Route(ws.GET("/{id}/property/history").To(func(request *restful.Request, response *restful.Response) {
restfulx.NewReqCtx(request, response).WithLog("获取设备属性的遥测历史").Handle(s.GetDeviceTelemetryHistory)
}).
Doc("获取设备属性的遥测历史").
Param(ws.PathParameter("id", "Id").DataType("string")).
Param(ws.QueryParameter("key", "属性Key").Required(false).DataType("string")).
Param(ws.QueryParameter("startTime", "开始时间").Required(false).DataType("string")).
Param(ws.QueryParameter("endTime", "结束时间").Required(false).DataType("string")).
Param(ws.QueryParameter("limit", "限制条数").Required(false).DataType("int")).
Metadata(restfulspec.KeyOpenAPITags, tags). // on the response
Returns(200, "OK", []map[string]any{}).
Returns(404, "Not Found", nil))
ws.Route(ws.GET("/{id}/attribute/down").To(func(request *restful.Request, response *restful.Response) {
restfulx.NewReqCtx(request, response).WithLog("获取Device属性下发").Handle(s.DownAttribute)
}).

View File

@@ -30,6 +30,7 @@ var DeviceModelDao DeviceModel = &deviceModelImpl{
}
func (m *deviceModelImpl) Insert(data entity.Device) *entity.Device {
tx := global.Db.Begin()
//1 检查设备名称是否存在
list := m.FindList(entity.Device{Name: data.Name})
biz.IsTrue(list != nil && len(*list) == 0, "设备名称已经存在")
@@ -39,8 +40,15 @@ func (m *deviceModelImpl) Insert(data entity.Device) *entity.Device {
data.Token = etoken.Token
}
//3 添加设备
err := global.Db.Table(m.table).Create(&data).Error
err := tx.Table(m.table).Create(&data).Error
biz.ErrIsNil(err, "添加设备失败")
// 创建超级表 失败就
err = createDeviceTable(data.Pid, data.Name)
if err != nil {
tx.Rollback()
biz.ErrIsNil(err, "添加设备失败,设备表创建失败")
}
tx.Commit()
return &data
}
@@ -163,7 +171,37 @@ func (m *deviceModelImpl) UpdateStatus(id, linkStatus string) {
func (m *deviceModelImpl) Delete(ids []string) {
biz.ErrIsNil(global.Db.Table(m.table).Delete(&entity.Device{}, "id in (?)", ids).Error, "删除设备失败")
for _, id := range ids {
list := m.FindOne(id)
// 删除表
err := deleteDeviceTable(list.Name)
global.Log.Error("设备时序表删除失败", err)
// 删除所有缓存
global.RedisDb.Del(context.Background(), id)
}
}
// 获取Tdengine时序数据
func createDeviceTable(productId, device string) error {
err := global.TdDb.CreateTable(productId+"_"+entity.ATTRIBUTES_TSL, device+"_"+entity.ATTRIBUTES_TSL)
if err != nil {
return err
}
err = global.TdDb.CreateTable(productId+"_"+entity.TELEMETRY_TSL, device+"_"+entity.TELEMETRY_TSL)
if err != nil {
return err
}
return nil
}
// 删除Tdengine时序数据
func deleteDeviceTable(device string) error {
err := global.TdDb.DropTable(device + "_" + entity.ATTRIBUTES_TSL)
if err != nil {
return err
}
err = global.TdDb.DropTable(device + "_" + entity.TELEMETRY_TSL)
if err != nil {
return err
}
return nil
}

View File

@@ -31,10 +31,18 @@ var ProductModelDao ProductModel = &productModelImpl{
}
func (m *productModelImpl) Insert(data entity.Product) *entity.Product {
tx := global.Db.Begin()
// 添加产品及规则链到redis中
setProductRule(&data)
err := global.Db.Table(m.table).Create(&data).Error
err := tx.Table(m.table).Create(&data).Error
biz.ErrIsNil(err, "添加产品失败")
// 创建taos数据库超级表
err = createDeviceStable(data.Id)
if err != nil {
tx.Rollback()
biz.ErrIsNil(err, "添加设备失败,超级表创建失败")
}
setProductRule(&data)
tx.Commit()
return &data
}
@@ -144,7 +152,34 @@ func (m *productModelImpl) Update(data entity.Product) *entity.Product {
func (m *productModelImpl) Delete(ids []string) {
biz.ErrIsNil(global.Db.Table(m.table).Delete(&entity.Product{}, "id in (?)", ids).Error, "删除产品失败")
for _, id := range ids {
// 删除超级表
err := deleteDeviceStable(id)
global.Log.Error("时序数据库超级表删除失败", err)
// 删除所有缓存
global.RedisDb.Del(context.Background(), id)
}
}
func createDeviceStable(productId string) error {
err := global.TdDb.CreateStable(productId + "_" + entity.ATTRIBUTES_TSL)
if err != nil {
return err
}
err = global.TdDb.CreateStable(productId + "_" + entity.TELEMETRY_TSL)
if err != nil {
return err
}
return nil
}
func deleteDeviceStable(productId string) error {
err := global.TdDb.DropStable(productId + "_" + entity.ATTRIBUTES_TSL)
if err != nil {
return err
}
err = global.TdDb.DropStable(productId + "_" + entity.TELEMETRY_TSL)
if err != nil {
return err
}
return nil
}