OpenWB: add rfid authorization

This commit is contained in:
andig 2021-11-11 20:03:55 +01:00
parent c7912e6388
commit 4eafc9453f
5 changed files with 32 additions and 11 deletions

View file

@ -103,6 +103,11 @@ type Identifier interface {
Identify() (string, error)
}
// Authorizer authorizes a charging session by supplying RFID credentials
type Authorizer interface {
Authorize(key string) error
}
// Vehicle represents the EV and it's battery
type Vehicle interface {
Battery

View file

@ -21,6 +21,7 @@ type OpenWB struct {
currentPowerG func() (float64, error)
totalEnergyG func() (float64, error)
currentsG []func() (float64, error)
authS func(string) error
}
// go:generate go run ../cmd/tools/decorate.go -f decorateOpenWB -b *OpenWB -r api.Charger -t "api.ChargePhases,Phases1p3p,func(int) (error)" -t "api.Battery,SoC,func() (float64, error)"
@ -102,6 +103,9 @@ func NewOpenWB(log *util.Logger, mqttconf mqtt.Config, id int, topic string, p1p
maxcurrentS := provider.NewMqtt(log, client,
fmt.Sprintf("%s/set/lp%d/%s", topic, id, openwb.MaxCurrentTopic),
1, timeout).IntSetter("maxcurrent")
authS := provider.NewMqtt(log, client,
fmt.Sprintf("%s/set/chargepoint/%d/get/%s", topic, id, openwb.RfidTopic),
1, timeout).StringSetter("rfid")
// meter getters
currentPowerG := floatG(fmt.Sprintf("%s/lp/%d/%s", topic, id, openwb.ChargePowerTopic))
@ -123,6 +127,7 @@ func NewOpenWB(log *util.Logger, mqttconf mqtt.Config, id int, topic string, p1p
currentPowerG: currentPowerG,
totalEnergyG: totalEnergyG,
currentsG: currentsG,
authS: authS,
}
// optional capabilities
@ -176,3 +181,10 @@ func (m *OpenWB) Currents() (float64, float64, float64, error) {
return currents[0], currents[1], currents[2], nil
}
var _ api.Authorizer = (*OpenWB)(nil)
// Authorize implements the api.Authorizer interface
func (m *OpenWB) Authorize(key string) error {
return m.authS(key)
}

View file

@ -21,6 +21,7 @@ const (
EnabledTopic = "ChargePointEnabled"
MaxCurrentTopic = "DirectChargeAmps"
PhasesTopic = "U1p3p"
RfidTopic = "rfid"
// charge power
ChargePowerTopic = "W"

View file

@ -22,6 +22,9 @@ type (
SetIntProvider interface {
IntSetter(param string) func(int64) error
}
SetStringProvider interface {
StringSetter(param string) func(string) error
}
SetBoolProvider interface {
BoolSetter(param string) func(bool) error
}

View file

@ -203,19 +203,19 @@ func (m *Mqtt) BoolSetter(param string) func(bool) error {
}
}
// var _ SetStringProvider = (*Mqtt)(nil)
var _ SetStringProvider = (*Mqtt)(nil)
// // StringSetter invokes script with parameter replaced by string value
// func (m *Mqtt) StringSetter(param string) func(string) error {
// return func(v string) error {
// payload, err := setFormattedValue(m.payload, param, v)
// if err != nil {
// return err
// }
// StringSetter invokes script with parameter replaced by string value
func (m *Mqtt) StringSetter(param string) func(string) error {
return func(v string) error {
payload, err := setFormattedValue(m.payload, param, v)
if err != nil {
return err
}
// return m.client.Publish(m.topic, false, payload)
// }
// }
return m.client.Publish(m.topic, false, payload)
}
}
type msgHandler struct {
mux *util.Waiter