Skip to content

Commit

Permalink
chore: simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Oct 18, 2023
1 parent c171d37 commit 387ce26
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
13 changes: 6 additions & 7 deletions charger/nrgble_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ func (nrg *NRGKickBLE) connect() (*device.Device1, error) {
return nil, fmt.Errorf("find device: %s", err)
}

err = ble.Connect(dev, nrg.agent, nrg.device)
if err != nil {
if err := ble.Connect(dev, nrg.agent, nrg.device); err != nil {
return nil, err
}

Expand Down Expand Up @@ -249,7 +248,7 @@ func (nrg *NRGKickBLE) Enabled() (bool, error) {

// Enable implements the api.Charger interface
func (nrg *NRGKickBLE) Enable(enable bool) error {
res := ble.Info{}
var res ble.Info
if err := nrg.read(ble.InfoService, &res); err != nil {
return err
}
Expand All @@ -276,7 +275,7 @@ func (nrg *NRGKickBLE) Enable(enable bool) error {

// MaxCurrent implements the api.Charger interface
func (nrg *NRGKickBLE) MaxCurrent(current int64) error {
res := ble.Info{}
var res ble.Info
if err := nrg.read(ble.InfoService, &res); err != nil {
return err
}
Expand All @@ -293,7 +292,7 @@ var _ api.Meter = (*NRGKickBLE)(nil)

// CurrentPower implements the api.Meter interface
func (nrg *NRGKickBLE) CurrentPower() (float64, error) {
res := ble.Power{}
var res ble.Power
if err := nrg.read(ble.PowerService, &res); err != nil {
return 0, err
}
Expand All @@ -307,7 +306,7 @@ var _ api.MeterEnergy = (*NRGKickBLE)(nil)

// TotalEnergy implements the api.MeterEnergy interface
func (nrg *NRGKickBLE) TotalEnergy() (float64, error) {
res := ble.Energy{}
var res ble.Energy
if err := nrg.read(ble.EnergyService, &res); err != nil {
return 0, err
}
Expand All @@ -321,7 +320,7 @@ var _ api.PhaseCurrents = (*NRGKickBLE)(nil)

// Currents implements the api.PhaseCurrents interface
func (nrg *NRGKickBLE) Currents() (float64, float64, float64, error) {
res := ble.VoltageCurrent{}
var res ble.VoltageCurrent
if err := nrg.read(ble.VoltageCurrentService, &res); err != nil {
return 0, 0, 0, err
}
Expand Down
32 changes: 16 additions & 16 deletions charger/nrgconnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,27 +87,27 @@ func (nrg *NRGKickConnect) apiURL(api string) string {

func (nrg *NRGKickConnect) putJSON(url string, data interface{}) error {
req, err := request.New(http.MethodPut, url, request.MarshalJSON(data), request.JSONEncoding)
if err != nil {
return err
}

if err == nil {
var res struct {
Message string
}

if err = nrg.DoJSON(req, &res); err != nil {
if err == io.EOF {
err = nil
} else if res.Message != "" {
return errors.New(res.Message)
}
}
var res struct {
Message string
}

if err == nil {
nrg.settingsG.Reset()
nrg.measurementsG.Reset()
if err := nrg.DoJSON(req, &res); err != nil {
switch {
case res.Message != "":
return errors.New(res.Message)
case err != io.EOF:
return err
}
}

return err
nrg.settingsG.Reset()
nrg.measurementsG.Reset()

return nil
}

// Status implements the api.Charger interface
Expand Down

0 comments on commit 387ce26

Please sign in to comment.