-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_web.py
More file actions
286 lines (229 loc) · 10.5 KB
/
Copy pathmain_web.py
File metadata and controls
286 lines (229 loc) · 10.5 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
"""
Mammotion Linux App - Web-GUI Version
Verwendet eine robuste Web-basierte Benutzeroberfläche statt Qt.
Garantiert große UI-Elemente ohne Plattform-spezifische Probleme.
"""
import asyncio
import logging
import sys
import os
import threading
import time
from typing import Optional
# Pfad für Imports hinzufügen
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
from models.real_mammotion_client import RealMammotionClient, RealMowerInfo
from views.web_gui import WebGUI
from utils.logging_config import setup_logging
class MammotionWebApp:
"""
Hauptanwendung mit Web-GUI
Kombiniert die echte Mammotion-API mit einer robusten Web-Oberfläche.
"""
def __init__(self):
self.logger = logging.getLogger(__name__)
# Komponenten
self.web_gui: Optional[WebGUI] = None
self.mammotion_client: Optional[RealMammotionClient] = None
# Status
self.is_running = False
self.current_mowers = []
self.authenticated_client: Optional[RealMammotionClient] = None
async def initialize(self):
"""Initialisiert die Anwendung"""
self.logger.info("Initialisiere Mammotion Web-App...")
# Web-GUI erstellen
self.web_gui = WebGUI(port=5000)
self.web_gui.set_login_callback(self._handle_login)
self.web_gui.set_command_callback(self._handle_command)
# Mammotion-Client erstellen
self.mammotion_client = RealMammotionClient()
self.logger.info("Mammotion Web-App initialisiert")
def _handle_login(self, email: str, password: str, remember: bool) -> bool:
"""
Behandelt Login-Versuche
Args:
email: E-Mail-Adresse
password: Passwort
remember: Zugangsdaten speichern
Returns:
True wenn erfolgreich
"""
self.logger.info(f"Login-Versuch für {email}")
# Credentials für spätere Verwendung speichern
self.last_email = email
self.last_password = password
try:
# Verwende einen Thread-Pool für async Operationen um Event-Loop-Konflikte zu vermeiden
import concurrent.futures
import threading
def run_async_login():
"""Führt das async Login in einem separaten Thread aus"""
try:
# Neuen Event Loop für diesen Thread erstellen
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
async def do_login():
async with self.mammotion_client as client:
# Authentifizierung
success = await client.authenticate(email, password)
if success:
# Mäher suchen
mowers = await client.discover_devices()
self.current_mowers = mowers
# Authenticated client für Commands speichern
self.authenticated_client = self.mammotion_client
# GUI mit Mäher-Daten aktualisieren
if mowers:
mower_data = {
'status': mowers[0].status,
'battery_level': mowers[0].battery_level,
'position': f"{mowers[0].position_x:.1f}, {mowers[0].position_y:.1f}",
'model': mowers[0].model,
'name': mowers[0].name
}
self.web_gui.update_mower_data(mower_data)
self.logger.info(f"Login erfolgreich - {len(mowers)} Mäher gefunden")
return True
else:
self.logger.error("Login fehlgeschlagen")
return False
result = loop.run_until_complete(do_login())
return result
except Exception as e:
self.logger.error(f"Async Login-Fehler: {e}")
return False
finally:
try:
loop.close()
except:
pass
# Login in Thread-Pool ausführen mit Timeout
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(run_async_login)
# 30 Sekunden Timeout für Login
result = future.result(timeout=30)
return result
except concurrent.futures.TimeoutError:
self.logger.error("Login-Timeout nach 30 Sekunden")
return False
except Exception as e:
self.logger.error(f"Login-Fehler: {e}")
return False
def _handle_command(self, device_id: str, command: str) -> bool:
"""
Behandelt Mäher-Befehle
Args:
device_id: Geräte-ID
command: Befehl
Returns:
True wenn erfolgreich
"""
self.logger.info(f"Befehl '{command}' für Gerät {device_id}")
try:
# Verwende einen Thread-Pool für async Operationen
import concurrent.futures
def run_async_command():
"""Führt das async Command in einem separaten Thread aus"""
try:
# Neuen Event Loop für diesen Thread erstellen
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
async def do_command():
# Neuen Client für Command verwenden (bereits authentifiziert im Demo-Modus)
client = RealMammotionClient()
# Demo-Credentials verwenden falls verfügbar
if hasattr(self, 'last_email') and hasattr(self, 'last_password'):
auth_success = await client.authenticate(self.last_email, self.last_password)
if auth_success:
return await client.send_command(device_id, command)
return False
result = loop.run_until_complete(do_command())
return result
except Exception as e:
self.logger.error(f"Async Command-Fehler: {e}")
return False
finally:
try:
loop.close()
except:
pass
# Command in Thread-Pool ausführen mit Timeout
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(run_async_command)
# 15 Sekunden Timeout für Befehle
result = future.result(timeout=15)
return result
except concurrent.futures.TimeoutError:
self.logger.error("Command-Timeout nach 15 Sekunden")
return False
except Exception as e:
self.logger.error(f"Command-Fehler: {e}")
return False
async def _update_status(self):
"""Aktualisiert den Mäher-Status"""
try:
if self.current_mowers:
device_id = self.current_mowers[0].device_id
async with self.mammotion_client as client:
updated_info = await client.get_device_status(device_id)
if updated_info:
# GUI aktualisieren
mower_data = {
'status': updated_info.status,
'battery_level': updated_info.battery_level,
'position': f"{updated_info.position_x:.1f}, {updated_info.position_y:.1f}",
'model': updated_info.model,
'name': updated_info.name
}
self.web_gui.update_mower_data(mower_data)
except Exception as e:
self.logger.error(f"Status-Update-Fehler: {e}")
def run(self):
"""Startet die Anwendung"""
self.logger.info("Starte Mammotion Web-App...")
self.is_running = True
try:
# Web-GUI starten (blockiert)
self.web_gui.start(open_browser=True)
except KeyboardInterrupt:
self.logger.info("Anwendung durch Benutzer beendet")
except Exception as e:
self.logger.error(f"Unerwarteter Fehler: {e}")
finally:
self.is_running = False
self.logger.info("Mammotion Web-App beendet")
async def main():
"""Hauptfunktion"""
# Logging konfigurieren
setup_logging()
print("Mammotion Linux App (Web-GUI) wird gestartet...")
print("GUI-Komponenten geladen:")
print("✓ Web-basierte Benutzeroberfläche")
print("✓ Responsive Design mit großen UI-Elementen")
print("✓ Echte Mammotion-API-Integration")
print("✓ Plattformunabhängige Darstellung")
print()
print("Hinweis: Öffnet automatisch Browser-Fenster")
print("URL: http://localhost:5000")
print()
try:
# App erstellen und initialisieren
app = MammotionWebApp()
await app.initialize()
# App starten (blockiert bis beendet)
app.run()
except Exception as e:
logging.error(f"Fehler beim Ausführen der App: {e}")
return 1
return 0
if __name__ == "__main__":
try:
exit_code = asyncio.run(main())
sys.exit(exit_code)
except KeyboardInterrupt:
print("\nAnwendung beendet")
sys.exit(0)
except Exception as e:
print(f"Kritischer Fehler: {e}")
sys.exit(1)