2019-10-07 08:14:47 -06:00
|
|
|
// Copyright (c) Mainflux
|
2018-08-26 13:15:48 +02:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2019-11-05 11:57:16 +01:00
|
|
|
package senml
|
2018-03-11 18:06:01 +01:00
|
|
|
|
|
|
|
import (
|
2020-06-03 15:16:19 +02:00
|
|
|
"github.com/mainflux/mainflux/pkg/errors"
|
|
|
|
"github.com/mainflux/mainflux/pkg/messaging"
|
|
|
|
"github.com/mainflux/mainflux/pkg/transformers"
|
2019-11-29 20:47:28 +01:00
|
|
|
"github.com/mainflux/senml"
|
2018-03-11 18:06:01 +01:00
|
|
|
)
|
|
|
|
|
2020-04-21 19:33:11 +02:00
|
|
|
const (
|
|
|
|
// JSON represents SenML in JSON format content type.
|
|
|
|
JSON = "application/senml+json"
|
|
|
|
// CBOR represents SenML in CBOR format content type.
|
|
|
|
CBOR = "application/senml+cbor"
|
|
|
|
)
|
|
|
|
|
2020-04-13 12:57:53 +02:00
|
|
|
var (
|
|
|
|
errDecode = errors.New("failed to decode senml")
|
2020-06-03 17:34:01 +02:00
|
|
|
errNormalize = errors.New("failed to normalize senml")
|
2020-04-13 12:57:53 +02:00
|
|
|
)
|
|
|
|
|
2019-07-25 02:22:28 +02:00
|
|
|
var formats = map[string]senml.Format{
|
2019-11-29 20:47:28 +01:00
|
|
|
JSON: senml.JSON,
|
|
|
|
CBOR: senml.CBOR,
|
2019-07-25 02:22:28 +02:00
|
|
|
}
|
|
|
|
|
2020-04-21 19:33:11 +02:00
|
|
|
type transformer struct {
|
|
|
|
format senml.Format
|
2018-03-11 18:06:01 +01:00
|
|
|
}
|
|
|
|
|
2020-04-21 19:33:11 +02:00
|
|
|
// New returns transformer service implementation for SenML messages.
|
|
|
|
func New(contentFormat string) transformers.Transformer {
|
|
|
|
format, ok := formats[contentFormat]
|
2019-07-25 02:22:28 +02:00
|
|
|
if !ok {
|
2020-04-21 19:33:11 +02:00
|
|
|
format = formats[JSON]
|
|
|
|
}
|
|
|
|
|
|
|
|
return transformer{
|
|
|
|
format: format,
|
2019-07-25 02:22:28 +02:00
|
|
|
}
|
2020-04-21 19:33:11 +02:00
|
|
|
}
|
2019-07-25 02:22:28 +02:00
|
|
|
|
2020-04-28 11:02:35 +02:00
|
|
|
func (t transformer) Transform(msg messaging.Message) (interface{}, error) {
|
2020-04-21 19:33:11 +02:00
|
|
|
raw, err := senml.Decode(msg.Payload, t.format)
|
2018-03-11 18:06:01 +01:00
|
|
|
if err != nil {
|
2020-04-13 12:57:53 +02:00
|
|
|
return nil, errors.Wrap(errDecode, err)
|
2018-03-11 18:06:01 +01:00
|
|
|
}
|
|
|
|
|
2019-11-29 20:47:28 +01:00
|
|
|
normalized, err := senml.Normalize(raw)
|
|
|
|
if err != nil {
|
2020-04-13 12:57:53 +02:00
|
|
|
return nil, errors.Wrap(errNormalize, err)
|
2019-11-29 20:47:28 +01:00
|
|
|
}
|
2018-03-11 18:06:01 +01:00
|
|
|
|
2019-11-05 11:57:16 +01:00
|
|
|
msgs := make([]Message, len(normalized.Records))
|
2019-11-29 20:47:28 +01:00
|
|
|
for i, v := range normalized.Records {
|
2020-04-13 15:05:05 +02:00
|
|
|
// Use reception timestamp if SenML messsage Time is missing
|
2020-04-28 11:02:35 +02:00
|
|
|
t := v.Time
|
|
|
|
if t == 0 {
|
2020-05-04 13:14:06 +02:00
|
|
|
// Convert the Unix timestamp in nanoseconds to float64
|
|
|
|
t = float64(msg.Created) / float64(1e9)
|
2020-04-13 15:05:05 +02:00
|
|
|
}
|
|
|
|
|
2019-11-29 20:47:28 +01:00
|
|
|
msgs[i] = Message{
|
|
|
|
Channel: msg.Channel,
|
|
|
|
Subtopic: msg.Subtopic,
|
|
|
|
Publisher: msg.Publisher,
|
|
|
|
Protocol: msg.Protocol,
|
|
|
|
Name: v.Name,
|
|
|
|
Unit: v.Unit,
|
2020-04-28 11:02:35 +02:00
|
|
|
Time: t,
|
2019-11-29 20:47:28 +01:00
|
|
|
UpdateTime: v.UpdateTime,
|
|
|
|
Value: v.Value,
|
|
|
|
BoolValue: v.BoolValue,
|
|
|
|
DataValue: v.DataValue,
|
|
|
|
StringValue: v.StringValue,
|
|
|
|
Sum: v.Sum,
|
2018-03-11 18:06:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 14:04:47 +01:00
|
|
|
return msgs, nil
|
2018-03-11 18:06:01 +01:00
|
|
|
}
|