settings.py 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. import io
  2. import json
  3. import zipfile
  4. from datetime import datetime
  5. from pathlib import Path
  6. from typing import Optional
  7. from fastapi import APIRouter, Depends, UploadFile, File, Query
  8. from fastapi.responses import JSONResponse, StreamingResponse
  9. from sqlalchemy.ext.asyncio import AsyncSession
  10. from sqlalchemy import select
  11. from backend.app.core.config import settings as app_settings
  12. from backend.app.core.database import get_db
  13. from backend.app.models.settings import Settings
  14. from backend.app.models.notification import NotificationProvider
  15. from backend.app.models.notification_template import NotificationTemplate
  16. from backend.app.models.smart_plug import SmartPlug
  17. from backend.app.models.printer import Printer
  18. from backend.app.models.filament import Filament
  19. from backend.app.models.maintenance import MaintenanceType, PrinterMaintenance, MaintenanceHistory
  20. from backend.app.models.archive import PrintArchive
  21. from backend.app.schemas.settings import AppSettings, AppSettingsUpdate
  22. router = APIRouter(prefix="/settings", tags=["settings"])
  23. # Default settings
  24. DEFAULT_SETTINGS = AppSettings()
  25. async def get_setting(db: AsyncSession, key: str) -> str | None:
  26. """Get a single setting value by key."""
  27. result = await db.execute(select(Settings).where(Settings.key == key))
  28. setting = result.scalar_one_or_none()
  29. return setting.value if setting else None
  30. async def set_setting(db: AsyncSession, key: str, value: str) -> None:
  31. """Set a single setting value."""
  32. result = await db.execute(select(Settings).where(Settings.key == key))
  33. setting = result.scalar_one_or_none()
  34. if setting:
  35. setting.value = value
  36. else:
  37. setting = Settings(key=key, value=value)
  38. db.add(setting)
  39. @router.get("/", response_model=AppSettings)
  40. async def get_settings(db: AsyncSession = Depends(get_db)):
  41. """Get all application settings."""
  42. settings_dict = DEFAULT_SETTINGS.model_dump()
  43. # Load saved settings from database
  44. result = await db.execute(select(Settings))
  45. db_settings = result.scalars().all()
  46. for setting in db_settings:
  47. if setting.key in settings_dict:
  48. # Parse the value based on the expected type
  49. if setting.key in ["auto_archive", "save_thumbnails", "capture_finish_photo", "spoolman_enabled", "check_updates"]:
  50. settings_dict[setting.key] = setting.value.lower() == "true"
  51. elif setting.key in ["default_filament_cost", "energy_cost_per_kwh", "ams_temp_good", "ams_temp_fair"]:
  52. settings_dict[setting.key] = float(setting.value)
  53. elif setting.key in ["ams_humidity_good", "ams_humidity_fair"]:
  54. settings_dict[setting.key] = int(setting.value)
  55. elif setting.key == "default_printer_id":
  56. # Handle nullable integer
  57. settings_dict[setting.key] = int(setting.value) if setting.value and setting.value != "None" else None
  58. else:
  59. settings_dict[setting.key] = setting.value
  60. return AppSettings(**settings_dict)
  61. @router.put("/", response_model=AppSettings)
  62. async def update_settings(
  63. settings_update: AppSettingsUpdate,
  64. db: AsyncSession = Depends(get_db),
  65. ):
  66. """Update application settings."""
  67. update_data = settings_update.model_dump(exclude_unset=True)
  68. for key, value in update_data.items():
  69. # Convert value to string for storage
  70. if isinstance(value, bool):
  71. str_value = "true" if value else "false"
  72. elif value is None:
  73. str_value = "None"
  74. else:
  75. str_value = str(value)
  76. await set_setting(db, key, str_value)
  77. await db.commit()
  78. # Return updated settings
  79. return await get_settings(db)
  80. @router.post("/reset", response_model=AppSettings)
  81. async def reset_settings(db: AsyncSession = Depends(get_db)):
  82. """Reset all settings to defaults."""
  83. # Delete all settings
  84. result = await db.execute(select(Settings))
  85. for setting in result.scalars().all():
  86. await db.delete(setting)
  87. await db.commit()
  88. return DEFAULT_SETTINGS
  89. @router.get("/check-ffmpeg")
  90. async def check_ffmpeg():
  91. """Check if ffmpeg is installed and available."""
  92. from backend.app.services.camera import get_ffmpeg_path
  93. ffmpeg_path = get_ffmpeg_path()
  94. return {
  95. "installed": ffmpeg_path is not None,
  96. "path": ffmpeg_path,
  97. }
  98. @router.get("/spoolman")
  99. async def get_spoolman_settings(db: AsyncSession = Depends(get_db)):
  100. """Get Spoolman integration settings."""
  101. spoolman_enabled = await get_setting(db, "spoolman_enabled") or "false"
  102. spoolman_url = await get_setting(db, "spoolman_url") or ""
  103. spoolman_sync_mode = await get_setting(db, "spoolman_sync_mode") or "auto"
  104. return {
  105. "spoolman_enabled": spoolman_enabled,
  106. "spoolman_url": spoolman_url,
  107. "spoolman_sync_mode": spoolman_sync_mode,
  108. }
  109. @router.put("/spoolman")
  110. async def update_spoolman_settings(
  111. settings: dict,
  112. db: AsyncSession = Depends(get_db),
  113. ):
  114. """Update Spoolman integration settings."""
  115. if "spoolman_enabled" in settings:
  116. await set_setting(db, "spoolman_enabled", settings["spoolman_enabled"])
  117. if "spoolman_url" in settings:
  118. await set_setting(db, "spoolman_url", settings["spoolman_url"])
  119. if "spoolman_sync_mode" in settings:
  120. await set_setting(db, "spoolman_sync_mode", settings["spoolman_sync_mode"])
  121. await db.commit()
  122. # Return updated settings
  123. return await get_spoolman_settings(db)
  124. @router.get("/backup")
  125. async def export_backup(
  126. db: AsyncSession = Depends(get_db),
  127. include_settings: bool = Query(True, description="Include app settings"),
  128. include_notifications: bool = Query(True, description="Include notification providers"),
  129. include_templates: bool = Query(True, description="Include notification templates"),
  130. include_smart_plugs: bool = Query(True, description="Include smart plugs"),
  131. include_printers: bool = Query(False, description="Include printers (without access codes)"),
  132. include_filaments: bool = Query(False, description="Include filament inventory"),
  133. include_maintenance: bool = Query(False, description="Include maintenance types and records"),
  134. include_archives: bool = Query(False, description="Include print archive metadata"),
  135. ):
  136. """Export selected data as JSON backup."""
  137. backup: dict = {
  138. "version": "2.0",
  139. "exported_at": datetime.utcnow().isoformat(),
  140. "included": [],
  141. }
  142. # Settings
  143. if include_settings:
  144. result = await db.execute(select(Settings))
  145. db_settings = result.scalars().all()
  146. backup["settings"] = {s.key: s.value for s in db_settings}
  147. backup["included"].append("settings")
  148. # Notification providers
  149. if include_notifications:
  150. result = await db.execute(select(NotificationProvider))
  151. providers = result.scalars().all()
  152. backup["notification_providers"] = []
  153. for p in providers:
  154. backup["notification_providers"].append({
  155. "name": p.name,
  156. "provider_type": p.provider_type,
  157. "enabled": p.enabled,
  158. "config": json.loads(p.config) if isinstance(p.config, str) else p.config,
  159. "on_print_start": p.on_print_start,
  160. "on_print_complete": p.on_print_complete,
  161. "on_print_failed": p.on_print_failed,
  162. "on_print_stopped": p.on_print_stopped,
  163. "on_print_progress": p.on_print_progress,
  164. "on_printer_offline": p.on_printer_offline,
  165. "on_printer_error": p.on_printer_error,
  166. "on_filament_low": p.on_filament_low,
  167. "on_maintenance_due": p.on_maintenance_due,
  168. "quiet_hours_enabled": p.quiet_hours_enabled,
  169. "quiet_hours_start": p.quiet_hours_start,
  170. "quiet_hours_end": p.quiet_hours_end,
  171. "daily_digest_enabled": getattr(p, 'daily_digest_enabled', False),
  172. "daily_digest_time": getattr(p, 'daily_digest_time', None),
  173. "printer_id": getattr(p, 'printer_id', None),
  174. })
  175. backup["included"].append("notification_providers")
  176. # Notification templates
  177. if include_templates:
  178. result = await db.execute(select(NotificationTemplate))
  179. templates = result.scalars().all()
  180. backup["notification_templates"] = []
  181. for t in templates:
  182. backup["notification_templates"].append({
  183. "event_type": t.event_type,
  184. "name": t.name,
  185. "title_template": t.title_template,
  186. "body_template": t.body_template,
  187. "is_default": t.is_default,
  188. })
  189. backup["included"].append("notification_templates")
  190. # Smart plugs
  191. if include_smart_plugs:
  192. result = await db.execute(select(SmartPlug))
  193. plugs = result.scalars().all()
  194. backup["smart_plugs"] = []
  195. for plug in plugs:
  196. backup["smart_plugs"].append({
  197. "name": plug.name,
  198. "ip_address": plug.ip_address,
  199. "printer_id": plug.printer_id,
  200. "enabled": plug.enabled,
  201. "auto_on": plug.auto_on,
  202. "auto_off": plug.auto_off,
  203. "off_delay_mode": plug.off_delay_mode,
  204. "off_delay_minutes": plug.off_delay_minutes,
  205. "off_temp_threshold": plug.off_temp_threshold,
  206. "username": plug.username,
  207. "password": plug.password,
  208. "power_alert_enabled": plug.power_alert_enabled,
  209. "power_alert_high": plug.power_alert_high,
  210. "power_alert_low": plug.power_alert_low,
  211. "schedule_enabled": plug.schedule_enabled,
  212. "schedule_on_time": plug.schedule_on_time,
  213. "schedule_off_time": plug.schedule_off_time,
  214. })
  215. backup["included"].append("smart_plugs")
  216. # Printers (without access codes for security)
  217. if include_printers:
  218. result = await db.execute(select(Printer))
  219. printers = result.scalars().all()
  220. backup["printers"] = []
  221. for printer in printers:
  222. backup["printers"].append({
  223. "name": printer.name,
  224. "serial_number": printer.serial_number,
  225. "ip_address": printer.ip_address,
  226. # access_code intentionally excluded for security
  227. "model": printer.model,
  228. "location": printer.location,
  229. "nozzle_count": printer.nozzle_count,
  230. "is_active": printer.is_active,
  231. "auto_archive": printer.auto_archive,
  232. "print_hours_offset": printer.print_hours_offset,
  233. })
  234. backup["included"].append("printers")
  235. # Filaments
  236. if include_filaments:
  237. result = await db.execute(select(Filament))
  238. filaments = result.scalars().all()
  239. backup["filaments"] = []
  240. for f in filaments:
  241. backup["filaments"].append({
  242. "name": f.name,
  243. "type": f.type,
  244. "brand": f.brand,
  245. "color": f.color,
  246. "color_hex": f.color_hex,
  247. "cost_per_kg": f.cost_per_kg,
  248. "spool_weight_g": f.spool_weight_g,
  249. "currency": f.currency,
  250. "density": f.density,
  251. "print_temp_min": f.print_temp_min,
  252. "print_temp_max": f.print_temp_max,
  253. "bed_temp_min": f.bed_temp_min,
  254. "bed_temp_max": f.bed_temp_max,
  255. })
  256. backup["included"].append("filaments")
  257. # Maintenance types and records
  258. if include_maintenance:
  259. # Maintenance types
  260. result = await db.execute(select(MaintenanceType))
  261. types = result.scalars().all()
  262. backup["maintenance_types"] = []
  263. for mt in types:
  264. backup["maintenance_types"].append({
  265. "name": mt.name,
  266. "description": mt.description,
  267. "default_interval_hours": mt.default_interval_hours,
  268. "interval_type": mt.interval_type,
  269. "icon": mt.icon,
  270. "is_system": mt.is_system,
  271. })
  272. backup["included"].append("maintenance_types")
  273. # Print archives with file paths for ZIP
  274. archive_files: list[tuple[str, Path]] = [] # (zip_path, local_path)
  275. if include_archives:
  276. result = await db.execute(select(PrintArchive))
  277. archives = result.scalars().all()
  278. backup["archives"] = []
  279. base_dir = app_settings.base_dir
  280. for a in archives:
  281. archive_data = {
  282. "filename": a.filename,
  283. "file_size": a.file_size,
  284. "content_hash": a.content_hash,
  285. "print_name": a.print_name,
  286. "print_time_seconds": a.print_time_seconds,
  287. "filament_used_grams": a.filament_used_grams,
  288. "filament_type": a.filament_type,
  289. "filament_color": a.filament_color,
  290. "layer_height": a.layer_height,
  291. "total_layers": a.total_layers,
  292. "nozzle_diameter": a.nozzle_diameter,
  293. "bed_temperature": a.bed_temperature,
  294. "nozzle_temperature": a.nozzle_temperature,
  295. "status": a.status,
  296. "started_at": a.started_at.isoformat() if a.started_at else None,
  297. "completed_at": a.completed_at.isoformat() if a.completed_at else None,
  298. "makerworld_url": a.makerworld_url,
  299. "designer": a.designer,
  300. "is_favorite": a.is_favorite,
  301. "tags": a.tags,
  302. "notes": a.notes,
  303. "cost": a.cost,
  304. "failure_reason": a.failure_reason,
  305. "energy_kwh": a.energy_kwh,
  306. "energy_cost": a.energy_cost,
  307. "extra_data": a.extra_data,
  308. "photos": a.photos,
  309. }
  310. # Collect file paths for ZIP
  311. if a.file_path:
  312. file_path = base_dir / a.file_path
  313. if file_path.exists():
  314. archive_data["file_path"] = a.file_path
  315. archive_files.append((a.file_path, file_path))
  316. if a.thumbnail_path:
  317. thumb_path = base_dir / a.thumbnail_path
  318. if thumb_path.exists():
  319. archive_data["thumbnail_path"] = a.thumbnail_path
  320. archive_files.append((a.thumbnail_path, thumb_path))
  321. if a.timelapse_path:
  322. timelapse_path = base_dir / a.timelapse_path
  323. if timelapse_path.exists():
  324. archive_data["timelapse_path"] = a.timelapse_path
  325. archive_files.append((a.timelapse_path, timelapse_path))
  326. if a.source_3mf_path:
  327. source_path = base_dir / a.source_3mf_path
  328. if source_path.exists():
  329. archive_data["source_3mf_path"] = a.source_3mf_path
  330. archive_files.append((a.source_3mf_path, source_path))
  331. # Include photos
  332. if a.photos:
  333. for photo in a.photos:
  334. photo_path = base_dir / "archive" / "photos" / photo
  335. if photo_path.exists():
  336. zip_photo_path = f"archive/photos/{photo}"
  337. archive_files.append((zip_photo_path, photo_path))
  338. backup["archives"].append(archive_data)
  339. backup["included"].append("archives")
  340. # If archives included, create ZIP file with all files
  341. if include_archives and archive_files:
  342. zip_buffer = io.BytesIO()
  343. with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zf:
  344. # Add backup.json
  345. zf.writestr("backup.json", json.dumps(backup, indent=2))
  346. # Add all archive files
  347. added_files = set()
  348. for zip_path, local_path in archive_files:
  349. if zip_path not in added_files and local_path.exists():
  350. try:
  351. zf.write(local_path, zip_path)
  352. added_files.add(zip_path)
  353. except Exception:
  354. pass # Skip files that can't be read
  355. zip_buffer.seek(0)
  356. filename = f"bambuddy-backup-{datetime.now().strftime('%Y%m%d-%H%M%S')}.zip"
  357. return StreamingResponse(
  358. zip_buffer,
  359. media_type="application/zip",
  360. headers={"Content-Disposition": f"attachment; filename={filename}"}
  361. )
  362. # Otherwise return JSON
  363. return JSONResponse(
  364. content=backup,
  365. headers={
  366. "Content-Disposition": f"attachment; filename=bambuddy-backup-{datetime.now().strftime('%Y%m%d-%H%M%S')}.json"
  367. }
  368. )
  369. @router.post("/restore")
  370. async def import_backup(
  371. file: UploadFile = File(...),
  372. overwrite: bool = Query(False, description="Overwrite existing data instead of skipping duplicates"),
  373. db: AsyncSession = Depends(get_db),
  374. ):
  375. """Restore data from JSON or ZIP backup. By default skips duplicates, set overwrite=true to replace existing."""
  376. try:
  377. content = await file.read()
  378. base_dir = app_settings.base_dir
  379. files_restored = 0
  380. # Check if it's a ZIP file
  381. if file.filename and file.filename.endswith('.zip'):
  382. try:
  383. zip_buffer = io.BytesIO(content)
  384. with zipfile.ZipFile(zip_buffer, 'r') as zf:
  385. # Extract backup.json
  386. if 'backup.json' not in zf.namelist():
  387. return {"success": False, "message": "Invalid ZIP: missing backup.json"}
  388. backup_content = zf.read('backup.json')
  389. backup = json.loads(backup_content.decode("utf-8"))
  390. # Extract all other files to base_dir
  391. for zip_path in zf.namelist():
  392. if zip_path == 'backup.json':
  393. continue
  394. # Ensure path is safe (no path traversal)
  395. if '..' in zip_path or zip_path.startswith('/'):
  396. continue
  397. target_path = base_dir / zip_path
  398. target_path.parent.mkdir(parents=True, exist_ok=True)
  399. with zf.open(zip_path) as src, open(target_path, 'wb') as dst:
  400. dst.write(src.read())
  401. files_restored += 1
  402. except zipfile.BadZipFile:
  403. return {"success": False, "message": "Invalid ZIP file"}
  404. else:
  405. backup = json.loads(content.decode("utf-8"))
  406. except json.JSONDecodeError as e:
  407. return {"success": False, "message": f"Invalid JSON: {str(e)}"}
  408. except Exception as e:
  409. return {"success": False, "message": f"Invalid backup file: {str(e)}"}
  410. restored = {
  411. "settings": 0,
  412. "notification_providers": 0,
  413. "notification_templates": 0,
  414. "smart_plugs": 0,
  415. "printers": 0,
  416. "filaments": 0,
  417. "maintenance_types": 0,
  418. }
  419. skipped = {
  420. "settings": 0,
  421. "notification_providers": 0,
  422. "notification_templates": 0,
  423. "smart_plugs": 0,
  424. "printers": 0,
  425. "filaments": 0,
  426. "maintenance_types": 0,
  427. "archives": 0,
  428. }
  429. skipped_details = {
  430. "notification_providers": [],
  431. "smart_plugs": [],
  432. "printers": [],
  433. "filaments": [],
  434. "maintenance_types": [],
  435. "archives": [],
  436. }
  437. # Restore settings (always overwrites)
  438. if "settings" in backup:
  439. for key, value in backup["settings"].items():
  440. await set_setting(db, key, value)
  441. restored["settings"] += 1
  442. # Restore notification providers (skip or overwrite duplicates by name)
  443. if "notification_providers" in backup:
  444. for provider_data in backup["notification_providers"]:
  445. result = await db.execute(
  446. select(NotificationProvider).where(NotificationProvider.name == provider_data["name"])
  447. )
  448. existing = result.scalar_one_or_none()
  449. if existing:
  450. if overwrite:
  451. # Update existing provider
  452. existing.provider_type = provider_data["provider_type"]
  453. existing.enabled = provider_data.get("enabled", True)
  454. existing.config = json.dumps(provider_data.get("config", {}))
  455. existing.on_print_start = provider_data.get("on_print_start", False)
  456. existing.on_print_complete = provider_data.get("on_print_complete", True)
  457. existing.on_print_failed = provider_data.get("on_print_failed", True)
  458. existing.on_print_stopped = provider_data.get("on_print_stopped", True)
  459. existing.on_print_progress = provider_data.get("on_print_progress", False)
  460. existing.on_printer_offline = provider_data.get("on_printer_offline", False)
  461. existing.on_printer_error = provider_data.get("on_printer_error", False)
  462. existing.on_filament_low = provider_data.get("on_filament_low", False)
  463. existing.on_maintenance_due = provider_data.get("on_maintenance_due", False)
  464. existing.quiet_hours_enabled = provider_data.get("quiet_hours_enabled", False)
  465. existing.quiet_hours_start = provider_data.get("quiet_hours_start")
  466. existing.quiet_hours_end = provider_data.get("quiet_hours_end")
  467. existing.daily_digest_enabled = provider_data.get("daily_digest_enabled", False)
  468. existing.daily_digest_time = provider_data.get("daily_digest_time")
  469. existing.printer_id = provider_data.get("printer_id")
  470. restored["notification_providers"] += 1
  471. else:
  472. skipped["notification_providers"] += 1
  473. skipped_details["notification_providers"].append(provider_data["name"])
  474. else:
  475. provider = NotificationProvider(
  476. name=provider_data["name"],
  477. provider_type=provider_data["provider_type"],
  478. enabled=provider_data.get("enabled", True),
  479. config=json.dumps(provider_data.get("config", {})),
  480. on_print_start=provider_data.get("on_print_start", False),
  481. on_print_complete=provider_data.get("on_print_complete", True),
  482. on_print_failed=provider_data.get("on_print_failed", True),
  483. on_print_stopped=provider_data.get("on_print_stopped", True),
  484. on_print_progress=provider_data.get("on_print_progress", False),
  485. on_printer_offline=provider_data.get("on_printer_offline", False),
  486. on_printer_error=provider_data.get("on_printer_error", False),
  487. on_filament_low=provider_data.get("on_filament_low", False),
  488. on_maintenance_due=provider_data.get("on_maintenance_due", False),
  489. quiet_hours_enabled=provider_data.get("quiet_hours_enabled", False),
  490. quiet_hours_start=provider_data.get("quiet_hours_start"),
  491. quiet_hours_end=provider_data.get("quiet_hours_end"),
  492. daily_digest_enabled=provider_data.get("daily_digest_enabled", False),
  493. daily_digest_time=provider_data.get("daily_digest_time"),
  494. printer_id=provider_data.get("printer_id"),
  495. )
  496. db.add(provider)
  497. restored["notification_providers"] += 1
  498. # Restore notification templates (update existing by event_type)
  499. if "notification_templates" in backup:
  500. for template_data in backup["notification_templates"]:
  501. result = await db.execute(
  502. select(NotificationTemplate).where(
  503. NotificationTemplate.event_type == template_data["event_type"]
  504. )
  505. )
  506. existing = result.scalar_one_or_none()
  507. if existing:
  508. # Update existing template
  509. existing.name = template_data.get("name", existing.name)
  510. existing.title_template = template_data.get("title_template", existing.title_template)
  511. existing.body_template = template_data.get("body_template", existing.body_template)
  512. existing.is_default = template_data.get("is_default", False)
  513. else:
  514. template = NotificationTemplate(
  515. event_type=template_data["event_type"],
  516. name=template_data["name"],
  517. title_template=template_data["title_template"],
  518. body_template=template_data["body_template"],
  519. is_default=template_data.get("is_default", False),
  520. )
  521. db.add(template)
  522. restored["notification_templates"] += 1
  523. # Restore smart plugs (skip or overwrite duplicates by IP)
  524. if "smart_plugs" in backup:
  525. for plug_data in backup["smart_plugs"]:
  526. result = await db.execute(
  527. select(SmartPlug).where(SmartPlug.ip_address == plug_data["ip_address"])
  528. )
  529. existing = result.scalar_one_or_none()
  530. if existing:
  531. if overwrite:
  532. existing.name = plug_data["name"]
  533. existing.printer_id = plug_data.get("printer_id")
  534. existing.enabled = plug_data.get("enabled", True)
  535. existing.auto_on = plug_data.get("auto_on", True)
  536. existing.auto_off = plug_data.get("auto_off", True)
  537. existing.off_delay_mode = plug_data.get("off_delay_mode", "time")
  538. existing.off_delay_minutes = plug_data.get("off_delay_minutes", 5)
  539. existing.off_temp_threshold = plug_data.get("off_temp_threshold", 70)
  540. existing.username = plug_data.get("username")
  541. existing.password = plug_data.get("password")
  542. existing.power_alert_enabled = plug_data.get("power_alert_enabled", False)
  543. existing.power_alert_high = plug_data.get("power_alert_high")
  544. existing.power_alert_low = plug_data.get("power_alert_low")
  545. existing.schedule_enabled = plug_data.get("schedule_enabled", False)
  546. existing.schedule_on_time = plug_data.get("schedule_on_time")
  547. existing.schedule_off_time = plug_data.get("schedule_off_time")
  548. restored["smart_plugs"] += 1
  549. else:
  550. skipped["smart_plugs"] += 1
  551. skipped_details["smart_plugs"].append(f"{plug_data['name']} ({plug_data['ip_address']})")
  552. else:
  553. plug = SmartPlug(
  554. name=plug_data["name"],
  555. ip_address=plug_data["ip_address"],
  556. printer_id=plug_data.get("printer_id"),
  557. enabled=plug_data.get("enabled", True),
  558. auto_on=plug_data.get("auto_on", True),
  559. auto_off=plug_data.get("auto_off", True),
  560. off_delay_mode=plug_data.get("off_delay_mode", "time"),
  561. off_delay_minutes=plug_data.get("off_delay_minutes", 5),
  562. off_temp_threshold=plug_data.get("off_temp_threshold", 70),
  563. username=plug_data.get("username"),
  564. password=plug_data.get("password"),
  565. power_alert_enabled=plug_data.get("power_alert_enabled", False),
  566. power_alert_high=plug_data.get("power_alert_high"),
  567. power_alert_low=plug_data.get("power_alert_low"),
  568. schedule_enabled=plug_data.get("schedule_enabled", False),
  569. schedule_on_time=plug_data.get("schedule_on_time"),
  570. schedule_off_time=plug_data.get("schedule_off_time"),
  571. )
  572. db.add(plug)
  573. restored["smart_plugs"] += 1
  574. # Restore printers (skip or overwrite duplicates by serial_number)
  575. # Note: access_code is never restored for security - must be set manually
  576. if "printers" in backup:
  577. for printer_data in backup["printers"]:
  578. result = await db.execute(
  579. select(Printer).where(Printer.serial_number == printer_data["serial_number"])
  580. )
  581. existing = result.scalar_one_or_none()
  582. if existing:
  583. if overwrite:
  584. existing.name = printer_data["name"]
  585. existing.ip_address = printer_data["ip_address"]
  586. existing.model = printer_data.get("model")
  587. existing.location = printer_data.get("location")
  588. existing.nozzle_count = printer_data.get("nozzle_count", 1)
  589. existing.auto_archive = printer_data.get("auto_archive", True)
  590. existing.print_hours_offset = printer_data.get("print_hours_offset", 0.0)
  591. # Don't overwrite access_code or is_active to preserve working connection
  592. restored["printers"] += 1
  593. else:
  594. skipped["printers"] += 1
  595. skipped_details["printers"].append(f"{printer_data['name']} ({printer_data['serial_number']})")
  596. else:
  597. printer = Printer(
  598. name=printer_data["name"],
  599. serial_number=printer_data["serial_number"],
  600. ip_address=printer_data["ip_address"],
  601. access_code="CHANGE_ME", # Must be set manually for security
  602. model=printer_data.get("model"),
  603. location=printer_data.get("location"),
  604. nozzle_count=printer_data.get("nozzle_count", 1),
  605. is_active=False, # Disabled until access_code is set
  606. auto_archive=printer_data.get("auto_archive", True),
  607. print_hours_offset=printer_data.get("print_hours_offset", 0.0),
  608. )
  609. db.add(printer)
  610. restored["printers"] += 1
  611. # Restore filaments (skip or overwrite duplicates by name+type+brand)
  612. if "filaments" in backup:
  613. for filament_data in backup["filaments"]:
  614. result = await db.execute(
  615. select(Filament).where(
  616. Filament.name == filament_data["name"],
  617. Filament.type == filament_data["type"],
  618. Filament.brand == filament_data.get("brand"),
  619. )
  620. )
  621. existing = result.scalar_one_or_none()
  622. if existing:
  623. if overwrite:
  624. existing.color = filament_data.get("color")
  625. existing.color_hex = filament_data.get("color_hex")
  626. existing.cost_per_kg = filament_data.get("cost_per_kg", 25.0)
  627. existing.spool_weight_g = filament_data.get("spool_weight_g", 1000.0)
  628. existing.currency = filament_data.get("currency", "USD")
  629. existing.density = filament_data.get("density")
  630. existing.print_temp_min = filament_data.get("print_temp_min")
  631. existing.print_temp_max = filament_data.get("print_temp_max")
  632. existing.bed_temp_min = filament_data.get("bed_temp_min")
  633. existing.bed_temp_max = filament_data.get("bed_temp_max")
  634. restored["filaments"] += 1
  635. else:
  636. skipped["filaments"] += 1
  637. skipped_details["filaments"].append(f"{filament_data.get('brand', '')} {filament_data['name']} ({filament_data['type']})")
  638. else:
  639. filament = Filament(
  640. name=filament_data["name"],
  641. type=filament_data["type"],
  642. brand=filament_data.get("brand"),
  643. color=filament_data.get("color"),
  644. color_hex=filament_data.get("color_hex"),
  645. cost_per_kg=filament_data.get("cost_per_kg", 25.0),
  646. spool_weight_g=filament_data.get("spool_weight_g", 1000.0),
  647. currency=filament_data.get("currency", "USD"),
  648. density=filament_data.get("density"),
  649. print_temp_min=filament_data.get("print_temp_min"),
  650. print_temp_max=filament_data.get("print_temp_max"),
  651. bed_temp_min=filament_data.get("bed_temp_min"),
  652. bed_temp_max=filament_data.get("bed_temp_max"),
  653. )
  654. db.add(filament)
  655. restored["filaments"] += 1
  656. # Restore maintenance types (skip or overwrite duplicates by name)
  657. if "maintenance_types" in backup:
  658. for mt_data in backup["maintenance_types"]:
  659. result = await db.execute(
  660. select(MaintenanceType).where(MaintenanceType.name == mt_data["name"])
  661. )
  662. existing = result.scalar_one_or_none()
  663. if existing:
  664. if overwrite:
  665. existing.description = mt_data.get("description")
  666. existing.default_interval_hours = mt_data.get("default_interval_hours", 100.0)
  667. existing.interval_type = mt_data.get("interval_type", "hours")
  668. existing.icon = mt_data.get("icon")
  669. # Don't overwrite is_system
  670. restored["maintenance_types"] += 1
  671. else:
  672. skipped["maintenance_types"] += 1
  673. skipped_details["maintenance_types"].append(mt_data["name"])
  674. else:
  675. mt = MaintenanceType(
  676. name=mt_data["name"],
  677. description=mt_data.get("description"),
  678. default_interval_hours=mt_data.get("default_interval_hours", 100.0),
  679. interval_type=mt_data.get("interval_type", "hours"),
  680. icon=mt_data.get("icon"),
  681. is_system=mt_data.get("is_system", False),
  682. )
  683. db.add(mt)
  684. restored["maintenance_types"] += 1
  685. # Restore archives (skip duplicates by content_hash - overwrite not supported for archives)
  686. if "archives" in backup:
  687. for archive_data in backup["archives"]:
  688. # Skip if no content_hash or already exists
  689. content_hash = archive_data.get("content_hash")
  690. if content_hash:
  691. result = await db.execute(
  692. select(PrintArchive).where(PrintArchive.content_hash == content_hash)
  693. )
  694. existing = result.scalar_one_or_none()
  695. if existing:
  696. skipped["archives"] += 1
  697. skipped_details["archives"].append(archive_data.get("filename", "Unknown"))
  698. continue
  699. # Only restore if file exists (from ZIP extraction)
  700. file_path = archive_data.get("file_path")
  701. if file_path and (base_dir / file_path).exists():
  702. archive = PrintArchive(
  703. filename=archive_data["filename"],
  704. file_path=file_path,
  705. file_size=archive_data.get("file_size", 0),
  706. content_hash=content_hash,
  707. thumbnail_path=archive_data.get("thumbnail_path"),
  708. timelapse_path=archive_data.get("timelapse_path"),
  709. source_3mf_path=archive_data.get("source_3mf_path"),
  710. print_name=archive_data.get("print_name"),
  711. print_time_seconds=archive_data.get("print_time_seconds"),
  712. filament_used_grams=archive_data.get("filament_used_grams"),
  713. filament_type=archive_data.get("filament_type"),
  714. filament_color=archive_data.get("filament_color"),
  715. layer_height=archive_data.get("layer_height"),
  716. total_layers=archive_data.get("total_layers"),
  717. nozzle_diameter=archive_data.get("nozzle_diameter"),
  718. bed_temperature=archive_data.get("bed_temperature"),
  719. nozzle_temperature=archive_data.get("nozzle_temperature"),
  720. status=archive_data.get("status", "completed"),
  721. makerworld_url=archive_data.get("makerworld_url"),
  722. designer=archive_data.get("designer"),
  723. is_favorite=archive_data.get("is_favorite", False),
  724. tags=archive_data.get("tags"),
  725. notes=archive_data.get("notes"),
  726. cost=archive_data.get("cost"),
  727. failure_reason=archive_data.get("failure_reason"),
  728. energy_kwh=archive_data.get("energy_kwh"),
  729. energy_cost=archive_data.get("energy_cost"),
  730. extra_data=archive_data.get("extra_data"),
  731. photos=archive_data.get("photos"),
  732. )
  733. db.add(archive)
  734. restored["archives"] = restored.get("archives", 0) + 1
  735. await db.commit()
  736. # Build summary message
  737. restored_parts = []
  738. for key, count in restored.items():
  739. if count > 0:
  740. restored_parts.append(f"{count} {key.replace('_', ' ')}")
  741. if files_restored > 0:
  742. restored_parts.append(f"{files_restored} files")
  743. skipped_parts = []
  744. total_skipped = sum(skipped.values())
  745. for key, count in skipped.items():
  746. if count > 0:
  747. skipped_parts.append(f"{count} {key.replace('_', ' ')}")
  748. message_parts = []
  749. if restored_parts:
  750. message_parts.append(f"Restored: {', '.join(restored_parts)}")
  751. if skipped_parts:
  752. message_parts.append(f"Skipped (already exist): {', '.join(skipped_parts)}")
  753. return {
  754. "success": True,
  755. "message": ". ".join(message_parts) if message_parts else "Nothing to restore",
  756. "restored": restored,
  757. "skipped": skipped,
  758. "skipped_details": skipped_details,
  759. "files_restored": files_restored,
  760. "total_skipped": total_skipped,
  761. }