-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
168 lines (136 loc) · 6.74 KB
/
app.py
File metadata and controls
168 lines (136 loc) · 6.74 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
import os
import logging
from PyQt6.QtWidgets import QMainWindow, QMessageBox
from PyQt6.QtCore import QSettings, Qt, QTimer
from ui.main_window import MainWindow
from midi_handler import MidiHandler
from api_client import ApiClient
from config_manager import ConfigManager
from update_checker import UpdateManager
logger = logging.getLogger(__name__)
class MidiRestApp(QMainWindow):
def __init__(self, dev_mode=False):
super().__init__()
# Store dev mode flag
self.dev_mode = dev_mode
logger.info("Initializing application components")
# Initialize components
self.config_manager = ConfigManager()
self.api_client = ApiClient()
self.midi_handler = MidiHandler(auto_connect=False) # Don't auto-connect
# Load settings
logger.debug("Loading application settings")
self.settings = QSettings("FoundryVTT", "MidiRestIntegration")
self.load_settings()
# Setup UI
logger.debug("Setting up UI")
self.ui = MainWindow(self)
self.setCentralWidget(self.ui)
self.setWindowTitle("MIDI to Foundry VTT REST API")
# Initialize update manager early to check for updates during startup
logger.debug("Initializing update manager")
self.update_manager = UpdateManager(self)
# Load style sheet
self.load_stylesheet()
# Connect signals
self.connect_signals()
# Setup automatic update checking immediately
QTimer.singleShot(1000, lambda: self.update_manager.check_for_updates_async(silent=True))
logger.info("Application initialization complete")
def load_stylesheet(self):
qss_path = os.path.join(os.path.dirname(__file__), "ui", "style.qss")
try:
with open(qss_path, "r") as f:
self.setStyleSheet(f.read())
logger.debug("Stylesheet loaded successfully")
except Exception as e:
logger.error("Failed to load stylesheet: %s", str(e))
def load_settings(self):
api_url = self.settings.value("api/url", "")
api_key = self.settings.value("api/key", "")
client_id = self.settings.value("api/client_id", "")
logger.info("Loading settings: API URL=%s, Client ID=%s", api_url, client_id)
self.api_client.set_api_config(api_url, api_key, client_id)
# Load MIDI mappings
mappings = self.config_manager.load_mappings()
logger.info("Loaded %d MIDI mappings", len(mappings))
self.midi_handler.set_mappings(mappings)
# Load window state
if self.settings.contains("window/geometry"):
self.restoreGeometry(self.settings.value("window/geometry"))
if self.settings.contains("window/state"):
self.restoreState(self.settings.value("window/state"))
else:
# Default to maximized
self.setWindowState(self.windowState() | Qt.WindowState.WindowMaximized)
def save_settings(self):
logger.debug("Saving application settings")
self.settings.setValue("api/url", self.api_client.api_url)
self.settings.setValue("api/key", self.api_client.api_key)
self.settings.setValue("api/client_id", self.api_client.client_id)
# Save MIDI mappings
self.config_manager.save_mappings(self.midi_handler.mappings)
logger.info("Settings saved successfully")
# Save window state
self.settings.setValue("window/geometry", self.saveGeometry())
self.settings.setValue("window/state", self.saveState())
def connect_signals(self):
# Connect UI signals to handlers
self.ui.config_widget.save_config_signal.connect(self.on_config_changed)
self.ui.mapping_widget.mapping_changed_signal.connect(self.on_mapping_changed)
# Connect MIDI handler to API client with parameters support
self.midi_handler.midi_signal_received.connect(self.on_midi_signal)
logger.debug("All signals connected")
def on_config_changed(self, url, key, client_id):
logger.info("Configuration changed: URL=%s, Client ID=%s", url, client_id)
self.api_client.set_api_config(url, key, client_id)
self.save_settings()
# Refresh client list
self.ui.refresh_clients()
def on_mapping_changed(self, mappings):
logger.info("MIDI mappings updated: %d mappings", len(mappings))
self.midi_handler.set_mappings(mappings)
self.save_settings()
def on_midi_signal(self, midi_event, endpoint, query_params, body_params, path_params):
"""Optimized handler for MIDI signals triggering API calls"""
try:
# Extract HTTP method if present in endpoint string
method = None
if " " in endpoint and endpoint.split(" ")[0] in ["GET", "POST", "PUT", "DELETE"]:
method, endpoint_path = endpoint.split(" ", 1)
else:
endpoint_path = endpoint
# Only log in dev mode to avoid performance overhead
if self.dev_mode:
logger.debug("MIDI signal triggered API call: %s%s with params: %s, %s, path: %s",
f"{method} " if method else "", endpoint_path, query_params, body_params, path_params)
# Make the API call directly with method
response = self.api_client.call_endpoint(
endpoint_path,
params=query_params,
data=body_params,
path_params=path_params,
method=method
)
# Update UI status in a non-blocking way
self.ui.show_status_nonblocking(f"API call: {f'{method} ' if method else ''}{endpoint_path}")
# Minimal feedback in production mode
if self.dev_mode:
logger.debug("API call succeeded: %s, Response: %s", endpoint_path, response)
except Exception as e:
if self.dev_mode:
logger.error("API call failed: %s, Error: %s", endpoint_path, str(e))
self.ui.show_status_nonblocking(f"Error: {str(e)}")
def check_for_updates(self):
"""Manually check for updates."""
logger.info("Manual update check requested")
self.update_manager.check_for_updates_async(silent=False)
def show_status(self, message):
"""Show status message in UI."""
if hasattr(self.ui, 'show_status'):
self.ui.show_status(message)
def closeEvent(self, event):
logger.info("Application closing")
self.save_settings()
self.midi_handler.close()
event.accept()