Add profile persistence for color changes and data saving
- Added save_current_profile() function to persist lights data to profile files - Updated all endpoints to save to profile files after changes - Ensures color changes, pattern changes, and tab modifications are persisted - Data now saves to both settings.json (patterns) and profile files (lights data)
This commit is contained in:
@@ -83,6 +83,40 @@ def save_pattern_settings(tab_name, pattern_name, colors=None, delay=None, n_par
|
||||
pattern_settings[f"n{i}"] = n_params[f"n{i}"]
|
||||
|
||||
|
||||
def save_current_profile():
|
||||
"""Save current settings to the active profile file."""
|
||||
current_profile = settings.get("current_profile")
|
||||
if not current_profile:
|
||||
return
|
||||
|
||||
try:
|
||||
profiles_dir = "profiles"
|
||||
os.makedirs(profiles_dir, exist_ok=True)
|
||||
profile_path = os.path.join(profiles_dir, f"{current_profile}.json")
|
||||
|
||||
# Get current tab order
|
||||
tab_order = settings.get("tab_order", [])
|
||||
if "lights" in settings:
|
||||
# Ensure all current tabs are in the order
|
||||
current_tabs = set(settings["lights"].keys())
|
||||
order_tabs = set(tab_order)
|
||||
for tab in current_tabs:
|
||||
if tab not in order_tabs:
|
||||
tab_order.append(tab)
|
||||
settings["tab_order"] = tab_order
|
||||
|
||||
# Save to profile file (exclude current_profile from profile)
|
||||
profile_data = dict(settings)
|
||||
profile_data.pop("current_profile", None)
|
||||
profile_data.pop("patterns", None) # Patterns stay in settings.json
|
||||
|
||||
with open(profile_path, 'w') as file:
|
||||
json.dump(profile_data, file, indent=4)
|
||||
print(f"Profile '{current_profile}' saved successfully.")
|
||||
except Exception as e:
|
||||
print(f"Error saving profile: {e}")
|
||||
|
||||
|
||||
async def send_to_lighting_controller(payload):
|
||||
"""Send data to the lighting controller via WebSocket."""
|
||||
global websocket_client
|
||||
@@ -257,7 +291,9 @@ def set_parameters():
|
||||
}
|
||||
run_async(send_to_lighting_controller(payload))
|
||||
|
||||
# Save to settings.json (for patterns) and to profile file (for lights data)
|
||||
settings.save()
|
||||
save_current_profile()
|
||||
|
||||
return jsonify({"success": True})
|
||||
|
||||
@@ -303,6 +339,7 @@ def create_tab():
|
||||
settings["tab_order"] = []
|
||||
settings["tab_order"].append(tab_name)
|
||||
settings.save()
|
||||
save_current_profile()
|
||||
|
||||
return jsonify({"success": True, "tab_name": tab_name})
|
||||
|
||||
@@ -336,6 +373,7 @@ def update_tab(tab_name):
|
||||
settings["lights"][tab_name]["names"] = ids if isinstance(ids, list) else [ids]
|
||||
|
||||
settings.save()
|
||||
save_current_profile()
|
||||
|
||||
return jsonify({"success": True, "tab_name": tab_name})
|
||||
|
||||
@@ -352,6 +390,7 @@ def delete_tab(tab_name):
|
||||
settings["tab_order"].remove(tab_name)
|
||||
|
||||
settings.save()
|
||||
save_current_profile()
|
||||
|
||||
return jsonify({"success": True})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user