Skip to content

Commit

Permalink
simplify code
Browse files Browse the repository at this point in the history
- remove support for "consumer" external switchable energy consumers.
- use slog instead of zerolog
- disable flaky undeterministic tests
- rewrite control loop without concurrency
  • Loading branch information
yvesf committed Aug 26, 2023
1 parent 04e8990 commit bd04dd2
Show file tree
Hide file tree
Showing 34 changed files with 864 additions and 1,393 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ The author and the project are not affiliated with the Victron Company. The bran
only in educational context. Everything here is shared for educational purpose only and
for use at your own risk only.

## Code structure

- `cmd/` commands/servers/entrypoints
- `pkg/` potentially re-usable packages.

## Usage

Interactive mode:
Expand Down Expand Up @@ -52,12 +57,9 @@ Configure NixOS Module:
enable = true;
maxInverter = 120;
maxInverterPeak = 800;
shellyUrl = "http://shellyem3-.1.localnet.cc";
consumer = [{
url = "shelly1://shellyplus1pm-/";
power = 350;
delaySec = 5;
}];
shellyEM3 = "http://shellyem3-.1.localnet.cc";
};
}
```
```


45 changes: 36 additions & 9 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,59 @@ package cmd

import (
"context"
"errors"
"flag"
"net"
"net/http"
"os"
"time"

"github.com/yvesf/ve-ctrl-tool/mk2"
"github.com/bsm/openmetrics"
"github.com/bsm/openmetrics/omhttp"
"github.com/yvesf/ve-ctrl-tool/pkg/mk2"
"github.com/yvesf/ve-ctrl-tool/pkg/timemock"
"github.com/yvesf/ve-ctrl-tool/pkg/vebus"

"github.com/rs/zerolog"
"golang.org/x/exp/slog"
)

var (
flagSerialDevice = flag.String("serialDevice", "/dev/ttyUSB0", "Device")
flagLow = flag.Bool("low", false, "Do not attempt to upgrade to 115200 baud")
flagVEAddress = flag.Int("veAddress", 0, "Set other address than 0")
flagDebug = flag.Bool("debug", false, "Set log level to debug")
flagTrace = flag.Bool("trace", false, "Set log level to trace (overrides -debug)")
flagMetricsHTTP = flag.String("metricsHTTP", "", "Address of a http server serving metrics under /metrics")
)

func CommonInit(ctx context.Context) *mk2.Adapter {
flag.Parse()

zerolog.SetGlobalLevel(zerolog.InfoLevel)
logLevel := slog.LevelInfo
if *flagDebug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
logLevel = slog.LevelDebug
}
if *flagTrace {
zerolog.SetGlobalLevel(zerolog.TraceLevel)
h := slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: logLevel})
slog.SetDefault(slog.New(h))

// Metrics HTTP endpoint
if *flagMetricsHTTP != `` {
mux := http.NewServeMux()
mux.Handle("/metrics", omhttp.NewHandler(openmetrics.DefaultRegistry()))

var lc net.ListenConfig
ln, err := lc.Listen(ctx, "tcp", *flagMetricsHTTP)
if err != nil {
slog.Error("Listen on http failed", slog.String("addr", *flagMetricsHTTP))
os.Exit(1)
}

srv := &http.Server{Handler: mux}
go func() {
err := srv.Serve(ln)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
slog.Error("http server failed", slog.Any("err", err))
os.Exit(1)
}
}()
}

mk2, err := mk2.NewAdapter(*flagSerialDevice)
Expand All @@ -41,7 +68,7 @@ func CommonInit(ctx context.Context) *mk2.Adapter {
panic(err)
}
mk2.Write(vebus.CommandR.Frame().Marshal())
time.Sleep(time.Second * 1)
timemock.Sleep(time.Second * 1)
err = mk2.SetBaudLow()
if err != nil {
panic(err)
Expand Down
Loading

0 comments on commit bd04dd2

Please sign in to comment.