Skip to content

Commit

Permalink
Merge pull request #3742 from weichou1229/issue-3679
Browse files Browse the repository at this point in the history
feat(data): Support Object value type in Reading
  • Loading branch information
cloudxxx8 authored Oct 8, 2021
2 parents d4ace7e + 94769bc commit ad4e2ec
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
14 changes: 14 additions & 0 deletions internal/pkg/infrastructure/redis/reading.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 22 additions & 2 deletions internal/pkg/infrastructure/redis/reading_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 34 additions & 1 deletion openapi/v2/core-data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -69,6 +69,7 @@ components:
- Int64Array
- Float32Array
- Float64Array
- Object
required:
- deviceName
- resourceName
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit ad4e2ec

Please sign in to comment.