Skip to content

Commit

Permalink
Merge pull request #6 from atbore-phx/feat/max-battery-capacity
Browse files Browse the repository at this point in the history
feat(fronius): Battery Max Capacity is now retrived from local API vi…
  • Loading branch information
atbore-phx authored Mar 4, 2024
2 parents d11624d + aab0425 commit 58d7cff
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 36 deletions.
2 changes: 1 addition & 1 deletion pkg/cmd/estimate.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var estCmd = &cobra.Command{
}

str := storage.New()
_, err = str.Handler(fronius_ip)
_, _, err = str.Handler(fronius_ip)
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ var scdCmd = &cobra.Command{
}

str := storage.New()
capacity2charge, err := str.Handler(fronius_ip)
capacity2charge, capacity_max, err := str.Handler(fronius_ip)
if err != nil {
panic(err)
}
u.Log.Infof("your Daily consumption is:%d W", int(pw_consumption))

scd := fronius.New()
_, err = scd.Handler(solarPowerProduction, capacity2charge, pw_consumption, max_charge, start_hr, end_hr, fronius_ip)
_, err = scd.Handler(solarPowerProduction, capacity2charge, capacity_max, pw_consumption, max_charge, start_hr, end_hr, fronius_ip)
if err != nil {
panic(err)
}
Expand Down
19 changes: 10 additions & 9 deletions pkg/fronius/fronius_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,16 @@ func TestHandler(t *testing.T) {
assert := assert.New(t)
fronius := fronius.New()

pwForecast := 1.0
pwBatt2charge := 1.0
pwConsumption := 1.0
maxCharge := 5
pwForecast := 1000.0
pwBatt2charge := 1000.0
pwBattMax := 10000.0
pwConsumption := 9000.0
maxCharge := 3500
startHr := "09:00"
endHr := "17:00"

setup()
_, err := fronius.Handler(pwForecast, pwBatt2charge, pwConsumption, maxCharge, startHr, endHr, modbus_ip, modbus_port)
_, err := fronius.Handler(pwForecast, pwBatt2charge, pwBattMax, pwConsumption, maxCharge, startHr, endHr, modbus_ip, modbus_port)
teardown()

assert.NoError(err, "Handler returned an error")
Expand Down Expand Up @@ -142,7 +143,7 @@ func TestCheckTimeRange(t *testing.T) {
func TestBatteryChargeMode1(t *testing.T) {
assert := assert.New(t)
setup()
result, err := fronius.SetFroniusChargeBatteryMode(1, 0, 9, 3500, "00:00", "05:00", modbus_ip, modbus_port)
result, err := fronius.SetFroniusChargeBatteryMode(1000, 0, 11000, 9000, 3500, "00:00", "05:00", modbus_ip, modbus_port)
assert.Equal(int16(0), result, "SetFroniusChargeBatteryMode returned wrong value")
assert.NoError(err, "CheckTimeRange returned an error")

Expand All @@ -153,8 +154,8 @@ func TestBatteryChargeMode2(t *testing.T) {
assert := assert.New(t)
setup()

result, err := fronius.SetFroniusChargeBatteryMode(1, 50, 9, 3500, "00:00", "23:59", modbus_ip, modbus_port)
assert.Equal(int16(0), result, "SetFroniusChargeBatteryMode returned wrong value")
result, err := fronius.SetFroniusChargeBatteryMode(1000, 11000, 11000, 9000, 3500, "00:00", "23:59", modbus_ip, modbus_port)
assert.Equal(int16(31), result, "SetFroniusChargeBatteryMode returned wrong value")
assert.NoError(err, "CheckTimeRange returned an error")

teardown()
Expand All @@ -164,7 +165,7 @@ func TestBatteryChargeMode3(t *testing.T) {
assert := assert.New(t)
setup()

result, err := fronius.SetFroniusChargeBatteryMode(10, 50, 9, 3500, "00:00", "23:59", modbus_ip, modbus_port)
result, err := fronius.SetFroniusChargeBatteryMode(10000, 5000, 11000, 9000, 3500, "00:00", "23:59", modbus_ip, modbus_port)
assert.Equal(int16(0), result, "SetFroniusChargeBatteryMode returned wrong value")
assert.NoError(err, "CheckTimeRange returned an error")

Expand Down
4 changes: 2 additions & 2 deletions pkg/fronius/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ func New() *Fronius {
return &Fronius{}
}

func (fronius *Fronius) Handler(pw_forecast float64, pw_batt2charge float64, pw_consumption float64, max_charge int, start_hr string, end_hr string, fronius_ip string, fronius_port ...string) (int16, error) {
func (fronius *Fronius) Handler(pw_forecast float64, pw_batt2charge float64, pw_batt_max float64, pw_consumption float64, max_charge int, start_hr string, end_hr string, fronius_ip string, fronius_port ...string) (int16, error) {
p := "502"
if len(fronius_port) > 0 {
p = fronius_port[0]
}

charge_pc, _ := SetFroniusChargeBatteryMode(pw_forecast, pw_batt2charge, pw_consumption, max_charge, start_hr, end_hr, fronius_ip, p)
charge_pc, _ := SetFroniusChargeBatteryMode(pw_forecast, pw_batt2charge, pw_batt_max, pw_consumption, max_charge, start_hr, end_hr, fronius_ip, p)

return charge_pc, nil

Expand Down
10 changes: 3 additions & 7 deletions pkg/fronius/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"
)

func SetFroniusChargeBatteryMode(pw_forecast float64, pw_batt2charge float64, pw_consumption float64, max_charge int, start_hr string, end_hr string, fronius_ip string, fronius_port ...string) (int16, error) {
func SetFroniusChargeBatteryMode(pw_forecast float64, pw_batt2charge float64, pw_batt_max float64, pw_consumption float64, max_charge int, start_hr string, end_hr string, fronius_ip string, fronius_port ...string) (int16, error) {
p := "502"
if len(fronius_port) > 0 {
p = fronius_port[0]
Expand All @@ -22,17 +22,13 @@ func SetFroniusChargeBatteryMode(pw_forecast float64, pw_batt2charge float64, pw
Setdefaults(fronius_ip, p)
} else { // in the time range
pw_pv_net := pw_forecast - pw_consumption
// TODO: we know that from Estimate via Solar API, use that vaule instead.
OpenModbusClient(fronius_ip, p)
pw_max, _ := ReadFroniusModbusRegister(WChaMax)
ClosemodbusClient()
if pw_pv_net <= 0 { // net pv power is not enough
u.Log.Infof("Net Forecast Power is not enough: %f W", pw_pv_net)
pw_batt := float64(pw_max) - pw_batt2charge
pw_batt := float64(pw_batt_max) - pw_batt2charge
pw_grid = pw_batt + pw_pv_net
if pw_grid < 0 { // Battery Capacity is not enough => charge the diff
u.Log.Infof("Battery Capacity is not enough: %f W", pw_batt)
ch_pc = SetChargePower(float64(pw_max), -1*pw_grid, float64(max_charge))
ch_pc = SetChargePower(float64(pw_batt_max), -1*pw_grid, float64(max_charge))
ForceCharge(fronius_ip, ch_pc, p)
} else { // Battery Capacity is enough => do not charge
u.Log.Infof("Battery Capacity is enough: %f W", pw_batt)
Expand Down
9 changes: 5 additions & 4 deletions pkg/storage/charge.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package storage
import (
"encoding/json"
"errors"
u "sbam/src/utils"
"net/http"
u "sbam/src/utils"
"time"
)

Expand Down Expand Up @@ -35,7 +35,7 @@ func GetStorage(fronius_ip string) (Batteries, error) {
return batteries, nil
}

func GetCapacityStorage2Charge(batteries Batteries) (float64, error) {
func GetCapacityStorage2Charge(batteries Batteries) (float64, float64, error) {
capacity := 0.0
status := 0.0
disabled := true
Expand All @@ -50,9 +50,10 @@ func GetCapacityStorage2Charge(batteries Batteries) (float64, error) {

if disabled {
err := errors.New("Battery Cluster is disabled")
return capacity - status, err
return capacity - status, capacity, err
}
tc := capacity - status
u.Log.Infof("Battery Capacity to charge: %d W", int(tc))
return tc, nil
u.Log.Infof("Battery Capacity Max: %d W", int(capacity))
return tc, capacity, nil
}
11 changes: 6 additions & 5 deletions pkg/storage/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@ func New() *Storage {
return &Storage{}
}

func (storage *Storage) Handler(fronius_ip string) (float64, error) {
func (storage *Storage) Handler(fronius_ip string) (float64, float64, error) {
charge := 0.0
charge_max := 0.0

batteries, err := GetStorage(fronius_ip)
if err != nil {
u.Log.Errorln("Error getting Storage Charge Data:", err)
return charge, err
return charge, charge_max, err
}

charge, err = GetCapacityStorage2Charge(batteries)
charge, charge_max, err = GetCapacityStorage2Charge(batteries)
if err != nil {
u.Log.Errorln("Error getting Full Storage Capacity to Charge:", err)
return charge, err
return charge, charge_max, err
}
return charge, nil
return charge, charge_max, nil

}
35 changes: 29 additions & 6 deletions pkg/storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestGetCapacityStorage2Charge(t *testing.T) {
t.Errorf("Error getting storage data: %s", err)
}

capacity, err := storage.GetCapacityStorage2Charge(batteries)
capacity, _, err := storage.GetCapacityStorage2Charge(batteries)
if err != nil {
t.Errorf("Error getting storage capacity: %s", err)
}
Expand All @@ -80,17 +80,37 @@ func TestGetCapacityStorage2Charge(t *testing.T) {
teardown()
}

func TestGetCapacityStorageMax(t *testing.T) {
setup()

ip := strings.TrimPrefix(mockServer.URL, "http://")
batteries, err := storage.GetStorage(ip)
if err != nil {
t.Errorf("Error getting storage data: %s", err)
}

_, capacity_max, err := storage.GetCapacityStorage2Charge(batteries)
if err != nil {
t.Errorf("Error getting storage capacity: %s", err)
}

assert.Equal(t, 24868.0, capacity_max)

teardown()
}

func TestHandler(t *testing.T) {
setup()

st := storage.New()
ip := strings.TrimPrefix(mockServer.URL, "http://")
charge, err := st.Handler(ip)
charge, charge_max, err := st.Handler(ip)
if err != nil {
t.Errorf("Error getting storage charge: %s", err)
}

assert.Equal(t, 6133.32, charge)
assert.Equal(t, 24868.0, charge_max)
assert.NoError(t, err)

teardown()
}
Expand All @@ -102,8 +122,9 @@ func TestHandlerError(t *testing.T) {

mockServer.Close() // Simulate an error by closing the mock server

charge, err := storage.Handler(mockServer.URL)
charge, charge_max, err := storage.Handler(mockServer.URL)
assert.Equal(t, float64(0), charge)
assert.Equal(t, float64(0), charge_max)
assert.Error(t, err)

teardown()
Expand All @@ -122,8 +143,9 @@ func TestHandlerError2(t *testing.T) {
}
}))

charge, err := st.Handler(mockServer.URL)
charge, charge_max, err := st.Handler(mockServer.URL)
assert.Equal(t, float64(0), charge)
assert.Equal(t, float64(0), charge_max)
assert.Error(t, err)

teardown()
Expand Down Expand Up @@ -151,8 +173,9 @@ func TestGetCapacityStorage2ChargeError(t *testing.T) {
},
}

capacity, err := storage.GetCapacityStorage2Charge(batteries)
capacity, capacity_max, err := storage.GetCapacityStorage2Charge(batteries)
assert.Equal(t, float64(0), capacity)
assert.Equal(t, float64(0), capacity_max)
assert.Error(t, err)

teardown()
Expand Down

0 comments on commit 58d7cff

Please sign in to comment.