From d2add5517bc5353d335ff3965557d906f1815a21 Mon Sep 17 00:00:00 2001 From: Mirko Teodorovic Date: Mon, 10 Feb 2020 15:42:25 +0100 Subject: [PATCH] NOISSUE - Export config from content (#21) * add view services via mqtt Signed-off-by: Mirko Teodorovic * small fix Signed-off-by: Mirko Teodorovic * minor changes Signed-off-by: Mirko Teodorovic * update comment Signed-off-by: Mirko Teodorovic * add different endpoint for services view Signed-off-by: Mirko Teodorovic * dont use pointers Signed-off-by: Mirko Teodorovic * fix comments Signed-off-by: Mirko Teodorovic * fix errror return Signed-off-by: Mirko Teodorovic * inline function Signed-off-by: Mirko Teodorovic * add export config Signed-off-by: Mirko Teodorovic * small changes Signed-off-by: Mirko Teodorovic * remove cmdType type Signed-off-by: Mirko Teodorovic * remove pointers Signed-off-by: Mirko Teodorovic * add support for writing export config Signed-off-by: Mirko Teodorovic * remove parenthesis Signed-off-by: Mirko Teodorovic * remove println Signed-off-by: Mirko Teodorovic * NOISSUE - add view services via mqtt (#19) * add view services via mqtt Signed-off-by: Mirko Teodorovic * small fix Signed-off-by: Mirko Teodorovic * minor changes Signed-off-by: Mirko Teodorovic * update comment Signed-off-by: Mirko Teodorovic * add different endpoint for services view Signed-off-by: Mirko Teodorovic * dont use pointers Signed-off-by: Mirko Teodorovic * fix comments Signed-off-by: Mirko Teodorovic * fix errror return Signed-off-by: Mirko Teodorovic * inline function Signed-off-by: Mirko Teodorovic * small changes Signed-off-by: Mirko Teodorovic * remove cmdType type Signed-off-by: Mirko Teodorovic * remove pointers Signed-off-by: Mirko Teodorovic * remove parenthesis Signed-off-by: Mirko Teodorovic * add service info type Signed-off-by: Mirko Teodorovic * add default file Signed-off-by: Mirko Teodorovic * add checking err Signed-off-by: Mirko Teodorovic * Enable mtls communication with mqtt broker (#15) * enable mtls Signed-off-by: Mirko Teodorovic * enable mtls Signed-off-by: Mirko Teodorovic * fix config endpoint Signed-off-by: Mirko Teodorovic * fix double broker connection Signed-off-by: Mirko Teodorovic * fix disconnect problem Signed-off-by: Mirko Teodorovic * killing the white lines Signed-off-by: Mirko Teodorovic * small corrections Signed-off-by: Mirko Teodorovic * remove some types and typos Signed-off-by: Mirko Teodorovic * fix logger usage Signed-off-by: Mirko Teodorovic * mqtt is moved to conn Signed-off-by: Mirko Teodorovic * resolve comments Signed-off-by: Mirko Teodorovic * resolve comments Signed-off-by: Mirko Teodorovic * kill whit line Signed-off-by: Mirko Teodorovic * update gitignore Signed-off-by: Mirko Teodorovic * dont export vars Signed-off-by: Mirko Teodorovic * remove vscode Signed-off-by: Mirko Teodorovic * add export const Signed-off-by: Mirko Teodorovic * add len check for senml payload Signed-off-by: Mirko Teodorovic * change constant exportConfigFile Signed-off-by: Mirko Teodorovic --- .gitignore | 2 +- internal/app/agent/service.go | 10 +++++----- internal/app/agent/services/service.go | 10 +++++----- internal/pkg/bootstrap/bootstrap.go | 24 +++++++++++++++++++----- internal/pkg/conn/conn.go | 6 +++++- 5 files changed, 35 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 6565ee3d..2b1c8246 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ build -cmd/config.toml \ No newline at end of file +cmd/config.toml diff --git a/internal/app/agent/service.go b/internal/app/agent/service.go index 50b6c762..41710fe5 100644 --- a/internal/app/agent/service.go +++ b/internal/app/agent/service.go @@ -16,7 +16,7 @@ import ( "github.com/mainflux/agent/internal/app/agent/services" "github.com/mainflux/agent/internal/pkg/config" "github.com/mainflux/agent/pkg/edgex" - export "github.com/mainflux/export/pkg/config" + exp "github.com/mainflux/export/pkg/config" "github.com/mainflux/mainflux/errors" log "github.com/mainflux/mainflux/logger" "github.com/mainflux/senml" @@ -31,6 +31,8 @@ const ( view = "view" save = "save" + + export = "export" ) var ( @@ -200,7 +202,6 @@ func (a *agent) ServiceConfig(uuid, cmdStr string) error { } resp := "" cmd := cmdArgs[0] - switch cmd { case view: services, err := json.Marshal(a.Services()) @@ -235,13 +236,12 @@ func (a *agent) processResponse(uuid, cmd, resp string) error { func (a *agent) saveConfig(service, fileName, fileCont string) error { switch service { - case "export": + case export: content, err := base64.StdEncoding.DecodeString(fileCont) if err != nil { return err } - - c := &export.Config{} + c := &exp.Config{} if err := c.ReadBytes([]byte(content)); err != nil { return err } diff --git a/internal/app/agent/services/service.go b/internal/app/agent/services/service.go index 5ad6a786..c02f9fd4 100644 --- a/internal/app/agent/services/service.go +++ b/internal/app/agent/services/service.go @@ -9,8 +9,8 @@ const ( timeout = 3 interval = 10000 - Online = "online" - Offline = "offline" + online = "online" + offline = "offline" ) type Service struct { @@ -27,7 +27,7 @@ type Service struct { func NewService(name string) *Service { ticker := time.NewTicker(interval * time.Millisecond) done := make(chan bool) - s := Service{Name: name, Status: Online, done: done, counter: timeout, ticker: ticker} + s := Service{Name: name, Status: online, done: done, counter: timeout, ticker: ticker} s.Listen() return &s } @@ -42,7 +42,7 @@ func (s *Service) Listen() { s.mu.Lock() s.counter = s.counter - 1 if s.counter == 0 { - s.Status = Offline + s.Status = offline s.counter = timeout } s.mu.Unlock() @@ -56,5 +56,5 @@ func (s *Service) Update() { s.mu.Lock() defer s.mu.Unlock() s.counter = timeout - s.Status = Online + s.Status = online } diff --git a/internal/pkg/bootstrap/bootstrap.go b/internal/pkg/bootstrap/bootstrap.go index cef64940..123bf5b6 100644 --- a/internal/pkg/bootstrap/bootstrap.go +++ b/internal/pkg/bootstrap/bootstrap.go @@ -14,10 +14,13 @@ import ( "github.com/mainflux/agent/internal/app/agent" "github.com/mainflux/agent/internal/pkg/config" + export "github.com/mainflux/export/pkg/config" log "github.com/mainflux/mainflux/logger" "github.com/mainflux/mainflux/things" ) +const exportConfigFile = "/configs/export/config.toml" + // Config represents the parameters for boostraping type Config struct { URL string @@ -36,11 +39,12 @@ type deviceConfig struct { } type infraConfig struct { - LogLevel string `json:"log_level"` - HTTPPort string `json:"http_port"` - MqttURL string `json:"mqtt_url"` - EdgexURL string `json:"edgex_url"` - NatsURL string `json:"nats_url"` + LogLevel string `json:"log_level"` + HTTPPort string `json:"http_port"` + MqttURL string `json:"mqtt_url"` + EdgexURL string `json:"edgex_url"` + NatsURL string `json:"nats_url"` + ExportConfig export.Config `json:"export_config"` } // Bootstrap - Retrieve device config @@ -80,6 +84,16 @@ func Bootstrap(cfg Config, logger log.Logger, file string) error { if err := json.Unmarshal([]byte(dc.Content), &ic); err != nil { return err } + econf := &ic.ExportConfig + if econf != nil { + if econf.File == "" { + econf.File = exportConfigFile + } + logger.Info(fmt.Sprintf("Saving export config file %s", econf.File)) + if err := econf.Save(); err != nil { + logger.Error(fmt.Sprintf("Failed to save export config file %s", err)) + } + } if len(dc.MainfluxChannels) < 2 { return agent.ErrMalformedEntity diff --git a/internal/pkg/conn/conn.go b/internal/pkg/conn/conn.go index 2450a2bf..1e33ea3b 100644 --- a/internal/pkg/conn/conn.go +++ b/internal/pkg/conn/conn.go @@ -101,6 +101,10 @@ func (b *broker) handleMsg(mc mqtt.Client, msg mqtt.Message) { return } + if len(sm.Records) == 0 { + b.logger.Error(fmt.Sprintf("SenML payload empty: `%s`", string(msg.Payload()))) + return + } cmdType := sm.Records[0].Name cmdStr := *sm.Records[0].StringValue uuid := strings.TrimSuffix(sm.Records[0].BaseName, ":") @@ -119,7 +123,7 @@ func (b *broker) handleMsg(mc mqtt.Client, msg mqtt.Message) { case config: b.logger.Info(fmt.Sprintf("Config service for uuid %s and command string %s", uuid, cmdStr)) if err := b.svc.ServiceConfig(uuid, cmdStr); err != nil { - b.logger.Warn(fmt.Sprintf("Config service operation failed: %s", err)) + b.logger.Warn(fmt.Sprintf("Execute operation failed: %s", err)) } case service: b.logger.Info(fmt.Sprintf("Services view for uuid %s and command string %s", uuid, cmdStr))