OpenWB Pro: add caching (#2731)
This commit is contained in:
parent
3f422c88eb
commit
56fc5c27d4
2 changed files with 43 additions and 24 deletions
|
|
@ -6,6 +6,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/evcc-io/evcc/api"
|
||||
"github.com/evcc-io/evcc/charger/openwb/pro"
|
||||
"github.com/evcc-io/evcc/util"
|
||||
"github.com/evcc-io/evcc/util/request"
|
||||
)
|
||||
|
|
@ -16,53 +17,39 @@ func init() {
|
|||
|
||||
// https://openwb.de/main/?page_id=771
|
||||
|
||||
type openwbProStatus struct {
|
||||
Date string
|
||||
Timestamp int64
|
||||
Currents []float64
|
||||
Powers []float64
|
||||
PowerAll float64 `json:"power_all"`
|
||||
Imported float64
|
||||
Exported float64
|
||||
PlugState bool `json:"plug_state"`
|
||||
ChargeState bool `json:"charge_state"`
|
||||
PhasesActual int `json:"phases_actual"`
|
||||
PhasesTarget int `json:"phases_target"`
|
||||
PhasesInUse int `json:"phases_in_use"`
|
||||
OfferedCurrent float64 `json:"offered_current"`
|
||||
EvseSignaling string `json:"evse_signaling"`
|
||||
VehicleID string `json:"vehicle_id"`
|
||||
Serial string
|
||||
}
|
||||
|
||||
// OpenWBPro charger implementation
|
||||
type OpenWBPro struct {
|
||||
*request.Helper
|
||||
uri string
|
||||
current float64
|
||||
status pro.Status
|
||||
updated time.Time
|
||||
cache time.Duration
|
||||
}
|
||||
|
||||
// NewOpenWBProFromConfig creates a OpenWBPro charger from generic config
|
||||
func NewOpenWBProFromConfig(other map[string]interface{}) (api.Charger, error) {
|
||||
cc := struct {
|
||||
URI string
|
||||
URI string
|
||||
Cache time.Duration
|
||||
}{}
|
||||
|
||||
if err := util.DecodeOther(other, &cc); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return NewOpenWBPro(util.DefaultScheme(cc.URI, "http"))
|
||||
return NewOpenWBPro(util.DefaultScheme(cc.URI, "http"), cc.Cache)
|
||||
}
|
||||
|
||||
// NewOpenWBPro creates OpenWBPro charger
|
||||
func NewOpenWBPro(uri string) (*OpenWBPro, error) {
|
||||
func NewOpenWBPro(uri string, cache time.Duration) (*OpenWBPro, error) {
|
||||
log := util.NewLogger("owbpro")
|
||||
|
||||
wb := &OpenWBPro{
|
||||
Helper: request.NewHelper(log),
|
||||
uri: strings.TrimRight(uri, "/"),
|
||||
current: 6, // 6A defined value
|
||||
cache: cache,
|
||||
}
|
||||
|
||||
go wb.hearbeat(log)
|
||||
|
|
@ -78,10 +65,19 @@ func (wb *OpenWBPro) hearbeat(log *util.Logger) {
|
|||
}
|
||||
}
|
||||
|
||||
func (wb *OpenWBPro) get() (openwbProStatus, error) {
|
||||
var res openwbProStatus
|
||||
func (wb *OpenWBPro) get() (pro.Status, error) {
|
||||
if time.Since(wb.updated) < wb.cache {
|
||||
return wb.status, nil
|
||||
}
|
||||
|
||||
var res pro.Status
|
||||
uri := fmt.Sprintf("%s/%s", wb.uri, "connect.php")
|
||||
err := wb.GetJSON(uri, &res)
|
||||
if err == nil {
|
||||
wb.updated = time.Now()
|
||||
wb.status = res
|
||||
}
|
||||
|
||||
return res, err
|
||||
}
|
||||
|
||||
|
|
@ -91,6 +87,7 @@ func (wb *OpenWBPro) set(payload string) error {
|
|||
if err == nil {
|
||||
resp.Body.Close()
|
||||
}
|
||||
wb.updated = time.Time{}
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
|||
22
charger/openwb/pro/types.go
Normal file
22
charger/openwb/pro/types.go
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
package pro
|
||||
|
||||
// https://openwb.de/main/?page_id=771
|
||||
|
||||
type Status struct {
|
||||
Date string
|
||||
Timestamp int64
|
||||
Currents []float64
|
||||
Powers []float64
|
||||
PowerAll float64 `json:"power_all"`
|
||||
Imported float64
|
||||
Exported float64
|
||||
PlugState bool `json:"plug_state"`
|
||||
ChargeState bool `json:"charge_state"`
|
||||
PhasesActual int `json:"phases_actual"`
|
||||
PhasesTarget int `json:"phases_target"`
|
||||
PhasesInUse int `json:"phases_in_use"`
|
||||
OfferedCurrent float64 `json:"offered_current"`
|
||||
EvseSignaling string `json:"evse_signaling"`
|
||||
VehicleID string `json:"vehicle_id"`
|
||||
Serial string
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue