-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsetup.py
More file actions
332 lines (283 loc) · 13.4 KB
/
setup.py
File metadata and controls
332 lines (283 loc) · 13.4 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Interaktives Setup-Tool für den KNX Logger und Explorer.
- Findet Gateways, fragt nach der Konfiguration und speichert sie in einer .env-Datei.
- Erfragt Pfade für Log-Dateien und die ETS-Projektdatei.
"""
import asyncio
import logging
import sys
import os
import re
from pathlib import Path
from typing import List, Tuple, Optional
# Setup-spezifische Imports
from dotenv import set_key, find_dotenv
try:
from textual.app import App, ComposeResult
from textual.containers import VerticalScroll
from textual.screen import Screen, ModalScreen
from textual.widgets import Button, Header, Footer, Label, LoadingIndicator, RadioButton, RadioSet, Input
TEXTUAL_INSTALLED = True
except ImportError:
TEXTUAL_INSTALLED = False
# XKNX-Imports
from xknx import XKNX
from xknx.io import GatewayScanner
from xknx.exceptions import XKNXException
from xknx.io.gateway_scanner import GatewayDescriptor
# --- Setup-TUI-Komponenten ---
class SelectionModeScreen(Screen[str]):
"""Fragt, ob ein Gateway gespeichert oder bei jedem Start gesucht werden soll."""
def compose(self) -> ComposeResult:
yield Header(show_clock=True)
yield VerticalScroll(
Label("Konfiguration des Gateways", id="question"),
RadioSet(
RadioButton("Ein bestimmtes Gateway suchen und speichern (empfohlen)", id="save"),
RadioButton("Bei jedem Start automatisch nach einem Gateway suchen", id="auto"),
id="mode_options"
),
Button("Weiter", variant="primary", id="confirm_mode"),
id="dialog",
)
yield Footer()
def on_mount(self) -> None:
self.query_one(RadioSet).focus()
self.query_one("#save").value = True
def on_button_pressed(self, event: Button.Pressed) -> None:
if self.query_one("#save").value:
self.dismiss("save")
else:
self.dismiss("auto")
class PathScreen(Screen[str]):
"""Fragt nach einem Pfad für Log-Dateien oder Projektdateien."""
def __init__(self, prompt: str, default_path: str, info_text: str):
super().__init__()
self.prompt = prompt
self.default_path = default_path
self.info_text = info_text
def compose(self) -> ComposeResult:
yield Header(show_clock=True)
yield VerticalScroll(
Label(self.prompt, id="question"),
Input(self.default_path, id="path_input"),
Label(self.info_text, classes="info-label"),
Button("Speichern und weiter", variant="primary", id="confirm_path"),
id="dialog",
)
yield Footer()
def on_mount(self) -> None:
self.query_one(Input).focus()
def on_button_pressed(self, event: Button.Pressed) -> None:
path = self.query_one(Input).value
self.dismiss(path)
class PasswordScreen(Screen[str]):
"""Fragt nach dem Passwort für die Projektdatei."""
def compose(self) -> ComposeResult:
yield Header(show_clock=True)
yield VerticalScroll(
Label("Passwort für die .knxproj-Datei (leer lassen, wenn keins)", id="question"),
Input(placeholder="Projektpasswort...", password=True, id="password_input"),
Button("Speichern und weiter", variant="primary", id="confirm_password"),
id="dialog",
)
yield Footer()
def on_mount(self) -> None:
self.query_one(Input).focus()
def on_button_pressed(self, event: Button.Pressed) -> None:
self.dismiss(self.query_one(Input).value)
class StartScreen(Screen[bool]):
"""Startbildschirm für den Gateway-Scan."""
BINDINGS = [("q", "request_quit", "Beenden")]
def compose(self) -> ComposeResult:
yield Header(show_clock=True)
yield VerticalScroll(
Label("Willkommen beim interaktiven Setup.", id="question"),
Label("Möchten Sie jetzt nach KNX-Gateways suchen?"),
Button("Scan starten", variant="primary", id="start_scan"),
Button("Abbrechen", variant="default", id="cancel"),
id="dialog",
)
yield Footer()
def on_button_pressed(self, event: Button.Pressed) -> None:
self.dismiss(event.button.id == "start_scan")
def action_request_quit(self) -> None:
self.app.exit(message="Setup wurde durch Benutzer beendet.")
class LoadingScreen(Screen[Tuple[List[GatewayDescriptor], Optional[Exception]]]):
"""Zeigt einen Ladeindikator während des Scans."""
def compose(self) -> ComposeResult:
yield Header(show_clock=True)
yield VerticalScroll(
Label("Suche nach KNX-Gateways im Netzwerk...", classes="centered-label"),
LoadingIndicator(),
Label("(Scan läuft für maximal 12 Sekunden)", classes="info-label"),
id="dialog"
)
async def on_mount(self) -> None:
self.app.log("Starte KNX Gateway-Suche...")
gateways: List[GatewayDescriptor] = []
error: Optional[Exception] = None
try:
scanner = GatewayScanner(XKNX(), timeout_in_seconds=12)
gateways = await scanner.scan()
self.app.log(f"Scan beendet. Gesammelte Gateways: {gateways}")
except Exception as e:
logging.exception("Ein Fehler ist bei der Gateway-Suche aufgetreten:")
error = e
self.dismiss(result=(gateways, error))
class SelectionScreen(Screen[str]):
"""Ermöglicht die Auswahl eines gefundenen Gateways."""
def __init__(self, gateways: List[GatewayDescriptor]):
super().__init__()
self.gateways = gateways
def compose(self) -> ComposeResult:
yield Header(show_clock=True)
gateway_labels = [f"{gw.name} ({gw.ip_addr}:{gw.port})" for gw in self.gateways]
radio_buttons = [RadioButton(label=gw, id=f"gateway_{i}") for i, gw in enumerate(gateway_labels)]
yield VerticalScroll(
Label("Bitte wählen Sie ein Gateway aus:", id="question"),
RadioSet(*radio_buttons, id="gateway_options"),
Button("Bestätigen", variant="primary", id="confirm_selection"),
id="dialog"
)
yield Footer()
def on_mount(self) -> None:
self.query_one(RadioSet).focus()
if self.query(RadioButton):
self.query(RadioButton).first().value = True
def on_button_pressed(self, event: Button.Pressed) -> None:
radioset = self.query_one(RadioSet)
if radioset.pressed_button:
self.dismiss(result=radioset.pressed_button.label.plain)
class ResultScreen(Screen[None]):
"""Zeigt das Endergebnis des Setups an."""
def __init__(self, message: str):
super().__init__()
self.message = message
def compose(self) -> ComposeResult:
yield Header(show_clock=True)
yield VerticalScroll(
Label(self.message, id="result_message"),
Button("Beenden", variant="primary", id="quit_app"),
id="dialog",
)
yield Footer()
def on_button_pressed(self, event: Button.Pressed) -> None:
self.app.exit()
class SetupApp(App):
"""Die Textual-App für das interaktive Setup."""
CSS = """
Screen { align: center middle; }
#dialog { padding: 1 2; width: 80; max-width: 90%; height: auto; border: round #666; background: $surface; }
#question { width: 100%; text-align: center; margin-bottom: 1; }
Button { width: 100%; }
Input { width: 100%; margin-bottom: 1; }
#result_message { width: 100%; text-align: center; margin: 1 0; }
.centered-label { text-align: center; width: 100%; margin: 1 0; }
.info-label { color: $text-muted; text-align: center; width: 100%; }
RadioSet { margin: 1 0; }
"""
BINDINGS = [("q", "request_quit", "Beenden")]
def on_mount(self) -> None:
logging.info("Setup-App gestartet.")
self.run_worker(self.run_setup_flow, exclusive=True)
async def run_setup_flow(self) -> None:
try:
# .env-Datei finden oder erstellen
dotenv_path = find_dotenv()
if not dotenv_path:
logging.warning("Keine .env-Datei gefunden, erstelle eine neue.")
env_file_path = Path(".env")
env_file_path.touch()
dotenv_path = str(env_file_path.resolve())
# 1. Gateway-Modus abfragen
selection_mode = await self.push_screen_wait(SelectionModeScreen())
logging.info(f"Gateway-Modus ausgewählt: {selection_mode}")
# 2. Pfade abfragen
log_path = await self.push_screen_wait(
PathScreen("In welchem Verzeichnis sollen die Log-Dateien gespeichert werden?", os.getcwd(), "Der Pfad wird erstellt, falls er nicht existiert.")
)
logging.info(f"Log-Pfad ausgewählt: {log_path}")
set_key(dotenv_path, "LOG_PATH", log_path)
knx_project_path = await self.push_screen_wait(
PathScreen("Pfad zur .knxproj-Datei:", "", "Kann ein relativer oder absoluter Pfad sein.")
)
logging.info(f"KNX-Projekt-Pfad ausgewählt: {knx_project_path}")
set_key(dotenv_path, "KNX_PROJECT_PATH", knx_project_path)
# 3. Passwort abfragen
knx_password = await self.push_screen_wait(PasswordScreen())
logging.info("KNX-Passwort eingegeben.")
set_key(dotenv_path, "KNX_PASSWORD", knx_password)
# 4. Gateway-spezifische Logik
if selection_mode == "auto":
set_key(dotenv_path, "KNX_GATEWAY_IP", "AUTO")
set_key(dotenv_path, "KNX_GATEWAY_PORT", "0")
logging.info("Konfiguration für AUTO-Modus in .env gespeichert.")
await self.push_screen(ResultScreen("✅ Setup erfolgreich!\n\nDie Konfiguration wurde in '.env' gespeichert."))
return
if selection_mode == "save":
start_scan = await self.push_screen_wait(StartScreen())
if not start_scan:
logging.warning("Gateway-Scan vom Benutzer abgebrochen.")
await self.push_screen(ResultScreen("Setup wurde abgebrochen."))
return
gateways, error = await self.push_screen_wait(LoadingScreen())
if error:
logging.error("Fehler von LoadingScreen erhalten.")
await self.push_screen(ResultScreen(f"❌ Fehler bei der Gateway-Suche:\n{error}"))
return
if not gateways:
logging.warning("Keine Gateways gefunden.")
await self.push_screen(ResultScreen("❌ Es konnten keine Gateways gefunden werden."))
return
selected_gateway_str: str
if len(gateways) == 1:
gw = gateways[0]
selected_gateway_str = f"{gw.name} ({gw.ip_addr}:{gw.port})"
else:
selected_gateway_str = await self.push_screen_wait(SelectionScreen(gateways))
if selected_gateway_str:
logging.info(f"Gateway ausgewählt: {selected_gateway_str}")
match = re.search(r"\((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d+)\)", selected_gateway_str)
if match:
ip_addr, port = match.groups()
set_key(dotenv_path, "KNX_GATEWAY_IP", ip_addr)
set_key(dotenv_path, "KNX_GATEWAY_PORT", port)
logging.info(f"Gateway {ip_addr}:{port} in .env gespeichert.")
await self.push_screen(ResultScreen(f"✅ Setup erfolgreich!\n\nAlle Einstellungen wurden in '.env' gespeichert."))
else:
logging.error(f"Konnte IP/Port nicht aus '{selected_gateway_str}' extrahieren.")
await self.push_screen(ResultScreen("Fehler: IP/Port konnten nicht extrahiert werden."))
else:
logging.warning("Kein Gateway in SelectionScreen bestätigt.")
await self.push_screen(ResultScreen("❌ Es wurde kein Gateway ausgewählt."))
except Exception as e:
logging.exception("Ein unerwarteter Fehler ist im Setup-Flow aufgetreten:")
await self.push_screen(ResultScreen(f"Ein schwerwiegender Fehler ist aufgetreten. Details siehe 'knx_app_debug.log'."))
def action_request_quit(self) -> None:
logging.info("Setup vom Benutzer über 'q' beendet.")
self.exit(message="Setup wurde durch Benutzer beendet.")
def main():
"""Startpunkt der Setup-Anwendung."""
# Basic logging setup for the application's debug log
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
handlers=[
logging.FileHandler("knx_setup_debug.log", mode='w', encoding='utf-8'),
logging.StreamHandler(sys.stdout)
]
)
if not TEXTUAL_INSTALLED:
logging.error("Für das interaktive Setup müssen die Pakete 'textual' und 'python-dotenv' installiert sein.")
logging.error("Bitte führen Sie aus: pip install textual python-dotenv")
sys.exit(1)
logging.info("Starte interaktives Setup...")
app = SetupApp()
app.run()
logging.info("Setup-Prozess beendet.")
print("\nFühren Sie 'knx_logger.py' zum Loggen oder 'knx_lens.py' zum Analysieren aus.")
if __name__ == "__main__":
main()