File size: 6,246 Bytes
5c78d2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# ReachyMiniApp Framework

The Reachy Mini SDK provides a base class `ReachyMiniApp` for building applications. It handles lifecycle management, stop signals, and optionally serves a settings web UI.

## Quick Reference

```python
import threading
from reachy_mini import ReachyMini, ReachyMiniApp

class MyApp(ReachyMiniApp):
    custom_app_url: str | None = "http://0.0.0.0:8042"
    
    def run(self, reachy_mini: ReachyMini, stop_event: threading.Event):
        while not stop_event.is_set():
            # Your main loop here
            pass

if __name__ == "__main__":
    app = MyApp()
    app.wrapped_run()
```

## Class Structure

### `ReachyMiniApp` (Abstract Base Class)

**Source:** `reachy_mini/apps/app.py`

```python
class ReachyMiniApp(ABC):
    custom_app_url: str | None = None      # URL for settings web UI
    dont_start_webserver: bool = False     # Skip web server startup
    
    def __init__(self) -> None:
        self.stop_event = threading.Event()
        self.error: str = ""
        self.settings_app: FastAPI | None = None
        # ...
    
    @abstractmethod
    def run(self, reachy_mini: ReachyMini, stop_event: threading.Event) -> None:
        """Main app logic - you implement this."""
        pass
    
    def wrapped_run(self, *args, **kwargs) -> None:
        """Wrapper that handles setup/teardown."""
        pass
    
    def stop(self) -> None:
        """Gracefully stop the app."""
        self.stop_event.set()
```

## Key Attributes

### `custom_app_url`

If set, the app will start a FastAPI web server at this URL.

```python
custom_app_url: str | None = "http://0.0.0.0:8042"  # Serve at port 8042
custom_app_url: str | None = None                   # No web UI
```

### `settings_app`

A FastAPI instance for adding custom API endpoints. Only available if `custom_app_url` is set.

```python
@self.settings_app.post("/my_endpoint")
def my_handler():
    return {"status": "ok"}
```

### `stop_event`

A `threading.Event` that gets set when the app should stop. Check this in your main loop:

```python
while not stop_event.is_set():
    # Keep running
    time.sleep(0.01)
```

## Static Files

The framework automatically serves static files from `<your_app>/static/`:

```
reachy_mini_metronome/
β”œβ”€β”€ main.py              # Your app code
└── static/
    β”œβ”€β”€ index.html       # Served at /
    β”œβ”€β”€ style.css        # Served at /static/style.css
    └── script.js        # Served at /static/script.js
```

**Source:** `reachy_mini/apps/app.py`

```python
static_dir = self._get_instance_path().parent / "static"
if static_dir.exists():
    self.settings_app.mount("/static", StaticFiles(directory=static_dir), name="static")
    
    index_file = static_dir / "index.html"
    if index_file.exists():
        @self.settings_app.get("/")
        async def index() -> FileResponse:
            return FileResponse(index_file)
```

## Adding API Endpoints

You can add FastAPI endpoints in your `run()` method:

```python
def run(self, reachy_mini: ReachyMini, stop_event: threading.Event):
    # State variables
    bpm = 120
    is_running = False
    
    # Define endpoints
    @self.settings_app.post("/bpm")
    def update_bpm(data: dict):
        nonlocal bpm
        bpm = data.get("bpm", bpm)
        return {"bpm": bpm}
    
    @self.settings_app.post("/start")
    def start():
        nonlocal is_running
        is_running = True
        return {"running": True}
    
    @self.settings_app.get("/status")
    def status():
        return {"bpm": bpm, "running": is_running}
    
    # Main loop
    while not stop_event.is_set():
        if is_running:
            # Do stuff
            pass
        time.sleep(0.01)
```

## Lifecycle

When you call `app.wrapped_run()`:

1. **Web server starts** (if `custom_app_url` is set) on a background thread
2. **ReachyMini connects** using context manager
3. **Your `run()` method executes**
4. **On stop** (Ctrl+C or `stop_event.set()`):
   - ReachyMini disconnects
   - Web server shuts down

```python
def wrapped_run(self, *args, **kwargs) -> None:
    # Start settings server in background thread
    if self.settings_app is not None:
        # ... uvicorn server setup ...
        settings_app_t.start()
    
    try:
        with ReachyMini(*args, **kwargs) as reachy_mini:
            self.run(reachy_mini, self.stop_event)
    finally:
        if settings_app_t is not None:
            self.stop_event.set()
            settings_app_t.join()
```

## Metronome App Structure

For our metronome, the structure will be:

```python
from pydantic import BaseModel

class BpmUpdate(BaseModel):
    bpm: int

class TimeSignatureUpdate(BaseModel):
    beats: int

class ReachyMiniMetronome(ReachyMiniApp):
    custom_app_url = "http://0.0.0.0:8042"
    
    def run(self, reachy_mini: ReachyMini, stop_event: threading.Event):
        # State
        bpm = 120
        time_signature = 4
        current_beat = 1
        is_running = False
        
        # API endpoints
        @self.settings_app.post("/bpm")
        def set_bpm(data: BpmUpdate):
            nonlocal bpm
            bpm = max(40, min(208, data.bpm))
            return {"bpm": bpm}
        
        @self.settings_app.post("/time_signature")
        def set_time_signature(data: TimeSignatureUpdate):
            nonlocal time_signature, current_beat
            time_signature = data.beats
            current_beat = 1
            return {"time_signature": time_signature}
        
        @self.settings_app.post("/start")
        def start():
            nonlocal is_running
            is_running = True
            return {"running": True}
        
        @self.settings_app.post("/stop")
        def stop():
            nonlocal is_running
            is_running = False
            return {"running": False}
        
        @self.settings_app.get("/status")
        def get_status():
            return {
                "bpm": bpm,
                "time_signature": time_signature,
                "current_beat": current_beat,
                "running": is_running
            }
        
        # Main loop
        while not stop_event.is_set():
            # ... metronome logic ...
            time.sleep(0.01)
```