@@ -9,21 +9,24 @@ import (
9
9
"context"
10
10
"encoding/json"
11
11
"fmt"
12
+ "strconv"
12
13
"strings"
13
14
14
15
pgClient "github.com/edgexfoundry/edgex-go/internal/pkg/db/postgres"
15
16
dbModels "github.com/edgexfoundry/edgex-go/internal/pkg/infrastructure/postgres/models"
17
+ "github.com/edgexfoundry/go-mod-core-contracts/v4/common"
16
18
"github.com/edgexfoundry/go-mod-core-contracts/v4/errors"
17
19
model "github.com/edgexfoundry/go-mod-core-contracts/v4/models"
18
20
19
21
"github.com/google/uuid"
20
22
"github.com/jackc/pgx/v5"
23
+ "github.com/jackc/pgx/v5/pgtype"
21
24
"github.com/jackc/pgx/v5/pgxpool"
22
25
)
23
26
24
27
var (
25
28
// insertReadingCols defines the reading table columns in slice used in inserting readings
26
- insertReadingCols = []string {eventIdFKCol , deviceInfoIdFKCol , originCol , valueCol , binaryValueCol , objectValueCol }
29
+ insertReadingCols = []string {eventIdFKCol , deviceInfoIdFKCol , originCol , valueCol , numericValueCol , binaryValueCol , objectValueCol }
27
30
)
28
31
29
32
func (c * Client ) ReadingTotalCount () (uint32 , errors.EdgeX ) {
@@ -279,6 +282,33 @@ func queryReadings(ctx context.Context, connPool *pgxpool.Pool, sql string, args
279
282
Value : * readingDBModel .Value ,
280
283
}
281
284
reading = simpleReading
285
+ } else if readingDBModel .NumericValue != nil {
286
+ // reading type is numeric SimpleReading
287
+ var val string
288
+ switch baseReading .ValueType {
289
+ case common .ValueTypeFloat32 :
290
+ numericVal , err := readingDBModel .NumericValue .Float64Value ()
291
+ if err != nil {
292
+ return nil , pgClient .WrapDBError ("failed to read the float value" , err )
293
+ }
294
+ val = strconv .FormatFloat (numericVal .Float64 , 'e' , - 1 , 32 )
295
+ case common .ValueTypeFloat64 :
296
+ numericVal , err := readingDBModel .NumericValue .Float64Value ()
297
+ if err != nil {
298
+ return nil , pgClient .WrapDBError ("failed to read the float value" , err )
299
+ }
300
+ val = strconv .FormatFloat (numericVal .Float64 , 'e' , - 1 , 64 )
301
+ default :
302
+ numericVal , err := readingDBModel .NumericValue .Value () // NumericValue.Value() return value in string
303
+ if err != nil {
304
+ return nil , pgClient .WrapDBError ("failed to read the float value" , err )
305
+ }
306
+ val = fmt .Sprintf ("%v" , numericVal )
307
+ }
308
+ reading = model.SimpleReading {
309
+ BaseReading : baseReading ,
310
+ Value : val ,
311
+ }
282
312
} else {
283
313
// reading type is NullReading
284
314
nullReading := model.NullReading {
@@ -373,8 +403,30 @@ func (c *Client) addReadingsInTx(tx pgx.Tx, readings []model.Reading, eventId st
373
403
case model.SimpleReading :
374
404
// convert SimpleReading struct to Reading DB model
375
405
readingDBModel = dbModels.Reading {
376
- BaseReading : baseReading ,
377
- SimpleReading : dbModels.SimpleReading {Value : & contractReadingModel .Value },
406
+ BaseReading : baseReading ,
407
+ }
408
+ switch contractReadingModel .ValueType {
409
+ case common .ValueTypeFloat32 :
410
+ var numericVal pgtype.Numeric
411
+ if err := numericVal .ScanScientific (contractReadingModel .Value ); err != nil {
412
+ return errors .NewCommonEdgeX (errors .KindContractInvalid , fmt .Sprintf ("invalid numeric value '%s'" , contractReadingModel .Value ), err )
413
+ }
414
+ readingDBModel .SimpleReading = dbModels.SimpleReading {NumericValue : & numericVal }
415
+ case common .ValueTypeFloat64 :
416
+ var numericVal pgtype.Numeric
417
+ if err := numericVal .ScanScientific (contractReadingModel .Value ); err != nil {
418
+ return errors .NewCommonEdgeX (errors .KindContractInvalid , fmt .Sprintf ("invalid numeric value '%s'" , contractReadingModel .Value ), err )
419
+ }
420
+ readingDBModel .SimpleReading = dbModels.SimpleReading {NumericValue : & numericVal }
421
+ case common .ValueTypeUint8 , common .ValueTypeUint16 , common .ValueTypeUint32 , common .ValueTypeUint64 ,
422
+ common .ValueTypeInt8 , common .ValueTypeInt16 , common .ValueTypeInt32 , common .ValueTypeInt64 :
423
+ var val pgtype.Numeric
424
+ if err := val .Scan (contractReadingModel .Value ); err != nil {
425
+ return errors .NewCommonEdgeX (errors .KindContractInvalid , fmt .Sprintf ("invalid numeric value '%s'" , contractReadingModel .Value ), err )
426
+ }
427
+ readingDBModel .SimpleReading = dbModels.SimpleReading {NumericValue : & val }
428
+ default :
429
+ readingDBModel .SimpleReading = dbModels.SimpleReading {Value : & contractReadingModel .Value }
378
430
}
379
431
case model.NullReading :
380
432
readingDBModel = dbModels.Reading {
@@ -413,6 +465,7 @@ func (c *Client) addReadingsInTx(tx pgx.Tx, readings []model.Reading, eventId st
413
465
deviceInfoId ,
414
466
r .Origin ,
415
467
r .Value ,
468
+ r .NumericValue ,
416
469
r .BinaryValue ,
417
470
objectValueBytes ,
418
471
}, nil
0 commit comments