views_1026備份.py 321 KB

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