[fix] 设备属性读取错误

This commit is contained in:
PandaX-Go
2024-06-13 20:39:26 +08:00
parent 60f23a5920
commit b1be42a00e
2 changed files with 21 additions and 14 deletions

View File

@@ -88,7 +88,7 @@ func (p *DeviceApi) GetDevice(rc *restfulx.ReqCtx) {
} }
// GetDeviceStatus 获取Device状态信息 // GetDeviceStatus 获取Device状态信息
func (p *DeviceApi) GetDeviceStatus(rc *restfulx.ReqCtx) { func (p *DeviceApi) GetDevigitceStatus(rc *restfulx.ReqCtx) {
id := restfulx.PathParam(rc, "id") id := restfulx.PathParam(rc, "id")
classify := restfulx.QueryParam(rc, "classify") classify := restfulx.QueryParam(rc, "classify")
device, err := p.DeviceApp.FindOne(id) device, err := p.DeviceApp.FindOne(id)
@@ -114,16 +114,9 @@ func (p *DeviceApi) GetDeviceStatus(rc *restfulx.ReqCtx) {
} }
// 有直接从设备影子中查询,没有查询时序数据库最后一条记录 // 有直接从设备影子中查询,没有查询时序数据库最后一条记录
if point, ok := rs[tel.Key]; ok { if point, ok := rs[tel.Key]; ok {
if classify == global.TslTelemetryType { value := point.Value
value := point.Value sdv.Time = point.UpdatedAt
sdv.Time = point.UpdatedAt sdv.Value = value
sdv.Value = value
}
if classify == global.TslAttributesType {
if value, ok := tel.Define["default_value"]; ok {
sdv.Value = value
}
}
} else { } else {
var table string var table string
if classify == global.TslTelemetryType { if classify == global.TslTelemetryType {
@@ -136,6 +129,15 @@ func (p *DeviceApi) GetDeviceStatus(rc *restfulx.ReqCtx) {
one, err := global.TdDb.GetOne(sql, strings.ToLower(tel.Key), table) one, err := global.TdDb.GetOne(sql, strings.ToLower(tel.Key), table)
if err == nil { if err == nil {
sdv.Value = one[strings.ToLower(tel.Key)] sdv.Value = one[strings.ToLower(tel.Key)]
sdv.Time = time.Now()
} else {
if value, ok := tel.Define["default_value"]; ok {
sdv.Value = value
sdv.Time = time.Now()
} else {
sdv.Value = "未知"
sdv.Time = time.Now()
}
} }
} }
res = append(res, sdv) res = append(res, sdv)

View File

@@ -1,6 +1,7 @@
package api package api
import ( import (
"encoding/json"
"pandax/kit/biz" "pandax/kit/biz"
"pandax/kit/model" "pandax/kit/model"
"pandax/kit/restfulx" "pandax/kit/restfulx"
@@ -65,8 +66,9 @@ func (p *ProductTemplateApi) InsertProductTemplate(rc *restfulx.ReqCtx) {
stable := data.Pid + "_" + data.Classify stable := data.Pid + "_" + data.Classify
if data.Classify == entity.TELEMETRY_TSL { if data.Classify == entity.TELEMETRY_TSL {
if data.Type == "string" { if data.Type == "string" {
if maxLength, ok := data.Define["maxLength"].(float64); ok { if maxLength, ok := data.Define["maxLength"].(json.Number); ok {
len = int(maxLength) length, _ := maxLength.Int64()
len = int(length)
} }
} }
} }
@@ -95,7 +97,10 @@ func (p *ProductTemplateApi) UpdateProductTemplate(rc *restfulx.ReqCtx) {
if data.Classify == entity.TELEMETRY_TSL { if data.Classify == entity.TELEMETRY_TSL {
stable = data.Pid + "_" + entity.TELEMETRY_TSL stable = data.Pid + "_" + entity.TELEMETRY_TSL
if data.Type == "string" { if data.Type == "string" {
len = int(data.Define["length"].(int64)) if maxLength, ok := data.Define["maxLength"].(json.Number); ok {
length, _ := maxLength.Int64()
len = int(length)
}
} }
global.TdDb.DelSTableField(stable, one.Key) global.TdDb.DelSTableField(stable, one.Key)
err := global.TdDb.AddSTableField(stable, data.Key, data.Type, len) err := global.TdDb.AddSTableField(stable, data.Key, data.Type, len)