diff --git a/internal/pkg/infrastructure/redis/reading.go b/internal/pkg/infrastructure/redis/reading.go index 9b2f68a7c2..a8a7895625 100644 --- a/internal/pkg/infrastructure/redis/reading.go +++ b/internal/pkg/infrastructure/redis/reading.go @@ -113,6 +113,13 @@ func addReading(conn redis.Conn, r models.Reading) (reading models.Reading, edge } m, err = json.Marshal(newReading) reading = newReading + case models.ObjectReading: + baseReading = &newReading.BaseReading + if err = checkReadingValue(baseReading); err != nil { + return nil, errors.NewCommonEdgeXWrapper(err) + } + m, err = json.Marshal(newReading) + reading = newReading default: return nil, errors.NewCommonEdgeX(errors.KindContractInvalid, "unsupported reading type", nil) } @@ -261,6 +268,13 @@ func convertObjectsToReadings(objects [][]byte) (readings []models.Reading, edge return []models.Reading{}, errors.NewCommonEdgeX(errors.KindDatabaseError, "binary reading format parsing failed from the database", err) } readings[i] = binaryReading + } else if alias.ValueType == common.ValueTypeObject { + var objectReading models.ObjectReading + err = json.Unmarshal(in, &objectReading) + if err != nil { + return []models.Reading{}, errors.NewCommonEdgeX(errors.KindDatabaseError, "object reading format parsing failed from the database", err) + } + readings[i] = objectReading } else { var simpleReading models.SimpleReading err = json.Unmarshal(in, &simpleReading) diff --git a/internal/pkg/infrastructure/redis/reading_test.go b/internal/pkg/infrastructure/redis/reading_test.go index c535020bfb..0cbd142856 100644 --- a/internal/pkg/infrastructure/redis/reading_test.go +++ b/internal/pkg/infrastructure/redis/reading_test.go @@ -51,18 +51,38 @@ func binaryReadingData() models.BinaryReading { } } +func objectReadingData() models.ObjectReading { + return models.ObjectReading{ + BaseReading: models.BaseReading{ + Id: exampleUUID, + Origin: 1616728256236000001, + DeviceName: testDeviceName, + ProfileName: testProfileName, + ResourceName: testResourceName, + ValueType: common.ValueTypeObject, + }, + ObjectValue: map[string]interface{}{ + "f1": "ABC", + "f2": float64(123), + }, + } +} + func TestConvertObjectsToReadings(t *testing.T) { simpleReading := simpleReadingData() binaryReading := binaryReadingData() + objectReading := objectReadingData() simpleReadingBytes, err := json.Marshal(simpleReading) require.NoError(t, err) binaryReadingBytes, err := json.Marshal(binaryReading) require.NoError(t, err) + objectReadingBytes, err := json.Marshal(objectReading) + require.NoError(t, err) - readingsData := [][]byte{simpleReadingBytes, binaryReadingBytes} + readingsData := [][]byte{simpleReadingBytes, binaryReadingBytes, objectReadingBytes} expectedReadings := []models.Reading{ - simpleReading, binaryReading, + simpleReading, binaryReading, objectReading, } events, err := convertObjectsToReadings(readingsData) diff --git a/openapi/v2/core-data.yaml b/openapi/v2/core-data.yaml index 2dce05aa6e..b8b6f3cff1 100644 --- a/openapi/v2/core-data.yaml +++ b/openapi/v2/core-data.yaml @@ -2,7 +2,7 @@ openapi: 3.0.0 info: title: Edgex Foundry - Core Data API description: This is the definition of the API for the Core Data service in the EdgeX Foundry IOT microservice platform. Core Data is responsible for storing event and reading data ingested from edge devices in the environment. - version: 2.1.0 + version: 2.2.0 servers: - url: http://localhost:59880/api/v2 @@ -69,6 +69,7 @@ components: - Int64Array - Float32Array - Float64Array + - Object required: - deviceName - resourceName @@ -134,6 +135,18 @@ components: required: - binaryValue - mediaType + ObjectReading: + description: "An event reading for a object data type" + allOf: + - $ref: '#/components/schemas/ObjectReading' + - type: object + properties: + objectValue: + description: "If the value type of the reading is Object, it will be found in this property as a object value" + type: string + format: byte + required: + - objectValue CountResponse: allOf: - $ref: '#/components/schemas/BaseResponse' @@ -465,6 +478,16 @@ components: valueType: "Binary" binaryValue: [12,34] mediaType: "image" + - deviceName: "device-002" + resourceName: "resource-003" + profileName: "profile-002" + id: "71c601d9-cb56-453a-8c75-54461e444715" + origin: 1602168089665565300 + valueType: "Object" + objectValue: + Attr1: "yyz" + Attr2: -45 + Attr3: [ 255, 1, 0 ] - apiVersion: "v2" deviceName: "device-002" profileName: "profile-002" @@ -513,6 +536,16 @@ components: origin: 1602168089665565300 valueType: "Float32" value: "12.2" + - deviceName: "device-002" + resourceName: "resource-003" + profileName: "profile-002" + id: "71c601d9-cb56-453a-8c75-54461e444715" + origin: 1602168089665565300 + valueType: "Object" + objectValue: + Attr1: "yyz" + Attr2: -45 + Attr3: [255, 1, 0] ReadingsByResourceNameAndTimeRangeExample: value: apiVersion: "v2"