Skip to content

Commit

Permalink
feat: Using isNull field instead of nil reading value (#1639)
Browse files Browse the repository at this point in the history
* feat: Using isNull field instead of nil reading value

Using isNull instead of nil reading value

Signed-off-by: bruce <weichou1229@gmail.com>

* feat: create commandValue with nil value

create commandValue with nil value

Signed-off-by: bruce <weichou1229@gmail.com>

* feat: upgrade core-contracts

upgrade core-contracts to v3.2.0-dev.53

Signed-off-by: bruce <weichou1229@gmail.com>

---------

Signed-off-by: bruce <weichou1229@gmail.com>
  • Loading branch information
weichou1229 authored Oct 17, 2024
1 parent 63928f2 commit 791e380
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 11 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.23
require (
github.com/OneOfOne/xxhash v1.2.8
github.com/edgexfoundry/go-mod-bootstrap/v3 v3.2.0-dev.66
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.2.0-dev.50
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.2.0-dev.53
github.com/edgexfoundry/go-mod-messaging/v3 v3.2.0-dev.40
github.com/google/uuid v1.6.0
github.com/hashicorp/go-multierror v1.1.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ github.com/edgexfoundry/go-mod-bootstrap/v3 v3.2.0-dev.66 h1:kmBEAhNi4ftrJMXM3Iv
github.com/edgexfoundry/go-mod-bootstrap/v3 v3.2.0-dev.66/go.mod h1:3IXVpc5Qez5nwFJ8IkMyJMba8Iavj620E0XB42BQzfQ=
github.com/edgexfoundry/go-mod-configuration/v3 v3.2.0-dev.19 h1:274NZdVBkJBuQP6yT3tVrb7psTFuIPogX/DLQqv7OCQ=
github.com/edgexfoundry/go-mod-configuration/v3 v3.2.0-dev.19/go.mod h1:BG6hCDxXizpgMdNEljwNfLWSsd4Op7GAHd3Pis1dVv8=
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.2.0-dev.50 h1:Ho1TAQki5DN/ALdYP4YGNzj/Z/CXBXSeBancA+YHtO4=
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.2.0-dev.50/go.mod h1:MLk37/79M26+bZr3IptNZuYmQBEVbXwzDp1VHQkFhIk=
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.2.0-dev.53 h1:aHnYwqpg0LcnMrgNQlkRQjzHAS/IML/9GI368OmNCz4=
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.2.0-dev.53/go.mod h1:MLk37/79M26+bZr3IptNZuYmQBEVbXwzDp1VHQkFhIk=
github.com/edgexfoundry/go-mod-messaging/v3 v3.2.0-dev.40 h1:YyB21HEapV5pENG01vFlpjPI6UkmKpJuaWFfgGFVUsY=
github.com/edgexfoundry/go-mod-messaging/v3 v3.2.0-dev.40/go.mod h1:8NpZ6/eAsiyZHgn/s3DRIpcOjUrve+ZONIgvcDvA3Yg=
github.com/edgexfoundry/go-mod-registry/v3 v3.2.0-dev.18 h1:AzILZ/xcEmSYVhIwSF4zkWpXyFoBA733E/j8ttzlNnI=
Expand Down
8 changes: 8 additions & 0 deletions internal/application/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,14 @@ func validateServiceAndDeviceState(deviceName string, dic *di.Container) (models
}

func createCommandValueFromDeviceResource(dr models.DeviceResource, value interface{}) (*sdkModels.CommandValue, errors.EdgeX) {
if value == nil {
return &sdkModels.CommandValue{
DeviceResourceName: dr.Name,
Type: dr.Properties.ValueType,
Value: value,
Tags: make(map[string]string)}, nil
}

var err error
var result *sdkModels.CommandValue

Expand Down
9 changes: 3 additions & 6 deletions internal/autoevent/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,17 @@ func TestCompareReadings(t *testing.T) {
e, err := NewExecutor("device-test", autoEvent, pool)
require.NoError(t, err)

value1 := "1"
value2 := "2"
value3 := "3"
testReadings := []dtos.BaseReading{{ResourceName: "r1"}, {ResourceName: "r2"}}
testReadings[0].ValueType = common.ValueTypeInt8
testReadings[0].Value = &value1
testReadings[0].Value = "1"
testReadings[0].ValueType = common.ValueTypeInt8
testReadings[1].Value = &value2
testReadings[1].Value = "2"

firstReadings := testReadings

readingsValueChanged := make([]dtos.BaseReading, len(firstReadings))
copy(readingsValueChanged, firstReadings)
readingsValueChanged[1].Value = &value3
readingsValueChanged[1].Value = "3"

readingsResourceChanged := make([]dtos.BaseReading, len(readingsValueChanged))
copy(readingsResourceChanged, readingsValueChanged)
Expand Down
6 changes: 4 additions & 2 deletions internal/transformer/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func CommandValuesToEventDTO(cvs []*models.CommandValue, deviceName string, sour
}

// perform data transformation
if dataTransform {
if dataTransform && cv.Value != nil {
edgexErr := TransformReadResult(cv, dr.Properties)
if edgexErr != nil {
lc.Errorf("failed to transform CommandValue (%s): %v", cv.String(), edgexErr)
Expand Down Expand Up @@ -145,7 +145,9 @@ func commandValueToReading(cv *models.CommandValue, deviceName, profileName, med
var err error
var reading dtos.BaseReading

if cv.Type == common.ValueTypeBinary {
if cv.Value == nil {
reading = dtos.NewNullReading(profileName, deviceName, cv.DeviceResourceName, cv.Type)
} else if cv.Type == common.ValueTypeBinary {
var binary []byte
binary, err = cv.BinaryValue()
if err != nil {
Expand Down
52 changes: 52 additions & 0 deletions internal/transformer/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,55 @@ func TestCommandValuesToEventDTO_ReadingUnits(t *testing.T) {
})
}
}

func TestCommandValuesToEventDTO_ReadingNilValue(t *testing.T) {
dic := NewMockDIC()
err := cache.InitCache(TestDeviceService, TestDeviceService, dic)
require.NoError(t, err)
cvs := []*sdkModels.CommandValue{
testCommandNilValue(t, common.ValueTypeBool),
testCommandNilValue(t, common.ValueTypeString),
testCommandNilValue(t, common.ValueTypeUint8),
testCommandNilValue(t, common.ValueTypeUint16),
testCommandNilValue(t, common.ValueTypeUint32),
testCommandNilValue(t, common.ValueTypeUint64),
testCommandNilValue(t, common.ValueTypeInt8),
testCommandNilValue(t, common.ValueTypeInt16),
testCommandNilValue(t, common.ValueTypeInt32),
testCommandNilValue(t, common.ValueTypeInt64),
testCommandNilValue(t, common.ValueTypeFloat32),
testCommandNilValue(t, common.ValueTypeFloat64),
testCommandNilValue(t, common.ValueTypeBinary),
testCommandNilValue(t, common.ValueTypeBoolArray),
testCommandNilValue(t, common.ValueTypeStringArray),
testCommandNilValue(t, common.ValueTypeUint8Array),
testCommandNilValue(t, common.ValueTypeUint16Array),
testCommandNilValue(t, common.ValueTypeUint32Array),
testCommandNilValue(t, common.ValueTypeUint64Array),
testCommandNilValue(t, common.ValueTypeInt8Array),
testCommandNilValue(t, common.ValueTypeInt16Array),
testCommandNilValue(t, common.ValueTypeInt32Array),
testCommandNilValue(t, common.ValueTypeInt64Array),
testCommandNilValue(t, common.ValueTypeFloat32Array),
testCommandNilValue(t, common.ValueTypeFloat64Array),
testCommandNilValue(t, common.ValueTypeObject),
testCommandNilValue(t, common.ValueTypeObjectArray),
}
event, err := CommandValuesToEventDTO(cvs, TestDevice, TestDeviceCommand, true, dic)
require.NoError(t, err)

for _, r := range event.Readings {
assert.Empty(t, r.Value)
assert.Empty(t, r.BinaryValue)
assert.Empty(t, r.ObjectValue)
expectedNullReading := dtos.NewNullReading(TestProfile, TestDevice, TestDeviceResource, r.ValueType)
expectedNullReading.Id = r.Id
expectedNullReading.Origin = r.Origin
assert.Equal(t, expectedNullReading, r)
}
}
func testCommandNilValue(t *testing.T, valueType string) *sdkModels.CommandValue {
cv, e := sdkModels.NewCommandValue(TestDeviceResource, valueType, nil)
require.NoError(t, e)
return cv
}
3 changes: 3 additions & 0 deletions internal/transformer/transformparam.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import (
)

func TransformWriteParameter(cv *dsModels.CommandValue, pv models.ResourceProperties) errors.EdgeX {
if cv.Value == nil {
return nil
}
if !isNumericValueType(cv) {
return nil
}
Expand Down

0 comments on commit 791e380

Please sign in to comment.