2018-08-26 13:15:48 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2018
|
|
|
|
// Mainflux
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
|
2018-06-01 15:50:23 +02:00
|
|
|
package mongodb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2019-04-16 14:58:56 +02:00
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
2018-06-01 15:50:23 +02:00
|
|
|
|
|
|
|
"github.com/mainflux/mainflux"
|
|
|
|
"github.com/mainflux/mainflux/writers"
|
|
|
|
)
|
|
|
|
|
|
|
|
const collectionName string = "mainflux"
|
|
|
|
|
|
|
|
var _ writers.MessageRepository = (*mongoRepo)(nil)
|
|
|
|
|
|
|
|
type mongoRepo struct {
|
|
|
|
db *mongo.Database
|
|
|
|
}
|
|
|
|
|
2018-11-05 19:18:51 +01:00
|
|
|
// Message struct is used as a MongoDB representation of Mainflux message.
|
|
|
|
type message struct {
|
2018-12-05 13:09:25 +01:00
|
|
|
Channel string `bson:"channel,omitempty"`
|
2019-03-15 18:38:07 +01:00
|
|
|
Subtopic string `bson:"subtopic,omitempty"`
|
2018-12-05 13:09:25 +01:00
|
|
|
Publisher string `bson:"publisher,omitempty"`
|
2018-11-05 19:18:51 +01:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2018-06-01 15:50:23 +02:00
|
|
|
// New returns new MongoDB writer.
|
2018-08-08 13:38:34 +02:00
|
|
|
func New(db *mongo.Database) writers.MessageRepository {
|
|
|
|
return &mongoRepo{db}
|
2018-06-01 15:50:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *mongoRepo) Save(msg mainflux.Message) error {
|
|
|
|
coll := repo.db.Collection(collectionName)
|
2018-11-05 19:18:51 +01:00
|
|
|
m := message{
|
|
|
|
Channel: msg.Channel,
|
2019-03-15 18:38:07 +01:00
|
|
|
Subtopic: msg.Subtopic,
|
2018-11-05 19:18:51 +01:00
|
|
|
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)
|
2018-06-01 15:50:23 +02:00
|
|
|
return err
|
|
|
|
}
|