1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-05-01 13:48:56 +08:00
Aleksandar Novaković 5799356b14 MF-549 - Change metadata format from JSON string to JSON object (#706)
* Update metadata type in things service

Update things service so that metadata has map type. Update repo
implementation by adding sqlx lib.

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Add sqlx lib to bootstrap service

Add sqlx lib to bootstrap service and update metadata field type.

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Update metadata in redis streams consumer

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Update tests for bootstrap service

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Fix mongo reader logging and driver version

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Fix mongo reader and writer

Fix mongo reader and writer by updating driver version.

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Update SDK with new metadata format

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Update LoRa adapter with new metadata format

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Update users service in order to use sqlx

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Replace anonymous struct with map

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Update docs for LoRa adapter

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Fix LoRa application metadata format

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Fix metadata format in LoRa docs

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Add metadata2 var to SDK things test

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-04-16 14:58:56 +02:00

87 lines
2.1 KiB
Go

//
// Copyright (c) 2018
// Mainflux
//
// SPDX-License-Identifier: Apache-2.0
//
package mongodb
import (
"context"
"go.mongodb.org/mongo-driver/mongo"
"github.com/mainflux/mainflux"
"github.com/mainflux/mainflux/writers"
)
const collectionName string = "mainflux"
var _ writers.MessageRepository = (*mongoRepo)(nil)
type mongoRepo struct {
db *mongo.Database
}
// Message struct is used as a MongoDB representation of Mainflux message.
type message struct {
Channel string `bson:"channel,omitempty"`
Subtopic string `bson:"subtopic,omitempty"`
Publisher string `bson:"publisher,omitempty"`
Protocol string `bson:"protocol,omitempty"`
Name string `bson:"name,omitempty"`
Unit string `bson:"unit,omitempty"`
FloatValue *float64 `bson:"value,omitempty"`
StringValue *string `bson:"stringValue,omitempty"`
BoolValue *bool `bson:"boolValue,omitempty"`
DataValue *string `bson:"dataValue,omitempty"`
ValueSum *float64 `bson:"valueSum,omitempty"`
Time float64 `bson:"time,omitempty"`
UpdateTime float64 `bson:"updateTime,omitempty"`
Link string `bson:"link,omitempty"`
}
// New returns new MongoDB writer.
func New(db *mongo.Database) writers.MessageRepository {
return &mongoRepo{db}
}
func (repo *mongoRepo) Save(msg mainflux.Message) error {
coll := repo.db.Collection(collectionName)
m := message{
Channel: msg.Channel,
Subtopic: msg.Subtopic,
Publisher: msg.Publisher,
Protocol: msg.Protocol,
Name: msg.Name,
Unit: msg.Unit,
Time: msg.Time,
UpdateTime: msg.UpdateTime,
Link: msg.Link,
}
switch msg.Value.(type) {
case *mainflux.Message_FloatValue:
v := msg.GetFloatValue()
m.FloatValue = &v
case *mainflux.Message_StringValue:
v := msg.GetStringValue()
m.StringValue = &v
case *mainflux.Message_DataValue:
v := msg.GetDataValue()
m.DataValue = &v
case *mainflux.Message_BoolValue:
v := msg.GetBoolValue()
m.BoolValue = &v
}
if msg.GetValueSum() != nil {
valueSum := msg.GetValueSum().Value
m.ValueSum = &valueSum
}
_, err := coll.InsertOne(context.Background(), m)
return err
}