# wrobell/btzen architecture

> Architecture diagram of the GitHub repository wrobell/btzen, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/wrobell/btzen
- Repository: https://github.com/wrobell/btzen

## Overview

##### Main Components

a. Core Library Components:
- Connection Manager (represented by _cm.pyx and cm.py)
- D-Bus Interface Layer (_sd_bus.pyx and _sd_bus.pxd)
- Device Management Layer (device.py)
- Service Layer (service.py)
- Session Management (session.py)

b. External Components:
- Bluez Daemon (with experimental features)
- D-Bus System Bus
- Physical Bluetooth Devices

##### Component Relationships

Layer 1 (Application Layer):
- User applications/scripts (from scripts/ directory)
- To BTZen Public API

Layer 2 (BTZen Public API):
- Device-specific modules (sensortag.py, thingy52.py, btweight.py)
- Async interface

Layer 3 (Core Services):
- Connection Manager
- Session Manager
- Device Manager
- Service Handler

Layer 4 (Low-Level Interface):
- D-Bus Interface
- Bluez Integration

Layer 5 (System Level):
- Bluez Daemon
- Physical Bluetooth Devices

##### Important Architectural Aspects to Highlight

a. Asynchronous Nature:
- Async communication paths
- Parallel device handling capability

b. Connection Management:
- Automatic reconnection logic
- Connection state management

c. Device Support:
- Extensibility for different device types
- Standard and non-standard device handling

##### Specific Features

- GATT notification interface
- Automatic reconnection paths
- Serial device support (Stollmann protocol)
- Device-specific implementations
- Battery level monitoring
- Connection management flows

##### Technical Details

- Python 3.11+ requirement
- Cython integration points
- Bluez 5.68 dependency
- sd-bus system interface
- uvloop compatibility

##### Notes Section

Important notes about:
- Experimental Bluez API requirement
- Connection management constraints
- MAC address requirements
- Performance considerations

Remember to emphasize the library's key strengths:
- Parallel device handling
- Efficient resource usage
- Integration with asyncio
- Extensible device support
- Low-level optimization with Cython

## Diagram (Mermaid)

```mermaid
graph TB
    %% Styles
    classDef core fill:#b3d1ff,stroke:#333
    classDef external fill:#98FB98,stroke:#333
    classDef device fill:#FFE4B5,stroke:#333
    classDef system fill:#D3D3D3,stroke:#333
    classDef scripts fill:#FFA07A,stroke:#333

    %% Layer 1: Application Layer
    subgraph Applications
        Scripts["User Scripts/Applications"]:::scripts
        BatteryScript["Battery Monitor"]:::scripts
        ConnectScript["Connection Tool"]:::scripts
        DetectScript["Device Detection"]:::scripts
    end

    %% Layer 2: BTZen Public API
    subgraph "BTZen Public API"
        CoreAPI["Core Library Entry"]:::core
        SensorTag["SensorTag Module"]:::device
        Thingy52["Thingy52 Module"]:::device
        BTWeight["Weight Scale Module"]:::device
    end

    %% Layer 3: Core Services
    subgraph "Core Services"
        ConnMgr["Connection Manager"]:::core
        SessionMgr["Session Manager"]:::core
        DeviceMgr["Device Manager"]:::core
        ServiceHandler["Service Handler"]:::core
        ErrorHandler["Error Handler"]:::core
        DataHandler["Data Handler"]:::core
        Config["Configuration"]:::core
    end

    %% Layer 4: Low-Level Interface
    subgraph "Low-Level Interface"
        DBusInterface["D-Bus Interface"]:::core
        BluezIntegration["Bluez Integration"]:::core
        SerialSupport["Serial Device Support"]:::core
    end

    %% Layer 5: System Level
    subgraph "System Level"
        BluezDaemon["Bluez Daemon"]:::system
        DBusSystem["D-Bus System Bus"]:::system
        BTDevices["Physical BT Devices"]:::external
    end

    %% Relationships
    Scripts --> CoreAPI
    BatteryScript --> CoreAPI
    ConnectScript --> CoreAPI
    DetectScript --> CoreAPI

    CoreAPI --> SensorTag & Thingy52 & BTWeight
    CoreAPI --> ConnMgr
    
    SensorTag & Thingy52 & BTWeight --> ServiceHandler
    
    ConnMgr --> SessionMgr
    SessionMgr --> DeviceMgr
    DeviceMgr --> ServiceHandler
    ServiceHandler --> DataHandler
    
    ServiceHandler --> DBusInterface
    DBusInterface --> BluezIntegration
    BluezIntegration --> SerialSupport
    
    DBusInterface --> DBusSystem
    DBusSystem --> BluezDaemon
    BluezDaemon --> BTDevices

    %% Click Events
    click Scripts "https://github.com/wrobell/btzen/tree/master/scripts/"
    click CoreAPI "https://github.com/wrobell/btzen/blob/master/btzen/__init__.py"
    click SensorTag "https://github.com/wrobell/btzen/blob/master/btzen/sensortag.py"
    click Thingy52 "https://github.com/wrobell/btzen/blob/master/btzen/thingy52.py"
    click BTWeight "https://github.com/wrobell/btzen/blob/master/btzen/btweight.py"
    click ConnMgr "https://github.com/wrobell/btzen/blob/master/btzen/_cm.pyx"
    click SessionMgr "https://github.com/wrobell/btzen/blob/master/btzen/session.py"
    click DeviceMgr "https://github.com/wrobell/btzen/blob/master/btzen/device.py"
    click ServiceHandler "https://github.com/wrobell/btzen/blob/master/btzen/service.py"
    click ErrorHandler "https://github.com/wrobell/btzen/blob/master/btzen/error.py"
    click DataHandler "https://github.com/wrobell/btzen/blob/master/btzen/data.py"
    click Config "https://github.com/wrobell/btzen/blob/master/btzen/config.py"
    click DBusInterface "https://github.com/wrobell/btzen/blob/master/btzen/_sd_bus.pyx"
    click BluezIntegration "https://github.com/wrobell/btzen/blob/master/btzen/bluez.py"
    click SerialSupport "https://github.com/wrobell/btzen/blob/master/btzen/serial.py"
    click BatteryScript "https://github.com/wrobell/btzen/tree/master/scripts/btzen-battery"
    click ConnectScript "https://github.com/wrobell/btzen/tree/master/scripts/btzen-connect"
    click DetectScript "https://github.com/wrobell/btzen/tree/master/scripts/btzen-detect"

    %% Legend
    subgraph Legend
        L1["Core Components"]:::core
        L2["External Systems"]:::external
        L3["Device Modules"]:::device
        L4["System Services"]:::system
        L5["User Scripts"]:::scripts
    end
```
