Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ type LoggingConfig struct {
}

type SungrowConfig struct {
Host string `validate:"required"`
Username string
Password string
Host string `validate:"required"`
Username string
Password string
ReconnectTries uint
}

type Config struct {
Expand Down Expand Up @@ -142,8 +143,9 @@ func loadDefaults(v *viper.Viper) error {
Format: AutoLoggingFormat,
},
Sungrow: SungrowConfig{
Username: "user",
Password: "pw1111",
Username: "user",
Password: "pw1111",
ReconnectTries: 3,
},
}

Expand Down
8 changes: 7 additions & 1 deletion internal/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ import (
func Run(c config.Config) error {
logger := zerolog.New(c.Logging.Format.Writer()).With().Timestamp().Logger().Level(c.Logging.Level)

sg := redgiant.NewSungrow(c.Sungrow.Host, c.Sungrow.Username, c.Sungrow.Password, redgiant.WithLogger(logger))
sg := redgiant.NewSungrow(
c.Sungrow.Host,
c.Sungrow.Username,
c.Sungrow.Password,
redgiant.WithLogger(logger),
redgiant.WithReconnect(c.Sungrow.ReconnectTries),
)
rg := redgiant.NewRedgiant(sg, redgiant.WithLogger(logger))

if err := rg.Connect(); err != nil {
Expand Down
14 changes: 6 additions & 8 deletions sungrow.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,15 @@ func (s *Sungrow) reconnect() error {
s.Close()

var err error
if s.reconnectTries >= 1 {

for try := range s.reconnectTries {
s.log.Info().Uint("try", try).Msg("reconnecting")
if err = s.Connect(); err == nil {
return nil
}
for try := range s.reconnectTries {
s.log.Info().Uint("try", try).Msg("reconnecting")
if err = s.Connect(); err == nil {
return nil
}
// FIXME: implement proper backoff here
time.Sleep(time.Second * 20)
}

s.log.Error().Err(err).Msg("unable to reconnect")
return newSungrowDisconnectedError("unable to reconnect")
}

Expand Down