forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add openWB support via MQTT (evcc-io#399)
- Loading branch information
Showing
8 changed files
with
173 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package charger | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/andig/evcc/api" | ||
"github.com/andig/evcc/charger/openwb" | ||
"github.com/andig/evcc/meter" | ||
"github.com/andig/evcc/provider" | ||
"github.com/andig/evcc/util" | ||
) | ||
|
||
func init() { | ||
registry.Add("openwb", NewOpenWBFromConfig) | ||
} | ||
|
||
// OpenWB configures generic charger and charge meter for an openWB loadpoint | ||
type OpenWB struct { | ||
api.Charger | ||
api.Meter | ||
} | ||
|
||
// NewOpenWBFromConfig creates a new configurable charger | ||
func NewOpenWBFromConfig(other map[string]interface{}) (api.Charger, error) { | ||
cc := struct { | ||
Broker string | ||
User, Password string | ||
Topic string | ||
ID int | ||
Timeout time.Duration | ||
}{ | ||
Topic: "openWB", | ||
ID: 1, | ||
Timeout: 5 * time.Second, | ||
} | ||
|
||
if err := util.DecodeOther(other, &cc); err != nil { | ||
return nil, err | ||
} | ||
|
||
clientID := provider.MqttClientID() | ||
client := provider.NewMqttClient(cc.Broker, cc.User, cc.Password, clientID, 1) | ||
|
||
// check if loadpoint configured | ||
configured := client.BoolGetter(fmt.Sprintf("%s/lp/%d/%s", cc.Topic, cc.ID, openwb.ConfiguredTopic), cc.Timeout) | ||
if isConfigured, err := configured(); err != nil || !isConfigured { | ||
return nil, fmt.Errorf("openWB loadpoint %d is not configured", cc.ID) | ||
} | ||
|
||
// adapt plugged/charging to status | ||
plugged := client.BoolGetter(fmt.Sprintf("%s/lp/%d/%s", cc.Topic, cc.ID, openwb.PluggedTopic), cc.Timeout) | ||
charging := client.BoolGetter(fmt.Sprintf("%s/lp/%d/%s", cc.Topic, cc.ID, openwb.ChargingTopic), cc.Timeout) | ||
status := provider.NewOpenWBStatusProvider(plugged, charging).StringGetter | ||
|
||
// remaining getters | ||
enabled := client.BoolGetter(fmt.Sprintf("%s/lp/%d/%s", cc.Topic, cc.ID, openwb.EnabledTopic), cc.Timeout) | ||
|
||
// setters | ||
enable := client.BoolSetter("enable", fmt.Sprintf("%s/set/lp%d/%s", cc.Topic, cc.ID, openwb.EnabledTopic), "") | ||
maxcurrent := client.IntSetter("maxcurrent", fmt.Sprintf("%s/set/lp%d/%s", cc.Topic, cc.ID, openwb.MaxCurrentTopic), "") | ||
|
||
// meter getters | ||
power := client.FloatGetter(fmt.Sprintf("%s/lp/%d/%s", cc.Topic, cc.ID, openwb.ChargePowerTopic), 1, cc.Timeout) | ||
totalEnergy := client.FloatGetter(fmt.Sprintf("%s/lp/%d/%s", cc.Topic, cc.ID, openwb.ChargeTotalEnergyTopic), 1, cc.Timeout) | ||
|
||
var currents []func() (float64, error) | ||
for i := 1; i <= 3; i++ { | ||
current := client.FloatGetter(fmt.Sprintf("%s/lp/%d/%s%d", cc.Topic, cc.ID, openwb.CurrentTopic, i), 1, cc.Timeout) | ||
currents = append(currents, current) | ||
} | ||
|
||
c, err := NewConfigurable(status, enabled, enable, maxcurrent) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
m, err := meter.NewConfigurable(power) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
res := &OpenWB{ | ||
Charger: c, | ||
Meter: m.Decorate(totalEnergy, currents, nil), | ||
} | ||
|
||
return res, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package openwb | ||
|
||
// predefined openWB topic names | ||
const ( | ||
// status | ||
PluggedTopic = "boolPlugStat" | ||
ChargingTopic = "boolChargeStat" | ||
ConfiguredTopic = "boolChargePointConfigured" | ||
|
||
// getter/setter | ||
EnabledTopic = "ChargePointEnabled" | ||
MaxCurrentTopic = "DirectChargeAmps" | ||
|
||
// charge power | ||
ChargePowerTopic = "W" | ||
ChargeTotalEnergyTopic = "kWhCounter" | ||
|
||
// general measurements | ||
PowerTopic = "W" | ||
CurrentTopic = "APhase" // 1..3 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters