main.py 271 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744
  1. import asyncio
  2. import logging
  3. import mimetypes as _mimetypes
  4. import os
  5. import posixpath
  6. import secrets
  7. import time
  8. from contextlib import asynccontextmanager
  9. from datetime import datetime, timedelta, timezone
  10. from logging.handlers import RotatingFileHandler
  11. from urllib.parse import urlparse
  12. from fastapi import FastAPI
  13. from fastapi.responses import FileResponse
  14. from fastapi.staticfiles import StaticFiles
  15. from sqlalchemy import delete, or_, select, text
  16. from backend.app.api.routes import (
  17. ams_history,
  18. api_keys,
  19. archive_purge,
  20. archives,
  21. auth,
  22. background_dispatch as background_dispatch_routes,
  23. bug_report,
  24. camera,
  25. cloud,
  26. discovery,
  27. external_links,
  28. filaments,
  29. firmware,
  30. github_backup,
  31. groups,
  32. inventory,
  33. kprofiles,
  34. labels,
  35. library,
  36. library_trash,
  37. local_backup,
  38. local_presets,
  39. maintenance,
  40. makerworld,
  41. metrics,
  42. mfa,
  43. notification_templates,
  44. notifications,
  45. obico,
  46. pending_uploads,
  47. print_log,
  48. print_queue,
  49. printers,
  50. projects,
  51. settings as settings_routes,
  52. slice_jobs,
  53. slicer_presets,
  54. smart_plugs,
  55. spoolbuddy,
  56. spoolman,
  57. spoolman_inventory,
  58. support,
  59. system,
  60. updates,
  61. user_notifications,
  62. users,
  63. virtual_printers,
  64. webhook,
  65. websocket,
  66. )
  67. from backend.app.api.routes.maintenance import _get_printer_maintenance_internal, ensure_default_types
  68. from backend.app.api.routes.support import init_debug_logging
  69. from backend.app.core.config import APP_VERSION, settings as app_settings
  70. from backend.app.core.database import async_session, engine, init_db
  71. from backend.app.core.websocket import ws_manager
  72. from backend.app.models.smart_plug import SmartPlug
  73. from backend.app.services.archive import ArchiveService, peek_plate_index_in_3mf, swap_plate_suffix
  74. from backend.app.services.archive_purge import archive_purge_service
  75. from backend.app.services.background_dispatch import background_dispatch
  76. from backend.app.services.bambu_ftp import (
  77. FileNotOnPrinterError,
  78. cache_3mf_download,
  79. clear_3mf_cache,
  80. download_file_async,
  81. get_cached_3mf,
  82. get_ftp_retry_settings,
  83. with_ftp_retry,
  84. )
  85. from backend.app.services.bambu_mqtt import PrinterState
  86. from backend.app.services.github_backup import github_backup_service
  87. from backend.app.services.homeassistant import homeassistant_service
  88. from backend.app.services.library_trash import library_trash_service
  89. from backend.app.services.local_backup import local_backup_service
  90. from backend.app.services.mqtt_relay import mqtt_relay
  91. from backend.app.services.mqtt_smart_plug import mqtt_smart_plug_service
  92. from backend.app.services.notification_service import notification_service
  93. from backend.app.services.obico_detection import obico_detection_service
  94. from backend.app.services.print_scheduler import scheduler as print_scheduler
  95. from backend.app.services.printer_manager import (
  96. init_printer_connections,
  97. parse_plate_id,
  98. printer_manager,
  99. printer_state_to_dict,
  100. )
  101. from backend.app.services.smart_plug_manager import smart_plug_manager
  102. from backend.app.services.spool_assignment_notifications import (
  103. notify_missing_spool_assignments_on_print_start,
  104. )
  105. from backend.app.services.spoolman import close_spoolman_client, get_spoolman_client, init_spoolman_client
  106. from backend.app.services.spoolman_tracking import (
  107. cleanup_tracking as _cleanup_spoolman_tracking,
  108. report_usage as _report_spoolman_usage,
  109. store_print_data as _store_spoolman_print_data,
  110. )
  111. from backend.app.services.tasmota import tasmota_service
  112. # =============================================================================
  113. # Dependency Check - runs before other imports to give helpful error messages
  114. # =============================================================================
  115. def _start_error_server(missing_packages: list):
  116. """Start a minimal HTTP server to display dependency errors in browser."""
  117. import os
  118. import signal
  119. from http.server import BaseHTTPRequestHandler, HTTPServer
  120. packages_html = "".join(f"<li><code>{p}</code></li>" for p in missing_packages)
  121. html = f"""<!DOCTYPE html>
  122. <html>
  123. <head>
  124. <title>Bambuddy - Setup Required</title>
  125. <style>
  126. body {{
  127. font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  128. background: #0f172a; color: #e2e8f0;
  129. display: flex; justify-content: center; align-items: center;
  130. min-height: 100vh; margin: 0; padding: 20px; box-sizing: border-box;
  131. }}
  132. .container {{
  133. background: #1e293b; border-radius: 12px; padding: 40px;
  134. max-width: 600px; text-align: center; box-shadow: 0 4px 20px rgba(0,0,0,0.3);
  135. }}
  136. h1 {{ color: #f87171; margin-bottom: 10px; }}
  137. h2 {{ color: #94a3b8; font-weight: normal; margin-top: 0; }}
  138. .packages {{
  139. background: #0f172a; border-radius: 8px; padding: 20px;
  140. margin: 20px 0; text-align: left;
  141. }}
  142. .packages ul {{ margin: 0; padding-left: 20px; }}
  143. .packages li {{ color: #fbbf24; margin: 8px 0; }}
  144. .command {{
  145. background: #0f172a; border-radius: 8px; padding: 15px 20px;
  146. margin: 15px 0; font-family: monospace; color: #4ade80;
  147. text-align: left; overflow-x: auto;
  148. }}
  149. .note {{ color: #94a3b8; font-size: 14px; margin-top: 20px; }}
  150. </style>
  151. </head>
  152. <body>
  153. <div class="container">
  154. <h1>Setup Required</h1>
  155. <h2>Missing Python packages</h2>
  156. <div class="packages"><ul>{packages_html}</ul></div>
  157. <p>To fix, run this command on your server:</p>
  158. <div class="command">pip install -r requirements.txt</div>
  159. <p>Or if using a virtual environment:</p>
  160. <div class="command">./venv/bin/pip install -r requirements.txt</div>
  161. <p class="note">After installing, restart Bambuddy:<br>
  162. <code>sudo systemctl restart bambuddy</code></p>
  163. </div>
  164. </body>
  165. </html>"""
  166. class ErrorHandler(BaseHTTPRequestHandler):
  167. def do_GET(self):
  168. self.send_response(503)
  169. self.send_header("Content-type", "text/html")
  170. self.end_headers()
  171. self.wfile.write(html.encode())
  172. def log_message(self, format, *args):
  173. print(f"[Error Server] {args[0]}")
  174. port = int(os.environ.get("PORT", 8000))
  175. print(f"\nStarting error server on http://0.0.0.0:{port}")
  176. print("Visit this URL in your browser to see the error details.\n")
  177. server = HTTPServer(("0.0.0.0", port), ErrorHandler) # nosec B104
  178. def shutdown(signum, frame):
  179. print("\nShutting down error server...")
  180. raise SystemExit(0)
  181. signal.signal(signal.SIGTERM, shutdown)
  182. signal.signal(signal.SIGINT, shutdown)
  183. server.serve_forever()
  184. def check_dependencies():
  185. """Check that all required packages are installed."""
  186. missing = []
  187. # Map of import name -> package name (for pip install)
  188. required = {
  189. "jwt": "PyJWT",
  190. "fastapi": "fastapi",
  191. "uvicorn": "uvicorn",
  192. "sqlalchemy": "sqlalchemy",
  193. "aiosqlite": "aiosqlite",
  194. "pydantic": "pydantic",
  195. "paho.mqtt": "paho-mqtt",
  196. }
  197. for module, package in required.items():
  198. try:
  199. __import__(module)
  200. except ImportError:
  201. missing.append(package)
  202. if missing:
  203. print("\n" + "=" * 60)
  204. print("ERROR: Missing required Python packages!")
  205. print("=" * 60)
  206. print(f"\nMissing packages: {', '.join(missing)}")
  207. print("\nTo fix, run:")
  208. print(" pip install -r requirements.txt")
  209. print("\nOr if using a virtual environment:")
  210. print(" ./venv/bin/pip install -r requirements.txt")
  211. print("=" * 60 + "\n")
  212. _start_error_server(missing)
  213. check_dependencies()
  214. # =============================================================================
  215. # Import settings first for logging configuration
  216. # Configure logging based on settings
  217. # DEBUG=true -> DEBUG level, else use LOG_LEVEL setting
  218. log_level_str = "DEBUG" if app_settings.debug else app_settings.log_level.upper()
  219. log_level = getattr(logging, log_level_str, logging.INFO)
  220. # Trace ID column ([-] when no request scope is active — startup, MQTT
  221. # callbacks, scheduled tasks not chained from a request — so the column
  222. # stays visually aligned and missing values are obvious in grep). See
  223. # backend/app/core/trace.py for the ContextVar that feeds this slot.
  224. log_format = "%(asctime)s %(levelname)s [%(name)s] [%(trace_id)s] %(message)s"
  225. # Create root logger
  226. root_logger = logging.getLogger()
  227. root_logger.setLevel(log_level)
  228. # Trace-ID injection: this filter populates record.trace_id from the
  229. # per-request ContextVar so the format string above can reference it.
  230. # Attached to each HANDLER (not the root logger) because Python's
  231. # logging semantics only invoke a logger's filters on records that
  232. # *originated* at that logger — records propagated up from child
  233. # loggers (every named logger in the app) never trigger root's filter.
  234. # Putting it on the handlers means every record any handler emits gets
  235. # trace_id injected just before the formatter runs, regardless of which
  236. # logger created the record. Without this, the formatter raises
  237. # KeyError on every child-logger record and the record is silently
  238. # dropped — which is exactly the "logs/bambuddy.log only shows logs
  239. # partially" bug we hit. See backend/app/core/trace.py for the
  240. # ContextVar the filter reads.
  241. from backend.app.core.trace import TraceIDFilter
  242. _trace_id_filter = TraceIDFilter()
  243. # Console handler - always enabled
  244. console_handler = logging.StreamHandler()
  245. console_handler.setLevel(log_level)
  246. console_handler.setFormatter(logging.Formatter(log_format))
  247. console_handler.addFilter(_trace_id_filter)
  248. root_logger.addHandler(console_handler)
  249. # File handler - only in production or if explicitly enabled
  250. if app_settings.log_to_file:
  251. log_file = app_settings.log_dir / "bambuddy.log"
  252. file_handler = RotatingFileHandler(
  253. log_file,
  254. maxBytes=5 * 1024 * 1024, # 5MB
  255. backupCount=3,
  256. encoding="utf-8",
  257. )
  258. file_handler.setLevel(log_level)
  259. file_handler.setFormatter(logging.Formatter(log_format))
  260. file_handler.addFilter(_trace_id_filter)
  261. root_logger.addHandler(file_handler)
  262. logging.info("Logging to file: %s", log_file)
  263. # Pipe uvicorn's HTTP access log to bambuddy.log too. Uvicorn ships its
  264. # access logger with propagate=False by default, so without this attach
  265. # there is no on-disk record of which endpoint triggered a server-state
  266. # change — the rogue stop_print mystery on 2026-04-26 was untraceable
  267. # for exactly this reason. Filtered to write methods only
  268. # (POST/PUT/PATCH/DELETE) so the high-volume status-poll GETs from the
  269. # frontend don't churn the rotation window faster than it's useful.
  270. from backend.app.core.logging_filters import (
  271. CancelledPoolNoiseFilter,
  272. WriteRequestsOnlyFilter,
  273. )
  274. uvicorn_access_logger = logging.getLogger("uvicorn.access")
  275. uvicorn_access_logger.addHandler(file_handler)
  276. uvicorn_access_logger.addFilter(WriteRequestsOnlyFilter())
  277. # Uvicorn's access logger has propagate=False (its own default), so the
  278. # root-attached TraceIDFilter never sees these records. Attach a
  279. # second instance directly so HTTP access lines carry the same trace
  280. # ID column as the application logs they correlate with.
  281. uvicorn_access_logger.addFilter(TraceIDFilter())
  282. # Drop SQLAlchemy connection-pool log noise that's caused by Starlette's
  283. # BaseHTTPMiddleware cancelling the inner task scope on client
  284. # disconnect (#1112). The cancel-safe `get_db` already prevents the
  285. # underlying transaction leak; this filter only suppresses the residual
  286. # log records that pre-existing pools still emit during their cleanup.
  287. logging.getLogger("sqlalchemy.pool").addFilter(CancelledPoolNoiseFilter())
  288. # Reduce noise from third-party libraries in production
  289. if not app_settings.debug:
  290. logging.getLogger("sqlalchemy.engine").setLevel(logging.WARNING)
  291. logging.getLogger("httpcore").setLevel(logging.WARNING)
  292. logging.getLogger("httpx").setLevel(logging.WARNING)
  293. logging.getLogger("paho.mqtt").setLevel(logging.WARNING)
  294. logging.info("Bambuddy starting - debug=%s, log_level=%s", app_settings.debug, log_level_str)
  295. # Track active prints: {(printer_id, filename): archive_id}
  296. _active_prints: dict[tuple[int, str], int] = {}
  297. # Track expected prints from reprint/scheduled (skip auto-archiving for these)
  298. # {(printer_id, filename): archive_id}
  299. _expected_prints: dict[tuple[int, str], int] = {}
  300. # Track AMS mapping for prints: {archive_id: [global_tray_id_per_slot]}
  301. # Used by usage tracker to map 3MF slots to physical AMS trays
  302. _print_ams_mappings: dict[int, list[int]] = {}
  303. # Track progress milestones for notifications: {printer_id: last_milestone_notified}
  304. # Milestones are 25, 50, 75. Value of 0 means no milestone notified yet for current print.
  305. _last_progress_milestone: dict[int, int] = {}
  306. # Track whether first layer complete notification has been sent for current print
  307. _first_layer_notified: dict[int, bool] = {}
  308. # Track HMS errors that have been notified: {printer_id: set of error codes}
  309. # This prevents sending duplicate notifications for the same error
  310. _notified_hms_errors: dict[int, set[str]] = {}
  311. # Track when HMS errors were last seen: {printer_id: timestamp}
  312. # Used to debounce clearing — prevents flapping errors from re-triggering notifications
  313. _hms_last_seen: dict[int, float] = {}
  314. _HMS_CLEAR_GRACE_SECONDS = 30.0
  315. # Track timelapse file baselines at print start: {printer_id: set of video filenames}
  316. # Used for snapshot-diff detection at print completion
  317. _timelapse_baselines: dict[int, set[str]] = {}
  318. # Track printers waiting for bed to cool after print completion.
  319. # Event-driven: fires when bed_temper arrives via MQTT below threshold.
  320. # {printer_id: {"threshold": float, "filename": str, "registered_at": float}}
  321. _bed_cool_waiters: dict[int, dict] = {}
  322. # Track printers where the user explicitly stopped the print from the queue UI.
  323. # When on_print_complete fires with status "failed" for these printers we treat it
  324. # as "cancelled" (stopped by user) so the correct notification email is sent.
  325. _user_stopped_printers: set[int] = set()
  326. # HMS short-code → human-readable failure reason. Used by _dispatch_archive_update
  327. # when status="failed" to label the print's failure_reason in archives.
  328. #
  329. # Earlier code matched on `module` alone (e.g. "any module 0x0C HMS → Layer shift"),
  330. # which is wrong on two counts:
  331. # 1. Real layer-shift codes live in module 0x03 (see Bambu wiki), not 0x0C.
  332. # 2. Module 0x0C is "Motion Controller" — broad category that also covers cameras
  333. # and visual markers, AND the H2D firmware emits a 0x0C HMS (0C00_001B, not in
  334. # the public wiki) as part of its user-cancel sequence. Matching on the module
  335. # alone caused user-cancellations to be archived as "Layer shift" failures.
  336. # We now match by full short code only — anything not in this map leaves
  337. # failure_reason=None rather than guessing.
  338. _HMS_FAILURE_REASONS: dict[str, str] = {
  339. # Layer shift / step loss
  340. "0300_4057": "Layer shift",
  341. "0300_4068": "Layer shift",
  342. "0300_800C": "Layer shift",
  343. # Filament runout (printer-side & per-AMS-slot)
  344. "0300_8004": "Filament runout",
  345. "0700_8011": "Filament runout",
  346. "0701_8011": "Filament runout",
  347. "0702_8011": "Filament runout",
  348. "0703_8011": "Filament runout",
  349. "0704_8011": "Filament runout",
  350. "0705_8011": "Filament runout",
  351. "0706_8011": "Filament runout",
  352. "0707_8011": "Filament runout",
  353. "07FF_8011": "Filament runout",
  354. # Clogged nozzle / extruder
  355. "0300_4006": "Clogged nozzle",
  356. "0300_8016": "Clogged nozzle",
  357. "0300_801C": "Clogged nozzle",
  358. "0700_8003": "Clogged nozzle",
  359. "0700_8007": "Clogged nozzle",
  360. "0700_8013": "Clogged nozzle",
  361. "0701_8003": "Clogged nozzle",
  362. "0701_8007": "Clogged nozzle",
  363. "0701_8013": "Clogged nozzle",
  364. "0702_8003": "Clogged nozzle",
  365. }
  366. def _hms_short_code(attr: int, code: int | str) -> str:
  367. """Build the canonical "MMMM_CCCC" HMS short code from raw attr/code values."""
  368. if isinstance(code, str):
  369. code_int = int(code.replace("0x", ""), 16) if code else 0
  370. else:
  371. code_int = int(code or 0)
  372. attr_int = int(attr or 0)
  373. return f"{(attr_int >> 16) & 0xFFFF:04X}_{code_int & 0xFFFF:04X}"
  374. def derive_failure_reason(status: str, hms_errors: list[dict] | None) -> str | None:
  375. """Derive a human-readable failure_reason for an archived print.
  376. Returns "User cancelled" for cancelled/aborted prints; for failed prints,
  377. returns the first matching reason from _HMS_FAILURE_REASONS, or None when
  378. no HMS code matches (don't guess — null is honest).
  379. """
  380. if status in ("aborted", "cancelled"):
  381. return "User cancelled"
  382. if status != "failed":
  383. return None
  384. for err in hms_errors or []:
  385. short_code = _hms_short_code(err.get("attr", 0), err.get("code", 0))
  386. if short_code in _HMS_FAILURE_REASONS:
  387. return _HMS_FAILURE_REASONS[short_code]
  388. return None
  389. # Track created_by_id for expected prints so the user email can be sent even when
  390. # the archive itself doesn't have created_by_id set (e.g. library-file-based prints).
  391. # {(printer_id, filename): created_by_id}
  392. _expected_print_creators: dict[tuple[int, str], int] = {}
  393. # Per-printer lock that serialises the spool-assignment side of on_ams_change
  394. # (auto-unlink stale + auto-assign new) when MQTT bursts deliver multiple AMS
  395. # updates for the same printer in quick succession (~30 ms apart, observed in
  396. # the wild on H2D + dual AMS).
  397. #
  398. # Without this serialisation, two concurrent on_ams_change callbacks each read
  399. # "no assignment for (printer, ams, tray)", each call auto_assign_spool, and
  400. # the second commit hits
  401. # IntegrityError: duplicate key value violates unique constraint
  402. # "spool_assignment_printer_id_ams_id_tray_id_key"
  403. # SQLite's WAL serial-write semantics had been silently swallowing the race
  404. # until optional Postgres support landed (asyncpg allows true concurrent
  405. # transactions and surfaces the constraint violation).
  406. #
  407. # Scope is intentionally narrow: only the two DB-mutating blocks (unlink +
  408. # assign) are inside the lock. The Spoolman sync block further down stays
  409. # concurrent because it's network-bound and idempotent.
  410. _ams_assignment_locks: dict[int, asyncio.Lock] = {}
  411. def _get_ams_assignment_lock(printer_id: int) -> asyncio.Lock:
  412. """Return the per-printer assignment lock, creating it on first use."""
  413. lock = _ams_assignment_locks.get(printer_id)
  414. if lock is None:
  415. lock = asyncio.Lock()
  416. _ams_assignment_locks[printer_id] = lock
  417. return lock
  418. # TTL for expected-print entries: evict registrations older than this to prevent
  419. # unbounded growth when a print is registered but never starts (e.g. printer
  420. # disconnect, app restart, print started from the printer panel).
  421. _EXPECTED_PRINT_TTL_SECONDS: int = 2 * 60 * 60 # 2 hours
  422. # Registration timestamps used for TTL eviction: {(printer_id, filename): monotonic_time}
  423. _expected_print_registered_at: dict[tuple[int, str], float] = {}
  424. # Cleanup loop interval
  425. _EXPECTED_PRINT_CLEANUP_INTERVAL: int = 15 * 60 # 15 minutes
  426. _expected_prints_cleanup_task: asyncio.Task | None = None
  427. async def _get_plug_energy(plug, db) -> dict | None:
  428. """Get energy from plug regardless of type (Tasmota, Home Assistant, MQTT, or REST).
  429. For HA plugs, configures the service with current settings from DB.
  430. For MQTT plugs, returns data from the subscription service.
  431. For REST plugs, polls the status URL with JSON path extraction.
  432. """
  433. if plug.plug_type == "homeassistant":
  434. from backend.app.api.routes.settings import get_homeassistant_settings
  435. ha_settings = await get_homeassistant_settings(db)
  436. homeassistant_service.configure(ha_settings["ha_url"], ha_settings["ha_token"])
  437. return await homeassistant_service.get_energy(plug)
  438. elif plug.plug_type == "mqtt":
  439. # MQTT plugs report "today" energy, not lifetime total
  440. # For per-print tracking, we use "today" as the counter (resets at midnight)
  441. mqtt_data = mqtt_relay.smart_plug_service.get_plug_data(plug.id)
  442. if mqtt_data:
  443. return {
  444. "power": mqtt_data.power,
  445. "today": mqtt_data.energy,
  446. "total": mqtt_data.energy, # Use today as total for per-print calculations
  447. }
  448. return None
  449. elif plug.plug_type == "rest":
  450. from backend.app.services.rest_smart_plug import rest_smart_plug_service
  451. return await rest_smart_plug_service.get_energy(plug)
  452. else:
  453. return await tasmota_service.get_energy(plug)
  454. async def _record_energy_start(archive, printer_id: int, db, *, context: str = "") -> bool:
  455. """Capture the smart plug lifetime counter on the archive at print start.
  456. Persists `energy_start_kwh` on the archive row (#941) so per-print energy
  457. tracking survives a backend restart mid-print. The print-end handler reads
  458. this value back from the DB and computes the delta against the current
  459. plug counter.
  460. """
  461. _logger = logging.getLogger(__name__)
  462. try:
  463. plug_result = await db.execute(select(SmartPlug).where(SmartPlug.printer_id == printer_id))
  464. plug = plug_result.scalar_one_or_none()
  465. if not plug:
  466. _logger.info("[ENERGY] No smart plug for printer %s (archive %s)", printer_id, archive.id)
  467. return False
  468. energy = await _get_plug_energy(plug, db)
  469. if not energy or energy.get("total") is None:
  470. _logger.warning("[ENERGY] No 'total' in energy response for archive %s", archive.id)
  471. return False
  472. archive.energy_start_kwh = float(energy["total"])
  473. await db.commit()
  474. _logger.info(
  475. "[ENERGY] Recorded starting energy%s for archive %s: %s kWh",
  476. f" ({context})" if context else "",
  477. archive.id,
  478. energy["total"],
  479. )
  480. return True
  481. except Exception as e:
  482. _logger.warning("[ENERGY] Failed to record starting energy for archive %s: %s", archive.id, e)
  483. return False
  484. def register_expected_print(
  485. printer_id: int,
  486. filename: str,
  487. archive_id: int,
  488. ams_mapping: list[int] | None = None,
  489. created_by_id: int | None = None,
  490. ):
  491. """Register an expected print from reprint/scheduled so we don't create duplicate archives."""
  492. # Store with multiple filename variations to catch different naming patterns
  493. _expected_prints[(printer_id, filename)] = archive_id
  494. # Also store without .3mf extension if present
  495. if filename.endswith(".3mf"):
  496. base = filename[:-4]
  497. _expected_prints[(printer_id, base)] = archive_id
  498. _expected_prints[(printer_id, f"{base}.gcode")] = archive_id
  499. # Store AMS mapping for usage tracking at print completion
  500. if ams_mapping is not None:
  501. _print_ams_mappings[archive_id] = ams_mapping
  502. # Store created_by_id so the user start email can be sent even when the archive
  503. # itself has no created_by_id (e.g. library-file-based queue prints)
  504. if created_by_id is not None:
  505. _expected_print_creators[(printer_id, filename)] = created_by_id
  506. if filename.endswith(".3mf"):
  507. base = filename[:-4]
  508. _expected_print_creators[(printer_id, base)] = created_by_id
  509. _expected_print_creators[(printer_id, f"{base}.gcode")] = created_by_id
  510. # Record registration time for TTL-based eviction
  511. _registered_at = time.monotonic()
  512. _expected_print_registered_at[(printer_id, filename)] = _registered_at
  513. if filename.endswith(".3mf"):
  514. base = filename[:-4]
  515. _expected_print_registered_at[(printer_id, base)] = _registered_at
  516. _expected_print_registered_at[(printer_id, f"{base}.gcode")] = _registered_at
  517. logging.getLogger(__name__).info(
  518. f"Registered expected print: printer={printer_id}, file={filename}, archive={archive_id}, ams_mapping={ams_mapping}"
  519. )
  520. def _compute_run_filament_grams(
  521. status: str,
  522. archive_filament_used_grams: float | None,
  523. progress: float | int | None,
  524. usage_results: list[dict] | None,
  525. ) -> float | None:
  526. """Per-run filament for PrintLogEntry, partial- and tracker-aware (#1378, #1390).
  527. Priority for every status:
  528. 1. Sum of tracked spool deltas in ``usage_results`` (AMS-measured
  529. weight delta — same source that drives "Total Consumed" on the
  530. Inventory page, so Stats and Inventory totals stay aligned).
  531. 2. For ``completed``: the slicer estimate (no tracker available, fall
  532. back to the canonical "this print used X" value).
  533. 3. For partial statuses: ``estimate * progress%``.
  534. 4. ``None`` if nothing is known.
  535. """
  536. tracked_grams = sum(r.get("weight_used") or 0 for r in (usage_results or []))
  537. if tracked_grams > 0:
  538. return round(tracked_grams, 1)
  539. if status == "completed":
  540. return archive_filament_used_grams
  541. if archive_filament_used_grams:
  542. scale = max(0.0, min(((progress or 0) / 100.0), 1.0))
  543. if scale > 0:
  544. return round(archive_filament_used_grams * scale, 1)
  545. return None
  546. def _get_start_ams_mapping(data: dict, archive_id: int | None) -> list[int] | None:
  547. """Resolve AMS mapping for print start without consuming stored queue/reprint state."""
  548. stored_ams_mapping = data.get("ams_mapping")
  549. if not stored_ams_mapping and archive_id:
  550. stored_ams_mapping = _print_ams_mappings.get(archive_id)
  551. return stored_ams_mapping
  552. def _extract_filament_data_from_mqtt(data: dict, ams_mapping: list[int] | None = None) -> dict[str, str]:
  553. """Best-effort filament metadata from the MQTT print-start snapshot.
  554. Used when the 3MF can't be downloaded (P1S/A1/P2S firmwares lock the
  555. file during print, see #1533) so the fallback PrintArchive still has
  556. enough filament info to support the inventory views and AMS-expansion
  557. planning the operator opens it for. Returns a dict with optional
  558. ``filament_type`` and ``filament_color`` keys in the same
  559. comma-separated format the 3MF extractor produces, so the rest of the
  560. codebase treats the fallback archive identically to a normal one.
  561. ``ams_mapping`` is the slicer's slot-per-print-filament list captured
  562. from the MQTT print payload (global tray IDs, possibly -1 for VT-tray
  563. entries). When supplied, only the slots actually consumed by this
  564. print contribute. Without it the function falls back to every loaded
  565. AMS slot — less accurate but still useful.
  566. """
  567. result: dict[str, str] = {}
  568. ams_root = (data or {}).get("ams") or {}
  569. ams_units = ams_root.get("ams") if isinstance(ams_root, dict) else None
  570. if not isinstance(ams_units, list) or not ams_units:
  571. return result
  572. # Map global tray id (unit * 4 + tray) → (type, color).
  573. loaded: dict[int, tuple[str, str]] = {}
  574. for unit in ams_units:
  575. if not isinstance(unit, dict):
  576. continue
  577. try:
  578. unit_id = int(unit.get("id", 0))
  579. except (TypeError, ValueError):
  580. continue
  581. for tray in unit.get("tray") or []:
  582. if not isinstance(tray, dict):
  583. continue
  584. try:
  585. tray_id = int(tray.get("id", 0))
  586. except (TypeError, ValueError):
  587. continue
  588. ttype = (tray.get("tray_type") or "").strip()
  589. tcolor = (tray.get("tray_color") or "").strip().upper()
  590. if not ttype:
  591. continue # Empty / unloaded slot.
  592. loaded[unit_id * 4 + tray_id] = (ttype, tcolor)
  593. if not loaded:
  594. return result
  595. if ams_mapping:
  596. used_ids = [int(x) for x in ams_mapping if isinstance(x, (int, float)) and int(x) >= 0]
  597. filaments = [loaded[g] for g in used_ids if g in loaded]
  598. if not filaments:
  599. return result # Mapping points entirely at slots we have no data for.
  600. else:
  601. filaments = [loaded[g] for g in sorted(loaded.keys())]
  602. types_joined = ",".join(f[0] for f in filaments)
  603. colors_joined = ",".join(f[1] for f in filaments if f[1])
  604. # Column limits per backend/app/models/archive.py: filament_type=50,
  605. # filament_color=200.
  606. if types_joined:
  607. result["filament_type"] = types_joined[:50]
  608. if colors_joined:
  609. result["filament_color"] = colors_joined[:200]
  610. return result
  611. def _maybe_start_layer_timelapse(printer, printer_id: int, archive_id: int) -> bool:
  612. """Start a layer-timelapse session for *archive_id* when the printer has
  613. an external camera configured. Returns True if a session was started.
  614. Three call sites in on_print_start (expected-archive promotion, fallback
  615. archive creation, fresh-archive creation) used to inline this same
  616. if-block; the inline copies kept drifting (#1353 fixed only one of them
  617. on the first pass). Centralising the conditional + call here makes the
  618. contract testable in isolation and keeps the three sites locked in step.
  619. """
  620. if not (printer.external_camera_enabled and printer.external_camera_url):
  621. return False
  622. from backend.app.services.layer_timelapse import start_session
  623. start_session(
  624. printer_id,
  625. archive_id,
  626. printer.external_camera_url,
  627. printer.external_camera_type or "mjpeg",
  628. snapshot_url=printer.external_camera_snapshot_url,
  629. )
  630. logging.getLogger(__name__).info("Started layer timelapse for printer %s, archive %s", printer_id, archive_id)
  631. return True
  632. def _format_hms_error_summary(hms_errors: list[dict]) -> str | None:
  633. """Build a human-readable failure reason from MQTT hms_errors for PrintQueueItem.error_message.
  634. Each entry has keys: code ('0x4038'), attr (32-bit int), module, severity.
  635. The short code used for the hms_errors.py lookup table is 'MMMM_EEEE' — module
  636. from attr bits 16-31, error from the numeric part of code. Falls back to the raw
  637. short code when no description is on file. Returns None for an empty list so
  638. callers can leave error_message unset.
  639. """
  640. if not hms_errors:
  641. return None
  642. from backend.app.services.hms_errors import get_error_description
  643. parts: list[str] = []
  644. for err in hms_errors:
  645. try:
  646. code_str = str(err.get("code", "")).replace("0x", "")
  647. error_num = int(code_str, 16) if code_str else 0
  648. module_num = (int(err.get("attr", 0)) >> 16) & 0xFFFF
  649. short_code = f"{module_num:04X}_{error_num:04X}"
  650. except (TypeError, ValueError):
  651. continue
  652. description = get_error_description(short_code)
  653. parts.append(f"[{short_code}] {description}" if description else f"[{short_code}]")
  654. return "; ".join(parts) if parts else None
  655. async def _bump_library_file_usage_if_completed(db, item, queue_status: str) -> None:
  656. """Increment LibraryFile.print_count and stamp last_printed_at when a queued
  657. print completes successfully. Gated to status=='completed': failed, cancelled
  658. and aborted prints do not count as usage. Caller is responsible for committing
  659. the session. No-op when the queue item has no linked library file (e.g. reprints
  660. from an archive). See #1008."""
  661. if queue_status != "completed" or item.library_file_id is None:
  662. return
  663. from backend.app.models.library import LibraryFile
  664. lib_file = await db.scalar(select(LibraryFile).where(LibraryFile.id == item.library_file_id))
  665. if lib_file is None:
  666. return
  667. lib_file.print_count = (lib_file.print_count or 0) + 1
  668. lib_file.last_printed_at = datetime.now(timezone.utc)
  669. def mark_printer_stopped_by_user(printer_id: int) -> None:
  670. """Mark that the active print on this printer was stopped by the user from the queue UI.
  671. When on_print_complete fires with status 'failed' for a printer in this set we
  672. reclassify it as 'cancelled' so the correct 'print stopped' notification is sent
  673. rather than a 'print failed' notification.
  674. """
  675. _user_stopped_printers.add(printer_id)
  676. logging.getLogger(__name__).info("Marked printer %s as user-stopped from queue", printer_id)
  677. _last_status_broadcast: dict[int, str] = {}
  678. # Track printers where we've updated nozzle_count
  679. _nozzle_count_updated: set[int] = set()
  680. async def on_printer_status_change(printer_id: int, state: PrinterState):
  681. """Handle printer status changes - broadcast via WebSocket."""
  682. # Only broadcast if something meaningful changed (reduce WebSocket spam)
  683. # Include rounded temperatures to detect meaningful temp changes (within 1 degree)
  684. temps = state.temperatures or {}
  685. nozzle_temp = round(temps.get("nozzle", 0))
  686. bed_temp = round(temps.get("bed", 0))
  687. nozzle_2_temp = round(temps.get("nozzle_2", 0)) if "nozzle_2" in temps else ""
  688. chamber_temp = round(temps.get("chamber", 0)) if "chamber" in temps else ""
  689. # Auto-detect dual-nozzle printers from MQTT temperature data
  690. if "nozzle_2" in temps and printer_id not in _nozzle_count_updated:
  691. _nozzle_count_updated.add(printer_id)
  692. # Update nozzle_count in database
  693. async with async_session() as db:
  694. from backend.app.models.printer import Printer
  695. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  696. printer = result.scalar_one_or_none()
  697. if printer and printer.nozzle_count != 2:
  698. printer.nozzle_count = 2
  699. await db.commit()
  700. logging.getLogger(__name__).info(
  701. f"Auto-detected dual-nozzle printer {printer_id}, updated nozzle_count=2"
  702. )
  703. # Include target temps for heating phase detection
  704. bed_target = round(temps.get("bed_target", 0))
  705. nozzle_target = round(temps.get("nozzle_target", 0))
  706. # Include tray_now and vt_tray hash so external spool changes trigger broadcasts
  707. vt_tray_key = hash(str(state.raw_data.get("vt_tray", []))) if state.raw_data else 0
  708. # Include AMS dry_time and tray state values so drying/slot changes trigger broadcasts
  709. ams_dry_key = tuple(a.get("dry_time", 0) for a in (state.raw_data.get("ams") or [])) if state.raw_data else ()
  710. # Include tray states so load/unload transitions (state 11→10) trigger broadcasts (#784)
  711. ams_tray_key = (
  712. tuple(
  713. (t.get("id"), t.get("tray_type", ""), t.get("state"))
  714. for a in (state.raw_data.get("ams") or [])
  715. for t in a.get("tray", [])
  716. )
  717. if state.raw_data
  718. else ()
  719. )
  720. status_key = (
  721. f"{state.connected}:{state.state}:{state.progress}:{state.layer_num}:"
  722. f"{nozzle_temp}:{bed_temp}:{nozzle_2_temp}:{chamber_temp}:"
  723. f"{state.stg_cur}:{bed_target}:{nozzle_target}:"
  724. f"{state.cooling_fan_speed}:{state.big_fan1_speed}:{state.big_fan2_speed}:"
  725. f"{state.chamber_light}:{state.active_extruder}:{state.tray_now}:{vt_tray_key}:"
  726. f"{ams_dry_key}:{ams_tray_key}:{state.door_open}"
  727. )
  728. # MQTT relay - publish status (before dedup check - always publish to MQTT)
  729. try:
  730. printer_info = printer_manager.get_printer(printer_id)
  731. if printer_info:
  732. await mqtt_relay.on_printer_status(printer_id, state, printer_info.name, printer_info.serial_number)
  733. except Exception:
  734. pass # Don't fail status callback if MQTT fails
  735. if _last_status_broadcast.get(printer_id) == status_key:
  736. return # No change, skip WebSocket broadcast
  737. _last_status_broadcast[printer_id] = status_key
  738. # Check for progress milestone notifications (25%, 50%, 75%)
  739. progress = state.progress or 0
  740. is_printing = state.state in ("RUNNING", "PRINTING")
  741. if is_printing and progress > 0:
  742. # Determine which milestone we've reached
  743. current_milestone = 0
  744. if progress >= 75:
  745. current_milestone = 75
  746. elif progress >= 50:
  747. current_milestone = 50
  748. elif progress >= 25:
  749. current_milestone = 25
  750. last_milestone = _last_progress_milestone.get(printer_id, 0)
  751. # If we've crossed a new milestone, send notification
  752. if current_milestone > last_milestone:
  753. _last_progress_milestone[printer_id] = current_milestone
  754. try:
  755. async with async_session() as db:
  756. from backend.app.models.printer import Printer
  757. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  758. printer = result.scalar_one_or_none()
  759. printer_name = printer.name if printer else f"Printer {printer_id}"
  760. filename = state.subtask_name or state.gcode_file or "Unknown"
  761. # remaining_time is in minutes, convert to seconds for notification
  762. remaining_time_seconds = state.remaining_time * 60 if state.remaining_time else None
  763. # Capture camera snapshot for notification image attachment
  764. image_data = await _capture_snapshot_for_notification(
  765. printer_id, printer, logging.getLogger(__name__)
  766. )
  767. await notification_service.on_print_progress(
  768. printer_id,
  769. printer_name,
  770. filename,
  771. current_milestone,
  772. db,
  773. remaining_time_seconds,
  774. image_data=image_data,
  775. )
  776. except Exception as e:
  777. logging.getLogger(__name__).warning(f"Progress milestone notification failed: {e}")
  778. elif progress < 5:
  779. # Reset milestone tracking when print restarts or new print begins
  780. _last_progress_milestone[printer_id] = 0
  781. _first_layer_notified[printer_id] = False
  782. # HMS error codes that should not trigger notifications even though they
  783. # have known descriptions (e.g. user-initiated actions, not real errors).
  784. _HMS_NOTIFICATION_SUPPRESS = {
  785. "0500_400E", # Printing was cancelled (user action, not an error)
  786. }
  787. # Check for new HMS errors and send notifications
  788. current_hms_errors = getattr(state, "hms_errors", []) or []
  789. if current_hms_errors:
  790. # Build set of current error codes (using attr for uniqueness)
  791. current_error_codes = {f"{e.attr:08x}" for e in current_hms_errors}
  792. previously_notified = _notified_hms_errors.get(printer_id, set())
  793. # Find new errors that haven't been notified yet
  794. new_error_codes = current_error_codes - previously_notified
  795. # Update tracking immediately to prevent duplicate notifications from concurrent callbacks
  796. _notified_hms_errors[printer_id] = current_error_codes
  797. _hms_last_seen[printer_id] = time.time()
  798. if new_error_codes:
  799. # Get the actual new errors for the notification
  800. # Filter to severity >= 2 (skip informational/status messages like H2D sends)
  801. new_errors = [e for e in current_hms_errors if f"{e.attr:08x}" in new_error_codes and e.severity >= 2]
  802. try:
  803. async with async_session() as db:
  804. from backend.app.models.printer import Printer
  805. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  806. printer = result.scalar_one_or_none()
  807. printer_name = printer.name if printer else f"Printer {printer_id}"
  808. # Format error details for notification
  809. # Module 0x07 = AMS/Filament, 0x05 = Nozzle, 0x0C = Motion Controller, etc.
  810. module_names = {
  811. 0x03: "Print/Task",
  812. 0x05: "Nozzle/Extruder",
  813. 0x07: "AMS/Filament",
  814. 0x0C: "Motion Controller",
  815. 0x12: "Chamber",
  816. }
  817. from backend.app.services.hms_errors import get_error_description
  818. # Capture camera snapshot once for all error notifications
  819. error_image_data = await _capture_snapshot_for_notification(
  820. printer_id, printer, logging.getLogger(__name__)
  821. )
  822. sent_count = 0
  823. for error in new_errors:
  824. module_name = module_names.get(error.module, f"Module 0x{error.module:02X}")
  825. # Build short code like "0700_8010"
  826. # Mask to 16 bits to handle printers that send larger values
  827. error_code_int = int(error.code.replace("0x", ""), 16) if error.code else 0
  828. error_code_masked = error_code_int & 0xFFFF
  829. short_code = f"{(error.attr >> 16) & 0xFFFF:04X}_{error_code_masked:04X}"
  830. # Only notify for errors with known descriptions — printers
  831. # send many undocumented/phantom codes that aren't real errors.
  832. description = get_error_description(short_code)
  833. if not description or short_code in _HMS_NOTIFICATION_SUPPRESS:
  834. continue
  835. error_type = f"{module_name} Error"
  836. error_detail = description
  837. await notification_service.on_printer_error(
  838. printer_id, printer_name, error_type, db, error_detail, image_data=error_image_data
  839. )
  840. sent_count += 1
  841. if sent_count:
  842. logging.getLogger(__name__).info(
  843. f"[HMS] Sent notification for {sent_count} error(s) on printer {printer_id}"
  844. )
  845. # Also publish to MQTT relay
  846. printer_info = printer_manager.get_printer(printer_id)
  847. if printer_info:
  848. errors_data = [
  849. {
  850. "code": e.code,
  851. "attr": e.attr,
  852. "module": e.module,
  853. "severity": e.severity,
  854. }
  855. for e in new_errors
  856. ]
  857. await mqtt_relay.on_printer_error(
  858. printer_id, printer_info.name, printer_info.serial_number, errors_data
  859. )
  860. except Exception as e:
  861. logging.getLogger(__name__).warning(f"HMS error notification failed: {e}")
  862. else:
  863. # No HMS errors — only clear tracking after a grace period to prevent
  864. # flapping errors (brief hms:[] gaps) from re-triggering notifications.
  865. # Some HMS codes (e.g. chamber temp regulation during PETG prints) toggle
  866. # on/off every few seconds as conditions fluctuate around thresholds.
  867. if printer_id in _notified_hms_errors:
  868. last_seen = _hms_last_seen.get(printer_id, 0)
  869. if time.time() - last_seen >= _HMS_CLEAR_GRACE_SECONDS:
  870. _notified_hms_errors.pop(printer_id, None)
  871. _hms_last_seen.pop(printer_id, None)
  872. await ws_manager.send_printer_status(
  873. printer_id,
  874. printer_state_to_dict(state, printer_id, printer_manager.get_model(printer_id)),
  875. )
  876. def _is_bambu_uuid(tray_uuid: str) -> bool:
  877. """Check if a tray UUID looks like a valid Bambu Lab RFID UUID (non-empty, non-zero)."""
  878. return bool(tray_uuid) and tray_uuid not in ("", "0" * len(tray_uuid))
  879. async def on_ams_change(printer_id: int, ams_data: list):
  880. """Handle AMS data changes - sync to Spoolman if enabled and auto mode."""
  881. logger = logging.getLogger(__name__)
  882. # Snapshot BEFORE any await: if a print is active, skip weight sync later.
  883. # on_print_complete may pop _active_sessions during our awaits (#880).
  884. from backend.app.services.usage_tracker import _active_sessions
  885. _print_active = printer_id in _active_sessions
  886. # MQTT relay - publish AMS change
  887. try:
  888. printer_info = printer_manager.get_printer(printer_id)
  889. if printer_info:
  890. await mqtt_relay.on_ams_change(printer_id, printer_info.name, printer_info.serial_number, ams_data)
  891. except Exception:
  892. pass # Don't fail AMS callback if MQTT fails
  893. # Broadcast AMS change via WebSocket (bypasses status_key deduplication)
  894. # This ensures frontend gets immediate updates when AMS slots are configured
  895. try:
  896. state = printer_manager.get_status(printer_id)
  897. if state:
  898. logger.info("[Printer %s] Broadcasting AMS change via WebSocket", printer_id)
  899. await ws_manager.send_printer_status(
  900. printer_id,
  901. printer_state_to_dict(state, printer_id, printer_manager.get_model(printer_id)),
  902. )
  903. except Exception as e:
  904. logger.warning("Failed to broadcast AMS change for printer %s: %s", printer_id, e)
  905. from backend.app.utils.color_utils import colors_similar as _colors_similar
  906. # Auto-unlink spool assignments with stale fingerprints
  907. try:
  908. async with async_session() as db:
  909. from sqlalchemy.orm import selectinload
  910. from backend.app.api.routes.inventory import _find_tray_in_ams_data
  911. from backend.app.models.spool import Spool as _Spool
  912. from backend.app.models.spool_assignment import SpoolAssignment as SA
  913. result = await db.execute(
  914. select(SA)
  915. .where(SA.printer_id == printer_id)
  916. .options(selectinload(SA.spool).selectinload(_Spool.k_profiles))
  917. )
  918. stale = []
  919. for assignment in result.scalars().all():
  920. # External spool assignments (ams_id=255) live in vt_tray, not AMS data
  921. if assignment.ams_id == 255:
  922. ps = printer_manager.get_status(printer_id)
  923. vt_tray_raw = ps.raw_data.get("vt_tray", []) if ps else []
  924. ext_id = assignment.tray_id + 254 # 0→254, 1→255
  925. current_tray = None
  926. for vt in vt_tray_raw:
  927. if isinstance(vt, dict) and int(vt.get("id", 254)) == ext_id:
  928. current_tray = vt
  929. break
  930. if not current_tray:
  931. # vt_tray data may not have arrived yet — keep assignment
  932. continue
  933. else:
  934. current_tray = _find_tray_in_ams_data(ams_data, assignment.ams_id, assignment.tray_id)
  935. if not current_tray:
  936. logger.info(
  937. "Auto-unlink: spool %d AMS%d-T%d — tray not found in AMS data (slot empty?)",
  938. assignment.spool_id,
  939. assignment.ams_id,
  940. assignment.tray_id,
  941. )
  942. stale.append(assignment) # Slot empty
  943. elif _is_bambu_uuid(current_tray.get("tray_uuid", "")):
  944. # A Bambu Lab spool is in this slot — check if it's the same spool
  945. # that's currently assigned. If yes, keep the assignment (avoids
  946. # unnecessary unlink/re-assign/ams_filament_setting cycle that clears
  947. # the printer's filament preset on every startup).
  948. tray_uuid = current_tray.get("tray_uuid", "")
  949. tag_uid = current_tray.get("tag_uid", "")
  950. spool = assignment.spool
  951. spool_matches = False
  952. if spool:
  953. if (spool.tray_uuid and spool.tray_uuid.upper() == tray_uuid.upper()) or (
  954. spool.tag_uid
  955. and tag_uid
  956. and tag_uid != "0000000000000000"
  957. and spool.tag_uid.upper() == tag_uid.upper()
  958. ):
  959. spool_matches = True
  960. if spool_matches:
  961. # Same BL spool still in slot — keep assignment, update fingerprint if needed
  962. cur_color = current_tray.get("tray_color", "")
  963. cur_type = current_tray.get("tray_type", "")
  964. fp_color = assignment.fingerprint_color or ""
  965. fp_type = assignment.fingerprint_type or ""
  966. if cur_color.upper() != fp_color.upper() or cur_type.upper() != fp_type.upper():
  967. assignment.fingerprint_color = cur_color
  968. assignment.fingerprint_type = cur_type
  969. logger.debug(
  970. "Auto-unlink: spool %d AMS%d-T%d — same BL spool, updated fingerprint",
  971. assignment.spool_id,
  972. assignment.ams_id,
  973. assignment.tray_id,
  974. )
  975. continue
  976. # Different BL spool or unrecognized — unlink so auto-assign can match
  977. logger.info(
  978. "Auto-unlink: spool %d AMS%d-T%d — different Bambu Lab spool detected (uuid=%s)",
  979. assignment.spool_id,
  980. assignment.ams_id,
  981. assignment.tray_id,
  982. tray_uuid,
  983. )
  984. stale.append(assignment)
  985. else:
  986. cur_color = current_tray.get("tray_color", "")
  987. cur_type = current_tray.get("tray_type", "")
  988. cur_state = current_tray.get("state")
  989. fp_color = assignment.fingerprint_color or ""
  990. fp_type = assignment.fingerprint_type or ""
  991. # SpoolBuddy pre-config replay: fingerprint_type empty means
  992. # the slot was empty when the user pre-assigned via SpoolBuddy
  993. # (the firmware drops ams_filament_setting on empty slots, so
  994. # MQTT was deferred). The moment any filament gets inserted
  995. # — Bambu RFID, 3rd-party, or even an existing-but-now-
  996. # reconfigured spool — fire the deferred configuration.
  997. # The "loaded" signal is state == 11 (Bambu's "filament fed to
  998. # extruder" code) OR, on firmwares that don't use the state
  999. # enum meaningfully, a non-empty tray_type when state is
  1000. # NOT one of the firmware's explicit empty signals (9, 10).
  1001. # state-only was wrong for firmwares that never set 11 — A1
  1002. # Mini BMCU 01.07.02.00 and P1S Standard AMS 00.00.06.75 both
  1003. # always report state=3 — so the replay never fired for them
  1004. # (#1322). The state ∉ {9,10} guard keeps the firmware's
  1005. # explicit "empty" signals authoritative over any stale
  1006. # tray_type that might survive the relay's auto-clearing.
  1007. loaded = cur_state == 11 or (cur_state not in (9, 10) and cur_type.strip())
  1008. if not fp_type.strip() and loaded and assignment.spool:
  1009. try:
  1010. from backend.app.api.routes.inventory import (
  1011. apply_spool_to_slot_via_mqtt,
  1012. )
  1013. await apply_spool_to_slot_via_mqtt(
  1014. db=db,
  1015. current_user=None,
  1016. spool=assignment.spool,
  1017. printer_id=printer_id,
  1018. ams_id=assignment.ams_id,
  1019. tray_id=assignment.tray_id,
  1020. current_tray_info_idx=current_tray.get("tray_info_idx", ""),
  1021. current_tray_type=cur_type,
  1022. )
  1023. logger.info(
  1024. "SpoolBuddy pre-config applied on insert: spool %d → printer %d AMS%d-T%d",
  1025. assignment.spool_id,
  1026. printer_id,
  1027. assignment.ams_id,
  1028. assignment.tray_id,
  1029. )
  1030. except Exception:
  1031. logger.exception(
  1032. "Pre-config apply failed for spool %d on printer %d AMS%d-T%d",
  1033. assignment.spool_id,
  1034. printer_id,
  1035. assignment.ams_id,
  1036. assignment.tray_id,
  1037. )
  1038. assignment.fingerprint_color = cur_color
  1039. assignment.fingerprint_type = cur_type
  1040. continue
  1041. if not _colors_similar(cur_color, fp_color) or cur_type.upper() != fp_type.upper():
  1042. # Fingerprint mismatch — but check if tray now matches the
  1043. # assigned spool (e.g. auto-configure changed the tray).
  1044. spool = assignment.spool
  1045. if spool:
  1046. spool_color = (spool.rgba or "FFFFFFFF").upper()
  1047. spool_type = (spool.material or "").upper()
  1048. if _colors_similar(cur_color, spool_color) and cur_type.upper() == spool_type:
  1049. logger.info(
  1050. "Auto-unlink: spool %d AMS%d-T%d — fingerprint mismatch but tray matches spool, updating fp",
  1051. assignment.spool_id,
  1052. assignment.ams_id,
  1053. assignment.tray_id,
  1054. )
  1055. assignment.fingerprint_color = cur_color
  1056. assignment.fingerprint_type = cur_type
  1057. continue
  1058. logger.info(
  1059. "Auto-unlink: spool %d AMS%d-T%d — fingerprint mismatch (cur=%s/%s fp=%s/%s spool=%s/%s)",
  1060. assignment.spool_id,
  1061. assignment.ams_id,
  1062. assignment.tray_id,
  1063. cur_color,
  1064. cur_type,
  1065. fp_color,
  1066. fp_type,
  1067. spool.rgba if spool else "?",
  1068. spool.material if spool else "?",
  1069. )
  1070. stale.append(assignment) # Spool changed
  1071. for a in stale:
  1072. await db.delete(a)
  1073. if stale:
  1074. logger.info("Auto-unlinked %d stale spool assignments for printer %d", len(stale), printer_id)
  1075. # Commit any changes (stale deletions and/or fingerprint updates)
  1076. await db.commit()
  1077. except Exception as e:
  1078. logger.warning("Spool assignment cleanup failed: %s", e, exc_info=True)
  1079. # Auto-manage inventory spools from AMS tray data (skip if Spoolman manages AMS).
  1080. # Serialised per-printer via _ams_assignment_locks: MQTT bursts can deliver
  1081. # two AMS pushes ~30 ms apart, and without the lock both callbacks read
  1082. # "no existing assignment" for the same (printer, ams, tray) and race to
  1083. # INSERT, hitting the spool_assignment_printer_id_ams_id_tray_id_key
  1084. # unique constraint on Postgres. SQLite's WAL serialises writes so the
  1085. # bug stayed latent there. See _ams_assignment_locks comment for details.
  1086. try:
  1087. async with _get_ams_assignment_lock(printer_id), async_session() as db:
  1088. from backend.app.api.routes.settings import get_setting
  1089. from backend.app.models.spool import Spool
  1090. from backend.app.models.spool_assignment import SpoolAssignment as SA
  1091. from backend.app.services.spool_tag_matcher import (
  1092. auto_assign_spool,
  1093. create_spool_from_tray,
  1094. find_matching_untagged_spool,
  1095. get_spool_by_tag,
  1096. is_bambu_tag,
  1097. is_valid_tag,
  1098. link_tag_to_inventory_spool,
  1099. )
  1100. _spoolman_on = await get_setting(db, "spoolman_enabled")
  1101. if not _spoolman_on or _spoolman_on.lower() != "true":
  1102. for ams_unit in ams_data:
  1103. if not isinstance(ams_unit, dict):
  1104. continue
  1105. ams_id = int(ams_unit.get("id", 0))
  1106. for tray in ams_unit.get("tray", []):
  1107. if not isinstance(tray, dict):
  1108. continue
  1109. tray_id = int(tray.get("id", 0))
  1110. tag_uid = tray.get("tag_uid", "")
  1111. tray_uuid = tray.get("tray_uuid", "")
  1112. tray_info_idx = tray.get("tray_info_idx", "")
  1113. if not tray.get("tray_type"):
  1114. continue # Empty slot
  1115. # Check if assignment already exists for this slot
  1116. existing = await db.execute(
  1117. select(SA)
  1118. .options(selectinload(SA.spool).selectinload(Spool.k_profiles))
  1119. .where(SA.printer_id == printer_id, SA.ams_id == ams_id, SA.tray_id == tray_id)
  1120. )
  1121. existing_assignment = existing.scalar_one_or_none()
  1122. if existing_assignment:
  1123. # Sync spool weight_used from AMS remain — only INCREASE, never decrease.
  1124. # The AMS remain% is low-resolution (integer %, i.e. 10g steps for 1kg spool)
  1125. # and must not overwrite precise values from the usage tracker (3MF/G-code).
  1126. # Skip during active prints: the usage tracker handles deduction
  1127. # precisely via 3MF data on print completion. Without this guard the
  1128. # AMS remain% SET and the usage tracker ADD both fire from the same
  1129. # MQTT message, doubling the deduction (#880).
  1130. if _print_active:
  1131. continue
  1132. remain_raw = tray.get("remain")
  1133. if (
  1134. remain_raw is not None
  1135. and existing_assignment.spool
  1136. and not existing_assignment.spool.weight_locked
  1137. ):
  1138. try:
  1139. remain_val = int(remain_raw)
  1140. except (TypeError, ValueError):
  1141. remain_val = -1
  1142. if 1 <= remain_val <= 100:
  1143. lw = existing_assignment.spool.label_weight or 1000
  1144. new_used = round(lw * (100 - remain_val) / 100.0, 1)
  1145. current_used = existing_assignment.spool.weight_used or 0
  1146. if new_used > current_used + 1:
  1147. logger.info(
  1148. "Weight sync: spool %d weight_used %s -> %s (remain=%d)",
  1149. existing_assignment.spool_id,
  1150. current_used,
  1151. new_used,
  1152. remain_val,
  1153. )
  1154. existing_assignment.spool.weight_used = new_used
  1155. await db.commit()
  1156. # Re-apply stored K-profile when the live tray's
  1157. # cali_idx drifted from the spool's stored profile.
  1158. # This catches "reset slot → re-read" and any other
  1159. # path where the firmware loses the user's K-profile
  1160. # selection while the SpoolAssignment row persists.
  1161. # Per the maintainer's rule: any time a spool tag is
  1162. # identified and matches inventory, the slot must be
  1163. # configured with the spool's stored settings. Without
  1164. # this block the existing-assignment branch only ran
  1165. # weight-sync and let the firmware-default cali_idx win.
  1166. try:
  1167. spool = existing_assignment.spool
  1168. if (
  1169. spool is not None
  1170. and is_bambu_tag(tag_uid, tray_uuid, tray_info_idx)
  1171. and spool.k_profiles
  1172. ):
  1173. state = printer_manager.get_status(printer_id)
  1174. nozzle_diameter = "0.4"
  1175. if state and state.nozzles:
  1176. nd = state.nozzles[0].nozzle_diameter
  1177. if nd:
  1178. nozzle_diameter = nd
  1179. slot_extruder: int | None = None
  1180. if state and state.ams_extruder_map:
  1181. if ams_id == 255:
  1182. slot_extruder = 1 - tray_id
  1183. else:
  1184. slot_extruder = state.ams_extruder_map.get(str(ams_id))
  1185. # Prefer exact extruder match, fall back to
  1186. # extruder-agnostic kp for the same printer +
  1187. # nozzle. Avoids hard-skipping when the AMS is
  1188. # mapped differently than at calibration time.
  1189. matching_kp = None
  1190. fallback_kp = None
  1191. for kp in spool.k_profiles:
  1192. if (
  1193. kp.printer_id != printer_id
  1194. or kp.nozzle_diameter != nozzle_diameter
  1195. or kp.cali_idx is None
  1196. ):
  1197. continue
  1198. if (
  1199. slot_extruder is not None
  1200. and kp.extruder is not None
  1201. and kp.extruder == slot_extruder
  1202. ):
  1203. matching_kp = kp
  1204. break
  1205. if fallback_kp is None:
  1206. fallback_kp = kp
  1207. chosen_kp = matching_kp or fallback_kp
  1208. if chosen_kp is not None:
  1209. live_cali_idx = tray.get("cali_idx")
  1210. # Only fire MQTT when the printer's live
  1211. # cali_idx differs from the stored value.
  1212. # Avoids spamming the broker on every
  1213. # MQTT push during steady-state operation.
  1214. if live_cali_idx != chosen_kp.cali_idx:
  1215. client = printer_manager.get_client(printer_id)
  1216. if client:
  1217. cali_filament_id = spool.slicer_filament or tray_info_idx or ""
  1218. client.extrusion_cali_sel(
  1219. ams_id=ams_id,
  1220. tray_id=tray_id,
  1221. cali_idx=chosen_kp.cali_idx,
  1222. filament_id=cali_filament_id,
  1223. nozzle_diameter=nozzle_diameter,
  1224. )
  1225. logger.info(
  1226. "Re-applied K-profile cali_idx=%d for spool %d "
  1227. "on printer %d AMS%d-T%d (live=%s drift detected)",
  1228. chosen_kp.cali_idx,
  1229. spool.id,
  1230. printer_id,
  1231. ams_id,
  1232. tray_id,
  1233. live_cali_idx,
  1234. )
  1235. except Exception:
  1236. logger.exception(
  1237. "K-profile re-apply failed for printer %d AMS%d-T%d",
  1238. printer_id,
  1239. ams_id,
  1240. tray_id,
  1241. )
  1242. continue
  1243. if is_bambu_tag(tag_uid, tray_uuid, tray_info_idx):
  1244. # BL spool with RFID tag: auto-match → inventory match → auto-create
  1245. spool = await get_spool_by_tag(db, tag_uid, tray_uuid)
  1246. if not spool:
  1247. # Try matching an untagged inventory spool (same material/color)
  1248. spool = await find_matching_untagged_spool(db, tray)
  1249. if spool:
  1250. await link_tag_to_inventory_spool(db, spool, tray)
  1251. else:
  1252. spool = await create_spool_from_tray(db, tray)
  1253. await auto_assign_spool(
  1254. printer_id,
  1255. ams_id,
  1256. tray_id,
  1257. spool,
  1258. printer_manager,
  1259. db,
  1260. tray_info_idx=tray_info_idx,
  1261. )
  1262. await db.commit()
  1263. await ws_manager.broadcast(
  1264. {
  1265. "type": "spool_auto_assigned",
  1266. "printer_id": printer_id,
  1267. "ams_id": ams_id,
  1268. "tray_id": tray_id,
  1269. "spool_id": spool.id,
  1270. }
  1271. )
  1272. logger.info(
  1273. "RFID auto-assigned spool %d to printer %d AMS%d-T%d",
  1274. spool.id,
  1275. printer_id,
  1276. ams_id,
  1277. tray_id,
  1278. )
  1279. elif is_valid_tag(tag_uid, tray_uuid):
  1280. # Non-BL spool with some tag — let user choose
  1281. await ws_manager.broadcast(
  1282. {
  1283. "type": "unknown_tag",
  1284. "printer_id": printer_id,
  1285. "ams_id": ams_id,
  1286. "tray_id": tray_id,
  1287. "tag_uid": tag_uid,
  1288. "tray_uuid": tray_uuid,
  1289. }
  1290. )
  1291. else:
  1292. # No tag at all — let user choose from inventory
  1293. await ws_manager.broadcast(
  1294. {
  1295. "type": "unknown_tag",
  1296. "printer_id": printer_id,
  1297. "ams_id": ams_id,
  1298. "tray_id": tray_id,
  1299. "tag_uid": "",
  1300. "tray_uuid": "",
  1301. }
  1302. )
  1303. except Exception as e:
  1304. logger.warning("RFID spool auto-assign failed: %s", e, exc_info=True)
  1305. try:
  1306. async with async_session() as db:
  1307. from backend.app.api.routes.settings import get_setting
  1308. from backend.app.models.printer import Printer
  1309. # Check if Spoolman is enabled
  1310. spoolman_enabled = await get_setting(db, "spoolman_enabled")
  1311. if not spoolman_enabled or spoolman_enabled.lower() != "true":
  1312. return
  1313. # Check sync mode
  1314. sync_mode = await get_setting(db, "spoolman_sync_mode")
  1315. if sync_mode and sync_mode != "auto":
  1316. return # Only sync on auto mode
  1317. # `spoolman_disable_weight_sync` is deprecated (#1119) — weight is now
  1318. # always owned by per-print tracking, never by AMS auto-sync. The
  1319. # setting is still read by the settings UI for backwards compat but
  1320. # has no effect on the sync path here.
  1321. # Get Spoolman URL
  1322. spoolman_url = await get_setting(db, "spoolman_url")
  1323. if not spoolman_url:
  1324. return
  1325. # Get or create Spoolman client
  1326. client = await get_spoolman_client()
  1327. if not client:
  1328. try:
  1329. client = await init_spoolman_client(spoolman_url)
  1330. except ValueError as exc:
  1331. logger.warning("Spoolman URL %r rejected by SSRF guard: %s", spoolman_url, exc)
  1332. return
  1333. # Check if Spoolman is reachable
  1334. if not await client.health_check():
  1335. logger.warning("Spoolman not reachable at %s", spoolman_url)
  1336. return
  1337. # Get printer name for location
  1338. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1339. printer = result.scalar_one_or_none()
  1340. printer_name = printer.name if printer else f"Printer {printer_id}"
  1341. # OPTIMIZATION: Fetch all spools once before processing trays
  1342. # This eliminates redundant API calls (one per tray) when syncing multiple trays
  1343. logger.debug("[Printer %s] Fetching spools cache for AMS sync...", printer_id)
  1344. try:
  1345. cached_spools = await client.get_spools()
  1346. logger.debug("[Printer %s] Cached %d spools for batch sync", printer_id, len(cached_spools))
  1347. except Exception as e:
  1348. logger.error(
  1349. "[Printer %s] Failed to fetch spools cache after retries, aborting AMS sync: %s",
  1350. printer_id,
  1351. e,
  1352. )
  1353. return
  1354. # Load inventory weights as fallback (when AMS MQTT data lacks remain values)
  1355. from sqlalchemy.orm import selectinload
  1356. from backend.app.models.spool_assignment import SpoolAssignment
  1357. from backend.app.models.spoolman_slot_assignment import SpoolmanSlotAssignment
  1358. inventory_weights: dict[tuple[int, int], float] = {}
  1359. try:
  1360. assign_result = await db.execute(
  1361. select(SpoolAssignment)
  1362. .options(selectinload(SpoolAssignment.spool))
  1363. .where(SpoolAssignment.printer_id == printer_id)
  1364. )
  1365. for assignment in assign_result.scalars().all():
  1366. spool = assignment.spool
  1367. if spool and spool.label_weight > 0:
  1368. remaining = max(0.0, spool.label_weight - (spool.weight_used or 0))
  1369. inventory_weights[(assignment.ams_id, assignment.tray_id)] = remaining
  1370. except Exception as e:
  1371. logger.warning("Could not load inventory weights for printer %s: %s", printer_id, e)
  1372. # Load existing Spoolman slot assignments for the no-RFID fallback path
  1373. spoolman_slot_map: dict[tuple[int, int], int] = {}
  1374. try:
  1375. slot_result = await db.execute(
  1376. select(SpoolmanSlotAssignment).where(SpoolmanSlotAssignment.printer_id == printer_id)
  1377. )
  1378. for slot in slot_result.scalars().all():
  1379. spoolman_slot_map[(slot.ams_id, slot.tray_id)] = slot.spoolman_spool_id
  1380. except Exception as e:
  1381. logger.warning("Could not load Spoolman slot assignments for printer %s: %s", printer_id, e)
  1382. # Sync each AMS tray and collect slot changes for DB persistence
  1383. synced = 0
  1384. slot_changes: list[tuple[int, int, int]] = [] # (ams_id, tray_id, spoolman_spool_id) to upsert
  1385. empty_slots: list[tuple[int, int]] = [] # (ams_id, tray_id) whose tray is now empty
  1386. for ams_unit in ams_data:
  1387. if not isinstance(ams_unit, dict):
  1388. continue
  1389. ams_id = int(ams_unit.get("id", 0))
  1390. trays = ams_unit.get("tray", [])
  1391. for tray_data in trays:
  1392. if not isinstance(tray_data, dict):
  1393. continue
  1394. tray_id_raw = int(tray_data.get("id", 0))
  1395. tray = client.parse_ams_tray(ams_id, tray_data)
  1396. if not tray:
  1397. # Empty tray slot — record for local assignment cleanup
  1398. empty_slots.append((ams_id, tray_id_raw))
  1399. continue
  1400. spool_tag = (
  1401. tray.tray_uuid
  1402. if tray.tray_uuid and tray.tray_uuid != "00000000000000000000000000000000"
  1403. else tray.tag_uid
  1404. )
  1405. # Provide the hint only when no RFID is available
  1406. hint = spoolman_slot_map.get((ams_id, tray.tray_id)) if not spool_tag else None
  1407. try:
  1408. inv_remaining = inventory_weights.get((ams_id, tray.tray_id))
  1409. result = await client.sync_ams_tray(
  1410. tray,
  1411. printer_name,
  1412. # Per-print tracking is the only weight writer (#1119).
  1413. # AMS auto-sync still maintains spool metadata / slot
  1414. # assignments but no longer touches remaining_weight.
  1415. disable_weight_sync=True,
  1416. cached_spools=cached_spools,
  1417. inventory_remaining=inv_remaining,
  1418. spoolman_spool_id_hint=hint,
  1419. )
  1420. if result:
  1421. synced += 1
  1422. if result.get("id"):
  1423. slot_changes.append((ams_id, tray.tray_id, result["id"]))
  1424. # If a new spool was created, add it to the cache
  1425. # so subsequent trays can find it if they reference the same tag
  1426. spool_exists = any(s.get("id") == result["id"] for s in cached_spools)
  1427. if not spool_exists:
  1428. cached_spools.append(result)
  1429. logger.debug(
  1430. "[Printer %s] Added newly created spool %s to cache",
  1431. printer_id,
  1432. result["id"],
  1433. )
  1434. except Exception as e:
  1435. logger.error("Error syncing AMS %s tray %s: %s", ams_id, tray.tray_id, e)
  1436. if synced > 0:
  1437. logger.info("Auto-synced %s AMS trays to Spoolman for printer %s", synced, printer_id)
  1438. # Persist slot assignment changes to the local table
  1439. if slot_changes or empty_slots:
  1440. try:
  1441. for ams_id, tray_id, spool_id in slot_changes:
  1442. await db.execute(
  1443. text(
  1444. "INSERT INTO spoolman_slot_assignments"
  1445. " (printer_id, ams_id, tray_id, spoolman_spool_id)"
  1446. " VALUES (:printer_id, :ams_id, :tray_id, :spool_id)"
  1447. " ON CONFLICT(printer_id, ams_id, tray_id)"
  1448. " DO UPDATE SET spoolman_spool_id = excluded.spoolman_spool_id"
  1449. ),
  1450. {
  1451. "printer_id": printer_id,
  1452. "ams_id": ams_id,
  1453. "tray_id": tray_id,
  1454. "spool_id": spool_id,
  1455. },
  1456. )
  1457. for ams_id, tray_id in empty_slots:
  1458. await db.execute(
  1459. delete(SpoolmanSlotAssignment).where(
  1460. SpoolmanSlotAssignment.printer_id == printer_id,
  1461. SpoolmanSlotAssignment.ams_id == ams_id,
  1462. SpoolmanSlotAssignment.tray_id == tray_id,
  1463. )
  1464. )
  1465. await db.commit()
  1466. except Exception as e:
  1467. await db.rollback()
  1468. logger.error("Error persisting Spoolman slot assignments for printer %s: %s", printer_id, e)
  1469. except Exception as e:
  1470. logging.getLogger(__name__).error("Spoolman AMS sync failed for printer %s: %s", printer_id, e)
  1471. async def _capture_snapshot_for_notification(printer_id: int, printer, logger) -> bytes | None:
  1472. """Capture a camera snapshot for notification image attachment.
  1473. Returns JPEG bytes (max 2.5MB) or None if capture fails or is unavailable.
  1474. Uses: external camera > buffered frame > fresh capture.
  1475. """
  1476. if not printer:
  1477. return None
  1478. try:
  1479. from backend.app.api.routes.settings import get_setting
  1480. async with async_session() as db:
  1481. capture_enabled = await get_setting(db, "capture_finish_photo")
  1482. if capture_enabled is not None and capture_enabled.lower() != "true":
  1483. return None
  1484. # Try external camera first
  1485. if printer.external_camera_enabled and printer.external_camera_url:
  1486. logger.info("[SNAPSHOT] Capturing from external camera for printer %s", printer_id)
  1487. from backend.app.services.external_camera import capture_frame
  1488. frame_data = await capture_frame(
  1489. printer.external_camera_url,
  1490. printer.external_camera_type or "mjpeg",
  1491. snapshot_url=printer.external_camera_snapshot_url,
  1492. )
  1493. if frame_data and len(frame_data) <= 2_500_000:
  1494. logger.info("[SNAPSHOT] External camera frame: %s bytes", len(frame_data))
  1495. return _apply_camera_rotation(frame_data, printer, logger)
  1496. # Try buffered frame from active stream
  1497. from backend.app.api.routes.camera import _active_chamber_streams, _active_streams, get_buffered_frame
  1498. active_for_printer = [k for k in _active_streams if k.startswith(f"{printer_id}-")]
  1499. active_chamber = [k for k in _active_chamber_streams if k.startswith(f"{printer_id}-")]
  1500. buffered_frame = get_buffered_frame(printer_id)
  1501. if (active_for_printer or active_chamber) and buffered_frame:
  1502. logger.info("[SNAPSHOT] Using buffered frame for printer %s: %s bytes", printer_id, len(buffered_frame))
  1503. if len(buffered_frame) <= 2_500_000:
  1504. return _apply_camera_rotation(buffered_frame, printer, logger)
  1505. # Fresh capture from printer camera
  1506. logger.info("[SNAPSHOT] Capturing fresh frame for printer %s", printer_id)
  1507. from backend.app.services.camera import capture_camera_frame_bytes
  1508. frame_data = await capture_camera_frame_bytes(
  1509. printer.ip_address, printer.access_code, printer.model, timeout=15
  1510. )
  1511. if frame_data and len(frame_data) <= 2_500_000:
  1512. logger.info("[SNAPSHOT] Fresh camera frame: %s bytes", len(frame_data))
  1513. return _apply_camera_rotation(frame_data, printer, logger)
  1514. except Exception as e:
  1515. logger.warning("[SNAPSHOT] Failed to capture snapshot for printer %s: %s", printer_id, e)
  1516. return None
  1517. def _apply_camera_rotation(image_data: bytes, printer, logger) -> bytes:
  1518. """Apply camera rotation to snapshot image if configured."""
  1519. rotation = getattr(printer, "camera_rotation", 0)
  1520. if not rotation or rotation == 0:
  1521. return image_data
  1522. try:
  1523. from io import BytesIO
  1524. from PIL import Image
  1525. img = Image.open(BytesIO(image_data))
  1526. # PIL rotate is counter-clockwise, so negate for clockwise rotation
  1527. img = img.rotate(-rotation, expand=True)
  1528. buf = BytesIO()
  1529. img.save(buf, format="JPEG", quality=90)
  1530. rotated = buf.getvalue()
  1531. logger.info("[SNAPSHOT] Applied %d° rotation: %s → %s bytes", rotation, len(image_data), len(rotated))
  1532. return rotated
  1533. except Exception as e:
  1534. logger.warning("[SNAPSHOT] Failed to apply rotation: %s", e)
  1535. return image_data
  1536. async def _send_print_start_notification(
  1537. printer_id: int,
  1538. data: dict,
  1539. archive_data: dict | None = None,
  1540. logger=None,
  1541. ):
  1542. """Helper to send print start notification with optional archive data."""
  1543. if logger is None:
  1544. logger = logging.getLogger(__name__)
  1545. try:
  1546. async with async_session() as db:
  1547. from backend.app.models.printer import Printer
  1548. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1549. printer = result.scalar_one_or_none()
  1550. printer_name = printer.name if printer else f"Printer {printer_id}"
  1551. # Capture camera snapshot for notification image attachment
  1552. image_data = await _capture_snapshot_for_notification(printer_id, printer, logger)
  1553. if image_data:
  1554. if archive_data is None:
  1555. archive_data = {}
  1556. archive_data["image_data"] = image_data
  1557. await notification_service.on_print_start(printer_id, printer_name, data, db, archive_data=archive_data)
  1558. # Send user-specific email notification for print start
  1559. if archive_data and archive_data.get("created_by_id"):
  1560. await notification_service.send_user_print_email(
  1561. event_type="user_print_start",
  1562. created_by_id=archive_data["created_by_id"],
  1563. printer_name=printer_name,
  1564. filename=data.get("subtask_name") or data.get("filename", "Unknown"),
  1565. db=db,
  1566. )
  1567. except Exception as e:
  1568. logger.warning("Notification on_print_start failed: %s", e)
  1569. async def _dispatch_user_print_email(
  1570. status: str,
  1571. created_by_id: int | None,
  1572. printer_name: str,
  1573. filename: str,
  1574. db,
  1575. ) -> None:
  1576. """Send a user-specific print-completion email based on print status.
  1577. Maps the normalised print status to the correct event type and delegates
  1578. to :meth:`NotificationService.send_user_print_email`. A single helper
  1579. avoids duplicating the ``if status == "completed" / elif "failed" / elif
  1580. "stopped"`` dispatch block at every call site.
  1581. Does nothing if *created_by_id* is ``None``.
  1582. """
  1583. if created_by_id is None:
  1584. return
  1585. if status == "completed":
  1586. event_type = "user_print_complete"
  1587. elif status == "failed":
  1588. event_type = "user_print_failed"
  1589. elif status in ("stopped", "aborted", "cancelled"):
  1590. event_type = "user_print_stopped"
  1591. else:
  1592. return
  1593. await notification_service.send_user_print_email(
  1594. event_type=event_type,
  1595. created_by_id=created_by_id,
  1596. printer_name=printer_name,
  1597. filename=filename,
  1598. db=db,
  1599. )
  1600. def _load_objects_from_archive(archive, printer_id: int, logger) -> None:
  1601. """Extract printable objects from an archive's 3MF file and store in printer state."""
  1602. try:
  1603. from backend.app.services.archive import extract_printable_objects_from_3mf
  1604. file_path = app_settings.base_dir / archive.file_path
  1605. if file_path.is_file() and str(file_path).endswith(".3mf"):
  1606. with open(file_path, "rb") as f:
  1607. threemf_data = f.read()
  1608. # Extract with positions for UI overlay
  1609. printable_objects, bbox_all = extract_printable_objects_from_3mf(threemf_data, include_positions=True)
  1610. if printable_objects:
  1611. client = printer_manager.get_client(printer_id)
  1612. if client:
  1613. client.state.printable_objects = printable_objects
  1614. client.state.printable_objects_bbox_all = bbox_all
  1615. client.state.skipped_objects = []
  1616. logger.info("Loaded %s printable objects for printer %s", len(printable_objects), printer_id)
  1617. except Exception as e:
  1618. logger.debug("Failed to extract printable objects from archive: %s", e)
  1619. async def on_print_start(printer_id: int, data: dict):
  1620. """Handle print start - archive the 3MF file immediately."""
  1621. logger = logging.getLogger(__name__)
  1622. logger.info("[CALLBACK] on_print_start called for printer %s, data keys: %s", printer_id, list(data.keys()))
  1623. # Clear any stale user-stopped flag from previous print cycles
  1624. _user_stopped_printers.discard(printer_id)
  1625. # Cancel any active bed cooldown waiter for this printer
  1626. if _bed_cool_waiters.pop(printer_id, None):
  1627. logger.info("[BED-COOL] Cancelled bed cooldown waiter for printer %s (new print started)", printer_id)
  1628. # Clear cached cover images so the new print's thumbnail is fetched fresh
  1629. from backend.app.api.routes.printers import clear_cover_cache
  1630. clear_cover_cache(printer_id)
  1631. await ws_manager.send_print_start(printer_id, data)
  1632. # Notify when the print-start AMS mapping references tray slots without spool assignments.
  1633. await notify_missing_spool_assignments_on_print_start(printer_id, data, logger)
  1634. # MQTT relay - publish print start
  1635. try:
  1636. printer_info = printer_manager.get_printer(printer_id)
  1637. if printer_info:
  1638. await mqtt_relay.on_print_start(
  1639. printer_id,
  1640. printer_info.name,
  1641. printer_info.serial_number,
  1642. data.get("filename", ""),
  1643. data.get("subtask_name", ""),
  1644. )
  1645. except Exception:
  1646. pass # Don't fail print start callback if MQTT fails
  1647. # Capture AMS tray remain% for filament consumption tracking (skip if Spoolman handles usage)
  1648. try:
  1649. async with async_session() as db:
  1650. from backend.app.api.routes.settings import get_setting
  1651. _spoolman_on = await get_setting(db, "spoolman_enabled")
  1652. if not _spoolman_on or _spoolman_on.lower() != "true":
  1653. from backend.app.services.usage_tracker import on_print_start as usage_on_print_start
  1654. await usage_on_print_start(printer_id, data, printer_manager, db=db)
  1655. except Exception as e:
  1656. logger.warning("Usage tracker on_print_start failed: %s", e)
  1657. # Track if notification was sent (to avoid sending twice)
  1658. notification_sent = False
  1659. # Smart plug automation: turn on plug when print starts
  1660. try:
  1661. async with async_session() as db:
  1662. await smart_plug_manager.on_print_start(printer_id, db)
  1663. except Exception as e:
  1664. logger.warning("Smart plug on_print_start failed: %s", e)
  1665. async with async_session() as db:
  1666. from backend.app.models.printer import Printer
  1667. from backend.app.services.bambu_ftp import list_files_async
  1668. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1669. printer = result.scalar_one_or_none()
  1670. # Plate detection check - pause if objects detected on build plate
  1671. logger.info(
  1672. f"[PLATE CHECK] printer_id={printer_id}, plate_detection_enabled={printer.plate_detection_enabled if printer else 'NO PRINTER'}"
  1673. )
  1674. if printer and printer.plate_detection_enabled:
  1675. logger.info("[PLATE CHECK] ENTERING plate detection code for printer %s", printer_id)
  1676. try:
  1677. from backend.app.services.plate_detection import check_plate_empty
  1678. # Build ROI tuple from printer settings if available
  1679. roi = None
  1680. if all(
  1681. [
  1682. printer.plate_detection_roi_x is not None,
  1683. printer.plate_detection_roi_y is not None,
  1684. printer.plate_detection_roi_w is not None,
  1685. printer.plate_detection_roi_h is not None,
  1686. ]
  1687. ):
  1688. roi = (
  1689. printer.plate_detection_roi_x,
  1690. printer.plate_detection_roi_y,
  1691. printer.plate_detection_roi_w,
  1692. printer.plate_detection_roi_h,
  1693. )
  1694. # Auto-turn on chamber light if it's off for better detection
  1695. light_was_off = False
  1696. client = printer_manager.get_client(printer_id)
  1697. if client and client.state:
  1698. light_was_off = not client.state.chamber_light
  1699. if light_was_off:
  1700. logger.info("[PLATE CHECK] Turning on chamber light for printer %s", printer_id)
  1701. client.set_chamber_light(True)
  1702. # Wait for light to physically turn on and camera to adjust exposure
  1703. await asyncio.sleep(2.5)
  1704. logger.info("[PLATE CHECK] Running plate detection for printer %s", printer_id)
  1705. plate_result = await check_plate_empty(
  1706. printer_id=printer_id,
  1707. ip_address=printer.ip_address,
  1708. access_code=printer.access_code,
  1709. model=printer.model,
  1710. include_debug_image=False,
  1711. external_camera_url=printer.external_camera_url,
  1712. external_camera_type=printer.external_camera_type,
  1713. use_external=printer.external_camera_enabled,
  1714. roi=roi,
  1715. external_camera_snapshot_url=printer.external_camera_snapshot_url,
  1716. )
  1717. # Restore chamber light to original state
  1718. if light_was_off and client:
  1719. logger.info("[PLATE CHECK] Restoring chamber light to off for printer %s", printer_id)
  1720. client.set_chamber_light(False)
  1721. if not plate_result.needs_calibration and not plate_result.is_empty:
  1722. # Objects detected - pause the print!
  1723. logger.warning(
  1724. f"[PLATE CHECK] Objects detected on plate for printer {printer_id}! "
  1725. f"Confidence: {plate_result.confidence:.0%}, Diff: {plate_result.difference_percent:.1f}%"
  1726. )
  1727. client = printer_manager.get_client(printer_id)
  1728. if client:
  1729. client.pause_print()
  1730. logger.info("[PLATE CHECK] Print paused for printer %s", printer_id)
  1731. # Send notification about plate not empty
  1732. await ws_manager.broadcast(
  1733. {
  1734. "type": "plate_not_empty",
  1735. "printer_id": printer_id,
  1736. "printer_name": printer.name,
  1737. "message": f"Objects detected on build plate! Print paused. (Diff: {plate_result.difference_percent:.1f}%)",
  1738. }
  1739. )
  1740. # Also send push notification
  1741. try:
  1742. await notification_service.on_plate_not_empty(
  1743. printer_id=printer_id,
  1744. printer_name=printer.name,
  1745. db=db,
  1746. difference_percent=plate_result.difference_percent,
  1747. )
  1748. except Exception as notif_err:
  1749. logger.warning("[PLATE CHECK] Failed to send notification: %s", notif_err)
  1750. else:
  1751. logger.info("[PLATE CHECK] Plate is empty for printer %s, proceeding with print", printer_id)
  1752. except Exception as plate_err:
  1753. # Don't block print on plate detection errors
  1754. logger.warning("[PLATE CHECK] Plate detection failed for printer %s: %s", printer_id, plate_err)
  1755. if not printer:
  1756. logger.info("[CALLBACK] Skipping archive - printer not found in database")
  1757. if not notification_sent:
  1758. await _send_print_start_notification(printer_id, data, logger=logger)
  1759. return
  1760. if not printer.auto_archive:
  1761. # auto-archive disabled — check if there's an expected print (dispatched
  1762. # by BamBuddy via queue/reprint) that already has an archive to promote.
  1763. # If so, fall through to the expected-print handling below so the archive
  1764. # is tracked in _active_prints and usage tracking works at completion.
  1765. _fn = data.get("filename", "")
  1766. _sn = data.get("subtask_name", "")
  1767. _check_keys: list[tuple[int, str]] = []
  1768. if _sn:
  1769. _check_keys += [
  1770. (printer_id, _sn),
  1771. (printer_id, f"{_sn}.3mf"),
  1772. (printer_id, f"{_sn}.gcode.3mf"),
  1773. ]
  1774. if _fn:
  1775. _base_fn = _fn.split("/")[-1] if "/" in _fn else _fn
  1776. _check_keys.append((printer_id, _base_fn))
  1777. _no_archive_base = _base_fn.replace(".gcode", "").replace(".3mf", "")
  1778. _check_keys += [
  1779. (printer_id, _no_archive_base),
  1780. (printer_id, f"{_no_archive_base}.3mf"),
  1781. ]
  1782. _has_expected = any(k in _expected_prints for k in _check_keys)
  1783. if not _has_expected:
  1784. # No expected print — truly external print (started from slicer/touchscreen)
  1785. logger.info("[CALLBACK] Skipping archive - auto_archive: False, no expected print")
  1786. if not notification_sent:
  1787. _no_archive_creator: int | None = None
  1788. for _key in _check_keys:
  1789. _expected_prints.pop(_key, None)
  1790. _expected_print_registered_at.pop(_key, None)
  1791. popped_creator = _expected_print_creators.pop(_key, None)
  1792. if _no_archive_creator is None:
  1793. _no_archive_creator = popped_creator
  1794. _creator_data = {"created_by_id": _no_archive_creator} if _no_archive_creator else None
  1795. await _send_print_start_notification(printer_id, data, _creator_data, logger)
  1796. return
  1797. else:
  1798. logger.info("[CALLBACK] auto_archive disabled but expected print found — promoting archive")
  1799. # Get the filename and subtask_name
  1800. filename = data.get("filename", "")
  1801. subtask_name = data.get("subtask_name", "")
  1802. # MQTT subtask_id uniquely identifies a print job on the printer. When
  1803. # present, it lets us match an archive across a backend restart (#972):
  1804. # same id → same print → resume the existing row instead of cancelling
  1805. # it and recreating from scratch (which loses started_at). Treat "0"
  1806. # and "" as absent — Bambu reports "0" for non-cloud / local prints.
  1807. raw_mqtt = data.get("raw_data") or {}
  1808. subtask_id = raw_mqtt.get("subtask_id")
  1809. if subtask_id is not None:
  1810. subtask_id = str(subtask_id).strip()
  1811. if subtask_id in ("", "0"):
  1812. subtask_id = None
  1813. logger.info("[CALLBACK] Print start detected - filename: %s, subtask: %s", filename, subtask_name)
  1814. # Skip calibration prints — internal printer files should not be archived
  1815. # Bambu calibration gcode lives under /usr/ (e.g. /usr/etc/print/auto_cali_for_user.gcode)
  1816. if filename and filename.startswith("/usr/"):
  1817. logger.info("[CALLBACK] Skipping archive — internal printer file detected: %s", filename)
  1818. if not notification_sent:
  1819. await _send_print_start_notification(printer_id, data, logger=logger)
  1820. return
  1821. if not filename and not subtask_name:
  1822. # Send notification without archive data (no filename)
  1823. logger.info("[CALLBACK] Skipping archive - no filename or subtask_name")
  1824. if not notification_sent:
  1825. await _send_print_start_notification(printer_id, data, logger=logger)
  1826. return
  1827. # Check if this is an expected print from reprint/scheduled
  1828. # Build list of possible keys to check
  1829. expected_keys = []
  1830. if subtask_name:
  1831. expected_keys.append((printer_id, subtask_name))
  1832. expected_keys.append((printer_id, f"{subtask_name}.3mf"))
  1833. expected_keys.append((printer_id, f"{subtask_name}.gcode.3mf"))
  1834. if filename:
  1835. fname = filename.split("/")[-1] if "/" in filename else filename
  1836. expected_keys.append((printer_id, fname))
  1837. # Strip extensions to match
  1838. base = fname.replace(".gcode", "").replace(".3mf", "")
  1839. expected_keys.append((printer_id, base))
  1840. expected_keys.append((printer_id, f"{base}.3mf"))
  1841. expected_archive_id = None
  1842. for key in expected_keys:
  1843. expected_archive_id = _expected_prints.pop(key, None)
  1844. _expected_print_registered_at.pop(key, None)
  1845. if expected_archive_id:
  1846. # Clean up other possible keys for this print
  1847. for other_key in expected_keys:
  1848. _expected_prints.pop(other_key, None)
  1849. _expected_print_registered_at.pop(other_key, None)
  1850. break
  1851. if expected_archive_id:
  1852. # This is a reprint/scheduled print - use existing archive, don't create new one
  1853. logger.info("Using expected archive %s for print (skipping duplicate)", expected_archive_id)
  1854. from backend.app.models.archive import PrintArchive
  1855. result = await db.execute(select(PrintArchive).where(PrintArchive.id == expected_archive_id))
  1856. archive = result.scalar_one_or_none()
  1857. if archive:
  1858. # Update archive status to printing
  1859. archive.status = "printing"
  1860. archive.started_at = datetime.now(timezone.utc)
  1861. # Persist a restart-stable id so a later restart resumes this
  1862. # archive by subtask_id instead of name-matching + duplicating
  1863. # it (#1485). The printer often hasn't echoed subtask_id back
  1864. # this soon after dispatch, so fall back to the id Bambuddy
  1865. # minted when it sent the print command. Scoped to this
  1866. # expected-print branch on purpose: an expected match means
  1867. # Bambuddy dispatched this exact print in this process, so the
  1868. # client's last-dispatch id genuinely belongs to it — using it
  1869. # for an externally-started print could mis-tag the archive.
  1870. effective_subtask_id = subtask_id
  1871. if not effective_subtask_id:
  1872. _client = printer_manager.get_client(printer_id)
  1873. _dispatched = getattr(_client, "last_dispatch_subtask_id", None) if _client else None
  1874. if _dispatched:
  1875. effective_subtask_id = str(_dispatched).strip() or None
  1876. if effective_subtask_id and not archive.subtask_id:
  1877. archive.subtask_id = effective_subtask_id
  1878. # #1403 follow-up: VP-queue archives are created with
  1879. # printer_id=None at queue-add time (we don't know which
  1880. # printer will run the job yet). When the print actually
  1881. # starts on a specific printer the expected-archive lookup
  1882. # used to skip this assignment, leaving printer_id=None
  1883. # forever — which then disables the "Scan for timelapse"
  1884. # button in ArchivesPage (gated on !archive.printer_id).
  1885. if archive.printer_id != printer_id:
  1886. archive.printer_id = printer_id
  1887. await db.commit()
  1888. # Track as active print
  1889. _active_prints[(printer_id, archive.filename)] = archive.id
  1890. if subtask_name:
  1891. _active_prints[(printer_id, f"{subtask_name}.3mf")] = archive.id
  1892. # Start timelapse session if external camera is enabled (#1353).
  1893. # Queue / VP-dispatched prints land here in the expected-archive
  1894. # branch and used to skip start_session entirely — frames were
  1895. # never captured and the post-print stitch silently returned None.
  1896. _maybe_start_layer_timelapse(printer, printer_id, archive.id)
  1897. # Inject ams_mapping into usage tracker session — the session was created
  1898. # before expected-print promotion, so it may have ams_mapping=None when
  1899. # the MQTT request topic subscription failed (common on P1S/A1).
  1900. _stored_map = _print_ams_mappings.get(expected_archive_id)
  1901. if _stored_map:
  1902. try:
  1903. from backend.app.services.usage_tracker import _active_sessions
  1904. _ut_session = _active_sessions.get(printer_id)
  1905. if _ut_session and not _ut_session.ams_mapping:
  1906. _ut_session.ams_mapping = _stored_map
  1907. logger.info("[CALLBACK] Injected ams_mapping into usage tracker session: %s", _stored_map)
  1908. except Exception:
  1909. pass
  1910. # Set up energy tracking (#941: persist start on archive row)
  1911. await _record_energy_start(archive, printer_id, db, context="expected-print")
  1912. await ws_manager.send_archive_updated(
  1913. {
  1914. "id": archive.id,
  1915. "status": "printing",
  1916. }
  1917. )
  1918. # Send notification with archive data (reprint/scheduled)
  1919. if not notification_sent:
  1920. # Use archive's created_by_id; fall back to the creator registered via
  1921. # register_expected_print (handles library-file-based queue items where
  1922. # the freshly-created archive has no created_by_id yet).
  1923. # Pop ALL matching keys so no stale entries remain in the dict.
  1924. fallback_creator = None
  1925. for key in expected_keys:
  1926. popped = _expected_print_creators.pop(key, None)
  1927. if fallback_creator is None:
  1928. fallback_creator = popped
  1929. archive_data = {
  1930. "print_time_seconds": archive.print_time_seconds,
  1931. "created_by_id": archive.created_by_id or fallback_creator,
  1932. }
  1933. await _send_print_start_notification(printer_id, data, archive_data, logger)
  1934. # Extract printable objects from the archived 3MF file
  1935. _load_objects_from_archive(archive, printer_id, logger)
  1936. # Store Spoolman tracking data for per-filament usage reporting
  1937. try:
  1938. await _store_spoolman_print_data(
  1939. printer_id,
  1940. archive.id,
  1941. archive.file_path,
  1942. db,
  1943. printer_manager,
  1944. ams_mapping=_get_start_ams_mapping(data, archive.id),
  1945. )
  1946. except Exception as e:
  1947. logger.warning("[SPOOLMAN] Failed to store tracking data: %s", e)
  1948. # Capture timelapse file baseline for snapshot-diff on completion
  1949. # (mirrors the new-archive branch). Queue / VP-dispatched prints
  1950. # hit this branch — without the baseline the completion-time scan
  1951. # falls into its "take baseline now" fallback, which snapshots
  1952. # AFTER the new MP4 already exists and never matches a diff
  1953. # (#1403 follow-up — see pwostran's 2026-05-18 support bundle).
  1954. await _capture_timelapse_baseline_at_start(printer, printer_id, logger)
  1955. return # Skip creating a new archive
  1956. # Check if there's already a "printing" archive for this printer/file
  1957. # This prevents duplicates when backend restarts during an active print
  1958. from backend.app.models.archive import PrintArchive
  1959. existing_archive: PrintArchive | None = None
  1960. # Preferred match: subtask_id equality. MQTT reports the same subtask_id
  1961. # across a backend restart for the same print, so this is the most
  1962. # reliable way to reattach. We also accept a previously stale-cancelled
  1963. # archive here so users upgrading mid-print get revived when the row
  1964. # their earlier Bambuddy version wrongly cancelled reappears (#972).
  1965. if subtask_id:
  1966. by_id = await db.execute(
  1967. select(PrintArchive)
  1968. .where(PrintArchive.printer_id == printer_id)
  1969. .where(PrintArchive.subtask_id == subtask_id)
  1970. .where(PrintArchive.status.in_(["printing", "cancelled"]))
  1971. .order_by(PrintArchive.created_at.desc())
  1972. .limit(1)
  1973. )
  1974. candidate = by_id.scalar_one_or_none()
  1975. if candidate and (candidate.status == "printing" or (candidate.failure_reason or "").startswith("Stale")):
  1976. existing_archive = candidate
  1977. # Fallback match: name-based lookup. Kept as-is for prints whose
  1978. # subtask_id is missing ("0" / local / non-cloud prints).
  1979. if existing_archive is None:
  1980. check_name = subtask_name or filename.split("/")[-1].replace(".gcode", "").replace(".3mf", "")
  1981. existing = await db.execute(
  1982. select(PrintArchive)
  1983. .where(PrintArchive.printer_id == printer_id)
  1984. .where(PrintArchive.status == "printing")
  1985. .where(
  1986. or_(
  1987. PrintArchive.print_name == check_name,
  1988. PrintArchive.filename.in_(
  1989. [
  1990. f"{check_name}.3mf",
  1991. f"{check_name}.gcode.3mf",
  1992. ]
  1993. ),
  1994. )
  1995. )
  1996. .order_by(PrintArchive.created_at.desc())
  1997. .limit(1)
  1998. )
  1999. existing_archive = existing.scalar_one_or_none()
  2000. if existing_archive:
  2001. # subtask_id match → always resume, regardless of age. Same print,
  2002. # just a backend restart. Revive if it was previously stale-cancelled.
  2003. subtask_match = bool(subtask_id and existing_archive.subtask_id == subtask_id)
  2004. if subtask_match:
  2005. if existing_archive.status == "cancelled":
  2006. logger.warning(
  2007. "Reviving stale-cancelled archive %s — matching subtask_id %s confirms same print (#972)",
  2008. existing_archive.id,
  2009. subtask_id,
  2010. )
  2011. existing_archive.status = "printing"
  2012. existing_archive.failure_reason = None
  2013. await db.commit()
  2014. else:
  2015. logger.info("Resuming archive %s on subtask_id match (%s)", existing_archive.id, subtask_id)
  2016. _active_prints[(printer_id, existing_archive.filename)] = existing_archive.id
  2017. if existing_archive.energy_start_kwh is None:
  2018. await _record_energy_start(existing_archive, printer_id, db, context="subtask-resume")
  2019. if not notification_sent:
  2020. archive_data = {
  2021. "print_time_seconds": existing_archive.print_time_seconds,
  2022. "created_by_id": existing_archive.created_by_id,
  2023. }
  2024. await _send_print_start_notification(printer_id, data, archive_data, logger)
  2025. _load_objects_from_archive(existing_archive, printer_id, logger)
  2026. return
  2027. # Name-match only (no subtask_id to anchor on): decide resume vs.
  2028. # stale from the printer's *current* progress, not wall-clock age.
  2029. # A genuinely long print used to trip a blind 4h cutoff and have its
  2030. # live archive cancelled + duplicated on every backend restart
  2031. # (#1485). If the printer reports real progress, this name-matched
  2032. # 'printing' archive IS that ongoing print — resume it whatever its
  2033. # age. Only treat it as a stale leftover when the printer clearly
  2034. # shows a different, freshly-started print: near-0% progress on an
  2035. # archive far too old to still be at 0%. Unknown progress (printer
  2036. # not connected) never cancels — resuming is the safe default.
  2037. archive_age = datetime.now(timezone.utc) - existing_archive.created_at.replace(tzinfo=timezone.utc)
  2038. live_status = printer_manager.get_status(printer_id)
  2039. live_progress = getattr(live_status, "progress", None) if live_status else None
  2040. looks_stale = (
  2041. live_progress is not None and live_progress < 1.0 and archive_age.total_seconds() > 2 * 60 * 60
  2042. )
  2043. if looks_stale:
  2044. logger.warning(
  2045. f"Found stale 'printing' archive {existing_archive.id} (age: {archive_age}, "
  2046. f"printer progress {live_progress:.0f}%) — marking cancelled and creating new archive"
  2047. )
  2048. existing_archive.status = "cancelled"
  2049. existing_archive.failure_reason = "Stale - print likely cancelled or failed without status update"
  2050. await db.commit()
  2051. # Fall through to create new archive (don't return)
  2052. else:
  2053. logger.info(
  2054. f"Skipping duplicate - already have printing archive {existing_archive.id} for {check_name}"
  2055. )
  2056. # Track this as the active print
  2057. _active_prints[(printer_id, existing_archive.filename)] = existing_archive.id
  2058. # Attach subtask_id retroactively so future restarts can resume
  2059. if subtask_id and not existing_archive.subtask_id:
  2060. existing_archive.subtask_id = subtask_id
  2061. await db.commit()
  2062. # Also set up energy tracking if not already tracked (#941: persisted column)
  2063. if existing_archive.energy_start_kwh is None:
  2064. await _record_energy_start(existing_archive, printer_id, db, context="existing-printing")
  2065. # Send notification with archive data (existing archive)
  2066. if not notification_sent:
  2067. archive_data = {
  2068. "print_time_seconds": existing_archive.print_time_seconds,
  2069. "created_by_id": existing_archive.created_by_id,
  2070. }
  2071. await _send_print_start_notification(printer_id, data, archive_data, logger)
  2072. # Extract printable objects from the archived 3MF file
  2073. _load_objects_from_archive(existing_archive, printer_id, logger)
  2074. return
  2075. # Build list of possible 3MF filenames to try
  2076. possible_names = []
  2077. # Bambu printers typically store files as "Name.gcode.3mf"
  2078. # The subtask_name is usually the best source for the filename
  2079. if subtask_name:
  2080. # Try common Bambu naming patterns
  2081. possible_names.append(f"{subtask_name}.gcode.3mf")
  2082. possible_names.append(f"{subtask_name}.3mf")
  2083. # Try original filename with .3mf extension
  2084. if filename:
  2085. # Extract just the filename part, not the full path
  2086. fname = filename.split("/")[-1] if "/" in filename else filename
  2087. if fname.endswith(".3mf"):
  2088. possible_names.append(fname)
  2089. elif fname.endswith(".gcode"):
  2090. base = fname.rsplit(".", 1)[0]
  2091. possible_names.append(f"{base}.gcode.3mf")
  2092. possible_names.append(f"{base}.3mf")
  2093. else:
  2094. possible_names.append(f"{fname}.gcode.3mf")
  2095. possible_names.append(f"{fname}.3mf")
  2096. # Also try with spaces converted to underscores (Bambu Studio may normalize filenames)
  2097. space_variants = []
  2098. for name in possible_names:
  2099. if " " in name:
  2100. space_variants.append(name.replace(" ", "_"))
  2101. possible_names.extend(space_variants)
  2102. # Remove duplicates while preserving order
  2103. seen = set()
  2104. possible_names = [x for x in possible_names if not (x in seen or seen.add(x))]
  2105. logger.info("Trying filenames: %s", possible_names)
  2106. # Try to find and download the 3MF file
  2107. temp_path = None
  2108. downloaded_filename = None
  2109. # Cache check: cover endpoint may have already pulled this 3MF during
  2110. # the print (frontend opens the card and shows the thumbnail) — reuse
  2111. # that file instead of re-downloading 36MB over the same FTP link that
  2112. # just served it (#972). The cache keys on a normalized filename so
  2113. # variants like "X", "X.3mf", "X.gcode.3mf" all collapse to one entry.
  2114. for try_filename in possible_names:
  2115. if not try_filename.endswith(".3mf"):
  2116. continue
  2117. cached = get_cached_3mf(printer_id, try_filename)
  2118. if cached:
  2119. logger.info("Reusing cached 3MF from %s (avoided duplicate FTP)", cached)
  2120. temp_path = cached
  2121. downloaded_filename = try_filename
  2122. break
  2123. # Get FTP retry settings
  2124. ftp_retry_enabled, ftp_retry_count, ftp_retry_delay, ftp_timeout = await get_ftp_retry_settings()
  2125. for try_filename in possible_names if not downloaded_filename else []:
  2126. if not try_filename.endswith(".3mf"):
  2127. continue
  2128. # Root (/) is where BambuStudio/OrcaSlicer uploads land on A1/P1-series
  2129. # printers, so try it first — deferring it to last cost #972's reporter
  2130. # ~48 minutes of retries on /cache//model//data//data/Metadata before
  2131. # landing on the path that actually had the file.
  2132. remote_paths = [
  2133. f"/{try_filename}",
  2134. f"/cache/{try_filename}",
  2135. f"/model/{try_filename}",
  2136. f"/data/{try_filename}",
  2137. f"/data/Metadata/{try_filename}",
  2138. ]
  2139. temp_path = app_settings.archive_dir / "temp" / try_filename
  2140. temp_path.parent.mkdir(parents=True, exist_ok=True)
  2141. for remote_path in remote_paths:
  2142. logger.debug("Trying FTP download: %s", remote_path)
  2143. try:
  2144. if ftp_retry_enabled:
  2145. downloaded = await with_ftp_retry(
  2146. download_file_async,
  2147. printer.ip_address,
  2148. printer.access_code,
  2149. remote_path,
  2150. temp_path,
  2151. timeout=ftp_timeout,
  2152. socket_timeout=ftp_timeout,
  2153. printer_model=printer.model,
  2154. max_retries=ftp_retry_count,
  2155. retry_delay=ftp_retry_delay,
  2156. operation_name=f"Download 3MF from {remote_path}",
  2157. non_retry_exceptions=(FileNotOnPrinterError,),
  2158. )
  2159. else:
  2160. downloaded = await download_file_async(
  2161. printer.ip_address,
  2162. printer.access_code,
  2163. remote_path,
  2164. temp_path,
  2165. timeout=ftp_timeout,
  2166. socket_timeout=ftp_timeout,
  2167. printer_model=printer.model,
  2168. )
  2169. if downloaded:
  2170. downloaded_filename = try_filename
  2171. logger.info("Downloaded: %s", remote_path)
  2172. # Populate shared cache so the cover endpoint (if it
  2173. # runs next) doesn't refetch the same 36MB over FTP.
  2174. cache_3mf_download(printer_id, try_filename, temp_path)
  2175. break
  2176. except FileNotOnPrinterError:
  2177. # 550 — file isn't at this path. Advance to next candidate
  2178. # without burning the retry budget.
  2179. logger.debug("3MF not at %s (550), trying next path", remote_path)
  2180. except Exception as e:
  2181. logger.debug("FTP download failed for %s: %s", remote_path, e)
  2182. if downloaded_filename:
  2183. break
  2184. # If still not found, try listing directories to find matching file
  2185. # Different printer models use different directory structures
  2186. if not downloaded_filename and (filename or subtask_name):
  2187. search_term = (subtask_name or filename).lower().replace(".gcode", "").replace(".3mf", "")
  2188. logger.info("Direct FTP download failed, searching directories for '%s'", search_term)
  2189. search_dirs = ["/cache", "/model", "/data", "/data/Metadata", "/"]
  2190. for search_dir in search_dirs:
  2191. if downloaded_filename:
  2192. break
  2193. try:
  2194. dir_files = await list_files_async(
  2195. printer.ip_address, printer.access_code, search_dir, printer_model=printer.model
  2196. )
  2197. threemf_files = [f.get("name") for f in dir_files if f.get("name", "").endswith(".3mf")]
  2198. if threemf_files:
  2199. logger.info(
  2200. f"Found {len(threemf_files)} 3MF files in {search_dir}: {threemf_files[:5]}{'...' if len(threemf_files) > 5 else ''}"
  2201. )
  2202. for f in dir_files:
  2203. if f.get("is_directory"):
  2204. continue
  2205. fname = f.get("name", "")
  2206. # Normalize both for comparison (spaces and underscores are equivalent)
  2207. fname_normalized = fname.lower().replace(" ", "_")
  2208. search_normalized = search_term.replace(" ", "_")
  2209. if fname.endswith(".3mf") and search_normalized in fname_normalized:
  2210. logger.info("Found matching file in %s: %s", search_dir, fname)
  2211. temp_path = app_settings.archive_dir / "temp" / fname
  2212. temp_path.parent.mkdir(parents=True, exist_ok=True)
  2213. remote_full_path = posixpath.join(search_dir, fname)
  2214. if ftp_retry_enabled:
  2215. downloaded = await with_ftp_retry(
  2216. download_file_async,
  2217. printer.ip_address,
  2218. printer.access_code,
  2219. remote_full_path,
  2220. temp_path,
  2221. timeout=ftp_timeout,
  2222. socket_timeout=ftp_timeout,
  2223. printer_model=printer.model,
  2224. max_retries=ftp_retry_count,
  2225. retry_delay=ftp_retry_delay,
  2226. operation_name=f"Download 3MF from {remote_full_path}",
  2227. )
  2228. else:
  2229. downloaded = await download_file_async(
  2230. printer.ip_address,
  2231. printer.access_code,
  2232. remote_full_path,
  2233. temp_path,
  2234. timeout=ftp_timeout,
  2235. socket_timeout=ftp_timeout,
  2236. printer_model=printer.model,
  2237. )
  2238. if downloaded:
  2239. downloaded_filename = fname
  2240. logger.info("Found and downloaded from %s: %s", search_dir, fname)
  2241. cache_3mf_download(printer_id, fname, temp_path)
  2242. break
  2243. except Exception as e:
  2244. logger.debug("Failed to list %s: %s", search_dir, e)
  2245. # Validate the downloaded 3MF actually matches the plate that's running
  2246. # (#1204): subtask_name lags across consecutive plates of the same model,
  2247. # so the first FTP candidate (built from subtask_name) can land on the
  2248. # previous plate's still-resident upload. Cross-check the slice_info
  2249. # plate index against the plate parsed from gcode_file (always fresh —
  2250. # it's the field whose change triggered this callback).
  2251. if downloaded_filename and temp_path:
  2252. expected_plate = parse_plate_id(filename)
  2253. actual_plate = peek_plate_index_in_3mf(temp_path) if expected_plate is not None else None
  2254. if expected_plate is not None and actual_plate is not None and actual_plate != expected_plate:
  2255. logger.warning(
  2256. "[CALLBACK] 3MF plate mismatch: downloaded %s reports plate %s but printer is "
  2257. "running plate %s — subtask_name=%r appears stale, retrying with corrected name",
  2258. downloaded_filename,
  2259. actual_plate,
  2260. expected_plate,
  2261. subtask_name,
  2262. )
  2263. corrected_subtask = swap_plate_suffix(subtask_name, expected_plate)
  2264. retry_succeeded = False
  2265. if corrected_subtask and corrected_subtask != subtask_name:
  2266. for try_filename in (f"{corrected_subtask}.gcode.3mf", f"{corrected_subtask}.3mf"):
  2267. retry_temp_path = app_settings.archive_dir / "temp" / try_filename
  2268. retry_temp_path.parent.mkdir(parents=True, exist_ok=True)
  2269. for remote_path in (
  2270. f"/{try_filename}",
  2271. f"/cache/{try_filename}",
  2272. f"/model/{try_filename}",
  2273. f"/data/{try_filename}",
  2274. f"/data/Metadata/{try_filename}",
  2275. ):
  2276. try:
  2277. if ftp_retry_enabled:
  2278. downloaded = await with_ftp_retry(
  2279. download_file_async,
  2280. printer.ip_address,
  2281. printer.access_code,
  2282. remote_path,
  2283. retry_temp_path,
  2284. timeout=ftp_timeout,
  2285. socket_timeout=ftp_timeout,
  2286. printer_model=printer.model,
  2287. max_retries=ftp_retry_count,
  2288. retry_delay=ftp_retry_delay,
  2289. operation_name=f"Re-download 3MF from {remote_path}",
  2290. non_retry_exceptions=(FileNotOnPrinterError,),
  2291. )
  2292. else:
  2293. downloaded = await download_file_async(
  2294. printer.ip_address,
  2295. printer.access_code,
  2296. remote_path,
  2297. retry_temp_path,
  2298. timeout=ftp_timeout,
  2299. socket_timeout=ftp_timeout,
  2300. printer_model=printer.model,
  2301. )
  2302. if downloaded and peek_plate_index_in_3mf(retry_temp_path) == expected_plate:
  2303. logger.info(
  2304. "[CALLBACK] Re-download succeeded with corrected name %s "
  2305. "(plate %s) — replacing wrong file",
  2306. try_filename,
  2307. expected_plate,
  2308. )
  2309. try:
  2310. temp_path.unlink(missing_ok=True)
  2311. except OSError:
  2312. pass
  2313. temp_path = retry_temp_path
  2314. downloaded_filename = try_filename
  2315. subtask_name = corrected_subtask
  2316. cache_3mf_download(printer_id, try_filename, temp_path)
  2317. retry_succeeded = True
  2318. break
  2319. elif downloaded:
  2320. # Wrong plate again — discard and keep trying
  2321. try:
  2322. retry_temp_path.unlink(missing_ok=True)
  2323. except OSError:
  2324. pass
  2325. except FileNotOnPrinterError:
  2326. continue
  2327. except Exception as e:
  2328. logger.debug("Re-download failed for %s: %s", remote_path, e)
  2329. if retry_succeeded:
  2330. break
  2331. # If the retry didn't find a matching file, drop the wrong 3MF
  2332. # so the no-3MF fallback below creates an archive whose name
  2333. # at least reflects the right plate.
  2334. if not retry_succeeded:
  2335. logger.warning(
  2336. "[CALLBACK] Could not re-download correct plate %s — falling back to no-3MF archive",
  2337. expected_plate,
  2338. )
  2339. try:
  2340. temp_path.unlink(missing_ok=True)
  2341. except OSError:
  2342. pass
  2343. temp_path = None
  2344. downloaded_filename = None
  2345. # Override the stale subtask_name so the fallback archive's
  2346. # print_name reflects the correct plate. Prefer the swapped
  2347. # name when we have one; otherwise let filename win.
  2348. if corrected_subtask:
  2349. subtask_name = corrected_subtask
  2350. else:
  2351. subtask_name = ""
  2352. if not downloaded_filename or not temp_path:
  2353. logger.warning("Could not find 3MF file for print: %s", filename or subtask_name)
  2354. # Create a fallback archive without 3MF data so the print is still tracked
  2355. # This commonly happens with P1S/A1 printers where FTP has file size limitations
  2356. try:
  2357. from backend.app.models.archive import PrintArchive
  2358. # Derive print name from subtask_name or filename
  2359. print_name = subtask_name or filename
  2360. if print_name:
  2361. # Clean up the name (remove extensions, path parts)
  2362. print_name = print_name.split("/")[-1]
  2363. print_name = print_name.replace(".gcode.3mf", "").replace(".gcode", "").replace(".3mf", "")
  2364. else:
  2365. print_name = "Unknown Print"
  2366. # Recover estimated print time from MQTT (best-effort for notifications)
  2367. fallback_print_time = None
  2368. mqtt_remaining = data.get("remaining_time")
  2369. if mqtt_remaining and isinstance(mqtt_remaining, (int, float)) and mqtt_remaining > 0:
  2370. fallback_print_time = int(mqtt_remaining)
  2371. if fallback_print_time is None:
  2372. mc_remaining = (data.get("raw_data") or {}).get("mc_remaining_time")
  2373. if mc_remaining and isinstance(mc_remaining, (int, float)) and mc_remaining > 0:
  2374. fallback_print_time = int(mc_remaining * 60)
  2375. # Best-effort filament metadata from MQTT — see
  2376. # _extract_filament_data_from_mqtt. Without this the fallback
  2377. # archive's filament fields stayed NULL even though the AMS
  2378. # state at print start was sitting right there in `data`.
  2379. # The slicer's ams_mapping (when present) narrows the result
  2380. # to slots actually used by the print (#1533).
  2381. mqtt_filament_meta = _extract_filament_data_from_mqtt(data, _get_start_ams_mapping(data, None))
  2382. # Create minimal archive entry
  2383. fallback_archive = PrintArchive(
  2384. printer_id=printer_id,
  2385. filename=filename or f"{print_name}.3mf",
  2386. file_path="", # Empty - no 3MF file available
  2387. file_size=0,
  2388. print_name=print_name,
  2389. print_time_seconds=fallback_print_time,
  2390. status="printing",
  2391. started_at=datetime.now(timezone.utc),
  2392. subtask_id=subtask_id,
  2393. filament_type=mqtt_filament_meta.get("filament_type"),
  2394. filament_color=mqtt_filament_meta.get("filament_color"),
  2395. extra_data={"no_3mf_available": True, "original_subtask": subtask_name, "_print_data": data},
  2396. )
  2397. db.add(fallback_archive)
  2398. await db.commit()
  2399. await db.refresh(fallback_archive)
  2400. logger.info("Created fallback archive %s for %s (no 3MF available)", fallback_archive.id, print_name)
  2401. _maybe_start_layer_timelapse(printer, printer_id, fallback_archive.id)
  2402. # Track as active print
  2403. _active_prints[(printer_id, fallback_archive.filename)] = fallback_archive.id
  2404. if filename:
  2405. _active_prints[(printer_id, filename)] = fallback_archive.id
  2406. if subtask_name:
  2407. _active_prints[(printer_id, f"{subtask_name}.3mf")] = fallback_archive.id
  2408. _active_prints[(printer_id, subtask_name)] = fallback_archive.id
  2409. # Record starting energy if smart plug available (#941: persisted column)
  2410. await _record_energy_start(fallback_archive, printer_id, db, context="fallback")
  2411. # Send WebSocket notification
  2412. await ws_manager.send_archive_created(
  2413. {
  2414. "id": fallback_archive.id,
  2415. "printer_id": fallback_archive.printer_id,
  2416. "filename": fallback_archive.filename,
  2417. "print_name": fallback_archive.print_name,
  2418. "status": fallback_archive.status,
  2419. }
  2420. )
  2421. # MQTT relay - publish archive created
  2422. try:
  2423. await mqtt_relay.on_archive_created(
  2424. archive_id=fallback_archive.id,
  2425. print_name=fallback_archive.print_name,
  2426. printer_name=printer.name,
  2427. status=fallback_archive.status,
  2428. )
  2429. except Exception:
  2430. pass # Don't fail if MQTT fails
  2431. # Store Spoolman tracking data (may not work for fallback since no 3MF)
  2432. try:
  2433. await _store_spoolman_print_data(
  2434. printer_id,
  2435. fallback_archive.id,
  2436. fallback_archive.file_path,
  2437. db,
  2438. printer_manager,
  2439. ams_mapping=_get_start_ams_mapping(data, fallback_archive.id),
  2440. )
  2441. except Exception as e:
  2442. logger.debug("[SPOOLMAN] Could not store tracking for fallback archive: %s", e)
  2443. # Send notification without archive data (file not found)
  2444. if not notification_sent:
  2445. await _send_print_start_notification(printer_id, data, logger=logger)
  2446. return
  2447. except Exception as e:
  2448. logger.error("Failed to create fallback archive: %s", e)
  2449. # Send notification without archive data (file not found)
  2450. if not notification_sent:
  2451. await _send_print_start_notification(printer_id, data, logger=logger)
  2452. return
  2453. try:
  2454. # Archive the file with status "printing"
  2455. service = ArchiveService(db)
  2456. archive = await service.archive_print(
  2457. printer_id=printer_id,
  2458. source_file=temp_path,
  2459. print_data={**data, "status": "printing"},
  2460. subtask_id=subtask_id,
  2461. )
  2462. if archive:
  2463. # Track this active print (use both original filename and downloaded filename)
  2464. _active_prints[(printer_id, downloaded_filename)] = archive.id
  2465. if filename and filename != downloaded_filename:
  2466. _active_prints[(printer_id, filename)] = archive.id
  2467. if subtask_name:
  2468. _active_prints[(printer_id, f"{subtask_name}.3mf")] = archive.id
  2469. logger.info("Created archive %s for %s", archive.id, downloaded_filename)
  2470. _maybe_start_layer_timelapse(printer, printer_id, archive.id)
  2471. # Record starting energy from smart plug if available (#941: persisted column)
  2472. await _record_energy_start(archive, printer_id, db, context="auto-archive")
  2473. await ws_manager.send_archive_created(
  2474. {
  2475. "id": archive.id,
  2476. "printer_id": archive.printer_id,
  2477. "filename": archive.filename,
  2478. "print_name": archive.print_name,
  2479. "status": archive.status,
  2480. }
  2481. )
  2482. # MQTT relay - publish archive created
  2483. try:
  2484. await mqtt_relay.on_archive_created(
  2485. archive_id=archive.id,
  2486. print_name=archive.print_name,
  2487. printer_name=printer.name,
  2488. status=archive.status,
  2489. )
  2490. except Exception:
  2491. pass # Don't fail if MQTT fails
  2492. # Send notification with archive data (new archive created)
  2493. if not notification_sent:
  2494. archive_data = {
  2495. "print_time_seconds": archive.print_time_seconds,
  2496. "created_by_id": archive.created_by_id,
  2497. }
  2498. await _send_print_start_notification(printer_id, data, archive_data, logger)
  2499. # Extract printable objects for skip object functionality
  2500. try:
  2501. from backend.app.services.archive import extract_printable_objects_from_3mf
  2502. with open(temp_path, "rb") as f:
  2503. threemf_data = f.read()
  2504. # Extract with positions for UI overlay
  2505. printable_objects, bbox_all = extract_printable_objects_from_3mf(
  2506. threemf_data, include_positions=True
  2507. )
  2508. if printable_objects:
  2509. # Store objects in printer state
  2510. client = printer_manager.get_client(printer_id)
  2511. if client:
  2512. client.state.printable_objects = printable_objects
  2513. client.state.printable_objects_bbox_all = bbox_all
  2514. client.state.skipped_objects = [] # Reset skipped objects for new print
  2515. logger.info(
  2516. "Loaded %s printable objects for printer %s", len(printable_objects), printer_id
  2517. )
  2518. except Exception as e:
  2519. logger.debug("Failed to extract printable objects: %s", e)
  2520. # Store Spoolman tracking data for per-filament usage reporting
  2521. try:
  2522. await _store_spoolman_print_data(
  2523. printer_id,
  2524. archive.id,
  2525. archive.file_path,
  2526. db,
  2527. printer_manager,
  2528. ams_mapping=_get_start_ams_mapping(data, archive.id),
  2529. )
  2530. except Exception as e:
  2531. logger.warning("[SPOOLMAN] Failed to store tracking data: %s", e)
  2532. # Capture timelapse file baseline for snapshot-diff on completion
  2533. await _capture_timelapse_baseline_at_start(printer, printer_id, logger)
  2534. finally:
  2535. # Keep temp_path around until print completes so the cover endpoint
  2536. # can reuse it (#972). Cache eviction in on_print_complete deletes
  2537. # the file. If the cache entry was evicted early (file vanished),
  2538. # clean up any stragglers here to avoid leaking disk on retries.
  2539. cached_now = get_cached_3mf(printer_id, downloaded_filename) if downloaded_filename else None
  2540. if temp_path and temp_path.exists() and cached_now != temp_path:
  2541. temp_path.unlink()
  2542. _TIMELAPSE_VIDEO_EXTENSIONS = (".mp4", ".avi")
  2543. async def _list_timelapse_videos(printer) -> tuple[list[dict], str | None]:
  2544. """List video files from printer's timelapse directory.
  2545. Finds MP4 (X1/A1 series) and AVI (P1 series) timelapse files.
  2546. Returns (video_files, found_path) where video_files is a list of file dicts
  2547. and found_path is the directory where they were found, or ([], None).
  2548. """
  2549. from backend.app.services.bambu_ftp import list_files_async
  2550. logger = logging.getLogger(__name__)
  2551. for timelapse_path in ["/timelapse", "/timelapse/video", "/record", "/recording"]:
  2552. try:
  2553. found_files = await list_files_async(
  2554. printer.ip_address, printer.access_code, timelapse_path, printer_model=printer.model
  2555. )
  2556. if found_files:
  2557. video_files = [
  2558. f
  2559. for f in found_files
  2560. if not f.get("is_directory") and f.get("name", "").lower().endswith(_TIMELAPSE_VIDEO_EXTENSIONS)
  2561. ]
  2562. if video_files:
  2563. return video_files, timelapse_path
  2564. except Exception as e:
  2565. logger.debug("[TIMELAPSE] Path %s failed: %s", timelapse_path, e)
  2566. continue
  2567. return [], None
  2568. async def _capture_timelapse_baseline_at_start(printer, printer_id: int, logger: logging.Logger) -> None:
  2569. """Snapshot the printer's timelapse directory at print start so the
  2570. completion-time scan can pick the new file by set-difference.
  2571. Must be called from every on_print_start path that proceeds to a real
  2572. print — both the new-archive branch and the expected-archive branch (which
  2573. queue / VP-dispatched prints take). Without a baseline,
  2574. _scan_for_timelapse_with_retries falls into its "take baseline now"
  2575. fallback that runs AFTER the new MP4 has already landed on the SD card,
  2576. so the new file ends up in the "baseline" set and no diff ever matches.
  2577. Bambu printers in LAN-only mode don't sync NTP, so mtime ordering is
  2578. unreliable — the snapshot-diff approach sidesteps that entirely.
  2579. """
  2580. try:
  2581. baseline_files, _ = await _list_timelapse_videos(printer)
  2582. _timelapse_baselines[printer_id] = {f.get("name", "") for f in baseline_files}
  2583. logger.info(
  2584. "[TIMELAPSE] Baseline at print start: %s video files for printer %s",
  2585. len(_timelapse_baselines[printer_id]),
  2586. printer_id,
  2587. )
  2588. except Exception as e:
  2589. logger.warning("[TIMELAPSE] Failed to capture baseline at print start: %s", e)
  2590. async def _scan_for_timelapse_with_retries(archive_id: int, baseline_names: set[str] | None = None):
  2591. """
  2592. Scan for timelapse with retries using a snapshot-diff approach.
  2593. Instead of picking the "most recent by mtime" (unreliable when the printer
  2594. clock is wrong in LAN-only mode), we snapshot existing MP4 filenames BEFORE
  2595. waiting, then look for any NEW filename that appears after each delay.
  2596. If baseline_names is provided (captured at print start), it is used directly.
  2597. Otherwise falls back to taking a baseline at completion time (best-effort
  2598. for prints started before app restart).
  2599. Falls back to name-matching (print name contained in MP4 filename) if no
  2600. new file appears after all retries.
  2601. """
  2602. from pathlib import Path
  2603. logger = logging.getLogger(__name__)
  2604. # --- Phase 1: Take baseline snapshot of existing timelapse files ---
  2605. try:
  2606. async with async_session() as db:
  2607. from backend.app.models.printer import Printer
  2608. service = ArchiveService(db)
  2609. archive = await service.get_archive(archive_id)
  2610. if not archive:
  2611. logger.warning("[TIMELAPSE] Archive %s not found, aborting", archive_id)
  2612. return
  2613. if archive.timelapse_path:
  2614. logger.info("[TIMELAPSE] Archive %s already has timelapse attached", archive_id)
  2615. return
  2616. if not archive.printer_id:
  2617. logger.warning("[TIMELAPSE] Archive %s has no printer, aborting", archive_id)
  2618. return
  2619. if baseline_names is not None:
  2620. # Use pre-captured baseline from print start (no race condition)
  2621. logger.info(
  2622. "[TIMELAPSE] Using print-start baseline: %s existing video files for archive %s",
  2623. len(baseline_names),
  2624. archive_id,
  2625. )
  2626. else:
  2627. # Fallback: take baseline now (e.g. app restarted mid-print)
  2628. result = await db.execute(select(Printer).where(Printer.id == archive.printer_id))
  2629. printer = result.scalar_one_or_none()
  2630. if not printer:
  2631. logger.warning("[TIMELAPSE] Printer not found for archive %s, aborting", archive_id)
  2632. return
  2633. baseline_files, _ = await _list_timelapse_videos(printer)
  2634. baseline_names = {f.get("name", "") for f in baseline_files}
  2635. logger.info(
  2636. "[TIMELAPSE] Baseline snapshot (fallback): %s existing video files for archive %s",
  2637. len(baseline_names),
  2638. archive_id,
  2639. )
  2640. # Derive base_name for name-matching fallback
  2641. base_name = Path(archive.filename).stem if archive.filename else ""
  2642. if base_name.endswith(".gcode"):
  2643. base_name = base_name[:-6]
  2644. except Exception as e:
  2645. logger.warning("[TIMELAPSE] Failed to take baseline snapshot for archive %s: %s", archive_id, e)
  2646. return
  2647. # --- Phase 2: Retry loop — look for NEW files that weren't in baseline ---
  2648. retry_delays = [5, 10, 20, 30]
  2649. for attempt, delay in enumerate(retry_delays, 1):
  2650. logger.info(
  2651. "[TIMELAPSE] Attempt %s/%s: waiting %ss before scanning for archive %s",
  2652. attempt,
  2653. len(retry_delays),
  2654. delay,
  2655. archive_id,
  2656. )
  2657. await asyncio.sleep(delay)
  2658. try:
  2659. async with async_session() as db:
  2660. from backend.app.models.printer import Printer
  2661. from backend.app.services.bambu_ftp import download_file_bytes_async
  2662. service = ArchiveService(db)
  2663. archive = await service.get_archive(archive_id)
  2664. if not archive:
  2665. logger.warning("[TIMELAPSE] Archive %s not found, stopping retries", archive_id)
  2666. return
  2667. if archive.timelapse_path:
  2668. logger.info("[TIMELAPSE] Archive %s already has timelapse attached, stopping retries", archive_id)
  2669. return
  2670. result = await db.execute(select(Printer).where(Printer.id == archive.printer_id))
  2671. printer = result.scalar_one_or_none()
  2672. if not printer:
  2673. logger.warning("[TIMELAPSE] Printer not found for archive %s, stopping retries", archive_id)
  2674. return
  2675. video_files, found_path = await _list_timelapse_videos(printer)
  2676. if not video_files:
  2677. logger.info("[TIMELAPSE] Attempt %s: No video files found, will retry", attempt)
  2678. continue
  2679. logger.info("[TIMELAPSE] Attempt %s: Found %s video files in %s", attempt, len(video_files), found_path)
  2680. for f in video_files[:5]:
  2681. logger.info("[TIMELAPSE] - %s", f.get("name"))
  2682. # Find files that are NEW (not in baseline snapshot)
  2683. new_files = [f for f in video_files if f.get("name", "") not in baseline_names]
  2684. if new_files:
  2685. # Pick the first new file (there should typically be exactly one)
  2686. target = new_files[0]
  2687. file_name = target.get("name")
  2688. remote_path = target.get("path") or f"/timelapse/{file_name}"
  2689. logger.info(
  2690. "[TIMELAPSE] Attempt %s: New file detected: %s (downloading for archive %s)",
  2691. attempt,
  2692. file_name,
  2693. archive_id,
  2694. )
  2695. timelapse_data = await download_file_bytes_async(
  2696. printer.ip_address, printer.access_code, remote_path, printer_model=printer.model
  2697. )
  2698. if timelapse_data:
  2699. success = await service.attach_timelapse(archive_id, timelapse_data, file_name)
  2700. if success:
  2701. logger.info("[TIMELAPSE] Successfully attached timelapse to archive %s", archive_id)
  2702. await ws_manager.send_archive_updated({"id": archive_id, "timelapse_attached": True})
  2703. return
  2704. else:
  2705. logger.warning("[TIMELAPSE] Failed to attach timelapse to archive %s", archive_id)
  2706. else:
  2707. logger.warning("[TIMELAPSE] Attempt %s: Failed to download new file, will retry", attempt)
  2708. else:
  2709. logger.info("[TIMELAPSE] Attempt %s: No new files since baseline, will retry", attempt)
  2710. except Exception as e:
  2711. logger.warning("[TIMELAPSE] Attempt %s failed with error: %s", attempt, e)
  2712. # --- Phase 3: Fallback — try name matching against all files ---
  2713. if base_name:
  2714. logger.info("[TIMELAPSE] Retries exhausted, trying name-match fallback for '%s'", base_name)
  2715. try:
  2716. async with async_session() as db:
  2717. from backend.app.models.printer import Printer
  2718. from backend.app.services.bambu_ftp import download_file_bytes_async
  2719. service = ArchiveService(db)
  2720. archive = await service.get_archive(archive_id)
  2721. if not archive or archive.timelapse_path:
  2722. return
  2723. result = await db.execute(select(Printer).where(Printer.id == archive.printer_id))
  2724. printer = result.scalar_one_or_none()
  2725. if not printer:
  2726. return
  2727. video_files, found_path = await _list_timelapse_videos(printer)
  2728. for f in video_files:
  2729. fname = f.get("name", "")
  2730. if base_name.lower() in fname.lower():
  2731. remote_path = f.get("path") or f"/timelapse/{fname}"
  2732. logger.info("[TIMELAPSE] Name-match fallback: '%s' matches '%s'", base_name, fname)
  2733. timelapse_data = await download_file_bytes_async(
  2734. printer.ip_address, printer.access_code, remote_path, printer_model=printer.model
  2735. )
  2736. if timelapse_data:
  2737. success = await service.attach_timelapse(archive_id, timelapse_data, fname)
  2738. if success:
  2739. logger.info(
  2740. "[TIMELAPSE] Name-match fallback attached timelapse to archive %s", archive_id
  2741. )
  2742. await ws_manager.send_archive_updated({"id": archive_id, "timelapse_attached": True})
  2743. return
  2744. break # Only try the first name match
  2745. except Exception as e:
  2746. logger.warning("[TIMELAPSE] Name-match fallback failed: %s", e)
  2747. logger.warning("[TIMELAPSE] All attempts exhausted for archive %s, giving up", archive_id)
  2748. async def on_print_running_observed(printer_id: int, data: dict):
  2749. """Restart-recovery: capture a fresh timelapse baseline for a print that
  2750. started before Bambuddy came up.
  2751. bambu_mqtt.py suppresses ``on_print_start`` on the first RUNNING push
  2752. after Bambuddy startup (#1304 guard, prevents duplicate archive
  2753. creation). Without that path, ``_capture_timelapse_baseline_at_start``
  2754. never runs and ``_scan_for_timelapse_with_retries`` falls into its
  2755. "take baseline now" fallback at completion time — but by then the
  2756. printer has already uploaded the in-flight MP4, so the baseline
  2757. includes it and no diff ever matches (#1485 follow-up).
  2758. Fires once per session, in lieu of on_print_start when restart-recovery
  2759. kicks in. The printer doesn't upload the timelapse until after PRINT
  2760. COMPLETE, so a baseline captured any time during the print is still
  2761. pre-upload.
  2762. """
  2763. logger = logging.getLogger(__name__)
  2764. # Avoid double-capture: on_print_start may have run earlier in this
  2765. # Bambuddy process if the print started AFTER startup and we crashed
  2766. # later in the same session. (Realistically this can't happen — the
  2767. # MQTT client object would have been recreated — but the cheap guard
  2768. # is correct regardless.)
  2769. if printer_id in _timelapse_baselines:
  2770. logger.debug(
  2771. "[TIMELAPSE] on_print_running_observed: baseline already present for printer %s, skipping",
  2772. printer_id,
  2773. )
  2774. return
  2775. async with async_session() as db:
  2776. from backend.app.models.printer import Printer
  2777. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  2778. printer = result.scalar_one_or_none()
  2779. if not printer:
  2780. logger.warning(
  2781. "[TIMELAPSE] on_print_running_observed: printer %s not found in DB, skipping baseline",
  2782. printer_id,
  2783. )
  2784. return
  2785. await _capture_timelapse_baseline_at_start(printer, printer_id, logger)
  2786. async def on_print_complete(printer_id: int, data: dict):
  2787. """Handle print completion - update the archive status."""
  2788. import time
  2789. logger = logging.getLogger(__name__)
  2790. start_time = time.time()
  2791. def log_timing(section: str):
  2792. elapsed = time.time() - start_time
  2793. logger.info("[TIMING] %s: %.3fs elapsed", section, elapsed)
  2794. logger.info("[CALLBACK] on_print_complete started for printer %s", printer_id)
  2795. # Drop the 3MF download cache for this printer (#972). The print is over,
  2796. # nothing else legitimately needs the bytes; keeping them would only risk
  2797. # handing a stale file to the next print if it reuses the same name.
  2798. clear_3mf_cache(printer_id)
  2799. try:
  2800. ws_data = {
  2801. "status": data.get("status"),
  2802. "filename": data.get("filename"),
  2803. "subtask_name": data.get("subtask_name"),
  2804. "timelapse_was_active": data.get("timelapse_was_active"),
  2805. }
  2806. await ws_manager.send_print_complete(printer_id, ws_data)
  2807. log_timing("WebSocket send_print_complete")
  2808. except Exception as e:
  2809. logger.warning("[CALLBACK] WebSocket send_print_complete failed: %s", e)
  2810. # Capture user info before clearing (needed for print log entry)
  2811. _print_user_info = printer_manager.get_current_print_user(printer_id)
  2812. # Clear current print user tracking (Issue #206)
  2813. printer_manager.clear_current_print_user(printer_id)
  2814. # If the user explicitly stopped this print from the queue UI the printer will
  2815. # report "failed" or "aborted" via MQTT. Override that to "cancelled" so the
  2816. # correct "print stopped" notification/email is sent instead of a failure alert.
  2817. _raw_status = data.get("status", "completed")
  2818. if printer_id in _user_stopped_printers and _raw_status in ("failed", "aborted"):
  2819. logger.info(
  2820. "[CALLBACK] Overriding status '%s' -> 'cancelled' for printer %s (print was stopped from queue by user)",
  2821. _raw_status,
  2822. printer_id,
  2823. )
  2824. data = {**data, "status": "cancelled"}
  2825. _user_stopped_printers.discard(printer_id)
  2826. # Raise the plate-clear gate for queued dispatch (#961). Any terminal status
  2827. # may have left material on the bed: a user can cancel ten hours into a
  2828. # twelve-hour print, a printer can self-abort mid-job after a clog, and a
  2829. # touchscreen-stop reports `aborted` rather than `cancelled` because
  2830. # `_user_stopped_printers` is only populated when the user stops via the
  2831. # Bambuddy queue UI. Earlier code raised the flag only for completed/failed,
  2832. # which auto-dispatched the next queued print onto a fouled bed two seconds
  2833. # after a touchscreen-abort (#1171). Persisted to DB so the gate survives
  2834. # Auto Off power cycles and Bambuddy restarts.
  2835. _final_status = data.get("status", "completed")
  2836. if _final_status in ("completed", "failed", "aborted", "cancelled"):
  2837. printer_manager.set_awaiting_plate_clear(printer_id, True)
  2838. # MQTT relay - publish print complete
  2839. try:
  2840. printer_info = printer_manager.get_printer(printer_id)
  2841. if printer_info:
  2842. await mqtt_relay.on_print_complete(
  2843. printer_id,
  2844. printer_info.name,
  2845. printer_info.serial_number,
  2846. data.get("filename", ""),
  2847. data.get("subtask_name", ""),
  2848. data.get("status", "completed"),
  2849. )
  2850. except Exception:
  2851. pass # Don't fail print complete callback if MQTT fails
  2852. filename = data.get("filename", "")
  2853. subtask_name = data.get("subtask_name", "")
  2854. if not filename and not subtask_name:
  2855. logger.warning("Print complete without filename or subtask_name")
  2856. return
  2857. logger.info("Print complete - filename: %s, subtask: %s, status: %s", filename, subtask_name, data.get("status"))
  2858. # Build list of possible keys to try (matching how they were registered in on_print_start)
  2859. possible_keys = []
  2860. # Try subtask_name variations first (most reliable for matching)
  2861. if subtask_name:
  2862. possible_keys.append((printer_id, f"{subtask_name}.3mf"))
  2863. possible_keys.append((printer_id, f"{subtask_name}.gcode.3mf"))
  2864. possible_keys.append((printer_id, subtask_name))
  2865. # Try filename variations
  2866. if filename:
  2867. # Extract just the filename if it's a path
  2868. fname = filename.split("/")[-1] if "/" in filename else filename
  2869. if fname.endswith(".3mf"):
  2870. possible_keys.append((printer_id, fname))
  2871. elif fname.endswith(".gcode"):
  2872. base_name = fname.rsplit(".", 1)[0]
  2873. possible_keys.append((printer_id, f"{base_name}.gcode.3mf"))
  2874. possible_keys.append((printer_id, f"{base_name}.3mf"))
  2875. possible_keys.append((printer_id, fname))
  2876. else:
  2877. possible_keys.append((printer_id, f"{fname}.gcode.3mf"))
  2878. possible_keys.append((printer_id, f"{fname}.3mf"))
  2879. possible_keys.append((printer_id, fname))
  2880. # Also try full path versions
  2881. if filename.endswith(".3mf"):
  2882. possible_keys.append((printer_id, filename))
  2883. elif filename.endswith(".gcode"):
  2884. base_name = filename.rsplit(".", 1)[0]
  2885. possible_keys.append((printer_id, f"{base_name}.3mf"))
  2886. possible_keys.append((printer_id, filename))
  2887. else:
  2888. possible_keys.append((printer_id, f"{filename}.3mf"))
  2889. possible_keys.append((printer_id, filename))
  2890. # Find the archive for this print
  2891. logger.info("Looking for archive in _active_prints, keys to try: %s...", possible_keys[:5])
  2892. logger.info("Current _active_prints: %s", list(_active_prints.keys()))
  2893. archive_id = None
  2894. for key in possible_keys:
  2895. archive_id = _active_prints.pop(key, None)
  2896. if archive_id:
  2897. logger.info("Found archive %s with key %s", archive_id, key)
  2898. # Also clean up any other keys pointing to this archive
  2899. keys_to_remove = [k for k, v in _active_prints.items() if v == archive_id]
  2900. for k in keys_to_remove:
  2901. _active_prints.pop(k, None)
  2902. break
  2903. if not archive_id:
  2904. # Try to find by filename or subtask_name if not tracked (for prints started before app)
  2905. async with async_session() as db:
  2906. from backend.app.models.archive import PrintArchive
  2907. # Try matching by subtask_name (stored as print_name) first
  2908. if subtask_name:
  2909. result = await db.execute(
  2910. select(PrintArchive)
  2911. .where(PrintArchive.printer_id == printer_id)
  2912. .where(PrintArchive.status == "printing")
  2913. .where(
  2914. or_(
  2915. PrintArchive.print_name.ilike(f"%{subtask_name}%"),
  2916. PrintArchive.filename.ilike(f"%{subtask_name}%"),
  2917. )
  2918. )
  2919. .order_by(PrintArchive.created_at.desc())
  2920. .limit(1)
  2921. )
  2922. archive = result.scalar_one_or_none()
  2923. if archive:
  2924. archive_id = archive.id
  2925. logger.info("Found archive %s by subtask_name match: %s", archive_id, subtask_name)
  2926. # Also try by filename
  2927. if not archive_id and filename:
  2928. result = await db.execute(
  2929. select(PrintArchive)
  2930. .where(PrintArchive.printer_id == printer_id)
  2931. .where(PrintArchive.filename == filename)
  2932. .where(PrintArchive.status == "printing")
  2933. .order_by(PrintArchive.created_at.desc())
  2934. .limit(1)
  2935. )
  2936. archive = result.scalar_one_or_none()
  2937. if archive:
  2938. archive_id = archive.id
  2939. # Cleanup: delete uploaded file from printer SD card to prevent phantom prints (Issue #374)
  2940. # The print scheduler uploads files to the SD card root (/). Some printers (e.g. P1S)
  2941. # auto-start files found in root on power cycle, causing ghost prints.
  2942. # Must run before the archive_id early-return so it executes even when archiving is disabled.
  2943. try:
  2944. if subtask_name:
  2945. async with async_session() as db:
  2946. from backend.app.models.printer import Printer
  2947. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  2948. printer = result.scalar_one_or_none()
  2949. if printer:
  2950. from backend.app.services.bambu_ftp import delete_file_async
  2951. # Try both .3mf and .gcode extensions — the printer may have either
  2952. for ext in (".3mf", ".gcode"):
  2953. remote_path = f"/{subtask_name}{ext}"
  2954. # Retry up to 3 times — the printer may still lock the filesystem briefly after a print ends
  2955. for attempt in range(1, 4):
  2956. try:
  2957. delete_result = await delete_file_async(
  2958. printer.ip_address,
  2959. printer.access_code,
  2960. remote_path,
  2961. printer_model=printer.model,
  2962. )
  2963. if delete_result:
  2964. logger.info("Deleted %s from printer %s SD card", remote_path, printer.name)
  2965. break
  2966. except Exception as e:
  2967. delete_result = False
  2968. logger.warning(
  2969. "SD card cleanup attempt %d/3 raised for %s: %s",
  2970. attempt,
  2971. remote_path,
  2972. e,
  2973. )
  2974. if not delete_result and attempt < 3:
  2975. await asyncio.sleep(2)
  2976. elif not delete_result:
  2977. logger.warning(
  2978. "SD card cleanup failed after 3 attempts for %s (file may linger on SD card)",
  2979. remote_path,
  2980. )
  2981. except Exception as e:
  2982. logger.warning("SD card file cleanup failed for printer %s: %s", printer_id, e)
  2983. log_timing("SD card cleanup")
  2984. # Update queue item status early — must run before the archive_id early-return
  2985. # so queue items don't get stuck in "printing" when archive lookup fails.
  2986. # Uses run_with_retry to handle SQLite "database is locked" errors (#897).
  2987. queue_item_id = None
  2988. queue_status = None
  2989. queue_auto_off = False
  2990. try:
  2991. from backend.app.core.database import run_with_retry
  2992. from backend.app.models.print_queue import PrintQueueItem
  2993. async def _update_queue_status(db):
  2994. nonlocal queue_item_id, queue_status, queue_auto_off
  2995. result = await db.execute(
  2996. select(PrintQueueItem)
  2997. .where(PrintQueueItem.printer_id == printer_id)
  2998. .where(PrintQueueItem.status == "printing")
  2999. )
  3000. printing_items = list(result.scalars().all())
  3001. if len(printing_items) > 1:
  3002. logger.warning(
  3003. "BUG: Multiple queue items in 'printing' status for printer %s: %s",
  3004. printer_id,
  3005. [(i.id, i.archive_id, i.library_file_id) for i in printing_items],
  3006. )
  3007. item = printing_items[0] if printing_items else None
  3008. if item:
  3009. queue_status = data.get("status", "completed")
  3010. # MQTT sends "aborted" for cancelled prints; normalise to
  3011. # "cancelled" so it matches the queue schema Literal.
  3012. if queue_status == "aborted":
  3013. queue_status = "cancelled"
  3014. item.status = queue_status
  3015. item.completed_at = datetime.now(timezone.utc)
  3016. if queue_status == "failed" and not item.error_message:
  3017. item.error_message = _format_hms_error_summary(data.get("hms_errors") or [])
  3018. # Bump usage counters on the source library file so admins can
  3019. # sort by "last printed" and (eventually) auto-purge stale
  3020. # files — #1008.
  3021. await _bump_library_file_usage_if_completed(db, item, queue_status)
  3022. await db.commit()
  3023. queue_item_id = item.id
  3024. queue_auto_off = item.auto_off_after
  3025. logger.info("Updated queue item %s status to %s", item.id, queue_status)
  3026. await run_with_retry(_update_queue_status, label="queue status update")
  3027. # Post-commit side effects (notifications, MQTT relay, auto-off) use
  3028. # their own sessions and have their own error handling — no retry needed.
  3029. if queue_item_id is not None:
  3030. # MQTT relay - publish queue job completed
  3031. try:
  3032. printer_info = printer_manager.get_printer(printer_id)
  3033. await mqtt_relay.on_queue_job_completed(
  3034. job_id=queue_item_id,
  3035. filename=filename or subtask_name,
  3036. printer_id=printer_id,
  3037. printer_name=printer_info.name if printer_info else "Unknown",
  3038. status=queue_status,
  3039. )
  3040. except Exception:
  3041. pass # Don't fail if MQTT fails
  3042. # Check if queue is now empty and send notification
  3043. try:
  3044. from sqlalchemy import func as sa_func
  3045. async with async_session() as db:
  3046. count_result = await db.execute(
  3047. select(sa_func.count(PrintQueueItem.id)).where(PrintQueueItem.status == "pending")
  3048. )
  3049. pending_count = count_result.scalar() or 0
  3050. if pending_count == 0:
  3051. today_start = datetime.now(timezone.utc).replace(hour=0, minute=0, second=0, microsecond=0)
  3052. completed_result = await db.execute(
  3053. select(sa_func.count(PrintQueueItem.id)).where(
  3054. PrintQueueItem.status.in_(["completed", "failed", "skipped"]),
  3055. PrintQueueItem.completed_at >= today_start,
  3056. )
  3057. )
  3058. completed_count = completed_result.scalar() or 1
  3059. await notification_service.on_queue_completed(
  3060. completed_count=completed_count,
  3061. db=db,
  3062. )
  3063. except Exception:
  3064. pass # Don't fail if notification fails
  3065. # Handle auto_off_after - power off printer if requested (after cooldown)
  3066. if queue_auto_off:
  3067. async with async_session() as db:
  3068. result = await db.execute(select(SmartPlug).where(SmartPlug.printer_id == printer_id))
  3069. plugs = list(result.scalars().all())
  3070. enabled_plugs = [p for p in plugs if p.enabled]
  3071. if enabled_plugs:
  3072. logger.info("Auto-off requested for printer %s, waiting for cooldown...", printer_id)
  3073. async def cooldown_and_poweroff(pid: int, plug_ids: list[int]):
  3074. # Wait for nozzle to cool down
  3075. await printer_manager.wait_for_cooldown(pid, target_temp=50.0, timeout=600)
  3076. # Re-fetch plugs in new session and turn off each one
  3077. async with async_session() as new_db:
  3078. for plug_id in plug_ids:
  3079. try:
  3080. result = await new_db.execute(select(SmartPlug).where(SmartPlug.id == plug_id))
  3081. p = result.scalar_one_or_none()
  3082. if p and p.enabled:
  3083. service = await smart_plug_manager.get_service_for_plug(p, new_db)
  3084. success = await service.turn_off(p)
  3085. if success:
  3086. logger.info("Powered off printer %s via smart plug '%s'", pid, p.name)
  3087. else:
  3088. logger.warning("Failed to power off plug '%s' for printer %s", p.name, pid)
  3089. except Exception as e:
  3090. logger.warning("Failed to power off plug %s for printer %s: %s", plug_id, pid, e)
  3091. asyncio.create_task(cooldown_and_poweroff(printer_id, [p.id for p in enabled_plugs]))
  3092. except Exception as e:
  3093. logging.getLogger(__name__).warning(f"Queue item update failed: {e}")
  3094. log_timing("Queue item update")
  3095. # Register bed cooldown waiter (event-driven via on_bed_temp_update callback).
  3096. # Must run before archive_id early-return so it fires for all prints (including
  3097. # prints started from BambuStudio/touchscreen that have no archive).
  3098. if data.get("status") == "completed":
  3099. try:
  3100. from backend.app.api.routes.settings import get_setting
  3101. async with async_session() as db:
  3102. threshold_str = await get_setting(db, "bed_cooled_threshold")
  3103. threshold = float(threshold_str) if threshold_str else 35.0
  3104. # Check if any provider has on_bed_cooled enabled (skip registration if none)
  3105. async with async_session() as db:
  3106. providers = await notification_service._get_providers_for_event(db, "on_bed_cooled", printer_id)
  3107. if providers:
  3108. _bed_cool_waiters[printer_id] = {
  3109. "threshold": threshold,
  3110. "filename": filename or subtask_name or "",
  3111. "registered_at": time.time(),
  3112. }
  3113. logger.info(
  3114. "[BED-COOL] Registered waiter for printer %s (threshold: %.0f°C)",
  3115. printer_id,
  3116. threshold,
  3117. )
  3118. else:
  3119. logger.debug("[BED-COOL] No providers enabled for bed_cooled on printer %s", printer_id)
  3120. except Exception as e:
  3121. logger.warning("[BED-COOL] Failed to register waiter: %s", e)
  3122. # --- Track filament consumption (must run before archive_id early-return so usage
  3123. # is recorded even when auto-archive is disabled) ---
  3124. usage_results: list[dict] = []
  3125. # Prefer ams_mapping captured from MQTT request topic (works for all print sources)
  3126. stored_ams_mapping = data.get("ams_mapping")
  3127. # Fallback to _print_ams_mappings for queue/reprint (set before print starts)
  3128. if not stored_ams_mapping and archive_id:
  3129. stored_ams_mapping = _print_ams_mappings.pop(archive_id, None)
  3130. # Internal inventory: track AMS remain% deltas (skip if Spoolman handles usage)
  3131. try:
  3132. async with async_session() as db:
  3133. from backend.app.api.routes.settings import get_setting
  3134. _spoolman_on = await get_setting(db, "spoolman_enabled")
  3135. if not _spoolman_on or _spoolman_on.lower() != "true":
  3136. from backend.app.services.usage_tracker import on_print_complete as usage_on_print_complete
  3137. async with async_session() as db:
  3138. usage_results = await usage_on_print_complete(
  3139. printer_id,
  3140. data,
  3141. printer_manager,
  3142. db,
  3143. archive_id=archive_id,
  3144. ams_mapping=stored_ams_mapping,
  3145. )
  3146. if usage_results:
  3147. await ws_manager.broadcast(
  3148. {
  3149. "type": "spool_usage_logged",
  3150. "printer_id": printer_id,
  3151. "usage": usage_results,
  3152. }
  3153. )
  3154. log_timing("Usage tracker")
  3155. except Exception as e:
  3156. logger.warning("Usage tracker on_print_complete failed: %s", e)
  3157. # Spoolman: report filament usage (requires archive_id for tracking data lookup)
  3158. if archive_id:
  3159. if data.get("status") == "completed":
  3160. try:
  3161. await _report_spoolman_usage(printer_id, archive_id)
  3162. log_timing("Spoolman usage report")
  3163. except Exception as e:
  3164. logger.warning("Spoolman usage reporting failed: %s", e)
  3165. else:
  3166. # Report partial usage if tracking data exists (only stored when weight sync is disabled)
  3167. try:
  3168. async with async_session() as db:
  3169. await _cleanup_spoolman_tracking(
  3170. printer_id,
  3171. archive_id,
  3172. db,
  3173. last_layer_num=data.get("last_layer_num"),
  3174. last_progress=data.get("last_progress"),
  3175. )
  3176. except Exception as e:
  3177. logger.debug("[SPOOLMAN] Cleanup failed: %s", e)
  3178. log_timing("Filament usage tracking")
  3179. if not archive_id:
  3180. logger.warning("Could not find archive for print complete: filename=%s, subtask=%s", filename, subtask_name)
  3181. # Still send print-complete/failed/stopped notifications even without an archive.
  3182. # Try to enrich with queue/library-file data so user-specific emails work too.
  3183. async def _notify_no_archive():
  3184. try:
  3185. async with async_session() as db:
  3186. from backend.app.models.library import LibraryFile
  3187. from backend.app.models.print_queue import PrintQueueItem
  3188. from backend.app.models.printer import Printer
  3189. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  3190. printer_obj = result.scalar_one_or_none()
  3191. p_name = printer_obj.name if printer_obj else f"Printer {printer_id}"
  3192. # Try to find the most-recent queue item for this printer so we can
  3193. # recover created_by_id and estimated print time.
  3194. # NOTE: By the time this task runs the queue item status has already
  3195. # been updated to a terminal state (completed/failed/cancelled), so
  3196. # we look for recently-completed items (within the last 5 minutes).
  3197. no_archive_data: dict | None = None
  3198. try:
  3199. cutoff = datetime.now(timezone.utc) - timedelta(minutes=5)
  3200. q_result = await db.execute(
  3201. select(PrintQueueItem)
  3202. .where(PrintQueueItem.printer_id == printer_id)
  3203. .where(PrintQueueItem.status.in_(["completed", "failed", "cancelled"]))
  3204. .where(PrintQueueItem.completed_at >= cutoff)
  3205. .order_by(PrintQueueItem.completed_at.desc())
  3206. .limit(1)
  3207. )
  3208. queue_item = q_result.scalar_one_or_none()
  3209. if queue_item:
  3210. no_archive_data = {"created_by_id": queue_item.created_by_id}
  3211. # Pull estimated time from library file when available
  3212. if queue_item.library_file_id:
  3213. lib_result = await db.execute(
  3214. select(LibraryFile).where(LibraryFile.id == queue_item.library_file_id)
  3215. )
  3216. lib_file = lib_result.scalar_one_or_none()
  3217. if lib_file and lib_file.print_time_seconds:
  3218. no_archive_data["print_time_seconds"] = lib_file.print_time_seconds
  3219. except Exception as lookup_err:
  3220. logger.debug(
  3221. "[NOTIFY-BG] Could not look up queue item for no-archive notification: %s", lookup_err
  3222. )
  3223. # Enrich with usage tracker results (captured in enclosing scope)
  3224. if usage_results:
  3225. if no_archive_data is None:
  3226. no_archive_data = {}
  3227. total_from_usage = sum(r.get("weight_used", 0) for r in usage_results)
  3228. if total_from_usage > 0:
  3229. no_archive_data["actual_filament_grams"] = round(total_from_usage, 1)
  3230. no_archive_data["usage_results"] = usage_results
  3231. # Try MQTT remaining_time for print duration when no queue/library data
  3232. if no_archive_data and not no_archive_data.get("print_time_seconds"):
  3233. mqtt_remaining = data.get("remaining_time")
  3234. if mqtt_remaining and isinstance(mqtt_remaining, (int, float)) and mqtt_remaining > 0:
  3235. no_archive_data["print_time_seconds"] = int(mqtt_remaining)
  3236. ps = data.get("status", "completed")
  3237. logger.info(
  3238. "[NOTIFY-BG] Sending notification without archive: printer=%s, status=%s", printer_id, ps
  3239. )
  3240. await notification_service.on_print_complete(
  3241. printer_id, p_name, ps, data, db, archive_data=no_archive_data
  3242. )
  3243. # Send user-specific email if we have a created_by_id
  3244. if no_archive_data and no_archive_data.get("created_by_id"):
  3245. raw_filename = data.get("subtask_name") or data.get("filename", "Unknown")
  3246. await _dispatch_user_print_email(
  3247. ps,
  3248. no_archive_data["created_by_id"],
  3249. p_name,
  3250. raw_filename,
  3251. db,
  3252. )
  3253. logger.info("[NOTIFY-BG] Completed (no-archive path)")
  3254. except Exception as e:
  3255. logger.warning("[NOTIFY-BG] Failed to send notification without archive: %s", e, exc_info=True)
  3256. task = asyncio.create_task(_notify_no_archive())
  3257. task.add_done_callback(lambda _t: None)
  3258. return
  3259. log_timing("Archive lookup")
  3260. # Update archive status
  3261. logger.info("[ARCHIVE] Updating archive %s status...", archive_id)
  3262. try:
  3263. async with async_session() as db:
  3264. service = ArchiveService(db)
  3265. status = data.get("status", "completed")
  3266. hms_errors = data.get("hms_errors", []) if status == "failed" else None
  3267. if hms_errors:
  3268. logger.info("[ARCHIVE] HMS errors at failure: %s", hms_errors)
  3269. failure_reason = derive_failure_reason(status, hms_errors)
  3270. if failure_reason:
  3271. logger.info("[ARCHIVE] failure_reason=%r (status=%s)", failure_reason, status)
  3272. elif status == "failed" and hms_errors:
  3273. logger.info("[ARCHIVE] HMS errors present but none matched a known failure-reason short code")
  3274. await service.update_archive_status(
  3275. archive_id,
  3276. status=status,
  3277. completed_at=(
  3278. datetime.now(timezone.utc) if status in ("completed", "failed", "aborted", "cancelled") else None
  3279. ),
  3280. failure_reason=failure_reason,
  3281. )
  3282. logger.info(
  3283. "[ARCHIVE] Archive %s status updated to %s, failure_reason=%s", archive_id, status, failure_reason
  3284. )
  3285. await ws_manager.send_archive_updated(
  3286. {
  3287. "id": archive_id,
  3288. "status": status,
  3289. }
  3290. )
  3291. logger.info("[ARCHIVE] WebSocket notification sent for archive %s", archive_id)
  3292. # MQTT relay - publish archive updated
  3293. try:
  3294. await mqtt_relay.on_archive_updated(
  3295. archive_id=archive_id,
  3296. print_name=filename or subtask_name,
  3297. status=status,
  3298. )
  3299. except Exception:
  3300. pass # Don't fail if MQTT fails
  3301. except Exception as e:
  3302. logger.error("[ARCHIVE] Failed to update archive %s status: %s", archive_id, e, exc_info=True)
  3303. # Continue with other operations even if archive update fails
  3304. log_timing("Archive status update")
  3305. # Write independent print log entry (separate table, never touches archives)
  3306. try:
  3307. async with async_session() as db:
  3308. from backend.app.models.archive import PrintArchive
  3309. from backend.app.services.print_log import write_log_entry
  3310. archive = await db.get(PrintArchive, archive_id)
  3311. if archive:
  3312. # Back-fill created_by_id on reprint (#730): reprint reuses the
  3313. # source archive row rather than creating a new one, so an
  3314. # archive that was auto-created from a printer-initiated
  3315. # print (created_by_id=NULL) would otherwise stay unattributed
  3316. # forever. When we have a print-session user AND the archive
  3317. # has no attribution yet, credit the current user. Never
  3318. # overwrite an existing attribution — the original uploader
  3319. # keeps ownership.
  3320. _print_user_id = _print_user_info.get("user_id") if _print_user_info else None
  3321. if archive.created_by_id is None and _print_user_id is not None:
  3322. archive.created_by_id = _print_user_id
  3323. p_info = printer_manager.get_printer(printer_id)
  3324. # Per-run actuals — written to PrintLogEntry so stats reflect
  3325. # what THIS print actually used, not the source archive's
  3326. # first-run values (#1378). Helper handles the partial-print
  3327. # math (failed / cancelled / stopped get scaled to progress
  3328. # or to tracked spool deltas).
  3329. _run_status = data.get("status", "completed")
  3330. _run_grams = _compute_run_filament_grams(
  3331. _run_status,
  3332. archive.filament_used_grams,
  3333. data.get("progress"),
  3334. usage_results,
  3335. )
  3336. # Per-run cost — prefer usage_results sum. For partial prints
  3337. # we deliberately skip the topup-to-estimate logic in
  3338. # usage_tracker (which assumes the print completed); the raw
  3339. # tracked-spool sum is closer to what THIS run actually cost.
  3340. _run_cost: float | None = None
  3341. if usage_results:
  3342. _run_cost = sum(r.get("cost") or 0 for r in usage_results) or None
  3343. if _run_cost is None and _run_status == "completed":
  3344. _run_cost = archive.cost
  3345. await write_log_entry(
  3346. db,
  3347. archive_id=archive.id,
  3348. status=_run_status,
  3349. print_name=archive.print_name,
  3350. printer_name=p_info.name if p_info else None,
  3351. printer_id=printer_id,
  3352. started_at=archive.started_at,
  3353. completed_at=archive.completed_at,
  3354. filament_type=archive.filament_type,
  3355. filament_color=archive.filament_color,
  3356. filament_used_grams=_run_grams,
  3357. cost=_run_cost,
  3358. failure_reason=archive.failure_reason,
  3359. thumbnail_path=archive.thumbnail_path,
  3360. created_by_id=archive.created_by_id,
  3361. created_by_username=_print_user_info.get("username") if _print_user_info else None,
  3362. )
  3363. await db.commit()
  3364. logger.info("[PRINT_LOG] Log entry written for archive %s", archive_id)
  3365. except Exception as e:
  3366. logger.warning("[PRINT_LOG] Failed to write log entry for archive %s: %s", archive_id, e)
  3367. log_timing("Print log entry")
  3368. # Run slow operations as background tasks to avoid blocking the event loop
  3369. # These operations can take 5-10+ seconds and would freeze the UI if awaited
  3370. async def _background_energy_calculation():
  3371. """Calculate and save energy usage in background.
  3372. Reads the starting kWh from the archive row (#941: persisted so a mid-print
  3373. backend restart no longer loses per-print energy data).
  3374. """
  3375. try:
  3376. logger.info("[ENERGY-BG] Starting energy calculation for archive %s", archive_id)
  3377. async with async_session() as db:
  3378. from backend.app.models.archive import PrintArchive
  3379. archive = await db.get(PrintArchive, archive_id)
  3380. if archive is None:
  3381. logger.warning("[ENERGY-BG] Archive %s no longer exists", archive_id)
  3382. return
  3383. starting_kwh = archive.energy_start_kwh
  3384. if starting_kwh is None:
  3385. logger.info("[ENERGY-BG] No start kWh recorded for archive %s", archive_id)
  3386. return
  3387. plug_result = await db.execute(select(SmartPlug).where(SmartPlug.printer_id == printer_id))
  3388. plug = plug_result.scalar_one_or_none()
  3389. if plug is None:
  3390. logger.info("[ENERGY-BG] No smart plug for printer %s", printer_id)
  3391. return
  3392. energy = await _get_plug_energy(plug, db)
  3393. logger.info("[ENERGY-BG] Energy response: %s", energy)
  3394. if not energy or energy.get("total") is None:
  3395. logger.warning("[ENERGY-BG] No 'total' in energy response")
  3396. return
  3397. energy_used = round(energy["total"] - starting_kwh, 4)
  3398. logger.info("[ENERGY-BG] Per-print energy: %s kWh", energy_used)
  3399. if energy_used < 0:
  3400. logger.warning(
  3401. "[ENERGY-BG] Negative energy delta for archive %s (start=%s, end=%s) — counter reset?",
  3402. archive_id,
  3403. starting_kwh,
  3404. energy["total"],
  3405. )
  3406. return
  3407. from backend.app.api.routes.settings import get_setting
  3408. energy_cost_per_kwh = await get_setting(db, "energy_cost_per_kwh")
  3409. cost_per_kwh = float(energy_cost_per_kwh) if energy_cost_per_kwh else 0.15
  3410. energy_cost_value = round(energy_used * cost_per_kwh, 3)
  3411. # First-run-only overwrite of archive.energy_kwh / energy_cost so a
  3412. # reprint doesn't visually clobber the source archive's energy data
  3413. # (#1378). Reprint energy lives in the matching PrintLogEntry below.
  3414. from sqlalchemy import func
  3415. from backend.app.models.print_log import PrintLogEntry
  3416. existing_runs = await db.scalar(
  3417. select(func.count(PrintLogEntry.id)).where(PrintLogEntry.archive_id == archive_id)
  3418. )
  3419. if (existing_runs or 0) <= 1:
  3420. # 0 = legacy archive that pre-dates per-run logging; 1 = the row
  3421. # we just wrote for THIS print. Either way it's the first run.
  3422. archive.energy_kwh = energy_used
  3423. archive.energy_cost = energy_cost_value
  3424. # Backfill the latest PrintLogEntry for this archive with energy
  3425. # (write_log_entry above ran before this background task completed,
  3426. # so energy fields are still NULL on that row).
  3427. latest_run = await db.execute(
  3428. select(PrintLogEntry)
  3429. .where(PrintLogEntry.archive_id == archive_id)
  3430. .order_by(PrintLogEntry.id.desc())
  3431. .limit(1)
  3432. )
  3433. run_row = latest_run.scalar_one_or_none()
  3434. if run_row is not None:
  3435. run_row.energy_kwh = energy_used
  3436. run_row.energy_cost = energy_cost_value
  3437. await db.commit()
  3438. logger.info("[ENERGY-BG] Saved: %s kWh, cost=%s", energy_used, energy_cost_value)
  3439. except Exception as e:
  3440. logger.warning("[ENERGY-BG] Failed: %s", e)
  3441. async def _background_finish_photo() -> str | None:
  3442. """Capture finish photo in background. Returns photo filename if captured."""
  3443. try:
  3444. logger.info("[PHOTO-BG] Starting finish photo capture for archive %s", archive_id)
  3445. from backend.app.api.routes.camera import _active_chamber_streams, _active_streams, get_buffered_frame
  3446. async with async_session() as db:
  3447. from backend.app.api.routes.settings import get_setting
  3448. capture_enabled = await get_setting(db, "capture_finish_photo")
  3449. if capture_enabled is None or capture_enabled.lower() == "true":
  3450. from backend.app.models.printer import Printer
  3451. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  3452. printer = result.scalar_one_or_none()
  3453. if printer and archive_id:
  3454. from backend.app.models.archive import PrintArchive
  3455. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  3456. archive = result.scalar_one_or_none()
  3457. if archive:
  3458. import uuid
  3459. from datetime import datetime
  3460. from pathlib import Path
  3461. if archive.file_path:
  3462. archive_dir = app_settings.base_dir / Path(archive.file_path).parent
  3463. else:
  3464. logger.warning("[PHOTO-BG] Archive %s has no file_path, using fallback dir", archive_id)
  3465. archive_dir = app_settings.archive_dir / str(archive.id)
  3466. photo_filename = None
  3467. # Check for external camera first
  3468. if printer.external_camera_enabled and printer.external_camera_url:
  3469. logger.info("[PHOTO-BG] Using external camera")
  3470. from backend.app.services.external_camera import capture_frame
  3471. frame_data = await capture_frame(
  3472. printer.external_camera_url,
  3473. printer.external_camera_type or "mjpeg",
  3474. snapshot_url=printer.external_camera_snapshot_url,
  3475. )
  3476. if frame_data:
  3477. photos_dir = archive_dir / "photos"
  3478. photos_dir.mkdir(parents=True, exist_ok=True)
  3479. timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
  3480. photo_filename = f"finish_{timestamp}_{uuid.uuid4().hex[:8]}.jpg"
  3481. photo_path = photos_dir / photo_filename
  3482. await asyncio.to_thread(photo_path.write_bytes, frame_data)
  3483. logger.info("[PHOTO-BG] Saved external camera frame: %s", photo_filename)
  3484. else:
  3485. # Check if camera stream is active - use buffered frame to avoid freeze
  3486. # Check both RTSP streams (_active_streams) and chamber image streams (_active_chamber_streams)
  3487. active_for_printer = [k for k in _active_streams if k.startswith(f"{printer_id}-")]
  3488. active_chamber_for_printer = [
  3489. k for k in _active_chamber_streams if k.startswith(f"{printer_id}-")
  3490. ]
  3491. buffered_frame = get_buffered_frame(printer_id)
  3492. if (active_for_printer or active_chamber_for_printer) and buffered_frame:
  3493. # Use frame from active stream
  3494. logger.info("[PHOTO-BG] Using buffered frame from active stream")
  3495. photos_dir = archive_dir / "photos"
  3496. photos_dir.mkdir(parents=True, exist_ok=True)
  3497. timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
  3498. photo_filename = f"finish_{timestamp}_{uuid.uuid4().hex[:8]}.jpg"
  3499. photo_path = photos_dir / photo_filename
  3500. await asyncio.to_thread(photo_path.write_bytes, buffered_frame)
  3501. logger.info("[PHOTO-BG] Saved buffered frame: %s", photo_filename)
  3502. else:
  3503. # No active stream - capture new frame
  3504. from backend.app.services.camera import capture_finish_photo
  3505. photo_filename = await capture_finish_photo(
  3506. printer_id=printer_id,
  3507. ip_address=printer.ip_address,
  3508. access_code=printer.access_code,
  3509. model=printer.model,
  3510. archive_dir=archive_dir,
  3511. )
  3512. if photo_filename:
  3513. photos = archive.photos or []
  3514. photos.append(photo_filename)
  3515. archive.photos = photos
  3516. await db.commit()
  3517. logger.info("[PHOTO-BG] Saved: %s", photo_filename)
  3518. return photo_filename
  3519. return None
  3520. except Exception as e:
  3521. logger.warning("[PHOTO-BG] Failed: %s", e)
  3522. return None
  3523. asyncio.create_task(_background_energy_calculation())
  3524. # Photo capture task - result will be used by notifications
  3525. photo_task = asyncio.create_task(_background_finish_photo())
  3526. log_timing("Background tasks scheduled (energy, photo)")
  3527. # Also run smart plug, notifications, and maintenance as background tasks
  3528. print_status = data.get("status", "completed")
  3529. async def _background_smart_plug():
  3530. """Handle smart plug automation in background."""
  3531. try:
  3532. logger.info("[AUTO-OFF-BG] Starting smart plug automation for printer %s", printer_id)
  3533. async with async_session() as db:
  3534. await smart_plug_manager.on_print_complete(printer_id, print_status, db)
  3535. logger.info("[AUTO-OFF-BG] Completed")
  3536. except Exception as e:
  3537. logger.warning("[AUTO-OFF-BG] Failed: %s", e)
  3538. async def _background_notifications(finish_photo_filename: str | None = None):
  3539. """Send print complete notifications in background."""
  3540. try:
  3541. logger.info(
  3542. "[NOTIFY-BG] Starting notifications for printer %s, photo=%s", printer_id, finish_photo_filename
  3543. )
  3544. async with async_session() as db:
  3545. from backend.app.models.archive import PrintArchive
  3546. from backend.app.models.printer import Printer
  3547. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  3548. printer = result.scalar_one_or_none()
  3549. printer_name = printer.name if printer else f"Printer {printer_id}"
  3550. archive_data = None
  3551. if archive_id:
  3552. archive_result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  3553. archive = archive_result.scalar_one_or_none()
  3554. if archive:
  3555. # Actual elapsed time from started_at/completed_at when both are
  3556. # populated (every terminal status sets completed_at after #1198).
  3557. # Falls back to None so the notification path can decide whether to
  3558. # render the slicer estimate as a last resort.
  3559. actual_time_seconds = None
  3560. if archive.started_at and archive.completed_at:
  3561. elapsed = (archive.completed_at - archive.started_at).total_seconds()
  3562. if elapsed > 0:
  3563. actual_time_seconds = int(elapsed)
  3564. archive_data = {
  3565. "print_time_seconds": archive.print_time_seconds,
  3566. "actual_time_seconds": actual_time_seconds,
  3567. "actual_filament_grams": archive.filament_used_grams,
  3568. "failure_reason": archive.failure_reason,
  3569. "created_by_id": archive.created_by_id,
  3570. }
  3571. # Scale filament usage for partial prints
  3572. if print_status != "completed" and archive.filament_used_grams:
  3573. progress = data.get("progress") or 0
  3574. scale = max(0.0, min(progress / 100.0, 1.0))
  3575. archive_data["actual_filament_grams"] = round(archive.filament_used_grams * scale, 1)
  3576. archive_data["progress"] = progress
  3577. # Pass per-slot data from archive.extra_data
  3578. if archive.extra_data and archive.extra_data.get("filament_slots"):
  3579. slots = archive.extra_data["filament_slots"]
  3580. if print_status != "completed":
  3581. scale = max(0.0, min((data.get("progress") or 0) / 100.0, 1.0))
  3582. slots = [{**s, "used_g": round(s["used_g"] * scale, 1)} for s in slots]
  3583. archive_data["filament_slots"] = slots
  3584. # Enrich filament_grams from usage_results when archive has no 3MF data
  3585. if not archive_data.get("actual_filament_grams") and usage_results:
  3586. total_from_usage = sum(r.get("weight_used", 0) for r in usage_results)
  3587. if total_from_usage > 0:
  3588. archive_data["actual_filament_grams"] = round(total_from_usage, 1)
  3589. # Pass usage tracker results for AMS slot info in notifications
  3590. if usage_results:
  3591. archive_data["usage_results"] = usage_results
  3592. # Add finish photo URL and image bytes if available
  3593. if finish_photo_filename:
  3594. from backend.app.api.routes.settings import get_setting
  3595. external_url = await get_setting(db, "external_url")
  3596. if external_url:
  3597. external_url = external_url.rstrip("/")
  3598. archive_data["finish_photo_url"] = (
  3599. f"{external_url}/api/v1/archives/{archive_id}/photos/{finish_photo_filename}"
  3600. )
  3601. else:
  3602. # Fallback to relative URL (won't work for external services)
  3603. archive_data["finish_photo_url"] = (
  3604. f"/api/v1/archives/{archive_id}/photos/{finish_photo_filename}"
  3605. )
  3606. # Read finish photo bytes for image attachment (e.g. Pushover)
  3607. try:
  3608. from pathlib import Path
  3609. photo_path = (
  3610. app_settings.base_dir
  3611. / Path(archive.file_path).parent
  3612. / "photos"
  3613. / finish_photo_filename
  3614. )
  3615. if photo_path.exists():
  3616. photo_bytes = await asyncio.to_thread(photo_path.read_bytes)
  3617. if len(photo_bytes) <= 2_500_000:
  3618. archive_data["image_data"] = photo_bytes
  3619. logger.info("[NOTIFY-BG] Loaded finish photo bytes: %s bytes", len(photo_bytes))
  3620. else:
  3621. logger.warning(
  3622. f"[NOTIFY-BG] Finish photo too large for attachment: "
  3623. f"{len(photo_bytes)} bytes"
  3624. )
  3625. except Exception as e:
  3626. logger.warning("[NOTIFY-BG] Failed to read finish photo bytes: %s", e)
  3627. await notification_service.on_print_complete(
  3628. printer_id, printer_name, print_status, data, db, archive_data=archive_data
  3629. )
  3630. # Send user-specific email notification
  3631. if archive_data:
  3632. created_by_id = archive_data.get("created_by_id")
  3633. raw_filename = data.get("subtask_name") or data.get("filename", "Unknown")
  3634. await _dispatch_user_print_email(
  3635. print_status,
  3636. created_by_id,
  3637. printer_name,
  3638. raw_filename,
  3639. db,
  3640. )
  3641. logger.info("[NOTIFY-BG] Completed")
  3642. except Exception as e:
  3643. logger.error("[NOTIFY-BG] Failed: %s", e, exc_info=True)
  3644. async def _background_maintenance_check():
  3645. """Check for maintenance due in background."""
  3646. if print_status != "completed":
  3647. return
  3648. try:
  3649. logger.info("[MAINT-BG] Starting maintenance check for printer %s", printer_id)
  3650. async with async_session() as db:
  3651. from backend.app.models.printer import Printer
  3652. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  3653. printer = result.scalar_one_or_none()
  3654. printer_name = printer.name if printer else f"Printer {printer_id}"
  3655. await ensure_default_types(db)
  3656. overview = await _get_printer_maintenance_internal(printer_id, db, commit=True)
  3657. items_needing_attention = [
  3658. {"name": item.maintenance_type_name, "is_due": item.is_due, "is_warning": item.is_warning}
  3659. for item in overview.maintenance_items
  3660. if item.enabled and (item.is_due or item.is_warning)
  3661. ]
  3662. if items_needing_attention:
  3663. await notification_service.on_maintenance_due(printer_id, printer_name, items_needing_attention, db)
  3664. logger.info("[MAINT-BG] Sent notification: %s items need attention", len(items_needing_attention))
  3665. # MQTT relay - publish maintenance alerts
  3666. for item in items_needing_attention:
  3667. try:
  3668. await mqtt_relay.on_maintenance_alert(
  3669. printer_id=printer_id,
  3670. printer_name=printer_name,
  3671. maintenance_type=item["name"],
  3672. current_value=0, # Not easily available here
  3673. threshold=0, # Not easily available here
  3674. )
  3675. except Exception:
  3676. pass # Don't fail if MQTT fails
  3677. else:
  3678. logger.info("[MAINT-BG] Completed (no items need attention)")
  3679. except Exception as e:
  3680. logger.warning("[MAINT-BG] Failed: %s", e)
  3681. asyncio.create_task(_background_smart_plug())
  3682. asyncio.create_task(_background_maintenance_check())
  3683. # Notification task waits for photo capture to complete first (with timeout)
  3684. async def _photo_then_notify():
  3685. """Wait for photo capture, then send notification with photo URL."""
  3686. finish_photo = None
  3687. try:
  3688. finish_photo = await asyncio.wait_for(photo_task, timeout=45)
  3689. logger.info("[PHOTO-NOTIFY] Photo task returned: %s", finish_photo)
  3690. except TimeoutError:
  3691. logger.warning("[PHOTO-NOTIFY] Photo capture timed out after 45s, sending notification without photo")
  3692. except Exception as e:
  3693. logger.warning("[PHOTO-NOTIFY] Photo task failed: %s", e)
  3694. try:
  3695. await _background_notifications(finish_photo)
  3696. except Exception as e:
  3697. logger.error("[PHOTO-NOTIFY] Notification sending failed: %s", e, exc_info=True)
  3698. asyncio.create_task(_photo_then_notify())
  3699. # Stitch external camera layer timelapse if session was active
  3700. print_status = data.get("status", "completed")
  3701. async def _background_layer_timelapse():
  3702. """Stitch layer timelapse and attach to archive."""
  3703. from backend.app.services.layer_timelapse import cancel_session, on_print_complete as tl_complete
  3704. try:
  3705. if print_status == "completed":
  3706. logger.info("[LAYER-TL] Stitching layer timelapse for printer %s", printer_id)
  3707. timelapse_path = await tl_complete(printer_id)
  3708. if timelapse_path and archive_id:
  3709. logger.info("[LAYER-TL] Attaching timelapse %s to archive %s", timelapse_path, archive_id)
  3710. async with async_session() as db:
  3711. service = ArchiveService(db)
  3712. timelapse_data = await asyncio.to_thread(timelapse_path.read_bytes)
  3713. await service.attach_timelapse(archive_id, timelapse_data, "layer_timelapse.mp4")
  3714. # Clean up the temp file
  3715. await asyncio.to_thread(timelapse_path.unlink, missing_ok=True)
  3716. logger.info("[LAYER-TL] Layer timelapse attached successfully")
  3717. elif timelapse_path:
  3718. # Timelapse created but no archive - just clean up
  3719. await asyncio.to_thread(timelapse_path.unlink, missing_ok=True)
  3720. else:
  3721. # Print failed or cancelled - cancel timelapse session
  3722. cancel_session(printer_id)
  3723. logger.info(
  3724. "[LAYER-TL] Cancelled layer timelapse for printer %s (status: %s)", printer_id, print_status
  3725. )
  3726. except Exception as e:
  3727. logger.warning("[LAYER-TL] Failed: %s", e)
  3728. # Try to cancel session on error
  3729. try:
  3730. cancel_session(printer_id)
  3731. except Exception:
  3732. pass # Best-effort timelapse session cancellation on error
  3733. asyncio.create_task(_background_layer_timelapse())
  3734. log_timing("All background tasks scheduled")
  3735. # Auto-scan for timelapse if recording was active during the print
  3736. if archive_id and data.get("timelapse_was_active") and data.get("status") == "completed":
  3737. logger.info("[TIMELAPSE] Timelapse was active during print, scheduling auto-scan for archive %s", archive_id)
  3738. # Schedule timelapse scan as background task with retries
  3739. # The printer needs time to encode the video after print completion
  3740. baseline = _timelapse_baselines.pop(printer_id, None)
  3741. asyncio.create_task(_scan_for_timelapse_with_retries(archive_id, baseline))
  3742. log_timing("Timelapse scan scheduled")
  3743. logger.info("[CALLBACK] on_print_complete finished for printer %s, archive %s", printer_id, archive_id)
  3744. # AMS sensor history recording
  3745. _ams_history_task: asyncio.Task | None = None
  3746. AMS_HISTORY_INTERVAL = 300 # Record every 5 minutes
  3747. AMS_HISTORY_RETENTION_DAYS = 30 # Keep data for 30 days
  3748. _ams_cleanup_counter = 0 # Track recordings to trigger periodic cleanup
  3749. # Track alarm cooldowns (printer_id:ams_id:type -> last_alarm_time)
  3750. _ams_alarm_cooldown: dict[str, datetime] = {}
  3751. AMS_ALARM_COOLDOWN_MINUTES = 60 # Don't send same alarm more than once per hour
  3752. async def record_ams_history():
  3753. """Background task to record AMS humidity and temperature data."""
  3754. logger = logging.getLogger(__name__)
  3755. # Wait a short time for MQTT connections to establish on startup
  3756. await asyncio.sleep(10)
  3757. while True:
  3758. try:
  3759. from backend.app.models.ams_history import AMSSensorHistory
  3760. from backend.app.models.printer import Printer
  3761. from backend.app.models.settings import Settings
  3762. async with async_session() as db:
  3763. # Get all active printers
  3764. result = await db.execute(select(Printer).where(Printer.is_active.is_(True)))
  3765. printers = result.scalars().all()
  3766. # Get alarm thresholds from settings
  3767. humidity_threshold = 60.0 # Default: fair threshold
  3768. temp_threshold = 35.0 # Default: fair threshold
  3769. result = await db.execute(select(Settings).where(Settings.key == "ams_humidity_fair"))
  3770. setting = result.scalar_one_or_none()
  3771. if setting:
  3772. try:
  3773. humidity_threshold = float(setting.value)
  3774. except (ValueError, TypeError):
  3775. pass # Keep default threshold if stored value is invalid
  3776. result = await db.execute(select(Settings).where(Settings.key == "ams_temp_fair"))
  3777. setting = result.scalar_one_or_none()
  3778. if setting:
  3779. try:
  3780. temp_threshold = float(setting.value)
  3781. except (ValueError, TypeError):
  3782. pass # Keep default threshold if stored value is invalid
  3783. recorded_count = 0
  3784. for printer in printers:
  3785. # Get current state from printer manager
  3786. state = printer_manager.get_status(printer.id)
  3787. if not state or not state.connected or not state.raw_data:
  3788. continue # Skip disconnected printers - don't use stale data
  3789. raw_data = state.raw_data
  3790. if "ams" not in raw_data or not isinstance(raw_data["ams"], list):
  3791. continue
  3792. # Record data for each AMS unit
  3793. for ams_data in raw_data["ams"]:
  3794. ams_id = int(ams_data.get("id", 0))
  3795. # Get humidity (prefer humidity_raw)
  3796. humidity_raw = ams_data.get("humidity_raw")
  3797. humidity_idx = ams_data.get("humidity")
  3798. humidity = None
  3799. if humidity_raw is not None:
  3800. try:
  3801. humidity = float(humidity_raw)
  3802. except (ValueError, TypeError):
  3803. pass # Skip unparseable humidity; will try fallback
  3804. if humidity is None and humidity_idx is not None:
  3805. try:
  3806. humidity = float(humidity_idx)
  3807. except (ValueError, TypeError):
  3808. pass # Skip unparseable humidity index value
  3809. # Get temperature
  3810. temperature = None
  3811. temp_str = ams_data.get("temp")
  3812. if temp_str is not None:
  3813. try:
  3814. temperature = float(temp_str)
  3815. except (ValueError, TypeError):
  3816. pass # Skip unparseable temperature value
  3817. # Skip if no data
  3818. if humidity is None and temperature is None:
  3819. continue
  3820. # Record the data point
  3821. history = AMSSensorHistory(
  3822. printer_id=printer.id,
  3823. ams_id=ams_id,
  3824. humidity=humidity,
  3825. humidity_raw=float(humidity_raw) if humidity_raw else None,
  3826. temperature=temperature,
  3827. )
  3828. db.add(history)
  3829. recorded_count += 1
  3830. # Generate AMS label and determine if it's AMS-HT (A, B, C, D or HT-A for AMS-Lite/Hub)
  3831. is_ams_ht = ams_id >= 128
  3832. if is_ams_ht:
  3833. ams_label = f"HT-{chr(65 + (ams_id - 128))}"
  3834. else:
  3835. ams_label = f"AMS-{chr(65 + ams_id)}"
  3836. # Check humidity alarm (only if above threshold)
  3837. if humidity is not None and humidity > humidity_threshold:
  3838. cooldown_key = f"{printer.id}:{ams_id}:humidity"
  3839. last_alarm = _ams_alarm_cooldown.get(cooldown_key)
  3840. now = datetime.now(timezone.utc)
  3841. if (
  3842. last_alarm is None
  3843. or (now - last_alarm).total_seconds() >= AMS_ALARM_COOLDOWN_MINUTES * 60
  3844. ):
  3845. _ams_alarm_cooldown[cooldown_key] = now
  3846. logger.info(
  3847. f"Sending humidity alarm for {printer.name} {ams_label}: {humidity}% > {humidity_threshold}%"
  3848. )
  3849. try:
  3850. # Call different notification method based on AMS type
  3851. if is_ams_ht:
  3852. await notification_service.on_ams_ht_humidity_high(
  3853. printer.id, printer.name, ams_label, humidity, humidity_threshold, db
  3854. )
  3855. else:
  3856. await notification_service.on_ams_humidity_high(
  3857. printer.id, printer.name, ams_label, humidity, humidity_threshold, db
  3858. )
  3859. except Exception as e:
  3860. logger.warning("Failed to send humidity alarm: %s", e)
  3861. # Check temperature alarm (only if above threshold)
  3862. if temperature is not None and temperature > temp_threshold:
  3863. cooldown_key = f"{printer.id}:{ams_id}:temperature"
  3864. last_alarm = _ams_alarm_cooldown.get(cooldown_key)
  3865. now = datetime.now(timezone.utc)
  3866. if (
  3867. last_alarm is None
  3868. or (now - last_alarm).total_seconds() >= AMS_ALARM_COOLDOWN_MINUTES * 60
  3869. ):
  3870. _ams_alarm_cooldown[cooldown_key] = now
  3871. logger.info(
  3872. f"Sending temperature alarm for {printer.name} {ams_label}: {temperature}°C > {temp_threshold}°C"
  3873. )
  3874. try:
  3875. # Call different notification method based on AMS type
  3876. if is_ams_ht:
  3877. await notification_service.on_ams_ht_temperature_high(
  3878. printer.id, printer.name, ams_label, temperature, temp_threshold, db
  3879. )
  3880. else:
  3881. await notification_service.on_ams_temperature_high(
  3882. printer.id, printer.name, ams_label, temperature, temp_threshold, db
  3883. )
  3884. except Exception as e:
  3885. logger.warning("Failed to send temperature alarm: %s", e)
  3886. await db.commit()
  3887. if recorded_count > 0:
  3888. logger.info("Recorded %s AMS sensor history entries", recorded_count)
  3889. # Periodic cleanup of old data (every ~288 recordings = ~24 hours at 5min interval)
  3890. global _ams_cleanup_counter
  3891. _ams_cleanup_counter += 1
  3892. if _ams_cleanup_counter >= 288:
  3893. _ams_cleanup_counter = 0
  3894. # Get retention days from settings
  3895. from backend.app.models.settings import Settings
  3896. result = await db.execute(select(Settings).where(Settings.key == "ams_history_retention_days"))
  3897. setting = result.scalar_one_or_none()
  3898. retention_days = int(setting.value) if setting else AMS_HISTORY_RETENTION_DAYS
  3899. cutoff = datetime.utcnow() - timedelta(days=retention_days)
  3900. result = await db.execute(delete(AMSSensorHistory).where(AMSSensorHistory.recorded_at < cutoff))
  3901. await db.commit()
  3902. if result.rowcount > 0:
  3903. logger.info(
  3904. f"Cleaned up {result.rowcount} old AMS sensor history entries (older than {retention_days} days)"
  3905. )
  3906. # Wait until next recording interval
  3907. await asyncio.sleep(AMS_HISTORY_INTERVAL)
  3908. except asyncio.CancelledError:
  3909. break
  3910. except Exception as e:
  3911. logger.warning("AMS history recording failed: %s", e)
  3912. await asyncio.sleep(60) # Wait a bit before retrying
  3913. def start_ams_history_recording():
  3914. """Start the AMS history recording background task."""
  3915. global _ams_history_task
  3916. if _ams_history_task is None:
  3917. _ams_history_task = asyncio.create_task(record_ams_history())
  3918. logging.getLogger(__name__).info("AMS history recording started")
  3919. def stop_ams_history_recording():
  3920. """Stop the AMS history recording background task."""
  3921. global _ams_history_task
  3922. if _ams_history_task:
  3923. _ams_history_task.cancel()
  3924. _ams_history_task = None
  3925. logging.getLogger(__name__).info("AMS history recording stopped")
  3926. # Printer runtime tracking
  3927. _runtime_tracking_task: asyncio.Task | None = None
  3928. RUNTIME_TRACKING_INTERVAL = 30 # Update every 30 seconds
  3929. async def track_printer_runtime():
  3930. """Background task to track printer active runtime (RUNNING state only).
  3931. PAUSE is intentionally excluded — the runtime counter feeds hours-based
  3932. maintenance intervals (rod lubrication, belt checks, nozzle cleaning)
  3933. which track mechanical wear. Pause time has no motion and no wear, so
  3934. counting it inflates maintenance warnings (#1521).
  3935. """
  3936. logger = logging.getLogger(__name__)
  3937. # Wait for MQTT connections to establish on startup
  3938. await asyncio.sleep(15)
  3939. while True:
  3940. try:
  3941. from backend.app.models.printer import Printer
  3942. # Fetch printer IDs in a short-lived read-only session
  3943. async with async_session() as db:
  3944. result = await db.execute(
  3945. select(Printer.id, Printer.name, Printer.runtime_seconds, Printer.last_runtime_update).where(
  3946. Printer.is_active.is_(True)
  3947. )
  3948. )
  3949. printer_rows = result.all()
  3950. now = datetime.now(timezone.utc)
  3951. updated_count = 0
  3952. # Update each printer in its own short session to minimise write-lock
  3953. # hold time and avoid blocking critical commits like queue status
  3954. # updates (#897).
  3955. for pid, pname, runtime_secs, last_update in printer_rows:
  3956. state = printer_manager.get_status(pid)
  3957. if not state:
  3958. logger.debug("[%s] Runtime tracking: no state available", pname)
  3959. continue
  3960. if not state.connected:
  3961. logger.debug("[%s] Runtime tracking: not connected", pname)
  3962. continue
  3963. needs_commit = False
  3964. new_runtime = runtime_secs
  3965. new_last_update = last_update
  3966. if state.state == "RUNNING":
  3967. if last_update:
  3968. lu = last_update if last_update.tzinfo else last_update.replace(tzinfo=timezone.utc)
  3969. elapsed = (now - lu).total_seconds()
  3970. if elapsed > 0:
  3971. new_runtime = runtime_secs + int(elapsed)
  3972. updated_count += 1
  3973. needs_commit = True
  3974. logger.debug(
  3975. f"[{pname}] Runtime tracking: added {int(elapsed)}s, "
  3976. f"total={new_runtime}s ({new_runtime / 3600:.2f}h)"
  3977. )
  3978. else:
  3979. needs_commit = True
  3980. logger.debug("[%s] Runtime tracking: first active detection", pname)
  3981. new_last_update = now
  3982. else:
  3983. if last_update is not None:
  3984. logger.debug(f"[{pname}] Runtime tracking: state={state.state}, clearing last_runtime_update")
  3985. new_last_update = None
  3986. needs_commit = True
  3987. if needs_commit:
  3988. try:
  3989. async with async_session() as db:
  3990. result = await db.execute(select(Printer).where(Printer.id == pid))
  3991. printer = result.scalar_one_or_none()
  3992. if printer:
  3993. printer.runtime_seconds = new_runtime
  3994. printer.last_runtime_update = new_last_update
  3995. await db.commit()
  3996. except Exception as e:
  3997. logger.warning("[%s] Runtime tracking commit failed: %s", pname, e)
  3998. if updated_count > 0:
  3999. logger.debug("Updated runtime for %s printer(s)", updated_count)
  4000. except asyncio.CancelledError:
  4001. logger.info("Runtime tracking cancelled")
  4002. break
  4003. except Exception as e:
  4004. logger.warning("Runtime tracking failed: %s", e)
  4005. await asyncio.sleep(RUNTIME_TRACKING_INTERVAL)
  4006. def start_runtime_tracking():
  4007. """Start the printer runtime tracking background task."""
  4008. global _runtime_tracking_task
  4009. if _runtime_tracking_task is None:
  4010. _runtime_tracking_task = asyncio.create_task(track_printer_runtime())
  4011. logging.getLogger(__name__).info("Printer runtime tracking started")
  4012. def stop_runtime_tracking():
  4013. """Stop the printer runtime tracking background task."""
  4014. global _runtime_tracking_task
  4015. if _runtime_tracking_task:
  4016. _runtime_tracking_task.cancel()
  4017. _runtime_tracking_task = None
  4018. logging.getLogger(__name__).info("Printer runtime tracking stopped")
  4019. # SpoolBuddy device watchdog
  4020. _spoolbuddy_watchdog_task: asyncio.Task | None = None
  4021. SPOOLBUDDY_WATCHDOG_INTERVAL = 15
  4022. async def _spoolbuddy_watchdog_loop():
  4023. """Periodic check for SpoolBuddy devices that have gone offline."""
  4024. from backend.app.api.routes.spoolbuddy import spoolbuddy_watchdog
  4025. while True:
  4026. try:
  4027. await spoolbuddy_watchdog()
  4028. except asyncio.CancelledError:
  4029. break
  4030. except Exception as e:
  4031. logging.getLogger(__name__).warning("SpoolBuddy watchdog failed: %s", e)
  4032. await asyncio.sleep(SPOOLBUDDY_WATCHDOG_INTERVAL)
  4033. def start_spoolbuddy_watchdog():
  4034. global _spoolbuddy_watchdog_task
  4035. if _spoolbuddy_watchdog_task is None:
  4036. _spoolbuddy_watchdog_task = asyncio.create_task(_spoolbuddy_watchdog_loop())
  4037. logging.getLogger(__name__).info("SpoolBuddy watchdog started")
  4038. def stop_spoolbuddy_watchdog():
  4039. global _spoolbuddy_watchdog_task
  4040. if _spoolbuddy_watchdog_task:
  4041. _spoolbuddy_watchdog_task.cancel()
  4042. _spoolbuddy_watchdog_task = None
  4043. logging.getLogger(__name__).info("SpoolBuddy watchdog stopped")
  4044. # Camera stream orphan cleanup
  4045. _camera_cleanup_task: asyncio.Task | None = None
  4046. CAMERA_CLEANUP_INTERVAL = 60
  4047. async def _camera_cleanup_loop():
  4048. """Periodically clean up orphaned ffmpeg processes."""
  4049. from backend.app.api.routes.camera import cleanup_orphaned_streams
  4050. while True:
  4051. try:
  4052. await cleanup_orphaned_streams()
  4053. except asyncio.CancelledError:
  4054. break
  4055. except Exception as e:
  4056. logging.getLogger(__name__).warning("Camera stream cleanup failed: %s", e)
  4057. await asyncio.sleep(CAMERA_CLEANUP_INTERVAL)
  4058. def start_camera_cleanup():
  4059. global _camera_cleanup_task
  4060. if _camera_cleanup_task is None:
  4061. _camera_cleanup_task = asyncio.create_task(_camera_cleanup_loop())
  4062. logging.getLogger(__name__).info("Camera stream cleanup started")
  4063. def stop_camera_cleanup():
  4064. global _camera_cleanup_task
  4065. if _camera_cleanup_task:
  4066. _camera_cleanup_task.cancel()
  4067. _camera_cleanup_task = None
  4068. logging.getLogger(__name__).info("Camera stream cleanup stopped")
  4069. # ---------------------------------------------------------------------------
  4070. # Expected-print TTL eviction
  4071. # ---------------------------------------------------------------------------
  4072. def _evict_stale_expected_prints() -> None:
  4073. """Remove entries from _expected_prints / _expected_print_creators that are
  4074. older than _EXPECTED_PRINT_TTL_SECONDS.
  4075. This prevents unbounded growth when a print is registered (via
  4076. register_expected_print) but on_print_start never fires — e.g. because the
  4077. printer disconnects, the app restarts, or the print is started directly from
  4078. the printer panel without going through the queue.
  4079. """
  4080. # Use monotonic time so the TTL is unaffected by system clock adjustments
  4081. # (e.g. NTP sync, DST changes).
  4082. cutoff = time.monotonic() - _EXPECTED_PRINT_TTL_SECONDS
  4083. stale_keys = [k for k, t in _expected_print_registered_at.items() if t < cutoff]
  4084. if not stale_keys:
  4085. return
  4086. evicted_archive_ids: set[int] = set()
  4087. for key in stale_keys:
  4088. archive_id = _expected_prints.pop(key, None)
  4089. if archive_id is not None:
  4090. evicted_archive_ids.add(archive_id)
  4091. _expected_print_creators.pop(key, None)
  4092. _expected_print_registered_at.pop(key, None)
  4093. # Also clean up _print_ams_mappings for archive_ids that have no remaining
  4094. # live keys in _expected_prints (i.e. all variants were just evicted).
  4095. live_archive_ids = set(_expected_prints.values())
  4096. for archive_id in evicted_archive_ids:
  4097. if archive_id not in live_archive_ids:
  4098. _print_ams_mappings.pop(archive_id, None)
  4099. logging.getLogger(__name__).info(
  4100. "Evicted %d stale expected-print entries (TTL=%ds)", len(stale_keys), _EXPECTED_PRINT_TTL_SECONDS
  4101. )
  4102. async def _expected_prints_cleanup_loop() -> None:
  4103. """Background task: periodically evict stale expected-print entries."""
  4104. while True:
  4105. try:
  4106. _evict_stale_expected_prints()
  4107. except asyncio.CancelledError:
  4108. raise
  4109. except Exception as e:
  4110. logging.getLogger(__name__).warning("Expected prints cleanup failed: %s", e)
  4111. await asyncio.sleep(_EXPECTED_PRINT_CLEANUP_INTERVAL)
  4112. def start_expected_prints_cleanup() -> None:
  4113. global _expected_prints_cleanup_task
  4114. if _expected_prints_cleanup_task is None:
  4115. _expected_prints_cleanup_task = asyncio.create_task(_expected_prints_cleanup_loop())
  4116. logging.getLogger(__name__).info("Expected prints cleanup started")
  4117. def stop_expected_prints_cleanup() -> None:
  4118. global _expected_prints_cleanup_task
  4119. if _expected_prints_cleanup_task:
  4120. _expected_prints_cleanup_task.cancel()
  4121. _expected_prints_cleanup_task = None
  4122. logging.getLogger(__name__).info("Expected prints cleanup stopped")
  4123. # ---------------------------------------------------------------------------
  4124. # L-2: Periodic auth-token cleanup (stale TOTP + expired revoked JTIs)
  4125. # ---------------------------------------------------------------------------
  4126. _auth_cleanup_task: asyncio.Task | None = None
  4127. _AUTH_CLEANUP_INTERVAL = 3600 # seconds (hourly)
  4128. async def _run_auth_cleanup() -> None:
  4129. """Single cleanup pass: remove stale TOTP records, expired revoked JTIs, and old rate-limit events."""
  4130. from backend.app.core.database import async_session
  4131. from backend.app.models.auth_ephemeral import AuthEphemeralToken, AuthRateLimitEvent
  4132. from backend.app.models.user_totp import UserTOTP
  4133. now = datetime.now(timezone.utc)
  4134. # Remove unconfirmed (is_enabled=False) TOTP records older than 1 hour.
  4135. try:
  4136. async with async_session() as db:
  4137. stale_cutoff = now - timedelta(hours=1)
  4138. result = await db.execute(
  4139. select(UserTOTP).where(
  4140. UserTOTP.is_enabled.is_(False),
  4141. UserTOTP.created_at < stale_cutoff,
  4142. )
  4143. )
  4144. stale_records = result.scalars().all()
  4145. if stale_records:
  4146. for rec in stale_records:
  4147. await db.delete(rec)
  4148. await db.commit()
  4149. logging.info("Auth cleanup: removed %d stale unconfirmed TOTP record(s)", len(stale_records))
  4150. except Exception as e:
  4151. logging.warning("Auth cleanup: failed to purge stale TOTP records: %s", e)
  4152. # Remove expired revoked-JTI entries (they are no longer needed once the
  4153. # original token's exp has passed — the token would be rejected by JWT
  4154. # signature verification regardless).
  4155. try:
  4156. async with async_session() as db:
  4157. await db.execute(
  4158. delete(AuthEphemeralToken).where(
  4159. AuthEphemeralToken.token_type == "revoked_jti",
  4160. AuthEphemeralToken.expires_at < now,
  4161. )
  4162. )
  4163. await db.commit()
  4164. except Exception as e:
  4165. logging.warning("Auth cleanup: failed to purge expired revoked JTIs: %s", e)
  4166. # L-R6-B: Purge AuthRateLimitEvent rows older than the lockout window (15 min).
  4167. # Events outside this window can never affect rate-limit decisions — they only
  4168. # consume DB space. Use the same window constant as the rate limiter so the
  4169. # two are always in sync.
  4170. try:
  4171. from backend.app.api.routes.mfa import LOCKOUT_WINDOW
  4172. async with async_session() as db:
  4173. await db.execute(
  4174. delete(AuthRateLimitEvent).where(
  4175. AuthRateLimitEvent.occurred_at < now - LOCKOUT_WINDOW,
  4176. )
  4177. )
  4178. await db.commit()
  4179. except Exception as e:
  4180. logging.warning("Auth cleanup: failed to purge stale rate-limit events: %s", e)
  4181. async def _auth_cleanup_loop() -> None:
  4182. """Periodic background task: run auth cleanup every hour."""
  4183. while True:
  4184. try:
  4185. await _run_auth_cleanup()
  4186. except asyncio.CancelledError:
  4187. break
  4188. except Exception as e:
  4189. logging.warning("Auth cleanup loop error: %s", e)
  4190. await asyncio.sleep(_AUTH_CLEANUP_INTERVAL)
  4191. def start_auth_cleanup() -> None:
  4192. global _auth_cleanup_task
  4193. if _auth_cleanup_task is None:
  4194. _auth_cleanup_task = asyncio.create_task(_auth_cleanup_loop())
  4195. logging.getLogger(__name__).info("Auth periodic cleanup started")
  4196. def stop_auth_cleanup() -> None:
  4197. global _auth_cleanup_task
  4198. if _auth_cleanup_task:
  4199. _auth_cleanup_task.cancel()
  4200. _auth_cleanup_task = None
  4201. logging.getLogger(__name__).info("Auth periodic cleanup stopped")
  4202. @asynccontextmanager
  4203. async def lifespan(app: FastAPI):
  4204. # Startup
  4205. # Install Windows-only asyncio Proactor cleanup-RST filter (#1113) before
  4206. # anything else can spawn tasks that might trip it.
  4207. from backend.app.core.asyncio_handlers import install_proactor_reset_filter
  4208. install_proactor_reset_filter()
  4209. await init_db()
  4210. # Register an app-scoped httpx client for Bambu Cloud services so
  4211. # per-request BambuCloudService instances reuse the same connection pool
  4212. # (important for routes like /cloud/filament-info that chain many
  4213. # get_setting_detail calls). The shared client stores no region/token
  4214. # state, so the per-request ownership pattern that fixed the region-bleed
  4215. # bug is preserved.
  4216. import httpx as _httpx
  4217. from backend.app.services.bambu_cloud import set_shared_http_client
  4218. from backend.app.services.makerworld import (
  4219. set_shared_http_client as set_shared_makerworld_http_client,
  4220. )
  4221. _shared_cloud_http_client = _httpx.AsyncClient(timeout=30.0)
  4222. set_shared_http_client(_shared_cloud_http_client)
  4223. # Reuse the same connection pool for MakerWorld — different host, same
  4224. # keep-alive pool saves a TLS handshake per request.
  4225. set_shared_makerworld_http_client(_shared_cloud_http_client)
  4226. # Fix queue items stuck with invalid "aborted" status (should be "cancelled").
  4227. # This can happen when a print was cancelled mid-print on versions before this fix.
  4228. try:
  4229. async with async_session() as db:
  4230. from backend.app.models.print_queue import PrintQueueItem
  4231. result = await db.execute(select(PrintQueueItem).where(PrintQueueItem.status == "aborted"))
  4232. aborted_items = result.scalars().all()
  4233. if aborted_items:
  4234. for item in aborted_items:
  4235. item.status = "cancelled"
  4236. await db.commit()
  4237. logging.info("Fixed %d queue item(s) with invalid 'aborted' status → 'cancelled'", len(aborted_items))
  4238. except Exception as e:
  4239. logging.warning("Failed to fix aborted queue items: %s", e)
  4240. # Restore debug logging state from previous session
  4241. await init_debug_logging()
  4242. # Set up printer manager callbacks
  4243. loop = asyncio.get_event_loop()
  4244. printer_manager.set_event_loop(loop)
  4245. printer_manager.set_status_change_callback(on_printer_status_change)
  4246. printer_manager.set_print_start_callback(on_print_start)
  4247. printer_manager.set_print_complete_callback(on_print_complete)
  4248. printer_manager.set_print_running_observed_callback(on_print_running_observed)
  4249. printer_manager.set_ams_change_callback(on_ams_change)
  4250. # Rehydrate persisted awaiting-plate-clear gate (#961) so prompts survive restarts
  4251. await printer_manager.load_awaiting_plate_clear_from_db()
  4252. # Layer change callback for external camera timelapse
  4253. async def on_layer_change(printer_id: int, layer_num: int):
  4254. """Capture timelapse frame on layer change + first layer notification."""
  4255. from backend.app.services.layer_timelapse import on_layer_change as tl_layer_change
  4256. await tl_layer_change(printer_id, layer_num)
  4257. # First layer complete notification (layer_num >= 2 means layer 1 is done)
  4258. if 2 <= layer_num <= 5 and not _first_layer_notified.get(printer_id, False):
  4259. _first_layer_notified[printer_id] = True
  4260. try:
  4261. async with async_session() as db:
  4262. from backend.app.models.printer import Printer
  4263. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  4264. printer = result.scalar_one_or_none()
  4265. if not printer:
  4266. return
  4267. printer_name = printer.name
  4268. client = printer_manager.get_client(printer_id)
  4269. state = client.state if client else None
  4270. filename = (state.subtask_name or state.gcode_file or "Unknown") if state else "Unknown"
  4271. total_layers = state.total_layers if state else 0
  4272. image_data = await _capture_snapshot_for_notification(
  4273. printer_id, printer, logging.getLogger(__name__)
  4274. )
  4275. await notification_service.on_first_layer_complete(
  4276. printer_id, printer_name, filename, total_layers, db, image_data=image_data
  4277. )
  4278. except Exception as e:
  4279. logging.getLogger(__name__).warning("First layer notification failed: %s", e)
  4280. printer_manager.set_layer_change_callback(on_layer_change)
  4281. # Event-driven bed cooldown: fires whenever bed_temper arrives via MQTT
  4282. async def on_bed_temp_update(printer_id: int, bed_temp: float):
  4283. waiter = _bed_cool_waiters.get(printer_id)
  4284. if not waiter:
  4285. return
  4286. threshold = waiter["threshold"]
  4287. if bed_temp > threshold:
  4288. return
  4289. # Bed is at or below threshold — fire notification and remove waiter
  4290. waiter_info = _bed_cool_waiters.pop(printer_id, None)
  4291. if not waiter_info:
  4292. return # Another callback already handled it
  4293. bed_cool_logger = logging.getLogger(__name__)
  4294. bed_cool_logger.info(
  4295. "[BED-COOL] Bed cooled to %.1f°C on printer %s (threshold: %.0f°C)",
  4296. bed_temp,
  4297. printer_id,
  4298. threshold,
  4299. )
  4300. try:
  4301. printer_info = printer_manager.get_printer(printer_id)
  4302. p_name = printer_info.name if printer_info else "Unknown"
  4303. async with async_session() as db:
  4304. await notification_service.on_bed_cooled(
  4305. printer_id=printer_id,
  4306. printer_name=p_name,
  4307. bed_temp=bed_temp,
  4308. threshold=threshold,
  4309. filename=waiter_info["filename"],
  4310. db=db,
  4311. )
  4312. except Exception as e:
  4313. bed_cool_logger.warning("[BED-COOL] Failed to send notification: %s", e)
  4314. printer_manager.set_bed_temp_update_callback(on_bed_temp_update)
  4315. async def on_drying_complete(printer_id: int, ams_id: int):
  4316. """Smart-plug auto-off-after-drying trigger (#1349).
  4317. Fires once per AMS unit when ``dry_time`` falls from >0 to 0. The
  4318. manager walks all plugs linked to this printer and turns off only
  4319. the ones with ``auto_off_after_drying`` enabled, after their
  4320. per-plug delay. Multiple AMS units finishing close together (e.g. a
  4321. dual-AMS dry that ends within the same MQTT push) call this once
  4322. per unit — the manager's ``_cancel_pending_off`` collapses
  4323. repeated scheduling on the same plug to one timer, so duplicate
  4324. fires are safe.
  4325. """
  4326. try:
  4327. async with async_session() as db:
  4328. await smart_plug_manager.on_drying_complete(printer_id, db)
  4329. except Exception as e:
  4330. logging.getLogger(__name__).warning(
  4331. "Failed to schedule auto-off-after-drying for printer %d (AMS %d): %s",
  4332. printer_id,
  4333. ams_id,
  4334. e,
  4335. )
  4336. printer_manager.set_drying_complete_callback(on_drying_complete)
  4337. # Initialize MQTT relay from settings
  4338. async with async_session() as db:
  4339. from backend.app.api.routes.settings import get_setting
  4340. mqtt_settings = {
  4341. "mqtt_enabled": (await get_setting(db, "mqtt_enabled") or "false") == "true",
  4342. "mqtt_broker": await get_setting(db, "mqtt_broker") or "",
  4343. "mqtt_port": int(await get_setting(db, "mqtt_port") or "1883"),
  4344. "mqtt_username": await get_setting(db, "mqtt_username") or "",
  4345. "mqtt_password": await get_setting(db, "mqtt_password") or "",
  4346. "mqtt_topic_prefix": await get_setting(db, "mqtt_topic_prefix") or "bambuddy",
  4347. "mqtt_use_tls": (await get_setting(db, "mqtt_use_tls") or "false") == "true",
  4348. }
  4349. await mqtt_relay.configure(mqtt_settings)
  4350. # Restore MQTT smart plug subscriptions
  4351. if mqtt_settings.get("mqtt_enabled"):
  4352. from backend.app.models.smart_plug import SmartPlug
  4353. from backend.app.services.mqtt_smart_plug import subscribe_plug_to_mqtt
  4354. result = await db.execute(select(SmartPlug).where(SmartPlug.plug_type == "mqtt"))
  4355. mqtt_plugs = result.scalars().all()
  4356. restored = 0
  4357. for plug in mqtt_plugs:
  4358. if subscribe_plug_to_mqtt(mqtt_relay.smart_plug_service, plug):
  4359. restored += 1
  4360. if restored:
  4361. logging.info("Restored %s MQTT smart plug subscriptions", restored)
  4362. # Connect to all active printers
  4363. async with async_session() as db:
  4364. await init_printer_connections(db)
  4365. # Auto-connect to Spoolman if enabled
  4366. async with async_session() as db:
  4367. from backend.app.api.routes.settings import get_setting
  4368. spoolman_enabled = await get_setting(db, "spoolman_enabled")
  4369. spoolman_url = await get_setting(db, "spoolman_url")
  4370. if spoolman_enabled and spoolman_enabled.lower() == "true" and spoolman_url:
  4371. try:
  4372. client = await init_spoolman_client(spoolman_url)
  4373. if await client.health_check():
  4374. logging.info("Auto-connected to Spoolman at %s", spoolman_url)
  4375. # Ensure the 'tag' extra field exists for RFID/UUID storage
  4376. field_ok = await client.ensure_tag_extra_field()
  4377. if not field_ok:
  4378. logging.error("Spoolman tag extra field registration failed — NFC tag links may not persist")
  4379. # Register the BambuStudio slicer-preset fields used by the
  4380. # spool-edit / assign flow. Spoolman rejects PATCHes with
  4381. # unknown extra keys, so these must exist before any update
  4382. # that touches them.
  4383. for field_name in ("bambu_slicer_filament", "bambu_slicer_filament_name"):
  4384. if not await client.ensure_extra_field(field_name):
  4385. logging.warning(
  4386. "Spoolman extra field %r registration failed — "
  4387. "spool slicer-preset edits will return 502",
  4388. field_name,
  4389. )
  4390. else:
  4391. logging.warning("Spoolman at %s is not reachable", spoolman_url)
  4392. except Exception as e:
  4393. logging.warning("Failed to auto-connect to Spoolman: %s", e)
  4394. # Start the print scheduler
  4395. asyncio.create_task(print_scheduler.run())
  4396. # Start background dispatch worker for send/start operations
  4397. await background_dispatch.start()
  4398. # Start the smart plug scheduler for time-based on/off
  4399. smart_plug_manager.start_scheduler()
  4400. # Resume any pending auto-offs that were interrupted by restart
  4401. await smart_plug_manager.resume_pending_auto_offs()
  4402. # Start the notification digest scheduler
  4403. notification_service.start_digest_scheduler()
  4404. # Start the GitHub backup scheduler
  4405. await github_backup_service.start_scheduler()
  4406. # Start the local backup scheduler
  4407. await local_backup_service.start_scheduler()
  4408. await obico_detection_service.start()
  4409. # Start the library trash sweeper (#1008)
  4410. await library_trash_service.start_scheduler()
  4411. # Start the archive auto-purge sweeper (#1008 follow-up)
  4412. await archive_purge_service.start_scheduler()
  4413. # Start AMS history recording
  4414. start_ams_history_recording()
  4415. # Start printer runtime tracking
  4416. start_runtime_tracking()
  4417. # Start SpoolBuddy device watchdog
  4418. start_spoolbuddy_watchdog()
  4419. # Start camera stream orphan cleanup
  4420. start_camera_cleanup()
  4421. # Start expected-print TTL eviction (prevents memory leak when prints are
  4422. # registered but on_print_start never fires)
  4423. start_expected_prints_cleanup()
  4424. # L-2: Start periodic auth cleanup (stale TOTP + expired revoked JTIs)
  4425. start_auth_cleanup()
  4426. # Event-loop stall watchdog: dumps all thread stacks to stderr if the loop
  4427. # freezes (#1486 — silent "container hangs after adding a printer" reports).
  4428. from backend.app.services.loop_watchdog import start_loop_watchdog
  4429. start_loop_watchdog()
  4430. # Initialize virtual printer manager and sync from DB
  4431. from backend.app.services.virtual_printer import virtual_printer_manager
  4432. virtual_printer_manager.set_session_factory(async_session)
  4433. virtual_printer_manager.set_printer_manager(printer_manager)
  4434. try:
  4435. await virtual_printer_manager.sync_from_db()
  4436. logging.info("Virtual printer manager synced from database")
  4437. except Exception as e:
  4438. logging.warning("Failed to sync virtual printers: %s", e)
  4439. yield
  4440. # Shutdown
  4441. print_scheduler.stop()
  4442. await background_dispatch.stop()
  4443. smart_plug_manager.stop_scheduler()
  4444. notification_service.stop_digest_scheduler()
  4445. github_backup_service.stop_scheduler()
  4446. local_backup_service.stop_scheduler()
  4447. library_trash_service.stop_scheduler()
  4448. archive_purge_service.stop_scheduler()
  4449. obico_detection_service.stop()
  4450. stop_ams_history_recording()
  4451. stop_runtime_tracking()
  4452. stop_spoolbuddy_watchdog()
  4453. stop_camera_cleanup()
  4454. from backend.app.services.loop_watchdog import stop_loop_watchdog
  4455. stop_loop_watchdog()
  4456. # Tear down all camera fan-out broadcasters (#1089) so subscribers exit
  4457. # cleanly rather than waiting on a queue that nothing will ever fill.
  4458. try:
  4459. from backend.app.services.camera_fanout import shutdown_all_broadcasters
  4460. await shutdown_all_broadcasters()
  4461. except Exception as e:
  4462. logging.warning("Failed to shut down camera broadcasters: %s", e)
  4463. stop_expected_prints_cleanup()
  4464. stop_auth_cleanup()
  4465. printer_manager.disconnect_all()
  4466. await close_spoolman_client()
  4467. # Stop all virtual printer services
  4468. await virtual_printer_manager.stop_all()
  4469. await mqtt_smart_plug_service.disconnect(timeout=2)
  4470. await mqtt_relay.disconnect(timeout=2)
  4471. # Drop the shared Bambu Cloud HTTP client we registered at startup.
  4472. set_shared_http_client(None)
  4473. set_shared_makerworld_http_client(None)
  4474. await _shared_cloud_http_client.aclose()
  4475. # Checkpoint WAL (SQLite only) and close all database connections
  4476. from backend.app.core.db_dialect import is_sqlite
  4477. if is_sqlite():
  4478. try:
  4479. async with engine.begin() as conn:
  4480. await conn.execute(text("PRAGMA wal_checkpoint(TRUNCATE)"))
  4481. logging.info("WAL checkpoint completed")
  4482. except Exception as e:
  4483. logging.warning("WAL checkpoint failed: %s", e)
  4484. await engine.dispose()
  4485. app = FastAPI(
  4486. title=app_settings.app_name,
  4487. description="Archive and manage Bambu Lab 3MF files",
  4488. version=APP_VERSION,
  4489. lifespan=lifespan,
  4490. )
  4491. # =============================================================================
  4492. # Authentication Middleware - Secures ALL API routes by default
  4493. # =============================================================================
  4494. # Public routes that don't require authentication even when auth is enabled
  4495. PUBLIC_API_ROUTES = {
  4496. # Auth routes needed before/during login
  4497. "/api/v1/auth/status",
  4498. "/api/v1/auth/login",
  4499. "/api/v1/auth/setup", # Needed for initial setup and recovery
  4500. # Advanced auth status needed for login page
  4501. "/api/v1/auth/advanced-auth/status",
  4502. "/api/v1/auth/forgot-password", # Password reset for advanced auth
  4503. "/api/v1/auth/forgot-password/confirm", # Complete password reset with token (H-6)
  4504. # 2FA routes that are called BEFORE a JWT is issued (pre-auth flow)
  4505. "/api/v1/auth/2fa/verify", # Exchange pre_auth_token + 2FA code for JWT
  4506. "/api/v1/auth/2fa/email/send", # Send OTP email (pre_auth_token based)
  4507. # OIDC routes that must be reachable without a JWT
  4508. "/api/v1/auth/oidc/providers", # Public list of enabled providers
  4509. "/api/v1/auth/oidc/callback", # Redirect target from OIDC provider
  4510. "/api/v1/auth/oidc/exchange", # Exchange short-lived OIDC token for JWT
  4511. # Version check for updates (no sensitive data)
  4512. "/api/v1/updates/version",
  4513. # Metrics endpoint handles its own prometheus_token authentication
  4514. "/api/v1/metrics",
  4515. }
  4516. # Route prefixes that are public (for routes with dynamic segments)
  4517. PUBLIC_API_PREFIXES = [
  4518. # WebSocket connections handle their own auth
  4519. "/api/v1/ws",
  4520. # OIDC authorize redirects — include provider_id in path
  4521. "/api/v1/auth/oidc/authorize/",
  4522. ]
  4523. # Route patterns that are public (read-only display data)
  4524. # These are checked with "in path" - needed because browsers load images/videos
  4525. # via <img src> and <video src> which don't include Authorization headers
  4526. PUBLIC_API_PATTERNS = [
  4527. # Thumbnails
  4528. "/thumbnail", # /archives/{id}/thumbnail, /library/files/{id}/thumbnail
  4529. "/plate-thumbnail/", # /archives/{id}/plate-thumbnail/{plate_id}
  4530. # Images and media
  4531. "/photos/", # /archives/{id}/photos/{filename}
  4532. "/project-image/", # /archives/{id}/project-image/{path}
  4533. "/qrcode", # /archives/{id}/qrcode
  4534. "/timelapse", # /archives/{id}/timelapse (video)
  4535. "/cover", # /printers/{id}/cover
  4536. "/icon", # /external-links/{id}/icon
  4537. # Camera (streams loaded via <img> tag)
  4538. "/camera/stream", # /printers/{id}/camera/stream
  4539. "/camera/snapshot", # /printers/{id}/camera/snapshot
  4540. # Slicer token-authenticated downloads — protocol handlers (bambustudioopen://,
  4541. # orcaslicer://) cannot send auth headers. These endpoints validate a short-lived
  4542. # download token in the URL path instead.
  4543. "/dl/", # /archives/{id}/dl/{token}/{filename}, /library/files/{id}/dl/{token}/{filename}
  4544. # Obico ML API fetches JPEG frames by one-shot nonce (issue #172 follow-up).
  4545. # The nonce itself is the credential: 32-byte random, single-use, ~30s TTL.
  4546. "/obico/cached-frame/", # /obico/cached-frame/{nonce}
  4547. ]
  4548. _security_headers_logger = logging.getLogger("backend.app.main.security_headers")
  4549. def _parse_trusted_frame_origins() -> tuple[str, ...]:
  4550. """Parse TRUSTED_FRAME_ORIGINS env var into a validated allowlist (#1191).
  4551. Format: comma-separated list of ``scheme://host[:port]`` origins.
  4552. Used by ``security_headers_middleware`` to relax ``frame-ancestors`` for
  4553. trusted same-LAN deployments (e.g. Home Assistant Webpage panel embedding
  4554. Bambuddy from a different port). Defaults to empty — strict ``'none'``.
  4555. Invalid entries are dropped with a warning rather than failing startup, so
  4556. a typo in one origin doesn't take the whole deployment down.
  4557. """
  4558. raw = os.environ.get("TRUSTED_FRAME_ORIGINS", "").strip()
  4559. if not raw:
  4560. return ()
  4561. valid: list[str] = []
  4562. for item in raw.split(","):
  4563. candidate = item.strip()
  4564. if not candidate:
  4565. continue
  4566. try:
  4567. parsed = urlparse(candidate)
  4568. except ValueError as e:
  4569. _security_headers_logger.warning("TRUSTED_FRAME_ORIGINS: dropping %r — %s", candidate, e)
  4570. continue
  4571. if parsed.scheme not in ("http", "https"):
  4572. _security_headers_logger.warning("TRUSTED_FRAME_ORIGINS: dropping %r — must be http(s)", candidate)
  4573. continue
  4574. if not parsed.netloc:
  4575. _security_headers_logger.warning("TRUSTED_FRAME_ORIGINS: dropping %r — missing host", candidate)
  4576. continue
  4577. if parsed.path and parsed.path != "/":
  4578. _security_headers_logger.warning("TRUSTED_FRAME_ORIGINS: dropping %r — paths not allowed", candidate)
  4579. continue
  4580. if parsed.query or parsed.fragment:
  4581. _security_headers_logger.warning(
  4582. "TRUSTED_FRAME_ORIGINS: dropping %r — query/fragment not allowed", candidate
  4583. )
  4584. continue
  4585. if "*" in parsed.netloc:
  4586. _security_headers_logger.warning("TRUSTED_FRAME_ORIGINS: dropping %r — wildcards not allowed", candidate)
  4587. continue
  4588. valid.append(f"{parsed.scheme}://{parsed.netloc}")
  4589. if valid:
  4590. _security_headers_logger.info("TRUSTED_FRAME_ORIGINS: %s", ", ".join(valid))
  4591. return tuple(valid)
  4592. _TRUSTED_FRAME_ORIGINS: tuple[str, ...] = _parse_trusted_frame_origins()
  4593. def _frame_ancestors(default_value: str) -> str:
  4594. """Compose the ``frame-ancestors`` CSP directive (#1191).
  4595. ``default_value`` is the strict directive used when the operator has not
  4596. configured ``TRUSTED_FRAME_ORIGINS`` — typically ``'none'`` (catch-all and
  4597. docs) or ``'self'`` (gcode-viewer, served same-origin). When trusted origins
  4598. are configured, ``'self'`` is always included so same-origin embedding never
  4599. breaks even if an operator forgets to add their own origin to the list.
  4600. """
  4601. if _TRUSTED_FRAME_ORIGINS:
  4602. return "frame-ancestors 'self' " + " ".join(_TRUSTED_FRAME_ORIGINS) + ";"
  4603. return f"frame-ancestors {default_value};"
  4604. @app.middleware("http")
  4605. async def security_headers_middleware(request, call_next):
  4606. """Add standard HTTP security headers to every response."""
  4607. # Per-request nonce stamped into `script-src` (#1460). On its own this
  4608. # changes nothing for Bambuddy's own pages — index.html has no inline
  4609. # scripts since the SW registration moved to /sw-register.js. The reason
  4610. # it's here is Cloudflare: a CF-fronted deployment has the bot-detection
  4611. # script injected into the HTML on the edge, with a fresh hash on every
  4612. # load (so hashes can't be allowlisted). When CF sees a nonce in our CSP,
  4613. # it clones the same nonce onto its injected <script>, and the inline
  4614. # script passes the policy without us needing 'unsafe-inline'. See
  4615. # https://developers.cloudflare.com/cloudflare-challenges/challenge-types/javascript-detections/#if-you-have-a-content-security-policy-csp
  4616. csp_nonce = secrets.token_urlsafe(16)
  4617. response = await call_next(request)
  4618. response.headers["X-Content-Type-Options"] = "nosniff"
  4619. # X-Frame-Options is the legacy cross-origin embedding control. Modern
  4620. # browsers honour CSP frame-ancestors instead, and the legacy
  4621. # `ALLOW-FROM <url>` syntax is deprecated and inconsistent across vendors.
  4622. # When operators have explicitly allowlisted trusted frame origins (#1191
  4623. # — typically Home Assistant on a different port), drop X-Frame-Options
  4624. # and let the CSP-side frame-ancestors directive govern embedding.
  4625. if not _TRUSTED_FRAME_ORIGINS:
  4626. response.headers["X-Frame-Options"] = "SAMEORIGIN"
  4627. response.headers["Referrer-Policy"] = "strict-origin-when-cross-origin"
  4628. # Content-Security-Policy for the React SPA.
  4629. # Notes:
  4630. # - 'unsafe-inline' for style-src: React and UI libs inject inline styles at runtime.
  4631. # - connect-src ws:/wss:: MQTT/printer WebSocket connections.
  4632. # - img-src data: / blob:: base64 thumbnails and Blob-URL timelapse previews.
  4633. # - media-src blob:: timelapse video player uses Blob URLs.
  4634. # - font-src data:: some icon fonts are embedded as data URIs.
  4635. if request.url.path.startswith("/gcode-viewer"):
  4636. # The gcode viewer is embedded in an iframe served by this same origin,
  4637. # so frame-ancestors must allow 'self'. prettygcode.js also uses eval()
  4638. # internally, so script-src needs 'unsafe-eval'.
  4639. response.headers["Content-Security-Policy"] = (
  4640. "default-src 'self'; "
  4641. "script-src 'self' 'unsafe-eval'; "
  4642. "style-src 'self' 'unsafe-inline'; "
  4643. "img-src 'self' data: blob:; "
  4644. "media-src 'self' blob:; "
  4645. "connect-src 'self' ws: wss:; "
  4646. "font-src 'self' data:; "
  4647. "object-src 'none'; "
  4648. "base-uri 'self'; "
  4649. "frame-src 'self' http: https:; " + _frame_ancestors("'self'")
  4650. )
  4651. elif request.url.path in ("/docs", "/redoc", "/docs/oauth2-redirect"):
  4652. # FastAPI's built-in Swagger UI / ReDoc pages load assets from
  4653. # cdn.jsdelivr.net and bootstrap with an inline <script>, so the
  4654. # default CSP would render a blank page.
  4655. response.headers["Content-Security-Policy"] = (
  4656. "default-src 'self'; "
  4657. "script-src 'self' 'unsafe-inline' https://cdn.jsdelivr.net; "
  4658. "style-src 'self' 'unsafe-inline' https://cdn.jsdelivr.net https://fonts.googleapis.com; "
  4659. "img-src 'self' data: blob: https://fastapi.tiangolo.com https://cdn.redoc.ly; "
  4660. "connect-src 'self'; "
  4661. "font-src 'self' data: https://fonts.gstatic.com; "
  4662. "worker-src 'self' blob:; "
  4663. "object-src 'none'; "
  4664. "base-uri 'self'; " + _frame_ancestors("'none'")
  4665. )
  4666. else:
  4667. response.headers["Content-Security-Policy"] = (
  4668. "default-src 'self'; "
  4669. f"script-src 'self' 'nonce-{csp_nonce}'; "
  4670. "style-src 'self' 'unsafe-inline'; "
  4671. "img-src 'self' data: blob:; "
  4672. "media-src 'self' blob:; "
  4673. "connect-src 'self' ws: wss:; "
  4674. "font-src 'self' data:; "
  4675. "object-src 'none'; "
  4676. "base-uri 'self'; "
  4677. "frame-src 'self' http: https:; " + _frame_ancestors("'none'")
  4678. )
  4679. if request.url.scheme == "https":
  4680. response.headers["Strict-Transport-Security"] = "max-age=31536000; includeSubDomains"
  4681. return response
  4682. @app.middleware("http")
  4683. async def auth_middleware(request, call_next):
  4684. """Enforce authentication on all API routes when auth is enabled.
  4685. This middleware provides defense-in-depth by checking auth at the API gateway level,
  4686. regardless of whether individual routes have auth dependencies.
  4687. """
  4688. from starlette.responses import JSONResponse
  4689. path = request.url.path
  4690. # Only apply to API routes
  4691. if not path.startswith("/api/"):
  4692. return await call_next(request)
  4693. # Allow public routes
  4694. if path in PUBLIC_API_ROUTES:
  4695. return await call_next(request)
  4696. # Allow public prefixes
  4697. for prefix in PUBLIC_API_PREFIXES:
  4698. if path.startswith(prefix):
  4699. return await call_next(request)
  4700. # Allow public patterns (read-only display data like thumbnails)
  4701. for pattern in PUBLIC_API_PATTERNS:
  4702. if pattern in path:
  4703. return await call_next(request)
  4704. # Check if auth is enabled
  4705. try:
  4706. async with async_session() as db:
  4707. from backend.app.core.auth import is_auth_enabled
  4708. auth_enabled = await is_auth_enabled(db)
  4709. if not auth_enabled:
  4710. # Auth disabled, allow all requests
  4711. return await call_next(request)
  4712. except Exception:
  4713. # If we can't check auth status, allow request (fail open for DB issues)
  4714. return await call_next(request)
  4715. # Auth is enabled - require valid token
  4716. auth_header = request.headers.get("Authorization")
  4717. x_api_key = request.headers.get("X-API-Key")
  4718. # Check for API key auth first
  4719. if x_api_key or (auth_header and auth_header.startswith("Bearer bb_")):
  4720. # API key authentication - let the request through to be validated by route handler
  4721. # API keys are validated per-route since they have different permission levels
  4722. return await call_next(request)
  4723. # Check for JWT auth
  4724. if not auth_header or not auth_header.startswith("Bearer "):
  4725. return JSONResponse(
  4726. status_code=401,
  4727. content={"detail": "Authentication required"},
  4728. headers={"WWW-Authenticate": "Bearer"},
  4729. )
  4730. # Validate JWT token
  4731. import jwt
  4732. try:
  4733. from backend.app.core.auth import (
  4734. ALGORITHM,
  4735. SECRET_KEY,
  4736. _is_token_fresh,
  4737. get_user_by_username,
  4738. is_jti_revoked,
  4739. )
  4740. token = auth_header.replace("Bearer ", "")
  4741. payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
  4742. username = payload.get("sub")
  4743. if not username:
  4744. raise ValueError("No username in token")
  4745. jti = payload.get("jti")
  4746. if not jti:
  4747. raise ValueError("No jti in token")
  4748. iat = payload.get("iat")
  4749. # Reject revoked tokens (defense-in-depth gateway check)
  4750. if await is_jti_revoked(jti):
  4751. return JSONResponse(
  4752. status_code=401,
  4753. content={"detail": "Token has been revoked"},
  4754. headers={"WWW-Authenticate": "Bearer"},
  4755. )
  4756. # Verify user exists, is active, and token is still fresh (L-R8-A)
  4757. async with async_session() as db:
  4758. user = await get_user_by_username(db, username)
  4759. if not user or not user.is_active:
  4760. return JSONResponse(
  4761. status_code=401,
  4762. content={"detail": "User not found or inactive"},
  4763. headers={"WWW-Authenticate": "Bearer"},
  4764. )
  4765. if not _is_token_fresh(iat, user):
  4766. return JSONResponse(
  4767. status_code=401,
  4768. content={"detail": "Token no longer valid"},
  4769. headers={"WWW-Authenticate": "Bearer"},
  4770. )
  4771. except jwt.ExpiredSignatureError:
  4772. return JSONResponse(
  4773. status_code=401,
  4774. content={"detail": "Token has expired"},
  4775. headers={"WWW-Authenticate": "Bearer"},
  4776. )
  4777. except (jwt.InvalidTokenError, ValueError, Exception):
  4778. return JSONResponse(
  4779. status_code=401,
  4780. content={"detail": "Invalid token"},
  4781. headers={"WWW-Authenticate": "Bearer"},
  4782. )
  4783. return await call_next(request)
  4784. @app.middleware("http")
  4785. async def trace_id_middleware(request, call_next):
  4786. """Stamp every HTTP request with a trace ID and echo it back.
  4787. Decorated AFTER auth_middleware on purpose: Starlette stacks
  4788. @app.middleware decorators LIFO, so the last-decorated runs first
  4789. inbound. Putting the trace stamp last makes it the OUTERMOST layer,
  4790. which means auth-middleware log lines (and every line emitted on the
  4791. way down to and back from the route handler) all carry the same
  4792. trace ID. If we put it before auth, auth's logs would be stamped
  4793. with the *previous* request's ID — useless for correlation.
  4794. Honours an inbound ``X-Trace-Id`` header so callers running their
  4795. own tracing can correlate their span IDs with our log lines, but
  4796. only if the value passes the whitelist gate in
  4797. ``backend.app.core.trace.normalise_inbound_trace_id`` — anything
  4798. rejected (too long, contains control chars, etc.) silently triggers
  4799. a freshly minted server-side ID rather than failing the request.
  4800. The minted (or echoed) ID is set on a ContextVar so that every log
  4801. record emitted during the request — application logs *and* uvicorn's
  4802. access log — carries it via TraceIDFilter, and is also written to
  4803. the ``X-Trace-Id`` response header so clients can pin a server-side
  4804. log search to the exact request they made.
  4805. """
  4806. from backend.app.core.trace import (
  4807. generate_trace_id,
  4808. normalise_inbound_trace_id,
  4809. trace_id_var,
  4810. )
  4811. inbound = normalise_inbound_trace_id(request.headers.get("X-Trace-Id"))
  4812. trace_id = inbound if inbound is not None else generate_trace_id()
  4813. token = trace_id_var.set(trace_id)
  4814. try:
  4815. response = await call_next(request)
  4816. finally:
  4817. # Reset the ContextVar so a record emitted in a totally
  4818. # unrelated background task that just happens to inherit this
  4819. # context doesn't keep referencing this request's ID forever.
  4820. # In practice ContextVar.reset is best-effort under asyncio
  4821. # task-spawn semantics, but the cost is one attribute write so
  4822. # we may as well do it.
  4823. trace_id_var.reset(token)
  4824. response.headers["X-Trace-Id"] = trace_id
  4825. return response
  4826. # API routes
  4827. app.include_router(auth.router, prefix=app_settings.api_prefix)
  4828. app.include_router(mfa.router, prefix=app_settings.api_prefix)
  4829. app.include_router(bug_report.router, prefix=app_settings.api_prefix)
  4830. app.include_router(users.router, prefix=app_settings.api_prefix)
  4831. app.include_router(groups.router, prefix=app_settings.api_prefix)
  4832. app.include_router(printers.router, prefix=app_settings.api_prefix)
  4833. app.include_router(archives.router, prefix=app_settings.api_prefix)
  4834. app.include_router(filaments.router, prefix=app_settings.api_prefix)
  4835. app.include_router(inventory.router, prefix=app_settings.api_prefix)
  4836. app.include_router(labels.router, prefix=app_settings.api_prefix)
  4837. app.include_router(settings_routes.router, prefix=app_settings.api_prefix)
  4838. app.include_router(cloud.router, prefix=app_settings.api_prefix)
  4839. app.include_router(local_presets.router, prefix=app_settings.api_prefix)
  4840. app.include_router(smart_plugs.router, prefix=app_settings.api_prefix)
  4841. app.include_router(print_log.router, prefix=app_settings.api_prefix)
  4842. app.include_router(print_queue.router, prefix=app_settings.api_prefix)
  4843. app.include_router(background_dispatch_routes.router, prefix=app_settings.api_prefix)
  4844. app.include_router(kprofiles.router, prefix=app_settings.api_prefix)
  4845. app.include_router(notifications.router, prefix=app_settings.api_prefix)
  4846. app.include_router(notification_templates.router, prefix=app_settings.api_prefix)
  4847. app.include_router(user_notifications.router, prefix=app_settings.api_prefix)
  4848. app.include_router(spoolman.router, prefix=app_settings.api_prefix)
  4849. app.include_router(spoolman_inventory.router, prefix=app_settings.api_prefix)
  4850. app.include_router(updates.router, prefix=app_settings.api_prefix)
  4851. app.include_router(maintenance.router, prefix=app_settings.api_prefix)
  4852. app.include_router(camera.router, prefix=app_settings.api_prefix)
  4853. app.include_router(external_links.router, prefix=app_settings.api_prefix)
  4854. app.include_router(projects.router, prefix=app_settings.api_prefix)
  4855. app.include_router(library.router, prefix=app_settings.api_prefix)
  4856. app.include_router(library_trash.router, prefix=app_settings.api_prefix)
  4857. app.include_router(slice_jobs.router, prefix=app_settings.api_prefix)
  4858. app.include_router(slicer_presets.router, prefix=app_settings.api_prefix)
  4859. app.include_router(archive_purge.router, prefix=app_settings.api_prefix)
  4860. app.include_router(makerworld.router, prefix=app_settings.api_prefix)
  4861. app.include_router(api_keys.router, prefix=app_settings.api_prefix)
  4862. app.include_router(webhook.router, prefix=app_settings.api_prefix)
  4863. app.include_router(ams_history.router, prefix=app_settings.api_prefix)
  4864. app.include_router(system.router, prefix=app_settings.api_prefix)
  4865. app.include_router(support.router, prefix=app_settings.api_prefix)
  4866. app.include_router(websocket.router, prefix=app_settings.api_prefix)
  4867. app.include_router(discovery.router, prefix=app_settings.api_prefix)
  4868. app.include_router(pending_uploads.router, prefix=app_settings.api_prefix)
  4869. app.include_router(firmware.router, prefix=app_settings.api_prefix)
  4870. app.include_router(github_backup.router, prefix=app_settings.api_prefix)
  4871. app.include_router(local_backup.router, prefix=app_settings.api_prefix)
  4872. app.include_router(obico.router, prefix=app_settings.api_prefix)
  4873. app.include_router(metrics.router, prefix=app_settings.api_prefix)
  4874. app.include_router(virtual_printers.router, prefix=app_settings.api_prefix)
  4875. app.include_router(spoolbuddy.router, prefix=app_settings.api_prefix)
  4876. # Serve static files (React build)
  4877. if app_settings.static_dir.exists() and any(app_settings.static_dir.iterdir()):
  4878. app.mount(
  4879. "/assets",
  4880. StaticFiles(directory=app_settings.static_dir / "assets"),
  4881. name="assets",
  4882. )
  4883. if (app_settings.static_dir / "img").exists():
  4884. app.mount(
  4885. "/img",
  4886. StaticFiles(directory=app_settings.static_dir / "img"),
  4887. name="img",
  4888. )
  4889. if (app_settings.static_dir / "icons").exists():
  4890. app.mount(
  4891. "/icons",
  4892. StaticFiles(directory=app_settings.static_dir / "icons"),
  4893. name="icons",
  4894. )
  4895. # Self-hosted Inter woff2 files (#1460). Without this mount /fonts/*.woff2
  4896. # falls through to the SPA catch-all and returns index.html, which the
  4897. # browser's font sanitizer rejects ("downloadable font: rejected by
  4898. # sanitizer").
  4899. if (app_settings.static_dir / "fonts").exists():
  4900. app.mount(
  4901. "/fonts",
  4902. StaticFiles(directory=app_settings.static_dir / "fonts"),
  4903. name="fonts",
  4904. )
  4905. @app.get("/")
  4906. async def serve_frontend():
  4907. """Serve the React frontend."""
  4908. index_file = app_settings.static_dir / "index.html"
  4909. if index_file.exists():
  4910. return FileResponse(index_file, headers=_HTML_CACHE_HEADERS)
  4911. return {
  4912. "message": "Bambuddy API",
  4913. "docs": "/docs",
  4914. "frontend": "Build and place React app in /static directory",
  4915. }
  4916. # index.html must always be revalidated — Vite emits content-hashed JS/CSS
  4917. # bundles (e.g. `index-JRaF_JhW.js`), so the JS itself is safe to cache
  4918. # forever, but the HTML wrapping it is the only file that knows which hash
  4919. # is current. Without explicit cache-control headers Chromium decides
  4920. # heuristically (typically 10% of the time since Last-Modified) and on
  4921. # long-running kiosks happily serves stale HTML across browser restarts.
  4922. # That stale HTML references an old bundle hash, the old bundle is also
  4923. # in the disk cache, and the user ends up running pre-update JS forever
  4924. # without ever knowing why. ``no-cache`` (revalidate every time, but a
  4925. # 304 is cheap) is the correct setting for an SPA's entry HTML.
  4926. _HTML_CACHE_HEADERS = {"Cache-Control": "no-cache, must-revalidate"}
  4927. @app.get("/health")
  4928. async def health_check():
  4929. """Health check endpoint."""
  4930. return {"status": "healthy"}
  4931. # GET + HEAD on the three PWA bootstrap routes (#1460). Scanners and a plain
  4932. # `curl -I` use HEAD; FastAPI's @app.get only registers GET, so HEAD answers
  4933. # with 405 Method Not Allowed and shows up as a "broken manifest" red herring
  4934. # in deployment debugging.
  4935. @app.api_route("/manifest.json", methods=["GET", "HEAD"])
  4936. async def serve_manifest():
  4937. """Serve PWA manifest."""
  4938. manifest_file = app_settings.static_dir / "manifest.json"
  4939. if manifest_file.exists():
  4940. return FileResponse(manifest_file, media_type="application/manifest+json")
  4941. return {"error": "Manifest not found"}
  4942. @app.api_route("/sw.js", methods=["GET", "HEAD"])
  4943. async def serve_service_worker():
  4944. """Serve service worker."""
  4945. sw_file = app_settings.static_dir / "sw.js"
  4946. if sw_file.exists():
  4947. return FileResponse(
  4948. sw_file,
  4949. media_type="application/javascript",
  4950. headers={"Cache-Control": "no-cache, no-store, must-revalidate"},
  4951. )
  4952. return {"error": "Service worker not found"}
  4953. @app.api_route("/sw-register.js", methods=["GET", "HEAD"])
  4954. async def serve_sw_register():
  4955. """Serve the service-worker registration bootstrap script.
  4956. Served as a real JS file so the strict `script-src 'self'` CSP covers it
  4957. without needing 'unsafe-inline' or per-build hashes on the inline tag.
  4958. """
  4959. reg_file = app_settings.static_dir / "sw-register.js"
  4960. if reg_file.exists():
  4961. return FileResponse(reg_file, media_type="application/javascript")
  4962. return {"error": "sw-register.js not found"}
  4963. # ── GCode viewer static files ────────────────────────────────────────────────
  4964. # Served via explicit routes so ordering is guaranteed (app.mount() loses
  4965. # to the /{full_path:path} catch-all in some Starlette versions).
  4966. _gcode_viewer_dir = (app_settings.static_dir.parent / "gcode_viewer").resolve()
  4967. # Surface packaging gaps at startup instead of as silent runtime 404s. If the
  4968. # directory is missing the explicit @app.get("/gcode-viewer/...") routes below
  4969. # return bare HTTPException(404) which renders as {"detail":"Not Found"} in
  4970. # the 3D Preview iframe (#1218) — easy to miss in normal operation, easy to
  4971. # spot if the operator scans the startup log or a support bundle.
  4972. if not (_gcode_viewer_dir / "index.html").is_file():
  4973. logging.getLogger(__name__).error(
  4974. "Embedded GCode viewer assets missing at %s — /gcode-viewer/ will return 404 "
  4975. "and 3D Preview will fail. This indicates a packaging bug; the gcode_viewer/ "
  4976. "directory must be present alongside static/.",
  4977. _gcode_viewer_dir,
  4978. )
  4979. def _gcode_viewer_response(rel: str) -> FileResponse:
  4980. from fastapi import HTTPException as _HTTPException
  4981. safe = (_gcode_viewer_dir / rel).resolve()
  4982. if not safe.is_relative_to(_gcode_viewer_dir):
  4983. raise _HTTPException(status_code=403)
  4984. if safe.is_file():
  4985. mt, _ = _mimetypes.guess_type(str(safe))
  4986. return FileResponse(str(safe), media_type=mt or "application/octet-stream")
  4987. raise _HTTPException(status_code=404)
  4988. @app.get("/gcode-viewer/")
  4989. async def serve_gcode_viewer_index() -> FileResponse:
  4990. """Raw PrettyGCode viewer for the iframe. The bare ``/gcode-viewer``
  4991. (no trailing slash) intentionally falls through to the SPA catch-all so a
  4992. full-page reload re-enters the React layout instead of serving the iframe
  4993. contents standalone."""
  4994. return _gcode_viewer_response("index.html")
  4995. @app.get("/gcode-viewer/{file_path:path}")
  4996. async def serve_gcode_viewer_file(file_path: str) -> FileResponse:
  4997. return _gcode_viewer_response(file_path)
  4998. # Catch-all route for React Router (must be last)
  4999. @app.get("/{full_path:path}")
  5000. async def serve_spa(full_path: str):
  5001. """Serve React app for client-side routing."""
  5002. # Don't intercept API routes - raise proper 404 so FastAPI can handle redirects
  5003. if full_path.startswith("api/"):
  5004. from fastapi import HTTPException
  5005. raise HTTPException(status_code=404, detail="Not found")
  5006. index_file = app_settings.static_dir / "index.html"
  5007. if index_file.exists():
  5008. return FileResponse(index_file, headers=_HTML_CACHE_HEADERS)
  5009. return {"error": "Frontend not built"}