Skip to content

Architecture: split the 1,500-line TeslaBleController and move the per-vehicle protocol state machine into core #118

Open

Description

@dzid26

Problem

TeslaBleController.kt is 1,483 lines. Its inner class VehicleLink (L608-L1442,`` about 840 lines) covers all of these, driven by android.os.Handler runnables:

  • transport lifecycle, reconnection and backoff;
  • session handshakes, the pending-request table and encrypted command I/O;
  • the pairing state machine and whitelist slot resolution;
  • polling cadence, history recording and logging.

The outer class adds scanning and discovery, vehicle persistence, selection and the UI state.

Proposal

  • Add a platform-agnostic VehicleClient to core, one instance per car:
    • input: a transport interface (incoming: Flow<ByteArray>, send(ByteArray)), an injectable clock and an injectable CoroutineScope/dispatcher;
    • output: state: StateFlow<VehicleState> and suspend functions (handshake(domain), wake(), readCharge(), pair(key));
    • session and pairing logic written as explicit state machines.
  • Keep Android-specific parts in app: TeslaGattClient (the Android transport), scanning, the foreground service, persistence and UI mapping.
  • Unit-test the client against FakeCarProtocol, moved into a core test fixture, using kotlinx-coroutines-test virtual time. That covers retries, timeouts, faults and pairing without an emulator.
  • Once the class is split, restore detekt's default LargeClass and TooManyFunctions thresholds.

Related: #108 (the KMP path).

Activity

  1. dzid26 commented on Oct 15, 2026

    @dzid26
    CollaboratorAuthor

    Progress: #215 moved the pairing state machine into core as PairingFlow, and #217 moved the session handshake into core as SessionHandshake. The rest of VehicleLink has not been split yet.


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions