mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-29 13:49:28 +08:00

* Add starter kit Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add http Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add cmd/main.go Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Remove reference to things from README.md Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add starter kit Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add http Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add cmd/main.go Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Remove reference to things from README.md Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Fix env vars in README.md Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Fix env vars in README.md Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Rename kit to mfxkit Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Rename kit to mfxkit Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add docker compose related files Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add docker compose related files Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add Dockerfile to mfxkit Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add Dockerfile to mfxkit Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add twin service to docker-compose.yml Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add twin service to docker-compose.yml Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add mongo db connection Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add mongo db connection Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add TwinRepository mockup Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Fix docker env vars Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Fix docker env vars Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add twins repo mongodb routines Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add twins repo mongodb routines Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add mongo db docker test suite Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add mongo db docker test suite Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add idp and toDBTwin Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add idp and toDBTwin Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add test cases to repo twin save test Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add test cases to repo twin save test Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add users grpc Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add users grpc Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add methods and tests for udpate and update key Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add methods and tests for udpate and update key Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add methods and tests for remove twin Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add methods and tests for remove twin Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add svc methods to loggin and metrics Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add svc methods to loggin and metrics Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add AddTwin endpoint Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add AddTwin endpoint Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add update endpoints Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add update endpoints Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add view and remove endpoints Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add view and remove endpoints Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add twin repo mock Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add twin repo mock Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add owner arg to twins repo methods Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add owner arg to twins repo methods Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add mock idp service Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add mock users service Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add tests for service Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add tests for service Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add mqtt client to twins service Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add mqtt client to twins service Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add protocol to mqtt string var Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add protocol to mqtt string var Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add time and attributes related data to Twin struct Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add time and attributes related data to Twin struct Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add mqtt publish JSON serialized twin to service Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add mqtt publish JSON serialized twin to service Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add endpoint tests for save and update twin Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add endpoint tests for save and update twin Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add endpoint tests for update key and view thing Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add endpoint tests for update key and view thing Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Fix test error for view twin Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Fix test error for view twin Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add endpoint tests for remove twin Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add endpoint tests for remove twin Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add NATS client Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add NATS client Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add normalizer to nats Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add normalizer to nats Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Refactor nats publish() and remove normalizer Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Refactor nats publish() and remove normalizer Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add ListThingsByChannel() and RetrieveByChannel() Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add ListThingsByChannel() and RetrieveByChannel() Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add Twin struct directly in mongodb Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add Twin struct directly in mongodb Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Change limit type to uint64 Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Change limit type to uint64 Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Compose nats subject from msg ch and subtopic Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Compose nats subject from msg ch and subtopic Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add mqtt notifs for thing creation and key update Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add mqtt notifs for thing creation and key update Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add RetrieveAll to twinRepository and ListTwins to service Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add RetrieveAll to twinRepository and ListTwins to service Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add tests for RetrieveAll and ListTwins Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add tests for RetrieveAll and ListTwins Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Fix Service interface error Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Remove test Ping endpoint Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add mqtt topic to service config Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Remove UpdateKey request and add fields to Twin related requests Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add Attributes and State add and view Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add ListTwins endpoint Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Fix service and database tests Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add ListTwinsByThing endpoint Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Remove twin directory Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add States and Definitions to Twin Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add defer publish to AddTwin Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add refactored and deferred mqtt client publish to Service methods Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add paho subservice Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add paho client to nats Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add mqtt publish and state update Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add Publish wrapper Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Use paho Publish wrapper Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Fix tests Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Save states in separate collection Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Replace []byte payload by []map[string]interface{} Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add offset to PageMetadata Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Rename TwinsPage to Page Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add StateRepository Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add StateRepository, mongodb StateRepository adapter and ListStates endpoint Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add RetrieveLast twin to states repository Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Parse def in nats to update state attrib Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add prepareState() helper Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Replace list things by id by view thing by id Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add state repo mock Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Fix service tests Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Fix mongo twins repo tests and remove owner from RetrieveByID params Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Fix mongo twins repo tests and remove owner from Remove params Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Replace ChannelID in Attribute by Channel Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Fix endpoint tests for add and update twin Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Fix endpoint tests for view and remove twin Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Use new auth service Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Fix mocks auth and mongodb tests Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Fix licensing info Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Fix 'for for' Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Replace short dec by var for default vals and add string map for mqtt op info Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Use Record from mainflux/senml and rename broker to nats in main.go Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Remove Key from Twin Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Remove redundant id uniqueness check from mongodb Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add attrib name to attrib update info Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Fix typos and remove isZeroOfUnderlyingType() helper Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Rename paho to mqtt Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Fix env vars in main.go and README.md Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Move twins/api/twins to twins/api and rename twins- prefix to tw- in main.go Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Remove flows for manual testing and revert to master docker-compose.yml Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Remove publish from nats and fix tests by updating package names Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Rename users.go to authn.go Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Fix crud op names Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Change id to twinID Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Inline if err != nil and change <prefix>ID to <prefix>_id Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Fix metadata db search test Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Rename mqtt and nats related files to publisher and subscriber respectively Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Move save state logic from nats to service Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Fix endpoint tests Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Remove close check from main.go Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Complete deploy section Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add unit tests for states Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Remove debugging artefacts and unneeded comments to exported funcs Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add port num to defAuthnURL Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>
309 lines
8.1 KiB
Go
309 lines
8.1 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package twins
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/mainflux/mainflux"
|
|
"github.com/mainflux/mainflux/twins/mqtt"
|
|
"github.com/mainflux/senml"
|
|
"github.com/nats-io/go-nats"
|
|
)
|
|
|
|
var (
|
|
// ErrMalformedEntity indicates malformed entity specification (e.g.
|
|
// invalid username or password).
|
|
ErrMalformedEntity = errors.New("malformed entity specification")
|
|
|
|
// ErrUnauthorizedAccess indicates missing or invalid credentials provided
|
|
// when accessing a protected resource.
|
|
ErrUnauthorizedAccess = errors.New("missing or invalid credentials provided")
|
|
|
|
// ErrNotFound indicates a non-existent entity request.
|
|
ErrNotFound = errors.New("non-existent entity")
|
|
|
|
// ErrConflict indicates that entity already exists.
|
|
ErrConflict = errors.New("entity already exists")
|
|
)
|
|
|
|
// Service specifies an API that must be fullfiled by the domain service
|
|
// implementation, and all of its decorators (e.g. logging & metrics).
|
|
type Service interface {
|
|
// AddTwin adds new twin related to user identified by the provided key.
|
|
AddTwin(context.Context, string, Twin, Definition) (Twin, error)
|
|
|
|
// UpdateTwin updates twin identified by the provided Twin that
|
|
// belongs to the user identified by the provided key.
|
|
UpdateTwin(context.Context, string, Twin, Definition) error
|
|
|
|
// ViewTwin retrieves data about twin with the provided
|
|
// ID belonging to the user identified by the provided key.
|
|
ViewTwin(context.Context, string, string) (Twin, error)
|
|
|
|
// ListTwins retrieves data about subset of twins that belongs to the
|
|
// user identified by the provided key.
|
|
ListTwins(context.Context, string, uint64, uint64, string, Metadata) (TwinsPage, error)
|
|
|
|
// ListStates retrieves data about subset of states that belongs to the
|
|
// twin identified by the id.
|
|
ListStates(context.Context, string, uint64, uint64, string) (StatesPage, error)
|
|
|
|
// SaveState persists state into database
|
|
SaveState(*mainflux.Message) error
|
|
|
|
// ListTwinsByThing retrieves data about subset of twins that represent
|
|
// specified thing belong to the user identified by
|
|
// the provided key.
|
|
ViewTwinByThing(context.Context, string, string) (Twin, error)
|
|
|
|
// RemoveTwin removes the twin identified with the provided ID, that
|
|
// belongs to the user identified by the provided key.
|
|
RemoveTwin(context.Context, string, string) error
|
|
}
|
|
|
|
var crudOp = map[string]string{
|
|
"createSucc": "create/success",
|
|
"createFail": "create/failure",
|
|
"updateSucc": "update/success",
|
|
"updateFail": "update/failure",
|
|
"getSucc": "get/success",
|
|
"getFail": "get/failure",
|
|
"removeSucc": "remove/success",
|
|
"removeFail": "remove/failure",
|
|
}
|
|
|
|
type twinsService struct {
|
|
natsClient *nats.Conn
|
|
mqttClient mqtt.Mqtt
|
|
auth mainflux.AuthNServiceClient
|
|
twins TwinRepository
|
|
states StateRepository
|
|
idp IdentityProvider
|
|
}
|
|
|
|
var _ Service = (*twinsService)(nil)
|
|
|
|
// New instantiates the twins service implementation.
|
|
func New(nc *nats.Conn, mc mqtt.Mqtt, auth mainflux.AuthNServiceClient, twins TwinRepository, sr StateRepository, idp IdentityProvider) Service {
|
|
return &twinsService{
|
|
natsClient: nc,
|
|
mqttClient: mc,
|
|
auth: auth,
|
|
twins: twins,
|
|
states: sr,
|
|
idp: idp,
|
|
}
|
|
}
|
|
|
|
func (ts *twinsService) AddTwin(ctx context.Context, token string, twin Twin, def Definition) (tw Twin, err error) {
|
|
var id string
|
|
var b []byte
|
|
defer ts.mqttClient.Publish(&id, &err, crudOp["createSucc"], crudOp["createFail"], &b)
|
|
|
|
res, err := ts.auth.Identify(ctx, &mainflux.Token{Value: token})
|
|
if err != nil {
|
|
return Twin{}, ErrUnauthorizedAccess
|
|
}
|
|
|
|
twin.ID, err = ts.idp.ID()
|
|
if err != nil {
|
|
return Twin{}, err
|
|
}
|
|
|
|
twin.Owner = res.GetValue()
|
|
|
|
twin.Created = time.Now()
|
|
twin.Updated = time.Now()
|
|
|
|
if len(def.Attributes) == 0 {
|
|
def = Definition{}
|
|
def.Attributes = make(map[string]Attribute)
|
|
}
|
|
def.Created = time.Now()
|
|
def.ID = 0
|
|
twin.Definitions = append(twin.Definitions, def)
|
|
|
|
twin.Revision = 0
|
|
if _, err = ts.twins.Save(ctx, twin); err != nil {
|
|
return Twin{}, err
|
|
}
|
|
|
|
id = twin.ID
|
|
b, err = json.Marshal(twin)
|
|
|
|
return twin, nil
|
|
}
|
|
|
|
func (ts *twinsService) UpdateTwin(ctx context.Context, token string, twin Twin, def Definition) (err error) {
|
|
var b []byte
|
|
var id string
|
|
defer ts.mqttClient.Publish(&id, &err, crudOp["updateSucc"], crudOp["updateFail"], &b)
|
|
|
|
_, err = ts.auth.Identify(ctx, &mainflux.Token{Value: token})
|
|
if err != nil {
|
|
return ErrUnauthorizedAccess
|
|
}
|
|
|
|
tw, err := ts.twins.RetrieveByID(ctx, twin.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
tw.Updated = time.Now()
|
|
tw.Revision++
|
|
|
|
if twin.Name != "" {
|
|
tw.Name = twin.Name
|
|
}
|
|
|
|
if twin.ThingID != "" {
|
|
tw.ThingID = twin.ThingID
|
|
}
|
|
|
|
if len(def.Attributes) > 0 {
|
|
def.Created = time.Now()
|
|
def.ID = tw.Definitions[len(tw.Definitions)-1].ID + 1
|
|
tw.Definitions = append(tw.Definitions, def)
|
|
}
|
|
|
|
if len(twin.Metadata) == 0 {
|
|
tw.Metadata = twin.Metadata
|
|
}
|
|
|
|
if err := ts.twins.Update(ctx, tw); err != nil {
|
|
return err
|
|
}
|
|
|
|
id = twin.ID
|
|
b, err = json.Marshal(tw)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (ts *twinsService) ViewTwin(ctx context.Context, token, id string) (tw Twin, err error) {
|
|
var b []byte
|
|
defer ts.mqttClient.Publish(&id, &err, crudOp["getSucc"], crudOp["getFail"], &b)
|
|
|
|
_, err = ts.auth.Identify(ctx, &mainflux.Token{Value: token})
|
|
if err != nil {
|
|
return Twin{}, ErrUnauthorizedAccess
|
|
}
|
|
|
|
twin, err := ts.twins.RetrieveByID(ctx, id)
|
|
if err != nil {
|
|
return Twin{}, err
|
|
}
|
|
|
|
b, err = json.Marshal(twin)
|
|
|
|
return twin, nil
|
|
}
|
|
|
|
func (ts *twinsService) ViewTwinByThing(ctx context.Context, token, thingid string) (Twin, error) {
|
|
_, err := ts.auth.Identify(ctx, &mainflux.Token{Value: token})
|
|
if err != nil {
|
|
return Twin{}, ErrUnauthorizedAccess
|
|
}
|
|
|
|
return ts.twins.RetrieveByThing(ctx, thingid)
|
|
}
|
|
|
|
func (ts *twinsService) RemoveTwin(ctx context.Context, token, id string) (err error) {
|
|
var b []byte
|
|
defer ts.mqttClient.Publish(&id, &err, crudOp["removeSucc"], crudOp["removeFail"], &b)
|
|
|
|
_, err = ts.auth.Identify(ctx, &mainflux.Token{Value: token})
|
|
if err != nil {
|
|
return ErrUnauthorizedAccess
|
|
}
|
|
|
|
if err := ts.twins.Remove(ctx, id); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (ts *twinsService) ListTwins(ctx context.Context, token string, offset uint64, limit uint64, name string, metadata Metadata) (TwinsPage, error) {
|
|
res, err := ts.auth.Identify(ctx, &mainflux.Token{Value: token})
|
|
if err != nil {
|
|
return TwinsPage{}, ErrUnauthorizedAccess
|
|
}
|
|
|
|
return ts.twins.RetrieveAll(ctx, res.GetValue(), offset, limit, name, metadata)
|
|
}
|
|
|
|
func (ts *twinsService) SaveState(msg *mainflux.Message) error {
|
|
var b []byte
|
|
var id string
|
|
var err error
|
|
defer ts.mqttClient.Publish(&id, &err, crudOp["stateSucc"], crudOp["stateFail"], &b)
|
|
|
|
tw, err := ts.twins.RetrieveByThing(context.TODO(), msg.Publisher)
|
|
if err != nil {
|
|
return fmt.Errorf("Retrieving twin for %s failed: %s", msg.Publisher, err)
|
|
}
|
|
|
|
var recs []senml.Record
|
|
if err := json.Unmarshal(msg.Payload, &recs); err != nil {
|
|
return fmt.Errorf("Unmarshal payload for %s failed: %s", msg.Publisher, err)
|
|
}
|
|
|
|
st, err := ts.states.RetrieveLast(context.TODO(), tw.ID)
|
|
if err != nil {
|
|
return fmt.Errorf("Retrieve last state for %s failed: %s", msg.Publisher, err)
|
|
}
|
|
|
|
if save := prepareState(&st, &tw, recs, msg); !save {
|
|
return nil
|
|
}
|
|
|
|
if err := ts.states.Save(context.TODO(), st); err != nil {
|
|
return fmt.Errorf("Updating state for %s failed: %s", msg.Publisher, err)
|
|
}
|
|
|
|
id = msg.Publisher
|
|
b = msg.Payload
|
|
|
|
return nil
|
|
}
|
|
|
|
func (ts *twinsService) ListStates(ctx context.Context, token string, offset uint64, limit uint64, id string) (StatesPage, error) {
|
|
_, err := ts.auth.Identify(ctx, &mainflux.Token{Value: token})
|
|
if err != nil {
|
|
return StatesPage{}, ErrUnauthorizedAccess
|
|
}
|
|
|
|
return ts.states.RetrieveAll(ctx, offset, limit, id)
|
|
}
|
|
|
|
func prepareState(st *State, tw *Twin, recs []senml.Record, msg *mainflux.Message) bool {
|
|
def := tw.Definitions[len(tw.Definitions)-1]
|
|
st.TwinID = tw.ID
|
|
st.ID++
|
|
st.Created = time.Now()
|
|
st.Definition = def.ID
|
|
if st.Payload == nil {
|
|
st.Payload = make(map[string]interface{})
|
|
}
|
|
|
|
save := false
|
|
for k, a := range def.Attributes {
|
|
if !a.PersistState {
|
|
continue
|
|
}
|
|
if a.Channel == msg.Channel && a.Subtopic == msg.Subtopic {
|
|
st.Payload[k] = recs[0].Value
|
|
save = true
|
|
break
|
|
}
|
|
}
|
|
|
|
return save
|
|
}
|