diff --git a/charger/smart-evse.go b/charger/smart-evse.go new file mode 100644 index 000000000..901d26985 --- /dev/null +++ b/charger/smart-evse.go @@ -0,0 +1,432 @@ +package charger + +// LICENSE + +// Copyright (c) evcc.io (andig, naltatis, premultiply) + +// This module is NOT covered by the MIT license. All rights reserved. + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +import ( + "fmt" + "net/http" + "strings" + "time" + + "github.com/evcc-io/evcc/api" + "github.com/evcc-io/evcc/util" + "github.com/evcc-io/evcc/util/request" + "github.com/evcc-io/evcc/util/sponsor" +) + +// SmartEVSE-3.5 REST API charger implementation +// https://github.com/dingo35/SmartEVSE-3.5/blob/master/docs/REST_API.md + +// SmartEVSE3 charger implementation +type SmartEVSE3 struct { + *request.Helper + uri string + curr int64 + apiG util.Cacheable[smartEvseRestSettings] +} + +// smartEvseRestSettings represents the JSON returned by GET /settings +type smartEvseRestSettings struct { + Mode string `json:"mode"` + ModeID int `json:"mode_id"` + CarConnected bool `json:"car_connected"` + Evse struct { + Connected int `json:"connected"` + Access int `json:"access"` + Mode int `json:"mode"` + ChargeTimer int `json:"charge_timer"` + State string `json:"state"` + StateID int `json:"state_id"` + Error string `json:"error"` + ErrorID int `json:"error_id"` + RFID string `json:"rfid"` + RFIDReader string `json:"rfidreader"` + RFIDLastRead string `json:"rfid_lastread"` + NrOfPhases int `json:"nrofphases"` + } `json:"evse"` + Settings struct { + ChargeCurrent int `json:"charge_current"` + OverrideCurrent int `json:"override_current"` + CurrentMin int `json:"current_min"` + CurrentMax int `json:"current_max"` + CurrentMain int `json:"current_main"` + EnableC2 string `json:"enable_C2"` + } `json:"settings"` + EvMeter struct { + Description string `json:"description"` + Address int `json:"address"` + ImportActivePower float64 `json:"import_active_power"` + TotalWh float64 `json:"total_wh"` + ChargedWh float64 `json:"charged_wh"` + Currents struct { + Total float64 `json:"TOTAL"` + L1 float64 `json:"L1"` + L2 float64 `json:"L2"` + L3 float64 `json:"L3"` + } `json:"currents"` + ImportActiveEnergy float64 `json:"import_active_energy"` + ExportActiveEnergy float64 `json:"export_active_energy"` + } `json:"ev_meter"` +} + +// SmartEVSE-3.5 operating mode IDs +const ( + smartEvse3ModeOff = 0 + smartEvse3ModeNormal = 1 + smartEvse3ModeSolar = 2 + smartEvse3ModeSmart = 3 + smartEvse3ModePause = 4 +) + +// SmartEVSE-3.5 C2 contactor IDs +const ( + smartEvse3C2NotPresent = 0 + smartEvse3C2AlwaysOff = 1 // single-phase + smartEvse3C2SolarOff = 2 + smartEvse3C2AlwaysOn = 3 // three-phase + smartEvse3C2Auto = 4 +) + +func init() { + registry.Add("smart-evse", NewSmartEVSE3FromConfig) +} + +//go:generate go tool decorate -f decorateSmartEVSE3 -b *SmartEVSE3 -r api.Charger -t api.Meter,api.MeterEnergy,api.PhaseCurrents,api.PhaseSwitcher,api.PhaseGetter,api.Identifier,api.StatusReasoner + +// NewSmartEVSE3FromConfig creates a SmartEVSE-3.5 REST charger from generic config +func NewSmartEVSE3FromConfig(other map[string]any) (api.Charger, error) { + cc := struct { + URI string + Cache time.Duration + }{ + Cache: time.Second, + } + + if err := util.DecodeOther(other, &cc); err != nil { + return nil, err + } + + if cc.URI == "" { + return nil, fmt.Errorf("missing uri") + } + + return NewSmartEVSE3(cc.URI, cc.Cache) +} + +// NewSmartEVSE3 creates a new SmartEVSE-3.5 REST charger +func NewSmartEVSE3(uri string, cache time.Duration) (api.Charger, error) { + log := util.NewLogger("smart-evse") + + wb := &SmartEVSE3{ + Helper: request.NewHelper(log), + uri: strings.TrimRight(util.DefaultScheme(uri, "http"), "/"), + curr: 60, // 6 A in 1/10 A + } + + if !sponsor.IsAuthorized() { + return nil, api.ErrSponsorRequired + } + + wb.apiG = util.ResettableCached(func() (smartEvseRestSettings, error) { + var res smartEvseRestSettings + err := wb.GetJSON(wb.uri+"/settings", &res) + return res, err + }, cache) + + // verify connectivity + res, err := wb.apiG.Get() + if err != nil { + return nil, err + } + + // force NORMAL mode so override_current takes effect + if res.ModeID != smartEvse3ModeNormal { + if err := wb.setMode(smartEvse3ModeNormal); err != nil { + return nil, err + } + } + + // decorate optional EV meter if configured in SmartEVSE + var currentPower, totalEnergy func() (float64, error) + var currents func() (float64, float64, float64, error) + if res.EvMeter.Description != "" && res.EvMeter.Description != "Disabled" { + currentPower = wb.currentPower + totalEnergy = wb.totalEnergy + currents = wb.currents + } + + // decorate optional 1P/3P phase switching via C2 contactor + var phases1p3p func(int) error + var getPhases func() (int, error) + if res.Settings.EnableC2 != "" && res.Settings.EnableC2 != "Not present" { + phases1p3p = wb.phases1p3p + getPhases = wb.getPhases + } + + // decorate optional RFID identification and status reason + var identify func() (string, error) + var statusReason func() (api.Reason, error) + if res.Evse.RFIDReader != "" && res.Evse.RFIDReader != "Disabled" { + identify = wb.identify + statusReason = wb.statusReason + } + + return decorateSmartEVSE3(wb, currentPower, totalEnergy, currents, phases1p3p, getPhases, identify, statusReason), nil +} + +// post issues a POST request to the SmartEVSE with given query parameters. +// Mongoose webserver requires an empty request body to avoid timeout. +func (wb *SmartEVSE3) post(path string, query string) error { + uri := fmt.Sprintf("%s/%s?%s", wb.uri, strings.TrimLeft(path, "/"), query) + + req, err := request.New(http.MethodPost, uri, strings.NewReader(""), request.URLEncoding) + if err != nil { + return err + } + + _, err = wb.DoBody(req) + wb.apiG.Reset() + + return err +} + +func (wb *SmartEVSE3) setMode(mode int) error { + return wb.post("settings", fmt.Sprintf("mode=%d", mode)) +} + +func (wb *SmartEVSE3) setOverrideCurrent(deciAmps int64) error { + return wb.post("settings", fmt.Sprintf("override_current=%d", deciAmps)) +} + +// Status implements the api.Charger interface +func (wb *SmartEVSE3) Status() (api.ChargeStatus, error) { + res, err := wb.apiG.Get() + if err != nil { + return api.StatusNone, err + } + + if !res.CarConnected { + return api.StatusA, nil + } + + // state IDs from SmartEVSE firmware (main_c.h) + switch res.Evse.StateID { + case 0: // STATE_A + return api.StatusA, nil + case 1, 4, 5, 8, 9, 11, 12, 13, 14: + // STATE_B, STATE_COMM_B, STATE_COMM_B_OK, STATE_ACTSTART, STATE_B1, + // STATE_MODEM_REQUEST, STATE_MODEM_WAIT, STATE_MODEM_DONE, STATE_MODEM_DENIED + return api.StatusB, nil + case 2, 3, 6, 7, 10: + // STATE_C, STATE_D, STATE_COMM_C, STATE_COMM_C_OK, STATE_C1 + return api.StatusC, nil + default: + return api.StatusNone, fmt.Errorf("invalid state: %d (%s)", res.Evse.StateID, res.Evse.State) + } +} + +// Enabled implements the api.Charger interface +func (wb *SmartEVSE3) Enabled() (bool, error) { + res, err := wb.apiG.Get() + if err != nil { + return false, err + } + + // STATE_B1 / STATE_C1: EVSE not ready, no PWM signal + if res.Evse.StateID == 9 || res.Evse.StateID == 10 { + return false, nil + } + + if res.ModeID == smartEvse3ModeOff || res.ModeID == smartEvse3ModePause { + return false, nil + } + + return true, nil +} + +// Enable implements the api.Charger interface +func (wb *SmartEVSE3) Enable(enable bool) error { + res, err := wb.apiG.Get() + if err != nil { + return err + } + + if enable { + if res.ModeID != smartEvse3ModeNormal { + return wb.setMode(smartEvse3ModeNormal) + } + return nil + } + + if res.ModeID != smartEvse3ModeOff && res.ModeID != smartEvse3ModePause { + return wb.setMode(smartEvse3ModePause) + } + + return nil +} + +// MaxCurrent implements the api.Charger interface +func (wb *SmartEVSE3) MaxCurrent(current int64) error { + if current < 6 { + return fmt.Errorf("invalid current %d", current) + } + + deciAmps := current * 10 + + if err := wb.setOverrideCurrent(deciAmps); err != nil { + return err + } + + wb.curr = deciAmps + return nil +} + +var _ api.ChargerEx = (*SmartEVSE3)(nil) + +// MaxCurrentMillis implements the api.ChargerEx interface +func (wb *SmartEVSE3) MaxCurrentMillis(current float64) error { + if current < 6 { + return fmt.Errorf("invalid current %.1f", current) + } + + deciAmps := int64(current * 10) + + if err := wb.setOverrideCurrent(deciAmps); err != nil { + return err + } + + wb.curr = deciAmps + return nil +} + +// currentPower implements the api.Meter interface +func (wb *SmartEVSE3) currentPower() (float64, error) { + res, err := wb.apiG.Get() + if err != nil { + return 0, err + } + + // import_active_power is reported in W + return res.EvMeter.ImportActivePower, nil +} + +// totalEnergy implements the api.MeterEnergy interface +func (wb *SmartEVSE3) totalEnergy() (float64, error) { + res, err := wb.apiG.Get() + if err != nil { + return 0, err + } + + // import_active_energy is reported in Wh + return res.EvMeter.ImportActiveEnergy / 1e3, nil +} + +// phases1p3p implements the api.PhaseSwitcher interface +func (wb *SmartEVSE3) phases1p3p(phases int) error { + var c2 int + switch phases { + case 1: + c2 = smartEvse3C2AlwaysOff + case 3: + c2 = smartEvse3C2AlwaysOn + default: + return fmt.Errorf("invalid phases: %d", phases) + } + + // C2 switching takes effect on next state change; disable charger during switch + return whenDisabled(wb, func() error { + return wb.post("settings", fmt.Sprintf("enable_C2=%d", c2)) + }) +} + +// getPhases implements the api.PhaseGetter interface +func (wb *SmartEVSE3) getPhases() (int, error) { + res, err := wb.apiG.Get() + if err != nil { + return 0, err + } + + return res.Evse.NrOfPhases, nil +} + +// currents implements the api.PhaseCurrents interface +func (wb *SmartEVSE3) currents() (float64, float64, float64, error) { + res, err := wb.apiG.Get() + if err != nil { + return 0, 0, 0, err + } + + // phase currents are reported in 1/10 A + return res.EvMeter.Currents.L1 / 10, res.EvMeter.Currents.L2 / 10, res.EvMeter.Currents.L3 / 10, nil +} + +// statusReason implements the api.StatusReasoner interface +func (wb *SmartEVSE3) statusReason() (api.Reason, error) { + res, err := wb.apiG.Get() + if err != nil { + return api.ReasonUnknown, err + } + + // RFID reader enabled, car connected, but access not yet granted + if res.CarConnected && res.Evse.Access == 0 { + return api.ReasonWaitingForAuthorization, nil + } + + return api.ReasonUnknown, nil +} + +// identify implements the api.Identifier interface +func (wb *SmartEVSE3) identify() (string, error) { + res, err := wb.apiG.Get() + if err != nil { + return "", err + } + + return res.Evse.RFIDLastRead, nil +} + +var _ api.Diagnosis = (*SmartEVSE3)(nil) + +// Diagnose implements the api.Diagnosis interface +func (wb *SmartEVSE3) Diagnose() { + res, err := wb.apiG.Get() + if err != nil { + fmt.Printf("\tError: %v\n", err) + return + } + + fmt.Printf("\tMode: %s (%d)\n", res.Mode, res.ModeID) + fmt.Printf("\tCar connected: %t\n", res.CarConnected) + + fmt.Printf("\tEVSE connected: %d\n", res.Evse.Connected) + fmt.Printf("\tEVSE access: %d\n", res.Evse.Access) + fmt.Printf("\tEVSE mode: %d\n", res.Evse.Mode) + fmt.Printf("\tEVSE charge timer: %d\n", res.Evse.ChargeTimer) + fmt.Printf("\tEVSE state: %s (%d)\n", res.Evse.State, res.Evse.StateID) + fmt.Printf("\tEVSE error: %s (%d)\n", res.Evse.Error, res.Evse.ErrorID) + fmt.Printf("\tEVSE RFID: %s\n", res.Evse.RFID) + fmt.Printf("\tEVSE RFID reader: %s\n", res.Evse.RFIDReader) + fmt.Printf("\tEVSE RFID last read: %s\n", res.Evse.RFIDLastRead) + fmt.Printf("\tEVSE nr of phases: %d\n", res.Evse.NrOfPhases) + + fmt.Printf("\tCharge current: %d A\n", res.Settings.ChargeCurrent) + fmt.Printf("\tOverride current: %.1f A\n", float64(res.Settings.OverrideCurrent)/10) + fmt.Printf("\tCurrent min/max: %d/%d A\n", res.Settings.CurrentMin, res.Settings.CurrentMax) + fmt.Printf("\tCurrent main: %d A\n", res.Settings.CurrentMain) + fmt.Printf("\tEnable C2: %s\n", res.Settings.EnableC2) +} diff --git a/charger/smart-evse_decorators.go b/charger/smart-evse_decorators.go new file mode 100644 index 000000000..eab122fb7 --- /dev/null +++ b/charger/smart-evse_decorators.go @@ -0,0 +1,116 @@ +package charger + +// Code generated by github.com/evcc-io/evcc/cmd/tools/decorate.go. DO NOT EDIT. + +import ( + "reflect" + + "github.com/evcc-io/evcc/api" +) + +func decorateSmartEVSE3(base *SmartEVSE3, meter func() (float64, error), meterEnergy func() (float64, error), phaseCurrents func() (float64, float64, float64, error), phaseSwitcher func(int) error, phaseGetter func() (int, error), identifier func() (string, error), statusReasoner func() (api.Reason, error)) api.Charger { + caps := make(map[reflect.Type]any) + + if meter != nil { + caps[reflect.TypeFor[api.Meter]()] = &decorateSmartEVSE3MeterImpl{meter: meter} + } + + if meterEnergy != nil { + caps[reflect.TypeFor[api.MeterEnergy]()] = &decorateSmartEVSE3MeterEnergyImpl{meterEnergy: meterEnergy} + } + + if phaseCurrents != nil { + caps[reflect.TypeFor[api.PhaseCurrents]()] = &decorateSmartEVSE3PhaseCurrentsImpl{phaseCurrents: phaseCurrents} + } + + if phaseSwitcher != nil { + caps[reflect.TypeFor[api.PhaseSwitcher]()] = &decorateSmartEVSE3PhaseSwitcherImpl{phaseSwitcher: phaseSwitcher} + } + + if phaseGetter != nil { + caps[reflect.TypeFor[api.PhaseGetter]()] = &decorateSmartEVSE3PhaseGetterImpl{phaseGetter: phaseGetter} + } + + if identifier != nil { + caps[reflect.TypeFor[api.Identifier]()] = &decorateSmartEVSE3IdentifierImpl{identifier: identifier} + } + + if statusReasoner != nil { + caps[reflect.TypeFor[api.StatusReasoner]()] = &decorateSmartEVSE3StatusReasonerImpl{statusReasoner: statusReasoner} + } + + if len(caps) == 0 { + return base + } + + return &decorateSmartEVSE3Capable{SmartEVSE3: base, caps: caps} +} + +type decorateSmartEVSE3Capable struct { + *SmartEVSE3 + caps map[reflect.Type]any +} + +func (d *decorateSmartEVSE3Capable) Capability(typ reflect.Type) (any, bool) { + c, ok := d.caps[typ] + if !ok && reflect.TypeOf(d).Implements(typ) { + return d, true + } + return c, ok +} + +type decorateSmartEVSE3IdentifierImpl struct { + identifier func() (string, error) +} + +func (impl *decorateSmartEVSE3IdentifierImpl) Identify() (string, error) { + return impl.identifier() +} + +type decorateSmartEVSE3MeterImpl struct { + meter func() (float64, error) +} + +func (impl *decorateSmartEVSE3MeterImpl) CurrentPower() (float64, error) { + return impl.meter() +} + +type decorateSmartEVSE3MeterEnergyImpl struct { + meterEnergy func() (float64, error) +} + +func (impl *decorateSmartEVSE3MeterEnergyImpl) TotalEnergy() (float64, error) { + return impl.meterEnergy() +} + +type decorateSmartEVSE3PhaseCurrentsImpl struct { + phaseCurrents func() (float64, float64, float64, error) +} + +func (impl *decorateSmartEVSE3PhaseCurrentsImpl) Currents() (float64, float64, float64, error) { + return impl.phaseCurrents() +} + +type decorateSmartEVSE3PhaseGetterImpl struct { + phaseGetter func() (int, error) +} + +func (impl *decorateSmartEVSE3PhaseGetterImpl) GetPhases() (int, error) { + return impl.phaseGetter() +} + +type decorateSmartEVSE3PhaseSwitcherImpl struct { + phaseSwitcher func(int) error +} + +func (impl *decorateSmartEVSE3PhaseSwitcherImpl) Phases1p3p(p0 int) error { + return impl.phaseSwitcher(p0) +} + +type decorateSmartEVSE3StatusReasonerImpl struct { + statusReasoner func() (api.Reason, error) +} + +func (impl *decorateSmartEVSE3StatusReasonerImpl) StatusReason() (api.Reason, error) { + return impl.statusReasoner() +} diff --git a/templates/definition/charger/smart-evse.yaml b/templates/definition/charger/smart-evse.yaml new file mode 100644 index 000000000..c15db7708 --- /dev/null +++ b/templates/definition/charger/smart-evse.yaml @@ -0,0 +1,24 @@ +template: smart-evse +products: + - brand: Stegen + description: + generic: Smart Evse v3 / v3.5 +capabilities: ["mA", "rfid", "1p3p"] +requirements: + evcc: ["sponsorship"] + description: + de: | + Anbindung über die REST API der Smart Evse Firmware (FW >= 3.6.0). + evcc setzt das Gerät in den NORMAL Modus und steuert den Ladestrom über `override_current`. + en: | + Uses the REST API of the Smart Evse firmware (FW >= 3.6.0). + evcc puts the device into NORMAL mode and controls the charge current via `override_current`. +params: + - name: host + - name: cache + default: 1s + advanced: true +render: | + type: smart-evse + uri: http://{{ .host }} + cache: {{ .cache }}