93_DbLog.pm 339 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351
  1. ############################################################################################################################################
  2. # $Id: 93_DbLog.pm 17599 2018-10-22 19:25:46Z DS_Starter $
  3. #
  4. # 93_DbLog.pm
  5. # written by Dr. Boris Neubert 2007-12-30
  6. # e-mail: omega at online dot de
  7. #
  8. # modified and maintained by Tobias Faust since 2012-06-26
  9. # e-mail: tobias dot faust at online dot de
  10. #
  11. # reduceLog() created by Claudiu Schuster (rapster)
  12. #
  13. # redesigned 2016-2018 by DS_Starter with credits by
  14. # JoeAllb, DeeSpe
  15. #
  16. ############################################################################################################################################
  17. # Versions History done by DS_Starter & DeeSPe:
  18. #
  19. # 3.12.6 22.10.2018 fix timer not deleted if reopen after reopen xxx (Forum: https://forum.fhem.de/index.php/topic,91869.msg848433.html#msg848433)
  20. # 3.12.5 12.10.2018 charFilter: "\xB0C" substitution by "°C" added and usage in DbLog_Log changed
  21. # 3.12.4 10.10.2018 return non-saved datasets back in asynch mode only if transaction is used
  22. # 3.12.3 08.10.2018 Log output of recuceLogNbl enhanced, some functions renamed
  23. # 3.12.2 07.10.2018 $hash->{HELPER}{REOPEN_RUNS_UNTIL} contains the time the DB is closed
  24. # 3.12.1 19.09.2018 use Time::Local (forum:#91285)
  25. # 3.12.0 04.09.2018 corrected SVG-select (https://forum.fhem.de/index.php/topic,65860.msg815640.html#msg815640)
  26. # 3.11.0 02.09.2018 reduceLog, reduceLogNbl - optional "days newer than" part added
  27. # 3.10.10 05.08.2018 commandref revised reducelogNbl
  28. # 3.10.9 23.06.2018 commandref added hint about special characters in passwords
  29. # 3.10.8 21.04.2018 addLog - not available reading can be added as new one (forum:#86966)
  30. # 3.10.7 16.04.2018 fix generate addLog-event if device or reading was not found by addLog
  31. # 3.10.6 13.04.2018 verbose level in addlog changed if reading not found
  32. # 3.10.5 12.04.2018 fix warnings
  33. # 3.10.4 11.04.2018 fix addLog if no valueFn is used
  34. # 3.10.3 10.04.2018 minor fixes in addLog
  35. # 3.10.2 09.04.2018 add qualifier CN=<caller name> to addlog
  36. # 3.10.1 04.04.2018 changed event parsing of Weather
  37. # 3.10.0 02.04.2018 addLog consider DbLogExclude in Devices, keyword "!useExcludes" to switch off considering
  38. # DbLogExclude in addLog, DbLogExclude & DbLogInclude can handle "/" in Readingname,
  39. # commandref (reduceLog) revised
  40. # 3.9.0 17.03.2018 DbLog_ConnectPush state-handling changed, attribute excludeDevs enhanced in DbLog_Log
  41. # 3.8.9 10.03.2018 commandref revised
  42. # 3.8.8 05.03.2018 fix device doesn't exit if configuration couldn't be read
  43. # 3.8.7 28.02.2018 changed DbLog_sampleDataFn - no change limits got fron SVG, commandref revised
  44. # 3.8.6 25.02.2018 commandref revised (forum:#84953)
  45. # 3.8.5 16.02.2018 changed ParseEvent for Zwave
  46. # 3.8.4 07.02.2018 minor fixes of "$@", code review, eval for userCommand, DbLog_ExecSQL1 (forum:#83973)
  47. # 3.8.3 03.02.2018 call execmemcache only syncInterval/2 if cacheLimit reached and DB is not reachable, fix handling of
  48. # "$@" in DbLog_PushAsync
  49. # 3.8.2 31.01.2018 RaiseError => 1 in DbLog_ConnectPush, DbLog_ConnectNewDBH, configCheck improved
  50. # 3.8.1 29.01.2018 Use of uninitialized value $txt if addlog has no value
  51. # 3.8.0 26.01.2018 escape "|" in events to log events containing it
  52. # 3.7.1 25.01.2018 fix typo in commandref
  53. # 3.7.0 21.01.2018 parsed event with Log 5 added, configCheck enhanced by configuration read check
  54. # 3.6.5 19.01.2018 fix lot of logentries if disabled and db not available
  55. # 3.6.4 17.01.2018 improve DbLog_Shutdown, extend configCheck by shutdown preparation check
  56. # 3.6.3 14.01.2018 change verbose level of addlog "no Reading of device ..." message from 2 to 4
  57. # 3.6.2 07.01.2018 new attribute "exportCacheAppend", change function exportCache to respect attr exportCacheAppend,
  58. # fix DbLog_execmemcache verbose 5 message
  59. # 3.6.1 04.01.2018 change SQLite PRAGMA from NORMAL to FULL (Default Value of SQLite)
  60. # 3.6.0 20.12.2017 check global blockingCallMax in configCheck, configCheck now available for SQLITE
  61. # 3.5.0 18.12.2017 importCacheFile, addCacheLine uses useCharfilter option, filter only $event by charfilter
  62. # 3.4.0 10.12.2017 avoid print out {RUNNING_PID} by "list device"
  63. # 3.3.0 07.12.2017 avoid print out the content of cache by "list device"
  64. # 3.2.0 06.12.2017 change attribute "autocommit" to "commitMode", activate choice of autocommit/transaction in logging
  65. # Addlog/addCacheLine change $TIMESTAMP check,
  66. # rebuild DbLog_Push/DbLog_PushAsync due to bugfix in update current (Forum:#80519),
  67. # new attribute "useCharfilter" for Characterfilter usage
  68. # 3.1.1 05.12.2017 Characterfilter added to avoid unwanted characters what may destroy transaction
  69. # 3.1.0 05.12.2017 new set command addCacheLine
  70. # 3.0.0 03.12.2017 set begin_work depending of AutoCommit value, new attribute "autocommit", some minor corrections,
  71. # report working progress of reduceLog,reduceLogNbl in logfile (verbose 3), enhanced log output
  72. # (e.g. of execute_array)
  73. # 2.22.15 28.11.2017 some Log3 verbose level adapted
  74. # 2.22.14 18.11.2017 create state-events if state has been changed (Forum:#78867)
  75. # 2.22.13 20.10.2017 output of reopen command improved
  76. # 2.22.12 19.10.2017 avoid illegible messages in "state"
  77. # 2.22.11 13.10.2017 DbLogType expanded by SampleFill, DbLog_sampleDataFn adapted to sort case insensitive, commandref revised
  78. # 2.22.10 04.10.2017 Encode::encode_utf8 of $error, DbLog_PushAsyncAborted adapted to use abortArg (Forum:77472)
  79. # 2.22.9 04.10.2017 added hint to SVG/DbRep in commandref
  80. # 2.22.8 29.09.2017 avoid multiple entries in Dopdown-list when creating SVG by group Device:Reading in DbLog_sampleDataFn
  81. # 2.22.7 24.09.2017 minor fixes in configcheck
  82. # 2.22.6 22.09.2017 commandref revised
  83. # 2.22.5 05.09.2017 fix Internal MODE isn't set correctly after DEF is edited, nextsynch is not renewed if reopen is
  84. # set manually after reopen was set with a delay Forum:#76213, Link to 98_FileLogConvert.pm added
  85. # 2.22.4 27.08.2017 fhem chrashes if database DBD driver is not installed (Forum:#75894)
  86. # 2.22.3 11.08.2017 Forum:#74690, bug unitialized in row 4322 -> $ret .= SVG_txt("par_${r}_0", "", "$f0:$f1:$f2:$f3", 20);
  87. # 2.22.2 08.08.2017 Forum:#74690, bug unitialized in row 737 -> $ret .= ($fld[0]?$fld[0]:" ").'.'.($fld[1]?$fld[1]:" ");
  88. # 2.22.1 07.08.2017 attribute "suppressAddLogV3" to suppress verbose3-logentries created by DbLog_AddLog
  89. # 2.22.0 25.07.2017 attribute "addStateEvent" added
  90. # 2.21.3 24.07.2017 commandref revised
  91. # 2.21.2 19.07.2017 changed readCfg to report more error-messages
  92. # 2.21.1 18.07.2017 change configCheck for DbRep Report_Idx
  93. # 2.21.0 17.07.2017 standard timeout increased to 86400, enhanced explaination in configCheck
  94. # 2.20.0 15.07.2017 state-Events complemented with state by using $events = deviceEvents($dev_hash,1)
  95. # 2.19.0 11.07.2017 replace {DBMODEL} by {MODEL} completely
  96. # 2.18.3 04.07.2017 bugfix (links with $FW_ME deleted), MODEL as Internal (for statistic)
  97. # 2.18.2 29.06.2017 check of index for DbRep added
  98. # 2.18.1 25.06.2017 DbLog_configCheck/ DbLog_sqlget some changes, commandref revised
  99. # 2.18.0 24.06.2017 configCheck added (MySQL, PostgreSQL)
  100. # 2.17.1 17.06.2017 fix log-entries "utf8 enabled" if SVG's called, commandref revised, enable UTF8 for DbLog_get
  101. # 2.17.0 15.06.2017 enable UTF8 for MySQL (entry in configuration file necessary)
  102. # 2.16.11 03.06.2017 execmemcache changed for SQLite avoid logging if deleteOldDaysNbl or reduceLogNbL is running
  103. # 2.16.10 15.05.2017 commandref revised
  104. # 2.16.9.1 11.05.2017 set userCommand changed -
  105. # Forum: https://forum.fhem.de/index.php/topic,71808.msg633607.html#msg633607
  106. # 2.16.9 07.05.2017 addlog syntax changed to "addLog devspec:Reading [Value]"
  107. # 2.16.8 06.05.2017 in valueFN $VALUE and $UNIT can now be set to '' or 0
  108. # 2.16.7 20.04.2017 fix $now at addLog
  109. # 2.16.6 18.04.2017 AddLog set lasttime, lastvalue of dev_name, dev_reading
  110. # 2.16.5 16.04.2017 DbLog_checkUsePK changed again, new attribute noSupportPK
  111. # 2.16.4 15.04.2017 commandref completed, DbLog_checkUsePK changed (@usepkh = "", @usepkc = "")
  112. # 2.16.3 07.04.2017 evaluate reading in DbLog_AddLog as regular expression
  113. # 2.16.2 06.04.2017 sub DbLog_cutCol for cutting fields to maximum length, return to "$lv = "" if(!$lv);" because
  114. # of problems with MinIntervall, DbLogType-Logging in database cycle verbose 5, make $TIMESTAMP
  115. # changable by valueFn
  116. # 2.16.1 04.04.2017 changed regexp $exc =~ s/(\s|\s*\n)/,/g; , DbLog_AddLog changed, enhanced sort of listCache
  117. # 2.16.0 03.04.2017 new set-command addLog
  118. # 2.15.0 03.04.2017 new attr valueFn using for perl expression which may change variables and skip logging
  119. # unwanted datasets, change DbLog_ParseEvent for ZWAVE,
  120. # change DbLogExclude / DbLogInclude in DbLog_Log to "$lv = "" if(!defined($lv));"
  121. # 2.14.4 28.03.2017 pre-connection check in DbLog_execmemcache deleted (avoid possible blocking), attr excludeDevs
  122. # can be specified as devspec
  123. # 2.14.3 24.03.2017 DbLog_Get, DbLog_Push changed for better plotfork-support
  124. # 2.14.2 23.03.2017 new reading "lastCachefile"
  125. # 2.14.1 22.03.2017 cacheFile will be renamed after successful import by set importCachefile
  126. # 2.14.0 19.03.2017 new set-commands exportCache, importCachefile, new attr expimpdir, all cache relevant set-commands
  127. # only in drop-down list when asynch mode is used, minor fixes
  128. # 2.13.6 13.03.2017 plausibility check in set reduceLog(Nbl) enhanced, minor fixes
  129. # 2.13.5 20.02.2017 check presence of table current in DbLog_sampleDataFn
  130. # 2.13.4 18.02.2017 DbLog_Push & DbLog_PushAsync: separate eval-routines for history & current table execution
  131. # to decouple commit or rollback transactions, DbLog_sampleDataFn changed to avoid fhem from crash if table
  132. # current is not present and DbLogType isn't set
  133. # 2.13.3 18.02.2017 default timeout of DbLog_PushAsync increased to 1800,
  134. # delete {HELPER}{xx_PID} in reopen function
  135. # 2.13.2 16.02.2017 deleteOldDaysNbl added (non-blocking implementation of deleteOldDays)
  136. # 2.13.1 15.02.2017 clearReadings limited to readings which won't be recreated periodicly in asynch mode and set readings only blank,
  137. # eraseReadings added to delete readings except reading "state",
  138. # countNbl non-blocking by DeeSPe,
  139. # rename reduceLog non-blocking to reduceLogNbl and implement the old reduceLog too
  140. # 2.13.0 13.02.2017 made reduceLog non-blocking by DeeSPe
  141. # 2.12.5 11.02.2017 add support for primary key of PostgreSQL DB (Rel. 9.5) in both modes for current table
  142. # 2.12.4 09.02.2017 support for primary key of PostgreSQL DB (Rel. 9.5) in both modes only history table
  143. # 2.12.3 07.02.2017 set command clearReadings added
  144. # 2.12.2 07.02.2017 support for primary key of SQLITE DB in both modes
  145. # 2.12.1 05.02.2017 support for primary key of MySQL DB in synch mode
  146. # 2.12 04.02.2017 support for primary key of MySQL DB in asynch mode
  147. # 2.11.4 03.02.2017 check of missing modules added
  148. # 2.11.3 01.02.2017 make errorlogging of DbLog_PushAsync more identical to DbLog_Push
  149. # 2.11.2 31.01.2017 if attr colEvent, colReading, colValue is set, the limitation of fieldlength is also valid
  150. # for SQLite databases
  151. # 2.11.1 30.01.2017 output to central logfile enhanced for DbLog_Push
  152. # 2.11 28.01.2017 DbLog_connect substituted by DbLog_connectPush completely
  153. # 2.10.8 27.01.2017 DbLog_setinternalcols delayed at fhem start
  154. # 2.10.7 25.01.2017 $hash->{HELPER}{COLSET} in DbLog_setinternalcols, DbLog_Push changed due to
  155. # issue Turning on AutoCommit failed
  156. # 2.10.6 24.01.2017 DbLog_connect changed "connect_cashed" to "connect", DbLog_Get, DbLog_chartQuery now uses
  157. # DbLog_ConnectNewDBH, Attr asyncMode changed -> delete reading cacheusage reliable if mode was switched
  158. # 2.10.5 23.01.2017 count, userCommand, deleteOldDays now uses DbLog_ConnectNewDBH
  159. # DbLog_Push line 1107 changed
  160. # 2.10.4 22.01.2017 new sub DbLog_setinternalcols, new attributes colEvent, colReading, colValue
  161. # 2.10.3 21.01.2017 query of cacheEvents changed, attr timeout adjustable
  162. # 2.10.2 19.01.2017 ReduceLog now uses DbLog_ConnectNewDBH -> makes start of ReduceLog stable
  163. # 2.10.1 19.01.2017 commandref edited, cache events don't get lost even if other errors than "db not available" occure
  164. # 2.10 18.10.2017 new attribute cacheLimit, showNotifyTime
  165. # 2.9.3 17.01.2017 new sub DbLog_ConnectNewDBH (own new dbh for separate use in functions except logging functions),
  166. # DbLog_sampleDataFn, DbLog_dbReadings now use DbLog_ConnectNewDBH
  167. # 2.9.2 16.01.2017 new bugfix for SQLite issue SVGs, DbLog_Log changed to $dev_hash->{CHANGETIME}, DbLog_Push
  168. # changed (db handle new separated)
  169. # 2.9.1 14.01.2017 changed DbLog_ParseEvent to CallInstanceFn, renamed flushCache to purgeCache,
  170. # renamed syncCache to commitCache, attr cacheEvents changed to 0,1,2
  171. # 2.9 11.01.2017 changed DbLog_ParseEvent to CallFn
  172. # 2.8.9 11.01.2017 own $dbhp (new DbLog_ConnectPush) for synchronous logging, delete $hash->{HELPER}{RUNNING_PID}
  173. # if DEAD, add func flushCache, syncCache
  174. # 2.8.8 10.01.2017 connection check in Get added, avoid warning "commit/rollback ineffective with AutoCommit enabled"
  175. # 2.8.7 10.01.2017 bugfix no dropdown list in SVG if asynchronous mode activated (func DbLog_sampleDataFn)
  176. # 2.8.6 09.01.2017 Workaround for Warning begin_work failed: Turning off AutoCommit failed, start new timer of
  177. # DbLog_execmemcache after reducelog
  178. # 2.8.5 08.01.2017 attr syncEvents, cacheEvents added to minimize events
  179. # 2.8.4 08.01.2017 $readingFnAttributes added
  180. # 2.8.3 08.01.2017 set NOTIFYDEV changed to use notifyRegexpChanged (Forum msg555619), attr noNotifyDev added
  181. # 2.8.2 06.01.2017 commandref maintained to cover new functions
  182. # 2.8.1 05.01.2017 use Time::HiRes qw(gettimeofday tv_interval), bugfix $hash->{HELPER}{RUNNING_PID}
  183. # 2.8 03.01.2017 attr asyncMode, you have a choice to use blocking (as V2.5) or non-blocking asynchronous
  184. # with caching, attr showproctime
  185. # 2.7 02.01.2017 initial release non-blocking using BlockingCall
  186. # 2.6 02.01.2017 asynchron writing to DB using cache, attr syncInterval, set listCache
  187. # 2.5 29.12.2016 commandref maintained to cover new attributes, attr "excludeDevs" and "verbose4Devs" now
  188. # accepting Regex
  189. # 2.4.4 28.12.2016 Attribut "excludeDevs" to exclude devices from db-logging (only if $hash->{NOTIFYDEV} eq ".*")
  190. # 2.4.3 28.12.2016 function DbLog_Log: changed separators of @row_array -> better splitting
  191. # 2.4.2 28.12.2016 Attribut "verbose4Devs" to restrict verbose4 loggings of specific devices
  192. # 2.4.1 27.12.2016 DbLog_Push: improved update/insert into current, analyze execute_array -> ArrayTupleStatus
  193. # 2.4 24.12.2016 some improvements of verbose 4 logging
  194. # 2.3.1 23.12.2016 fix due to https://forum.fhem.de/index.php/topic,62998.msg545541.html#msg545541
  195. # 2.3 22.12.2016 fix eval{} in DbLog_Log
  196. # 2.2 21.12.2016 set DbLogType only to "History" if attr DbLogType not set
  197. # 2.1 21.12.2016 use execute_array in DbLog_Push
  198. # 2.0 19.12.2016 some improvements DbLog_Log
  199. # 1.9.3 17.12.2016 $hash->{NOTIFYDEV} added to process only events from devices are in Regex
  200. # 1.9.2 17.12.2016 some improvemnts DbLog_Log, DbLog_Push
  201. # 1.9.1 16.12.2016 DbLog_Log no using encode_base64
  202. # 1.9 16.12.2016 DbLog_Push changed to use deviceEvents
  203. # 1.8.1 16.12.2016 DbLog_Push changed
  204. # 1.8 15.12.2016 bugfix of don't logging all received events
  205. # 1.7.1 15.12.2016 attr procedure of "disabled" changed
  206. package main;
  207. use strict;
  208. use warnings;
  209. eval "use DBI;1" or my $DbLogMMDBI = "DBI";
  210. use Data::Dumper;
  211. use Blocking;
  212. use Time::HiRes qw(gettimeofday tv_interval);
  213. use Time::Local;
  214. use Encode qw(encode_utf8);
  215. no if $] >= 5.017011, warnings => 'experimental::smartmatch';
  216. my $DbLogVersion = "3.12.6";
  217. my %columns = ("DEVICE" => 64,
  218. "TYPE" => 64,
  219. "EVENT" => 512,
  220. "READING" => 64,
  221. "VALUE" => 128,
  222. "UNIT" => 32
  223. );
  224. sub DbLog_dbReadings($@);
  225. ################################################################
  226. sub DbLog_Initialize($)
  227. {
  228. my ($hash) = @_;
  229. $hash->{DefFn} = "DbLog_Define";
  230. $hash->{UndefFn} = "DbLog_Undef";
  231. $hash->{NotifyFn} = "DbLog_Log";
  232. $hash->{SetFn} = "DbLog_Set";
  233. $hash->{GetFn} = "DbLog_Get";
  234. $hash->{AttrFn} = "DbLog_Attr";
  235. $hash->{SVG_regexpFn} = "DbLog_regexpFn";
  236. $hash->{ShutdownFn} = "DbLog_Shutdown";
  237. $hash->{AttrList} = "addStateEvent:0,1 ".
  238. "commitMode:basic_ta:on,basic_ta:off,ac:on_ta:on,ac:on_ta:off,ac:off_ta:on ".
  239. "colEvent ".
  240. "colReading ".
  241. "colValue ".
  242. "disable:1,0 ".
  243. "DbLogType:Current,History,Current/History,SampleFill/History ".
  244. "shutdownWait ".
  245. "suppressUndef:0,1 ".
  246. "verbose4Devs ".
  247. "excludeDevs ".
  248. "expimpdir ".
  249. "exportCacheAppend:1,0 ".
  250. "syncInterval ".
  251. "noNotifyDev:1,0 ".
  252. "showproctime:1,0 ".
  253. "suppressAddLogV3:1,0 ".
  254. "asyncMode:1,0 ".
  255. "cacheEvents:2,1,0 ".
  256. "cacheLimit ".
  257. "noSupportPK:1,0 ".
  258. "syncEvents:1,0 ".
  259. "showNotifyTime:1,0 ".
  260. "timeout ".
  261. "useCharfilter:0,1 ".
  262. "valueFn:textField-long ".
  263. "DbLogSelectionMode:Exclude,Include,Exclude/Include ".
  264. $readingFnAttributes;
  265. # Das Attribut DbLogSelectionMode legt fest, wie die Device-Spezifischen Atrribute
  266. # DbLogExclude und DbLogInclude behandelt werden sollen.
  267. # - Exclude: Es wird nur das Device-spezifische Attribut Exclude beruecksichtigt,
  268. # d.h. generell wird alles geloggt, was nicht per DBLogExclude ausgeschlossen wird
  269. # - Include: Es wird nur das Device-spezifische Attribut Include beruecksichtigt,
  270. # d.h. generell wird nichts geloggt, ausßer dem was per DBLogInclude eingeschlossen wird
  271. # - Exclude/Include: Es wird zunaechst Exclude geprueft und bei Ausschluß wird ggf. noch zusaetzlich Include geprueft,
  272. # d.h. generell wird alles geloggt, es sei denn es wird per DBLogExclude ausgeschlossen.
  273. # Wird es von DBLogExclude ausgeschlossen, kann es trotzdem wieder per DBLogInclude
  274. # eingeschlossen werden.
  275. addToAttrList("DbLogInclude");
  276. addToAttrList("DbLogExclude");
  277. $hash->{FW_detailFn} = "DbLog_fhemwebFn";
  278. $hash->{SVG_sampleDataFn} = "DbLog_sampleDataFn";
  279. }
  280. ###############################################################
  281. sub DbLog_Define($@)
  282. {
  283. my ($hash, $def) = @_;
  284. my @a = split("[ \t][ \t]*", $def);
  285. return "Error: Perl module ".$DbLogMMDBI." is missing.
  286. Install it on Debian with: sudo apt-get install libdbi-perl" if($DbLogMMDBI);
  287. return "wrong syntax: define <name> DbLog configuration regexp"
  288. if(int(@a) != 4);
  289. $hash->{CONFIGURATION} = $a[2];
  290. my $regexp = $a[3];
  291. eval { "Hallo" =~ m/^$regexp$/ };
  292. return "Bad regexp: $@" if($@);
  293. $hash->{REGEXP} = $regexp;
  294. $hash->{VERSION} = $DbLogVersion;
  295. $hash->{MODE} = AttrVal($hash->{NAME}, "asyncMode", undef)?"asynchronous":"synchronous"; # Mode setzen Forum:#76213
  296. $hash->{HELPER}{OLDSTATE} = "initialized";
  297. # nur Events dieser Devices an NotifyFn weiterleiten, NOTIFYDEV wird gesetzt wenn möglich
  298. notifyRegexpChanged($hash, $regexp);
  299. #remember PID for plotfork
  300. $hash->{PID} = $$;
  301. # CacheIndex für Events zum asynchronen Schreiben in DB
  302. $hash->{cache}{index} = 0;
  303. # read configuration data
  304. my $ret = DbLog_readCfg($hash);
  305. if ($ret) {
  306. # return on error while reading configuration
  307. Log3($hash->{NAME}, 1, "DbLog $hash->{NAME} - Error while reading $hash->{CONFIGURATION}: '$ret' ");
  308. return $ret;
  309. }
  310. # set used COLUMNS
  311. InternalTimer(gettimeofday()+2, "DbLog_setinternalcols", $hash, 0);
  312. readingsSingleUpdate($hash, 'state', 'waiting for connection', 1);
  313. DbLog_ConnectPush($hash);
  314. # initial execution of DbLog_execmemcache
  315. DbLog_execmemcache($hash);
  316. return undef;
  317. }
  318. ################################################################
  319. sub DbLog_Undef($$) {
  320. my ($hash, $name) = @_;
  321. my $dbh= $hash->{DBHP};
  322. BlockingKill($hash->{HELPER}{".RUNNING_PID"}) if($hash->{HELPER}{".RUNNING_PID"});
  323. BlockingKill($hash->{HELPER}{REDUCELOG_PID}) if($hash->{HELPER}{REDUCELOG_PID});
  324. BlockingKill($hash->{HELPER}{COUNT_PID}) if($hash->{HELPER}{COUNT_PID});
  325. BlockingKill($hash->{HELPER}{DELDAYS_PID}) if($hash->{HELPER}{DELDAYS_PID});
  326. $dbh->disconnect() if(defined($dbh));
  327. RemoveInternalTimer($hash);
  328. return undef;
  329. }
  330. ################################################################
  331. sub DbLog_Shutdown($) {
  332. my ($hash) = @_;
  333. my $name = $hash->{NAME};
  334. $hash->{HELPER}{SHUTDOWNSEQ} = 1;
  335. DbLog_execmemcache($hash);
  336. my $shutdownWait = AttrVal($name,"shutdownWait",undef);
  337. if(defined($shutdownWait)) {
  338. Log3($name, 2, "DbLog $name - waiting for shutdown $shutdownWait seconds ...");
  339. sleep($shutdownWait);
  340. Log3($name, 2, "DbLog $name - continuing shutdown sequence");
  341. }
  342. if($hash->{HELPER}{".RUNNING_PID"}) {
  343. BlockingKill($hash->{HELPER}{".RUNNING_PID"});
  344. }
  345. return undef;
  346. }
  347. ################################################################
  348. #
  349. # Wird bei jeder Aenderung eines Attributes dieser
  350. # DbLog-Instanz aufgerufen
  351. #
  352. ################################################################
  353. sub DbLog_Attr(@) {
  354. my($cmd,$name,$aName,$aVal) = @_;
  355. # my @a = @_;
  356. my $hash = $defs{$name};
  357. my $dbh = $hash->{DBHP};
  358. my $do = 0;
  359. if($cmd eq "set") {
  360. if ($aName eq "syncInterval" || $aName eq "cacheLimit" || $aName eq "timeout") {
  361. unless ($aVal =~ /^[0-9]+$/) { return " The Value of $aName is not valid. Use only figures 0-9 !";}
  362. }
  363. if( $aName eq 'valueFn' ) {
  364. my %specials= (
  365. "%TIMESTAMP" => $name,
  366. "%DEVICE" => $name,
  367. "%DEVICETYPE" => $name,
  368. "%EVENT" => $name,
  369. "%READING" => $name,
  370. "%VALUE" => $name,
  371. "%UNIT" => $name,
  372. "%IGNORE" => $name,
  373. "%CN" => $name,
  374. );
  375. my $err = perlSyntaxCheck($aVal, %specials);
  376. return $err if($err);
  377. }
  378. }
  379. if($aName eq "colEvent" || $aName eq "colReading" || $aName eq "colValue") {
  380. if ($cmd eq "set" && $aVal) {
  381. unless ($aVal =~ /^[0-9]+$/) { return " The Value of $aName is not valid. Use only figures 0-9 !";}
  382. }
  383. InternalTimer(gettimeofday()+0.5, "DbLog_setinternalcols", $hash, 0);
  384. }
  385. if($aName eq "asyncMode") {
  386. if ($cmd eq "set" && $aVal) {
  387. $hash->{MODE} = "asynchronous";
  388. InternalTimer(gettimeofday()+2, "DbLog_execmemcache", $hash, 0);
  389. } else {
  390. $hash->{MODE} = "synchronous";
  391. delete($defs{$name}{READINGS}{NextSync});
  392. delete($defs{$name}{READINGS}{CacheUsage});
  393. InternalTimer(gettimeofday()+5, "DbLog_execmemcache", $hash, 0);
  394. }
  395. }
  396. if($aName eq "commitMode") {
  397. if ($dbh) {
  398. $dbh->commit() if(!$dbh->{AutoCommit});
  399. $dbh->disconnect();
  400. }
  401. }
  402. if($aName eq "showproctime") {
  403. if ($cmd ne "set" || !$aVal) {
  404. delete($defs{$name}{READINGS}{background_processing_time});
  405. delete($defs{$name}{READINGS}{sql_processing_time});
  406. }
  407. }
  408. if($aName eq "showNotifyTime") {
  409. if ($cmd ne "set" || !$aVal) {
  410. delete($defs{$name}{READINGS}{notify_processing_time});
  411. }
  412. }
  413. if($aName eq "noNotifyDev") {
  414. my $regexp = $hash->{REGEXP};
  415. if ($cmd eq "set" && $aVal) {
  416. delete($hash->{NOTIFYDEV});
  417. } else {
  418. notifyRegexpChanged($hash, $regexp);
  419. }
  420. }
  421. if ($aName eq "disable") {
  422. my $async = AttrVal($name, "asyncMode", 0);
  423. if($cmd eq "set") {
  424. $do = ($aVal) ? 1 : 0;
  425. }
  426. $do = 0 if($cmd eq "del");
  427. my $val = ($do == 1 ? "disabled" : "active");
  428. # letzter CacheSync vor disablen
  429. DbLog_execmemcache($hash) if($do == 1);
  430. readingsSingleUpdate($hash, "state", $val, 1);
  431. $hash->{HELPER}{OLDSTATE} = $val;
  432. if ($do == 0) {
  433. InternalTimer(gettimeofday()+2, "DbLog_execmemcache", $hash, 0) if($async);
  434. InternalTimer(gettimeofday()+2, "DbLog_ConnectPush", $hash, 0) if(!$async);
  435. }
  436. }
  437. return undef;
  438. }
  439. ################################################################
  440. sub DbLog_Set($@) {
  441. my ($hash, @a) = @_;
  442. my $name = $hash->{NAME};
  443. my $async = AttrVal($name, "asyncMode", undef);
  444. my $usage = "Unknown argument, choose one of reduceLog reduceLogNbl reopen rereadcfg:noArg count:noArg countNbl:noArg
  445. deleteOldDays deleteOldDaysNbl userCommand clearReadings:noArg
  446. eraseReadings:noArg addLog ";
  447. $usage .= "listCache:noArg addCacheLine purgeCache:noArg commitCache:noArg exportCache:nopurge,purgecache " if (AttrVal($name, "asyncMode", undef));
  448. $usage .= "configCheck:noArg ";
  449. my (@logs,$dir);
  450. if (!AttrVal($name,"expimpdir",undef)) {
  451. $dir = $attr{global}{modpath}."/log/";
  452. } else {
  453. $dir = AttrVal($name,"expimpdir",undef);
  454. }
  455. opendir(DIR,$dir);
  456. my $sd = "cache_".$name."_";
  457. while (my $file = readdir(DIR)) {
  458. next unless (-f "$dir/$file");
  459. next unless ($file =~ /^$sd/);
  460. push @logs,$file;
  461. }
  462. closedir(DIR);
  463. my $cj = join(",",reverse(sort @logs)) if (@logs);
  464. if (@logs) {
  465. $usage .= "importCachefile:".$cj." ";
  466. } else {
  467. $usage .= "importCachefile ";
  468. }
  469. return $usage if(int(@a) < 2);
  470. my $dbh = $hash->{DBHP};
  471. my $db = (split(/;|=/, $hash->{dbconn}))[1];
  472. my $ret;
  473. if ($a[1] eq 'reduceLog') {
  474. my ($od,$nd) = split(":",$a[2]); # $od - Tage älter als , $nd - Tage neuer als
  475. if ($nd && $nd <= $od) {return "The second day value must be greater than the first one ! ";}
  476. if (defined($a[3]) && $a[3] !~ /^average$|^average=.+|^EXCLUDE=.+$|^INCLUDE=.+$/i) {
  477. return "ReduceLog syntax error in set command. Please see commandref for help.";
  478. }
  479. if (defined $a[2] && $a[2] =~ /(^\d+$)|(^\d+:\d+$)/) {
  480. $ret = DbLog_reduceLog($hash,@a);
  481. InternalTimer(gettimeofday()+5, "DbLog_execmemcache", $hash, 0);
  482. } else {
  483. Log3($name, 1, "DbLog $name: reduceLog error, no <days> given.");
  484. $ret = "reduceLog error, no <days> given.";
  485. }
  486. }
  487. elsif ($a[1] eq 'reduceLogNbl') {
  488. my ($od,$nd) = split(":",$a[2]); # $od - Tage älter als , $nd - Tage neuer als
  489. if ($nd && $nd <= $od) {return "The second day value must be greater than the first one ! ";}
  490. if (defined($a[3]) && $a[3] !~ /^average$|^average=.+|^EXCLUDE=.+$|^INCLUDE=.+$/i) {
  491. return "ReduceLogNbl syntax error in set command. Please see commandref for help.";
  492. }
  493. if (defined $a[2] && $a[2] =~ /(^\d+$)|(^\d+:\d+$)/) {
  494. if ($hash->{HELPER}{REDUCELOG_PID} && $hash->{HELPER}{REDUCELOG_PID}{pid} !~ m/DEAD/) {
  495. $ret = "reduceLogNbl already in progress. Please wait for the current process to finish.";
  496. } else {
  497. delete $hash->{HELPER}{REDUCELOG_PID};
  498. my @b = @a;
  499. shift(@b);
  500. readingsSingleUpdate($hash,"reduceLogState","@b started",1);
  501. $hash->{HELPER}{REDUCELOG} = \@a;
  502. $hash->{HELPER}{REDUCELOG_PID} = BlockingCall("DbLog_reduceLogNbl","$name","DbLog_reduceLogNbl_finished");
  503. return;
  504. }
  505. } else {
  506. Log3($name, 1, "DbLog $name: reduceLogNbl syntax error, no <days>[:<days>] given.");
  507. $ret = "reduceLogNbl error, no <days> given.";
  508. }
  509. }
  510. elsif ($a[1] eq 'clearReadings') {
  511. my @allrds = keys%{$defs{$name}{READINGS}};
  512. foreach my $key(@allrds) {
  513. next if($key =~ m/state/ || $key =~ m/CacheUsage/ || $key =~ m/NextSync/);
  514. readingsSingleUpdate($hash,$key," ",0);
  515. }
  516. }
  517. elsif ($a[1] eq 'eraseReadings') {
  518. my @allrds = keys%{$defs{$name}{READINGS}};
  519. foreach my $key(@allrds) {
  520. delete($defs{$name}{READINGS}{$key}) if($key !~ m/^state$/);
  521. }
  522. }
  523. elsif ($a[1] eq 'addLog') {
  524. unless ($a[2]) { return "The argument of $a[1] is not valid. Please check commandref.";}
  525. my $nce = ("\!useExcludes" ~~ @a)?1:0;
  526. map(s/\!useExcludes//g, @a);
  527. my $cn;
  528. if(/CN=/ ~~ @a) {
  529. my $t = join(" ",@a);
  530. ($cn) = ($t =~ /^.*CN=(\w+).*$/);
  531. map(s/CN=$cn//g, @a);
  532. }
  533. DbLog_AddLog($hash,$a[2],$a[3],$nce,$cn);
  534. my $skip_trigger = 1; # kein Event erzeugen falls addLog device/reading not found aber Abarbeitung erfolgreich
  535. return undef,$skip_trigger;
  536. }
  537. elsif ($a[1] eq 'reopen') {
  538. if ($dbh) {
  539. $dbh->commit() if(!$dbh->{AutoCommit});
  540. $dbh->disconnect();
  541. }
  542. if (!$a[2]) {
  543. Log3($name, 3, "DbLog $name: Reopen requested.");
  544. DbLog_ConnectPush($hash);
  545. if($hash->{HELPER}{REOPEN_RUNS}) {
  546. delete $hash->{HELPER}{REOPEN_RUNS};
  547. delete $hash->{HELPER}{REOPEN_RUNS_UNTIL};
  548. RemoveInternalTimer($hash, "DbLog_reopen");
  549. }
  550. DbLog_execmemcache($hash) if($async);
  551. $ret = "Reopen executed.";
  552. } else {
  553. unless ($a[2] =~ /^[0-9]+$/) { return " The Value of $a[1]-time is not valid. Use only figures 0-9 !";}
  554. # Statusbit "Kein Schreiben in DB erlauben" wenn reopen mit Zeitangabe
  555. $hash->{HELPER}{REOPEN_RUNS} = $a[2];
  556. # falls ein hängender Prozess vorhanden ist -> löschen
  557. BlockingKill($hash->{HELPER}{".RUNNING_PID"}) if($hash->{HELPER}{".RUNNING_PID"});
  558. BlockingKill($hash->{HELPER}{REDUCELOG_PID}) if($hash->{HELPER}{REDUCELOG_PID});
  559. BlockingKill($hash->{HELPER}{COUNT_PID}) if($hash->{HELPER}{COUNT_PID});
  560. BlockingKill($hash->{HELPER}{DELDAYS_PID}) if($hash->{HELPER}{DELDAYS_PID});
  561. delete $hash->{HELPER}{".RUNNING_PID"};
  562. delete $hash->{HELPER}{COUNT_PID};
  563. delete $hash->{HELPER}{DELDAYS_PID};
  564. delete $hash->{HELPER}{REDUCELOG_PID};
  565. my $ts = (split(" ",FmtDateTime(gettimeofday()+$a[2])))[1];
  566. Log3($name, 2, "DbLog $name: Connection closed until $ts ($a[2] seconds).");
  567. readingsSingleUpdate($hash, "state", "closed until $ts ($a[2] seconds)", 1);
  568. InternalTimer(gettimeofday()+$a[2], "DbLog_reopen", $hash, 0);
  569. $hash->{HELPER}{REOPEN_RUNS_UNTIL} = $ts;
  570. }
  571. }
  572. elsif ($a[1] eq 'rereadcfg') {
  573. Log3($name, 3, "DbLog $name: Rereadcfg requested.");
  574. if ($dbh) {
  575. $dbh->commit() if(!$dbh->{AutoCommit});
  576. $dbh->disconnect();
  577. }
  578. $ret = DbLog_readCfg($hash);
  579. return $ret if $ret;
  580. DbLog_ConnectPush($hash);
  581. $ret = "Rereadcfg executed.";
  582. }
  583. elsif ($a[1] eq 'purgeCache') {
  584. delete $hash->{cache};
  585. readingsSingleUpdate($hash, 'CacheUsage', 0, 1);
  586. }
  587. elsif ($a[1] eq 'commitCache') {
  588. DbLog_execmemcache($hash);
  589. }
  590. elsif ($a[1] eq 'listCache') {
  591. my $cache;
  592. foreach my $key (sort{$a <=>$b}keys%{$hash->{cache}{".memcache"}}) {
  593. $cache .= $key." => ".$hash->{cache}{".memcache"}{$key}."\n";
  594. }
  595. return $cache;
  596. }
  597. elsif ($a[1] eq 'addCacheLine') {
  598. if(!$a[2]) {
  599. return "Syntax error in set $a[1] command. Use this line format: YYYY-MM-DD HH:MM:SS|<device>|<type>|<event>|<reading>|<value>|[<unit>] ";
  600. }
  601. my @b = @a;
  602. shift @b;
  603. shift @b;
  604. my $aa;
  605. foreach my $k (@b) {
  606. $aa .= "$k ";
  607. }
  608. chop($aa); #letztes Leerzeichen entfernen
  609. $aa = DbLog_charfilter($aa) if(AttrVal($name, "useCharfilter",0));
  610. my ($i_timestamp, $i_dev, $i_type, $i_evt, $i_reading, $i_val, $i_unit) = split("\\|",$aa);
  611. if($i_timestamp !~ /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/ || !$i_dev || !$i_reading) {
  612. return "Syntax error in set $a[1] command. Use this line format: YYYY-MM-DD HH:MM:SS|<device>|<type>|<event>|<reading>|<value>|[<unit>] ";
  613. }
  614. my ($yyyy, $mm, $dd, $hh, $min, $sec) = ($i_timestamp =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/);
  615. eval { my $ts = timelocal($sec, $min, $hh, $dd, $mm-1, $yyyy-1900); };
  616. if ($@) {
  617. my @l = split (/at/, $@);
  618. return " Timestamp is out of range - $l[0]";
  619. }
  620. DbLog_addCacheLine($hash,$i_timestamp,$i_dev,$i_type,$i_evt,$i_reading,$i_val,$i_unit);
  621. }
  622. elsif ($a[1] eq 'configCheck') {
  623. my $check = DbLog_configcheck($hash);
  624. return $check;
  625. }
  626. elsif ($a[1] eq 'exportCache') {
  627. my $cln;
  628. my $crows = 0;
  629. my ($out,$outfile,$error);
  630. my $now = strftime('%Y-%m-%d_%H-%M-%S',localtime);
  631. # return wenn "reopen" mit Ablaufzeit gestartet ist oder disabled, nicht im asynch-Mode
  632. return if(IsDisabled($name) || $hash->{HELPER}{REOPEN_RUNS});
  633. return if(!AttrVal($name, "asyncMode", undef));
  634. if(@logs && AttrVal($name, "exportCacheAppend", 0)) {
  635. # exportiertes Cachefile existiert und es soll an das neueste angehängt werden
  636. $outfile = $dir.pop(@logs);
  637. $out = ">>$outfile";
  638. } else {
  639. $outfile = $dir."cache_".$name."_".$now;
  640. $out = ">$outfile";
  641. }
  642. if(open(FH, $out)) {
  643. binmode (FH);
  644. } else {
  645. readingsSingleUpdate($hash, "lastCachefile", $outfile." - Error - ".$!, 1);
  646. $error = "could not open ".$outfile.": ".$!;
  647. }
  648. if(!$error) {
  649. foreach my $key (sort(keys%{$hash->{cache}{".memcache"}})) {
  650. $cln = $hash->{cache}{".memcache"}{$key}."\n";
  651. print FH $cln ;
  652. $crows++;
  653. }
  654. close(FH);
  655. readingsSingleUpdate($hash, "lastCachefile", $outfile." (".$crows." cache rows exported)", 1);
  656. }
  657. # readingsSingleUpdate($hash, "state", $crows." cache rows exported to ".$outfile, 1);
  658. my $state = $error?$error:(IsDisabled($name))?"disabled":"connected";
  659. my $evt = ($state eq $hash->{HELPER}{OLDSTATE})?0:1;
  660. readingsSingleUpdate($hash, "state", $state, $evt);
  661. $hash->{HELPER}{OLDSTATE} = $state;
  662. Log3($name, 3, "DbLog $name: $crows cache rows exported to $outfile.");
  663. if (lc($a[-1]) =~ m/^purgecache/i) {
  664. delete $hash->{cache};
  665. readingsSingleUpdate($hash, 'CacheUsage', 0, 1);
  666. Log3($name, 3, "DbLog $name: Cache purged after exporting rows to $outfile.");
  667. }
  668. return;
  669. }
  670. elsif ($a[1] eq 'importCachefile') {
  671. my $cln;
  672. my $crows = 0;
  673. my $infile;
  674. my @row_array;
  675. readingsSingleUpdate($hash, "lastCachefile", "", 0);
  676. # return wenn "reopen" mit Ablaufzeit gestartet ist oder disabled
  677. return if(IsDisabled($name) || $hash->{HELPER}{REOPEN_RUNS});
  678. if (!$a[2]) {
  679. return "Wrong function-call. Use set <name> importCachefile <file> without directory (see attr expimpdir)." ;
  680. } else {
  681. $infile = $dir.$a[2];
  682. }
  683. if (open(FH, "$infile")) {
  684. binmode (FH);
  685. } else {
  686. return "could not open ".$infile.": ".$!;
  687. }
  688. while (<FH>) {
  689. my $row = $_;
  690. $row = DbLog_charfilter($row) if(AttrVal($name, "useCharfilter",0));
  691. push(@row_array, $row);
  692. $crows++;
  693. }
  694. close(FH);
  695. if(@row_array) {
  696. my $error = DbLog_Push($hash, 1, @row_array);
  697. if($error) {
  698. readingsSingleUpdate($hash, "lastCachefile", $infile." - Error - ".$!, 1);
  699. readingsSingleUpdate($hash, "state", $error, 1);
  700. Log3 $name, 5, "DbLog $name -> DbLog_Push Returncode: $error";
  701. } else {
  702. unless(rename($dir.$a[2], $dir."impdone_".$a[2])) {
  703. Log3($name, 2, "DbLog $name: cachefile $infile couldn't be renamed after import !");
  704. }
  705. readingsSingleUpdate($hash, "lastCachefile", $infile." import successful", 1);
  706. readingsSingleUpdate($hash, "state", $crows." cache rows processed from ".$infile, 1);
  707. Log3($name, 3, "DbLog $name: $crows cache rows processed from $infile.");
  708. }
  709. } else {
  710. readingsSingleUpdate($hash, "state", "no rows in ".$infile, 1);
  711. Log3($name, 3, "DbLog $name: $infile doesn't contain any rows - no imports done.");
  712. }
  713. return;
  714. }
  715. elsif ($a[1] eq 'count') {
  716. $dbh = DbLog_ConnectNewDBH($hash);
  717. if(!$dbh) {
  718. Log3($name, 1, "DbLog $name: DBLog_Set - count - DB connect not possible");
  719. return;
  720. } else {
  721. Log3($name, 4, "DbLog $name: Records count requested.");
  722. my $c = $dbh->selectrow_array('SELECT count(*) FROM history');
  723. readingsSingleUpdate($hash, 'countHistory', $c ,1);
  724. $c = $dbh->selectrow_array('SELECT count(*) FROM current');
  725. readingsSingleUpdate($hash, 'countCurrent', $c ,1);
  726. $dbh->disconnect();
  727. InternalTimer(gettimeofday()+5, "DbLog_execmemcache", $hash, 0);
  728. }
  729. }
  730. elsif ($a[1] eq 'countNbl') {
  731. if ($hash->{HELPER}{COUNT_PID} && $hash->{HELPER}{COUNT_PID}{pid} !~ m/DEAD/){
  732. $ret = "DbLog count already in progress. Please wait for the current process to finish.";
  733. } else {
  734. delete $hash->{HELPER}{COUNT_PID};
  735. $hash->{HELPER}{COUNT_PID} = BlockingCall("DbLog_countNbl","$name","DbLog_countNbl_finished");
  736. return;
  737. }
  738. }
  739. elsif ($a[1] eq 'deleteOldDays') {
  740. Log3 ($name, 3, "DbLog $name -> Deletion of records older than $a[2] days in database $db requested");
  741. my ($c, $cmd);
  742. $dbh = DbLog_ConnectNewDBH($hash);
  743. if(!$dbh) {
  744. Log3($name, 1, "DbLog $name: DBLog_Set - deleteOldDays - DB connect not possible");
  745. return;
  746. } else {
  747. $cmd = "delete from history where TIMESTAMP < ";
  748. if ($hash->{MODEL} eq 'SQLITE') { $cmd .= "datetime('now', '-$a[2] days')"; }
  749. elsif ($hash->{MODEL} eq 'MYSQL') { $cmd .= "DATE_SUB(CURDATE(),INTERVAL $a[2] DAY)"; }
  750. elsif ($hash->{MODEL} eq 'POSTGRESQL') { $cmd .= "NOW() - INTERVAL '$a[2]' DAY"; }
  751. else { $cmd = undef; $ret = 'Unknown database type. Maybe you can try userCommand anyway.'; }
  752. if(defined($cmd)) {
  753. $c = $dbh->do($cmd);
  754. $c = 0 if($c == 0E0);
  755. eval {$dbh->commit() if(!$dbh->{AutoCommit});};
  756. $dbh->disconnect();
  757. Log3 ($name, 3, "DbLog $name -> deleteOldDays finished. $c entries of database $db deleted.");
  758. readingsSingleUpdate($hash, 'lastRowsDeleted', $c ,1);
  759. }
  760. InternalTimer(gettimeofday()+5, "DbLog_execmemcache", $hash, 0);
  761. }
  762. }
  763. elsif ($a[1] eq 'deleteOldDaysNbl') {
  764. if (defined $a[2] && $a[2] =~ /^\d+$/) {
  765. if ($hash->{HELPER}{DELDAYS_PID} && $hash->{HELPER}{DELDAYS_PID}{pid} !~ m/DEAD/) {
  766. $ret = "deleteOldDaysNbl already in progress. Please wait for the current process to finish.";
  767. } else {
  768. delete $hash->{HELPER}{DELDAYS_PID};
  769. $hash->{HELPER}{DELDAYS} = $a[2];
  770. Log3 ($name, 3, "DbLog $name -> Deletion of records older than $a[2] days in database $db requested");
  771. $hash->{HELPER}{DELDAYS_PID} = BlockingCall("DbLog_deldaysNbl","$name","DbLog_deldaysNbl_done");
  772. return;
  773. }
  774. } else {
  775. Log3($name, 1, "DbLog $name: deleteOldDaysNbl error, no <days> given.");
  776. $ret = "deleteOldDaysNbl error, no <days> given.";
  777. }
  778. }
  779. elsif ($a[1] eq 'userCommand') {
  780. $dbh = DbLog_ConnectNewDBH($hash);
  781. if(!$dbh) {
  782. Log3($name, 1, "DbLog $name: DBLog_Set - userCommand - DB connect not possible");
  783. return;
  784. } else {
  785. Log3($name, 4, "DbLog $name: userCommand execution requested.");
  786. my ($c, @cmd, $sql);
  787. @cmd = @a;
  788. shift(@cmd); shift(@cmd);
  789. $sql = join(" ",@cmd);
  790. readingsSingleUpdate($hash, 'userCommand', $sql, 1);
  791. $dbh->{RaiseError} = 1;
  792. $dbh->{PrintError} = 0;
  793. my $error;
  794. eval { $c = $dbh->selectrow_array($sql); };
  795. if($@) {
  796. $error = $@;
  797. Log3($name, 1, "DbLog $name: DBLog_Set - $error");
  798. }
  799. my $res = $error?$error:(defined($c))?$c:"no result";
  800. Log3($name, 4, "DbLog $name: DBLog_Set - userCommand - result: $res");
  801. readingsSingleUpdate($hash, 'userCommandResult', $res ,1);
  802. $dbh->disconnect();
  803. InternalTimer(gettimeofday()+5, "DbLog_execmemcache", $hash, 0);
  804. }
  805. }
  806. else { $ret = $usage; }
  807. return $ret;
  808. }
  809. ###############################################################################################
  810. #
  811. # Exrahieren des Filters aus der ColumnsSpec (gplot-Datei)
  812. #
  813. # Die grundlegend idee ist das jeder svg plot einen filter hat der angibt
  814. # welches device und reading dargestellt wird so das der plot sich neu
  815. # lädt wenn es ein entsprechendes event gibt.
  816. #
  817. # Parameter: Quell-Instanz-Name, und alle FileLog-Parameter, die diese Instanz betreffen.
  818. # Quelle: http://forum.fhem.de/index.php/topic,40176.msg325200.html#msg325200
  819. ###############################################################################################
  820. sub DbLog_regexpFn($$) {
  821. my ($name, $filter) = @_;
  822. my $ret;
  823. my @a = split( ' ', $filter );
  824. for(my $i = 0; $i < int(@a); $i++) {
  825. my @fld = split(":", $a[$i]);
  826. $ret .= '|' if( $ret );
  827. no warnings 'uninitialized'; # Forum:74690, bug unitialized
  828. $ret .= $fld[0] .'.'. $fld[1];
  829. use warnings;
  830. }
  831. return $ret;
  832. }
  833. ################################################################
  834. #
  835. # Parsefunktion, abhaengig vom Devicetyp
  836. #
  837. ################################################################
  838. sub DbLog_ParseEvent($$$)
  839. {
  840. my ($device, $type, $event)= @_;
  841. my @result;
  842. my $reading;
  843. my $value;
  844. my $unit;
  845. # Splitfunktion der Eventquelle aufrufen (ab 2.9.1)
  846. ($reading, $value, $unit) = CallInstanceFn($device, "DbLog_splitFn", $event, $device);
  847. # undef bedeutet, Modul stellt keine DbLog_splitFn bereit
  848. if($reading) {
  849. return ($reading, $value, $unit);
  850. }
  851. # split the event into reading, value and unit
  852. # "day-temp: 22.0 (Celsius)" -> "day-temp", "22.0 (Celsius)"
  853. my @parts = split(/: /,$event);
  854. $reading = shift @parts;
  855. if(@parts == 2) {
  856. $value = $parts[0];
  857. $unit = $parts[1];
  858. } else {
  859. $value = join(": ", @parts);
  860. $unit = "";
  861. }
  862. #default
  863. if(!defined($reading)) { $reading = ""; }
  864. if(!defined($value)) { $value = ""; }
  865. if( $value eq "" ) {
  866. $reading= "state";
  867. $value= $event;
  868. }
  869. #globales Abfangen von
  870. # - temperature
  871. # - humidity
  872. if ($reading =~ m(^temperature)) { $unit= "°C"; } # wenn reading mit temperature beginnt
  873. elsif($reading =~ m(^humidity)) { $unit= "%"; }
  874. # the interpretation of the argument depends on the device type
  875. # EMEM, M232Counter, M232Voltage return plain numbers
  876. if(($type eq "M232Voltage") ||
  877. ($type eq "M232Counter") ||
  878. ($type eq "EMEM")) {
  879. }
  880. #OneWire
  881. elsif(($type eq "OWMULTI")) {
  882. if(int(@parts)>1) {
  883. $reading = "data";
  884. $value = $event;
  885. } else {
  886. @parts = split(/\|/, AttrVal($device, $reading."VUnit", ""));
  887. $unit = $parts[1] if($parts[1]);
  888. if(lc($reading) =~ m/temp/) {
  889. $value=~ s/ \(Celsius\)//;
  890. $value=~ s/([-\.\d]+).*/$1/;
  891. $unit= "°C";
  892. }
  893. elsif(lc($reading) =~ m/(humidity|vwc)/) {
  894. $value=~ s/ \(\%\)//;
  895. $unit= "%";
  896. }
  897. }
  898. }
  899. # Onewire
  900. elsif(($type eq "OWAD") ||
  901. ($type eq "OWSWITCH")) {
  902. if(int(@parts)>1) {
  903. $reading = "data";
  904. $value = $event;
  905. } else {
  906. @parts = split(/\|/, AttrVal($device, $reading."Unit", ""));
  907. $unit = $parts[1] if($parts[1]);
  908. }
  909. }
  910. # ZWAVE
  911. elsif ($type eq "ZWAVE") {
  912. if ( $value=~/([-\.\d]+)\s([a-z].*)/i ) {
  913. $value = $1;
  914. $unit = $2;
  915. }
  916. }
  917. # FBDECT
  918. elsif ($type eq "FBDECT") {
  919. if ( $value=~/([\.\d]+)\s([a-z].*)/i ) {
  920. $value = $1;
  921. $unit = $2;
  922. }
  923. }
  924. # MAX
  925. elsif(($type eq "MAX")) {
  926. $unit= "°C" if(lc($reading) =~ m/temp/);
  927. $unit= "%" if(lc($reading) eq "valveposition");
  928. }
  929. # FS20
  930. elsif(($type eq "FS20") || ($type eq "X10")) {
  931. if($reading =~ m/^dim(\d+).*/o) {
  932. $value = $1;
  933. $reading= "dim";
  934. $unit= "%";
  935. }
  936. elsif(!defined($value) || $value eq "") {
  937. $value= $reading;
  938. $reading= "data";
  939. }
  940. }
  941. # FHT
  942. elsif($type eq "FHT") {
  943. if($reading =~ m(-from[12]\ ) || $reading =~ m(-to[12]\ )) {
  944. @parts= split(/ /,$event);
  945. $reading= $parts[0];
  946. $value= $parts[1];
  947. $unit= "";
  948. }
  949. elsif($reading =~ m(-temp)) { $value=~ s/ \(Celsius\)//; $unit= "°C"; }
  950. elsif($reading =~ m(temp-offset)) { $value=~ s/ \(Celsius\)//; $unit= "°C"; }
  951. elsif($reading =~ m(^actuator[0-9]*)) {
  952. if($value eq "lime-protection") {
  953. $reading= "actuator-lime-protection";
  954. undef $value;
  955. }
  956. elsif($value =~ m(^offset:)) {
  957. $reading= "actuator-offset";
  958. @parts= split(/: /,$value);
  959. $value= $parts[1];
  960. if(defined $value) {
  961. $value=~ s/%//; $value= $value*1.; $unit= "%";
  962. }
  963. }
  964. elsif($value =~ m(^unknown_)) {
  965. @parts= split(/: /,$value);
  966. $reading= "actuator-" . $parts[0];
  967. $value= $parts[1];
  968. if(defined $value) {
  969. $value=~ s/%//; $value= $value*1.; $unit= "%";
  970. }
  971. }
  972. elsif($value =~ m(^synctime)) {
  973. $reading= "actuator-synctime";
  974. undef $value;
  975. }
  976. elsif($value eq "test") {
  977. $reading= "actuator-test";
  978. undef $value;
  979. }
  980. elsif($value eq "pair") {
  981. $reading= "actuator-pair";
  982. undef $value;
  983. }
  984. else {
  985. $value=~ s/%//; $value= $value*1.; $unit= "%";
  986. }
  987. }
  988. }
  989. # KS300
  990. elsif($type eq "KS300") {
  991. if($event =~ m(T:.*)) { $reading= "data"; $value= $event; }
  992. elsif($event =~ m(avg_day)) { $reading= "data"; $value= $event; }
  993. elsif($event =~ m(avg_month)) { $reading= "data"; $value= $event; }
  994. elsif($reading eq "temperature") { $value=~ s/ \(Celsius\)//; $unit= "°C"; }
  995. elsif($reading eq "wind") { $value=~ s/ \(km\/h\)//; $unit= "km/h"; }
  996. elsif($reading eq "rain") { $value=~ s/ \(l\/m2\)//; $unit= "l/m2"; }
  997. elsif($reading eq "rain_raw") { $value=~ s/ \(counter\)//; $unit= ""; }
  998. elsif($reading eq "humidity") { $value=~ s/ \(\%\)//; $unit= "%"; }
  999. elsif($reading eq "israining") {
  1000. $value=~ s/ \(yes\/no\)//;
  1001. $value=~ s/no/0/;
  1002. $value=~ s/yes/1/;
  1003. }
  1004. }
  1005. # HMS
  1006. elsif($type eq "HMS" ||
  1007. $type eq "CUL_WS" ||
  1008. $type eq "OWTHERM") {
  1009. if($event =~ m(T:.*)) { $reading= "data"; $value= $event; }
  1010. elsif($reading eq "temperature") {
  1011. $value=~ s/ \(Celsius\)//;
  1012. $value=~ s/([-\.\d]+).*/$1/; #OWTHERM
  1013. $unit= "°C";
  1014. }
  1015. elsif($reading eq "humidity") { $value=~ s/ \(\%\)//; $unit= "%"; }
  1016. elsif($reading eq "battery") {
  1017. $value=~ s/ok/1/;
  1018. $value=~ s/replaced/1/;
  1019. $value=~ s/empty/0/;
  1020. }
  1021. }
  1022. # CUL_HM
  1023. elsif ($type eq "CUL_HM") {
  1024. # remove trailing %
  1025. $value=~ s/ \%$//;
  1026. }
  1027. # BS
  1028. elsif($type eq "BS") {
  1029. if($event =~ m(brightness:.*)) {
  1030. @parts= split(/ /,$event);
  1031. $reading= "lux";
  1032. $value= $parts[4]*1.;
  1033. $unit= "lux";
  1034. }
  1035. }
  1036. # RFXTRX Lighting
  1037. elsif($type eq "TRX_LIGHT") {
  1038. if($reading =~ m/^level (\d+)/) {
  1039. $value = $1;
  1040. $reading= "level";
  1041. }
  1042. }
  1043. # RFXTRX Sensors
  1044. elsif($type eq "TRX_WEATHER") {
  1045. if($reading eq "energy_current") { $value=~ s/ W//; }
  1046. elsif($reading eq "energy_total") { $value=~ s/ kWh//; }
  1047. # elsif($reading eq "temperature") {TODO}
  1048. # elsif($reading eq "temperature") {TODO
  1049. elsif($reading eq "battery") {
  1050. if ($value=~ m/(\d+)\%/) {
  1051. $value= $1;
  1052. }
  1053. else {
  1054. $value= ($value eq "ok");
  1055. }
  1056. }
  1057. }
  1058. # Weather
  1059. elsif($type eq "WEATHER") {
  1060. if($event =~ m(^wind_condition)) {
  1061. @parts= split(/ /,$event); # extract wind direction from event
  1062. if(defined $parts[0]) {
  1063. $reading = "wind_condition";
  1064. $value= "$parts[1] $parts[2] $parts[3]";
  1065. }
  1066. }
  1067. if($reading eq "wind_condition") { $unit= "km/h"; }
  1068. elsif($reading eq "wind_chill") { $unit= "°C"; }
  1069. elsif($reading eq "wind_direction") { $unit= ""; }
  1070. elsif($reading =~ m(^wind)) { $unit= "km/h"; } # wind, wind_speed
  1071. elsif($reading =~ m(^temperature)) { $unit= "°C"; } # wenn reading mit temperature beginnt
  1072. elsif($reading =~ m(^humidity)) { $unit= "%"; }
  1073. elsif($reading =~ m(^pressure)) { $unit= "hPa"; }
  1074. elsif($reading =~ m(^pressure_trend)) { $unit= ""; }
  1075. }
  1076. # FHT8V
  1077. elsif($type eq "FHT8V") {
  1078. if($reading =~ m(valve)) {
  1079. @parts= split(/ /,$event);
  1080. $reading= $parts[0];
  1081. $value= $parts[1];
  1082. $unit= "%";
  1083. }
  1084. }
  1085. # Dummy
  1086. elsif($type eq "DUMMY") {
  1087. if( $value eq "" ) {
  1088. $reading= "data";
  1089. $value= $event;
  1090. }
  1091. $unit= "";
  1092. }
  1093. @result= ($reading,$value,$unit);
  1094. return @result;
  1095. }
  1096. ##################################################################################################################
  1097. #
  1098. # Hauptroutine zum Loggen. Wird bei jedem Eventchange
  1099. # aufgerufen
  1100. #
  1101. ##################################################################################################################
  1102. # Es werden nur die Events von Geräten verarbeitet die im Hash $hash->{NOTIFYDEV} gelistet sind (wenn definiert).
  1103. # Dadurch kann die Menge der Events verringert werden. In sub DbRep_Define angeben.
  1104. # Beispiele:
  1105. # $hash->{NOTIFYDEV} = "global";
  1106. # $hash->{NOTIFYDEV} = "global,Definition_A,Definition_B";
  1107. sub DbLog_Log($$) {
  1108. # $hash is my entry, $dev_hash is the entry of the changed device
  1109. my ($hash, $dev_hash) = @_;
  1110. my $name = $hash->{NAME};
  1111. my $dev_name = $dev_hash->{NAME};
  1112. my $dev_type = uc($dev_hash->{TYPE});
  1113. my $async = AttrVal($name, "asyncMode", undef);
  1114. my $clim = AttrVal($name, "cacheLimit", 500);
  1115. my $ce = AttrVal($name, "cacheEvents", 0);
  1116. my $net;
  1117. return if(IsDisabled($name) || !$hash->{HELPER}{COLSET} || $init_done != 1);
  1118. # Notify-Routine Startzeit
  1119. my $nst = [gettimeofday];
  1120. my $events = deviceEvents($dev_hash, AttrVal($name, "addStateEvent", 1));
  1121. return if(!$events);
  1122. my $max = int(@{$events});
  1123. # verbose4 Logs nur für Devices in Attr "verbose4Devs"
  1124. my $vb4show = 0;
  1125. my @vb4devs = split(",", AttrVal($name, "verbose4Devs", ""));
  1126. if (!@vb4devs) {
  1127. $vb4show = 1;
  1128. } else {
  1129. foreach (@vb4devs) {
  1130. if($dev_name =~ m/$_/i) {
  1131. $vb4show = 1;
  1132. last;
  1133. }
  1134. }
  1135. # Log3 $name, 5, "DbLog $name -> verbose 4 output of device $dev_name skipped due to attribute \"verbose4Devs\" restrictions" if(!$vb4show);
  1136. }
  1137. if($vb4show && !$hash->{HELPER}{".RUNNING_PID"}) {
  1138. Log3 $name, 4, "DbLog $name -> ################################################################";
  1139. Log3 $name, 4, "DbLog $name -> ### start of new Logcycle ###";
  1140. Log3 $name, 4, "DbLog $name -> ################################################################";
  1141. Log3 $name, 4, "DbLog $name -> number of events received: $max for device: $dev_name";
  1142. }
  1143. my $re = $hash->{REGEXP};
  1144. my @row_array;
  1145. my ($event,$reading,$value,$unit);
  1146. my $ts_0 = TimeNow(); # timestamp in SQL format YYYY-MM-DD hh:mm:ss
  1147. my $now = gettimeofday(); # get timestamp in seconds since epoch
  1148. my $DbLogExclude = AttrVal($dev_name, "DbLogExclude", undef);
  1149. my $DbLogInclude = AttrVal($dev_name, "DbLogInclude",undef);
  1150. my $DbLogSelectionMode = AttrVal($name, "DbLogSelectionMode","Exclude");
  1151. my $value_fn = AttrVal( $name, "valueFn", "" );
  1152. # Funktion aus Attr valueFn validieren
  1153. if( $value_fn =~ m/^\s*(\{.*\})\s*$/s ) {
  1154. $value_fn = $1;
  1155. } else {
  1156. $value_fn = '';
  1157. }
  1158. #one Transaction
  1159. eval {
  1160. for (my $i = 0; $i < $max; $i++) {
  1161. my $next = 0;
  1162. my $event = $events->[$i];
  1163. $event = "" if(!defined($event));
  1164. $event = DbLog_charfilter($event) if(AttrVal($name, "useCharfilter",0));
  1165. Log3 $name, 4, "DbLog $name -> check Device: $dev_name , Event: $event" if($vb4show && !$hash->{HELPER}{".RUNNING_PID"});
  1166. if($dev_name =~ m/^$re$/ || "$dev_name:$event" =~ m/^$re$/ || $DbLogSelectionMode eq 'Include') {
  1167. my $timestamp = $ts_0;
  1168. $timestamp = $dev_hash->{CHANGETIME}[$i] if(defined($dev_hash->{CHANGETIME}[$i]));
  1169. $event =~ s/\|/_ESC_/g; # escape Pipe "|"
  1170. my @r = DbLog_ParseEvent($dev_name, $dev_type, $event);
  1171. $reading = $r[0];
  1172. $value = $r[1];
  1173. $unit = $r[2];
  1174. if(!defined $reading) {$reading = "";}
  1175. if(!defined $value) {$value = "";}
  1176. if(!defined $unit || $unit eq "") {$unit = AttrVal("$dev_name", "unit", "");}
  1177. $unit = DbLog_charfilter($unit) if(AttrVal($name, "useCharfilter",0));
  1178. # Devices / Readings ausschließen durch Attribut "excludeDevs"
  1179. # attr <device> excludeDevs [<devspec>#]<Reading1>,[<devspec>#]<Reading2>,[<devspec>#]<Reading..>
  1180. my ($exc,@excldr,$ds,$rd,@exdvs);
  1181. $exc = AttrVal($name, "excludeDevs", "");
  1182. if($exc) {
  1183. $exc =~ s/[\s\n]/,/g;
  1184. @excldr = split(",",$exc);
  1185. foreach my $excl (@excldr) {
  1186. ($ds,$rd) = split("#",$excl);
  1187. @exdvs = devspec2array($ds);
  1188. if(@exdvs) {
  1189. # Log3 $name, 3, "DbLog $name -> excludeDevs: @exdvs";
  1190. foreach (@exdvs) {
  1191. if($rd) {
  1192. if("$dev_name:$reading" =~ m/^$_:$rd$/) {
  1193. Log3 $name, 4, "DbLog $name -> Device:Reading \"$dev_name:$reading\" global excluded from logging by attribute \"excludeDevs\" " if($vb4show && !$hash->{HELPER}{".RUNNING_PID"});
  1194. $next = 1;
  1195. }
  1196. } else {
  1197. if($dev_name =~ m/^$_$/) {
  1198. Log3 $name, 4, "DbLog $name -> Device \"$dev_name\" global excluded from logging by attribute \"excludeDevs\" " if($vb4show && !$hash->{HELPER}{".RUNNING_PID"});
  1199. $next = 1;
  1200. }
  1201. }
  1202. }
  1203. }
  1204. }
  1205. next if($next);
  1206. }
  1207. Log3 $name, 5, "DbLog $name -> parsed Event: $dev_name , Event: $event" if($vb4show && !$hash->{HELPER}{".RUNNING_PID"});
  1208. Log3 $name, 5, "DbLog $name -> DbLogExclude of \"$dev_name\": $DbLogExclude" if($vb4show && !$hash->{HELPER}{".RUNNING_PID"} && $DbLogExclude);
  1209. Log3 $name, 5, "DbLog $name -> DbLogInclude of \"$dev_name\": $DbLogInclude" if($vb4show && !$hash->{HELPER}{".RUNNING_PID"} && $DbLogInclude);
  1210. #Je nach DBLogSelectionMode muss das vorgegebene Ergebnis der Include-, bzw. Exclude-Pruefung
  1211. #entsprechend unterschiedlich vorbelegt sein.
  1212. #keine Readings loggen die in DbLogExclude explizit ausgeschlossen sind
  1213. my $DoIt = 0;
  1214. $DoIt = 1 if($DbLogSelectionMode =~ m/Exclude/ );
  1215. if($DbLogExclude && $DbLogSelectionMode =~ m/Exclude/) {
  1216. # Bsp: "(temperature|humidity):300 battery:3600"
  1217. my @v1 = split(/,/, $DbLogExclude);
  1218. for (my $i=0; $i<int(@v1); $i++) {
  1219. my @v2 = split(/:/, $v1[$i]);
  1220. $DoIt = 0 if(!$v2[1] && $reading =~ m,^$v2[0]$,); #Reading matcht auf Regexp, kein MinIntervall angegeben
  1221. if(($v2[1] && $reading =~ m,^$v2[0]$,) && ($v2[1] =~ m/^(\d+)$/)) {
  1222. #Regexp matcht und MinIntervall ist angegeben
  1223. my $lt = $defs{$dev_hash->{NAME}}{Helper}{DBLOG}{$reading}{$hash->{NAME}}{TIME};
  1224. my $lv = $defs{$dev_hash->{NAME}}{Helper}{DBLOG}{$reading}{$hash->{NAME}}{VALUE};
  1225. $lt = 0 if(!$lt);
  1226. $lv = "" if(!$lv);
  1227. if(($now-$lt < $v2[1]) && ($lv eq $value)) {
  1228. # innerhalb MinIntervall und LastValue=Value
  1229. $DoIt = 0;
  1230. }
  1231. }
  1232. }
  1233. }
  1234. #Hier ggf. zusätzlich noch dbLogInclude pruefen, falls bereits durch DbLogExclude ausgeschlossen
  1235. #Im Endeffekt genau die gleiche Pruefung, wie fuer DBLogExclude, lediglich mit umgegkehrtem Ergebnis.
  1236. if($DoIt == 0) {
  1237. if($DbLogInclude && ($DbLogSelectionMode =~ m/Include/)) {
  1238. my @v1 = split(/,/, $DbLogInclude);
  1239. for (my $i=0; $i<int(@v1); $i++) {
  1240. my @v2 = split(/:/, $v1[$i]);
  1241. $DoIt = 1 if($reading =~ m,^$v2[0]$,); #Reading matcht auf Regexp
  1242. if(($v2[1] && $reading =~ m,^$v2[0]$,) && ($v2[1] =~ m/^(\d+)$/)) {
  1243. #Regexp matcht und MinIntervall ist angegeben
  1244. my $lt = $defs{$dev_hash->{NAME}}{Helper}{DBLOG}{$reading}{$hash->{NAME}}{TIME};
  1245. my $lv = $defs{$dev_hash->{NAME}}{Helper}{DBLOG}{$reading}{$hash->{NAME}}{VALUE};
  1246. $lt = 0 if(!$lt);
  1247. $lv = "" if(!$lv);
  1248. if(($now-$lt < $v2[1]) && ($lv eq $value)) {
  1249. # innerhalb MinIntervall und LastValue=Value
  1250. $DoIt = 0;
  1251. }
  1252. }
  1253. }
  1254. }
  1255. }
  1256. next if($DoIt == 0);
  1257. if ($DoIt) {
  1258. $defs{$dev_name}{Helper}{DBLOG}{$reading}{$hash->{NAME}}{TIME} = $now;
  1259. $defs{$dev_name}{Helper}{DBLOG}{$reading}{$hash->{NAME}}{VALUE} = $value;
  1260. # Anwender kann Feldwerte mit Funktion aus Attr valueFn verändern oder Datensatz-Log überspringen
  1261. if($value_fn ne '') {
  1262. my $TIMESTAMP = $timestamp;
  1263. my $DEVICE = $dev_name;
  1264. my $DEVICETYPE = $dev_type;
  1265. my $EVENT = $event;
  1266. my $READING = $reading;
  1267. my $VALUE = $value;
  1268. my $UNIT = $unit;
  1269. my $IGNORE = 0;
  1270. my $CN = " ";
  1271. eval $value_fn;
  1272. Log3 $name, 2, "DbLog $name -> error valueFn: ".$@ if($@);
  1273. if($IGNORE) {
  1274. # aktueller Event wird nicht geloggt wenn $IGNORE=1 gesetzt in $value_fn
  1275. Log3 $hash->{NAME}, 4, "DbLog $name -> Event ignored by valueFn - TS: $timestamp, Device: $dev_name, Type: $dev_type, Event: $event, Reading: $reading, Value: $value, Unit: $unit"
  1276. if($vb4show && !$hash->{HELPER}{".RUNNING_PID"});
  1277. next;
  1278. }
  1279. $timestamp = $TIMESTAMP if($TIMESTAMP =~ /(19[0-9][0-9]|2[0-9][0-9][0-9])-(0[1-9]|1[1-2])-(0[1-9]|1[0-9]|2[0-9]|3[0-1]) (0[0-9]|1[1-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])/);
  1280. $dev_name = $DEVICE if($DEVICE ne '');
  1281. $dev_type = $DEVICETYPE if($DEVICETYPE ne '');
  1282. $reading = $READING if($READING ne '');
  1283. $value = $VALUE if(defined $VALUE);
  1284. $unit = $UNIT if(defined $UNIT);
  1285. }
  1286. # Daten auf maximale Länge beschneiden
  1287. ($dev_name,$dev_type,$event,$reading,$value,$unit) = DbLog_cutCol($hash,$dev_name,$dev_type,$event,$reading,$value,$unit);
  1288. my $row = ($timestamp."|".$dev_name."|".$dev_type."|".$event."|".$reading."|".$value."|".$unit);
  1289. Log3 $hash->{NAME}, 4, "DbLog $name -> added event - Timestamp: $timestamp, Device: $dev_name, Type: $dev_type, Event: $event, Reading: $reading, Value: $value, Unit: $unit"
  1290. if($vb4show && !$hash->{HELPER}{".RUNNING_PID"});
  1291. if($async) {
  1292. # asynchoner non-blocking Mode
  1293. # Cache & CacheIndex für Events zum asynchronen Schreiben in DB
  1294. $hash->{cache}{index}++;
  1295. my $index = $hash->{cache}{index};
  1296. $hash->{cache}{".memcache"}{$index} = $row;
  1297. my $memcount = $hash->{cache}{".memcache"}?scalar(keys%{$hash->{cache}{".memcache"}}):0;
  1298. if($ce == 1) {
  1299. readingsSingleUpdate($hash, "CacheUsage", $memcount, 1);
  1300. } else {
  1301. readingsSingleUpdate($hash, 'CacheUsage', $memcount, 0);
  1302. }
  1303. # asynchrone Schreibroutine aufrufen wenn Füllstand des Cache erreicht ist
  1304. if($memcount >= $clim) {
  1305. my $lmlr = $hash->{HELPER}{LASTLIMITRUNTIME};
  1306. my $syncival = AttrVal($name, "syncInterval", 30);
  1307. if(!$lmlr || gettimeofday() > $lmlr+($syncival/2)) {
  1308. Log3 $hash->{NAME}, 4, "DbLog $name -> Number of cache entries reached cachelimit $clim - start database sync.";
  1309. DbLog_execmemcache($hash);
  1310. $hash->{HELPER}{LASTLIMITRUNTIME} = gettimeofday();
  1311. }
  1312. }
  1313. # Notify-Routine Laufzeit ermitteln
  1314. $net = tv_interval($nst);
  1315. } else {
  1316. # synchoner Mode
  1317. push(@row_array, $row);
  1318. }
  1319. }
  1320. }
  1321. }
  1322. };
  1323. if(!$async) {
  1324. if(@row_array) {
  1325. # synchoner Mode
  1326. # return wenn "reopen" mit Ablaufzeit gestartet ist
  1327. return if($hash->{HELPER}{REOPEN_RUNS});
  1328. my $error = DbLog_Push($hash, $vb4show, @row_array);
  1329. Log3 $name, 5, "DbLog $name -> DbLog_Push Returncode: $error" if($vb4show);
  1330. my $state = $error?$error:(IsDisabled($name))?"disabled":"connected";
  1331. my $evt = ($state eq $hash->{HELPER}{OLDSTATE})?0:1;
  1332. readingsSingleUpdate($hash, "state", $state, $evt);
  1333. $hash->{HELPER}{OLDSTATE} = $state;
  1334. # Notify-Routine Laufzeit ermitteln
  1335. $net = tv_interval($nst);
  1336. }
  1337. }
  1338. if($net && AttrVal($name, "showNotifyTime", undef)) {
  1339. readingsSingleUpdate($hash, "notify_processing_time", sprintf("%.4f",$net), 1);
  1340. }
  1341. return;
  1342. }
  1343. #################################################################################################
  1344. #
  1345. # Schreibroutine Einfügen Werte in DB im Synchronmode
  1346. #
  1347. #################################################################################################
  1348. sub DbLog_Push(@) {
  1349. my ($hash, $vb4show, @row_array) = @_;
  1350. my $name = $hash->{NAME};
  1351. my $DbLogType = AttrVal($name, "DbLogType", "History");
  1352. my $supk = AttrVal($name, "noSupportPK", 0);
  1353. my $errorh = 0;
  1354. my $error = 0;
  1355. my $doins = 0; # Hilfsvariable, wenn "1" sollen inserts in Tabele current erfolgen (updates schlugen fehl)
  1356. my $dbh;
  1357. my $nh = ($hash->{MODEL} ne 'SQLITE')?1:0;
  1358. # Unterscheidung $dbh um Abbrüche in Plots (SQLite) zu vermeiden und
  1359. # andererseite kein "MySQL-Server has gone away" Fehler
  1360. if ($nh) {
  1361. $dbh = DbLog_ConnectNewDBH($hash);
  1362. return if(!$dbh);
  1363. } else {
  1364. $dbh = $hash->{DBHP};
  1365. eval {
  1366. if ( !$dbh || not $dbh->ping ) {
  1367. # DB Session dead, try to reopen now !
  1368. DbLog_ConnectPush($hash,1);
  1369. }
  1370. };
  1371. if ($@) {
  1372. Log3($name, 1, "DbLog $name: DBLog_Push - DB Session dead! - $@");
  1373. return $@;
  1374. } else {
  1375. $dbh = $hash->{DBHP};
  1376. }
  1377. }
  1378. $dbh->{RaiseError} = 1;
  1379. $dbh->{PrintError} = 0;
  1380. my ($useac,$useta) = DbLog_commitMode($hash);
  1381. my $ac = ($dbh->{AutoCommit})?"ON":"OFF";
  1382. my $tm = ($useta)?"ON":"OFF";
  1383. Log3 $name, 4, "DbLog $name -> ################################################################";
  1384. Log3 $name, 4, "DbLog $name -> ### New database processing cycle - synchronous ###";
  1385. Log3 $name, 4, "DbLog $name -> ################################################################";
  1386. Log3 $name, 4, "DbLog $name -> DbLogType is: $DbLogType";
  1387. Log3 $name, 4, "DbLog $name -> AutoCommit mode: $ac, Transaction mode: $tm";
  1388. # check ob PK verwendet wird, @usepkx?Anzahl der Felder im PK:0 wenn kein PK, $pkx?Namen der Felder:none wenn kein PK
  1389. my ($usepkh,$usepkc,$pkh,$pkc);
  1390. if (!$supk) {
  1391. ($usepkh,$usepkc,$pkh,$pkc) = DbLog_checkUsePK($hash,$dbh);
  1392. } else {
  1393. Log3 $hash->{NAME}, 5, "DbLog $name -> Primary Key usage suppressed by attribute noSupportPK";
  1394. }
  1395. my (@timestamp,@device,@type,@event,@reading,@value,@unit);
  1396. my (@timestamp_cur,@device_cur,@type_cur,@event_cur,@reading_cur,@value_cur,@unit_cur);
  1397. my ($sth_ih,$sth_ic,$sth_uc);
  1398. no warnings 'uninitialized';
  1399. my $ceti = $#row_array+1;
  1400. foreach my $row (@row_array) {
  1401. my @a = split("\\|",$row);
  1402. s/_ESC_/\|/g for @a; # escaped Pipe return to "|"
  1403. push(@timestamp, "$a[0]");
  1404. push(@device, "$a[1]");
  1405. push(@type, "$a[2]");
  1406. push(@event, "$a[3]");
  1407. push(@reading, "$a[4]");
  1408. push(@value, "$a[5]");
  1409. push(@unit, "$a[6]");
  1410. Log3 $hash->{NAME}, 4, "DbLog $name -> processing event Timestamp: $a[0], Device: $a[1], Type: $a[2], Event: $a[3], Reading: $a[4], Value: $a[5], Unit: $a[6]"
  1411. if($vb4show);
  1412. }
  1413. use warnings;
  1414. if (lc($DbLogType) =~ m(history)) {
  1415. # insert history mit/ohne primary key
  1416. if ($usepkh && $hash->{MODEL} eq 'MYSQL') {
  1417. eval { $sth_ih = $dbh->prepare("INSERT IGNORE INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  1418. } elsif ($usepkh && $hash->{MODEL} eq 'SQLITE') {
  1419. eval { $sth_ih = $dbh->prepare("INSERT OR IGNORE INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  1420. } elsif ($usepkh && $hash->{MODEL} eq 'POSTGRESQL') {
  1421. eval { $sth_ih = $dbh->prepare("INSERT INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?) ON CONFLICT DO NOTHING"); };
  1422. } else {
  1423. # old behavior
  1424. eval { $sth_ih = $dbh->prepare("INSERT INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  1425. }
  1426. if ($@) {
  1427. return $@;
  1428. }
  1429. $sth_ih->bind_param_array(1, [@timestamp]);
  1430. $sth_ih->bind_param_array(2, [@device]);
  1431. $sth_ih->bind_param_array(3, [@type]);
  1432. $sth_ih->bind_param_array(4, [@event]);
  1433. $sth_ih->bind_param_array(5, [@reading]);
  1434. $sth_ih->bind_param_array(6, [@value]);
  1435. $sth_ih->bind_param_array(7, [@unit]);
  1436. }
  1437. if (lc($DbLogType) =~ m(current) ) {
  1438. # insert current mit/ohne primary key, insert-values für current werden generiert
  1439. if ($usepkc && $hash->{MODEL} eq 'MYSQL') {
  1440. eval { $sth_ic = $dbh->prepare("INSERT IGNORE INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  1441. } elsif ($usepkc && $hash->{MODEL} eq 'SQLITE') {
  1442. eval { $sth_ic = $dbh->prepare("INSERT OR IGNORE INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  1443. } elsif ($usepkc && $hash->{MODEL} eq 'POSTGRESQL') {
  1444. eval { $sth_ic = $dbh->prepare("INSERT INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?) ON CONFLICT DO NOTHING"); };
  1445. } else {
  1446. # old behavior
  1447. eval { $sth_ic = $dbh->prepare("INSERT INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  1448. }
  1449. if ($@) {
  1450. return $@;
  1451. }
  1452. if ($usepkc && $hash->{MODEL} eq 'MYSQL') {
  1453. # update current (mit PK), insert-values für current wird generiert
  1454. $sth_uc = $dbh->prepare("REPLACE INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)");
  1455. $sth_uc->bind_param_array(1, [@timestamp]);
  1456. $sth_uc->bind_param_array(2, [@device]);
  1457. $sth_uc->bind_param_array(3, [@type]);
  1458. $sth_uc->bind_param_array(4, [@event]);
  1459. $sth_uc->bind_param_array(5, [@reading]);
  1460. $sth_uc->bind_param_array(6, [@value]);
  1461. $sth_uc->bind_param_array(7, [@unit]);
  1462. } elsif ($usepkc && $hash->{MODEL} eq 'SQLITE') {
  1463. # update current (mit PK), insert-values für current wird generiert
  1464. $sth_uc = $dbh->prepare("INSERT OR REPLACE INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)");
  1465. $sth_uc->bind_param_array(1, [@timestamp]);
  1466. $sth_uc->bind_param_array(2, [@device]);
  1467. $sth_uc->bind_param_array(3, [@type]);
  1468. $sth_uc->bind_param_array(4, [@event]);
  1469. $sth_uc->bind_param_array(5, [@reading]);
  1470. $sth_uc->bind_param_array(6, [@value]);
  1471. $sth_uc->bind_param_array(7, [@unit]);
  1472. } elsif ($usepkc && $hash->{MODEL} eq 'POSTGRESQL') {
  1473. # update current (mit PK), insert-values für current wird generiert
  1474. $sth_uc = $dbh->prepare("INSERT INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?) ON CONFLICT ($pkc)
  1475. DO UPDATE SET TIMESTAMP=EXCLUDED.TIMESTAMP, DEVICE=EXCLUDED.DEVICE, TYPE=EXCLUDED.TYPE, EVENT=EXCLUDED.EVENT, READING=EXCLUDED.READING,
  1476. VALUE=EXCLUDED.VALUE, UNIT=EXCLUDED.UNIT");
  1477. $sth_uc->bind_param_array(1, [@timestamp]);
  1478. $sth_uc->bind_param_array(2, [@device]);
  1479. $sth_uc->bind_param_array(3, [@type]);
  1480. $sth_uc->bind_param_array(4, [@event]);
  1481. $sth_uc->bind_param_array(5, [@reading]);
  1482. $sth_uc->bind_param_array(6, [@value]);
  1483. $sth_uc->bind_param_array(7, [@unit]);
  1484. } else {
  1485. # for update current (ohne PK), insert-values für current wird generiert
  1486. $sth_uc = $dbh->prepare("UPDATE current SET TIMESTAMP=?, TYPE=?, EVENT=?, VALUE=?, UNIT=? WHERE (DEVICE=?) AND (READING=?)");
  1487. $sth_uc->bind_param_array(1, [@timestamp]);
  1488. $sth_uc->bind_param_array(2, [@type]);
  1489. $sth_uc->bind_param_array(3, [@event]);
  1490. $sth_uc->bind_param_array(4, [@value]);
  1491. $sth_uc->bind_param_array(5, [@unit]);
  1492. $sth_uc->bind_param_array(6, [@device]);
  1493. $sth_uc->bind_param_array(7, [@reading]);
  1494. }
  1495. }
  1496. my ($tuples, $rows);
  1497. # insert into history-Tabelle
  1498. eval { $dbh->begin_work() if($useta && $dbh->{AutoCommit}); }; # Transaktion wenn gewünscht und autocommit ein
  1499. if ($@) {
  1500. Log3($name, 2, "DbLog $name -> Error start transaction for history - $@");
  1501. }
  1502. eval {
  1503. if (lc($DbLogType) =~ m(history) ) {
  1504. ($tuples, $rows) = $sth_ih->execute_array( { ArrayTupleStatus => \my @tuple_status } );
  1505. my $nins_hist = 0;
  1506. for my $tuple (0..$#row_array) {
  1507. my $status = $tuple_status[$tuple];
  1508. $status = 0 if($status eq "0E0");
  1509. next if($status); # $status ist "1" wenn insert ok
  1510. Log3 $hash->{NAME}, 3, "DbLog $name -> Insert into history rejected".($usepkh?" (possible PK violation) ":" ")."- TS: $timestamp[$tuple], Device: $device[$tuple], Event: $event[$tuple]";
  1511. $nins_hist++;
  1512. }
  1513. if(!$nins_hist) {
  1514. Log3 $hash->{NAME}, 4, "DbLog $name -> $ceti of $ceti events inserted into table history".($usepkh?" using PK on columns $pkh":"");
  1515. } else {
  1516. Log3 $hash->{NAME}, 4, "DbLog $name -> ".($ceti-$nins_hist)." of $ceti events inserted into table history".($usepkh?" using PK on columns $pkh":"");
  1517. }
  1518. eval {$dbh->commit() if(!$dbh->{AutoCommit});}; # issue Turning on AutoCommit failed
  1519. if ($@) {
  1520. Log3($name, 2, "DbLog $name -> Error commit history - $@");
  1521. } else {
  1522. if(!$dbh->{AutoCommit}) {
  1523. Log3($name, 4, "DbLog $name -> insert table history committed");
  1524. } else {
  1525. Log3($name, 4, "DbLog $name -> insert table history committed by autocommit");
  1526. }
  1527. }
  1528. }
  1529. };
  1530. if ($@) {
  1531. Log3 $hash->{NAME}, 2, "DbLog $name -> Error table history - $@";
  1532. $errorh = $@;
  1533. eval {$dbh->rollback() if(!$dbh->{AutoCommit});}; # issue Turning on AutoCommit failed
  1534. if ($@) {
  1535. Log3($name, 2, "DbLog $name -> Error rollback history - $@");
  1536. } else {
  1537. Log3($name, 4, "DbLog $name -> insert history rolled back");
  1538. }
  1539. }
  1540. # update or insert current
  1541. eval { $dbh->begin_work() if($useta && $dbh->{AutoCommit}); }; # Transaktion wenn gewünscht und autocommit ein
  1542. if ($@) {
  1543. Log3($name, 2, "DbLog $name -> Error start transaction for history - $@");
  1544. }
  1545. eval {
  1546. if (lc($DbLogType) =~ m(current) ) {
  1547. ($tuples, $rows) = $sth_uc->execute_array( { ArrayTupleStatus => \my @tuple_status } );
  1548. # Log3 $hash->{NAME}, 2, "DbLog $name -> Rows: $rows, Ceti: $ceti";
  1549. my $nupd_cur = 0;
  1550. for my $tuple (0..$#row_array) {
  1551. my $status = $tuple_status[$tuple];
  1552. $status = 0 if($status eq "0E0");
  1553. next if($status); # $status ist "1" wenn update ok
  1554. Log3 $hash->{NAME}, 4, "DbLog $name -> Failed to update in current, try to insert - TS: $timestamp[$tuple], Device: $device[$tuple], Reading: $reading[$tuple], Status = $status";
  1555. push(@timestamp_cur, "$timestamp[$tuple]");
  1556. push(@device_cur, "$device[$tuple]");
  1557. push(@type_cur, "$type[$tuple]");
  1558. push(@event_cur, "$event[$tuple]");
  1559. push(@reading_cur, "$reading[$tuple]");
  1560. push(@value_cur, "$value[$tuple]");
  1561. push(@unit_cur, "$unit[$tuple]");
  1562. $nupd_cur++;
  1563. }
  1564. if(!$nupd_cur) {
  1565. Log3 $hash->{NAME}, 4, "DbLog $name -> $ceti of $ceti events updated in table current".($usepkc?" using PK on columns $pkc":"");
  1566. } else {
  1567. Log3 $hash->{NAME}, 4, "DbLog $name -> $nupd_cur of $ceti events not updated and try to insert into table current".($usepkc?" using PK on columns $pkc":"");
  1568. $doins = 1;
  1569. }
  1570. if ($doins) {
  1571. # events die nicht in Tabelle current updated wurden, werden in current neu eingefügt
  1572. $sth_ic->bind_param_array(1, [@timestamp_cur]);
  1573. $sth_ic->bind_param_array(2, [@device_cur]);
  1574. $sth_ic->bind_param_array(3, [@type_cur]);
  1575. $sth_ic->bind_param_array(4, [@event_cur]);
  1576. $sth_ic->bind_param_array(5, [@reading_cur]);
  1577. $sth_ic->bind_param_array(6, [@value_cur]);
  1578. $sth_ic->bind_param_array(7, [@unit_cur]);
  1579. ($tuples, $rows) = $sth_ic->execute_array( { ArrayTupleStatus => \my @tuple_status } );
  1580. my $nins_cur = 0;
  1581. for my $tuple (0..$#device_cur) {
  1582. my $status = $tuple_status[$tuple];
  1583. $status = 0 if($status eq "0E0");
  1584. next if($status); # $status ist "1" wenn insert ok
  1585. Log3 $hash->{NAME}, 3, "DbLog $name -> Insert into current rejected - TS: $timestamp[$tuple], Device: $device_cur[$tuple], Reading: $reading_cur[$tuple], Status = $status";
  1586. $nins_cur++;
  1587. }
  1588. if(!$nins_cur) {
  1589. Log3 $hash->{NAME}, 4, "DbLog $name -> ".($#device_cur+1)." of ".($#device_cur+1)." events inserted into table current ".($usepkc?" using PK on columns $pkc":"");
  1590. } else {
  1591. Log3 $hash->{NAME}, 4, "DbLog $name -> ".($#device_cur+1-$nins_cur)." of ".($#device_cur+1)." events inserted into table current".($usepkc?" using PK on columns $pkc":"");
  1592. }
  1593. }
  1594. eval {$dbh->commit() if(!$dbh->{AutoCommit});}; # issue Turning on AutoCommit failed
  1595. if ($@) {
  1596. Log3($name, 2, "DbLog $name -> Error commit table current - $@");
  1597. } else {
  1598. if(!$dbh->{AutoCommit}) {
  1599. Log3($name, 4, "DbLog $name -> insert / update table current committed");
  1600. } else {
  1601. Log3($name, 4, "DbLog $name -> insert / update table current committed by autocommit");
  1602. }
  1603. }
  1604. }
  1605. };
  1606. if ($errorh) {
  1607. $error = $errorh;
  1608. }
  1609. $dbh->{RaiseError} = 0;
  1610. $dbh->{PrintError} = 1;
  1611. $dbh->disconnect if ($nh);
  1612. return Encode::encode_utf8($error);
  1613. }
  1614. #################################################################################################
  1615. #
  1616. # MemCache auswerten und Schreibroutine asynchron und non-blocking aufrufen
  1617. #
  1618. #################################################################################################
  1619. sub DbLog_execmemcache ($) {
  1620. my ($hash) = @_;
  1621. my $name = $hash->{NAME};
  1622. my $syncival = AttrVal($name, "syncInterval", 30);
  1623. my $clim = AttrVal($name, "cacheLimit", 500);
  1624. my $async = AttrVal($name, "asyncMode", undef);
  1625. my $ce = AttrVal($name, "cacheEvents", 0);
  1626. my $timeout = AttrVal($name, "timeout", 86400);
  1627. my $DbLogType = AttrVal($name, "DbLogType", "History");
  1628. my $dbconn = $hash->{dbconn};
  1629. my $dbuser = $hash->{dbuser};
  1630. my $dbpassword = $attr{"sec$name"}{secret};
  1631. my $dolog = 1;
  1632. my $error = 0;
  1633. my (@row_array,$memcount,$dbh);
  1634. RemoveInternalTimer($hash, "DbLog_execmemcache");
  1635. if($init_done != 1) {
  1636. InternalTimer(gettimeofday()+5, "DbLog_execmemcache", $hash, 0);
  1637. return;
  1638. }
  1639. # return wenn "reopen" mit Zeitangabe läuft, oder kein asynchroner Mode oder wenn disabled
  1640. if(!$async || IsDisabled($name) || $hash->{HELPER}{REOPEN_RUNS}) {
  1641. return;
  1642. }
  1643. # tote PID's löschen
  1644. if($hash->{HELPER}{".RUNNING_PID"} && $hash->{HELPER}{".RUNNING_PID"}{pid} =~ m/DEAD/) {
  1645. delete $hash->{HELPER}{".RUNNING_PID"};
  1646. }
  1647. if($hash->{HELPER}{REDUCELOG_PID} && $hash->{HELPER}{REDUCELOG_PID}{pid} =~ m/DEAD/) {
  1648. delete $hash->{HELPER}{REDUCELOG_PID};
  1649. }
  1650. if($hash->{HELPER}{DELDAYS_PID} && $hash->{HELPER}{DELDAYS_PID}{pid} =~ m/DEAD/) {
  1651. delete $hash->{HELPER}{DELDAYS_PID};
  1652. }
  1653. # bei SQLite Sperrverwaltung Logging wenn andere schreibende Zugriffe laufen
  1654. if($hash->{MODEL} eq "SQLITE") {
  1655. if($hash->{HELPER}{DELDAYS_PID}) {
  1656. $error = "deleteOldDaysNbl is running - resync at NextSync";
  1657. $dolog = 0;
  1658. }
  1659. if($hash->{HELPER}{REDUCELOG_PID}) {
  1660. $error = "reduceLogNbl is running - resync at NextSync";
  1661. $dolog = 0;
  1662. }
  1663. if($hash->{HELPER}{".RUNNING_PID"}) {
  1664. $error = "Commit already running - resync at NextSync";
  1665. $dolog = 0;
  1666. }
  1667. }
  1668. $memcount = $hash->{cache}{".memcache"}?scalar(keys%{$hash->{cache}{".memcache"}}):0;
  1669. if($ce == 2) {
  1670. readingsSingleUpdate($hash, "CacheUsage", $memcount, 1);
  1671. } else {
  1672. readingsSingleUpdate($hash, 'CacheUsage', $memcount, 0);
  1673. }
  1674. if($memcount && $dolog && !$hash->{HELPER}{".RUNNING_PID"}) {
  1675. Log3 $name, 4, "DbLog $name -> ################################################################";
  1676. Log3 $name, 4, "DbLog $name -> ### New database processing cycle - asynchronous ###";
  1677. Log3 $name, 4, "DbLog $name -> ################################################################";
  1678. Log3 $name, 4, "DbLog $name -> MemCache contains $memcount entries to process";
  1679. Log3 $name, 4, "DbLog $name -> DbLogType is: $DbLogType";
  1680. foreach my $key (sort(keys%{$hash->{cache}{".memcache"}})) {
  1681. Log3 $hash->{NAME}, 5, "DbLog $name -> MemCache contains: ".$hash->{cache}{".memcache"}{$key};
  1682. push(@row_array, delete($hash->{cache}{".memcache"}{$key}));
  1683. }
  1684. my $rowlist = join('§', @row_array);
  1685. $rowlist = encode_base64($rowlist,"");
  1686. $hash->{HELPER}{".RUNNING_PID"} = BlockingCall (
  1687. "DbLog_PushAsync",
  1688. "$name|$rowlist",
  1689. "DbLog_PushAsyncDone",
  1690. $timeout,
  1691. "DbLog_PushAsyncAborted",
  1692. $hash );
  1693. $hash->{HELPER}{".RUNNING_PID"}{loglevel} = 4;
  1694. Log3 $hash->{NAME}, 5, "DbLog $name -> DbLog_PushAsync called with timeout: $timeout";
  1695. } else {
  1696. if($dolog && $hash->{HELPER}{".RUNNING_PID"}) {
  1697. $error = "Commit already running - resync at NextSync";
  1698. }
  1699. }
  1700. # $memcount = scalar(keys%{$hash->{cache}{".memcache"}});
  1701. my $nextsync = gettimeofday()+$syncival;
  1702. my $nsdt = FmtDateTime($nextsync);
  1703. if(AttrVal($name, "syncEvents", undef)) {
  1704. readingsSingleUpdate($hash, "NextSync", $nsdt. " or if CacheUsage ".$clim." reached", 1);
  1705. } else {
  1706. readingsSingleUpdate($hash, "NextSync", $nsdt. " or if CacheUsage ".$clim." reached", 0);
  1707. }
  1708. my $state = $error?$error:$hash->{HELPER}{OLDSTATE};
  1709. my $evt = ($state eq $hash->{HELPER}{OLDSTATE})?0:1;
  1710. readingsSingleUpdate($hash, "state", $state, $evt);
  1711. $hash->{HELPER}{OLDSTATE} = $state;
  1712. InternalTimer($nextsync, "DbLog_execmemcache", $hash, 0);
  1713. return;
  1714. }
  1715. #################################################################################################
  1716. #
  1717. # Schreibroutine Einfügen Werte in DB asynchron non-blocking
  1718. #
  1719. #################################################################################################
  1720. sub DbLog_PushAsync(@) {
  1721. my ($string) = @_;
  1722. my ($name,$rowlist) = split("\\|", $string);
  1723. my $hash = $defs{$name};
  1724. my $dbconn = $hash->{dbconn};
  1725. my $dbuser = $hash->{dbuser};
  1726. my $dbpassword = $attr{"sec$name"}{secret};
  1727. my $DbLogType = AttrVal($name, "DbLogType", "History");
  1728. my $supk = AttrVal($name, "noSupportPK", 0);
  1729. my $utf8 = defined($hash->{UTF8})?$hash->{UTF8}:0;
  1730. my $errorh = 0;
  1731. my $error = 0;
  1732. my $doins = 0; # Hilfsvariable, wenn "1" sollen inserts in Tabelle current erfolgen (updates schlugen fehl)
  1733. my $dbh;
  1734. my $rowlback = 0; # Eventliste für Rückgabe wenn Fehler
  1735. Log3 ($name, 5, "DbLog $name -> Start DbLog_PushAsync");
  1736. Log3 ($name, 5, "DbLog $name -> DbLogType is: $DbLogType");
  1737. # Background-Startzeit
  1738. my $bst = [gettimeofday];
  1739. my ($useac,$useta) = DbLog_commitMode($hash);
  1740. if(!$useac) {
  1741. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoCommit => 0, mysql_enable_utf8 => $utf8 });};
  1742. } elsif($useac == 1) {
  1743. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoCommit => 1, mysql_enable_utf8 => $utf8 });};
  1744. } else {
  1745. # Server default
  1746. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, mysql_enable_utf8 => $utf8 });};
  1747. }
  1748. if ($@) {
  1749. $error = encode_base64($@,"");
  1750. Log3 ($name, 2, "DbLog $name - Error: $@");
  1751. Log3 ($name, 5, "DbLog $name -> DbLog_PushAsync finished");
  1752. return "$name|$error|0|$rowlist";
  1753. }
  1754. my $ac = ($dbh->{AutoCommit})?"ON":"OFF";
  1755. my $tm = ($useta)?"ON":"OFF";
  1756. Log3 $hash->{NAME}, 4, "DbLog $name -> AutoCommit mode: $ac, Transaction mode: $tm";
  1757. # check ob PK verwendet wird, @usepkx?Anzahl der Felder im PK:0 wenn kein PK, $pkx?Namen der Felder:none wenn kein PK
  1758. my ($usepkh,$usepkc,$pkh,$pkc);
  1759. if (!$supk) {
  1760. ($usepkh,$usepkc,$pkh,$pkc) = DbLog_checkUsePK($hash,$dbh);
  1761. } else {
  1762. Log3 $hash->{NAME}, 5, "DbLog $name -> Primary Key usage suppressed by attribute noSupportPK";
  1763. }
  1764. my $rowldec = decode_base64($rowlist);
  1765. my @row_array = split('§', $rowldec);
  1766. my (@timestamp,@device,@type,@event,@reading,@value,@unit);
  1767. my (@timestamp_cur,@device_cur,@type_cur,@event_cur,@reading_cur,@value_cur,@unit_cur);
  1768. my ($sth_ih,$sth_ic,$sth_uc);
  1769. no warnings 'uninitialized';
  1770. my $ceti = $#row_array+1;
  1771. foreach my $row (@row_array) {
  1772. my @a = split("\\|",$row);
  1773. s/_ESC_/\|/g for @a; # escaped Pipe return to "|"
  1774. push(@timestamp, "$a[0]");
  1775. push(@device, "$a[1]");
  1776. push(@type, "$a[2]");
  1777. push(@event, "$a[3]");
  1778. push(@reading, "$a[4]");
  1779. push(@value, "$a[5]");
  1780. push(@unit, "$a[6]");
  1781. Log3 $hash->{NAME}, 5, "DbLog $name -> processing event Timestamp: $a[0], Device: $a[1], Type: $a[2], Event: $a[3], Reading: $a[4], Value: $a[5], Unit: $a[6]";
  1782. }
  1783. use warnings;
  1784. if (lc($DbLogType) =~ m(history)) {
  1785. # insert history mit/ohne primary key
  1786. if ($usepkh && $hash->{MODEL} eq 'MYSQL') {
  1787. eval { $sth_ih = $dbh->prepare("INSERT IGNORE INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  1788. } elsif ($usepkh && $hash->{MODEL} eq 'SQLITE') {
  1789. eval { $sth_ih = $dbh->prepare("INSERT OR IGNORE INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  1790. } elsif ($usepkh && $hash->{MODEL} eq 'POSTGRESQL') {
  1791. eval { $sth_ih = $dbh->prepare("INSERT INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?) ON CONFLICT DO NOTHING"); };
  1792. } else {
  1793. # old behavior
  1794. eval { $sth_ih = $dbh->prepare("INSERT INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  1795. }
  1796. if ($@) {
  1797. # Eventliste zurückgeben wenn z.B. disk I/O error bei SQLITE
  1798. $error = encode_base64($@,"");
  1799. Log3 ($name, 2, "DbLog $name - Error: $@");
  1800. Log3 ($name, 5, "DbLog $name -> DbLog_PushAsync finished");
  1801. $dbh->disconnect();
  1802. return "$name|$error|0|$rowlist";
  1803. }
  1804. $sth_ih->bind_param_array(1, [@timestamp]);
  1805. $sth_ih->bind_param_array(2, [@device]);
  1806. $sth_ih->bind_param_array(3, [@type]);
  1807. $sth_ih->bind_param_array(4, [@event]);
  1808. $sth_ih->bind_param_array(5, [@reading]);
  1809. $sth_ih->bind_param_array(6, [@value]);
  1810. $sth_ih->bind_param_array(7, [@unit]);
  1811. }
  1812. if (lc($DbLogType) =~ m(current) ) {
  1813. # insert current mit/ohne primary key, insert-values für current werden generiert
  1814. if ($usepkc && $hash->{MODEL} eq 'MYSQL') {
  1815. eval { $sth_ic = $dbh->prepare("INSERT IGNORE INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  1816. } elsif ($usepkc && $hash->{MODEL} eq 'SQLITE') {
  1817. eval { $sth_ic = $dbh->prepare("INSERT OR IGNORE INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  1818. } elsif ($usepkc && $hash->{MODEL} eq 'POSTGRESQL') {
  1819. eval { $sth_ic = $dbh->prepare("INSERT INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?) ON CONFLICT DO NOTHING"); };
  1820. } else {
  1821. # old behavior
  1822. eval { $sth_ic = $dbh->prepare("INSERT INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  1823. }
  1824. if ($@) {
  1825. # Eventliste zurückgeben wenn z.B. Disk I/O error bei SQLITE
  1826. $error = encode_base64($@,"");
  1827. Log3 ($name, 2, "DbLog $name - Error: $@");
  1828. Log3 ($name, 5, "DbLog $name -> DbLog_PushAsync finished");
  1829. $dbh->disconnect();
  1830. return "$name|$error|0|$rowlist";
  1831. }
  1832. if ($usepkc && $hash->{MODEL} eq 'MYSQL') {
  1833. # update current (mit PK), insert-values für current wird generiert
  1834. $sth_uc = $dbh->prepare("REPLACE INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)");
  1835. $sth_uc->bind_param_array(1, [@timestamp]);
  1836. $sth_uc->bind_param_array(2, [@device]);
  1837. $sth_uc->bind_param_array(3, [@type]);
  1838. $sth_uc->bind_param_array(4, [@event]);
  1839. $sth_uc->bind_param_array(5, [@reading]);
  1840. $sth_uc->bind_param_array(6, [@value]);
  1841. $sth_uc->bind_param_array(7, [@unit]);
  1842. } elsif ($usepkc && $hash->{MODEL} eq 'SQLITE') {
  1843. # update current (mit PK), insert-values für current wird generiert
  1844. $sth_uc = $dbh->prepare("INSERT OR REPLACE INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)");
  1845. $sth_uc->bind_param_array(1, [@timestamp]);
  1846. $sth_uc->bind_param_array(2, [@device]);
  1847. $sth_uc->bind_param_array(3, [@type]);
  1848. $sth_uc->bind_param_array(4, [@event]);
  1849. $sth_uc->bind_param_array(5, [@reading]);
  1850. $sth_uc->bind_param_array(6, [@value]);
  1851. $sth_uc->bind_param_array(7, [@unit]);
  1852. } elsif ($usepkc && $hash->{MODEL} eq 'POSTGRESQL') {
  1853. # update current (mit PK), insert-values für current wird generiert
  1854. $sth_uc = $dbh->prepare("INSERT INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?) ON CONFLICT ($pkc)
  1855. DO UPDATE SET TIMESTAMP=EXCLUDED.TIMESTAMP, DEVICE=EXCLUDED.DEVICE, TYPE=EXCLUDED.TYPE, EVENT=EXCLUDED.EVENT, READING=EXCLUDED.READING,
  1856. VALUE=EXCLUDED.VALUE, UNIT=EXCLUDED.UNIT");
  1857. $sth_uc->bind_param_array(1, [@timestamp]);
  1858. $sth_uc->bind_param_array(2, [@device]);
  1859. $sth_uc->bind_param_array(3, [@type]);
  1860. $sth_uc->bind_param_array(4, [@event]);
  1861. $sth_uc->bind_param_array(5, [@reading]);
  1862. $sth_uc->bind_param_array(6, [@value]);
  1863. $sth_uc->bind_param_array(7, [@unit]);
  1864. } else {
  1865. # update current (ohne PK), insert-values für current wird generiert
  1866. $sth_uc = $dbh->prepare("UPDATE current SET TIMESTAMP=?, TYPE=?, EVENT=?, VALUE=?, UNIT=? WHERE (DEVICE=?) AND (READING=?)");
  1867. $sth_uc->bind_param_array(1, [@timestamp]);
  1868. $sth_uc->bind_param_array(2, [@type]);
  1869. $sth_uc->bind_param_array(3, [@event]);
  1870. $sth_uc->bind_param_array(4, [@value]);
  1871. $sth_uc->bind_param_array(5, [@unit]);
  1872. $sth_uc->bind_param_array(6, [@device]);
  1873. $sth_uc->bind_param_array(7, [@reading]);
  1874. }
  1875. }
  1876. # SQL-Startzeit
  1877. my $st = [gettimeofday];
  1878. my ($tuples, $rows);
  1879. # insert into history
  1880. eval { $dbh->begin_work() if($useta && $dbh->{AutoCommit}); }; # Transaktion wenn gewünscht und autocommit ein
  1881. if ($@) {
  1882. Log3($name, 2, "DbLog $name -> Error start transaction for history - $@");
  1883. }
  1884. eval {
  1885. if (lc($DbLogType) =~ m(history) ) {
  1886. ($tuples, $rows) = $sth_ih->execute_array( { ArrayTupleStatus => \my @tuple_status } );
  1887. my $nins_hist = 0;
  1888. my @n2hist;
  1889. for my $tuple (0..$#row_array) {
  1890. my $status = $tuple_status[$tuple];
  1891. $status = 0 if($status eq "0E0");
  1892. next if($status); # $status ist "1" wenn insert ok
  1893. Log3 $hash->{NAME}, 3, "DbLog $name -> Insert into history rejected".($usepkh?" (possible PK violation) ":" ")."- TS: $timestamp[$tuple], Device: $device[$tuple], Event: $event[$tuple]";
  1894. my $nlh = ($timestamp[$tuple]."|".$device[$tuple]."|".$type[$tuple]."|".$event[$tuple]."|".$reading[$tuple]."|".$value[$tuple]."|".$unit[$tuple]);
  1895. push(@n2hist, "$nlh");
  1896. $nins_hist++;
  1897. }
  1898. if(!$nins_hist) {
  1899. Log3 $hash->{NAME}, 4, "DbLog $name -> $ceti of $ceti events inserted into table history".($usepkh?" using PK on columns $pkh":"");
  1900. } else {
  1901. Log3 $hash->{NAME}, 4, "DbLog $name -> ".($ceti-$nins_hist)." of $ceti events inserted into table history".($usepkh?" using PK on columns $pkh":"");
  1902. s/\|/_ESC_/g for @n2hist; # escape Pipe "|"
  1903. $rowlist = join('§', @n2hist);
  1904. $rowlist = encode_base64($rowlist,"");
  1905. }
  1906. eval {$dbh->commit() if(!$dbh->{AutoCommit});}; # issue Turning on AutoCommit failed
  1907. if ($@) {
  1908. Log3($name, 2, "DbLog $name -> Error commit history - $@");
  1909. } else {
  1910. if(!$dbh->{AutoCommit}) {
  1911. Log3($name, 4, "DbLog $name -> insert table history committed");
  1912. } else {
  1913. Log3($name, 4, "DbLog $name -> insert table history committed by autocommit");
  1914. }
  1915. }
  1916. }
  1917. };
  1918. if ($@) {
  1919. $errorh = $@;
  1920. Log3 $hash->{NAME}, 2, "DbLog $name -> Error table history - $errorh";
  1921. $error = encode_base64($errorh,"");
  1922. $rowlback = $rowlist if($useta); # nicht gespeicherte Datensätze nur zurück geben wenn Transaktion ein
  1923. }
  1924. # update or insert current
  1925. eval { $dbh->begin_work() if($useta && $dbh->{AutoCommit}); }; # Transaktion wenn gewünscht und autocommit ein
  1926. if ($@) {
  1927. Log3($name, 2, "DbLog $name -> Error start transaction for current - $@");
  1928. }
  1929. eval {
  1930. if (lc($DbLogType) =~ m(current) ) {
  1931. ($tuples, $rows) = $sth_uc->execute_array( { ArrayTupleStatus => \my @tuple_status } );
  1932. my $nupd_cur = 0;
  1933. for my $tuple (0..$#row_array) {
  1934. my $status = $tuple_status[$tuple];
  1935. $status = 0 if($status eq "0E0");
  1936. next if($status); # $status ist "1" wenn update ok
  1937. Log3 $hash->{NAME}, 4, "DbLog $name -> Failed to update in current, try to insert - TS: $timestamp[$tuple], Device: $device[$tuple], Reading: $reading[$tuple], Status = $status";
  1938. push(@timestamp_cur, "$timestamp[$tuple]");
  1939. push(@device_cur, "$device[$tuple]");
  1940. push(@type_cur, "$type[$tuple]");
  1941. push(@event_cur, "$event[$tuple]");
  1942. push(@reading_cur, "$reading[$tuple]");
  1943. push(@value_cur, "$value[$tuple]");
  1944. push(@unit_cur, "$unit[$tuple]");
  1945. $nupd_cur++;
  1946. }
  1947. if(!$nupd_cur) {
  1948. Log3 $hash->{NAME}, 4, "DbLog $name -> $ceti of $ceti events updated in table current".($usepkc?" using PK on columns $pkc":"");
  1949. } else {
  1950. Log3 $hash->{NAME}, 4, "DbLog $name -> $nupd_cur of $ceti events not updated and try to insert into table current".($usepkc?" using PK on columns $pkc":"");
  1951. $doins = 1;
  1952. }
  1953. if ($doins) {
  1954. # events die nicht in Tabelle current updated wurden, werden in current neu eingefügt
  1955. $sth_ic->bind_param_array(1, [@timestamp_cur]);
  1956. $sth_ic->bind_param_array(2, [@device_cur]);
  1957. $sth_ic->bind_param_array(3, [@type_cur]);
  1958. $sth_ic->bind_param_array(4, [@event_cur]);
  1959. $sth_ic->bind_param_array(5, [@reading_cur]);
  1960. $sth_ic->bind_param_array(6, [@value_cur]);
  1961. $sth_ic->bind_param_array(7, [@unit_cur]);
  1962. ($tuples, $rows) = $sth_ic->execute_array( { ArrayTupleStatus => \my @tuple_status } );
  1963. my $nins_cur = 0;
  1964. for my $tuple (0..$#device_cur) {
  1965. my $status = $tuple_status[$tuple];
  1966. $status = 0 if($status eq "0E0");
  1967. next if($status); # $status ist "1" wenn insert ok
  1968. Log3 $hash->{NAME}, 3, "DbLog $name -> Insert into current rejected - TS: $timestamp[$tuple], Device: $device_cur[$tuple], Reading: $reading_cur[$tuple], Status = $status";
  1969. $nins_cur++;
  1970. }
  1971. if(!$nins_cur) {
  1972. Log3 $hash->{NAME}, 4, "DbLog $name -> ".($#device_cur+1)." of ".($#device_cur+1)." events inserted into table current ".($usepkc?" using PK on columns $pkc":"");
  1973. } else {
  1974. Log3 $hash->{NAME}, 4, "DbLog $name -> ".($#device_cur+1-$nins_cur)." of ".($#device_cur+1)." events inserted into table current".($usepkc?" using PK on columns $pkc":"");
  1975. }
  1976. }
  1977. eval {$dbh->commit() if(!$dbh->{AutoCommit});}; # issue Turning on AutoCommit failed
  1978. if ($@) {
  1979. Log3($name, 2, "DbLog $name -> Error commit table current - $@");
  1980. } else {
  1981. if(!$dbh->{AutoCommit}) {
  1982. Log3($name, 4, "DbLog $name -> insert / update table current committed");
  1983. } else {
  1984. Log3($name, 4, "DbLog $name -> insert / update table current committed by autocommit");
  1985. }
  1986. }
  1987. }
  1988. };
  1989. $dbh->disconnect();
  1990. # SQL-Laufzeit ermitteln
  1991. my $rt = tv_interval($st);
  1992. Log3 ($name, 5, "DbLog $name -> DbLog_PushAsync finished");
  1993. # Background-Laufzeit ermitteln
  1994. my $brt = tv_interval($bst);
  1995. $rt = $rt.",".$brt;
  1996. return "$name|$error|$rt|$rowlback";
  1997. }
  1998. #############################################################################################
  1999. # Auswertung non-blocking asynchron DbLog_PushAsync
  2000. #############################################################################################
  2001. sub DbLog_PushAsyncDone ($) {
  2002. my ($string) = @_;
  2003. my @a = split("\\|",$string);
  2004. my $name = $a[0];
  2005. my $hash = $defs{$name};
  2006. my $error = $a[1]?decode_base64($a[1]):0;
  2007. my $bt = $a[2];
  2008. my $rowlist = $a[3];
  2009. my $asyncmode = AttrVal($name, "asyncMode", undef);
  2010. my $memcount;
  2011. Log3 ($name, 5, "DbLog $name -> Start DbLog_PushAsyncDone");
  2012. if($rowlist) {
  2013. $rowlist = decode_base64($rowlist);
  2014. my @row_array = split('§', $rowlist);
  2015. #one Transaction
  2016. eval {
  2017. foreach my $row (@row_array) {
  2018. # Cache & CacheIndex für Events zum asynchronen Schreiben in DB
  2019. $hash->{cache}{index}++;
  2020. my $index = $hash->{cache}{index};
  2021. $hash->{cache}{".memcache"}{$index} = $row;
  2022. }
  2023. $memcount = scalar(keys%{$hash->{cache}{".memcache"}});
  2024. };
  2025. }
  2026. $memcount = $hash->{cache}{".memcache"}?scalar(keys%{$hash->{cache}{".memcache"}}):0;
  2027. readingsSingleUpdate($hash, 'CacheUsage', $memcount, 0);
  2028. if(AttrVal($name, "showproctime", undef) && $bt) {
  2029. my ($rt,$brt) = split(",", $bt);
  2030. readingsBeginUpdate($hash);
  2031. readingsBulkUpdate($hash, "background_processing_time", sprintf("%.4f",$brt));
  2032. readingsBulkUpdate($hash, "sql_processing_time", sprintf("%.4f",$rt));
  2033. readingsEndUpdate($hash, 1);
  2034. }
  2035. my $state = $error?$error:(IsDisabled($name))?"disabled":"connected";
  2036. my $evt = ($state eq $hash->{HELPER}{OLDSTATE})?0:1;
  2037. readingsSingleUpdate($hash, "state", $state, $evt);
  2038. $hash->{HELPER}{OLDSTATE} = $state;
  2039. if(!$asyncmode) {
  2040. delete($defs{$name}{READINGS}{NextSync});
  2041. delete($defs{$name}{READINGS}{background_processing_time});
  2042. delete($defs{$name}{READINGS}{sql_processing_time});
  2043. delete($defs{$name}{READINGS}{CacheUsage});
  2044. }
  2045. delete $hash->{HELPER}{".RUNNING_PID"};
  2046. delete $hash->{HELPER}{LASTLIMITRUNTIME} if(!$error);
  2047. Log3 ($name, 5, "DbLog $name -> DbLog_PushAsyncDone finished");
  2048. return;
  2049. }
  2050. #############################################################################################
  2051. # Abbruchroutine Timeout non-blocking asynchron DbLog_PushAsync
  2052. #############################################################################################
  2053. sub DbLog_PushAsyncAborted(@) {
  2054. my ($hash,$cause) = @_;
  2055. my $name = $hash->{NAME};
  2056. $cause = $cause?$cause:"Timeout: process terminated";
  2057. Log3 ($name, 2, "DbLog $name -> ".$hash->{HELPER}{".RUNNING_PID"}{fn}." ".$cause) if(!$hash->{HELPER}{SHUTDOWNSEQ});
  2058. readingsSingleUpdate($hash,"state",$cause, 1);
  2059. delete $hash->{HELPER}{".RUNNING_PID"};
  2060. delete $hash->{HELPER}{LASTLIMITRUNTIME};
  2061. }
  2062. ################################################################
  2063. #
  2064. # zerlegt uebergebenes FHEM-Datum in die einzelnen Bestandteile
  2065. # und fuegt noch Defaultwerte ein
  2066. # uebergebenes SQL-Format: YYYY-MM-DD HH24:MI:SS
  2067. #
  2068. ################################################################
  2069. sub DbLog_explode_datetime($%) {
  2070. my ($t, %def) = @_;
  2071. my %retv;
  2072. my (@datetime, @date, @time);
  2073. @datetime = split(" ", $t); #Datum und Zeit auftrennen
  2074. @date = split("-", $datetime[0]);
  2075. @time = split(":", $datetime[1]) if ($datetime[1]);
  2076. if ($date[0]) {$retv{year} = $date[0];} else {$retv{year} = $def{year};}
  2077. if ($date[1]) {$retv{month} = $date[1];} else {$retv{month} = $def{month};}
  2078. if ($date[2]) {$retv{day} = $date[2];} else {$retv{day} = $def{day};}
  2079. if ($time[0]) {$retv{hour} = $time[0];} else {$retv{hour} = $def{hour};}
  2080. if ($time[1]) {$retv{minute}= $time[1];} else {$retv{minute}= $def{minute};}
  2081. if ($time[2]) {$retv{second}= $time[2];} else {$retv{second}= $def{second};}
  2082. $retv{datetime}=DbLog_implode_datetime($retv{year}, $retv{month}, $retv{day}, $retv{hour}, $retv{minute}, $retv{second});
  2083. # Log 1, Dumper(%retv);
  2084. return %retv
  2085. }
  2086. sub DbLog_implode_datetime($$$$$$) {
  2087. my ($year, $month, $day, $hour, $minute, $second) = @_;
  2088. my $retv = $year."-".$month."-".$day." ".$hour.":".$minute.":".$second;
  2089. return $retv;
  2090. }
  2091. ###################################################################################
  2092. # Verbindungen zur DB aufbauen
  2093. ###################################################################################
  2094. sub DbLog_readCfg($){
  2095. my ($hash)= @_;
  2096. my $name = $hash->{NAME};
  2097. my $configfilename= $hash->{CONFIGURATION};
  2098. my %dbconfig;
  2099. # use generic fileRead to get configuration data
  2100. my ($err, @config) = FileRead($configfilename);
  2101. return $err if($err);
  2102. eval join("\n", @config);
  2103. return "could not read connection" if (!defined $dbconfig{connection});
  2104. $hash->{dbconn} = $dbconfig{connection};
  2105. return "could not read user" if (!defined $dbconfig{user});
  2106. $hash->{dbuser} = $dbconfig{user};
  2107. return "could not read password" if (!defined $dbconfig{password});
  2108. $attr{"sec$name"}{secret} = $dbconfig{password};
  2109. #check the database model
  2110. if($hash->{dbconn} =~ m/pg:/i) {
  2111. $hash->{MODEL}="POSTGRESQL";
  2112. } elsif ($hash->{dbconn} =~ m/mysql:/i) {
  2113. $hash->{MODEL}="MYSQL";
  2114. } elsif ($hash->{dbconn} =~ m/oracle:/i) {
  2115. $hash->{MODEL}="ORACLE";
  2116. } elsif ($hash->{dbconn} =~ m/sqlite:/i) {
  2117. $hash->{MODEL}="SQLITE";
  2118. } else {
  2119. $hash->{MODEL}="unknown";
  2120. Log3 $hash->{NAME}, 1, "Unknown database model found in configuration file $configfilename.";
  2121. Log3 $hash->{NAME}, 1, "Only MySQL/MariaDB, PostgreSQL, Oracle, SQLite are fully supported.";
  2122. return "unknown database type";
  2123. }
  2124. if($hash->{MODEL} eq "MYSQL") {
  2125. $hash->{UTF8} = defined($dbconfig{utf8})?$dbconfig{utf8}:0;
  2126. }
  2127. return;
  2128. }
  2129. sub DbLog_ConnectPush($;$$) {
  2130. # own $dbhp for synchronous logging and dblog_get
  2131. my ($hash,$get)= @_;
  2132. my $name = $hash->{NAME};
  2133. my $dbconn = $hash->{dbconn};
  2134. my $dbuser = $hash->{dbuser};
  2135. my $dbpassword = $attr{"sec$name"}{secret};
  2136. my $utf8 = defined($hash->{UTF8})?$hash->{UTF8}:0;
  2137. my ($dbhp,$state,$evt,$err);
  2138. return 0 if(IsDisabled($name));
  2139. if($init_done != 1) {
  2140. InternalTimer(gettimeofday()+5, "DbLog_ConnectPush", $hash, 0);
  2141. return;
  2142. }
  2143. Log3 $hash->{NAME}, 3, "DbLog $name - Creating Push-Handle to database $dbconn with user $dbuser" if(!$get);
  2144. my ($useac,$useta) = DbLog_commitMode($hash);
  2145. if(!$useac) {
  2146. eval {$dbhp = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoCommit => 0, mysql_enable_utf8 => $utf8 });};
  2147. } elsif($useac == 1) {
  2148. eval {$dbhp = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoCommit => 1, mysql_enable_utf8 => $utf8 });};
  2149. } else {
  2150. # Server default
  2151. eval {$dbhp = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, mysql_enable_utf8 => $utf8 });};
  2152. }
  2153. if($@) {
  2154. $err = $@;
  2155. Log3 $hash->{NAME}, 2, "DbLog $name - Error: $@";
  2156. }
  2157. if(!$dbhp) {
  2158. RemoveInternalTimer($hash, "DbLog_ConnectPush");
  2159. Log3 $hash->{NAME}, 4, "DbLog $name - Trying to connect to database";
  2160. $state = $err?$err:(IsDisabled($name))?"disabled":"disconnected";
  2161. $evt = ($state eq $hash->{HELPER}{OLDSTATE})?0:1;
  2162. readingsSingleUpdate($hash, "state", $state, $evt);
  2163. $hash->{HELPER}{OLDSTATE} = $state;
  2164. InternalTimer(gettimeofday()+5, 'DbLog_ConnectPush', $hash, 0);
  2165. Log3 $hash->{NAME}, 4, "DbLog $name - Waiting for database connection";
  2166. return 0;
  2167. }
  2168. $dbhp->{RaiseError} = 0;
  2169. $dbhp->{PrintError} = 1;
  2170. Log3 $hash->{NAME}, 3, "DbLog $name - Push-Handle to db $dbconn created" if(!$get);
  2171. Log3 $hash->{NAME}, 3, "DbLog $name - UTF8 support enabled" if($utf8 && $hash->{MODEL} eq "MYSQL" && !$get);
  2172. if(!$get) {
  2173. $state = "connected";
  2174. $evt = ($state eq $hash->{HELPER}{OLDSTATE})?0:1;
  2175. readingsSingleUpdate($hash, "state", $state, $evt);
  2176. $hash->{HELPER}{OLDSTATE} = $state;
  2177. }
  2178. $hash->{DBHP}= $dbhp;
  2179. if ($hash->{MODEL} eq "SQLITE") {
  2180. $dbhp->do("PRAGMA temp_store=MEMORY");
  2181. $dbhp->do("PRAGMA synchronous=FULL"); # For maximum reliability and for robustness against database corruption,
  2182. # SQLite should always be run with its default synchronous setting of FULL.
  2183. # https://sqlite.org/howtocorrupt.html
  2184. $dbhp->do("PRAGMA journal_mode=WAL");
  2185. $dbhp->do("PRAGMA cache_size=4000");
  2186. }
  2187. return 1;
  2188. }
  2189. sub DbLog_ConnectNewDBH($) {
  2190. # new dbh for common use (except DbLog_Push and get-function)
  2191. my ($hash)= @_;
  2192. my $name = $hash->{NAME};
  2193. my $dbconn = $hash->{dbconn};
  2194. my $dbuser = $hash->{dbuser};
  2195. my $dbpassword = $attr{"sec$name"}{secret};
  2196. my $utf8 = defined($hash->{UTF8})?$hash->{UTF8}:0;
  2197. my $dbh;
  2198. my ($useac,$useta) = DbLog_commitMode($hash);
  2199. if(!$useac) {
  2200. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoCommit => 0, mysql_enable_utf8 => $utf8 });};
  2201. } elsif($useac == 1) {
  2202. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoCommit => 1, mysql_enable_utf8 => $utf8 });};
  2203. } else {
  2204. # Server default
  2205. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, mysql_enable_utf8 => $utf8 });};
  2206. }
  2207. if($@) {
  2208. Log3($name, 2, "DbLog $name - $@");
  2209. my $state = $@?$@:(IsDisabled($name))?"disabled":"disconnected";
  2210. my $evt = ($state eq $hash->{HELPER}{OLDSTATE})?0:1;
  2211. readingsSingleUpdate($hash, "state", $state, $evt);
  2212. $hash->{HELPER}{OLDSTATE} = $state;
  2213. }
  2214. if($dbh) {
  2215. $dbh->{RaiseError} = 0;
  2216. $dbh->{PrintError} = 1;
  2217. return $dbh;
  2218. } else {
  2219. return 0;
  2220. }
  2221. }
  2222. ##########################################################################
  2223. #
  2224. # Prozedur zum Ausfuehren von SQL-Statements durch externe Module
  2225. #
  2226. # param1: DbLog-hash
  2227. # param2: SQL-Statement
  2228. ##########################################################################
  2229. sub DbLog_ExecSQL($$)
  2230. {
  2231. my ($hash,$sql)= @_;
  2232. Log3 $hash->{NAME}, 4, "Executing $sql";
  2233. my $dbh = DbLog_ConnectNewDBH($hash);
  2234. return if(!$dbh);
  2235. my $sth = DbLog_ExecSQL1($hash,$dbh,$sql);
  2236. if(!$sth) {
  2237. #retry
  2238. $dbh->disconnect();
  2239. $dbh = DbLog_ConnectNewDBH($hash);
  2240. return if(!$dbh);
  2241. $sth = DbLog_ExecSQL1($hash,$dbh,$sql);
  2242. if(!$sth) {
  2243. Log3 $hash->{NAME}, 2, "DBLog retry failed.";
  2244. $dbh->disconnect();
  2245. return 0;
  2246. }
  2247. Log3 $hash->{NAME}, 2, "DBLog retry ok.";
  2248. }
  2249. eval {$dbh->commit() if(!$dbh->{AutoCommit});};
  2250. $dbh->disconnect();
  2251. return $sth;
  2252. }
  2253. sub DbLog_ExecSQL1($$$)
  2254. {
  2255. my ($hash,$dbh,$sql)= @_;
  2256. $dbh->{RaiseError} = 1;
  2257. $dbh->{PrintError} = 0;
  2258. my $sth;
  2259. eval { $sth = $dbh->do($sql); };
  2260. if($@) {
  2261. Log3 $hash->{NAME}, 2, "DBLog error: $@";
  2262. return 0;
  2263. }
  2264. return $sth;
  2265. }
  2266. ################################################################
  2267. #
  2268. # GET Funktion
  2269. # wird zb. zur Generierung der Plots implizit aufgerufen
  2270. # infile : [-|current|history]
  2271. # outfile: [-|ALL|INT|WEBCHART]
  2272. #
  2273. ################################################################
  2274. sub DbLog_Get($@) {
  2275. my ($hash, @a) = @_;
  2276. my $name = $hash->{NAME};
  2277. my $utf8 = defined($hash->{UTF8})?$hash->{UTF8}:0;
  2278. my $dbh;
  2279. return DbLog_dbReadings($hash,@a) if $a[1] =~ m/^Readings/;
  2280. return "Usage: get $a[0] <in> <out> <from> <to> <column_spec>...\n".
  2281. " where column_spec is <device>:<reading>:<default>:<fn>\n" .
  2282. " see the #DbLog entries in the .gplot files\n" .
  2283. " <in> is not used, only for compatibility for FileLog, please use - \n" .
  2284. " <out> is a prefix, - means stdout\n"
  2285. if(int(@a) < 5);
  2286. shift @a;
  2287. my $inf = lc(shift @a);
  2288. my $outf = lc(shift @a);
  2289. my $from = shift @a;
  2290. my $to = shift @a; # Now @a contains the list of column_specs
  2291. my ($internal, @fld);
  2292. if($inf eq "-") {
  2293. $inf = "history";
  2294. }
  2295. if($outf eq "int" && $inf eq "current") {
  2296. $inf = "history";
  2297. Log3 $hash->{NAME}, 3, "Defining DbLog SVG-Plots with :CURRENT is deprecated. Please define DbLog SVG-Plots with :HISTORY instead of :CURRENT. (define <mySVG> SVG <DbLogDev>:<gplotfile>:HISTORY)";
  2298. }
  2299. if($outf eq "int") {
  2300. $outf = "-";
  2301. $internal = 1;
  2302. } elsif($outf eq "array"){
  2303. } elsif(lc($outf) eq "webchart") {
  2304. # redirect the get request to the DbLog_chartQuery function
  2305. return DbLog_chartQuery($hash, @_);
  2306. }
  2307. my @readings = ();
  2308. my (%sqlspec, %from_datetime, %to_datetime);
  2309. #uebergebenen Timestamp anpassen
  2310. #moegliche Formate: YYYY | YYYY-MM | YYYY-MM-DD | YYYY-MM-DD_HH24
  2311. $from =~ s/_/\ /g;
  2312. $to =~ s/_/\ /g;
  2313. %from_datetime = DbLog_explode_datetime($from, DbLog_explode_datetime("2000-01-01 00:00:00", ()));
  2314. %to_datetime = DbLog_explode_datetime($to, DbLog_explode_datetime("2099-01-01 00:00:00", ()));
  2315. $from = $from_datetime{datetime};
  2316. $to = $to_datetime{datetime};
  2317. if($to =~ /(\d{4})-(\d{2})-(\d{2}) 23:59:59/) {
  2318. # 03.09.2018 : https://forum.fhem.de/index.php/topic,65860.msg815640.html#msg815640
  2319. $to =~ /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/;
  2320. my $tc = timelocal($6, $5, $4, $3, $2-1, $1-1900);
  2321. $tc++;
  2322. $to = strftime "%Y-%m-%d %H:%M:%S", localtime($tc);
  2323. }
  2324. my ($retval,$retvaldummy,$hour,$sql_timestamp, $sql_device, $sql_reading, $sql_value, $type, $event, $unit) = "";
  2325. my @ReturnArray;
  2326. my $writeout = 0;
  2327. my (@min, @max, @sum, @cnt, @lastv, @lastd, @mind, @maxd);
  2328. my (%tstamp, %lasttstamp, $out_tstamp, $out_value, $minval, $maxval, $deltacalc); #fuer delta-h/d Berechnung
  2329. #extract the Device:Reading arguments into @readings array
  2330. for(my $i = 0; $i < int(@a); $i++) {
  2331. @fld = split(":", $a[$i], 5);
  2332. $readings[$i][0] = $fld[0]; # Device
  2333. $readings[$i][1] = $fld[1]; # Reading
  2334. $readings[$i][2] = $fld[2]; # Default
  2335. $readings[$i][3] = $fld[3]; # function
  2336. $readings[$i][4] = $fld[4]; # regexp
  2337. $readings[$i][1] = "%" if(!$readings[$i][1] || length($readings[$i][1])==0); #falls Reading nicht gefuellt setze Joker
  2338. }
  2339. $dbh = $hash->{DBHP};
  2340. if ( !$dbh || not $dbh->ping ) {
  2341. # DB Session dead, try to reopen now !
  2342. return "Can't connect to database." if(!DbLog_ConnectPush($hash,1));
  2343. $dbh = $hash->{DBHP};
  2344. }
  2345. if( $hash->{PID} != $$ ) {
  2346. #create new connection for plotfork
  2347. $dbh->disconnect();
  2348. return "Can't connect to database." if(!DbLog_ConnectPush($hash,1));
  2349. $dbh = $hash->{DBHP};
  2350. }
  2351. #vorbereiten der DB-Abfrage, DB-Modell-abhaengig
  2352. if ($hash->{MODEL} eq "POSTGRESQL") {
  2353. $sqlspec{get_timestamp} = "TO_CHAR(TIMESTAMP, 'YYYY-MM-DD HH24:MI:SS')";
  2354. $sqlspec{from_timestamp} = "TO_TIMESTAMP('$from', 'YYYY-MM-DD HH24:MI:SS')";
  2355. $sqlspec{to_timestamp} = "TO_TIMESTAMP('$to', 'YYYY-MM-DD HH24:MI:SS')";
  2356. #$sqlspec{reading_clause} = "(DEVICE || '|' || READING)";
  2357. $sqlspec{order_by_hour} = "TO_CHAR(TIMESTAMP, 'YYYY-MM-DD HH24')";
  2358. $sqlspec{max_value} = "MAX(VALUE)";
  2359. $sqlspec{day_before} = "($sqlspec{from_timestamp} - INTERVAL '1 DAY')";
  2360. } elsif ($hash->{MODEL} eq "ORACLE") {
  2361. $sqlspec{get_timestamp} = "TO_CHAR(TIMESTAMP, 'YYYY-MM-DD HH24:MI:SS')";
  2362. $sqlspec{from_timestamp} = "TO_TIMESTAMP('$from', 'YYYY-MM-DD HH24:MI:SS')";
  2363. $sqlspec{to_timestamp} = "TO_TIMESTAMP('$to', 'YYYY-MM-DD HH24:MI:SS')";
  2364. $sqlspec{order_by_hour} = "TO_CHAR(TIMESTAMP, 'YYYY-MM-DD HH24')";
  2365. $sqlspec{max_value} = "MAX(VALUE)";
  2366. $sqlspec{day_before} = "DATE_SUB($sqlspec{from_timestamp},INTERVAL 1 DAY)";
  2367. } elsif ($hash->{MODEL} eq "MYSQL") {
  2368. $sqlspec{get_timestamp} = "DATE_FORMAT(TIMESTAMP, '%Y-%m-%d %H:%i:%s')";
  2369. $sqlspec{from_timestamp} = "STR_TO_DATE('$from', '%Y-%m-%d %H:%i:%s')";
  2370. $sqlspec{to_timestamp} = "STR_TO_DATE('$to', '%Y-%m-%d %H:%i:%s')";
  2371. $sqlspec{order_by_hour} = "DATE_FORMAT(TIMESTAMP, '%Y-%m-%d %H')";
  2372. $sqlspec{max_value} = "MAX(CAST(VALUE AS DECIMAL(20,8)))";
  2373. $sqlspec{day_before} = "DATE_SUB($sqlspec{from_timestamp},INTERVAL 1 DAY)";
  2374. } elsif ($hash->{MODEL} eq "SQLITE") {
  2375. $sqlspec{get_timestamp} = "TIMESTAMP";
  2376. $sqlspec{from_timestamp} = "'$from'";
  2377. $sqlspec{to_timestamp} = "'$to'";
  2378. $sqlspec{order_by_hour} = "strftime('%Y-%m-%d %H', TIMESTAMP)";
  2379. $sqlspec{max_value} = "MAX(VALUE)";
  2380. $sqlspec{day_before} = "date($sqlspec{from_timestamp},'-1 day')";
  2381. } else {
  2382. $sqlspec{get_timestamp} = "TIMESTAMP";
  2383. $sqlspec{from_timestamp} = "'$from'";
  2384. $sqlspec{to_timestamp} = "'$to'";
  2385. $sqlspec{order_by_hour} = "strftime('%Y-%m-%d %H', TIMESTAMP)";
  2386. $sqlspec{max_value} = "MAX(VALUE)";
  2387. $sqlspec{day_before} = "date($sqlspec{from_timestamp},'-1 day')";
  2388. }
  2389. if($outf =~ m/(all|array)/) {
  2390. $sqlspec{all} = ",TYPE,EVENT,UNIT";
  2391. $sqlspec{all_max} = ",MAX(TYPE) AS TYPE,MAX(EVENT) AS EVENT,MAX(UNIT) AS UNIT";
  2392. } else {
  2393. $sqlspec{all} = "";
  2394. $sqlspec{all_max} = "";
  2395. }
  2396. for(my $i=0; $i<int(@readings); $i++) {
  2397. # ueber alle Readings
  2398. # Variablen initialisieren
  2399. $min[$i] = (~0 >> 1);
  2400. $max[$i] = -(~0 >> 1);
  2401. $sum[$i] = 0;
  2402. $cnt[$i] = 0;
  2403. $lastv[$i] = 0;
  2404. $lastd[$i] = "undef";
  2405. $mind[$i] = "undef";
  2406. $maxd[$i] = "undef";
  2407. $minval = (~0 >> 1);
  2408. $maxval = -(~0 >> 1);
  2409. $deltacalc = 0;
  2410. if($readings[$i]->[3] && ($readings[$i]->[3] eq "delta-h" || $readings[$i]->[3] eq "delta-d")) {
  2411. $deltacalc = 1;
  2412. }
  2413. my $stm;
  2414. my $stm2;
  2415. my $stmdelta;
  2416. $stm = "SELECT
  2417. MAX($sqlspec{get_timestamp}) AS TIMESTAMP,
  2418. MAX(DEVICE) AS DEVICE,
  2419. MAX(READING) AS READING,
  2420. $sqlspec{max_value}
  2421. $sqlspec{all_max} ";
  2422. $stm .= "FROM current " if($inf eq "current");
  2423. $stm .= "FROM history " if($inf eq "history");
  2424. $stm .= "WHERE 1=1 ";
  2425. $stm .= "AND DEVICE = '".$readings[$i]->[0]."' " if ($readings[$i]->[0] !~ m(\%));
  2426. $stm .= "AND DEVICE LIKE '".$readings[$i]->[0]."' " if(($readings[$i]->[0] !~ m(^\%$)) && ($readings[$i]->[0] =~ m(\%)));
  2427. $stm .= "AND READING = '".$readings[$i]->[1]."' " if ($readings[$i]->[1] !~ m(\%));
  2428. $stm .= "AND READING LIKE '".$readings[$i]->[1]."' " if(($readings[$i]->[1] !~ m(^%$)) && ($readings[$i]->[1] =~ m(\%)));
  2429. $stmdelta = $stm;
  2430. $stm .= "AND TIMESTAMP < $sqlspec{from_timestamp} ";
  2431. $stm .= "AND TIMESTAMP > $sqlspec{day_before} ";
  2432. $stm .= "UNION ALL ";
  2433. $stm2 = "SELECT
  2434. $sqlspec{get_timestamp},
  2435. DEVICE,
  2436. READING,
  2437. VALUE
  2438. $sqlspec{all} ";
  2439. $stm2 .= "FROM current " if($inf eq "current");
  2440. $stm2 .= "FROM history " if($inf eq "history");
  2441. $stm2 .= "WHERE 1=1 ";
  2442. $stm2 .= "AND DEVICE = '".$readings[$i]->[0]."' " if ($readings[$i]->[0] !~ m(\%));
  2443. $stm2 .= "AND DEVICE LIKE '".$readings[$i]->[0]."' " if(($readings[$i]->[0] !~ m(^\%$)) && ($readings[$i]->[0] =~ m(\%)));
  2444. $stm2 .= "AND READING = '".$readings[$i]->[1]."' " if ($readings[$i]->[1] !~ m(\%));
  2445. $stm2 .= "AND READING LIKE '".$readings[$i]->[1]."' " if(($readings[$i]->[1] !~ m(^%$)) && ($readings[$i]->[1] =~ m(\%)));
  2446. $stm2 .= "AND TIMESTAMP >= $sqlspec{from_timestamp} ";
  2447. $stm2 .= "AND TIMESTAMP <= $sqlspec{to_timestamp} "; # 03.09.2018 : https://forum.fhem.de/index.php/topic,65860.msg815640.html#msg815640
  2448. $stm2 .= "ORDER BY TIMESTAMP";
  2449. if($deltacalc) {
  2450. $stmdelta .= "AND TIMESTAMP >= $sqlspec{from_timestamp} ";
  2451. $stmdelta .= "AND TIMESTAMP <= $sqlspec{to_timestamp} "; # 03.09.2018 : https://forum.fhem.de/index.php/topic,65860.msg815640.html#msg815640
  2452. $stmdelta .= "GROUP BY $sqlspec{order_by_hour} " if($deltacalc);
  2453. $stmdelta .= "ORDER BY TIMESTAMP";
  2454. $stm .= $stmdelta;
  2455. } else {
  2456. $stm = $stm2;
  2457. }
  2458. Log3 $hash->{NAME}, 4, "Processing Statement: $stm";
  2459. my $sth= $dbh->prepare($stm) ||
  2460. return "Cannot prepare statement $stm: $DBI::errstr";
  2461. my $rc= $sth->execute() ||
  2462. return "Cannot execute statement $stm: $DBI::errstr";
  2463. if($outf =~ m/(all|array)/) {
  2464. $sth->bind_columns(undef, \$sql_timestamp, \$sql_device, \$sql_reading, \$sql_value, \$type, \$event, \$unit);
  2465. }
  2466. else {
  2467. $sth->bind_columns(undef, \$sql_timestamp, \$sql_device, \$sql_reading, \$sql_value);
  2468. }
  2469. if ($outf =~ m/(all)/) {
  2470. $retval .= "Timestamp: Device, Type, Event, Reading, Value, Unit\n";
  2471. $retval .= "=====================================================\n";
  2472. }
  2473. while($sth->fetch()) {
  2474. ############ Auswerten des 5. Parameters: Regexp ###################
  2475. # die Regexep wird vor der Function ausgewertet und der Wert im Feld
  2476. # Value angepasst.
  2477. ####################################################################
  2478. if($readings[$i]->[4]) {
  2479. #evaluate
  2480. my $val = $sql_value;
  2481. my $ts = $sql_timestamp;
  2482. eval("$readings[$i]->[4]");
  2483. $sql_value = $val;
  2484. $sql_timestamp = $ts;
  2485. if($@) {Log3 $hash->{NAME}, 3, "DbLog: Error in inline function: <".$readings[$i]->[4].">, Error: $@";}
  2486. }
  2487. if($sql_timestamp lt $from && $deltacalc) {
  2488. if(Scalar::Util::looks_like_number($sql_value)){
  2489. #nur setzen wenn nummerisch
  2490. $minval = $sql_value if($sql_value < $minval);
  2491. $maxval = $sql_value if($sql_value > $maxval);
  2492. $lastv[$i] = $sql_value;
  2493. }
  2494. } else {
  2495. $writeout = 0;
  2496. $out_value = "";
  2497. $out_tstamp = "";
  2498. $retvaldummy = "";
  2499. if($readings[$i]->[4]) {
  2500. $out_tstamp = $sql_timestamp;
  2501. $writeout=1 if(!$deltacalc);
  2502. }
  2503. ############ Auswerten des 4. Parameters: function ###################
  2504. if($readings[$i]->[3] && $readings[$i]->[3] eq "int") {
  2505. #nur den integerwert uebernehmen falls zb value=15°C
  2506. $out_value = $1 if($sql_value =~ m/^(\d+).*/o);
  2507. $out_tstamp = $sql_timestamp;
  2508. $writeout=1;
  2509. } elsif ($readings[$i]->[3] && $readings[$i]->[3] =~ m/^int(\d+).*/o) {
  2510. #Uebernehme den Dezimalwert mit den angegebenen Stellen an Nachkommastellen
  2511. $out_value = $1 if($sql_value =~ m/^([-\.\d]+).*/o);
  2512. $out_tstamp = $sql_timestamp;
  2513. $writeout=1;
  2514. } elsif ($readings[$i]->[3] && $readings[$i]->[3] eq "delta-ts" && lc($sql_value) !~ m(ignore)) {
  2515. #Berechung der vergangen Sekunden seit dem letten Logeintrag
  2516. #zb. die Zeit zwischen on/off
  2517. my @a = split("[- :]", $sql_timestamp);
  2518. my $akt_ts = mktime($a[5],$a[4],$a[3],$a[2],$a[1]-1,$a[0]-1900,0,0,-1);
  2519. if($lastd[$i] ne "undef") {
  2520. @a = split("[- :]", $lastd[$i]);
  2521. }
  2522. my $last_ts = mktime($a[5],$a[4],$a[3],$a[2],$a[1]-1,$a[0]-1900,0,0,-1);
  2523. $out_tstamp = $sql_timestamp;
  2524. $out_value = sprintf("%02d", $akt_ts - $last_ts);
  2525. if(lc($sql_value) =~ m(hide)){$writeout=0;} else {$writeout=1;}
  2526. } elsif ($readings[$i]->[3] && $readings[$i]->[3] eq "delta-h") {
  2527. #Berechnung eines Stundenwertes
  2528. %tstamp = DbLog_explode_datetime($sql_timestamp, ());
  2529. if($lastd[$i] eq "undef") {
  2530. %lasttstamp = DbLog_explode_datetime($sql_timestamp, ());
  2531. $lasttstamp{hour} = "00";
  2532. } else {
  2533. %lasttstamp = DbLog_explode_datetime($lastd[$i], ());
  2534. }
  2535. # 04 01
  2536. # 06 23
  2537. if("$tstamp{hour}" ne "$lasttstamp{hour}") {
  2538. # Aenderung der stunde, Berechne Delta
  2539. #wenn die Stundendifferenz größer 1 ist muss ein Dummyeintrag erstellt werden
  2540. $retvaldummy = "";
  2541. if(($tstamp{hour}-$lasttstamp{hour}) > 1) {
  2542. for (my $j=$lasttstamp{hour}+1; $j < $tstamp{hour}; $j++) {
  2543. $out_value = "0";
  2544. $hour = $j;
  2545. $hour = '0'.$j if $j<10;
  2546. $cnt[$i]++;
  2547. $out_tstamp = DbLog_implode_datetime($tstamp{year}, $tstamp{month}, $tstamp{day}, $hour, "30", "00");
  2548. if ($outf =~ m/(all)/) {
  2549. # Timestamp: Device, Type, Event, Reading, Value, Unit
  2550. $retvaldummy .= sprintf("%s: %s, %s, %s, %s, %s, %s\n", $out_tstamp, $sql_device, $type, $event, $sql_reading, $out_value, $unit);
  2551. } elsif ($outf =~ m/(array)/) {
  2552. push(@ReturnArray, {"tstamp" => $out_tstamp, "device" => $sql_device, "type" => $type, "event" => $event, "reading" => $sql_reading, "value" => $out_value, "unit" => $unit});
  2553. } else {
  2554. $out_tstamp =~ s/\ /_/g; #needed by generating plots
  2555. $retvaldummy .= "$out_tstamp $out_value\n";
  2556. }
  2557. }
  2558. }
  2559. if(($tstamp{hour}-$lasttstamp{hour}) < 0) {
  2560. for (my $j=0; $j < $tstamp{hour}; $j++) {
  2561. $out_value = "0";
  2562. $hour = $j;
  2563. $hour = '0'.$j if $j<10;
  2564. $cnt[$i]++;
  2565. $out_tstamp = DbLog_implode_datetime($tstamp{year}, $tstamp{month}, $tstamp{day}, $hour, "30", "00");
  2566. if ($outf =~ m/(all)/) {
  2567. # Timestamp: Device, Type, Event, Reading, Value, Unit
  2568. $retvaldummy .= sprintf("%s: %s, %s, %s, %s, %s, %s\n", $out_tstamp, $sql_device, $type, $event, $sql_reading, $out_value, $unit);
  2569. } elsif ($outf =~ m/(array)/) {
  2570. push(@ReturnArray, {"tstamp" => $out_tstamp, "device" => $sql_device, "type" => $type, "event" => $event, "reading" => $sql_reading, "value" => $out_value, "unit" => $unit});
  2571. } else {
  2572. $out_tstamp =~ s/\ /_/g; #needed by generating plots
  2573. $retvaldummy .= "$out_tstamp $out_value\n";
  2574. }
  2575. }
  2576. }
  2577. $out_value = sprintf("%g", $maxval - $minval);
  2578. $sum[$i] += $out_value;
  2579. $cnt[$i]++;
  2580. $out_tstamp = DbLog_implode_datetime($lasttstamp{year}, $lasttstamp{month}, $lasttstamp{day}, $lasttstamp{hour}, "30", "00");
  2581. #$minval = (~0 >> 1);
  2582. $minval = $maxval;
  2583. # $maxval = -(~0 >> 1);
  2584. $writeout=1;
  2585. }
  2586. } elsif ($readings[$i]->[3] && $readings[$i]->[3] eq "delta-d") {
  2587. #Berechnung eines Tageswertes
  2588. %tstamp = DbLog_explode_datetime($sql_timestamp, ());
  2589. if($lastd[$i] eq "undef") {
  2590. %lasttstamp = DbLog_explode_datetime($sql_timestamp, ());
  2591. } else {
  2592. %lasttstamp = DbLog_explode_datetime($lastd[$i], ());
  2593. }
  2594. if("$tstamp{day}" ne "$lasttstamp{day}") {
  2595. # Aenderung des Tages, Berechne Delta
  2596. $out_value = sprintf("%g", $maxval - $minval);
  2597. $sum[$i] += $out_value;
  2598. $cnt[$i]++;
  2599. $out_tstamp = DbLog_implode_datetime($lasttstamp{year}, $lasttstamp{month}, $lasttstamp{day}, "12", "00", "00");
  2600. # $minval = (~0 >> 1);
  2601. $minval = $maxval;
  2602. # $maxval = -(~0 >> 1);
  2603. $writeout=1;
  2604. }
  2605. } else {
  2606. $out_value = $sql_value;
  2607. $out_tstamp = $sql_timestamp;
  2608. $writeout=1;
  2609. }
  2610. # Wenn Attr SuppressUndef gesetzt ist, dann ausfiltern aller undef-Werte
  2611. $writeout = 0 if (!defined($sql_value) && AttrVal($hash->{NAME}, "suppressUndef", 0));
  2612. ###################### Ausgabe ###########################
  2613. if($writeout) {
  2614. if ($outf =~ m/(all)/) {
  2615. # Timestamp: Device, Type, Event, Reading, Value, Unit
  2616. $retval .= sprintf("%s: %s, %s, %s, %s, %s, %s\n", $out_tstamp, $sql_device, $type, $event, $sql_reading, $out_value, $unit);
  2617. $retval .= $retvaldummy;
  2618. } elsif ($outf =~ m/(array)/) {
  2619. push(@ReturnArray, {"tstamp" => $out_tstamp, "device" => $sql_device, "type" => $type, "event" => $event, "reading" => $sql_reading, "value" => $out_value, "unit" => $unit});
  2620. } else {
  2621. $out_tstamp =~ s/\ /_/g; #needed by generating plots
  2622. $retval .= "$out_tstamp $out_value\n";
  2623. $retval .= $retvaldummy;
  2624. }
  2625. }
  2626. if(Scalar::Util::looks_like_number($sql_value)){
  2627. #nur setzen wenn nummerisch
  2628. if($deltacalc) {
  2629. if(Scalar::Util::looks_like_number($out_value)){
  2630. if($out_value < $min[$i]) {
  2631. $min[$i] = $out_value;
  2632. $mind[$i] = $out_tstamp;
  2633. }
  2634. if($out_value > $max[$i]) {
  2635. $max[$i] = $out_value;
  2636. $maxd[$i] = $out_tstamp;
  2637. }
  2638. }
  2639. $maxval = $sql_value;
  2640. } else {
  2641. if($sql_value < $min[$i]) {
  2642. $min[$i] = $sql_value;
  2643. $mind[$i] = $sql_timestamp;
  2644. }
  2645. if($sql_value > $max[$i]) {
  2646. $max[$i] = $sql_value;
  2647. $maxd[$i] = $sql_timestamp;
  2648. }
  2649. $sum[$i] += $sql_value;
  2650. $minval = $sql_value if($sql_value < $minval);
  2651. $maxval = $sql_value if($sql_value > $maxval);
  2652. }
  2653. } else {
  2654. $min[$i] = 0;
  2655. $max[$i] = 0;
  2656. $sum[$i] = 0;
  2657. $minval = 0;
  2658. $maxval = 0;
  2659. }
  2660. if(!$deltacalc) {
  2661. $cnt[$i]++;
  2662. $lastv[$i] = $sql_value;
  2663. } else {
  2664. $lastv[$i] = $out_value if($out_value);
  2665. }
  2666. $lastd[$i] = $sql_timestamp;
  2667. }
  2668. } #while fetchrow
  2669. ######## den letzten Abschlusssatz rausschreiben ##########
  2670. if($readings[$i]->[3] && ($readings[$i]->[3] eq "delta-h" || $readings[$i]->[3] eq "delta-d")) {
  2671. if($lastd[$i] eq "undef") {
  2672. $out_value = "0";
  2673. $out_tstamp = DbLog_implode_datetime($from_datetime{year}, $from_datetime{month}, $from_datetime{day}, $from_datetime{hour}, "30", "00") if($readings[$i]->[3] eq "delta-h");
  2674. $out_tstamp = DbLog_implode_datetime($from_datetime{year}, $from_datetime{month}, $from_datetime{day}, "12", "00", "00") if($readings[$i]->[3] eq "delta-d");
  2675. } else {
  2676. %lasttstamp = DbLog_explode_datetime($lastd[$i], ());
  2677. $out_value = sprintf("%g", $maxval - $minval);
  2678. $out_tstamp = DbLog_implode_datetime($lasttstamp{year}, $lasttstamp{month}, $lasttstamp{day}, $lasttstamp{hour}, "30", "00") if($readings[$i]->[3] eq "delta-h");
  2679. $out_tstamp = DbLog_implode_datetime($lasttstamp{year}, $lasttstamp{month}, $lasttstamp{day}, "12", "00", "00") if($readings[$i]->[3] eq "delta-d");
  2680. }
  2681. $sum[$i] += $out_value;
  2682. $cnt[$i]++;
  2683. if($outf =~ m/(all)/) {
  2684. $retval .= sprintf("%s: %s %s %s %s %s %s\n", $out_tstamp, $sql_device, $type, $event, $sql_reading, $out_value, $unit);
  2685. } elsif ($outf =~ m/(array)/) {
  2686. push(@ReturnArray, {"tstamp" => $out_tstamp, "device" => $sql_device, "type" => $type, "event" => $event, "reading" => $sql_reading, "value" => $out_value, "unit" => $unit});
  2687. } else {
  2688. $out_tstamp =~ s/\ /_/g; #needed by generating plots
  2689. $retval .= "$out_tstamp $out_value\n";
  2690. }
  2691. }
  2692. # DatenTrenner setzen
  2693. $retval .= "#$readings[$i]->[0]";
  2694. $retval .= ":";
  2695. $retval .= "$readings[$i]->[1]" if($readings[$i]->[1]);
  2696. $retval .= ":";
  2697. $retval .= "$readings[$i]->[2]" if($readings[$i]->[2]);
  2698. $retval .= ":";
  2699. $retval .= "$readings[$i]->[3]" if($readings[$i]->[3]);
  2700. $retval .= ":";
  2701. $retval .= "$readings[$i]->[4]" if($readings[$i]->[4]);
  2702. $retval .= "\n";
  2703. } #for @readings
  2704. #Ueberfuehren der gesammelten Werte in die globale Variable %data
  2705. for(my $j=0; $j<int(@readings); $j++) {
  2706. my $k = $j+1;
  2707. $data{"min$k"} = $min[$j];
  2708. $data{"max$k"} = $max[$j];
  2709. $data{"avg$k"} = $cnt[$j] ? sprintf("%0.2f", $sum[$j]/$cnt[$j]) : 0;
  2710. $data{"sum$k"} = $sum[$j];
  2711. $data{"cnt$k"} = $cnt[$j];
  2712. $data{"currval$k"} = $lastv[$j];
  2713. $data{"currdate$k"} = $lastd[$j];
  2714. $data{"mindate$k"} = $mind[$j];
  2715. $data{"maxdate$k"} = $maxd[$j];
  2716. }
  2717. #cleanup (plotfork) connection
  2718. $dbh->disconnect() if( $hash->{PID} != $$ );
  2719. if($internal) {
  2720. $internal_data = \$retval;
  2721. return undef;
  2722. } elsif($outf =~ m/(array)/) {
  2723. return @ReturnArray;
  2724. } else {
  2725. $retval = Encode::encode_utf8($retval) if($utf8);
  2726. # Log3 $name, 5, "DbLog $name -> Result of get:\n$retval";
  2727. return $retval;
  2728. }
  2729. }
  2730. ##########################################################################
  2731. #
  2732. # Konfigurationscheck DbLog <-> Datenbank
  2733. #
  2734. ##########################################################################
  2735. sub DbLog_configcheck($) {
  2736. my ($hash)= @_;
  2737. my $name = $hash->{NAME};
  2738. my $dbmodel = $hash->{MODEL};
  2739. my $dbconn = $hash->{dbconn};
  2740. my $dbname = (split(/;|=/, $dbconn))[1];
  2741. my ($check, $rec,%dbconfig);
  2742. ### Start
  2743. #######################################################################
  2744. $check = "<html>";
  2745. $check .= "<u><b>Result of DbLog version check</u></b><br><br>";
  2746. $check .= "Used DbLog version: $hash->{VERSION} <br>";
  2747. $check .= "<b>Recommendation:</b> Your running version may be the current one. Please check for updates of DbLog periodically. <br><br>";
  2748. ### Configuration read check
  2749. #######################################################################
  2750. $check .= "<u><b>Result of configuration read check</u></b><br><br>";
  2751. my $st = configDBUsed()?"configDB (don't forget upload configuration file if changed. Use \"configdb filelist\" and look for your configuration file.)":"file";
  2752. $check .= "Connection parameter store type: $st <br>";
  2753. my ($err, @config) = FileRead($hash->{CONFIGURATION});
  2754. if (!$err) {
  2755. eval join("\n", @config);
  2756. $rec = "parameter: ";
  2757. $rec .= "Connection -> could not read, " if (!defined $dbconfig{connection});
  2758. $rec .= "Connection -> ".$dbconfig{connection}.", " if (defined $dbconfig{connection});
  2759. $rec .= "User -> could not read, " if (!defined $dbconfig{user});
  2760. $rec .= "User -> ".$dbconfig{user}.", " if (defined $dbconfig{user});
  2761. $rec .= "Password -> could not read " if (!defined $dbconfig{password});
  2762. $rec .= "Password -> read o.k. " if (defined $dbconfig{password});
  2763. } else {
  2764. $rec = $err;
  2765. }
  2766. $check .= "Connection $rec <br><br>";
  2767. ### Connection und Encoding check
  2768. #######################################################################
  2769. my (@ce,@se);
  2770. my ($chutf8mod,$chutf8dat);
  2771. if($dbmodel =~ /MYSQL/) {
  2772. @ce = DbLog_sqlget($hash,"SHOW VARIABLES LIKE 'character_set_connection'");
  2773. $chutf8mod = @ce?uc($ce[1]):"no result";
  2774. @se = DbLog_sqlget($hash,"SHOW VARIABLES LIKE 'character_set_database'");
  2775. $chutf8dat = @se?uc($se[1]):"no result";
  2776. if($chutf8mod eq $chutf8dat) {
  2777. $rec = "settings o.k.";
  2778. } else {
  2779. $rec = "Both encodings should be identical. You can adjust the usage of UTF8 connection by setting the UTF8 parameter in file '$hash->{CONFIGURATION}' to the right value. ";
  2780. }
  2781. }
  2782. if($dbmodel =~ /POSTGRESQL/) {
  2783. @ce = DbLog_sqlget($hash,"SHOW CLIENT_ENCODING");
  2784. $chutf8mod = @ce?uc($ce[0]):"no result";
  2785. @se = DbLog_sqlget($hash,"select character_set_name from information_schema.character_sets");
  2786. $chutf8dat = @se?uc($se[0]):"no result";
  2787. if($chutf8mod eq $chutf8dat) {
  2788. $rec = "settings o.k.";
  2789. } else {
  2790. $rec = "This is only an information. PostgreSQL supports automatic character set conversion between server and client for certain character set combinations. The conversion information is stored in the pg_conversion system catalog. PostgreSQL comes with some predefined conversions.";
  2791. }
  2792. }
  2793. if($dbmodel =~ /SQLITE/) {
  2794. @ce = DbLog_sqlget($hash,"PRAGMA encoding");
  2795. $chutf8dat = @ce?uc($ce[0]):"no result";
  2796. @se = DbLog_sqlget($hash,"PRAGMA table_info(history)");
  2797. $rec = "This is only an information about text encoding used by the main database.";
  2798. }
  2799. $check .= "<u><b>Result of connection check</u></b><br><br>";
  2800. if(@ce && @se) {
  2801. $check .= "Connection to database $dbname successfully done. <br>";
  2802. $check .= "<b>Recommendation:</b> settings o.k. <br><br>";
  2803. }
  2804. if(!@ce || !@se) {
  2805. $check .= "Connection to database was not successful. <br>";
  2806. $check .= "<b>Recommendation:</b> Plese check logfile for further information. <br><br>";
  2807. $check .= "</html>";
  2808. return $check;
  2809. }
  2810. $check .= "<u><b>Result of encoding check</u></b><br><br>";
  2811. $check .= "Encoding used by Client (connection): $chutf8mod <br>" if($dbmodel !~ /SQLITE/);
  2812. $check .= "Encoding used by DB $dbname: $chutf8dat <br>";
  2813. $check .= "<b>Recommendation:</b> $rec <br><br>";
  2814. ### Check Betriebsmodus
  2815. #######################################################################
  2816. my $mode = $hash->{MODE};
  2817. my $sfx = AttrVal("global", "language", "EN");
  2818. $sfx = ($sfx eq "EN" ? "" : "_$sfx");
  2819. $check .= "<u><b>Result of logmode check</u></b><br><br>";
  2820. $check .= "Logmode of DbLog-device $name is: $mode <br>";
  2821. if($mode =~ /asynchronous/) {
  2822. my $max = AttrVal("global", "blockingCallMax", 0);
  2823. if(!$max || $max >= 6) {
  2824. $rec = "settings o.k.";
  2825. } else {
  2826. $rec = "WARNING - you are running asynchronous mode that is recommended, but the value of global device attribute \"blockingCallMax\" is set quite small. <br>";
  2827. $rec .= "This may cause problems in operation. It is recommended to <b>increase</b> the <b>global blockingCallMax</b> attribute.";
  2828. }
  2829. } else {
  2830. $rec = "Switch $name to the asynchronous logmode by setting the 'asyncMode' attribute. The advantage of this mode is to log events non-blocking. <br>";
  2831. $rec .= "There are attributes 'syncInterval' and 'cacheLimit' relevant for this working mode. <br>";
  2832. $rec .= "Please refer to commandref for further informations about these attributes.";
  2833. }
  2834. $check .= "<b>Recommendation:</b> $rec <br><br>";
  2835. if($mode =~ /asynchronous/) {
  2836. my $shutdownWait = AttrVal($name,"shutdownWait",undef);
  2837. my $bpt = ReadingsVal($name,"background_processing_time",undef);
  2838. my $bptv = defined($bpt)?int($bpt)+2:2;
  2839. # $shutdownWait = defined($shutdownWait)?$shutdownWait:undef;
  2840. my $sdw = defined($shutdownWait)?$shutdownWait:" ";
  2841. $check .= "<u><b>Result of shutdown sequence preparation check</u></b><br><br>";
  2842. $check .= "Attribute \"shutdownWait\" is set to: $sdw<br>";
  2843. if(!defined($shutdownWait) || $shutdownWait < $bptv) {
  2844. if(!$bpt) {
  2845. $rec = "Due to Reading \"background_processing_time\" is not available (you may set attribute \"showproctime\"), there is only a rough estimate to<br>";
  2846. $rec .= "set attribute \"shutdownWait\" to $bptv seconds.<br>";
  2847. } else {
  2848. $rec = "Please set this attribute at least to $bptv seconds to avoid data loss when system shutdown is initiated.";
  2849. }
  2850. } else {
  2851. if(!$bpt) {
  2852. $rec = "The setting may be ok. But due to the Reading \"background_processing_time\" is not available (you may set attribute \"showproctime\"), the current <br>";
  2853. $rec .= "setting is only a rough estimate.<br>";
  2854. } else {
  2855. $rec = "settings o.k.";
  2856. }
  2857. }
  2858. $check .= "<b>Recommendation:</b> $rec <br><br>";
  2859. }
  2860. ### Check Plot Erstellungsmodus
  2861. #######################################################################
  2862. $check .= "<u><b>Result of plot generation method check</u></b><br><br>";
  2863. my @webdvs = devspec2array("TYPE=FHEMWEB:FILTER=STATE=Initialized");
  2864. my $forks = 1;
  2865. my $wall;
  2866. foreach (@webdvs) {
  2867. my $web = $_;
  2868. $wall .= $web.": plotfork=".AttrVal($web,"plotfork",0)."<br>";
  2869. $forks = 0 if(!AttrVal($web,"plotfork",0));
  2870. }
  2871. if(!$forks) {
  2872. $check .= "WARNING - at least one of your FHEMWEB devices have attribute \"plotfork = 1\" not set. This may cause blocking situations when creating plots. <br>";
  2873. $check .= $wall;
  2874. $rec = "You should set attribute \"plotfork = 1\" in relevant devices";
  2875. } else {
  2876. $check .= $wall;
  2877. $rec = "settings o.k.";
  2878. }
  2879. $check .= "<b>Recommendation:</b> $rec <br><br>";
  2880. ### Check Spaltenbreite history
  2881. #######################################################################
  2882. my (@sr_dev,@sr_typ,@sr_evt,@sr_rdg,@sr_val,@sr_unt);
  2883. my ($cdat_dev,$cdat_typ,$cdat_evt,$cdat_rdg,$cdat_val,$cdat_unt);
  2884. my ($cmod_dev,$cmod_typ,$cmod_evt,$cmod_rdg,$cmod_val,$cmod_unt);
  2885. if($dbmodel =~ /MYSQL/) {
  2886. @sr_dev = DbLog_sqlget($hash,"SHOW FIELDS FROM history where FIELD='DEVICE'");
  2887. @sr_typ = DbLog_sqlget($hash,"SHOW FIELDS FROM history where FIELD='TYPE'");
  2888. @sr_evt = DbLog_sqlget($hash,"SHOW FIELDS FROM history where FIELD='EVENT'");
  2889. @sr_rdg = DbLog_sqlget($hash,"SHOW FIELDS FROM history where FIELD='READING'");
  2890. @sr_val = DbLog_sqlget($hash,"SHOW FIELDS FROM history where FIELD='VALUE'");
  2891. @sr_unt = DbLog_sqlget($hash,"SHOW FIELDS FROM history where FIELD='UNIT'");
  2892. }
  2893. if($dbmodel =~ /POSTGRESQL/) {
  2894. @sr_dev = DbLog_sqlget($hash,"select column_name,character_maximum_length from information_schema.columns where table_name='history' and column_name='device'");
  2895. @sr_typ = DbLog_sqlget($hash,"select column_name,character_maximum_length from information_schema.columns where table_name='history' and column_name='type'");
  2896. @sr_evt = DbLog_sqlget($hash,"select column_name,character_maximum_length from information_schema.columns where table_name='history' and column_name='event'");
  2897. @sr_rdg = DbLog_sqlget($hash,"select column_name,character_maximum_length from information_schema.columns where table_name='history' and column_name='reading'");
  2898. @sr_val = DbLog_sqlget($hash,"select column_name,character_maximum_length from information_schema.columns where table_name='history' and column_name='value'");
  2899. @sr_unt = DbLog_sqlget($hash,"select column_name,character_maximum_length from information_schema.columns where table_name='history' and column_name='unit'");
  2900. }
  2901. if($dbmodel =~ /SQLITE/) {
  2902. my $dev = (DbLog_sqlget($hash,"SELECT sql FROM sqlite_master WHERE name = 'history'"))[0];
  2903. $cdat_dev = $dev?$dev:"no result";
  2904. $cdat_typ = $cdat_evt = $cdat_rdg = $cdat_val = $cdat_unt = $cdat_dev;
  2905. $cdat_dev =~ s/.*DEVICE.varchar\(([\d]*)\).*/$1/e;
  2906. $cdat_typ =~ s/.*TYPE.varchar\(([\d]*)\).*/$1/e;
  2907. $cdat_evt =~ s/.*EVENT.varchar\(([\d]*)\).*/$1/e;
  2908. $cdat_rdg =~ s/.*READING.varchar\(([\d]*)\).*/$1/e;
  2909. $cdat_val =~ s/.*VALUE.varchar\(([\d]*)\).*/$1/e;
  2910. $cdat_unt =~ s/.*UNIT.varchar\(([\d]*)\).*/$1/e;
  2911. }
  2912. if ($dbmodel !~ /SQLITE/) {
  2913. $cdat_dev = @sr_dev?($sr_dev[1]):"no result";
  2914. $cdat_dev =~ tr/varchar\(|\)//d if($cdat_dev ne "no result");
  2915. $cdat_typ = @sr_typ?($sr_typ[1]):"no result";
  2916. $cdat_typ =~ tr/varchar\(|\)//d if($cdat_typ ne "no result");
  2917. $cdat_evt = @sr_evt?($sr_evt[1]):"no result";
  2918. $cdat_evt =~ tr/varchar\(|\)//d if($cdat_evt ne "no result");
  2919. $cdat_rdg = @sr_rdg?($sr_rdg[1]):"no result";
  2920. $cdat_rdg =~ tr/varchar\(|\)//d if($cdat_rdg ne "no result");
  2921. $cdat_val = @sr_val?($sr_val[1]):"no result";
  2922. $cdat_val =~ tr/varchar\(|\)//d if($cdat_val ne "no result");
  2923. $cdat_unt = @sr_unt?($sr_unt[1]):"no result";
  2924. $cdat_unt =~ tr/varchar\(|\)//d if($cdat_unt ne "no result");
  2925. }
  2926. $cmod_dev = $hash->{HELPER}{DEVICECOL};
  2927. $cmod_typ = $hash->{HELPER}{TYPECOL};
  2928. $cmod_evt = $hash->{HELPER}{EVENTCOL};
  2929. $cmod_rdg = $hash->{HELPER}{READINGCOL};
  2930. $cmod_val = $hash->{HELPER}{VALUECOL};
  2931. $cmod_unt = $hash->{HELPER}{UNITCOL};
  2932. if($cdat_dev >= $cmod_dev && $cdat_typ >= $cmod_typ && $cdat_evt >= $cmod_evt && $cdat_rdg >= $cmod_rdg && $cdat_val >= $cmod_val && $cdat_unt >= $cmod_unt) {
  2933. $rec = "settings o.k.";
  2934. } else {
  2935. if ($dbmodel !~ /SQLITE/) {
  2936. $rec = "The relation between column width in table history and the field width used in device $name don't meet the requirements. ";
  2937. $rec .= "Please make sure that the width of database field definition is equal or larger than the field width used by the module. Compare the given results.<br>";
  2938. $rec .= "Currently the default values for field width are: <br><br>";
  2939. $rec .= "DEVICE: $columns{DEVICE} <br>";
  2940. $rec .= "TYPE: $columns{TYPE} <br>";
  2941. $rec .= "EVENT: $columns{EVENT} <br>";
  2942. $rec .= "READING: $columns{READING} <br>";
  2943. $rec .= "VALUE: $columns{VALUE} <br>";
  2944. $rec .= "UNIT: $columns{UNIT} <br><br>";
  2945. $rec .= "You can change the column width in database by a statement like <b>'alter table history modify VALUE varchar(128);</b>' (example for changing field 'VALUE'). ";
  2946. $rec .= "You can do it for example by executing 'sqlCmd' in DbRep or in a SQL-Editor of your choice. (switch $name to asynchron mode for non-blocking). <br>";
  2947. $rec .= "Alternatively the field width used by $name can be adjusted by setting attributes 'colEvent', 'colReading', 'colValue'. (pls. refer to commandref)";
  2948. } else {
  2949. $rec = "WARNING - The relation between column width in table history and the field width used by device $name should be equal but it differs.";
  2950. $rec .= "The field width used by $name can be adjusted by setting attributes 'colEvent', 'colReading', 'colValue'. (pls. refer to commandref)";
  2951. $rec .= "Because you use SQLite this is only a warning. Normally the database can handle these differences. ";
  2952. }
  2953. }
  2954. $check .= "<u><b>Result of table 'history' check</u></b><br><br>";
  2955. $check .= "Column width set in DB $dbname.history: 'DEVICE' = $cdat_dev, 'TYPE' = $cdat_typ, 'EVENT' = $cdat_evt, 'READING' = $cdat_rdg, 'VALUE' = $cdat_val, 'UNIT' = $cdat_unt <br>";
  2956. $check .= "Column width used by $name: 'DEVICE' = $cmod_dev, 'TYPE' = $cmod_typ, 'EVENT' = $cmod_evt, 'READING' = $cmod_rdg, 'VALUE' = $cmod_val, 'UNIT' = $cmod_unt <br>";
  2957. $check .= "<b>Recommendation:</b> $rec <br><br>";
  2958. ### Check Spaltenbreite current
  2959. #######################################################################
  2960. if($dbmodel =~ /MYSQL/) {
  2961. @sr_dev = DbLog_sqlget($hash,"SHOW FIELDS FROM current where FIELD='DEVICE'");
  2962. @sr_typ = DbLog_sqlget($hash,"SHOW FIELDS FROM current where FIELD='TYPE'");
  2963. @sr_evt = DbLog_sqlget($hash,"SHOW FIELDS FROM current where FIELD='EVENT'");
  2964. @sr_rdg = DbLog_sqlget($hash,"SHOW FIELDS FROM current where FIELD='READING'");
  2965. @sr_val = DbLog_sqlget($hash,"SHOW FIELDS FROM current where FIELD='VALUE'");
  2966. @sr_unt = DbLog_sqlget($hash,"SHOW FIELDS FROM current where FIELD='UNIT'");
  2967. }
  2968. if($dbmodel =~ /POSTGRESQL/) {
  2969. @sr_dev = DbLog_sqlget($hash,"select column_name,character_maximum_length from information_schema.columns where table_name='current' and column_name='device'");
  2970. @sr_typ = DbLog_sqlget($hash,"select column_name,character_maximum_length from information_schema.columns where table_name='current' and column_name='type'");
  2971. @sr_evt = DbLog_sqlget($hash,"select column_name,character_maximum_length from information_schema.columns where table_name='current' and column_name='event'");
  2972. @sr_rdg = DbLog_sqlget($hash,"select column_name,character_maximum_length from information_schema.columns where table_name='current' and column_name='reading'");
  2973. @sr_val = DbLog_sqlget($hash,"select column_name,character_maximum_length from information_schema.columns where table_name='current' and column_name='value'");
  2974. @sr_unt = DbLog_sqlget($hash,"select column_name,character_maximum_length from information_schema.columns where table_name='current' and column_name='unit'");
  2975. }
  2976. if($dbmodel =~ /SQLITE/) {
  2977. my $dev = (DbLog_sqlget($hash,"SELECT sql FROM sqlite_master WHERE name = 'current'"))[0];
  2978. $cdat_dev = $dev?$dev:"no result";
  2979. $cdat_typ = $cdat_evt = $cdat_rdg = $cdat_val = $cdat_unt = $cdat_dev;
  2980. $cdat_dev =~ s/.*DEVICE.varchar\(([\d]*)\).*/$1/e;
  2981. $cdat_typ =~ s/.*TYPE.varchar\(([\d]*)\).*/$1/e;
  2982. $cdat_evt =~ s/.*EVENT.varchar\(([\d]*)\).*/$1/e;
  2983. $cdat_rdg =~ s/.*READING.varchar\(([\d]*)\).*/$1/e;
  2984. $cdat_val =~ s/.*VALUE.varchar\(([\d]*)\).*/$1/e;
  2985. $cdat_unt =~ s/.*UNIT.varchar\(([\d]*)\).*/$1/e;
  2986. }
  2987. if ($dbmodel !~ /SQLITE/) {
  2988. $cdat_dev = @sr_dev?($sr_dev[1]):"no result";
  2989. $cdat_dev =~ tr/varchar\(|\)//d if($cdat_dev ne "no result");
  2990. $cdat_typ = @sr_typ?($sr_typ[1]):"no result";
  2991. $cdat_typ =~ tr/varchar\(|\)//d if($cdat_typ ne "no result");
  2992. $cdat_evt = @sr_evt?($sr_evt[1]):"no result";
  2993. $cdat_evt =~ tr/varchar\(|\)//d if($cdat_evt ne "no result");
  2994. $cdat_rdg = @sr_rdg?($sr_rdg[1]):"no result";
  2995. $cdat_rdg =~ tr/varchar\(|\)//d if($cdat_rdg ne "no result");
  2996. $cdat_val = @sr_val?($sr_val[1]):"no result";
  2997. $cdat_val =~ tr/varchar\(|\)//d if($cdat_val ne "no result");
  2998. $cdat_unt = @sr_unt?($sr_unt[1]):"no result";
  2999. $cdat_unt =~ tr/varchar\(|\)//d if($cdat_unt ne "no result");
  3000. }
  3001. $cmod_dev = $hash->{HELPER}{DEVICECOL};
  3002. $cmod_typ = $hash->{HELPER}{TYPECOL};
  3003. $cmod_evt = $hash->{HELPER}{EVENTCOL};
  3004. $cmod_rdg = $hash->{HELPER}{READINGCOL};
  3005. $cmod_val = $hash->{HELPER}{VALUECOL};
  3006. $cmod_unt = $hash->{HELPER}{UNITCOL};
  3007. if($cdat_dev >= $cmod_dev && $cdat_typ >= $cmod_typ && $cdat_evt >= $cmod_evt && $cdat_rdg >= $cmod_rdg && $cdat_val >= $cmod_val && $cdat_unt >= $cmod_unt) {
  3008. $rec = "settings o.k.";
  3009. } else {
  3010. if ($dbmodel !~ /SQLITE/) {
  3011. $rec = "The relation between column width in table current and the field width used in device $name don't meet the requirements. ";
  3012. $rec .= "Please make sure that the width of database field definition is equal or larger than the field width used by the module. Compare the given results.<br>";
  3013. $rec .= "Currently the default values for field width are: <br><br>";
  3014. $rec .= "DEVICE: $columns{DEVICE} <br>";
  3015. $rec .= "TYPE: $columns{TYPE} <br>";
  3016. $rec .= "EVENT: $columns{EVENT} <br>";
  3017. $rec .= "READING: $columns{READING} <br>";
  3018. $rec .= "VALUE: $columns{VALUE} <br>";
  3019. $rec .= "UNIT: $columns{UNIT} <br><br>";
  3020. $rec .= "You can change the column width in database by a statement like <b>'alter table current modify VALUE varchar(128);</b>' (example for changing field 'VALUE'). ";
  3021. $rec .= "You can do it for example by executing 'sqlCmd' in DbRep or in a SQL-Editor of your choice. (switch $name to asynchron mode for non-blocking). <br>";
  3022. $rec .= "Alternatively the field width used by $name can be adjusted by setting attributes 'colEvent', 'colReading', 'colValue'. (pls. refer to commandref)";
  3023. } else {
  3024. $rec = "WARNING - The relation between column width in table current and the field width used by device $name should be equal but it differs. ";
  3025. $rec .= "The field width used by $name can be adjusted by setting attributes 'colEvent', 'colReading', 'colValue'. (pls. refer to commandref)";
  3026. $rec .= "Because you use SQLite this is only a warning. Normally the database can handle these differences. ";
  3027. }
  3028. }
  3029. $check .= "<u><b>Result of table 'current' check</u></b><br><br>";
  3030. $check .= "Column width set in DB $dbname.current: 'DEVICE' = $cdat_dev, 'TYPE' = $cdat_typ, 'EVENT' = $cdat_evt, 'READING' = $cdat_rdg, 'VALUE' = $cdat_val, 'UNIT' = $cdat_unt <br>";
  3031. $check .= "Column width used by $name: 'DEVICE' = $cmod_dev, 'TYPE' = $cmod_typ, 'EVENT' = $cmod_evt, 'READING' = $cmod_rdg, 'VALUE' = $cmod_val, 'UNIT' = $cmod_unt <br>";
  3032. $check .= "<b>Recommendation:</b> $rec <br><br>";
  3033. #}
  3034. ### Check Vorhandensein Search_Idx mit den empfohlenen Spalten
  3035. #######################################################################
  3036. my (@six,@six_dev,@six_rdg,@six_tsp);
  3037. my ($idef,$idef_dev,$idef_rdg,$idef_tsp);
  3038. $check .= "<u><b>Result of check 'Search_Idx' availability</u></b><br><br>";
  3039. if($dbmodel =~ /MYSQL/) {
  3040. @six = DbLog_sqlget($hash,"SHOW INDEX FROM history where Key_name='Search_Idx'");
  3041. if (!@six) {
  3042. $check .= "The index 'Search_Idx' is missing. <br>";
  3043. $rec = "You can create the index by executing statement <b>'CREATE INDEX Search_Idx ON `history` (DEVICE, READING, TIMESTAMP) USING BTREE;'</b> <br>";
  3044. $rec .= "Depending on your database size this command may running a long time. <br>";
  3045. $rec .= "Please make sure the device '$name' is operating in asynchronous mode to avoid FHEM from blocking when creating the index. <br>";
  3046. $rec .= "<b>Note:</b> If you have just created another index which covers the same fields and order as suggested (e.g. a primary key) you don't need to create the 'Search_Idx' as well ! <br>";
  3047. } else {
  3048. @six_dev = DbLog_sqlget($hash,"SHOW INDEX FROM history where Key_name='Search_Idx' and Column_name='DEVICE'");
  3049. @six_rdg = DbLog_sqlget($hash,"SHOW INDEX FROM history where Key_name='Search_Idx' and Column_name='READING'");
  3050. @six_tsp = DbLog_sqlget($hash,"SHOW INDEX FROM history where Key_name='Search_Idx' and Column_name='TIMESTAMP'");
  3051. if (@six_dev && @six_rdg && @six_tsp) {
  3052. $check .= "Index 'Search_Idx' exists and contains recommended fields 'DEVICE', 'READING', 'TIMESTAMP'. <br>";
  3053. $rec = "settings o.k.";
  3054. } else {
  3055. $check .= "Index 'Search_Idx' exists but doesn't contain recommended field 'DEVICE'. <br>" if (!@six_dev);
  3056. $check .= "Index 'Search_Idx' exists but doesn't contain recommended field 'READING'. <br>" if (!@six_rdg);
  3057. $check .= "Index 'Search_Idx' exists but doesn't contain recommended field 'TIMESTAMP'. <br>" if (!@six_tsp);
  3058. $rec = "The index should contain the fields 'DEVICE', 'READING', 'TIMESTAMP'. ";
  3059. $rec .= "You can change the index by executing e.g. <br>";
  3060. $rec .= "<b>'ALTER TABLE `history` DROP INDEX `Search_Idx`, ADD INDEX `Search_Idx` (`DEVICE`, `READING`, `TIMESTAMP`) USING BTREE;'</b> <br>";
  3061. $rec .= "Depending on your database size this command may running a long time. <br>";
  3062. }
  3063. }
  3064. }
  3065. if($dbmodel =~ /POSTGRESQL/) {
  3066. @six = DbLog_sqlget($hash,"SELECT * FROM pg_indexes WHERE tablename='history' and indexname ='Search_Idx'");
  3067. if (!@six) {
  3068. $check .= "The index 'Search_Idx' is missing. <br>";
  3069. $rec = "You can create the index by executing statement <b>'CREATE INDEX \"Search_Idx\" ON history USING btree (device, reading, \"timestamp\")'</b> <br>";
  3070. $rec .= "Depending on your database size this command may running a long time. <br>";
  3071. $rec .= "Please make sure the device '$name' is operating in asynchronous mode to avoid FHEM from blocking when creating the index. <br>";
  3072. $rec .= "<b>Note:</b> If you have just created another index which covers the same fields and order as suggested (e.g. a primary key) you don't need to create the 'Search_Idx' as well ! <br>";
  3073. } else {
  3074. $idef = $six[4];
  3075. $idef_dev = 1 if($idef =~ /device/);
  3076. $idef_rdg = 1 if($idef =~ /reading/);
  3077. $idef_tsp = 1 if($idef =~ /timestamp/);
  3078. if ($idef_dev && $idef_rdg && $idef_tsp) {
  3079. $check .= "Index 'Search_Idx' exists and contains recommended fields 'DEVICE', 'READING', 'TIMESTAMP'. <br>";
  3080. $rec = "settings o.k.";
  3081. } else {
  3082. $check .= "Index 'Search_Idx' exists but doesn't contain recommended field 'DEVICE'. <br>" if (!$idef_dev);
  3083. $check .= "Index 'Search_Idx' exists but doesn't contain recommended field 'READING'. <br>" if (!$idef_rdg);
  3084. $check .= "Index 'Search_Idx' exists but doesn't contain recommended field 'TIMESTAMP'. <br>" if (!$idef_tsp);
  3085. $rec = "The index should contain the fields 'DEVICE', 'READING', 'TIMESTAMP'. ";
  3086. $rec .= "You can change the index by executing e.g. <br>";
  3087. $rec .= "<b>'DROP INDEX \"Search_Idx\"; CREATE INDEX \"Search_Idx\" ON history USING btree (device, reading, \"timestamp\")'</b> <br>";
  3088. $rec .= "Depending on your database size this command may running a long time. <br>";
  3089. }
  3090. }
  3091. }
  3092. if($dbmodel =~ /SQLITE/) {
  3093. @six = DbLog_sqlget($hash,"SELECT name,sql FROM sqlite_master WHERE type='index' AND name='Search_Idx'");
  3094. if (!$six[0]) {
  3095. $check .= "The index 'Search_Idx' is missing. <br>";
  3096. $rec = "You can create the index by executing statement <b>'CREATE INDEX Search_Idx ON `history` (DEVICE, READING, TIMESTAMP)'</b> <br>";
  3097. $rec .= "Depending on your database size this command may running a long time. <br>";
  3098. $rec .= "Please make sure the device '$name' is operating in asynchronous mode to avoid FHEM from blocking when creating the index. <br>";
  3099. $rec .= "<b>Note:</b> If you have just created another index which covers the same fields and order as suggested (e.g. a primary key) you don't need to create the 'Search_Idx' as well ! <br>";
  3100. } else {
  3101. $idef = $six[1];
  3102. $idef_dev = 1 if(lc($idef) =~ /device/);
  3103. $idef_rdg = 1 if(lc($idef) =~ /reading/);
  3104. $idef_tsp = 1 if(lc($idef) =~ /timestamp/);
  3105. if ($idef_dev && $idef_rdg && $idef_tsp) {
  3106. $check .= "Index 'Search_Idx' exists and contains recommended fields 'DEVICE', 'READING', 'TIMESTAMP'. <br>";
  3107. $rec = "settings o.k.";
  3108. } else {
  3109. $check .= "Index 'Search_Idx' exists but doesn't contain recommended field 'DEVICE'. <br>" if (!$idef_dev);
  3110. $check .= "Index 'Search_Idx' exists but doesn't contain recommended field 'READING'. <br>" if (!$idef_rdg);
  3111. $check .= "Index 'Search_Idx' exists but doesn't contain recommended field 'TIMESTAMP'. <br>" if (!$idef_tsp);
  3112. $rec = "The index should contain the fields 'DEVICE', 'READING', 'TIMESTAMP'. ";
  3113. $rec .= "You can change the index by executing e.g. <br>";
  3114. $rec .= "<b>'DROP INDEX \"Search_Idx\"; CREATE INDEX Search_Idx ON `history` (DEVICE, READING, TIMESTAMP)'</b> <br>";
  3115. $rec .= "Depending on your database size this command may running a long time. <br>";
  3116. }
  3117. }
  3118. }
  3119. $check .= "<b>Recommendation:</b> $rec <br><br>";
  3120. ### Check Index Report_Idx für DbRep-Device falls DbRep verwendet wird
  3121. #######################################################################
  3122. my ($dbrp,$irep,);
  3123. my (@dix,@dix_rdg,@dix_tsp,$irep_rdg,$irep_tsp);
  3124. my $isused = 0;
  3125. my @repdvs = devspec2array("TYPE=DbRep");
  3126. $check .= "<u><b>Result of check 'Report_Idx' availability for DbRep-devices</u></b><br><br>";
  3127. foreach (@repdvs) {
  3128. $dbrp = $_;
  3129. if(!$defs{$dbrp}) {
  3130. Log3 ($name, 2, "DbLog $name -> Device '$dbrp' found by configCheck doesn't exist !");
  3131. next;
  3132. }
  3133. if ($defs{$dbrp}->{DEF} eq $name) {
  3134. # DbRep Device verwendet aktuelles DbLog-Device
  3135. Log3 ($name, 5, "DbLog $name -> DbRep-Device '$dbrp' uses $name.");
  3136. $isused = 1;
  3137. }
  3138. }
  3139. if ($isused) {
  3140. if($dbmodel =~ /MYSQL/) {
  3141. @dix = DbLog_sqlget($hash,"SHOW INDEX FROM history where Key_name='Report_Idx'");
  3142. if (!@dix) {
  3143. $check .= "At least one DbRep-device assigned to $name is used, but the recommended index 'Report_Idx' is missing. <br>";
  3144. $rec = "You can create the index by executing statement <b>'CREATE INDEX Report_Idx ON `history` (TIMESTAMP, READING) USING BTREE;'</b> <br>";
  3145. $rec .= "Depending on your database size this command may running a long time. <br>";
  3146. $rec .= "Please make sure the device '$name' is operating in asynchronous mode to avoid FHEM from blocking when creating the index. <br>";
  3147. $rec .= "<b>Note:</b> If you have just created another index which covers the same fields and order as suggested (e.g. a primary key) you don't need to create the 'Report_Idx' as well ! <br>";
  3148. } else {
  3149. @dix_rdg = DbLog_sqlget($hash,"SHOW INDEX FROM history where Key_name='Report_Idx' and Column_name='READING'");
  3150. @dix_tsp = DbLog_sqlget($hash,"SHOW INDEX FROM history where Key_name='Report_Idx' and Column_name='TIMESTAMP'");
  3151. if (@dix_rdg && @dix_tsp) {
  3152. $check .= "At least one DbRep-device assigned to $name is used. ";
  3153. $check .= "Index 'Report_Idx' exists and contains recommended fields 'TIMESTAMP', 'READING'. <br>";
  3154. $rec = "settings o.k.";
  3155. } else {
  3156. $check .= "You use at least one DbRep-device assigned to $name. ";
  3157. $check .= "Index 'Report_Idx' exists but doesn't contain recommended field 'READING'. <br>" if (!@dix_rdg);
  3158. $check .= "Index 'Report_Idx' exists but doesn't contain recommended field 'TIMESTAMP'. <br>" if (!@dix_tsp);
  3159. $rec = "The index should contain the fields 'TIMESTAMP', 'READING'. ";
  3160. $rec .= "You can change the index by executing e.g. <br>";
  3161. $rec .= "<b>'ALTER TABLE `history` DROP INDEX `Report_Idx`, ADD INDEX `Report_Idx` (`TIMESTAMP`, `READING`) USING BTREE'</b> <br>";
  3162. $rec .= "Depending on your database size this command may running a long time. <br>";
  3163. }
  3164. }
  3165. }
  3166. if($dbmodel =~ /POSTGRESQL/) {
  3167. @dix = DbLog_sqlget($hash,"SELECT * FROM pg_indexes WHERE tablename='history' and indexname ='Report_Idx'");
  3168. if (!@dix) {
  3169. $check .= "You use at least one DbRep-device assigned to $name, but the recommended index 'Report_Idx' is missing. <br>";
  3170. $rec = "You can create the index by executing statement <b>'CREATE INDEX \"Report_Idx\" ON history USING btree (\"timestamp\", reading)'</b> <br>";
  3171. $rec .= "Depending on your database size this command may running a long time. <br>";
  3172. $rec .= "Please make sure the device '$name' is operating in asynchronous mode to avoid FHEM from blocking when creating the index. <br>";
  3173. $rec .= "<b>Note:</b> If you have just created another index which covers the same fields and order as suggested (e.g. a primary key) you don't need to create the 'Report_Idx' as well ! <br>";
  3174. } else {
  3175. $irep = $dix[4];
  3176. $irep_rdg = 1 if($irep =~ /reading/);
  3177. $irep_tsp = 1 if($irep =~ /timestamp/);
  3178. if ($irep_rdg && $irep_tsp) {
  3179. $check .= "Index 'Report_Idx' exists and contains recommended fields 'TIMESTAMP', 'READING'. <br>";
  3180. $rec = "settings o.k.";
  3181. } else {
  3182. $check .= "Index 'Report_Idx' exists but doesn't contain recommended field 'READING'. <br>" if (!$irep_rdg);
  3183. $check .= "Index 'Report_Idx' exists but doesn't contain recommended field 'TIMESTAMP'. <br>" if (!$irep_tsp);
  3184. $rec = "The index should contain the fields 'TIMESTAMP', 'READING'. ";
  3185. $rec .= "You can change the index by executing e.g. <br>";
  3186. $rec .= "<b>'DROP INDEX \"Report_Idx\"; CREATE INDEX \"Report_Idx\" ON history USING btree (\"timestamp\", reading)'</b> <br>";
  3187. $rec .= "Depending on your database size this command may running a long time. <br>";
  3188. }
  3189. }
  3190. }
  3191. if($dbmodel =~ /SQLITE/) {
  3192. @dix = DbLog_sqlget($hash,"SELECT name,sql FROM sqlite_master WHERE type='index' AND name='Report_Idx'");
  3193. if (!$dix[0]) {
  3194. $check .= "The index 'Report_Idx' is missing. <br>";
  3195. $rec = "You can create the index by executing statement <b>'CREATE INDEX Report_Idx ON `history` (TIMESTAMP, READING)'</b> <br>";
  3196. $rec .= "Depending on your database size this command may running a long time. <br>";
  3197. $rec .= "Please make sure the device '$name' is operating in asynchronous mode to avoid FHEM from blocking when creating the index. <br>";
  3198. $rec .= "<b>Note:</b> If you have just created another index which covers the same fields and order as suggested (e.g. a primary key) you don't need to create the 'Search_Idx' as well ! <br>";
  3199. } else {
  3200. $irep = $dix[1];
  3201. $irep_rdg = 1 if(lc($irep) =~ /reading/);
  3202. $irep_tsp = 1 if(lc($irep) =~ /timestamp/);
  3203. if ($irep_rdg && $irep_tsp) {
  3204. $check .= "Index 'Report_Idx' exists and contains recommended fields 'TIMESTAMP', 'READING'. <br>";
  3205. $rec = "settings o.k.";
  3206. } else {
  3207. $check .= "Index 'Report_Idx' exists but doesn't contain recommended field 'READING'. <br>" if (!$irep_rdg);
  3208. $check .= "Index 'Report_Idx' exists but doesn't contain recommended field 'TIMESTAMP'. <br>" if (!$irep_tsp);
  3209. $rec = "The index should contain the fields 'TIMESTAMP', 'READING'. ";
  3210. $rec .= "You can change the index by executing e.g. <br>";
  3211. $rec .= "<b>'DROP INDEX \"Report_Idx\"; CREATE INDEX Report_Idx ON `history` (TIMESTAMP, READING)'</b> <br>";
  3212. $rec .= "Depending on your database size this command may running a long time. <br>";
  3213. }
  3214. }
  3215. }
  3216. } else {
  3217. $check .= "No DbRep-device assigned to $name is used. Hence an index for DbRep isn't needed. <br>";
  3218. $rec = "settings o.k.";
  3219. }
  3220. $check .= "<b>Recommendation:</b> $rec <br><br>";
  3221. $check .= "</html>";
  3222. return $check;
  3223. }
  3224. sub DbLog_sqlget($$) {
  3225. my ($hash,$sql)= @_;
  3226. my $name = $hash->{NAME};
  3227. my ($dbh,$sth,@sr);
  3228. Log3 ($name, 4, "DbLog $name - Executing SQL: $sql");
  3229. $dbh = DbLog_ConnectNewDBH($hash);
  3230. return if(!$dbh);
  3231. eval { $sth = $dbh->prepare("$sql");
  3232. $sth->execute;
  3233. };
  3234. if($@) {
  3235. $dbh->disconnect if($dbh);
  3236. Log3 ($name, 2, "DbLog $name - $@");
  3237. return @sr;
  3238. }
  3239. @sr = $sth->fetchrow;
  3240. $sth->finish;
  3241. $dbh->disconnect;
  3242. no warnings 'uninitialized';
  3243. Log3 ($name, 4, "DbLog $name - SQL result: @sr");
  3244. use warnings;
  3245. return @sr;
  3246. }
  3247. #########################################################################################
  3248. #
  3249. # Addlog - einfügen des Readingwertes eines gegebenen Devices
  3250. #
  3251. #########################################################################################
  3252. sub DbLog_AddLog($$$$$) {
  3253. my ($hash,$devrdspec,$value,$nce,$cn)= @_;
  3254. my $name = $hash->{NAME};
  3255. my $async = AttrVal($name, "asyncMode", undef);
  3256. my $value_fn = AttrVal( $name, "valueFn", "" );
  3257. my $ce = AttrVal($name, "cacheEvents", 0);
  3258. my ($dev_type,$dev_name,$dev_reading,$read_val,$event,$ut);
  3259. my @row_array;
  3260. my $ts;
  3261. return if(IsDisabled($name) || !$hash->{HELPER}{COLSET} || $init_done != 1);
  3262. # Funktion aus Attr valueFn validieren
  3263. if( $value_fn =~ m/^\s*(\{.*\})\s*$/s ) {
  3264. $value_fn = $1;
  3265. } else {
  3266. $value_fn = '';
  3267. }
  3268. my $now = gettimeofday();
  3269. my $rdspec = (split ":",$devrdspec)[-1];
  3270. my @dc = split(":",$devrdspec);
  3271. pop @dc;
  3272. my $devspec = join(':',@dc);
  3273. my @exdvs = devspec2array($devspec);
  3274. Log3 $name, 4, "DbLog $name -> Addlog known devices by devspec: @exdvs";
  3275. foreach (@exdvs) {
  3276. $dev_name = $_;
  3277. if(!$defs{$dev_name}) {
  3278. Log3 $name, 2, "DbLog $name -> Device '$dev_name' used by addLog doesn't exist !";
  3279. next;
  3280. }
  3281. my $r = $defs{$dev_name}{READINGS};
  3282. my $DbLogExclude = AttrVal($dev_name, "DbLogExclude", undef);
  3283. my @exrds;
  3284. my $found = 0;
  3285. foreach my $rd (sort keys %{$r}) {
  3286. # jedes Reading des Devices auswerten
  3287. my $do = 1;
  3288. $found = 1 if($rd =~ m/^$rdspec$/); # Reading gefunden
  3289. if($DbLogExclude && !$nce) {
  3290. my @v1 = split(/,/, $DbLogExclude);
  3291. for (my $i=0; $i<int(@v1); $i++) {
  3292. my @v2 = split(/:/, $v1[$i]); # MinInterval wegschneiden, Bsp: "(temperature|humidity):600,battery:3600"
  3293. if($rd =~ m,^$v2[0]$,) {
  3294. # Reading matcht und soll vom addLog ausgeschlossen werden
  3295. Log3 $name, 2, "DbLog $name -> Device: \"$dev_name\", reading: \"$v2[0]\" excluded by attribute DbLogExclude from addLog !" if($rd =~ m/^$rdspec$/);
  3296. $do = 0;
  3297. }
  3298. }
  3299. }
  3300. next if(!$do);
  3301. push @exrds,$rd if($rd =~ m/^$rdspec$/);
  3302. }
  3303. Log3 $name, 4, "DbLog $name -> Readings extracted from Regex: @exrds";
  3304. if(!$found) {
  3305. if(goodReadingName($rdspec) && defined($value)) {
  3306. Log3 $name, 3, "DbLog $name -> Reading '$rdspec' of device '$dev_name' not found - add it as new reading.";
  3307. push @exrds,$rdspec;
  3308. } elsif (goodReadingName($rdspec) && !defined($value)) {
  3309. Log3 $name, 2, "DbLog $name -> WARNING - new Reading '$rdspec' has no value - can't add it !";
  3310. } else {
  3311. Log3 $name, 2, "DbLog $name -> WARNING - Readingname '$rdspec' is no valid or regexp - can't add regexp as new reading !";
  3312. }
  3313. }
  3314. no warnings 'uninitialized';
  3315. foreach (@exrds) {
  3316. $dev_reading = $_;
  3317. $read_val = $value ne ""?$value:ReadingsVal($dev_name,$dev_reading,"");
  3318. $dev_type = uc($defs{$dev_name}{TYPE});
  3319. # dummy-Event zusammenstellen
  3320. $event = $dev_reading.": ".$read_val;
  3321. # den zusammengestellten Event parsen lassen (evtl. Unit zuweisen)
  3322. my @r = DbLog_ParseEvent($dev_name, $dev_type, $event);
  3323. $dev_reading = $r[0];
  3324. $read_val = $r[1];
  3325. $ut = $r[2];
  3326. if(!defined $dev_reading) {$dev_reading = "";}
  3327. if(!defined $read_val) {$read_val = "";}
  3328. if(!defined $ut || $ut eq "") {$ut = AttrVal("$dev_name", "unit", "");}
  3329. $event = "addLog";
  3330. $defs{$dev_name}{Helper}{DBLOG}{$dev_reading}{$hash->{NAME}}{TIME} = $now;
  3331. $defs{$dev_name}{Helper}{DBLOG}{$dev_reading}{$hash->{NAME}}{VALUE} = $read_val;
  3332. $ts = TimeNow();
  3333. # Anwender spezifische Funktion anwenden
  3334. if($value_fn ne '') {
  3335. my $TIMESTAMP = $ts;
  3336. my $DEVICE = $dev_name;
  3337. my $DEVICETYPE = $dev_type;
  3338. my $EVENT = $event;
  3339. my $READING = $dev_reading;
  3340. my $VALUE = $read_val;
  3341. my $UNIT = $ut;
  3342. my $IGNORE = 0;
  3343. my $CN = $cn?$cn:"";
  3344. eval $value_fn;
  3345. Log3 $name, 2, "DbLog $name -> error valueFn: ".$@ if($@);
  3346. next if($IGNORE); # aktueller Event wird nicht geloggt wenn $IGNORE=1 gesetzt in $value_fn
  3347. $ts = $TIMESTAMP if($TIMESTAMP =~ /^(\d{4})-(\d{2})-(\d{2} \d{2}):(\d{2}):(\d{2})$/);
  3348. $dev_name = $DEVICE if($DEVICE ne '');
  3349. $dev_type = $DEVICETYPE if($DEVICETYPE ne '');
  3350. $dev_reading = $READING if($READING ne '');
  3351. $read_val = $VALUE if(defined $VALUE);
  3352. $ut = $UNIT if(defined $UNIT);
  3353. }
  3354. # Daten auf maximale Länge beschneiden
  3355. ($dev_name,$dev_type,$event,$dev_reading,$read_val,$ut) = DbLog_cutCol($hash,$dev_name,$dev_type,$event,$dev_reading,$read_val,$ut);
  3356. if(AttrVal($name, "useCharfilter",0)) {
  3357. $dev_reading = DbLog_charfilter($dev_reading);
  3358. $read_val = DbLog_charfilter($read_val);
  3359. }
  3360. my $row = ($ts."|".$dev_name."|".$dev_type."|".$event."|".$dev_reading."|".$read_val."|".$ut);
  3361. Log3 $hash->{NAME}, 3, "DbLog $name -> addLog created - TS: $ts, Device: $dev_name, Type: $dev_type, Event: $event, Reading: $dev_reading, Value: $read_val, Unit: $ut"
  3362. if(!AttrVal($name, "suppressAddLogV3",0));
  3363. if($async) {
  3364. # asynchoner non-blocking Mode
  3365. # Cache & CacheIndex für Events zum asynchronen Schreiben in DB
  3366. $hash->{cache}{index}++;
  3367. my $index = $hash->{cache}{index};
  3368. $hash->{cache}{".memcache"}{$index} = $row;
  3369. my $memcount = $hash->{cache}{".memcache"}?scalar(keys%{$hash->{cache}{".memcache"}}):0;
  3370. if($ce == 1) {
  3371. readingsSingleUpdate($hash, "CacheUsage", $memcount, 1);
  3372. } else {
  3373. readingsSingleUpdate($hash, 'CacheUsage', $memcount, 0);
  3374. }
  3375. } else {
  3376. # synchoner Mode
  3377. push(@row_array, $row);
  3378. }
  3379. }
  3380. use warnings;
  3381. }
  3382. if(!$async) {
  3383. if(@row_array) {
  3384. # synchoner Mode
  3385. # return wenn "reopen" mit Ablaufzeit gestartet ist
  3386. return if($hash->{HELPER}{REOPEN_RUNS});
  3387. my $error = DbLog_Push($hash, 1, @row_array);
  3388. my $state = $error?$error:(IsDisabled($name))?"disabled":"connected";
  3389. my $evt = ($state eq $hash->{HELPER}{OLDSTATE})?0:1;
  3390. readingsSingleUpdate($hash, "state", $state, $evt);
  3391. $hash->{HELPER}{OLDSTATE} = $state;
  3392. Log3 $name, 5, "DbLog $name -> DbLog_Push Returncode: $error";
  3393. }
  3394. }
  3395. return;
  3396. }
  3397. #########################################################################################
  3398. #
  3399. # Subroutine addCacheLine - einen Datensatz zum Cache hinzufügen
  3400. #
  3401. #########################################################################################
  3402. sub DbLog_addCacheLine($$$$$$$$) {
  3403. my ($hash,$i_timestamp,$i_dev,$i_type,$i_evt,$i_reading,$i_val,$i_unit) = @_;
  3404. my $name = $hash->{NAME};
  3405. my $ce = AttrVal($name, "cacheEvents", 0);
  3406. my $value_fn = AttrVal( $name, "valueFn", "" );
  3407. # Funktion aus Attr valueFn validieren
  3408. if( $value_fn =~ m/^\s*(\{.*\})\s*$/s ) {
  3409. $value_fn = $1;
  3410. } else {
  3411. $value_fn = '';
  3412. }
  3413. if($value_fn ne '') {
  3414. my $TIMESTAMP = $i_timestamp;
  3415. my $DEVICE = $i_dev;
  3416. my $DEVICETYPE = $i_type;
  3417. my $EVENT = $i_evt;
  3418. my $READING = $i_reading;
  3419. my $VALUE = $i_val;
  3420. my $UNIT = $i_unit;
  3421. my $IGNORE = 0;
  3422. eval $value_fn;
  3423. Log3 $name, 2, "DbLog $name -> error valueFn: ".$@ if($@);
  3424. if($IGNORE) {
  3425. # aktueller Event wird nicht geloggt wenn $IGNORE=1 gesetzt in $value_fn
  3426. Log3 $hash->{NAME}, 4, "DbLog $name -> Event ignored by valueFn - TS: $i_timestamp, Device: $i_dev, Type: $i_type, Event: $i_evt, Reading: $i_reading, Value: $i_val, Unit: $i_unit";
  3427. next;
  3428. }
  3429. $i_timestamp = $TIMESTAMP if($TIMESTAMP =~ /(19[0-9][0-9]|2[0-9][0-9][0-9])-(0[1-9]|1[1-2])-(0[1-9]|1[0-9]|2[0-9]|3[0-1]) (0[0-9]|1[1-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])/);
  3430. $i_dev = $DEVICE if($DEVICE ne '');
  3431. $i_type = $DEVICETYPE if($DEVICETYPE ne '');
  3432. $i_reading = $READING if($READING ne '');
  3433. $i_val = $VALUE if(defined $VALUE);
  3434. $i_unit = $UNIT if(defined $UNIT);
  3435. }
  3436. no warnings 'uninitialized';
  3437. # Daten auf maximale Länge beschneiden
  3438. ($i_dev,$i_type,$i_evt,$i_reading,$i_val,$i_unit) = DbLog_cutCol($hash,$i_dev,$i_type,$i_evt,$i_reading,$i_val,$i_unit);
  3439. my $row = ($i_timestamp."|".$i_dev."|".$i_type."|".$i_evt."|".$i_reading."|".$i_val."|".$i_unit);
  3440. $row = DbLog_charfilter($row) if(AttrVal($name, "useCharfilter",0));
  3441. Log3 $hash->{NAME}, 3, "DbLog $name -> added by addCacheLine - TS: $i_timestamp, Device: $i_dev, Type: $i_type, Event: $i_evt, Reading: $i_reading, Value: $i_val, Unit: $i_unit";
  3442. use warnings;
  3443. eval { # one transaction
  3444. $hash->{cache}{index}++;
  3445. my $index = $hash->{cache}{index};
  3446. $hash->{cache}{".memcache"}{$index} = $row;
  3447. my $memcount = $hash->{cache}{".memcache"}?scalar(keys%{$hash->{cache}{".memcache"}}):0;
  3448. if($ce == 1) {
  3449. readingsSingleUpdate($hash, "CacheUsage", $memcount, 1);
  3450. } else {
  3451. readingsSingleUpdate($hash, 'CacheUsage', $memcount, 0);
  3452. }
  3453. };
  3454. return;
  3455. }
  3456. #########################################################################################
  3457. #
  3458. # Subroutine cutCol - Daten auf maximale Länge beschneiden
  3459. #
  3460. #########################################################################################
  3461. sub DbLog_cutCol($$$$$$$) {
  3462. my ($hash,$dn,$dt,$evt,$rd,$val,$unit)= @_;
  3463. my $name = $hash->{NAME};
  3464. my $colevent = AttrVal($name, 'colEvent', undef);
  3465. my $colreading = AttrVal($name, 'colReading', undef);
  3466. my $colvalue = AttrVal($name, 'colValue', undef);
  3467. if ($hash->{MODEL} ne 'SQLITE' || defined($colevent) || defined($colreading) || defined($colvalue) ) {
  3468. $dn = substr($dn,0, $hash->{HELPER}{DEVICECOL});
  3469. $dt = substr($dt,0, $hash->{HELPER}{TYPECOL});
  3470. $evt = substr($evt,0, $hash->{HELPER}{EVENTCOL});
  3471. $rd = substr($rd,0, $hash->{HELPER}{READINGCOL});
  3472. $val = substr($val,0, $hash->{HELPER}{VALUECOL});
  3473. $unit = substr($unit,0, $hash->{HELPER}{UNITCOL}) if($unit);
  3474. }
  3475. return ($dn,$dt,$evt,$rd,$val,$unit);
  3476. }
  3477. ###############################################################################
  3478. # liefert zurück ob Autocommit ($useac) bzw. Transaktion ($useta)
  3479. # verwendet werden soll
  3480. #
  3481. # basic_ta:on - Autocommit Servereinstellung / Transaktion ein
  3482. # basic_ta:off - Autocommit Servereinstellung / Transaktion aus
  3483. # ac:on_ta:on - Autocommit ein / Transaktion ein
  3484. # ac:on_ta:off - Autocommit ein / Transaktion aus
  3485. # ac:off_ta:on - Autocommit aus / Transaktion ein (AC aus impliziert TA ein)
  3486. #
  3487. # Autocommit: 0/1/2 = aus/ein/Servereinstellung
  3488. # Transaktion: 0/1 = aus/ein
  3489. ###############################################################################
  3490. sub DbLog_commitMode ($) {
  3491. my ($hash) = @_;
  3492. my $name = $hash->{NAME};
  3493. my $useac = 2; # default Servereinstellung
  3494. my $useta = 1; # default Transaktion ein
  3495. my $cm = AttrVal($name, "commitMode", "basic_ta:on");
  3496. my ($ac,$ta) = split("_",$cm);
  3497. $useac = ($ac =~ /off/)?0:($ac =~ /on/)?1:2;
  3498. $useta = 0 if($ta =~ /off/);
  3499. return($useac,$useta);
  3500. }
  3501. ###############################################################################
  3502. # Zeichen von Feldevents filtern
  3503. ###############################################################################
  3504. sub DbLog_charfilter ($) {
  3505. my ($txt) = @_;
  3506. my ($p,$a);
  3507. # nur erwünschte Zeichen ASCII %d32-126 und Sonderzeichen
  3508. $txt =~ s/ß/ss/g;
  3509. $txt =~ s/ä/ae/g;
  3510. $txt =~ s/ö/oe/g;
  3511. $txt =~ s/ü/ue/g;
  3512. $txt =~ s/Ä/Ae/g;
  3513. $txt =~ s/Ö/Oe/g;
  3514. $txt =~ s/Ü/Ue/g;
  3515. $txt =~ s/€/EUR/g;
  3516. $txt =~ s/\xb0/1degree1/g;
  3517. $txt =~ tr/ A-Za-z0-9!"#$%&'()*+,-.\/:;<=>?@[\\]^_`{|}~//cd;
  3518. $txt =~ s/1degree1/°/g;
  3519. return($txt);
  3520. }
  3521. #########################################################################################
  3522. ### DBLog - Historische Werte ausduennen (alte blockiernde Variante) > Forum #41089
  3523. #########################################################################################
  3524. sub DbLog_reduceLog($@) {
  3525. my ($hash,@a) = @_;
  3526. my ($ret,$row,$err,$filter,$exclude,$c,$day,$hour,$lastHour,$updDate,$updHour,$average,$processingDay,$lastUpdH,%hourlyKnown,%averageHash,@excludeRegex,@dayRows,@averageUpd,@averageUpdD);
  3527. my ($name,$startTime,$currentHour,$currentDay,$deletedCount,$updateCount,$sum,$rowCount,$excludeCount) = ($hash->{NAME},time(),99,0,0,0,0,0,0);
  3528. my $dbh = DbLog_ConnectNewDBH($hash);
  3529. return if(!$dbh);
  3530. if ($a[-1] =~ /^EXCLUDE=(.+:.+)+/i) {
  3531. ($filter) = $a[-1] =~ /^EXCLUDE=(.+)/i;
  3532. @excludeRegex = split(',',$filter);
  3533. } elsif ($a[-1] =~ /^INCLUDE=.+:.+$/i) {
  3534. $filter = 1;
  3535. }
  3536. if (defined($a[3])) {
  3537. $average = ($a[3] =~ /average=day/i) ? "AVERAGE=DAY" : ($a[3] =~ /average/i) ? "AVERAGE=HOUR" : 0;
  3538. }
  3539. Log3($name, 3, "DbLog $name: reduceLog requested with DAYS=$a[2]"
  3540. .(($average || $filter) ? ', ' : '').(($average) ? "$average" : '')
  3541. .(($average && $filter) ? ", " : '').(($filter) ? uc((split('=',$a[-1]))[0]).'='.(split('=',$a[-1]))[1] : ''));
  3542. my ($useac,$useta) = DbLog_commitMode($hash);
  3543. my $ac = ($dbh->{AutoCommit})?"ON":"OFF";
  3544. my $tm = ($useta)?"ON":"OFF";
  3545. Log3 $hash->{NAME}, 4, "DbLog $name -> AutoCommit mode: $ac, Transaction mode: $tm";
  3546. my ($od,$nd) = split(":",$a[2]); # $od - Tage älter als , $nd - Tage neuer als
  3547. my ($ots,$nts);
  3548. if ($hash->{MODEL} eq 'SQLITE') {
  3549. $ots = "datetime('now', '-$od days')";
  3550. $nts = "datetime('now', '-$nd days')" if($nd);
  3551. } elsif ($hash->{MODEL} eq 'MYSQL') {
  3552. $ots = "DATE_SUB(CURDATE(),INTERVAL $od DAY)";
  3553. $nts = "DATE_SUB(CURDATE(),INTERVAL $nd DAY)" if($nd);
  3554. } elsif ($hash->{MODEL} eq 'POSTGRESQL') {
  3555. $ots = "NOW() - INTERVAL '$od' DAY";
  3556. $nts = "NOW() - INTERVAL '$nd' DAY" if($nd);
  3557. } else {
  3558. $ret = 'Unknown database type.';
  3559. }
  3560. if ($ots) {
  3561. my ($sth_del, $sth_upd, $sth_delD, $sth_updD, $sth_get);
  3562. eval { $sth_del = $dbh->prepare_cached("DELETE FROM history WHERE (DEVICE=?) AND (READING=?) AND (TIMESTAMP=?) AND (VALUE=?)");
  3563. $sth_upd = $dbh->prepare_cached("UPDATE history SET TIMESTAMP=?, EVENT=?, VALUE=? WHERE (DEVICE=?) AND (READING=?) AND (TIMESTAMP=?) AND (VALUE=?)");
  3564. $sth_delD = $dbh->prepare_cached("DELETE FROM history WHERE (DEVICE=?) AND (READING=?) AND (TIMESTAMP=?)");
  3565. $sth_updD = $dbh->prepare_cached("UPDATE history SET TIMESTAMP=?, EVENT=?, VALUE=? WHERE (DEVICE=?) AND (READING=?) AND (TIMESTAMP=?)");
  3566. $sth_get = $dbh->prepare("SELECT TIMESTAMP,DEVICE,'',READING,VALUE FROM history WHERE "
  3567. .($a[-1] =~ /^INCLUDE=(.+):(.+)$/i ? "DEVICE like '$1' AND READING like '$2' AND " : '')
  3568. ."TIMESTAMP < $ots".($nts?" AND TIMESTAMP >= $nts ":" ")."ORDER BY TIMESTAMP ASC"); # '' was EVENT, no longer in use
  3569. };
  3570. $sth_get->execute();
  3571. do {
  3572. $row = $sth_get->fetchrow_arrayref || ['0000-00-00 00:00:00','D','','R','V']; # || execute last-day dummy
  3573. $ret = 1;
  3574. ($day,$hour) = $row->[0] =~ /-(\d{2})\s(\d{2}):/;
  3575. $rowCount++ if($day != 00);
  3576. if ($day != $currentDay) {
  3577. if ($currentDay) { # false on first executed day
  3578. if (scalar @dayRows) {
  3579. ($lastHour) = $dayRows[-1]->[0] =~ /(.*\d+\s\d{2}):/;
  3580. $c = 0;
  3581. for my $delRow (@dayRows) {
  3582. $c++ if($day != 00 || $delRow->[0] !~ /$lastHour/);
  3583. }
  3584. if($c) {
  3585. $deletedCount += $c;
  3586. Log3($name, 3, "DbLog $name: reduceLog deleting $c records of day: $processingDay");
  3587. $dbh->{RaiseError} = 1;
  3588. $dbh->{PrintError} = 0;
  3589. eval {$dbh->begin_work() if($dbh->{AutoCommit});};
  3590. eval {
  3591. my $i = 0;
  3592. my $k = 1;
  3593. my $th = ($#dayRows <= 2000)?100:($#dayRows <= 30000)?1000:10000;
  3594. for my $delRow (@dayRows) {
  3595. if($day != 00 || $delRow->[0] !~ /$lastHour/) {
  3596. Log3($name, 5, "DbLog $name: DELETE FROM history WHERE (DEVICE=$delRow->[1]) AND (READING=$delRow->[3]) AND (TIMESTAMP=$delRow->[0]) AND (VALUE=$delRow->[4])");
  3597. $sth_del->execute(($delRow->[1], $delRow->[3], $delRow->[0], $delRow->[4]));
  3598. $i++;
  3599. if($i == $th) {
  3600. my $prog = $k * $i;
  3601. Log3($name, 3, "DbLog $name: reduceLog deletion progress of day: $processingDay is: $prog");
  3602. $i = 0;
  3603. $k++;
  3604. }
  3605. }
  3606. }
  3607. };
  3608. if ($@) {
  3609. Log3($hash->{NAME}, 3, "DbLog $name: reduceLog ! FAILED ! for day $processingDay");
  3610. eval {$dbh->rollback() if(!$dbh->{AutoCommit});};
  3611. $ret = 0;
  3612. } else {
  3613. eval {$dbh->commit() if(!$dbh->{AutoCommit});};
  3614. }
  3615. $dbh->{RaiseError} = 0;
  3616. $dbh->{PrintError} = 1;
  3617. }
  3618. @dayRows = ();
  3619. }
  3620. if ($ret && defined($a[3]) && $a[3] =~ /average/i) {
  3621. $dbh->{RaiseError} = 1;
  3622. $dbh->{PrintError} = 0;
  3623. eval {$dbh->begin_work() if($dbh->{AutoCommit});};
  3624. eval {
  3625. push(@averageUpd, {%hourlyKnown}) if($day != 00);
  3626. $c = 0;
  3627. for my $hourHash (@averageUpd) { # Only count for logging...
  3628. for my $hourKey (keys %$hourHash) {
  3629. $c++ if ($hourHash->{$hourKey}->[0] && scalar(@{$hourHash->{$hourKey}->[4]}) > 1);
  3630. }
  3631. }
  3632. $updateCount += $c;
  3633. Log3($name, 3, "DbLog $name: reduceLog (hourly-average) updating $c records of day: $processingDay") if($c); # else only push to @averageUpdD
  3634. my $i = 0;
  3635. my $k = 1;
  3636. my $th = ($c <= 2000)?100:($c <= 30000)?1000:10000;
  3637. for my $hourHash (@averageUpd) {
  3638. for my $hourKey (keys %$hourHash) {
  3639. if ($hourHash->{$hourKey}->[0]) { # true if reading is a number
  3640. ($updDate,$updHour) = $hourHash->{$hourKey}->[0] =~ /(.*\d+)\s(\d{2}):/;
  3641. if (scalar(@{$hourHash->{$hourKey}->[4]}) > 1) { # true if reading has multiple records this hour
  3642. for (@{$hourHash->{$hourKey}->[4]}) { $sum += $_; }
  3643. $average = sprintf('%.3f', $sum/scalar(@{$hourHash->{$hourKey}->[4]}) );
  3644. $sum = 0;
  3645. Log3($name, 5, "DbLog $name: UPDATE history SET TIMESTAMP=$updDate $updHour:30:00, EVENT='rl_av_h', VALUE=$average WHERE DEVICE=$hourHash->{$hourKey}->[1] AND READING=$hourHash->{$hourKey}->[3] AND TIMESTAMP=$hourHash->{$hourKey}->[0] AND VALUE=$hourHash->{$hourKey}->[4]->[0]");
  3646. $sth_upd->execute(("$updDate $updHour:30:00", 'rl_av_h', $average, $hourHash->{$hourKey}->[1], $hourHash->{$hourKey}->[3], $hourHash->{$hourKey}->[0], $hourHash->{$hourKey}->[4]->[0]));
  3647. $i++;
  3648. if($i == $th) {
  3649. my $prog = $k * $i;
  3650. Log3($name, 3, "DbLog $name: reduceLog (hourly-average) updating progress of day: $processingDay is: $prog");
  3651. $i = 0;
  3652. $k++;
  3653. }
  3654. push(@averageUpdD, ["$updDate $updHour:30:00", 'rl_av_h', $average, $hourHash->{$hourKey}->[1], $hourHash->{$hourKey}->[3], $updDate]) if (defined($a[3]) && $a[3] =~ /average=day/i);
  3655. } else {
  3656. push(@averageUpdD, [$hourHash->{$hourKey}->[0], $hourHash->{$hourKey}->[2], $hourHash->{$hourKey}->[4]->[0], $hourHash->{$hourKey}->[1], $hourHash->{$hourKey}->[3], $updDate]) if (defined($a[3]) && $a[3] =~ /average=day/i);
  3657. }
  3658. }
  3659. }
  3660. }
  3661. };
  3662. if ($@) {
  3663. $err = $@;
  3664. Log3($hash->{NAME}, 2, "DbLog $name - reduceLogNbl ! FAILED ! for day $processingDay: $err");
  3665. eval {$dbh->rollback() if(!$dbh->{AutoCommit});};
  3666. @averageUpdD = ();
  3667. } else {
  3668. eval {$dbh->commit() if(!$dbh->{AutoCommit});};
  3669. }
  3670. $dbh->{RaiseError} = 0;
  3671. $dbh->{PrintError} = 1;
  3672. @averageUpd = ();
  3673. }
  3674. if (defined($a[3]) && $a[3] =~ /average=day/i && scalar(@averageUpdD) && $day != 00) {
  3675. $dbh->{RaiseError} = 1;
  3676. $dbh->{PrintError} = 0;
  3677. eval {$dbh->begin_work() if($dbh->{AutoCommit});};
  3678. eval {
  3679. for (@averageUpdD) {
  3680. push(@{$averageHash{$_->[3].$_->[4]}->{tedr}}, [$_->[0], $_->[1], $_->[3], $_->[4]]);
  3681. $averageHash{$_->[3].$_->[4]}->{sum} += $_->[2];
  3682. $averageHash{$_->[3].$_->[4]}->{date} = $_->[5];
  3683. }
  3684. $c = 0;
  3685. for (keys %averageHash) {
  3686. if(scalar @{$averageHash{$_}->{tedr}} == 1) {
  3687. delete $averageHash{$_};
  3688. } else {
  3689. $c += (scalar(@{$averageHash{$_}->{tedr}}) - 1);
  3690. }
  3691. }
  3692. $deletedCount += $c;
  3693. $updateCount += keys(%averageHash);
  3694. my ($id,$iu) = 0;
  3695. my ($kd,$ku) = 1;
  3696. my $thd = ($c <= 2000)?100:($c <= 30000)?1000:10000;
  3697. my $thu = ((keys %averageHash) <= 2000)?100:((keys %averageHash) <= 30000)?1000:10000;
  3698. Log3($name, 3, "DbLog $name: reduceLog (daily-average) updating ".(keys %averageHash).", deleting $c records of day: $processingDay") if(keys %averageHash);
  3699. for my $reading (keys %averageHash) {
  3700. $average = sprintf('%.3f', $averageHash{$reading}->{sum}/scalar(@{$averageHash{$reading}->{tedr}}));
  3701. $lastUpdH = pop @{$averageHash{$reading}->{tedr}};
  3702. for (@{$averageHash{$reading}->{tedr}}) {
  3703. Log3($name, 5, "DbLog $name: DELETE FROM history WHERE DEVICE='$_->[2]' AND READING='$_->[3]' AND TIMESTAMP='$_->[0]'");
  3704. $sth_delD->execute(($_->[2], $_->[3], $_->[0]));
  3705. $id++;
  3706. if($id == $thd) {
  3707. my $prog = $kd * $id;
  3708. Log3($name, 3, "DbLog $name: reduceLog (daily-average) deleting progress of day: $processingDay is: $prog");
  3709. $id = 0;
  3710. $kd++;
  3711. }
  3712. }
  3713. Log3($name, 5, "DbLog $name: UPDATE history SET TIMESTAMP=$averageHash{$reading}->{date} 12:00:00, EVENT='rl_av_d', VALUE=$average WHERE (DEVICE=$lastUpdH->[2]) AND (READING=$lastUpdH->[3]) AND (TIMESTAMP=$lastUpdH->[0])");
  3714. $sth_updD->execute(($averageHash{$reading}->{date}." 12:00:00", 'rl_av_d', $average, $lastUpdH->[2], $lastUpdH->[3], $lastUpdH->[0]));
  3715. $iu++;
  3716. if($iu == $thu) {
  3717. my $prog = $ku * $id;
  3718. Log3($name, 3, "DbLog $name: reduceLog (daily-average) updating progress of day: $processingDay is: $prog");
  3719. $iu = 0;
  3720. $ku++;
  3721. }
  3722. }
  3723. };
  3724. if ($@) {
  3725. $err = $@;
  3726. Log3($hash->{NAME}, 2, "DbLog $name - reduceLogNbl ! FAILED ! for day $processingDay: $err");
  3727. eval {$dbh->rollback() if(!$dbh->{AutoCommit});};
  3728. } else {
  3729. eval {$dbh->commit() if(!$dbh->{AutoCommit});};
  3730. }
  3731. $dbh->{RaiseError} = 0;
  3732. $dbh->{PrintError} = 1;
  3733. }
  3734. %averageHash = ();
  3735. %hourlyKnown = ();
  3736. @averageUpd = ();
  3737. @averageUpdD = ();
  3738. $currentHour = 99;
  3739. }
  3740. $currentDay = $day;
  3741. }
  3742. if ($hour != $currentHour) { # forget records from last hour, but remember these for average
  3743. if (defined($a[3]) && $a[3] =~ /average/i && keys(%hourlyKnown)) {
  3744. push(@averageUpd, {%hourlyKnown});
  3745. }
  3746. %hourlyKnown = ();
  3747. $currentHour = $hour;
  3748. }
  3749. if (defined $hourlyKnown{$row->[1].$row->[3]}) { # remember first readings for device per h, other can be deleted
  3750. push(@dayRows, [@$row]);
  3751. if (defined($a[3]) && $a[3] =~ /average/i && defined($row->[4]) && $row->[4] =~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ && $hourlyKnown{$row->[1].$row->[3]}->[0]) {
  3752. if ($hourlyKnown{$row->[1].$row->[3]}->[0]) {
  3753. push(@{$hourlyKnown{$row->[1].$row->[3]}->[4]}, $row->[4]);
  3754. }
  3755. }
  3756. } else {
  3757. $exclude = 0;
  3758. for (@excludeRegex) {
  3759. $exclude = 1 if("$row->[1]:$row->[3]" =~ /^$_$/);
  3760. }
  3761. if ($exclude) {
  3762. $excludeCount++ if($day != 00);
  3763. } else {
  3764. $hourlyKnown{$row->[1].$row->[3]} = (defined($row->[4]) && $row->[4] =~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/) ? [$row->[0],$row->[1],$row->[2],$row->[3],[$row->[4]]] : [0];
  3765. }
  3766. }
  3767. $processingDay = (split(' ',$row->[0]))[0];
  3768. } while( $day != 00 );
  3769. my $result = "Rows processed: $rowCount, deleted: $deletedCount"
  3770. .((defined($a[3]) && $a[3] =~ /average/i)? ", updated: $updateCount" : '')
  3771. .(($excludeCount)? ", excluded: $excludeCount" : '')
  3772. .", time: ".sprintf('%.2f',time() - $startTime)."sec";
  3773. Log3($name, 3, "DbLog $name: reduceLog executed. $result");
  3774. readingsSingleUpdate($hash,"reduceLogState",$result,1);
  3775. $ret = "reduceLog executed. $result";
  3776. }
  3777. $dbh->disconnect();
  3778. return $ret;
  3779. }
  3780. #########################################################################################
  3781. ### DBLog - Historische Werte ausduennen non-blocking > Forum #41089
  3782. #########################################################################################
  3783. sub DbLog_reduceLogNbl($) {
  3784. my ($name) = @_;
  3785. my $hash = $defs{$name};
  3786. my $dbconn = $hash->{dbconn};
  3787. my $dbuser = $hash->{dbuser};
  3788. my $dbpassword = $attr{"sec$name"}{secret};
  3789. my @a = @{$hash->{HELPER}{REDUCELOG}};
  3790. my $utf8 = defined($hash->{UTF8})?$hash->{UTF8}:0;
  3791. delete $hash->{HELPER}{REDUCELOG};
  3792. my ($ret,$row,$filter,$exclude,$c,$day,$hour,$lastHour,$updDate,$updHour,$average,$processingDay,$lastUpdH,%hourlyKnown,%averageHash,@excludeRegex,@dayRows,@averageUpd,@averageUpdD);
  3793. my ($startTime,$currentHour,$currentDay,$deletedCount,$updateCount,$sum,$rowCount,$excludeCount) = (time(),99,0,0,0,0,0,0);
  3794. my ($dbh,$err);
  3795. Log3 ($name, 5, "DbLog $name -> Start DbLog_reduceLogNbl");
  3796. my ($useac,$useta) = DbLog_commitMode($hash);
  3797. if(!$useac) {
  3798. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoCommit => 0 });};
  3799. } elsif($useac == 1) {
  3800. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoCommit => 1 });};
  3801. } else {
  3802. # Server default
  3803. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1 });};
  3804. }
  3805. if ($@) {
  3806. $err = encode_base64($@,"");
  3807. Log3 ($name, 2, "DbLog $name -> DbLog_reduceLogNbl - $@");
  3808. Log3 ($name, 5, "DbLog $name -> DbLog_reduceLogNbl finished");
  3809. return "$name|''|$err";
  3810. }
  3811. if ($a[-1] =~ /^EXCLUDE=(.+:.+)+/i) {
  3812. ($filter) = $a[-1] =~ /^EXCLUDE=(.+)/i;
  3813. @excludeRegex = split(',',$filter);
  3814. } elsif ($a[-1] =~ /^INCLUDE=.+:.+$/i) {
  3815. $filter = 1;
  3816. }
  3817. if (defined($a[3])) {
  3818. $average = ($a[3] =~ /average=day/i) ? "AVERAGE=DAY" : ($a[3] =~ /average/i) ? "AVERAGE=HOUR" : 0;
  3819. }
  3820. Log3($name, 3, "DbLog $name: reduceLogNbl requested with DAYS=$a[2]"
  3821. .(($average || $filter) ? ', ' : '').(($average) ? "$average" : '')
  3822. .(($average && $filter) ? ", " : '').(($filter) ? uc((split('=',$a[-1]))[0]).'='.(split('=',$a[-1]))[1] : ''));
  3823. my $ac = ($dbh->{AutoCommit})?"ON":"OFF";
  3824. my $tm = ($useta)?"ON":"OFF";
  3825. Log3 $hash->{NAME}, 4, "DbLog $name -> AutoCommit mode: $ac, Transaction mode: $tm";
  3826. my ($od,$nd) = split(":",$a[2]); # $od - Tage älter als , $nd - Tage neuer als
  3827. my ($ots,$nts);
  3828. if ($hash->{MODEL} eq 'SQLITE') {
  3829. $ots = "datetime('now', '-$od days')";
  3830. $nts = "datetime('now', '-$nd days')" if($nd);
  3831. } elsif ($hash->{MODEL} eq 'MYSQL') {
  3832. $ots = "DATE_SUB(CURDATE(),INTERVAL $od DAY)";
  3833. $nts = "DATE_SUB(CURDATE(),INTERVAL $nd DAY)" if($nd);
  3834. } elsif ($hash->{MODEL} eq 'POSTGRESQL') {
  3835. $ots = "NOW() - INTERVAL '$od' DAY";
  3836. $nts = "NOW() - INTERVAL '$nd' DAY" if($nd);
  3837. } else {
  3838. $ret = 'Unknown database type.';
  3839. }
  3840. if ($ots) {
  3841. my ($sth_del, $sth_upd, $sth_delD, $sth_updD, $sth_get);
  3842. eval { $sth_del = $dbh->prepare_cached("DELETE FROM history WHERE (DEVICE=?) AND (READING=?) AND (TIMESTAMP=?) AND (VALUE=?)");
  3843. $sth_upd = $dbh->prepare_cached("UPDATE history SET TIMESTAMP=?, EVENT=?, VALUE=? WHERE (DEVICE=?) AND (READING=?) AND (TIMESTAMP=?) AND (VALUE=?)");
  3844. $sth_delD = $dbh->prepare_cached("DELETE FROM history WHERE (DEVICE=?) AND (READING=?) AND (TIMESTAMP=?)");
  3845. $sth_updD = $dbh->prepare_cached("UPDATE history SET TIMESTAMP=?, EVENT=?, VALUE=? WHERE (DEVICE=?) AND (READING=?) AND (TIMESTAMP=?)");
  3846. $sth_get = $dbh->prepare("SELECT TIMESTAMP,DEVICE,'',READING,VALUE FROM history WHERE "
  3847. .($a[-1] =~ /^INCLUDE=(.+):(.+)$/i ? "DEVICE like '$1' AND READING like '$2' AND " : '')
  3848. ."TIMESTAMP < $ots".($nts?" AND TIMESTAMP >= $nts ":" ")."ORDER BY TIMESTAMP ASC"); # '' was EVENT, no longer in use
  3849. };
  3850. if ($@) {
  3851. $err = encode_base64($@,"");
  3852. Log3 ($name, 2, "DbLog $name -> DbLog_reduceLogNbl - $@");
  3853. Log3 ($name, 5, "DbLog $name -> DbLog_reduceLogNbl finished");
  3854. return "$name|''|$err";
  3855. }
  3856. eval { $sth_get->execute(); };
  3857. if ($@) {
  3858. $err = encode_base64($@,"");
  3859. Log3 ($name, 2, "DbLog $name -> DbLog_reduceLogNbl - $@");
  3860. Log3 ($name, 5, "DbLog $name -> DbLog_reduceLogNbl finished");
  3861. return "$name|''|$err";
  3862. }
  3863. do {
  3864. $row = $sth_get->fetchrow_arrayref || ['0000-00-00 00:00:00','D','','R','V']; # || execute last-day dummy
  3865. $ret = 1;
  3866. ($day,$hour) = $row->[0] =~ /-(\d{2})\s(\d{2}):/;
  3867. $rowCount++ if($day != 00);
  3868. if ($day != $currentDay) {
  3869. if ($currentDay) { # false on first executed day
  3870. if (scalar @dayRows) {
  3871. ($lastHour) = $dayRows[-1]->[0] =~ /(.*\d+\s\d{2}):/;
  3872. $c = 0;
  3873. for my $delRow (@dayRows) {
  3874. $c++ if($day != 00 || $delRow->[0] !~ /$lastHour/);
  3875. }
  3876. if($c) {
  3877. $deletedCount += $c;
  3878. Log3($name, 3, "DbLog $name: reduceLogNbl deleting $c records of day: $processingDay");
  3879. $dbh->{RaiseError} = 1;
  3880. $dbh->{PrintError} = 0;
  3881. eval {$dbh->begin_work() if($dbh->{AutoCommit});};
  3882. if ($@) {
  3883. Log3 ($name, 2, "DbLog $name -> DbLog_reduceLogNbl - $@");
  3884. }
  3885. eval {
  3886. my $i = 0;
  3887. my $k = 1;
  3888. my $th = ($#dayRows <= 2000)?100:($#dayRows <= 30000)?1000:10000;
  3889. for my $delRow (@dayRows) {
  3890. if($day != 00 || $delRow->[0] !~ /$lastHour/) {
  3891. Log3($name, 4, "DbLog $name: DELETE FROM history WHERE (DEVICE=$delRow->[1]) AND (READING=$delRow->[3]) AND (TIMESTAMP=$delRow->[0]) AND (VALUE=$delRow->[4])");
  3892. $sth_del->execute(($delRow->[1], $delRow->[3], $delRow->[0], $delRow->[4]));
  3893. $i++;
  3894. if($i == $th) {
  3895. my $prog = $k * $i;
  3896. Log3($name, 3, "DbLog $name: reduceLogNbl deletion progress of day: $processingDay is: $prog");
  3897. $i = 0;
  3898. $k++;
  3899. }
  3900. }
  3901. }
  3902. };
  3903. if ($@) {
  3904. $err = $@;
  3905. Log3($hash->{NAME}, 2, "DbLog $name - reduceLogNbl ! FAILED ! for day $processingDay: $err");
  3906. eval {$dbh->rollback() if(!$dbh->{AutoCommit});};
  3907. if ($@) {
  3908. Log3 ($name, 2, "DbLog $name -> DbLog_reduceLogNbl - $@");
  3909. }
  3910. $ret = 0;
  3911. } else {
  3912. eval {$dbh->commit() if(!$dbh->{AutoCommit});};
  3913. if ($@) {
  3914. Log3 ($name, 2, "DbLog $name -> DbLog_reduceLogNbl - $@");
  3915. }
  3916. }
  3917. $dbh->{RaiseError} = 0;
  3918. $dbh->{PrintError} = 1;
  3919. }
  3920. @dayRows = ();
  3921. }
  3922. if ($ret && defined($a[3]) && $a[3] =~ /average/i) {
  3923. $dbh->{RaiseError} = 1;
  3924. $dbh->{PrintError} = 0;
  3925. eval {$dbh->begin_work() if($dbh->{AutoCommit});};
  3926. if ($@) {
  3927. Log3 ($name, 2, "DbLog $name -> DbLog_reduceLogNbl - $@");
  3928. }
  3929. eval {
  3930. push(@averageUpd, {%hourlyKnown}) if($day != 00);
  3931. $c = 0;
  3932. for my $hourHash (@averageUpd) { # Only count for logging...
  3933. for my $hourKey (keys %$hourHash) {
  3934. $c++ if ($hourHash->{$hourKey}->[0] && scalar(@{$hourHash->{$hourKey}->[4]}) > 1);
  3935. }
  3936. }
  3937. $updateCount += $c;
  3938. Log3($name, 3, "DbLog $name: reduceLogNbl (hourly-average) updating $c records of day: $processingDay") if($c); # else only push to @averageUpdD
  3939. my $i = 0;
  3940. my $k = 1;
  3941. my $th = ($c <= 2000)?100:($c <= 30000)?1000:10000;
  3942. for my $hourHash (@averageUpd) {
  3943. for my $hourKey (keys %$hourHash) {
  3944. if ($hourHash->{$hourKey}->[0]) { # true if reading is a number
  3945. ($updDate,$updHour) = $hourHash->{$hourKey}->[0] =~ /(.*\d+)\s(\d{2}):/;
  3946. if (scalar(@{$hourHash->{$hourKey}->[4]}) > 1) { # true if reading has multiple records this hour
  3947. for (@{$hourHash->{$hourKey}->[4]}) { $sum += $_; }
  3948. $average = sprintf('%.3f', $sum/scalar(@{$hourHash->{$hourKey}->[4]}) );
  3949. $sum = 0;
  3950. Log3($name, 4, "DbLog $name: UPDATE history SET TIMESTAMP=$updDate $updHour:30:00, EVENT='rl_av_h', VALUE=$average WHERE DEVICE=$hourHash->{$hourKey}->[1] AND READING=$hourHash->{$hourKey}->[3] AND TIMESTAMP=$hourHash->{$hourKey}->[0] AND VALUE=$hourHash->{$hourKey}->[4]->[0]");
  3951. $sth_upd->execute(("$updDate $updHour:30:00", 'rl_av_h', $average, $hourHash->{$hourKey}->[1], $hourHash->{$hourKey}->[3], $hourHash->{$hourKey}->[0], $hourHash->{$hourKey}->[4]->[0]));
  3952. $i++;
  3953. if($i == $th) {
  3954. my $prog = $k * $i;
  3955. Log3($name, 3, "DbLog $name: reduceLogNbl (hourly-average) updating progress of day: $processingDay is: $prog");
  3956. $i = 0;
  3957. $k++;
  3958. }
  3959. push(@averageUpdD, ["$updDate $updHour:30:00", 'rl_av_h', $average, $hourHash->{$hourKey}->[1], $hourHash->{$hourKey}->[3], $updDate]) if (defined($a[3]) && $a[3] =~ /average=day/i);
  3960. } else {
  3961. push(@averageUpdD, [$hourHash->{$hourKey}->[0], $hourHash->{$hourKey}->[2], $hourHash->{$hourKey}->[4]->[0], $hourHash->{$hourKey}->[1], $hourHash->{$hourKey}->[3], $updDate]) if (defined($a[3]) && $a[3] =~ /average=day/i);
  3962. }
  3963. }
  3964. }
  3965. }
  3966. };
  3967. if ($@) {
  3968. $err = $@;
  3969. Log3($hash->{NAME}, 2, "DbLog $name - reduceLogNbl average=hour ! FAILED ! for day $processingDay: $err");
  3970. eval {$dbh->rollback() if(!$dbh->{AutoCommit});};
  3971. if ($@) {
  3972. Log3 ($name, 2, "DbLog $name -> DbLog_reduceLogNbl - $@");
  3973. }
  3974. @averageUpdD = ();
  3975. } else {
  3976. eval {$dbh->commit() if(!$dbh->{AutoCommit});};
  3977. if ($@) {
  3978. Log3 ($name, 2, "DbLog $name -> DbLog_reduceLogNbl - $@");
  3979. }
  3980. }
  3981. $dbh->{RaiseError} = 0;
  3982. $dbh->{PrintError} = 1;
  3983. @averageUpd = ();
  3984. }
  3985. if (defined($a[3]) && $a[3] =~ /average=day/i && scalar(@averageUpdD) && $day != 00) {
  3986. $dbh->{RaiseError} = 1;
  3987. $dbh->{PrintError} = 0;
  3988. eval {$dbh->begin_work() if($dbh->{AutoCommit});};
  3989. if ($@) {
  3990. Log3 ($name, 2, "DbLog $name -> DbLog_reduceLogNbl - $@");
  3991. }
  3992. eval {
  3993. for (@averageUpdD) {
  3994. push(@{$averageHash{$_->[3].$_->[4]}->{tedr}}, [$_->[0], $_->[1], $_->[3], $_->[4]]);
  3995. $averageHash{$_->[3].$_->[4]}->{sum} += $_->[2];
  3996. $averageHash{$_->[3].$_->[4]}->{date} = $_->[5];
  3997. }
  3998. $c = 0;
  3999. for (keys %averageHash) {
  4000. if(scalar @{$averageHash{$_}->{tedr}} == 1) {
  4001. delete $averageHash{$_};
  4002. } else {
  4003. $c += (scalar(@{$averageHash{$_}->{tedr}}) - 1);
  4004. }
  4005. }
  4006. $deletedCount += $c;
  4007. $updateCount += keys(%averageHash);
  4008. my ($id,$iu) = 0;
  4009. my ($kd,$ku) = 1;
  4010. my $thd = ($c <= 2000)?100:($c <= 30000)?1000:10000;
  4011. my $thu = ((keys %averageHash) <= 2000)?100:((keys %averageHash) <= 30000)?1000:10000;
  4012. Log3($name, 3, "DbLog $name: reduceLogNbl (daily-average) updating ".(keys %averageHash).", deleting $c records of day: $processingDay") if(keys %averageHash);
  4013. for my $reading (keys %averageHash) {
  4014. $average = sprintf('%.3f', $averageHash{$reading}->{sum}/scalar(@{$averageHash{$reading}->{tedr}}));
  4015. $lastUpdH = pop @{$averageHash{$reading}->{tedr}};
  4016. for (@{$averageHash{$reading}->{tedr}}) {
  4017. Log3($name, 5, "DbLog $name: DELETE FROM history WHERE DEVICE='$_->[2]' AND READING='$_->[3]' AND TIMESTAMP='$_->[0]'");
  4018. $sth_delD->execute(($_->[2], $_->[3], $_->[0]));
  4019. $id++;
  4020. if($id == $thd) {
  4021. my $prog = $kd * $id;
  4022. Log3($name, 3, "DbLog $name: reduceLogNbl (daily-average) deleting progress of day: $processingDay is: $prog");
  4023. $id = 0;
  4024. $kd++;
  4025. }
  4026. }
  4027. Log3($name, 4, "DbLog $name: UPDATE history SET TIMESTAMP=$averageHash{$reading}->{date} 12:00:00, EVENT='rl_av_d', VALUE=$average WHERE (DEVICE=$lastUpdH->[2]) AND (READING=$lastUpdH->[3]) AND (TIMESTAMP=$lastUpdH->[0])");
  4028. $sth_updD->execute(($averageHash{$reading}->{date}." 12:00:00", 'rl_av_d', $average, $lastUpdH->[2], $lastUpdH->[3], $lastUpdH->[0]));
  4029. $iu++;
  4030. if($iu == $thu) {
  4031. my $prog = $ku * $id;
  4032. Log3($name, 3, "DbLog $name: reduceLogNbl (daily-average) updating progress of day: $processingDay is: $prog");
  4033. $iu = 0;
  4034. $ku++;
  4035. }
  4036. }
  4037. };
  4038. if ($@) {
  4039. Log3($hash->{NAME}, 3, "DbLog $name: reduceLogNbl average=day ! FAILED ! for day $processingDay");
  4040. eval {$dbh->rollback() if(!$dbh->{AutoCommit});};
  4041. if ($@) {
  4042. Log3 ($name, 2, "DbLog $name -> DbLog_reduceLogNbl - $@");
  4043. }
  4044. } else {
  4045. eval {$dbh->commit() if(!$dbh->{AutoCommit});};
  4046. if ($@) {
  4047. Log3 ($name, 2, "DbLog $name -> DbLog_reduceLogNbl - $@");
  4048. }
  4049. }
  4050. $dbh->{RaiseError} = 0;
  4051. $dbh->{PrintError} = 1;
  4052. }
  4053. %averageHash = ();
  4054. %hourlyKnown = ();
  4055. @averageUpd = ();
  4056. @averageUpdD = ();
  4057. $currentHour = 99;
  4058. }
  4059. $currentDay = $day;
  4060. }
  4061. if ($hour != $currentHour) { # forget records from last hour, but remember these for average
  4062. if (defined($a[3]) && $a[3] =~ /average/i && keys(%hourlyKnown)) {
  4063. push(@averageUpd, {%hourlyKnown});
  4064. }
  4065. %hourlyKnown = ();
  4066. $currentHour = $hour;
  4067. }
  4068. if (defined $hourlyKnown{$row->[1].$row->[3]}) { # remember first readings for device per h, other can be deleted
  4069. push(@dayRows, [@$row]);
  4070. if (defined($a[3]) && $a[3] =~ /average/i && defined($row->[4]) && $row->[4] =~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ && $hourlyKnown{$row->[1].$row->[3]}->[0]) {
  4071. if ($hourlyKnown{$row->[1].$row->[3]}->[0]) {
  4072. push(@{$hourlyKnown{$row->[1].$row->[3]}->[4]}, $row->[4]);
  4073. }
  4074. }
  4075. } else {
  4076. $exclude = 0;
  4077. for (@excludeRegex) {
  4078. $exclude = 1 if("$row->[1]:$row->[3]" =~ /^$_$/);
  4079. }
  4080. if ($exclude) {
  4081. $excludeCount++ if($day != 00);
  4082. } else {
  4083. $hourlyKnown{$row->[1].$row->[3]} = (defined($row->[4]) && $row->[4] =~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/) ? [$row->[0],$row->[1],$row->[2],$row->[3],[$row->[4]]] : [0];
  4084. }
  4085. }
  4086. $processingDay = (split(' ',$row->[0]))[0];
  4087. } while( $day != 00 );
  4088. my $result = "Rows processed: $rowCount, deleted: $deletedCount"
  4089. .((defined($a[3]) && $a[3] =~ /average/i)? ", updated: $updateCount" : '')
  4090. .(($excludeCount)? ", excluded: $excludeCount" : '')
  4091. .", time: ".sprintf('%.2f',time() - $startTime)."sec";
  4092. Log3($name, 3, "DbLog $name: reduceLogNbl finished. $result");
  4093. $ret = $result;
  4094. $ret = "reduceLogNbl finished. $result";
  4095. }
  4096. $dbh->disconnect();
  4097. $ret = encode_base64($ret,"");
  4098. Log3 ($name, 5, "DbLog $name -> DbLog_reduceLogNbl finished");
  4099. return "$name|$ret|0";
  4100. }
  4101. #########################################################################################
  4102. # DBLog - reduceLogNbl non-blocking Rückkehrfunktion
  4103. #########################################################################################
  4104. sub DbLog_reduceLogNbl_finished($) {
  4105. my ($string) = @_;
  4106. my @a = split("\\|",$string);
  4107. my $name = $a[0];
  4108. my $hash = $defs{$name};
  4109. my $ret = decode_base64($a[1]);
  4110. my $err = decode_base64($a[2]) if ($a[2]);
  4111. readingsSingleUpdate($hash,"reduceLogState",$err?$err:$ret,1);
  4112. delete $hash->{HELPER}{REDUCELOG_PID};
  4113. return;
  4114. }
  4115. #########################################################################################
  4116. # DBLog - count non-blocking
  4117. #########################################################################################
  4118. sub DbLog_countNbl($) {
  4119. my ($name) = @_;
  4120. my $hash = $defs{$name};
  4121. my ($cc,$hc,$bst,$st,$rt);
  4122. # Background-Startzeit
  4123. $bst = [gettimeofday];
  4124. my $dbh = DbLog_ConnectNewDBH($hash);
  4125. if (!$dbh) {
  4126. my $err = encode_base64("DbLog $name: DBLog_Set - count - DB connect not possible","");
  4127. return "$name|0|0|$err|0";
  4128. } else {
  4129. Log3 $name,4,"DbLog $name: Records count requested.";
  4130. # SQL-Startzeit
  4131. $st = [gettimeofday];
  4132. $hc = $dbh->selectrow_array('SELECT count(*) FROM history');
  4133. $cc = $dbh->selectrow_array('SELECT count(*) FROM current');
  4134. $dbh->disconnect();
  4135. # SQL-Laufzeit ermitteln
  4136. $rt = tv_interval($st);
  4137. }
  4138. # Background-Laufzeit ermitteln
  4139. my $brt = tv_interval($bst);
  4140. $rt = $rt.",".$brt;
  4141. return "$name|$cc|$hc|0|$rt";
  4142. }
  4143. #########################################################################################
  4144. # DBLog - count non-blocking Rückkehrfunktion
  4145. #########################################################################################
  4146. sub DbLog_countNbl_finished($)
  4147. {
  4148. my ($string) = @_;
  4149. my @a = split("\\|",$string);
  4150. my $name = $a[0];
  4151. my $hash = $defs{$name};
  4152. my $cc = $a[1];
  4153. my $hc = $a[2];
  4154. my $err = decode_base64($a[3]) if ($a[3]);
  4155. my $bt = $a[4] if($a[4]);
  4156. readingsSingleUpdate($hash,"state",$err,1) if($err);
  4157. readingsSingleUpdate($hash,"countHistory",$hc,1) if ($hc);
  4158. readingsSingleUpdate($hash,"countCurrent",$cc,1) if ($cc);
  4159. if(AttrVal($name, "showproctime", undef) && $bt) {
  4160. my ($rt,$brt) = split(",", $bt);
  4161. readingsBeginUpdate($hash);
  4162. readingsBulkUpdate($hash, "background_processing_time", sprintf("%.4f",$brt));
  4163. readingsBulkUpdate($hash, "sql_processing_time", sprintf("%.4f",$rt));
  4164. readingsEndUpdate($hash, 1);
  4165. }
  4166. delete $hash->{HELPER}{COUNT_PID};
  4167. return;
  4168. }
  4169. #########################################################################################
  4170. # DBLog - deleteOldDays non-blocking
  4171. #########################################################################################
  4172. sub DbLog_deldaysNbl($) {
  4173. my ($name) = @_;
  4174. my $hash = $defs{$name};
  4175. my $dbconn = $hash->{dbconn};
  4176. my $dbuser = $hash->{dbuser};
  4177. my $dbpassword = $attr{"sec$name"}{secret};
  4178. my $days = delete($hash->{HELPER}{DELDAYS});
  4179. my ($cmd,$dbh,$rows,$error,$sth,$ret,$bst,$brt,$st,$rt);
  4180. Log3 ($name, 5, "DbLog $name -> Start DbLog_deldaysNbl $days");
  4181. # Background-Startzeit
  4182. $bst = [gettimeofday];
  4183. my ($useac,$useta) = DbLog_commitMode($hash);
  4184. if(!$useac) {
  4185. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoCommit => 0 });};
  4186. } elsif($useac == 1) {
  4187. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoCommit => 1 });};
  4188. } else {
  4189. # Server default
  4190. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1 });};
  4191. }
  4192. if ($@) {
  4193. $error = encode_base64($@,"");
  4194. Log3 ($name, 2, "DbLog $name - Error: $@");
  4195. Log3 ($name, 5, "DbLog $name -> DbLog_deldaysNbl finished");
  4196. return "$name|0|0|$error";
  4197. }
  4198. my $ac = ($dbh->{AutoCommit})?"ON":"OFF";
  4199. my $tm = ($useta)?"ON":"OFF";
  4200. Log3 $hash->{NAME}, 4, "DbLog $name -> AutoCommit mode: $ac, Transaction mode: $tm";
  4201. $cmd = "delete from history where TIMESTAMP < ";
  4202. if ($hash->{MODEL} eq 'SQLITE') {
  4203. $cmd .= "datetime('now', '-$days days')";
  4204. } elsif ($hash->{MODEL} eq 'MYSQL') {
  4205. $cmd .= "DATE_SUB(CURDATE(),INTERVAL $days DAY)";
  4206. } elsif ($hash->{MODEL} eq 'POSTGRESQL') {
  4207. $cmd .= "NOW() - INTERVAL '$days' DAY";
  4208. } else {
  4209. $ret = 'Unknown database type. Maybe you can try userCommand anyway.';
  4210. $error = encode_base64($ret,"");
  4211. Log3 ($name, 2, "DbLog $name - Error: $ret");
  4212. Log3 ($name, 5, "DbLog $name -> DbLog_deldaysNbl finished");
  4213. return "$name|0|0|$error";
  4214. }
  4215. # SQL-Startzeit
  4216. $st = [gettimeofday];
  4217. eval {
  4218. $sth = $dbh->prepare($cmd);
  4219. $sth->execute();
  4220. };
  4221. if ($@) {
  4222. $error = encode_base64($@,"");
  4223. Log3 ($name, 2, "DbLog $name - $@");
  4224. $dbh->disconnect;
  4225. Log3 ($name, 4, "DbLog $name -> BlockingCall DbLog_deldaysNbl finished");
  4226. return "$name|0|0|$error";
  4227. } else {
  4228. $rows = $sth->rows;
  4229. $dbh->commit() if(!$dbh->{AutoCommit});
  4230. $dbh->disconnect;
  4231. }
  4232. # SQL-Laufzeit ermitteln
  4233. $rt = tv_interval($st);
  4234. # Background-Laufzeit ermitteln
  4235. $brt = tv_interval($bst);
  4236. $rt = $rt.",".$brt;
  4237. Log3 ($name, 5, "DbLog $name -> DbLog_deldaysNbl finished");
  4238. return "$name|$rows|$rt|0";
  4239. }
  4240. #########################################################################################
  4241. # DBLog - deleteOldDays non-blocking Rückkehrfunktion
  4242. #########################################################################################
  4243. sub DbLog_deldaysNbl_done($) {
  4244. my ($string) = @_;
  4245. my @a = split("\\|",$string);
  4246. my $name = $a[0];
  4247. my $hash = $defs{$name};
  4248. my $rows = $a[1];
  4249. my $bt = $a[2] if($a[2]);
  4250. my $err = decode_base64($a[3]) if ($a[3]);
  4251. Log3 ($name, 5, "DbLog $name -> Start DbLog_deldaysNbl_done");
  4252. if ($err) {
  4253. readingsSingleUpdate($hash,"state",$err,1);
  4254. delete $hash->{HELPER}{DELDAYS_PID};
  4255. Log3 ($name, 5, "DbLog $name -> DbLog_deldaysNbl_done finished");
  4256. return;
  4257. } else {
  4258. if(AttrVal($name, "showproctime", undef) && $bt) {
  4259. my ($rt,$brt) = split(",", $bt);
  4260. readingsBeginUpdate($hash);
  4261. readingsBulkUpdate($hash, "background_processing_time", sprintf("%.4f",$brt));
  4262. readingsBulkUpdate($hash, "sql_processing_time", sprintf("%.4f",$rt));
  4263. readingsEndUpdate($hash, 1);
  4264. }
  4265. readingsSingleUpdate($hash, "lastRowsDeleted", $rows ,1);
  4266. }
  4267. my $db = (split(/;|=/, $hash->{dbconn}))[1];
  4268. Log3 ($name, 3, "DbLog $name -> deleteOldDaysNbl finished. $rows entries of database $db deleted.");
  4269. delete $hash->{HELPER}{DELDAYS_PID};
  4270. Log3 ($name, 5, "DbLog $name -> DbLog_deldaysNbl_done finished");
  4271. return;
  4272. }
  4273. ################################################################
  4274. # benutzte DB-Feldlängen in Helper und Internals setzen
  4275. ################################################################
  4276. sub DbLog_setinternalcols ($){
  4277. my ($hash)= @_;
  4278. my $name = $hash->{NAME};
  4279. $hash->{HELPER}{DEVICECOL} = $columns{DEVICE};
  4280. $hash->{HELPER}{TYPECOL} = $columns{TYPE};
  4281. $hash->{HELPER}{EVENTCOL} = AttrVal($name, "colEvent", $columns{EVENT});
  4282. $hash->{HELPER}{READINGCOL} = AttrVal($name, "colReading", $columns{READING});
  4283. $hash->{HELPER}{VALUECOL} = AttrVal($name, "colValue", $columns{VALUE});
  4284. $hash->{HELPER}{UNITCOL} = $columns{UNIT};
  4285. $hash->{COLUMNS} = "field length used for Device: $hash->{HELPER}{DEVICECOL}, Type: $hash->{HELPER}{TYPECOL}, Event: $hash->{HELPER}{EVENTCOL}, Reading: $hash->{HELPER}{READINGCOL}, Value: $hash->{HELPER}{VALUECOL}, Unit: $hash->{HELPER}{UNITCOL} ";
  4286. # Statusbit "Columns sind gesetzt"
  4287. $hash->{HELPER}{COLSET} = 1;
  4288. return;
  4289. }
  4290. ################################################################
  4291. # reopen DB-Connection nach Ablauf set ... reopen [n] seconds
  4292. ################################################################
  4293. sub DbLog_reopen ($){
  4294. my ($hash) = @_;
  4295. my $name = $hash->{NAME};
  4296. my $async = AttrVal($name, "asyncMode", undef);
  4297. RemoveInternalTimer($hash, "DbLog_reopen");
  4298. if(DbLog_ConnectPush($hash)) {
  4299. # Statusbit "Kein Schreiben in DB erlauben" löschen
  4300. my $delay = delete $hash->{HELPER}{REOPEN_RUNS};
  4301. delete $hash->{HELPER}{REOPEN_RUNS_UNTIL};
  4302. Log3($name, 2, "DbLog $name: Database connection reopened (it was $delay seconds closed).") if($delay);
  4303. readingsSingleUpdate($hash, "state", "reopened", 1);
  4304. $hash->{HELPER}{OLDSTATE} = "reopened";
  4305. DbLog_execmemcache($hash) if($async);
  4306. } else {
  4307. InternalTimer(gettimeofday()+30, "DbLog_reopen", $hash, 0);
  4308. }
  4309. return;
  4310. }
  4311. ################################################################
  4312. # check ob primary key genutzt wird
  4313. ################################################################
  4314. sub DbLog_checkUsePK ($$){
  4315. my ($hash,$dbh) = @_;
  4316. my $name = $hash->{NAME};
  4317. my $dbconn = $hash->{dbconn};
  4318. my $upkh = 0;
  4319. my $upkc = 0;
  4320. my (@pkh,@pkc);
  4321. my $db = (split("=",(split(";",$dbconn))[0]))[1];
  4322. eval {@pkh = $dbh->primary_key( undef, undef, 'history' );};
  4323. eval {@pkc = $dbh->primary_key( undef, undef, 'current' );};
  4324. my $pkh = (!@pkh || @pkh eq "")?"none":join(",",@pkh);
  4325. my $pkc = (!@pkc || @pkc eq "")?"none":join(",",@pkc);
  4326. $pkh =~ tr/"//d;
  4327. $pkc =~ tr/"//d;
  4328. $upkh = 1 if(@pkh && @pkh ne "none");
  4329. $upkc = 1 if(@pkc && @pkc ne "none");
  4330. Log3 $hash->{NAME}, 5, "DbLog $name -> Primary Key used in $db.history: $pkh";
  4331. Log3 $hash->{NAME}, 5, "DbLog $name -> Primary Key used in $db.current: $pkc";
  4332. return ($upkh,$upkc,$pkh,$pkc);
  4333. }
  4334. ################################################################
  4335. # Routine für FHEMWEB Detailanzeige
  4336. ################################################################
  4337. sub DbLog_fhemwebFn($$$$) {
  4338. my ($FW_wname, $d, $room, $pageHash) = @_; # pageHash is set for summaryFn.
  4339. my $ret;
  4340. my $newIdx=1;
  4341. while($defs{"SVG_${d}_$newIdx"}) {
  4342. $newIdx++;
  4343. }
  4344. my $name = "SVG_${d}_$newIdx";
  4345. $ret .= FW_pH("cmd=define $name SVG $d:templateDB:HISTORY;".
  4346. "set $name copyGplotFile&detail=$name",
  4347. "<div class=\"dval\">Create SVG plot from DbLog</div>", 0, "dval", 1);
  4348. return $ret;
  4349. }
  4350. ################################################################
  4351. # Dropdown-Menü cuurent-Tabelle SVG-Editor
  4352. ################################################################
  4353. sub DbLog_sampleDataFn($$$$$) {
  4354. my ($dlName, $dlog, $max, $conf, $wName) = @_;
  4355. my $desc = "Device:Reading";
  4356. my @htmlArr;
  4357. my @example;
  4358. my @colregs;
  4359. my $counter;
  4360. my $currentPresent = AttrVal($dlName,'DbLogType','History');
  4361. my $dbhf = DbLog_ConnectNewDBH($defs{$dlName});
  4362. return if(!$dbhf);
  4363. # check presence of table current
  4364. # avoids fhem from crash if table 'current' is not present and attr DbLogType is set to /Current/
  4365. my $prescurr = eval {$dbhf->selectrow_array("select count(*) from current");} || 0;
  4366. Log3($dlName, 5, "DbLog $dlName: Table current present : $prescurr (0 = not present or no content)");
  4367. if($currentPresent =~ m/Current|SampleFill/ && $prescurr) {
  4368. # Table Current present, use it for sample data
  4369. my $query = "select device,reading from current where device <> '' group by device,reading";
  4370. my $sth = $dbhf->prepare( $query );
  4371. $sth->execute();
  4372. while (my @line = $sth->fetchrow_array()) {
  4373. $counter++;
  4374. push (@example, join (" ",@line)) if($counter <= 8); # show max 8 examples
  4375. push (@colregs, "$line[0]:$line[1]"); # push all eventTypes to selection list
  4376. }
  4377. $dbhf->disconnect();
  4378. my $cols = join(",", sort { "\L$a" cmp "\L$b" } @colregs);
  4379. # $max = 8 if($max > 8); # auskommentiert 27.02.2018, Notwendigkeit unklar (forum:#76008)
  4380. for(my $r=0; $r < $max; $r++) {
  4381. my @f = split(":", ($dlog->[$r] ? $dlog->[$r] : ":::"), 4);
  4382. my $ret = "";
  4383. $ret .= SVG_sel("par_${r}_0", $cols, "$f[0]:$f[1]");
  4384. # $ret .= SVG_txt("par_${r}_2", "", $f[2], 1); # Default not yet implemented
  4385. # $ret .= SVG_txt("par_${r}_3", "", $f[3], 3); # Function
  4386. # $ret .= SVG_txt("par_${r}_4", "", $f[4], 3); # RegExp
  4387. push @htmlArr, $ret;
  4388. }
  4389. } else {
  4390. # Table Current not present, so create an empty input field
  4391. push @example, "No sample data due to missing table 'Current'";
  4392. # $max = 8 if($max > 8); # auskommentiert 27.02.2018, Notwendigkeit unklar (forum:#76008)
  4393. for(my $r=0; $r < $max; $r++) {
  4394. my @f = split(":", ($dlog->[$r] ? $dlog->[$r] : ":::"), 4);
  4395. my $ret = "";
  4396. no warnings 'uninitialized'; # Forum:74690, bug unitialized
  4397. $ret .= SVG_txt("par_${r}_0", "", "$f[0]:$f[1]:$f[2]:$f[3]", 20);
  4398. use warnings;
  4399. # $ret .= SVG_txt("par_${r}_2", "", $f[2], 1); # Default not yet implemented
  4400. # $ret .= SVG_txt("par_${r}_3", "", $f[3], 3); # Function
  4401. # $ret .= SVG_txt("par_${r}_4", "", $f[4], 3); # RegExp
  4402. push @htmlArr, $ret;
  4403. }
  4404. }
  4405. return ($desc, \@htmlArr, join("<br>", @example));
  4406. }
  4407. ################################################################
  4408. #
  4409. # Charting Specific functions start here
  4410. #
  4411. ################################################################
  4412. ################################################################
  4413. #
  4414. # Error handling, returns a JSON String
  4415. #
  4416. ################################################################
  4417. sub DbLog_jsonError($) {
  4418. my $errormsg = $_[0];
  4419. my $json = '{"success": "false", "msg":"'.$errormsg.'"}';
  4420. return $json;
  4421. }
  4422. ################################################################
  4423. #
  4424. # Prepare the SQL String
  4425. #
  4426. ################################################################
  4427. sub DbLog_prepareSql(@) {
  4428. my ($hash, @a) = @_;
  4429. my $starttime = $_[5];
  4430. $starttime =~ s/_/ /;
  4431. my $endtime = $_[6];
  4432. $endtime =~ s/_/ /;
  4433. my $device = $_[7];
  4434. my $userquery = $_[8];
  4435. my $xaxis = $_[9];
  4436. my $yaxis = $_[10];
  4437. my $savename = $_[11];
  4438. my $jsonChartConfig = $_[12];
  4439. my $pagingstart = $_[13];
  4440. my $paginglimit = $_[14];
  4441. my $dbmodel = $hash->{MODEL};
  4442. my ($sql, $jsonstring, $countsql, $hourstats, $daystats, $weekstats, $monthstats, $yearstats);
  4443. if ($dbmodel eq "POSTGRESQL") {
  4444. ### POSTGRESQL Queries for Statistics ###
  4445. ### hour:
  4446. $hourstats = "SELECT to_char(timestamp, 'YYYY-MM-DD HH24:00:00') AS TIMESTAMP, SUM(VALUE::float) AS SUM, ";
  4447. $hourstats .= "AVG(VALUE::float) AS AVG, MIN(VALUE::float) AS MIN, MAX(VALUE::float) AS MAX, ";
  4448. $hourstats .= "COUNT(VALUE) AS COUNT FROM history WHERE READING = '$yaxis' AND DEVICE = '$device' ";
  4449. $hourstats .= "AND TIMESTAMP Between '$starttime' AND '$endtime' GROUP BY 1 ORDER BY 1;";
  4450. ### day:
  4451. $daystats = "SELECT to_char(timestamp, 'YYYY-MM-DD 00:00:00') AS TIMESTAMP, SUM(VALUE::float) AS SUM, ";
  4452. $daystats .= "AVG(VALUE::float) AS AVG, MIN(VALUE::float) AS MIN, MAX(VALUE::float) AS MAX, ";
  4453. $daystats .= "COUNT(VALUE) AS COUNT FROM history WHERE READING = '$yaxis' AND DEVICE = '$device' ";
  4454. $daystats .= "AND TIMESTAMP Between '$starttime' AND '$endtime' GROUP BY 1 ORDER BY 1;";
  4455. ### week:
  4456. $weekstats = "SELECT date_trunc('week',timestamp) AS TIMESTAMP, SUM(VALUE::float) AS SUM, ";
  4457. $weekstats .= "AVG(VALUE::float) AS AVG, MIN(VALUE::float) AS MIN, MAX(VALUE::float) AS MAX, ";
  4458. $weekstats .= "COUNT(VALUE) AS COUNT FROM history WHERE READING = '$yaxis' AND DEVICE = '$device' ";
  4459. $weekstats .= "AND TIMESTAMP Between '$starttime' AND '$endtime' GROUP BY 1 ORDER BY 1;";
  4460. ### month:
  4461. $monthstats = "SELECT to_char(timestamp, 'YYYY-MM-01 00:00:00') AS TIMESTAMP, SUM(VALUE::float) AS SUM, ";
  4462. $monthstats .= "AVG(VALUE::float) AS AVG, MIN(VALUE::float) AS MIN, MAX(VALUE::float) AS MAX, ";
  4463. $monthstats .= "COUNT(VALUE) AS COUNT FROM history WHERE READING = '$yaxis' AND DEVICE = '$device' ";
  4464. $monthstats .= "AND TIMESTAMP Between '$starttime' AND '$endtime' GROUP BY 1 ORDER BY 1;";
  4465. ### year:
  4466. $yearstats = "SELECT to_char(timestamp, 'YYYY-01-01 00:00:00') AS TIMESTAMP, SUM(VALUE::float) AS SUM, ";
  4467. $yearstats .= "AVG(VALUE::float) AS AVG, MIN(VALUE::float) AS MIN, MAX(VALUE::float) AS MAX, ";
  4468. $yearstats .= "COUNT(VALUE) AS COUNT FROM history WHERE READING = '$yaxis' AND DEVICE = '$device' ";
  4469. $yearstats .= "AND TIMESTAMP Between '$starttime' AND '$endtime' GROUP BY 1 ORDER BY 1;";
  4470. } elsif ($dbmodel eq "MYSQL") {
  4471. ### MYSQL Queries for Statistics ###
  4472. ### hour:
  4473. $hourstats = "SELECT date_format(timestamp, '%Y-%m-%d %H:00:00') AS TIMESTAMP, SUM(CAST(VALUE AS DECIMAL(12,4))) AS SUM, ";
  4474. $hourstats .= "AVG(CAST(VALUE AS DECIMAL(12,4))) AS AVG, MIN(CAST(VALUE AS DECIMAL(12,4))) AS MIN, ";
  4475. $hourstats .= "MAX(CAST(VALUE AS DECIMAL(12,4))) AS MAX, COUNT(VALUE) AS COUNT FROM history WHERE READING = '$yaxis' ";
  4476. $hourstats .= "AND DEVICE = '$device' AND TIMESTAMP Between '$starttime' AND '$endtime' GROUP BY 1 ORDER BY 1;";
  4477. ### day:
  4478. $daystats = "SELECT date_format(timestamp, '%Y-%m-%d 00:00:00') AS TIMESTAMP, SUM(CAST(VALUE AS DECIMAL(12,4))) AS SUM, ";
  4479. $daystats .= "AVG(CAST(VALUE AS DECIMAL(12,4))) AS AVG, MIN(CAST(VALUE AS DECIMAL(12,4))) AS MIN, ";
  4480. $daystats .= "MAX(CAST(VALUE AS DECIMAL(12,4))) AS MAX, COUNT(VALUE) AS COUNT FROM history WHERE READING = '$yaxis' ";
  4481. $daystats .= "AND DEVICE = '$device' AND TIMESTAMP Between '$starttime' AND '$endtime' GROUP BY 1 ORDER BY 1;";
  4482. ### week:
  4483. $weekstats = "SELECT date_format(timestamp, '%Y-%m-%d 00:00:00') AS TIMESTAMP, SUM(CAST(VALUE AS DECIMAL(12,4))) AS SUM, ";
  4484. $weekstats .= "AVG(CAST(VALUE AS DECIMAL(12,4))) AS AVG, MIN(CAST(VALUE AS DECIMAL(12,4))) AS MIN, ";
  4485. $weekstats .= "MAX(CAST(VALUE AS DECIMAL(12,4))) AS MAX, COUNT(VALUE) AS COUNT FROM history WHERE READING = '$yaxis' ";
  4486. $weekstats .= "AND DEVICE = '$device' AND TIMESTAMP Between '$starttime' AND '$endtime' ";
  4487. $weekstats .= "GROUP BY date_format(timestamp, '%Y-%u 00:00:00') ORDER BY 1;";
  4488. ### month:
  4489. $monthstats = "SELECT date_format(timestamp, '%Y-%m-01 00:00:00') AS TIMESTAMP, SUM(CAST(VALUE AS DECIMAL(12,4))) AS SUM, ";
  4490. $monthstats .= "AVG(CAST(VALUE AS DECIMAL(12,4))) AS AVG, MIN(CAST(VALUE AS DECIMAL(12,4))) AS MIN, ";
  4491. $monthstats .= "MAX(CAST(VALUE AS DECIMAL(12,4))) AS MAX, COUNT(VALUE) AS COUNT FROM history WHERE READING = '$yaxis' ";
  4492. $monthstats .= "AND DEVICE = '$device' AND TIMESTAMP Between '$starttime' AND '$endtime' GROUP BY 1 ORDER BY 1;";
  4493. ### year:
  4494. $yearstats = "SELECT date_format(timestamp, '%Y-01-01 00:00:00') AS TIMESTAMP, SUM(CAST(VALUE AS DECIMAL(12,4))) AS SUM, ";
  4495. $yearstats .= "AVG(CAST(VALUE AS DECIMAL(12,4))) AS AVG, MIN(CAST(VALUE AS DECIMAL(12,4))) AS MIN, ";
  4496. $yearstats .= "MAX(CAST(VALUE AS DECIMAL(12,4))) AS MAX, COUNT(VALUE) AS COUNT FROM history WHERE READING = '$yaxis' ";
  4497. $yearstats .= "AND DEVICE = '$device' AND TIMESTAMP Between '$starttime' AND '$endtime' GROUP BY 1 ORDER BY 1;";
  4498. } elsif ($dbmodel eq "SQLITE") {
  4499. ### SQLITE Queries for Statistics ###
  4500. ### hour:
  4501. $hourstats = "SELECT TIMESTAMP, SUM(CAST(VALUE AS FLOAT)) AS SUM, AVG(CAST(VALUE AS FLOAT)) AS AVG, ";
  4502. $hourstats .= "MIN(CAST(VALUE AS FLOAT)) AS MIN, MAX(CAST(VALUE AS FLOAT)) AS MAX, COUNT(VALUE) AS COUNT ";
  4503. $hourstats .= "FROM history WHERE READING = '$yaxis' AND DEVICE = '$device' ";
  4504. $hourstats .= "AND TIMESTAMP Between '$starttime' AND '$endtime' GROUP BY strftime('%Y-%m-%d %H:00:00', TIMESTAMP);";
  4505. ### day:
  4506. $daystats = "SELECT TIMESTAMP, SUM(CAST(VALUE AS FLOAT)) AS SUM, AVG(CAST(VALUE AS FLOAT)) AS AVG, ";
  4507. $daystats .= "MIN(CAST(VALUE AS FLOAT)) AS MIN, MAX(CAST(VALUE AS FLOAT)) AS MAX, COUNT(VALUE) AS COUNT ";
  4508. $daystats .= "FROM history WHERE READING = '$yaxis' AND DEVICE = '$device' ";
  4509. $daystats .= "AND TIMESTAMP Between '$starttime' AND '$endtime' GROUP BY strftime('%Y-%m-%d 00:00:00', TIMESTAMP);";
  4510. ### week:
  4511. $weekstats = "SELECT TIMESTAMP, SUM(CAST(VALUE AS FLOAT)) AS SUM, AVG(CAST(VALUE AS FLOAT)) AS AVG, ";
  4512. $weekstats .= "MIN(CAST(VALUE AS FLOAT)) AS MIN, MAX(CAST(VALUE AS FLOAT)) AS MAX, COUNT(VALUE) AS COUNT ";
  4513. $weekstats .= "FROM history WHERE READING = '$yaxis' AND DEVICE = '$device' ";
  4514. $weekstats .= "AND TIMESTAMP Between '$starttime' AND '$endtime' GROUP BY strftime('%Y-%W 00:00:00', TIMESTAMP);";
  4515. ### month:
  4516. $monthstats = "SELECT TIMESTAMP, SUM(CAST(VALUE AS FLOAT)) AS SUM, AVG(CAST(VALUE AS FLOAT)) AS AVG, ";
  4517. $monthstats .= "MIN(CAST(VALUE AS FLOAT)) AS MIN, MAX(CAST(VALUE AS FLOAT)) AS MAX, COUNT(VALUE) AS COUNT ";
  4518. $monthstats .= "FROM history WHERE READING = '$yaxis' AND DEVICE = '$device' ";
  4519. $monthstats .= "AND TIMESTAMP Between '$starttime' AND '$endtime' GROUP BY strftime('%Y-%m 00:00:00', TIMESTAMP);";
  4520. ### year:
  4521. $yearstats = "SELECT TIMESTAMP, SUM(CAST(VALUE AS FLOAT)) AS SUM, AVG(CAST(VALUE AS FLOAT)) AS AVG, ";
  4522. $yearstats .= "MIN(CAST(VALUE AS FLOAT)) AS MIN, MAX(CAST(VALUE AS FLOAT)) AS MAX, COUNT(VALUE) AS COUNT ";
  4523. $yearstats .= "FROM history WHERE READING = '$yaxis' AND DEVICE = '$device' ";
  4524. $yearstats .= "AND TIMESTAMP Between '$starttime' AND '$endtime' GROUP BY strftime('%Y 00:00:00', TIMESTAMP);";
  4525. } else {
  4526. $sql = "errordb";
  4527. }
  4528. if($userquery eq "getreadings") {
  4529. $sql = "SELECT distinct(reading) FROM history WHERE device = '".$device."'";
  4530. } elsif($userquery eq "getdevices") {
  4531. $sql = "SELECT distinct(device) FROM history";
  4532. } elsif($userquery eq "timerange") {
  4533. $sql = "SELECT ".$xaxis.", VALUE FROM history WHERE READING = '$yaxis' AND DEVICE = '$device' AND TIMESTAMP Between '$starttime' AND '$endtime' ORDER BY TIMESTAMP;";
  4534. } elsif($userquery eq "hourstats") {
  4535. $sql = $hourstats;
  4536. } elsif($userquery eq "daystats") {
  4537. $sql = $daystats;
  4538. } elsif($userquery eq "weekstats") {
  4539. $sql = $weekstats;
  4540. } elsif($userquery eq "monthstats") {
  4541. $sql = $monthstats;
  4542. } elsif($userquery eq "yearstats") {
  4543. $sql = $yearstats;
  4544. } elsif($userquery eq "savechart") {
  4545. $sql = "INSERT INTO frontend (TYPE, NAME, VALUE) VALUES ('savedchart', '$savename', '$jsonChartConfig')";
  4546. } elsif($userquery eq "renamechart") {
  4547. $sql = "UPDATE frontend SET NAME = '$savename' WHERE ID = '$jsonChartConfig'";
  4548. } elsif($userquery eq "deletechart") {
  4549. $sql = "DELETE FROM frontend WHERE TYPE = 'savedchart' AND ID = '".$savename."'";
  4550. } elsif($userquery eq "updatechart") {
  4551. $sql = "UPDATE frontend SET VALUE = '$jsonChartConfig' WHERE ID = '".$savename."'";
  4552. } elsif($userquery eq "getcharts") {
  4553. $sql = "SELECT * FROM frontend WHERE TYPE = 'savedchart'";
  4554. } elsif($userquery eq "getTableData") {
  4555. if ($device ne '""' && $yaxis ne '""') {
  4556. $sql = "SELECT * FROM history WHERE READING = '$yaxis' AND DEVICE = '$device' ";
  4557. $sql .= "AND TIMESTAMP Between '$starttime' AND '$endtime'";
  4558. $sql .= " LIMIT '$paginglimit' OFFSET '$pagingstart'";
  4559. $countsql = "SELECT count(*) FROM history WHERE READING = '$yaxis' AND DEVICE = '$device' ";
  4560. $countsql .= "AND TIMESTAMP Between '$starttime' AND '$endtime'";
  4561. } elsif($device ne '""' && $yaxis eq '""') {
  4562. $sql = "SELECT * FROM history WHERE DEVICE = '$device' ";
  4563. $sql .= "AND TIMESTAMP Between '$starttime' AND '$endtime'";
  4564. $sql .= " LIMIT '$paginglimit' OFFSET '$pagingstart'";
  4565. $countsql = "SELECT count(*) FROM history WHERE DEVICE = '$device' ";
  4566. $countsql .= "AND TIMESTAMP Between '$starttime' AND '$endtime'";
  4567. } else {
  4568. $sql = "SELECT * FROM history";
  4569. $sql .= " WHERE TIMESTAMP Between '$starttime' AND '$endtime'";
  4570. $sql .= " LIMIT '$paginglimit' OFFSET '$pagingstart'";
  4571. $countsql = "SELECT count(*) FROM history";
  4572. $countsql .= " WHERE TIMESTAMP Between '$starttime' AND '$endtime'";
  4573. }
  4574. return ($sql, $countsql);
  4575. } else {
  4576. $sql = "error";
  4577. }
  4578. return $sql;
  4579. }
  4580. ################################################################
  4581. #
  4582. # Do the query
  4583. #
  4584. ################################################################
  4585. sub DbLog_chartQuery($@) {
  4586. my ($sql, $countsql) = DbLog_prepareSql(@_);
  4587. if ($sql eq "error") {
  4588. return DbLog_jsonError("Could not setup SQL String. Maybe the Database is busy, please try again!");
  4589. } elsif ($sql eq "errordb") {
  4590. return DbLog_jsonError("The Database Type is not supported!");
  4591. }
  4592. my ($hash, @a) = @_;
  4593. my $dbhf = DbLog_ConnectNewDBH($hash);
  4594. return if(!$dbhf);
  4595. my $totalcount;
  4596. if (defined $countsql && $countsql ne "") {
  4597. my $query_handle = $dbhf->prepare($countsql)
  4598. or return DbLog_jsonError("Could not prepare statement: " . $dbhf->errstr . ", SQL was: " .$countsql);
  4599. $query_handle->execute()
  4600. or return DbLog_jsonError("Could not execute statement: " . $query_handle->errstr);
  4601. my @data = $query_handle->fetchrow_array();
  4602. $totalcount = join(", ", @data);
  4603. }
  4604. # prepare the query
  4605. my $query_handle = $dbhf->prepare($sql)
  4606. or return DbLog_jsonError("Could not prepare statement: " . $dbhf->errstr . ", SQL was: " .$sql);
  4607. # execute the query
  4608. $query_handle->execute()
  4609. or return DbLog_jsonError("Could not execute statement: " . $query_handle->errstr);
  4610. my $columns = $query_handle->{'NAME'};
  4611. my $columncnt;
  4612. # When columns are empty but execution was successful, we have done a successful INSERT, UPDATE or DELETE
  4613. if($columns) {
  4614. $columncnt = scalar @$columns;
  4615. } else {
  4616. return '{"success": "true", "msg":"All ok"}';
  4617. }
  4618. my $i = 0;
  4619. my $jsonstring = '{"data":[';
  4620. while ( my @data = $query_handle->fetchrow_array()) {
  4621. if($i == 0) {
  4622. $jsonstring .= '{';
  4623. } else {
  4624. $jsonstring .= ',{';
  4625. }
  4626. for ($i = 0; $i < $columncnt; $i++) {
  4627. $jsonstring .= '"';
  4628. $jsonstring .= uc($query_handle->{NAME}->[$i]);
  4629. $jsonstring .= '":';
  4630. if (defined $data[$i]) {
  4631. my $fragment = substr($data[$i],0,1);
  4632. if ($fragment eq "{") {
  4633. $jsonstring .= $data[$i];
  4634. } else {
  4635. $jsonstring .= '"'.$data[$i].'"';
  4636. }
  4637. } else {
  4638. $jsonstring .= '""'
  4639. }
  4640. if($i != ($columncnt -1)) {
  4641. $jsonstring .= ',';
  4642. }
  4643. }
  4644. $jsonstring .= '}';
  4645. }
  4646. $dbhf->disconnect();
  4647. $jsonstring .= ']';
  4648. if (defined $totalcount && $totalcount ne "") {
  4649. $jsonstring .= ',"totalCount": '.$totalcount.'}';
  4650. } else {
  4651. $jsonstring .= '}';
  4652. }
  4653. return $jsonstring;
  4654. }
  4655. #
  4656. # get <dbLog> ReadingsVal <device> <reading> <default>
  4657. # get <dbLog> ReadingsTimestamp <device> <reading> <default>
  4658. #
  4659. sub DbLog_dbReadings($@) {
  4660. my($hash,@a) = @_;
  4661. my $dbhf = DbLog_ConnectNewDBH($hash);
  4662. return if(!$dbhf);
  4663. return 'Wrong Syntax for ReadingsVal!' unless defined($a[4]);
  4664. my $DbLogType = AttrVal($a[0],'DbLogType','current');
  4665. my $query;
  4666. if (lc($DbLogType) =~ m(current) ) {
  4667. $query = "select VALUE,TIMESTAMP from current where DEVICE= '$a[2]' and READING= '$a[3]'";
  4668. } else {
  4669. $query = "select VALUE,TIMESTAMP from history where DEVICE= '$a[2]' and READING= '$a[3]' order by TIMESTAMP desc limit 1";
  4670. }
  4671. my ($reading,$timestamp) = $dbhf->selectrow_array($query);
  4672. $dbhf->disconnect();
  4673. $reading = (defined($reading)) ? $reading : $a[4];
  4674. $timestamp = (defined($timestamp)) ? $timestamp : $a[4];
  4675. return $reading if $a[1] eq 'ReadingsVal';
  4676. return $timestamp if $a[1] eq 'ReadingsTimestamp';
  4677. return "Syntax error: $a[1]";
  4678. }
  4679. 1;
  4680. =pod
  4681. =item helper
  4682. =item summary logs events into a database
  4683. =item summary_DE loggt Events in eine Datenbank
  4684. =begin html
  4685. <a name="DbLog"></a>
  4686. <h3>DbLog</h3>
  4687. <ul>
  4688. <br>
  4689. With DbLog events can be stored in a database. SQLite, MySQL/MariaDB and PostgreSQL are supported databases. <br><br>
  4690. <b>Prereqisites</b> <br><br>
  4691. The Perl-modules <code>DBI</code> and <code>DBD::&lt;dbtype&gt;</code> are needed to be installed (use <code>cpan -i &lt;module&gt;</code>
  4692. if your distribution does not have it).
  4693. <br><br>
  4694. On a debian based system you may install these modules for instance by: <br><br>
  4695. <ul>
  4696. <table>
  4697. <colgroup> <col width=5%> <col width=95%> </colgroup>
  4698. <tr><td> <b>DBI</b> </td><td>: <code> sudo apt-get install libdbi-perl </code> </td></tr>
  4699. <tr><td> <b>MySQL</b> </td><td>: <code> sudo apt-get install [mysql-server] mysql-client libdbd-mysql libdbd-mysql-perl </code> (mysql-server only if you use a local MySQL-server installation) </td></tr>
  4700. <tr><td> <b>SQLite</b> </td><td>: <code> sudo apt-get install sqlite3 libdbi-perl libdbd-sqlite3-perl </code> </td></tr>
  4701. <tr><td> <b>PostgreSQL</b> </td><td>: <code> sudo apt-get install libdbd-pg-perl </code> </td></tr>
  4702. </table>
  4703. </ul>
  4704. <br>
  4705. <br>
  4706. <b>Preparations</b> <br><br>
  4707. At first you need to setup the database. <br>
  4708. Sample code and Scripts to prepare a MySQL/PostgreSQL/SQLite database you can find in
  4709. <a href="https://svn.fhem.de/trac/browser/trunk/fhem/contrib/dblog">SVN -&gt; contrib/dblog/db_create_&lt;DBType&gt;.sql</a>. <br>
  4710. (<b>Caution:</b> The local FHEM-Installation subdirectory ./contrib/dblog doesn't contain the freshest scripts !!)
  4711. <br><br>
  4712. The database contains two tables: <code>current</code> and <code>history</code>. <br>
  4713. The latter contains all events whereas the former only contains the last event for any given reading and device.
  4714. Please consider the <a href="#DbLogattr">attribute</a> DbLogType implicitly to determine the usage of tables
  4715. <code>current</code> and <code>history</code>.
  4716. <br><br>
  4717. The columns have the following meaning: <br><br>
  4718. <ul>
  4719. <table>
  4720. <colgroup> <col width=5%> <col width=95%> </colgroup>
  4721. <tr><td> TIMESTAMP </td><td>: timestamp of event, e.g. <code>2007-12-30 21:45:22</code> </td></tr>
  4722. <tr><td> DEVICE </td><td>: device name, e.g. <code>Wetterstation</code> </td></tr>
  4723. <tr><td> TYPE </td><td>: device type, e.g. <code>KS300</code> </td></tr>
  4724. <tr><td> EVENT </td><td>: event specification as full string, e.g. <code>humidity: 71 (%)</code> </td></tr>
  4725. <tr><td> READING </td><td>: name of reading extracted from event, e.g. <code>humidity</code> </td></tr>
  4726. <tr><td> VALUE </td><td>: actual reading extracted from event, e.g. <code>71</code> </td></tr>
  4727. <tr><td> UNIT </td><td>: unit extracted from event, e.g. <code>%</code> </td></tr>
  4728. </table>
  4729. </ul>
  4730. <br>
  4731. <br>
  4732. <b>create index</b> <br>
  4733. Due to reading performance, e.g. on creation of SVG-plots, it is very important that the <b>index "Search_Idx"</b>
  4734. or a comparable index (e.g. a primary key) is applied.
  4735. A sample code for creation of that index is also available in mentioned scripts of
  4736. <a href="https://svn.fhem.de/trac/browser/trunk/fhem/contrib/dblog">SVN -&gt; contrib/dblog/db_create_&lt;DBType&gt;.sql</a>.
  4737. <br><br>
  4738. The index "Search_Idx" can be created, e.g. in database 'fhem', by these statements (also subsequently): <br><br>
  4739. <ul>
  4740. <table>
  4741. <colgroup> <col width=5%> <col width=95%> </colgroup>
  4742. <tr><td> <b>MySQL</b> </td><td>: <code> CREATE INDEX Search_Idx ON `fhem`.`history` (DEVICE, READING, TIMESTAMP); </code> </td></tr>
  4743. <tr><td> <b>SQLite</b> </td><td>: <code> CREATE INDEX Search_Idx ON `history` (DEVICE, READING, TIMESTAMP); </code> </td></tr>
  4744. <tr><td> <b>PostgreSQL</b> </td><td>: <code> CREATE INDEX "Search_Idx" ON history USING btree (device, reading, "timestamp"); </code> </td></tr>
  4745. </table>
  4746. </ul>
  4747. <br>
  4748. For the connection to the database a <b>configuration file</b> is used.
  4749. The configuration is stored in a separate file to avoid storing the password in the main configuration file and to have it
  4750. visible in the output of the <a href="https://fhem.de/commandref.html#list">list</a> command.
  4751. <br><br>
  4752. The <b>configuration file</b> should be copied e.g. to /opt/fhem and has the following structure you have to customize
  4753. suitable to your conditions (decomment the appropriate raws and adjust it): <br><br>
  4754. <pre>
  4755. ####################################################################################
  4756. # database configuration file
  4757. #
  4758. # NOTE:
  4759. # If you don't use a value for user / password please delete the leading hash mark
  4760. # and write 'user => ""' respectively 'password => ""' instead !
  4761. #
  4762. #
  4763. ## for MySQL
  4764. ####################################################################################
  4765. #%dbconfig= (
  4766. # connection => "mysql:database=fhem;host=&lt;database host&gt;;port=3306",
  4767. # user => "fhemuser",
  4768. # password => "fhempassword",
  4769. # # optional enable(1) / disable(0) UTF-8 support (at least V 4.042 is necessary)
  4770. # utf8 => 1
  4771. #);
  4772. ####################################################################################
  4773. #
  4774. ## for PostgreSQL
  4775. ####################################################################################
  4776. #%dbconfig= (
  4777. # connection => "Pg:database=fhem;host=&lt;database host&gt;",
  4778. # user => "fhemuser",
  4779. # password => "fhempassword"
  4780. #);
  4781. ####################################################################################
  4782. #
  4783. ## for SQLite (username and password stay empty for SQLite)
  4784. ####################################################################################
  4785. #%dbconfig= (
  4786. # connection => "SQLite:dbname=/opt/fhem/fhem.db",
  4787. # user => "",
  4788. # password => ""
  4789. #);
  4790. ####################################################################################
  4791. </pre>
  4792. If configDB is used, the configuration file has to be uploaded into the configDB ! <br><br>
  4793. <b>Note about special characters:</b><br>
  4794. If special characters, e.g. @,$ or % which have a meaning in the perl programming
  4795. language are used in a password, these special characters have to be escaped.
  4796. That means in this example you have to use: \@,\$ respectively \%.
  4797. <br>
  4798. <br>
  4799. <br>
  4800. <a name="DbLogdefine"></a>
  4801. <b>Define</b>
  4802. <ul>
  4803. <br>
  4804. <code>define &lt;name&gt; DbLog &lt;configfilename&gt; &lt;regexp&gt;</code>
  4805. <br><br>
  4806. <code>&lt;configfilename&gt;</code> is the prepared <b>configuration file</b>. <br>
  4807. <code>&lt;regexp&gt;</code> is identical to the specification of regex in the <a href="https://fhem.de/commandref.html#FileLog">FileLog</a> definition.
  4808. <br><br>
  4809. <b>Example:</b>
  4810. <ul>
  4811. <code>define myDbLog DbLog /etc/fhem/db.conf .*:.*</code><br>
  4812. all events will stored into the database
  4813. </ul>
  4814. <br>
  4815. After you have defined your DbLog-device it is recommended to run the <b>configuration check</b> <br><br>
  4816. <ul>
  4817. <code>set &lt;name&gt; configCheck</code> <br>
  4818. </ul>
  4819. <br>
  4820. This check reports some important settings and gives recommendations back to you if proposals are indentified.
  4821. <br><br>
  4822. DbLog distinguishes between the synchronous (default) and asynchronous logmode. The logmode is adjustable by the
  4823. <a href="#DbLogattr">attribute</a> asyncMode. Since version 2.13.5 DbLog is supporting primary key (PK) set in table
  4824. current or history. If you want use PostgreSQL with PK it has to be at lest version 9.5.
  4825. <br><br>
  4826. The content of VALUE will be optimized for automated post-processing, e.g. <code>yes</code> is translated to <code>1</code>
  4827. <br><br>
  4828. The stored values can be retrieved by the following code like FileLog:<br>
  4829. <ul>
  4830. <code>get myDbLog - - 2012-11-10 2012-11-10 KS300:temperature::</code>
  4831. </ul>
  4832. <br>
  4833. <b>transfer FileLog-data to DbLog </b> <br><br>
  4834. There is the special module 98_FileLogConvert.pm available to transfer filelog-data to the DbLog-database. <br>
  4835. The module can be downloaded <a href="https://svn.fhem.de/trac/browser/trunk/fhem/contrib/98_FileLogConvert.pm"> here</a>
  4836. or from directory ./contrib instead.
  4837. Further informations and help you can find in the corresponding <a href="https://forum.fhem.de/index.php/topic,66383.0.html">
  4838. Forumthread </a>. <br><br><br>
  4839. <b>Reporting and Management of DbLog database content</b> <br><br>
  4840. By using <a href="https://fhem.de/commandref.html#SVG">SVG</a> database content can be visualized. <br>
  4841. Beyond that the module <a href="https://fhem.de/commandref.html#DbRep">DbRep</a> can be used to prepare tabular
  4842. database reports or you can manage the database content with available functions of that module.
  4843. <br><br><br>
  4844. <b>Troubleshooting</b> <br><br>
  4845. If after successful definition the DbLog-device doesn't work as expected, the following notes may help:
  4846. <br><br>
  4847. <ul>
  4848. <li> Have the preparatory steps as described in commandref been done ? (install software components, create tables and index) </li>
  4849. <li> Was "set &lt;name&gt; configCheck" executed after definition and potential errors fixed or rather the hints implemented ? </li>
  4850. <li> If configDB is used ... has the database configuration file been imported into configDB (e.g. by "configDB fileimport ./db.conf") ? </li>
  4851. <li> When creating a SVG-plot and no drop-down list with proposed values appear -> set attribute "DbLogType" to "Current/History". </li>
  4852. </ul>
  4853. <br>
  4854. If the notes don't lead to success, please increase verbose level of the DbLog-device to 4 or 5 and observe entries in
  4855. logfile relating to the DbLog-device.
  4856. For problem analysis please post the output of "list &lt;name&gt;", the result of "set &lt;name&gt; configCheck" and the
  4857. logfile entries of DbLog-device to the forum thread.
  4858. <br><br>
  4859. </ul>
  4860. <br>
  4861. <br>
  4862. <a name="DbLogset"></a>
  4863. <b>Set</b>
  4864. <ul>
  4865. <code>set &lt;name&gt; addCacheLine YYYY-MM-DD HH:MM:SS|&lt;device&gt;|&lt;type&gt;|&lt;event&gt;|&lt;reading&gt;|&lt;value&gt;|[&lt;unit&gt;] </code><br><br>
  4866. <ul> In asynchronous mode a new dataset is inserted to the Cache and will be processed at the next database sync cycle.
  4867. <br><br>
  4868. <b>Example:</b> <br>
  4869. set &lt;name&gt; addCacheLine 2017-12-05 17:03:59|MaxBathRoom|MAX|valveposition: 95|valveposition|95|% <br>
  4870. </ul><br>
  4871. <code>set &lt;name&gt; addLog &lt;devspec&gt;:&lt;Reading&gt; [Value] [CN=&lt;caller name&gt;] [!useExcludes] </code><br><br>
  4872. <ul> Inserts an additional log entry of a device/reading combination into the database. <br><br>
  4873. <ul>
  4874. <li> <b>&lt;devspec&gt;:&lt;Reading&gt;</b> - The device can be declared by a <a href="#devspec">device specification
  4875. (devspec)</a>. "Reading" will be evaluated as regular expression. If
  4876. The reading isn't available and the value "Value" is specified, the
  4877. reading will be added to database as new one if it isn't a regular
  4878. expression and the readingname is valid. </li>
  4879. <li> <b>Value</b> - Optionally you can enter a "Value" that is used as reading value in the dataset. If the value isn't
  4880. specified (default), the current value of the specified reading will be inserted into the database. </li>
  4881. <li> <b>CN=&lt;caller name&gt;</b> - By the key "CN=" (<b>C</b>aller <b>N</b>ame) you can specify an additional string,
  4882. e.g. the name of a calling device (for example an at- or notify-device).
  4883. Via the function defined in <a href="#DbLogattr">attribute</a> "valueFn" this key can be analyzed
  4884. by the variable $CN. Thereby it is possible to control the behavior of the addLog dependend from
  4885. the calling source. </li>
  4886. <li> <b>!useExcludes</b> - The function considers attribute "DbLogExclude" in the source device if it is set. If the optional
  4887. keyword "!useExcludes" is set, the attribute "DbLogExclude" isn't considered. </li>
  4888. </ul>
  4889. <br>
  4890. The database field "EVENT" will be filled with the string "addLog" automatically. <br>
  4891. The addLog-command dosn't create an additional event in your system !<br><br>
  4892. <b>Examples:</b> <br>
  4893. set &lt;name&gt; addLog SMA_Energymeter:Bezug_Wirkleistung <br>
  4894. set &lt;name&gt; addLog TYPE=SSCam:state <br>
  4895. set &lt;name&gt; addLog MyWetter:(fc10.*|fc8.*) <br>
  4896. set &lt;name&gt; addLog MyWetter:(wind|wind_ch.*) 20 !useExcludes <br>
  4897. set &lt;name&gt; addLog TYPE=CUL_HM:FILTER=model=HM-CC-RT-DN:FILTER=subType!=(virtual|):(measured-temp|desired-temp|actuator) <br><br>
  4898. set &lt;name&gt; addLog USV:state CN=di.cronjob <br>
  4899. In the valueFn-function the caller "di.cronjob" is evaluated via the variable $CN and the timestamp is corrected: <br><br>
  4900. valueFn = if($CN eq "di.cronjob" and $TIMESTAMP =~ m/\s00:00:[\d:]+/) { $TIMESTAMP =~ s/\s([^\s]+)/ 23:59:59/ }
  4901. </ul><br>
  4902. <code>set &lt;name&gt; clearReadings </code><br><br>
  4903. <ul> This function clears readings which were created by different DbLog-functions. </ul><br>
  4904. <code>set &lt;name&gt; commitCache </code><br><br>
  4905. <ul>In asynchronous mode (<a href="#DbLogattr">attribute</a> asyncMode=1), the cached data in memory will be written into the database
  4906. and subsequently the cache will be cleared. Thereby the internal timer for the asynchronous mode Modus will be set new.
  4907. The command can be usefull in case of you want to write the cached data manually or e.g. by an AT-device on a defined
  4908. point of time into the database. </ul><br>
  4909. <code>set &lt;name&gt; configCheck </code><br><br>
  4910. <ul>This command checks some important settings and give recommendations back to you if proposals are identified.
  4911. </ul><br>
  4912. <code>set &lt;name&gt; count </code><br/><br/>
  4913. <ul>Count records in tables current and history and write results into readings countCurrent and countHistory.</ul><br/>
  4914. <code>set &lt;name&gt; countNbl </code><br/><br/>
  4915. <ul>The non-blocking execution of "set &lt;name&gt; count".</ul><br/>
  4916. <code>set &lt;name&gt; deleteOldDays &lt;n&gt;</code><br/><br/>
  4917. <ul>Delete records from history older than &lt;n&gt; days. Number of deleted records will be written into reading
  4918. lastRowsDeleted.
  4919. </ul><br>
  4920. <code>set &lt;name&gt; deleteOldDaysNbl &lt;n&gt;</code><br/><br/>
  4921. <ul>
  4922. Is identical to function "deleteOldDays" whereupon deleteOldDaysNbl will be executed non-blocking.
  4923. <br><br>
  4924. <b>Note:</b> <br>
  4925. Even though the function itself is non-blocking, you have to set DbLog into the asynchronous mode (attr asyncMode = 1) to
  4926. avoid a blocking situation of FHEM !
  4927. </ul>
  4928. <br>
  4929. <code>set &lt;name&gt; eraseReadings </code><br><br>
  4930. <ul> This function deletes all readings except reading "state". </ul><br>
  4931. <a name="DbLogsetexportCache"></a>
  4932. <code>set &lt;name&gt; exportCache [nopurge | purgecache] </code><br><br>
  4933. <ul>If DbLog is operating in asynchronous mode, it's possible to exoprt the cache content into a textfile.
  4934. The file will be written to the directory (global->modpath)/log/ by default setting. The detination directory can be
  4935. changed by the <a href="#DbLogattr">attribute</a> expimpdir. <br>
  4936. The filename will be generated automatically and is built by a prefix "cache_", followed by DbLog-devicename and the
  4937. present timestmp, e.g. "cache_LogDB_2017-03-23_22-13-55". <br>
  4938. There are two options possible, "nopurge" respectively "purgecache". The option determines whether the cache content
  4939. will be deleted after export or not.
  4940. Using option "nopurge" (default) the cache content will be preserved. <br>
  4941. The <a href="#DbLogattr">attribute</a> "exportCacheAppend" defines, whether every export process creates a new export file
  4942. (default) or the cache content is appended to an existing (newest) export file.
  4943. </ul><br>
  4944. <code>set &lt;name&gt; importCachefile &lt;file&gt; </code><br><br>
  4945. <ul>Imports an textfile into the database which has been written by the "exportCache" function.
  4946. The allocatable files will be searched in directory (global->modpath)/log/ by default and a drop-down list will be
  4947. generated from the files which are found in the directory.
  4948. The source directory can be changed by the <a href="#DbLogattr">attribute</a> expimpdir. <br>
  4949. Only that files will be shown which are correlate on pattern starting with "cache_", followed by the DbLog-devicename. <br>
  4950. For example a file with the name "cache_LogDB_2017-03-23_22-13-55", will match if Dblog-device has name "LogDB". <br>
  4951. After the import has been successfully done, a prefix "impdone_" will be added at begin of the filename and this file
  4952. ddoesn't appear on the drop-down list anymore. <br>
  4953. If you want to import a cachefile from another source database, you may adapt the filename so it fits the search criteria
  4954. "DbLog-Device" in its name. After renaming the file appeares again on the drop-down list. </ul><br>
  4955. <code>set &lt;name&gt; listCache </code><br><br>
  4956. <ul>If DbLog is set to asynchronous mode (attribute asyncMode=1), you can use that command to list the events are cached in memory.</ul><br>
  4957. <code>set &lt;name&gt; purgeCache </code><br><br>
  4958. <ul>In asynchronous mode (<a href="#DbLogattr">attribute</a> asyncMode=1), the in memory cached data will be deleted.
  4959. With this command data won't be written from cache into the database. </ul><br>
  4960. <code>set &lt;name&gt; reduceLog &lt;no&gt;[:&lt;nn&gt;] [average[=day]] [exclude=device1:reading1,device2:reading2,...]</code> <br><br>
  4961. <ul>Reduces records older than &lt;no&gt; days and (optional) newer than &lt;nn&gt; days to one record (the 1st) each hour per device & reading. <br>
  4962. Within the device/reading name <b>SQL-Wildcards "%" and "_"</b> can be used. <br><br>
  4963. With the optional argument 'average' not only the records will be reduced, but all numerical values of an hour
  4964. will be reduced to a single average. <br>
  4965. With the optional argument 'average=day' not only the records will be reduced, but all numerical values of a
  4966. day will be reduced to a single average. (implies 'average') <br><br>
  4967. You can optional set the last argument to "exclude=device1:reading1,device2:reading2,..." to exclude
  4968. device/readings from reduceLog. <br>
  4969. Also you can optional set the last argument to "include=device:reading" to delimit the SELECT statement which
  4970. is executed on the database. This may reduce the system RAM load and increases the performance. <br><br>
  4971. <ul>
  4972. <b>Example: </b> <br>
  4973. set &lt;name&gt; reduceLog 270 average include=Luftdaten_remote:% <br>
  4974. </ul>
  4975. <br>
  4976. <b>CAUTION:</b> It is strongly recommended to check if the default INDEX 'Search_Idx' exists on the table 'history'! <br>
  4977. The execution of this command may take (without INDEX) extremely long. FHEM will be <b>blocked completely</b> after issuing the command to completion ! <br><br>
  4978. </ul><br>
  4979. <code>set &lt;name&gt; reduceLogNbl &lt;no&gt;[:&lt;nn&gt;] [average[=day]] [exclude=device1:reading1,device2:reading2,...]</code> <br><br>
  4980. <ul>Same function as "set &lt;name&gt; reduceLog" but FHEM won't be blocked due to this function is implemented
  4981. non-blocking ! <br><br>
  4982. <b>Note:</b> <br>
  4983. Even though the function itself is non-blocking, you have to set DbLog into the asynchronous mode (attr asyncMode = 1) to
  4984. avoid a blocking situation of FHEM !
  4985. </ul><br>
  4986. <code>set &lt;name&gt; reopen [n] </code><br/><br/>
  4987. <ul>Perform a database disconnect and immediate reconnect to clear cache and flush journal file if no time [n] was set. <br>
  4988. If optionally a delay time of [n] seconds was set, the database connection will be disconnect immediately but it was only reopened
  4989. after [n] seconds. In synchronous mode the events won't saved during that time. In asynchronous mode the events will be
  4990. stored in the memory cache and saved into database after the reconnect was done. </ul><br/>
  4991. <code>set &lt;name&gt; rereadcfg </code><br/><br/>
  4992. <ul>Perform a database disconnect and immediate reconnect to clear cache and flush journal file.<br/>
  4993. Probably same behavior als reopen, but rereadcfg will read the configuration data before reconnect.</ul><br/>
  4994. <code>set &lt;name&gt; userCommand &lt;validSqlStatement&gt;</code><br/><br/>
  4995. <ul><b>DO NOT USE THIS COMMAND UNLESS YOU REALLY (REALLY!) KNOW WHAT YOU ARE DOING!!!</b><br/><br/>
  4996. Performs any (!!!) sql statement on connected database. Usercommand and result will be written into
  4997. corresponding readings.</br>
  4998. The result can only be a single line. If the SQL-Statement seems to deliver a multiline result, it can be suitable
  4999. to use the analysis module <a href=https://fhem.de/commandref.html#DbRep>DbRep</a>.</br>
  5000. If the database interface delivers no result (undef), the reading "userCommandResult" contains the message
  5001. "no result".
  5002. </ul><br/>
  5003. </ul><br>
  5004. <a name="DbLogget"></a>
  5005. <b>Get</b>
  5006. <ul>
  5007. <code>get &lt;name&gt; ReadingsVal&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;device&gt; &lt;reading&gt; &lt;default&gt;</code><br/>
  5008. <code>get &lt;name&gt; ReadingsTimestamp &lt;device&gt; &lt;reading&gt; &lt;default&gt;</code><br/>
  5009. <br/>
  5010. Retrieve one single value, use and syntax are similar to ReadingsVal() and ReadingsTimestamp() functions.<br/>
  5011. </ul>
  5012. <br/>
  5013. <br/>
  5014. <ul>
  5015. <code>get &lt;name&gt; &lt;infile&gt; &lt;outfile&gt; &lt;from&gt;
  5016. &lt;to&gt; &lt;column_spec&gt; </code>
  5017. <br><br>
  5018. Read data from the Database, used by frontends to plot data without direct
  5019. access to the Database.<br>
  5020. <ul>
  5021. <li>&lt;in&gt;<br>
  5022. A dummy parameter for FileLog compatibility. Sessing by defaultto <code>-</code><br>
  5023. <ul>
  5024. <li>current: reading actual readings from table "current"</li>
  5025. <li>history: reading history readings from table "history"</li>
  5026. <li>-: identical to "history"</li>
  5027. </ul>
  5028. </li>
  5029. <li>&lt;out&gt;<br>
  5030. A dummy parameter for FileLog compatibility. Setting by default to <code>-</code>
  5031. to check the output for plot-computing.<br>
  5032. Set it to the special keyword
  5033. <code>all</code> to get all columns from Database.
  5034. <ul>
  5035. <li>ALL: get all colums from table, including a header</li>
  5036. <li>Array: get the columns as array of hashes</li>
  5037. <li>INT: internally used by generating plots</li>
  5038. <li>-: default</li>
  5039. </ul>
  5040. </li>
  5041. <li>&lt;from&gt; / &lt;to&gt;<br>
  5042. Used to select the data. Please use the following timeformat or
  5043. an initial substring of it:<br>
  5044. <ul><code>YYYY-MM-DD_HH24:MI:SS</code></ul></li>
  5045. <li>&lt;column_spec&gt;<br>
  5046. For each column_spec return a set of data separated by
  5047. a comment line on the current connection.<br>
  5048. Syntax: &lt;device&gt;:&lt;reading&gt;:&lt;default&gt;:&lt;fn&gt;:&lt;regexp&gt;<br>
  5049. <ul>
  5050. <li>&lt;device&gt;<br>
  5051. The name of the device. Case sensitive. Using a the joker "%" is supported.</li>
  5052. <li>&lt;reading&gt;<br>
  5053. The reading of the given device to select. Case sensitive. Using a the joker "%" is supported.
  5054. </li>
  5055. <li>&lt;default&gt;<br>
  5056. no implemented yet
  5057. </li>
  5058. <li>&lt;fn&gt;
  5059. One of the following:
  5060. <ul>
  5061. <li>int<br>
  5062. Extract the integer at the beginning of the string. Used e.g.
  5063. for constructs like 10%</li>
  5064. <li>int&lt;digit&gt;<br>
  5065. Extract the decimal digits including negative character and
  5066. decimal point at the beginning og the string. Used e.g.
  5067. for constructs like 15.7&deg;C</li>
  5068. <li>delta-h / delta-d<br>
  5069. Return the delta of the values for a given hour or a given day.
  5070. Used if the column contains a counter, as is the case for the
  5071. KS300 rain column.</li>
  5072. <li>delta-ts<br>
  5073. Replaced the original value with a measured value of seconds since
  5074. the last and the actual logentry.
  5075. </li>
  5076. </ul></li>
  5077. <li>&lt;regexp&gt;<br>
  5078. The string is evaluated as a perl expression. The regexp is executed
  5079. before &lt;fn&gt; parameter.<br>
  5080. Note: The string/perl expression cannot contain spaces,
  5081. as the part after the space will be considered as the
  5082. next column_spec.<br>
  5083. <b>Keywords</b>
  5084. <li>$val is the current value returned from the Database.</li>
  5085. <li>$ts is the current timestamp returned from the Database.</li>
  5086. <li>This Logentry will not print out if $val contains th keyword "hide".</li>
  5087. <li>This Logentry will not print out and not used in the following processing
  5088. if $val contains th keyword "ignore".</li>
  5089. </li>
  5090. </ul></li>
  5091. </ul>
  5092. <br><br>
  5093. Examples:
  5094. <ul>
  5095. <li><code>get myDbLog - - 2012-11-10 2012-11-20 KS300:temperature</code></li>
  5096. <li><code>get myDbLog current ALL - - %:temperature</code></li><br>
  5097. you will get all actual readings "temperature" from all logged devices.
  5098. Be careful by using "history" as inputfile because a long execution time will be expected!
  5099. <li><code>get myDbLog - - 2012-11-10_10 2012-11-10_20 KS300:temperature::int1</code><br>
  5100. like from 10am until 08pm at 10.11.2012</li>
  5101. <li><code>get myDbLog - all 2012-11-10 2012-11-20 KS300:temperature</code></li>
  5102. <li><code>get myDbLog - - 2012-11-10 2012-11-20 KS300:temperature KS300:rain::delta-h KS300:rain::delta-d</code></li>
  5103. <li><code>get myDbLog - - 2012-11-10 2012-11-20 MyFS20:data:::$val=~s/(on|off).*/$1eq"on"?1:0/eg</code><br>
  5104. return 1 for all occurance of on* (on|on-for-timer etc) and 0 for all off*</li>
  5105. <li><code>get myDbLog - - 2012-11-10 2012-11-20 Bodenfeuchte:data:::$val=~s/.*B:\s([-\.\d]+).*/$1/eg</code><br>
  5106. Example of OWAD: value like this: <code>"A: 49.527 % B: 66.647 % C: 9.797 % D: 0.097 V"</code><br>
  5107. and output for port B is like this: <code>2012-11-20_10:23:54 66.647</code></li>
  5108. <li><code>get DbLog - - 2013-05-26 2013-05-28 Pumpe:data::delta-ts:$val=~s/on/hide/</code><br>
  5109. Setting up a "Counter of Uptime". The function delta-ts gets the seconds between the last and the
  5110. actual logentry. The keyword "hide" will hide the logentry of "on" because this time
  5111. is a "counter of Downtime"</li>
  5112. </ul>
  5113. <br><br>
  5114. </ul>
  5115. <b>Get</b> when used for webcharts
  5116. <ul>
  5117. <code>get &lt;name&gt; &lt;infile&gt; &lt;outfile&gt; &lt;from&gt;
  5118. &lt;to&gt; &lt;device&gt; &lt;querytype&gt; &lt;xaxis&gt; &lt;yaxis&gt; &lt;savename&gt; </code>
  5119. <br><br>
  5120. Query the Database to retrieve JSON-Formatted Data, which is used by the charting frontend.
  5121. <br>
  5122. <ul>
  5123. <li>&lt;name&gt;<br>
  5124. The name of the defined DbLog, like it is given in fhem.cfg.</li>
  5125. <li>&lt;in&gt;<br>
  5126. A dummy parameter for FileLog compatibility. Always set to <code>-</code></li>
  5127. <li>&lt;out&gt;<br>
  5128. A dummy parameter for FileLog compatibility. Set it to <code>webchart</code>
  5129. to use the charting related get function.
  5130. </li>
  5131. <li>&lt;from&gt; / &lt;to&gt;<br>
  5132. Used to select the data. Please use the following timeformat:<br>
  5133. <ul><code>YYYY-MM-DD_HH24:MI:SS</code></ul></li>
  5134. <li>&lt;device&gt;<br>
  5135. A string which represents the device to query.</li>
  5136. <li>&lt;querytype&gt;<br>
  5137. A string which represents the method the query should use. Actually supported values are: <br>
  5138. <code>getreadings</code> to retrieve the possible readings for a given device<br>
  5139. <code>getdevices</code> to retrieve all available devices<br>
  5140. <code>timerange</code> to retrieve charting data, which requires a given xaxis, yaxis, device, to and from<br>
  5141. <code>savechart</code> to save a chart configuration in the database. Requires a given xaxis, yaxis, device, to and from, and a 'savename' used to save the chart<br>
  5142. <code>deletechart</code> to delete a saved chart. Requires a given id which was set on save of the chart<br>
  5143. <code>getcharts</code> to get a list of all saved charts.<br>
  5144. <code>getTableData</code> to get jsonformatted data from the database. Uses paging Parameters like start and limit.<br>
  5145. <code>hourstats</code> to get statistics for a given value (yaxis) for an hour.<br>
  5146. <code>daystats</code> to get statistics for a given value (yaxis) for a day.<br>
  5147. <code>weekstats</code> to get statistics for a given value (yaxis) for a week.<br>
  5148. <code>monthstats</code> to get statistics for a given value (yaxis) for a month.<br>
  5149. <code>yearstats</code> to get statistics for a given value (yaxis) for a year.<br>
  5150. </li>
  5151. <li>&lt;xaxis&gt;<br>
  5152. A string which represents the xaxis</li>
  5153. <li>&lt;yaxis&gt;<br>
  5154. A string which represents the yaxis</li>
  5155. <li>&lt;savename&gt;<br>
  5156. A string which represents the name a chart will be saved with</li>
  5157. <li>&lt;chartconfig&gt;<br>
  5158. A jsonstring which represents the chart to save</li>
  5159. <li>&lt;pagingstart&gt;<br>
  5160. An integer used to determine the start for the sql used for query 'getTableData'</li>
  5161. <li>&lt;paginglimit&gt;<br>
  5162. An integer used to set the limit for the sql used for query 'getTableData'</li>
  5163. </ul>
  5164. <br><br>
  5165. Examples:
  5166. <ul>
  5167. <li><code>get logdb - webchart "" "" "" getcharts</code><br>
  5168. Retrieves all saved charts from the Database</li>
  5169. <li><code>get logdb - webchart "" "" "" getdevices</code><br>
  5170. Retrieves all available devices from the Database</li>
  5171. <li><code>get logdb - webchart "" "" ESA2000_LED_011e getreadings</code><br>
  5172. Retrieves all available Readings for a given device from the Database</li>
  5173. <li><code>get logdb - webchart 2013-02-11_00:00:00 2013-02-12_00:00:00 ESA2000_LED_011e timerange TIMESTAMP day_kwh</code><br>
  5174. Retrieves charting data, which requires a given xaxis, yaxis, device, to and from<br>
  5175. Will ouput a JSON like this: <code>[{'TIMESTAMP':'2013-02-11 00:10:10','VALUE':'0.22431388090756'},{'TIMESTAMP'.....}]</code></li>
  5176. <li><code>get logdb - webchart 2013-02-11_00:00:00 2013-02-12_00:00:00 ESA2000_LED_011e savechart TIMESTAMP day_kwh tageskwh</code><br>
  5177. Will save a chart in the database with the given name and the chart configuration parameters</li>
  5178. <li><code>get logdb - webchart "" "" "" deletechart "" "" 7</code><br>
  5179. Will delete a chart from the database with the given id</li>
  5180. </ul>
  5181. <br><br>
  5182. </ul>
  5183. <a name="DbLogattr"></a>
  5184. <b>Attributes</b>
  5185. <br><br>
  5186. <ul><b>addStateEvent</b>
  5187. <ul>
  5188. <code>attr &lt;device&gt; addStateEvent [0|1]
  5189. </code><br>
  5190. As you probably know the event associated with the state Reading is special, as the "state: "
  5191. string is stripped, i.e event is not "state: on" but just "on". <br>
  5192. Mostly it is desireable to get the complete event without "state: " stripped, so it is the default behavior of DbLog.
  5193. That means you will get state-event complete as "state: xxx". <br>
  5194. In some circumstances, e.g. older or special modules, it is a good idea to set addStateEvent to "0".
  5195. Try it if you have trouble with the default adjustment.
  5196. <br>
  5197. </ul>
  5198. </ul>
  5199. <br>
  5200. <ul><b>asyncMode</b>
  5201. <ul>
  5202. <code>attr &lt;device&gt; asyncMode [1|0]
  5203. </code><br>
  5204. This attribute determines the operation mode of DbLog. If asynchronous mode is active (asyncMode=1), the events which should be saved
  5205. at first will be cached in memory. After synchronisation time cycle (attribute syncInterval), or if the count limit of datasets in cache
  5206. is reached (attribute cacheLimit), the cached events get saved into the database using bulk insert.
  5207. If the database isn't available, the events will be cached in memeory furthermore, and tried to save into database again after
  5208. the next synchronisation time cycle if the database is available. <br>
  5209. In asynchronous mode the data insert into database will be executed non-blocking by a background process.
  5210. You can adjust the timeout value for this background process by attribute "timeout" (default 86400s). <br>
  5211. In synchronous mode (normal mode) the events won't be cached im memory and get saved into database immediately. If the database isn't
  5212. available the events are get lost. <br>
  5213. </ul>
  5214. </ul>
  5215. <br>
  5216. <ul><b>commitMode</b>
  5217. <ul>
  5218. <code>attr &lt;device&gt; commitMode [basic_ta:on | basic_ta:off | ac:on_ta:on | ac:on_ta:off | ac:off_ta:on]
  5219. </code><br>
  5220. Change the usage of database autocommit- and/or transaction- behavior. <br>
  5221. If transaction "off" is used, not saved datasets are not returned to cache in asynchronous mode. <br>
  5222. This attribute is an advanced feature and should only be used in a concrete situation or support case. <br><br>
  5223. <ul>
  5224. <li>basic_ta:on - autocommit server basic setting / transaktion on (default) </li>
  5225. <li>basic_ta:off - autocommit server basic setting / transaktion off </li>
  5226. <li>ac:on_ta:on - autocommit on / transaktion on </li>
  5227. <li>ac:on_ta:off - autocommit on / transaktion off </li>
  5228. <li>ac:off_ta:on - autocommit off / transaktion on (autocommit "off" set transaktion "on" implicitly) </li>
  5229. </ul>
  5230. </ul>
  5231. </ul>
  5232. <br>
  5233. <ul><b>cacheEvents</b>
  5234. <ul>
  5235. <code>attr &lt;device&gt; cacheEvents [2|1|0]
  5236. </code><br>
  5237. <ul>
  5238. <li>cacheEvents=1: creates events of reading CacheUsage at point of time when a new dataset has been added to the cache. </li>
  5239. <li>cacheEvents=2: creates events of reading CacheUsage at point of time when in aychronous mode a new write cycle to the
  5240. database starts. In that moment CacheUsage contains the number of datasets which will be written to
  5241. the database. </li><br>
  5242. </ul>
  5243. </ul>
  5244. </ul>
  5245. <br>
  5246. <ul><b>cacheLimit</b>
  5247. <ul>
  5248. <code>
  5249. attr &lt;device&gt; cacheLimit &lt;n&gt;
  5250. </code><br>
  5251. In asynchronous logging mode the content of cache will be written into the database and cleared if the number &lt;n&gt; datasets
  5252. in cache has reached (default: 500). Thereby the timer of asynchronous logging mode will be set new to the value of
  5253. attribute "syncInterval". In case of error the next write attempt will be started at the earliest after syncInterval/2. <br>
  5254. </ul>
  5255. </ul>
  5256. <br>
  5257. <ul><b>colEvent</b>
  5258. <ul>
  5259. <code>
  5260. attr &lt;device&gt; colEvent &lt;n&gt;
  5261. </code><br>
  5262. The field length of database field EVENT will be adjusted. By this attribute the default value in the DbLog-device can be
  5263. adjusted if the field length in the databse was changed nanually. If colEvent=0 is set, the database field
  5264. EVENT won't be filled . <br>
  5265. <b>Note:</b> <br>
  5266. If the attribute is set, all of the field length limits are valid also for SQLite databases as noticed in Internal COLUMNS ! <br>
  5267. </ul>
  5268. </ul>
  5269. <br>
  5270. <ul><b>colReading</b>
  5271. <ul>
  5272. <code>
  5273. attr &lt;device&gt; colReading &lt;n&gt;
  5274. </code><br>
  5275. The field length of database field READING will be adjusted. By this attribute the default value in the DbLog-device can be
  5276. adjusted if the field length in the databse was changed nanually. If colReading=0 is set, the database field
  5277. READING won't be filled . <br>
  5278. <b>Note:</b> <br>
  5279. If the attribute is set, all of the field length limits are valid also for SQLite databases as noticed in Internal COLUMNS ! <br>
  5280. </ul>
  5281. </ul>
  5282. <br>
  5283. <ul><b>colValue</b>
  5284. <ul>
  5285. <code>
  5286. attr &lt;device&gt; colValue &lt;n&gt;
  5287. </code><br>
  5288. The field length of database field VALUE will be adjusted. By this attribute the default value in the DbLog-device can be
  5289. adjusted if the field length in the databse was changed nanually. If colEvent=0 is set, the database field
  5290. VALUE won't be filled . <br>
  5291. <b>Note:</b> <br>
  5292. If the attribute is set, all of the field length limits are valid also for SQLite databases as noticed in Internal COLUMNS ! <br>
  5293. </ul>
  5294. </ul>
  5295. <br>
  5296. <ul><b>DbLogType</b>
  5297. <ul>
  5298. <code>
  5299. attr &lt;device&gt; DbLogType [Current|History|Current/History]
  5300. </code><br>
  5301. This attribute determines which table or which tables in the database are wanted to use. If the attribute isn't set,
  5302. the adjustment <i>history</i> will be used as default. <br>
  5303. The meaning of the adjustments in detail are: <br><br>
  5304. <ul>
  5305. <table>
  5306. <colgroup> <col width=10%> <col width=90%> </colgroup>
  5307. <tr><td> <b>Current</b> </td><td>Events are only logged into the current-table.
  5308. The entries of current-table will evaluated with SVG-creation. </td></tr>
  5309. <tr><td> <b>History</b> </td><td>Events are only logged into the history-table. No dropdown list with proposals will created with the
  5310. SVG-creation. </td></tr>
  5311. <tr><td> <b>Current/History</b> </td><td>Events will be logged both the current- and the history-table.
  5312. The entries of current-table will evaluated with SVG-creation. </td></tr>
  5313. <tr><td> <b>SampleFill/History</b> </td><td>Events are only logged into the history-table. The entries of current-table will evaluated with SVG-creation
  5314. and can be filled up with a customizable extract of the history-table by using a
  5315. <a href="http://fhem.de/commandref.html#DbRep">DbRep-device</a> command
  5316. "set &lt;DbRep-name&gt; tableCurrentFillup" (advanced feature). </td></tr>
  5317. </table>
  5318. </ul>
  5319. <br>
  5320. <br>
  5321. <b>Note:</b> <br>
  5322. The current-table has to be used to get a Device:Reading-DropDown list when a SVG-Plot will be created. <br>
  5323. </ul>
  5324. </ul>
  5325. <br>
  5326. <ul><b>DbLogSelectionMode</b>
  5327. <ul>
  5328. <code>
  5329. attr &lt;device&gt; DbLogSelectionMode [Exclude|Include|Exclude/Include]
  5330. </code><br>
  5331. Thise DbLog-Device-Attribute specifies how the device specific Attributes DbLogExclude and DbLogInclude are handled.
  5332. If this Attribute is missing it defaults to "Exclude".
  5333. <ul>
  5334. <li>Exclude: DbLog behaves just as usual. This means everything specified in the regex in DEF will be logged by default and anything excluded
  5335. via the DbLogExclude attribute will not be logged</li>
  5336. <li>Include: Nothing will be logged, except the readings specified via regex in the DbLogInclude attribute
  5337. (in source devices).
  5338. Neither the Regex set in DEF will be considered nor the device name of the source device itself. </li>
  5339. <li>Exclude/Include: Just almost the same as Exclude, but if the reading matches the DbLogExclude attribute, then
  5340. it will further be checked against the regex in DbLogInclude whicht may possibly re-include the already
  5341. excluded reading. </li>
  5342. </ul>
  5343. </ul>
  5344. </ul>
  5345. <br>
  5346. <ul><b>DbLogInclude</b>
  5347. <ul>
  5348. <code>
  5349. attr &lt;device&gt; DbLogInclude regex:MinInterval,[regex:MinInterval] ...
  5350. </code><br>
  5351. A new Attribute DbLogInclude will be propagated to all Devices if DBLog is used.
  5352. DbLogInclude works just like DbLogExclude but to include matching readings.
  5353. See also DbLogSelectionMode-Attribute of DbLog-Device which takes influence on
  5354. on how DbLogExclude and DbLogInclude are handled. <br>
  5355. <b>Example</b> <br>
  5356. <code>attr MyDevice1 DbLogInclude .*</code> <br>
  5357. <code>attr MyDevice2 DbLogInclude state,(floorplantext|MyUserReading):300,battery:3600</code>
  5358. </ul>
  5359. </ul>
  5360. <br>
  5361. <ul><b>DbLogExclude</b>
  5362. <ul>
  5363. <code>
  5364. attr &lt;device&gt; DbLogExclude regex:MinInterval,[regex:MinInterval] ...
  5365. </code><br>
  5366. A new Attribute DbLogExclude will be propagated to all Devices if DBLog is used.
  5367. DbLogExclude will work as regexp to exclude defined readings to log. Each individual regexp-group are separated by comma.
  5368. If a MinInterval is set, the logentry is dropped if the defined interval is not reached and value vs. lastvalue is eqal.
  5369. <br><br>
  5370. <b>Example</b> <br>
  5371. <code>attr MyDevice1 DbLogExclude .*</code> <br>
  5372. <code>attr MyDevice2 DbLogExclude state,(floorplantext|MyUserReading):300,battery:3600</code>
  5373. </ul>
  5374. </ul>
  5375. <br>
  5376. <ul><b>excludeDevs</b>
  5377. <ul>
  5378. <code>
  5379. attr &lt;device&gt; excludeDevs &lt;devspec1&gt;[#Reading],&lt;devspec2&gt;[#Reading],&lt;devspec...&gt;
  5380. </code><br>
  5381. The device/reading-combinations "devspec1#Reading", "devspec2#Reading" up to "devspec.." are globally excluded from
  5382. logging into the database. <br>
  5383. The specification of a reading is optional. <br>
  5384. Thereby devices are explicit and consequently excluded from logging without consideration of another excludes or
  5385. includes (e.g. in DEF).
  5386. The devices to exclude can be specified as <a href="#devspec">device-specification</a>.
  5387. <br><br>
  5388. <b>Examples</b> <br>
  5389. <code>
  5390. attr &lt;device&gt; excludeDevs global,Log.*,Cam.*,TYPE=DbLog
  5391. </code><br>
  5392. # The devices global respectively devices starting with "Log" or "Cam" and devices with Type=DbLog are excluded from database logging. <br>
  5393. <code>
  5394. attr &lt;device&gt; excludeDevs .*#.*Wirkleistung.*
  5395. </code><br>
  5396. # All device/reading-combinations which contain "Wirkleistung" in reading are excluded from logging. <br>
  5397. <code>
  5398. attr &lt;device&gt; excludeDevs SMA_Energymeter#Bezug_WirkP_Zaehler_Diff
  5399. </code><br>
  5400. # The event containing device "SMA_Energymeter" and reading "Bezug_WirkP_Zaehler_Diff" are excluded from logging. <br>
  5401. </ul>
  5402. </ul>
  5403. <br>
  5404. <ul><b>expimpdir</b>
  5405. <ul>
  5406. <code>
  5407. attr &lt;device&gt; expimpdir &lt;directory&gt;
  5408. </code><br>
  5409. If the cache content will be exported by <a href="#DbLogsetexportCache">"exportCache"</a> or the "importCachefile"
  5410. command, the file will be written into or read from that directory. The default directory is
  5411. "(global->modpath)/log/".
  5412. Make sure the specified directory is existing and writable. <br><br>
  5413. <b>Example</b> <br>
  5414. <code>
  5415. attr &lt;device&gt; expimpdir /opt/fhem/cache/
  5416. </code><br>
  5417. </ul>
  5418. </ul>
  5419. <br>
  5420. <ul><b>exportCacheAppend</b>
  5421. <ul>
  5422. <code>
  5423. attr &lt;device&gt; exportCacheAppend [1|0]
  5424. </code><br>
  5425. If set, the export of cache ("set &lt;device&gt; exportCache") appends the content to the newest available
  5426. export file. If there is no exististing export file, it will be new created. <br>
  5427. If the attribute not set, every export process creates a new export file . (default)<br/>
  5428. </ul>
  5429. </ul>
  5430. <br>
  5431. <ul><b>noNotifyDev</b>
  5432. <ul>
  5433. <code>
  5434. attr &lt;device&gt; noNotifyDev [1|0]
  5435. </code><br>
  5436. Enforces that NOTIFYDEV won't set and hence won't used. <br>
  5437. </ul>
  5438. </ul>
  5439. <br>
  5440. <ul><b>noSupportPK</b>
  5441. <ul>
  5442. <code>
  5443. attr &lt;device&gt; noSupportPK [1|0]
  5444. </code><br>
  5445. Deactivates the support of a set primary key by the module.<br>
  5446. </ul>
  5447. </ul>
  5448. <br>
  5449. <ul><b>syncEvents</b>
  5450. <ul>
  5451. <code>attr &lt;device&gt; syncEvents [1|0]
  5452. </code><br>
  5453. events of reading syncEvents will be created. <br>
  5454. </ul>
  5455. </ul>
  5456. <br>
  5457. <ul><b>shutdownWait</b>
  5458. <ul>
  5459. <code>attr &lt;device&gt; shutdownWait <n>
  5460. </code><br>
  5461. causes fhem shutdown to wait n seconds for pending database commit<br/>
  5462. </ul>
  5463. </ul>
  5464. <br>
  5465. <ul><b>showproctime</b>
  5466. <ul>
  5467. <code>attr &lt;device&gt; [1|0]
  5468. </code><br>
  5469. If set, the reading "sql_processing_time" shows the required execution time (in seconds) for the sql-requests. This is not calculated
  5470. for a single sql-statement, but the summary of all sql-statements necessary for within an executed DbLog-function in background.
  5471. The reading "background_processing_time" shows the total time used in background. <br>
  5472. </ul>
  5473. </ul>
  5474. <br>
  5475. <ul><b>showNotifyTime</b>
  5476. <ul>
  5477. <code>attr &lt;device&gt; showNotifyTime [1|0]
  5478. </code><br>
  5479. If set, the reading "notify_processing_time" shows the required execution time (in seconds) in the DbLog
  5480. Notify-function. This attribute is practical for performance analyses and helps to determine the differences of time
  5481. required when the operation mode was switched from synchronous to the asynchronous mode. <br>
  5482. </ul>
  5483. </ul>
  5484. <br>
  5485. <ul><b>syncInterval</b>
  5486. <ul>
  5487. <code>attr &lt;device&gt; syncInterval &lt;n&gt;
  5488. </code><br>
  5489. If DbLog is set to asynchronous operation mode (attribute asyncMode=1), with this attribute you can setup the interval in seconds
  5490. used for storage the in memory cached events into the database. THe default value is 30 seconds. <br>
  5491. </ul>
  5492. </ul>
  5493. <br>
  5494. <ul><b>suppressAddLogV3</b>
  5495. <ul>
  5496. <code>attr &lt;device&gt; suppressAddLogV3 [1|0]
  5497. </code><br>
  5498. If set, verbose3-Logfileentries done by the addLog-function will be suppressed. <br>
  5499. </ul>
  5500. </ul>
  5501. <br>
  5502. <ul><b>suppressUndef</b>
  5503. <ul>
  5504. <code>
  5505. attr &lt;device&gt; ignoreUndef <n>
  5506. </code><br>
  5507. suppresses all undef values when returning data from the DB via get <br>
  5508. <b>Example</b> <br>
  5509. <code>#DbLog eMeter:power:::$val=($val>1500)?undef:$val</code>
  5510. </ul>
  5511. </ul>
  5512. <br>
  5513. <ul><b>timeout</b>
  5514. <ul>
  5515. <code>
  5516. attr &lt;device&gt; timeout <n>
  5517. </code><br>
  5518. setup timeout of the write cycle into database in asynchronous mode (default 86400s) <br>
  5519. </ul>
  5520. </ul>
  5521. <br>
  5522. <ul><b>useCharfilter</b>
  5523. <ul>
  5524. <code>
  5525. attr &lt;device&gt; useCharfilter [0|1] <n>
  5526. </code><br>
  5527. If set, only ASCII characters from 32 to 126 are accepted in event.
  5528. That are the characters " A-Za-z0-9!"#$%&'()*+,-.\/:;<=>?@[\\]^_`{|}~" .<br>
  5529. Mutated vowel and "€" are transcribed (e.g. ä to ae). (default: 0). <br>
  5530. </ul>
  5531. </ul>
  5532. <br>
  5533. <ul><b>valueFn</b>
  5534. <ul>
  5535. <code>
  5536. attr &lt;device&gt; valueFn {}
  5537. </code><br>
  5538. Perl expression that can use and change values of $TIMESTAMP, $DEVICE, $DEVICETYPE, $READING, $VALUE (value of reading) and
  5539. $UNIT (unit of reading value).
  5540. It also has readonly-access to $EVENT for evaluation in your expression. <br>
  5541. If $TIMESTAMP should be changed, it must meet the condition "yyyy-mm-dd hh:mm:ss", otherwise the $timestamp wouldn't
  5542. be changed.
  5543. In addition you can set the variable $IGNORE=1 if you want skip a dataset from logging. <br><br>
  5544. <b>Examples</b> <br>
  5545. <code>
  5546. attr &lt;device&gt; valueFn {if ($DEVICE eq "living_Clima" && $VALUE eq "off" ){$VALUE=0;} elsif ($DEVICE eq "e-power"){$VALUE= sprintf "%.1f", $VALUE;}}
  5547. </code> <br>
  5548. # change value "off" to "0" of device "living_Clima" and rounds value of e-power to 1f <br><br>
  5549. <code>
  5550. attr &lt;device&gt; valueFn {if ($DEVICE eq "SMA_Energymeter" && $READING eq "state"){$IGNORE=1;}}
  5551. </code><br>
  5552. # don't log the dataset of device "SMA_Energymeter" if the reading is "state" <br><br>
  5553. <code>
  5554. attr &lt;device&gt; valueFn {if ($DEVICE eq "Dum.Energy" && $READING eq "TotalConsumption"){$UNIT="W";}}
  5555. </code><br>
  5556. # set the unit of device "Dum.Energy" to "W" if reading is "TotalConsumption" <br><br>
  5557. </ul>
  5558. </ul>
  5559. <br>
  5560. <ul><b>verbose4Devs</b>
  5561. <ul>
  5562. <code>
  5563. attr &lt;device&gt; verbose4Devs &lt;device1&gt;,&lt;device2&gt;,&lt;device..&gt;
  5564. </code><br>
  5565. If verbose level 4 is used, only output of devices set in this attribute will be reported in FHEM central logfile. If this attribute
  5566. isn't set, output of all relevant devices will be reported if using verbose level 4.
  5567. The given devices are evaluated as Regex. <br>
  5568. <b>Example</b> <br>
  5569. <code>
  5570. attr &lt;device&gt; verbose4Devs sys.*,.*5000.*,Cam.*,global
  5571. </code><br>
  5572. # The devices starting with "sys", "Cam" respectively devices are containing "5000" in its name and the device "global" will be reported in FHEM
  5573. central Logfile if verbose=4 is set. <br>
  5574. </ul>
  5575. </ul>
  5576. <br>
  5577. </ul>
  5578. =end html
  5579. =begin html_DE
  5580. <a name="DbLog"></a>
  5581. <h3>DbLog</h3>
  5582. <ul>
  5583. <br>
  5584. Mit DbLog werden Events in einer Datenbank gespeichert. Es wird SQLite, MySQL/MariaDB und PostgreSQL unterstützt. <br><br>
  5585. <b>Voraussetzungen</b> <br><br>
  5586. Die Perl-Module <code>DBI</code> und <code>DBD::&lt;dbtype&gt;</code> müssen installiert werden (use <code>cpan -i &lt;module&gt;</code>
  5587. falls die eigene Distribution diese nicht schon mitbringt).
  5588. <br><br>
  5589. Auf einem Debian-System können diese Module z.Bsp. installiert werden mit: <br><br>
  5590. <ul>
  5591. <table>
  5592. <colgroup> <col width=5%> <col width=95%> </colgroup>
  5593. <tr><td> <b>DBI</b> </td><td>: <code> sudo apt-get install libdbi-perl </code> </td></tr>
  5594. <tr><td> <b>MySQL</b> </td><td>: <code> sudo apt-get install [mysql-server] mysql-client libdbd-mysql libdbd-mysql-perl </code> (mysql-server nur bei lokaler MySQL-Server-Installation) </td></tr>
  5595. <tr><td> <b>SQLite</b> </td><td>: <code> sudo apt-get install sqlite3 libdbi-perl libdbd-sqlite3-perl </code> </td></tr>
  5596. <tr><td> <b>PostgreSQL</b> </td><td>: <code> sudo apt-get install libdbd-pg-perl </code> </td></tr>
  5597. </table>
  5598. </ul>
  5599. <br>
  5600. <br>
  5601. <b>Vorbereitungen</b> <br><br>
  5602. Zunächst muss die Datenbank angelegt werden. <br>
  5603. Beispielcode bzw. Scripts zum Erstellen einer MySQL/PostgreSQL/SQLite Datenbank ist im
  5604. <a href="https://svn.fhem.de/trac/browser/trunk/fhem/contrib/dblog">SVN -&gt; contrib/dblog/db_create_&lt;DBType&gt;.sql</a>
  5605. enthalten. <br>
  5606. (<b>Achtung:</b> Die lokale FHEM-Installation enthält im Unterverzeichnis ./contrib/dblog nicht die aktuellsten
  5607. Scripte !!) <br><br>
  5608. Die Datenbank beinhaltet 2 Tabellen: <code>current</code> und <code>history</code>. <br>
  5609. Die Tabelle <code>current</code> enthält den letzten Stand pro Device und Reading. <br>
  5610. In der Tabelle <code>history</code> sind alle Events historisch gespeichert. <br>
  5611. Beachten sie bitte unbedingt das <a href="#DbLogattr">Attribut</a> DbLogType um die Benutzung der Tabellen
  5612. <code>current</code> und <code>history</code> festzulegen.
  5613. <br><br>
  5614. Die Tabellenspalten haben folgende Bedeutung: <br><br>
  5615. <ul>
  5616. <table>
  5617. <colgroup> <col width=5%> <col width=95%> </colgroup>
  5618. <tr><td> TIMESTAMP </td><td>: Zeitpunkt des Events, z.B. <code>2007-12-30 21:45:22</code> </td></tr>
  5619. <tr><td> DEVICE </td><td>: Name des Devices, z.B. <code>Wetterstation</code> </td></tr>
  5620. <tr><td> TYPE </td><td>: Type des Devices, z.B. <code>KS300</code> </td></tr>
  5621. <tr><td> EVENT </td><td>: das auftretende Event als volle Zeichenkette, z.B. <code>humidity: 71 (%)</code> </td></tr>
  5622. <tr><td> READING </td><td>: Name des Readings, ermittelt aus dem Event, z.B. <code>humidity</code> </td></tr>
  5623. <tr><td> VALUE </td><td>: aktueller Wert des Readings, ermittelt aus dem Event, z.B. <code>71</code> </td></tr>
  5624. <tr><td> UNIT </td><td>: Einheit, ermittelt aus dem Event, z.B. <code>%</code> </td></tr>
  5625. </table>
  5626. </ul>
  5627. <br>
  5628. <br>
  5629. <b>Index anlegen</b> <br>
  5630. Für die Leseperformance, z.B. bei der Erstellung von SVG-PLots, ist es von besonderer Bedeutung dass der <b>Index "Search_Idx"</b>
  5631. oder ein vergleichbarer Index (z.B. ein Primary Key) angelegt ist. <br><br>
  5632. Der Index "Search_Idx" kann mit diesen Statements, z.B. in der Datenbank 'fhem', angelegt werden (auch nachträglich): <br><br>
  5633. <ul>
  5634. <table>
  5635. <colgroup> <col width=5%> <col width=95%> </colgroup>
  5636. <tr><td> <b>MySQL</b> </td><td>: <code> CREATE INDEX Search_Idx ON `fhem`.`history` (DEVICE, READING, TIMESTAMP); </code> </td></tr>
  5637. <tr><td> <b>SQLite</b> </td><td>: <code> CREATE INDEX Search_Idx ON `history` (DEVICE, READING, TIMESTAMP); </code> </td></tr>
  5638. <tr><td> <b>PostgreSQL</b> </td><td>: <code> CREATE INDEX "Search_Idx" ON history USING btree (device, reading, "timestamp"); </code> </td></tr>
  5639. </table>
  5640. </ul>
  5641. <br>
  5642. Der Code zur Anlage ist ebenfalls in den Scripten
  5643. <a href="https://svn.fhem.de/trac/browser/trunk/fhem/contrib/dblog">SVN -&gt; contrib/dblog/db_create_&lt;DBType&gt;.sql</a>
  5644. enthalten. <br><br>
  5645. Für die Verbindung zur Datenbank wird eine <b>Konfigurationsdatei</b> verwendet.
  5646. Die Konfiguration ist in einer sparaten Datei abgelegt um das Datenbankpasswort nicht in Klartext in der
  5647. FHEM-Haupt-Konfigurationsdatei speichern zu müssen.
  5648. Ansonsten wäre es mittels des <a href="https://fhem.de/commandref_DE.html#list">list</a> Befehls einfach auslesbar.
  5649. <br><br>
  5650. Die <b>Konfigurationsdatei</b> wird z.B. nach /opt/fhem kopiert und hat folgenden Aufbau, den man an seine Umgebung
  5651. anpassen muß (entsprechende Zeilen entkommentieren und anpassen): <br><br>
  5652. <pre>
  5653. ####################################################################################
  5654. # database configuration file
  5655. #
  5656. # NOTE:
  5657. # If you don't use a value for user / password please delete the leading hash mark
  5658. # and write 'user => ""' respectively 'password => ""' instead !
  5659. #
  5660. #
  5661. ## for MySQL
  5662. ####################################################################################
  5663. #%dbconfig= (
  5664. # connection => "mysql:database=fhem;host=&lt;database host&gt;;port=3306",
  5665. # user => "fhemuser",
  5666. # password => "fhempassword",
  5667. # # optional enable(1) / disable(0) UTF-8 support (at least V 4.042 is necessary)
  5668. # utf8 => 1
  5669. #);
  5670. ####################################################################################
  5671. #
  5672. ## for PostgreSQL
  5673. ####################################################################################
  5674. #%dbconfig= (
  5675. # connection => "Pg:database=fhem;host=&lt;database host&gt;",
  5676. # user => "fhemuser",
  5677. # password => "fhempassword"
  5678. #);
  5679. ####################################################################################
  5680. #
  5681. ## for SQLite (username and password stay empty for SQLite)
  5682. ####################################################################################
  5683. #%dbconfig= (
  5684. # connection => "SQLite:dbname=/opt/fhem/fhem.db",
  5685. # user => "",
  5686. # password => ""
  5687. #);
  5688. ####################################################################################
  5689. </pre>
  5690. Wird configDB genutzt, ist das Konfigurationsfile in die configDB hochzuladen ! <br><br>
  5691. <b>Hinweis zu Sonderzeichen:</b><br>
  5692. Werden Sonderzeichen, wie z.B. @, $ oder %, welche eine programmtechnische Bedeutung in Perl haben im Passwort verwendet,
  5693. sind diese Zeichen zu escapen.
  5694. Das heißt in diesem Beispiel wäre zu verwenden: \@,\$ bzw. \%.
  5695. <br>
  5696. <br>
  5697. <br>
  5698. <a name="DbLogdefine"></a>
  5699. <b>Define</b>
  5700. <ul>
  5701. <br>
  5702. <code>define &lt;name&gt; DbLog &lt;configfilename&gt; &lt;regexp&gt;</code>
  5703. <br><br>
  5704. <code>&lt;configfilename&gt;</code> ist die vorbereitete <b>Konfigurationsdatei</b>. <br>
  5705. <code>&lt;regexp&gt;</code> ist identisch <a href="https://fhem.de/commandref_DE.html#FileLog">FileLog</a> der Filelog-Definition.
  5706. <br><br>
  5707. <b>Beispiel:</b>
  5708. <ul>
  5709. <code>define myDbLog DbLog /etc/fhem/db.conf .*:.*</code><br>
  5710. speichert alles in der Datenbank
  5711. </ul>
  5712. <br>
  5713. Nachdem das DbLog-Device definiert wurde, ist empfohlen einen <b>Konfigurationscheck</b> auszuführen: <br><br>
  5714. <ul>
  5715. <code>set &lt;name&gt; configCheck</code> <br>
  5716. </ul>
  5717. <br>
  5718. Dieser Check prüft einige wichtige Einstellungen des DbLog-Devices und gibt Empfehlungen für potentielle Verbesserungen.
  5719. <br><br>
  5720. <br>
  5721. DbLog unterscheidet den synchronen (Default) und asynchronen Logmodus. Der Logmodus ist über das
  5722. <a href="#DbLogattr">Attribut</a> asyncMode einstellbar. Ab Version 2.13.5 unterstützt DbLog einen gesetzten
  5723. Primary Key (PK) in den Tabellen Current und History. Soll PostgreSQL mit PK genutzt werden, muss PostgreSQL mindestens
  5724. Version 9.5 sein.
  5725. <br><br>
  5726. Der gespeicherte Wert des Readings wird optimiert für eine automatisierte Nachverarbeitung, z.B. <code>yes</code> wird transformiert
  5727. nach <code>1</code>. <br><br>
  5728. Die gespeicherten Werte können mittels GET Funktion angezeigt werden:
  5729. <ul>
  5730. <code>get myDbLog - - 2012-11-10 2012-11-10 KS300:temperature</code>
  5731. </ul>
  5732. <br>
  5733. <b>FileLog-Dateien nach DbLog übertragen</b> <br><br>
  5734. Zur Übertragung von vorhandenen Filelog-Daten in die DbLog-Datenbank steht das spezielle Modul 98_FileLogConvert.pm
  5735. zur Verfügung. <br>
  5736. Dieses Modul kann <a href="https://svn.fhem.de/trac/browser/trunk/fhem/contrib/98_FileLogConvert.pm"> hier</a>
  5737. bzw. aus dem Verzeichnis ./contrib geladen werden.
  5738. Weitere Informationen und Hilfestellung gibt es im entsprechenden <a href="https://forum.fhem.de/index.php/topic,66383.0.html">
  5739. Forumthread </a>. <br><br><br>
  5740. <b>Reporting und Management von DbLog-Datenbankinhalten</b> <br><br>
  5741. Mit Hilfe <a href="https://fhem.de/commandref_DE.html#SVG">SVG</a> können Datenbankinhalte visualisiert werden. <br>
  5742. Darüber hinaus kann das Modul <a href="https://fhem.de/commandref_DE.html#DbRep">DbRep</a> genutzt werden um tabellarische
  5743. Datenbankauswertungen anzufertigen oder den Datenbankinhalt mit den zur Verfügung stehenden Funktionen zu verwalten.
  5744. <br><br><br>
  5745. <b>Troubleshooting</b> <br><br>
  5746. Wenn nach der erfolgreichen Definition das DbLog-Device nicht wie erwartet arbeitet,
  5747. können folgende Hinweise hilfreich sein: <br><br>
  5748. <ul>
  5749. <li> Wurden die vorbereitenden Schritte gemacht, die in der commandref beschrieben sind ? (Softwarekomponenten installieren, Tabellen, Index anlegen) </li>
  5750. <li> Wurde ein "set &lt;name&gt; configCheck" nach dem Define durchgeführt und eventuelle Fehler beseitigt bzw. Empfehlungen umgesetzt ? </li>
  5751. <li> Falls configDB in Benutzung ... wurde das DB-Konfigurationsfile in configDB importiert (z.B. mit "configDB fileimport ./db.conf") ? </li>
  5752. <li> Beim Anlegen eines SVG-Plots erscheint keine Drop-Down Liste mit Vorschlagswerten -> Attribut "DbLogType" auf "Current/History" setzen. </li>
  5753. </ul>
  5754. <br>
  5755. Sollten diese Hinweise nicht zum Erfolg führen, bitte den verbose-Level im DbLog Device auf 4 oder 5 hochsetzen und
  5756. die Einträge bezüglich des DbLog-Device im Logfile beachten.
  5757. Zur Problemanalyse bitte die Ausgabe von "list &lt;name&gt;", das Ergebnis von "set &lt;name&gt; configCheck" und die
  5758. Ausgaben des DbLog-Device im Logfile im Forumthread posten.
  5759. <br><br>
  5760. </ul>
  5761. <br>
  5762. <br>
  5763. <a name="DbLogset"></a>
  5764. <b>Set</b>
  5765. <ul>
  5766. <code>set &lt;name&gt; addCacheLine YYYY-MM-DD HH:MM:SS|&lt;device&gt;|&lt;type&gt;|&lt;event&gt;|&lt;reading&gt;|&lt;value&gt;|[&lt;unit&gt;] </code><br><br>
  5767. <ul> Im asynchronen Modus wird ein neuer Datensatz in den Cache eingefügt und beim nächsten Synclauf mit abgearbeitet.
  5768. <br><br>
  5769. <b>Beispiel:</b> <br>
  5770. set &lt;name&gt; addCacheLine 2017-12-05 17:03:59|MaxBathRoom|MAX|valveposition: 95|valveposition|95|% <br>
  5771. </ul><br>
  5772. <code>set &lt;name&gt; addLog &lt;devspec&gt;:&lt;Reading&gt; [Value] [CN=&lt;caller name&gt;] [!useExcludes] </code><br><br>
  5773. <ul> Fügt einen zusatzlichen Logeintrag einer Device/Reading-Kombination in die Datenbank ein. <br><br>
  5774. <ul>
  5775. <li> <b>&lt;devspec&gt;:&lt;Reading&gt;</b> - Das Device kann als <a href="#devspec">Geräte-Spezifikation</a> angegeben werden. <br>
  5776. Die Angabe von "Reading" wird als regulärer Ausdruck ausgewertet. Ist
  5777. das Reading nicht vorhanden und der Wert "Value" angegeben, wird das Reading
  5778. in die DB eingefügt wenn es kein regulärer Ausdruck und ein valider
  5779. Readingname ist. </li>
  5780. <li> <b>Value</b> - Optional kann "Value" für den Readingwert angegeben werden. Ist Value nicht angegeben, wird der aktuelle
  5781. Wert des Readings in die DB eingefügt. </li>
  5782. <li> <b>CN=&lt;caller name&gt;</b> - Mit dem Schlüssel "CN=" (<b>C</b>aller <b>N</b>ame) kann dem addLog-Aufruf ein String,
  5783. z.B. der Name des aufrufenden Devices (z.B. eines at- oder notify-Devices), mitgegeben
  5784. werden. Mit Hilfe der im <a href="#DbLogattr">Attribut</a> "valueFn" hinterlegten
  5785. Funktion kann dieser Schlüssel über die Variable $CN ausgewertet werden. Dadurch ist es
  5786. möglich, das Verhalten des addLogs abhängig von der aufrufenden Quelle zu beeinflussen.
  5787. </li>
  5788. <li> <b>!useExcludes</b> - Ein eventuell im Quell-Device gesetztes Attribut "DbLogExclude" wird von der Funktion berücksichtigt. Soll dieses
  5789. Attribut nicht berücksichtigt werden, kann das Schüsselwort "!useExcludes" verwendet werden. </li>
  5790. </ul>
  5791. <br>
  5792. Das Datenbankfeld "EVENT" wird automatisch mit "addLog" belegt. <br>
  5793. Es wird KEIN zusätzlicher Event im System erzeugt !<br><br>
  5794. <b>Beispiele:</b> <br>
  5795. set &lt;name&gt; addLog SMA_Energymeter:Bezug_Wirkleistung <br>
  5796. set &lt;name&gt; addLog TYPE=SSCam:state <br>
  5797. set &lt;name&gt; addLog MyWetter:(fc10.*|fc8.*) <br>
  5798. set &lt;name&gt; addLog MyWetter:(wind|wind_ch.*) 20 !useExcludes <br>
  5799. set &lt;name&gt; addLog TYPE=CUL_HM:FILTER=model=HM-CC-RT-DN:FILTER=subType!=(virtual|):(measured-temp|desired-temp|actuator) <br><br>
  5800. set &lt;name&gt; addLog USV:state CN=di.cronjob <br>
  5801. In der valueFn-Funktion wird der Aufrufer "di.cronjob" über die Variable $CN ausgewertet und davon abhängig der
  5802. Timestamp dieses addLog korrigiert: <br><br>
  5803. valueFn = if($CN eq "di.cronjob" and $TIMESTAMP =~ m/\s00:00:[\d:]+/) { $TIMESTAMP =~ s/\s([^\s]+)/ 23:59:59/ }
  5804. </ul><br>
  5805. <code>set &lt;name&gt; clearReadings </code><br><br>
  5806. <ul> Leert Readings die von verschiedenen DbLog-Funktionen angelegt wurden. </ul><br>
  5807. <code>set &lt;name&gt; eraseReadings </code><br><br>
  5808. <ul> Löscht alle Readings außer dem Reading "state". </ul><br>
  5809. <code>set &lt;name&gt; commitCache </code><br><br>
  5810. <ul>Im asynchronen Modus (<a href="#DbLogattr">Attribut</a> asyncMode=1), werden die im Speicher gecachten Daten in die Datenbank geschrieben
  5811. und danach der Cache geleert. Der interne Timer des asynchronen Modus wird dabei neu gesetzt.
  5812. Der Befehl kann nützlich sein um manuell oder z.B. über ein AT den Cacheinhalt zu einem definierten Zeitpunkt in die
  5813. Datenbank zu schreiben. </ul><br>
  5814. <code>set &lt;name&gt; configCheck </code><br><br>
  5815. <ul>Es werden einige wichtige Einstellungen geprüft und Empfehlungen gegeben falls potentielle Verbesserungen
  5816. identifiziert wurden.
  5817. </ul><br/>
  5818. <code>set &lt;name&gt; count </code><br><br>
  5819. <ul>Zählt die Datensätze in den Tabellen current und history und schreibt die Ergebnisse in die Readings
  5820. countCurrent und countHistory.</ul><br>
  5821. <code>set &lt;name&gt; countNbl </code><br><br>
  5822. <ul>
  5823. Die non-blocking Ausführung von "set &lt;name&gt; count".
  5824. <br><br>
  5825. <b>Hinweis:</b> <br>
  5826. Obwohl die Funktion selbst non-blocking ist, muß das DbLog-Device im asynchronen Modus betrieben werden (asyncMode = 1)
  5827. um FHEM nicht zu blockieren !
  5828. </ul><br>
  5829. <code>set &lt;name&gt; deleteOldDays &lt;n&gt;</code><br/><br>
  5830. <ul>Löscht Datensätze in Tabelle history, die älter sind als &lt;n&gt; Tage sind.
  5831. Die Anzahl der gelöschten Datens&auml;tze wird in das Reading lastRowsDeleted geschrieben.</ul><br>
  5832. <code>set &lt;name&gt; deleteOldDaysNbl &lt;n&gt;</code><br><br>
  5833. <ul>
  5834. Identisch zu Funktion "deleteOldDays" wobei deleteOldDaysNbl nicht blockierend ausgeführt wird.
  5835. <br><br>
  5836. <b>Hinweis:</b> <br>
  5837. Obwohl die Funktion selbst non-blocking ist, muß das DbLog-Device im asynchronen Modus betrieben werden (asyncMode = 1)
  5838. um FHEM nicht zu blockieren !
  5839. </ul><br>
  5840. <a name="DbLogsetexportCache"></a>
  5841. <code>set &lt;name&gt; exportCache [nopurge | purgecache] </code><br><br>
  5842. <ul>Wenn DbLog im asynchronen Modus betrieben wird, kann der Cache mit diesem Befehl in ein Textfile geschrieben
  5843. werden. Das File wird per Default in dem Verzeichnis (global->modpath)/log/ erstellt. Das Zielverzeichnis kann mit
  5844. dem <a href="#DbLogattr">Attribut</a> "expimpdir" geändert werden. <br>
  5845. Der Name des Files wird automatisch generiert und enthält den Präfix "cache_", gefolgt von dem DbLog-Devicenamen und
  5846. dem aktuellen Zeitstempel, z.B. "cache_LogDB_2017-03-23_22-13-55". <br>
  5847. Mit den Optionen "nopurge" bzw. "purgecache" wird festgelegt, ob der Cacheinhalt nach dem Export gelöscht werden
  5848. soll oder nicht. Mit "nopurge" (default) bleibt der Cacheinhalt erhalten. <br>
  5849. Das <a href="#DbLogattr">Attribut</a> "exportCacheAppend" bestimmt dabei, ob mit jedem Exportvorgang ein neues Exportfile
  5850. angelegt wird (default) oder der Cacheinhalt an das bestehende (neueste) Exportfile angehängt wird.
  5851. </ul><br>
  5852. <code>set &lt;name&gt; importCachefile &lt;file&gt; </code><br><br>
  5853. <ul>Importiert ein mit "exportCache" geschriebenes File in die Datenbank.
  5854. Die verfügbaren Dateien werden per Default im Verzeichnis (global->modpath)/log/ gesucht und eine Drop-Down Liste
  5855. erzeugt sofern Dateien gefunden werden. Das Quellverzeichnis kann mit dem <a href="#DbLogattr">Attribut</a> expimpdir geändert werden. <br>
  5856. Es werden nur die Dateien angezeigt, die dem Muster "cache_", gefolgt von dem DbLog-Devicenamen entsprechen. <br>
  5857. Zum Beispiel "cache_LogDB_2017-03-23_22-13-55", falls das Log-Device "LogDB" heißt. <br>
  5858. Nach einem erfolgreichen Import wird das File mit dem Präfix "impdone_" versehen und erscheint dann nicht mehr
  5859. in der Drop-Down Liste. Soll ein Cachefile in eine andere als der Quelldatenbank importiert werden, kann das
  5860. DbLog-Device im Filenamen angepasst werden damit dieses File den Suchktiterien entspricht und in der Drop-Down Liste
  5861. erscheint. </ul><br>
  5862. <code>set &lt;name&gt; listCache </code><br><br>
  5863. <ul>Wenn DbLog im asynchronen Modus betrieben wird (Attribut asyncMode=1), können mit diesem Befehl die im Speicher gecachten Events
  5864. angezeigt werden.</ul><br>
  5865. <code>set &lt;name&gt; purgeCache </code><br><br>
  5866. <ul>Im asynchronen Modus (<a href="#DbLogattr">Attribut</a> asyncMode=1), werden die im Speicher gecachten Daten gelöscht.
  5867. Es werden keine Daten aus dem Cache in die Datenbank geschrieben. </ul><br>
  5868. <code>set &lt;name&gt; reduceLog &lt;no&gt;[:&lt;nn&gt;] [average[=day]] [exclude=device1:reading1,device2:reading2,...] </code><br><br>
  5869. <ul>Reduziert historische Datensätze, die älter sind als &lt;no&gt; Tage und (optional) neuer sind als &lt;nn&gt; Tage
  5870. auf einen Eintrag (den ersten) pro Stunde je Device & Reading.<br>
  5871. Innerhalb von device/reading können <b>SQL-Wildcards "%" und "_"</b> verwendet werden. <br><br>
  5872. Das Reading "reduceLogState" zeigt den Ausführungsstatus des letzten reduceLog-Befehls. <br><br>
  5873. Durch die optionale Angabe von 'average' wird nicht nur die Datenbank bereinigt, sondern alle numerischen Werte
  5874. einer Stunde werden auf einen einzigen Mittelwert reduziert. <br>
  5875. Durch die optionale Angabe von 'average=day' wird nicht nur die Datenbank bereinigt, sondern alle numerischen
  5876. Werte eines Tages auf einen einzigen Mittelwert reduziert. (impliziert 'average') <br><br>
  5877. Optional kann als letzer Parameter "exclude=device1:reading1,device2:reading2,...."
  5878. angegeben werden um device/reading Kombinationen von reduceLog auszuschließen. <br><br>
  5879. Optional kann als letzer Parameter "include=device:reading" angegeben werden um
  5880. die auf die Datenbank ausgeführte SELECT-Abfrage einzugrenzen, was die RAM-Belastung verringert und die
  5881. Performance erhöht. <br><br>
  5882. <ul>
  5883. <b>Beispiel: </b> <br>
  5884. set &lt;name&gt; reduceLog 270 average include=Luftdaten_remote:% <br>
  5885. </ul>
  5886. <br>
  5887. <b>ACHTUNG:</b> Es wird dringend empfohlen zu überprüfen ob der standard INDEX 'Search_Idx' in der Tabelle 'history' existiert! <br>
  5888. Die Abarbeitung dieses Befehls dauert unter Umständen (ohne INDEX) extrem lange. FHEM wird durch den Befehl bis
  5889. zur Fertigstellung <b>komplett blockiert !</b> <br><br>
  5890. </ul><br>
  5891. <code>set &lt;name&gt; reduceLogNbl &lt;no&gt;[:&lt;nn&gt;] [average[=day]] [exclude=device1:reading1,device2:reading2,...]</code><br><br>
  5892. <ul>
  5893. Führt die gleiche Funktion wie "set &lt;name&gt; reduceLog" aus. Im Gegensatz zu reduceLog wird mit FHEM wird durch den Befehl reduceLogNbl nicht
  5894. mehr blockiert da diese Funktion non-blocking implementiert ist !
  5895. <br><br>
  5896. <b>Hinweis:</b> <br>
  5897. Obwohl die Funktion selbst non-blocking ist, muß das DbLog-Device im asynchronen Modus betrieben werden (asyncMode = 1)
  5898. um FHEM nicht zu blockieren !
  5899. </ul><br>
  5900. <code>set &lt;name&gt; reopen [n]</code><br/><br/>
  5901. <ul>Schließt die Datenbank und öffnet sie danach sofort wieder wenn keine Zeit [n] in Sekunden angegeben wurde.
  5902. Dabei wird die Journaldatei geleert und neu angelegt.<br/>
  5903. Verbessert den Datendurchsatz und vermeidet Speicherplatzprobleme. <br>
  5904. Wurde eine optionale Verzögerungszeit [n] in Sekunden angegeben, wird die Verbindung zur Datenbank geschlossen und erst
  5905. nach Ablauf von [n] Sekunden wieder neu verbunden.
  5906. Im synchronen Modus werden die Events in dieser Zeit nicht gespeichert.
  5907. Im asynchronen Modus werden die Events im Cache gespeichert und nach dem Reconnect in die Datenbank geschrieben. </ul><br>
  5908. <code>set &lt;name&gt; rereadcfg </code><br/><br/>
  5909. <ul>Schließt die Datenbank und öffnet sie danach sofort wieder. Dabei wird die Journaldatei geleert und neu angelegt.<br/>
  5910. Verbessert den Datendurchsatz und vermeidet Speicherplatzprobleme.<br/>
  5911. Zwischen dem Schließen der Verbindung und dem Neuverbinden werden die Konfigurationsdaten neu gelesen</ul><br/>
  5912. <code>set &lt;name&gt; userCommand &lt;validSqlStatement&gt;</code><br/><br/>
  5913. <ul><b>BENUTZE DIESE FUNKTION NUR, WENN DU WIRKLICH (WIRKLICH!) WEISST, WAS DU TUST!!!</b><br/><br/>
  5914. Führt einen beliebigen (!!!) sql Befehl in der Datenbank aus. Der Befehl und ein zurückgeliefertes
  5915. Ergebnis wird in das Reading "userCommand" bzw. "userCommandResult" geschrieben. Das Ergebnis kann nur
  5916. einzeilig sein.
  5917. Für SQL-Statements, die mehrzeilige Ergebnisse liefern, kann das Auswertungsmodul
  5918. <a href=https://fhem.de/commandref_DE.html#DbRep>DbRep</a> genutzt werden.</br>
  5919. Wird von der Datenbankschnittstelle kein Ergebnis (undef) zurückgeliefert, erscheint die Meldung "no result"
  5920. im Reading "userCommandResult".
  5921. </ul><br>
  5922. </ul><br>
  5923. <a name="DbLogget"></a>
  5924. <b>Get</b>
  5925. <ul>
  5926. <code>get &lt;name&gt; ReadingsVal&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;device&gt; &lt;reading&gt; &lt;default&gt;</code><br/>
  5927. <code>get &lt;name&gt; ReadingsTimestamp &lt;device&gt; &lt;reading&gt; &lt;default&gt;</code><br/>
  5928. <br/>
  5929. Liest einen einzelnen Wert aus der Datenbank, Benutzung und Syntax sind weitgehend identisch zu ReadingsVal() und ReadingsTimestamp().<br/>
  5930. </ul>
  5931. <br/>
  5932. <br/>
  5933. <ul>
  5934. <code>get &lt;name&gt; &lt;infile&gt; &lt;outfile&gt; &lt;from&gt;
  5935. &lt;to&gt; &lt;column_spec&gt; </code>
  5936. <br><br>
  5937. Liesst Daten aus der Datenbank. Wird durch die Frontends benutzt um Plots
  5938. zu generieren ohne selbst auf die Datenank zugreifen zu müssen.
  5939. <br>
  5940. <ul>
  5941. <li>&lt;in&gt;<br>
  5942. Ein Parameter um eine Kompatibilität zum Filelog herzustellen.
  5943. Dieser Parameter ist per default immer auf <code>-</code> zu setzen.<br>
  5944. Folgende Ausprägungen sind zugelassen:<br>
  5945. <ul>
  5946. <li>current: die aktuellen Werte aus der Tabelle "current" werden gelesen.</li>
  5947. <li>history: die historischen Werte aus der Tabelle "history" werden gelesen.</li>
  5948. <li>-: identisch wie "history"</li>
  5949. </ul>
  5950. </li>
  5951. <li>&lt;out&gt;<br>
  5952. Ein Parameter um eine Kompatibilität zum Filelog herzustellen.
  5953. Dieser Parameter ist per default immer auf <code>-</code> zu setzen um die
  5954. Ermittlung der Daten aus der Datenbank für die Plotgenerierung zu prüfen.<br>
  5955. Folgende Ausprägungen sind zugelassen:<br>
  5956. <ul>
  5957. <li>ALL: Es werden alle Spalten der Datenbank ausgegeben. Inclusive einer Überschrift.</li>
  5958. <li>Array: Es werden alle Spalten der Datenbank als Hash ausgegeben. Alle Datensätze als Array zusammengefasst.</li>
  5959. <li>INT: intern zur Plotgenerierung verwendet</li>
  5960. <li>-: default</li>
  5961. </ul>
  5962. </li>
  5963. <li>&lt;from&gt; / &lt;to&gt;<br>
  5964. Wird benutzt um den Zeitraum der Daten einzugrenzen. Es ist das folgende
  5965. Zeitformat oder ein Teilstring davon zu benutzen:<br>
  5966. <ul><code>YYYY-MM-DD_HH24:MI:SS</code></ul></li>
  5967. <li>&lt;column_spec&gt;<br>
  5968. Für jede column_spec Gruppe wird ein Datenset zurückgegeben welches
  5969. durch einen Kommentar getrennt wird. Dieser Kommentar repräsentiert
  5970. die column_spec.<br>
  5971. Syntax: &lt;device&gt;:&lt;reading&gt;:&lt;default&gt;:&lt;fn&gt;:&lt;regexp&gt;<br>
  5972. <ul>
  5973. <li>&lt;device&gt;<br>
  5974. Der Name des Devices. Achtung: Gross/Kleinschreibung beachten!<br>
  5975. Es kann ein % als Jokerzeichen angegeben werden.</li>
  5976. <li>&lt;reading&gt;<br>
  5977. Das Reading des angegebenen Devices zur Datenselektion.<br>
  5978. Es kann ein % als Jokerzeichen angegeben werden.<br>
  5979. Achtung: Gross/Kleinschreibung beachten!
  5980. </li>
  5981. <li>&lt;default&gt;<br>
  5982. Zur Zeit noch nicht implementiert.
  5983. </li>
  5984. <li>&lt;fn&gt;
  5985. Angabe einer speziellen Funktion:
  5986. <ul>
  5987. <li>int<br>
  5988. Ermittelt den Zahlenwert ab dem Anfang der Zeichenkette aus der
  5989. Spalte "VALUE". Benutzt z.B. für Ausprägungen wie 10%.
  5990. </li>
  5991. <li>int&lt;digit&gt;<br>
  5992. Ermittelt den Zahlenwert ab dem Anfang der Zeichenkette aus der
  5993. Spalte "VALUE", inclusive negativen Vorzeichen und Dezimaltrenner.
  5994. Benutzt z.B. für Auspägungen wie -5.7&deg;C.
  5995. </li>
  5996. <li>delta-h / delta-d<br>
  5997. Ermittelt die relative Veränderung eines Zahlenwertes pro Stunde
  5998. oder pro Tag. Wird benutzt z.B. für Spalten die einen
  5999. hochlaufenden Zähler enthalten wie im Falle für ein KS300 Regenzähler
  6000. oder dem 1-wire Modul OWCOUNT.
  6001. </li>
  6002. <li>delta-ts<br>
  6003. Ermittelt die vergangene Zeit zwischen dem letzten und dem aktuellen Logeintrag
  6004. in Sekunden und ersetzt damit den originalen Wert.
  6005. </li>
  6006. </ul></li>
  6007. <li>&lt;regexp&gt;<br>
  6008. Diese Zeichenkette wird als Perl Befehl ausgewertet.
  6009. Die regexp wird vor dem angegebenen &lt;fn&gt; Parameter ausgeführt.
  6010. <br>
  6011. Bitte zur Beachtung: Diese Zeichenkette darf keine Leerzeichen
  6012. enthalten da diese sonst als &lt;column_spec&gt; Trennung
  6013. interpretiert werden und alles nach dem Leerzeichen als neue
  6014. &lt;column_spec&gt; gesehen wird.<br>
  6015. <b>Schlüsselwörter</b>
  6016. <li>$val ist der aktuelle Wert die die Datenbank für ein Device/Reading ausgibt.</li>
  6017. <li>$ts ist der aktuelle Timestamp des Logeintrages.</li>
  6018. <li>Wird als $val das Schlüsselwort "hide" zurückgegeben, so wird dieser Logeintrag nicht
  6019. ausgegeben, trotzdem aber für die Zeitraumberechnung verwendet.</li>
  6020. <li>Wird als $val das Schlüsselwort "ignore" zurückgegeben, so wird dieser Logeintrag
  6021. nicht für eine Folgeberechnung verwendet.</li>
  6022. </li>
  6023. </ul></li>
  6024. </ul>
  6025. <br><br>
  6026. <b>Beispiele:</b>
  6027. <ul>
  6028. <li><code>get myDbLog - - 2012-11-10 2012-11-20 KS300:temperature</code></li>
  6029. <li><code>get myDbLog current ALL - - %:temperature</code></li><br>
  6030. Damit erhält man alle aktuellen Readings "temperature" von allen in der DB geloggten Devices.
  6031. Achtung: bei Nutzung von Jokerzeichen auf die history-Tabelle kann man sein FHEM aufgrund langer Laufzeit lahmlegen!
  6032. <li><code>get myDbLog - - 2012-11-10_10 2012-11-10_20 KS300:temperature::int1</code><br>
  6033. gibt Daten aus von 10Uhr bis 20Uhr am 10.11.2012</li>
  6034. <li><code>get myDbLog - all 2012-11-10 2012-11-20 KS300:temperature</code></li>
  6035. <li><code>get myDbLog - - 2012-11-10 2012-11-20 KS300:temperature KS300:rain::delta-h KS300:rain::delta-d</code></li>
  6036. <li><code>get myDbLog - - 2012-11-10 2012-11-20 MyFS20:data:::$val=~s/(on|off).*/$1eq"on"?1:0/eg</code><br>
  6037. gibt 1 zurück für alle Ausprägungen von on* (on|on-for-timer etc) und 0 für alle off*</li>
  6038. <li><code>get myDbLog - - 2012-11-10 2012-11-20 Bodenfeuchte:data:::$val=~s/.*B:\s([-\.\d]+).*/$1/eg</code><br>
  6039. Beispiel von OWAD: Ein Wert wie z.B.: <code>"A: 49.527 % B: 66.647 % C: 9.797 % D: 0.097 V"</code><br>
  6040. und die Ausgabe ist für das Reading B folgende: <code>2012-11-20_10:23:54 66.647</code></li>
  6041. <li><code>get DbLog - - 2013-05-26 2013-05-28 Pumpe:data::delta-ts:$val=~s/on/hide/</code><br>
  6042. Realisierung eines Betriebsstundenzählers. Durch delta-ts wird die Zeit in Sek zwischen den Log-
  6043. Einträgen ermittelt. Die Zeiten werden bei den on-Meldungen nicht ausgegeben welche einer Abschaltzeit
  6044. entsprechen würden.</li>
  6045. </ul>
  6046. <br><br>
  6047. </ul>
  6048. <b>Get</b> für die Nutzung von webcharts
  6049. <ul>
  6050. <code>get &lt;name&gt; &lt;infile&gt; &lt;outfile&gt; &lt;from&gt;
  6051. &lt;to&gt; &lt;device&gt; &lt;querytype&gt; &lt;xaxis&gt; &lt;yaxis&gt; &lt;savename&gt; </code>
  6052. <br><br>
  6053. Liest Daten aus der Datenbank aus und gibt diese in JSON formatiert aus. Wird für das Charting Frontend genutzt
  6054. <br>
  6055. <ul>
  6056. <li>&lt;name&gt;<br>
  6057. Der Name des definierten DbLogs, so wie er in der fhem.cfg angegeben wurde.</li>
  6058. <li>&lt;in&gt;<br>
  6059. Ein Dummy Parameter um eine Kompatibilität zum Filelog herzustellen.
  6060. Dieser Parameter ist immer auf <code>-</code> zu setzen.</li>
  6061. <li>&lt;out&gt;<br>
  6062. Ein Dummy Parameter um eine Kompatibilität zum Filelog herzustellen.
  6063. Dieser Parameter ist auf <code>webchart</code> zu setzen um die Charting Get Funktion zu nutzen.
  6064. </li>
  6065. <li>&lt;from&gt; / &lt;to&gt;<br>
  6066. Wird benutzt um den Zeitraum der Daten einzugrenzen. Es ist das folgende
  6067. Zeitformat zu benutzen:<br>
  6068. <ul><code>YYYY-MM-DD_HH24:MI:SS</code></ul></li>
  6069. <li>&lt;device&gt;<br>
  6070. Ein String, der das abzufragende Device darstellt.</li>
  6071. <li>&lt;querytype&gt;<br>
  6072. Ein String, der die zu verwendende Abfragemethode darstellt. Zur Zeit unterstützte Werte sind: <br>
  6073. <code>getreadings</code> um für ein bestimmtes device alle Readings zu erhalten<br>
  6074. <code>getdevices</code> um alle verfügbaren devices zu erhalten<br>
  6075. <code>timerange</code> um Chart-Daten abzufragen. Es werden die Parameter 'xaxis', 'yaxis', 'device', 'to' und 'from' benötigt<br>
  6076. <code>savechart</code> um einen Chart unter Angabe eines 'savename' und seiner zugehörigen Konfiguration abzuspeichern<br>
  6077. <code>deletechart</code> um einen zuvor gespeicherten Chart unter Angabe einer id zu löschen<br>
  6078. <code>getcharts</code> um eine Liste aller gespeicherten Charts zu bekommen.<br>
  6079. <code>getTableData</code> um Daten aus der Datenbank abzufragen und in einer Tabelle darzustellen. Benötigt paging Parameter wie start und limit.<br>
  6080. <code>hourstats</code> um Statistiken für einen Wert (yaxis) für eine Stunde abzufragen.<br>
  6081. <code>daystats</code> um Statistiken für einen Wert (yaxis) für einen Tag abzufragen.<br>
  6082. <code>weekstats</code> um Statistiken für einen Wert (yaxis) für eine Woche abzufragen.<br>
  6083. <code>monthstats</code> um Statistiken für einen Wert (yaxis) für einen Monat abzufragen.<br>
  6084. <code>yearstats</code> um Statistiken für einen Wert (yaxis) für ein Jahr abzufragen.<br>
  6085. </li>
  6086. <li>&lt;xaxis&gt;<br>
  6087. Ein String, der die X-Achse repräsentiert</li>
  6088. <li>&lt;yaxis&gt;<br>
  6089. Ein String, der die Y-Achse repräsentiert</li>
  6090. <li>&lt;savename&gt;<br>
  6091. Ein String, unter dem ein Chart in der Datenbank gespeichert werden soll</li>
  6092. <li>&lt;chartconfig&gt;<br>
  6093. Ein jsonstring der den zu speichernden Chart repräsentiert</li>
  6094. <li>&lt;pagingstart&gt;<br>
  6095. Ein Integer um den Startwert für die Abfrage 'getTableData' festzulegen</li>
  6096. <li>&lt;paginglimit&gt;<br>
  6097. Ein Integer um den Limitwert für die Abfrage 'getTableData' festzulegen</li>
  6098. </ul>
  6099. <br><br>
  6100. Beispiele:
  6101. <ul>
  6102. <li><code>get logdb - webchart "" "" "" getcharts</code><br>
  6103. Liefert alle gespeicherten Charts aus der Datenbank</li>
  6104. <li><code>get logdb - webchart "" "" "" getdevices</code><br>
  6105. Liefert alle verfügbaren Devices aus der Datenbank</li>
  6106. <li><code>get logdb - webchart "" "" ESA2000_LED_011e getreadings</code><br>
  6107. Liefert alle verfügbaren Readings aus der Datenbank unter Angabe eines Gerätes</li>
  6108. <li><code>get logdb - webchart 2013-02-11_00:00:00 2013-02-12_00:00:00 ESA2000_LED_011e timerange TIMESTAMP day_kwh</code><br>
  6109. Liefert Chart-Daten, die auf folgenden Parametern basieren: 'xaxis', 'yaxis', 'device', 'to' und 'from'<br>
  6110. Die Ausgabe erfolgt als JSON, z.B.: <code>[{'TIMESTAMP':'2013-02-11 00:10:10','VALUE':'0.22431388090756'},{'TIMESTAMP'.....}]</code></li>
  6111. <li><code>get logdb - webchart 2013-02-11_00:00:00 2013-02-12_00:00:00 ESA2000_LED_011e savechart TIMESTAMP day_kwh tageskwh</code><br>
  6112. Speichert einen Chart unter Angabe eines 'savename' und seiner zugehörigen Konfiguration</li>
  6113. <li><code>get logdb - webchart "" "" "" deletechart "" "" 7</code><br>
  6114. Löscht einen zuvor gespeicherten Chart unter Angabe einer id</li>
  6115. </ul>
  6116. <br><br>
  6117. </ul>
  6118. <a name="DbLogattr"></a>
  6119. <b>Attribute</b>
  6120. <br><br>
  6121. <ul><b>addStateEvent</b>
  6122. <ul>
  6123. <code>attr &lt;device&gt; addStateEvent [0|1]
  6124. </code><br>
  6125. Bekanntlich wird normalerweise bei einem Event mit dem Reading "state" der state-String entfernt, d.h.
  6126. der Event ist nicht zum Beispiel "state: on" sondern nur "on". <br>
  6127. Meistens ist es aber hilfreich in DbLog den kompletten Event verarbeiten zu können. Deswegen übernimmt DbLog per Default
  6128. den Event inklusive dem Reading-String "state". <br>
  6129. In einigen Fällen, z.B. alten oder speziellen Modulen, ist es allerdings wünschenswert den state-String wie gewöhnlich
  6130. zu entfernen. In diesen Fällen bitte addStateEvent = "0" setzen.
  6131. Versuchen sie bitte diese Einstellung, falls es mit dem Standard Probleme geben sollte.
  6132. <br>
  6133. </ul>
  6134. </ul>
  6135. <br>
  6136. <ul><b>asyncMode</b>
  6137. <ul>
  6138. <code>attr &lt;device&gt; asyncMode [1|0]
  6139. </code><br>
  6140. Dieses Attribut stellt den Arbeitsmodus von DbLog ein. Im asynchronen Modus (asyncMode=1), werden die zu speichernden Events zunächst in Speicher
  6141. gecacht. Nach Ablauf der Synchronisationszeit (Attribut syncInterval) oder bei Erreichen der maximalen Anzahl der Datensätze im Cache
  6142. (Attribut cacheLimit) werden die gecachten Events im Block in die Datenbank geschrieben.
  6143. Ist die Datenbank nicht verfügbar, werden die Events weiterhin im Speicher gehalten und nach Ablauf des Syncintervalls in die Datenbank
  6144. geschrieben falls sie dann verfügbar ist. <br>
  6145. Im asynchronen Mode werden die Daten nicht blockierend mit einem separaten Hintergrundprozess in die Datenbank geschrieben.
  6146. Det Timeout-Wert für diesen Hintergrundprozess kann mit dem Attribut "timeout" (Default 86400s) eingestellt werden.
  6147. Im synchronen Modus (Normalmodus) werden die Events nicht gecacht und sofort in die Datenbank geschrieben. Ist die Datenbank nicht
  6148. verfügbar gehen sie verloren.<br>
  6149. </ul>
  6150. </ul>
  6151. <br>
  6152. <ul><b>commitMode</b>
  6153. <ul>
  6154. <code>attr &lt;device&gt; commitMode [basic_ta:on | basic_ta:off | ac:on_ta:on | ac:on_ta:off | ac:off_ta:on]
  6155. </code><br>
  6156. Ändert die Verwendung der Datenbank Autocommit- und/oder Transaktionsfunktionen.
  6157. Wird Transaktion "aus" verwendet, werden im asynchronen Modus nicht gespeicherte Datensätze nicht an den Cache zurück
  6158. gegeben.
  6159. Dieses Attribut ist ein advanced feature und sollte nur im konkreten Bedarfs- bzw. Supportfall geändert werden.<br><br>
  6160. <ul>
  6161. <li>basic_ta:on - Autocommit Servereinstellung / Transaktion ein (default) </li>
  6162. <li>basic_ta:off - Autocommit Servereinstellung / Transaktion aus </li>
  6163. <li>ac:on_ta:on - Autocommit ein / Transaktion ein </li>
  6164. <li>ac:on_ta:off - Autocommit ein / Transaktion aus </li>
  6165. <li>ac:off_ta:on - Autocommit aus / Transaktion ein (Autocommit "aus" impliziert Transaktion "ein") </li>
  6166. </ul>
  6167. </ul>
  6168. </ul>
  6169. <br>
  6170. <ul><b>cacheEvents</b>
  6171. <ul>
  6172. <code>attr &lt;device&gt; cacheEvents [2|1|0]
  6173. </code><br>
  6174. <ul>
  6175. <li>cacheEvents=1: es werden Events für das Reading CacheUsage erzeugt wenn ein Event zum Cache hinzugefügt wurde. </li>
  6176. <li>cacheEvents=2: es werden Events für das Reading CacheUsage erzeugt wenn im asynchronen Mode der Schreibzyklus in die
  6177. Datenbank beginnt. CacheUsage enthält zu diesem Zeitpunkt die Anzahl der in die Datenbank zu schreibenden
  6178. Datensätze. </li><br>
  6179. </ul>
  6180. </ul>
  6181. </ul>
  6182. <br>
  6183. <ul><b>cacheLimit</b>
  6184. <ul>
  6185. <code>
  6186. attr &lt;device&gt; cacheLimit &lt;n&gt;
  6187. </code><br>
  6188. Im asynchronen Logmodus wird der Cache in die Datenbank weggeschrieben und geleert wenn die Anzahl &lt;n&gt; Datensätze
  6189. im Cache erreicht ist (Default: 500). Der Timer des asynchronen Logmodus wird dabei neu auf den Wert des Attributs "syncInterval"
  6190. gesetzt. Im Fehlerfall wird ein erneuter Schreibversuch frühestens nach syncInterval/2 gestartet. <br>
  6191. </ul>
  6192. </ul>
  6193. <br>
  6194. <ul><b>colEvent</b>
  6195. <ul>
  6196. <code>
  6197. attr &lt;device&gt; colEvent &lt;n&gt;
  6198. </code><br>
  6199. Die Feldlänge für das DB-Feld EVENT wird userspezifisch angepasst. Mit dem Attribut kann der Default-Wert im Modul
  6200. verändert werden wenn die Feldlänge in der Datenbank manuell geändert wurde. Mit colEvent=0 wird das Datenbankfeld
  6201. EVENT nicht gefüllt. <br>
  6202. <b>Hinweis:</b> <br>
  6203. Mit gesetztem Attribut gelten alle Feldlängenbegrenzungen auch für SQLite DB wie im Internal COLUMNS angezeigt ! <br>
  6204. </ul>
  6205. </ul>
  6206. <br>
  6207. <ul><b>colReading</b>
  6208. <ul>
  6209. <code>
  6210. attr &lt;device&gt; colReading &lt;n&gt;
  6211. </code><br>
  6212. Die Feldlänge für das DB-Feld READING wird userspezifisch angepasst. Mit dem Attribut kann der Default-Wert im Modul
  6213. verändert werden wenn die Feldlänge in der Datenbank manuell geändert wurde. Mit colReading=0 wird das Datenbankfeld
  6214. READING nicht gefüllt. <br>
  6215. <b>Hinweis:</b> <br>
  6216. Mit gesetztem Attribut gelten alle Feldlängenbegrenzungen auch für SQLite DB wie im Internal COLUMNS angezeigt ! <br>
  6217. </ul>
  6218. </ul>
  6219. <br>
  6220. <ul><b>colValue</b>
  6221. <ul>
  6222. <code>
  6223. attr &lt;device&gt; colValue &lt;n&gt;
  6224. </code><br>
  6225. Die Feldlänge für das DB-Feld VALUE wird userspezifisch angepasst. Mit dem Attribut kann der Default-Wert im Modul
  6226. verändert werden wenn die Feldlänge in der Datenbank manuell geändert wurde. Mit colValue=0 wird das Datenbankfeld
  6227. VALUE nicht gefüllt. <br>
  6228. <b>Hinweis:</b> <br>
  6229. Mit gesetztem Attribut gelten alle Feldlängenbegrenzungen auch für SQLite DB wie im Internal COLUMNS angezeigt ! <br>
  6230. </ul>
  6231. </ul>
  6232. <br>
  6233. <ul><b>DbLogType</b>
  6234. <ul>
  6235. <code>
  6236. attr &lt;device&gt; DbLogType [Current|History|Current/History|SampleFill/History]
  6237. </code><br>
  6238. Dieses Attribut legt fest, welche Tabelle oder Tabellen in der Datenbank genutzt werden sollen. Ist dieses Attribut nicht gesetzt, wird
  6239. per default die Einstellung <i>history</i> verwendet. <br><br>
  6240. Bedeutung der Einstellungen sind: <br><br>
  6241. <ul>
  6242. <table>
  6243. <colgroup> <col width=10%> <col width=90%> </colgroup>
  6244. <tr><td> <b>Current</b> </td><td>Events werden nur in die current-Tabelle geloggt.
  6245. Die current-Tabelle wird bei der SVG-Erstellung ausgewertet. </td></tr>
  6246. <tr><td> <b>History</b> </td><td>Events werden nur in die history-Tabelle geloggt. Es wird keine DropDown-Liste mit Vorschlägen bei der SVG-Erstellung
  6247. erzeugt. </td></tr>
  6248. <tr><td> <b>Current/History</b> </td><td>Events werden sowohl in die current- also auch in die hitory Tabelle geloggt.
  6249. Die current-Tabelle wird bei der SVG-Erstellung ausgewertet.</td></tr>
  6250. <tr><td> <b>SampleFill/History</b> </td><td>Events werden nur in die history-Tabelle geloggt. Die current-Tabelle wird bei der SVG-Erstellung ausgewertet und
  6251. kann zur Erzeugung einer DropDown-Liste mittels einem
  6252. <a href="#DbRep">DbRep-Device</a> <br> "set &lt;DbRep-Name&gt; tableCurrentFillup" mit
  6253. einem einstellbaren Extract der history-Tabelle gefüllt werden (advanced Feature). </td></tr>
  6254. </table>
  6255. </ul>
  6256. <br>
  6257. <br>
  6258. <b>Hinweis:</b> <br>
  6259. Die Current-Tabelle muß genutzt werden um eine Device:Reading-DropDownliste zur Erstellung eines
  6260. SVG-Plots zu erhalten. <br>
  6261. </ul>
  6262. </ul>
  6263. <br>
  6264. <ul><b>DbLogSelectionMode</b>
  6265. <ul>
  6266. <code>
  6267. attr &lt;device&gt; DbLogSelectionMode [Exclude|Include|Exclude/Include]
  6268. </code><br>
  6269. Dieses, fuer DbLog-Devices spezifische Attribut beeinflußt, wie die Device-spezifischen Attributes
  6270. DbLogExclude und DbLogInclude (s.u.) ausgewertet werden.<br>
  6271. Fehlt dieses Attribut, wird dafuer "Exclude" als Default angenommen. <br>
  6272. <ul>
  6273. <li>Exclude: DbLog verhaelt sich wie bisher auch, alles was ueber die RegExp im DEF angegeben ist, wird geloggt, bis auf das,
  6274. was ueber die RegExp in DbLogExclude ausgeschlossen wird. <br>
  6275. Das Attribut DbLogInclude wird in diesem Fall nicht beruecksichtigt</li>
  6276. <li>Include: Es wird nur das geloggt was ueber die RegExp in DbLogInclude (im Quelldevice) eingeschlossen wird. <br>
  6277. Das Attribut DbLogExclude wird in diesem Fall ebenso wenig beruecksichtigt wie die Regex im DEF. Auch
  6278. der Devicename (des Quelldevice) geht in die Auswertung nicht mit ein. </li>
  6279. <li>Exclude/Include: Funktioniert im Wesentlichen wie "Exclude", nur das sowohl DbLogExclude als auch DbLogInclude
  6280. geprueft werden. Readings die durch DbLogExclude zwar ausgeschlossen wurden, mit DbLogInclude aber wiederum eingeschlossen werden,
  6281. werden somit dennoch geloggt. </li>
  6282. </ul>
  6283. </ul>
  6284. </ul>
  6285. <br>
  6286. <ul><b>DbLogInclude</b>
  6287. <ul>
  6288. <code>
  6289. attr &lt;device&gt; DbLogInclude regex:MinInterval,[regex:MinInterval] ...
  6290. </code><br>
  6291. Wenn DbLog genutzt wird, wird in allen Devices das Attribut <i>DbLogInclude</i> propagiert.
  6292. DbLogInclude funktioniert im Endeffekt genau wie DbLogExclude, ausser dass eben readings mit diesen RegExp
  6293. in das Logging eingeschlossen werden koennen, statt ausgeschlossen.
  6294. Siehe dazu auch das DbLog-Device-Spezifische Attribut DbLogSelectionMode, das beeinflußt wie
  6295. DbLogExclude und DbLogInclude ausgewertet werden. <br>
  6296. <b>Beispiel</b> <br>
  6297. <code>attr MyDevice1 DbLogInclude .*</code> <br>
  6298. <code>attr MyDevice2 DbLogInclude state,(floorplantext|MyUserReading):300,battery:3600</code>
  6299. </ul>
  6300. </ul>
  6301. <br>
  6302. <ul><b>DbLogExclude</b>
  6303. <ul>
  6304. <code>
  6305. attr &lt;device&gt; DbLogExclude regex:MinInterval,[regex:MinInterval] ...
  6306. </code><br>
  6307. Wenn DbLog genutzt wird, wird in alle Devices das Attribut <i>DbLogExclude</i> propagiert.
  6308. Der Wert des Attributes wird als Regexp ausgewertet und schliesst die damit matchenden Readings von einem Logging aus.
  6309. Einzelne Regexp werden durch Kommata getrennt. Ist MinIntervall angegeben, so wird der Logeintrag nur
  6310. dann nicht geloggt, wenn das Intervall noch nicht erreicht und der Wert des Readings sich nicht verändert hat. <br><br>
  6311. <b>Beispiel</b> <br>
  6312. <code>attr MyDevice1 DbLogExclude .*</code> <br>
  6313. <code>attr MyDevice2 DbLogExclude state,(floorplantext|MyUserReading):300,battery:3600</code>
  6314. </ul>
  6315. </ul>
  6316. <br>
  6317. <ul><b>excludeDevs</b>
  6318. <ul>
  6319. <code>
  6320. attr &lt;device&gt; excludeDevs &lt;devspec1&gt;[#Reading],&lt;devspec2&gt;[#Reading],&lt;devspec...&gt;
  6321. </code><br>
  6322. Die Device/Reading-Kombinationen "devspec1#Reading", "devspec2#Reading" bis "devspec..." werden vom Logging in die
  6323. Datenbank global ausgeschlossen. <br>
  6324. Die Angabe eines auszuschließenden Readings ist optional. <br>
  6325. Somit können Device/Readings explizit bzw. konsequent vom Logging ausgeschlossen werden ohne Berücksichtigung anderer
  6326. Excludes oder Includes (z.B. im DEF).
  6327. Die auszuschließenden Devices können als <a href="#devspec">Geräte-Spezifikation</a> angegeben werden.
  6328. Für weitere Details bezüglich devspec siehe <a href="#devspec">Geräte-Spezifikation</a>. <br><br>
  6329. <b>Beispiel</b> <br>
  6330. <code>
  6331. attr &lt;device&gt; excludeDevs global,Log.*,Cam.*,TYPE=DbLog
  6332. </code><br>
  6333. # Es werden die Devices global bzw. Devices beginnend mit "Log" oder "Cam" bzw. Devices vom Typ "DbLog" vom Logging ausgeschlossen. <br>
  6334. <code>
  6335. attr &lt;device&gt; excludeDevs .*#.*Wirkleistung.*
  6336. </code><br>
  6337. # Es werden alle Device/Reading-Kombinationen mit "Wirkleistung" im Reading vom Logging ausgeschlossen. <br>
  6338. <code>
  6339. attr &lt;device&gt; excludeDevs SMA_Energymeter#Bezug_WirkP_Zaehler_Diff
  6340. </code><br>
  6341. # Es wird der Event mit Device "SMA_Energymeter" und Reading "Bezug_WirkP_Zaehler_Diff" vom Logging ausgeschlossen. <br>
  6342. </ul>
  6343. </ul>
  6344. <br>
  6345. <ul><b>expimpdir</b>
  6346. <ul>
  6347. <code>
  6348. attr &lt;device&gt; expimpdir &lt;directory&gt;
  6349. </code><br>
  6350. In diesem Verzeichnis wird das Cachefile beim Export angelegt bzw. beim Import gesucht. Siehe set-Kommandos
  6351. <a href="#DbLogsetexportCache">"exportCache"</a> bzw. "importCachefile". Das Default-Verzeichnis ist "(global->modpath)/log/".
  6352. Das im Attribut angegebene Verzeichnis muss vorhanden und beschreibbar sein. <br><br>
  6353. <b>Beispiel</b> <br>
  6354. <code>
  6355. attr &lt;device&gt; expimpdir /opt/fhem/cache/
  6356. </code><br>
  6357. </ul>
  6358. </ul>
  6359. <br>
  6360. <ul><b>exportCacheAppend</b>
  6361. <ul>
  6362. <code>
  6363. attr &lt;device&gt; exportCacheAppend [1|0]
  6364. </code><br>
  6365. Wenn gesetzt, wird beim Export des Cache ("set &lt;device&gt; exportCache") der Cacheinhalt an das neueste bereits vorhandene
  6366. Exportfile angehängt. Ist noch kein Exportfile vorhanden, wird es neu angelegt. <br>
  6367. Ist das Attribut nicht gesetzt, wird bei jedem Exportvorgang ein neues Exportfile angelegt. (default)<br/>
  6368. </ul>
  6369. </ul>
  6370. <br>
  6371. <ul><b>noNotifyDev</b>
  6372. <ul>
  6373. <code>
  6374. attr &lt;device&gt; noNotifyDev [1|0]
  6375. </code><br>
  6376. Erzwingt dass NOTIFYDEV nicht gesetzt und somit nicht verwendet wird.<br>
  6377. </ul>
  6378. </ul>
  6379. <br>
  6380. <ul><b>noSupportPK</b>
  6381. <ul>
  6382. <code>
  6383. attr &lt;device&gt; noSupportPK [1|0]
  6384. </code><br>
  6385. Deaktiviert die programmtechnische Unterstützung eines gesetzten Primary Key durch das Modul.<br>
  6386. </ul>
  6387. </ul>
  6388. <br>
  6389. <ul><b>shutdownWait</b>
  6390. <ul>
  6391. <code>
  6392. attr &lt;device&gt; shutdownWait <n>
  6393. </code><br>
  6394. FHEM wartet während des shutdowns fuer n Sekunden, um die Datenbank korrekt zu beenden<br/>
  6395. </ul>
  6396. </ul>
  6397. <br>
  6398. <ul><b>showproctime</b>
  6399. <ul>
  6400. <code>attr &lt;device&gt; showproctime [1|0]
  6401. </code><br>
  6402. Wenn gesetzt, zeigt das Reading "sql_processing_time" die benötigte Abarbeitungszeit (in Sekunden) für die SQL-Ausführung der
  6403. durchgeführten Funktion. Dabei wird nicht ein einzelnes SQL-Statement, sondern die Summe aller notwendigen SQL-Abfragen innerhalb der
  6404. jeweiligen Funktion betrachtet. Das Reading "background_processing_time" zeigt die im Kindprozess BlockingCall verbrauchte Zeit.<br>
  6405. </ul>
  6406. </ul>
  6407. <br>
  6408. <ul><b>showNotifyTime</b>
  6409. <ul>
  6410. <code>attr &lt;device&gt; showNotifyTime [1|0]
  6411. </code><br>
  6412. Wenn gesetzt, zeigt das Reading "notify_processing_time" die benötigte Abarbeitungszeit (in Sekunden) für die
  6413. Abarbeitung der DbLog Notify-Funktion. Das Attribut ist für Performance Analysen geeignet und hilft auch die Unterschiede
  6414. im Zeitbedarf bei der Umschaltung des synchronen in den asynchronen Modus festzustellen. <br>
  6415. </ul>
  6416. </ul>
  6417. <br>
  6418. <ul><b>syncEvents</b>
  6419. <ul>
  6420. <code>attr &lt;device&gt; syncEvents [1|0]
  6421. </code><br>
  6422. es werden Events für Reading NextSync erzeugt. <br>
  6423. </ul>
  6424. </ul>
  6425. <br>
  6426. <ul><b>syncInterval</b>
  6427. <ul>
  6428. <code>attr &lt;device&gt; syncInterval &lt;n&gt;
  6429. </code><br>
  6430. Wenn DbLog im asynchronen Modus betrieben wird (Attribut asyncMode=1), wird mit diesem Attribut das Intervall in Sekunden zur Speicherung
  6431. der im Speicher gecachten Events in die Datenbank eingestellt. Der Defaultwert ist 30 Sekunden. <br>
  6432. </ul>
  6433. </ul>
  6434. <br>
  6435. <ul><b>suppressAddLogV3</b>
  6436. <ul>
  6437. <code>attr &lt;device&gt; suppressAddLogV3 [1|0]
  6438. </code><br>
  6439. Wenn gesetzt werden verbose3-Logeinträge durch die addLog-Funktion unterdrückt. <br>
  6440. </ul>
  6441. </ul>
  6442. <br>
  6443. <ul><b>suppressUndef</b>
  6444. <ul>
  6445. <code>attr &lt;device&gt; ignoreUndef <n>
  6446. </code><br>
  6447. Unterdrueckt alle undef Werte die durch eine Get-Anfrage zb. Plot aus der Datenbank selektiert werden <br>
  6448. <b>Beispiel</b> <br>
  6449. <code>#DbLog eMeter:power:::$val=($val>1500)?undef:$val</code>
  6450. </ul>
  6451. </ul>
  6452. <br>
  6453. <ul><b>timeout</b>
  6454. <ul>
  6455. <code>
  6456. attr &lt;device&gt; timeout <n>
  6457. </code><br>
  6458. Setzt den Timeout-Wert für den Schreibzyklus in die Datenbank im asynchronen Modus (default 86400s). <br>
  6459. </ul>
  6460. </ul>
  6461. <br>
  6462. <ul><b>useCharfilter</b>
  6463. <ul>
  6464. <code>
  6465. attr &lt;device&gt; useCharfilter [0|1] <n>
  6466. </code><br>
  6467. wenn gesetzt, werden nur ASCII Zeichen von 32 bis 126 im Event akzeptiert. (default: 0) <br>
  6468. Das sind die Zeichen " A-Za-z0-9!"#$%&'()*+,-.\/:;<=>?@[\\]^_`{|}~". <br>
  6469. Umlaute und "€" werden umgesetzt (z.B. ä nach ae, € nach EUR). <br>
  6470. </ul>
  6471. </ul>
  6472. <br>
  6473. <ul><b>valueFn</b>
  6474. <ul>
  6475. <code>
  6476. attr &lt;device&gt; valueFn {}
  6477. </code><br>
  6478. Es kann über einen Perl-Ausdruck auf die Variablen $TIMESTAMP, $DEVICE, $DEVICETYPE, $READING, $VALUE (Wert des Readings) und
  6479. $UNIT (Einheit des Readingswert) zugegriffen werden und diese verändern, d.h. die veränderten Werte werden geloggt.
  6480. Außerdem hat man lesenden Zugriff auf $EVENT für eine Auswertung im Perl-Ausdruck.
  6481. Diese Variable kann aber nicht verändert werden. <br>
  6482. Soll $TIMESTAMP verändert werden, muss die Form "yyyy-mm-dd hh:mm:ss" eingehalten werden, ansonsten wird der
  6483. geänderte $timestamp nicht übernommen.
  6484. Zusätzlich kann durch Setzen der Variable "$IGNORE=1" ein Datensatz vom Logging ausgeschlossen werden. <br><br>
  6485. <b>Beispiele</b> <br>
  6486. <code>
  6487. attr &lt;device&gt; valueFn {if ($DEVICE eq "living_Clima" && $VALUE eq "off" ){$VALUE=0;} elsif ($DEVICE eq "e-power"){$VALUE= sprintf "%.1f", $VALUE;}}
  6488. </code> <br>
  6489. # ändert den Reading-Wert des Gerätes "living_Clima" von "off" zu "0" und rundet den Wert vom Gerät "e-power" <br><br>
  6490. <code>
  6491. attr &lt;device&gt; valueFn {if ($DEVICE eq "SMA_Energymeter" && $READING eq "state"){$IGNORE=1;}}
  6492. </code><br>
  6493. # der Datensatz wird nicht geloggt wenn Device = "SMA_Energymeter" und das Reading = "state" ist <br><br>
  6494. <code>
  6495. attr &lt;device&gt; valueFn {if ($DEVICE eq "Dum.Energy" && $READING eq "TotalConsumption"){$UNIT="W";}}
  6496. </code><br>
  6497. # setzt die Einheit des Devices "Dum.Energy" auf "W" wenn das Reading = "TotalConsumption" ist <br><br>
  6498. </ul>
  6499. </ul>
  6500. <br>
  6501. <ul><b>verbose4Devs</b>
  6502. <ul>
  6503. <code>
  6504. attr &lt;device&gt; verbose4Devs &lt;device1&gt;,&lt;device2&gt;,&lt;device..&gt;
  6505. </code><br>
  6506. Mit verbose Level 4 werden nur Ausgaben bezüglich der in diesem Attribut aufgeführten Devices im Logfile protokolliert. Ohne dieses
  6507. Attribut werden mit verbose 4 Ausgaben aller relevanten Devices im Logfile protokolliert.
  6508. Die angegebenen Devices werden als Regex ausgewertet. <br>
  6509. <b>Beispiel</b> <br>
  6510. <code>
  6511. attr &lt;device&gt; verbose4Devs sys.*,.*5000.*,Cam.*,global
  6512. </code><br>
  6513. # Es werden Devices beginnend mit "sys", "Cam" bzw. Devices die "5000" enthalten und das Device "global" protokolliert falls verbose=4
  6514. eingestellt ist. <br>
  6515. </ul>
  6516. </ul>
  6517. <br>
  6518. </ul>
  6519. =end html_DE
  6520. =cut