# tma/tm-to-abrp architecture

> Architecture diagram of the GitHub repository tma/tm-to-abrp, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-07-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tma/tm-to-abrp
- Repository: https://github.com/tma/tm-to-abrp

## Overview

##### Project type and purpose
- This is a small, Dockerized Go-based microservice (“TM-to-ABRP Bridge”) whose job is to subscribe to Teslamate’s MQTT feed, transform the live data for A Better Route Planner (ABRP), and optionally expose a minimal web UI for manual control.

##### File structure
- Top-level Docker & VSCode configs (.devcontainer/, Dockerfile, .github/workflows) indicate containerized CI/CD.
- cmd/main.go is the entry point.
- internal/app/ contains the core modules:
- abrp.go – ABRP API client
- car.go – domain model for Tesla car data
- http.go – HTTP server & UI handlers
- messages.go – MQTT subscription and message parsing
- web/public and web/templates hold the static assets and HTML templates for the user interface.
- test/send_messages.go is a smoke test tool.

##### README for additional insights
- The README confirms the service subscribes to an MQTT broker (Teslamate must be configured), processes messages, and forwards them to ABRP using its REST API (requires API keys and tokens).
- It also describes a tiny web UI on a configurable PATH_PREFIX to enable/disable data forwarding for a time window.
- Docker-compose examples show it runs in isolation or alongside Teslamate.

##### Components and their relationships
a. Main components:
- MQTT Broker (external, via Teslamate)
- TM-to-ABRP Bridge (Docker container hosting Go service)
- MQTT Client (internal/messages.go)
- Data Processor & Car Model (internal/car.go)
- ABRP REST Client (internal/abrp.go)
- HTTP Server & UI (internal/http.go + web/templates + web/public)
- ABRP External Service (REST API)
- Web Browser / User (interacts with UI)
b. Interactions:
- Bridge → subscribes to MQTT topics ← Teslamate
- Bridge → transforms incoming payloads → ABRP via HTTPS
- User ← HTTP GET/POST → Bridge’s web server (enable/disable forwarding)
c. Architectural patterns & principles:
- Single-container microservice (12-factor app): environment-driven config, stateless.
- Clear separation of concerns: messaging, domain modeling, external API client, HTTP/UI.
- Minimalist web UI embedded in service.
d. Key technologies: Go, MQTT client library, net/http, HTML templates, Docker

##### Putting it all together
1) MQTT Client ← subscribe
2) Message Processor & Car Model
3) ABRP REST Client → publish
4) HTTP Server + Templates/Static UI
- Environment-variable-driven config at the container perimeter.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Systems
    Broker["Teslamate MQTT Broker"]:::external
    ABRP["ABRP REST API"]:::external
    Browser["User Browser"]:::external
    SmokeTest["Smoke Test Tool"]:::external

    %% Container Boundary
    subgraph "Docker Container: TM-to-ABRP Bridge"
        direction TB
        Main["Main (cmd/main.go)"]:::internal
        MQTT["MQTT Client (internal/app/messages.go)"]:::internal
        Processor["Data Processor & Car Model (internal/app/car.go)"]:::internal
        ABRPClient["ABRP REST Client (internal/app/abrp.go)"]:::internal
        HTTPServer["HTTP Server & UI Handlers (internal/app/http.go)"]:::internal
        subgraph "Web UI"
            direction TB
            Templates["HTML Templates (web/templates/index.html)"]:::ui
            Static["Static Assets (web/public/)"]:::ui
        end
    end

    %% Connections
    Broker -->|MQTT/TCP:1883 subscribe| MQTT
    MQTT -->|parsed data| Processor
    Processor -->|HTTP POST/HTTPS:443| ABRPClient
    ABRPClient -->|HTTPS:443| ABRP
    Browser <-->|HTTP GET/POST:8080| HTTPServer
    HTTPServer --> Templates
    HTTPServer --> Static
    SmokeTest -->|simulate MQTT| Broker
    Main --> MQTT
    Main --> HTTPServer
    Main --> ABRPClient

    %% Click Events
    click Main "https://github.com/tma/tm-to-abrp/blob/main/cmd/main.go"
    click MQTT "https://github.com/tma/tm-to-abrp/blob/main/internal/app/messages.go"
    click Processor "https://github.com/tma/tm-to-abrp/blob/main/internal/app/car.go"
    click ABRPClient "https://github.com/tma/tm-to-abrp/blob/main/internal/app/abrp.go"
    click HTTPServer "https://github.com/tma/tm-to-abrp/blob/main/internal/app/http.go"
    click Templates "https://github.com/tma/tm-to-abrp/blob/main/web/templates/index.html"
    click Static "https://github.com/tma/tm-to-abrp/tree/main/web/public/"
    click SmokeTest "https://github.com/tma/tm-to-abrp/blob/main/test/send_messages.go"

    %% Styles
    classDef external fill:#eeeeee,stroke:#888,stroke-width:1px;
    classDef internal fill:#bbdefb,stroke:#1e88e5,stroke-width:2px;
    classDef ui fill:#c8e6c9,stroke:#388e3c,stroke-width:2px;
```
