From 56fc5c27d430a054a603317c097f2f46a6199048 Mon Sep 17 00:00:00 2001 From: andig Date: Mon, 28 Feb 2022 11:00:04 +0100 Subject: [PATCH] OpenWB Pro: add caching (#2731) --- charger/openwb-pro.go | 45 +++++++++++++++++-------------------- charger/openwb/pro/types.go | 22 ++++++++++++++++++ 2 files changed, 43 insertions(+), 24 deletions(-) create mode 100644 charger/openwb/pro/types.go diff --git a/charger/openwb-pro.go b/charger/openwb-pro.go index b6b4599f8..af229f453 100644 --- a/charger/openwb-pro.go +++ b/charger/openwb-pro.go @@ -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 } diff --git a/charger/openwb/pro/types.go b/charger/openwb/pro/types.go new file mode 100644 index 000000000..92441570f --- /dev/null +++ b/charger/openwb/pro/types.go @@ -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 +}