mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-02 22:17:10 +08:00

* MF-1154 - Move UUID provider to project root Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Use naming uuidProvider/up instead of identityProvider/idp Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix typo Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Mutualize UUID mocks Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Move uuid into pkg directory Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
72 lines
1.7 KiB
Go
72 lines
1.7 KiB
Go
package mocks
|
|
|
|
import (
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"github.com/mainflux/mainflux/pkg/messaging"
|
|
"github.com/mainflux/mainflux/pkg/uuid"
|
|
"github.com/mainflux/mainflux/twins"
|
|
"github.com/mainflux/senml"
|
|
)
|
|
|
|
const (
|
|
publisher = "twins"
|
|
)
|
|
|
|
// NewService use mock dependencies to create real twins service
|
|
func NewService(tokens map[string]string) twins.Service {
|
|
auth := NewAuthNServiceClient(tokens)
|
|
twinsRepo := NewTwinRepository()
|
|
statesRepo := NewStateRepository()
|
|
uuidProvider := uuid.NewMock()
|
|
subs := map[string]string{"chanID": "chanID"}
|
|
broker := NewBroker(subs)
|
|
return twins.New(broker, auth, twinsRepo, statesRepo, uuidProvider, "chanID", nil)
|
|
}
|
|
|
|
// CreateDefinition creates twin definition
|
|
func CreateDefinition(names []string, subtopics []string) twins.Definition {
|
|
var def twins.Definition
|
|
for i, v := range names {
|
|
id, _ := uuid.New().ID()
|
|
attr := twins.Attribute{
|
|
Name: v,
|
|
Channel: id,
|
|
Subtopic: subtopics[i],
|
|
PersistState: true,
|
|
}
|
|
def.Attributes = append(def.Attributes, attr)
|
|
}
|
|
return def
|
|
}
|
|
|
|
// CreateSenML creates SenML record array
|
|
func CreateSenML(n int, bn string) []senml.Record {
|
|
var recs []senml.Record
|
|
for i := 0; i < n; i++ {
|
|
rec := senml.Record{
|
|
BaseName: bn,
|
|
BaseTime: float64(time.Now().Unix()),
|
|
Time: float64(i),
|
|
Value: nil,
|
|
}
|
|
recs = append(recs, rec)
|
|
}
|
|
return recs
|
|
}
|
|
|
|
// CreateMessage creates Mainflux message using SenML record array
|
|
func CreateMessage(attr twins.Attribute, recs []senml.Record) (*messaging.Message, error) {
|
|
mRecs, err := json.Marshal(recs)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &messaging.Message{
|
|
Channel: attr.Channel,
|
|
Subtopic: attr.Subtopic,
|
|
Payload: mRecs,
|
|
Publisher: publisher,
|
|
}, nil
|
|
}
|