Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(data): Implement get Binary Reading from database #3303

Merged
merged 2 commits into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions internal/pkg/v2/infrastructure/redis/reading.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (C) 2020 IOTech Ltd
// Copyright (C) 2020-2021 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0

Expand Down Expand Up @@ -231,15 +231,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
cloudxxx8 marked this conversation as resolved.
Show resolved Hide resolved
} 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
}
71 changes: 71 additions & 0 deletions internal/pkg/v2/infrastructure/redis/reading_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 2 additions & 2 deletions openapi/v2/core-data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ components:
id: "71c601d9-cb56-453a-8c75-54461e444713"
origin: 1602168089665565300
valueType: "Binary"
binaryValue: "83010203"
binaryValue: [12,34]
mediaType: "image"
- apiVersion: "v2"
created: 1594983105886
Expand Down Expand Up @@ -490,7 +490,7 @@ components:
id: "71c601d9-cb56-453a-8c75-54461e444713"
origin: 1602168089665565300
valueType: "Binary"
binaryValue: "83010203"
binaryValue: [12,34]
mediaType: "image"
- created: 594983105886
deviceName: "device-002"
Expand Down