The broker transport could move a session along — start, stop, boost, skip the delay, cap the current — and nothing else. Everything the charger is actually configured with sat one field away in the same messages we were already decoding: the schedule it charges on, the plug lock, auto-start, the LED, load balancing, solar charging, and the Modbus server the local transport depends on. Readable, and unreachable. The obstacle was never the cloud, it was the shape of the protocol. A setting is not a register write. It is a *command*, and a command owns a set of fields inside a message type — mostly one, but five own several, and the charger reads the whole command as the new truth. A light-off schedule sent carrying only its switch is a schedule whose start and end have just been set to midnight. So a grouped write resends the siblings the caller did not name, using the values the charger itself last reported, and refuses when it has never reported them. That last part is not caution for its own sake: load balancing and solar charging carry the serial of the meter they watch, and nothing outside the charger knows it. An empty one would be adopted. Those values do not arrive with the telemetry, either. The fast 0410 stream a realtime trigger turns on carries none of them — the settings come on 0405, 0840 and 0900, which the charger sends when it has something to acknowledge. So a grouped write may have to send a trigger first purely to make the charger talk about itself, and says so plainly when even that produces nothing. Everything a caller supplies is encoded before the cloud is touched at all. A request naming one bad value changes nothing rather than half of what it asked for, and a mistyped setting costs a validation error instead of a sign-in, a certificate fetch and a broker connection to be told no. mqttsettings.go holds one table and it is the only place a setting is defined: the wire field, the name a caller uses, the state key its current value comes from, and how a value becomes bytes. The names are the snapshot's own, so a caller can read a status, change one entry and send it back. The existing limit command now builds its frame from that table too rather than encoding field a8 a second time. Reading grew to match. The frame decoder gains the fields the grouped writes must carry back — the two load-balance settings, both monitor serials, the solar monitoring mode — plus the swipe gestures, and the snapshot exposes the rest of what is now writable. One name was wrong and is corrected: field d9 was called chargingMode after the Modbus register at 20088, but the reference has it as the solar charging mode, so it becomes solarChargeMode and moves in beside the solar settings. A mislabelled reading is bad; a mislabelled writable field is worse. Over HTTP it is one action rather than a dozen, because the charger groups the fields anyway: POST .../settings with a settings object, and settings sharing a command travel in one frame instead of overwriting each other. The other two transports refuse it by name and say which one has it, the way they already refuse each other's commands. The audit trail records the values, not just that a write happened — a setting that changes what the charger will draw, or whether it answers on the LAN at all, is worth being able to trace afterwards. Two things worth saying plainly. This is built from the reference project's message maps and checked against its own frame layout, not against hardware — there is no charger on this end to point it at. And modbusEnabled is a loaded gun: writing it off stops the charger serving the register map, and the way back is this transport, or the app. The ignore rule for the local Modbus map artifact widens to the protocol maps that now sit beside it. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
249 lines
8.6 KiB
Go
249 lines
8.6 KiB
Go
package api
|
|
|
|
// The local half of the Anker Solix control plane. Where the OCPP path in
|
|
// integrations_ankersolix_control.go waits for the charger to dial in to us,
|
|
// this one dials the charger: Modbus TCP on its own network, using the register
|
|
// map Anker publishes for the V1 (see the plugin's modbus.go). That inversion is
|
|
// the whole point — a charger behind a customer's router can be controlled this
|
|
// way without any inbound reachability, a public endpoint or a TLS certificate.
|
|
//
|
|
// The trade-off is that the server must share a network with the charger, so
|
|
// this mode is selectable per user rather than assumed.
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"drivervault/apiserver/internal/modbus"
|
|
"drivervault/apiserver/internal/plugins/builtin/ankersolix"
|
|
)
|
|
|
|
// modbusActionTimeout bounds one dial-plus-exchange. The charger is on the LAN,
|
|
// so this is generous; it exists so an unreachable address fails the request
|
|
// rather than holding it open.
|
|
const modbusActionTimeout = 10 * time.Second
|
|
|
|
// ankerModbusConfig reads a charger's local address out of its stored binding.
|
|
func ankerModbusConfig(b ankerChargerBinding) ankersolix.ModbusConfig {
|
|
return ankersolix.ModbusConfig{Host: strings.TrimSpace(b.ModbusHost), Port: b.ModbusPort}
|
|
}
|
|
|
|
// ankerModbusAction issues one control command over Modbus TCP. The gate,
|
|
// rate limit, destructive-action confirmation and audit have already run in
|
|
// handleAnkerControlAction; this decides what to write and reports the outcome.
|
|
func (s *Server) ankerModbusAction(w http.ResponseWriter, r *http.Request, who *callerIdentity,
|
|
binding ankerChargerBinding, sn, action string, body ankerControlBody) {
|
|
|
|
// Actions the register map has no equivalent for. Saying which transport is
|
|
// missing them beats a bare "unknown action" the caller cannot act on.
|
|
switch action {
|
|
case "reset", "unlock", "availability", "trigger", "config":
|
|
writeError(w, http.StatusBadRequest,
|
|
"\""+action+"\" is an OCPP command; the local Modbus connection cannot send it. Switch the control mode to a CSMS mode to use it.")
|
|
return
|
|
case "settings":
|
|
// The register map holds five control registers; the charger's own
|
|
// configuration — schedules, switches, load balancing — is not among them.
|
|
writeError(w, http.StatusBadRequest,
|
|
"\"settings\" writes the charger's own configuration, which the register map does not expose. Switch the control mode to Anker cloud (MQTT) to use it.")
|
|
return
|
|
case "clear-limit":
|
|
// The register takes an explicit ceiling and the charger clamps anything
|
|
// above its rating, so "no limit" would mean writing a value we would have
|
|
// to guess. Asking for the real one is better than guessing wrong.
|
|
writeError(w, http.StatusBadRequest,
|
|
"the local connection has no \"clear limit\" command; send \"limit\" with the amps you want instead")
|
|
return
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(r.Context(), modbusActionTimeout)
|
|
defer cancel()
|
|
|
|
client, err := ankersolix.ModbusDial(ctx, ankerModbusConfig(binding))
|
|
if err != nil {
|
|
s.auditControl(who, sn, action, nil, "error", err)
|
|
writeJSON(w, http.StatusBadGateway, map[string]any{"error": err.Error()})
|
|
return
|
|
}
|
|
defer client.Close()
|
|
|
|
var (
|
|
result any
|
|
params = map[string]any{"transport": "modbus"}
|
|
)
|
|
switch action {
|
|
case "start":
|
|
err = ankersolix.ModbusStartCharging(ctx, client)
|
|
case "stop":
|
|
err = ankersolix.ModbusStopCharging(ctx, client)
|
|
case "limit":
|
|
params["amps"] = body.Amps
|
|
err = ankersolix.ModbusSetMaxCurrent(ctx, client, body.Amps)
|
|
case "boost":
|
|
on := body.On == nil || *body.On
|
|
params["on"] = on
|
|
err = ankersolix.ModbusSetBoost(ctx, client, on)
|
|
case "phase":
|
|
if body.PhaseMode == nil {
|
|
writeError(w, http.StatusBadRequest, "phase requires \"phase\": 0 (automatic), 1 (single) or 2 (three)")
|
|
return
|
|
}
|
|
params["phase"] = *body.PhaseMode
|
|
err = ankersolix.ModbusSetPhaseMode(ctx, client, *body.PhaseMode)
|
|
case "timeout":
|
|
params["seconds"] = body.Seconds
|
|
err = ankersolix.ModbusSetTimeout(ctx, client, body.Seconds)
|
|
case "status":
|
|
var snap ankersolix.ModbusSnapshot
|
|
snap, err = ankersolix.ModbusRead(ctx, client, true)
|
|
result = snap
|
|
default:
|
|
writeError(w, http.StatusBadRequest, "unknown control action: "+action)
|
|
return
|
|
}
|
|
|
|
outcome := "accepted"
|
|
if err != nil {
|
|
outcome = "error"
|
|
}
|
|
s.auditControl(who, sn, action, params, outcome, err)
|
|
|
|
if err != nil {
|
|
// A register the charger refused is a bad request, not a bad gateway: the
|
|
// link worked and the charger said no.
|
|
status := http.StatusBadGateway
|
|
var mbErr *modbus.Error
|
|
if errors.As(err, &mbErr) {
|
|
status = http.StatusBadRequest
|
|
}
|
|
writeJSON(w, status, map[string]any{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
resp := map[string]any{"status": outcome}
|
|
if result != nil {
|
|
resp["result"] = result
|
|
}
|
|
writeJSON(w, http.StatusOK, resp)
|
|
}
|
|
|
|
// handleAnkerControlAddress saves (or clears, on DELETE) the local address of one
|
|
// charger's Modbus TCP server — the address the Anker app shows once Modbus is
|
|
// enabled on the device.
|
|
func (s *Server) handleAnkerControlAddress(w http.ResponseWriter, r *http.Request) {
|
|
who := caller(r)
|
|
if who == nil {
|
|
writeError(w, http.StatusUnauthorized, "not authenticated")
|
|
return
|
|
}
|
|
if !s.pb.Configured() {
|
|
writeError(w, http.StatusServiceUnavailable, "integration settings not configured on the server")
|
|
return
|
|
}
|
|
sn := strings.TrimSpace(r.PathValue("sn"))
|
|
if sn == "" {
|
|
writeError(w, http.StatusBadRequest, "missing charger serial")
|
|
return
|
|
}
|
|
|
|
var host string
|
|
var port int
|
|
if r.Method != http.MethodDelete {
|
|
var body struct {
|
|
Host string `json:"host"`
|
|
Port int `json:"port"`
|
|
}
|
|
if r.Body != nil {
|
|
_ = json.NewDecoder(r.Body).Decode(&body)
|
|
}
|
|
host = strings.TrimSpace(body.Host)
|
|
port = body.Port
|
|
if err := validateModbusAddress(host, port); err != nil {
|
|
writeError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
}
|
|
|
|
userRaw := s.userPluginSettings(r.Context(), who.ID)
|
|
newDoc := mergeAnker(userRaw, func(as *ankerStored) {
|
|
if as.ControlChargers == nil {
|
|
as.ControlChargers = map[string]ankerChargerBinding{}
|
|
}
|
|
// Keep whatever else the binding holds: a charger may carry both an OCPP
|
|
// token and a local address, and switching control mode must not discard
|
|
// the other one.
|
|
b := as.ControlChargers[sn]
|
|
b.ModbusHost, b.ModbusPort = host, port
|
|
if b.AddedAt == "" {
|
|
b.AddedAt = time.Now().UTC().Format(time.RFC3339)
|
|
}
|
|
as.ControlChargers[sn] = b
|
|
})
|
|
if err := s.pb.Update(r.Context(), s.usersCollection(), who.ID,
|
|
map[string]any{"pluginSettings": newDoc}, nil); err != nil {
|
|
writePBError(w, err)
|
|
return
|
|
}
|
|
|
|
action := "address.set"
|
|
if host == "" {
|
|
action = "address.clear"
|
|
}
|
|
s.auditControl(who, sn, action, map[string]any{"host": host, "port": port}, "ok", nil)
|
|
writeJSON(w, http.StatusOK, map[string]any{"serial": sn, "modbusHost": host, "modbusPort": port})
|
|
}
|
|
|
|
// validateModbusAddress checks a user-supplied charger address. Loopback is
|
|
// refused because this address is one the server dials on the caller's behalf,
|
|
// and 127.0.0.1 would point it at services running on the server itself rather
|
|
// than at a charger. Other addresses are allowed: the charger is expected on a
|
|
// private LAN, but a routed or VPN deployment is legitimate too.
|
|
func validateModbusAddress(host string, port int) error {
|
|
if host == "" {
|
|
return errors.New("the charger's local address is required")
|
|
}
|
|
if port < 0 || port > 65535 {
|
|
return fmt.Errorf("%d is not a valid port", port)
|
|
}
|
|
if strings.ContainsAny(host, " /\\?#@") {
|
|
return errors.New("the address should be just a hostname or IP, with no scheme or path")
|
|
}
|
|
if ip := net.ParseIP(host); ip != nil {
|
|
if ip.IsLoopback() || ip.IsUnspecified() {
|
|
return errors.New("that address points at the server itself, not at a charger")
|
|
}
|
|
} else if strings.EqualFold(host, "localhost") {
|
|
return errors.New("that address points at the server itself, not at a charger")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ankerModbusSnapshot reads a charger's live state for the status endpoint. It
|
|
// is best effort: a charger that is switched off or on another network simply
|
|
// has no snapshot, which is not an error in a status report.
|
|
func (s *Server) ankerModbusSnapshot(ctx context.Context, binding ankerChargerBinding) (ankersolix.ModbusSnapshot, bool) {
|
|
if strings.TrimSpace(binding.ModbusHost) == "" {
|
|
return ankersolix.ModbusSnapshot{}, false
|
|
}
|
|
ctx, cancel := context.WithTimeout(ctx, modbusActionTimeout)
|
|
defer cancel()
|
|
|
|
client, err := ankersolix.ModbusDial(ctx, ankerModbusConfig(binding))
|
|
if err != nil {
|
|
return ankersolix.ModbusSnapshot{}, false
|
|
}
|
|
defer client.Close()
|
|
|
|
snap, err := ankersolix.ModbusRead(ctx, client, true)
|
|
if err != nil {
|
|
return ankersolix.ModbusSnapshot{}, false
|
|
}
|
|
return snap, true
|
|
}
|