1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-28 13:48:49 +08:00
Nick Neisen 66487eda42 MF-788 - Remove date and minimize copyright comments (#876)
* Update copyright comment for go files

Signed-off-by: nwneisen <nwneisen@gmail.com>

* Update copyright in assortment of file types

Signed-off-by: nwneisen <nwneisen@gmail.com>

* Remove missed copyright date

Signed-off-by: nwneisen <nwneisen@gmail.com>
2019-10-07 16:14:47 +02:00

77 lines
1.6 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package normalizer
import (
"strings"
"github.com/cisco/senml"
"github.com/mainflux/mainflux"
)
var formats = map[string]senml.Format{
mainflux.SenMLJSON: senml.JSON,
mainflux.SenMLCBOR: senml.CBOR,
}
type normalizer struct{}
// New returns normalizer service implementation.
func New() Service {
return normalizer{}
}
func (n normalizer) Normalize(msg mainflux.RawMessage) (NormalizedData, error) {
format, ok := formats[msg.ContentType]
if !ok {
format = senml.JSON
}
raw, err := senml.Decode(msg.Payload, format)
if err != nil {
return NormalizedData{}, err
}
normalized := senml.Normalize(raw)
msgs := make([]mainflux.Message, len(normalized.Records))
for k, v := range normalized.Records {
m := mainflux.Message{
Channel: msg.Channel,
Subtopic: msg.Subtopic,
Publisher: msg.Publisher,
Protocol: msg.Protocol,
Name: v.Name,
Unit: v.Unit,
Time: v.Time,
UpdateTime: v.UpdateTime,
Link: v.Link,
}
switch {
case v.Value != nil:
m.Value = &mainflux.Message_FloatValue{FloatValue: *v.Value}
case v.BoolValue != nil:
m.Value = &mainflux.Message_BoolValue{BoolValue: *v.BoolValue}
case v.DataValue != "":
m.Value = &mainflux.Message_DataValue{DataValue: v.DataValue}
case v.StringValue != "":
m.Value = &mainflux.Message_StringValue{StringValue: v.StringValue}
}
if v.Sum != nil {
m.ValueSum = &mainflux.SumValue{Value: *v.Sum}
}
msgs[k] = m
}
output := strings.ToLower(msg.ContentType)
return NormalizedData{
ContentType: output,
Messages: msgs,
}, nil
}