diff --git a/internal/pkg/v2/infrastructure/redis/reading.go b/internal/pkg/v2/infrastructure/redis/reading.go index d16c6dcd31..4c9c25aa98 100644 --- a/internal/pkg/v2/infrastructure/redis/reading.go +++ b/internal/pkg/v2/infrastructure/redis/reading.go @@ -228,15 +228,29 @@ func readingsByTimeRange(conn redis.Conn, start int, end int, offset int, limit func convertObjectsToReadings(objects [][]byte) (readings []models.Reading, edgeXerr errors.EdgeX) { readings = make([]models.Reading, len(objects)) + var alias struct { + ValueType string + } for i, in := range objects { - // as V2 APi doesn't deal with BinaryReading at this moment, convert to SimpleReading here - // Shall update the logic here when working on BinaryReading in the future - sr := models.SimpleReading{} - err := json.Unmarshal(in, &sr) + err := json.Unmarshal(in, &alias) if err != nil { return []models.Reading{}, errors.NewCommonEdgeX(errors.KindDatabaseError, "reading format parsing failed from the database", err) } - readings[i] = sr + if alias.ValueType == v2.ValueTypeBinary { + var binaryReading models.BinaryReading + err = json.Unmarshal(in, &binaryReading) + if err != nil { + return []models.Reading{}, errors.NewCommonEdgeX(errors.KindDatabaseError, "binary reading format parsing failed from the database", err) + } + readings[i] = binaryReading + } else { + var simpleReading models.SimpleReading + err = json.Unmarshal(in, &simpleReading) + if err != nil { + return []models.Reading{}, errors.NewCommonEdgeX(errors.KindDatabaseError, "simple reading format parsing failed from the database", err) + } + readings[i] = simpleReading + } } return readings, nil } diff --git a/internal/pkg/v2/infrastructure/redis/reading_test.go b/internal/pkg/v2/infrastructure/redis/reading_test.go new file mode 100644 index 0000000000..236da14367 --- /dev/null +++ b/internal/pkg/v2/infrastructure/redis/reading_test.go @@ -0,0 +1,71 @@ +// +// Copyright (C) 2021 IOTech Ltd +// +// SPDX-License-Identifier: Apache-2.0 + +package redis + +import ( + "encoding/json" + "testing" + + "github.com/edgexfoundry/go-mod-core-contracts/v2/v2" + "github.com/edgexfoundry/go-mod-core-contracts/v2/v2/models" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const ( + exampleUUID = "82eb2e26-0f24-48aa-ae4c-de9dac3fb9bc" + testDeviceName = "testDeviceName" + testProfileName = "testProfileName" + testResourceName = "testResourceName" +) + +func simpleReadingData() models.SimpleReading { + return models.SimpleReading{ + BaseReading: models.BaseReading{ + Id: exampleUUID, + Origin: 1616728256236000000, + DeviceName: testDeviceName, + ProfileName: testProfileName, + ResourceName: testResourceName, + ValueType: v2.ValueTypeString, + }, + Value: "123", + } +} + +func binaryReadingData() models.BinaryReading { + return models.BinaryReading{ + BaseReading: models.BaseReading{ + Id: exampleUUID, + Origin: 1616728256236000001, + DeviceName: testDeviceName, + ProfileName: testProfileName, + ResourceName: testResourceName, + ValueType: v2.ValueTypeBinary, + }, + BinaryValue: make([]byte, 0), + MediaType: "image", + } +} + +func TestConvertObjectsToReadings(t *testing.T) { + simpleReading := simpleReadingData() + binaryReading := binaryReadingData() + + simpleReadingBytes, err := json.Marshal(simpleReading) + require.NoError(t, err) + binaryReadingBytes, err := json.Marshal(binaryReading) + require.NoError(t, err) + + readingsData := [][]byte{simpleReadingBytes, binaryReadingBytes} + expectedReadings := []models.Reading{ + simpleReading, binaryReading, + } + + events, err := convertObjectsToReadings(readingsData) + require.NoError(t, err) + assert.Equal(t, expectedReadings, events) +} diff --git a/openapi/v2/core-data.yaml b/openapi/v2/core-data.yaml index a4c0a0ad9d..d5d393f19b 100644 --- a/openapi/v2/core-data.yaml +++ b/openapi/v2/core-data.yaml @@ -430,7 +430,7 @@ components: id: "71c601d9-cb56-453a-8c75-54461e444713" origin: 1602168089665565300 valueType: "Binary" - binaryValue: "83010203" + binaryValue: [12,34] mediaType: "image" - apiVersion: "v2" deviceName: "device-002" @@ -473,7 +473,7 @@ components: id: "71c601d9-cb56-453a-8c75-54461e444713" origin: 1602168089665565300 valueType: "Binary" - binaryValue: "83010203" + binaryValue: [12,34] mediaType: "image" - deviceName: "device-002" resourceName: "resource-002"