Files
DriverVault/API Server/internal/plugins/builtin/ankersolix/mqttsnapshot_test.go
T
tajniak81andClaude Opus 5 576df58776 Go the way the owner's phone already goes
Control had two transports and neither fitted the ordinary customer. OCPP waits
for the charger to dial in, which needs a public endpoint it can reach, a
certificate, and a firmware willing to talk to our CSMS. Modbus TCP dials the
charger, which needs the server on the charger's own network. Between them they
cover a charger we host and a charger we stand next to; the common case is a
charger behind someone else's router, and that had nothing.

It was never unreachable, though. The charger holds a connection open to Anker's
own broker — it is how the mobile app drives it from anywhere, and it is the
mqttStatus register the Modbus snapshot has been reporting all along. So a third
control mode joins that broker as the account: get_user_mqtt_info issues a client
certificate, mTLS to aiot-mqtt-eu.anker.com:8883, and commands go out on the same
topics the app publishes on. Nothing on the customer's side has to be forwarded,
addressed or certificated.

What travels is not an API call. The payload is a JSON envelope around a base64
binary frame the device itself speaks — marker, little-endian length, message
type, name/length/type/value fields, XOR checksum — so mqttframe.go is a codec
rather than a client, written from the message maps in anker-solix-api and
anchored on the one frame that project documents byte for byte. A frame whose
fields do not tile exactly up to the checksum is refused rather than half-read:
these arrive over a link we do not control, and a truncated frame must not read
as a charger reporting zeros.

Two of the charger's habits shape the rest. It publishes nothing unless asked, so
a status read arms a telemetry trigger and waits for the next frame, and a poll
inside that window answers from what has since arrived. And a broker connection
costs a fetched certificate and a TLS handshake while the plugin manager builds a
throwaway instance per request — so the connection lives on the account's shared
session beside the auth token, for exactly the reason the token lives there, and
closes itself after five idle minutes.

The transport also sees two signals no other one does: the boost flag, and the
plug and start countdowns. The package doc has said since the first commit that
they are never set and the derived mode must do without them. Here they are set,
so a charger that has been told to start and is counting down a delay says so
rather than sitting in "preparing", and "skip the delay" is offered only while
there is a delay to skip.

The clients generalise instead of growing a second layout. Both snapshots name
the same quantities the same way, so what was Modbus-only in the readouts is now
whichever transport read the charger — ModbusStatus becomes ChargerStatus on the
phone, mb becomes dev on the web. What each transport can be *told* still
differs, and the buttons branch on that: reset and clear-limit stay with OCPP,
the timeout and phase registers with Modbus, skip-delay with the cloud. A command
a transport has no equivalent for is refused by name, saying which one has it.

The cost is worth saying plainly. This leans on Anker's cloud being up and on an
unofficial protocol the app may change under us, where Modbus leans on nothing
but the LAN. And it is checked against the reference implementation's own worked
example rather than against hardware — there is no charger on this end to point
it at.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-02 16:47:10 +02:00

147 lines
5.3 KiB
Go

package ankersolix
import (
"slices"
"testing"
)
func TestProjectSnapshotNamesTheChargerState(t *testing.T) {
snap := projectMqttSnapshot("SN1", "A5191", map[string]any{
"voltageL1": 230.1,
"currentL1": 16.0,
"powerTotal": 3680.0,
"sessionSeconds": 3725.0,
"sessionWh": 12500.0,
"status": 2.0,
"ocppStatus": 2.0,
"cpSignal": 5.0,
"phaseMode": 1.0,
})
if snap.Serial != "SN1" || snap.Model != "A5191" {
t.Errorf("snapshot identifies %s/%s", snap.Serial, snap.Model)
}
if snap.StatusDesc != stateCharging {
t.Errorf("statusDesc = %q, want %q", snap.StatusDesc, stateCharging)
}
if snap.OcppStatusDesc != "connected" {
t.Errorf("ocppStatusDesc = %q, want connected", snap.OcppStatusDesc)
}
// The control-pilot names are the charger's own, shared with the Modbus map.
if snap.CPSignalDesc != cpSignalNames[5] {
t.Errorf("cpSignalDesc = %q, want %q", snap.CPSignalDesc, cpSignalNames[5])
}
if snap.VoltageL1 == nil || *snap.VoltageL1 != 230.1 {
t.Errorf("voltageL1 = %v", snap.VoltageL1)
}
// A quantity the charger did not send stays nil, so a view can tell "not
// reported" from "zero" — an unplugged charger really does read 0 A.
if snap.VoltageL2 != nil {
t.Errorf("voltageL2 = %v, want nil for a value that was not sent", *snap.VoltageL2)
}
if snap.Settings != nil {
t.Errorf("settings = %+v, want none until a settings message arrives", snap.Settings)
}
if snap.Local != nil {
t.Errorf("local = %+v, want none until a settings message arrives", snap.Local)
}
}
// The mode is the one thing the cloud REST view can only approximate: it has no
// boost flag and no countdowns, so a charger waiting out a start delay reads to
// it as simply started. Over MQTT those fields exist, and the mode must use them.
func TestProjectSnapshotDerivesModeFromTheMqttOnlySignals(t *testing.T) {
waiting := projectMqttSnapshot("SN1", "A5191", map[string]any{
"status": 1.0, // preparing
"startCountdownSeconds": 45.0,
})
if waiting.Mode != modeWaitStart {
t.Errorf("mode = %q, want %q while a start delay is running", waiting.Mode, modeWaitStart)
}
if !slices.Contains(waiting.ModeOptions, modeSkipDelay) {
t.Errorf("modeOptions = %v, want the delay to be skippable", waiting.ModeOptions)
}
plugging := projectMqttSnapshot("SN1", "A5191", map[string]any{
"status": 1.0,
"plugCountdownSeconds": 60.0,
})
if plugging.Mode != modeWaitPlug {
t.Errorf("mode = %q, want %q while it waits for a plug", plugging.Mode, modeWaitPlug)
}
boosting := projectMqttSnapshot("SN1", "A5191", map[string]any{
"status": 2.0, // charging
"boostMode": 1.0,
})
if boosting.Mode != modeBoostCharge {
t.Errorf("mode = %q, want %q while boost is running", boosting.Mode, modeBoostCharge)
}
idle := projectMqttSnapshot("SN1", "A5191", map[string]any{"status": 0.0})
if idle.Mode != modeStopCharge {
t.Errorf("mode = %q, want %q in standby", idle.Mode, modeStopCharge)
}
if !slices.Contains(idle.ModeOptions, modeStartCharge) {
t.Errorf("modeOptions = %v, want a standby charger to be startable", idle.ModeOptions)
}
}
// Two of the charger's settings read 1 for on and 2 for off, which is the
// opposite of every other flag it sends: read as booleans they would both come
// back on.
func TestProjectSnapshotHandlesTheInvertedSwitches(t *testing.T) {
off := projectMqttSnapshot("SN1", "A5191", map[string]any{
"plugLockSwitch": 2.0,
"scheduleSwitch": 2.0,
})
if off.Settings == nil {
t.Fatal("settings missing")
}
if off.Settings.PlugLock == nil || *off.Settings.PlugLock {
t.Errorf("plugLock = %v, want off for the charger's value 2", off.Settings.PlugLock)
}
if off.Settings.ScheduleEnabled == nil || *off.Settings.ScheduleEnabled {
t.Errorf("scheduleEnabled = %v, want off for the charger's value 2", off.Settings.ScheduleEnabled)
}
on := projectMqttSnapshot("SN1", "A5191", map[string]any{"plugLockSwitch": 1.0})
if on.Settings == nil || on.Settings.PlugLock == nil || !*on.Settings.PlugLock {
t.Errorf("plugLock = %v, want on for the charger's value 1", on.Settings)
}
}
// The settings message carries the charger's own view of its LAN side, which is
// the address the Modbus mode otherwise has to be told by hand.
func TestProjectSnapshotReportsLocalAccess(t *testing.T) {
snap := projectMqttSnapshot("SN1", "A5191", map[string]any{
"modbusSwitch": 1.0,
"ipAddress": "192.168.1.44",
"modbusPort": 502.0,
"modbusTimeoutSeconds": 60.0,
"maxCurrentSetA": 32.0,
})
if snap.Local == nil {
t.Fatal("local access missing")
}
if snap.Local.ModbusEnabled == nil || !*snap.Local.ModbusEnabled {
t.Errorf("modbusEnabled = %v, want on", snap.Local.ModbusEnabled)
}
if snap.Local.Host != "192.168.1.44" {
t.Errorf("host = %q", snap.Local.Host)
}
if snap.Local.Port == nil || *snap.Local.Port != 502 {
t.Errorf("port = %v, want 502", snap.Local.Port)
}
if snap.Settings == nil || snap.Settings.MaxCurrentA == nil || *snap.Settings.MaxCurrentA != 32 {
t.Errorf("settings.maxCurrentA = %+v, want 32", snap.Settings)
}
}
func TestProjectSnapshotOfNothingIsEmpty(t *testing.T) {
snap := projectMqttSnapshot("SN1", "A5191", map[string]any{})
if snap.Status != nil || snap.Mode != "" || snap.Settings != nil || snap.Local != nil {
t.Errorf("an empty message set produced state: %+v", snap)
}
}