From 38b6fde571f9ada749c547ac468ac9139c25d6fb Mon Sep 17 00:00:00 2001 From: microrain Date: Mon, 4 Mar 2024 09:04:32 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E5=A4=8D=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E8=AF=A6=E6=83=85=E4=B8=AD=E6=95=B0=E6=8D=AE=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E6=98=BE=E7=A4=BABug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/logic/product/dev_device.go | 6 ++--- pkg/dcache/device_data.go | 39 +++++++++++++++++++++------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/internal/logic/product/dev_device.go b/internal/logic/product/dev_device.go index e728b8c..15db40c 100644 --- a/internal/logic/product/dev_device.go +++ b/internal/logic/product/dev_device.go @@ -834,8 +834,8 @@ func (s *sDevDevice) RunStatus(ctx context.Context, deviceKey string) (out *mode out = new(model.DeviceRunStatusOutput) out.Status = dcache.GetDeviceStatus(ctx, deviceKey) - //获取数据示例 - deviceValueList := dcache.GetDeviceDetailData(context.Background(), deviceKey) + //获取数据 + deviceValueList := dcache.GetDeviceDetailData(context.Background(), deviceKey, consts.MsgTypePropertyReport, consts.MsgTypeGatewayBatch) if len(deviceValueList) == 0 { return } @@ -973,7 +973,7 @@ func (s *sDevDevice) GetProperty(ctx context.Context, in *model.DeviceGetPropert // GetPropertyList 设备属性详情列表 func (s *sDevDevice) GetPropertyList(ctx context.Context, in *model.DeviceGetPropertyListInput) (out *model.DeviceGetPropertyListOutput, err error) { - resultList, total, currentPage := dcache.GetDeviceDetailDataByPage(ctx, in.DeviceKey, in.PageNum, in.PageSize) + resultList, total, currentPage := dcache.GetDeviceDetailDataByPage(ctx, in.DeviceKey, in.PageNum, in.PageSize, consts.MsgTypePropertyReport, consts.MsgTypeGatewayBatch) if err != nil { return } diff --git a/pkg/dcache/device_data.go b/pkg/dcache/device_data.go index 0646557..3da060a 100644 --- a/pkg/dcache/device_data.go +++ b/pkg/dcache/device_data.go @@ -30,9 +30,9 @@ func InertDeviceLog(ctx context.Context, logType, deviceKey string, obj interfac } // GetDeviceDetailData 获取设备解析后的详细数据 -func GetDeviceDetailData(ctx context.Context, deviceKey string) (res []map[string]iotModel.ReportPropertyNode) { +func GetDeviceDetailData(ctx context.Context, deviceKey string, dataType ...string) (res []map[string]iotModel.ReportPropertyNode) { // 从设备缓存数据库获取数据 - dataList, err := DB().GetData(context.Background(), deviceKey) + dataList, err := DB().GetData(ctx, deviceKey) if err != nil { g.Log().Debugf(ctx, "Failed to get data: %v", err) return @@ -45,13 +45,23 @@ func GetDeviceDetailData(ctx context.Context, deviceKey string) (res []map[strin if err := json.Unmarshal([]byte(data), &value); err != nil { g.Log().Debugf(ctx, "Failed to unmarshal data: %v", err) } - // 基于物模型解析数据 - tmp, err := service.DevTSLParse().ParseData(ctx, deviceKey, []byte(value.Content)) + dataContent, err := service.DevTSLParse().ParseData(ctx, deviceKey, []byte(value.Content)) if err != nil { - return + continue + } + if len(dataContent) == 0 { + continue + } + if len(dataType) > 0 { + for _, vt := range dataType { + if vt == value.Type { + res = append(res, dataContent) + } + } + } else { + res = append(res, dataContent) } - res = append(res, tmp) } return } @@ -80,7 +90,7 @@ func GetDeviceDetailDataByLatest(ctx context.Context, deviceKey string) (res iot } // GetDeviceDetailDataByPage 按分页获取设备详细数据,分页参数: pageNum 为页码, pageSize 为每页数量 -func GetDeviceDetailDataByPage(ctx context.Context, deviceKey string, pageNum, pageSize int) (res []map[string]iotModel.ReportPropertyNode, total, currentPage int) { +func GetDeviceDetailDataByPage(ctx context.Context, deviceKey string, pageNum, pageSize int, dataType ...string) (res []map[string]iotModel.ReportPropertyNode, total, currentPage int) { // 获取 list 的名称 listName := DeviceDataCachePrefix + deviceKey @@ -123,11 +133,22 @@ func GetDeviceDetailDataByPage(ctx context.Context, deviceKey string, pageNum, p } // 基于物模型解析数据 - tmp, err := service.DevTSLParse().ParseData(ctx, deviceKey, []byte(value.Content)) + dataContent, err := service.DevTSLParse().ParseData(ctx, deviceKey, []byte(value.Content)) if err != nil { return } - res = append(res, tmp) + if len(dataContent) == 0 { + continue + } + if len(dataType) > 0 { + for _, vt := range dataType { + if vt == value.Type { + res = append(res, dataContent) + } + } + } else { + res = append(res, dataContent) + } } return }