mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-24 11:28:40 +08:00
设备历史
This commit is contained in:
@@ -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, "删除时序遥测表失败")
|
||||
}
|
||||
|
||||
@@ -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, "删除时序遥测超级表失败")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user