views_1027_改成def.py 311 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774
  1. # 主業務邏輯中的視圖和路由的定義
  2. import os
  3. import datetime
  4. from flask import Flask, redirect, render_template, request, session, Response, jsonify
  5. from sqlalchemy.sql.expression import desc, false, null
  6. # 導入藍圖程序,用於構建路由
  7. from werkzeug.utils import redirect
  8. from werkzeug.wrappers import response
  9. from . import main
  10. from coffee_manage import mqtt
  11. # 導入db,用於操作數據庫
  12. from coffee_manage import db
  13. # 導入實體類,用於操作數據庫
  14. from ..models import *
  15. import json
  16. from datetime import datetime as dt
  17. from sqlalchemy import text
  18. import pymysql
  19. import pandas as pd
  20. from concurrent.futures import ThreadPoolExecutor
  21. import cv2
  22. import pickle
  23. import socket
  24. import time
  25. import threading
  26. import numpy as np
  27. import math
  28. from .mqtt import MQTT
  29. from matplotlib import pyplot as plt
  30. import random
  31. from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg as FigureCanvas
  32. import io
  33. from flask import make_response
  34. from distutils.util import strtobool # 將字串 string 轉成布林 boolean
  35. import psutil
  36. pool = ThreadPoolExecutor(25)
  37. s_sock = 0
  38. lock = threading.Lock()
  39. '''
  40. mydb = pymysql.connect(host='52.69.200.169', port=3306, user='coffee',
  41. password='skyeye', database='Coffee', charset='utf8')
  42. mycursor = mydb.cursor()
  43. '''
  44. # 主頁的訪問路徑
  45. @main.route('/')
  46. def main_index():
  47. # 獲取登入信息
  48. if 'id' in session and 'uname' in session and 'status' in session:
  49. username = session['uname']
  50. status = session['status']
  51. if status == 9:
  52. return render_template('signin_disable.html', params=locals())
  53. elif status == 8:
  54. return render_template('signin_new.html', params=locals())
  55. # Rita 參數 params 是用來取得參數的 locals=() 所有參數
  56. return render_template('index.html', params=locals())
  57. else:
  58. return render_template('sign_in.html')
  59. # Rita 測試
  60. # 使用者名稱測試
  61. @main.route('/test', methods=['GET', 'POST'])
  62. def test():
  63. info = request.args.to_dict()
  64. print(info)
  65. print("info[command]: ", info['command'])
  66. return json.dumps(info)
  67. # !!! AttributeError: 'NoneType' object has no attribute 'vacuum'
  68. @main.route('/demo', methods=['GET', 'POST'])
  69. def demo():
  70. if 'id' in session and 'uname' in session and 'status' in session:
  71. username = session['uname']
  72. status = session['status']
  73. if status == 9:
  74. return render_template('signin_disable.html', params=locals())
  75. elif status == 8:
  76. return render_template('signin_new.html', params=locals())
  77. clean_status = clean_container_status.query.order_by(text('datetime desc')).first()
  78. CI1 = clean_status.Clean_Input_1
  79. CI2 = clean_status.Clean_Input_2
  80. C1 = clean_status.Clean_Tank_1
  81. C2 = clean_status.Clean_Tank_2
  82. # 中水桶
  83. S1 = clean_status.ColorSelect_Tank_1
  84. S2 = clean_status.ColorSelect_Tank_2
  85. SOg1 = clean_status.ColorSelect_Output_g1
  86. SOb1 = clean_status.ColorSelect_Output_b1
  87. SOg2 = clean_status.ColorSelect_Output_g2
  88. SOb2 = clean_status.ColorSelect_Output_b2
  89. P1 = clean_status.Peel_Tank_1
  90. P2 = clean_status.Peel_Tank_2
  91. PO1 = clean_status.Peel_Output_1
  92. PO2 = clean_status.Peel_Output_2
  93. ferment_status = ferment_container_status.query.order_by(text('datetime desc')).first()
  94. F1 = ferment_status.Ferment_Tank_1
  95. F2 = ferment_status.Ferment_Tank_2
  96. dry_status = dry_container_status.query.order_by(text('datetime desc')).first()
  97. D1 = dry_status.Dry_Tank_1
  98. D2 = dry_status.Dry_Tank_2
  99. DO1 = dry_status.Dry_Output_1
  100. DO2 = dry_status.Dry_Output_2
  101. POb1 = clean_status.Peel_Output_b1
  102. return render_template('demo.html', title="[測試] DEMO 貨櫃自動化", **locals())
  103. else:
  104. return render_template('sign_in.html')
  105. # 乾燥槽自動化測試頁
  106. @main.route('/dry_auto', methods=['GET', 'POST'])
  107. def dry_auto():
  108. # 獲取登入信息
  109. if 'id' in session and 'uname' in session and 'status' in session:
  110. username = session['uname']
  111. status = session['status']
  112. dry_status = dry_container_status.query.order_by(text('datetime desc')).first()
  113. DI1 = dry_status.Dry_Input_1
  114. DI2 = dry_status.Dry_Input_2
  115. D1 = dry_status.Dry_Tank_1
  116. D2 = dry_status.Dry_Tank_2
  117. D3 = dry_status.Dry_Tank_3
  118. D4 = dry_status.Dry_Tank_4
  119. D5 = dry_status.Dry_Tank_5
  120. D6 = dry_status.Dry_Tank_6
  121. D7 = dry_status.Dry_Tank_7
  122. D8 = dry_status.Dry_Tank_8
  123. D9 = dry_status.Dry_Tank_9
  124. D10 = dry_status.Dry_Tank_10
  125. D11 = dry_status.Dry_Tank_11
  126. D12 = dry_status.Dry_Tank_12
  127. DO1 = dry_status.Dry_Output_1
  128. DO2 = dry_status.Dry_Output_2
  129. return render_template('dry_auto.html', title="[測試] 乾燥自動化", **locals())
  130. else:
  131. return render_template('sign_in.html')
  132. @main.route('/dry_auto_status', methods=['GET', 'POST'])
  133. def dry_auto_test():
  134. # [介面] 讓使用者可以選擇排程內有乾燥/清洗/校正, 此處預設為 True
  135. Dry_btn = True
  136. # 從資料庫資料表中取得最新狀態
  137. dry_status = dry_container_status.query.order_by(text('datetime desc')).first()
  138. DI1 = dry_status.Dry_Input_1
  139. DI2 = dry_status.Dry_Input_2
  140. D1 = dry_status.Dry_Tank_1
  141. D2 = dry_status.Dry_Tank_2
  142. D3 = dry_status.Dry_Tank_3
  143. D4 = dry_status.Dry_Tank_4
  144. D5 = dry_status.Dry_Tank_5
  145. D6 = dry_status.Dry_Tank_6
  146. D7 = dry_status.Dry_Tank_7
  147. D8 = dry_status.Dry_Tank_8
  148. D9 = dry_status.Dry_Tank_9
  149. D10 = dry_status.Dry_Tank_10
  150. D11 = dry_status.Dry_Tank_11
  151. D12 = dry_status.Dry_Tank_12
  152. DO1 = dry_status.Dry_Output_1
  153. DO2 = dry_status.Dry_Output_2
  154. FO1 = 'FO_Waiting' # 'FO_OutputtingBean'
  155. FO2 = 'FO_Waiting'
  156. D_UP_tanklist = [D1, D2, D3, D4, D5, D6]
  157. D_DOWN_tanklist = [D7, D8, D9, D10, D11, D12]
  158. # ----- 乾燥桶槽 D1~D6 入料暫停→入料判斷 ------------------------------
  159. Dry_InputtingBeanPause_number = int(D_UP_tanklist.count('D_InputtingBean_Pause'))
  160. # 若入料儲豆槽可出豆, 且 D1~D6 其中有桶槽入料暫停 (就是入料時桶槽豆子未滿 or 儲豆槽=='DI_Waiting' 已空) 時, 優先入豆
  161. if DI1 == 'DI_OutputtingBean' and Dry_InputtingBeanPause_number >= 1:
  162. if D1 == 'D_InputtingBean_Pause':
  163. D1 = 'D_InputtingBean'
  164. print('------- D1 狀態更新:入料中 -------')
  165. elif D2 == 'D_InputtingBean_Pause':
  166. D2 = 'D_InputtingBean'
  167. print('------- D2 狀態更新:入料中 -------')
  168. elif D3 == 'D_InputtingBean_Pause':
  169. D3 = 'D_InputtingBean'
  170. print('------- D3 狀態更新:入料中 -------')
  171. elif D4 == 'D_InputtingBean_Pause':
  172. D4 = 'D_InputtingBean'
  173. print('------- D4 狀態更新:入料中 -------')
  174. elif D5 == 'D_InputtingBean_Pause':
  175. D5 = 'D_InputtingBean'
  176. print('------- D5 狀態更新:入料中 -------')
  177. elif D6 == 'D_InputtingBean_Pause':
  178. D6 = 'D_InputtingBean'
  179. print('------- D6 狀態更新:入料中 -------')
  180. # ----- 乾燥桶槽 D1~D6 等待→入料判斷 ------------------------------
  181. # 目前乾燥槽 D1~D6 正在入豆的桶槽數量 (應小於一桶)
  182. Dry_InputtingBean_number = int(D_UP_tanklist.count('D_InputtingBean'))
  183. # [介面] 若未啟用 發酵 狀態
  184. if not Dry_btn:
  185. print('未指定乾燥流程')
  186. # 入料儲豆槽 非可出料狀態
  187. elif Dry_btn and DI1 != 'DI_OutputtingBean':
  188. print('入料儲豆槽非可出豆, 無法入料')
  189. # 若 D1~D6 乾燥桶槽為 入豆暫停, 則狀態改為入豆優先入豆
  190. elif Dry_btn and DI1 == 'DI_OutputtingBean' and Dry_InputtingBeanPause_number >= 1:
  191. print('乾燥槽 D', D_UP_tanklist.index('D_InputtingBean_Pause')+1, '優先入料中')
  192. print('------- D', D_UP_tanklist.index('D_InputtingBean_Pause')+1, ' 入料中 -------')
  193. # 若 D1~D6 發酵桶槽為 入豆, 則桶槽數字小者 優先入豆
  194. elif Dry_btn and DI1 == 'DI_OutputtingBean' and Dry_InputtingBean_number >= 1:
  195. print('乾燥槽 D', D_UP_tanklist.index('D_InputtingBean')+1, '優先入料中')
  196. print('------- D', D_UP_tanklist.index('D_InputtingBean')+1, ' 入料中 -------')
  197. elif Dry_btn and DI1 == 'DI_OutputtingBean' \
  198. and Dry_InputtingBean_number == 0 and Dry_InputtingBeanPause_number == 0:
  199. # 當達標準入料條件
  200. if D1 == 'D_Waiting':
  201. D1 = 'D_InputtingBean'
  202. print('------- D1 狀態更新:入料中 -------')
  203. elif D2 == 'D_Waiting':
  204. D2 = 'D_InputtingBean'
  205. print('------- D2 狀態更新:入料中 -------')
  206. elif D3 == 'D_Waiting':
  207. D3 = 'D_InputtingBean'
  208. print('------- D3 狀態更新:入料中 -------')
  209. elif D4 == 'D_Waiting':
  210. D4 = 'D_InputtingBean'
  211. print('------- D4 狀態更新:入料中 -------')
  212. elif D5 == 'D_Waiting':
  213. D5 = 'D_InputtingBean'
  214. print('------- D5 狀態更新:入料中 -------')
  215. elif D6 == 'D_Waiting':
  216. D6 = 'D_InputtingBean'
  217. print('------- D6 狀態更新:入料中 -------')
  218. # ----- 乾燥桶槽 D1~D6 入料→入料暫停判斷 ------------------------------
  219. Dry_InputtingBean_number = int(D_UP_tanklist.count('D_InputtingBean'))
  220. # -- 取得乾燥桶槽 D1~D6 超音波生豆高度 UltraSonic -------------
  221. D_UP_tank_UltraSonic = []
  222. for i in range(1, 7, 1):
  223. tank_UltraSonic = dry_tank_UltraSonic.query.filter_by(tank_num='D' + str(i)).order_by(text('datetime desc')).first()
  224. UltraSonic = int(tank_UltraSonic.UltraSonic)
  225. D_UP_tank_UltraSonic.append(UltraSonic)
  226. print('D_UP_tank_UltraSonic: ', D_UP_tank_UltraSonic)
  227. # -- 取得乾燥桶槽 D1~D6 超音波生豆高度 UltraSonic -------------
  228. if DI1 != 'DI_OutputtingBean' and Dry_InputtingBean_number >= 1:
  229. if D1 == 'D_InputtingBean' and D_UP_tank_UltraSonic[0] < 110:
  230. D1 = 'D_InputtingBean_Pause'
  231. print('------- D1 狀態更新:入料暫停 -------')
  232. else:
  233. D1 = 'D_InputtingBean_Finish'
  234. print('------- D1 狀態更新:入料完成 -------')
  235. if D2 == 'D_InputtingBean' and D_UP_tank_UltraSonic[1] < 110:
  236. D2 = 'D_InputtingBean_Pause'
  237. print('------- D2 狀態更新:入料暫停 -------')
  238. else:
  239. D2 = 'D_InputtingBean_Finish'
  240. print('------- D2 狀態更新:入料完成 -------')
  241. if D3 == 'D_InputtingBean' and D_UP_tank_UltraSonic[2] < 110:
  242. D3 = 'D_InputtingBean_Pause'
  243. print('------- D3 狀態更新:入料暫停 -------')
  244. else:
  245. D3 = 'D_InputtingBean_Finish'
  246. print('------- D3 狀態更新:入料完成 -------')
  247. if D4 == 'D_InputtingBean' and D_UP_tank_UltraSonic[3] < 110:
  248. D4 = 'D_InputtingBean_Pause'
  249. print('------- D4 狀態更新:入料暫停 -------')
  250. else:
  251. D4 = 'D_InputtingBean_Finish'
  252. print('------- D4 狀態更新:入料完成 -------')
  253. if D5 == 'D_InputtingBean' and D_UP_tank_UltraSonic[4] < 110:
  254. D5 = 'D_InputtingBean_Pause'
  255. print('------- D5 狀態更新:入料暫停 -------')
  256. else:
  257. D5 = 'D_InputtingBean_Finish'
  258. print('------- D5 狀態更新:入料完成 -------')
  259. if D6 == 'D_InputtingBean' and D_UP_tank_UltraSonic[5] < 110:
  260. D6 = 'D_InputtingBean_Pause'
  261. print('------- D6 狀態更新:入料暫停 -------')
  262. else:
  263. D6 = 'D_InputtingBean_Finish'
  264. print('------- D6 狀態更新:入料完成 -------')
  265. # # ----- 乾燥桶槽 D1~D6 入料完成→乾燥判斷 ------------------------------
  266. # Dry_InputtingBeanFinish_number = int(D_UP_tanklist.count('D_InputtingBean_Finish'))
  267. # # 乾燥條件:當前桶槽入豆達指定高度, 則開始乾燥 (可以多桶乾燥)
  268. # # !!! 乾燥槽 D_Drying 狀態應該請硬體判斷
  269. # # !!! 若狀態為 D_InputtingBean 且 超音波值>指定生豆高度, 狀態改為 D_Drying 乾燥
  270. # if D1 == 'D_InputtingBean_Finish' or D1 == 'D_InputtingBean_Pause' and D_UP_tank_UltraSonic[0] >= 110:
  271. # D1 = 'D_Drying'
  272. # print('------- D1 狀態更新:乾燥中 -------')
  273. # if D2 == 'D_InputtingBean_Finish' and D_UP_tank_UltraSonic[1] >= 110:
  274. # D2 = 'D_Drying'
  275. # print('------- D2 狀態更新:乾燥中 -------')
  276. # if D3 == 'D_InputtingBean_Finish' and D_UP_tank_UltraSonic[2] >= 110:
  277. # D3 = 'D_Drying'
  278. # print('------- D3 狀態更新:乾燥中 -------')
  279. # if D4 == 'D_InputtingBean_Finish' and D_UP_tank_UltraSonic[3] >= 110:
  280. # D4 = 'D_Drying'
  281. # print('------- D4 狀態更新:乾燥中 -------')
  282. # if D5 == 'D_InputtingBean_Finish' and D_UP_tank_UltraSonic[4] >= 110:
  283. # D5 = 'D_Drying'
  284. # print('------- D5 狀態更新:乾燥中 -------')
  285. # if D6 == 'D_InputtingBean_Finish' and D_UP_tank_UltraSonic[5] >= 110:
  286. # D6 = 'D_Drying'
  287. # print('------- D6 狀態更新:乾燥中 -------')
  288. # ----- 乾燥桶槽 D1~D6 發酵→可出豆 判斷 ------------------------------
  289. # !!! 發酵槽 D_OutputtingBean 狀態應該請硬體判斷
  290. # !!! 若狀態為 D_Drying 且 達指定乾燥條件, 狀態改為 D_OutputtingBean 可出豆
  291. # 可以多桶各自執行發酵排程
  292. # ----- [測試用] !!!!! D1~D6 入料完成→可出豆 判斷 ------------------------------
  293. if D1 == 'D_InputtingBean_Finish': D1 = 'D_OutputtingBean'
  294. if D2 == 'D_InputtingBean_Finish': D2 = 'D_OutputtingBean'
  295. if D3 == 'D_InputtingBean_Finish': D3 = 'D_OutputtingBean'
  296. if D4 == 'D_InputtingBean_Finish': D4 = 'D_OutputtingBean'
  297. if D5 == 'D_InputtingBean_Finish': D5 = 'D_OutputtingBean'
  298. if D6 == 'D_InputtingBean_Finish': D6 = 'D_OutputtingBean'
  299. # ----- [測試用] !!!!! D1~D6 入料完成→可出豆 判斷 ------------------------------
  300. # # ----- 乾燥桶槽 D1~D6 可出豆→出料儲豆槽 DO1 判斷 ------------------------------
  301. Dry_OutputtingBean_number = int(D_UP_tanklist.count('D_OutputtingBean'))
  302. # # !!! 若出料儲豆槽狀態為 DO_Waiting(空桶等待) 且 桶槽狀態為 D_OutputtingBean 可出豆, 與 DO1 配合開始出豆
  303. # if DO1 == 'DO_Waiting' and Dry_OutputtingBean_number >= 1:
  304. # if D1 == 'D_OutputtingBean':
  305. # DO1 = 'DO_InputtingBean'
  306. # print('------- D1 狀態更新:出豆中 -------')
  307. # print('------- DO1 狀態更新:入豆中 -------')
  308. # elif D2 == 'D_OutputtingBean':
  309. # DO1 = 'DO_InputtingBean'
  310. # print('------- D2 狀態更新:出豆中 -------')
  311. # print('------- DO1 狀態更新:入豆中 -------')
  312. # elif D3 == 'D_OutputtingBean':
  313. # DO1 = 'DO_InputtingBean'
  314. # print('------- D3 狀態更新:出豆中 -------')
  315. # print('------- DO1 狀態更新:入豆中 -------')
  316. # elif D4 == 'D_OutputtingBean':
  317. # DO1 = 'DO_InputtingBean'
  318. # print('------- D4 狀態更新:出豆中 -------')
  319. # print('------- DO1 狀態更新:入豆中 -------')
  320. # elif D5 == 'D_OutputtingBean':
  321. # DO1 = 'DO_InputtingBean'
  322. # print('------- D5 狀態更新:出豆中 -------')
  323. # print('------- DO1 狀態更新:入豆中 -------')
  324. # elif D6 == 'D_OutputtingBean':
  325. # DO1 = 'DO_InputtingBean'
  326. # print('------- D6 狀態更新:出豆中 -------')
  327. # print('------- DO1 狀態更新:入豆中 -------')
  328. # ----- 乾燥入料儲豆槽 DI1~DI2 入豆→可出豆判斷 ------------------------------
  329. # -- 取得乾燥入料儲豆槽 FI1~FI2 桶內高度 UltraSonic -------------
  330. DI_UP_UltraSoniclist = []
  331. for i in range(1, 3, 1):
  332. input_UltraSonic = dry_input_sensor.query.filter_by(tank_num='DI' + str(i)).order_by(text('datetime desc')).first()
  333. UltraSonic = int(input_UltraSonic.UltraSonic)
  334. DI_UP_UltraSoniclist.append(UltraSonic)
  335. print('DI_UP_UltraSoniclist: ', DI_UP_UltraSoniclist)
  336. # !!! 乾燥入料儲豆槽 D_OutputtingBean 狀態應該請硬體判斷
  337. # 入料儲豆槽等待中、且發酵槽出料儲豆槽可出豆時, 入料儲豆槽入豆
  338. if DI1 == 'DI_Waiting' and FO1 == 'FO_OutputtingBean':
  339. DI1 = 'DI_InputtingBean'
  340. print('------- DI1 狀態更新:入豆中 -------')
  341. # 入料儲豆槽入豆時 且 (發酵儲豆槽有入料 或者 前方儲豆槽非可出豆), 則停止入豆
  342. elif DI1 == 'DI_InputtingBean' and (DI_UP_UltraSoniclist[0] > 35) and (FO1 != 'FO_OutputtingBean') :
  343. DI1 = 'DI_OutputtingBean'
  344. print('------- DI1 狀態更新:可出豆 -------')
  345. # 若入料儲豆槽可出豆 且 儲豆槽內高度低於 40 且 前方脫皮機儲豆槽可出豆 且 目前無桶槽需入料時
  346. elif DI1 == 'DI_OutputtingBean' and (DI_UP_UltraSoniclist[0] < 40) and FO1 == 'FO_OutputtingBean' and Dry_InputtingBean_number == 0 and Dry_InputtingBeanPause_number == 0:
  347. DI1 = 'DI_InputtingBean'
  348. print('------- DI1 狀態更新:入豆中 -------')
  349. # if DI2 == 'DI_Waiting' and FO2 == 'FO_OutputtingBean':
  350. # DI2 = 'DI_InputtingBean'
  351. # print('------- DI2 狀態更新:入豆中 -------')
  352. # elif DI2 == 'DI_InputtingBean' and ((DI_DOWN_UltraSoniclist[1] > 35) or (FO2 == 'FO_OutputtingBean')) :
  353. # DI2 = 'DI_OutputtingBean'
  354. # print('------- DI2 狀態更新:可出豆 -------')
  355. # elif DI2 == 'DI_OutputtingBean' and (DI_DOWN_UltraSoniclist[1] < 40) and FO2 == 'FO_OutputtingBean' and Dry_InputtingBean_number == 0 and Dry_InputtingBeanPause_number == 0:
  356. # DI2 = 'DI_InputtingBean'
  357. # print('------- DI2 狀態更新:入豆中 -------')
  358. # ----- 乾燥出料儲豆槽 DO1~DO2 入豆→可出豆判斷 ------------------------------
  359. # -- 取得乾燥出料儲豆槽 DO1~DO2 桶內高度 UltraSonic -------------
  360. DO_UP_UltraSoniclist = []
  361. for i in range(1, 3, 1):
  362. output_UltraSonic = dry_output_sensor.query.filter_by(tank_num='DO' + str(i)).order_by(text('datetime desc')).first()
  363. UltraSonic = int(output_UltraSonic.UltraSonic)
  364. DO_UP_UltraSoniclist.append(UltraSonic)
  365. print('DO_UP_UltraSoniclist: ', DO_UP_UltraSoniclist)
  366. # 桶槽可出豆, 且出料儲豆槽等待時, 出料儲豆槽入豆
  367. # 參考 -- 乾燥桶槽 F1~F6 可出豆→出料儲豆槽 FO1 判斷 --
  368. # 出料儲豆槽入料 且滿時, 出料儲豆槽可出豆
  369. if DO1 == ('DO_InputtingBean' and DO_UP_UltraSoniclist[0] > 35):
  370. DO1 = 'DO_OutputtingBean'
  371. # 出料儲豆槽入料 且未滿, 桶槽可出豆已空
  372. # -- 取得乾燥桶槽 D1~D6 桶內高度 UltraSonic -------------
  373. D_UP_UltraSoniclist = []
  374. for i in range(1, 7, 1):
  375. tank_UltraSonic = dry_tank_UltraSonic.query.filter_by(tank_num='D' + str(i)).order_by(text('datetime desc')).first()
  376. UltraSonic = int(tank_UltraSonic.UltraSonic)
  377. D_UP_UltraSoniclist.append(UltraSonic)
  378. print('D_UP_UltraSoniclist: ', D_UP_UltraSoniclist)
  379. # -- 取得乾燥桶槽 D1~D6 桶內高度 UltraSonic -------------
  380. if DO1 == 'DO_InputtingBean' and Dry_OutputtingBean_number >= 1:
  381. if D1 == 'D_OutputtingBean' and D_UP_UltraSoniclist[0] < 5:
  382. D1 = 'D_Waiting'
  383. DO1 = 'DO_OutputtingBean'
  384. print('------- D1 狀態更新:空桶等待 -------')
  385. print('------- DO1 狀態更新:可出豆 -------')
  386. elif D2 == 'D_OutputtingBean' and D_UP_UltraSoniclist[1] < 5:
  387. D2 = 'D_Waiting'
  388. DO1 = 'DO_OutputtingBean'
  389. print('------- D2 狀態更新:空桶等待 -------')
  390. print('------- DO1 狀態更新:可出豆 -------')
  391. elif D3 == 'D_OutputtingBean' and D_UP_UltraSoniclist[2] < 5:
  392. D3 = 'D_Waiting'
  393. DO1 = 'DO_OutputtingBean'
  394. print('------- D3 狀態更新:空桶等待 -------')
  395. print('------- DO1 狀態更新:可出豆 -------')
  396. elif D4 == 'D_OutputtingBean' and D_UP_UltraSoniclist[3] < 5:
  397. D4 = 'D_Waiting'
  398. DO1 = 'DO_OutputtingBean'
  399. print('------- D4 狀態更新:空桶等待 -------')
  400. print('------- DO1 狀態更新:可出豆 -------')
  401. elif D5 == 'D_OutputtingBean' and D_UP_UltraSoniclist[4] < 5:
  402. D5 = 'D_Waiting'
  403. DO1 = 'DO_OutputtingBean'
  404. print('------- D5 狀態更新:空桶等待 -------')
  405. print('------- DO1 狀態更新:可出豆 -------')
  406. elif D6 == 'D_OutputtingBean' and D_UP_UltraSoniclist[5] < 5:
  407. D6 = 'D_Waiting'
  408. DO1 = 'DO_OutputtingBean'
  409. print('------- D6 狀態更新:空桶等待 -------')
  410. print('------- DO1 狀態更新:可出豆 -------')
  411. elif DO1 == 'DO_OutputtingBean' and DO_UP_UltraSoniclist[0] < 5:
  412. DO1 = 'DO_Waiting'
  413. # # ----- 將狀態寫入資料庫 ------------------------------
  414. # # 獲取文本框的值並賦值給user實體對象
  415. # D_status = dry_container_status()
  416. # D_status.Dry_Input_1 = DI1
  417. # D_status.Dry_Input_2 = DI2
  418. # D_status.Dry_Tank_1 = D1
  419. # D_status.Dry_Tank_2 = D2
  420. # D_status.Dry_Tank_3 = D3
  421. # D_status.Dry_Tank_4 = D4
  422. # D_status.Dry_Tank_5 = D5
  423. # D_status.Dry_Tank_6 = D6
  424. # D_status.Dry_Tank_7 = D7
  425. # D_status.Dry_Tank_8 = D8
  426. # D_status.Dry_Tank_9 = D9
  427. # D_status.Dry_Tank_10 = D10
  428. # D_status.Dry_Tank_11 = D11
  429. # D_status.Dry_Tank_12 = D12
  430. # D_status.Dry_Output_1 = DO1
  431. # D_status.Dry_Output_2 = DO2
  432. # #將數據保存進資料庫
  433. # db.session.add(D_status)
  434. # # 手動提交
  435. # db.session.commit()
  436. # # ----- 將狀態寫入資料庫 ------------------------------
  437. return jsonify({"Dry_Input_1":DI1,
  438. "Dry_Tank_1":D1,
  439. "Dry_Tank_2":D2,
  440. "Dry_Tank_3":D3,
  441. "Dry_Tank_4":D4,
  442. "Dry_Tank_5":D5,
  443. "Dry_Tank_6":D6,
  444. "Dry_Output_1":DO1
  445. })
  446. # 發酵槽自動化測試頁
  447. @main.route('/ferment_auto', methods=['GET', 'POST'])
  448. def ferment_auto():
  449. # 獲取登入信息
  450. if 'id' in session and 'uname' in session and 'status' in session:
  451. username = session['uname']
  452. status = session['status']
  453. ferment_status = ferment_container_status.query.order_by(text('datetime desc')).first()
  454. FI1 = ferment_status.Ferment_Input_1
  455. FI2 = ferment_status.Ferment_Input_2
  456. F1 = ferment_status.Ferment_Tank_1
  457. F2 = ferment_status.Ferment_Tank_2
  458. F3 = ferment_status.Ferment_Tank_3
  459. F4 = ferment_status.Ferment_Tank_4
  460. F5 = ferment_status.Ferment_Tank_5
  461. F6 = ferment_status.Ferment_Tank_6
  462. F7 = ferment_status.Ferment_Tank_7
  463. F8 = ferment_status.Ferment_Tank_8
  464. F9 = ferment_status.Ferment_Tank_9
  465. F10 = ferment_status.Ferment_Tank_10
  466. F11 = ferment_status.Ferment_Tank_11
  467. F12 = ferment_status.Ferment_Tank_12
  468. FO1 = ferment_status.Ferment_Output_1
  469. FO2 = ferment_status.Ferment_Output_2
  470. clean_status = clean_container_status.query.order_by(text('datetime desc')).first()
  471. PO1 = clean_status.Peel_Output_1 # PO_Waiting # PO_OutputtingBean
  472. return render_template('ferment_auto.html', title="[測試] 發酵自動化", **locals())
  473. else:
  474. return render_template('sign_in.html')
  475. @main.route('/ferment_auto_status', methods=['GET', 'POST'])
  476. def ferment_auto_test():
  477. info = request.args.to_dict()
  478. # 寫定入料、桶槽、出料的空桶標準, 若 <= 2 則為空桶
  479. Ferment_Input_bean_empty = 2
  480. Ferment_Tank_bean_empty = 2
  481. Ferment_Output_bean_empty = 2
  482. # 從介面取得 指定生豆高度/發酵桶槽排程:發酵、清洗、校正 EC、校正攪拌棒
  483. FermentAuto_fermenting = int(info['FermentAuto_fermenting'])
  484. FermentAuto_cleaning = int(info['FermentAuto_cleaning'])
  485. FermentAuto_calibratingEC = int(info['FermentAuto_calibratingEC'])
  486. FermentAuto_calibratingSTIR = int(info['FermentAuto_calibratingSTIR'])
  487. Ferment_Input_bean_height = float(info['Ferment_Input_bean_height'])
  488. Ferment_Input_vacuumON_time = float(info['Ferment_Input_vacuumON_time'])
  489. Ferment_Input_vacuumOFF_time = float(info['Ferment_Input_vacuumOFF_time'])
  490. Ferment_Tank_bean_height = float(info['Ferment_Tank_bean_height'])
  491. Ferment_Tank_vacuumON_time = float(info['Ferment_Tank_vacuumON_time'])
  492. Ferment_Tank_vacuumOFF_time = float(info['Ferment_Tank_vacuumOFF_time'])
  493. Ferment_Tank_water_height = float(info['Ferment_Tank_water_height'])
  494. Ferment_Tank_fermenting_temp = float(info['Ferment_Tank_fermenting_temp'])
  495. Ferment_Tank_fermenting_time = float(info['Ferment_Tank_fermenting_time'])
  496. Ferment_Tank_motor_rpm = float(info['Ferment_Tank_motor_rpm'])
  497. Ferment_Tank_motor_time = float(info['Ferment_Tank_motor_time'])
  498. Ferment_Tank_fermenting_pH = float(info['Ferment_Tank_fermenting_pH'])
  499. Ferment_Output_bean_height = float(info['Ferment_Output_bean_height'])
  500. Ferment_Output_vacuumON_time = float(info['Ferment_Output_vacuumON_time'])
  501. Ferment_Output_vacuumOFF_time = float(info['Ferment_Output_vacuumOFF_time'])
  502. Ferment_Tank_WaterOut_time = float(info['Ferment_Tank_WaterOut_time'])
  503. Ferment_Tank_Disinfect_time = float(info['Ferment_Tank_Disinfect_time'])
  504. Ferment_cb_vacuum = info['Ferment_cb_vacuum'] # 'true' 'false'
  505. Ferment_cb_vacuum_time = float(info['Ferment_cb_vacuum_time'])
  506. # [介面] 讓使用者可以選擇排程內有發酵/清洗/校正, 此處預設為 True
  507. Ferment_btn = FermentAuto_fermenting # Ferment_btn = True
  508. # 從資料庫資料表中取得最新狀態
  509. ferment_status = ferment_container_status.query.order_by(text('datetime desc')).first()
  510. FI1 = ferment_status.Ferment_Input_1
  511. FI2 = ferment_status.Ferment_Input_2
  512. F1 = ferment_status.Ferment_Tank_1
  513. F2 = ferment_status.Ferment_Tank_2
  514. F3 = ferment_status.Ferment_Tank_3
  515. F4 = ferment_status.Ferment_Tank_4
  516. F5 = ferment_status.Ferment_Tank_5
  517. F6 = ferment_status.Ferment_Tank_6
  518. F7 = ferment_status.Ferment_Tank_7
  519. F8 = ferment_status.Ferment_Tank_8
  520. F9 = ferment_status.Ferment_Tank_9
  521. F10 = ferment_status.Ferment_Tank_10
  522. F11 = ferment_status.Ferment_Tank_11
  523. F12 = ferment_status.Ferment_Tank_12
  524. FO1 = ferment_status.Ferment_Output_1
  525. FO2 = ferment_status.Ferment_Output_2
  526. clean_status = clean_container_status.query.order_by(text('datetime desc')).first()
  527. PO1 = clean_status.Peel_Output_1 # PO_Waiting # PO_OutputtingBean
  528. PO2 = clean_status.Peel_Output_2
  529. F_UP_tanklist = [F1, F2, F3, F4, F5, F6]
  530. F_DOWN_tanklist = [F7, F8, F9, F10, F11, F12]
  531. FOutputtingBean_Tank = '' # 這是給出料儲豆槽判斷目前出豆的桶槽
  532. # ----- 發酵入料儲豆槽 FI1~FI2 入豆→可出豆判斷 ------------------------------
  533. # 目前發酵槽 F1~F6 正在入豆、入水的桶槽數量 (應小於一桶)
  534. FermentUp_Waiting_number = int(F_UP_tanklist.count('F_Waiting'))
  535. FermentUp_InputtingBean_number = int(F_UP_tanklist.count('F_InputtingBean'))
  536. FermentUp_InputtingBeanPause_number = int(F_UP_tanklist.count('F_InputtingBean_Pause'))
  537. FermentUp_InputtingBeanFinish_number = int(F_UP_tanklist.count('F_InputtingBean_Finish'))
  538. FermentUp_InputtingWater_number = int(F_UP_tanklist.count('F_InputtingWater'))
  539. # -- 取得發酵 入料儲豆槽 FI1~FI2 / 桶槽 F1~F6 / 出料 FO1~FO2 桶內高度 UltraSonic start -------------
  540. FI_UP_UltraSoniclist = []
  541. for i in range(1, 3, 1):
  542. input_UltraSonic = ferment_input_UltraSonic.query.filter_by(tank_num='F' + str(i)).order_by(text('datetime desc')).first()
  543. # UltraSonic = float("{:.2f}".format(34.6 - float(input_UltraSonic.UltraSonic)))
  544. UltraSonic = float(input_UltraSonic.UltraSonic)
  545. FI_UP_UltraSoniclist.append(UltraSonic)
  546. print('FI_UP_UltraSoniclist: ', FI_UP_UltraSoniclist)
  547. F_UP_UltraSoniclist = []
  548. for i in range(1, 7, 1):
  549. tank_UltraSonic = ferment_tank_UltraSonic.query.filter_by(tank_num='F' + str(i)).order_by(text('datetime desc')).first()
  550. UltraSonic = (float(tank_UltraSonic.UltraSonic))
  551. F_UP_UltraSoniclist.append(UltraSonic)
  552. print('F_UP_UltraSoniclist: ', F_UP_UltraSoniclist)
  553. FO_UP_UltraSoniclist = []
  554. for i in range(1, 3, 1):
  555. output_UltraSonic = ferment_output_UltraSonic.query.filter_by(tank_num='FO' + str(i)).order_by(text('datetime desc')).first()
  556. UltraSonic = float(output_UltraSonic.UltraSonic)
  557. FO_UP_UltraSoniclist.append(UltraSonic)
  558. print('FO_UP_UltraSoniclist: ', FO_UP_UltraSoniclist)
  559. # -- 取得發酵 入料儲豆槽 FI1~FI2 / 桶槽 F1~F6 / 出料 FO1~FO2 桶內高度 UltraSonic end -------------
  560. # -- 取得發酵桶槽 F1~F6 夾層水位高度 WaterLevel start -------------
  561. F_UP_tank_WaterLevel = []
  562. for i in range(1, 7, 1):
  563. tank_WaterLevel = ferment_tank_WaterLevel.query.filter_by(tank_num='F' + str(i)).order_by(text('datetime desc')).first()
  564. WaterLevel = float(tank_WaterLevel.WaterLevel)
  565. F_UP_tank_WaterLevel.append(WaterLevel)
  566. print('F_UP_tank_WaterLevel: ', F_UP_tank_WaterLevel)
  567. # -- 取得發酵桶槽 F1~F6 夾層水位高度 WaterLevel end -------------
  568. # -- 取得發酵桶槽 F1~F6 pH -------------
  569. F_UP_tank_PH = []
  570. for i in range(1, 7, 1):
  571. tank_PH = ferment_tank_PH.query.filter_by(tank_num='F' + str(i)).order_by(text('datetime desc')).first()
  572. PH = float(tank_PH.PH)
  573. F_UP_tank_PH.append(PH)
  574. print('F_UP_tank_PH: ', F_UP_tank_PH)
  575. # -- 取得發酵桶槽 F1~F6 pH -------------
  576. # -- 取得發酵桶槽 F1~F6 SHT11_TEMP -------------
  577. F_UP_tank_SHT11_Temp = []
  578. for i in range(1, 7, 1):
  579. tank_SHT11 = ferment_tank_SHT11.query.filter_by(tank_num='F' + str(i)).order_by(text('datetime desc')).first()
  580. SHT11_Temp = float(tank_SHT11.SHT11_Temp)
  581. F_UP_tank_SHT11_Temp.append(SHT11_Temp)
  582. print('F_UP_tank_SHT11_Temp: ', F_UP_tank_SHT11_Temp)
  583. # -- 取得發酵桶槽 F1~F6 SHT11_TEMP -------------
  584. # 入料儲豆槽等待中、且脫皮機儲豆槽可出豆時, 入料儲豆槽入豆
  585. if FI1 == 'FI_Waiting' and PO1 == 'PO_OutputtingBean':
  586. FI1 = 'FI_InputtingBean'
  587. print('------- FI1 狀態更新:入豆中 -------')
  588. # 入料儲豆槽入豆時
  589. elif FI1 == 'FI_InputtingBean':
  590. # 桶槽內達指定生豆高度時, 入料儲豆槽狀態轉為 可出豆
  591. if (FI_UP_UltraSoniclist[0] >= Ferment_Input_bean_height):
  592. # [致動器] 入料真空吸料機 OFF
  593. data = { "tank_num": "F1", "command": "input_vacuum_status", "value": "off" }
  594. print('data: ', data)
  595. # mqtt_f(data)
  596. FI1 = 'FI_OutputtingBean'
  597. print('------- FI1 狀態更新:可出豆 -------')
  598. # 桶槽內未達指定生豆高度, 且 前方 PO1 可出豆, 就執行入料動作
  599. elif (FI_UP_UltraSoniclist[0] < Ferment_Input_bean_height) and PO1 == 'PO_OutputtingBean':
  600. # [致動器] 入料真空吸料機 ON
  601. data = { "tank_num": "F1", "command": "input_vacuum_status", "value": "on" }
  602. print('data: ', data)
  603. # mqtt_f(data)
  604. timer = time.time()
  605. while True:
  606. if (time.time() - timer) > Ferment_Input_vacuumON_time:
  607. # [致動器] 入料真空吸料機 OFF
  608. data = { "tank_num": "F1", "command": "input_vacuum_status", "value": "off" }
  609. print('data: ', data)
  610. # mqtt_f(data)
  611. timer = time.time()
  612. break
  613. while True:
  614. if (time.time() - timer) > Ferment_Input_vacuumOFF_time:
  615. break
  616. # 桶槽內大於空桶高度, 且 前方 PO1 非可出豆, 將狀態轉為可出豆
  617. elif (FI_UP_UltraSoniclist[0] > Ferment_Input_bean_empty) and PO1 != 'PO_OutputtingBean':
  618. FI1 = 'FI_OutputtingBean'
  619. print('------- FI1 狀態更新:可出豆 -------')
  620. # 桶槽內小於空桶高度, 且 前方 PO1 非可出豆, 將狀態轉為空桶等待
  621. elif (FI_UP_UltraSoniclist[0] <= Ferment_Input_bean_empty) and PO1 != 'PO_OutputtingBean':
  622. FI1 = 'FI_Waiting'
  623. print('------- FI1 狀態更新:空桶等待 -------')
  624. # 入料儲豆槽出豆時, 桶槽高度為 空桶高度時, 狀態轉為 空桶等待
  625. elif FI1 == 'FI_OutputtingBean' and (FI_UP_UltraSoniclist[0] <= Ferment_Input_bean_empty):
  626. FI1 = 'FI_Waiting'
  627. print('------- FI1 狀態更新:空桶等待 -------')
  628. # ----- 發酵排程動作 start ------------------------------------------------------------------
  629. def FInputtingBean_TO_FInputtingBeanFinish(tid):
  630. # [致動器] 真空吸料機 OFF
  631. data = { "tank_num": tid, "command": "tank_vacuum_status", "value": "off" }
  632. print('data: ', data)
  633. # mqtt_f(data)
  634. # [致動器] 馬達 0
  635. data = { "tank_num": tid, "command": "tank_motor_status", "value": "0" }
  636. print('data: ', data)
  637. # mqtt_f(data)
  638. # [致動器] 入料三通閥 ON排氣
  639. data = { "tank_num": tid, "command": "tank_threewayvalve_input_status", "value": "on" }
  640. print('data: ', data)
  641. # mqtt_f(data)
  642. print('------- ', tid,' 狀態更新:入料完成 -------')
  643. def FInputtingBean_butnot_filled(tid):
  644. # [致動器] 入料三通閥 OFF入豆
  645. data = { "tank_num": tid, "command": "tank_threewayvalve_input_status", "value": "off" }
  646. print('data: ', data)
  647. # mqtt_f(data)
  648. # [致動器] 真空吸料機 ON
  649. data = { "tank_num": tid, "command": "tank_vacuum_status", "value": "on" }
  650. print('data: ', data)
  651. # mqtt_f(data)
  652. timer = time.time()
  653. while True:
  654. if (time.time() - timer) > Ferment_Tank_vacuumON_time:
  655. # [致動器] 入料真空吸料機 OFF
  656. data = { "tank_num": tid, "command": "tank_vacuum_status", "value": "off" }
  657. print('data: ', data)
  658. # mqtt_f(data)
  659. timer = time.time()
  660. break
  661. while True:
  662. if (time.time() - timer) > Ferment_Tank_vacuumOFF_time:
  663. break
  664. def FInputtingBean_butnot_FIOutputtingBean(tid):
  665. # [致動器] 真空吸料機 OFF
  666. data = { "tank_num": tid, "command": "tank_vacuum_status", "value": "off" }
  667. print('data: ', data)
  668. # mqtt_f(data)
  669. # [致動器] 入料三通閥 OFF入豆
  670. data = { "tank_num": tid, "command": "tank_threewayvalve_input_status", "value": "off" }
  671. print('data: ', data)
  672. # mqtt_f(data)
  673. # [致動器] 馬達 0
  674. data = { "tank_num": tid, "command": "tank_motor_status", "value": "0" }
  675. print('data: ', data)
  676. # mqtt_f(data)
  677. print('------- ', tid,' 狀態更新:入豆暫停 -------')
  678. # ----- 發酵排程動作 end ------------------------------------------------------------------
  679. # [介面] 若啟用 發酵 狀態
  680. if Ferment_btn:
  681. # ----- 發酵桶槽 F1~F6 入料桶槽優先入料判斷 ------------------------------
  682. # 若 F1~F6 桶槽中有一個桶槽入豆中
  683. if FermentUp_InputtingBean_number == 1:
  684. # 若 F1 桶槽為入豆中
  685. if F1 == 'F_InputtingBean':
  686. if (F_UP_UltraSoniclist[0] >= Ferment_Tank_bean_height):
  687. FInputtingBean_TO_FInputtingBeanFinish("F1")
  688. F1 = 'F_InputtingBean_Finish'
  689. elif FI1 == 'FI_OutputtingBean' and (F_UP_UltraSoniclist[0] < Ferment_Tank_bean_height):
  690. FInputtingBean_butnot_filled("F1")
  691. elif FI1 != 'FI_OutputtingBean':
  692. FInputtingBean_butnot_FIOutputtingBean("F1")
  693. F1 = 'F_InputtingBean_Pause'
  694. elif F2 == 'F_InputtingBean':
  695. pass
  696. elif F3 == 'F_InputtingBean':
  697. pass
  698. elif F4 == 'F_InputtingBean':
  699. pass
  700. elif F5 == 'F_InputtingBean':
  701. pass
  702. elif F6 == 'F_InputtingBean':
  703. pass
  704. # 若 F1~F6 桶槽中有一個桶槽入豆暫停中, 且 FI1 可出豆
  705. elif FermentUp_InputtingBeanPause_number == 1 and FI1 == 'FI_OutputtingBean':
  706. # 若 F1 桶槽為入豆暫停 且 FI1 可出料
  707. if F1 == 'F_InputtingBean_Pause':
  708. # [致動器] 入料三通閥 OFF入豆
  709. data = { "tank_num": "F1", "command": "tank_threewayvalve_input_status", "value": "off" }
  710. print('data: ', data)
  711. # mqtt_f(data)
  712. # [致動器] 馬達 5
  713. data = { "tank_num": "F1", "command": "tank_motor_status", "value": "5" }
  714. print('data: ', data)
  715. # mqtt_f(data)
  716. F1 = 'F_InputtingBean'
  717. print('------- F1 狀態更新:入料中 -------')
  718. elif F2 == 'F_InputtingBean_Pause':
  719. pass
  720. elif F3 == 'F_InputtingBean_Pause':
  721. pass
  722. elif F4 == 'F_InputtingBean_Pause':
  723. pass
  724. elif F5 == 'F_InputtingBean_Pause':
  725. pass
  726. elif F6 == 'F_InputtingBean_Pause':
  727. pass
  728. # 若 F1~F6 桶槽中 無桶槽正在入料或入料等待, 且 FI1 可出豆時
  729. elif FermentUp_InputtingBean_number == 0 and FermentUp_InputtingBeanPause_number == 0 and FI1 == 'FI_OutputtingBean' and FermentUp_Waiting_number >= 1:
  730. if F1 == 'F_Waiting':
  731. # [致動器] 馬達 5
  732. data = { "tank_num": "F1", "command": "tank_motor_status", "value": "5" }
  733. print('data: ', data)
  734. # mqtt_f(data)
  735. F1 = 'F_InputtingBean'
  736. print('------- F1 狀態更新:入料中 -------')
  737. elif F2 == 'F_Waiting':
  738. pass
  739. elif F3 == 'F_Waiting':
  740. pass
  741. elif F4 == 'F_Waiting':
  742. pass
  743. elif F5 == 'F_Waiting':
  744. pass
  745. elif F6 == 'F_Waiting':
  746. pass
  747. # 若 F1~F6 桶槽中 無桶槽正在入水, 則入豆完成者轉換狀態 開始入水
  748. elif FermentUp_InputtingWater_number == 0 and FermentUp_InputtingBeanFinish_number >= 1:
  749. if F1 == 'F_InputtingBean_Finish':
  750. F1 = 'F_InputtingWater'
  751. print('------- F1 狀態更新:入水中 -------')
  752. # [致動器] 浮選三通閥 ON
  753. data = { "tank_num": "F1", "command": "outer_threewayvalve_float_status", "value": "on" }
  754. print('data: ', data)
  755. # mqtt_f(data)
  756. if (F_UP_tank_WaterLevel[0] == 0):
  757. # [致動器] 桶外進水電磁閥 ON
  758. data = { "tank_num": "F1", "command": "outer_solenoid_water_status", "value": "on" }
  759. print('data: ', data)
  760. # mqtt_f(data)
  761. if (F_UP_UltraSoniclist[0] < Ferment_Tank_water_height):
  762. # 桶內高度若低於水位指定高度, [致動器] 桶內進水電磁閥 ON
  763. data = { "tank_num": "F1", "command": "tank_solenoid_water_in_status", "value": "on" }
  764. print('data: ', data)
  765. # mqtt_f(data)
  766. elif F2 == 'F_InputtingBean_Finish':
  767. pass
  768. elif F3 == 'F_InputtingBean_Finish':
  769. pass
  770. elif F4 == 'F_InputtingBean_Finish':
  771. pass
  772. elif F5 == 'F_InputtingBean_Finish':
  773. pass
  774. elif F6 == 'F_InputtingBean_Finish':
  775. pass
  776. elif FermentUp_InputtingWater_number == 1:
  777. if F1 == 'F_InputtingWater':
  778. # 若夾層入水達水位計高度 且 桶內入水達指定水位高度, 狀態轉換為發酵中
  779. if (F_UP_tank_WaterLevel[0] == 1) and (F_UP_UltraSoniclist[0] >= Ferment_Tank_water_height):
  780. # 桶外水位高度高於水位計高度, [致動器] 桶外進水電磁閥 OFF
  781. data = { "tank_num": "F1", "command": "outer_solenoid_water_status", "value": "off" }
  782. print('data: ', data)
  783. # mqtt_f(data)
  784. # 桶內高度若高於水位指定高度, [致動器] 桶內進水電磁閥 OFF
  785. data = { "tank_num": "F1", "command": "tank_solenoid_water_in_status", "value": "off" }
  786. print('data: ', data)
  787. # mqtt_f(data)
  788. F1 = 'F_Fermenting'
  789. print('------- F1 狀態更新:發酵中 -------')
  790. # 若勾選 發酵時桶內抽真空?
  791. if Ferment_cb_vacuum == 'true':
  792. # [致動器] 入料三通閥 ON排氣
  793. data = { "tank_num": "F1", "command": "tank_threewayvalve_input_status", "value": "on" }
  794. print('data: ', data)
  795. # mqtt_f(data)
  796. # [致動器] 外桶浮選三通閥 OFF
  797. data = { "tank_num": "F1", "command": "outer_threewayvalve_float_status", "value": "off" }
  798. print('data: ', data)
  799. # mqtt_f(data)
  800. # [致動器] 真空吸料機 ON
  801. data = { "tank_num": "F1", "command": "tank_vacuum_status", "value": "on" }
  802. print('data: ', data)
  803. # mqtt_f(data)
  804. print('真空發酵, 桶槽抽氣 ' , Ferment_cb_vacuum_time, ' 秒')
  805. timer = time.time()
  806. while True:
  807. if (time.time() - timer) > Ferment_cb_vacuum_time:
  808. # [致動器] 入料真空吸料機 OFF
  809. data = { "tank_num": "F1", "command": "tank_vacuum_status", "value": "off" }
  810. print('data: ', data)
  811. # mqtt_f(data)
  812. break
  813. # [致動器] 馬達 (指定轉速)
  814. data = { "tank_num": "F1", "command": "tank_motor_status", "value": Ferment_Tank_motor_rpm }
  815. print('data: ', data)
  816. # mqtt_f(data)
  817. # [致動器] 溫控開關 ON
  818. # data = { "tank_num": "F1", "command": "tank_temp_enable_status", "value": "on", "duration": Ferment_Tank_fermenting_time}
  819. # print('data: ', data)
  820. # # mqtt_f(data)
  821. # [致動器] 設定溫度、持溫時間
  822. # [致動器] 加熱器 1 ON
  823. data = { "tank_num": "F1", "command": "tank_heater1_status", "value": "on" }
  824. print('data: ', data)
  825. # mqtt_f(data)
  826. # [致動器] 雙核薄膜泵 水質檢測 ON
  827. data = { "tank_num": "F1", "command": "tank_pump_sensor_status", "value": "on"}
  828. print('data: ', data)
  829. # mqtt_f(data)
  830. # TODO 增加發酵條件等
  831. elif (F_UP_tank_WaterLevel[0] == 1):
  832. # [致動器] 桶外進水電磁閥 OFF
  833. data = { "tank_num": "F1", "command": "outer_solenoid_water_status", "value": "off" }
  834. print('data: ', data)
  835. # mqtt_f(data)
  836. elif (F_UP_UltraSoniclist[0] >= Ferment_Tank_water_height):
  837. # 桶內高度若高於水位指定高度, [致動器] 桶內進水電磁閥 OFF
  838. data = { "tank_num": "F1", "command": "tank_solenoid_water_in_status", "value": "off" }
  839. print('data: ', data)
  840. # mqtt_f(data)
  841. else:
  842. if F1 == 'F_Fermenting' and F_UP_tank_SHT11_Temp[0] >= Ferment_Tank_fermenting_temp and F_UP_tank_PH[0] <= Ferment_Tank_fermenting_pH:
  843. print('發酵等待 ', Ferment_Tank_fermenting_time, ' 秒')
  844. timer = time.time()
  845. while True:
  846. if (time.time() - timer) > Ferment_Tank_fermenting_time :
  847. # [致動器] 外桶浮選三通閥 OFF
  848. data = { "tank_num": "F1", "command": "outer_threewayvalve_float_status", "value": "off" }
  849. print('data: ', data)
  850. # mqtt_f(data)
  851. # [致動器] 馬達 0
  852. data = { "tank_num": "F1", "command": "tank_motor_status", "value": "0" }
  853. print('data: ', data)
  854. # mqtt_f(data)
  855. # [致動器] 溫控開關 OFF
  856. # data = { "tank_num": "F1", "command": "tank_temp_enable_status", "value": "off" }
  857. # print('data: ', data)
  858. # # mqtt_f(data)
  859. # [致動器] 加熱器 1 OFF
  860. data = { "tank_num": "F1", "command": "tank_heater1_status", "value": "off" }
  861. print('data: ', data)
  862. # mqtt_f(data)
  863. # [致動器] 雙核薄膜泵 水質檢測 OFF
  864. data = { "tank_num": "F1", "command": "tank_pump_sensor_status", "value": "off"}
  865. print('data: ', data)
  866. # mqtt_f(data)
  867. # TODO 增加發酵條件等
  868. break
  869. # [致動器] 廢水排水閥 (桶內) ON
  870. data = { "tank_num": "F1", "command": "tank_solenoid_water_out_status", "value": "on" }
  871. print('data: ', data)
  872. # mqtt_f(data)
  873. print('桶內廢水排水 ', Ferment_Tank_WaterOut_time, ' 秒')
  874. timer = time.time()
  875. while True:
  876. if (time.time() - timer) > Ferment_Tank_WaterOut_time:
  877. # [致動器] 廢水排水閥 (桶內) OFF
  878. data = { "tank_num": "F1", "command": "tank_solenoid_water_out_status", "value": "off" }
  879. print('data: ', data)
  880. # mqtt_f(data)
  881. break
  882. F1 = 'F_OutputtingBean'
  883. print('------- F1 狀態更新:可出豆 -------')
  884. # 將可出豆桶槽賦值
  885. if FOutputtingBean_Tank == '': FOutputtingBean_Tank = 'F1'
  886. if F2 == 'F_Fermenting':
  887. pass
  888. if F3 == 'F_Fermenting':
  889. pass
  890. if F4 == 'F_Fermenting':
  891. pass
  892. if F5 == 'F_Fermenting':
  893. pass
  894. if F6 == 'F_Fermenting':
  895. pass
  896. # 若桶槽 F1~F6 可出豆 且 桶槽內為空桶 且 有指定清洗排程, 進行清洗桶槽
  897. # TODO 發酵次數計算、消毒次數、校正
  898. if F1 == 'F_OutputtingBean' and (F_UP_UltraSoniclist[0] <= Ferment_Tank_bean_empty):
  899. FOutputtingBean_Tank = ''
  900. if (FermentAuto_cleaning == 0): F1 = 'F_Waiting'
  901. elif (FermentAuto_cleaning != 0): F1 = 'F_Cleaning'
  902. if F2 == 'F_OutputtingBean' and (F_UP_UltraSoniclist[1] <= Ferment_Tank_bean_empty):
  903. pass
  904. if F3 == 'F_OutputtingBean' and (F_UP_UltraSoniclist[2] <= Ferment_Tank_bean_empty):
  905. pass
  906. if F4 == 'F_OutputtingBean' and (F_UP_UltraSoniclist[3] <= Ferment_Tank_bean_empty):
  907. pass
  908. if F5 == 'F_OutputtingBean' and (F_UP_UltraSoniclist[4] <= Ferment_Tank_bean_empty):
  909. pass
  910. if F6 == 'F_OutputtingBean' and (F_UP_UltraSoniclist[5] <= Ferment_Tank_bean_empty):
  911. pass
  912. # 避免桶槽可出豆時未賦值
  913. if FOutputtingBean_Tank == '':
  914. if F1 == 'F_OutputtingBean': FOutputtingBean_Tank = 'F1'
  915. elif F2 == 'F_OutputtingBean': FOutputtingBean_Tank = 'F2'
  916. elif F3 == 'F_OutputtingBean': FOutputtingBean_Tank = 'F3'
  917. elif F4 == 'F_OutputtingBean': FOutputtingBean_Tank = 'F4'
  918. elif F5 == 'F_OutputtingBean': FOutputtingBean_Tank = 'F5'
  919. elif F6 == 'F_OutputtingBean': FOutputtingBean_Tank = 'F6'
  920. # 若桶槽 F1~F6 為清洗中
  921. if F1 == 'F_Cleaning':
  922. # [致動器] 浮選三通閥 OFF
  923. data = { "tank_num": "F1", "command": "outer_threewayvalve_float_status", "value": "off" }
  924. print('data: ', data)
  925. # mqtt_f(data)
  926. # [致動器] 逆洗雙核薄膜泵電磁閥 ON
  927. data = { "tank_num": "F1", "command": "solenoid_tank_pump_status", "value": "on" }
  928. print('data: ', data)
  929. # mqtt_f(data)
  930. # [致動器] 馬達 10
  931. data = { "tank_num": "F1", "command": "tank_motor_status", "value": "10" }
  932. print('data: ', data)
  933. # mqtt_f(data)
  934. # [致動器] 消毒電磁閥 ON
  935. data = { "tank_num": "F1", "command": "tank_solenoid_disinfect_status", "value": "on" }
  936. print('data: ', data)
  937. # mqtt_f(data)
  938. # [致動器] 消毒抽水 Pump ON TODO
  939. # 暫停 (消毒時間) 秒
  940. timer = time.time()
  941. while True:
  942. if (time.time() - timer) > Ferment_Tank_Disinfect_time :
  943. # [致動器] 消毒電磁閥 OFF
  944. data = { "tank_num": "F1", "command": "tank_solenoid_disinfect_status", "value": "off" }
  945. print('data: ', data)
  946. # mqtt_f(data)
  947. break
  948. # [致動器] 消毒抽水 Pump OFF TODO
  949. # [致動器] 馬達 0
  950. data = { "tank_num": "F1", "command": "tank_motor_status", "value": "0" }
  951. print('data: ', data)
  952. # mqtt_f(data)
  953. # [致動器] 浮選三通閥 ON
  954. data = { "tank_num": "F1", "command": "outer_threewayvalve_float_status", "value": "on" }
  955. print('data: ', data)
  956. # mqtt_f(data)
  957. # [致動器] 逆洗雙核薄膜泵電磁閥 OFF
  958. data = { "tank_num": "F1", "command": "solenoid_tank_pump_status", "value": "off" }
  959. print('data: ', data)
  960. # mqtt_f(data)
  961. # [致動器] 消毒電磁閥 OFF
  962. data = { "tank_num": "F1", "command": "tank_solenoid_disinfect_status", "value": "off" }
  963. print('data: ', data)
  964. # mqtt_f(data)
  965. # [致動器] 廢水排水閥 (桶內) ON
  966. data = { "tank_num": "F1", "command": "tank_solenoid_water_out_status", "value": "on" }
  967. print('data: ', data)
  968. # mqtt_f(data)
  969. print('桶內廢水排水 ', Ferment_Tank_WaterOut_time, ' 秒')
  970. timer = time.time()
  971. while True:
  972. if (time.time() - timer) > Ferment_Tank_WaterOut_time:
  973. # [致動器] 廢水排水閥 (桶內) OFF
  974. data = { "tank_num": "F1", "command": "tank_solenoid_water_out_status", "value": "off" }
  975. print('data: ', data)
  976. # mqtt_f(data)
  977. break
  978. F1 = 'F_Waiting'
  979. print('------- F1 狀態更新:空桶等待 -------')
  980. if F2 == 'F_Cleaning':
  981. pass
  982. if F3 == 'F_Cleaning':
  983. pass
  984. if F4 == 'F_Cleaning':
  985. pass
  986. if F5 == 'F_Cleaning':
  987. pass
  988. if F6 == 'F_Cleaning':
  989. pass
  990. # # ----- [測試用] !!!!! F1~F6 入料完成→可出豆 判斷 ------------------------------
  991. # if F1 == 'F_InputtingBean_Finish': F1 = 'F_OutputtingBean'
  992. # if F2 == 'F_InputtingBean_Finish': F2 = 'F_OutputtingBean'
  993. # if F3 == 'F_InputtingBean_Finish': F3 = 'F_OutputtingBean'
  994. # if F4 == 'F_InputtingBean_Finish': F4 = 'F_OutputtingBean'
  995. # if F5 == 'F_InputtingBean_Finish': F5 = 'F_OutputtingBean'
  996. # if F6 == 'F_InputtingBean_Finish': F6 = 'F_OutputtingBean'
  997. # # ----- [測試用] !!!!! F1~F6 入料完成→可出豆 判斷 ------------------------------
  998. else:
  999. # [介面] 若未啟用 發酵 狀態
  1000. print('未啟用發酵流程')
  1001. # ----- 發酵出料儲豆槽 FO1~FO2 入豆→可出豆判斷 ------------------------------
  1002. # 目前發酵槽 F1~F6 正在入豆、入水的桶槽數量 (應小於一桶)
  1003. # Ferment_OutputtingBean_number = int(F_UP_tanklist.count('F_OutputtingBean'))
  1004. # 若出料儲豆槽狀態為 空桶等待 且 桶槽狀態為 F_OutputtingBean 可出豆, 與 FO1 配合開始出豆
  1005. if FO1 == 'FO_Waiting' and FOutputtingBean_Tank != '':
  1006. # 判斷要對應出料的桶槽為何, 設定成 FOutputtingBean_Tank = 'F1', 待出完料再把 FOutputtingBean_Tank == ''
  1007. FO1 = 'FO_InputtingBean'
  1008. print('------- ' + FOutputtingBean_Tank + ' 狀態更新:出豆中 -------')
  1009. print('------- FO1 狀態更新:入豆中 -------')
  1010. # [致動器] F1 蝴蝶閥 ON
  1011. data = { "tank_num": FOutputtingBean_Tank, "command": "tank_diskvalve_status", "value": "on" }
  1012. print('data: ', data)
  1013. # mqtt_f(data)
  1014. elif FO1 == 'FO_InputtingBean' and FOutputtingBean_Tank != '':
  1015. if FO_UP_UltraSoniclist[0] >= Ferment_Output_bean_height:
  1016. # [致動器] 出料真空吸料機 OFF
  1017. data = { "tank_num": FOutputtingBean_Tank, "command": "input_vacuum_status", "value": "off" }
  1018. print('data: ', data)
  1019. # mqtt_f(data)
  1020. # [致動器] F1 蝴蝶閥 OFF
  1021. data = { "tank_num": FOutputtingBean_Tank, "command": "tank_diskvalve_status", "value": "off" }
  1022. print('data: ', data)
  1023. # mqtt_f(data)
  1024. FO1 = 'FO_OutputtingBean'
  1025. print('------- FO1 狀態更新:出豆中 -------')
  1026. elif FO_UP_UltraSoniclist[0] < Ferment_Output_bean_height:
  1027. # [致動器] 出料真空吸料機 ON
  1028. data = { "tank_num": FOutputtingBean_Tank, "command": "input_vacuum_status", "value": "on" }
  1029. print('data: ', data)
  1030. # mqtt_f(data)
  1031. timer = time.time()
  1032. while True:
  1033. if (time.time() - timer) > Ferment_Output_vacuumON_time:
  1034. # [致動器] 出料真空吸料機 OFF
  1035. data = { "tank_num": FOutputtingBean_Tank, "command": "input_vacuum_status", "value": "off" }
  1036. print('data: ', data)
  1037. # mqtt_f(data)
  1038. timer = time.time()
  1039. break
  1040. while True:
  1041. if (time.time() - timer) > Ferment_Output_vacuumOFF_time:
  1042. break
  1043. # 若是無桶槽可入豆, 出料儲豆槽判斷是否可出豆或空桶等待入豆
  1044. elif FOutputtingBean_Tank == '':
  1045. if FO_UP_UltraSoniclist[0] > Ferment_Output_bean_empty:
  1046. FO1 == 'FO_OutputtingBean'
  1047. print('------- FO1 狀態更新:出豆中 -------')
  1048. elif FO_UP_UltraSoniclist[0] <= Ferment_Output_bean_empty:
  1049. FO1 == 'FO_Waiting'
  1050. print('------- FO1 狀態更新:空桶等待 -------')
  1051. elif FO1 == 'FO_OutputtingBean' and FO_UP_UltraSoniclist[0] <= Ferment_Output_bean_empty:
  1052. FO1 == 'FO_Waiting'
  1053. print('------- FO1 狀態更新:空桶等待 -------')
  1054. # ----- 將狀態寫入資料庫 ------------------------------
  1055. # 獲取文本框的值並賦值給user實體對象
  1056. F_status = ferment_container_status()
  1057. F_status.Ferment_Input_1 = FI1
  1058. F_status.Ferment_Input_2 = FI2
  1059. F_status.Ferment_Tank_1 = F1
  1060. F_status.Ferment_Tank_2 = F2
  1061. F_status.Ferment_Tank_3 = F3
  1062. F_status.Ferment_Tank_4 = F4
  1063. F_status.Ferment_Tank_5 = F5
  1064. F_status.Ferment_Tank_6 = F6
  1065. F_status.Ferment_Tank_7 = F7
  1066. F_status.Ferment_Tank_8 = F8
  1067. F_status.Ferment_Tank_9 = F9
  1068. F_status.Ferment_Tank_10 = F10
  1069. F_status.Ferment_Tank_11 = F11
  1070. F_status.Ferment_Tank_12 = F12
  1071. F_status.Ferment_Output_1 = FO1
  1072. F_status.Ferment_Output_2 = FO2
  1073. #將數據保存進資料庫
  1074. db.session.add(F_status)
  1075. # 手動提交
  1076. db.session.commit()
  1077. # ----- 將狀態寫入資料庫 ------------------------------
  1078. return jsonify({"Peel_Output_1":PO1,
  1079. "Ferment_Input_1":FI1,
  1080. "Ferment_Tank_1":F1,
  1081. "Ferment_Tank_2":F2,
  1082. "Ferment_Tank_3":F3,
  1083. "Ferment_Tank_4":F4,
  1084. "Ferment_Tank_5":F5,
  1085. "Ferment_Tank_6":F6,
  1086. "Ferment_Output_1":FO1
  1087. })
  1088. # 1026 以流程圖判斷, 改寫 def ferment_auto_test, 以下為原備份
  1089. @main.route('/ferment_auto_test_1026backup')
  1090. def ferment_auto_test_1026backup():
  1091. info = request.args.to_dict()
  1092. # 從介面取得 指定生豆高度/發酵桶槽排程:發酵、清洗、校正 EC、校正攪拌棒
  1093. Ferment_Input_bean_empty = 2
  1094. Ferment_Tank_bean_empty = 2
  1095. Ferment_Output_bean_empty = 2
  1096. FermentAuto_fermenting = int(info['FermentAuto_fermenting'])
  1097. FermentAuto_cleaning = int(info['FermentAuto_cleaning'])
  1098. FermentAuto_calibratingEC = int(info['FermentAuto_calibratingEC'])
  1099. FermentAuto_calibratingSTIR = int(info['FermentAuto_calibratingSTIR'])
  1100. Ferment_Input_bean_height = float(info['Ferment_Input_bean_height'])
  1101. Ferment_Input_vacuumON_time = float(info['Ferment_Input_vacuumON_time'])
  1102. Ferment_Input_vacuumOFF_time = float(info['Ferment_Input_vacuumOFF_time'])
  1103. Ferment_Tank_bean_height = float(info['Ferment_Tank_bean_height'])
  1104. Ferment_Tank_vacuumON_time = float(info['Ferment_Tank_vacuumON_time'])
  1105. Ferment_Tank_vacuumOFF_time = float(info['Ferment_Tank_vacuumOFF_time'])
  1106. Ferment_Tank_water_height = float(info['Ferment_Tank_water_height'])
  1107. Ferment_Tank_fermenting_temp = float(info['Ferment_Tank_fermenting_temp'])
  1108. Ferment_Tank_fermenting_time = float(info['Ferment_Tank_fermenting_time'])
  1109. Ferment_Tank_motor_rpm = float(info['Ferment_Tank_motor_rpm'])
  1110. Ferment_Tank_motor_time = float(info['Ferment_Tank_motor_time'])
  1111. Ferment_Tank_fermenting_pH = float(info['Ferment_Tank_fermenting_pH'])
  1112. Ferment_Output_bean_height = float(info['Ferment_Output_bean_height'])
  1113. Ferment_Output_vacuumON_time = float(info['Ferment_Output_vacuumON_time'])
  1114. Ferment_Output_vacuumOFF_time = float(info['Ferment_Output_vacuumOFF_time'])
  1115. Ferment_Tank_WaterOut_time = float(info['Ferment_Tank_WaterOut_time'])
  1116. Ferment_Tank_Disinfect_time = float(info['Ferment_Tank_Disinfect_time'])
  1117. # print('發酵自動化_指定生豆高度: ', Ferment_Tank_bean_height)
  1118. # print('發酵自動化_發酵排程: ', FermentAuto_fermenting, type(FermentAuto_fermenting))
  1119. # print('發酵自動化_清洗排程: ', FermentAuto_cleaning, type(FermentAuto_cleaning))
  1120. # print('發酵自動化_校正 EC 排程: ', FermentAuto_calibratingEC, type(FermentAuto_calibratingEC))
  1121. # print('發酵自動化_校正攪拌棒排程: ', FermentAuto_calibratingSTIR, type(FermentAuto_calibratingSTIR))
  1122. # [介面] 讓使用者可以選擇排程內有發酵/清洗/校正, 此處預設為 True
  1123. Ferment_btn = FermentAuto_fermenting # Ferment_btn = True
  1124. # 從資料庫資料表中取得最新狀態
  1125. ferment_status = ferment_container_status.query.order_by(text('datetime desc')).first()
  1126. FI1 = ferment_status.Ferment_Input_1
  1127. FI2 = ferment_status.Ferment_Input_2
  1128. F1 = ferment_status.Ferment_Tank_1
  1129. F2 = ferment_status.Ferment_Tank_2
  1130. F3 = ferment_status.Ferment_Tank_3
  1131. F4 = ferment_status.Ferment_Tank_4
  1132. F5 = ferment_status.Ferment_Tank_5
  1133. F6 = ferment_status.Ferment_Tank_6
  1134. F7 = ferment_status.Ferment_Tank_7
  1135. F8 = ferment_status.Ferment_Tank_8
  1136. F9 = ferment_status.Ferment_Tank_9
  1137. F10 = ferment_status.Ferment_Tank_10
  1138. F11 = ferment_status.Ferment_Tank_11
  1139. F12 = ferment_status.Ferment_Tank_12
  1140. FO1 = ferment_status.Ferment_Output_1
  1141. FO2 = ferment_status.Ferment_Output_2
  1142. clean_status = clean_container_status.query.order_by(text('datetime desc')).first()
  1143. PO1 = clean_status.Peel_Output_1 # PO_Waiting # PO_OutputtingBean
  1144. PO2 = clean_status.Peel_Output_2
  1145. F_UP_tanklist = [F1, F2, F3, F4, F5, F6]
  1146. F_DOWN_tanklist = [F7, F8, F9, F10, F11, F12]
  1147. # ----- 發酵入料儲豆槽 FI1~FI2 入豆→可出豆判斷 ------------------------------
  1148. # 目前發酵槽 F1~F6 正在入豆的桶槽數量 (應小於一桶)
  1149. FermentUp_InputtingBean_number = int(F_UP_tanklist.count('F_InputtingBean'))
  1150. FermentUp_InputtingBeanPause_number = int(F_UP_tanklist.count('F_InputtingBean_Pause'))
  1151. # -- 取得發酵入料儲豆槽 FI1~FI2 桶內高度 UltraSonic -------------
  1152. FI_UP_UltraSoniclist = []
  1153. for i in range(1, 3, 1):
  1154. input_UltraSonic = ferment_input_UltraSonic.query.filter_by(tank_num='F' + str(i)).order_by(text('datetime desc')).first()
  1155. # UltraSonic = float("{:.2f}".format(34.6 - float(input_UltraSonic.UltraSonic)))
  1156. UltraSonic = float(input_UltraSonic.UltraSonic)
  1157. FI_UP_UltraSoniclist.append(UltraSonic)
  1158. print('FI_UP_UltraSoniclist: ', FI_UP_UltraSoniclist)
  1159. # -- 取得發酵入料儲豆槽 FI1~FI2 桶內高度 UltraSonic -------------
  1160. # 入料儲豆槽等待中、且脫皮機儲豆槽可出豆時, 入料儲豆槽入豆
  1161. if FI1 == 'FI_Waiting' and PO1 == 'PO_OutputtingBean':
  1162. FI1 = 'FI_InputtingBean'
  1163. print('------- FI1 狀態更新:入豆中 -------')
  1164. # [致動器] 入料真空吸料機 ON
  1165. data = { "tank_num": "F1", "command": "input_vacuum_status", "value": "on" }
  1166. print('data: ', data)
  1167. # mqtt_f(data)
  1168. timer = time.time()
  1169. while True:
  1170. if (time.time() - timer) > Ferment_Input_vacuumON_time:
  1171. # [致動器] 入料真空吸料機 OFF
  1172. data = { "tank_num": "F1", "command": "input_vacuum_status", "value": "off" }
  1173. print('data: ', data)
  1174. # mqtt_f(data)
  1175. timer = time.time()
  1176. break
  1177. while True:
  1178. if (time.time() - timer) > Ferment_Input_vacuumOFF_time:
  1179. break
  1180. # 入料儲豆槽入豆時
  1181. elif FI1 == 'FI_InputtingBean':
  1182. # 桶槽內未達指定生豆高度, 且 前方 PO1 可出豆, 就執行入料動作
  1183. if (FI_UP_UltraSoniclist[0] < Ferment_Input_bean_height) and PO1 == 'PO_OutputtingBean':
  1184. # [致動器] 入料真空吸料機 ON
  1185. data = { "tank_num": "F1", "command": "input_vacuum_status", "value": "on" }
  1186. print('data: ', data)
  1187. # mqtt_f(data)
  1188. timer = time.time()
  1189. while True:
  1190. if (time.time() - timer) > Ferment_Input_vacuumON_time:
  1191. # [致動器] 入料真空吸料機 OFF
  1192. data = { "tank_num": "F1", "command": "input_vacuum_status", "value": "off" }
  1193. print('data: ', data)
  1194. # mqtt_f(data)
  1195. timer = time.time()
  1196. break
  1197. while True:
  1198. if (time.time() - timer) > Ferment_Input_vacuumOFF_time:
  1199. break
  1200. # 桶槽內達指定生豆高度時, 入料儲豆槽狀態轉為 可出豆
  1201. if (FI_UP_UltraSoniclist[0] >= Ferment_Input_bean_height):
  1202. FI1 = 'FI_OutputtingBean'
  1203. print('------- FI1 狀態更新:可出豆 -------')
  1204. # 入料儲豆槽出豆時, 桶槽高度為 空桶高度時, 狀態轉為 空桶等待
  1205. elif FI1 == 'FI_OutputtingBean' and (FI_UP_UltraSoniclist[0] <= Ferment_Input_bean_empty):
  1206. FI1 = 'FI_Waiting'
  1207. print('------- FI1 狀態更新:空桶等待 -------')
  1208. # [1012 刪除, 避免混豆]若入料儲豆槽可出豆 且 儲豆槽內高度低於 40 且 前方脫皮機儲豆槽可出豆 且 目前無桶槽需入料時
  1209. # elif FI1 == 'FI_OutputtingBean' and (FI_UP_UltraSoniclist[0] < 40) and PO1 == 'PO_OutputtingBean' and FermentUp_InputtingBean_number == 0 and FermentUp_InputtingBeanPause_number == 0:
  1210. # FI1 = 'FI_InputtingBean'
  1211. # print('------- FI1 狀態更新:入豆中 -------')
  1212. # if FI2 == 'FI_Waiting' and PO2 == 'PO_OutputtingBean':
  1213. # FI2 = 'FI_InputtingBean'
  1214. # print('------- FI2 狀態更新:入豆中 -------')
  1215. # elif FI2 == 'FI_InputtingBean' and ((FI_UP_UltraSoniclist[1] >= 35) or (PO2 != 'PO_OutputtingBean')) :
  1216. # FI2 = 'FI_OutputtingBean'
  1217. # print('------- FI2 狀態更新:可出豆 -------')
  1218. # elif FI2 == 'FI_OutputtingBean' and (FI_UP_UltraSoniclist[1] < 40) and PO2 == 'PO_OutputtingBean' and FermentUp_InputtingBean_number == 0 and FermentUp_InputtingBeanPause_number == 0:
  1219. # FI2 = 'FI_InputtingBean'
  1220. # print('------- FI2 狀態更新:入豆中 -------')
  1221. # ----- 發酵桶槽 F1~F6 入料暫停→入料判斷 ------------------------------
  1222. ### 移到入料
  1223. # ----- 發酵桶槽 F1~F6 等待→入料判斷 ------------------------------
  1224. # -- 取得發酵桶槽 F1~F6 桶內高度 UltraSonic -------------
  1225. F_UP_UltraSoniclist = []
  1226. for i in range(1, 7, 1):
  1227. tank_UltraSonic = ferment_tank_UltraSonic.query.filter_by(tank_num='F' + str(i)).order_by(text('datetime desc')).first()
  1228. UltraSonic = (float(tank_UltraSonic.UltraSonic))
  1229. F_UP_UltraSoniclist.append(UltraSonic)
  1230. print('F_UP_UltraSoniclist: ', F_UP_UltraSoniclist)
  1231. # -- 取得發酵桶槽 F1~F6 桶內高度 UltraSonic -------------
  1232. # [介面] 若啟用 發酵 狀態
  1233. if Ferment_btn:
  1234. # 若入料儲豆槽可出料狀態
  1235. if FI1 == 'FI_OutputtingBean':
  1236. # 若 F1~F6 發酵桶槽為 入豆中, 繼續入豆, 當高度 > 指定生豆高度 時入豆完成
  1237. if F1 == 'F_InputtingBean':
  1238. if (F_UP_UltraSoniclist[0] >= Ferment_Tank_bean_height):
  1239. F1 = 'F_InputtingBean_Finish'
  1240. print('------- F1 狀態更新:入料完成 -------')
  1241. # [致動器] 入料三通閥 ON排氣
  1242. data = { "tank_num": "F1", "command": "tank_threewayvalve_input_status", "value": "on" }
  1243. print('data: ', data)
  1244. # mqtt_f(data)
  1245. # [致動器] 馬達 0
  1246. data = { "tank_num": "F1", "command": "tank_motor_status", "value": "0" }
  1247. print('data: ', data)
  1248. # mqtt_f(data)
  1249. # [致動器] 真空吸料機 OFF
  1250. data = { "tank_num": "F1", "command": "tank_vacuum_status", "value": "off" }
  1251. print('data: ', data)
  1252. # mqtt_f(data)
  1253. else:
  1254. # [致動器] 真空吸料機 ON
  1255. data = { "tank_num": "F1", "command": "tank_vacuum_status", "value": "on" }
  1256. print('data: ', data)
  1257. # mqtt_f(data)
  1258. timer = time.time()
  1259. while True:
  1260. if (time.time() - timer) > Ferment_Tank_vacuumON_time:
  1261. # [致動器] 入料真空吸料機 OFF
  1262. data = { "tank_num": "F1", "command": "tank_vacuum_status", "value": "off" }
  1263. print('data: ', data)
  1264. # mqtt_f(data)
  1265. timer = time.time()
  1266. break
  1267. while True:
  1268. if (time.time() - timer) > Ferment_Tank_vacuumOFF_time:
  1269. break
  1270. if F2 == 'F_InputtingBean' and (F_UP_UltraSoniclist[1] >= Ferment_Tank_bean_height):
  1271. F2 = 'F_InputtingBean_Finish'
  1272. print('------- F2 狀態更新:入料完成 -------')
  1273. if F3 == 'F_InputtingBean' and (F_UP_UltraSoniclist[2] >= Ferment_Tank_bean_height):
  1274. F3 = 'F_InputtingBean_Finish'
  1275. print('------- F3 狀態更新:入料完成 -------')
  1276. if F4 == 'F_InputtingBean' and (F_UP_UltraSoniclist[3] >= Ferment_Tank_bean_height):
  1277. F4 = 'F_InputtingBean_Finish'
  1278. print('------- F4 狀態更新:入料完成 -------')
  1279. if F5 == 'F_InputtingBean' and (F_UP_UltraSoniclist[4] >= Ferment_Tank_bean_height):
  1280. F5 = 'F_InputtingBean_Finish'
  1281. print('------- F5 狀態更新:入料完成 -------')
  1282. if F6 == 'F_InputtingBean' and (F_UP_UltraSoniclist[5] >= Ferment_Tank_bean_height):
  1283. F6 = 'F_InputtingBean_Finish'
  1284. print('------- F6 狀態更新:入料完成 -------')
  1285. # 若有兩個以上桶槽入料中, 則桶號最小者優先入料 ←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←← 1021 優先入豆判斷
  1286. if FermentUp_InputtingBean_number == 1:
  1287. if F1 == 'F_InputtingBean':
  1288. # [致動器] 桶槽真空吸料機 ON
  1289. data = { "tank_num": "F1", "command": "tank_vacuum_status", "value": "on" }
  1290. print('data: ', data)
  1291. # mqtt_f(data)
  1292. timer = time.time()
  1293. while True:
  1294. if (time.time() - timer) > Ferment_Tank_vacuumON_time:
  1295. # [致動器] 桶槽真空吸料機 OFF
  1296. data = { "tank_num": "F1", "command": "tank_vacuum_status", "value": "off" }
  1297. print('data: ', data)
  1298. # mqtt_f(data)
  1299. timer = time.time()
  1300. break
  1301. while True:
  1302. if (time.time() - timer) > Ferment_Tank_vacuumOFF_time:
  1303. break
  1304. # else if F2 == ''...
  1305. # 若 F1~F6 發酵桶槽為 入豆暫停, 則狀態改為優先入豆 ←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←← 1021 優先入豆判斷
  1306. elif FermentUp_InputtingBeanPause_number >= 1:
  1307. if F1 == 'F_InputtingBean_Pause':
  1308. F1 = 'F_InputtingBean'
  1309. print('------- F1 狀態更新:入料中 -------')
  1310. # [致動器] 桶槽真空吸料機 ON
  1311. data = { "tank_num": "F1", "command": "tank_vacuum_status", "value": "on" }
  1312. print('data: ', data)
  1313. # mqtt_f(data)
  1314. timer = time.time()
  1315. while True:
  1316. if (time.time() - timer) > Ferment_Tank_vacuumON_time:
  1317. # [致動器] 桶槽真空吸料機 OFF
  1318. data = { "tank_num": "F1", "command": "tank_vacuum_status", "value": "off" }
  1319. print('data: ', data)
  1320. # mqtt_f(data)
  1321. timer = time.time()
  1322. break
  1323. while True:
  1324. if (time.time() - timer) > Ferment_Tank_vacuumOFF_time:
  1325. break
  1326. elif F2 == 'F_InputtingBean_Pause':
  1327. F2 = 'F_InputtingBean'
  1328. print('------- F2 狀態更新:入料中 -------')
  1329. elif F3 == 'F_InputtingBean_Pause':
  1330. F3 = 'F_InputtingBean'
  1331. print('------- F3 狀態更新:入料中 -------')
  1332. elif F4 == 'F_InputtingBean_Pause':
  1333. F4 = 'F_InputtingBean'
  1334. print('------- F4 狀態更新:入料中 -------')
  1335. elif F5 == 'F_InputtingBean_Pause':
  1336. F5 = 'F_InputtingBean'
  1337. print('------- F5 狀態更新:入料中 -------')
  1338. elif F6 == 'F_InputtingBean_Pause':
  1339. F6 = 'F_InputtingBean'
  1340. print('------- F6 狀態更新:入料中 -------')
  1341. # 若 F1~F6 皆無入豆 無入豆暫停, 則桶號最小者開始入豆
  1342. elif FermentUp_InputtingBean_number == 0 and FermentUp_InputtingBeanPause_number == 0:
  1343. if F1 == 'F_Waiting':
  1344. F1 = 'F_InputtingBean'
  1345. print('------- F1 狀態更新:入料中 -------')
  1346. # [致動器] 入料入料三通閥 OFF入豆
  1347. data = { "tank_num": "F1", "command": "tank_threewayvalve_input_status", "value": "off" }
  1348. print('data: ', data)
  1349. # mqtt_f(data)
  1350. # [致動器] 馬達 5
  1351. data = { "tank_num": "F1", "command": "tank_motor_status", "value": "5" }
  1352. print('data: ', data)
  1353. # mqtt_f(data)
  1354. # [致動器] 桶槽真空吸料機 ON
  1355. data = { "tank_num": "F1", "command": "tank_vacuum_status", "value": "on" }
  1356. print('data: ', data)
  1357. # mqtt_f(data)
  1358. timer = time.time()
  1359. while True:
  1360. if (time.time() - timer) > Ferment_Tank_vacuumON_time:
  1361. # [致動器] 桶槽真空吸料機 OFF
  1362. data = { "tank_num": "F1", "command": "tank_vacuum_status", "value": "off" }
  1363. print('data: ', data)
  1364. # mqtt_f(data)
  1365. timer = time.time()
  1366. break
  1367. while True:
  1368. if (time.time() - timer) > Ferment_Tank_vacuumON_time:
  1369. break
  1370. elif F2 == 'F_Waiting':
  1371. F2 = 'F_InputtingBean'
  1372. print('------- F2 狀態更新:入料中 -------')
  1373. elif F3 == 'F_Waiting':
  1374. F3 = 'F_InputtingBean'
  1375. print('------- F3 狀態更新:入料中 -------')
  1376. elif F4 == 'F_Waiting':
  1377. F4 = 'F_InputtingBean'
  1378. print('------- F4 狀態更新:入料中 -------')
  1379. elif F5 == 'F_Waiting':
  1380. F5 = 'F_InputtingBean'
  1381. print('------- F5 狀態更新:入料中 -------')
  1382. elif F6 == 'F_Waiting':
  1383. F6 = 'F_InputtingBean'
  1384. print('------- F6 狀態更新:入料中 -------')
  1385. # 若入料儲豆槽非可出豆, 且 F1~F6 其中有桶槽入料中, 若桶槽未滿則入料暫停
  1386. elif FI1 != 'FI_InputtingBean' and FermentUp_InputtingBean_number >= 1:
  1387. if F1 == 'F_InputtingBean' and F_UP_UltraSoniclist[0] < Ferment_Tank_bean_height:
  1388. F1 = 'F_InputtingBean_Pause'
  1389. print('------- F1 狀態更新:入料暫停 -------')
  1390. # [致動器] 桶槽真空吸料機 OFF
  1391. data = { "tank_num": "F1", "command": "tank_vacuum_status", "value": "off" }
  1392. print('data: ', data)
  1393. # mqtt_f(data)
  1394. elif F1 == 'F_InputtingBean' and F_UP_UltraSoniclist[0] >= Ferment_Tank_bean_height:
  1395. F1 = 'F_InputtingBean_Finish'
  1396. print('------- F1 狀態更新:入料完成 -------')
  1397. # [致動器] 入料三通閥 ON排氣
  1398. data = { "tank_num": "F1", "command": "tank_threewayvalve_input_status", "value": "on" }
  1399. print('data: ', data)
  1400. # mqtt_f(data)
  1401. # [致動器] 馬達 0
  1402. data = { "tank_num": "F1", "command": "tank_motor_status", "value": "0" }
  1403. print('data: ', data)
  1404. # mqtt_f(data)
  1405. # [致動器] 入料真空吸料機 OFF
  1406. data = { "tank_num": "F1", "command": "input_vacuum_status", "value": "off" }
  1407. print('data: ', data)
  1408. # mqtt_f(data)
  1409. if F2 == 'F_InputtingBean' and F_UP_UltraSoniclist[1] < Ferment_Tank_bean_height:
  1410. F2 = 'F_InputtingBean_Pause'
  1411. print('------- F2 狀態更新:入料暫停 -------')
  1412. elif F2 == 'F_InputtingBean' and F_UP_UltraSoniclist[1] >= Ferment_Tank_bean_height:
  1413. F2 = 'F_InputtingBean_Finish'
  1414. print('------- F2 狀態更新:入料完成 -------')
  1415. if F3 == 'F_InputtingBean' and F_UP_UltraSoniclist[2] < Ferment_Tank_bean_height:
  1416. F3 = 'F_InputtingBean_Pause'
  1417. print('------- F3 狀態更新:入料暫停 -------')
  1418. elif F3 == 'F_InputtingBean' and F_UP_UltraSoniclist[2] >= Ferment_Tank_bean_height:
  1419. F3 = 'F_InputtingBean_Finish'
  1420. print('------- F3 狀態更新:入料完成 -------')
  1421. if F4 == 'F_InputtingBean' and F_UP_UltraSoniclist[3] < Ferment_Tank_bean_height:
  1422. F4 = 'F_InputtingBean_Pause'
  1423. print('------- F4 狀態更新:入料暫停 -------')
  1424. elif F4 == 'F_InputtingBean' and F_UP_UltraSoniclist[3] >= Ferment_Tank_bean_height:
  1425. F4 = 'F_InputtingBean_Finish'
  1426. print('------- F4 狀態更新:入料完成 -------')
  1427. if F5 == 'F_InputtingBean' and F_UP_UltraSoniclist[4] < Ferment_Tank_bean_height:
  1428. F5 = 'F_InputtingBean_Pause'
  1429. print('------- F5 狀態更新:入料暫停 -------')
  1430. elif F5 == 'F_InputtingBean' and F_UP_UltraSoniclist[4] >= Ferment_Tank_bean_height:
  1431. F5 = 'F_InputtingBean_Finish'
  1432. print('------- F5 狀態更新:入料完成 -------')
  1433. if F6 == 'F_InputtingBean' and F_UP_UltraSoniclist[5] < Ferment_Tank_bean_height:
  1434. F6 = 'F_InputtingBean_Pause'
  1435. print('------- F6 狀態更新:入料暫停 -------')
  1436. elif F6 == 'F_InputtingBean' and F_UP_UltraSoniclist[5] >= Ferment_Tank_bean_height:
  1437. F6 = 'F_InputtingBean_Finish'
  1438. print('------- F6 狀態更新:入料完成 -------')
  1439. else:
  1440. # 若入料儲豆槽非可出料狀態
  1441. print('發酵入料儲豆槽空, 桶槽無法入料')
  1442. else:
  1443. # [介面] 若未啟用 發酵 狀態
  1444. print('未啟用發酵流程')
  1445. # ----- 10/07 --------------------------------------------------------------------------------------------------------------------------------
  1446. # ----- 發酵桶槽 F1~F6 入料→入料暫停判斷 ------------------------------
  1447. ### 移到入料
  1448. # ----- 發酵桶槽 F1~F6 入料→入水判斷 ------------------------------
  1449. # -- 取得發酵桶槽 F1~F6 夾層水位高度 WaterLevel -------------
  1450. F_UP_tank_WaterLevel = []
  1451. for i in range(1, 7, 1):
  1452. tank_WaterLevel = ferment_tank_WaterLevel.query.filter_by(tank_num='F' + str(i)).order_by(text('datetime desc')).first()
  1453. WaterLevel = float(tank_WaterLevel.WaterLevel)
  1454. F_UP_tank_WaterLevel.append(WaterLevel)
  1455. print('F_UP_tank_WaterLevel: ', F_UP_tank_WaterLevel)
  1456. # -- 取得發酵桶槽 F1~F6 夾層水位高度 WaterLevel -------------
  1457. Ferment_InputtingBeanFinish_number = int(F_UP_tanklist.count('F_InputtingBean_Finish'))
  1458. Ferment_InputtingWater_number = int(F_UP_tanklist.count('F_InputtingWater'))
  1459. # 入水條件:當前桶槽入豆 + 沒有其他桶槽入水
  1460. if Ferment_InputtingBeanFinish_number >= 1 and Ferment_InputtingWater_number == 0:
  1461. # 當入豆到指定高度時, 狀態轉換為入水 (桶槽編號小者優先)
  1462. # !!! 發酵槽 F_InputtingBean_Finish 狀態應該請硬體判斷
  1463. # !!! 若狀態為 F_InputtingBean 且 超音波值>指定生豆高度, 狀態改為 F_InputtingBean_Finish 並等待入水 F_InputtingWater
  1464. # 這邊是考慮一次僅一個桶槽入水, 多桶入水會不會速度變慢?
  1465. if F1 == 'F_InputtingBean_Finish':
  1466. F1 = 'F_InputtingWater'
  1467. print('------- F1 狀態更新:入水中 -------')
  1468. # [致動器] 外桶浮選三通閥 ON
  1469. data = { "tank_num": "F1", "command": "outer_threewayvalve_float_status", "value": "on" }
  1470. print('data: ', data)
  1471. # mqtt_f(data)
  1472. # 桶內高度若低於水位指定高度, [致動器] 桶內進水電磁閥 ON
  1473. if F_UP_UltraSoniclist[0] < Ferment_Tank_water_height:
  1474. data = { "tank_num": "F1", "command": "tank_solenoid_water_in_status", "value": "on" }
  1475. print('data: ', data)
  1476. # mqtt_f(data)
  1477. # [致動器] 桶內進水電磁閥 OFF
  1478. else:
  1479. data = { "tank_num": "F1", "command": "tank_solenoid_water_in_status", "value": "off" }
  1480. print('data: ', data)
  1481. # mqtt_f(data)
  1482. # 桶外水位高度若低於水位計高度, [致動器] 桶外進水電磁閥 ON
  1483. if F_UP_tank_WaterLevel[0] != 1:
  1484. data = { "tank_num": "F1", "command": "outer_solenoid_water_status", "value": "on" }
  1485. print('data: ', data)
  1486. # mqtt_f(data)
  1487. # [致動器] 桶外進水電磁閥 OFF
  1488. else:
  1489. data = { "tank_num": "F1", "command": "outer_solenoid_water_status", "value": "off" }
  1490. print('data: ', data)
  1491. # mqtt_f(data)
  1492. elif F2 == 'F_InputtingBean_Finish':
  1493. F2 = 'F_InputtingWater'
  1494. print('------- F2 狀態更新:入水中 -------')
  1495. elif F3 == 'F_InputtingBean_Finish':
  1496. F3 = 'F_InputtingWater'
  1497. print('------- F3 狀態更新:入水中 -------')
  1498. elif F4 == 'F_InputtingBean_Finish':
  1499. F4 = 'F_InputtingWater'
  1500. print('------- F4 狀態更新:入水中 -------')
  1501. elif F5 == 'F_InputtingBean_Finish':
  1502. F5 = 'F_InputtingWater'
  1503. print('------- F5 狀態更新:入水中 -------')
  1504. elif F6 == 'F_InputtingBean_Finish':
  1505. F6 = 'F_InputtingWater'
  1506. print('------- F6 狀態更新:入水中 -------')
  1507. # ----- 發酵桶槽 F1~F6 入水→發酵判斷 ------------------------------
  1508. # !!! 發酵槽 F_Fermenting 狀態應該請硬體判斷
  1509. # !!! 若狀態為 F_InputtingWater 且 超音波值>指定水位高度 且 夾層水位計 == 1, 狀態改為 F_Fermenting 發酵
  1510. if F1 == 'F_InputtingWater':
  1511. if F_UP_tank_WaterLevel[0] == 1 and F_UP_UltraSoniclist[0] >= Ferment_Tank_water_height:
  1512. # 桶外水位高度高於水位計高度, [致動器] 桶外進水電磁閥 OFF
  1513. data = { "tank_num": "F1", "command": "outer_solenoid_water_status", "value": "off" }
  1514. print('data: ', data)
  1515. # mqtt_f(data)
  1516. # 桶內高度若高於水位指定高度, [致動器] 桶內進水電磁閥 OFF
  1517. data = { "tank_num": "F1", "command": "tank_solenoid_water_in_status", "value": "off" }
  1518. print('data: ', data)
  1519. # mqtt_f(data)
  1520. F1 = 'F_Fermenting'
  1521. print('------- F1 狀態更新:發酵中 -------')
  1522. # [致動器] 馬達 (指定轉速)
  1523. data = { "tank_num": "F1", "command": "tank_motor_status", "value": Ferment_Tank_motor_rpm }
  1524. print('data: ', data)
  1525. # mqtt_f(data)
  1526. # [致動器] 溫控開關 ON
  1527. # data = { "tank_num": "F1", "command": "tank_temp_enable_status", "value": "on" }
  1528. # print('data: ', data)
  1529. # # mqtt_f(data)
  1530. # [致動器] 設定溫度、持溫時間
  1531. # [致動器] 加熱器 1 ON
  1532. data = { "tank_num": "F1", "command": "tank_heater1_status", "value": "on" }
  1533. print('data: ', data)
  1534. # mqtt_f(data)
  1535. elif F_UP_tank_WaterLevel[0] == 1:
  1536. # 桶外水位高度高於水位計高度, [致動器] 桶外進水電磁閥 OFF
  1537. data = { "tank_num": "F1", "command": "outer_solenoid_water_status", "value": "off" }
  1538. print('data: ', data)
  1539. # mqtt_f(data)
  1540. elif F_UP_UltraSoniclist[0] >= Ferment_Tank_water_height:
  1541. # 桶內高度若高於水位指定高度, [致動器] 桶內進水電磁閥 OFF
  1542. data = { "tank_num": "F1", "command": "tank_solenoid_water_in_status", "value": "off" }
  1543. print('data: ', data)
  1544. # mqtt_f(data)
  1545. if F2 == 'F_InputtingWater' and F_UP_tank_WaterLevel[1] == 1 and F_UP_UltraSoniclist[1] >= Ferment_Tank_water_height:
  1546. F2 = 'F_Fermenting'
  1547. print('------- F2 狀態更新:發酵中 -------')
  1548. if F3 == 'F_InputtingWater' and F_UP_tank_WaterLevel[2] == 1 and F_UP_UltraSoniclist[2] >= Ferment_Tank_water_height:
  1549. F3 = 'F_Fermenting'
  1550. print('------- F3 狀態更新:發酵中 -------')
  1551. if F4 == 'F_InputtingWater' and F_UP_tank_WaterLevel[3] == 1 and F_UP_UltraSoniclist[3] >= Ferment_Tank_water_height:
  1552. F4 = 'F_Fermenting'
  1553. print('------- F4 狀態更新:發酵中 -------')
  1554. if F5 == 'F_InputtingWater' and F_UP_tank_WaterLevel[4] == 1 and F_UP_UltraSoniclist[4] >= Ferment_Tank_water_height:
  1555. F5 = 'F_Fermenting'
  1556. print('------- F5 狀態更新:發酵中 -------')
  1557. if F6 == 'F_InputtingWater' and F_UP_tank_WaterLevel[5] == 1 and F_UP_UltraSoniclist[5] >= Ferment_Tank_water_height:
  1558. F6 = 'F_Fermenting'
  1559. print('------- F6 狀態更新:發酵中 -------')
  1560. # ----- 發酵桶槽 F1~F6 發酵→可出豆 判斷 ------------------------------
  1561. # TODO
  1562. # !!! 發酵槽 F_Fermenting 狀態應該請硬體判斷
  1563. # !!! 若狀態為 F_InputtingWater 且 超音波值>指定水位高度 且 夾層水位計 == 1, 狀態改為 F_Fermenting 發酵
  1564. # 可以多桶各自執行發酵排程
  1565. # -- 取得發酵桶槽 F1~F6 pH -------------
  1566. F_UP_tank_PH = []
  1567. for i in range(1, 7, 1):
  1568. tank_PH = ferment_tank_PH.query.filter_by(tank_num='F' + str(i)).order_by(text('datetime desc')).first()
  1569. PH = float(tank_PH.PH)
  1570. F_UP_tank_PH.append(PH)
  1571. print('F_UP_tank_PH: ', F_UP_tank_PH)
  1572. # -- 取得發酵桶槽 F1~F6 pH -------------
  1573. # -- 取得發酵桶槽 F1~F6 SHT11_TEMP -------------
  1574. F_UP_tank_SHT11_Temp = []
  1575. for i in range(1, 7, 1):
  1576. tank_SHT11 = ferment_tank_SHT11.query.filter_by(tank_num='F' + str(i)).order_by(text('datetime desc')).first()
  1577. SHT11_Temp = float(tank_SHT11.SHT11_Temp)
  1578. F_UP_tank_SHT11_Temp.append(SHT11_Temp)
  1579. print('F_UP_tank_SHT11_Temp: ', F_UP_tank_SHT11_Temp)
  1580. # -- 取得發酵桶槽 F1~F6 SHT11_TEMP -------------
  1581. #
  1582. if F1 == 'F_Fermenting' and F_UP_tank_SHT11_Temp[0] >= Ferment_Tank_fermenting_temp and F_UP_tank_PH[0] <= Ferment_Tank_fermenting_pH:
  1583. print('發酵等待 ', Ferment_Tank_fermenting_time, ' 秒')
  1584. timer = time.time()
  1585. while True:
  1586. if (time.time() - timer) > Ferment_Tank_fermenting_time :
  1587. # [致動器] 外桶浮選三通閥 OFF
  1588. data = { "tank_num": "F1", "command": "outer_threewayvalve_float_status", "value": "off" }
  1589. print('data: ', data)
  1590. # mqtt_f(data)
  1591. # [致動器] 馬達 0
  1592. data = { "tank_num": "F1", "command": "tank_motor_status", "value": "0" }
  1593. print('data: ', data)
  1594. # mqtt_f(data)
  1595. # [致動器] 溫控開關 OFF
  1596. # data = { "tank_num": "F1", "command": "tank_temp_enable_status", "value": "off" }
  1597. # print('data: ', data)
  1598. # # mqtt_f(data)
  1599. # [致動器] 加熱器 1 OFF
  1600. data = { "tank_num": "F1", "command": "tank_heater1_status", "value": "off" }
  1601. print('data: ', data)
  1602. # mqtt_f(data)
  1603. break
  1604. # [致動器] 廢水排水閥 (桶內) ON
  1605. data = { "tank_num": "F1", "command": "tank_solenoid_water_out_status", "value": "on" }
  1606. print('data: ', data)
  1607. # mqtt_f(data)
  1608. print('桶內廢水排水 ', Ferment_Tank_WaterOut_time, ' 秒')
  1609. timer = time.time()
  1610. while True:
  1611. if (time.time() - timer) > Ferment_Tank_WaterOut_time:
  1612. # [致動器] 廢水排水閥 (桶內) OFF
  1613. data = { "tank_num": "F1", "command": "tank_solenoid_water_out_status", "value": "off" }
  1614. print('data: ', data)
  1615. # mqtt_f(data)
  1616. break
  1617. F1 = 'F_OutputtingBean'
  1618. print('------- F1 狀態更新:可出豆 -------')
  1619. if F2 == 'F_Fermenting' and F_UP_tank_PH[1] <= Ferment_Tank_fermenting_pH:
  1620. F2 = 'F_OutputtingBean'
  1621. print('------- F1 狀態更新:可出豆 -------')
  1622. if F3 == 'F_Fermenting' and F_UP_tank_PH[2] <= Ferment_Tank_fermenting_pH:
  1623. F3 = 'F_OutputtingBean'
  1624. print('------- F1 狀態更新:可出豆 -------')
  1625. if F4 == 'F_Fermenting' and F_UP_tank_PH[3] <= Ferment_Tank_fermenting_pH:
  1626. F4 = 'F_OutputtingBean'
  1627. print('------- F1 狀態更新:可出豆 -------')
  1628. if F5 == 'F_Fermenting' and F_UP_tank_PH[4] <= Ferment_Tank_fermenting_pH:
  1629. F5 = 'F_OutputtingBean'
  1630. print('------- F1 狀態更新:可出豆 -------')
  1631. if F6 == 'F_Fermenting' and F_UP_tank_PH[5] <= Ferment_Tank_fermenting_pH:
  1632. F6 = 'F_OutputtingBean'
  1633. print('------- F1 狀態更新:可出豆 -------')
  1634. if F1 == 'F_OutputtingBean' and F_UP_UltraSoniclist[0] < Ferment_Tank_bean_empty:
  1635. # 增加消毒電磁閥開關
  1636. # [致動器] 消毒電磁閥 ON
  1637. data = { "tank_num": "F1", "command": "tank_solenoid_disinfect_status", "value": "on" }
  1638. print('data: ', data)
  1639. # mqtt_f(data)
  1640. # 暫停 (消毒時間) 秒
  1641. timer = time.time()
  1642. while True:
  1643. if (time.time() - timer) > Ferment_Tank_Disinfect_time :
  1644. # [致動器] 消毒電磁閥 OFF
  1645. data = { "tank_num": "F1", "command": "tank_solenoid_disinfect_status", "value": "off" }
  1646. print('data: ', data)
  1647. # mqtt_f(data)
  1648. break
  1649. print('------- F1 狀態更新:空桶等待 -------')
  1650. F1 = 'F_Waiting'
  1651. # # ----- [測試用] !!!!! F1~F6 入料完成→可出豆 判斷 ------------------------------
  1652. # if F1 == 'F_InputtingBean_Finish': F1 = 'F_OutputtingBean'
  1653. # if F2 == 'F_InputtingBean_Finish': F2 = 'F_OutputtingBean'
  1654. # if F3 == 'F_InputtingBean_Finish': F3 = 'F_OutputtingBean'
  1655. # if F4 == 'F_InputtingBean_Finish': F4 = 'F_OutputtingBean'
  1656. # if F5 == 'F_InputtingBean_Finish': F5 = 'F_OutputtingBean'
  1657. # if F6 == 'F_InputtingBean_Finish': F6 = 'F_OutputtingBean'
  1658. # # ----- [測試用] !!!!! F1~F6 入料完成→可出豆 判斷 ------------------------------
  1659. # ----- 發酵桶槽 F1~F6 可出豆→出料儲豆槽 FO1 判斷 ------------------------------
  1660. # ----- 發酵出料儲豆槽 FO1~FO2 入豆→可出豆判斷 ------------------------------
  1661. # -- 取得發酵出料儲豆槽 FO1~FO2 桶內高度 UltraSonic -------------
  1662. FO_UP_UltraSoniclist = []
  1663. for i in range(1, 3, 1):
  1664. output_UltraSonic = ferment_output_UltraSonic.query.filter_by(tank_num='FO' + str(i)).order_by(text('datetime desc')).first()
  1665. UltraSonic = float(output_UltraSonic.UltraSonic)
  1666. FO_UP_UltraSoniclist.append(UltraSonic)
  1667. print('FO_UP_UltraSoniclist: ', FO_UP_UltraSoniclist)
  1668. # 桶槽可出豆, 且出料儲豆槽等待時, 出料儲豆槽入豆
  1669. # 參考 -- 發酵桶槽 F1~F6 可出豆→出料儲豆槽 FO1 判斷 --
  1670. Ferment_OutputtingBean_number = int(F_UP_tanklist.count('F_OutputtingBean'))
  1671. # !!! 若出料儲豆槽狀態為 FO_Waiting(空桶等待) 且 桶槽狀態為 F_OutputtingBean 可出豆, 與 FO1 配合開始出豆
  1672. if FO1 == 'FO_Waiting' and Ferment_OutputtingBean_number >= 1:
  1673. if F1 == 'F_OutputtingBean':
  1674. FO1 = 'FO_InputtingBean'
  1675. print('------- F1 狀態更新:出豆中 -------')
  1676. print('------- FO1 狀態更新:入豆中 -------')
  1677. # [致動器] 出料真空吸料機 ON
  1678. data = { "tank_num": "F1", "command": "input_vacuum_status", "value": "on" }
  1679. print('data: ', data)
  1680. # mqtt_f(data)
  1681. # 暫停 3 秒
  1682. timer = time.time()
  1683. while True:
  1684. if (time.time() - timer) > 3 :
  1685. # [致動器] 蝴蝶閥 ON
  1686. data = { "tank_num": "F1", "command": "tank_diskvalve_status", "value": "on" }
  1687. print('data: ', data)
  1688. # mqtt_f(data)
  1689. break
  1690. timer = time.time()
  1691. while True:
  1692. if (time.time() - timer) > Ferment_Output_vacuumON_time:
  1693. # [致動器] 出料真空吸料機 OFF
  1694. data = { "tank_num": "F1", "command": "input_vacuum_status", "value": "off" }
  1695. print('data: ', data)
  1696. # mqtt_f(data)
  1697. timer = time.time()
  1698. break
  1699. while True:
  1700. if (time.time() - timer) > Ferment_Output_vacuumOFF_time:
  1701. break
  1702. elif F2 == 'F_OutputtingBean':
  1703. FO1 = 'FO_InputtingBean'
  1704. print('------- F2 狀態更新:出豆中 -------')
  1705. print('------- FO1 狀態更新:入豆中 -------')
  1706. elif F3 == 'F_OutputtingBean':
  1707. FO1 = 'FO_InputtingBean'
  1708. print('------- F3 狀態更新:出豆中 -------')
  1709. print('------- FO1 狀態更新:入豆中 -------')
  1710. elif F4 == 'F_OutputtingBean':
  1711. FO1 = 'FO_InputtingBean'
  1712. print('------- F4 狀態更新:出豆中 -------')
  1713. print('------- FO1 狀態更新:入豆中 -------')
  1714. elif F5 == 'F_OutputtingBean':
  1715. FO1 = 'FO_InputtingBean'
  1716. print('------- F5 狀態更新:出豆中 -------')
  1717. print('------- FO1 狀態更新:入豆中 -------')
  1718. elif F6 == 'F_OutputtingBean':
  1719. FO1 = 'FO_InputtingBean'
  1720. print('------- F6 狀態更新:出豆中 -------')
  1721. print('------- FO1 狀態更新:入豆中 -------')
  1722. # 出料儲豆槽入料滿時, 出料儲豆槽狀態改為可出豆 (需判斷是哪個桶槽, 好關閉蝴蝶閥)
  1723. elif FO1 == 'FO_InputtingBean' and FO_UP_UltraSoniclist[0] >= Ferment_Output_bean_height:
  1724. if F1 == 'F_OutputtingBean':
  1725. FO1 = 'FO_OutputtingBean'
  1726. print('------- FO1 狀態更新:可出豆 -------')
  1727. # [致動器] 出料真空吸料機 OFF
  1728. data = { "tank_num": "F1", "command": "input_vacuum_status", "value": "off" }
  1729. print('data: ', data)
  1730. # mqtt_f(data)
  1731. # [致動器] 蝴蝶閥 OFF
  1732. data = { "tank_num": "F1", "command": "tank_diskvalve_status", "value": "off" }
  1733. print('data: ', data)
  1734. # mqtt_f(data)
  1735. elif F2 == 'F_OutputtingBean':
  1736. pass # TODO ←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←← 1020 TODO
  1737. # 出料儲豆槽入料時, 桶槽已空, 則桶槽回到空桶等待, 出料儲豆槽可出豆 ←←←←←←←←←←←←←←←←←←←←←←←←←← 1012 這段怪怪的
  1738. elif FO1 == 'FO_InputtingBean' and Ferment_OutputtingBean_number == 1:
  1739. if F1 == 'F_OutputtingBean':
  1740. if F_UP_UltraSoniclist[0] < Ferment_Tank_bean_empty:
  1741. FO1 = 'FO_OutputtingBean'
  1742. print('------- FO1 狀態更新:可出豆 -------')
  1743. # 增加消毒電磁閥開關
  1744. # [致動器] 消毒電磁閥 ON
  1745. data = { "tank_num": "F1", "command": "tank_solenoid_disinfect_status", "value": "on" }
  1746. print('data: ', data)
  1747. # mqtt_f(data)
  1748. # 暫停 (消毒時間) 秒
  1749. timer = time.time()
  1750. while True:
  1751. if (time.time() - timer) > Ferment_Tank_Disinfect_time :
  1752. # [致動器] 消毒電磁閥 OFF
  1753. data = { "tank_num": "F1", "command": "tank_solenoid_disinfect_status", "value": "off" }
  1754. print('data: ', data)
  1755. # mqtt_f(data)
  1756. break
  1757. print('------- F1 狀態更新:空桶等待 -------')
  1758. F1 = 'F_Waiting'
  1759. else:
  1760. # [致動器] 出料真空吸料機 ON
  1761. data = { "tank_num": "F1", "command": "input_vacuum_status", "value": "on" }
  1762. print('data: ', data)
  1763. # mqtt_f(data)
  1764. # 暫停 3 秒
  1765. timer = time.time()
  1766. while True:
  1767. if (time.time() - timer) > 3 :
  1768. # [致動器] 蝴蝶閥 ON
  1769. data = { "tank_num": "F1", "command": "tank_diskvalve_status", "value": "on" }
  1770. print('data: ', data)
  1771. # mqtt_f(data)
  1772. break
  1773. timer = time.time()
  1774. while True:
  1775. if (time.time() - timer) > Ferment_Output_vacuumON_time:
  1776. # [致動器] 出料真空吸料機 OFF
  1777. data = { "tank_num": "F1", "command": "input_vacuum_status", "value": "off" }
  1778. print('data: ', data)
  1779. # mqtt_f(data)
  1780. timer = time.time()
  1781. break
  1782. while True:
  1783. if (time.time() - timer) > Ferment_Output_vacuumOFF_time:
  1784. break
  1785. elif F2 == 'F_OutputtingBean' and F_UP_UltraSoniclist[1] < 5:
  1786. F2 = 'F_Waiting'
  1787. FO1 = 'FO_OutputtingBean'
  1788. print('------- F2 狀態更新:空桶等待 -------')
  1789. print('------- FO1 狀態更新:可出豆 -------')
  1790. elif F3 == 'F_OutputtingBean' and F_UP_UltraSoniclist[2] < 5:
  1791. F3 = 'F_Waiting'
  1792. FO1 = 'FO_OutputtingBean'
  1793. print('------- F3 狀態更新:空桶等待 -------')
  1794. print('------- FO1 狀態更新:可出豆 -------')
  1795. elif F4 == 'F_OutputtingBean' and F_UP_UltraSoniclist[3] < 5:
  1796. F4 = 'F_Waiting'
  1797. FO1 = 'FO_OutputtingBean'
  1798. print('------- F4 狀態更新:空桶等待 -------')
  1799. print('------- FO1 狀態更新:可出豆 -------')
  1800. elif F5 == 'F_OutputtingBean' and F_UP_UltraSoniclist[4] < 5:
  1801. F5 = 'F_Waiting'
  1802. FO1 = 'FO_OutputtingBean'
  1803. print('------- F5 狀態更新:空桶等待 -------')
  1804. print('------- FO1 狀態更新:可出豆 -------')
  1805. elif F6 == 'F_OutputtingBean' and F_UP_UltraSoniclist[5] < 5:
  1806. F6 = 'F_Waiting'
  1807. FO1 = 'FO_OutputtingBean'
  1808. print('------- F6 狀態更新:空桶等待 -------')
  1809. print('------- FO1 狀態更新:可出豆 -------')
  1810. # 出料儲豆槽出料 且 儲豆槽內高度低於 5 , 為空桶等待
  1811. elif FO1 == 'FO_OutputtingBean' and FO_UP_UltraSoniclist[0] < Ferment_Output_bean_empty:
  1812. FO1 = 'FO_Waiting'
  1813. # ----- 發酵入料儲豆槽 FI1~FI2 入豆→可出豆判斷 ------------------------------
  1814. # 1008 移到桶槽前面
  1815. # ----- 發酵出料儲豆槽 FO1~FO2 入豆→可出豆判斷 ------------------------------
  1816. # 1012 移到桶槽可出豆內
  1817. # ----- 將狀態寫入資料庫 ------------------------------
  1818. # 獲取文本框的值並賦值給user實體對象
  1819. F_status = ferment_container_status()
  1820. F_status.Ferment_Input_1 = FI1
  1821. F_status.Ferment_Input_2 = FI2
  1822. F_status.Ferment_Tank_1 = F1
  1823. F_status.Ferment_Tank_2 = F2
  1824. F_status.Ferment_Tank_3 = F3
  1825. F_status.Ferment_Tank_4 = F4
  1826. F_status.Ferment_Tank_5 = F5
  1827. F_status.Ferment_Tank_6 = F6
  1828. F_status.Ferment_Tank_7 = F7
  1829. F_status.Ferment_Tank_8 = F8
  1830. F_status.Ferment_Tank_9 = F9
  1831. F_status.Ferment_Tank_10 = F10
  1832. F_status.Ferment_Tank_11 = F11
  1833. F_status.Ferment_Tank_12 = F12
  1834. F_status.Ferment_Output_1 = FO1
  1835. F_status.Ferment_Output_2 = FO2
  1836. #將數據保存進資料庫
  1837. db.session.add(F_status)
  1838. # 手動提交
  1839. db.session.commit()
  1840. # ----- 將狀態寫入資料庫 ------------------------------
  1841. return jsonify({"Peel_Output_1":PO1,
  1842. "Ferment_Input_1":FI1,
  1843. "Ferment_Tank_1":F1,
  1844. "Ferment_Tank_2":F2,
  1845. "Ferment_Tank_3":F3,
  1846. "Ferment_Tank_4":F4,
  1847. "Ferment_Tank_5":F5,
  1848. "Ferment_Tank_6":F6,
  1849. "Ferment_Output_1":FO1
  1850. })
  1851. '''
  1852. return jsonify({"Ferment_Input_1":FI1,
  1853. "Ferment_Input_2":FI2,
  1854. "Ferment_Tank_1":F1,
  1855. "Ferment_Tank_2":F2,
  1856. "Ferment_Tank_3":F3,
  1857. "Ferment_Tank_4":F4,
  1858. "Ferment_Tank_5":F5,
  1859. "Ferment_Tank_6":F6,
  1860. "Ferment_Tank_7":F7,
  1861. "Ferment_Tank_8":F8,
  1862. "Ferment_Tank_9":F9,
  1863. "Ferment_Tank_10":F10,
  1864. "Ferment_Tank_11":F11,
  1865. "Ferment_Tank_12":F12,
  1866. "Ferment_Output_1":FO1,
  1867. "Ferment_Output_2":FO2
  1868. })
  1869. '''
  1870. # 1026 以流程圖判斷, 改寫 def ferment_auto_test, 以上為原備份
  1871. # 舊有 video 介面
  1872. @main.route('/video')
  1873. def main_video():
  1874. # 獲取登入信息
  1875. if 'id' in session and 'uname' in session and 'status' in session:
  1876. username = session['uname']
  1877. status = session['status']
  1878. if status == 9:
  1879. return render_template('signin_disable.html', params=locals())
  1880. elif status == 8:
  1881. return render_template('signin_new.html', params=locals())
  1882. return render_template('video.html', params=locals())
  1883. else:
  1884. return render_template('sign_in.html')
  1885. @main.route('/sitemap')
  1886. def sitemap():
  1887. if request.method == 'GET':
  1888. if 'id' in session and 'uname' in session and 'status' in session:
  1889. username = session['uname']
  1890. status = session['status']
  1891. print('username: ', username)
  1892. print('status: ', status)
  1893. if status == 9:
  1894. return render_template('signin_disable.html', **locals())
  1895. elif status == 8:
  1896. return render_template('signin_new.html', **locals())
  1897. return render_template('sitemap.html', **locals())
  1898. else:
  1899. return render_template('sign_in.html')
  1900. @main.route('/index_navbar', methods=['GET'])
  1901. def index_navbar():
  1902. return render_template('index_navbar.html')
  1903. @main.route('/camera_dry', methods=['GET', 'POST'])
  1904. def camera_dry():
  1905. if request.method == 'GET':
  1906. if 'id' in session and 'uname' in session and 'status' in session:
  1907. username = session['uname']
  1908. status = session['status']
  1909. if status == 9:
  1910. return render_template('signin_disable.html', params=locals())
  1911. elif status == 8:
  1912. return render_template('signin_new.html', params=locals())
  1913. return render_template('camera_dry.html', title='乾燥貨櫃攝影機', **locals())
  1914. else:
  1915. return render_template('sign_in.html')
  1916. @main.route('/camera_ferment', methods=['GET', 'POST'])
  1917. def camera_ferment():
  1918. if request.method == 'GET':
  1919. if 'id' in session and 'uname' in session and 'status' in session:
  1920. username = session['uname']
  1921. status = session['status']
  1922. if status == 9:
  1923. return render_template('signin_disable.html', params=locals())
  1924. elif status == 8:
  1925. return render_template('signin_new.html', params=locals())
  1926. return render_template('camera_ferment.html', title='發酵貨櫃攝影機', **locals())
  1927. else:
  1928. return render_template('sign_in.html')
  1929. @main.route('/camera_clean', methods=['GET', 'POST'])
  1930. def camera_clean():
  1931. if request.method == 'GET':
  1932. if 'id' in session and 'uname' in session and 'status' in session:
  1933. username = session['uname']
  1934. status = session['status']
  1935. if status == 9:
  1936. return render_template('signin_disable.html', params=locals())
  1937. elif status == 8:
  1938. return render_template('signin_new.html', params=locals())
  1939. return render_template('camera_clean.html', title='清洗貨櫃攝影機', **locals())
  1940. else:
  1941. return render_template('sign_in.html')
  1942. @main.route('/camera_dry_1', methods=['GET', 'POST'])
  1943. def camera_dry_2():
  1944. if request.method == 'GET':
  1945. return render_template('camera_dry_1.html', title='乾燥貨櫃攝影機', **locals())
  1946. @main.route('/camera_<tid>', methods=['GET', 'POST'])
  1947. def camera(tid):
  1948. if request.method == 'GET':
  1949. if 'id' in session and 'uname' in session and 'status' in session:
  1950. username = session['uname']
  1951. status = session['status']
  1952. if status == 9:
  1953. return render_template('signin_disable.html', params=locals())
  1954. elif status == 8:
  1955. return render_template('signin_new.html', params=locals())
  1956. if tid == 'CCargo_in':
  1957. WS_URL = 'ws:///60.250.156.230:1111'
  1958. camera_title = '清洗貨櫃內部'
  1959. return render_template('camera.html', title='[畫面]' + camera_title, **locals())
  1960. elif tid == 'CCargo_out':
  1961. WS_URL = 'ws:///60.250.156.230:1111'
  1962. camera_title = '清洗貨櫃外部'
  1963. return render_template('camera.html', title='[畫面]' + camera_title, **locals())
  1964. elif tid == 'FCargo_in':
  1965. WS_URL = 'ws:///60.250.156.230:1111'
  1966. camera_title = '發酵貨櫃內部'
  1967. return render_template('camera.html', title='[畫面]' + camera_title, **locals())
  1968. elif tid == 'FCargo_out':
  1969. WS_URL = 'ws:///60.250.156.230:1111'
  1970. camera_title = '發酵貨櫃外部'
  1971. return render_template('camera.html', title='[畫面]' + camera_title, **locals())
  1972. elif tid == 'DCargo_in':
  1973. WS_URL = 'ws:///60.250.156.230:1111'
  1974. camera_title = '乾燥貨櫃內部'
  1975. return render_template('camera.html', title='[畫面]' + camera_title, **locals())
  1976. elif tid == 'DCargo_out':
  1977. WS_URL = 'ws:///60.250.156.230:1111'
  1978. camera_title = '乾燥貨櫃外部'
  1979. return render_template('camera.html', title='[畫面]' + camera_title, **locals())
  1980. elif tid[:2] == 'DO':
  1981. WS_URL_list = ['ws:///60.250.156.230:1111',
  1982. 'ws:///60.250.156.230:2222'
  1983. ]
  1984. WS_URL = WS_URL_list[int(tid[2:])-1]
  1985. camera_title = '乾燥出料儲豆槽'
  1986. return render_template('camera.html', title='[畫面]' + camera_title + ' ' + tid, **locals())
  1987. elif tid[:2] == 'DI':
  1988. WS_URL_list = ['ws:///60.250.156.230:1111',
  1989. 'ws:///60.250.156.230:2222'
  1990. ]
  1991. WS_URL = WS_URL_list[int(tid[2:])-1]
  1992. camera_title = '乾燥入料儲豆槽'
  1993. return render_template('camera.html', title='[畫面]' + camera_title + ' ' + tid, **locals())
  1994. elif tid[:1] == 'D':
  1995. WS_URL_list = ['ws:///60.250.156.230:8093', # D1 攝影機已安裝
  1996. 'ws:///60.250.156.230:2222',
  1997. 'ws:///60.250.156.230:3333',
  1998. 'ws:///60.250.156.230:4444',
  1999. 'ws:///60.250.156.230:5555',
  2000. 'ws:///60.250.156.230:6666',
  2001. 'ws:///60.250.156.230:7777',
  2002. 'ws:///60.250.156.230:8888',
  2003. 'ws:///60.250.156.230:9999',
  2004. 'ws:///60.250.156.230:1010',
  2005. 'ws:///60.250.156.230:1111',
  2006. 'ws:///60.250.156.230:1212'
  2007. ]
  2008. WS_URL = WS_URL_list[int(tid[1:])-1]
  2009. camera_title = '乾燥槽'
  2010. return render_template('camera.html', title='[畫面]' + camera_title + ' ' + tid, **locals())
  2011. elif tid[:2] == 'FO':
  2012. WS_URL_list = ['ws:///60.250.156.230:1111',
  2013. 'ws:///60.250.156.230:2222'
  2014. ]
  2015. WS_URL = WS_URL_list[int(tid[2:])-1]
  2016. camera_title = '發酵出料儲豆槽'
  2017. return render_template('camera.html', title='[畫面]' + camera_title + ' ' + tid, **locals())
  2018. elif tid[:2] == 'FI':
  2019. WS_URL_list = ['ws:///60.250.156.230:1111',
  2020. 'ws:///60.250.156.230:2222'
  2021. ]
  2022. WS_URL = WS_URL_list[int(tid[2:])-1]
  2023. camera_title = '發酵入料儲豆槽'
  2024. return render_template('camera.html', title='[畫面]' + camera_title + ' ' + tid, **locals())
  2025. elif tid[:1] == 'F':
  2026. WS_URL_list = ['ws:///60.250.156.230:8089', # F1 攝影機已安裝
  2027. 'ws:///60.250.156.230:2222',
  2028. 'ws:///60.250.156.230:3333',
  2029. 'ws:///60.250.156.230:4444',
  2030. 'ws:///60.250.156.230:5555',
  2031. 'ws:///60.250.156.230:6666',
  2032. 'ws:///60.250.156.230:7777',
  2033. 'ws:///60.250.156.230:8888',
  2034. 'ws:///60.250.156.230:9999',
  2035. 'ws:///60.250.156.230:1010',
  2036. 'ws:///60.250.156.230:1111',
  2037. 'ws:///60.250.156.230:1212'
  2038. ]
  2039. WS_URL = WS_URL_list[int(tid[1:])-1]
  2040. camera_title = '發酵槽'
  2041. return render_template('camera.html', title='[畫面]' + camera_title + ' ' + tid, **locals())
  2042. elif tid[:2] == 'CO':
  2043. WS_URL_list = ['ws:///60.250.156.230:1111',
  2044. 'ws:///60.250.156.230:2222'
  2045. ]
  2046. WS_URL = WS_URL_list[int(tid[2:])-1]
  2047. camera_title = '清洗浮選出料儲豆槽'
  2048. return render_template('camera.html', title='[畫面]' + camera_title + ' ' + tid, **locals())
  2049. elif tid[:1] == 'C':
  2050. WS_URL_list = ['ws:///60.250.156.230:8088', # C1 攝影機已安裝
  2051. 'ws:///60.250.156.230:2222'
  2052. ]
  2053. WS_URL = WS_URL_list[int(tid[1:])-1]
  2054. camera_title = '清洗浮選槽'
  2055. return render_template('camera.html', title='[畫面]' + camera_title + ' ' + tid, **locals())
  2056. elif tid[:2] == 'SO':
  2057. WS_URL_list = ['ws:///60.250.156.230:1111',
  2058. 'ws:///60.250.156.230:2222'
  2059. ]
  2060. WS_URL = WS_URL_list[int(tid[2:])-1]
  2061. camera_title = '色選機出料儲豆槽'
  2062. return render_template('camera.html', title='[畫面]' + camera_title + ' ' + tid, **locals())
  2063. elif tid[:1] == 'S':
  2064. WS_URL_list = ['ws:///60.250.156.230:1111',
  2065. 'ws:///60.250.156.230:2222'
  2066. ]
  2067. WS_URL = WS_URL_list[int(tid[1:])-1]
  2068. camera_title = '色選機'
  2069. return render_template('camera.html', title='[畫面]' + camera_title + ' ' + tid, **locals())
  2070. elif tid[:2] == 'PO':
  2071. WS_URL_list = ['ws:///60.250.156.230:1111',
  2072. 'ws:///60.250.156.230:2222'
  2073. ]
  2074. WS_URL = WS_URL_list[int(tid[2:])-1]
  2075. camera_title = '脫皮機出料儲豆槽'
  2076. return render_template('camera.html', title='[畫面]' + camera_title + ' ' + tid, **locals())
  2077. elif tid[:1] == 'P':
  2078. WS_URL_list = ['ws:///60.250.156.230:1111',
  2079. 'ws:///60.250.156.230:2222'
  2080. ]
  2081. WS_URL = WS_URL_list[int(tid[1:])-1]
  2082. camera_title = '脫皮機'
  2083. return render_template('camera.html', title='[畫面]' + camera_title + ' ' + tid, **locals())
  2084. else:
  2085. return render_template('sign_in.html')
  2086. else:
  2087. pass
  2088. @main.route('/dry_SHT11_<dtid>', methods=['GET', 'POST'])
  2089. def dry_SHT11(dtid):
  2090. if request.method == 'GET':
  2091. tank_Soil = dry_tank_Soil.query.filter_by(tank_num='D' + dtid).order_by(text('datetime desc')).first()
  2092. tank_SHT11 = dry_tank_SHT11.query.filter_by(tank_num='D' + dtid).order_by(text('datetime desc')).first()
  2093. SHT11_Temp = tank_SHT11.SHT11_Temp
  2094. SHT11_Humidity = tank_SHT11.SHT11_Humidity
  2095. Soil_Temp = tank_Soil.soil_Temp
  2096. return jsonify({"SHT11_Temp":SHT11_Temp,
  2097. "SHT11_Humidity":SHT11_Humidity,
  2098. "Soil_Temp":Soil_Temp
  2099. })
  2100. else:
  2101. pass
  2102. @main.route('/dry_UltraSonic_<dtid>', methods=['GET', 'POST'])
  2103. def dry_UltraSonic(dtid):
  2104. if request.method == 'GET':
  2105. tank_UltraSonic = dry_tank_UltraSonic.query.filter_by(tank_num='D' + dtid).order_by(text('datetime desc')).first()
  2106. UltraSonic = tank_UltraSonic.UltraSonic
  2107. return jsonify({"UltraSonic":UltraSonic
  2108. })
  2109. else:
  2110. pass
  2111. @main.route('/dry_input_UltraSonic_<dtid>', methods=['GET', 'POST'])
  2112. def dry_input_UltraSonic(dtid):
  2113. if request.method == 'GET':
  2114. input_UltraSonic = dry_input_sensor.query.filter_by(tank_num='DI' + dtid).order_by(text('datetime desc')).first()
  2115. UltraSonic = input_UltraSonic.UltraSonic
  2116. return jsonify({"UltraSonic":UltraSonic
  2117. })
  2118. else:
  2119. pass
  2120. @main.route('/dry_output_UltraSonic_<dtid>', methods=['GET', 'POST'])
  2121. def dry_output_UltraSonic(dtid):
  2122. if request.method == 'GET':
  2123. output_UltraSonic = dry_output_sensor.query.filter_by(tank_num='DO' + dtid).order_by(text('datetime desc')).first()
  2124. UltraSonic = output_UltraSonic.UltraSonic
  2125. return jsonify({"UltraSonic":UltraSonic
  2126. })
  2127. else:
  2128. pass
  2129. @main.route('/ferment_SHT11_<ftid>', methods=['GET', 'POST'])
  2130. def ferment_SHT11(ftid):
  2131. if request.method == 'GET':
  2132. tank_SHT11 = ferment_tank_SHT11.query.filter_by(tank_num='F' + ftid).order_by(text('datetime desc')).first()
  2133. SHT11_Temp = tank_SHT11.SHT11_Temp
  2134. return jsonify({"SHT11_Temp":SHT11_Temp
  2135. })
  2136. else:
  2137. pass
  2138. @main.route('/ferment_WaterLevel_<ftid>', methods=['GET', 'POST'])
  2139. def ferment_WaterLevel(ftid):
  2140. if request.method == 'GET':
  2141. tank_WaterLevel = ferment_tank_WaterLevel.query.filter_by(tank_num='F' + ftid).order_by(text('datetime desc')).first()
  2142. WaterLevel = tank_WaterLevel.WaterLevel
  2143. return jsonify({"WaterLevel":WaterLevel
  2144. })
  2145. else:
  2146. pass
  2147. @main.route('/ferment_WaterIn_<ftid>', methods=['GET', 'POST'])
  2148. def ferment_WaterIn(ftid):
  2149. if request.method == 'GET':
  2150. tank_UltraSonic = ferment_tank_UltraSonic.query.filter_by(tank_num='F' + ftid).order_by(text('datetime desc')).first()
  2151. UltraSonic = (float(tank_UltraSonic.UltraSonic))
  2152. # tank_PressureWaterLevel = ferment_tank_PressureWaterLevel.query.filter_by(tank_num='F' + ftid).order_by(text('datetime desc')).first()
  2153. # PressureWaterLevel = '{:.2f}'.format((float(tank_PressureWaterLevel.PressureWaterLevel) -15 -90.278)/9.4214)
  2154. tank_WaterLevel = ferment_tank_WaterLevel.query.filter_by(tank_num='F' + ftid).order_by(text('datetime desc')).first()
  2155. WaterLevel = tank_WaterLevel.WaterLevel
  2156. return jsonify({"UltraSonic":UltraSonic,
  2157. "WaterLevel":WaterLevel
  2158. })
  2159. else:
  2160. pass
  2161. @main.route('/ferment_UltraSonic_<ftid>', methods=['GET', 'POST'])
  2162. def ferment_UltraSonic(ftid):
  2163. if request.method == 'GET':
  2164. tank_UltraSonic = ferment_tank_UltraSonic.query.filter_by(tank_num='F' + ftid).order_by(text('datetime desc')).first()
  2165. UltraSonic = (float(tank_UltraSonic.UltraSonic))
  2166. return jsonify({"UltraSonic":UltraSonic
  2167. })
  2168. else:
  2169. pass
  2170. '''
  2171. @main.route('/ferment_LiDAR_<ftid>', methods=['GET', 'POST'])
  2172. def ferment_LiDAR(ftid):
  2173. if request.method == 'GET':
  2174. tank_LiDAR = ferment_tank_LiDAR.query.filter_by(tank_num='F' + ftid).order_by(text('datetime desc')).first()
  2175. LiDAR = tank_LiDAR.LiDAR
  2176. return jsonify({"LiDAR":LiDAR
  2177. })
  2178. else:
  2179. pass
  2180. '''
  2181. @main.route('/ferment_input_UltraSonic_<ftid>', methods=['GET', 'POST'])
  2182. def ferment_input_UltraSonic_(ftid):
  2183. if request.method == 'GET':
  2184. input_UltraSonic = ferment_input_UltraSonic.query.filter_by(tank_num='FI' + ftid).order_by(text('datetime desc')).first()
  2185. UltraSonic = input_UltraSonic.UltraSonic
  2186. return jsonify({"UltraSonic":UltraSonic
  2187. })
  2188. else:
  2189. pass
  2190. @main.route('/ferment_output_UltraSonic_<ftid>', methods=['GET', 'POST'])
  2191. def ferment_output_UltraSonic_(ftid):
  2192. if request.method == 'GET':
  2193. output_UltraSonic = ferment_output_UltraSonic.query.filter_by(tank_num='FO' + ftid).order_by(text('datetime desc')).first()
  2194. UltraSonic = output_UltraSonic.UltraSonic
  2195. return jsonify({"UltraSonic":UltraSonic
  2196. })
  2197. else:
  2198. pass
  2199. @main.route('/ferment_watertesting_<tid>', methods=['GET', 'POST'])
  2200. def ferment_watertesting(tid):
  2201. if request.method == 'GET':
  2202. tank_PH = ferment_tank_PH.query.filter_by(tank_num='F' + tid).order_by(text('datetime desc')).first()
  2203. tank_ORP = ferment_tank_ORP.query.filter_by(tank_num='F' + tid).order_by(text('datetime desc')).first()
  2204. tank_DO = ferment_tank_DO.query.filter_by(tank_num='F' + tid).order_by(text('datetime desc')).first()
  2205. tank_EC = ferment_tank_EC.query.filter_by(tank_num='F' + tid).order_by(text('datetime desc')).first()
  2206. PH = tank_PH.PH
  2207. ORP = tank_ORP.ORP
  2208. DO = tank_DO.DO
  2209. EC = tank_EC.EC
  2210. return jsonify({"PH":PH,
  2211. "ORP":ORP,
  2212. "DO":DO,
  2213. "EC":EC,
  2214. })
  2215. '''
  2216. @main.route('/ferment_PressureWaterLevel_<ftid>', methods=['GET', 'POST'])
  2217. def ferment_PressureWaterLevel(ftid):
  2218. if request.method == 'GET':
  2219. tank_PressureWaterLevel = ferment_tank_PressureWaterLevel.query.filter_by(tank_num='F' + ftid).order_by(text('datetime desc')).first()
  2220. PressureWaterLevel = float(tank_PressureWaterLevel.PressureWaterLevel)
  2221. #PressureWaterLevel = '{:.2f}'.format((float(tank_PressureWaterLevel.PressureWaterLevel) -15 -90.278)/9.4214)
  2222. print("PressureWaterLevel_start: ", PressureWaterLevel)
  2223. return jsonify({"PressureWaterLevel":PressureWaterLevel
  2224. })
  2225. # 此段刪除
  2226. while int(PressureWaterLevel) < int(testing_water_height):
  2227. tank_water_status = 'NO'
  2228. time.sleep(3)
  2229. # print(ferment_water_height(testing_water_height))
  2230. # [目前桶內水位高度]發酵槽_感測器_壓力式水位計
  2231. tank_PressureWaterLevel = ferment_tank_PressureWaterLevel.query.filter_by(tank_num='F1').order_by(text('datetime desc')).first()
  2232. PressureWaterLevel = tank_PressureWaterLevel.PressureWaterLevel
  2233. print("-- MySQL --")
  2234. print("tank_PressureWaterLevel: ", tank_PressureWaterLevel)
  2235. # tank_water_height()
  2236. # print("PressureWaterLevel_while: ", PressureWaterLevel)
  2237. ferment_water_height(testing_water_height)
  2238. return jsonify({"testing_water_height":testing_water_height,
  2239. "tank_water_status":tank_water_status
  2240. })
  2241. else:
  2242. tank_water_status = 'OK'
  2243. print("PressureWaterLevel_else: ", PressureWaterLevel)
  2244. return jsonify({"testing_water_height":testing_water_height,
  2245. "tank_water_status":tank_water_status
  2246. })
  2247. '''
  2248. '''
  2249. def tank_water_height():
  2250. tank_PressureWaterLevel = ferment_tank_PressureWaterLevel.query.filter_by(tank_num='F1').order_by(text('datetime desc')).first()
  2251. PressureWaterLevel = tank_PressureWaterLevel.PressureWaterLevel
  2252. print("tank_water_height", PressureWaterLevel)
  2253. return PressureWaterLevel
  2254. '''
  2255. @main.route('/loading/SO<sotid>', methods=['GET', 'POST'])
  2256. def SO_loading(sotid):
  2257. if request.method == 'GET':
  2258. # 致動器_脫皮機_ALL
  2259. output_actuator = colorselect_output_actuator.query.filter_by(tank_num='SO'+sotid).order_by(text('datetime desc')).first()
  2260. output_vacuum = output_actuator.vacuum
  2261. return jsonify({"output_vacuum":output_vacuum
  2262. })
  2263. else:
  2264. pass
  2265. @main.route('/loading/S<stid>', methods=['GET', 'POST'])
  2266. def S_loading(stid):
  2267. if request.method == 'GET':
  2268. # 色選機
  2269. return
  2270. else:
  2271. pass
  2272. @main.route('/loading/PO<potid>', methods=['GET', 'POST'])
  2273. def PO_loading(potid):
  2274. if request.method == 'GET':
  2275. # 致動器_脫皮機_ALL
  2276. output_actuator = peel_output_actuator.query.filter_by(tank_num='PO'+potid).order_by(text('datetime desc')).first()
  2277. output_vacuum = output_actuator.vacuum
  2278. return jsonify({"output_vacuum":output_vacuum
  2279. })
  2280. else:
  2281. pass
  2282. @main.route('/loading/P<ptid>', methods=['GET', 'POST'])
  2283. def P_loading(ptid):
  2284. if request.method == 'GET':
  2285. # 致動器_脫皮機_ALL
  2286. tank_actuator = peel_tank_actuator.query.filter_by(tank_num='P'+ptid).order_by(text('datetime desc')).first()
  2287. tank_vacuum = tank_actuator.vacuum
  2288. tank_motor = tank_actuator.motor
  2289. return jsonify({"tank_vacuum":tank_vacuum,
  2290. "tank_motor":tank_motor})
  2291. else:
  2292. pass
  2293. @main.route('/loading/CO<cotid>', methods=['GET', 'POST'])
  2294. def CO_loading(cotid):
  2295. if request.method == 'GET':
  2296. output_actuator = clean_output_actuator.query.filter_by(tank_num='CO'+cotid).order_by(text('datetime desc')).first()
  2297. output_vacuum = output_actuator.vacuum
  2298. return jsonify({"output_vacuum":output_vacuum
  2299. })
  2300. else:
  2301. pass
  2302. @main.route('/loading/C<ctid>', methods=['GET', 'POST'])
  2303. def C_loading(ctid):
  2304. if request.method == 'GET':
  2305. # 致動器_浮選清洗_ALL
  2306. tank_actuator = clean_tank_actuator.query.filter_by(tank_num='C'+ctid).order_by(text('datetime desc')).first()
  2307. tank_vacuum = tank_actuator.vacuum
  2308. tank_pump_waterInput = tank_actuator.pump_waterInput
  2309. tank_pump_waterL2L3 = tank_actuator.pump_waterL2L3
  2310. tank_pump_waterL4L5 = tank_actuator.pump_waterL4L5
  2311. tank_solenoid_waterL2L3 = tank_actuator.solenoid_waterL2L3
  2312. tank_solenoid_waterL4L5 = tank_actuator.solenoid_waterL4L5
  2313. tank_stepping_motor = tank_actuator.stepping_motor
  2314. print('tank_stepping_motor:', tank_stepping_motor)
  2315. # 感測器_浮選清洗_超音波感測器
  2316. tank_UltraSonic = clean_tank_UltraSonic.query.filter_by(tank_num='C'+ctid).order_by(text('datetime desc')).first()
  2317. UltraSonic= tank_UltraSonic.UltraSonic
  2318. return jsonify({"UltraSonic":UltraSonic,
  2319. "tank_vacuum":tank_vacuum,
  2320. "tank_pump_waterInput":tank_pump_waterInput,
  2321. "tank_pump_waterL2L3":tank_pump_waterL2L3,
  2322. "tank_pump_waterL4L5":tank_pump_waterL4L5,
  2323. "tank_solenoid_waterL2L3":tank_solenoid_waterL2L3,
  2324. "tank_solenoid_waterL4L5":tank_solenoid_waterL4L5,
  2325. "tank_stepping_motor":tank_stepping_motor
  2326. })
  2327. else:
  2328. pass
  2329. @main.route('/loading/F<ftid>', methods=['GET', 'POST'])
  2330. def F_loading(ftid):
  2331. if request.method == 'GET':
  2332. # 致動器_發酵槽_ALL
  2333. tank_actuator = ferment_tank_actuator.query.filter_by(tank_num='F'+ftid).order_by(text('datetime desc')).first()
  2334. tank_vacuum = tank_actuator.vacuum
  2335. tank_threewayvalve_input = tank_actuator.threewayvalve_input
  2336. tank_diskvalve = tank_actuator.diskvalve
  2337. solenoid_tank_pump = tank_actuator.solenoid_tank_pump
  2338. tank_solenoid_disinfect = tank_actuator.solenoid_tank_disinfect
  2339. outer_solenoid_water = tank_actuator.solenoid_outer_water
  2340. tank_solenoid_water_in = tank_actuator.solenoid_tank_water_in
  2341. tank_solenoid_water_out = tank_actuator.solenoid_tank_water_out
  2342. tank_pump_sensor = tank_actuator.pump_sensor
  2343. outer_threewayvalve_float = tank_actuator.threewayvalve_outer_float
  2344. tank_motor = tank_actuator.motor
  2345. tank_heater1 = tank_actuator.heater1
  2346. tank_heater2 = tank_actuator.heater2
  2347. tank_temp_enable = tank_actuator.temp_enable
  2348. tank_temp = tank_actuator.temp
  2349. # 感測器_發酵桶_SHT11
  2350. tank_SHT11 = ferment_tank_SHT11.query.filter_by(tank_num='F'+ftid).order_by(text('datetime desc')).first()
  2351. SHT11_Temp = float(tank_SHT11.SHT11_Temp)
  2352. SHT11_Humidity = float(tank_SHT11.SHT11_Humidity)
  2353. # 感測器_發酵桶_二氧化碳
  2354. tank_CO2 = ferment_tank_CO2.query.filter_by(tank_num='F'+ftid).order_by(text('datetime desc')).first()
  2355. CO2 = float(tank_CO2.CO2)
  2356. # 感測器_發酵桶_酸鹼值
  2357. tank_PH = ferment_tank_PH.query.filter_by(tank_num='F'+ftid).order_by(text('datetime desc')).first()
  2358. tank_ORP = ferment_tank_ORP.query.filter_by(tank_num='F'+ftid).order_by(text('datetime desc')).first()
  2359. tank_DO = ferment_tank_DO.query.filter_by(tank_num='F'+ftid).order_by(text('datetime desc')).first()
  2360. tank_EC = ferment_tank_EC.query.filter_by(tank_num='F'+ftid).order_by(text('datetime desc')).first()
  2361. PH = float(tank_PH.PH)
  2362. ORP = float(tank_ORP.ORP)
  2363. DO = float(tank_DO.DO)
  2364. EC = float(tank_EC.EC)
  2365. # 感測器_發酵桶_氣壓
  2366. tank_PA = ferment_tank_PA.query.filter_by(tank_num='F'+ftid).order_by(text('datetime desc')).first()
  2367. PA = float(tank_PA.PA)
  2368. # 感測器_發酵桶_超音波感測器
  2369. tank_UltraSonic = ferment_tank_UltraSonic.query.filter_by(tank_num='F'+ftid).order_by(text('datetime desc')).first()
  2370. UltraSonic= (float(tank_UltraSonic.UltraSonic))
  2371. # 感測器_發酵桶_保溫夾層水位計
  2372. tank_WaterLevel = ferment_tank_WaterLevel.query.filter_by(tank_num='F'+ftid).order_by(text('datetime desc')).first()
  2373. WaterLevel = tank_WaterLevel.WaterLevel
  2374. # 感測器_發酵桶_咖啡生豆高度
  2375. # tank_LiDAR = ferment_tank_LiDAR.query.filter_by(tank_num='F'+ftid).order_by(text('datetime desc')).first()
  2376. # LiDAR = tank_LiDAR.LiDAR
  2377. # 感測器_發酵桶_水位高度
  2378. # tank_PressureWaterLevel = ferment_tank_PressureWaterLevel.query.filter_by(tank_num='F'+ftid).order_by(text('datetime desc')).first()
  2379. # PressureWaterLevel = '{:.2f}'.format((float(tank_PressureWaterLevel.PressureWaterLevel) -15 -90.278)/9.4214)
  2380. return jsonify({"SHT11_Temp": SHT11_Temp,
  2381. "SHT11_Humidity":SHT11_Humidity,
  2382. "CO2":CO2,
  2383. "PH":PH,
  2384. "ORP":ORP,
  2385. "DO":DO,
  2386. "EC":EC,
  2387. "PA":PA,
  2388. "WaterLevel":WaterLevel,
  2389. "UltraSonic":UltraSonic,
  2390. "tank_vacuum":tank_vacuum,
  2391. "tank_threewayvalve_input":tank_threewayvalve_input,
  2392. "tank_diskvalve":tank_diskvalve,
  2393. "solenoid_tank_pump":solenoid_tank_pump,
  2394. "tank_solenoid_disinfect":tank_solenoid_disinfect,
  2395. "outer_solenoid_water":outer_solenoid_water,
  2396. "tank_solenoid_water_in":tank_solenoid_water_in,
  2397. "tank_solenoid_water_out":tank_solenoid_water_out,
  2398. "tank_pump_sensor":tank_pump_sensor,
  2399. "outer_threewayvalve_float":outer_threewayvalve_float,
  2400. "tank_motor":tank_motor,
  2401. "tank_heater1":tank_heater1,
  2402. "tank_heater2":tank_heater2,
  2403. "tank_temp_enable":tank_temp_enable,
  2404. "tank_temp":tank_temp
  2405. })
  2406. else:
  2407. pass
  2408. @main.route('/loading/FI<fitid>', methods=['GET', 'POST'])
  2409. def FI_loading(fitid):
  2410. if request.method == 'GET':
  2411. input_actuator = ferment_input_actuator.query.filter_by(tank_num='F'+fitid).order_by(text('datetime desc')).first()
  2412. input_vacuum = input_actuator.vacuum
  2413. input_sensor = ferment_input_UltraSonic.query.filter_by(tank_num='F'+fitid).order_by(text('datetime desc')).first()
  2414. # input_UltraSonic = float("{:.2f}".format(34.6 - float(input_sensor.UltraSonic)))
  2415. input_UltraSonic = float(input_sensor.UltraSonic)
  2416. return jsonify({"input_UltraSonic":input_UltraSonic,
  2417. "input_vacuum":input_vacuum
  2418. })
  2419. else:
  2420. pass
  2421. @main.route('/loading/FO<fotid>', methods=['GET', 'POST'])
  2422. def FO_loading(fotid):
  2423. if request.method == 'GET':
  2424. output_actuator = ferment_output_actuator.query.filter_by(tank_num='FO'+fotid).order_by(text('datetime desc')).first()
  2425. output_vacuum = output_actuator.vacuum
  2426. output_sensor = ferment_output_UltraSonic.query.filter_by(tank_num='FO'+fotid).order_by(text('datetime desc')).first()
  2427. output_UltraSonic = output_sensor.UltraSonic
  2428. tank_LiDAR = ferment_tank_LiDAR.query.filter_by(tank_num='F'+fotid).order_by(text('datetime desc')).first()
  2429. LiDAR = tank_LiDAR.LiDAR
  2430. return jsonify({"output_UltraSonic":output_UltraSonic,
  2431. "output_vacuum":output_vacuum,
  2432. "LiDAR":LiDAR
  2433. })
  2434. else:
  2435. pass
  2436. @main.route('/loading/DI<ditid>', methods=['GET', 'POST'])
  2437. def DI_loading(ditid):
  2438. if request.method == 'GET':
  2439. input_actuator = dry_input_brake.query.filter_by(tank_num='DI'+ditid).order_by(text('datetime desc')).first()
  2440. input_vacuum = input_actuator.vacuum
  2441. input_sensor = dry_input_sensor.query.filter_by(tank_num='DI'+ditid).order_by(text('datetime desc')).first()
  2442. input_UltraSonic = input_sensor.UltraSonic
  2443. return jsonify({"input_UltraSonic":input_UltraSonic,
  2444. "input_vacuum":input_vacuum
  2445. })
  2446. else:
  2447. pass
  2448. @main.route('/loading/DO<dotid>', methods=['GET', 'POST'])
  2449. def DO_loading(dotid):
  2450. if request.method == 'GET':
  2451. output_actuator = dry_output_brake.query.filter_by(tank_num='DO'+dotid).order_by(text('datetime desc')).first()
  2452. output_vacuum = output_actuator.vacuum
  2453. output_sensor = dry_output_sensor.query.filter_by(tank_num='DO'+dotid).order_by(text('datetime desc')).first()
  2454. output_UltraSonic = output_sensor.UltraSonic
  2455. return jsonify({"output_vacuum":output_vacuum,
  2456. "output_UltraSonic":output_UltraSonic
  2457. })
  2458. else:
  2459. pass
  2460. @main.route('/loading/D<dtid>', methods=['GET', 'POST'])
  2461. def D_loading(dtid):
  2462. if request.method == 'GET':
  2463. # 感測器_乾燥桶_超音波感測器
  2464. tank_UltraSonic = dry_tank_UltraSonic.query.filter_by(tank_num="D"+dtid).order_by(text('datetime desc')).first()
  2465. UltraSonic = tank_UltraSonic.UltraSonic
  2466. # 感測器_乾燥桶_SHT11
  2467. tank_SHT11 = dry_tank_SHT11.query.filter_by(tank_num="D"+dtid).order_by(text('datetime desc')).first()
  2468. SHT11_Temp = tank_SHT11.SHT11_Temp
  2469. SHT11_Humidity = tank_SHT11.SHT11_Humidity
  2470. # 感測器_乾燥桶_土壤三合一
  2471. tank_Soil = dry_tank_Soil.query.filter_by(tank_num="D"+dtid).order_by(text('datetime desc')).first()
  2472. soil_Temp = tank_Soil.soil_Temp
  2473. soil_Humidity = tank_Soil.soil_Humidity
  2474. soil_EC = tank_Soil.soil_EC
  2475. # 感測器_乾燥桶_氣壓
  2476. tank_PA = dry_tank_PA.query.filter_by(tank_num="D"+dtid).order_by(text('datetime desc')).first()
  2477. PA = tank_PA.PA
  2478. # 致動器_乾燥桶_ALL
  2479. tank_brake = dry_tank_brake.query.filter_by(tank_num="D"+dtid).order_by(text('datetime desc')).first()
  2480. # tank_vacuum = 'ON' if i<100 else 2 if i>100 else 0
  2481. tank_vacuum = tank_brake.vacuum
  2482. tank_threewayvalve = tank_brake.threewayvalve
  2483. tank_diskvalve = tank_brake.diskvalve
  2484. tank_solenoid_disinfect = tank_brake.solenoid_disinfect
  2485. tank_solenoid_water = tank_brake.solenoid_water
  2486. tank_motor = tank_brake.motor
  2487. tank_blower = tank_brake.blower
  2488. tank_heater1 = tank_brake.heater1
  2489. tank_heater2 = tank_brake.heater2
  2490. tank_temp1_enable = tank_brake.temp1_enable
  2491. tank_temp1 = tank_brake.temp1
  2492. #return jsonify({"peeling": peeling_rpm})
  2493. '''
  2494. info = psutil.virtual_memory()
  2495. print('記憶體使用: ', psutil.Process(os.getpid()).memory_info().rss)
  2496. # print('總記憶體: ', info.total)
  2497. print('記憶體占比: ', info.percent)
  2498. print('cpu 個數: ', psutil.cpu_count())
  2499. '''
  2500. return jsonify({"UltraSonic":UltraSonic,
  2501. "SHT11_Temp": SHT11_Temp,
  2502. "SHT11_Humidity":SHT11_Humidity,
  2503. "soil_Temp":soil_Temp,
  2504. "soil_Humidity":soil_Humidity,
  2505. "soil_EC":soil_EC,
  2506. "PA":PA,
  2507. "tank_vacuum":tank_vacuum,
  2508. "tank_threewayvalve":tank_threewayvalve,
  2509. "tank_diskvalve":tank_diskvalve,
  2510. "tank_solenoid_disinfect":tank_solenoid_disinfect,
  2511. "tank_solenoid_water":tank_solenoid_water,
  2512. "tank_motor":tank_motor,
  2513. "tank_blower":tank_blower,
  2514. "tank_heater1":tank_heater1,
  2515. "tank_heater2":tank_heater2,
  2516. "tank_temp1_enable":tank_temp1_enable,
  2517. "tank_temp1":tank_temp1
  2518. })
  2519. else:
  2520. pass
  2521. @main.route('/index_new')
  2522. def index_new():
  2523. if 'id' in session and 'uname' in session and 'status' in session:
  2524. username = session['uname']
  2525. status = session['status']
  2526. if status == 9:
  2527. return render_template('signin_disable.html', **locals())
  2528. elif status == 8:
  2529. return render_template('signin_new.html', **locals())
  2530. else:
  2531. return render_template('oops.html', **locals())
  2532. return render_template('index_new.html', title='Smart Coffee', **locals())
  2533. @main.route('/user/<name>')
  2534. def user(name):
  2535. return render_template('hello.html', name=name)
  2536. '''
  2537. @main.route('/plot.png')
  2538. def plot():
  2539. # 重新整理會有問題
  2540. # 圖的寬和高
  2541. fig = plt.figure()
  2542. # 在圖中新增子圖, 通常多個圖才會用到?
  2543. axis = fig.add_subplot(1, 1, 1)
  2544. # x、 軸資料
  2545. xs = range(100)
  2546. ys = [random.randint(1, 50) for x in xs]
  2547. axis.plot(xs, ys)
  2548. canvas = FigureCanvas(fig)
  2549. output = io.BytesIO()
  2550. canvas.print_png(output)
  2551. response = make_response(output.getvalue())
  2552. response.mimetype = 'image/png'
  2553. return response
  2554. '''
  2555. @main.route('/chart_<tank>/<sensor_name>/<tid>', methods=['GET', 'POST'])
  2556. def chart_D(tank, sensor_name, tid):
  2557. if request.method == 'GET':
  2558. if 'id' in session and 'uname' in session and 'status' in session:
  2559. username = session['uname']
  2560. status = session['status']
  2561. if status == 9:
  2562. return render_template('signin_disable.html', params=locals())
  2563. elif status == 8:
  2564. return render_template('signin_new.html', params=locals())
  2565. else:
  2566. return render_template('oops.html', params=locals())
  2567. if tank == 'DI':
  2568. return render_template('sensor_chart_DI.html', title='[圖表]' + tank + tid + '乾燥入料儲豆槽', **locals())
  2569. elif tank == 'DO':
  2570. return render_template('sensor_chart_DO.html', title='[圖表]' + tank + tid + '乾燥出料儲豆槽', **locals())
  2571. elif tank == 'FI':
  2572. return render_template('sensor_chart_FI.html', title='[圖表]' + tank + tid + '發酵入料儲豆槽', **locals())
  2573. elif tank == 'FO':
  2574. return render_template('sensor_chart_FO.html', title='[圖表]' + tank + tid + '發酵出料儲豆槽', **locals())
  2575. elif tank == 'F':
  2576. return render_template('sensor_chart_F.html', title='[圖表]' + tank + tid + '發酵槽', **locals())
  2577. else:
  2578. return render_template('sensor_chart_D.html', title='[圖表]' + tank + tid + '乾燥槽', **locals())
  2579. elif request.method == 'POST':
  2580. if 'id' in session and 'uname' in session and 'status' in session:
  2581. username = session['uname']
  2582. status = session['status']
  2583. if status == 9:
  2584. return render_template('signin_disable.html', **locals())
  2585. elif status == 8:
  2586. return render_template('signin_new.html', **locals())
  2587. else:
  2588. return render_template('oops.html', **locals())
  2589. starttime = request.values['sensor_starttime'] # 2021-07-19T09:00
  2590. endtime = request.values['sensor_endtime'] # 2021-07-19T12:00
  2591. sensor_name = request.values['sensors']
  2592. print('sensor_name:', sensor_name)
  2593. # 判斷資料索取區間 起<迄
  2594. if starttime < endtime :
  2595. # print('starttime endtime:', starttime, endtime)
  2596. # print('TF:', starttime<endtime ) # 可以比較 起<迄
  2597. # [html]2021-07-19T09:00 轉換成 [python]datetime.datetime(2021, 7, 19, 9, 0, 0)
  2598. start_YY = starttime[:4] # '2021'
  2599. start_MM = starttime[6:7] if starttime[5:7][:1] == '0' else starttime[5:7] # '07'
  2600. start_DD = starttime[9:10] if starttime[8:10][:1] == '0' else starttime[8:10] # '19'
  2601. start_hh = starttime[12:13] if starttime[11:13][:1] == '0' else starttime[11:13] # '09'
  2602. start_mm = starttime[15:] if starttime[14:][:1] == '0' else starttime[14:] # '00'
  2603. start_ss = 0
  2604. end_YY = endtime[:4] # '2021'
  2605. end_MM = endtime[6:7] if endtime[5:7][:1] == '0' else endtime[5:7] # '07'
  2606. end_DD = endtime[9:10] if endtime[8:10][:1] == '0' else endtime[8:10] # '19'
  2607. end_hh = endtime[12:13] if endtime[11:13][:1] == '0' else endtime[11:13] # '09'
  2608. end_mm = endtime[15:] if endtime[14:][:1] == '0' else endtime[14:] # '00'
  2609. end_ss = 0
  2610. time_range = []
  2611. temp_range = []
  2612. # 拿掉會 Error, 要使用沒有 GUI 的 matplotlib
  2613. plt.switch_backend('agg')
  2614. plt.figure()
  2615. if tank == 'DI':
  2616. print('DI')
  2617. if sensor_name == 'UltraSonic':
  2618. for d in db.session.query(dry_input_sensor) \
  2619. .filter_by(tank_num='DI' + tid) \
  2620. .filter(dt(int(start_YY), int(start_MM), int(start_DD), int(start_hh), int(start_mm), int(start_ss)) < dry_input_sensor.datetime) \
  2621. .filter(dry_input_sensor.datetime < dt(int(end_YY), int(end_MM), int(end_DD), int(end_hh), int(end_mm), int(end_ss))) \
  2622. .order_by(dry_input_sensor.datetime):
  2623. # print('d_test', f'{d.datetime} - {d.SHT11_Temp}')
  2624. time_range.append(str(d.datetime))
  2625. temp_range.append(float(d.UltraSonic))
  2626. print('time_range:', time_range)
  2627. print('temp_range:', temp_range)
  2628. return render_template('sensor_chart_DI.html', **locals())
  2629. elif tank == 'DO':
  2630. print('DO')
  2631. if sensor_name == 'UltraSonic':
  2632. for d in db.session.query(dry_output_sensor) \
  2633. .filter_by(tank_num='DO' + tid) \
  2634. .filter(dt(int(start_YY), int(start_MM), int(start_DD), int(start_hh), int(start_mm), int(start_ss)) < dry_output_sensor.datetime) \
  2635. .filter(dry_output_sensor.datetime < dt(int(end_YY), int(end_MM), int(end_DD), int(end_hh), int(end_mm), int(end_ss))) \
  2636. .order_by(dry_output_sensor.datetime):
  2637. # print('d_test', f'{d.datetime} - {d.SHT11_Temp}')
  2638. time_range.append(str(d.datetime))
  2639. temp_range.append(float(d.UltraSonic))
  2640. print('time_range:', time_range)
  2641. print('temp_range:', temp_range)
  2642. return render_template('sensor_chart_DO.html', **locals())
  2643. elif tank == 'D':
  2644. print('D')
  2645. if sensor_name == 'SHT11_Temp':
  2646. for d in db.session.query(dry_tank_SHT11) \
  2647. .filter_by(tank_num='D' + tid) \
  2648. .filter(dt(int(start_YY), int(start_MM), int(start_DD), int(start_hh), int(start_mm), int(start_ss)) < dry_tank_SHT11.datetime) \
  2649. .filter(dry_tank_SHT11.datetime < dt(int(end_YY), int(end_MM), int(end_DD), int(end_hh), int(end_mm), int(end_ss))) \
  2650. .order_by(dry_tank_SHT11.datetime):
  2651. # print('d_test', f'{d.datetime} - {d.SHT11_Temp}')
  2652. time_range.append(str(d.datetime))
  2653. temp_range.append(float(d.SHT11_Temp))
  2654. # 畫出 x 軸基準線
  2655. # plt.axhline(28.5, color= 'r')
  2656. elif sensor_name == 'SHT11_Humidity':
  2657. for d in db.session.query(dry_tank_SHT11) \
  2658. .filter_by(tank_num='D' + tid) \
  2659. .filter(dt(int(start_YY), int(start_MM), int(start_DD), int(start_hh), int(start_mm), int(start_ss)) < dry_tank_SHT11.datetime) \
  2660. .filter(dry_tank_SHT11.datetime < dt(int(end_YY), int(end_MM), int(end_DD), int(end_hh), int(end_mm), int(end_ss))) \
  2661. .order_by(dry_tank_SHT11.datetime):
  2662. # print('d_test', f'{d.datetime} - {d.SHT11_Temp}')
  2663. time_range.append(str(d.datetime))
  2664. temp_range.append(float(d.SHT11_Humidity))
  2665. elif sensor_name == 'UltraSonic':
  2666. for d in db.session.query(dry_tank_UltraSonic) \
  2667. .filter_by(tank_num='D' + tid) \
  2668. .filter(dt(int(start_YY), int(start_MM), int(start_DD), int(start_hh), int(start_mm), int(start_ss)) < dry_tank_UltraSonic.datetime) \
  2669. .filter(dry_tank_UltraSonic.datetime < dt(int(end_YY), int(end_MM), int(end_DD), int(end_hh), int(end_mm), int(end_ss))) \
  2670. .order_by(dry_tank_UltraSonic.datetime):
  2671. # print('d_test', f'{d.datetime} - {d.SHT11_Temp}')
  2672. time_range.append(str(d.datetime))
  2673. temp_range.append(float(d.UltraSonic))
  2674. elif sensor_name == 'PA':
  2675. for d in db.session.query(dry_tank_PA) \
  2676. .filter_by(tank_num='D' + tid) \
  2677. .filter(dt(int(start_YY), int(start_MM), int(start_DD), int(start_hh), int(start_mm), int(start_ss)) < dry_tank_PA.datetime) \
  2678. .filter(dry_tank_PA.datetime < dt(int(end_YY), int(end_MM), int(end_DD), int(end_hh), int(end_mm), int(end_ss))) \
  2679. .order_by(dry_tank_PA.datetime):
  2680. # print('d_test', f'{d.datetime} - {d.SHT11_Temp}')
  2681. time_range.append(str(d.datetime))
  2682. temp_range.append(float(d.PA))
  2683. elif sensor_name == 'soil_Temp':
  2684. for d in db.session.query(dry_tank_Soil) \
  2685. .filter_by(tank_num='D' + tid) \
  2686. .filter(dt(int(start_YY), int(start_MM), int(start_DD), int(start_hh), int(start_mm), int(start_ss)) < dry_tank_Soil.datetime) \
  2687. .filter(dry_tank_Soil.datetime < dt(int(end_YY), int(end_MM), int(end_DD), int(end_hh), int(end_mm), int(end_ss))) \
  2688. .order_by(dry_tank_Soil.datetime):
  2689. # print('d_test', f'{d.datetime} - {d.SHT11_Temp}')
  2690. time_range.append(str(d.datetime))
  2691. temp_range.append(float(d.soil_Temp))
  2692. # 畫出 x 軸基準線
  2693. plt.axhline(28.5, color= 'r')
  2694. elif sensor_name == 'soil_Humidity':
  2695. for d in db.session.query(dry_tank_Soil) \
  2696. .filter_by(tank_num='D' + tid) \
  2697. .filter(dt(int(start_YY), int(start_MM), int(start_DD), int(start_hh), int(start_mm), int(start_ss)) < dry_tank_Soil.datetime) \
  2698. .filter(dry_tank_Soil.datetime < dt(int(end_YY), int(end_MM), int(end_DD), int(end_hh), int(end_mm), int(end_ss))) \
  2699. .order_by(dry_tank_Soil.datetime):
  2700. # print('d_test', f'{d.datetime} - {d.SHT11_Temp}')
  2701. time_range.append(str(d.datetime))
  2702. temp_range.append(float(d.soil_Humidity))
  2703. elif sensor_name == 'soil_EC':
  2704. for d in db.session.query(dry_tank_Soil) \
  2705. .filter_by(tank_num='D' + tid) \
  2706. .filter(dt(int(start_YY), int(start_MM), int(start_DD), int(start_hh), int(start_mm), int(start_ss)) < dry_tank_Soil.datetime) \
  2707. .filter(dry_tank_Soil.datetime < dt(int(end_YY), int(end_MM), int(end_DD), int(end_hh), int(end_mm), int(end_ss))) \
  2708. .order_by(dry_tank_Soil.datetime):
  2709. # print('d_test', f'{d.datetime} - {d.SHT11_Temp}')
  2710. time_range.append(str(d.datetime))
  2711. temp_range.append(float(d.soil_EC))
  2712. print('time_range:', time_range)
  2713. print('temp_range:', temp_range)
  2714. return render_template('sensor_chart_D.html', **locals())
  2715. elif tank == 'FI':
  2716. print('FI')
  2717. if sensor_name == 'UltraSonic':
  2718. for d in db.session.query(ferment_input_UltraSonic) \
  2719. .filter_by(tank_num='FI' + tid) \
  2720. .filter(dt(int(start_YY), int(start_MM), int(start_DD), int(start_hh), int(start_mm), int(start_ss)) < ferment_input_UltraSonic.datetime) \
  2721. .filter(ferment_input_UltraSonic.datetime < dt(int(end_YY), int(end_MM), int(end_DD), int(end_hh), int(end_mm), int(end_ss))) \
  2722. .order_by(ferment_input_UltraSonic.datetime):
  2723. # print('d_test', f'{d.datetime} - {d.SHT11_Temp}')
  2724. time_range.append(str(d.datetime))
  2725. temp_range.append(float(d.UltraSonic))
  2726. print('time_range:', time_range)
  2727. print('temp_range:', temp_range)
  2728. return render_template('sensor_chart_FI.html', **locals())
  2729. elif tank == 'FO':
  2730. print('FO')
  2731. if sensor_name == 'UltraSonic':
  2732. for d in db.session.query(ferment_output_UltraSonic) \
  2733. .filter_by(tank_num='FO' + tid) \
  2734. .filter(dt(int(start_YY), int(start_MM), int(start_DD), int(start_hh), int(start_mm), int(start_ss)) < ferment_output_UltraSonic.datetime) \
  2735. .filter(ferment_output_UltraSonic.datetime < dt(int(end_YY), int(end_MM), int(end_DD), int(end_hh), int(end_mm), int(end_ss))) \
  2736. .order_by(ferment_output_UltraSonic.datetime):
  2737. # print('d_test', f'{d.datetime} - {d.SHT11_Temp}')
  2738. time_range.append(str(d.datetime))
  2739. temp_range.append(float(d.UltraSonic))
  2740. print('time_range:', time_range)
  2741. print('temp_range:', temp_range)
  2742. return render_template('sensor_chart_FO.html', **locals())
  2743. elif tank == 'F':
  2744. print('F')
  2745. if sensor_name == 'LiDAR':
  2746. for d in db.session.query(ferment_tank_LiDAR) \
  2747. .filter_by(tank_num='F' + tid) \
  2748. .filter(dt(int(start_YY), int(start_MM), int(start_DD), int(start_hh), int(start_mm), int(start_ss)) < ferment_tank_LiDAR.datetime) \
  2749. .filter(ferment_tank_LiDAR.datetime < dt(int(end_YY), int(end_MM), int(end_DD), int(end_hh), int(end_mm), int(end_ss))) \
  2750. .order_by(ferment_tank_LiDAR.datetime):
  2751. time_range.append(str(d.datetime))
  2752. temp_range.append(float(d.LiDAR))
  2753. elif sensor_name == 'PressureWaterLevel':
  2754. for d in db.session.query(ferment_tank_PressureWaterLevel) \
  2755. .filter_by(tank_num='F' + tid) \
  2756. .filter(dt(int(start_YY), int(start_MM), int(start_DD), int(start_hh), int(start_mm), int(start_ss)) < ferment_tank_PressureWaterLevel.datetime) \
  2757. .filter(ferment_tank_PressureWaterLevel.datetime < dt(int(end_YY), int(end_MM), int(end_DD), int(end_hh), int(end_mm), int(end_ss))) \
  2758. .order_by(ferment_tank_PressureWaterLevel.datetime):
  2759. time_range.append(str(d.datetime))
  2760. temp_range.append('{:.2f}'.format((float(d.PressureWaterLevel) -10 -90.278)/9.4214))
  2761. elif sensor_name == 'SHT11_Temp':
  2762. for d in db.session.query(ferment_tank_SHT11) \
  2763. .filter_by(tank_num='F' + tid) \
  2764. .filter(dt(int(start_YY), int(start_MM), int(start_DD), int(start_hh), int(start_mm), int(start_ss)) < ferment_tank_PressureWaterLevel.datetime) \
  2765. .filter(ferment_tank_SHT11.datetime < dt(int(end_YY), int(end_MM), int(end_DD), int(end_hh), int(end_mm), int(end_ss))) \
  2766. .order_by(ferment_tank_SHT11.datetime):
  2767. time_range.append(str(d.datetime))
  2768. temp_range.append(float(d.SHT11_Temp))
  2769. elif sensor_name == 'SHT11_Humidity':
  2770. for d in db.session.query(ferment_tank_SHT11) \
  2771. .filter_by(tank_num='F' + tid) \
  2772. .filter(dt(int(start_YY), int(start_MM), int(start_DD), int(start_hh), int(start_mm), int(start_ss)) < ferment_tank_SHT11.datetime) \
  2773. .filter(ferment_tank_SHT11.datetime < dt(int(end_YY), int(end_MM), int(end_DD), int(end_hh), int(end_mm), int(end_ss))) \
  2774. .order_by(ferment_tank_SHT11.datetime):
  2775. time_range.append(str(d.datetime))
  2776. temp_range.append(float(d.SHT11_Humidity))
  2777. elif sensor_name == 'CO2':
  2778. for d in db.session.query(ferment_tank_CO2) \
  2779. .filter_by(tank_num='F' + tid) \
  2780. .filter(dt(int(start_YY), int(start_MM), int(start_DD), int(start_hh), int(start_mm), int(start_ss)) < ferment_tank_CO2.datetime) \
  2781. .filter(ferment_tank_CO2.datetime < dt(int(end_YY), int(end_MM), int(end_DD), int(end_hh), int(end_mm), int(end_ss))) \
  2782. .order_by(ferment_tank_CO2.datetime):
  2783. time_range.append(str(d.datetime))
  2784. temp_range.append(float(d.CO2))
  2785. elif sensor_name == 'PH':
  2786. for d in db.session.query(ferment_tank_PH) \
  2787. .filter_by(tank_num='F' + tid) \
  2788. .filter(dt(int(start_YY), int(start_MM), int(start_DD), int(start_hh), int(start_mm), int(start_ss)) < ferment_tank_PH.datetime) \
  2789. .filter(ferment_tank_PH.datetime < dt(int(end_YY), int(end_MM), int(end_DD), int(end_hh), int(end_mm), int(end_ss))) \
  2790. .order_by(ferment_tank_PH.datetime):
  2791. time_range.append(str(d.datetime))
  2792. temp_range.append(float(d.PH))
  2793. elif sensor_name == 'ORP':
  2794. for d in db.session.query(ferment_tank_ORP) \
  2795. .filter_by(tank_num='F' + tid) \
  2796. .filter(dt(int(start_YY), int(start_MM), int(start_DD), int(start_hh), int(start_mm), int(start_ss)) < ferment_tank_ORP.datetime) \
  2797. .filter(ferment_tank_ORP.datetime < dt(int(end_YY), int(end_MM), int(end_DD), int(end_hh), int(end_mm), int(end_ss))) \
  2798. .order_by(ferment_tank_ORP.datetime):
  2799. time_range.append(str(d.datetime))
  2800. temp_range.append(float(d.ORP))
  2801. elif sensor_name == 'DO':
  2802. for d in db.session.query(ferment_tank_DO) \
  2803. .filter_by(tank_num='F' + tid) \
  2804. .filter(dt(int(start_YY), int(start_MM), int(start_DD), int(start_hh), int(start_mm), int(start_ss)) < ferment_tank_DO.datetime) \
  2805. .filter(ferment_tank_DO.datetime < dt(int(end_YY), int(end_MM), int(end_DD), int(end_hh), int(end_mm), int(end_ss))) \
  2806. .order_by(ferment_tank_DO.datetime):
  2807. time_range.append(str(d.datetime))
  2808. temp_range.append(float(d.DO))
  2809. elif sensor_name == 'EC':
  2810. for d in db.session.query(ferment_tank_EC) \
  2811. .filter_by(tank_num='F' + tid) \
  2812. .filter(dt(int(start_YY), int(start_MM), int(start_DD), int(start_hh), int(start_mm), int(start_ss)) < ferment_tank_EC.datetime) \
  2813. .filter(ferment_tank_EC.datetime < dt(int(end_YY), int(end_MM), int(end_DD), int(end_hh), int(end_mm), int(end_ss))) \
  2814. .order_by(ferment_tank_EC.datetime):
  2815. time_range.append(str(d.datetime))
  2816. temp_range.append(float(d.EC))
  2817. elif sensor_name == 'PA':
  2818. for d in db.session.query(ferment_tank_PA) \
  2819. .filter_by(tank_num='F' + tid) \
  2820. .filter(dt(int(start_YY), int(start_MM), int(start_DD), int(start_hh), int(start_mm), int(start_ss)) < ferment_tank_PA.datetime) \
  2821. .filter(ferment_tank_PA.datetime < dt(int(end_YY), int(end_MM), int(end_DD), int(end_hh), int(end_mm), int(end_ss))) \
  2822. .order_by(ferment_tank_PA.datetime):
  2823. time_range.append(str(d.datetime))
  2824. temp_range.append(float(d.PA))
  2825. print('time_range:', time_range)
  2826. print('temp_range:', temp_range)
  2827. return render_template('sensor_chart_DO.html', **locals())
  2828. '''
  2829. # 設定 x 軸資料、y 軸資料、折線顏色
  2830. plt.plot(time_range, temp_range, 'bo')
  2831. # x 軸標題 (中文無法顯示)
  2832. # plt.xlabel('time')
  2833. # plt.ylabel(sensor_name)
  2834. # x 軸 label 和垂直顯示, rotation='vertical'
  2835. plt.xticks(time_range, rotation='vertical' )
  2836. # 設定圖表標題
  2837. plt.title(sensor_name)
  2838. # 調整圖片與周圍的間距
  2839. plt.tight_layout()
  2840. # plt.show()
  2841. # 儲存圖片 應改為相對路徑
  2842. plt.savefig('C:/Users/USER/Rita/Coffee/CoffeeProject/app/static/img/new_plot.png')
  2843. '''
  2844. # 判斷資料索取區間 起>迄
  2845. else:
  2846. return render_template('sensor_chart_D.html', **locals())
  2847. '''
  2848. @main.route('/chart_DI/<sensor_name>/<tid>', methods=['GET', 'POST'])
  2849. def chart_DI(sensor_name, tid):
  2850. if request.method == 'GET':
  2851. return render_template('sensor_chart_DI.html', **locals())
  2852. elif request.method == 'POST':
  2853. starttime = request.values['sensor_starttime'] # 2021-07-19T09:00
  2854. endtime = request.values['sensor_endtime'] # 2021-07-19T12:00
  2855. sensor_name = request.values['sensors']
  2856. print('sensor_name:', sensor_name)
  2857. # 判斷資料索取區間 起<迄
  2858. if starttime < endtime :
  2859. start_YY = starttime[:4] # '2021'
  2860. start_MM = starttime[6:7] if starttime[5:7][:1] == '0' else starttime[5:7] # '07'
  2861. start_DD = starttime[9:10] if starttime[8:10][:1] == '0' else starttime[8:10] # '19'
  2862. start_hh = starttime[12:13] if starttime[11:13][:1] == '0' else starttime[11:13] # '09'
  2863. start_mm = starttime[15:] if starttime[14:][:1] == '0' else starttime[14:] # '00'
  2864. start_ss = 0
  2865. end_YY = endtime[:4] # '2021'
  2866. end_MM = endtime[6:7] if endtime[5:7][:1] == '0' else endtime[5:7] # '07'
  2867. end_DD = endtime[9:10] if endtime[8:10][:1] == '0' else endtime[8:10] # '19'
  2868. end_hh = endtime[12:13] if endtime[11:13][:1] == '0' else endtime[11:13] # '09'
  2869. end_mm = endtime[15:] if endtime[14:][:1] == '0' else endtime[14:] # '00'
  2870. end_ss = 0
  2871. time_range = []
  2872. temp_range = []
  2873. # 拿掉會 Error, 要使用沒有 GUI 的 matplotlib
  2874. plt.switch_backend('agg')
  2875. plt.figure()
  2876. if sensor_name == 'UltraSonic':
  2877. for d in db.session.query(dry_input_sensor) \
  2878. .filter_by(tank_num='DI' + tid) \
  2879. .filter(dt(int(start_YY), int(start_MM), int(start_DD), int(start_hh), int(start_mm), int(start_ss)) < dry_tank_UltraSonic.datetime) \
  2880. .filter(dry_tank_UltraSonic.datetime < dt(int(end_YY), int(end_MM), int(end_DD), int(end_hh), int(end_mm), int(end_ss))) \
  2881. .order_by(dry_tank_UltraSonic.datetime):
  2882. # print('d_test', f'{d.datetime} - {d.SHT11_Temp}')
  2883. time_range.append(str(d.datetime))
  2884. temp_range.append(float(d.UltraSonic))
  2885. # 畫出 x 軸基準線
  2886. # plt.axhline(28.5, color= 'r')
  2887. print('time_range:', time_range)
  2888. print('temp_range:', temp_range)
  2889. return render_template('sensor_chart_DI.html', **locals())
  2890. # 判斷資料索取區間 起>迄
  2891. else:
  2892. return render_template('sensor_chart_DI.html', **locals())
  2893. @main.route('/chart_DO/<sensor_name>/<tid>', methods=['GET', 'POST'])
  2894. def chart_DO(sensor_name, tid):
  2895. if request.method == 'GET':
  2896. return render_template('sensor_chart_DO.html', **locals())
  2897. elif request.method == 'POST':
  2898. starttime = request.values['sensor_starttime'] # 2021-07-19T09:00
  2899. endtime = request.values['sensor_endtime'] # 2021-07-19T12:00
  2900. sensor_name = request.values['sensors']
  2901. print('sensor_name:', sensor_name)
  2902. # 判斷資料索取區間 起<迄
  2903. if starttime < endtime :
  2904. start_YY = starttime[:4] # '2021'
  2905. start_MM = starttime[6:7] if starttime[5:7][:1] == '0' else starttime[5:7] # '07'
  2906. start_DD = starttime[9:10] if starttime[8:10][:1] == '0' else starttime[8:10] # '19'
  2907. start_hh = starttime[12:13] if starttime[11:13][:1] == '0' else starttime[11:13] # '09'
  2908. start_mm = starttime[15:] if starttime[14:][:1] == '0' else starttime[14:] # '00'
  2909. start_ss = 0
  2910. end_YY = endtime[:4] # '2021'
  2911. end_MM = endtime[6:7] if endtime[5:7][:1] == '0' else endtime[5:7] # '07'
  2912. end_DD = endtime[9:10] if endtime[8:10][:1] == '0' else endtime[8:10] # '19'
  2913. end_hh = endtime[12:13] if endtime[11:13][:1] == '0' else endtime[11:13] # '09'
  2914. end_mm = endtime[15:] if endtime[14:][:1] == '0' else endtime[14:] # '00'
  2915. end_ss = 0
  2916. time_range = []
  2917. temp_range = []
  2918. # 拿掉會 Error, 要使用沒有 GUI 的 matplotlib
  2919. plt.switch_backend('agg')
  2920. plt.figure()
  2921. if sensor_name == 'UltraSonic':
  2922. for d in db.session.query(dry_input_sensor) \
  2923. .filter_by(tank_num='DO' + tid) \
  2924. .filter(dt(int(start_YY), int(start_MM), int(start_DD), int(start_hh), int(start_mm), int(start_ss)) < dry_tank_UltraSonic.datetime) \
  2925. .filter(dry_tank_UltraSonic.datetime < dt(int(end_YY), int(end_MM), int(end_DD), int(end_hh), int(end_mm), int(end_ss))) \
  2926. .order_by(dry_tank_UltraSonic.datetime):
  2927. # print('d_test', f'{d.datetime} - {d.SHT11_Temp}')
  2928. time_range.append(str(d.datetime))
  2929. temp_range.append(float(d.UltraSonic))
  2930. # 畫出 x 軸基準線
  2931. # plt.axhline(28.5, color= 'r')
  2932. print('time_range:', time_range)
  2933. print('temp_range:', temp_range)
  2934. return render_template('sensor_chart_DO.html', **locals())
  2935. # 判斷資料索取區間 起>迄
  2936. else:
  2937. return render_template('sensor_chart_DO.html', **locals())
  2938. '''
  2939. # Rita 測試
  2940. # 乾燥桶感測器/致動器測試
  2941. # Rita 須加上 <tid> 區分桶號 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  2942. @main.route('/dry_tank/1', methods=['GET'])
  2943. def dry_tank():
  2944. if request.method == 'GET':
  2945. return render_template('dry_tank.html')
  2946. @main.route('/clean', methods=['GET'])
  2947. def clean():
  2948. if 'id' in session and 'uname' in session and 'status' in session:
  2949. username = session['uname']
  2950. status = session['status']
  2951. if status == 9:
  2952. return render_template('signin_disable.html', params=locals())
  2953. elif status == 8:
  2954. return render_template('signin_new.html', params=locals())
  2955. else:
  2956. return render_template('oops.html', params=locals())
  2957. return render_template('clean.html', title='清洗貨櫃', **locals())
  2958. @main.route('/ferment', methods=['GET'])
  2959. def ferment():
  2960. if 'id' in session and 'uname' in session and 'status' in session:
  2961. username = session['uname']
  2962. status = session['status']
  2963. if status == 9:
  2964. return render_template('signin_disable.html', params=locals())
  2965. elif status == 8:
  2966. return render_template('signin_new.html', params=locals())
  2967. else:
  2968. return render_template('oops.html', params=locals())
  2969. return render_template('ferment.html', title='發酵貨櫃', **locals())
  2970. @main.route('/dry', methods=['GET'])
  2971. def dry():
  2972. if request.method == 'GET':
  2973. if 'id' in session and 'uname' in session and 'status' in session:
  2974. username = session['uname']
  2975. status = session['status']
  2976. if status == 9:
  2977. return render_template('signin_disable.html', params=locals())
  2978. elif status == 8:
  2979. return render_template('signin_new.html', params=locals())
  2980. else:
  2981. return render_template('oops.html', params=locals())
  2982. return render_template('dry.html', title='乾燥貨櫃', **locals())
  2983. @main.route('/peel_container_tank/<tid>', methods=['GET', 'POST'])
  2984. def peel_container_tnak(tid):
  2985. if request.method == 'GET':
  2986. if 'id' in session and 'uname' in session and 'status' in session:
  2987. username = session['uname']
  2988. status = session['status']
  2989. if status == 9:
  2990. return render_template('signin_disable.html', params=locals())
  2991. elif status == 8:
  2992. return render_template('signin_new.html', params=locals())
  2993. else:
  2994. return render_template('oops.html', params=locals())
  2995. # 致動器_脫皮機_ALL
  2996. tank_actuator = peel_tank_actuator.query.filter_by(tank_num='P'+str(tid)).order_by(text('datetime desc')).first()
  2997. tank_vacuum = tank_actuator.vacuum
  2998. tank_motor = tank_actuator.motor
  2999. return render_template('peel_container_tank.html', title='[操作] P' + tid + ' 脫皮機操作介面', **locals())
  3000. elif request.method == 'POST':
  3001. pass
  3002. @main.route('/peel_container/<tid>', methods=['GET', 'POST'])
  3003. def peel_container(tid):
  3004. if request.method == 'GET':
  3005. if 'id' in session and 'uname' in session and 'status' in session:
  3006. username = session['uname']
  3007. status = session['status']
  3008. if status == 9:
  3009. return render_template('signin_disable.html', params=locals())
  3010. elif status == 8:
  3011. return render_template('signin_new.html', params=locals())
  3012. else:
  3013. return render_template('oops.html', params=locals())
  3014. # 致動器_入料儲豆槽_真空吸料機
  3015. input_vacuum = peel_input_actuator.query.filter_by(tank_num='P'+str(tid)).order_by(text('datetime desc')).first()
  3016. input_vacuum = input_vacuum.vacuum
  3017. print("input_vacuum:", input_vacuum)
  3018. # 致動器_脫皮機_ALL
  3019. tank_actuator = peel_tank_actuator.query.filter_by(tank_num='P'+str(tid)).order_by(text('datetime desc')).first()
  3020. tank_vacuum = tank_actuator.vacuum
  3021. tank_motor = tank_actuator.motor
  3022. # 致動器_出料儲豆槽_真空吸料機
  3023. output_vacuum = peel_output_actuator.query.filter_by(tank_num='P'+str(tid)).order_by(text('datetime desc')).first()
  3024. output_vacuum = output_vacuum.vacuum
  3025. return render_template('peel_container.html', title='脫皮機P' + tid + '總操作介面', **locals())
  3026. elif request.method == 'POST':
  3027. pass
  3028. @main.route('/clean_container/<tid>', methods=['GET', 'POST'])
  3029. def clean_container(tid):
  3030. if request.method == 'GET':
  3031. if 'id' in session and 'uname' in session and 'status' in session:
  3032. username = session['uname']
  3033. status = session['status']
  3034. if status == 9:
  3035. return render_template('signin_disable.html', params=locals())
  3036. elif status == 8:
  3037. return render_template('signin_new.html', params=locals())
  3038. else:
  3039. return render_template('oops.html', params=locals())
  3040. # 致動器_入料儲豆槽_真空吸料機
  3041. input_vacuum = clean_input_actuator.query.filter_by(tank_num='C'+str(tid)).order_by(text('datetime desc')).first()
  3042. input_vacuum = 'ON' if (input_vacuum.vacuum == 1) else 'OFF'
  3043. # 致動器_浮選清洗_ALL
  3044. tank_actuator = clean_tank_actuator.query.filter_by(tank_num='C'+str(tid)).order_by(text('datetime desc')).first()
  3045. tank_vacuum = 'ON' if tank_actuator.vacuum == 1 else 'OFF'
  3046. tank_pump_waterInput = 'ON' if tank_actuator.pump_waterInput == 1 else 'OFF'
  3047. tank_pump_waterL2L3 = 'ON' if tank_actuator.pump_waterL2L3 == 1 else 'OFF'
  3048. tank_pump_waterL4L5 = 'ON' if tank_actuator.pump_waterL4L5 == 1 else 'OFF'
  3049. tank_solenoid_waterL2L3 = 'ON' if tank_actuator.solenoid_waterL2L3 == 1 else 'OFF'
  3050. tank_solenoid_waterL4L5 = 'ON' if tank_actuator.solenoid_waterL4L5 == 1 else 'OFF'
  3051. tank_stepping_motor = tank_actuator.stepping_motor
  3052. # 致動器_出料儲豆槽_真空吸料機
  3053. output_vacuum = clean_output_actuator.query.filter_by(tank_num='C'+str(tid)).order_by(text('datetime desc')).first()
  3054. output_vacuum = 'ON' if (output_vacuum.vacuum == 1) else 'OFF'
  3055. # 感測器_浮選清洗_超音波感測器
  3056. tank_UltraSonic = clean_tank_UltraSonic.query.filter_by(tank_num='C'+str(tid)).order_by(text('datetime desc')).first()
  3057. return render_template('clean_container.html', title='清洗浮選槽C' + tid + '總操作介面', **locals())
  3058. elif request.method == 'POST':
  3059. pass
  3060. @main.route('/ferment_container/<tid>', methods=['GET', 'POST'])
  3061. def ferment_container(tid):
  3062. if request.method == 'GET':
  3063. if 'id' in session and 'uname' in session and 'status' in session:
  3064. username = session['uname']
  3065. status = session['status']
  3066. if status == 9:
  3067. return render_template('signin_disable.html', params=locals())
  3068. elif status == 8:
  3069. return render_template('signin_new.html', params=locals())
  3070. else:
  3071. return render_template('oops.html', params=locals())
  3072. # 致動器_發酵槽_ALL
  3073. tank_actuator = ferment_tank_actuator.query.filter_by(tank_num='F'+str(tid)).order_by(text('datetime desc')).first()
  3074. tank_vacuum = 'ON' if tank_actuator.vacuum == 1 else 'OFF'
  3075. tank_threewayvalve_input = 'ON' if tank_actuator.threewayvalve_input == 1 else 'OFF'
  3076. tank_diskvalve = 'ON' if tank_actuator.diskvalve == 1 else 'OFF'
  3077. tank_solenoid_water_total = 'ON' if tank_actuator.solenoid_tank_water_total == 1 else 'OFF'
  3078. tank_solenoid_disinfect = 'ON' if tank_actuator.solenoid_tank_disinfect == 1 else 'OFF'
  3079. outer_solenoid_water = 'ON' if tank_actuator.solenoid_outer_water == 1 else 'OFF'
  3080. tank_solenoid_water_in = 'ON' if tank_actuator.solenoid_tank_water_in == 1 else 'OFF'
  3081. tank_pump_sensor = 'ON' if tank_actuator.pump_sensor == 1 else 'OFF'
  3082. tank_threewayvalve_bean = 'ON' if tank_actuator.threewayvalve_bean == 1 else 'OFF'
  3083. outer_threewayvalve_float = 'ON' if tank_actuator.threewayvalve_outer_float == 1 else 'OFF'
  3084. tank_motor = tank_actuator.motor
  3085. tank_heater1 = 'ON' if tank_actuator.heater1 == 1 else 'OFF'
  3086. tank_heater2 = 'ON' if tank_actuator.heater2 == 1 else 'OFF'
  3087. tank_temp_enable = 'ON' if tank_actuator.temp_enable == 1 else 'OFF'
  3088. tank_temp = tank_actuator.temp
  3089. # 感測器_發酵桶_SHT11
  3090. tank_SHT11 = ferment_tank_SHT11.query.filter_by(tank_num='F'+str(tid)).order_by(text('datetime desc')).first()
  3091. # 感測器_發酵桶_二氧化碳
  3092. tank_CO2 = ferment_tank_CO2.query.filter_by(tank_num='F'+str(tid)).order_by(text('datetime desc')).first()
  3093. # 感測器_發酵桶_酸鹼值
  3094. tank_PH = ferment_tank_PH.query.filter_by(tank_num='F'+str(tid)).order_by(text('datetime desc')).first()
  3095. tank_ORP = ferment_tank_ORP.query.filter_by(tank_num='F'+str(tid)).order_by(text('datetime desc')).first()
  3096. tank_DO = ferment_tank_DO.query.filter_by(tank_num='F'+str(tid)).order_by(text('datetime desc')).first()
  3097. tank_EC = ferment_tank_EC.query.filter_by(tank_num='F'+str(tid)).order_by(text('datetime desc')).first()
  3098. # 感測器_發酵桶_氣壓
  3099. tank_PA = ferment_tank_PA.query.filter_by(tank_num='F'+str(tid)).order_by(text('datetime desc')).first()
  3100. # 感測器_發酵桶_超音波感測器
  3101. tank_UltraSonic = ferment_tank_UltraSonic.query.filter_by(tank_num='F'+str(tid)).order_by(text('datetime desc')).first()
  3102. # 感測器_發酵桶_水位計
  3103. tank_WaterLevel = ferment_tank_WaterLevel.query.filter_by(tank_num='F'+str(tid)).order_by(text('datetime desc')).first()
  3104. num_tank_WaterLevel = float(tank_WaterLevel.WaterLevel)
  3105. print('tank_WaterLevel', tank_WaterLevel.WaterLevel)
  3106. if tank_WaterLevel.WaterLevel == '1':
  3107. WaterLevel = '滿'
  3108. elif tank_WaterLevel.WaterLevel == '0':
  3109. WaterLevel = '未達滿水高度'
  3110. else:
  3111. WaterLevel = 'ERROR'
  3112. # 感測器_發酵桶_咖啡生豆高度
  3113. tank_LiDAR = ferment_tank_LiDAR.query.filter_by(tank_num='F'+str(tid)).order_by(text('datetime desc')).first()
  3114. LiDAR = tank_LiDAR.LiDAR
  3115. # 感測器_發酵桶_水位高度
  3116. tank_PressureWaterLevel = ferment_tank_PressureWaterLevel.query.filter_by(tank_num='F'+str(tid)).order_by(text('datetime desc')).first()
  3117. PressureWaterLevel = '{:.2f}'.format((float(tank_PressureWaterLevel.PressureWaterLevel) -15 -90.278)/9.4214)
  3118. if tid in ['1', '2', '3', '4', '5', '6']:
  3119. # print("1")
  3120. # 感測器_入料儲豆槽_超音波感測器
  3121. input_UltraSonic = ferment_input_UltraSonic.query.filter_by(tank_num='FI1').order_by(text('datetime desc')).first()
  3122. # 致動器_入料儲豆槽_真空吸引機
  3123. input_vacuum = ferment_input_actuator.query.filter_by(tank_num='FI1').order_by(text('datetime desc')).first()
  3124. input_vacuum = input_vacuum.vacuum
  3125. # 感測器_出料儲豆槽_超音波感測器
  3126. output_UltraSonic = ferment_output_UltraSonic.query.filter_by(tank_num='FO1').order_by(text('datetime desc')).first()
  3127. # 致動器_出料儲豆槽_真空吸引機
  3128. output_vacuum = ferment_output_actuator.query.filter_by(tank_num='FO1').order_by(text('datetime desc')).first()
  3129. output_vacuum = output_vacuum.vacuum
  3130. elif tid in ['7', '8', '9', '10', '11', '12']:
  3131. # print("2")
  3132. # 感測器_入料儲豆槽_超音波感測器
  3133. input_UltraSonic = ferment_input_UltraSonic.query.filter_by(tank_num='FI2').order_by(text('datetime desc')).first()
  3134. # 致動器_入料儲豆槽_真空吸引機
  3135. input_vacuum = ferment_input_actuator.query.filter_by(tank_num='FI2').order_by(text('datetime desc')).first()
  3136. input_vacuum = input_vacuum.vacuum
  3137. print("input_vacuum", input_vacuum)
  3138. # 感測器_出料儲豆槽_超音波感測器
  3139. output_UltraSonic = ferment_output_UltraSonic.query.filter_by(tank_num='FO2').order_by(text('datetime desc')).first()
  3140. # 致動器_出料儲豆槽_真空吸引機
  3141. output_vacuum = ferment_output_actuator.query.filter_by(tank_num='FO2').order_by(text('datetime desc')).first()
  3142. output_vacuum = output_vacuum.vacuum
  3143. print("output_vacuum", output_vacuum)
  3144. return render_template('ferment_container.html', title='發酵槽F' + tid + '總操作介面' , **locals())
  3145. elif request.method == 'POST':
  3146. pass
  3147. @main.route('/ferment_container_tank/<tid>', methods=['GET', 'POST'])
  3148. def ferment_container_tank(tid):
  3149. if request.method == 'GET':
  3150. if 'id' in session and 'uname' in session and 'status' in session:
  3151. username = session['uname']
  3152. status = session['status']
  3153. if status == 9:
  3154. return render_template('signin_disable.html', params=locals())
  3155. elif status == 8:
  3156. return render_template('signin_new.html', params=locals())
  3157. else:
  3158. return render_template('oops.html', params=locals())
  3159. # 致動器_發酵槽_ALL
  3160. tank_actuator = ferment_tank_actuator.query.filter_by(tank_num='F'+str(tid)).order_by(text('datetime desc')).first()
  3161. tank_vacuum = 'ON' if tank_actuator.vacuum == 1 else 'OFF'
  3162. tank_threewayvalve_input = 'ON' if tank_actuator.threewayvalve_input == 1 else 'OFF'
  3163. tank_diskvalve = 'ON' if tank_actuator.diskvalve == 1 else 'OFF'
  3164. solenoid_tank_pump = 'ON' if tank_actuator.solenoid_tank_pump == 1 else 'OFF'
  3165. tank_solenoid_disinfect = 'ON' if tank_actuator.solenoid_tank_disinfect == 1 else 'OFF'
  3166. outer_solenoid_water = 'ON' if tank_actuator.solenoid_outer_water == 1 else 'OFF'
  3167. tank_solenoid_water_in = 'ON' if tank_actuator.solenoid_tank_water_in == 1 else 'OFF'
  3168. tank_solenoid_water_out = 'ON' if tank_actuator.solenoid_tank_water_out == 1 else 'OFF'
  3169. tank_pump_sensor = 'ON' if tank_actuator.pump_sensor == 1 else 'OFF'
  3170. outer_threewayvalve_float = 'ON' if tank_actuator.threewayvalve_outer_float == 1 else 'OFF'
  3171. tank_motor = tank_actuator.motor
  3172. tank_heater1 = 'ON' if tank_actuator.heater1 == 1 else 'OFF'
  3173. tank_heater2 = 'ON' if tank_actuator.heater2 == 1 else 'OFF'
  3174. tank_temp_enable = 'ON' if tank_actuator.temp_enable == 1 else 'OFF'
  3175. tank_temp = tank_actuator.temp
  3176. # 感測器_發酵桶_SHT11
  3177. tank_SHT11 = ferment_tank_SHT11.query.filter_by(tank_num='F'+str(tid)).order_by(text('datetime desc')).first()
  3178. # 感測器_發酵桶_二氧化碳
  3179. tank_CO2 = ferment_tank_CO2.query.filter_by(tank_num='F'+str(tid)).order_by(text('datetime desc')).first()
  3180. # 感測器_發酵桶_酸鹼值
  3181. tank_PH = ferment_tank_PH.query.filter_by(tank_num='F'+str(tid)).order_by(text('datetime desc')).first()
  3182. tank_ORP = ferment_tank_ORP.query.filter_by(tank_num='F'+str(tid)).order_by(text('datetime desc')).first()
  3183. tank_DO = ferment_tank_DO.query.filter_by(tank_num='F'+str(tid)).order_by(text('datetime desc')).first()
  3184. tank_EC = ferment_tank_EC.query.filter_by(tank_num='F'+str(tid)).order_by(text('datetime desc')).first()
  3185. # 感測器_發酵桶_氣壓
  3186. tank_PA = ferment_tank_PA.query.filter_by(tank_num='F'+str(tid)).order_by(text('datetime desc')).first()
  3187. # 感測器_發酵桶_超音波感測器
  3188. tank_UltraSonic = ferment_tank_UltraSonic.query.filter_by(tank_num='F'+str(tid)).order_by(text('datetime desc')).first()
  3189. # 感測器_發酵桶_水位計
  3190. tank_WaterLevel = ferment_tank_WaterLevel.query.filter_by(tank_num='F'+str(tid)).order_by(text('datetime desc')).first()
  3191. num_tank_WaterLevel = float(tank_WaterLevel.WaterLevel)
  3192. if tank_WaterLevel.WaterLevel == '1':
  3193. WaterLevel = '滿'
  3194. elif tank_WaterLevel.WaterLevel == '0':
  3195. WaterLevel = '未達滿水高度'
  3196. else:
  3197. WaterLevel = 'ERROR'
  3198. # 感測器_發酵桶_咖啡生豆高度
  3199. #tank_LiDAR = ferment_tank_LiDAR.query.filter_by(tank_num='F'+str(tid)).order_by(text('datetime desc')).first()
  3200. #LiDAR = tank_LiDAR.LiDAR
  3201. # 感測器_發酵桶_水位高度
  3202. #tank_PressureWaterLevel = ferment_tank_PressureWaterLevel.query.filter_by(tank_num='F'+str(tid)).order_by(text('datetime desc')).first()
  3203. #PressureWaterLevel = '{:.2f}'.format((float(tank_PressureWaterLevel.PressureWaterLevel) -15 -90.278)/9.4214)
  3204. return render_template('ferment_container_tank.html', title='[操作] F' + tid + ' 發酵槽操作介面', **locals())
  3205. elif request.method == 'POST':
  3206. pass
  3207. @main.route('/ferment_container_input/<tid>', methods=['GET', 'POST'])
  3208. def ferment_container_input(tid):
  3209. if request.method == 'GET':
  3210. if 'id' in session and 'uname' in session and 'status' in session:
  3211. username = session['uname']
  3212. status = session['status']
  3213. if status == 9:
  3214. return render_template('signin_disable.html', params=locals())
  3215. elif status == 8:
  3216. return render_template('signin_new.html', params=locals())
  3217. else:
  3218. return render_template('oops.html', params=locals())
  3219. # 感測器_入料儲豆槽_超音波感測器
  3220. input_UltraSonic = ferment_input_UltraSonic.query.filter_by(tank_num='FI' + tid).order_by(text('datetime desc')).first()
  3221. # 致動器_入料儲豆槽_真空吸引機
  3222. input_vacuum = ferment_input_actuator.query.filter_by(tank_num='FI' + tid).order_by(text('datetime desc')).first()
  3223. input_vacuum = input_vacuum.vacuum
  3224. return render_template('ferment_container_input.html', title='[操作] FI' + tid + ' 發酵入料儲豆槽操作介面', **locals())
  3225. elif request.method == 'POST':
  3226. pass
  3227. @main.route('/ferment_container_output/<tid>', methods=['GET', 'POST'])
  3228. def ferment_container_output(tid):
  3229. if request.method == 'GET':
  3230. if 'id' in session and 'uname' in session and 'status' in session:
  3231. username = session['uname']
  3232. status = session['status']
  3233. if status == 9:
  3234. return render_template('signin_disable.html', params=locals())
  3235. elif status == 8:
  3236. return render_template('signin_new.html', params=locals())
  3237. else:
  3238. return render_template('oops.html', params=locals())
  3239. # 感測器_出料儲豆槽_超音波感測器
  3240. output_UltraSonic = ferment_output_UltraSonic.query.filter_by(tank_num='FO' + tid).order_by(text('datetime desc')).first()
  3241. # 致動器_出料儲豆槽_真空吸引機
  3242. output_vacuum = ferment_output_actuator.query.filter_by(tank_num='FO' + tid).order_by(text('datetime desc')).first()
  3243. output_vacuum = output_vacuum.vacuum
  3244. tank_LiDAR = ferment_tank_LiDAR.query.filter_by(tank_num='F'+str(tid)).order_by(text('datetime desc')).first()
  3245. LiDAR = tank_LiDAR.LiDAR
  3246. return render_template('ferment_container_output.html', title='[操作] FO' + tid + ' 發酵出料儲豆槽操作介面', **locals())
  3247. elif request.method == 'POST':
  3248. pass
  3249. @main.route('/dry_container_schedule', methods=['GET', 'POST'])
  3250. def dry_container_schedule():
  3251. if request.method == 'GET':
  3252. if 'id' in session and 'uname' in session:
  3253. username = session['uname']
  3254. # 感測器_入料儲豆槽_超音波感測器
  3255. input_UltraSonic = dry_input_sensor.query.order_by(text('datetime desc')).first()
  3256. # 感測器_乾燥桶_超音波感測器
  3257. tank_UltraSonic = dry_tank_UltraSonic.query.order_by(text('datetime desc')).first()
  3258. return render_template('dry_container_schedule.html', title='dry_container_schedule', **locals())
  3259. elif request.method == 'POST':
  3260. pass
  3261. # 乾燥貨櫃_各致動器控制
  3262. @main.route('/ctrl_DI_<actuator>/<tid>', methods=['GET', 'POST'])
  3263. def ctrl_DI(actuator, tid):
  3264. if request.method == 'GET':
  3265. if actuator == 'Vacuum':
  3266. if 'id' in session and 'uname' in session and 'status' in session:
  3267. username = session['uname']
  3268. status = session['status']
  3269. if status == 9:
  3270. return render_template('signin_disable.html', params=locals())
  3271. elif status == 8:
  3272. return render_template('signin_new.html', params=locals())
  3273. else:
  3274. return render_template('oops.html', params=locals())
  3275. input_brake = dry_input_brake.query.filter_by(tank_num='DI'+str(tid)).order_by(text('datetime desc')).first()
  3276. input_vacuum = input_brake.vacuum
  3277. tank_UltraSonic = dry_input_sensor.query.filter_by(tank_num='DI'+str(tid)).order_by(text('datetime desc')).first()
  3278. UltraSonic = tank_UltraSonic.UltraSonic
  3279. return render_template('ctrl_DI_Vacuum.html', title='[操作]DI' + tid + '乾燥槽入料_真空吸料機', **locals())
  3280. elif request.method == 'POST':
  3281. pass
  3282. @main.route('/ctrl_DO_<actuator>/<tid>', methods=['GET', 'POST'])
  3283. def ctrl_DO(actuator, tid):
  3284. if request.method == 'GET':
  3285. if 'id' in session and 'uname' in session and 'status' in session:
  3286. username = session['uname']
  3287. status = session['status']
  3288. if status == 9:
  3289. return render_template('signin_disable.html', params=locals())
  3290. elif status == 8:
  3291. return render_template('signin_new.html', params=locals())
  3292. else:
  3293. return render_template('oops.html', params=locals())
  3294. if actuator == 'Vacuum':
  3295. tank_brake = dry_output_brake.query.filter_by(tank_num='DO'+str(tid)).order_by(text('datetime desc')).first()
  3296. tank_vacuum = tank_brake.vacuum
  3297. tank_UltraSonic = dry_output_sensor.query.filter_by(tank_num='DO'+str(tid)).order_by(text('datetime desc')).first()
  3298. UltraSonic = tank_UltraSonic.UltraSonic
  3299. return render_template('ctrl_DO_Vacuum.html', title='[操作]DI' + tid + '乾燥槽出料_真空吸料機', **locals())
  3300. elif request.method == 'POST':
  3301. pass
  3302. @main.route('/ctrl_D_<actuator>/<tid>', methods=['GET', 'POST'])
  3303. def ctrl_D(actuator, tid):
  3304. if request.method == 'GET':
  3305. if 'id' in session and 'uname' in session and 'status' in session:
  3306. username = session['uname']
  3307. status = session['status']
  3308. if status == 9:
  3309. return render_template('signin_disable.html', params=locals())
  3310. elif status == 8:
  3311. return render_template('signin_new.html', params=locals())
  3312. else:
  3313. return render_template('oops.html', params=locals())
  3314. tank_brake = dry_tank_brake.query.filter_by(tank_num='D'+str(tid)).order_by(text('datetime desc')).first()
  3315. if actuator == 'Vacuum':
  3316. tank_vacuum = tank_brake.vacuum
  3317. tank_UltraSonic = dry_tank_UltraSonic.query.filter_by(tank_num='D'+str(tid)).order_by(text('datetime desc')).first()
  3318. UltraSonic = tank_UltraSonic.UltraSonic
  3319. return render_template('ctrl_D_Vacuum.html', title='[操作]D' + tid + '乾燥槽_真空吸料機', **locals())
  3320. elif actuator == 'ThreeWayValve':
  3321. tank_threewayvalve = tank_brake.threewayvalve
  3322. return render_template('ctrl_D_ThreeWayValve.html', title='[操作]D' + tid + '乾燥槽_三通閥', **locals())
  3323. elif actuator == 'DiskValve':
  3324. tank_diskvalve = tank_brake.diskvalve
  3325. return render_template('ctrl_D_DiskValve.html', title='[操作]D' + tid + '乾燥槽_蝴蝶閥', **locals())
  3326. elif actuator == 'SolenoidDisinfect':
  3327. tank_solenoid_disinfect = tank_brake.solenoid_disinfect
  3328. return render_template('ctrl_D_SolenoidDisinfect.html', title='[操作]D' + tid + '乾燥槽_消毒電磁閥', **locals())
  3329. elif actuator == 'SolenoidWater':
  3330. tank_solenoid_water = tank_brake.solenoid_water
  3331. return render_template('ctrl_D_SolenoidWater.html', title='[操作]D' + tid + '乾燥槽_排水電磁閥', **locals())
  3332. elif actuator == 'Motor':
  3333. tank_motor = tank_brake.motor
  3334. return render_template('ctrl_D_Motor.html', title='[操作]D' + tid + '乾燥槽_馬達', **locals())
  3335. elif actuator == 'Blower':
  3336. tank_blower = tank_brake.blower
  3337. tank_SHT11 = dry_tank_SHT11.query.filter_by(tank_num='D'+str(tid)).order_by(text('datetime desc')).first()
  3338. tank_Soil = dry_tank_Soil.query.filter_by(tank_num='D'+str(tid)).order_by(text('datetime desc')).first()
  3339. return render_template('ctrl_D_Blower.html', title='[操作]D' + tid + '乾燥槽_鼓風機', **locals())
  3340. elif actuator == 'Heater1':
  3341. tank_heater1 = tank_brake.heater1
  3342. tank_SHT11 = dry_tank_SHT11.query.filter_by(tank_num='D'+str(tid)).order_by(text('datetime desc')).first()
  3343. tank_Soil = dry_tank_Soil.query.filter_by(tank_num='D'+str(tid)).order_by(text('datetime desc')).first()
  3344. return render_template('ctrl_D_Heater1.html', title='[操作]D' + tid + '乾燥槽_加熱器 1', **locals())
  3345. elif actuator == 'Heater2':
  3346. tank_heater2 = tank_brake.heater2
  3347. tank_SHT11 = dry_tank_SHT11.query.filter_by(tank_num='D'+str(tid)).order_by(text('datetime desc')).first()
  3348. tank_Soil = dry_tank_Soil.query.filter_by(tank_num='D'+str(tid)).order_by(text('datetime desc')).first()
  3349. return render_template('ctrl_D_Heater2.html', title='[操作]D' + tid + '乾燥槽_加熱器 2', **locals())
  3350. elif actuator == 'TempEnable':
  3351. tank_temp1_enable = tank_brake.temp1_enable
  3352. return render_template('ctrl_D_TempEnable.html', title='[操作]D' + tid + '乾燥槽_溫控開關', **locals())
  3353. elif actuator == 'Temp':
  3354. tank_temp1_enable = tank_brake.temp1_enable
  3355. tank_temp1 = tank_brake.temp1
  3356. tank_SHT11 = dry_tank_SHT11.query.filter_by(tank_num='D'+str(tid)).order_by(text('datetime desc')).first()
  3357. tank_Soil = dry_tank_Soil.query.filter_by(tank_num='D'+str(tid)).order_by(text('datetime desc')).first()
  3358. return render_template('ctrl_D_Temp.html', title='[操作]D' + tid + '乾燥槽_設定溫度', **locals())
  3359. elif request.method == 'POST':
  3360. pass
  3361. # 發酵貨櫃_各致動器控制
  3362. @main.route('/ctrl_FI_<actuator>/<tid>', methods=['GET', 'POST'])
  3363. def ctrl_FI(actuator, tid):
  3364. if request.method == 'GET':
  3365. if 'id' in session and 'uname' in session and 'status' in session:
  3366. username = session['uname']
  3367. status = session['status']
  3368. if status == 9:
  3369. return render_template('signin_disable.html', params=locals())
  3370. elif status == 8:
  3371. return render_template('signin_new.html', params=locals())
  3372. else:
  3373. return render_template('oops.html', params=locals())
  3374. if actuator == 'Vacuum':
  3375. if 'id' in session and 'uname' in session and 'status' in session:
  3376. username = session['uname']
  3377. status = session['status']
  3378. if status == 9:
  3379. return render_template('signin_disable.html', params=locals())
  3380. elif status == 8:
  3381. return render_template('signin_new.html', params=locals())
  3382. else:
  3383. return render_template('oops.html', params=locals())
  3384. tank_actuator = ferment_input_actuator.query.filter_by(tank_num='FI'+str(tid)).order_by(text('datetime desc')).first()
  3385. tank_vacuum = tank_actuator.vacuum
  3386. tank_UltraSonic = ferment_input_UltraSonic.query.filter_by(tank_num='FI'+str(tid)).order_by(text('datetime desc')).first()
  3387. UltraSonic = tank_UltraSonic.UltraSonic
  3388. return render_template('ctrl_FI_Vacuum.html', title='[操作]FI' + tid + '發酵入料儲豆槽_真空吸料機', **locals())
  3389. elif request.method == 'POST':
  3390. pass
  3391. @main.route('/ctrl_FO_<actuator>/<tid>', methods=['GET', 'POST'])
  3392. def ctrl_FO(actuator, tid):
  3393. if request.method == 'GET':
  3394. if 'id' in session and 'uname' in session and 'status' in session:
  3395. username = session['uname']
  3396. status = session['status']
  3397. if status == 9:
  3398. return render_template('signin_disable.html', params=locals())
  3399. elif status == 8:
  3400. return render_template('signin_new.html', params=locals())
  3401. else:
  3402. return render_template('oops.html', params=locals())
  3403. if actuator == 'Vacuum':
  3404. tank_actuator = ferment_output_actuator.query.filter_by(tank_num='FO'+str(tid)).order_by(text('datetime desc')).first()
  3405. tank_vacuum = tank_actuator.vacuum
  3406. tank_UltraSonic = ferment_output_UltraSonic.query.filter_by(tank_num='FO'+str(tid)).order_by(text('datetime desc')).first()
  3407. UltraSonic = tank_UltraSonic.UltraSonic
  3408. return render_template('ctrl_FO_Vacuum.html', title='[操作]FO' + tid + '發酵出料儲豆槽_真空吸料機', **locals())
  3409. elif request.method == 'POST':
  3410. pass
  3411. @main.route('/ctrl_F_<actuator>/<tid>', methods=['GET', 'POST'])
  3412. def ctrl_F(actuator, tid):
  3413. if request.method == 'GET':
  3414. if 'id' in session and 'uname' in session and 'status' in session:
  3415. username = session['uname']
  3416. status = session['status']
  3417. if status == 9:
  3418. return render_template('signin_disable.html', params=locals())
  3419. elif status == 8:
  3420. return render_template('signin_new.html', params=locals())
  3421. else:
  3422. return render_template('oops.html', params=locals())
  3423. tank_actuator = ferment_tank_actuator.query.filter_by(tank_num='F'+str(tid)).order_by(text('datetime desc')).first()
  3424. if actuator == 'Vacuum':
  3425. tank_vacuum = tank_actuator.vacuum
  3426. tank_LiDAR = ferment_tank_LiDAR.query.filter_by(tank_num='F'+str(tid)).order_by(text('datetime desc')).first()
  3427. LiDAR = tank_LiDAR.LiDAR
  3428. return render_template('ctrl_F_Vacuum.html', title='[操作]F' + tid + '發酵槽_真空吸料機', **locals())
  3429. elif actuator == 'ThreeWayValveInput':
  3430. tank_threewayvalveinput = tank_actuator.threewayvalve_input
  3431. return render_template('ctrl_F_ThreeWayValveInput.html', title='[操作]F' + tid + '發酵槽_入料三通閥', **locals())
  3432. elif actuator == 'SolenoidWaterTotal':
  3433. tank_solenoid_tank_water_total = tank_actuator.solenoid_tank_water_total
  3434. return render_template('ctrl_F_SolenoidWaterTotal.html', title='[操作]F' + tid + '發酵槽_總進水電磁閥', **locals())
  3435. elif actuator == 'SolenoidOuterWater':
  3436. tank_solenoid_outer_water = tank_actuator.solenoid_outer_water
  3437. tank_WaterLevel = ferment_tank_WaterLevel.query.filter_by(tank_num='F' + tid).order_by(text('datetime desc')).first()
  3438. WaterLevel = tank_WaterLevel.WaterLevel
  3439. return render_template('ctrl_F_SolenoidOuterWater.html', title='[操作]F' + tid + '發酵槽_保溫夾層進水電磁閥', **locals())
  3440. elif actuator == 'SolenoidWaterIn':
  3441. tank_solenoid_tank_water_in = tank_actuator.solenoid_tank_water_in
  3442. tank_PressureWaterLevel = ferment_tank_PressureWaterLevel.query.filter_by(tank_num='F' + tid).order_by(text('datetime desc')).first()
  3443. PressureWaterLevel = tank_PressureWaterLevel.PressureWaterLevel
  3444. return render_template('ctrl_F_SolenoidWaterIn.html', title='[操作]F' + tid + '發酵槽_桶內進水電磁閥', **locals())
  3445. elif actuator == 'PumpSensor':
  3446. tank_pump_sensor = tank_actuator.pump_sensor
  3447. tank_PH = ferment_tank_PH.query.filter_by(tank_num='F' + tid).order_by(text('datetime desc')).first()
  3448. tank_ORP = ferment_tank_ORP.query.filter_by(tank_num='F' + tid).order_by(text('datetime desc')).first()
  3449. tank_DO = ferment_tank_DO.query.filter_by(tank_num='F' + tid).order_by(text('datetime desc')).first()
  3450. tank_EC = ferment_tank_EC.query.filter_by(tank_num='F' + tid).order_by(text('datetime desc')).first()
  3451. PH = tank_PH.PH
  3452. ORP = tank_ORP.ORP
  3453. DO = tank_DO.DO
  3454. EC = tank_EC.EC
  3455. return render_template('ctrl_F_PumpSensor.html', title='[操作]F' + tid + '發酵槽_感測器抽水雙核隔膜泵', **locals())
  3456. elif actuator == 'ThreeWayValveBean':
  3457. tank_threewayvalve_bean = tank_actuator.threewayvalve_bean
  3458. return render_template('ctrl_F_ThreeWayValveBean.html', title='[操作]F' + tid + '發酵槽_感測模組下豆三通閥', **locals())
  3459. elif actuator == 'ThreeWayValveFloat':
  3460. tank_threewayvalve_outer_float = tank_actuator.threewayvalve_outer_float
  3461. return render_template('ctrl_F_ThreeWayValveFloat.html', title='[操作]F' + tid + '發酵槽_外桶浮選三通閥', **locals())
  3462. elif actuator == 'SolenoidDisinfect':
  3463. tank_solenoid_tank_disinfect = tank_actuator.solenoid_tank_disinfect
  3464. return render_template('ctrl_F_SolenoidDisinfect.html', title='[操作]F' + tid + '發酵槽_桶內消毒電磁閥', **locals())
  3465. elif actuator == 'Motor':
  3466. tank_motor = tank_actuator.motor
  3467. return render_template('ctrl_F_Motor.html', title='[操作]F' + tid + '發酵槽_馬達', **locals())
  3468. elif actuator == 'Heater1':
  3469. tank_heater1 = tank_actuator.heater1
  3470. tank_SHT11 = ferment_tank_SHT11.query.filter_by(tank_num='F' + tid).order_by(text('datetime desc')).first()
  3471. return render_template('ctrl_F_Heater1.html', title='[操作]F' + tid + '發酵槽_加熱器 1', **locals())
  3472. elif actuator == 'Heater2':
  3473. tank_heater2 = tank_actuator.heater2
  3474. tank_SHT11 = ferment_tank_SHT11.query.filter_by(tank_num='F' + tid).order_by(text('datetime desc')).first()
  3475. return render_template('ctrl_F_Heater2.html', title='[操作]F' + tid + '發酵槽_加熱器 2', **locals())
  3476. elif actuator == 'TempEnable':
  3477. tank_temp_enable = tank_actuator.temp_enable
  3478. return render_template('ctrl_F_TempEnable.html', title='[操作]F' + tid + '發酵槽_溫控開關', **locals())
  3479. elif actuator == 'Temp':
  3480. tank_temp = tank_actuator.temp
  3481. tank_temp_enable = tank_actuator.temp_enable
  3482. tank_SHT11 = ferment_tank_SHT11.query.filter_by(tank_num='F' + tid).order_by(text('datetime desc')).first()
  3483. return render_template('ctrl_F_Temp.html', title='[操作]F' + tid + '發酵槽_溫度設定', **locals())
  3484. elif actuator == 'DiskValve':
  3485. tank_diskvalve = tank_actuator.diskvalve
  3486. return render_template('ctrl_F_DiskValve.html', title='[操作]F' + tid + '發酵槽_蝴蝶閥', **locals())
  3487. @main.route('/dry_Temp/<tid>/<Temp_Weights_SHT11>/<Temp_Weights_Soil>/<temp_data>', methods=['GET', 'POST'])
  3488. def dry_Temp(tid, Temp_Weights_SHT11, Temp_Weights_Soil, temp_data):
  3489. if request.method == 'GET':
  3490. if 'id' in session and 'uname' in session and 'status' in session:
  3491. username = session['uname']
  3492. status = session['status']
  3493. if status == 9:
  3494. return render_template('signin_disable.html', params=locals())
  3495. elif status == 8:
  3496. return render_template('signin_new.html', params=locals())
  3497. else:
  3498. return render_template('oops.html', params=locals())
  3499. tank_SHT11 = dry_tank_SHT11.query.filter_by(tank_num='D'+str(tid)).order_by(text('datetime desc')).first()
  3500. SHT11_Temp = float(tank_SHT11.SHT11_Temp)
  3501. SHT11_Humidity = tank_SHT11.SHT11_Humidity
  3502. tank_Soil = dry_tank_Soil.query.filter_by(tank_num='D'+str(tid)).order_by(text('datetime desc')).first()
  3503. Soil_Temp = float(tank_Soil.soil_Temp)
  3504. Soil_Humidity = tank_Soil.soil_Humidity
  3505. Temp_Weights_SHT11= float(Temp_Weights_SHT11)
  3506. Temp_Weights_Soil = float(Temp_Weights_Soil)
  3507. temp_data = float(temp_data)
  3508. present_temp = math.ceil( (SHT11_Temp*Temp_Weights_SHT11 + Soil_Temp*Temp_Weights_Soil)/(Temp_Weights_SHT11+Temp_Weights_Soil) )
  3509. # Math.round((present_SHT11_Temp*Temp_Weights_SHT11 + present_SOIL_Temp*Temp_Weights_Soil)/(Temp_Weights_SHT11+Temp_Weights_Soil)*100) / 100
  3510. target_temp = math.ceil(temp_data)
  3511. TempUp = {
  3512. 22:0, 23:6, 24:12, 25:18, 26:24, 27:28, 28:32, 29:36, 30:40, 31:44, 32:48, 33:56, 34:64, 35:72, 36:96, 37:168, 38:288, 39:384, 40:529, 41:697, 42:793, 43:865, 44:1394
  3513. }
  3514. if target_temp in TempUp and present_temp in TempUp:
  3515. TempUpTime = ', 預計 ' + str(TempUp[target_temp] - TempUp[present_temp]) + ' 秒達到指定溫度'
  3516. elif target_temp < 22 or present_temp < 22 :
  3517. TempUpTime = '℃, 指定溫度過低, 持續升溫中'
  3518. elif target_temp > 44 or present_temp > 44 :
  3519. TempUpTime = '℃, 指定溫度過高, 持續升溫中'
  3520. return jsonify({"SHT11_Temp":SHT11_Temp,
  3521. "SHT11_Humidity":SHT11_Humidity,
  3522. "Soil_Temp":Soil_Temp,
  3523. "Soil_Humidity":Soil_Humidity,
  3524. "present_temp":present_temp,
  3525. "TempUpTime":TempUpTime
  3526. })
  3527. else:
  3528. pass
  3529. @main.route('/dry_container_tank/<tid>', methods=['GET', 'POST'])
  3530. def dry_container_tank(tid):
  3531. if request.method == 'GET':
  3532. if 'id' in session and 'uname' in session and 'status' in session:
  3533. username = session['uname']
  3534. status = session['status']
  3535. if status == 9:
  3536. return render_template('signin_disable.html', params=locals())
  3537. elif status == 8:
  3538. return render_template('signin_new.html', params=locals())
  3539. else:
  3540. return render_template('oops.html', params=locals())
  3541. # 感測器_乾燥桶_SHT11
  3542. tank_SHT11 = dry_tank_SHT11.query.filter_by(tank_num='D'+str(tid)).order_by(text('datetime desc')).first()
  3543. num_SHT11_temp = float(tank_SHT11.SHT11_Temp)
  3544. # 感測器_乾燥桶_土壤三合一
  3545. tank_Soil = dry_tank_Soil.query.filter_by(tank_num='D'+str(tid)).order_by(text('datetime desc')).first()
  3546. num_Soil_temp = float(tank_Soil.soil_Temp)
  3547. # 感測器_乾燥桶_氣壓
  3548. tank_PA = dry_tank_PA.query.filter_by(tank_num='D'+str(tid)).order_by(text('datetime desc')).first()
  3549. # 感測器_乾燥桶_超音波感測器
  3550. tank_UltraSonic = dry_tank_UltraSonic.query.filter_by(tank_num='D'+str(tid)).order_by(text('datetime desc')).first()
  3551. # 致動器_乾燥桶_ALL
  3552. tank_brake = dry_tank_brake.query.filter_by(tank_num='D'+str(tid)).order_by(text('datetime desc')).first()
  3553. tank_vacuum = tank_brake.vacuum
  3554. tank_threewayvalve = tank_brake.threewayvalve
  3555. tank_diskvalve = tank_brake.diskvalve
  3556. tank_solenoid_disinfect = tank_brake.solenoid_disinfect
  3557. tank_solenoid_water = tank_brake.solenoid_water
  3558. tank_motor = tank_brake.motor
  3559. tank_blower = tank_brake.blower
  3560. tank_heater1 = tank_brake.heater1
  3561. tank_heater2 = tank_brake.heater2
  3562. tank_temp1_enable = tank_brake.temp1_enable
  3563. tank_temp1 = tank_brake.temp1
  3564. Temp_weight = num_SHT11_temp * 0.5 + num_Soil_temp * 0.5
  3565. return render_template('dry_container_tank.html', title='[操作] D' + tid + ' 乾燥槽操作介面', **locals())
  3566. elif request.method == 'POST':
  3567. pass
  3568. @main.route('/dry_container_input/<tid>', methods=['GET', 'POST'])
  3569. def dry_container_input(tid):
  3570. if request.method == 'GET':
  3571. if 'id' in session and 'uname' in session and 'status' in session:
  3572. username = session['uname']
  3573. status = session['status']
  3574. if status == 9:
  3575. return render_template('signin_disable.html', params=locals())
  3576. elif status == 8:
  3577. return render_template('signin_new.html', params=locals())
  3578. else:
  3579. return render_template('oops.html', params=locals())
  3580. input_UltraSonic = dry_input_sensor.query.filter_by(tank_num='DI'+str(tid)).order_by(text('datetime desc')).first()
  3581. input_vacuum = dry_input_brake.query.filter_by(tank_num='DI'+str(tid)).order_by(text('datetime desc')).first()
  3582. input_vacuum = input_vacuum.vacuum
  3583. return render_template('dry_container_input.html', title='[操作] DI' + tid + ' 乾燥入料儲豆槽操作介面', **locals())
  3584. elif request.method == 'POST':
  3585. pass
  3586. @main.route('/dry_container_output/<tid>', methods=['GET', 'POST'])
  3587. def dry_container_output(tid):
  3588. if request.method == 'GET':
  3589. if 'id' in session and 'uname' in session and 'status' in session:
  3590. username = session['uname']
  3591. status = session['status']
  3592. if status == 9:
  3593. return render_template('signin_disable.html', params=locals())
  3594. elif status == 8:
  3595. return render_template('signin_new.html', params=locals())
  3596. else:
  3597. return render_template('oops.html', params=locals())
  3598. output_UltraSonic = dry_output_sensor.query.filter_by(tank_num='DO'+str(tid)).order_by(text('datetime desc')).first()
  3599. output_vacuum = dry_output_brake.query.filter_by(tank_num='DO'+str(tid)).order_by(text('datetime desc')).first()
  3600. output_vacuum = output_vacuum.vacuum
  3601. return render_template('dry_container_output.html', title='[操作] DO' + tid + ' 乾燥出料儲豆槽操作介面', **locals())
  3602. elif request.method == 'POST':
  3603. pass
  3604. # 原本的乾燥貨櫃控制, 備份用
  3605. @main.route('/dry_container/<tid>', methods=['GET', 'POST'])
  3606. def dry_container(tid):
  3607. if request.method == 'GET':
  3608. if 'id' in session and 'uname' in session and 'status' in session:
  3609. username = session['uname']
  3610. status = session['status']
  3611. if status == 9:
  3612. return render_template('signin_disable.html', params=locals())
  3613. elif status == 8:
  3614. return render_template('signin_new.html', params=locals())
  3615. else:
  3616. return render_template('oops.html', params=locals())
  3617. # Rita 參數 params 是用來取得參數的 locals=() 所有參數
  3618. '''
  3619. # 開啟本機 coffeetest 資料庫
  3620. # mydb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='g53743001', database='coffeetest', charset='utf8')
  3621. mydb = pymysql.connect(host='52.69.200.169', port=3306, user='coffee',
  3622. password='skyeye', database='Coffee', charset='utf8')
  3623. mycursor = mydb.cursor()
  3624. '''
  3625. # 感測器_乾燥桶_SHT11
  3626. tank_SHT11 = dry_tank_SHT11.query.filter_by(tank_num='D'+str(tid)).order_by(text('datetime desc')).first()
  3627. num_SHT11_temp = float(tank_SHT11.SHT11_Temp)
  3628. # 感測器_乾燥桶_土壤三合一
  3629. tank_Soil = dry_tank_Soil.query.filter_by(tank_num='D'+str(tid)).order_by(text('datetime desc')).first()
  3630. num_Soil_temp = float(tank_Soil.soil_Temp)
  3631. # 感測器_乾燥桶_氣壓
  3632. tank_PA = dry_tank_PA.query.filter_by(tank_num='D'+str(tid)).order_by(text('datetime desc')).first()
  3633. # 感測器_乾燥桶_超音波感測器
  3634. tank_UltraSonic = dry_tank_UltraSonic.query.filter_by(tank_num='D'+str(tid)).order_by(text('datetime desc')).first()
  3635. # 感測器_出料儲豆槽_超音波感測器
  3636. output_UltraSonic = dry_output_sensor.query.filter_by(tank_num='D'+str(tid)).order_by(text('datetime desc')).first()
  3637. # 致動器_乾燥桶_ALL
  3638. tank_brake = dry_tank_brake.query.filter_by(tank_num='D'+str(tid)).order_by(text('datetime desc')).first()
  3639. tank_vacuum = tank_brake.vacuum
  3640. tank_threewayvalve = tank_brake.threewayvalve
  3641. tank_diskvalve = tank_brake.diskvalve
  3642. tank_solenoid_disinfect = tank_brake.solenoid_disinfect
  3643. tank_solenoid_water = tank_brake.solenoid_water
  3644. tank_motor = tank_brake.motor
  3645. tank_blower = tank_brake.blower
  3646. tank_heater1 = tank_brake.heater1
  3647. tank_heater2 = tank_brake.heater2
  3648. tank_temp1_enable = tank_brake.temp1_enable
  3649. tank_temp1 = tank_brake.temp1
  3650. '''
  3651. tank_vacuum = 'ON' if tank_brake.vacuum == 1 else 'OFF'
  3652. tank_threewayvalve = 'ON' if tank_brake.threewayvalve == 1 else 'OFF'
  3653. tank_diskvalve = 'ON' if tank_brake.diskvalve == 1 else 'OFF'
  3654. tank_solenoid_disinfect = 'ON' if tank_brake.solenoid_disinfect == 1 else 'OFF'
  3655. tank_solenoid_water = 'ON' if tank_brake.solenoid_water == 1 else 'OFF'
  3656. tank_motor = tank_brake.motor
  3657. tank_blower = 'ON' if tank_brake.blower == 1 else 'OFF'
  3658. tank_heater1 = 'ON' if tank_brake.heater1 == 1 else 'OFF'
  3659. tank_heater2 = 'ON' if tank_brake.heater2 == 1 else 'OFF'
  3660. tank_temp1_enable = 'ON' if tank_brake.temp1_enable == 1 else 'OFF'
  3661. tank_temp1 = tank_brake.temp1
  3662. '''
  3663. '''
  3664. data = mycursor.execute(
  3665. 'SELECT * FROM dry_tank_brake ORDER BY datetime DESC;')
  3666. data = mycursor.fetchone()
  3667. '''
  3668. if tid in ['1', '2', '3', '4', '5', '6']:
  3669. # print("1")
  3670. # 感測器_入料儲豆槽_超音波感測器
  3671. input_UltraSonic = dry_input_sensor.query.filter_by(tank_num='DI1').order_by(text('datetime desc')).first()
  3672. # 致動器_入料儲豆槽_真空吸引機
  3673. input_vacuum = dry_input_brake.query.filter_by(tank_num='DI1').order_by(text('datetime desc')).first()
  3674. input_vacuum = input_vacuum.vacuum
  3675. # 感測器_出料儲豆槽_超音波感測器
  3676. output_UltraSonic = dry_output_sensor.query.filter_by(tank_num='DO1').order_by(text('datetime desc')).first()
  3677. # 致動器_出料儲豆槽_真空吸引機
  3678. output_vacuum = dry_output_brake.query.filter_by(tank_num='DO1').order_by(text('datetime desc')).first()
  3679. output_vacuum = output_vacuum.vacuum
  3680. elif tid in ['7', '8', '9', '10', '11', '12']:
  3681. print("2")
  3682. # 感測器_入料儲豆槽_超音波感測器
  3683. input_UltraSonic = dry_input_sensor.query.filter_by(tank_num='DI2').order_by(text('datetime desc')).first()
  3684. # 致動器_入料儲豆槽_真空吸引機
  3685. input_vacuum = dry_input_brake.query.filter_by(tank_num='DI2').order_by(text('datetime desc')).first()
  3686. input_vacuum = input_vacuum.vacuum
  3687. # 感測器_出料儲豆槽_超音波感測器
  3688. output_UltraSonic = dry_output_sensor.query.filter_by(tank_num='DO2').order_by(text('datetime desc')).first()
  3689. # 致動器_出料儲豆槽_真空吸引機
  3690. output_vacuum = dry_output_brake.query.filter_by(tank_num='DO2').order_by(text('datetime desc')).first()
  3691. output_vacuum = output_vacuum.vacuum
  3692. '''
  3693. # 感測器_入料儲豆槽_超音波感測器
  3694. input_UltraSonic = dry_input_sensor.query.filter_by(tank_num='D'+str(tid)).order_by(text('datetime desc')).first()
  3695. # 致動器_入料儲豆槽_真空吸引機
  3696. input_vacuum = dry_input_brake.query.filter_by(tank_num='D'+str(tid)).order_by(text('datetime desc')).first()
  3697. # print("input_vacuum:", input_vacuum)
  3698. input_vacuum = input_vacuum.vacuum
  3699. #print('input_vacuum:', input_vacuum.vacuum)
  3700. # 致動器_出料儲豆槽_真空吸引機
  3701. output_vacuum = dry_output_brake.query.filter_by(tank_num='D'+str(tid)).order_by(text('datetime desc')).first()
  3702. #print('output_vacuum.vacuum: ', output_vacuum.vacuum)
  3703. output_vacuum = 'ON' if (output_vacuum.vacuum == 1) else 'OFF'
  3704. #print('output_vacuum: ', output_vacuum)
  3705. '''
  3706. # 傳 MQTT 給阿超, 阿超讓硬體動作, 將資料儲存到資料庫
  3707. input_data = set_dry_input.query.order_by(text('datetime desc')).first()
  3708. schedule_input_height = input_data.input_height
  3709. schedule_input_entertime = input_data.input_entertime
  3710. schedule_input_exittime = input_data.input_exittime
  3711. schedule_tank_height = input_data.tank_height
  3712. return render_template('dry_container.html', title='dry_container', **locals())
  3713. elif request.method == 'POST':
  3714. # 表單輸入處
  3715. print('/dry_input_setting POST')
  3716. set = set_dry_input()
  3717. set.input_height = request.form['schedule_input_height']
  3718. set.input_entertime = request.form['schedule_input_entertime']
  3719. set.input_exittime = request.form['schedule_input_exittime']
  3720. set.tank_height = request.form['schedule_tank_height']
  3721. #將數據保存進資料庫 - 註冊
  3722. db.session.add(set)
  3723. # 手動提交,目的是為了獲取提交後的user的id
  3724. db.session.commit()
  3725. # 當user成功插入進資料庫之後,程序會自動將所有信息取出來在賦值給user
  3726. # 完成登入的操作
  3727. # setting = Set_dry_input.query.order_by(text('datetime desc')).first()
  3728. #session['id'] = set.userID
  3729. #session['uname'] = set.username
  3730. return redirect('/dry_container')
  3731. # 登入頁面的訪問路徑
  3732. @main.route('/login', methods=['GET', 'POST'])
  3733. def login_views():
  3734. if request.method == 'GET':
  3735. if 'id' in session and 'uname' in session and 'status' in session:
  3736. return redirect('/')
  3737. else:
  3738. return render_template('sign_in.html')
  3739. else:
  3740. # 接收前端傳過來的資料
  3741. username = request.form['username']
  3742. password = request.form['password']
  3743. # 使用接收的用戶和密碼到資料庫中查詢
  3744. user = User.query.filter_by(
  3745. username=username, password=password).first()
  3746. # 如果用戶存在,將信息保存置session並重定向回首頁,否則重定向回登入頁
  3747. if user:
  3748. resp = redirect('/')
  3749. # 判斷是否有記住密碼
  3750. if 'rem' in request.form:
  3751. userID = str(user.userID)
  3752. max_age = 60*60*24*365
  3753. status = str(user.status)
  3754. print('status: ', status)
  3755. # Rita 以秒為單位, 一年
  3756. # Rita 功能和 expires 很像,但此參數並非所有瀏覽器均支持,所以建議使用 expires 參數。
  3757. # Rita expires:指定 Cookie 的有效日期, 當過了有效日期後就不會儲存在瀏覽器
  3758. resp.set_cookie("username", username, max_age=max_age)
  3759. resp.set_cookie("userID", userID, max_age=max_age)
  3760. resp.set_cookie("status", status, max_age=max_age)
  3761. session['uname'] = user.username
  3762. session['id'] = user.userID
  3763. session['status'] = user.status
  3764. return resp
  3765. else:
  3766. errMsg = "Wrong login or password"
  3767. return render_template('sign_in.html', errMsg=errMsg)
  3768. # 登入重設密碼的頁面
  3769. # Rita reset_pwd1 - Email
  3770. # Rita reset_pwd2 - New password / Confirm password
  3771. @main.route('/reset_password', methods=['POST', 'GET'])
  3772. def reset_password_views():
  3773. if request.method == 'GET':
  3774. if 'mail' in session:
  3775. del session['mail']
  3776. return render_template('reset_pwd1.html')
  3777. else:
  3778. # POST
  3779. # 如果有id在session裡,代表從reset_pwd2過來的 ? 對, reset_pwd2 POST (但也有其他可能)
  3780. if "mail" in session:
  3781. new_pwd = request.form['new_pwd']
  3782. confirm_pwd = request.form['confirm_pwd']
  3783. # 判斷密碼是否一致
  3784. if new_pwd == confirm_pwd:
  3785. mail = session['mail']
  3786. user = User.query.filter_by(mail=mail).first()
  3787. print("user: ", user)
  3788. print("user.password_before: ", user.password)
  3789. print("new_pwd: ", new_pwd)
  3790. user.password = new_pwd
  3791. print("user.password_after: ", user.password)
  3792. db.session.add(user)
  3793. db.session.commit()
  3794. del session['mail']
  3795. # 修改完後回登入頁
  3796. return redirect('/login')
  3797. else:
  3798. errMsg = "Passwords does not match"
  3799. # Rita 前面的 errMsg 是指網頁上的 {{errMsg}} 位置
  3800. return render_template('reset_pwd2.html', errMsg=errMsg)
  3801. email = request.form['email']
  3802. user = User.query.filter_by(mail=email).first()
  3803. if user: # Rita 有找到對應 email
  3804. session['mail'] = user.mail
  3805. return render_template('reset_pwd2.html')
  3806. else:
  3807. errMsg = "Wrong email.Please try again"
  3808. return render_template('reset_pwd1.html', errMsg=errMsg)
  3809. # 註冊頁面的訪問路徑
  3810. @main.route('/register', methods=['POST', "GET"])
  3811. def register_views():
  3812. if request.method == 'GET':
  3813. return render_template('registration.html')
  3814. else:
  3815. # 獲取文本框的值並賦值給user實體對象
  3816. user = User()
  3817. user.firstname = request.form['firstname']
  3818. user.lastname = request.form['lastname']
  3819. user.mail = request.form['email']
  3820. user.phone = request.form['phone']
  3821. user.username = request.form['username']
  3822. user.password = request.form['password']
  3823. user.status = 8 # Rita app\models.py 設定 0:admin;1:new;9:disable [0906 更新] 0:admin;1:superuser;2:enduser;8:new;9:disable
  3824. #將數據保存進資料庫 - 註冊
  3825. db.session.add(user)
  3826. # 手動提交,目的是為了獲取提交後的user的id
  3827. db.session.commit()
  3828. # 當user成功插入進資料庫之後,程序會自動將所有信息取出來在賦值給user
  3829. # 完成登入的操作
  3830. user = User.query.filter_by(username=user.username).first()
  3831. session['status'] = user.status
  3832. session['id'] = user.userID
  3833. session['uname'] = user.username
  3834. return redirect('/')
  3835. # 驗證email訪問路徑
  3836. @main.route('/check_email')
  3837. def check_email_views():
  3838. email = request.args['email']
  3839. user = User.query.filter_by(mail=email).first()
  3840. if user:
  3841. # Rita email 錯誤時會顯示 Incorrect email address
  3842. result = {"errMsg": " "}
  3843. else:
  3844. result = {"pass": " "}
  3845. return json.dumps(result)
  3846. # 驗證username訪問路徑
  3847. @main.route('/check_username')
  3848. def check_username_views():
  3849. username = request.args['username']
  3850. user = User.query.filter_by(username=username).first()
  3851. if user:
  3852. result = {"errMsg": " "}
  3853. else:
  3854. result = {"pass": " "}
  3855. return json.dumps(result)
  3856. # 咖啡貨櫃的訪問路徑
  3857. @main.route('/cargo_list', methods=['GET', 'POST'])
  3858. def cargo_list_views():
  3859. username = session['uname']
  3860. if request.method == 'GET':
  3861. return render_template('cargo_list.html', params=locals())
  3862. else:
  3863. pass
  3864. # 貨櫃1的訪問路徑
  3865. @main.route('/cargo1', methods=['GET', 'POST'])
  3866. def cargo1_views():
  3867. username = session['uname']
  3868. if request.method == 'GET':
  3869. return render_template('cargo1.html', params=locals())
  3870. else:
  3871. pass
  3872. # 貨櫃1排程的訪問路徑
  3873. @main.route('/cargo1_schedule', methods=['GET', 'POST'])
  3874. def cargo1_schedule_views():
  3875. username = session['uname']
  3876. if request.method == 'GET':
  3877. # 將已儲存的排程資料傳給前端
  3878. # 進豆閥
  3879. try:
  3880. bean = BeanValve.query.order_by(text('datetime desc')).first()
  3881. datetime = bean.datetime
  3882. beans = BeanValve.query.filter_by(
  3883. datetime=datetime).all()
  3884. except Exception as e:
  3885. pass
  3886. # 清洗機
  3887. try:
  3888. wash = WashMachine.query.order_by(text('datetime desc')).first()
  3889. datetime = wash.datetime
  3890. washes = WashMachine.query.filter_by(datetime=datetime).all()
  3891. except Exception as e:
  3892. pass
  3893. # 輸送帶1
  3894. try:
  3895. belt1 = ConveyorBelt1.query.order_by(text('datetime desc')).first()
  3896. datetime = belt1.datetime
  3897. belts1 = ConveyorBelt1.query.filter_by(datetime=datetime).all()
  3898. except Exception as e:
  3899. pass
  3900. # ///////////////////////////////////////////////////////////////////////////////////////////
  3901. # 消毒機
  3902. try:
  3903. disinfect = Cargo1Disinfect.query.order_by(
  3904. text('datetime desc')).first()
  3905. datetime = disinfect.datetime
  3906. disinfects = Cargo1Disinfect.query.filter_by(
  3907. datetime=datetime).all()
  3908. except Exception as e:
  3909. pass
  3910. # 色選機
  3911. try:
  3912. color = ColorMachine.query.order_by(text('datetime desc')).first()
  3913. datetime = color.datetime
  3914. colors = ColorMachine.query.filter_by(datetime=datetime).all()
  3915. except Exception as e:
  3916. pass
  3917. # 輸送帶2
  3918. try:
  3919. belt2 = ConveyorBelt2.query.order_by(text('datetime desc')).first()
  3920. datetime = belt2.datetime
  3921. belts2 = ConveyorBelt2.query.filter_by(datetime=datetime).all()
  3922. except Exception as e:
  3923. pass
  3924. # 去皮機
  3925. try:
  3926. peeling = PeelingMachine.query.order_by(
  3927. text('datetime desc')).first()
  3928. datetime = peeling.datetime
  3929. peelings = PeelingMachine.query.filter_by(datetime=datetime).all()
  3930. except Exception as e:
  3931. pass
  3932. # 輸送帶3
  3933. try:
  3934. belt3 = ConveyorBelt3.query.order_by(text('datetime desc')).first()
  3935. datetime = belt3.datetime
  3936. belts3 = ConveyorBelt3.query.filter_by(datetime=datetime).all()
  3937. except Exception as e:
  3938. pass
  3939. return render_template('cargo1_schedule.html', params=locals())
  3940. else:
  3941. current_time = dt.now()
  3942. # 循環從前端提交過來的資料
  3943. for i in request.form:
  3944. # 進豆閥
  3945. if i[:10] == 'inletValve':
  3946. if i[:19] == 'inletValve_duration':
  3947. bean = BeanValve()
  3948. bean.duration = request.form[i]
  3949. elif i[:18] == 'inletValve_from_hr':
  3950. iv_from_hr = request.form[i]
  3951. elif i[:19] == 'inletValve_from_min':
  3952. iv_from_min = request.form[i]
  3953. bean.start = iv_from_hr + ":" + iv_from_min
  3954. elif i[:16] == 'inletValve_to_hr':
  3955. iv_to_hr = request.form[i]
  3956. elif i[:17] == 'inletValve_to_min':
  3957. iv_to_min = request.form[i]
  3958. bean.end = iv_to_hr + ":" + iv_to_min
  3959. bean.datetime = current_time
  3960. db.session.add(bean)
  3961. db.session.commit()
  3962. # 清洗機
  3963. elif i[:4] == 'wash':
  3964. if i[:13] == 'wash_duration':
  3965. wash = WashMachine()
  3966. wash.duration = request.form[i]
  3967. elif i[:12] == 'wash_from_hr':
  3968. wash_from_hr = request.form[i]
  3969. elif i[:13] == 'wash_from_min':
  3970. wash_from_min = request.form[i]
  3971. wash.start = wash_from_hr + ":" + wash_from_min
  3972. elif i[:10] == 'wash_to_hr':
  3973. wash_to_hr = request.form[i]
  3974. elif i[:11] == 'wash_to_min':
  3975. wash_to_min = request.form[i]
  3976. wash.end = wash_to_hr + ":" + wash_to_min
  3977. wash.datetime = current_time
  3978. db.session.add(wash)
  3979. db.session.commit()
  3980. # 輸送帶1
  3981. elif i[:5] == 'belt1':
  3982. if i[:14] == 'belt1_duration':
  3983. belt1 = ConveyorBelt1()
  3984. belt1.duration = request.form[i]
  3985. elif i[:13] == 'belt1_from_hr':
  3986. belt1_from_hr = request.form[i]
  3987. elif i[:14] == 'belt1_from_min':
  3988. belt1_from_min = request.form[i]
  3989. belt1.start = belt1_from_hr + ":" + belt1_from_min
  3990. elif i[:11] == 'belt1_to_hr':
  3991. belt1_to_hr = request.form[i]
  3992. elif i[:12] == 'belt1_to_min':
  3993. belt1_to_min = request.form[i]
  3994. belt1.end = belt1_to_hr + ":" + belt1_to_min
  3995. belt1.datetime = current_time
  3996. db.session.add(belt1)
  3997. db.session.commit()
  3998. # 消毒
  3999. elif i[:9] == 'disinfect':
  4000. if i[:18] == 'disinfect_duration':
  4001. disinfect = Cargo1Disinfect()
  4002. disinfect.duration = request.form[i]
  4003. elif i[:17] == 'disinfect_from_hr':
  4004. di_from_hr = request.form[i]
  4005. elif i[:18] == 'disinfect_from_min':
  4006. di_from_min = request.form[i]
  4007. disinfect.start = di_from_hr + ":" + di_from_min
  4008. elif i[:15] == 'disinfect_to_hr':
  4009. di_to_hr = request.form[i]
  4010. elif i[:16] == 'disinfect_to_min':
  4011. di_to_min = request.form[i]
  4012. disinfect.end = di_to_hr + ":" + di_to_min
  4013. disinfect.datetime = current_time
  4014. db.session.add(disinfect)
  4015. db.session.commit()
  4016. # 色選機
  4017. elif i[:5] == 'color':
  4018. if i[:14] == 'color_duration':
  4019. color = ColorMachine()
  4020. color.duration = request.form[i]
  4021. elif i[:13] == 'color_from_hr':
  4022. color_from_hr = request.form[i]
  4023. elif i[:14] == 'color_from_min':
  4024. color_from_min = request.form[i]
  4025. color.start = color_from_hr + ":" + color_from_min
  4026. elif i[:11] == 'color_to_hr':
  4027. color_to_hr = request.form[i]
  4028. elif i[:12] == 'color_to_min':
  4029. color_to_min = request.form[i]
  4030. color.end = color_to_hr + ":" + color_to_min
  4031. color.datetime = current_time
  4032. db.session.add(color)
  4033. db.session.commit()
  4034. # 輸送帶2
  4035. elif i[:5] == 'belt2':
  4036. if i[:14] == 'belt2_duration':
  4037. belt2 = ConveyorBelt2()
  4038. belt2.duration = request.form[i]
  4039. elif i[:13] == 'belt2_from_hr':
  4040. belt2_from_hr = request.form[i]
  4041. elif i[:14] == 'belt2_from_min':
  4042. belt2_from_min = request.form[i]
  4043. belt2.start = belt2_from_hr + ":" + belt2_from_min
  4044. elif i[:11] == 'belt2_to_hr':
  4045. belt2_to_hr = request.form[i]
  4046. elif i[:12] == 'belt2_to_min':
  4047. belt2_to_min = request.form[i]
  4048. belt2.end = belt2_to_hr + ":" + belt2_to_min
  4049. belt2.datetime = current_time
  4050. db.session.add(belt2)
  4051. db.session.commit()
  4052. # 去皮機
  4053. elif i[:6] == 'peeled':
  4054. if i[:15] == 'peeled_duration':
  4055. peeling = PeelingMachine()
  4056. peeling.duration = request.form[i]
  4057. elif i[:14] == 'peeled_from_hr':
  4058. peeled_from_hr = request.form[i]
  4059. elif i[:15] == 'peeled_from_min':
  4060. peeled_from_min = request.form[i]
  4061. peeling.start = peeled_from_hr + ":" + peeled_from_min
  4062. elif i[:12] == 'peeled_to_hr':
  4063. peeled_to_hr = request.form[i]
  4064. elif i[:13] == 'peeled_to_min':
  4065. peeled_to_min = request.form[i]
  4066. peeling.end = peeled_to_hr + ":" + peeled_to_min
  4067. peeling.datetime = current_time
  4068. db.session.add(peeling)
  4069. db.session.commit()
  4070. # 輸送帶3
  4071. elif i[:5] == 'belt3':
  4072. if i[:14] == 'belt3_duration':
  4073. belt3 = ConveyorBelt3()
  4074. belt3.duration = request.form[i]
  4075. elif i[:13] == 'belt3_from_hr':
  4076. belt3_from_hr = request.form[i]
  4077. elif i[:14] == 'belt3_from_min':
  4078. belt3_from_min = request.form[i]
  4079. belt3.start = belt3_from_hr + ":" + belt3_from_min
  4080. elif i[:11] == 'belt3_to_hr':
  4081. belt3_to_hr = request.form[i]
  4082. elif i[:12] == 'belt3_to_min':
  4083. belt3_to_min = request.form[i]
  4084. belt3.end = belt3_to_hr + ":" + belt3_to_min
  4085. belt3.datetime = current_time
  4086. db.session.add(belt3)
  4087. db.session.commit()
  4088. return render_template('cargo1.html', params=locals())
  4089. # 貨櫃2的訪問路徑
  4090. @main.route('/cargo2', methods=['GET', 'POST'])
  4091. def cargo2_views():
  4092. username = session['uname']
  4093. if request.method == 'GET':
  4094. return render_template('cargo2.html', params=locals())
  4095. else:
  4096. pass
  4097. # 貨櫃2排程的發酵槽訪問路徑
  4098. @main.route('/cargo2_schedule_tanks', methods=['GET', 'POST'])
  4099. def cargo2_schedule_tanks_views():
  4100. username = session['uname']
  4101. if request.method == 'GET':
  4102. return render_template('cargo2_schedule_tanks.html', params=locals())
  4103. else:
  4104. pass
  4105. # 貨櫃2排程發酵槽的清單訪問路徑
  4106. @main.route('/cargo2_schedule/<tid>', methods=['GET', 'POST'])
  4107. def cargo2_schedule_views(tid):
  4108. username = session['uname']
  4109. if request.method == 'GET':
  4110. # 將已儲存的排程資料傳給前端
  4111. # 打菌
  4112. try:
  4113. bacteria = Bacteria.query.filter_by(tank_num=int(
  4114. tid)).order_by(text('datetime desc')).first()
  4115. datetime = bacteria.datetime
  4116. bacterias = Bacteria.query.filter_by(datetime=datetime).all()
  4117. except Exception as e:
  4118. pass
  4119. # 消毒
  4120. try:
  4121. disinfect = Cargo2Disinfect.query.filter_by(
  4122. tank_num=int(tid)).order_by(text('datetime desc')).first()
  4123. datetime = disinfect.datetime
  4124. disinfects = Cargo2Disinfect.query.filter_by(
  4125. datetime=datetime).all()
  4126. except Exception as e:
  4127. pass
  4128. # 加熱
  4129. try:
  4130. heating = Heating.query.filter_by(tank_num=int(
  4131. tid)).order_by(text('datetime desc')).first()
  4132. datetime = heating.datetime
  4133. heatings = Heating.query.filter_by(datetime=datetime).all()
  4134. except Exception as e:
  4135. pass
  4136. # 攪拌
  4137. try:
  4138. stir = Stir.query.filter_by(tank_num=int(tid)).order_by(
  4139. text('datetime desc')).first()
  4140. datetime = stir.datetime
  4141. stirs = Stir.query.filter_by(datetime=datetime).all()
  4142. except Exception as e:
  4143. pass
  4144. # 注水
  4145. try:
  4146. water = WaterInjection.query.filter_by(
  4147. tank_num=int(tid)).order_by(text('datetime desc')).first()
  4148. datetime = water.datetime
  4149. waters = WaterInjection.query.filter_by(datetime=datetime).all()
  4150. except Exception as e:
  4151. pass
  4152. # 開上閥
  4153. try:
  4154. top = TopValve.query.filter_by(tank_num=int(
  4155. tid)).order_by(text('datetime desc')).first()
  4156. datetime = top.datetime
  4157. tops = TopValve.query.filter_by(datetime=datetime).all()
  4158. except Exception as e:
  4159. pass
  4160. # 開下閥
  4161. try:
  4162. bottom = BottomValve.query.filter_by(tank_num=int(
  4163. tid)).order_by(text('datetime desc')).first()
  4164. datetime = bottom.datetime
  4165. bottoms = BottomValve.query.filter_by(datetime=datetime).all()
  4166. except Exception as e:
  4167. pass
  4168. # 溫度
  4169. try:
  4170. tem = Temperature.query.filter_by(tank_num=int(
  4171. tid)).order_by(text('datetime desc')).first()
  4172. datetime = tem.datetime
  4173. tems = Temperature.query.filter_by(datetime=datetime).all()
  4174. except Exception as e:
  4175. pass
  4176. return render_template('cargo2_schedule.html', params=locals())
  4177. else:
  4178. current_time = dt.now()
  4179. # 循環從前端提交過來的資料
  4180. for i in request.form:
  4181. # 打菌
  4182. if i[:8] == 'bacteria':
  4183. if i[:17] == 'bacteria_duration':
  4184. bacteria = Bacteria()
  4185. bacteria.duration = request.form[i]
  4186. elif i[:16] == 'bacteria_from_hr':
  4187. bac_from_hr = request.form[i]
  4188. elif i[:17] == 'bacteria_from_min':
  4189. bac_from_min = request.form[i]
  4190. bacteria.start = bac_from_hr + ":" + bac_from_min
  4191. elif i[:14] == 'bacteria_to_hr':
  4192. bac_to_hr = request.form[i]
  4193. elif i[:15] == 'bacteria_to_min':
  4194. bac_to_min = request.form[i]
  4195. bacteria.end = bac_to_hr + ":" + bac_to_min
  4196. bacteria.datetime = current_time
  4197. bacteria.tank_num = int(tid)
  4198. db.session.add(bacteria)
  4199. db.session.commit()
  4200. # 消毒
  4201. elif i[:9] == 'disinfect':
  4202. if i[:18] == 'disinfect_duration':
  4203. disinfect = Cargo2Disinfect()
  4204. disinfect.duration = request.form[i]
  4205. elif i[:17] == 'disinfect_from_hr':
  4206. di_from_hr = request.form[i]
  4207. elif i[:18] == 'disinfect_from_min':
  4208. di_from_min = request.form[i]
  4209. disinfect.start = di_from_hr + ":" + di_from_min
  4210. elif i[:15] == 'disinfect_to_hr':
  4211. di_to_hr = request.form[i]
  4212. elif i[:16] == 'disinfect_to_min':
  4213. di_to_min = request.form[i]
  4214. disinfect.end = di_to_hr + ":" + di_to_min
  4215. disinfect.datetime = current_time
  4216. disinfect.tank_num = int(tid)
  4217. db.session.add(disinfect)
  4218. db.session.commit()
  4219. # 加熱
  4220. elif i[:7] == 'heating':
  4221. if i[:16] == 'heating_duration':
  4222. heating = Heating()
  4223. heating.duration = request.form[i]
  4224. elif i[:15] == 'heating_from_hr':
  4225. heat_from_hr = request.form[i]
  4226. elif i[:16] == 'heating_from_min':
  4227. heat_from_min = request.form[i]
  4228. heating.start = heat_from_hr + ":" + heat_from_min
  4229. elif i[:13] == 'heating_to_hr':
  4230. heat_to_hr = request.form[i]
  4231. elif i[:14] == 'heating_to_min':
  4232. heat_to_min = request.form[i]
  4233. heating.end = heat_to_hr + ":" + heat_to_min
  4234. heating.datetime = current_time
  4235. heating.tank_num = int(tid)
  4236. db.session.add(heating)
  4237. db.session.commit()
  4238. # 攪拌
  4239. elif i[:4] == 'stir':
  4240. if i[:13] == 'stir_duration':
  4241. stir = Stir()
  4242. stir.duration = request.form[i]
  4243. elif i[:12] == 'stir_from_hr':
  4244. stir_from_hr = request.form[i]
  4245. elif i[:13] == 'stir_from_min':
  4246. stir_from_min = request.form[i]
  4247. stir.start = stir_from_hr + ":" + stir_from_min
  4248. elif i[:10] == 'stir_to_hr':
  4249. stir_to_hr = request.form[i]
  4250. elif i[:11] == 'stir_to_min':
  4251. stir_to_min = request.form[i]
  4252. stir.end = stir_to_hr + ":" + stir_to_min
  4253. stir.datetime = current_time
  4254. stir.tank_num = int(tid)
  4255. db.session.add(stir)
  4256. db.session.commit()
  4257. # 注水
  4258. elif i[:5] == 'water':
  4259. if i[:14] == 'water_duration':
  4260. water = WaterInjection()
  4261. water.duration = request.form[i]
  4262. elif i[:13] == 'water_from_hr':
  4263. water_from_hr = request.form[i]
  4264. elif i[:14] == 'water_from_min':
  4265. water_from_min = request.form[i]
  4266. water.start = water_from_hr + ":" + water_from_min
  4267. elif i[:11] == 'water_to_hr':
  4268. water_to_hr = request.form[i]
  4269. elif i[:12] == 'water_to_min':
  4270. water_to_min = request.form[i]
  4271. water.end = water_to_hr + ":" + water_to_min
  4272. water.datetime = current_time
  4273. water.tank_num = int(tid)
  4274. db.session.add(water)
  4275. db.session.commit()
  4276. # 開上閥
  4277. elif i[:9] == 'highValve':
  4278. if i[:18] == 'highValve_duration':
  4279. top = TopValve()
  4280. top.duration = request.form[i]
  4281. elif i[:17] == 'highValve_from_hr':
  4282. h_valve_from_hr = request.form[i]
  4283. elif i[:18] == 'highValve_from_min':
  4284. h_valve_from_min = request.form[i]
  4285. top.start = h_valve_from_hr + ":" + h_valve_from_min
  4286. elif i[:15] == 'highValve_to_hr':
  4287. h_valve_to_hr = request.form[i]
  4288. elif i[:16] == 'highValve_to_min':
  4289. h_valve_to_min = request.form[i]
  4290. top.end = h_valve_to_hr + ":" + h_valve_to_min
  4291. top.datetime = current_time
  4292. top.tank_num = int(tid)
  4293. db.session.add(top)
  4294. db.session.commit()
  4295. # 開下閥
  4296. elif i[:8] == 'lowValve':
  4297. if i[:17] == 'lowValve_duration':
  4298. bottom = BottomValve()
  4299. bottom.duration = request.form[i]
  4300. elif i[:16] == 'lowValve_from_hr':
  4301. l_valve_from_hr = request.form[i]
  4302. elif i[:17] == 'lowValve_from_min':
  4303. l_valve_from_min = request.form[i]
  4304. bottom.start = l_valve_from_hr + ":" + l_valve_from_min
  4305. elif i[:14] == 'lowValve_to_hr':
  4306. l_valve_to_hr = request.form[i]
  4307. elif i[:15] == 'lowValve_to_min':
  4308. l_valve_to_min = request.form[i]
  4309. bottom.end = l_valve_to_hr + ":" + l_valve_to_min
  4310. bottom.datetime = current_time
  4311. bottom.tank_num = int(tid)
  4312. db.session.add(bottom)
  4313. db.session.commit()
  4314. # 溫度
  4315. elif i[:3] == 'tem':
  4316. if i[:12] == 'tem_duration':
  4317. tem = Temperature()
  4318. tem.duration = request.form[i]
  4319. elif i[:11] == 'tem_from_hr':
  4320. tem_from_hr = request.form[i]
  4321. elif i[:12] == 'tem_from_min':
  4322. tem_from_min = request.form[i]
  4323. tem.start = tem_from_hr + ":" + tem_from_min
  4324. elif i[:9] == 'tem_to_hr':
  4325. tem_to_hr = request.form[i]
  4326. elif i[:10] == 'tem_to_min':
  4327. tem_to_min = request.form[i]
  4328. tem.end = tem_to_hr + ":" + tem_to_min
  4329. tem.datetime = current_time
  4330. tem.tank_num = int(tid)
  4331. db.session.add(tem)
  4332. db.session.commit()
  4333. return render_template('cargo2.html', params=locals())
  4334. # 貨櫃2感測器的發酵槽訪問路徑
  4335. @main.route('/cargo2_sensor_tanks', methods=['GET', 'POST'])
  4336. def cargo2_sensor_tanks_views():
  4337. username = session['uname']
  4338. if request.method == 'GET':
  4339. return render_template('cargo2_sensor_tanks.html', params=locals())
  4340. else:
  4341. pass
  4342. # 貨櫃2感測器發酵槽的清單訪問路徑
  4343. @main.route('/cargo2_sensor/<tid>', methods=['GET', 'POST'])
  4344. def s_tank_views(tid):
  4345. # 判斷用戶是否已關閉瀏覽器或登出後,在訪問這個route,如果沒有session就回登入頁
  4346. try:
  4347. username = session['uname']
  4348. except KeyError:
  4349. return redirect('/')
  4350. if request.method == 'GET':
  4351. # Rita 網頁端 params.tank_tem.tem
  4352. # Coffee/CoffeeProject/app/templates/cargo2_sensor.html
  4353. tank_tem = TankTemSensor.query.filter_by(tank_num=int(tid)).order_by(text('datetime desc')).first()
  4354. tank_ph = TankPHSensor.query.filter_by(tank_num=int(tid)).order_by(text('datetime desc')).first()
  4355. tank_ec = TankECSensor.query.filter_by(tank_num=int(tid)).order_by(text('datetime desc')).first()
  4356. tank_sonic = TankSonicSensor.query.filter_by(tank_num=int(tid)).order_by(text('datetime desc')).first()
  4357. return render_template('cargo2_sensor.html', params=locals())
  4358. else:
  4359. pass
  4360. # 貨櫃2感測器發酵槽歷史數據的訪問路徑
  4361. @main.route('/history_data', methods=['GET', 'POST'])
  4362. def history_data_views():
  4363. if request.method == 'GET':
  4364. # json
  4365. # tid:tid_D1
  4366. # sensor_name:soil_Temp
  4367. # avg:1
  4368. # max:1
  4369. # min:1
  4370. # time-interval:month
  4371. # date-start:2021-07-01
  4372. # date-end:2021-07-28
  4373. info = request.args.to_dict()
  4374. evt = info['evt']
  4375. avg = int(info['avg'])
  4376. max = int(info['max'])
  4377. min = int(info['min'])
  4378. time_interval = info['time-interval']
  4379. print(info)
  4380. # Rita {'evt': '1-溫濕度', 'avg': '1', 'max': '0', 'min': '0', 'time-interval': 'month',
  4381. # 'date-start': '2021-05-04', 'date-end': '2021-05-18'} {'all': [], 'avg': []}
  4382. # print(info)
  4383. # {'evt': '1-溫濕度', 'avg': '1', 'max': '0', 'min': '0', 'time-interval': 'hour', 'date-start': '2021-01-01', 'date-end': '2021-07-27'}
  4384. # print(D)
  4385. # {'all': [['2021-Feb-19_16:46:10', '24.4'], ['2021-Feb-19_16:46:20', '24.4'], ['2021-Feb-19_16:46:30', '24.4'], ['2021-Feb-19_16:46:41', '24.4'], ['2021-Feb-19_16:46:50', '24.4'], ['2021-Feb-19_16:47:00', '24.4'], ['2021-Feb-19_16:47:10', '24.4'], ['2021-Feb-19_16:47:20', '24.4'], ['2021-Feb-19_16:47:30', '24.4'], ['2021-Feb-19_16:47:40', '24.5'], ['2021-Feb-19_16:47:50', '24.5'], ['2021-Feb-19_16:48:00', '24.5'], ['2021-Feb-19_16:48:10', '24.5'], ['2021-Feb-19_16:48:20', '24.5'], ['2021-Feb-19_16:48:29', '24.5'], ['2021-Feb-19_16:48:39', '24.5'], ['2021-Feb-19_16:48:50', '24.5'], ['2021-Feb-19_16:49:00', '24.5'], ['2021-Feb-19_16:49:10', '24.5'], ['2021-Feb-19_16:49:19', '24.5'], ['2021-Feb-19_16:49:29', '24.5'], ['2021-Feb-19_16:49:39', '24.5'], ['2021-Feb-19_16:49:49', '24.5'], ['2021-Feb-19_16:49:59', '24.5'], ['2021-Feb-19_16:50:10', '24.5'], ['2021-Feb-19_16:50:19', '24.5'], ['2021-Feb-19_16:50:29', '24.5'], ['2021-Feb-19_16:50:39', '24.5'], ['2021-Feb-19_16:50:49', '24.5'], ['2021-Feb-19_16:50:59', '24.5'], ['2021-Feb-19_18:07:10', '25.3'], ['2021-Feb-19_18:07:20', '25.3'], ['2021-Feb-19_18:07:30', '25.3'], ['2021-Feb-19_18:07:40', '25.3'], ['2021-Feb-19_18:07:49', '25.3'], ['2021-Feb-19_18:07:59', '25.3'], ['2021-Feb-19_18:08:10', '25.3'], ['2021-Feb-19_18:08:20', '25.3'], ['2021-Feb-19_18:08:30', '25.2'], ['2021-Feb-19_18:08:39', '25.2'], ['2021-Feb-19_18:08:49', '25.2']],
  4386. # 'avg': [['2021-02-19 16:00:00', '24.5'], ['2021-02-19 18:00:00', '25.3']]}
  4387. # 192.168.50.65 - - [27/Jul/2021 11:57:23] "GET /history_data?evt=1-溫濕度&avg=1&max=0&min=0&time-interval=hour&date-start=2021-01-01&date-end=2021-07-27 HTTP/1.1" 200 -
  4388. date_start = info['date-start']
  4389. date_end_list = info['date-end'].split('-') # 先把 'date-end' 的 '2021-05-18' 拆成 ['2021', '05', '18']
  4390. date_end_list[2] = str(int(date_end_list[2]) + 1) # 18 + 1 = 19 再轉成 str → '19'
  4391. date_end = '-'.join(date_end_list) # '2021-05-19'
  4392. tank_num = evt.split('-')[0] # 1
  4393. sensor = evt.split('-')[1] # 溫濕度
  4394. L = []
  4395. D = {}
  4396. maxData = []
  4397. minData = []
  4398. avgData = []
  4399. def sensorData(data_name): # data_name = "tem"
  4400. def dataResample(time_interval, how, data_name): # dataResample('month', how???, "tem") # 下方 "hour"
  4401. interval = 0
  4402. # 設置resample的第一個參數,要按照逐月、逐日或逐時
  4403. if time_interval == 'month':
  4404. interval = "M" # interval = "M"
  4405. elif time_interval == 'day':
  4406. interval = "D"
  4407. elif time_interval == 'hour':
  4408. interval = "H"
  4409. if how == "max":
  4410. # data = df.resample(interval, how={data_name:how})
  4411. # 最新版的pandas
  4412. data = df.resample(interval).max() # 取樣頻率 'H'
  4413. # 刪除有 NaN 的列, any 任何一欄位為空則刪 (all 全部欄位為空值才刪)
  4414. data = data.dropna(axis=0, how='any')
  4415. if time_interval == ('hour' or 'day'):
  4416. data.index = pd.to_datetime(
  4417. data.index, format='%Y-%b-%d_%H:%M:%S') # 将数据类型转换为日期类型
  4418. for a in range(0, len(data.index)):
  4419. l = []
  4420. # 如果是逐時,才顯示小時分秒
  4421. if time_interval == 'hour':
  4422. l.append(str(data.index[a]))
  4423. else:
  4424. l.append(str(data.index[a]).split(' ')[0])
  4425. l.append(str(data[data_name][a]))
  4426. maxData.append(l)
  4427. D['max'] = maxData
  4428. elif how == "mean":
  4429. # data = df.resample(interval, how={data_name:how})
  4430. # 最新版的pandas
  4431. data = df.resample(interval).mean()
  4432. # 刪除具有NaN的值
  4433. data = data.dropna(axis=0, how='any')
  4434. # 將平均數取自小數1位
  4435. data = data.round({data_name: 1})
  4436. for a in range(0, len(data.index)):
  4437. l = []
  4438. # 如果是逐時,才顯示小時分秒
  4439. if time_interval == 'hour':
  4440. l.append(str(data.index[a]))
  4441. else:
  4442. l.append(str(data.index[a]).split(' ')[0])
  4443. l.append(str(data[data_name][a]))
  4444. avgData.append(l)
  4445. D['avg'] = avgData
  4446. elif how == "min":
  4447. # data = df.resample(interval, how={data_name:how})
  4448. # 最新版的pandas
  4449. data = df.resample(interval).min()
  4450. # 刪除具有NaN的值
  4451. data = data.dropna(axis=0, how='any')
  4452. for a in range(0, len(data.index)):
  4453. # print(data.index[a])
  4454. l = []
  4455. # 如果是逐時,才顯示小時分秒
  4456. if time_interval == 'hour':
  4457. l.append(str(data.index[a]))
  4458. else:
  4459. l.append(str(data.index[a]).split(' ')[0])
  4460. l.append(str(data[data_name][a]))
  4461. minData.append(l)
  4462. D['min'] = minData
  4463. df = pd.DataFrame(L, columns=["datetime", data_name]) # 指定欄標籤名稱 為 ["datetime", "tem"]
  4464. df['datetime'] = pd.to_datetime(
  4465. df['datetime'], format='%Y-%b-%d_%H:%M:%S') # 将数据类型转换为日期类型
  4466. # print(df['datetime']) # 0 2021-02-19 16:46:10
  4467. # 1 2021-02-19 16:46:20
  4468. # 2 2021-02-19 16:46:30
  4469. # 3 2021-02-19 16:46:41
  4470. df[data_name] = df[data_name].astype(float)
  4471. df = df.set_index('datetime') # 将datetime设置为index
  4472. # print(df) # datetime tem
  4473. # 2021-02-19 16:46:10 24.4
  4474. # 2021-02-19 16:46:20 24.4
  4475. if time_interval == 'month':
  4476. if max:
  4477. dataResample(time_interval, "max", data_name)
  4478. if avg:
  4479. dataResample(time_interval, "mean", data_name)
  4480. if min:
  4481. dataResample(time_interval, "min", data_name)
  4482. elif time_interval == 'day':
  4483. if max:
  4484. dataResample(time_interval, "max", data_name)
  4485. if avg:
  4486. dataResample(time_interval, "mean", data_name)
  4487. if min:
  4488. dataResample(time_interval, "min", data_name)
  4489. elif time_interval == 'hour':
  4490. if max:
  4491. dataResample(time_interval, "max", data_name)
  4492. if avg:
  4493. dataResample(time_interval, "mean", data_name)
  4494. if min:
  4495. dataResample(time_interval, "min", data_name)
  4496. if sensor == '溫濕度':
  4497. # tank_tem = TankTemSensor.query.filter_by(tank_num=int(tank_num)).order_by(text('datetime desc')).all()
  4498. # [7/27 Benson] tank_tem = TankTemSensor.query.filter_by(tank_num=int(tank_num)).filter(TankTemSensor.datetime.between(date_start, date_end)).all()
  4499. tank_tem = dry_tank_SHT11.query.filter(dry_tank_SHT11.datetime.between(date_start, date_end)).all()
  4500. # .filter_by(tank_num="D"+tank_num)
  4501. for tem in tank_tem:
  4502. l = [] # l = []
  4503. time = dt.strftime(tem.datetime, '%Y-%b-%d_%H:%M:%S') # time = '2021-Feb-19_16:46:10' 轉換時間格式
  4504. # time = dt.strftime(tem.datetime, '%Y-%m-%d')
  4505. l.append(time) # l = ['2021-Feb-19_16:46:10']
  4506. #l.append(tem.tem) # l = ['2021-Feb-19_16:46:10', '24.4']
  4507. l.append(tem.SHT11_Temp)
  4508. L.append(l) # L = [['2021-Feb-19_16:46:10', '24.4']]
  4509. # print(L)
  4510. D['all'] = L # D = {'all': [['2021-Feb-19_16:46:10', '24.4']]} # all 所有符合數據
  4511. sensorData("tem")
  4512. elif sensor == '酸鹼值':
  4513. tank_ph = TankPHSensor.query.filter_by(tank_num=int(tank_num)).filter(
  4514. TankPHSensor.datetime.between(date_start, date_end)).all()
  4515. for ph in tank_ph:
  4516. l = []
  4517. time = dt.strftime(ph.datetime, '%Y-%b-%d_%H:%M:%S')
  4518. # time = dt.strftime(tem.datetime, '%Y-%m-%d')
  4519. l.append(time)
  4520. l.append(ph.ph)
  4521. L.append(l)
  4522. # print(L)
  4523. D['all'] = L
  4524. sensorData("ph")
  4525. elif sensor == 'EC值':
  4526. tank_ec = TankECSensor.query.filter_by(tank_num=int(tank_num)).filter(
  4527. TankECSensor.datetime.between(date_start, date_end)).all()
  4528. for ec in tank_ec:
  4529. l = []
  4530. time = dt.strftime(ec.datetime, '%Y-%b-%d_%H:%M:%S')
  4531. # time = dt.strftime(tem.datetime, '%Y-%m-%d')
  4532. l.append(time)
  4533. l.append(ec.ec)
  4534. L.append(l)
  4535. # print(L)
  4536. D['all'] = L
  4537. sensorData("ec")
  4538. elif sensor == '超音波':
  4539. tank_sonic = TankSonicSensor.query.filter_by(tank_num=int(tank_num)).filter(
  4540. TankSonicSensor.datetime.between(date_start, date_end)).all()
  4541. for sonic in tank_sonic:
  4542. l = []
  4543. time = dt.strftime(sonic.datetime, '%Y-%b-%d_%H:%M:%S')
  4544. # time = dt.strftime(tem.datetime, '%Y-%m-%d')
  4545. l.append(time)
  4546. l.append(sonic.sonic)
  4547. L.append(l)
  4548. # print(L)
  4549. D['all'] = L
  4550. sensorData("sonic")
  4551. print(D)
  4552. return json.dumps(D)
  4553. else:
  4554. pass
  4555. # Rita 改寫
  4556. @main.route('/history_data_new', methods=['GET', 'POST'])
  4557. def history_data_new_views():
  4558. if request.method == 'GET':
  4559. # json
  4560. # tid:tid_D1
  4561. # sensor_name:soil_Temp
  4562. # avg:1
  4563. # max:1
  4564. # min:1
  4565. # time-interval:month
  4566. # date-start:2021-07-01
  4567. # date-end:2021-07-28
  4568. info = request.args.to_dict()
  4569. # evt = info['evt']
  4570. tid = info['tid']
  4571. sensor_name = info['sensor_name']
  4572. avg = int(info['avg'])
  4573. max = int(info['max'])
  4574. min = int(info['min'])
  4575. time_interval = info['time-interval']
  4576. print("info:", info)
  4577. # Rita {'evt': '1-溫濕度', 'avg': '1', 'max': '0', 'min': '0', 'time-interval': 'month',
  4578. # 'date-start': '2021-05-04', 'date-end': '2021-05-18'} {'all': [], 'avg': []}
  4579. # print(info)
  4580. # {'evt': '1-溫濕度', 'avg': '1', 'max': '0', 'min': '0', 'time-interval': 'hour', 'date-start': '2021-01-01', 'date-end': '2021-07-27'}
  4581. # print(D)
  4582. # {'all': [['2021-Feb-19_16:46:10', '24.4'], ['2021-Feb-19_16:46:20', '24.4'], ['2021-Feb-19_16:46:30', '24.4'], ['2021-Feb-19_16:46:41', '24.4'], ['2021-Feb-19_16:46:50', '24.4'], ['2021-Feb-19_16:47:00', '24.4'], ['2021-Feb-19_16:47:10', '24.4'], ['2021-Feb-19_16:47:20', '24.4'], ['2021-Feb-19_16:47:30', '24.4'], ['2021-Feb-19_16:47:40', '24.5'], ['2021-Feb-19_16:47:50', '24.5'], ['2021-Feb-19_16:48:00', '24.5'], ['2021-Feb-19_16:48:10', '24.5'], ['2021-Feb-19_16:48:20', '24.5'], ['2021-Feb-19_16:48:29', '24.5'], ['2021-Feb-19_16:48:39', '24.5'], ['2021-Feb-19_16:48:50', '24.5'], ['2021-Feb-19_16:49:00', '24.5'], ['2021-Feb-19_16:49:10', '24.5'], ['2021-Feb-19_16:49:19', '24.5'], ['2021-Feb-19_16:49:29', '24.5'], ['2021-Feb-19_16:49:39', '24.5'], ['2021-Feb-19_16:49:49', '24.5'], ['2021-Feb-19_16:49:59', '24.5'], ['2021-Feb-19_16:50:10', '24.5'], ['2021-Feb-19_16:50:19', '24.5'], ['2021-Feb-19_16:50:29', '24.5'], ['2021-Feb-19_16:50:39', '24.5'], ['2021-Feb-19_16:50:49', '24.5'], ['2021-Feb-19_16:50:59', '24.5'], ['2021-Feb-19_18:07:10', '25.3'], ['2021-Feb-19_18:07:20', '25.3'], ['2021-Feb-19_18:07:30', '25.3'], ['2021-Feb-19_18:07:40', '25.3'], ['2021-Feb-19_18:07:49', '25.3'], ['2021-Feb-19_18:07:59', '25.3'], ['2021-Feb-19_18:08:10', '25.3'], ['2021-Feb-19_18:08:20', '25.3'], ['2021-Feb-19_18:08:30', '25.2'], ['2021-Feb-19_18:08:39', '25.2'], ['2021-Feb-19_18:08:49', '25.2']],
  4583. # 'avg': [['2021-02-19 16:00:00', '24.5'], ['2021-02-19 18:00:00', '25.3']]}
  4584. # 192.168.50.65 - - [27/Jul/2021 11:57:23] "GET /history_data?evt=1-溫濕度&avg=1&max=0&min=0&time-interval=hour&date-start=2021-01-01&date-end=2021-07-27 HTTP/1.1" 200 -
  4585. date_start = info['date-start']
  4586. date_end_list = info['date-end'].split('-') # 先把 'date-end' 的 '2021-05-18' 拆成 ['2021', '05', '18']
  4587. date_end_list[2] = str(int(date_end_list[2]) + 1) # 18 + 1 = 19 再轉成 str → '19'
  4588. date_end = '-'.join(date_end_list) # '2021-05-19'
  4589. # tank_num = evt.split('-')[0] # 1
  4590. # sensor = evt.split('-')[1] # 溫濕度
  4591. tank_num = tid # D1
  4592. sensor = sensor_name # soil_Temp
  4593. L = []
  4594. D = {}
  4595. maxData = []
  4596. minData = []
  4597. avgData = []
  4598. def sensorData(data_name): # data_name = "tem"
  4599. def dataResample(time_interval, how, data_name): # dataResample('month', how???, "tem") # 下方 "hour"
  4600. interval = 0
  4601. # 設置resample的第一個參數,要按照逐月、逐日或逐時
  4602. if time_interval == 'month':
  4603. interval = "M" # interval = "M"
  4604. elif time_interval == 'day':
  4605. interval = "D"
  4606. elif time_interval == 'hour':
  4607. interval = "H"
  4608. if how == "max":
  4609. # data = df.resample(interval, how={data_name:how})
  4610. # 最新版的pandas
  4611. data = df.resample(interval).max() # 取樣頻率 'H'
  4612. # 刪除有 NaN 的列, any 任何一欄位為空則刪 (all 全部欄位為空值才刪)
  4613. data = data.dropna(axis=0, how='any')
  4614. if time_interval == ('hour' or 'day'):
  4615. data.index = pd.to_datetime(
  4616. data.index, format='%Y-%b-%d_%H:%M:%S') # 将数据类型转换为日期类型
  4617. for a in range(0, len(data.index)):
  4618. l = []
  4619. # 如果是逐時,才顯示小時分秒
  4620. if time_interval == 'hour':
  4621. l.append(str(data.index[a]))
  4622. else:
  4623. l.append(str(data.index[a]).split(' ')[0])
  4624. l.append(str(data[data_name][a]))
  4625. maxData.append(l)
  4626. D['max'] = maxData
  4627. elif how == "mean":
  4628. # data = df.resample(interval, how={data_name:how})
  4629. # 最新版的pandas
  4630. data = df.resample(interval).mean()
  4631. # 刪除具有NaN的值
  4632. data = data.dropna(axis=0, how='any')
  4633. # 將平均數取自小數1位
  4634. data = data.round({data_name: 1})
  4635. for a in range(0, len(data.index)):
  4636. l = []
  4637. # 如果是逐時,才顯示小時分秒
  4638. if time_interval == 'hour':
  4639. l.append(str(data.index[a]))
  4640. else:
  4641. l.append(str(data.index[a]).split(' ')[0])
  4642. l.append(str(data[data_name][a]))
  4643. avgData.append(l)
  4644. D['avg'] = avgData
  4645. elif how == "min":
  4646. # data = df.resample(interval, how={data_name:how})
  4647. # 最新版的pandas
  4648. data = df.resample(interval).min()
  4649. # 刪除具有NaN的值
  4650. data = data.dropna(axis=0, how='any')
  4651. for a in range(0, len(data.index)):
  4652. # print(data.index[a])
  4653. l = []
  4654. # 如果是逐時,才顯示小時分秒
  4655. if time_interval == 'hour':
  4656. l.append(str(data.index[a]))
  4657. else:
  4658. l.append(str(data.index[a]).split(' ')[0])
  4659. l.append(str(data[data_name][a]))
  4660. minData.append(l)
  4661. D['min'] = minData
  4662. df = pd.DataFrame(L, columns=["datetime", data_name]) # 指定欄標籤名稱 為 ["datetime", "tem"]
  4663. df['datetime'] = pd.to_datetime(
  4664. df['datetime'], format='%Y-%b-%d_%H:%M:%S') # 将数据类型转换为日期类型
  4665. # print(df['datetime']) # 0 2021-02-19 16:46:10
  4666. # 1 2021-02-19 16:46:20
  4667. # 2 2021-02-19 16:46:30
  4668. # 3 2021-02-19 16:46:41
  4669. df[data_name] = df[data_name].astype(float)
  4670. df = df.set_index('datetime') # 将datetime设置为index
  4671. # print(df) # datetime tem
  4672. # 2021-02-19 16:46:10 24.4
  4673. # 2021-02-19 16:46:20 24.4
  4674. if time_interval == 'month':
  4675. if max:
  4676. dataResample(time_interval, "max", data_name)
  4677. if avg:
  4678. dataResample(time_interval, "mean", data_name)
  4679. if min:
  4680. dataResample(time_interval, "min", data_name)
  4681. elif time_interval == 'day':
  4682. if max:
  4683. dataResample(time_interval, "max", data_name)
  4684. if avg:
  4685. dataResample(time_interval, "mean", data_name)
  4686. if min:
  4687. dataResample(time_interval, "min", data_name)
  4688. elif time_interval == 'hour':
  4689. if max:
  4690. dataResample(time_interval, "max", data_name)
  4691. if avg:
  4692. dataResample(time_interval, "mean", data_name)
  4693. if min:
  4694. dataResample(time_interval, "min", data_name)
  4695. if tank_num[:2] == 'DI':
  4696. # 乾燥入料儲豆槽
  4697. if sensor == 'UltraSonic':
  4698. tank_UltraSonic = dry_input_sensor.query.filter_by(tank_num=tank_num).filter(dry_input_sensor.datetime.between(date_start, date_end)).all()
  4699. for sonic in tank_UltraSonic:
  4700. l = []
  4701. time = dt.strftime(sonic.datetime, '%Y-%b-%d_%H:%M:%S')
  4702. l.append(time)
  4703. l.append(sonic.UltraSonic)
  4704. L.append(l)
  4705. D['all'] = L
  4706. sensorData("UltraSonic")
  4707. elif tank_num[:2] == 'DO':
  4708. # 乾燥出料儲豆槽
  4709. if sensor == 'UltraSonic':
  4710. tank_UltraSonic = dry_output_sensor.query.filter_by(tank_num=tank_num).filter(dry_output_sensor.datetime.between(date_start, date_end)).all()
  4711. for sonic in tank_UltraSonic:
  4712. l = []
  4713. time = dt.strftime(sonic.datetime, '%Y-%b-%d_%H:%M:%S')
  4714. l.append(time)
  4715. l.append(sonic.UltraSonic)
  4716. L.append(l)
  4717. D['all'] = L
  4718. sensorData("UltraSonic")
  4719. elif tank_num[:1] == 'D':
  4720. # 乾燥槽
  4721. if sensor == 'SHT11_Temp':
  4722. tank_tem = dry_tank_SHT11.query.filter_by(tank_num=tank_num).filter(dry_tank_SHT11.datetime.between(date_start, date_end)).all()
  4723. for tem in tank_tem:
  4724. l = [] # l = []
  4725. time = dt.strftime(tem.datetime, '%Y-%b-%d_%H:%M:%S') # time = '2021-Feb-19_16:46:10' 轉換時間格式
  4726. # time = dt.strftime(tem.datetime, '%Y-%m-%d')
  4727. l.append(time) # l = ['2021-Feb-19_16:46:10']
  4728. #l.append(tem.tem) # l = ['2021-Feb-19_16:46:10', '24.4']
  4729. l.append(tem.SHT11_Temp)
  4730. L.append(l) # L = [['2021-Feb-19_16:46:10', '24.4']]
  4731. # print(L)
  4732. D['all'] = L # D = {'all': [['2021-Feb-19_16:46:10', '24.4']]} # all 所有符合數據
  4733. sensorData("SHT11_Temp")
  4734. elif sensor == 'SHT11_Humidity':
  4735. tank_hum = dry_tank_SHT11.query.filter_by(tank_num=tank_num).filter(dry_tank_SHT11.datetime.between(date_start, date_end)).all()
  4736. for hum in tank_hum:
  4737. l = []
  4738. time = dt.strftime(hum.datetime, '%Y-%b-%d_%H:%M:%S')
  4739. l.append(time)
  4740. l.append(hum.SHT11_Humidity)
  4741. L.append(l)
  4742. D['all'] = L
  4743. sensorData("SHT11_Humidity")
  4744. elif sensor == 'UltraSonic':
  4745. tank_UltraSonic = dry_tank_UltraSonic.query.filter_by(tank_num=tank_num).filter(dry_tank_UltraSonic.datetime.between(date_start, date_end)).all()
  4746. for sonic in tank_UltraSonic:
  4747. l = []
  4748. time = dt.strftime(sonic.datetime, '%Y-%b-%d_%H:%M:%S')
  4749. l.append(time)
  4750. l.append(sonic.UltraSonic)
  4751. L.append(l)
  4752. D['all'] = L
  4753. sensorData("UltraSonic")
  4754. elif sensor == 'PA':
  4755. tank_PA = dry_tank_PA.query.filter_by(tank_num=tank_num).filter(dry_tank_PA.datetime.between(date_start, date_end)).all()
  4756. for pa in tank_PA:
  4757. l = []
  4758. time = dt.strftime(pa.datetime, '%Y-%b-%d_%H:%M:%S')
  4759. l.append(time)
  4760. l.append(pa.PA)
  4761. L.append(l)
  4762. D['all'] = L
  4763. sensorData("PA")
  4764. elif sensor == 'soil_Temp':
  4765. tank_soiltemp = dry_tank_Soil.query.filter_by(tank_num=tank_num).filter(dry_tank_Soil.datetime.between(date_start, date_end)).all()
  4766. for temp in tank_soiltemp:
  4767. l = []
  4768. time = dt.strftime(temp.datetime, '%Y-%b-%d_%H:%M:%S')
  4769. l.append(time)
  4770. l.append(temp.soil_Temp)
  4771. L.append(l)
  4772. D['all'] = L
  4773. sensorData("soil_Temp")
  4774. elif sensor == 'soil_Humidity':
  4775. tank_soilHum = dry_tank_Soil.query.filter_by(tank_num=tank_num).filter(dry_tank_Soil.datetime.between(date_start, date_end)).all()
  4776. for hum in tank_soilHum:
  4777. l = []
  4778. time = dt.strftime(hum.datetime, '%Y-%b-%d_%H:%M:%S')
  4779. l.append(time)
  4780. l.append(hum.soil_EC)
  4781. L.append(l)
  4782. D['all'] = L
  4783. sensorData("soil_Humidity")
  4784. elif sensor == 'soil_EC':
  4785. tank_soilec = dry_tank_Soil.query.filter_by(tank_num=tank_num).filter(dry_tank_Soil.datetime.between(date_start, date_end)).all()
  4786. for ec in tank_soilec:
  4787. l = []
  4788. time = dt.strftime(ec.datetime, '%Y-%b-%d_%H:%M:%S')
  4789. # time = dt.strftime(tem.datetime, '%Y-%m-%d')
  4790. l.append(time)
  4791. l.append(ec.soil_EC)
  4792. L.append(l)
  4793. D['all'] = L
  4794. sensorData("soil_EC")
  4795. elif tank_num[:2] == 'FI':
  4796. if sensor == 'UltraSonic':
  4797. input_UltraSonic = ferment_input_UltraSonic.query.filter_by(tank_num=tank_num).filter(ferment_input_UltraSonic.datetime.between(date_start, date_end)).all()
  4798. for UltraSonic in input_UltraSonic:
  4799. l = []
  4800. time = dt.strftime(UltraSonic.datetime, '%Y-%b-%d_%H:%M:%S')
  4801. l.append(time)
  4802. l.append(UltraSonic.UltraSonic)
  4803. L.append(l)
  4804. D['all'] = L
  4805. sensorData("UltraSonic")
  4806. elif tank_num[:2] == 'FO':
  4807. if sensor == 'UltraSonic':
  4808. output_UltraSonic = ferment_output_UltraSonic.query.filter_by(tank_num=tank_num).filter(ferment_output_UltraSonic.datetime.between(date_start, date_end)).all()
  4809. for UltraSonic in output_UltraSonic:
  4810. l = []
  4811. time = dt.strftime(UltraSonic.datetime, '%Y-%b-%d_%H:%M:%S')
  4812. l.append(time)
  4813. l.append(UltraSonic.UltraSonic)
  4814. L.append(l)
  4815. D['all'] = L
  4816. sensorData("UltraSonic")
  4817. elif tank_num[:1] == 'F':
  4818. if sensor == 'LiDAR':
  4819. tank_LiDAR = ferment_tank_LiDAR.query.filter_by(tank_num = tank_num)\
  4820. .filter(ferment_tank_LiDAR.datetime.between(date_start, date_end)).all()
  4821. for LiDAR in tank_LiDAR:
  4822. l = []
  4823. time = dt.strftime(LiDAR.datetime, '%Y-%b-%d_%H:%M:%S')
  4824. l.append(time)
  4825. l.append(LiDAR.LiDAR)
  4826. L.append(l)
  4827. D['all'] = L
  4828. sensorData("LiDAR")
  4829. elif sensor == 'PressureWaterLevel':
  4830. tank_PressureWaterLevel = ferment_tank_PressureWaterLevel.query.filter_by(tank_num = tank_num)\
  4831. .filter(ferment_tank_PressureWaterLevel.datetime.between(date_start, date_end)).all()
  4832. for PressureWaterLevel in tank_PressureWaterLevel:
  4833. l = []
  4834. time = dt.strftime(PressureWaterLevel.datetime, '%Y-%b-%d_%H:%M:%S')
  4835. l.append(time)
  4836. l.append(PressureWaterLevel.PressureWaterLevel)
  4837. l.append('{:.2f}'.format((float(PressureWaterLevel.PressureWaterLevel) -10 -90.278)/9.4214))
  4838. L.append(l)
  4839. D['all'] = L
  4840. sensorData("PressureWaterLevel")
  4841. elif sensor == 'SHT11_Temp':
  4842. tank_Temp = ferment_tank_SHT11.query.filter_by(tank_num = tank_num)\
  4843. .filter(ferment_tank_SHT11.datetime.between(date_start, date_end)).all()
  4844. for Temp in tank_Temp:
  4845. l = []
  4846. time = dt.strftime(Temp.datetime, '%Y-%b-%d_%H:%M:%S')
  4847. l.append(time)
  4848. l.append(Temp.SHT11_Temp)
  4849. L.append(l)
  4850. D['all'] = L
  4851. sensorData("SHT11_Temp")
  4852. elif sensor == 'SHT11_Humidity':
  4853. tank_Humidity = ferment_tank_SHT11.query.filter_by(tank_num = tank_num)\
  4854. .filter(ferment_tank_SHT11.datetime.between(date_start, date_end)).all()
  4855. for Humidity in tank_Humidity:
  4856. l = []
  4857. time = dt.strftime(Humidity.datetime, '%Y-%b-%d_%H:%M:%S')
  4858. l.append(time)
  4859. l.append(Humidity.SHT11_Humidity)
  4860. L.append(l)
  4861. D['all'] = L
  4862. sensorData("SHT11_Humidity")
  4863. elif sensor == 'CO2':
  4864. tank_CO2 = ferment_tank_CO2.query.filter_by(tank_num = tank_num)\
  4865. .filter(ferment_tank_CO2.datetime.between(date_start, date_end)).all()
  4866. for CO2 in tank_CO2:
  4867. l = []
  4868. time = dt.strftime(CO2.datetime, '%Y-%b-%d_%H:%M:%S')
  4869. l.append(time)
  4870. l.append(CO2.CO2)
  4871. L.append(l)
  4872. D['all'] = L
  4873. sensorData("CO2")
  4874. elif sensor == 'PH':
  4875. tank_PH = ferment_tank_PH.query.filter_by(tank_num = tank_num)\
  4876. .filter(ferment_tank_PH.datetime.between(date_start, date_end)).all()
  4877. for PH in tank_PH:
  4878. l = []
  4879. time = dt.strftime(PH.datetime, '%Y-%b-%d_%H:%M:%S')
  4880. l.append(time)
  4881. l.append(PH.PH)
  4882. L.append(l)
  4883. D['all'] = L
  4884. sensorData("PH")
  4885. elif sensor == 'ORP':
  4886. tank_ORP = ferment_tank_ORP.query.filter_by(tank_num = tank_num)\
  4887. .filter(ferment_tank_ORP.datetime.between(date_start, date_end)).all()
  4888. for ORP in tank_ORP:
  4889. l = []
  4890. time = dt.strftime(ORP.datetime, '%Y-%b-%d_%H:%M:%S')
  4891. l.append(time)
  4892. l.append(ORP.ORP)
  4893. L.append(l)
  4894. D['all'] = L
  4895. sensorData("ORP")
  4896. elif sensor == 'DO':
  4897. tank_DO = ferment_tank_DO.query.filter_by(tank_num = tank_num)\
  4898. .filter(ferment_tank_DO.datetime.between(date_start, date_end)).all()
  4899. for DO in tank_DO:
  4900. l = []
  4901. time = dt.strftime(DO.datetime, '%Y-%b-%d_%H:%M:%S')
  4902. l.append(time)
  4903. l.append(DO.DO)
  4904. L.append(l)
  4905. D['all'] = L
  4906. sensorData("DO")
  4907. elif sensor == 'EC':
  4908. tank_EC = ferment_tank_EC.query.filter_by(tank_num = tank_num)\
  4909. .filter(ferment_tank_EC.datetime.between(date_start, date_end)).all()
  4910. for EC in tank_EC:
  4911. l = []
  4912. time = dt.strftime(EC.datetime, '%Y-%b-%d_%H:%M:%S')
  4913. l.append(time)
  4914. l.append(EC.EC)
  4915. L.append(l)
  4916. D['all'] = L
  4917. sensorData("EC")
  4918. elif sensor == 'PA':
  4919. tank_PA = ferment_tank_PA.query.filter_by(tank_num = tank_num)\
  4920. .filter(ferment_tank_PA.datetime.between(date_start, date_end)).all()
  4921. for PA in tank_PA:
  4922. l = []
  4923. time = dt.strftime(PA.datetime, '%Y-%b-%d_%H:%M:%S')
  4924. l.append(time)
  4925. l.append(PA.PA)
  4926. L.append(l)
  4927. D['all'] = L
  4928. sensorData("PA")
  4929. print(D)
  4930. return json.dumps(D)
  4931. else:
  4932. pass
  4933. # 貨櫃2致動器的發酵槽訪問路徑
  4934. @main.route('/cargo2_actuator_tanks', methods=['GET', 'POST'])
  4935. def cargo2_actuator_tanks_views():
  4936. username = session['uname']
  4937. if request.method == 'GET':
  4938. return render_template('cargo2_actuator_tanks.html', params=locals())
  4939. else:
  4940. pass
  4941. # 貨櫃2致動器發酵槽的清單訪問路徑
  4942. @main.route('/cargo2_actuator/<tid>', methods=['GET', 'POST'])
  4943. def cargo2_actuator_views(tid):
  4944. username = session['uname']
  4945. if request.method == 'GET':
  4946. return render_template('cargo2_actuator.html', params=locals())
  4947. else:
  4948. pass
  4949. # 貨櫃3的訪問路徑
  4950. @main.route('/cargo3', methods=['GET', 'POST'])
  4951. def cargo3_views():
  4952. username = session['uname']
  4953. if request.method == 'GET':
  4954. return render_template('cargo3.html', params=locals())
  4955. else:
  4956. pass
  4957. # 貨櫃3排程的訪問路徑
  4958. @main.route('/cargo3_schedule', methods=['GET', 'POST'])
  4959. def cargo3_schedule_views():
  4960. username = session['uname']
  4961. if request.method == 'GET':
  4962. # 將已儲存的排程資料傳給前端
  4963. # 提升機
  4964. try:
  4965. hoist = Hoist.query.order_by(text('datetime desc')).first()
  4966. datetime = hoist.datetime
  4967. hoists = Hoist.query.filter_by(datetime=datetime).all()
  4968. except Exception as e:
  4969. pass
  4970. # 烘乾機
  4971. try:
  4972. dryer = Dryer.query.order_by(text('datetime desc')).first()
  4973. datetime = dryer.datetime
  4974. dryers = Dryer.query.filter_by(datetime=datetime).all()
  4975. except Exception as e:
  4976. pass
  4977. return render_template('cargo3_schedule.html', params=locals())
  4978. else:
  4979. # Rita: "POST /b_cargo3 HTTP/1.1" 404 -
  4980. current_time = dt.now()
  4981. # 循環從前端提交過來的資料
  4982. for i in request.form:
  4983. print('Rita test i :', i)
  4984. # 堤升機
  4985. if i[:5] == 'hoist':
  4986. if i[:14] == 'hoist_duration':
  4987. hoist = Hoist()
  4988. hoist.duration = request.form[i]
  4989. elif i[:13] == 'hoist_from_hr':
  4990. ho_from_hr = request.form[i]
  4991. elif i[:14] == 'hoist_from_min':
  4992. ho_from_min = request.form[i]
  4993. hoist.start = ho_from_hr + ":" + ho_from_min
  4994. elif i[:11] == 'hoist_to_hr':
  4995. ho_to_hr = request.form[i]
  4996. elif i[:12] == 'hoist_to_min':
  4997. ho_to_min = request.form[i]
  4998. hoist.end = ho_to_hr + ":" + ho_to_min
  4999. hoist.datetime = current_time
  5000. db.session.add(hoist)
  5001. db.session.commit()
  5002. # 烘乾機
  5003. elif i[:5] == 'dryer':
  5004. if i[:14] == 'dryer_duration':
  5005. dryer = Dryer()
  5006. dryer.duration = request.form[i]
  5007. elif i[:13] == 'dryer_from_hr':
  5008. dry_from_hr = request.form[i]
  5009. elif i[:14] == 'dryer_from_min':
  5010. dry_from_min = request.form[i]
  5011. dryer.start = dry_from_hr + ":" + dry_from_min
  5012. elif i[:11] == 'dryer_to_hr':
  5013. dry_to_hr = request.form[i]
  5014. elif i[:12] == 'dryer_to_min':
  5015. dry_to_min = request.form[i]
  5016. dryer.end = dry_to_hr + ":" + dry_to_min
  5017. dryer.datetime = current_time
  5018. db.session.add(dryer)
  5019. db.session.commit()
  5020. return render_template('cargo3.html', params=locals())
  5021. # 影像串流的訪問路徑
  5022. @main.route('/video', methods=['GET', 'POST'])
  5023. def video_views():
  5024. username = session['uname']
  5025. if request.method == 'GET':
  5026. return render_template('video.html', params=locals())
  5027. else:
  5028. pass
  5029. # 影像的訪問路徑
  5030. @main.route('/learn', methods=['GET', 'POST'])
  5031. def learn_views():
  5032. username = session['uname']
  5033. if request.method == 'GET':
  5034. return render_template('learn.html', params=locals())
  5035. else:
  5036. pass
  5037. # 獲取relay狀態路徑
  5038. @main.route('/relay', methods=['GET', 'POST'])
  5039. def relay_views():
  5040. if request.method == 'GET':
  5041. relay = Relay.query.order_by(text('datetime desc')).first()
  5042. relay_status = relay.status
  5043. return jsonify({"relay": relay_status})
  5044. else:
  5045. pass
  5046. # 獲取脫皮機狀態路徑
  5047. @main.route('/peel', methods=['GET', 'POST'])
  5048. def peeling_views():
  5049. if request.method == 'GET':
  5050. peeling = PeelingMachineRPM.query.order_by(
  5051. text('datetime desc')).first()
  5052. peeling_rpm = peeling.rpm
  5053. return jsonify({"peeling": peeling_rpm})
  5054. else:
  5055. pass
  5056. # 退出的訪問路徑
  5057. @main.route('/logout')
  5058. def logout_views():
  5059. if 'id' in session and 'uname' in session:
  5060. del session['id']
  5061. del session['uname']
  5062. return redirect('/')
  5063. @main.route("/udp_client", methods=['POST', 'GET'])
  5064. def udp_views():
  5065. # sl(0.5)
  5066. global c_sock
  5067. if request.method == 'GET':
  5068. # def close():
  5069. # global c_sock
  5070. # sl(600)
  5071. # c_sock.close()
  5072. # c_sock = 0
  5073. # print("c_sock is closing")
  5074. max_length = 65000
  5075. # max_length = 95000
  5076. # lab1的IP
  5077. host = "192.168.50.65"
  5078. # lab2的IP
  5079. # host = "192.168.51.160"
  5080. port = 8000
  5081. c_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  5082. # t = threading.Thread(target=close)
  5083. # t.daemon = True
  5084. # t.start()
  5085. print("test")
  5086. # dict = request.args.to_dict()
  5087. # nr = int(dict['nr'])
  5088. # if nr == 1:
  5089. # camara = 0
  5090. # elif nr == 2:
  5091. # camara = 'rtsp://admin:admin@192.168.50.182/av2_0'
  5092. # elif nr == 3:
  5093. # camara = 'rtsp://admin:abcd1234@192.168.51.101/av2_0'
  5094. # 地下室小兵
  5095. # cap = cv2.VideoCapture('rtsp://admin:abcd1234@192.168.51.48/av2_0')
  5096. # 主機攝像頭
  5097. # cap = cv2.VideoCapture(0)
  5098. # 外面小兵
  5099. # cap = cv2.VideoCapture('rtsp://admin:admin@192.168.50.182/av2_0')
  5100. cap = cv2.VideoCapture('rtsp://192.168.50.182/av0_0')
  5101. # cap = cv2.VideoCapture(0)
  5102. # cap = cv2.VideoCapture(camara)
  5103. ret, frame = cap.read()
  5104. while ret:
  5105. # compress frame
  5106. frame = cv2.resize(frame, (550, 400), interpolation=cv2.INTER_AREA)
  5107. retval, buffer = cv2.imencode(".jpg", frame)
  5108. if retval:
  5109. # convert to byte array
  5110. buffer = buffer.tobytes()
  5111. # get size of the frame
  5112. buffer_size = len(buffer)
  5113. num_of_packs = 1
  5114. if buffer_size > max_length:
  5115. num_of_packs = math.ceil(buffer_size / max_length)
  5116. frame_info = {"packs": num_of_packs}
  5117. # send the number of packs to be expected
  5118. # print("Number of packs:", num_of_packs)
  5119. c_sock.sendto(pickle.dumps(frame_info), (host, port))
  5120. left = 0
  5121. right = max_length
  5122. for i in range(num_of_packs):
  5123. # print("left:", left)
  5124. # print("right:", right)
  5125. # truncate data to send
  5126. data = buffer[left:right]
  5127. left = right
  5128. right += max_length
  5129. # send the frames accordingly
  5130. c_sock.sendto(data, (host, port))
  5131. ret, frame = cap.read()
  5132. print("done")
  5133. return "done"
  5134. else:
  5135. c_sock.shutdown(2)
  5136. c_sock.close()
  5137. c_sock = 0
  5138. print("c_sock is closing")
  5139. # 影像串流的路徑
  5140. @main.route("/video_feed", methods=['POST', 'GET'])
  5141. def video_feed_views():
  5142. print("test2")
  5143. global s_sock
  5144. if request.method == 'GET':
  5145. # 於10分鐘之後,自動關閉socket server
  5146. # if s_sock == 0:
  5147. # def socket_server_views():
  5148. # global s_sock
  5149. # print("test")
  5150. # print(s_sock)
  5151. # sl(600)
  5152. # if s_sock != 0:
  5153. # s_sock.close()
  5154. # s_sock = 0
  5155. # print('s_sock is closed')
  5156. #
  5157. # pool.submit(socket_server_views)
  5158. host = "192.168.50.65"
  5159. port = 8000
  5160. max_length = 65540
  5161. # max_length = 95540
  5162. s_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  5163. s_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  5164. s_sock.bind((host, port))
  5165. frame_info = None
  5166. buffer = None
  5167. frame = None
  5168. encodedImage = None
  5169. print("-> waiting for connection")
  5170. # 影像生成器函數,將影像以jpg格式傳給前端
  5171. def generate():
  5172. while True:
  5173. with lock:
  5174. global s_sock, frame_info, buffer, frame, encodedImage
  5175. if s_sock == 0:
  5176. break
  5177. data, address = s_sock.recvfrom(max_length)
  5178. if len(data) < 100:
  5179. frame_info = pickle.loads(data)
  5180. if frame_info:
  5181. nums_of_packs = frame_info["packs"]
  5182. for i in range(nums_of_packs):
  5183. if s_sock == 0:
  5184. break
  5185. data, address = s_sock.recvfrom(max_length)
  5186. if i == 0:
  5187. buffer = data
  5188. else:
  5189. buffer += data
  5190. frame = np.frombuffer(buffer, dtype=np.uint8)
  5191. frame = frame.reshape(frame.shape[0], 1)
  5192. frame = cv2.imdecode(frame, cv2.IMREAD_UNCHANGED)
  5193. # print(frame)
  5194. # 如果frame為None就跳過
  5195. if frame is None:
  5196. continue
  5197. frame = cv2.resize(
  5198. frame, (640, 360), interpolation=cv2.INTER_AREA)
  5199. # encode the frame in JPEG format
  5200. (flag, encodedImage) = cv2.imencode(".jpg", frame)
  5201. # ensure the frame was successfully encoded
  5202. if not flag:
  5203. continue
  5204. # yield the output frame in the byte format
  5205. yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' +
  5206. bytearray(encodedImage) + b'\r\n')
  5207. # return the response generated along with the specific media
  5208. # type (mime type)
  5209. return Response(generate(), mimetype="multipart/x-mixed-replace; boundary=frame")
  5210. else:
  5211. if s_sock != 0:
  5212. s_sock.close()
  5213. s_sock = 0
  5214. print('s_sock is closed')
  5215. return "s_sock is closed"
  5216. print("test")
  5217. return "s_sock is closed"
  5218. D = {"input_vacuum_status": 0,
  5219. "tank_vacuum_status": 0,
  5220. "tank_threewayvalve_status": 0,
  5221. "tank_diskvalve_status": 0,
  5222. "tank_solenoid_disinfect_status": 0,
  5223. "tank_solenoid_water_status": 0,
  5224. "tank_motor_status": 'none',
  5225. "tank_blower_status": 0,
  5226. "tank_heater1_status": 0,
  5227. "tank_heater2_status": 0,
  5228. "temp1_enable": 0,
  5229. "temp1": 0,
  5230. "output_vacuum_status": 0,
  5231. "tank_threewayvalve_input_status":0,
  5232. "tank_solenoid_water_total_status": 0,
  5233. "tank_solenoid_disinfect_status": 0,
  5234. "outer_solenoid_water_status": 0,
  5235. "tank_solenoid_water_in_status":0,
  5236. "tank_pump_sensor_status": 0,
  5237. "tank_threewayvalve_bean_status":0,
  5238. "outer_threewayvalve_float_status":0,
  5239. "tank_temp_enable_status":0,
  5240. "tank_temp_status": 'none',
  5241. "tank_pump_waterFloat_status": 0,
  5242. "tank_pump_waterL2_status": 0,
  5243. "tank_pump_waterL4_status": 0,
  5244. "tank_solenoid_waterL3_status": 0,
  5245. "tank_solenoid_waterL5_status": 0,
  5246. "tank_stepping_motor_status": 'none',
  5247. }
  5248. pub_topic = 'AISKY/Coffee/MK-G/b8:27:eb:b4:59:3e'
  5249. # sub_topic = 'AISKY/Coffee/MK-G/b8:27:eb:b4:59:3e/Log'
  5250. # mqttObj = MQTT('aisky-client', 'aiskyc', '60.250.156.234', 1883, 60, sub_topic)
  5251. mqttObj = MQTT()
  5252. # mqtt發布
  5253. # @main.route('/mqtt/<tid>', methods=['POST'])
  5254. @main.route('/mqtt/<tid>', methods=['POST'])
  5255. def mqtt_views(tid):
  5256. import json
  5257. dict = request.form.to_dict()
  5258. json = json.dumps(dict)
  5259. # print('dict:', dict) # dict: {'tank-number': '1', 'command': 'tank_vacuum_status', 'value': 'on'}
  5260. # print('json:', json) # json: {"tank-number": "1", tank_vacuum_status", "value": "on"}
  5261. topic = 'AISKY/Coffee/MK-G/b8:27:eb:b4:59:3e'
  5262. # res = mqttObj.mqttPublish(pub_topic, json)
  5263. # print(res)
  5264. # sl(2)
  5265. # print(mqttObj.res)
  5266. #
  5267. # print('test')
  5268. mqtt.publish(topic, json)
  5269. # 入料儲豆槽真空吸料機(ON吸料/OFF排氣)
  5270. if dict['command'] == 'input_vacuum_status':
  5271. time.sleep(1)
  5272. # D[input_vacuum_status]: 0
  5273. print('D[input_vacuum_status]:', D['input_vacuum_status'])
  5274. if D['input_vacuum_status'] == 'on':
  5275. return 'on'
  5276. elif D['input_vacuum_status'] == 'off':
  5277. return 'off'
  5278. else:
  5279. return "input_vacuum_status signal was not received"
  5280. # 真空吸料機
  5281. elif dict['command'] == 'tank_vacuum_status':
  5282. time.sleep(1)
  5283. if D['tank_vacuum_status'] == 'on':
  5284. return 'on'
  5285. elif D['tank_vacuum_status'] == 'off':
  5286. return 'off'
  5287. else:
  5288. return "tank_vacuum_status signal was not received"
  5289. # 三通閥(ON吸料/OFF排氣)
  5290. elif dict['command'] == 'tank_threewayvalve_status':
  5291. time.sleep(1)
  5292. if D['tank_threewayvalve_status'] == 'on':
  5293. return 'on'
  5294. elif D['tank_threewayvalve_status'] == 'off':
  5295. return 'off'
  5296. else:
  5297. return "tank_threewayvalve_status signal was not received"
  5298. # 蝴蝶閥(ON開/OFF關)
  5299. elif dict['command'] == 'tank_diskvalve_status':
  5300. time.sleep(1)
  5301. print("D['tank_diskvalve_status']", D['tank_diskvalve_status'])
  5302. if D['tank_diskvalve_status'] == 'on':
  5303. return 'on'
  5304. elif D['tank_diskvalve_status'] == 'off':
  5305. return 'off'
  5306. else:
  5307. return "tank_diskvalve_status signal was not received"
  5308. # 電磁閥(ON噴灑消毒/OFF關)
  5309. elif dict['command'] == 'tank_solenoid_disinfect_status':
  5310. time.sleep(1)
  5311. if D['tank_solenoid_disinfect_status'] == 'on':
  5312. return 'on'
  5313. elif D['tank_solenoid_disinfect_status'] == 'off':
  5314. return 'off'
  5315. else:
  5316. return "tank_solenoid_disinfect_status signal was not received"
  5317. # 電磁閥(ON排水/OFF關)
  5318. elif dict['command'] == 'tank_solenoid_water_status':
  5319. time.sleep(1)
  5320. if D['tank_solenoid_water_status'] == 'on':
  5321. return 'on'
  5322. elif D['tank_solenoid_water_status'] == 'off':
  5323. return 'off'
  5324. else:
  5325. return "tank_solenoid_water_status signal was not received"
  5326. # 馬達(單位RPM)
  5327. elif dict['command'] == 'tank_motor_status':
  5328. print("D['tank_motor_status']", D['tank_motor_status'])
  5329. time.sleep(1)
  5330. if D['tank_motor_status'] == '0':
  5331. return 'off'
  5332. elif type(D['tank_motor_status']) == int:
  5333. return 'on'
  5334. else:
  5335. return "tank_motor_status signal was not received"
  5336. # 鼓風機(ON開/OFF關)
  5337. elif dict['command'] == 'tank_blower_status':
  5338. time.sleep(1)
  5339. if D['tank_blower_status'] == 'on':
  5340. return 'on'
  5341. elif D['tank_blower_status'] == 'off':
  5342. return 'off'
  5343. else:
  5344. return "tank_blower_status signal was not received"
  5345. # 加熱棒 1(ON開/OFF關)
  5346. elif dict['command'] == 'tank_heater1_status':
  5347. time.sleep(1)
  5348. if D['tank_heater1_status'] == 'on':
  5349. return 'on'
  5350. elif D['tank_heater1_status'] == 'off':
  5351. return 'off'
  5352. else:
  5353. return "tank_heater1_status signal was not received"
  5354. # 加熱棒 2(ON開/OFF關)
  5355. elif dict['command'] == 'tank_heater2_status':
  5356. time.sleep(1)
  5357. if D['tank_heater2_status'] == 'on':
  5358. return 'on'
  5359. elif D['tank_heater2_status'] == 'off':
  5360. return 'off'
  5361. else:
  5362. return "tank_heater2_status signal was not received"
  5363. # 溫度控制(ON開/OFF關)
  5364. elif dict['command'] == 'temp1_enable':
  5365. time.sleep(1)
  5366. if D['temp1_enable'] == 'on':
  5367. return 'on'
  5368. elif D['temp1_enable'] == 'off':
  5369. return 'off'
  5370. else:
  5371. return "temp1_enable_status signal was not received"
  5372. # 設定溫度 (限整數)
  5373. elif dict['command'] == 'temp1':
  5374. time.sleep(1)
  5375. if D['temp1'] == '0':
  5376. return 'off'
  5377. elif type(D['temp1']) == int:
  5378. return 'on'
  5379. else:
  5380. return "temp1 signal was not received"
  5381. # 出料儲豆槽真空吸料機(ON吸料/OFF排氣)
  5382. elif dict['command'] == 'output_vacuum_status':
  5383. time.sleep(1)
  5384. if D['output_vacuum_status'] == 'on':
  5385. return 'on'
  5386. elif D['output_vacuum_status'] == 'off':
  5387. return 'off'
  5388. else:
  5389. return "output_vacuum_status signal was not received"
  5390. # 發酵槽 入料三通閥
  5391. elif dict['command'] == 'tank_threewayvalve_input_status':
  5392. time.sleep(1)
  5393. if D['tank_threewayvalve_input_status'] == 'on':
  5394. return 'on'
  5395. elif D['tank_threewayvalve_input_status'] == 'off':
  5396. return 'off'
  5397. else:
  5398. return "tank_threewayvalve_input_status signal was not received"
  5399. # 發酵槽 總進水
  5400. elif dict['command'] == 'tank_solenoid_water_total_status':
  5401. time.sleep(1)
  5402. if D['tank_solenoid_water_total_status'] == 'on':
  5403. return 'on'
  5404. elif D['tank_solenoid_water_total_status'] == 'off':
  5405. return 'off'
  5406. else:
  5407. return "tank_solenoid_water_total_status signal was not received"
  5408. # 發酵槽 桶內消毒/打菌
  5409. elif dict['command'] == 'tank_solenoid_disinfect_status':
  5410. time.sleep(1)
  5411. if D['tank_solenoid_disinfect_status'] == 'on':
  5412. return 'on'
  5413. elif D['tank_solenoid_disinfect_status'] == 'off':
  5414. return 'off'
  5415. else:
  5416. return "tank_solenoid_disinfect_status signal was not received"
  5417. # 發酵槽 夾層進水
  5418. elif dict['command'] == 'outer_solenoid_water_status':
  5419. time.sleep(1)
  5420. if D['outer_solenoid_water_status'] == 'on':
  5421. return 'on'
  5422. elif D['outer_solenoid_water_status'] == 'off':
  5423. return 'off'
  5424. else:
  5425. return "outer_solenoid_water_status signal was not received"
  5426. # 發酵槽 桶內進水
  5427. elif dict['command'] == 'tank_solenoid_water_in_status':
  5428. time.sleep(1)
  5429. if D['tank_solenoid_water_in_status'] == 'on':
  5430. return 'on'
  5431. elif D['tank_solenoid_water_in_status'] == 'off':
  5432. return 'off'
  5433. else:
  5434. return "tank_solenoid_water_in_status signal was not received"
  5435. # 發酵槽 雙核隔膜泵
  5436. elif dict['command'] == 'tank_pump_sensor_status':
  5437. time.sleep(1)
  5438. if D['tank_pump_sensor_status'] == 'on':
  5439. return 'on'
  5440. elif D['tank_pump_sensor_status'] == 'off':
  5441. return 'off'
  5442. else:
  5443. return "tank_pump_sensor_status signal was not received"
  5444. # 發酵槽 感測器下豆三通閥
  5445. elif dict['command'] == 'tank_threewayvalve_bean_status':
  5446. time.sleep(1)
  5447. if D['tank_threewayvalve_bean_status'] == 'on':
  5448. return 'on'
  5449. elif D['tank_threewayvalve_bean_status'] == 'off':
  5450. return 'off'
  5451. else:
  5452. return "tank_threewayvalve_bean_status signal was not received"
  5453. # 發酵槽 外桶浮選三通閥
  5454. elif dict['command'] == 'outer_threewayvalve_float_status':
  5455. time.sleep(1)
  5456. if D['outer_threewayvalve_float_status'] == 'on':
  5457. return 'on'
  5458. elif D['outer_threewayvalve_float_status'] == 'off':
  5459. return 'off'
  5460. else:
  5461. return "outer_threewayvalve_float_status signal was not received"
  5462. # 發酵槽 溫控開關
  5463. elif dict['command'] == 'tank_temp_enable_status':
  5464. time.sleep(1)
  5465. if D['tank_temp_enable_status'] == 'on':
  5466. return 'on'
  5467. elif D['tank_temp_enable_status'] == 'off':
  5468. return 'off'
  5469. else:
  5470. return "tank_temp_enable_status signal was not received"
  5471. # 發酵槽 設定溫度
  5472. elif dict['command'] == 'tank_temp_status':
  5473. time.sleep(1)
  5474. if D['tank_temp_status'] == '0':
  5475. return 'off'
  5476. elif type(D['tank_temp_status']) == int:
  5477. return 'on'
  5478. else:
  5479. return "tank_temp_status signal was not received"
  5480. # 清洗浮選槽 浮選槽注水
  5481. elif dict['command'] == 'tank_pump_waterFloat_status':
  5482. time.sleep(1)
  5483. if D['tank_pump_waterFloat_status'] == 'on':
  5484. return 'on'
  5485. elif D['tank_pump_waterFloat_status'] == 'off':
  5486. return 'off'
  5487. else:
  5488. return "tank_pump_waterFloat_status signal was not received"
  5489. # 清洗浮選槽 清洗第 2 層灑水
  5490. elif dict['command'] == 'tank_pump_waterL2_status':
  5491. time.sleep(1)
  5492. if D['tank_pump_waterL2_status'] == 'on':
  5493. return 'on'
  5494. elif D['tank_pump_waterL2_status'] == 'off':
  5495. return 'off'
  5496. else:
  5497. return "tank_pump_waterL2_status signal was not received"
  5498. # 清洗浮選槽 清洗第 4 層灑水
  5499. elif dict['command'] == 'tank_pump_waterL4_status':
  5500. time.sleep(1)
  5501. if D['tank_pump_waterL4_status'] == 'on':
  5502. return 'on'
  5503. elif D['tank_pump_waterL4_status'] == 'off':
  5504. return 'off'
  5505. else:
  5506. return "tank_pump_waterL4_status signal was not received"
  5507. # 清洗浮選槽 清洗第 3 層電磁閥
  5508. elif dict['command'] == 'tank_solenoid_waterL3_status':
  5509. time.sleep(1)
  5510. if D['tank_solenoid_waterL3_status'] == 'on':
  5511. return 'on'
  5512. elif D['tank_solenoid_waterL3_status'] == 'off':
  5513. return 'off'
  5514. else:
  5515. return "tank_solenoid_waterL3_status signal was not received"
  5516. # 清洗浮選槽 清洗第 5 層電磁閥
  5517. elif dict['command'] == 'tank_solenoid_waterL5_status':
  5518. time.sleep(1)
  5519. if D['tank_solenoid_waterL5_status'] == 'on':
  5520. return 'on'
  5521. elif D['tank_solenoid_waterL5_status'] == 'off':
  5522. return 'off'
  5523. else:
  5524. return "tank_solenoid_waterL5_status signal was not received"
  5525. # 清洗浮選槽 步進馬達
  5526. elif dict['command'] == 'tank_stepping_motor_status':
  5527. time.sleep(1)
  5528. if D['tank_stepping_motor_status'] == '0':
  5529. return 'off'
  5530. elif type(D['tank_stepping_motor_status']) == int:
  5531. return 'on'
  5532. else:
  5533. return "tank_stepping_motor_status signal was not received"
  5534. return "publish done"
  5535. # --- 10/19 ------------------------------------------ start
  5536. # mqtt發布
  5537. # @main.route('/mqtt/<tid>', methods=['POST'])
  5538. @main.route('/mqtt_f/<tid>', methods=['POST'])
  5539. def mqtt_f(data):
  5540. import json
  5541. # dict = request.form.to_dict()
  5542. json = json.dumps(data)
  5543. # print('dict:', dict) # dict: {'tank-number': '1', 'command': 'tank_vacuum_status', 'value': 'on'}
  5544. # print('json:', json) # json: {"tank-number": "1", tank_vacuum_status", "value": "on"}
  5545. print('json:', json)
  5546. topic = 'AISKY/Coffee/MK-G/b8:27:eb:b4:59:3e'
  5547. # res = mqttObj.mqttPublish(pub_topic, json)
  5548. # print(res)
  5549. # sl(2)
  5550. # print(mqttObj.res)
  5551. #
  5552. # print('test')
  5553. mqtt.publish(topic, json)
  5554. # 入料儲豆槽真空吸料機(ON吸料/OFF排氣)
  5555. if dict['command'] == 'input_vacuum_status':
  5556. time.sleep(1)
  5557. # D[input_vacuum_status]: 0
  5558. print('D[input_vacuum_status]:', D['input_vacuum_status'])
  5559. if D['input_vacuum_status'] == 'on':
  5560. return 'on'
  5561. elif D['input_vacuum_status'] == 'off':
  5562. return 'off'
  5563. else:
  5564. return "input_vacuum_status signal was not received"
  5565. # 真空吸料機
  5566. elif dict['command'] == 'tank_vacuum_status':
  5567. time.sleep(1)
  5568. if D['tank_vacuum_status'] == 'on':
  5569. return 'on'
  5570. elif D['tank_vacuum_status'] == 'off':
  5571. return 'off'
  5572. else:
  5573. return "tank_vacuum_status signal was not received"
  5574. # 三通閥(ON吸料/OFF排氣)
  5575. elif dict['command'] == 'tank_threewayvalve_status':
  5576. time.sleep(1)
  5577. if D['tank_threewayvalve_status'] == 'on':
  5578. return 'on'
  5579. elif D['tank_threewayvalve_status'] == 'off':
  5580. return 'off'
  5581. else:
  5582. return "tank_threewayvalve_status signal was not received"
  5583. # 蝴蝶閥(ON開/OFF關)
  5584. elif dict['command'] == 'tank_diskvalve_status':
  5585. time.sleep(1)
  5586. print("D['tank_diskvalve_status']", D['tank_diskvalve_status'])
  5587. if D['tank_diskvalve_status'] == 'on':
  5588. return 'on'
  5589. elif D['tank_diskvalve_status'] == 'off':
  5590. return 'off'
  5591. else:
  5592. return "tank_diskvalve_status signal was not received"
  5593. # 電磁閥(ON噴灑消毒/OFF關)
  5594. elif dict['command'] == 'tank_solenoid_disinfect_status':
  5595. time.sleep(1)
  5596. if D['tank_solenoid_disinfect_status'] == 'on':
  5597. return 'on'
  5598. elif D['tank_solenoid_disinfect_status'] == 'off':
  5599. return 'off'
  5600. else:
  5601. return "tank_solenoid_disinfect_status signal was not received"
  5602. # 電磁閥(ON排水/OFF關)
  5603. elif dict['command'] == 'tank_solenoid_water_status':
  5604. time.sleep(1)
  5605. if D['tank_solenoid_water_status'] == 'on':
  5606. return 'on'
  5607. elif D['tank_solenoid_water_status'] == 'off':
  5608. return 'off'
  5609. else:
  5610. return "tank_solenoid_water_status signal was not received"
  5611. # 馬達(單位RPM)
  5612. elif dict['command'] == 'tank_motor_status':
  5613. print("D['tank_motor_status']", D['tank_motor_status'])
  5614. time.sleep(1)
  5615. if D['tank_motor_status'] == '0':
  5616. return 'off'
  5617. elif type(D['tank_motor_status']) == int:
  5618. return 'on'
  5619. else:
  5620. return "tank_motor_status signal was not received"
  5621. # 鼓風機(ON開/OFF關)
  5622. elif dict['command'] == 'tank_blower_status':
  5623. time.sleep(1)
  5624. if D['tank_blower_status'] == 'on':
  5625. return 'on'
  5626. elif D['tank_blower_status'] == 'off':
  5627. return 'off'
  5628. else:
  5629. return "tank_blower_status signal was not received"
  5630. # 加熱棒 1(ON開/OFF關)
  5631. elif dict['command'] == 'tank_heater1_status':
  5632. time.sleep(1)
  5633. if D['tank_heater1_status'] == 'on':
  5634. return 'on'
  5635. elif D['tank_heater1_status'] == 'off':
  5636. return 'off'
  5637. else:
  5638. return "tank_heater1_status signal was not received"
  5639. # 加熱棒 2(ON開/OFF關)
  5640. elif dict['command'] == 'tank_heater2_status':
  5641. time.sleep(1)
  5642. if D['tank_heater2_status'] == 'on':
  5643. return 'on'
  5644. elif D['tank_heater2_status'] == 'off':
  5645. return 'off'
  5646. else:
  5647. return "tank_heater2_status signal was not received"
  5648. # 溫度控制(ON開/OFF關)
  5649. elif dict['command'] == 'temp1_enable':
  5650. time.sleep(1)
  5651. if D['temp1_enable'] == 'on':
  5652. return 'on'
  5653. elif D['temp1_enable'] == 'off':
  5654. return 'off'
  5655. else:
  5656. return "temp1_enable_status signal was not received"
  5657. # 設定溫度 (限整數)
  5658. elif dict['command'] == 'temp1':
  5659. time.sleep(1)
  5660. if D['temp1'] == '0':
  5661. return 'off'
  5662. elif type(D['temp1']) == int:
  5663. return 'on'
  5664. else:
  5665. return "temp1 signal was not received"
  5666. # 出料儲豆槽真空吸料機(ON吸料/OFF排氣)
  5667. elif dict['command'] == 'output_vacuum_status':
  5668. time.sleep(1)
  5669. if D['output_vacuum_status'] == 'on':
  5670. return 'on'
  5671. elif D['output_vacuum_status'] == 'off':
  5672. return 'off'
  5673. else:
  5674. return "output_vacuum_status signal was not received"
  5675. # 發酵槽 入料三通閥
  5676. elif dict['command'] == 'tank_threewayvalve_input_status':
  5677. time.sleep(1)
  5678. if D['tank_threewayvalve_input_status'] == 'on':
  5679. return 'on'
  5680. elif D['tank_threewayvalve_input_status'] == 'off':
  5681. return 'off'
  5682. else:
  5683. return "tank_threewayvalve_input_status signal was not received"
  5684. # 發酵槽 總進水
  5685. elif dict['command'] == 'tank_solenoid_water_total_status':
  5686. time.sleep(1)
  5687. if D['tank_solenoid_water_total_status'] == 'on':
  5688. return 'on'
  5689. elif D['tank_solenoid_water_total_status'] == 'off':
  5690. return 'off'
  5691. else:
  5692. return "tank_solenoid_water_total_status signal was not received"
  5693. # 發酵槽 桶內消毒/打菌
  5694. elif dict['command'] == 'tank_solenoid_disinfect_status':
  5695. time.sleep(1)
  5696. if D['tank_solenoid_disinfect_status'] == 'on':
  5697. return 'on'
  5698. elif D['tank_solenoid_disinfect_status'] == 'off':
  5699. return 'off'
  5700. else:
  5701. return "tank_solenoid_disinfect_status signal was not received"
  5702. # 發酵槽 夾層進水
  5703. elif dict['command'] == 'outer_solenoid_water_status':
  5704. time.sleep(1)
  5705. if D['outer_solenoid_water_status'] == 'on':
  5706. return 'on'
  5707. elif D['outer_solenoid_water_status'] == 'off':
  5708. return 'off'
  5709. else:
  5710. return "outer_solenoid_water_status signal was not received"
  5711. # 發酵槽 桶內進水
  5712. elif dict['command'] == 'tank_solenoid_water_in_status':
  5713. time.sleep(1)
  5714. if D['tank_solenoid_water_in_status'] == 'on':
  5715. return 'on'
  5716. elif D['tank_solenoid_water_in_status'] == 'off':
  5717. return 'off'
  5718. else:
  5719. return "tank_solenoid_water_in_status signal was not received"
  5720. # 發酵槽 雙核隔膜泵
  5721. elif dict['command'] == 'tank_pump_sensor_status':
  5722. time.sleep(1)
  5723. if D['tank_pump_sensor_status'] == 'on':
  5724. return 'on'
  5725. elif D['tank_pump_sensor_status'] == 'off':
  5726. return 'off'
  5727. else:
  5728. return "tank_pump_sensor_status signal was not received"
  5729. # 發酵槽 感測器下豆三通閥
  5730. elif dict['command'] == 'tank_threewayvalve_bean_status':
  5731. time.sleep(1)
  5732. if D['tank_threewayvalve_bean_status'] == 'on':
  5733. return 'on'
  5734. elif D['tank_threewayvalve_bean_status'] == 'off':
  5735. return 'off'
  5736. else:
  5737. return "tank_threewayvalve_bean_status signal was not received"
  5738. # 發酵槽 外桶浮選三通閥
  5739. elif dict['command'] == 'outer_threewayvalve_float_status':
  5740. time.sleep(1)
  5741. if D['outer_threewayvalve_float_status'] == 'on':
  5742. return 'on'
  5743. elif D['outer_threewayvalve_float_status'] == 'off':
  5744. return 'off'
  5745. else:
  5746. return "outer_threewayvalve_float_status signal was not received"
  5747. # 發酵槽 溫控開關
  5748. elif dict['command'] == 'tank_temp_enable_status':
  5749. time.sleep(1)
  5750. if D['tank_temp_enable_status'] == 'on':
  5751. return 'on'
  5752. elif D['tank_temp_enable_status'] == 'off':
  5753. return 'off'
  5754. else:
  5755. return "tank_temp_enable_status signal was not received"
  5756. # 發酵槽 設定溫度
  5757. elif dict['command'] == 'tank_temp_status':
  5758. time.sleep(1)
  5759. if D['tank_temp_status'] == '0':
  5760. return 'off'
  5761. elif type(D['tank_temp_status']) == int:
  5762. return 'on'
  5763. else:
  5764. return "tank_temp_status signal was not received"
  5765. # 清洗浮選槽 浮選槽注水
  5766. elif dict['command'] == 'tank_pump_waterFloat_status':
  5767. time.sleep(1)
  5768. if D['tank_pump_waterFloat_status'] == 'on':
  5769. return 'on'
  5770. elif D['tank_pump_waterFloat_status'] == 'off':
  5771. return 'off'
  5772. else:
  5773. return "tank_pump_waterFloat_status signal was not received"
  5774. # 清洗浮選槽 清洗第 2 層灑水
  5775. elif dict['command'] == 'tank_pump_waterL2_status':
  5776. time.sleep(1)
  5777. if D['tank_pump_waterL2_status'] == 'on':
  5778. return 'on'
  5779. elif D['tank_pump_waterL2_status'] == 'off':
  5780. return 'off'
  5781. else:
  5782. return "tank_pump_waterL2_status signal was not received"
  5783. # 清洗浮選槽 清洗第 4 層灑水
  5784. elif dict['command'] == 'tank_pump_waterL4_status':
  5785. time.sleep(1)
  5786. if D['tank_pump_waterL4_status'] == 'on':
  5787. return 'on'
  5788. elif D['tank_pump_waterL4_status'] == 'off':
  5789. return 'off'
  5790. else:
  5791. return "tank_pump_waterL4_status signal was not received"
  5792. # 清洗浮選槽 清洗第 3 層電磁閥
  5793. elif dict['command'] == 'tank_solenoid_waterL3_status':
  5794. time.sleep(1)
  5795. if D['tank_solenoid_waterL3_status'] == 'on':
  5796. return 'on'
  5797. elif D['tank_solenoid_waterL3_status'] == 'off':
  5798. return 'off'
  5799. else:
  5800. return "tank_solenoid_waterL3_status signal was not received"
  5801. # 清洗浮選槽 清洗第 5 層電磁閥
  5802. elif dict['command'] == 'tank_solenoid_waterL5_status':
  5803. time.sleep(1)
  5804. if D['tank_solenoid_waterL5_status'] == 'on':
  5805. return 'on'
  5806. elif D['tank_solenoid_waterL5_status'] == 'off':
  5807. return 'off'
  5808. else:
  5809. return "tank_solenoid_waterL5_status signal was not received"
  5810. # 清洗浮選槽 步進馬達
  5811. elif dict['command'] == 'tank_stepping_motor_status':
  5812. time.sleep(1)
  5813. if D['tank_stepping_motor_status'] == '0':
  5814. return 'off'
  5815. elif type(D['tank_stepping_motor_status']) == int:
  5816. return 'on'
  5817. else:
  5818. return "tank_stepping_motor_status signal was not received"
  5819. return "publish done"
  5820. # ---10/19 ------------------------------------------- end
  5821. # 處理mqtt訂閱的信息
  5822. @mqtt.on_message()
  5823. def handle_mqtt_message(client, userdata, message):
  5824. payload = message.payload.decode()
  5825. payload = json.loads(payload)
  5826. # print("-------msg-------")
  5827. # print('name :', p['name'])
  5828. # print('email :', p['email'])
  5829. print('payload:', payload)
  5830. # 入料儲豆槽真空吸料機(ON吸料/OFF排氣)
  5831. if payload['command'] == 'input_vacuum_status':
  5832. if payload['response'] == 'on':
  5833. D['input_vacuum_status'] = 'on'
  5834. else:
  5835. D['input_vacuum_status'] = 'off'
  5836. # 真空吸料機
  5837. elif payload['command'] == 'tank_vacuum_status':
  5838. if payload['response'] == 'on':
  5839. # print('test')
  5840. D['tank_vacuum_status'] = 'on'
  5841. else:
  5842. D['tank_vacuum_status'] = 'off'
  5843. # 三通閥(ON吸料/OFF排氣)
  5844. elif payload['command'] == 'tank_threewayvalve_status':
  5845. if payload['response'] == 'on':
  5846. # print('test')
  5847. D['tank_threewayvalve_status'] = 'on'
  5848. else:
  5849. D['tank_threewayvalve_status'] = 'off'
  5850. # 蝴蝶閥(ON開/OFF關)
  5851. elif payload['command'] == 'tank_diskvalve_status':
  5852. if payload['response'] == 'on':
  5853. # print('test')
  5854. D['tank_diskvalve_status'] = 'on'
  5855. else:
  5856. D['tank_diskvalve_status'] = 'off'
  5857. # 電磁閥(ON噴灑消毒/OFF關)
  5858. elif payload['command'] == 'tank_solenoid_disinfect_status':
  5859. if payload['response'] == 'on':
  5860. # print('test')
  5861. D['tank_solenoid_disinfect_status'] = 'on'
  5862. else:
  5863. D['tank_solenoid_disinfect_status'] = 'off'
  5864. # 電磁閥(ON排水/OFF關)
  5865. elif payload['command'] == 'tank_solenoid_water_status':
  5866. if payload['response'] == 'on':
  5867. # print('test')
  5868. D['tank_solenoid_water_status'] = 'on'
  5869. else:
  5870. D['tank_solenoid_water_status'] = 'off'
  5871. # 馬達(單位RPM)
  5872. elif payload['command'] == 'tank_motor_status':
  5873. if payload['response'] == 'off':
  5874. D['tank_motor_status'] = '0'
  5875. else:
  5876. D['tank_motor_status'] = payload['response']
  5877. # 鼓風機(ON開/OFF關)
  5878. elif payload['command'] == 'tank_blower_status':
  5879. if payload['response'] == 'on':
  5880. # print('test')
  5881. D['tank_blower_status'] = 'on'
  5882. else:
  5883. D['tank_blower_status'] = 'off'
  5884. # 加熱棒 1(ON開/OFF關)
  5885. elif payload['command'] == 'tank_heater1_status':
  5886. if payload['response'] == 'on':
  5887. # print('test')
  5888. D['tank_heater1_status'] = 'on'
  5889. else:
  5890. D['tank_heater1_status'] = 'off'
  5891. # 加熱棒 2(ON開/OFF關)
  5892. elif payload['command'] == 'tank_heater2_status':
  5893. if payload['response'] == 'on':
  5894. # print('test')
  5895. D['tank_heater2_status'] = 'on'
  5896. else:
  5897. D['tank_heater2_status'] = 'off'
  5898. # 出料儲豆槽真空吸料機(ON吸料/OFF排氣)
  5899. elif payload['command'] == 'output_vacuum_status':
  5900. if payload['response'] == 'on':
  5901. # print('test')
  5902. D['output_vacuum_status'] = 'on'
  5903. else:
  5904. D['output_vacuum_status'] = 'off'
  5905. # 發酵槽 入料三通閥
  5906. elif payload['command'] == 'tank_threewayvalve_input_status':
  5907. if payload['response'] == 'on':
  5908. # print('test')
  5909. D['tank_threewayvalve_input_status'] = 'on'
  5910. else:
  5911. D['tank_threewayvalve_input_status'] = 'off'
  5912. # 發酵槽 總進水
  5913. elif payload['command'] == 'tank_solenoid_water_total_status':
  5914. if payload['response'] == 'on':
  5915. D['tank_solenoid_water_total_status'] = 'on'
  5916. else:
  5917. D['tank_solenoid_water_total_status'] = 'off'
  5918. # 發酵槽 桶內消毒/打菌
  5919. elif payload['command'] == 'tank_solenoid_disinfect_status':
  5920. if payload['response'] == 'on':
  5921. D['tank_solenoid_disinfect_status'] = 'on'
  5922. else:
  5923. D['tank_solenoid_disinfect_status'] = 'off'
  5924. # 發酵槽 夾層進水
  5925. elif payload['command'] == 'outer_solenoid_water_status':
  5926. if payload['response'] == 'on':
  5927. D['outer_solenoid_water_status'] = 'on'
  5928. else:
  5929. D['outer_solenoid_water_status'] = 'off'
  5930. # 發酵槽 桶內進水
  5931. elif payload['command'] == 'tank_solenoid_water_in_status':
  5932. if payload['response'] == 'on':
  5933. D['tank_solenoid_water_in_status'] = 'on'
  5934. else:
  5935. D['tank_solenoid_water_in_status'] = 'off'
  5936. # 發酵槽 雙核隔膜泵
  5937. elif payload['command'] == 'tank_pump_sensor_status':
  5938. if payload['response'] == 'on':
  5939. D['tank_pump_sensor_status'] = 'on'
  5940. else:
  5941. D['tank_pump_sensor_status'] = 'off'
  5942. # 發酵槽 感測器下豆三通閥
  5943. elif payload['command'] == 'tank_threewayvalve_bean_status':
  5944. if payload['response'] == 'on':
  5945. D['tank_threewayvalve_bean_status'] = 'on'
  5946. else:
  5947. D['tank_threewayvalve_bean_status'] = 'off'
  5948. # 發酵槽 外桶浮選三通閥
  5949. elif payload['command'] == 'outer_threewayvalve_float_status':
  5950. if payload['response'] == 'on':
  5951. D['outer_threewayvalve_float_status'] = 'on'
  5952. else:
  5953. D['outer_threewayvalve_float_status'] = 'off'
  5954. # 發酵槽 溫控開關
  5955. elif payload['command'] == 'tank_temp_enable_status':
  5956. if payload['response'] == 'on':
  5957. D['tank_temp_enable_status'] = 'on'
  5958. else:
  5959. D['tank_temp_enable_status'] = 'off'
  5960. # 發酵槽 設定溫度
  5961. elif payload['command'] == 'tank_temp_status':
  5962. if payload['response'] == 'off':
  5963. D['tank_temp_status'] = '0'
  5964. else:
  5965. D['tank_temp_status'] = payload['response']
  5966. # 清洗浮選槽 浮選槽注水
  5967. elif payload['command'] == 'tank_pump_waterFloat_status':
  5968. if payload['response'] == 'on':
  5969. D['tank_temp_status'] = 'on'
  5970. else:
  5971. D['tank_temp_status'] = 'off'
  5972. # 清洗浮選槽 清洗第 2 層灑水
  5973. elif payload['command'] == 'tank_pump_waterL2_status':
  5974. if payload['response'] == 'on':
  5975. D['tank_pump_waterL2_status'] = 'on'
  5976. else:
  5977. D['tank_pump_waterL2_status'] = 'off'
  5978. # 清洗浮選槽 清洗第 4 層灑水
  5979. elif payload['command'] == 'tank_pump_waterL4_status':
  5980. if payload['response'] == 'on':
  5981. D['tank_pump_waterL4_status'] = 'on'
  5982. else:
  5983. D['tank_pump_waterL4_status'] = 'off'
  5984. # 清洗浮選槽 清洗第 3 層電磁閥
  5985. elif payload['command'] == 'tank_solenoid_waterL3_status':
  5986. if payload['response'] == 'on':
  5987. D['tank_solenoid_waterL3_status'] = 'on'
  5988. else:
  5989. D['tank_solenoid_waterL3_status'] = 'off'
  5990. # 清洗浮選槽 清洗第 5 層電磁閥
  5991. elif payload['command'] == 'tank_solenoid_waterL5_status':
  5992. if payload['response'] == 'on':
  5993. D['tank_solenoid_waterL5_status'] = 'on'
  5994. else:
  5995. D['tank_solenoid_waterL5_status'] = 'off'
  5996. # 清洗浮選槽 步進馬達
  5997. elif payload['command'] == 'tank_stepping_motor_status':
  5998. if payload['response'] == 'off':
  5999. D['tank_stepping_motor_status'] = '0'
  6000. else:
  6001. D['tank_stepping_motor_status'] = payload['response']