Skip to content

Commit

Permalink
Daily generation filter
Browse files Browse the repository at this point in the history
  • Loading branch information
kubaceg committed Nov 5, 2024
1 parent 375fc69 commit fcd77e1
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 6 deletions.
24 changes: 24 additions & 0 deletions adapters/filters/daily_generation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package filters

import "errors"

const maxDailyGenerationDiff = 10000

type DailyGenerationFilter struct {
lastDailyGenerationValue int
}

func NewDailyGenerationFilter() *DailyGenerationFilter {
return &DailyGenerationFilter{}
}

func (d *DailyGenerationFilter) Filter(data map[string]interface{}) (map[string]interface{}, error) {
if d.lastDailyGenerationValue > 0 &&
data["PV_Generation_Today"].(int)-d.lastDailyGenerationValue > maxDailyGenerationDiff {
return nil, errors.New("PV generation today diff is too high, skipping")
}

d.lastDailyGenerationValue = data["PV_Generation_Today"].(int)

return data, nil
}
65 changes: 65 additions & 0 deletions adapters/filters/daily_generation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package filters

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
)

func TestFilterCases(t *testing.T) {
tests := []struct {
name string
lastDailyGenerationValue int
data map[string]interface{}
expectedResult map[string]interface{}
expectedError error
}{
{
name: "ValidData",
lastDailyGenerationValue: 0,
data: map[string]interface{}{"PV_Generation_Today": 5000},
expectedResult: map[string]interface{}{"PV_Generation_Today": 5000},
expectedError: nil,
},
{
name: "DiffTooHigh",
lastDailyGenerationValue: 9000,
data: map[string]interface{}{"PV_Generation_Today": 20000},
expectedResult: nil,
expectedError: errors.New("PV generation today diff is too high, skipping"),
},
{
name: "FirstDataPoint",
lastDailyGenerationValue: 0,
data: map[string]interface{}{"PV_Generation_Today": 15000},
expectedResult: map[string]interface{}{"PV_Generation_Today": 15000},
expectedError: nil,
},
{
name: "ExactMaxDiff",
lastDailyGenerationValue: 10000,
data: map[string]interface{}{"PV_Generation_Today": 20000},
expectedResult: map[string]interface{}{"PV_Generation_Today": 20000},
expectedError: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
filter := NewDailyGenerationFilter()
filter.lastDailyGenerationValue = tt.lastDailyGenerationValue

result, err := filter.Filter(tt.data)

if tt.expectedError != nil {
assert.Error(t, err)
assert.Equal(t, tt.expectedError.Error(), err.Error())
} else {
assert.NoError(t, err)
}

assert.Equal(t, tt.expectedResult, result)
})
}
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.19
require (
github.com/eclipse/paho.mqtt.golang v1.3.1
github.com/sigurn/crc16 v0.0.0-20211026045750-20ab5afb07e3
github.com/stretchr/testify v1.8.2
go.bug.st/serial v1.4.0
go.opentelemetry.io/otel v1.14.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v0.37.0
Expand All @@ -18,11 +19,13 @@ require (
require (
github.com/cenkalti/backoff/v4 v4.2.0 // indirect
github.com/creack/goselect v0.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.14.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.37.0 // indirect
go.opentelemetry.io/otel/trace v1.14.0 // indirect
Expand All @@ -33,4 +36,5 @@ require (
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
google.golang.org/grpc v1.53.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ github.com/creack/goselect v0.1.2 h1:2DNy14+JPjRBgPzAd1thbQp4BSIihxcBf0IXhQXDRa0
github.com/creack/goselect v0.1.2/go.mod h1:a/NhLweNvqIYMuxcMOuWY516Cimucms3DglDzQP3hKY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/eclipse/paho.mqtt.golang v1.3.1 h1:6F5FYb1hxVSZS+p0ji5xBQamc5ltOolTYRy5R15uVmI=
github.com/eclipse/paho.mqtt.golang v1.3.1/go.mod h1:eTzb4gxwwyWpqBUHGQZ4ABAV7+Jgm1PklsYT/eo8Hcc=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
Expand Down Expand Up @@ -153,10 +154,15 @@ github.com/sigurn/crc16 v0.0.0-20211026045750-20ab5afb07e3 h1:aQKxg3+2p+IFXXg97M
github.com/sigurn/crc16 v0.0.0-20211026045750-20ab5afb07e3/go.mod h1:9/etS5gpQq9BJsJMWg1wpLbfuSnkm8dPF6FdW2JXVhA=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down Expand Up @@ -451,6 +457,7 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
23 changes: 17 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/kubaceg/sofar_g3_lsw3_logger_reader/adapters/devices/sofar"
"github.com/kubaceg/sofar_g3_lsw3_logger_reader/adapters/export/mosquitto"
"github.com/kubaceg/sofar_g3_lsw3_logger_reader/adapters/export/otlp"

"github.com/kubaceg/sofar_g3_lsw3_logger_reader/adapters/filters"
"github.com/kubaceg/sofar_g3_lsw3_logger_reader/ports"
)

Expand All @@ -23,11 +23,12 @@ import (
const maximumFailedConnections = 3

var (
config *Config
port ports.CommunicationPort
mqtt ports.DatabaseWithListener
device ports.Device
telem *otlp.Service
config *Config
port ports.CommunicationPort
mqtt ports.DatabaseWithListener
device ports.Device
telem *otlp.Service
dataFilters []ports.Filter

hasMQTT bool
hasOTLP bool
Expand Down Expand Up @@ -66,6 +67,8 @@ func initialize() {
}
}

dataFilters = []ports.Filter{filters.NewDailyGenerationFilter()}

device = sofar.NewSofarLogger(config.Inverter.LoggerSerial, port)
}

Expand All @@ -89,6 +92,14 @@ func main() {
continue
}

for _, filter := range dataFilters {
measurements, err = filter.Filter(measurements)
if err != nil {
log.Printf("filter failed: %s", err)
continue
}
}

failedConnections = 0

if hasMQTT {
Expand Down
5 changes: 5 additions & 0 deletions ports/filters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package ports

type Filter interface {
Filter(map[string]interface{}) (map[string]interface{}, error)
}

0 comments on commit fcd77e1

Please sign in to comment.