Skip to content

Commit

Permalink
feat(storage): improve fronius tests
Browse files Browse the repository at this point in the history
  • Loading branch information
atbore-phx committed Mar 5, 2024
1 parent c3381e9 commit 80a19d8
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 28 deletions.
61 changes: 47 additions & 14 deletions pkg/fronius/configure.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package fronius

import (
"fmt"

"errors"
u "sbam/src/utils"

"github.com/simonvetter/modbus"
Expand Down Expand Up @@ -33,7 +32,7 @@ func WriteFroniusModbusRegisters(modbusStorageCfg map[uint16]int16) error {
err = modbusClient.WriteRegister(r-1, uint16(v))
if err != nil {
u.Log.Errorf("Something goes wrong writing the register: %d, value: %d", r, v)
panic(err)
return err

Check warning on line 35 in pkg/fronius/configure.go

View check run for this annotation

Codecov / codecov/patch

pkg/fronius/configure.go#L35

Added line #L35 was not covered by tests
}
}
return nil
Expand All @@ -46,7 +45,7 @@ func ReadFroniusModbusRegisters(modbusStorageCfg map[uint16]int16) ([]int16, err
u.Log.Infof("Reading register: %d ; value: %v", r, value)
if err != nil {
u.Log.Errorf("Something goes wrong reading the register: %d, value: %d", r, v)
panic(err)
return values, err

Check warning on line 48 in pkg/fronius/configure.go

View check run for this annotation

Codecov / codecov/patch

pkg/fronius/configure.go#L48

Added line #L48 was not covered by tests
}
values = append(values, int16(value))
}
Expand All @@ -58,7 +57,7 @@ func ReadFroniusModbusRegister(address uint16) (int16, error) {
u.Log.Infof("Reading register: %d ; value: %v", address, value)
if err != nil {
u.Log.Errorf("Something goes wrong reading the register: %d, value: %d", address, value)
panic(err)
return int16(value), err

Check warning on line 60 in pkg/fronius/configure.go

View check run for this annotation

Codecov / codecov/patch

pkg/fronius/configure.go#L60

Added line #L60 was not covered by tests
}
return int16(value), nil
}
Expand All @@ -70,12 +69,28 @@ func Setdefaults(modbus_ip string, port ...string) error {
}
u.Log.Info("Setting Fronius Storage Defaults start...")
regList := mdsc
OpenModbusClient(modbus_ip, p)
err = OpenModbusClient(modbus_ip, p)
if err != nil {
u.Log.Errorf("Something goes wrong %s", err)
return err
}

ReadFroniusModbusRegisters(regList)
WriteFroniusModbusRegisters(regList)
_, err = ReadFroniusModbusRegisters(regList)
if err != nil {
u.Log.Errorf("Something goes wrong %s", err)
return err

Check warning on line 81 in pkg/fronius/configure.go

View check run for this annotation

Codecov / codecov/patch

pkg/fronius/configure.go#L80-L81

Added lines #L80 - L81 were not covered by tests
}
err = WriteFroniusModbusRegisters(regList)
if err != nil {
u.Log.Errorf("Something goes wrong %s", err)
return err

Check warning on line 86 in pkg/fronius/configure.go

View check run for this annotation

Codecov / codecov/patch

pkg/fronius/configure.go#L85-L86

Added lines #L85 - L86 were not covered by tests
}

ClosemodbusClient()
err = ClosemodbusClient()
if err != nil {
u.Log.Errorf("Something goes wrong %s", err)
return err

Check warning on line 92 in pkg/fronius/configure.go

View check run for this annotation

Codecov / codecov/patch

pkg/fronius/configure.go#L91-L92

Added lines #L91 - L92 were not covered by tests
}
u.Log.Info("Setting Fronius Modbus Defaults done.")
return nil
}
Expand All @@ -92,16 +107,34 @@ func ForceCharge(modbus_ip string, power_prc int16, port ...string) error {
regList[StorCtl_Mod] = 2 // Limit Decharging
regList[OutWRte] = -100 * power_prc

OpenModbusClient(modbus_ip, p)
err = OpenModbusClient(modbus_ip, p)
if err != nil {
u.Log.Errorf("Something goes wrong %s", err)
return err
}

ReadFroniusModbusRegisters(regList)
WriteFroniusModbusRegisters(regList)
_, err = ReadFroniusModbusRegisters(regList)
if err != nil {
u.Log.Errorf("Something goes wrong %s", err)
return err

Check warning on line 119 in pkg/fronius/configure.go

View check run for this annotation

Codecov / codecov/patch

pkg/fronius/configure.go#L118-L119

Added lines #L118 - L119 were not covered by tests
}
err = WriteFroniusModbusRegisters(regList)
if err != nil {
u.Log.Errorf("Something goes wrong %s", err)
return err

Check warning on line 124 in pkg/fronius/configure.go

View check run for this annotation

Codecov / codecov/patch

pkg/fronius/configure.go#L123-L124

Added lines #L123 - L124 were not covered by tests
}

ClosemodbusClient()
err = ClosemodbusClient()
if err != nil {
u.Log.Errorf("Something goes wrong %s", err)
return err

Check warning on line 130 in pkg/fronius/configure.go

View check run for this annotation

Codecov / codecov/patch

pkg/fronius/configure.go#L129-L130

Added lines #L129 - L130 were not covered by tests
}
} else if power_prc == 0 {
Setdefaults(modbus_ip, p)
} else {
panic(fmt.Errorf("someting goes wrong when force charging, percent of charging is negative: %d", power_prc))
err = errors.New("percent of charging is negative")
u.Log.Errorf("someting goes wrong when force charging, %s", err)
return err
}
u.Log.Info("Setting Fronius Storage Force Charge done.")
return nil
Expand Down
91 changes: 91 additions & 0 deletions pkg/fronius/fronius_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ func TestSetdefaults(t *testing.T) {
assert.NoError(t, err)
}

func TestSetdefaultsError(t *testing.T) {
err := fronius.Setdefaults(modbus_ip, modbus_port)
assert.Error(t, err)
}

func TestForceCharge(t *testing.T) {

test_power_prc := []int16{
Expand All @@ -94,6 +99,32 @@ func TestForceCharge(t *testing.T) {
}
}

func TestForceChargeError(t *testing.T) {

test_power_prc := []int16{
50,
}

for _, power_prc := range test_power_prc {
err := fronius.ForceCharge(modbus_ip, power_prc, modbus_port)
assert.Error(t, err)
}
}

func TestForceCharge2(t *testing.T) {

test_power_prc := []int16{
-50,
}

for _, power_prc := range test_power_prc {
setup()
err := fronius.ForceCharge(modbus_ip, power_prc, modbus_port)
teardown()
assert.Error(t, err)
}
}

func TestHandler(t *testing.T) {
assert := assert.New(t)
fronius := fronius.New()
Expand All @@ -114,6 +145,24 @@ func TestHandler(t *testing.T) {
assert.NoError(err, "Handler returned an error")
}

func TestHandlerError(t *testing.T) {
assert := assert.New(t)
fronius := fronius.New()

pwForecast := 1000.0
pwBatt2charge := 1000.0
pwBattMax := 10000.0
pwConsumption := 9000.0
maxCharge := 3500.0
pw_batt_reserve := 0.0
startHr := "09:00"
endHr := "17:00"

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

assert.Error(err, "Handler returned an error")
}

func TestOpenCloseModbusClient(t *testing.T) {
assert := assert.New(t)
setup()
Expand All @@ -124,6 +173,15 @@ func TestOpenCloseModbusClient(t *testing.T) {

}

func TestOpenClientError(t *testing.T) {
assert := assert.New(t)
setup()
err = fronius.OpenModbusClient("123", modbus_port)
teardown()
assert.Error(err, "OpenModbusClient returned an error")

}

func TestSetChargePower(t *testing.T) {
assert := assert.New(t)

Expand Down Expand Up @@ -200,3 +258,36 @@ func TestBatteryChargeMode5(t *testing.T) {

teardown()
}

func TestBatteryChargeMode6(t *testing.T) {
assert := assert.New(t)
setup()

result, err := fronius.SetFroniusChargeBatteryMode(8000, 2000, 11000, 9000, 3500, 0, "00:00", "23:59", modbus_ip, modbus_port)
assert.Equal(int16(0), result, "SetFroniusChargeBatteryMode returned wrong value")
assert.NoError(err)

teardown()
}

func TestBatteryChargeError(t *testing.T) {
assert := assert.New(t)
setup()

result, err := fronius.SetFroniusChargeBatteryMode(1000, 11000, 11000, 9000, 3500, 2500, "25:59", "00:00", modbus_ip, modbus_port)
assert.Equal(int16(0), result, "SetFroniusChargeBatteryMode returned wrong value")
assert.Error(err)

teardown()
}

func TestBatteryChargeError2(t *testing.T) {
assert := assert.New(t)
setup()

result, err := fronius.SetFroniusChargeBatteryMode(1000, 11000, 11000, 9000, 3500, 2500, "00:00", "25:00", modbus_ip, modbus_port)
assert.Equal(int16(0), result, "SetFroniusChargeBatteryMode returned wrong value")
assert.Error(err)

teardown()
}
8 changes: 7 additions & 1 deletion pkg/fronius/handler.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package fronius

import u "sbam/src/utils"

func New() *Fronius {
return &Fronius{}
}
Expand All @@ -10,7 +12,11 @@ func (fronius *Fronius) Handler(pw_forecast float64, pw_batt2charge float64, pw_
p = fronius_port[0]
}

charge_pc, _ := SetFroniusChargeBatteryMode(pw_forecast, pw_batt2charge, pw_batt_max, pw_consumption, max_charge, pw_batt_reserve, start_hr, end_hr, fronius_ip, p)
charge_pc, err := SetFroniusChargeBatteryMode(pw_forecast, pw_batt2charge, pw_batt_max, pw_consumption, max_charge, pw_batt_reserve, start_hr, end_hr, fronius_ip, p)
if err != nil {
u.Log.Errorln("Error setting Fronius Battery charge: %s ", err)
return charge_pc, err
}

return charge_pc, nil

Expand Down
8 changes: 4 additions & 4 deletions pkg/fronius/modbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ func OpenModbusClient(url string, port ...string) error {
})
if err != nil {
u.Log.Error("Someting goes wrong configuring Modbus Client")
panic(err)
return err

Check warning on line 25 in pkg/fronius/modbus.go

View check run for this annotation

Codecov / codecov/patch

pkg/fronius/modbus.go#L25

Added line #L25 was not covered by tests
}
err = modbusClient.Open()
if err != nil {
u.Log.Error("Someting goes wrong opening Modbus Client")
panic(err)
return err
}
err = modbusClient.SetUnitId(1)
if err != nil {
u.Log.Error("Someting goes wrong setting Modbus Client SlaveID")
panic(err)
return err

Check warning on line 35 in pkg/fronius/modbus.go

View check run for this annotation

Codecov / codecov/patch

pkg/fronius/modbus.go#L35

Added line #L35 was not covered by tests
}

return nil
Expand All @@ -43,7 +43,7 @@ func ClosemodbusClient() error {
modbusClient.Close()
if err != nil {
u.Log.Error("Someting goes wrong closing Modbus Client")
panic(err)
return err

Check warning on line 46 in pkg/fronius/modbus.go

View check run for this annotation

Codecov / codecov/patch

pkg/fronius/modbus.go#L46

Added line #L46 was not covered by tests
}

return nil
Expand Down
42 changes: 33 additions & 9 deletions pkg/fronius/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,50 @@ func SetFroniusChargeBatteryMode(pw_forecast float64, pw_batt2charge float64, pw
pw_pv_net := pw_forecast - pw_consumption // Net solar power
pw_batt := pw_batt_max - pw_batt2charge // actual battery power

time, _ := CheckTimeRange(start_hr, end_hr)

time, err := CheckTimeRange(start_hr, end_hr)
if err != nil {
u.Log.Errorln("Error checking time range: %s ", err)
return ch_pc, err
}
switch {
case !time: // out of the time range => do not charge
u.Log.Infof("Out time range start_time: %s - end_time: %s", start_hr, end_hr)
Setdefaults(fronius_ip, p)
err = Setdefaults(fronius_ip, p)
if err != nil {
u.Log.Errorln("Error Setting Defaults: %s ", err)
return ch_pc, err
}
case pw_batt2charge == 0: // battery 100% => do not charge
u.Log.Info("Battery is full charged")
Setdefaults(fronius_ip, p)
err = Setdefaults(fronius_ip, p)
if err != nil {
u.Log.Errorln("Error Setting Defaults: %s ", err)
return ch_pc, err

Check warning on line 35 in pkg/fronius/schedule.go

View check run for this annotation

Codecov / codecov/patch

pkg/fronius/schedule.go#L34-L35

Added lines #L34 - L35 were not covered by tests
}
case pw_batt < pw_batt_reserve: // battery is less than reserve => charge
ch_pc = SetChargePower(pw_batt_max, pw_batt_reserve, max_charge)
ForceCharge(fronius_ip, ch_pc, p)
err = ForceCharge(fronius_ip, ch_pc, p)
if err != nil {
u.Log.Errorln("Error forcing charge: %s ", err)
return ch_pc, err

Check warning on line 42 in pkg/fronius/schedule.go

View check run for this annotation

Codecov / codecov/patch

pkg/fronius/schedule.go#L41-L42

Added lines #L41 - L42 were not covered by tests
}

default:
pw_grid, charge_enabled := ChargeBattery(pw_pv_net, pw_batt)

if charge_enabled {
ch_pc = SetChargePower(pw_batt_max, -1*pw_grid, max_charge)
ForceCharge(fronius_ip, ch_pc, p)
err = ForceCharge(fronius_ip, ch_pc, p)
if err != nil {
u.Log.Errorln("Error forcing charge: %s ", err)
return ch_pc, err

Check warning on line 53 in pkg/fronius/schedule.go

View check run for this annotation

Codecov / codecov/patch

pkg/fronius/schedule.go#L52-L53

Added lines #L52 - L53 were not covered by tests
}
} else {
Setdefaults(fronius_ip, p)
err = Setdefaults(fronius_ip, p)
if err != nil {
u.Log.Errorln("Error Setting Defaults: %s ", err)
return ch_pc, err

Check warning on line 59 in pkg/fronius/schedule.go

View check run for this annotation

Codecov / codecov/patch

pkg/fronius/schedule.go#L58-L59

Added lines #L58 - L59 were not covered by tests
}
}

}
Expand Down Expand Up @@ -73,13 +97,13 @@ func CheckTimeRange(start_hr string, end_hr string) (bool, error) {
startTime, err := time.Parse(layout, start_hr)
if err != nil {
u.Log.Error("Something goes wrong parsing start time")
panic(err)
return false, err
}

endTime, err := time.Parse(layout, end_hr)
if err != nil {
u.Log.Error("Something goes wrong parsing end time")
panic(err)
return false, err
}

// Convert the current time to a time.Time value for today's date with the hour and minute set to the parsed start and end times
Expand Down

0 comments on commit 80a19d8

Please sign in to comment.