Skip to content

Commit

Permalink
NOISSUE - Export config from content (#21)
Browse files Browse the repository at this point in the history
* add view services via mqtt

Signed-off-by: Mirko Teodorovic <[email protected]>

* small fix

Signed-off-by: Mirko Teodorovic <[email protected]>

* minor changes

Signed-off-by: Mirko Teodorovic <[email protected]>

* update comment

Signed-off-by: Mirko Teodorovic <[email protected]>

* add different endpoint for services view

Signed-off-by: Mirko Teodorovic <[email protected]>

* dont use pointers

Signed-off-by: Mirko Teodorovic <[email protected]>

* fix comments

Signed-off-by: Mirko Teodorovic <[email protected]>

* fix errror return

Signed-off-by: Mirko Teodorovic <[email protected]>

* inline function

Signed-off-by: Mirko Teodorovic <[email protected]>

* add export config

Signed-off-by: Mirko Teodorovic <[email protected]>

* small changes

Signed-off-by: Mirko Teodorovic <[email protected]>

* remove cmdType type

Signed-off-by: Mirko Teodorovic <[email protected]>

* remove pointers

Signed-off-by: Mirko Teodorovic <[email protected]>

* add support for writing export config

Signed-off-by: Mirko Teodorovic <[email protected]>

* remove parenthesis

Signed-off-by: Mirko Teodorovic <[email protected]>

* remove println

Signed-off-by: Mirko Teodorovic <[email protected]>

* NOISSUE - add view services via mqtt (#19)

* add view services via mqtt

Signed-off-by: Mirko Teodorovic <[email protected]>

* small fix

Signed-off-by: Mirko Teodorovic <[email protected]>

* minor changes

Signed-off-by: Mirko Teodorovic <[email protected]>

* update comment

Signed-off-by: Mirko Teodorovic <[email protected]>

* add different endpoint for services view

Signed-off-by: Mirko Teodorovic <[email protected]>

* dont use pointers

Signed-off-by: Mirko Teodorovic <[email protected]>

* fix comments

Signed-off-by: Mirko Teodorovic <[email protected]>

* fix errror return

Signed-off-by: Mirko Teodorovic <[email protected]>

* inline function

Signed-off-by: Mirko Teodorovic <[email protected]>

* small changes

Signed-off-by: Mirko Teodorovic <[email protected]>

* remove cmdType type

Signed-off-by: Mirko Teodorovic <[email protected]>

* remove pointers

Signed-off-by: Mirko Teodorovic <[email protected]>

* remove parenthesis

Signed-off-by: Mirko Teodorovic <[email protected]>

* add service info type

Signed-off-by: Mirko Teodorovic <[email protected]>

* add default file

Signed-off-by: Mirko Teodorovic <[email protected]>

* add checking err

Signed-off-by: Mirko Teodorovic <[email protected]>

* Enable mtls communication with mqtt broker (#15)

* enable mtls

Signed-off-by: Mirko Teodorovic <[email protected]>

* enable mtls

Signed-off-by: Mirko Teodorovic <[email protected]>

* fix config endpoint

Signed-off-by: Mirko Teodorovic <[email protected]>

* fix double broker connection

Signed-off-by: Mirko Teodorovic <[email protected]>

* fix disconnect problem

Signed-off-by: Mirko Teodorovic <[email protected]>

* killing the white lines

Signed-off-by: Mirko Teodorovic <[email protected]>

* small corrections

Signed-off-by: Mirko Teodorovic <[email protected]>

* remove some types and typos

Signed-off-by: Mirko Teodorovic <[email protected]>

* fix logger usage

Signed-off-by: Mirko Teodorovic <[email protected]>

* mqtt is moved to conn

Signed-off-by: Mirko Teodorovic <[email protected]>

* resolve comments

Signed-off-by: Mirko Teodorovic <[email protected]>

* resolve comments

Signed-off-by: Mirko Teodorovic <[email protected]>

* kill whit line

Signed-off-by: Mirko Teodorovic <[email protected]>

* update gitignore

Signed-off-by: Mirko Teodorovic <[email protected]>

* dont export vars

Signed-off-by: Mirko Teodorovic <[email protected]>

* remove vscode

Signed-off-by: Mirko Teodorovic <[email protected]>

* add export const

Signed-off-by: Mirko Teodorovic <[email protected]>

* add len check for senml payload

Signed-off-by: Mirko Teodorovic <[email protected]>

* change constant exportConfigFile

Signed-off-by: Mirko Teodorovic <[email protected]>
  • Loading branch information
mteodor committed Feb 10, 2020
1 parent c8bcea3 commit d2add55
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
build
cmd/config.toml
cmd/config.toml
10 changes: 5 additions & 5 deletions internal/app/agent/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -31,6 +31,8 @@ const (

view = "view"
save = "save"

export = "export"
)

var (
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions internal/app/agent/services/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const (
timeout = 3
interval = 10000

Online = "online"
Offline = "offline"
online = "online"
offline = "offline"
)

type Service struct {
Expand All @@ -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
}
Expand All @@ -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()
Expand All @@ -56,5 +56,5 @@ func (s *Service) Update() {
s.mu.Lock()
defer s.mu.Unlock()
s.counter = timeout
s.Status = Online
s.Status = online
}
24 changes: 19 additions & 5 deletions internal/pkg/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion internal/pkg/conn/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ":")
Expand All @@ -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))
Expand Down

0 comments on commit d2add55

Please sign in to comment.