93_DbRep.pm 730 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384
  1. ##########################################################################################################
  2. # $Id: 93_DbRep.pm 17703 2018-11-07 21:32:53Z DS_Starter $
  3. ##########################################################################################################
  4. # 93_DbRep.pm
  5. #
  6. # (c) 2016-2018 by Heiko Maaz
  7. # e-mail: Heiko dot Maaz at t-online dot de
  8. #
  9. # This Module can be used to select and report content of databases written by 93_DbLog module
  10. # in different manner.
  11. #
  12. # This script is part of fhem.
  13. #
  14. # Fhem is free software: you can redistribute it and/or modify
  15. # it under the terms of the GNU General Public License as published by
  16. # the Free Software Foundation, either version 2 of the License, or
  17. # (at your option) any later version.
  18. #
  19. # Fhem is distributed in the hope that it will be useful,
  20. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. # GNU General Public License for more details.
  23. #
  24. # You should have received a copy of the GNU General Public License
  25. # along with fhem. If not, see <http://www.gnu.org/licenses/>.
  26. #
  27. # Credits:
  28. # - viegener for some input
  29. # - some proposals to boost and improve SQL-Statements by JoeALLb
  30. # - function reduceLog created by Claudiu Schuster (rapster) was copied from DbLog (Version 3.12.3 08.10.2018)
  31. # and changed to meet the requirements of DbRep
  32. #
  33. ###########################################################################################################################
  34. #
  35. # Definition: define <name> DbRep <DbLog-Device>
  36. #
  37. # This module uses credentials of the DbLog-Device
  38. #
  39. ###########################################################################################################################
  40. package main;
  41. use strict;
  42. use warnings;
  43. use POSIX qw(strftime);
  44. use Time::HiRes qw(gettimeofday tv_interval);
  45. use Scalar::Util qw(looks_like_number);
  46. eval "use DBI;1" or my $DbRepMMDBI = "DBI";
  47. use DBI::Const::GetInfoType;
  48. use Blocking;
  49. use Color; # colorpicker Widget
  50. use Time::Local;
  51. use Encode qw(encode_utf8);
  52. use IO::Compress::Gzip qw(gzip $GzipError);
  53. use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
  54. # no if $] >= 5.018000, warnings => 'experimental';
  55. no if $] >= 5.017011, warnings => 'experimental::smartmatch';
  56. # Versions History intern
  57. our %DbRep_vNotesIntern = (
  58. "8.8.0" => "06.11.2018 first connect routine switched to DbRep_Main, get COLSET from DBLOG-instance, attribute 'fastStart' added ",
  59. "8.7.0" => "04.11.2018 attribute valueFilter applied to functions based on 'SELECT', 'UPDATE', 'DELETE' and 'valueFilter' generally applied to field 'VALUE' ",
  60. "8.6.0" => "29.10.2018 reduceLog use attributes device/reading (can be overwritten by set-options) ",
  61. "8.5.0" => "27.10.2018 versionNotes revised, EXCLUDE of reading/device possible (DbRep_specsForSql changed) ",
  62. "8.4.0" => "22.10.2018 countEntries separately for every reading if attribute \"countEntriesDetail\" is set, ".
  63. "versionNotes changed to support en/de, get dbValue as textfield-long ",
  64. "8.3.0" => "17.10.2018 reduceLog from DbLog integrated into DbRep, textField-long as default for sqlCmd, both attributes timeOlderThan and timeDiffToNow can be set at same time",
  65. "8.2.3" => "07.10.2018 check availability of DbLog-device at definition time of DbRep-device ",
  66. "8.2.2" => "07.10.2018 DbRep_getInitData changed, fix don't get the real min timestamp in rare cases ",
  67. "8.2.1" => "07.10.2018 \$hash->{dbloghash}{HELPER}{REOPEN_RUNS_UNTIL} contains time until DB is closed ",
  68. "8.2.0" => "05.10.2018 direct help for attributes ",
  69. "8.1.0" => "02.10.2018 new get versionNotes command ",
  70. "8.0.1" => "20.09.2018 DbRep_getInitData improved",
  71. "8.0.0" => "11.09.2018 get filesize in DbRep_WriteToDumpFile corrected, restoreMySQL for clientSide dumps, minor fixes ",
  72. "7.20.0" => "04.09.2018 deviceRename can operate a Device name with blank, e.g. 'current balance' as old device name ",
  73. "7.19.0" => "25.08.2018 attribute 'valueFilter' to filter datasets in fetchrows ",
  74. "7.18.2" => "02.08.2018 fix in fetchrow function (forum:#89886), fix highlighting ",
  75. "7.18.1" => "03.06.2018 commandref revised ",
  76. "7.18.0" => "02.06.2018 possible use of y:(\\d) for timeDiffToNow, timeOlderThan , minor fixes of timeOlderThan, delEntries considers executeBeforeDump,executeAfterDump ",
  77. "7.17.3" => "30.04.2018 writeToDB - readingname can be replaced by the value of attribute 'readingNameMap' ",
  78. "7.17.2" => "22.04.2018 fix don't writeToDB if device name contain '.' only, minor fix in DbReadingsVal ",
  79. "7.17.1" => "20.04.2018 fix '§' is deleted by carfilter ",
  80. "7.17.0" => "17.04.2018 new function DbReadingsVal ",
  81. "7.16.0" => "13.04.2018 new function dbValue (blocking) ",
  82. "7.15.2" => "12.04.2018 fix in setting MODEL, prevent fhem from crash if wrong timestamp '0000-00-00' found in db ",
  83. "7.15.1" => "11.04.2018 sqlCmd accept widget textField-long, Internal MODEL is set ",
  84. "7.15.0" => "24.03.2018 new command sqlSpecial ",
  85. "7.14.8" => "21.03.2018 fix no save into database if value=0 (DbRep_OutputWriteToDB) ",
  86. "7.14.7" => "21.03.2018 exportToFile,importFromFile can use file as an argument and executeBeforeDump, executeAfterDump is considered ",
  87. "7.14.6" => "18.03.2018 attribute expimpfile can use some kinds of wildcards (exportToFile, importFromFile adapted) ",
  88. "7.14.5" => "17.03.2018 perl warnings of DbLog \$dn,\$dt,\$evt,\$rd in changeval_Push & complex ",
  89. "7.14.4" => "11.03.2018 increased timeout of BlockingCall in DbRep_firstconnect ",
  90. "7.14.3" => "07.03.2018 DbRep_firstconnect changed - get lowest timestamp in database, DbRep_Connect deleted ",
  91. "7.14.2" => "04.03.2018 fix perl warning ",
  92. "7.14.1" => "01.03.2018 currentfillup_Push bugfix for PostgreSQL ",
  93. "7.14.0" => "26.02.2018 syncStandby ",
  94. "7.13.3" => "25.02.2018 commandref revised (forum:#84953) ",
  95. "7.13.2" => "24.02.2018 DbRep_firstconnect changed, bug fix in DbRep_collaggstr for aggregation = month ",
  96. "7.13.1" => "20.02.2018 commandref revised ",
  97. "7.13.0" => "17.02.2018 changeValue can handle perl code {} as 'new string' ",
  98. "7.12.0" => "16.02.2018 compression of dumpfile, restore of compressed files possible ",
  99. "7.11.0" => "12.02.2018 new command 'repairSQLite' to repair a corrupted SQLite database ",
  100. "7.10.0" => "10.02.2018 bugfix delete attr timeYearPeriod if set other time attributes, new 'changeValue' command ",
  101. "7.9.0" => "09.02.2018 new attribute 'avgTimeWeightMean' (time weight mean calculation), code review of selection routines, maxValue handle negative values correctly, one security second for correct create TimeArray in DbRep_normRelTime ",
  102. "7.8.1" => "04.02.2018 bugfix if IsDisabled (again), code review, bugfix last dataset is not selected if timestamp is fully set ('date time'), fix '\$runtime_string_next' = '\$runtime_string_next.999';' if \$runtime_string_next is part of sql-execute place holder AND contains date+time ",
  103. "7.8.0" => "04.02.2018 new command 'eraseReadings' ",
  104. "7.7.1" => "03.02.2018 minor fix in DbRep_firstconnect if IsDisabled ",
  105. "7.7.0" => "29.01.2018 attribute 'averageCalcForm', calculation sceme 'avgDailyMeanGWS', 'avgArithmeticMean' for averageValue ",
  106. "7.6.1" => "27.01.2018 new attribute 'sqlCmdHistoryLength' and 'fetchMarkDuplicates' for highlighting multiple datasets by fetchrows ",
  107. "7.6.0" => "26.01.2018 events containing '|' possible in fetchrows & delSeqDoublets, fetchrows displays multiple \$k entries with timestamp suffix \$k (as index), sqlCmdHistory (avaiable if sqlCmd was executed) ",
  108. "7.5.5" => "25.01.2018 minor change in delSeqDoublets ",
  109. "7.5.4" => "24.01.2018 delseqdoubl_DoParse reviewed to optimize memory usage, executeBeforeDump executeAfterDump now available for 'delSeqDoublets' ",
  110. "7.5.3" => "23.01.2018 new attribute 'ftpDumpFilesKeep', version management added to FTP-usage ",
  111. "7.5.2" => "23.01.2018 fix typo DumpRowsCurrrent, dumpFilesKeep can be set to '0', commandref revised ",
  112. "7.5.1" => "20.01.2018 DbRep_DumpDone changed to create background_processing_time before execute 'executeAfterProc' Commandref updated ",
  113. "7.5.0" => "16.01.2018 DbRep_OutputWriteToDB, set options display/writeToDB for (max|min|sum|average|diff)Value ",
  114. "7.4.1" => "14.01.2018 fix old dumpfiles not deleted by dumpMySQL clientSide ",
  115. "7.4.0" => "09.01.2018 dumpSQLite/restoreSQLite, backup/restore now available when DbLog-device has reopen xxxx running, executeBeforeDump executeAfterDump also available for optimizeTables, vacuum, restoreMySQL, restoreSQLite, attribute executeBeforeDump / executeAfterDump renamed to executeBeforeProc & executeAfterProc ",
  116. "7.3.1" => "08.01.2018 fix syntax error for perl < 5.20 ",
  117. "7.3.0" => "07.01.2018 DbRep-charfilter avoid control characters in datasets to export, impfile_Push errortext improved, expfile_DoParse changed to use aggregation for split selects in timeslices (avoid heavy memory consumption) ",
  118. "7.2.1" => "04.01.2018 bugfix month out of range that causes fhem crash ",
  119. "1.0.0" => "19.05.2016 Initial"
  120. );
  121. # Versions History extern:
  122. our %DbRep_vNotesExtern = (
  123. "8.8.0" => "06.11.2018 new attribute 'fastStart'. Usually every DbRep-device is making a short connect to its database when "
  124. ."FHEM is restarted. When this attribute is set, the initial connect is done when the DbRep-device is doing its "
  125. ."first task. ",
  126. "8.7.0" => "04.11.2018 attribute valueFilter applied to functions 'averageValue, changeValue, countEntries, delEntries, "
  127. ."delSeqDoublets, diffValue, exportToFile, fetchrows, maxValue, minValue, reduceLog, sumValue, syncStandby' ,"
  128. ." 'valueFilter' generally applied to database field 'VALUE' ",
  129. "8.6.0" => "29.10.2018 reduceLog use attributes device/reading (can be overwritten by set-options) ",
  130. "8.5.0" => "27.10.2018 devices and readings can be excluded by EXCLUDE-option in attributes \$reading/\$device ",
  131. "8.4.0" => "22.10.2018 New attribute \"countEntriesDetail\". Function countEntries creates number of datasets for every ".
  132. "reading separately if attribute \"countEntriesDetail\" is set. Get versionNotes changed to support en/de. ".
  133. "Function \"get dbValue\" opens an editor window ",
  134. "8.3.0" => "17.10.2018 reduceLog from DbLog integrated into DbRep, textField-long as default for sqlCmd, both attributes ".
  135. "timeOlderThan and timeDiffToNow can be set at same time -> the selection time between timeOlderThan ".
  136. "and timeDiffToNow can be calculated dynamically ",
  137. "8.2.2" => "07.10.2018 fix don't get the real min timestamp in rare cases ",
  138. "8.2.0" => "05.10.2018 direct help for attributes ",
  139. "8.1.0" => "01.10.2018 new get versionNotes command ",
  140. "8.0.0" => "11.09.2018 get filesize in DbRep_WriteToDumpFile corrected, restoreMySQL for clientSide dumps, minor fixes ",
  141. "7.20.0" => "04.09.2018 deviceRename can operate a Device name with blank, e.g. 'current balance' as old device name ",
  142. "7.19.0" => "25.08.2018 attribute 'valueFilter' to filter datasets in fetchrows ",
  143. "7.18.2" => "02.08.2018 fix in fetchrow function (forum:#89886), fix highlighting ",
  144. "7.18.0" => "02.06.2018 possible use of y:(\\d) for timeDiffToNow, timeOlderThan , minor fixes of timeOlderThan, delEntries considers executeBeforeDump,executeAfterDump ",
  145. "7.17.3" => "30.04.2018 writeToDB - readingname can be replaced by the value of attribute 'readingNameMap' ",
  146. "7.17.0" => "17.04.2018 new function DbReadingsVal ",
  147. "7.16.0" => "13.04.2018 new function dbValue (blocking) ",
  148. "7.15.2" => "12.04.2018 fix in setting MODEL, prevent fhem from crash if wrong timestamp '0000-00-00' found in db ",
  149. "7.15.1" => "11.04.2018 sqlCmd accept widget textField-long, Internal MODEL is set ",
  150. "7.15.0" => "24.03.2018 new command sqlSpecial ",
  151. "7.14.7" => "21.03.2018 exportToFile,importFromFile can use file as an argument and executeBeforeDump, executeAfterDump is considered ",
  152. "7.14.6" => "18.03.2018 attribute expimpfile can use some kinds of wildcards (exportToFile, importFromFile adapted) ",
  153. "7.14.3" => "07.03.2018 DbRep_firstconnect changed - get lowest timestamp in database, DbRep_Connect deleted ",
  154. "7.14.0" => "26.02.2018 new syncStandby command",
  155. "7.12.0" => "16.02.2018 compression of dumpfile, restore of compressed files possible ",
  156. "7.11.0" => "12.02.2018 new command 'repairSQLite' to repair a corrupted SQLite database ",
  157. "7.10.0" => "10.02.2018 bugfix delete attr timeYearPeriod if set other time attributes, new 'changeValue' command ",
  158. "7.9.0" => "09.02.2018 new attribute 'avgTimeWeightMean' (time weight mean calculation), code review of selection routines, maxValue handle negative values correctly, one security second for correct create TimeArray in DbRep_normRelTime ",
  159. "7.8.1" => "04.02.2018 bugfix if IsDisabled (again), code review, bugfix last dataset is not selected if timestamp is fully set ('date time'), fix '\$runtime_string_next' = '\$runtime_string_next.999';' if \$runtime_string_next is part of sql-execute place holder AND contains date+time ",
  160. "7.8.0" => "04.02.2018 new command 'eraseReadings' ",
  161. "7.7.1" => "03.02.2018 minor fix in DbRep_firstconnect if IsDisabled ",
  162. "7.7.0" => "29.01.2018 attribute 'averageCalcForm', calculation sceme 'avgDailyMeanGWS', 'avgArithmeticMean' for averageValue ",
  163. "7.6.1" => "27.01.2018 new attribute 'sqlCmdHistoryLength' and 'fetchMarkDuplicates' for highlighting multiple datasets by fetchrows ",
  164. "7.5.3" => "23.01.2018 new attribute 'ftpDumpFilesKeep', version management added to FTP-usage ",
  165. "7.4.1" => "14.01.2018 fix old dumpfiles not deleted by dumpMySQL clientSide ",
  166. "7.4.0" => "09.01.2018 dumpSQLite/restoreSQLite, backup/restore now available when DbLog-device has reopen xxxx running, executeBeforeDump executeAfterDump also available for optimizeTables, vacuum, restoreMySQL, restoreSQLite, attribute executeBeforeDump / executeAfterDump renamed to executeBeforeProc & executeAfterProc ",
  167. "7.3.1" => "08.01.2018 fix syntax error for perl < 5.20 ",
  168. "7.3.0" => "07.01.2018 charfilter avoid control characters in datasets to exportToFile / importFromFile, changed to use aggregation for split selects in timeslices by exportToFile (avoid heavy memory consumption) ",
  169. "7.1.0" => "22.12.2017 new attribute timeYearPeriod for reports correspondig to e.g. electricity billing, bugfix connection check is running after restart allthough dev is disabled ",
  170. "6.4.1" => "13.12.2017 new Attribute 'sqlResultFieldSep' for field separate options of sqlCmd result ",
  171. "6.4.0" => "10.12.2017 prepare module for usage of datetime picker widget (Forum:#35736) ",
  172. "6.1.0" => "29.11.2017 new command delSeqDoublets (adviceRemain,adviceDelete), add Option to LASTCMD ",
  173. "6.0.0" => "18.11.2017 FTP transfer dumpfile after dump, delete old dumpfiles within Blockingcall (avoid freezes) commandref revised, minor fixes ",
  174. "5.6.4" => "05.10.2017 abortFn's adapted to use abortArg (Forum:77472) ",
  175. "5.6.3" => "01.10.2017 fix crash of fhem due to wrong rmday-calculation if month is changed, Forum:#77328 ",
  176. "5.6.0" => "17.07.2017 default timeout changed to 86400, new get-command 'procinfo' (MySQL) ",
  177. "5.4.0" => "03.07.2017 restoreMySQL - restore of csv-files (from dumpServerSide), RestoreRowsHistory/ DumpRowsHistory, Commandref revised ",
  178. "5.3.1" => "28.06.2017 vacuum for SQLite added, readings enhanced for optimizeTables / vacuum, commandref revised ",
  179. "5.3.0" => "26.06.2017 change of DbRep_mysqlOptimizeTables, new command optimizeTables ",
  180. "5.0.6" => "13.06.2017 add Aria engine to DbRep_mysqlOptimizeTables ",
  181. "5.0.3" => "07.06.2017 mysql_DoDumpServerSide added ",
  182. "5.0.1" => "05.06.2017 dependencies between dumpMemlimit and dumpSpeed created, enhanced verbose 5 logging ",
  183. "5.0.0" => "04.06.2017 MySQL Dump nonblocking added ",
  184. "4.16.1" => "22.05.2017 encode json without JSON module, requires at least fhem.pl 14348 2017-05-22 20:25:06Z ",
  185. "4.14.1" => "16.05.2017 limitation of fetchrows result datasets to 1000 by attr limit ",
  186. "4.14.0" => "15.05.2017 UserExitFn added as separate sub (DbRep_userexit) and attr userExitFn defined, new subs ReadingsBulkUpdateTimeState, ReadingsBulkUpdateValue, ReadingsSingleUpdateValue, commandref revised ",
  187. "4.13.4" => "09.05.2017 attribute sqlResultSingleFormat: mline sline table, attribute 'allowDeletion' is now also valid for sqlResult, sqlResultSingle and delete command is forced ",
  188. "4.13.2" => "09.05.2017 sqlResult, sqlResultSingle are able to execute delete, insert, update commands error corrections ",
  189. "4.12.0" => "31.03.2017 support of primary key for insert functions ",
  190. "4.11.4" => "29.03.2017 bugfix timestamp in minValue, maxValue if VALUE contains more than one numeric value (like in sysmon) ",
  191. "4.11.3" => "26.03.2017 usage of daylight saving time changed to avoid wrong selection when wintertime switch to summertime, minor bug fixes ",
  192. "4.11.2" => "16.03.2017 bugfix in func dbmeta_DoParse (SQLITE_DB_FILENAME) ",
  193. "4.11.0" => "18.02.2017 added [current|previous]_[month|week|day|hour]_begin and [current|previous]_[month|week|day|hour]_end as options of timestamp ",
  194. "4.10.2" => "16.01.2017 bugfix uninitialized value \$renmode if RenameAgent ",
  195. "4.10.1" => "30.11.2016 bugfix importFromFile format problem if UNIT-field wasn't set ",
  196. "4.9.0" => "23.12.2016 function readingRename added ",
  197. "4.8.6" => "17.12.2016 new bugfix group by-clause due to incompatible changes made in MyQL 5.7.5 (Forum #msg541103) ",
  198. "4.8.5" => "16.12.2016 bugfix group by-clause due to Forum #msg540610 ",
  199. "4.7.6" => "07.12.2016 DbRep version as internal, check if perl module DBI is installed ",
  200. "4.7.4" => "28.11.2016 sub DbRep_calcount changed due to Forum #msg529312 ",
  201. "4.7.3" => "20.11.2016 new diffValue function made suitable to SQLite ",
  202. "4.6.0" => "31.10.2016 bugfix calc issue due to daylight saving time end (winter time) ",
  203. "4.5.1" => "18.10.2016 get svrinfo contains SQLite database file size (MB), modified timeout routine ",
  204. "4.2.0" => "10.10.2016 allow SQL-Wildcards in attr reading & attr device ",
  205. "4.1.3" => "09.10.2016 bugfix delEntries running on SQLite ",
  206. "3.13.0" => "03.10.2016 added deviceRename to rename devices in database, new Internal DATABASE ",
  207. "3.12.0" => "02.10.2016 function minValue added ",
  208. "3.11.1" => "30.09.2016 bugfix include first and next day in calculation if Timestamp is exactly 'YYYY-MM-DD 00:00:00' ",
  209. "3.9.0" => "26.09.2016 new function importFromFile to import data from file (CSV format) ",
  210. "3.8.0" => "16.09.2016 new attr readingPreventFromDel to prevent readings from deletion when a new operation starts ",
  211. "3.7.2" => "04.09.2016 problem in diffValue fixed if if no value was selected ",
  212. "3.7.1" => "31.08.2016 Reading 'errortext' added, commandref continued, exportToFile changed, diffValue changed to fix wrong timestamp if error occur ",
  213. "3.7.0" => "30.08.2016 exportToFile added exports data to file (CSV format) ",
  214. "3.5.0" => "18.08.2016 new attribute timeOlderThan ",
  215. "3.4.4" => "12.08.2016 current_year_begin, previous_year_begin, current_year_end, previous_year_end added as possible values for timestamp attribute ",
  216. "3.4.0" => "03.08.2016 function 'insert' added ",
  217. "3.3.1" => "15.07.2016 function 'diffValue' changed, write '-' if no value ",
  218. "3.3.0" => "12.07.2016 function 'diffValue' added ",
  219. "3.1.1" => "10.07.2016 state turns to initialized and connected after attr 'disabled' is switched from '1' to '0' ",
  220. "3.1.0" => "09.07.2016 new Attr 'timeDiffToNow' and change subs according to that ",
  221. "3.0.0" => "04.07.2016 no selection if timestamp isn't set and aggregation isn't set with fetchrows, delEntries ",
  222. "2.9.8" => "01.07.2016 changed fetchrows_ParseDone to handle readingvalues with whitespaces correctly ",
  223. "2.9.5" => "30.06.2016 format of readingnames changed again (substitute ':' with '-' in time) ",
  224. "2.9.4" => "30.06.2016 change readingmap to readingNameMap, prove of unsupported characters added ",
  225. "2.9.3" => "27.06.2016 format of readingnames changed avoiding some problems after restart and splitting ",
  226. "2.9.0" => "25.06.2016 attributes showproctime, timeout added ",
  227. "2.8.0" => "24.06.2016 function averageValue changed to nonblocking function ",
  228. "2.7.0" => "23.06.2016 changed function countEntries to nonblocking ",
  229. "2.6.2" => "21.06.2016 aggregation week corrected ",
  230. "2.6.1" => "20.06.2016 routine maxval_ParseDone corrected ",
  231. "2.6.0" => "31.05.2016 maxValue changed to nonblocking function ",
  232. "2.4.0" => "29.05.2016 changed to nonblocking function for sumValue ",
  233. "2.0.0" => "24.05.2016 added nonblocking function for fetchrow ",
  234. "1.2.0" => "21.05.2016 function and attribute for delEntries added ",
  235. "1.0.0" => "19.05.2016 Initial"
  236. );
  237. # Hint Hash en
  238. our %DbRep_vHintsExt_en = (
  239. "4" => "The attribute 'valueFilter' can specify a REGEXP expression that is used for additional field selection as described in set-function. "
  240. ."If you need more assistance please to the manual of your used database. For example the overview about REGEXP for "
  241. ."MariaDB refer to <a href=\"https://mariadb.com/kb/en/library/regular-expressions-overview\">Regular Expressions "
  242. ."Overview</a>. ",
  243. "3" => "Features and restrictions of complex <a href=\"https://fhem.de/commandref.html#devspec\">device specifications (devspec) ",
  244. "2" => "<a href='https://www.dwd.de/DE/leistungen/klimadatendeutschland/beschreibung_tagesmonatswerte.html'>Rules</a> of german weather service for calculation of average temperatures. ",
  245. "1" => "Some helpful <a href=\"https://wiki.fhem.de/wiki/DbRep_-_Reporting_und_Management_von_DbLog-Datenbankinhalten#Praxisbeispiele_.2F_Hinweise_und_L.C3.B6sungsans.C3.A4tze_f.C3.BCr_verschiedene_Aufgaben\">FHEM-Wiki</a> Entries."
  246. );
  247. # Hint Hash de
  248. our %DbRep_vHintsExt_de = (
  249. "4" => "Im Attribut 'valueFilter' können REGEXP zur erweiterten Feldselektion angegeben werden. Welche Felder berücksichtigt "
  250. ."werden, ist in der jeweiligen set-Funktion beschrieben. Für weitere Hilfe bitte die REGEXP-Dokumentation ihrer "
  251. ."verwendeten Datenbank konsultieren. Ein Überblick über REGEXP mit MariaDB ist zum Beispiel hier verfügbar:\n"
  252. ."<a href=\"https://mariadb.com/kb/en/library/regular-expressions-overview\">Regular Expressions Overview</a>. ",
  253. "3" => "Merkmale und Restriktionen von komplexen <a href=\"https://fhem.de/commandref_DE.html#devspec\">Geräte-Spezifikationen (devspec) ",
  254. "2" => "<a href='https://www.dwd.de/DE/leistungen/klimadatendeutschland/beschreibung_tagesmonatswerte.html'>Regularien</a> des deutschen Wetterdienstes zur Berechnung von Durchschnittstemperaturen. ",
  255. "1" => "Hilfreiche Hinweise zu DbRep im <a href=\"https://wiki.fhem.de/wiki/DbRep_-_Reporting_und_Management_von_DbLog-Datenbankinhalten#Praxisbeispiele_.2F_Hinweise_und_L.C3.B6sungsans.C3.A4tze_f.C3.BCr_verschiedene_Aufgaben\">FHEM-Wiki</a>."
  256. );
  257. sub DbRep_Main($$;$);
  258. sub DbLog_cutCol($$$$$$$); # DbLog-Funktion nutzen um Daten auf maximale Länge beschneiden
  259. my %dbrep_col = ("DEVICE" => 64,
  260. "TYPE" => 64,
  261. "EVENT" => 512,
  262. "READING" => 64,
  263. "VALUE" => 128,
  264. "UNIT" => 32
  265. );
  266. ###################################################################################
  267. # DbRep_Initialize
  268. ###################################################################################
  269. sub DbRep_Initialize($) {
  270. my ($hash) = @_;
  271. $hash->{DefFn} = "DbRep_Define";
  272. $hash->{UndefFn} = "DbRep_Undef";
  273. $hash->{ShutdownFn} = "DbRep_Shutdown";
  274. $hash->{NotifyFn} = "DbRep_Notify";
  275. $hash->{SetFn} = "DbRep_Set";
  276. $hash->{GetFn} = "DbRep_Get";
  277. $hash->{AttrFn} = "DbRep_Attr";
  278. $hash->{FW_deviceOverview} = 1;
  279. $hash->{AttrList} = "disable:1,0 ".
  280. "reading ".
  281. "allowDeletion:1,0 ".
  282. "averageCalcForm:avgArithmeticMean,avgDailyMeanGWS,avgTimeWeightMean ".
  283. "countEntriesDetail:1,0 ".
  284. "device " .
  285. "dumpComment ".
  286. "dumpCompress:1,0 ".
  287. "dumpDirLocal ".
  288. "dumpDirRemote ".
  289. "dumpMemlimit ".
  290. "dumpSpeed ".
  291. "dumpFilesKeep:0,1,2,3,4,5,6,7,8,9,10 ".
  292. "executeBeforeProc ".
  293. "executeAfterProc ".
  294. "expimpfile ".
  295. "fastStart:1,0 ".
  296. "fetchRoute:ascent,descent ".
  297. "fetchMarkDuplicates:red,blue,brown,green,orange ".
  298. "ftpDebug:1,0 ".
  299. "ftpDir ".
  300. "ftpDumpFilesKeep:1,2,3,4,5,6,7,8,9,10 ".
  301. "ftpPassive:1,0 ".
  302. "ftpPwd ".
  303. "ftpPort ".
  304. "ftpServer ".
  305. "ftpTimeout ".
  306. "ftpUse:1,0 ".
  307. "ftpUser ".
  308. "ftpUseSSL:1,0 ".
  309. "aggregation:hour,day,week,month,no ".
  310. "diffAccept ".
  311. "limit ".
  312. "optimizeTablesBeforeDump:1,0 ".
  313. "readingNameMap ".
  314. "readingPreventFromDel ".
  315. "role:Client,Agent ".
  316. "seqDoubletsVariance ".
  317. "showproctime:1,0 ".
  318. "showSvrInfo ".
  319. "showVariables ".
  320. "showStatus ".
  321. "showTableInfo ".
  322. "sqlCmdHistoryLength:0,5,10,15,20,25,30,35,40,45,50 ".
  323. "sqlResultFormat:separated,mline,sline,table,json ".
  324. "sqlResultFieldSep:|,:,\/ ".
  325. "timeYearPeriod ".
  326. "timestamp_begin ".
  327. "timestamp_end ".
  328. "timeDiffToNow ".
  329. "timeOlderThan ".
  330. "timeout ".
  331. "userExitFn ".
  332. "valueFilter ".
  333. $readingFnAttributes;
  334. # Umbenennen von existierenden Attrbuten
  335. # $hash->{AttrRenameMap} = { "reading" => "readingFilter",
  336. # "device" => "deviceFilter",
  337. # };
  338. return undef;
  339. }
  340. ###################################################################################
  341. # DbRep_Define
  342. ###################################################################################
  343. sub DbRep_Define($@) {
  344. # define <name> DbRep <DbLog-Device>
  345. # ($hash) [1] [2]
  346. #
  347. my ($hash, $def) = @_;
  348. my $name = $hash->{NAME};
  349. return "Error: Perl module ".$DbRepMMDBI." is missing. Install it on Debian with: sudo apt-get install libdbi-perl" if($DbRepMMDBI);
  350. my @a = split("[ \t][ \t]*", $def);
  351. if(!$a[2]) {
  352. return "You need to specify more parameters.\n". "Format: define <name> DbRep <DbLog-Device>";
  353. } elsif (!$defs{$a[2]}) {
  354. return "The specified DbLog-Device \"$a[2]\" doesn't exist.";
  355. }
  356. $hash->{LASTCMD} = " ";
  357. $hash->{ROLE} = AttrVal($name, "role", "Client");
  358. $hash->{MODEL} = $hash->{ROLE};
  359. $hash->{HELPER}{DBLOGDEVICE} = $a[2];
  360. $hash->{HELPER}{IDRETRIES} = 3; # Anzahl wie oft versucht wird initiale Daten zu holen
  361. $hash->{VERSION} = (reverse sort(keys %DbRep_vNotesIntern))[0];
  362. $hash->{NOTIFYDEV} = "global,".$name; # nur Events dieser Devices an DbRep_Notify weiterleiten
  363. my $dbconn = $defs{$a[2]}{dbconn};
  364. $hash->{DATABASE} = (split(/;|=/, $dbconn))[1];
  365. $hash->{UTF8} = defined($defs{$a[2]}{UTF8})?$defs{$a[2]}{UTF8}:0;
  366. my ($err,$hl) = DbRep_getCmdFile($name."_sqlCmdList");
  367. if(!$err) {
  368. $hash->{HELPER}{SQLHIST} = $hl;
  369. Log3 ($name, 4, "DbRep $name - history sql commandlist read from file ".$attr{global}{modpath}."/FHEM/FhemUtils/cacheDbRep");
  370. }
  371. Log3 ($name, 4, "DbRep $name - initialized");
  372. ReadingsSingleUpdateValue ($hash, 'state', 'initialized', 1);
  373. RemoveInternalTimer($hash);
  374. InternalTimer(gettimeofday()+int(rand(45)), 'DbRep_firstconnect', "$name||onBoot|", 0);
  375. return undef;
  376. }
  377. ###################################################################################
  378. # DbRep_Set
  379. ###################################################################################
  380. sub DbRep_Set($@) {
  381. my ($hash, @a) = @_;
  382. return "\"set X\" needs at least an argument" if ( @a < 2 );
  383. my $name = $a[0];
  384. my $opt = $a[1];
  385. my $prop = $a[2];
  386. my $dbh = $hash->{DBH};
  387. my $dblogdevice = $hash->{HELPER}{DBLOGDEVICE};
  388. $hash->{dbloghash} = $defs{$dblogdevice};
  389. my $dbmodel = $hash->{dbloghash}{MODEL};
  390. my $dbname = $hash->{DATABASE};
  391. my $sd ="";
  392. my (@bkps,$dir);
  393. $dir = AttrVal($name, "dumpDirLocal", "./"); # 'dumpDirRemote' (Backup-Verz. auf dem MySQL-Server) muß gemountet sein und in 'dumpDirLocal' eingetragen sein
  394. $dir = $dir."/" unless($dir =~ m/\/$/);
  395. opendir(DIR,$dir);
  396. if ($dbmodel =~ /MYSQL/) {
  397. $dbname = $hash->{DATABASE};
  398. $sd = $dbname.".*(csv|sql)";
  399. } elsif ($dbmodel =~ /SQLITE/) {
  400. $dbname = $hash->{DATABASE};
  401. $dbname = (split /[\/]/, $dbname)[-1];
  402. $dbname = (split /\./, $dbname)[0];
  403. $sd = $dbname."_.*.sqlitebkp";
  404. }
  405. while (my $file = readdir(DIR)) {
  406. next unless (-f "$dir/$file");
  407. next unless ($file =~ /^$sd/);
  408. push @bkps,$file;
  409. }
  410. closedir(DIR);
  411. my $cj = @bkps?join(",",reverse(sort @bkps)):" ";
  412. # Drop-Down Liste bisherige Befehle in "sqlCmd" erstellen
  413. my $hl = $hash->{HELPER}{SQLHIST}.",___purge_historylist___" if($hash->{HELPER}{SQLHIST});
  414. my $setlist = "Unknown argument $opt, choose one of ".
  415. "eraseReadings:noArg ".
  416. (($hash->{ROLE} ne "Agent")?"sumValue:display,writeToDB ":"").
  417. (($hash->{ROLE} ne "Agent")?"averageValue:display,writeToDB ":"").
  418. (($hash->{ROLE} ne "Agent")?"changeValue ":"").
  419. (($hash->{ROLE} ne "Agent")?"delEntries:noArg ":"").
  420. (($hash->{ROLE} ne "Agent")?"delSeqDoublets:adviceRemain,adviceDelete,delete ":"").
  421. "deviceRename ".
  422. (($hash->{ROLE} ne "Agent")?"readingRename ":"").
  423. (($hash->{ROLE} ne "Agent")?"exportToFile ":"").
  424. (($hash->{ROLE} ne "Agent")?"importFromFile ":"").
  425. (($hash->{ROLE} ne "Agent")?"maxValue:display,writeToDB ":"").
  426. (($hash->{ROLE} ne "Agent")?"minValue:display,writeToDB ":"").
  427. (($hash->{ROLE} ne "Agent")?"fetchrows:history,current ":"").
  428. (($hash->{ROLE} ne "Agent")?"diffValue:display,writeToDB ":"").
  429. (($hash->{ROLE} ne "Agent")?"insert ":"").
  430. (($hash->{ROLE} ne "Agent")?"reduceLog ":"").
  431. (($hash->{ROLE} ne "Agent")?"sqlCmd:textField-long ":"").
  432. (($hash->{ROLE} ne "Agent" && $hl)?"sqlCmdHistory:".$hl." ":"").
  433. (($hash->{ROLE} ne "Agent")?"sqlSpecial:50mostFreqLogsLast2days,allDevCount,allDevReadCount ":"").
  434. (($hash->{ROLE} ne "Agent")?"syncStandby ":"").
  435. (($hash->{ROLE} ne "Agent")?"tableCurrentFillup:noArg ":"").
  436. (($hash->{ROLE} ne "Agent")?"tableCurrentPurge:noArg ":"").
  437. (($hash->{ROLE} ne "Agent" && $dbmodel =~ /MYSQL/ )?"dumpMySQL:clientSide,serverSide ":"").
  438. (($hash->{ROLE} ne "Agent" && $dbmodel =~ /SQLITE/ )?"dumpSQLite:noArg ":"").
  439. (($hash->{ROLE} ne "Agent" && $dbmodel =~ /SQLITE/ )?"repairSQLite ":"").
  440. (($hash->{ROLE} ne "Agent" && $dbmodel =~ /MYSQL/ )?"optimizeTables:noArg ":"").
  441. (($hash->{ROLE} ne "Agent" && $dbmodel =~ /SQLITE|POSTGRESQL/ )?"vacuum:noArg ":"").
  442. (($hash->{ROLE} ne "Agent" && $dbmodel =~ /MYSQL/)?"restoreMySQL:".$cj." ":"").
  443. (($hash->{ROLE} ne "Agent" && $dbmodel =~ /SQLITE/)?"restoreSQLite:".$cj." ":"").
  444. (($hash->{ROLE} ne "Agent")?"countEntries:history,current ":"");
  445. return if(IsDisabled($name));
  446. if ($opt =~ /eraseReadings/) {
  447. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  448. # Readings löschen die nicht in der Ausnahmeliste (Attr readingPreventFromDel) stehen
  449. DbRep_delread($hash);
  450. return undef;
  451. }
  452. if ($opt eq "dumpMySQL" && $hash->{ROLE} ne "Agent") {
  453. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  454. if ($prop eq "serverSide") {
  455. Log3 ($name, 3, "DbRep $name - ################################################################");
  456. Log3 ($name, 3, "DbRep $name - ### New database serverSide dump ###");
  457. Log3 ($name, 3, "DbRep $name - ################################################################");
  458. } else {
  459. Log3 ($name, 3, "DbRep $name - ################################################################");
  460. Log3 ($name, 3, "DbRep $name - ### New database clientSide dump ###");
  461. Log3 ($name, 3, "DbRep $name - ################################################################");
  462. }
  463. # Befehl vor Procedure ausführen
  464. DbRep_beforeproc($hash, "dump");
  465. DbRep_Main($hash,$opt,$prop);
  466. return undef;
  467. }
  468. if ($opt eq "dumpSQLite" && $hash->{ROLE} ne "Agent") {
  469. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  470. Log3 ($name, 3, "DbRep $name - ################################################################");
  471. Log3 ($name, 3, "DbRep $name - ### New SQLite dump ###");
  472. Log3 ($name, 3, "DbRep $name - ################################################################");
  473. # Befehl vor Procedure ausführen
  474. DbRep_beforeproc($hash, "dump");
  475. DbRep_Main($hash,$opt,$prop);
  476. return undef;
  477. }
  478. if ($opt eq "repairSQLite" && $hash->{ROLE} ne "Agent") {
  479. $prop = $prop?$prop:36000;
  480. if($prop) {
  481. unless($prop =~ /^(\d+)$/) { return " The Value of $opt is not valid. Use only figures 0-9 without decimal places !";};
  482. # unless ($aVal =~ /^[0-9]+$/) { return " The Value of $aName is not valid. Use only figures 0-9 without decimal places !";}
  483. }
  484. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  485. Log3 ($name, 3, "DbRep $name - ################################################################");
  486. Log3 ($name, 3, "DbRep $name - ### New SQLite repair attempt ###");
  487. Log3 ($name, 3, "DbRep $name - ################################################################");
  488. Log3 ($name, 3, "DbRep $name - start repair attempt of database ".$hash->{DATABASE});
  489. # closetime Datenbank
  490. my $dbloghash = $hash->{dbloghash};
  491. my $dbl = $dbloghash->{NAME};
  492. CommandSet(undef,"$dbl reopen $prop");
  493. # Befehl vor Procedure ausführen
  494. DbRep_beforeproc($hash, "repair");
  495. DbRep_Main($hash,$opt);
  496. return undef;
  497. }
  498. if ($opt =~ /restoreMySQL|restoreSQLite/ && $hash->{ROLE} ne "Agent") {
  499. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  500. Log3 ($name, 3, "DbRep $name - ################################################################");
  501. Log3 ($name, 3, "DbRep $name - ### New database Restore/Recovery ###");
  502. Log3 ($name, 3, "DbRep $name - ################################################################");
  503. # Befehl vor Procedure ausführen
  504. DbRep_beforeproc($hash, "restore");
  505. DbRep_Main($hash,$opt,$prop);
  506. return undef;
  507. }
  508. if ($opt =~ /optimizeTables|vacuum/ && $hash->{ROLE} ne "Agent") {
  509. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  510. Log3 ($name, 3, "DbRep $name - ################################################################");
  511. Log3 ($name, 3, "DbRep $name - ### New optimize table / vacuum execution ###");
  512. Log3 ($name, 3, "DbRep $name - ################################################################");
  513. # Befehl vor Procedure ausführen
  514. DbRep_beforeproc($hash, "optimize");
  515. DbRep_Main($hash,$opt);
  516. return undef;
  517. }
  518. if ($opt =~ m/delSeqDoublets/ && $hash->{ROLE} ne "Agent") {
  519. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  520. if ($prop =~ /delete/ && !AttrVal($hash->{NAME}, "allowDeletion", 0)) {
  521. return " Set attribute 'allowDeletion' if you want to allow deletion of any database entries. Use it with care !";
  522. }
  523. DbRep_beforeproc($hash, "delSeq");
  524. DbRep_Main($hash,$opt,$prop);
  525. return undef;
  526. }
  527. if ($opt =~ m/reduceLog/ && $hash->{ROLE} ne "Agent") {
  528. if ($hash->{HELPER}{RUNNING_REDUCELOG} && $hash->{HELPER}{RUNNING_REDUCELOG}{pid} !~ m/DEAD/) {
  529. return "reduceLog already in progress. Please wait for the current process to finish.";
  530. } else {
  531. delete $hash->{HELPER}{RUNNING_REDUCELOG};
  532. my @b = @a;
  533. shift(@b);
  534. $hash->{LASTCMD} = join(" ",@b);
  535. $hash->{HELPER}{REDUCELOG} = \@a;
  536. Log3 ($name, 3, "DbRep $name - ################################################################");
  537. Log3 ($name, 3, "DbRep $name - ### new reduceLog run ###");
  538. Log3 ($name, 3, "DbRep $name - ################################################################");
  539. # Befehl vor Procedure ausführen
  540. DbRep_beforeproc($hash, "reduceLog");
  541. DbRep_Main($hash,$opt);
  542. return undef;
  543. }
  544. }
  545. if ($hash->{HELPER}{RUNNING_BACKUP_CLIENT}) {
  546. $setlist = "Unknown argument $opt, choose one of ".
  547. (($hash->{ROLE} ne "Agent")?"cancelDump:noArg ":"");
  548. }
  549. if ($hash->{HELPER}{RUNNING_REPAIR}) {
  550. $setlist = "Unknown argument $opt, choose one of ".
  551. (($hash->{ROLE} ne "Agent")?"cancelRepair:noArg ":"");
  552. }
  553. if ($hash->{HELPER}{RUNNING_RESTORE}) {
  554. $setlist = "Unknown argument $opt, choose one of ".
  555. (($hash->{ROLE} ne "Agent")?"cancelRestore:noArg ":"");
  556. }
  557. if ($opt eq "cancelDump" && $hash->{ROLE} ne "Agent") {
  558. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  559. BlockingKill($hash->{HELPER}{RUNNING_BACKUP_CLIENT});
  560. Log3 ($name, 3, "DbRep $name -> running Dump has been canceled");
  561. ReadingsSingleUpdateValue ($hash, "state", "Dump canceled", 1);
  562. return undef;
  563. }
  564. if ($opt eq "cancelRepair" && $hash->{ROLE} ne "Agent") {
  565. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  566. BlockingKill($hash->{HELPER}{RUNNING_REPAIR});
  567. Log3 ($name, 3, "DbRep $name -> running Repair has been canceled");
  568. ReadingsSingleUpdateValue ($hash, "state", "Repair canceled", 1);
  569. return undef;
  570. }
  571. if ($opt eq "cancelRestore" && $hash->{ROLE} ne "Agent") {
  572. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  573. BlockingKill($hash->{HELPER}{RUNNING_RESTORE});
  574. Log3 ($name, 3, "DbRep $name -> running Restore has been canceled");
  575. ReadingsSingleUpdateValue ($hash, "state", "Restore canceled", 1);
  576. return undef;
  577. }
  578. #######################################################################################################
  579. ## keine Aktionen außer die über diesem Eintrag solange Reopen xxxx im DbLog-Device läuft
  580. #######################################################################################################
  581. if ($hash->{dbloghash}{HELPER}{REOPEN_RUNS} && $opt !~ /\?/) {
  582. my $ro = $hash->{dbloghash}{HELPER}{REOPEN_RUNS_UNTIL};
  583. Log3 ($name, 3, "DbRep $name - connection $dblogdevice to db $dbname is closed until $ro - $opt postponed");
  584. ReadingsSingleUpdateValue ($hash, "state", "connection $dblogdevice to $dbname is closed until $ro - $opt postponed", 1);
  585. return;
  586. }
  587. #######################################################################################################
  588. if ($opt =~ /countEntries/ && $hash->{ROLE} ne "Agent") {
  589. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  590. my $table = $prop?$prop:"history";
  591. DbRep_Main($hash,$opt,$table);
  592. } elsif ($opt =~ /fetchrows/ && $hash->{ROLE} ne "Agent") {
  593. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  594. my $table = $prop?$prop:"history";
  595. DbRep_Main($hash,$opt,$table);
  596. } elsif ($opt =~ m/(max|min|sum|average|diff)Value/ && $hash->{ROLE} ne "Agent") {
  597. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  598. if (!AttrVal($hash->{NAME}, "reading", "")) {
  599. return " The attribute reading to analyze is not set !";
  600. }
  601. if ($prop && $prop =~ /writeToDB/) {
  602. if (!AttrVal($hash->{NAME}, "device", "") || AttrVal($hash->{NAME}, "device", "") =~ /[%*:=,]/ || AttrVal($hash->{NAME}, "reading", "") =~ /[,\s]/) {
  603. return "<html>If you want write results back to database, attributes \"device\" and \"reading\" must be set.<br>
  604. In that case \"device\" mustn't be a <a href='https://fhem.de/commandref.html#devspec\'>devspec</a> and mustn't contain SQL-Wildcard (%).<br>
  605. The \"reading\" to evaluate has to be a single reading and no list.</html>";
  606. }
  607. }
  608. DbRep_Main($hash,$opt,$prop);
  609. } elsif ($opt =~ m/delEntries|tableCurrentPurge/ && $hash->{ROLE} ne "Agent") {
  610. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  611. if (!AttrVal($hash->{NAME}, "allowDeletion", undef)) {
  612. return " Set attribute 'allowDeletion' if you want to allow deletion of any database entries. Use it with care !";
  613. }
  614. DbRep_beforeproc($hash, "delEntries");
  615. DbRep_Main($hash,$opt);
  616. } elsif ($opt =~ m/tableCurrentFillup/ && $hash->{ROLE} ne "Agent") {
  617. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  618. DbRep_Main($hash,$opt);
  619. } elsif ($opt eq "deviceRename") {
  620. shift @a;
  621. shift @a;
  622. $prop = join(" ",@a); # Device Name kann Leerzeichen enthalten
  623. Log3 ($name, 1, "DbRep $name - a: @a");
  624. my ($olddev, $newdev) = split(",",$prop);
  625. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  626. if (!$olddev || !$newdev) {return "Both entries \"old device name\", \"new device name\" are needed. Use \"set $name deviceRename olddevname,newdevname\" ";}
  627. $hash->{HELPER}{OLDDEV} = $olddev;
  628. $hash->{HELPER}{NEWDEV} = $newdev;
  629. $hash->{HELPER}{RENMODE} = "devren";
  630. DbRep_Main($hash,$opt);
  631. } elsif ($opt eq "readingRename") {
  632. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  633. my ($oldread, $newread) = split(",",$prop);
  634. if (!$oldread || !$newread) {return "Both entries \"old reading name\", \"new reading name\" are needed. Use \"set $name readingRename oldreadingname,newreadingname\" ";}
  635. $hash->{HELPER}{OLDREAD} = $oldread;
  636. $hash->{HELPER}{NEWREAD} = $newread;
  637. $hash->{HELPER}{RENMODE} = "readren";
  638. DbRep_Main($hash,$opt);
  639. } elsif ($opt eq "insert" && $hash->{ROLE} ne "Agent") {
  640. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  641. if ($prop) {
  642. if (!AttrVal($hash->{NAME}, "device", "") || !AttrVal($hash->{NAME}, "reading", "") ) {
  643. return "One or both of attributes \"device\", \"reading\" are not set. It's mandatory to set both to complete dataset for manual insert !";
  644. }
  645. # Attribute device & reading dürfen kein SQL-Wildcard % enthalten
  646. return "One or both of attributes \"device\", \"reading\" containing SQL wildcard \"%\". Wildcards are not allowed in function manual insert !"
  647. if(AttrVal($hash->{NAME},"device","") =~ m/%/ || AttrVal($hash->{NAME},"reading","") =~ m/%/ );
  648. my ($i_date, $i_time, $i_value, $i_unit) = split(",",$prop);
  649. if (!$i_date || !$i_time || !$i_value) {return "At least data for \"Date\", \"Time\" and \"Value\" is needed to insert. \"Unit\" is optional. Inputformat is 'YYYY-MM-DD,HH:MM:SS,<Value(32)>,<Unit(32)>' ";}
  650. unless ($i_date =~ /(\d{4})-(\d{2})-(\d{2})/) {return "Input for date is not valid. Use format YYYY-MM-DD !";}
  651. unless ($i_time =~ /(\d{2}):(\d{2}):(\d{2})/) {return "Input for time is not valid. Use format HH:MM:SS !";}
  652. my $i_timestamp = $i_date." ".$i_time;
  653. my ($yyyy, $mm, $dd, $hh, $min, $sec) = ($i_timestamp =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/);
  654. eval { my $ts = timelocal($sec, $min, $hh, $dd, $mm-1, $yyyy-1900); };
  655. if ($@) {
  656. my @l = split (/at/, $@);
  657. return " Timestamp is out of range - $l[0]";
  658. }
  659. my $i_device = AttrVal($hash->{NAME}, "device", "");
  660. my $i_reading = AttrVal($hash->{NAME}, "reading", "");
  661. # Daten auf maximale Länge (entsprechend der Feldlänge in DbLog DB create-scripts) beschneiden wenn nicht SQLite
  662. if ($dbmodel ne 'SQLITE') {
  663. $i_device = substr($i_device,0, $dbrep_col{DEVICE});
  664. $i_reading = substr($i_reading,0, $dbrep_col{READING});
  665. $i_value = substr($i_value,0, $dbrep_col{VALUE});
  666. $i_unit = substr($i_unit,0, $dbrep_col{UNIT}) if($i_unit);
  667. }
  668. $hash->{HELPER}{I_TIMESTAMP} = $i_timestamp;
  669. $hash->{HELPER}{I_DEVICE} = $i_device;
  670. $hash->{HELPER}{I_READING} = $i_reading;
  671. $hash->{HELPER}{I_VALUE} = $i_value;
  672. $hash->{HELPER}{I_UNIT} = $i_unit;
  673. $hash->{HELPER}{I_TYPE} = my $i_type = "manual";
  674. $hash->{HELPER}{I_EVENT} = my $i_event = "manual";
  675. } else {
  676. return "Data to insert to table 'history' are needed like this pattern: 'Date,Time,Value,[Unit]'. \"Unit\" is optional. Spaces are not allowed !";
  677. }
  678. DbRep_Main($hash,$opt);
  679. } elsif ($opt eq "exportToFile" && $hash->{ROLE} ne "Agent") {
  680. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  681. my $f = $prop if($prop);
  682. if (!AttrVal($hash->{NAME}, "expimpfile", "") && !$f) {
  683. return "\"$opt\" needs a file as an argument or the attribute \"expimpfile\" (path and filename) to be set !";
  684. }
  685. DbRep_Main($hash,$opt,$f);
  686. } elsif ($opt eq "importFromFile" && $hash->{ROLE} ne "Agent") {
  687. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  688. my $f = $prop if($prop);
  689. if (!AttrVal($hash->{NAME}, "expimpfile", "") && !$f) {
  690. return "\"$opt\" needs a file as an argument or the attribute \"expimpfile\" (path and filename) to be set !";
  691. }
  692. DbRep_Main($hash,$opt,$f);
  693. } elsif ($opt =~ /sqlCmd|sqlSpecial|sqlCmdHistory/) {
  694. return "\"set $opt\" needs at least an argument" if ( @a < 3 );
  695. # remove arg 0, 1 to get SQL command
  696. my $sqlcmd;
  697. if($opt eq "sqlSpecial") {
  698. $sqlcmd = $prop;
  699. }
  700. if($opt eq "sqlCmd") {
  701. my @cmd = @a;
  702. shift @cmd; shift @cmd;
  703. $sqlcmd = join(" ", @cmd);
  704. $sqlcmd =~ tr/ A-Za-z0-9!"#$§%&'()*+,-.\/:;<=>?@[\\]^_`{|}~äöüÄÖÜ߀/ /cs;
  705. }
  706. if($opt eq "sqlCmdHistory") {
  707. $prop =~ tr/ A-Za-z0-9!"#$%&'()*+,-.\/:;<=>?@[\\]^_`{|}~äöüÄÖÜ߀/ /cs;
  708. $prop =~ s/<c>/,/g;
  709. $sqlcmd = $prop;
  710. if($sqlcmd eq "___purge_historylist___") {
  711. delete($hash->{HELPER}{SQLHIST});
  712. DbRep_setCmdFile($name."_sqlCmdList","",$hash); # Löschen der sql History Liste im DbRep-Keyfile
  713. return "SQL command historylist of $name deleted.";
  714. }
  715. }
  716. $hash->{LASTCMD} = $sqlcmd?"$opt $sqlcmd":"$opt";
  717. if ($sqlcmd =~ m/^\s*delete/is && !AttrVal($hash->{NAME}, "allowDeletion", undef)) {
  718. return "Attribute 'allowDeletion = 1' is needed for command '$sqlcmd'. Use it with care !";
  719. }
  720. DbRep_Main($hash,$opt,$sqlcmd);
  721. } elsif ($opt =~ /changeValue/) {
  722. shift @a;
  723. shift @a;
  724. $prop = join(" ", @a);
  725. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  726. unless($prop =~ m/^\s*(".*",".*")\s*$/) {return "Both entries \"old string\", \"new string\" are needed. Use \"set $name changeValue \"old string\",\"new string\" (use quotes)";}
  727. my $complex = 0;
  728. my ($oldval,$newval) = ($prop =~ /^\s*"(.*?)","(.*?)"\s*$/);
  729. if($newval =~ m/[{}]/) {
  730. if($newval =~ m/^\s*(\{.*\})\s*$/s) {
  731. $newval = $1;
  732. $complex = 1;
  733. my %specials = (
  734. "%VALUE" => $name,
  735. "%UNIT" => $name,
  736. );
  737. $newval = EvalSpecials($newval, %specials);
  738. } else {
  739. return "The expression of \"new string\" has to be included in \"{ }\" ";
  740. }
  741. }
  742. $hash->{HELPER}{COMPLEX} = $complex;
  743. $hash->{HELPER}{OLDVAL} = $oldval;
  744. $hash->{HELPER}{NEWVAL} = $newval;
  745. $hash->{HELPER}{RENMODE} = "changeval";
  746. DbRep_beforeproc($hash, "changeval");
  747. DbRep_Main($hash,$opt);
  748. } elsif ($opt =~ m/syncStandby/ && $hash->{ROLE} ne "Agent") {
  749. unless($prop) {return "A DbLog-device (standby) is needed to sync. Use \"set $name syncStandby <DbLog-standby name>\" ";}
  750. if(!exists($defs{$prop}) || $defs{$prop}->{TYPE} ne "DbLog") {
  751. return "The device \"$prop\" doesn't exist or is not a DbLog-device. ";
  752. }
  753. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  754. DbRep_Main($hash,$opt,$prop);
  755. } else {
  756. return "$setlist";
  757. }
  758. return undef;
  759. }
  760. ###################################################################################
  761. # DbRep_Get
  762. ###################################################################################
  763. sub DbRep_Get($@) {
  764. my ($hash, @a) = @_;
  765. return "\"get X\" needs at least an argument" if ( @a < 2 );
  766. my $name = $a[0];
  767. my $opt = $a[1];
  768. my $prop = $a[2];
  769. my $dbh = $hash->{DBH};
  770. my $dblogdevice = $hash->{HELPER}{DBLOGDEVICE};
  771. $hash->{dbloghash} = $defs{$dblogdevice};
  772. my $dbmodel = $hash->{dbloghash}{MODEL};
  773. my $dbname = $hash->{DATABASE};
  774. my $to = AttrVal($name, "timeout", "86400");
  775. my $getlist = "Unknown argument $opt, choose one of ".
  776. "svrinfo:noArg ".
  777. "blockinginfo:noArg ".
  778. "minTimestamp:noArg ".
  779. "dbValue:textField-long ".
  780. (($dbmodel eq "MYSQL")?"dbstatus:noArg ":"").
  781. (($dbmodel eq "MYSQL")?"tableinfo:noArg ":"").
  782. (($dbmodel eq "MYSQL")?"procinfo:noArg ":"").
  783. (($dbmodel eq "MYSQL")?"dbvars:noArg ":"").
  784. "versionNotes "
  785. ;
  786. return if(IsDisabled($name));
  787. if ($hash->{dbloghash}{HELPER}{REOPEN_RUNS} && $opt !~ /\?|procinfo|blockinginfo/) {
  788. my $ro = $hash->{dbloghash}{HELPER}{REOPEN_RUNS_UNTIL};
  789. Log3 ($name, 3, "DbRep $name - connection $dblogdevice to db $dbname is closed until $ro - $opt postponed");
  790. ReadingsSingleUpdateValue ($hash, "state", "connection $dblogdevice to $dbname is closed until $ro - $opt postponed", 1);
  791. return;
  792. }
  793. if ($opt =~ /dbvars|dbstatus|tableinfo|procinfo/) {
  794. return "Dump is running - try again later !" if($hash->{HELPER}{RUNNING_BACKUP_CLIENT});
  795. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  796. return "The operation \"$opt\" isn't available with database type $dbmodel" if ($dbmodel ne 'MYSQL');
  797. ReadingsSingleUpdateValue ($hash, "state", "running", 1);
  798. DbRep_delread($hash); # Readings löschen die nicht in der Ausnahmeliste (Attr readingPreventFromDel) stehen
  799. $hash->{HELPER}{RUNNING_PID} = BlockingCall("dbmeta_DoParse", "$name|$opt", "dbmeta_ParseDone", $to, "DbRep_ParseAborted", $hash);
  800. } elsif ($opt eq "svrinfo") {
  801. return "Dump is running - try again later !" if($hash->{HELPER}{RUNNING_BACKUP_CLIENT});
  802. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  803. DbRep_delread($hash);
  804. ReadingsSingleUpdateValue ($hash, "state", "running", 1);
  805. $hash->{HELPER}{RUNNING_PID} = BlockingCall("dbmeta_DoParse", "$name|$opt", "dbmeta_ParseDone", $to, "DbRep_ParseAborted", $hash);
  806. } elsif ($opt eq "blockinginfo") {
  807. return "Dump is running - try again later !" if($hash->{HELPER}{RUNNING_BACKUP_CLIENT});
  808. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  809. DbRep_delread($hash);
  810. ReadingsSingleUpdateValue ($hash, "state", "running", 1);
  811. DbRep_getblockinginfo($hash);
  812. } elsif ($opt eq "minTimestamp") {
  813. return "Dump is running - try again later !" if($hash->{HELPER}{RUNNING_BACKUP_CLIENT});
  814. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  815. DbRep_delread($hash);
  816. ReadingsSingleUpdateValue ($hash, "state", "running", 1);
  817. $hash->{HELPER}{IDRETRIES} = 3; # Anzahl wie oft versucht wird initiale Daten zu holen
  818. $prop = $prop?$prop:'';
  819. DbRep_firstconnect("$name|$opt|$prop|");
  820. } elsif ($opt =~ /dbValue/) {
  821. return "get \"$opt\" needs at least an argument" if ( @a < 3 );
  822. # remove arg 0, 1 to get SQL command
  823. my @cmd = @a;
  824. shift @cmd; shift @cmd;
  825. my $sqlcmd = join(" ",@cmd);
  826. $sqlcmd =~ tr/ A-Za-z0-9!"#$§%&'()*+,-.\/:;<=>?@[\\]^_`{|}~äöüÄÖÜ߀/ /cs;
  827. $hash->{LASTCMD} = $sqlcmd?"$opt $sqlcmd":"$opt";
  828. if ($sqlcmd =~ m/^\s*delete/is && !AttrVal($hash->{NAME}, "allowDeletion", undef)) {
  829. return "Attribute 'allowDeletion = 1' is needed for command '$sqlcmd'. Use it with care !";
  830. }
  831. my ($err,$ret) = DbRep_dbValue($name,$sqlcmd);
  832. return $err?$err:$ret;
  833. } elsif ($opt =~ /versionNotes/) {
  834. my $header = "<b>Module release information</b><br>";
  835. my $header1 = "<b>Helpful hints</b><br>";
  836. my %hs;
  837. # Ausgabetabelle erstellen
  838. my ($ret,$val0,$val1);
  839. my $i = 0;
  840. $ret = "<html>";
  841. # Hints
  842. if(!$prop || $prop =~ /hints/ || $prop =~ /[\d]+/) {
  843. $ret .= sprintf("<div class=\"makeTable wide\"; style=\"text-align:left\">$header1 <br>");
  844. $ret .= "<table class=\"block wide internals\">";
  845. $ret .= "<tbody>";
  846. $ret .= "<tr class=\"even\">";
  847. if($prop && $prop =~ /[\d]+/) {
  848. if(AttrVal("global","language","EN") eq "DE") {
  849. %hs = ( $prop => $DbRep_vHintsExt_de{$prop} );
  850. } else {
  851. %hs = ( $prop => $DbRep_vHintsExt_en{$prop} );
  852. }
  853. } else {
  854. if(AttrVal("global","language","EN") eq "DE") {
  855. %hs = %DbRep_vHintsExt_de;
  856. } else {
  857. %hs = %DbRep_vHintsExt_en;
  858. }
  859. }
  860. $i = 0;
  861. foreach my $key (reverse sort(keys %hs)) {
  862. $val0 = $hs{$key};
  863. $ret .= sprintf("<td style=\"vertical-align:top\"><b>$key</b> </td><td style=\"vertical-align:top\">$val0</td>" );
  864. $ret .= "</tr>";
  865. $i++;
  866. if ($i & 1) {
  867. # $i ist ungerade
  868. $ret .= "<tr class=\"odd\">";
  869. } else {
  870. $ret .= "<tr class=\"even\">";
  871. }
  872. }
  873. $ret .= "</tr>";
  874. $ret .= "</tbody>";
  875. $ret .= "</table>";
  876. $ret .= "</div>";
  877. }
  878. # Notes
  879. if(!$prop || $prop =~ /rel/) {
  880. $ret .= sprintf("<div class=\"makeTable wide\"; style=\"text-align:left\">$header <br>");
  881. $ret .= "<table class=\"block wide internals\">";
  882. $ret .= "<tbody>";
  883. $ret .= "<tr class=\"even\">";
  884. $i = 0;
  885. foreach my $key (reverse sort(keys %DbRep_vNotesExtern)) {
  886. ($val0,$val1) = split(/\s/,$DbRep_vNotesExtern{$key},2);
  887. $ret .= sprintf("<td style=\"vertical-align:top\"><b>$key</b> </td><td style=\"vertical-align:top\">$val0 </td><td>$val1</td>" );
  888. $ret .= "</tr>";
  889. $i++;
  890. if ($i & 1) {
  891. # $i ist ungerade
  892. $ret .= "<tr class=\"odd\">";
  893. } else {
  894. $ret .= "<tr class=\"even\">";
  895. }
  896. }
  897. $ret .= "</tr>";
  898. $ret .= "</tbody>";
  899. $ret .= "</table>";
  900. $ret .= "</div>";
  901. }
  902. $ret .= "</html>";
  903. return $ret;
  904. } else {
  905. return "$getlist";
  906. }
  907. return undef;
  908. }
  909. ###################################################################################
  910. # DbRep_Attr
  911. ###################################################################################
  912. sub DbRep_Attr($$$$) {
  913. my ($cmd,$name,$aName,$aVal) = @_;
  914. my $hash = $defs{$name};
  915. $hash->{dbloghash} = $defs{$hash->{HELPER}{DBLOGDEVICE}};
  916. my $dbmodel = $hash->{dbloghash}{MODEL};
  917. my $do;
  918. # $cmd can be "del" or "set"
  919. # $name is device name
  920. # aName and aVal are Attribute name and value
  921. # nicht erlaubte / nicht setzbare Attribute wenn role = Agent
  922. my @agentnoattr = qw(aggregation
  923. allowDeletion
  924. dumpDirLocal
  925. reading
  926. readingNameMap
  927. readingPreventFromDel
  928. device
  929. diffAccept
  930. executeBeforeProc
  931. executeAfterProc
  932. expimpfile
  933. fastStart
  934. ftpUse
  935. ftpUser
  936. ftpUseSSL
  937. ftpDebug
  938. ftpDir
  939. ftpPassive
  940. ftpPort
  941. ftpPwd
  942. ftpServer
  943. ftpTimeout
  944. dumpMemlimit
  945. dumpComment
  946. dumpSpeed
  947. optimizeTablesBeforeDump
  948. seqDoubletsVariance
  949. sqlCmdHistoryLength
  950. timeYearPeriod
  951. timestamp_begin
  952. timestamp_end
  953. timeDiffToNow
  954. timeOlderThan
  955. sqlResultFormat
  956. );
  957. if ($aName eq "disable") {
  958. if($cmd eq "set") {
  959. $do = ($aVal) ? 1 : 0;
  960. }
  961. $do = 0 if($cmd eq "del");
  962. my $val = ($do == 1 ? "disabled" : "initialized");
  963. ReadingsSingleUpdateValue ($hash, "state", $val, 1);
  964. if ($do == 1) {
  965. my $dbh = $hash->{DBH};
  966. $dbh->disconnect() if($dbh);
  967. }
  968. }
  969. if ($cmd eq "set" && $hash->{ROLE} eq "Agent") {
  970. foreach (@agentnoattr) {
  971. return ("Attribute $aName is not usable due to role of $name is \"$hash->{ROLE}\" ") if ($_ eq $aName);
  972. }
  973. }
  974. if ($aName eq "readingPreventFromDel") {
  975. if($cmd eq "set") {
  976. if($aVal =~ / /) {return "Usage of $aName is wrong. Use a comma separated list of readings which are should prevent from deletion when a new selection starts.";}
  977. $hash->{HELPER}{RDPFDEL} = $aVal;
  978. } else {
  979. delete $hash->{HELPER}{RDPFDEL} if($hash->{HELPER}{RDPFDEL});
  980. }
  981. }
  982. if ($aName eq "sqlCmdHistoryLength") {
  983. if($cmd eq "set") {
  984. $do = ($aVal) ? 1 : 0;
  985. }
  986. $do = 0 if($cmd eq "del");
  987. if ($do == 0) {
  988. delete($hash->{HELPER}{SQLHIST});
  989. DbRep_setCmdFile($name."_sqlCmdList","",$hash); # Löschen der sql History Liste im DbRep-Keyfile
  990. }
  991. }
  992. if ($aName eq "userExitFn") {
  993. if($cmd eq "set") {
  994. if(!$aVal) {return "Usage of $aName is wrong. The function has to be specified as \"<UserExitFn> [reading:value]\" ";}
  995. my @a = split(/ /,$aVal,2);
  996. $hash->{HELPER}{USEREXITFN} = $a[0];
  997. $hash->{HELPER}{UEFN_REGEXP} = $a[1] if($a[1]);
  998. } else {
  999. delete $hash->{HELPER}{USEREXITFN} if($hash->{HELPER}{USEREXITFN});
  1000. delete $hash->{HELPER}{UEFN_REGEXP} if($hash->{HELPER}{UEFN_REGEXP});
  1001. }
  1002. }
  1003. if ($aName eq "role") {
  1004. if($cmd eq "set") {
  1005. if ($aVal eq "Agent") {
  1006. # check ob bereits ein Agent für die angeschlossene Datenbank existiert -> DbRep-Device kann dann keine Agent-Rolle einnehmen
  1007. foreach(devspec2array("TYPE=DbRep")) {
  1008. my $devname = $_;
  1009. next if($devname eq $name);
  1010. my $devrole = $defs{$_}{ROLE};
  1011. my $devdb = $defs{$_}{DATABASE};
  1012. if ($devrole eq "Agent" && $devdb eq $hash->{DATABASE}) { return "There is already an Agent device: $devname defined for database $hash->{DATABASE} !"; }
  1013. }
  1014. # nicht erlaubte Attribute löschen falls gesetzt
  1015. foreach (@agentnoattr) {
  1016. delete($attr{$name}{$_});
  1017. }
  1018. $attr{$name}{icon} = "security";
  1019. }
  1020. $do = $aVal;
  1021. } else {
  1022. $do = "Client";
  1023. }
  1024. $hash->{ROLE} = $do;
  1025. $hash->{MODEL} = $hash->{ROLE};
  1026. delete($attr{$name}{icon}) if($do eq "Client");
  1027. }
  1028. if ($cmd eq "set") {
  1029. if ($aName =~ /valueFilter/) {
  1030. eval { "Hallo" =~ m/$aVal/ };
  1031. return "Bad regexp: $@" if($@);
  1032. }
  1033. if ($aName =~ /seqDoubletsVariance/) {
  1034. unless (looks_like_number($aVal)) { return " The Value of $aName is not valid. Only figures are allowed !";}
  1035. }
  1036. if ($aName eq "timeYearPeriod") {
  1037. # 06-01 02-28
  1038. unless ($aVal =~ /^(\d{2})-(\d{2}) (\d{2})-(\d{2})$/ )
  1039. { return "The Value of \"$aName\" isn't valid. Set the account period as \"MM-DD MM-DD\".";}
  1040. my ($mm1, $dd1, $mm2, $dd2) = ($aVal =~ /^(\d{2})-(\d{2}) (\d{2})-(\d{2})$/);
  1041. my (undef,undef,undef,$mday,$mon,$year1,undef,undef,undef) = localtime(time); # Istzeit Ableitung
  1042. my $year2 = $year1;
  1043. # a b c d
  1044. # 06-01 02-28 , wenn c < a && $mon < a -> Jahr(a)-1, sonst Jahr(c)+1
  1045. my $c = ($mon+1).$mday;
  1046. my $e = $mm2.$dd2;
  1047. if ($mm2 <= $mm1 && $c <= $e) {
  1048. $year1--;
  1049. } else {
  1050. $year2++;
  1051. }
  1052. eval { my $t1 = timelocal(00, 00, 00, $dd1, $mm1-1, $year1-1900);
  1053. my $t2 = timelocal(00, 00, 00, $dd2, $mm2-1, $year2-1900); };
  1054. if ($@) {
  1055. my @l = split (/at/, $@);
  1056. return " The Value of $aName is out of range - $l[0]";
  1057. }
  1058. delete($attr{$name}{timestamp_begin}) if ($attr{$name}{timestamp_begin});
  1059. delete($attr{$name}{timestamp_end}) if ($attr{$name}{timestamp_end});
  1060. delete($attr{$name}{timeDiffToNow}) if ($attr{$name}{timeDiffToNow});
  1061. delete($attr{$name}{timeOlderThan}) if ($attr{$name}{timeOlderThan});
  1062. return undef;
  1063. }
  1064. if ($aName eq "timestamp_begin" || $aName eq "timestamp_end") {
  1065. my ($a,$b,$c) = split('_',$aVal);
  1066. if ($a =~ /^current$|^previous$/ && $b =~ /^hour$|^day$|^week$|^month$|^year$/ && $c =~ /^begin$|^end$/) {
  1067. delete($attr{$name}{timeDiffToNow}) if ($attr{$name}{timeDiffToNow});
  1068. delete($attr{$name}{timeOlderThan}) if ($attr{$name}{timeOlderThan});
  1069. delete($attr{$name}{timeYearPeriod}) if ($attr{$name}{timeYearPeriod});
  1070. return undef;
  1071. }
  1072. $aVal = DbRep_formatpicker($aVal);
  1073. unless ($aVal =~ /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/)
  1074. {return " The Value of $aName is not valid. Use format YYYY-MM-DD HH:MM:SS or one of \"current_[year|month|day|hour]_begin\",\"current_[year|month|day|hour]_end\", \"previous_[year|month|day|hour]_begin\", \"previous_[year|month|day|hour]_end\" !";}
  1075. my ($yyyy, $mm, $dd, $hh, $min, $sec) = ($aVal =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/);
  1076. eval { my $epoch_seconds_begin = timelocal($sec, $min, $hh, $dd, $mm-1, $yyyy-1900); };
  1077. if ($@) {
  1078. my @l = split (/at/, $@);
  1079. return " The Value of $aName is out of range - $l[0]";
  1080. }
  1081. delete($attr{$name}{timeDiffToNow}) if ($attr{$name}{timeDiffToNow});
  1082. delete($attr{$name}{timeOlderThan}) if ($attr{$name}{timeOlderThan});
  1083. delete($attr{$name}{timeYearPeriod}) if ($attr{$name}{timeYearPeriod});
  1084. }
  1085. if ($aName =~ /ftpTimeout|timeout|diffAccept/) {
  1086. unless ($aVal =~ /^[0-9]+$/) { return " The Value of $aName is not valid. Use only figures 0-9 without decimal places !";}
  1087. }
  1088. if ($aName eq "readingNameMap") {
  1089. unless ($aVal =~ m/^[A-Za-z\d_\.-]+$/) { return " Unsupported character in $aName found. Use only A-Z a-z _ . -";}
  1090. }
  1091. if ($aName eq "timeDiffToNow") {
  1092. unless ($aVal =~ /^[0-9]+$/ || $aVal =~ /^\s*[ydhms]:([\d]+)\s*/ && $aVal !~ /.*,.*/ )
  1093. { return "The Value of \"$aName\" isn't valid. Set simple seconds like \"86400\" or use form like \"y:1 d:10 h:6 m:12 s:20\". Refer to commandref !";}
  1094. delete($attr{$name}{timestamp_begin}) if ($attr{$name}{timestamp_begin});
  1095. delete($attr{$name}{timestamp_end}) if ($attr{$name}{timestamp_end});
  1096. delete($attr{$name}{timeYearPeriod}) if ($attr{$name}{timeYearPeriod});
  1097. }
  1098. if ($aName eq "timeOlderThan") {
  1099. unless ($aVal =~ /^[0-9]+$/ || $aVal =~ /^\s*[ydhms]:([\d]+)\s*/ && $aVal !~ /.*,.*/ )
  1100. { return "The Value of \"$aName\" isn't valid. Set simple seconds like \"86400\" or use form like \"y:1 d:10 h:6 m:12 s:20\". Refer to commandref !";}
  1101. delete($attr{$name}{timestamp_begin}) if ($attr{$name}{timestamp_begin});
  1102. delete($attr{$name}{timestamp_end}) if ($attr{$name}{timestamp_end});
  1103. delete($attr{$name}{timeYearPeriod}) if ($attr{$name}{timeYearPeriod});
  1104. }
  1105. if ($aName eq "dumpMemlimit" || $aName eq "dumpSpeed") {
  1106. unless ($aVal =~ /^[0-9]+$/) { return "The Value of $aName is not valid. Use only figures 0-9 without decimal places.";}
  1107. my $dml = AttrVal($name, "dumpMemlimit", 100000);
  1108. my $ds = AttrVal($name, "dumpSpeed", 10000);
  1109. if($aName eq "dumpMemlimit") {
  1110. unless($aVal >= (10 * $ds)) {return "The Value of $aName has to be at least '10 x dumpSpeed' ! ";}
  1111. }
  1112. if($aName eq "dumpSpeed") {
  1113. unless($aVal <= ($dml / 10)) {return "The Value of $aName mustn't be greater than 'dumpMemlimit / 10' ! ";}
  1114. }
  1115. }
  1116. if ($aName eq "ftpUse") {
  1117. delete($attr{$name}{ftpUseSSL});
  1118. }
  1119. if ($aName eq "ftpUseSSL") {
  1120. delete($attr{$name}{ftpUse});
  1121. }
  1122. if ($aName eq "reading" || $aName eq "device") {
  1123. if ($dbmodel && $dbmodel ne 'SQLITE') {
  1124. my $attrname = uc($aName);
  1125. if ($dbmodel eq 'POSTGRESQL' && $aVal !~ m/,/) {
  1126. return "Length of \"$aName\" is too big. Maximum length for database type $dbmodel is $dbrep_col{$attrname}" if(length($aVal) > $dbrep_col{$attrname});
  1127. } elsif ($dbmodel eq 'MYSQL' && $aVal !~ m/,/) {
  1128. return "Length of \"$aName\" is too big. Maximum length for database type $dbmodel is $dbrep_col{$attrname}" if(length($aVal) > $dbrep_col{$attrname});
  1129. }
  1130. }
  1131. }
  1132. }
  1133. return undef;
  1134. }
  1135. ###################################################################################
  1136. # DbRep_Notify Eventverarbeitung
  1137. ###################################################################################
  1138. sub DbRep_Notify($$) {
  1139. # Es werden nur die Events von Geräten verarbeitet die im Hash $hash->{NOTIFYDEV} gelistet sind (wenn definiert).
  1140. # Dadurch kann die Menge der Events verringert werden. In sub DbRep_Define angeben.
  1141. # Beispiele:
  1142. # $hash->{NOTIFYDEV} = "global";
  1143. # $hash->{NOTIFYDEV} = "global,Definition_A,Definition_B";
  1144. my ($own_hash, $dev_hash) = @_;
  1145. my $myName = $own_hash->{NAME}; # Name des eigenen Devices
  1146. my $devName = $dev_hash->{NAME}; # Device welches Events erzeugt hat
  1147. return if(IsDisabled($myName)); # Return if the module is disabled
  1148. my $events = deviceEvents($dev_hash,0);
  1149. return if(!$events);
  1150. foreach my $event (@{$events}) {
  1151. $event = "" if(!defined($event));
  1152. my @evl = split("[ \t][ \t]*", $event);
  1153. # if ($devName = $myName && $evl[0] =~ /done/) {
  1154. # InternalTimer(time+1, "browser_refresh", $own_hash, 0);
  1155. # }
  1156. if ($own_hash->{ROLE} eq "Agent") {
  1157. # wenn Rolle "Agent" Verbeitung von RENAMED Events
  1158. next if ($event !~ /RENAMED/);
  1159. my $strucChanged;
  1160. # altes in neues device in der DEF des angeschlossenen DbLog-device ändern (neues device loggen)
  1161. my $dblog_name = $own_hash->{dbloghash}{NAME}; # Name des an den DbRep-Agenten angeschlossenen DbLog-Dev
  1162. my $dblog_hash = $defs{$dblog_name};
  1163. if ( $dblog_hash->{DEF} =~ m/( |\(|\|)$evl[1]( |\)|\||:)/ ) {
  1164. $dblog_hash->{DEF} =~ s/$evl[1]/$evl[2]/;
  1165. $dblog_hash->{REGEXP} =~ s/$evl[1]/$evl[2]/;
  1166. # Definitionsänderung wurde vorgenommen
  1167. $strucChanged = 1;
  1168. Log3 ($myName, 3, "DbRep Agent $myName - $dblog_name substituted in DEF, old: \"$evl[1]\", new: \"$evl[2]\" ");
  1169. }
  1170. # DEVICE innerhalb angeschlossener Datenbank umbenennen
  1171. Log3 ($myName, 4, "DbRep Agent $myName - Evt RENAMED rec - old device: $evl[1], new device: $evl[2] -> start deviceRename in DB: $own_hash->{DATABASE} ");
  1172. $own_hash->{HELPER}{OLDDEV} = $evl[1];
  1173. $own_hash->{HELPER}{NEWDEV} = $evl[2];
  1174. $own_hash->{HELPER}{RENMODE} = "devren";
  1175. DbRep_Main($own_hash,"deviceRename");
  1176. # die Attribute "device" in allen DbRep-Devices mit der Datenbank = DB des Agenten von alten Device in neues Device ändern
  1177. foreach(devspec2array("TYPE=DbRep")) {
  1178. my $repname = $_;
  1179. next if($_ eq $myName);
  1180. my $repattrdevice = $attr{$_}{device};
  1181. next if(!$repattrdevice);
  1182. my $repdb = $defs{$_}{DATABASE};
  1183. if ($repattrdevice eq $evl[1] && $repdb eq $own_hash->{DATABASE}) {
  1184. $attr{$_}{device} = $evl[2];
  1185. # Definitionsänderung wurde vorgenommen
  1186. $strucChanged = 1;
  1187. Log3 ($myName, 3, "DbRep Agent $myName - $_ attr device changed, old: \"$evl[1]\", new: \"$evl[2]\" ");
  1188. }
  1189. }
  1190. # if ($strucChanged) {CommandSave("","")};
  1191. }
  1192. }
  1193. return;
  1194. }
  1195. ###################################################################################
  1196. # DbRep_Undef
  1197. ###################################################################################
  1198. sub DbRep_Undef($$) {
  1199. my ($hash, $arg) = @_;
  1200. RemoveInternalTimer($hash);
  1201. my $dbh = $hash->{DBH};
  1202. $dbh->disconnect() if(defined($dbh));
  1203. BlockingKill($hash->{HELPER}{RUNNING_PID}) if (exists($hash->{HELPER}{RUNNING_PID}));
  1204. BlockingKill($hash->{HELPER}{RUNNING_BACKUP_CLIENT}) if (exists($hash->{HELPER}{RUNNING_BACKUP_CLIENT}));
  1205. BlockingKill($hash->{HELPER}{RUNNING_RESTORE}) if (exists($hash->{HELPER}{RUNNING_RESTORE}));
  1206. BlockingKill($hash->{HELPER}{RUNNING_BCKPREST_SERVER}) if (exists($hash->{HELPER}{RUNNING_BCKPREST_SERVER}));
  1207. BlockingKill($hash->{HELPER}{RUNNING_OPTIMIZE}) if (exists($hash->{HELPER}{RUNNING_OPTIMIZE}));
  1208. BlockingKill($hash->{HELPER}{RUNNING_REPAIR}) if (exists($hash->{HELPER}{RUNNING_REPAIR}));
  1209. DbRep_delread($hash,1);
  1210. return undef;
  1211. }
  1212. ###################################################################################
  1213. # DbRep_Shutdown
  1214. ###################################################################################
  1215. sub DbRep_Shutdown($) {
  1216. my ($hash) = @_;
  1217. my $dbh = $hash->{DBH};
  1218. $dbh->disconnect() if(defined($dbh));
  1219. DbRep_delread($hash,1);
  1220. RemoveInternalTimer($hash);
  1221. return undef;
  1222. }
  1223. ###################################################################################
  1224. # First Init DB Connect
  1225. # Verbindung zur DB aufbauen und den Timestamp des ältesten
  1226. # Datensatzes ermitteln
  1227. ###################################################################################
  1228. sub DbRep_firstconnect(@) {
  1229. my ($string) = @_;
  1230. my ($name,$opt,$prop,$fret) = split("\\|", $string);
  1231. my $hash = $defs{$name};
  1232. my $to = "120";
  1233. my $dbloghash = $hash->{dbloghash};
  1234. my $dbconn = $dbloghash->{dbconn};
  1235. my $dbuser = $dbloghash->{dbuser};
  1236. RemoveInternalTimer($hash, "DbRep_firstconnect");
  1237. return if(IsDisabled($name));
  1238. if ($init_done == 1) {
  1239. if (AttrVal($name, "fastStart", 0) && $prop eq "onBoot" ) {
  1240. $hash->{LASTCMD} = "initial database connect stopped due to attribute 'fastStart'";
  1241. return;
  1242. }
  1243. # DB Struktur aus DbLog Instanz übernehmen
  1244. $hash->{HELPER}{DBREPCOL}{COLSET} = $dbloghash->{HELPER}{COLSET};
  1245. $hash->{HELPER}{DBREPCOL}{DEVICECOL} = $dbloghash->{HELPER}{DEVICECOL};
  1246. $hash->{HELPER}{DBREPCOL}{EVENTCOL} = $dbloghash->{HELPER}{EVENTCOL};
  1247. $hash->{HELPER}{DBREPCOL}{READINGCOL} = $dbloghash->{HELPER}{READINGCOL};
  1248. $hash->{HELPER}{DBREPCOL}{TYPECOL} = $dbloghash->{HELPER}{TYPECOL};
  1249. $hash->{HELPER}{DBREPCOL}{UNITCOL} = $dbloghash->{HELPER}{UNITCOL};
  1250. $hash->{HELPER}{DBREPCOL}{VALUECOL} = $dbloghash->{HELPER}{VALUECOL};
  1251. # DB Strukturelemente abrufen
  1252. Log3 ($name, 3, "DbRep $name - Connectiontest to database $dbconn with user $dbuser") if($hash->{LASTCMD} ne "minTimestamp");
  1253. $hash->{HELPER}{RUNNING_PID} = BlockingCall("DbRep_getInitData", "$name|$opt|$prop|$fret", "DbRep_getInitDataDone", $to, "DbRep_getInitDataAborted", $hash);
  1254. $hash->{HELPER}{RUNNING_PID}{loglevel} = 5 if($hash->{HELPER}{RUNNING_PID}); # Forum #77057
  1255. } else {
  1256. InternalTimer(time+1, "DbRep_firstconnect", "$name|$opt|$prop|$fret", 0);
  1257. }
  1258. return;
  1259. }
  1260. ####################################################################################################
  1261. # den ältesten Datensatz (Timestamp) in der DB bestimmen
  1262. ####################################################################################################
  1263. sub DbRep_getInitData($) {
  1264. my ($string) = @_;
  1265. my ($name,$opt,$prop,$fret) = split("\\|", $string);
  1266. my $hash = $defs{$name};
  1267. my $dbloghash = $hash->{dbloghash};
  1268. my $dbconn = $dbloghash->{dbconn};
  1269. my $dbuser = $dbloghash->{dbuser};
  1270. my $dblogname = $dbloghash->{NAME};
  1271. my $dbpassword = $attr{"sec$dblogname"}{secret};
  1272. my $mintsdef = "1970-01-01 01:00:00";
  1273. my ($dbh,$sql,$err,$mints);
  1274. # Background-Startzeit
  1275. my $bst = [gettimeofday];
  1276. eval { $dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1 }); };
  1277. if ($@) {
  1278. $err = encode_base64($@,"");
  1279. Log3 ($name, 2, "DbRep $name - $@");
  1280. return "$name|''|''|$err";
  1281. }
  1282. # SQL-Startzeit
  1283. my $st = [gettimeofday];
  1284. eval { $mints = $dbh->selectrow_array("SELECT min(TIMESTAMP) FROM history;"); };
  1285. $dbh->disconnect;
  1286. # SQL-Laufzeit ermitteln
  1287. my $rt = tv_interval($st);
  1288. $mints = $mints?encode_base64($mints,""):encode_base64($mintsdef,"");
  1289. # Background-Laufzeit ermitteln
  1290. my $brt = tv_interval($bst);
  1291. $rt = $rt.",".$brt;
  1292. no warnings 'uninitialized';
  1293. return "$name|$mints|$rt|0|$opt|$prop|$fret";
  1294. }
  1295. ####################################################################################################
  1296. # Auswertungsroutine den ältesten Datensatz (Timestamp) in der DB bestimmen
  1297. ####################################################################################################
  1298. sub DbRep_getInitDataDone($) {
  1299. my ($string) = @_;
  1300. my @a = split("\\|",$string);
  1301. my $hash = $defs{$a[0]};
  1302. my $name = $hash->{NAME};
  1303. my $mints = decode_base64($a[1]);
  1304. my $bt = $a[2];
  1305. my ($rt,$brt) = split(",", $bt);
  1306. my $err = $a[3]?decode_base64($a[3]):undef;
  1307. my $opt = $a[4];
  1308. my $prop = $a[5];
  1309. my $fret = \&{$a[6]} if($a[6]);
  1310. my $dblogdevice = $hash->{HELPER}{DBLOGDEVICE};
  1311. $hash->{dbloghash} = $defs{$dblogdevice};
  1312. my $dbconn = $hash->{dbloghash}{dbconn};
  1313. if ($err) {
  1314. readingsBeginUpdate($hash);
  1315. ReadingsBulkUpdateValue ($hash, "errortext", $err);
  1316. ReadingsBulkUpdateValue ($hash, "state", "disconnected");
  1317. readingsEndUpdate($hash, 1);
  1318. delete($hash->{HELPER}{RUNNING_PID});
  1319. Log3 ($name, 2, "DbRep $name - DB connect failed. Make sure credentials of database $hash->{DATABASE} are valid and database is reachable.");
  1320. } else {
  1321. my $state = ($hash->{LASTCMD} eq "minTimestamp")?"done":"connected";
  1322. $state = "invalid timestamp \"$mints\" found in database - please delete it" if($mints =~ /^0000-00-00.*$/);
  1323. readingsBeginUpdate($hash);
  1324. ReadingsBulkUpdateValue ($hash, "timestamp_oldest_dataset", $mints) if($hash->{LASTCMD} eq "minTimestamp");
  1325. ReadingsBulkUpdateTimeState($hash,$brt,$rt,$state);
  1326. readingsEndUpdate($hash, 1);
  1327. Log3 ($name, 3, "DbRep $name - Connectiontest to db $dbconn successful") if($hash->{LASTCMD} ne "minTimestamp");
  1328. $hash->{HELPER}{MINTS} = $mints;
  1329. }
  1330. delete($hash->{HELPER}{RUNNING_PID});
  1331. return if(!$fret);
  1332. return &$fret($hash,$opt,$prop);
  1333. }
  1334. ####################################################################################################
  1335. # Abbruchroutine den ältesten Datensatz (Timestamp) in der DB bestimmen
  1336. ####################################################################################################
  1337. sub DbRep_getInitDataAborted(@) {
  1338. my ($hash,$cause) = @_;
  1339. my $name = $hash->{NAME};
  1340. $cause = $cause?$cause:"Timeout: process terminated";
  1341. Log3 ($name, 1, "DbRep $name -> BlockingCall $hash->{HELPER}{RUNNING_PID}{fn} pid:$hash->{HELPER}{RUNNING_PID}{pid} $cause");
  1342. readingsBeginUpdate($hash);
  1343. ReadingsBulkUpdateValue ($hash, "errortext", $cause);
  1344. ReadingsBulkUpdateValue ($hash, "state", "disconnected");
  1345. readingsEndUpdate($hash, 1);
  1346. delete($hash->{HELPER}{RUNNING_PID});
  1347. return;
  1348. }
  1349. ################################################################################################################
  1350. # Hauptroutine
  1351. ################################################################################################################
  1352. sub DbRep_Main($$;$) {
  1353. my ($hash,$opt,$prop) = @_;
  1354. my $name = $hash->{NAME};
  1355. my $to = AttrVal($name, "timeout", "86400");
  1356. my $reading = AttrVal($name, "reading", "%");
  1357. my $device = AttrVal($name, "device", "%");
  1358. my $dbloghash = $hash->{dbloghash};
  1359. my $dbmodel = $dbloghash->{MODEL};
  1360. # Entkommentieren für Testroutine im Vordergrund
  1361. # testexit($hash);
  1362. return if( ($hash->{HELPER}{RUNNING_BACKUP_CLIENT} ||
  1363. $hash->{HELPER}{RUNNING_BCKPREST_SERVER} ||
  1364. $hash->{HELPER}{RUNNING_RESTORE} ||
  1365. $hash->{HELPER}{RUNNING_REPAIR} ||
  1366. $hash->{HELPER}{RUNNING_REDUCELOG} ||
  1367. $hash->{HELPER}{RUNNING_OPTIMIZE}) &&
  1368. $opt !~ /dumpMySQL|restoreMySQL|dumpSQLite|restoreSQLite|optimizeTables|vacuum|repairSQLite/ );
  1369. # Readings löschen die nicht in der Ausnahmeliste (Attr readingPreventFromDel) stehen
  1370. DbRep_delread($hash);
  1371. if ($opt =~ /dumpMySQL|dumpSQLite/) {
  1372. BlockingKill($hash->{HELPER}{RUNNING_BACKUP_CLIENT}) if (exists($hash->{HELPER}{RUNNING_BACKUP_CLIENT}));
  1373. BlockingKill($hash->{HELPER}{RUNNING_BCKPREST_SERVER}) if (exists($hash->{HELPER}{RUNNING_BCKPREST_SERVER}));
  1374. BlockingKill($hash->{HELPER}{RUNNING_OPTIMIZE}) if (exists($hash->{HELPER}{RUNNING_OPTIMIZE}));
  1375. if($dbmodel =~ /MYSQL/) {
  1376. if ($prop eq "serverSide") {
  1377. $hash->{HELPER}{RUNNING_BCKPREST_SERVER} = BlockingCall("mysql_DoDumpServerSide", "$name", "DbRep_DumpDone", $to, "DbRep_DumpAborted", $hash);
  1378. ReadingsSingleUpdateValue ($hash, "state", "serverSide Dump is running - be patient and see Logfile !", 1);
  1379. } else {
  1380. $hash->{HELPER}{RUNNING_BACKUP_CLIENT} = BlockingCall("mysql_DoDumpClientSide", "$name", "DbRep_DumpDone", $to, "DbRep_DumpAborted", $hash);
  1381. ReadingsSingleUpdateValue ($hash, "state", "clientSide Dump is running - be patient and see Logfile !", 1);
  1382. }
  1383. }
  1384. if($dbmodel =~ /SQLITE/) {
  1385. $hash->{HELPER}{RUNNING_BACKUP_CLIENT} = BlockingCall("DbRep_sqliteDoDump", "$name", "DbRep_DumpDone", $to, "DbRep_DumpAborted", $hash);
  1386. ReadingsSingleUpdateValue ($hash, "state", "SQLite Dump is running - be patient and see Logfile !", 1);
  1387. }
  1388. return;
  1389. }
  1390. if ($opt =~ /restoreMySQL/) {
  1391. BlockingKill($hash->{HELPER}{RUNNING_RESTORE}) if (exists($hash->{HELPER}{RUNNING_RESTORE}));
  1392. BlockingKill($hash->{HELPER}{RUNNING_OPTIMIZE}) if (exists($hash->{HELPER}{RUNNING_OPTIMIZE}));
  1393. if($prop =~ /csv/) {
  1394. $hash->{HELPER}{RUNNING_RESTORE} = BlockingCall("mysql_RestoreServerSide", "$name|$prop", "DbRep_restoreDone", $to, "DbRep_restoreAborted", $hash);
  1395. } elsif ($prop =~ /sql/) {
  1396. $hash->{HELPER}{RUNNING_RESTORE} = BlockingCall("mysql_RestoreClientSide", "$name|$prop", "DbRep_restoreDone", $to, "DbRep_restoreAborted", $hash);
  1397. } else {
  1398. ReadingsSingleUpdateValue ($hash, "state", "restore database error - unknown fileextension \"$prop\"", 1);
  1399. }
  1400. ReadingsSingleUpdateValue ($hash, "state", "restore database is running - be patient and see Logfile !", 1);
  1401. return;
  1402. }
  1403. if ($opt =~ /restoreSQLite/) {
  1404. BlockingKill($hash->{HELPER}{RUNNING_RESTORE}) if (exists($hash->{HELPER}{RUNNING_RESTORE}));
  1405. BlockingKill($hash->{HELPER}{RUNNING_OPTIMIZE}) if (exists($hash->{HELPER}{RUNNING_OPTIMIZE}));
  1406. $hash->{HELPER}{RUNNING_RESTORE} = BlockingCall("DbRep_sqliteRestore", "$name|$prop", "DbRep_restoreDone", $to, "DbRep_restoreAborted", $hash);
  1407. ReadingsSingleUpdateValue ($hash, "state", "restore database is running - be patient and see Logfile !", 1);
  1408. return;
  1409. }
  1410. if ($opt =~ /optimizeTables|vacuum/) {
  1411. BlockingKill($hash->{HELPER}{RUNNING_OPTIMIZE}) if (exists($hash->{HELPER}{RUNNING_OPTIMIZE}));
  1412. BlockingKill($hash->{HELPER}{RUNNING_RESTORE}) if (exists($hash->{HELPER}{RUNNING_RESTORE}));
  1413. $hash->{HELPER}{RUNNING_OPTIMIZE} = BlockingCall("DbRep_optimizeTables", "$name", "DbRep_OptimizeDone", $to, "DbRep_OptimizeAborted", $hash);
  1414. ReadingsSingleUpdateValue ($hash, "state", "optimize tables is running - be patient and see Logfile !", 1);
  1415. return;
  1416. }
  1417. if ($opt =~ /repairSQLite/) {
  1418. BlockingKill($hash->{HELPER}{RUNNING_BACKUP_CLIENT}) if (exists($hash->{HELPER}{RUNNING_BACKUP_CLIENT}));
  1419. BlockingKill($hash->{HELPER}{RUNNING_OPTIMIZE}) if (exists($hash->{HELPER}{RUNNING_OPTIMIZE}));
  1420. BlockingKill($hash->{HELPER}{RUNNING_REPAIR}) if (exists($hash->{HELPER}{RUNNING_REPAIR}));
  1421. $hash->{HELPER}{RUNNING_REPAIR} = BlockingCall("DbRep_sqliteRepair", "$name", "DbRep_RepairDone", $to, "DbRep_RepairAborted", $hash);
  1422. ReadingsSingleUpdateValue ($hash, "state", "repair database is running - be patient and see Logfile !", 1);
  1423. return;
  1424. }
  1425. if (exists($hash->{HELPER}{RUNNING_PID}) && $hash->{ROLE} ne "Agent") {
  1426. Log3 ($name, 3, "DbRep $name - WARNING - old process $hash->{HELPER}{RUNNING_PID}{pid} will be killed now to start a new BlockingCall");
  1427. BlockingKill($hash->{HELPER}{RUNNING_PID});
  1428. }
  1429. # initiale Datenermittlung wie minimal Timestamp, Datenbankstrukturen, ...
  1430. if(!$hash->{HELPER}{MINTS} or !$hash->{HELPER}{DBREPCOL}{COLSET}) {
  1431. my $dbname = $hash->{DATABASE};
  1432. Log3 ($name, 3, "DbRep $name - get initial structure information of database \"$dbname\", remaining attempts: ".$hash->{HELPER}{IDRETRIES});
  1433. $prop = $prop?$prop:'';
  1434. DbRep_firstconnect("$name|$opt|$prop|DbRep_Main") if($hash->{HELPER}{IDRETRIES} > 0);
  1435. $hash->{HELPER}{IDRETRIES}--;
  1436. return;
  1437. }
  1438. ReadingsSingleUpdateValue ($hash, "state", "running", 1);
  1439. # only for this block because of warnings if details of readings are not set
  1440. no warnings 'uninitialized';
  1441. # Ausgaben und Zeitmanipulationen
  1442. Log3 ($name, 4, "DbRep $name - -------- New selection --------- ");
  1443. Log3 ($name, 4, "DbRep $name - Command: $opt $prop");
  1444. # zentrales Timestamp-Array und Zeitgrenzen bereitstellen
  1445. my ($epoch_seconds_begin,$epoch_seconds_end,$runtime_string_first,$runtime_string_next);
  1446. my $ts = "no_aggregation"; # Dummy für eine Select-Schleife wenn != $IsTimeSet || $IsAggrSet
  1447. my ($IsTimeSet,$IsAggrSet,$aggregation) = DbRep_checktimeaggr($hash);
  1448. if($IsTimeSet || $IsAggrSet) {
  1449. ($epoch_seconds_begin,$epoch_seconds_end,$runtime_string_first,$runtime_string_next,$ts) = DbRep_createTimeArray($hash,$aggregation,$opt);
  1450. } else {
  1451. Log3 ($name, 4, "DbRep $name - Timestamp begin human readable: not set") if($opt !~ /tableCurrentPurge/);
  1452. Log3 ($name, 4, "DbRep $name - Timestamp end human readable: not set") if($opt !~ /tableCurrentPurge/);
  1453. }
  1454. Log3 ($name, 4, "DbRep $name - Aggregation: $aggregation") if($opt !~ /tableCurrentPurge|tableCurrentFillup|fetchrows|insert|reduceLog/);
  1455. ##### Funktionsaufrufe #####
  1456. if ($opt eq "sumValue") {
  1457. $hash->{HELPER}{RUNNING_PID} = BlockingCall("sumval_DoParse", "$name§$device§$reading§$prop§$ts", "sumval_ParseDone", $to, "DbRep_ParseAborted", $hash);
  1458. } elsif ($opt =~ m/countEntries/) {
  1459. my $table = $prop;
  1460. $hash->{HELPER}{RUNNING_PID} = BlockingCall("count_DoParse", "$name§$table§$device§$reading§$ts", "count_ParseDone", $to, "DbRep_ParseAborted", $hash);
  1461. } elsif ($opt eq "averageValue") {
  1462. Log3 ($name, 4, "DbRep $name - averageValue calculation sceme: ".AttrVal($name,"averageCalcForm","avgArithmeticMean"));
  1463. $hash->{HELPER}{RUNNING_PID} = BlockingCall("averval_DoParse", "$name§$device§$reading§$prop§$ts", "averval_ParseDone", $to, "DbRep_ParseAborted", $hash);
  1464. } elsif ($opt eq "fetchrows") {
  1465. my $table = $prop;
  1466. $hash->{HELPER}{RUNNING_PID} = BlockingCall("fetchrows_DoParse", "$name|$table|$device|$reading|$runtime_string_first|$runtime_string_next", "fetchrows_ParseDone", $to, "DbRep_ParseAborted", $hash);
  1467. } elsif ($opt =~ /delSeqDoublets/) {
  1468. my $cmd = $prop?$prop:"adviceRemain";
  1469. $hash->{HELPER}{RUNNING_PID} = BlockingCall("delseqdoubl_DoParse", "$name§$cmd§$device§$reading§$ts", "delseqdoubl_ParseDone", $to, "DbRep_ParseAborted", $hash);
  1470. } elsif ($opt eq "exportToFile") {
  1471. my $file = $prop;
  1472. DbRep_beforeproc($hash, "export");
  1473. $hash->{HELPER}{RUNNING_PID} = BlockingCall("expfile_DoParse", "$name§$device§$reading§$runtime_string_first§$file§$ts", "expfile_ParseDone", $to, "DbRep_ParseAborted", $hash);
  1474. } elsif ($opt eq "importFromFile") {
  1475. my $file = $prop;
  1476. DbRep_beforeproc($hash, "import");
  1477. $hash->{HELPER}{RUNNING_PID} = BlockingCall("impfile_Push", "$name|$runtime_string_first|$file", "impfile_PushDone", $to, "DbRep_ParseAborted", $hash);
  1478. } elsif ($opt eq "maxValue") {
  1479. $hash->{HELPER}{RUNNING_PID} = BlockingCall("maxval_DoParse", "$name§$device§$reading§$prop§$ts", "maxval_ParseDone", $to, "DbRep_ParseAborted", $hash);
  1480. } elsif ($opt eq "minValue") {
  1481. $hash->{HELPER}{RUNNING_PID} = BlockingCall("minval_DoParse", "$name§$device§$reading§$prop§$ts", "minval_ParseDone", $to, "DbRep_ParseAborted", $hash);
  1482. } elsif ($opt eq "delEntries") {
  1483. $hash->{HELPER}{RUNNING_PID} = BlockingCall("del_DoParse", "$name|history|$device|$reading|$runtime_string_first|$runtime_string_next", "del_ParseDone", $to, "DbRep_ParseAborted", $hash);
  1484. } elsif ($opt eq "tableCurrentPurge") {
  1485. undef $runtime_string_first;
  1486. undef $runtime_string_next;
  1487. $hash->{HELPER}{RUNNING_PID} = BlockingCall("del_DoParse", "$name|current|$device|$reading|$runtime_string_first|$runtime_string_next", "del_ParseDone", $to, "DbRep_ParseAborted", $hash);
  1488. } elsif ($opt eq "tableCurrentFillup") {
  1489. $hash->{HELPER}{RUNNING_PID} = BlockingCall("currentfillup_Push", "$name|$device|$reading|$runtime_string_first|$runtime_string_next", "currentfillup_Done", $to, "DbRep_ParseAborted", $hash);
  1490. } elsif ($opt eq "diffValue") {
  1491. $hash->{HELPER}{RUNNING_PID} = BlockingCall("diffval_DoParse", "$name§$device§$reading§$prop§$ts", "diffval_ParseDone", $to, "DbRep_ParseAborted", $hash);
  1492. } elsif ($opt eq "insert") {
  1493. $hash->{HELPER}{RUNNING_PID} = BlockingCall("insert_Push", "$name", "insert_Done", $to, "DbRep_ParseAborted", $hash);
  1494. } elsif ($opt =~ /deviceRename|readingRename/) {
  1495. $hash->{HELPER}{RUNNING_PID} = BlockingCall("change_Push", "$name|$device|$reading|$runtime_string_first|$runtime_string_next", "change_Done", $to, "DbRep_ParseAborted", $hash);
  1496. } elsif ($opt =~ /changeValue/) {
  1497. $hash->{HELPER}{RUNNING_PID} = BlockingCall("changeval_Push", "$name§$device§$reading§$runtime_string_first§$runtime_string_next§$ts", "change_Done", $to, "DbRep_ParseAborted", $hash);
  1498. } elsif ($opt =~ /sqlCmd|sqlSpecial/ ) {
  1499. # Execute a generic sql command or special sql
  1500. if ($opt =~ /sqlSpecial/) {
  1501. if($prop eq "50mostFreqLogsLast2days") {
  1502. $prop = "select Device, reading, count(0) AS `countA` from history where ( TIMESTAMP > (now() - interval 2 day)) group by DEVICE, READING order by countA desc, DEVICE limit 50;" if($dbmodel =~ /MYSQL/);
  1503. $prop = "select Device, reading, count(0) AS `countA` from history where ( TIMESTAMP > ('now' - '2 days')) group by DEVICE, READING order by countA desc, DEVICE limit 50;" if($dbmodel =~ /SQLITE/);
  1504. $prop = "select Device, reading, count(0) AS countA from history where ( TIMESTAMP > (NOW() - INTERVAL '2' DAY)) group by DEVICE, READING order by countA desc, DEVICE limit 50;" if($dbmodel =~ /POSTGRESQL/);
  1505. } elsif ($prop eq "allDevReadCount") {
  1506. $prop = "select device, reading, count(*) from history group by DEVICE, READING;";
  1507. } elsif ($prop eq "allDevCount") {
  1508. $prop = "select device, count(*) from history group by DEVICE;";
  1509. }
  1510. }
  1511. $hash->{HELPER}{RUNNING_PID} = BlockingCall("sqlCmd_DoParse", "$name|$opt|$runtime_string_first|$runtime_string_next|$prop", "sqlCmd_ParseDone", $to, "DbRep_ParseAborted", $hash);
  1512. } elsif ($opt =~ /syncStandby/ ) {
  1513. # Befehl vor Procedure ausführen
  1514. DbRep_beforeproc($hash, "syncStandby");
  1515. $hash->{HELPER}{RUNNING_PID} = BlockingCall("DbRep_syncStandby", "$name§$device§$reading§$runtime_string_first§$runtime_string_next§$ts§$prop", "DbRep_syncStandbyDone", $to, "DbRep_ParseAborted", $hash);
  1516. }
  1517. if ($opt =~ /reduceLog/) {
  1518. $hash->{HELPER}{RUNNING_REDUCELOG} = BlockingCall("DbRep_reduceLog", "$name|$device|$reading|$runtime_string_first|$runtime_string_next", "DbRep_reduceLogDone", $to, "DbRep_reduceLogAborted", $hash);
  1519. ReadingsSingleUpdateValue ($hash, "state", "reduceLog database is running - be patient and see Logfile !", 1);
  1520. $hash->{HELPER}{RUNNING_REDUCELOG}{loglevel} = 5 if($hash->{HELPER}{RUNNING_REDUCELOG}); # Forum #77057
  1521. return;
  1522. }
  1523. $hash->{HELPER}{RUNNING_PID}{loglevel} = 5 if($hash->{HELPER}{RUNNING_PID}); # Forum #77057
  1524. return;
  1525. }
  1526. ################################################################################################################
  1527. # Create zentrales Timsstamp-Array
  1528. ################################################################################################################
  1529. sub DbRep_createTimeArray($$$) {
  1530. my ($hash,$aggregation,$opt) = @_;
  1531. my $name = $hash->{NAME};
  1532. # year als Jahre seit 1900
  1533. # $mon als 0..11
  1534. # $time = timelocal( $sec, $min, $hour, $mday, $mon, $year );
  1535. my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); # Istzeit Ableitung
  1536. my ($tsbegin,$tsend,$dim,$tsub,$tadd);
  1537. my ($rsec,$rmin,$rhour,$rmday,$rmon,$ryear);
  1538. # absolute Auswertungszeiträume statische und dynamische (Beginn / Ende) berechnen
  1539. if($hash->{HELPER}{MINTS} && $hash->{HELPER}{MINTS} =~ m/0000-00-00/) {
  1540. Log3 ($name, 1, "DbRep $name - ERROR - wrong timestamp \"$hash->{HELPER}{MINTS}\" found in database. Please delete it !");
  1541. delete $hash->{HELPER}{MINTS};
  1542. }
  1543. my $mints = $hash->{HELPER}{MINTS}?$hash->{HELPER}{MINTS}:"1970-01-01 01:00:00"; # Timestamp des 1. Datensatzes verwenden falls ermittelt
  1544. $tsbegin = AttrVal($name, "timestamp_begin", $mints);
  1545. $tsbegin = DbRep_formatpicker($tsbegin);
  1546. $tsend = AttrVal($name, "timestamp_end", strftime "%Y-%m-%d %H:%M:%S", localtime(time));
  1547. $tsend = DbRep_formatpicker($tsend);
  1548. if ( my $tap = AttrVal($name, "timeYearPeriod", undef)) {
  1549. # a b c d
  1550. # 06-01 02-28 , wenn c < a && $mon < a -> Jahr(a)-1, sonst Jahr(c)+1
  1551. my $ybp = $year+1900;
  1552. my $yep = $year+1900;
  1553. $tap =~ qr/^(\d{2})-(\d{2}) (\d{2})-(\d{2})$/p;
  1554. my $mbp = $1;
  1555. my $dbp = $2;
  1556. my $mep = $3;
  1557. my $dep = $4;
  1558. my $c = ($mon+1).$mday;
  1559. my $e = $mep.$dep;
  1560. if ($mep <= $mbp && $c <= $e) {
  1561. $ybp--;
  1562. } else {
  1563. $yep++;
  1564. }
  1565. $tsbegin = "$ybp-$mbp-$dbp 00:00:00";
  1566. $tsend = "$yep-$mep-$dep 23:59:59";
  1567. }
  1568. if (AttrVal($name,"timestamp_begin","") eq "current_year_begin" ||
  1569. AttrVal($name,"timestamp_end","") eq "current_year_begin") {
  1570. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,0,1,0,$year)) if(AttrVal($name,"timestamp_begin","") eq "current_year_begin");
  1571. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,0,1,0,$year)) if(AttrVal($name,"timestamp_end","") eq "current_year_begin");
  1572. }
  1573. if (AttrVal($name, "timestamp_begin", "") eq "current_year_end" ||
  1574. AttrVal($name, "timestamp_end", "") eq "current_year_end") {
  1575. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,23,31,11,$year)) if(AttrVal($name,"timestamp_begin","") eq "current_year_end");
  1576. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,23,31,11,$year)) if(AttrVal($name,"timestamp_end","") eq "current_year_end");
  1577. }
  1578. if (AttrVal($name, "timestamp_begin", "") eq "previous_year_begin" ||
  1579. AttrVal($name, "timestamp_end", "") eq "previous_year_begin") {
  1580. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,0,1,0,$year-1)) if(AttrVal($name, "timestamp_begin", "") eq "previous_year_begin");
  1581. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,0,1,0,$year-1)) if(AttrVal($name, "timestamp_end", "") eq "previous_year_begin");
  1582. }
  1583. if (AttrVal($name, "timestamp_begin", "") eq "previous_year_end" ||
  1584. AttrVal($name, "timestamp_end", "") eq "previous_year_end") {
  1585. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,23,31,11,$year-1)) if(AttrVal($name, "timestamp_begin", "") eq "previous_year_end");
  1586. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,23,31,11,$year-1)) if(AttrVal($name, "timestamp_end", "") eq "previous_year_end");
  1587. }
  1588. if (AttrVal($name, "timestamp_begin", "") eq "current_month_begin" ||
  1589. AttrVal($name, "timestamp_end", "") eq "current_month_begin") {
  1590. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,0,1,$mon,$year)) if(AttrVal($name, "timestamp_begin", "") eq "current_month_begin");
  1591. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,0,1,$mon,$year)) if(AttrVal($name, "timestamp_end", "") eq "current_month_begin");
  1592. }
  1593. if (AttrVal($name, "timestamp_begin", "") eq "current_month_end" ||
  1594. AttrVal($name, "timestamp_end", "") eq "current_month_end") {
  1595. $dim = $mon-1?30+(($mon+1)*3%7<4):28+!($year%4||$year%400*!($year%100));
  1596. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,23,$dim,$mon,$year)) if(AttrVal($name, "timestamp_begin", "") eq "current_month_end");
  1597. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,23,$dim,$mon,$year)) if(AttrVal($name, "timestamp_end", "") eq "current_month_end");
  1598. }
  1599. if (AttrVal($name, "timestamp_begin", "") eq "previous_month_begin" ||
  1600. AttrVal($name, "timestamp_end", "") eq "previous_month_begin") {
  1601. $ryear = ($mon-1<0)?$year-1:$year;
  1602. $rmon = ($mon-1<0)?11:$mon-1;
  1603. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,0,1,$rmon,$ryear)) if(AttrVal($name, "timestamp_begin", "") eq "previous_month_begin");
  1604. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,0,1,$rmon,$ryear)) if(AttrVal($name, "timestamp_end", "") eq "previous_month_begin");
  1605. }
  1606. if (AttrVal($name, "timestamp_begin", "") eq "previous_month_end" ||
  1607. AttrVal($name, "timestamp_end", "") eq "previous_month_end") {
  1608. $ryear = ($mon-1<0)?$year-1:$year;
  1609. $rmon = ($mon-1<0)?11:$mon-1;
  1610. $dim = $rmon-1?30+(($rmon+1)*3%7<4):28+!($ryear%4||$ryear%400*!($ryear%100));
  1611. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,23,$dim,$rmon,$ryear)) if(AttrVal($name, "timestamp_begin", "") eq "previous_month_end");
  1612. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,23,$dim,$rmon,$ryear)) if(AttrVal($name, "timestamp_end", "") eq "previous_month_end");
  1613. }
  1614. if (AttrVal($name, "timestamp_begin", "") eq "current_week_begin" ||
  1615. AttrVal($name, "timestamp_end", "") eq "current_week_begin") {
  1616. $tsub = 0 if($wday == 1); # wenn Start am "Mo" keine Korrektur
  1617. $tsub = 86400 if($wday == 2); # wenn Start am "Di" dann Korrektur -1 Tage
  1618. $tsub = 172800 if($wday == 3); # wenn Start am "Mi" dann Korrektur -2 Tage
  1619. $tsub = 259200 if($wday == 4); # wenn Start am "Do" dann Korrektur -3 Tage
  1620. $tsub = 345600 if($wday == 5); # wenn Start am "Fr" dann Korrektur -4 Tage
  1621. $tsub = 432000 if($wday == 6); # wenn Start am "Sa" dann Korrektur -5 Tage
  1622. $tsub = 518400 if($wday == 0); # wenn Start am "So" dann Korrektur -6 Tage
  1623. ($rsec,$rmin,$rhour,$rmday,$rmon,$ryear) = localtime(time-$tsub);
  1624. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,0,$rmday,$rmon,$ryear)) if(AttrVal($name, "timestamp_begin", "") eq "current_week_begin");
  1625. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,0,$rmday,$rmon,$ryear)) if(AttrVal($name, "timestamp_end", "") eq "current_week_begin");
  1626. }
  1627. if (AttrVal($name, "timestamp_begin", "") eq "current_week_end" ||
  1628. AttrVal($name, "timestamp_end", "") eq "current_week_end") {
  1629. $tadd = 518400 if($wday == 1); # wenn Start am "Mo" dann Korrektur +6 Tage
  1630. $tadd = 432000 if($wday == 2); # wenn Start am "Di" dann Korrektur +5 Tage
  1631. $tadd = 345600 if($wday == 3); # wenn Start am "Mi" dann Korrektur +4 Tage
  1632. $tadd = 259200 if($wday == 4); # wenn Start am "Do" dann Korrektur +3 Tage
  1633. $tadd = 172800 if($wday == 5); # wenn Start am "Fr" dann Korrektur +2 Tage
  1634. $tadd = 86400 if($wday == 6); # wenn Start am "Sa" dann Korrektur +1 Tage
  1635. $tadd = 0 if($wday == 0); # wenn Start am "So" keine Korrektur
  1636. ($rsec,$rmin,$rhour,$rmday,$rmon,$ryear) = localtime(time+$tadd);
  1637. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,23,$rmday,$rmon,$ryear)) if(AttrVal($name, "timestamp_begin", "") eq "current_week_end");
  1638. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,23,$rmday,$rmon,$ryear)) if(AttrVal($name, "timestamp_end", "") eq "current_week_end");
  1639. }
  1640. if (AttrVal($name, "timestamp_begin", "") eq "previous_week_begin" ||
  1641. AttrVal($name, "timestamp_end", "") eq "previous_week_begin") {
  1642. $tsub = 604800 if($wday == 1); # wenn Start am "Mo" dann Korrektur -7 Tage
  1643. $tsub = 691200 if($wday == 2); # wenn Start am "Di" dann Korrektur -8 Tage
  1644. $tsub = 777600 if($wday == 3); # wenn Start am "Mi" dann Korrektur -9 Tage
  1645. $tsub = 864000 if($wday == 4); # wenn Start am "Do" dann Korrektur -10 Tage
  1646. $tsub = 950400 if($wday == 5); # wenn Start am "Fr" dann Korrektur -11 Tage
  1647. $tsub = 1036800 if($wday == 6); # wenn Start am "Sa" dann Korrektur -12 Tage
  1648. $tsub = 1123200 if($wday == 0); # wenn Start am "So" dann Korrektur -13 Tage
  1649. ($rsec,$rmin,$rhour,$rmday,$rmon,$ryear) = localtime(time-$tsub);
  1650. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,0,$rmday,$rmon,$ryear)) if(AttrVal($name, "timestamp_begin", "") eq "previous_week_begin");
  1651. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,0,$rmday,$rmon,$ryear)) if(AttrVal($name, "timestamp_end", "") eq "previous_week_begin");
  1652. }
  1653. if (AttrVal($name, "timestamp_begin", "") eq "previous_week_end" ||
  1654. AttrVal($name, "timestamp_end", "") eq "previous_week_end") {
  1655. $tsub = 86400 if($wday == 1); # wenn Start am "Mo" dann Korrektur -1 Tage
  1656. $tsub = 172800 if($wday == 2); # wenn Start am "Di" dann Korrektur -2 Tage
  1657. $tsub = 259200 if($wday == 3); # wenn Start am "Mi" dann Korrektur -3 Tage
  1658. $tsub = 345600 if($wday == 4); # wenn Start am "Do" dann Korrektur -4 Tage
  1659. $tsub = 432000 if($wday == 5); # wenn Start am "Fr" dann Korrektur -5 Tage
  1660. $tsub = 518400 if($wday == 6); # wenn Start am "Sa" dann Korrektur -6 Tage
  1661. $tsub = 604800 if($wday == 0); # wenn Start am "So" dann Korrektur -7 Tage
  1662. ($rsec,$rmin,$rhour,$rmday,$rmon,$ryear) = localtime(time-$tsub);
  1663. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,23,$rmday,$rmon,$ryear)) if(AttrVal($name, "timestamp_begin", "") eq "previous_week_end");
  1664. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,23,$rmday,$rmon,$ryear)) if(AttrVal($name, "timestamp_end", "") eq "previous_week_end");
  1665. }
  1666. if (AttrVal($name, "timestamp_begin", "") eq "current_day_begin" ||
  1667. AttrVal($name, "timestamp_end", "") eq "current_day_begin") {
  1668. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,0,$mday,$mon,$year)) if(AttrVal($name, "timestamp_begin", "") eq "current_day_begin");
  1669. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,0,$mday,$mon,$year)) if(AttrVal($name, "timestamp_end", "") eq "current_day_begin");
  1670. }
  1671. if (AttrVal($name, "timestamp_begin", "") eq "current_day_end" ||
  1672. AttrVal($name, "timestamp_end", "") eq "current_day_end") {
  1673. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,23,$mday,$mon,$year)) if(AttrVal($name, "timestamp_begin", "") eq "current_day_end");
  1674. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,23,$mday,$mon,$year)) if(AttrVal($name, "timestamp_end", "") eq "current_day_end");
  1675. }
  1676. if (AttrVal($name, "timestamp_begin", "") eq "previous_day_begin" ||
  1677. AttrVal($name, "timestamp_end", "") eq "previous_day_begin") {
  1678. $rmday = $mday-1;
  1679. $rmon = $mon;
  1680. $ryear = $year;
  1681. if($rmday<1) {
  1682. $rmon--;
  1683. if ($rmon<0) {
  1684. $rmon=11;
  1685. $ryear--;
  1686. }
  1687. $rmday = $rmon-1?30+(($rmon+1)*3%7<4):28+!($ryear%4||$ryear%400*!($ryear%100)); # Achtung: Monat als 1...12 (statt 0...11)
  1688. }
  1689. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,0,$rmday,$rmon,$ryear)) if(AttrVal($name, "timestamp_begin", "") eq "previous_day_begin");
  1690. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,0,$rmday,$rmon,$ryear)) if(AttrVal($name, "timestamp_end", "") eq "previous_day_begin");
  1691. }
  1692. if (AttrVal($name, "timestamp_begin", "") eq "previous_day_end" ||
  1693. AttrVal($name, "timestamp_end", "") eq "previous_day_end") {
  1694. $rmday = $mday-1;
  1695. $rmon = $mon;
  1696. $ryear = $year;
  1697. if($rmday<1) {
  1698. $rmon--;
  1699. if ($rmon<0) {
  1700. $rmon=11;
  1701. $ryear--;
  1702. }
  1703. $rmday = $rmon-1?30+(($rmon+1)*3%7<4):28+!($ryear%4||$ryear%400*!($ryear%100)); # Achtung: Monat als 1...12 (statt 0...11)
  1704. }
  1705. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,23,$rmday,$rmon,$ryear)) if(AttrVal($name, "timestamp_begin", "") eq "previous_day_end");
  1706. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,23,$rmday,$rmon,$ryear)) if(AttrVal($name, "timestamp_end", "") eq "previous_day_end");
  1707. }
  1708. if (AttrVal($name, "timestamp_begin", "") eq "current_hour_begin" ||
  1709. AttrVal($name, "timestamp_end", "") eq "current_hour_begin") {
  1710. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,$hour,$mday,$mon,$year)) if(AttrVal($name, "timestamp_begin", "") eq "current_hour_begin");
  1711. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,$hour,$mday,$mon,$year)) if(AttrVal($name, "timestamp_end", "") eq "current_hour_begin");
  1712. }
  1713. if (AttrVal($name, "timestamp_begin", "") eq "current_hour_end" ||
  1714. AttrVal($name, "timestamp_end", "") eq "current_hour_end") {
  1715. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,$hour,$mday,$mon,$year)) if(AttrVal($name, "timestamp_begin", "") eq "current_hour_end");
  1716. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,$hour,$mday,$mon,$year)) if(AttrVal($name, "timestamp_end", "") eq "current_hour_end");
  1717. }
  1718. if (AttrVal($name, "timestamp_begin", "") eq "previous_hour_begin" ||
  1719. AttrVal($name, "timestamp_end", "") eq "previous_hour_begin") {
  1720. $rhour = $hour-1;
  1721. $rmday = $mday;
  1722. $rmon = $mon;
  1723. $ryear = $year;
  1724. if($rhour<0) {
  1725. $rhour = 23;
  1726. $rmday = $mday-1;
  1727. if($rmday<1) {
  1728. $rmon--;
  1729. if ($rmon<0) {
  1730. $rmon=11;
  1731. $ryear--;
  1732. }
  1733. $rmday = $rmon-1?30+(($rmon+1)*3%7<4):28+!($ryear%4||$ryear%400*!($ryear%100)); # Achtung: Monat als 1...12 (statt 0...11)
  1734. }
  1735. }
  1736. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,$rhour,$rmday,$rmon,$ryear)) if(AttrVal($name, "timestamp_begin", "") eq "previous_hour_begin");
  1737. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,$rhour,$rmday,$rmon,$ryear)) if(AttrVal($name, "timestamp_end", "") eq "previous_hour_begin");
  1738. }
  1739. if (AttrVal($name, "timestamp_begin", "") eq "previous_hour_end" ||
  1740. AttrVal($name, "timestamp_end", "") eq "previous_hour_end") {
  1741. $rhour = $hour-1;
  1742. $rmday = $mday;
  1743. $rmon = $mon;
  1744. $ryear = $year;
  1745. if($rhour<0) {
  1746. $rhour = 23;
  1747. $rmday = $mday-1;
  1748. if($rmday<1) {
  1749. $rmon--;
  1750. if ($rmon<0) {
  1751. $rmon=11;
  1752. $ryear--;
  1753. }
  1754. $rmday = $rmon-1?30+(($rmon+1)*3%7<4):28+!($ryear%4||$ryear%400*!($ryear%100)); # Achtung: Monat als 1...12 (statt 0...11)
  1755. }
  1756. }
  1757. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,$rhour,$rmday,$rmon,$ryear)) if(AttrVal($name, "timestamp_begin", "") eq "previous_hour_end");
  1758. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,$rhour,$rmday,$rmon,$ryear)) if(AttrVal($name, "timestamp_end", "") eq "previous_hour_end");
  1759. }
  1760. # extrahieren der Einzelwerte von Datum/Zeit Beginn
  1761. my ($yyyy1, $mm1, $dd1, $hh1, $min1, $sec1) = ($tsbegin =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/);
  1762. # extrahieren der Einzelwerte von Datum/Zeit Ende
  1763. my ($yyyy2, $mm2, $dd2, $hh2, $min2, $sec2) = ($tsend =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/);
  1764. # relative Auswertungszeit Beginn berücksichtigen # Umwandeln in Epochesekunden Beginn
  1765. my $epoch_seconds_begin = timelocal($sec1, $min1, $hh1, $dd1, $mm1-1, $yyyy1-1900) if($tsbegin);
  1766. my ($timeolderthan,$timedifftonow) = DbRep_normRelTime($hash);
  1767. if($timedifftonow) {
  1768. $epoch_seconds_begin = time() - $timedifftonow;
  1769. Log3 ($name, 4, "DbRep $name - Time difference to current time for calculating Timestamp begin: $timedifftonow sec");
  1770. } elsif ($timeolderthan) {
  1771. my $mints = $hash->{HELPER}{MINTS}?$hash->{HELPER}{MINTS}:"1970-01-01 01:00:00"; # Timestamp des 1. Datensatzes verwenden falls ermittelt
  1772. $mints =~ /^(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+)$/;
  1773. $epoch_seconds_begin = timelocal($6, $5, $4, $3, $2-1, $1-1900);
  1774. }
  1775. my $tsbegin_string = strftime "%Y-%m-%d %H:%M:%S", localtime($epoch_seconds_begin);
  1776. Log3 ($name, 5, "DbRep $name - Timestamp begin epocheseconds: $epoch_seconds_begin") if($opt !~ /tableCurrentPurge/);
  1777. Log3 ($name, 4, "DbRep $name - Timestamp begin human readable: $tsbegin_string") if($opt !~ /tableCurrentPurge/);
  1778. # relative Auswertungszeit Ende berücksichtigen # Umwandeln in Epochesekunden Endezeit
  1779. my $epoch_seconds_end = timelocal($sec2, $min2, $hh2, $dd2, $mm2-1, $yyyy2-1900);
  1780. $epoch_seconds_end = $timeolderthan ? (time() - $timeolderthan) : $epoch_seconds_end;
  1781. #$epoch_seconds_end = AttrVal($name, "timeOlderThan", undef) ?
  1782. # (time() - AttrVal($name, "timeOlderThan", undef)) : $epoch_seconds_end;
  1783. Log3 ($name, 4, "DbRep $name - Time difference to current time for calculating Timestamp end: $timeolderthan sec") if(AttrVal($name, "timeOlderThan", undef));
  1784. my $tsend_string = strftime "%Y-%m-%d %H:%M:%S", localtime($epoch_seconds_end);
  1785. Log3 ($name, 5, "DbRep $name - Timestamp end epocheseconds: $epoch_seconds_end") if($opt !~ /tableCurrentPurge/);
  1786. Log3 ($name, 4, "DbRep $name - Timestamp end human readable: $tsend_string") if($opt !~ /tableCurrentPurge/);
  1787. # Erstellung Wertehash für Aggregationen
  1788. my $runtime = $epoch_seconds_begin; # Schleifenlaufzeit auf Beginn der Zeitselektion setzen
  1789. my $runtime_string; # Datum/Zeit im SQL-Format für Readingname Teilstring
  1790. my $runtime_string_first; # Datum/Zeit Auswertungsbeginn im SQL-Format für SQL-Statement
  1791. my $runtime_string_next; # Datum/Zeit + Periode (Granularität) für Auswertungsende im SQL-Format
  1792. my $reading_runtime_string; # zusammengesetzter Readingname+Aggregation für Update
  1793. my $tsstr = strftime "%H:%M:%S", localtime($runtime); # für Berechnung Tagesverschieber / Stundenverschieber
  1794. my $testr = strftime "%H:%M:%S", localtime($epoch_seconds_end); # für Berechnung Tagesverschieber / Stundenverschieber
  1795. my $dsstr = strftime "%Y-%m-%d", localtime($runtime); # für Berechnung Tagesverschieber / Stundenverschieber
  1796. my $destr = strftime "%Y-%m-%d", localtime($epoch_seconds_end); # für Berechnung Tagesverschieber / Stundenverschieber
  1797. my $msstr = strftime "%m", localtime($runtime); # Startmonat für Berechnung Monatsverschieber
  1798. my $mestr = strftime "%m", localtime($epoch_seconds_end); # Endemonat für Berechnung Monatsverschieber
  1799. my $ysstr = strftime "%Y", localtime($runtime); # Startjahr für Berechnung Monatsverschieber
  1800. my $yestr = strftime "%Y", localtime($epoch_seconds_end); # Endejahr für Berechnung Monatsverschieber
  1801. my $wd = strftime "%a", localtime($runtime); # Wochentag des aktuellen Startdatum/Zeit
  1802. my $wdadd = 604800 if($wd eq "Mo"); # wenn Start am "Mo" dann nächste Grenze +7 Tage
  1803. $wdadd = 518400 if($wd eq "Di"); # wenn Start am "Di" dann nächste Grenze +6 Tage
  1804. $wdadd = 432000 if($wd eq "Mi"); # wenn Start am "Mi" dann nächste Grenze +5 Tage
  1805. $wdadd = 345600 if($wd eq "Do"); # wenn Start am "Do" dann nächste Grenze +4 Tage
  1806. $wdadd = 259200 if($wd eq "Fr"); # wenn Start am "Fr" dann nächste Grenze +3 Tage
  1807. $wdadd = 172800 if($wd eq "Sa"); # wenn Start am "Sa" dann nächste Grenze +2 Tage
  1808. $wdadd = 86400 if($wd eq "So"); # wenn Start am "So" dann nächste Grenze +1 Tage
  1809. Log3 ($name, 5, "DbRep $name - weekday of start for selection: $wd -> wdadd: $wdadd") if($wdadd);
  1810. my $aggsec;
  1811. if ($aggregation eq "hour") {
  1812. $aggsec = 3600;
  1813. } elsif ($aggregation eq "day") {
  1814. $aggsec = 86400;
  1815. } elsif ($aggregation eq "week") {
  1816. $aggsec = 604800;
  1817. } elsif ($aggregation eq "month") {
  1818. $aggsec = 2678400; # Initialwert, wird in DbRep_collaggstr für jeden Monat berechnet
  1819. } elsif ($aggregation eq "no") {
  1820. $aggsec = 1;
  1821. } else {
  1822. return;
  1823. }
  1824. my %cv = (
  1825. tsstr => $tsstr,
  1826. testr => $testr,
  1827. dsstr => $dsstr,
  1828. destr => $destr,
  1829. msstr => $msstr,
  1830. mestr => $mestr,
  1831. ysstr => $ysstr,
  1832. yestr => $yestr,
  1833. aggsec => $aggsec,
  1834. aggregation => $aggregation,
  1835. epoch_seconds_end => $epoch_seconds_end,
  1836. wdadd => $wdadd
  1837. );
  1838. $hash->{HELPER}{CV} = \%cv;
  1839. my $ts; # für Erstellung Timestamp-Array zur nonblocking SQL-Abarbeitung
  1840. my $i = 1; # Schleifenzähler -> nur Indikator für ersten Durchlauf -> anderer $runtime_string_first
  1841. my $ll; # loopindikator, wenn 1 = loopausstieg
  1842. # Aufbau Timestampstring mit Zeitgrenzen entsprechend Aggregation
  1843. while (!$ll) {
  1844. # collect aggregation strings
  1845. ($runtime,$runtime_string,$runtime_string_first,$runtime_string_next,$ll) = DbRep_collaggstr($hash,$runtime,$i,$runtime_string_next);
  1846. $ts .= $runtime_string."#".$runtime_string_first."#".$runtime_string_next."|";
  1847. $i++;
  1848. }
  1849. return ($epoch_seconds_begin,$epoch_seconds_end,$runtime_string_first,$runtime_string_next,$ts);
  1850. }
  1851. ####################################################################################################
  1852. # Zusammenstellung Aggregationszeiträume
  1853. ####################################################################################################
  1854. sub DbRep_collaggstr($$$$) {
  1855. my ($hash,$runtime,$i,$runtime_string_next) = @_;
  1856. my $name = $hash->{NAME};
  1857. my $runtime_string; # Datum/Zeit im SQL-Format für Readingname Teilstring
  1858. my $runtime_string_first; # Datum/Zeit Auswertungsbeginn im SQL-Format für SQL-Statement
  1859. my $ll; # loopindikator, wenn 1 = loopausstieg
  1860. my $runtime_orig; # orig. runtime als Grundlage für Addition mit $aggsec
  1861. my $tsstr = $hash->{HELPER}{CV}{tsstr}; # für Berechnung Tagesverschieber / Stundenverschieber
  1862. my $testr = $hash->{HELPER}{CV}{testr}; # für Berechnung Tagesverschieber / Stundenverschieber
  1863. my $dsstr = $hash->{HELPER}{CV}{dsstr}; # für Berechnung Tagesverschieber / Stundenverschieber
  1864. my $destr = $hash->{HELPER}{CV}{destr}; # für Berechnung Tagesverschieber / Stundenverschieber
  1865. my $msstr = $hash->{HELPER}{CV}{msstr}; # Startmonat für Berechnung Monatsverschieber
  1866. my $mestr = $hash->{HELPER}{CV}{mestr}; # Endemonat für Berechnung Monatsverschieber
  1867. my $ysstr = $hash->{HELPER}{CV}{ysstr}; # Startjahr für Berechnung Monatsverschieber
  1868. my $yestr = $hash->{HELPER}{CV}{yestr}; # Endejahr für Berechnung Monatsverschieber
  1869. my $aggregation = $hash->{HELPER}{CV}{aggregation}; # Aggregation
  1870. my $aggsec = $hash->{HELPER}{CV}{aggsec}; # laufende Aggregationssekunden
  1871. my $epoch_seconds_end = $hash->{HELPER}{CV}{epoch_seconds_end};
  1872. my $wdadd = $hash->{HELPER}{CV}{wdadd}; # Ergänzungstage. Starttag + Ergänzungstage = der folgende Montag (für week-Aggregation)
  1873. # only for this block because of warnings if some values not set
  1874. no warnings 'uninitialized';
  1875. # keine Aggregation (all between timestamps)
  1876. if ($aggregation eq "no") {
  1877. $runtime_string = "no_aggregation"; # für Readingname
  1878. $runtime_string_first = strftime "%Y-%m-%d %H:%M:%S", localtime($runtime);
  1879. $runtime_string_next = strftime "%Y-%m-%d %H:%M:%S", localtime($epoch_seconds_end);
  1880. $ll = 1;
  1881. }
  1882. # Monatsaggregation
  1883. if ($aggregation eq "month") {
  1884. $runtime_orig = $runtime;
  1885. # Hilfsrechnungen
  1886. my $rm = strftime "%m", localtime($runtime); # Monat des aktuell laufenden Startdatums d. SQL-Select
  1887. my $ry = strftime "%Y", localtime($runtime); # Jahr des aktuell laufenden Startdatums d. SQL-Select
  1888. my $dim = $rm-2?30+($rm*3%7<4):28+!($ry%4||$ry%400*!($ry%100)); # Anzahl Tage des aktuell laufenden Monats
  1889. Log3 ($name, 5, "DbRep $name - act year: $ry, act month: $rm, days in month: $dim, endyear: $yestr, endmonth: $mestr");
  1890. $aggsec = $dim * 86400;
  1891. $runtime = $runtime+3600 if(DbRep_dsttest($hash,$runtime,$aggsec) && (strftime "%m", localtime($runtime)) > 6); # Korrektur Winterzeitumstellung (Uhr wurde 1 Stunde zurück gestellt)
  1892. $runtime_string = strftime "%Y-%m", localtime($runtime); # für Readingname
  1893. if ($i==1) {
  1894. # nur im ersten Durchlauf
  1895. $runtime_string_first = strftime "%Y-%m-%d %H:%M:%S", localtime($runtime_orig);
  1896. }
  1897. if ($ysstr == $yestr && $msstr == $mestr || $ry == $yestr && $rm == $mestr) {
  1898. $runtime_string_first = strftime "%Y-%m-01", localtime($runtime) if($i>1);
  1899. $runtime_string_next = strftime "%Y-%m-%d %H:%M:%S", localtime($epoch_seconds_end);
  1900. $ll=1;
  1901. } else {
  1902. if(($runtime) > $epoch_seconds_end) {
  1903. #$runtime_string_first = strftime "%Y-%m-01", localtime($runtime) if($i>11); # ausgebaut 24.02.2018
  1904. $runtime_string_first = strftime "%Y-%m-01", localtime($runtime);
  1905. $runtime_string_next = strftime "%Y-%m-%d %H:%M:%S", localtime($epoch_seconds_end);
  1906. $ll=1;
  1907. } else {
  1908. $runtime_string_first = strftime "%Y-%m-01", localtime($runtime) if($i>1);
  1909. $runtime_string_next = strftime "%Y-%m-01", localtime($runtime+($dim*86400));
  1910. }
  1911. }
  1912. my ($yyyy1, $mm1, $dd1) = ($runtime_string_next =~ /(\d+)-(\d+)-(\d+)/);
  1913. $runtime = timelocal("00", "00", "00", "01", $mm1-1, $yyyy1-1900);
  1914. # neue Beginnzeit in Epoche-Sekunden
  1915. $runtime = $runtime_orig+$aggsec;
  1916. }
  1917. # Wochenaggregation
  1918. if ($aggregation eq "week") {
  1919. $runtime = $runtime+3600 if($i!=1 && DbRep_dsttest($hash,$runtime,$aggsec) && (strftime "%m", localtime($runtime)) > 6); # Korrektur Winterzeitumstellung (Uhr wurde 1 Stunde zurück gestellt)
  1920. $runtime_orig = $runtime;
  1921. my $w = strftime "%V", localtime($runtime); # Wochennummer des aktuellen Startdatum/Zeit
  1922. $runtime_string = "week_".$w; # für Readingname
  1923. my $ms = strftime "%m", localtime($runtime); # Startmonat (01-12)
  1924. my $me = strftime "%m", localtime($epoch_seconds_end); # Endemonat (01-12)
  1925. if ($i==1) {
  1926. # nur im ersten Schleifendurchlauf
  1927. $runtime_string_first = strftime "%Y-%m-%d %H:%M:%S", localtime($runtime);
  1928. # Korrektur $runtime_orig für Berechnung neue Beginnzeit für nächsten Durchlauf
  1929. my ($yyyy1, $mm1, $dd1) = ($runtime_string_first =~ /(\d+)-(\d+)-(\d+)/);
  1930. $runtime = timelocal("00", "00", "00", $dd1, $mm1-1, $yyyy1-1900);
  1931. $runtime = $runtime+3600 if(DbRep_dsttest($hash,$runtime,$aggsec) && (strftime "%m", localtime($runtime)) > 6); # Korrektur Winterzeitumstellung (Uhr wurde 1 Stunde zurück gestellt)
  1932. $runtime = $runtime+$wdadd;
  1933. $runtime_orig = $runtime-$aggsec;
  1934. # die Woche Beginn ist gleich der Woche vom Ende Auswertung
  1935. if((strftime "%V", localtime($epoch_seconds_end)) eq ($w) && ($ms+$me != 13)) {
  1936. $runtime_string_next = strftime "%Y-%m-%d %H:%M:%S", localtime($epoch_seconds_end);
  1937. $ll=1;
  1938. } else {
  1939. $runtime_string_next = strftime "%Y-%m-%d", localtime($runtime);
  1940. }
  1941. } else {
  1942. # weitere Durchläufe
  1943. if(($runtime+$aggsec) > $epoch_seconds_end) {
  1944. $runtime_string_first = strftime "%Y-%m-%d", localtime($runtime_orig);
  1945. $runtime_string_next = strftime "%Y-%m-%d %H:%M:%S", localtime($epoch_seconds_end);
  1946. $ll=1;
  1947. } else {
  1948. $runtime_string_first = strftime "%Y-%m-%d", localtime($runtime_orig) ;
  1949. $runtime_string_next = strftime "%Y-%m-%d", localtime($runtime+$aggsec);
  1950. }
  1951. }
  1952. # neue Beginnzeit in Epoche-Sekunden
  1953. $runtime = $runtime_orig+$aggsec;
  1954. }
  1955. # Tagesaggregation
  1956. if ($aggregation eq "day") {
  1957. $runtime_string = strftime "%Y-%m-%d", localtime($runtime); # für Readingname
  1958. $runtime_string_first = strftime "%Y-%m-%d %H:%M:%S", localtime($runtime) if($i==1);
  1959. $runtime_string_first = strftime "%Y-%m-%d", localtime($runtime) if($i>1);
  1960. $runtime = $runtime+3600 if(DbRep_dsttest($hash,$runtime,$aggsec) && (strftime "%m", localtime($runtime)) > 6); # Korrektur Winterzeitumstellung (Uhr wurde 1 Stunde zurück gestellt)
  1961. if((($tsstr gt $testr) ? $runtime : ($runtime+$aggsec)) > $epoch_seconds_end) {
  1962. $runtime_string_first = strftime "%Y-%m-%d", localtime($runtime);
  1963. $runtime_string_first = strftime "%Y-%m-%d %H:%M:%S", localtime($runtime) if( $dsstr eq $destr);
  1964. $runtime_string_next = strftime "%Y-%m-%d %H:%M:%S", localtime($epoch_seconds_end);
  1965. $ll=1;
  1966. } else {
  1967. $runtime_string_next = strftime "%Y-%m-%d", localtime($runtime+$aggsec);
  1968. }
  1969. Log3 ($name, 5, "DbRep $name - runtime_string: $runtime_string, runtime_string_first: $runtime_string_first, runtime_string_next: $runtime_string_next");
  1970. # neue Beginnzeit in Epoche-Sekunden
  1971. $runtime = $runtime+$aggsec;
  1972. }
  1973. # Stundenaggregation
  1974. if ($aggregation eq "hour") {
  1975. $runtime_string = strftime "%Y-%m-%d_%H", localtime($runtime); # für Readingname
  1976. $runtime_string_first = strftime "%Y-%m-%d %H:%M:%S", localtime($runtime) if($i==1);
  1977. $runtime = $runtime+3600 if(DbRep_dsttest($hash,$runtime,$aggsec) && (strftime "%m", localtime($runtime)) > 6); # Korrektur Winterzeitumstellung (Uhr wurde 1 Stunde zurück gestellt)
  1978. $runtime_string_first = strftime "%Y-%m-%d %H", localtime($runtime) if($i>1);
  1979. my @a = split (":",$tsstr);
  1980. my $hs = $a[0];
  1981. my $msstr = $a[1].":".$a[2];
  1982. @a = split (":",$testr);
  1983. my $he = $a[0];
  1984. my $mestr = $a[1].":".$a[2];
  1985. if((($msstr gt $mestr) ? $runtime : ($runtime+$aggsec)) > $epoch_seconds_end) {
  1986. $runtime_string_first = strftime "%Y-%m-%d %H", localtime($runtime);
  1987. $runtime_string_first = strftime "%Y-%m-%d %H:%M:%S", localtime($runtime) if( $dsstr eq $destr && $hs eq $he);
  1988. $runtime_string_next = strftime "%Y-%m-%d %H:%M:%S", localtime($epoch_seconds_end);
  1989. $ll=1;
  1990. } else {
  1991. $runtime_string_next = strftime "%Y-%m-%d %H", localtime($runtime+$aggsec);
  1992. }
  1993. # neue Beginnzeit in Epoche-Sekunden
  1994. $runtime = $runtime+$aggsec;
  1995. }
  1996. return ($runtime,$runtime_string,$runtime_string_first,$runtime_string_next,$ll);
  1997. }
  1998. ####################################################################################################
  1999. # nichtblockierende DB-Abfrage averageValue
  2000. ####################################################################################################
  2001. sub averval_DoParse($) {
  2002. my ($string) = @_;
  2003. my ($name,$device,$reading,$prop,$ts) = split("\\§", $string);
  2004. my $hash = $defs{$name};
  2005. my $dbloghash = $hash->{dbloghash};
  2006. my $dbconn = $dbloghash->{dbconn};
  2007. my $dbuser = $dbloghash->{dbuser};
  2008. my $dblogname = $dbloghash->{NAME};
  2009. my $dbpassword = $attr{"sec$dblogname"}{secret};
  2010. my $acf = AttrVal($name, "averageCalcForm", "avgArithmeticMean"); # Festlegung Berechnungsschema f. Mittelwert
  2011. my $qlf = "avg";
  2012. my ($dbh,$sql,$sth,$err,$selspec,$addon);
  2013. # Background-Startzeit
  2014. my $bst = [gettimeofday];
  2015. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1 });};
  2016. if ($@) {
  2017. $err = encode_base64($@,"");
  2018. Log3 ($name, 2, "DbRep $name - $@");
  2019. return "$name|''|$device|$reading|''|$err|''";
  2020. }
  2021. # only for this block because of warnings if details of readings are not set
  2022. no warnings 'uninitialized';
  2023. # ist Zeiteingrenzung und/oder Aggregation gesetzt ? (wenn ja -> "?" in SQL sonst undef)
  2024. my ($IsTimeSet,$IsAggrSet) = DbRep_checktimeaggr($hash);
  2025. Log3 ($name, 5, "DbRep $name - IsTimeSet: $IsTimeSet, IsAggrSet: $IsAggrSet");
  2026. # Timestampstring to Array
  2027. my @ts = split("\\|", $ts);
  2028. Log3 ($name, 5, "DbRep $name - Timestamp-Array: \n@ts");
  2029. if($acf eq "avgArithmeticMean") {
  2030. # arithmetischer Mittelwert
  2031. # vorbereiten der DB-Abfrage, DB-Modell-abhaengig
  2032. $addon = '';
  2033. if ($dbloghash->{MODEL} eq "POSTGRESQL") {
  2034. $selspec = "AVG(VALUE::numeric)";
  2035. } elsif ($dbloghash->{MODEL} eq "MYSQL") {
  2036. $selspec = "AVG(VALUE)";
  2037. } elsif ($dbloghash->{MODEL} eq "SQLITE") {
  2038. $selspec = "AVG(VALUE)";
  2039. } else {
  2040. $selspec = "AVG(VALUE)";
  2041. }
  2042. $qlf = "avgam";
  2043. } elsif ($acf eq "avgDailyMeanGWS") {
  2044. # Tagesmittelwert Temperaturen nach Schema des deutschen Wetterdienstes
  2045. # SELECT VALUE FROM history WHERE DEVICE="MyWetter" AND READING="temperature" AND TIMESTAMP >= "2018-01-28 $i:00:00" AND TIMESTAMP <= "2018-01-28 ($i+1):00:00" ORDER BY TIMESTAMP DESC LIMIT 1;
  2046. $addon = "ORDER BY TIMESTAMP DESC LIMIT 1";
  2047. $selspec = "VALUE";
  2048. $qlf = "avgdmgws";
  2049. } elsif ($acf eq "avgTimeWeightMean") {
  2050. $addon = "ORDER BY TIMESTAMP ASC";
  2051. $selspec = "TIMESTAMP,VALUE";
  2052. $qlf = "avgtwm";
  2053. }
  2054. # SQL-Startzeit
  2055. my $st = [gettimeofday];
  2056. # DB-Abfrage zeilenweise für jeden Array-Eintrag
  2057. my $arrstr;
  2058. foreach my $row (@ts) {
  2059. my @a = split("#", $row);
  2060. my $runtime_string = $a[0];
  2061. my $runtime_string_first = $a[1];
  2062. my $runtime_string_next = $a[2];
  2063. if($acf eq "avgArithmeticMean") {
  2064. # arithmetischer Mittelwert (Standard)
  2065. #
  2066. if ($IsTimeSet || $IsAggrSet) {
  2067. $sql = DbRep_createSelectSql($hash,"history",$selspec,$device,$reading,"'$runtime_string_first'","'$runtime_string_next'",$addon);
  2068. } else {
  2069. $sql = DbRep_createSelectSql($hash,"history",$selspec,$device,$reading,undef,undef,$addon);
  2070. }
  2071. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  2072. eval{ $sth = $dbh->prepare($sql);
  2073. $sth->execute();
  2074. };
  2075. if ($@) {
  2076. $err = encode_base64($@,"");
  2077. Log3 ($name, 2, "DbRep $name - $@");
  2078. $dbh->disconnect;
  2079. return "$name|''|$device|$reading|''|$err|''";
  2080. }
  2081. my @line = $sth->fetchrow_array();
  2082. Log3 ($name, 5, "DbRep $name - SQL result: $line[0]") if($line[0]);
  2083. if(AttrVal($name, "aggregation", "") eq "hour") {
  2084. my @rsf = split(/[" "\|":"]/,$runtime_string_first);
  2085. $arrstr .= $runtime_string."#".$line[0]."#".$rsf[0]."_".$rsf[1]."|";
  2086. } else {
  2087. my @rsf = split(" ",$runtime_string_first);
  2088. $arrstr .= $runtime_string."#".$line[0]."#".$rsf[0]."|";
  2089. }
  2090. } elsif ($acf eq "avgDailyMeanGWS") {
  2091. # Berechnung des Tagesmittelwertes (Temperatur) nach der Vorschrift des deutschen Wetterdienstes
  2092. # Berechnung der Tagesmittel aus 24 Stundenwerten, Bezugszeit für einen Tag i.d.R. 23:51 UTC des
  2093. # Vortages bis 23:50 UTC, d.h. 00:51 bis 23:50 MEZ
  2094. # Wenn mehr als 3 Stundenwerte fehlen -> Berechnung aus den 4 Hauptterminen (00, 06, 12, 18 UTC),
  2095. # d.h. 01, 07, 13, 19 MEZ
  2096. # https://www.dwd.de/DE/leistungen/klimadatendeutschland/beschreibung_tagesmonatswerte.html
  2097. #
  2098. my $sum = 0;
  2099. my $anz = 0; # Anzahl der Messwerte am Tag
  2100. my($t01,$t07,$t13,$t19); # Temperaturen der Haupttermine
  2101. my ($bdate,undef) = split(" ",$runtime_string_first);
  2102. for my $i (0..23) {
  2103. my $bsel = $bdate." ".sprintf("%02d",$i).":00:00";
  2104. my $esel = ($i<23)?$bdate." ".sprintf("%02d",$i).":59:59":$runtime_string_next;
  2105. $sql = DbRep_createSelectSql($hash,"history",$selspec,$device,$reading,"'$bsel'","'$esel'",$addon);
  2106. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  2107. eval{ $sth = $dbh->prepare($sql);
  2108. $sth->execute();
  2109. };
  2110. if ($@) {
  2111. $err = encode_base64($@,"");
  2112. Log3 ($name, 2, "DbRep $name - $@");
  2113. $dbh->disconnect;
  2114. return "$name|''|$device|$reading|''|$err|''";
  2115. }
  2116. my $val = $sth->fetchrow_array();
  2117. Log3 ($name, 5, "DbRep $name - SQL result: $val") if($val);
  2118. $val = DbRep_numval ($val); # nichtnumerische Zeichen eliminieren
  2119. if(defined($val) && looks_like_number($val)) {
  2120. $sum += $val;
  2121. $t01 = $val if($val && $i == 00); # Wert f. Stunde 01 ist zw. letzter Wert vor 01
  2122. $t07 = $val if($val && $i == 06);
  2123. $t13 = $val if($val && $i == 12);
  2124. $t19 = $val if($val && $i == 18);
  2125. $anz++;
  2126. }
  2127. }
  2128. if($anz >= 21) {
  2129. $sum = $sum/24;
  2130. } elsif ($anz >= 4 && $t01 && $t07 && $t13 && $t19) {
  2131. $sum = ($t01+$t07+$t13+$t19)/4;
  2132. } else {
  2133. $sum = "insufficient values";
  2134. }
  2135. if(AttrVal($name, "aggregation", "") eq "hour") {
  2136. my @rsf = split(/[" "\|":"]/,$runtime_string_first);
  2137. $arrstr .= $runtime_string."#".$sum."#".$rsf[0]."_".$rsf[1]."|";
  2138. } else {
  2139. my @rsf = split(" ",$runtime_string_first);
  2140. $arrstr .= $runtime_string."#".$sum."#".$rsf[0]."|";
  2141. }
  2142. } elsif ($acf eq "avgTimeWeightMean") {
  2143. # zeitgewichteten Mittelwert berechnen
  2144. # http://massmatics.de/merkzettel/#!837:Gewichteter_Mittelwert
  2145. #
  2146. # $tsum = timestamp letzter Messpunkt - timestamp erster Messpunkt
  2147. # $t1 = timestamp wert1
  2148. # $t2 = timestamp wert2
  2149. # $dt = $t2 - $t1
  2150. # $t1 = $t2
  2151. # .....
  2152. # (val1*$dt/$tsum) + (val2*$dt/$tsum) + .... + (valn*$dt/$tsum)
  2153. #
  2154. # gesamte Zeitspanne $tsum zwischen ersten und letzten Datensatz der Zeitscheibe ermitteln
  2155. my ($tsum,$tf,$tl,$tn,$to,$dt,$val,$val1);
  2156. my $sum = 0;
  2157. my $addonf = 'ORDER BY TIMESTAMP ASC LIMIT 1';
  2158. my $addonl = 'ORDER BY TIMESTAMP DESC LIMIT 1';
  2159. my $sqlf = DbRep_createSelectSql($hash,"history","TIMESTAMP",$device,$reading,"'$runtime_string_first'","'$runtime_string_next'",$addonf);
  2160. my $sqll = DbRep_createSelectSql($hash,"history","TIMESTAMP",$device,$reading,"'$runtime_string_first'","'$runtime_string_next'",$addonl);
  2161. eval { $tf = ($dbh->selectrow_array($sqlf))[0];
  2162. $tl = ($dbh->selectrow_array($sqll))[0];
  2163. };
  2164. if ($@) {
  2165. $err = encode_base64($@,"");
  2166. Log3 ($name, 2, "DbRep $name - $@");
  2167. $dbh->disconnect;
  2168. return "$name|''|$device|$reading|''|$err|''";
  2169. }
  2170. if(!$tf || !$tl) {
  2171. # kein Start- und/oder Ende Timestamp in Zeitscheibe vorhanden -> keine Werteberechnung möglich
  2172. $sum = "insufficient values";
  2173. } else {
  2174. my ($yyyyf, $mmf, $ddf, $hhf, $minf, $secf) = ($tf =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/);
  2175. my ($yyyyl, $mml, $ddl, $hhl, $minl, $secl) = ($tl =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/);
  2176. $tsum = (timelocal($secl, $minl, $hhl, $ddl, $mml-1, $yyyyl-1900))-(timelocal($secf, $minf, $hhf, $ddf, $mmf-1, $yyyyf-1900));
  2177. if ($IsTimeSet || $IsAggrSet) {
  2178. $sql = DbRep_createSelectSql($hash,"history",$selspec,$device,$reading,"'$runtime_string_first'","'$runtime_string_next'",$addon);
  2179. } else {
  2180. $sql = DbRep_createSelectSql($hash,"history",$selspec,$device,$reading,undef,undef,$addon);
  2181. }
  2182. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  2183. eval{ $sth = $dbh->prepare($sql);
  2184. $sth->execute();
  2185. };
  2186. if ($@) {
  2187. $err = encode_base64($@,"");
  2188. Log3 ($name, 2, "DbRep $name - $@");
  2189. $dbh->disconnect;
  2190. return "$name|''|$device|$reading|''|$err|''";
  2191. }
  2192. my @twm_array = map { $_->[0]."_ESC_".$_->[1] } @{$sth->fetchall_arrayref()};
  2193. foreach my $twmrow (@twm_array) {
  2194. ($tn,$val) = split("_ESC_",$twmrow);
  2195. $val = DbRep_numval ($val); # nichtnumerische Zeichen eliminieren
  2196. my ($yyyyt1, $mmt1, $ddt1, $hht1, $mint1, $sect1) = ($tn =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/);
  2197. $tn = timelocal($sect1, $mint1, $hht1, $ddt1, $mmt1-1, $yyyyt1-1900);
  2198. if(!$to) {
  2199. $val1 = $val;
  2200. $to = $tn;
  2201. next;
  2202. }
  2203. $dt = $tn - $to;
  2204. $sum += $val1*($dt/$tsum);
  2205. $val1 = $val;
  2206. $to = $tn;
  2207. Log3 ($name, 5, "DbRep $name - data element: $twmrow");
  2208. Log3 ($name, 5, "DbRep $name - time sum: $tsum, delta time: $dt, value: $val1, twm: ".$val1*($dt/$tsum));
  2209. }
  2210. }
  2211. if(AttrVal($name, "aggregation", "") eq "hour") {
  2212. my @rsf = split(/[" "\|":"]/,$runtime_string_first);
  2213. $arrstr .= $runtime_string."#".$sum."#".$rsf[0]."_".$rsf[1]."|";
  2214. } else {
  2215. my @rsf = split(" ",$runtime_string_first);
  2216. $arrstr .= $runtime_string."#".$sum."#".$rsf[0]."|";
  2217. }
  2218. }
  2219. }
  2220. $sth->finish;
  2221. $dbh->disconnect;
  2222. # SQL-Laufzeit ermitteln
  2223. my $rt = tv_interval($st);
  2224. # Ergebnisse in Datenbank schreiben
  2225. my ($wrt,$irowdone);
  2226. if($prop =~ /writeToDB/) {
  2227. ($wrt,$irowdone,$err) = DbRep_OutputWriteToDB($name,$device,$reading,$arrstr,$qlf);
  2228. if ($err) {
  2229. Log3 $hash->{NAME}, 2, "DbRep $name - $err";
  2230. $err = encode_base64($err,"");
  2231. return "$name|''|$device|$reading|''|$err|''";
  2232. }
  2233. $rt = $rt+$wrt;
  2234. }
  2235. # Daten müssen als Einzeiler zurückgegeben werden
  2236. $arrstr = encode_base64($arrstr,"");
  2237. # Background-Laufzeit ermitteln
  2238. my $brt = tv_interval($bst);
  2239. $rt = $rt.",".$brt;
  2240. return "$name|$arrstr|$device|$reading|$rt|0|$irowdone";
  2241. }
  2242. ####################################################################################################
  2243. # Auswertungsroutine der nichtblockierenden DB-Abfrage averageValue
  2244. ####################################################################################################
  2245. sub averval_ParseDone($) {
  2246. my ($string) = @_;
  2247. my @a = split("\\|",$string);
  2248. my $hash = $defs{$a[0]};
  2249. my $name = $hash->{NAME};
  2250. my $arrstr = decode_base64($a[1]);
  2251. my $device = $a[2];
  2252. $device =~ s/[^A-Za-z\/\d_\.-]/\//g;
  2253. my $reading = $a[3];
  2254. $reading =~ s/[^A-Za-z\/\d_\.-]/\//g;
  2255. my $bt = $a[4];
  2256. my ($rt,$brt) = split(",", $bt);
  2257. my $err = $a[5]?decode_base64($a[5]):undef;
  2258. my $irowdone = $a[6];
  2259. my $reading_runtime_string;
  2260. if ($err) {
  2261. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  2262. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  2263. delete($hash->{HELPER}{RUNNING_PID});
  2264. return;
  2265. }
  2266. # only for this block because of warnings if details of readings are not set
  2267. no warnings 'uninitialized';
  2268. my $acf = AttrVal($name, "averageCalcForm", "avgArithmeticMean");
  2269. if($acf eq "avgArithmeticMean") {
  2270. $acf = "AM"
  2271. } elsif ($acf eq "avgDailyMeanGWS") {
  2272. $acf = "DMGWS";
  2273. } elsif ($acf eq "avgTimeWeightMean") {
  2274. $acf = "TWM";
  2275. }
  2276. # Readingaufbereitung
  2277. readingsBeginUpdate($hash);
  2278. my @arr = split("\\|", $arrstr);
  2279. foreach my $row (@arr) {
  2280. my @a = split("#", $row);
  2281. my $runtime_string = $a[0];
  2282. my $c = $a[1];
  2283. my $rsf = $a[2]."__";
  2284. if (AttrVal($hash->{NAME}, "readingNameMap", "")) {
  2285. $reading_runtime_string = $rsf.AttrVal($hash->{NAME}, "readingNameMap", "")."__".$runtime_string;
  2286. } else {
  2287. my $ds = $device."__" if ($device);
  2288. my $rds = $reading."__" if ($reading);
  2289. $reading_runtime_string = $rsf.$ds.$rds."AVG".$acf."__".$runtime_string;
  2290. }
  2291. if($acf eq "DMGWS") {
  2292. ReadingsBulkUpdateValue ($hash, $reading_runtime_string, looks_like_number($c)?sprintf("%.1f",$c):$c);
  2293. } else {
  2294. ReadingsBulkUpdateValue ($hash, $reading_runtime_string, $c?sprintf("%.4f",$c):"-");
  2295. }
  2296. }
  2297. ReadingsBulkUpdateValue ($hash, "db_lines_processed", $irowdone) if($hash->{LASTCMD} =~ /writeToDB/);
  2298. ReadingsBulkUpdateTimeState($hash,$brt,$rt,"done");
  2299. readingsEndUpdate($hash, 1);
  2300. delete($hash->{HELPER}{RUNNING_PID});
  2301. return;
  2302. }
  2303. ####################################################################################################
  2304. # nichtblockierende DB-Abfrage count
  2305. ####################################################################################################
  2306. sub count_DoParse($) {
  2307. my ($string) = @_;
  2308. my ($name,$table,$device,$reading,$ts) = split("\\§", $string);
  2309. my $hash = $defs{$name};
  2310. my $dbloghash = $hash->{dbloghash};
  2311. my $dbconn = $dbloghash->{dbconn};
  2312. my $dbuser = $dbloghash->{dbuser};
  2313. my $dblogname = $dbloghash->{NAME};
  2314. my $dbpassword = $attr{"sec$dblogname"}{secret};
  2315. my $ced = AttrVal($name,"countEntriesDetail",0);
  2316. my $vf = AttrVal($name,"valueFilter","");
  2317. my ($dbh,$sql,$sth,$err);
  2318. # Background-Startzeit
  2319. my $bst = [gettimeofday];
  2320. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1 });};
  2321. if ($@) {
  2322. $err = encode_base64($@,"");
  2323. Log3 ($name, 2, "DbRep $name - $@");
  2324. return "$name|''|$device|$reading|''|$err|$table";
  2325. }
  2326. # only for this block because of warnings if details of readings are not set
  2327. no warnings 'uninitialized';
  2328. # ist Zeiteingrenzung und/oder Aggregation gesetzt ? (wenn ja -> "?" in SQL sonst undef)
  2329. my ($IsTimeSet,$IsAggrSet,$aggregation) = DbRep_checktimeaggr($hash);
  2330. Log3 ($name, 5, "DbRep $name - IsTimeSet: $IsTimeSet, IsAggrSet: $IsAggrSet");
  2331. # Timestampstring to Array
  2332. my @ts = split("\\|", $ts);
  2333. Log3 ($name, 5, "DbRep $name - Timestamp-Array: \n@ts");
  2334. # SQL-Startzeit
  2335. my $st = [gettimeofday];
  2336. # DB-Abfrage zeilenweise für jeden Timearray-Eintrag
  2337. my ($arrstr,@rsf,$ttail);
  2338. my $addon = '';
  2339. my $selspec = "COUNT(*)";
  2340. if($ced) {
  2341. $addon = "group by READING";
  2342. $selspec = "READING, COUNT(*)";
  2343. }
  2344. foreach my $row (@ts) {
  2345. my @a = split("#", $row);
  2346. my $runtime_string = $a[0];
  2347. my $runtime_string_first = $a[1];
  2348. my $runtime_string_next = $a[2];
  2349. my $tc = 0;
  2350. if($aggregation eq "hour") {
  2351. @rsf = split(/[" "\|":"]/,$runtime_string_first);
  2352. $ttail = $rsf[0]."_".$rsf[1]."|";
  2353. } else {
  2354. @rsf = split(" ",$runtime_string_first);
  2355. $ttail = $rsf[0]."|";
  2356. }
  2357. if ($IsTimeSet || $IsAggrSet) {
  2358. $sql = DbRep_createSelectSql($hash,$table,$selspec,$device,$reading,"'$runtime_string_first'","'$runtime_string_next'",$addon);
  2359. } else {
  2360. $sql = DbRep_createSelectSql($hash,$table,$selspec,$device,$reading,undef,undef,$addon);
  2361. }
  2362. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  2363. eval{ $sth = $dbh->prepare($sql);
  2364. $sth->execute();
  2365. };
  2366. if ($@) {
  2367. $err = encode_base64($@,"");
  2368. Log3 ($name, 2, "DbRep $name - $@");
  2369. $dbh->disconnect;
  2370. return "$name|''|$device|$reading|''|$err|$table";
  2371. }
  2372. if($ced) {
  2373. # detaillierter Readings-Count
  2374. while (my @line = $sth->fetchrow_array()) {
  2375. Log3 ($name, 5, "DbRep $name - SQL result: @line");
  2376. $tc += $line[1] if($line[1]); # total count für Reading
  2377. $arrstr .= $runtime_string."#".$line[0]."#".$line[1]."#".$ttail;
  2378. }
  2379. # total count (über alle selected Readings) für Zeitabschnitt einfügen
  2380. $arrstr .= $runtime_string."#"."ALLREADINGS"."#".$tc."#".$ttail;
  2381. } else {
  2382. my @line = $sth->fetchrow_array();
  2383. Log3 ($name, 5, "DbRep $name - SQL result: $line[0]") if($line[0]);
  2384. $arrstr .= $runtime_string."#"."ALLREADINGS"."#".$line[0]."#".$ttail;
  2385. }
  2386. }
  2387. $sth->finish;
  2388. $dbh->disconnect;
  2389. # SQL-Laufzeit ermitteln
  2390. my $rt = tv_interval($st);
  2391. # Daten müssen als Einzeiler zurückgegeben werden
  2392. $arrstr = encode_base64($arrstr,"");
  2393. # Background-Laufzeit ermitteln
  2394. my $brt = tv_interval($bst);
  2395. $rt = $rt.",".$brt;
  2396. return "$name|$arrstr|$device|$rt|0|$table";
  2397. }
  2398. ####################################################################################################
  2399. # Auswertungsroutine der nichtblockierenden DB-Abfrage count
  2400. ####################################################################################################
  2401. sub count_ParseDone($) {
  2402. my ($string) = @_;
  2403. my @a = split("\\|",$string);
  2404. my $hash = $defs{$a[0]};
  2405. my $name = $hash->{NAME};
  2406. my $arrstr = decode_base64($a[1]);
  2407. my $device = $a[2];
  2408. $device =~ s/[^A-Za-z\/\d_\.-]/\//g;
  2409. my $bt = $a[3];
  2410. my ($rt,$brt) = split(",", $bt);
  2411. my $err = $a[4]?decode_base64($a[4]):undef;
  2412. my $table = $a[5];
  2413. my $reading_runtime_string;
  2414. if ($err) {
  2415. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  2416. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  2417. delete($hash->{HELPER}{RUNNING_PID});
  2418. return;
  2419. }
  2420. Log3 ($name, 5, "DbRep $name - SQL result decoded: $arrstr") if($arrstr);
  2421. # only for this block because of warnings if details of readings are not set
  2422. no warnings 'uninitialized';
  2423. # Readingaufbereitung
  2424. readingsBeginUpdate($hash);
  2425. my @arr = split("\\|", $arrstr);
  2426. foreach my $row (@arr) {
  2427. my @a = split("#", $row);
  2428. my $runtime_string = $a[0];
  2429. my $reading = $a[1];
  2430. $reading =~ s/[^A-Za-z\/\d_\.-]/\//g;
  2431. my $c = $a[2];
  2432. my $rsf = $a[3]."__";
  2433. if (AttrVal($hash->{NAME}, "readingNameMap", "")) {
  2434. $reading_runtime_string = $rsf.AttrVal($hash->{NAME}, "readingNameMap", "")."__".$runtime_string;
  2435. } else {
  2436. my $ds = $device."__" if ($device);
  2437. my $rds = $reading."__" if ($reading);
  2438. $reading_runtime_string = $rsf.$ds.$rds."COUNT_".$table."__".$runtime_string;
  2439. }
  2440. ReadingsBulkUpdateValue ($hash, $reading_runtime_string, $c?$c:"-");
  2441. }
  2442. ReadingsBulkUpdateTimeState($hash,$brt,$rt,"done");
  2443. readingsEndUpdate($hash, 1);
  2444. delete($hash->{HELPER}{RUNNING_PID});
  2445. return;
  2446. }
  2447. ####################################################################################################
  2448. # nichtblockierende DB-Abfrage maxValue
  2449. ####################################################################################################
  2450. sub maxval_DoParse($) {
  2451. my ($string) = @_;
  2452. my ($name,$device,$reading,$prop,$ts) = split("\\§", $string);
  2453. my $hash = $defs{$name};
  2454. my $dbloghash = $hash->{dbloghash};
  2455. my $dbconn = $dbloghash->{dbconn};
  2456. my $dbuser = $dbloghash->{dbuser};
  2457. my $dblogname = $dbloghash->{NAME};
  2458. my $dbpassword = $attr{"sec$dblogname"}{secret};
  2459. my ($dbh,$sql,$sth,$err);
  2460. # Background-Startzeit
  2461. my $bst = [gettimeofday];
  2462. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1 });};
  2463. if ($@) {
  2464. $err = encode_base64($@,"");
  2465. Log3 ($name, 2, "DbRep $name - $@");
  2466. return "$name|''|$device|$reading|''|$err|''";
  2467. }
  2468. # only for this block because of warnings if details of readings are not set
  2469. no warnings 'uninitialized';
  2470. # ist Zeiteingrenzung und/oder Aggregation gesetzt ? (wenn ja -> "?" in SQL sonst undef)
  2471. my ($IsTimeSet,$IsAggrSet) = DbRep_checktimeaggr($hash);
  2472. Log3 ($name, 5, "DbRep $name - IsTimeSet: $IsTimeSet, IsAggrSet: $IsAggrSet");
  2473. # Timestampstring to Array
  2474. my @ts = split("\\|", $ts);
  2475. Log3 ($name, 5, "DbRep $name - Timestamp-Array: \n@ts");
  2476. # SQL-Startzeit
  2477. my $st = [gettimeofday];
  2478. # DB-Abfrage zeilenweise für jeden Array-Eintrag
  2479. my @row_array;
  2480. foreach my $row (@ts) {
  2481. my @a = split("#", $row);
  2482. my $runtime_string = $a[0];
  2483. my $runtime_string_first = $a[1];
  2484. my $runtime_string_next = $a[2];
  2485. $runtime_string = encode_base64($runtime_string,"");
  2486. if ($IsTimeSet || $IsAggrSet) {
  2487. $sql = DbRep_createSelectSql($hash,"history","VALUE,TIMESTAMP",$device,$reading,"'$runtime_string_first'","'$runtime_string_next'","ORDER BY TIMESTAMP");
  2488. } else {
  2489. $sql = DbRep_createSelectSql($hash,"history","VALUE,TIMESTAMP",$device,$reading,undef,undef,"ORDER BY TIMESTAMP");
  2490. }
  2491. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  2492. eval{ $sth = $dbh->prepare($sql);
  2493. $sth->execute();
  2494. };
  2495. if ($@) {
  2496. $err = encode_base64($@,"");
  2497. Log3 ($name, 2, "DbRep $name - $@");
  2498. $dbh->disconnect;
  2499. return "$name|''|$device|$reading|''|$err|''";
  2500. }
  2501. my @array= map { $runtime_string." ".$_ -> [0]." ".$_ -> [1]."\n" } @{ $sth->fetchall_arrayref() };
  2502. if(!@array) {
  2503. if(AttrVal($name, "aggregation", "") eq "hour") {
  2504. my @rsf = split(/[" "\|":"]/,$runtime_string_first);
  2505. @array = ($runtime_string." "."0"." ".$rsf[0]."_".$rsf[1]."\n");
  2506. } else {
  2507. my @rsf = split(" ",$runtime_string_first);
  2508. @array = ($runtime_string." "."0"." ".$rsf[0]."\n");
  2509. }
  2510. }
  2511. push(@row_array, @array);
  2512. }
  2513. $sth->finish;
  2514. $dbh->disconnect;
  2515. # SQL-Laufzeit ermitteln
  2516. my $rt = tv_interval($st);
  2517. Log3 ($name, 5, "DbRep $name -> raw data of row_array result:\n @row_array");
  2518. #---------- Berechnung Ergebnishash maxValue ------------------------
  2519. my $i = 1;
  2520. my %rh = ();
  2521. my ($lastruntimestring,$row_max_time,$max_value);
  2522. foreach my $row (@row_array) {
  2523. my @a = split("[ \t][ \t]*", $row);
  2524. my $runtime_string = decode_base64($a[0]);
  2525. $lastruntimestring = $runtime_string if ($i == 1);
  2526. my $value = $a[1];
  2527. $a[-1] =~ s/:/-/g if($a[-1]); # substituieren unsupported characters -> siehe fhem.pl
  2528. my $timestamp = ($a[-1]&&$a[-2])?$a[-2]."_".$a[-1]:$a[-1];
  2529. # Leerzeichen am Ende $timestamp entfernen
  2530. $timestamp =~ s/\s+$//g;
  2531. # Test auf $value = "numeric"
  2532. if (!looks_like_number($value)) {
  2533. Log3 ($name, 2, "DbRep $name - ERROR - value isn't numeric in maxValue function. Faulty dataset was \nTIMESTAMP: $timestamp, DEVICE: $device, READING: $reading, VALUE: $value.");
  2534. $err = encode_base64("Value isn't numeric. Faulty dataset was - TIMESTAMP: $timestamp, VALUE: $value", "");
  2535. return "$name|''|$device|$reading|''|$err|''";
  2536. }
  2537. Log3 ($name, 5, "DbRep $name - Runtimestring: $runtime_string, DEVICE: $device, READING: $reading, TIMESTAMP: $timestamp, VALUE: $value");
  2538. if ($runtime_string eq $lastruntimestring) {
  2539. if (!defined($max_value) || $value >= $max_value) {
  2540. $max_value = $value;
  2541. $row_max_time = $timestamp;
  2542. $rh{$runtime_string} = $runtime_string."|".$max_value."|".$row_max_time;
  2543. }
  2544. } else {
  2545. # neuer Zeitabschnitt beginnt, ersten Value-Wert erfassen
  2546. $lastruntimestring = $runtime_string;
  2547. undef $max_value;
  2548. if (!defined($max_value) || $value >= $max_value) {
  2549. $max_value = $value;
  2550. $row_max_time = $timestamp;
  2551. $rh{$runtime_string} = $runtime_string."|".$max_value."|".$row_max_time;
  2552. }
  2553. }
  2554. $i++;
  2555. }
  2556. #---------------------------------------------------------------------------------------------
  2557. Log3 ($name, 5, "DbRep $name - result of maxValue calculation before encoding:");
  2558. foreach my $key (sort(keys(%rh))) {
  2559. Log3 ($name, 5, "runtimestring Key: $key, value: ".$rh{$key});
  2560. }
  2561. # Ergebnishash als Einzeiler zurückgeben bzw. Übergabe Schreibroutine
  2562. my $rows = join('§', %rh);
  2563. # Ergebnisse in Datenbank schreiben
  2564. my ($wrt,$irowdone);
  2565. if($prop =~ /writeToDB/) {
  2566. ($wrt,$irowdone,$err) = DbRep_OutputWriteToDB($name,$device,$reading,$rows,"max");
  2567. if ($err) {
  2568. Log3 $hash->{NAME}, 2, "DbRep $name - $err";
  2569. $err = encode_base64($err,"");
  2570. return "$name|''|$device|$reading|''|$err|''";
  2571. }
  2572. $rt = $rt+$wrt;
  2573. }
  2574. my $rowlist = encode_base64($rows,"");
  2575. # Background-Laufzeit ermitteln
  2576. my $brt = tv_interval($bst);
  2577. $rt = $rt.",".$brt;
  2578. return "$name|$rowlist|$device|$reading|$rt|0|$irowdone";
  2579. }
  2580. ####################################################################################################
  2581. # Auswertungsroutine der nichtblockierenden DB-Abfrage maxValue
  2582. ####################################################################################################
  2583. sub maxval_ParseDone($) {
  2584. my ($string) = @_;
  2585. my @a = split("\\|",$string);
  2586. my $hash = $defs{$a[0]};
  2587. my $name = $hash->{NAME};
  2588. my $rowlist = decode_base64($a[1]);
  2589. my $device = $a[2];
  2590. $device =~ s/[^A-Za-z\/\d_\.-]/\//g;
  2591. my $reading = $a[3];
  2592. $reading =~ s/[^A-Za-z\/\d_\.-]/\//g;
  2593. my $bt = $a[4];
  2594. my ($rt,$brt) = split(",", $bt);
  2595. my $err = $a[5]?decode_base64($a[5]):undef;
  2596. my $irowdone = $a[6];
  2597. my $reading_runtime_string;
  2598. if ($err) {
  2599. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  2600. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  2601. delete($hash->{HELPER}{RUNNING_PID});
  2602. return;
  2603. }
  2604. my %rh = split("§", $rowlist);
  2605. Log3 ($name, 5, "DbRep $name - result of maxValue calculation after decoding:");
  2606. foreach my $key (sort(keys(%rh))) {
  2607. Log3 ($name, 5, "DbRep $name - runtimestring Key: $key, value: ".$rh{$key});
  2608. }
  2609. # Readingaufbereitung
  2610. readingsBeginUpdate($hash);
  2611. # only for this block because of warnings if details of readings are not set
  2612. no warnings 'uninitialized';
  2613. foreach my $key (sort(keys(%rh))) {
  2614. my @k = split("\\|",$rh{$key});
  2615. my $rsf = $k[2]."__" if($k[2]);
  2616. if (AttrVal($hash->{NAME}, "readingNameMap", "")) {
  2617. $reading_runtime_string = $rsf.AttrVal($hash->{NAME}, "readingNameMap", "")."__".$k[0];
  2618. } else {
  2619. my $ds = $device."__" if ($device);
  2620. my $rds = $reading."__" if ($reading);
  2621. $reading_runtime_string = $rsf.$ds.$rds."MAX__".$k[0];
  2622. }
  2623. my $rv = $k[1];
  2624. ReadingsBulkUpdateValue ($hash, $reading_runtime_string, defined($rv)?sprintf("%.4f",$rv):"-");
  2625. }
  2626. ReadingsBulkUpdateValue ($hash, "db_lines_processed", $irowdone) if($hash->{LASTCMD} =~ /writeToDB/);
  2627. ReadingsBulkUpdateTimeState($hash,$brt,$rt,"done");
  2628. readingsEndUpdate($hash, 1);
  2629. delete($hash->{HELPER}{RUNNING_PID});
  2630. return;
  2631. }
  2632. ####################################################################################################
  2633. # nichtblockierende DB-Abfrage minValue
  2634. ####################################################################################################
  2635. sub minval_DoParse($) {
  2636. my ($string) = @_;
  2637. my ($name,$device,$reading,$prop,$ts) = split("\\§", $string);
  2638. my $hash = $defs{$name};
  2639. my $dbloghash = $hash->{dbloghash};
  2640. my $dbconn = $dbloghash->{dbconn};
  2641. my $dbuser = $dbloghash->{dbuser};
  2642. my $dblogname = $dbloghash->{NAME};
  2643. my $dbpassword = $attr{"sec$dblogname"}{secret};
  2644. my ($dbh,$sql,$sth,$err);
  2645. # Background-Startzeit
  2646. my $bst = [gettimeofday];
  2647. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1 });};
  2648. if ($@) {
  2649. $err = encode_base64($@,"");
  2650. Log3 ($name, 2, "DbRep $name - $@");
  2651. return "$name|''|$device|$reading|''|$err|''";
  2652. }
  2653. # only for this block because of warnings if details of readings are not set
  2654. no warnings 'uninitialized';
  2655. # ist Zeiteingrenzung und/oder Aggregation gesetzt ? (wenn ja -> "?" in SQL sonst undef)
  2656. my ($IsTimeSet,$IsAggrSet) = DbRep_checktimeaggr($hash);
  2657. Log3 ($name, 5, "DbRep $name - IsTimeSet: $IsTimeSet, IsAggrSet: $IsAggrSet");
  2658. # Timestampstring to Array
  2659. my @ts = split("\\|", $ts);
  2660. Log3 ($name, 5, "DbRep $name - Timestamp-Array: \n@ts");
  2661. # SQL-Startzeit
  2662. my $st = [gettimeofday];
  2663. # DB-Abfrage zeilenweise für jeden Array-Eintrag
  2664. my @row_array;
  2665. foreach my $row (@ts) {
  2666. my @a = split("#", $row);
  2667. my $runtime_string = $a[0];
  2668. my $runtime_string_first = $a[1];
  2669. my $runtime_string_next = $a[2];
  2670. $runtime_string = encode_base64($runtime_string,"");
  2671. if ($IsTimeSet || $IsAggrSet) {
  2672. $sql = DbRep_createSelectSql($hash,"history","VALUE,TIMESTAMP",$device,$reading,"'$runtime_string_first'","'$runtime_string_next'","ORDER BY TIMESTAMP");
  2673. } else {
  2674. $sql = DbRep_createSelectSql($hash,"history","VALUE,TIMESTAMP",$device,$reading,undef,undef,"ORDER BY TIMESTAMP");
  2675. }
  2676. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  2677. eval{ $sth = $dbh->prepare($sql);
  2678. $sth->execute();
  2679. };
  2680. if ($@) {
  2681. $err = encode_base64($@,"");
  2682. Log3 ($name, 2, "DbRep $name - $@");
  2683. $dbh->disconnect;
  2684. return "$name|''|$device|$reading|''|$err|''";
  2685. }
  2686. my @array= map { $runtime_string." ".$_ -> [0]." ".$_ -> [1]."\n" } @{ $sth->fetchall_arrayref() };
  2687. if(!@array) {
  2688. if(AttrVal($name, "aggregation", "") eq "hour") {
  2689. my @rsf = split(/[" "\|":"]/,$runtime_string_first);
  2690. @array = ($runtime_string." "."0"." ".$rsf[0]."_".$rsf[1]."\n");
  2691. } else {
  2692. my @rsf = split(" ",$runtime_string_first);
  2693. @array = ($runtime_string." "."0"." ".$rsf[0]."\n");
  2694. }
  2695. }
  2696. push(@row_array, @array);
  2697. }
  2698. $sth->finish;
  2699. $dbh->disconnect;
  2700. # SQL-Laufzeit ermitteln
  2701. my $rt = tv_interval($st);
  2702. Log3 ($name, 5, "DbRep $name -> raw data of row_array result:\n @row_array");
  2703. #---------- Berechnung Ergebnishash minValue ------------------------
  2704. my $i = 1;
  2705. my %rh = ();
  2706. my $lastruntimestring;
  2707. my $row_min_time;
  2708. my ($min_value,$value);
  2709. foreach my $row (@row_array) {
  2710. my @a = split("[ \t][ \t]*", $row);
  2711. my $runtime_string = decode_base64($a[0]);
  2712. $lastruntimestring = $runtime_string if ($i == 1);
  2713. $value = $a[1];
  2714. $min_value = $a[1] if ($i == 1);
  2715. $a[-1] =~ s/:/-/g if($a[-1]); # substituieren unsupported characters -> siehe fhem.pl
  2716. my $timestamp = ($a[-1]&&$a[-2])?$a[-2]."_".$a[-1]:$a[-1];
  2717. # Leerzeichen am Ende $timestamp entfernen
  2718. $timestamp =~ s/\s+$//g;
  2719. # Test auf $value = "numeric"
  2720. if (!looks_like_number($value)) {
  2721. # $a[-1] =~ s/\s+$//g;
  2722. Log3 ($name, 2, "DbRep $name - ERROR - value isn't numeric in minValue function. Faulty dataset was \nTIMESTAMP: $timestamp, DEVICE: $device, READING: $reading, VALUE: $value.");
  2723. $err = encode_base64("Value isn't numeric. Faulty dataset was - TIMESTAMP: $timestamp, VALUE: $value", "");
  2724. return "$name|''|$device|$reading|''|$err|''";
  2725. }
  2726. Log3 ($name, 5, "DbRep $name - Runtimestring: $runtime_string, DEVICE: $device, READING: $reading, TIMESTAMP: $timestamp, VALUE: $value");
  2727. $rh{$runtime_string} = $runtime_string."|".$min_value."|".$timestamp if ($i == 1); # minValue des ersten SQL-Statements in hash einfügen
  2728. if ($runtime_string eq $lastruntimestring) {
  2729. if (!defined($min_value) || $value < $min_value) {
  2730. $min_value = $value;
  2731. $row_min_time = $timestamp;
  2732. $rh{$runtime_string} = $runtime_string."|".$min_value."|".$row_min_time;
  2733. }
  2734. } else {
  2735. # neuer Zeitabschnitt beginnt, ersten Value-Wert erfassen
  2736. $lastruntimestring = $runtime_string;
  2737. $min_value = $value;
  2738. $row_min_time = $timestamp;
  2739. $rh{$runtime_string} = $runtime_string."|".$min_value."|".$row_min_time;
  2740. }
  2741. $i++;
  2742. }
  2743. #---------------------------------------------------------------------------------------------
  2744. Log3 ($name, 5, "DbRep $name - result of minValue calculation before encoding:");
  2745. foreach my $key (sort(keys(%rh))) {
  2746. Log3 ($name, 5, "runtimestring Key: $key, value: ".$rh{$key});
  2747. }
  2748. # Ergebnishash als Einzeiler zurückgeben bzw. an Schreibroutine übergeben
  2749. my $rows = join('§', %rh);
  2750. # Ergebnisse in Datenbank schreiben
  2751. my ($wrt,$irowdone);
  2752. if($prop =~ /writeToDB/) {
  2753. ($wrt,$irowdone,$err) = DbRep_OutputWriteToDB($name,$device,$reading,$rows,"min");
  2754. if ($err) {
  2755. Log3 $hash->{NAME}, 2, "DbRep $name - $err";
  2756. $err = encode_base64($err,"");
  2757. return "$name|''|$device|$reading|''|$err|''";
  2758. }
  2759. $rt = $rt+$wrt;
  2760. }
  2761. my $rowlist = encode_base64($rows,"");
  2762. # Background-Laufzeit ermitteln
  2763. my $brt = tv_interval($bst);
  2764. $rt = $rt.",".$brt;
  2765. return "$name|$rowlist|$device|$reading|$rt|0|$irowdone";
  2766. }
  2767. ####################################################################################################
  2768. # Auswertungsroutine der nichtblockierenden DB-Abfrage minValue
  2769. ####################################################################################################
  2770. sub minval_ParseDone($) {
  2771. my ($string) = @_;
  2772. my @a = split("\\|",$string);
  2773. my $hash = $defs{$a[0]};
  2774. my $name = $hash->{NAME};
  2775. my $rowlist = decode_base64($a[1]);
  2776. my $device = $a[2];
  2777. $device =~ s/[^A-Za-z\/\d_\.-]/\//g;
  2778. my $reading = $a[3];
  2779. $reading =~ s/[^A-Za-z\/\d_\.-]/\//g;
  2780. my $bt = $a[4];
  2781. my ($rt,$brt) = split(",", $bt);
  2782. my $err = $a[5]?decode_base64($a[5]):undef;
  2783. my $irowdone = $a[6];
  2784. my $reading_runtime_string;
  2785. if ($err) {
  2786. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  2787. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  2788. delete($hash->{HELPER}{RUNNING_PID});
  2789. return;
  2790. }
  2791. my %rh = split("§", $rowlist);
  2792. Log3 ($name, 5, "DbRep $name - result of minValue calculation after decoding:");
  2793. foreach my $key (sort(keys(%rh))) {
  2794. Log3 ($name, 5, "DbRep $name - runtimestring Key: $key, value: ".$rh{$key});
  2795. }
  2796. # Readingaufbereitung
  2797. readingsBeginUpdate($hash);
  2798. # only for this block because of warnings if details of readings are not set
  2799. no warnings 'uninitialized';
  2800. foreach my $key (sort(keys(%rh))) {
  2801. my @k = split("\\|",$rh{$key});
  2802. my $rsf = $k[2]."__" if($k[2]);
  2803. if (AttrVal($hash->{NAME}, "readingNameMap", "")) {
  2804. $reading_runtime_string = $rsf.AttrVal($hash->{NAME}, "readingNameMap", "")."__".$k[0];
  2805. } else {
  2806. my $ds = $device."__" if ($device);
  2807. my $rds = $reading."__" if ($reading);
  2808. $reading_runtime_string = $rsf.$ds.$rds."MIN__".$k[0];
  2809. }
  2810. my $rv = $k[1];
  2811. ReadingsBulkUpdateValue ($hash, $reading_runtime_string, defined($rv)?sprintf("%.4f",$rv):"-");
  2812. }
  2813. ReadingsBulkUpdateValue ($hash, "db_lines_processed", $irowdone) if($hash->{LASTCMD} =~ /writeToDB/);
  2814. ReadingsBulkUpdateTimeState($hash,$brt,$rt,"done");
  2815. readingsEndUpdate($hash, 1);
  2816. delete($hash->{HELPER}{RUNNING_PID});
  2817. return;
  2818. }
  2819. ####################################################################################################
  2820. # nichtblockierende DB-Abfrage diffValue
  2821. ####################################################################################################
  2822. sub diffval_DoParse($) {
  2823. my ($string) = @_;
  2824. my ($name,$device,$reading,$prop,$ts) = split("\\§", $string);
  2825. my $hash = $defs{$name};
  2826. my $dbloghash = $hash->{dbloghash};
  2827. my $dbconn = $dbloghash->{dbconn};
  2828. my $dbuser = $dbloghash->{dbuser};
  2829. my $dblogname = $dbloghash->{NAME};
  2830. my $dbmodel = $dbloghash->{MODEL};
  2831. my $dbpassword = $attr{"sec$dblogname"}{secret};
  2832. my ($dbh,$sql,$sth,$err,$selspec);
  2833. # Background-Startzeit
  2834. my $bst = [gettimeofday];
  2835. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1 });};
  2836. if ($@) {
  2837. $err = encode_base64($@,"");
  2838. Log3 ($name, 2, "DbRep $name - $@");
  2839. return "$name|''|$device|$reading|''|''|''|$err|''";
  2840. }
  2841. # only for this block because of warnings if details of readings are not set
  2842. no warnings 'uninitialized';
  2843. # ist Zeiteingrenzung und/oder Aggregation gesetzt ? (wenn ja -> "?" in SQL sonst undef)
  2844. my ($IsTimeSet,$IsAggrSet) = DbRep_checktimeaggr($hash);
  2845. Log3 ($name, 5, "DbRep $name - IsTimeSet: $IsTimeSet, IsAggrSet: $IsAggrSet");
  2846. # Timestampstring to Array
  2847. my @ts = split("\\|", $ts);
  2848. Log3 ($name, 5, "DbRep $name - Timestamp-Array: \n@ts");
  2849. #vorbereiten der DB-Abfrage, DB-Modell-abhaengig
  2850. if($dbmodel eq "MYSQL") {
  2851. $selspec = "TIMESTAMP,VALUE, if(VALUE-\@V < 0 OR \@RB = 1 , \@diff:= 0, \@diff:= VALUE-\@V ) as DIFF, \@V:= VALUE as VALUEBEFORE, \@RB:= '0' as RBIT ";
  2852. } else {
  2853. $selspec = "TIMESTAMP,VALUE";
  2854. }
  2855. # SQL-Startzeit
  2856. my $st = [gettimeofday];
  2857. # DB-Abfrage zeilenweise für jeden Array-Eintrag
  2858. my @row_array;
  2859. my @array;
  2860. foreach my $row (@ts) {
  2861. my @a = split("#", $row);
  2862. my $runtime_string = $a[0];
  2863. my $runtime_string_first = $a[1];
  2864. my $runtime_string_next = $a[2];
  2865. $runtime_string = encode_base64($runtime_string,"");
  2866. if($dbmodel eq "MYSQL") {
  2867. eval {$dbh->do("set \@V:= 0, \@diff:= 0, \@diffTotal:= 0, \@RB:= 1;");}; # @\RB = Resetbit wenn neues Selektionsintervall beginnt
  2868. }
  2869. if ($@) {
  2870. $err = encode_base64($@,"");
  2871. Log3 ($name, 2, "DbRep $name - $@");
  2872. $dbh->disconnect;
  2873. return "$name|''|$device|$reading|''|''|''|$err|''";
  2874. }
  2875. if ($IsTimeSet || $IsAggrSet) {
  2876. $sql = DbRep_createSelectSql($hash,"history",$selspec,$device,$reading,"'$runtime_string_first'","'$runtime_string_next'",'');
  2877. } else {
  2878. $sql = DbRep_createSelectSql($hash,"history",$selspec,$device,$reading,undef,undef,'');
  2879. }
  2880. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  2881. eval{ $sth = $dbh->prepare($sql);
  2882. $sth->execute();
  2883. };
  2884. if ($@) {
  2885. $err = encode_base64($@,"");
  2886. Log3 ($name, 2, "DbRep $name - $@");
  2887. $dbh->disconnect;
  2888. return "$name|''|$device|$reading|''|''|''|$err|''";
  2889. } else {
  2890. if($dbmodel eq "MYSQL") {
  2891. @array = map { $runtime_string." ".$_ -> [0]." ".$_ -> [1]." ".$_ -> [2]."\n" } @{ $sth->fetchall_arrayref() };
  2892. } else {
  2893. @array = map { $runtime_string." ".$_ -> [0]." ".$_ -> [1]."\n" } @{ $sth->fetchall_arrayref() };
  2894. if (@array) {
  2895. my @sp;
  2896. my $dse = 0;
  2897. my $vold;
  2898. my @sqlite_array;
  2899. foreach my $row (@array) {
  2900. @sp = split("[ \t][ \t]*", $row, 4);
  2901. my $runtime_string = $sp[0];
  2902. my $timestamp = $sp[2]?$sp[1]." ".$sp[2]:$sp[1];
  2903. my $vnew = $sp[3];
  2904. $vnew =~ tr/\n//d;
  2905. $dse = ($vold && (($vnew-$vold) > 0))?($vnew-$vold):0;
  2906. @sp = $runtime_string." ".$timestamp." ".$vnew." ".$dse."\n";
  2907. $vold = $vnew;
  2908. push(@sqlite_array, @sp);
  2909. }
  2910. @array = @sqlite_array;
  2911. }
  2912. }
  2913. if(!@array) {
  2914. if(AttrVal($name, "aggregation", "") eq "hour") {
  2915. my @rsf = split(/[" "\|":"]/,$runtime_string_first);
  2916. @array = ($runtime_string." ".$rsf[0]."_".$rsf[1]."\n");
  2917. } else {
  2918. my @rsf = split(" ",$runtime_string_first);
  2919. @array = ($runtime_string." ".$rsf[0]."\n");
  2920. }
  2921. }
  2922. push(@row_array, @array);
  2923. }
  2924. }
  2925. # SQL-Laufzeit ermitteln
  2926. my $rt = tv_interval($st);
  2927. $dbh->disconnect;
  2928. Log3 ($name, 5, "DbRep $name - raw data of row_array result:\n @row_array");
  2929. my $difflimit = AttrVal($name, "diffAccept", "20"); # legt fest, bis zu welchem Wert Differenzen akzeptiert werden (Ausreißer eliminieren)
  2930. # Berechnung diffValue aus Selektionshash
  2931. my %rh = (); # Ergebnishash, wird alle Ergebniszeilen enthalten
  2932. my %ch = (); # counthash, enthält die Anzahl der verarbeiteten Datasets pro runtime_string
  2933. my $lastruntimestring;
  2934. my $i = 1;
  2935. my $lval; # immer der letzte Wert von $value
  2936. my $rslval; # runtimestring von lval
  2937. my $uediff; # Übertragsdifferenz (Differenz zwischen letzten Wert einer Aggregationsperiode und dem ersten Wert der Folgeperiode)
  2938. my $diff_current; # Differenzwert des aktuellen Datasets
  2939. my $diff_before; # Differenzwert vorheriger Datensatz
  2940. my $rejectstr; # String der ignorierten Differenzsätze
  2941. my $diff_total; # Summenwert aller berücksichtigten Teildifferenzen
  2942. my $max = ($#row_array)+1; # Anzahl aller Listenelemente
  2943. Log3 ($name, 5, "DbRep $name - data of row_array result assigned to fields:\n");
  2944. foreach my $row (@row_array) {
  2945. my @a = split("[ \t][ \t]*", $row, 6);
  2946. my $runtime_string = decode_base64($a[0]);
  2947. $lastruntimestring = $runtime_string if ($i == 1);
  2948. my $timestamp = $a[2]?$a[1]."_".$a[2]:$a[1];
  2949. my $value = $a[3]?$a[3]:0;
  2950. my $diff = $a[4]?sprintf("%.4f",$a[4]):0;
  2951. # if ($uediff) {
  2952. # $diff = $diff + $uediff;
  2953. # Log3 ($name, 4, "DbRep $name - balance difference of $uediff between $rslval and $runtime_string");
  2954. # $uediff = 0;
  2955. # }
  2956. # Leerzeichen am Ende $timestamp entfernen
  2957. $timestamp =~ s/\s+$//g;
  2958. # Test auf $value = "numeric"
  2959. if (!looks_like_number($value)) {
  2960. $a[3] =~ s/\s+$//g;
  2961. Log3 ($name, 2, "DbRep $name - ERROR - value isn't numeric in diffValue function. Faulty dataset was \nTIMESTAMP: $timestamp, DEVICE: $device, READING: $reading, VALUE: $value.");
  2962. $err = encode_base64("Value isn't numeric. Faulty dataset was - TIMESTAMP: $timestamp, VALUE: $value", "");
  2963. return "$name|''|$device|$reading|''|''|''|$err|''";
  2964. }
  2965. Log3 ($name, 5, "DbRep $name - Runtimestring: $runtime_string, DEVICE: $device, READING: $reading, \nTIMESTAMP: $timestamp, VALUE: $value, DIFF: $diff");
  2966. # String ignorierter Zeilen erzeugen
  2967. $diff_current = $timestamp." ".$diff;
  2968. if($diff > $difflimit) {
  2969. $rejectstr .= $diff_before." -> ".$diff_current."\n";
  2970. }
  2971. $diff_before = $diff_current;
  2972. # Ergebnishash erzeugen
  2973. if ($runtime_string eq $lastruntimestring) {
  2974. if ($i == 1) {
  2975. $diff_total = $diff?$diff:0 if($diff <= $difflimit);
  2976. $rh{$runtime_string} = $runtime_string."|".$diff_total."|".$timestamp;
  2977. $ch{$runtime_string} = 1 if($value);
  2978. $lval = $value;
  2979. $rslval = $runtime_string;
  2980. }
  2981. if ($diff) {
  2982. if($diff <= $difflimit) {
  2983. $diff_total = $diff_total+$diff;
  2984. }
  2985. $rh{$runtime_string} = $runtime_string."|".$diff_total."|".$timestamp;
  2986. $ch{$runtime_string}++ if($value && $i > 1);
  2987. $lval = $value;
  2988. $rslval = $runtime_string;
  2989. }
  2990. } else {
  2991. # neuer Zeitabschnitt beginnt, ersten Value-Wert erfassen und Übertragsdifferenz bilden
  2992. $lastruntimestring = $runtime_string;
  2993. $i = 1;
  2994. $uediff = $value - $lval if($value > $lval);
  2995. $diff = $uediff;
  2996. $lval = $value if($value); # Übetrag über Perioden mit value = 0 hinweg !
  2997. $rslval = $runtime_string;
  2998. Log3 ($name, 4, "DbRep $name - balance difference of $uediff between $rslval and $runtime_string");
  2999. $diff_total = $diff?$diff:0 if($diff <= $difflimit);
  3000. $rh{$runtime_string} = $runtime_string."|".$diff_total."|".$timestamp;
  3001. $ch{$runtime_string} = 1 if($value);
  3002. $uediff = 0;
  3003. }
  3004. $i++;
  3005. }
  3006. Log3 ($name, 4, "DbRep $name - result of diffValue calculation before encoding:");
  3007. foreach my $key (sort(keys(%rh))) {
  3008. Log3 ($name, 4, "runtimestring Key: $key, value: ".$rh{$key});
  3009. }
  3010. my $ncp = DbRep_calcount($hash,\%ch);
  3011. my ($ncps,$ncpslist);
  3012. if(%$ncp) {
  3013. Log3 ($name, 3, "DbRep $name - time/aggregation periods containing only one dataset -> no diffValue calc was possible in period:");
  3014. foreach my $key (sort(keys%{$ncp})) {
  3015. Log3 ($name, 3, $key) ;
  3016. }
  3017. $ncps = join('§', %$ncp);
  3018. $ncpslist = encode_base64($ncps,"");
  3019. }
  3020. # Ergebnishash als Einzeiler zurückgeben
  3021. # ignorierte Zeilen ($diff > $difflimit)
  3022. my $rowsrej = encode_base64($rejectstr,"") if($rejectstr);
  3023. # Ergebnishash
  3024. my $rows = join('§', %rh);
  3025. # Ergebnisse in Datenbank schreiben
  3026. my ($wrt,$irowdone);
  3027. if($prop =~ /writeToDB/) {
  3028. ($wrt,$irowdone,$err) = DbRep_OutputWriteToDB($name,$device,$reading,$rows,"diff");
  3029. if ($err) {
  3030. Log3 $hash->{NAME}, 2, "DbRep $name - $err";
  3031. $err = encode_base64($err,"");
  3032. return "$name|''|$device|$reading|''|''|''|$err|''";
  3033. }
  3034. $rt = $rt+$wrt;
  3035. }
  3036. my $rowlist = encode_base64($rows,"");
  3037. # Background-Laufzeit ermitteln
  3038. my $brt = tv_interval($bst);
  3039. $rt = $rt.",".$brt;
  3040. return "$name|$rowlist|$device|$reading|$rt|$rowsrej|$ncpslist|0|$irowdone";
  3041. }
  3042. ####################################################################################################
  3043. # Auswertungsroutine der nichtblockierenden DB-Abfrage diffValue
  3044. ####################################################################################################
  3045. sub diffval_ParseDone($) {
  3046. my ($string) = @_;
  3047. my @a = split("\\|",$string);
  3048. my $hash = $defs{$a[0]};
  3049. my $name = $hash->{NAME};
  3050. my $rowlist = decode_base64($a[1]);
  3051. my $device = $a[2];
  3052. $device =~ s/[^A-Za-z\/\d_\.-]/\//g;
  3053. my $reading = $a[3];
  3054. $reading =~ s/[^A-Za-z\/\d_\.-]/\//g;
  3055. my $bt = $a[4];
  3056. my ($rt,$brt) = split(",", $bt);
  3057. my $rowsrej = $a[5]?decode_base64($a[5]):undef; # String von Datensätzen die nicht berücksichtigt wurden (diff Schwellenwert Überschreitung)
  3058. my $ncpslist = decode_base64($a[6]); # Hash von Perioden die nicht kalkuliert werden konnten "no calc in period"
  3059. my $err = $a[7]?decode_base64($a[7]):undef;
  3060. my $irowdone = $a[8];
  3061. my $reading_runtime_string;
  3062. my $difflimit = AttrVal($name, "diffAccept", "20"); # legt fest, bis zu welchem Wert Differenzen akzeptoert werden (Ausreißer eliminieren)AttrVal($name, "diffAccept", "20");
  3063. if ($err) {
  3064. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  3065. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  3066. delete($hash->{HELPER}{RUNNING_PID});
  3067. return;
  3068. }
  3069. # only for this block because of warnings if details of readings are not set
  3070. no warnings 'uninitialized';
  3071. # Auswertung hashes für state-Warning
  3072. $rowsrej =~ s/_/ /g;
  3073. Log3 ($name, 3, "DbRep $name -> data ignored while calc diffValue due to threshold overrun (diffAccept = $difflimit): \n$rowsrej")
  3074. if($rowsrej);
  3075. $rowsrej =~ s/\n/ \|\| /g;
  3076. my %ncp = split("§", $ncpslist);
  3077. my $ncpstr;
  3078. if(%ncp) {
  3079. foreach my $ncpkey (sort(keys(%ncp))) {
  3080. $ncpstr .= $ncpkey." || ";
  3081. }
  3082. }
  3083. # Readingaufbereitung
  3084. my %rh = split("§", $rowlist);
  3085. Log3 ($name, 4, "DbRep $name - result of diffValue calculation after decoding:");
  3086. foreach my $key (sort(keys(%rh))) {
  3087. Log3 ($name, 4, "DbRep $name - runtimestring Key: $key, value: ".$rh{$key});
  3088. }
  3089. readingsBeginUpdate($hash);
  3090. foreach my $key (sort(keys(%rh))) {
  3091. my @k = split("\\|",$rh{$key});
  3092. my $rts = $k[2]."__";
  3093. $rts =~ s/:/-/g; # substituieren unsupported characters -> siehe fhem.pl
  3094. if (AttrVal($hash->{NAME}, "readingNameMap", "")) {
  3095. $reading_runtime_string = $rts.AttrVal($hash->{NAME}, "readingNameMap", "")."__".$k[0];
  3096. } else {
  3097. my $ds = $device."__" if ($device);
  3098. my $rds = $reading."__" if ($reading);
  3099. $reading_runtime_string = $rts.$ds.$rds."DIFF__".$k[0];
  3100. }
  3101. my $rv = $k[1];
  3102. ReadingsBulkUpdateValue ($hash, $reading_runtime_string, $rv?sprintf("%.4f",$rv):"-");
  3103. }
  3104. ReadingsBulkUpdateValue ($hash, "db_lines_processed", $irowdone) if($hash->{LASTCMD} =~ /writeToDB/);
  3105. ReadingsBulkUpdateValue ($hash, "diff_overrun_limit_".$difflimit, $rowsrej) if($rowsrej);
  3106. ReadingsBulkUpdateValue ($hash, "less_data_in_period", $ncpstr) if($ncpstr);
  3107. ReadingsBulkUpdateTimeState($hash,$brt,$rt,($ncpstr||$rowsrej)?"Warning":"done");
  3108. readingsEndUpdate($hash, 1);
  3109. delete($hash->{HELPER}{RUNNING_PID});
  3110. return;
  3111. }
  3112. ####################################################################################################
  3113. # nichtblockierende DB-Abfrage sumValue
  3114. ####################################################################################################
  3115. sub sumval_DoParse($) {
  3116. my ($string) = @_;
  3117. my ($name,$device,$reading,$prop,$ts) = split("\\§", $string);
  3118. my $hash = $defs{$name};
  3119. my $dbloghash = $hash->{dbloghash};
  3120. my $dbconn = $dbloghash->{dbconn};
  3121. my $dbuser = $dbloghash->{dbuser};
  3122. my $dblogname = $dbloghash->{NAME};
  3123. my $dbpassword = $attr{"sec$dblogname"}{secret};
  3124. my ($dbh,$sql,$sth,$err,$selspec);
  3125. # Background-Startzeit
  3126. my $bst = [gettimeofday];
  3127. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1 });};
  3128. if ($@) {
  3129. $err = encode_base64($@,"");
  3130. Log3 ($name, 2, "DbRep $name - $@");
  3131. return "$name|''|$device|$reading|''|$err|''";
  3132. }
  3133. # only for this block because of warnings if details of readings are not set
  3134. no warnings 'uninitialized';
  3135. # ist Zeiteingrenzung und/oder Aggregation gesetzt ? (wenn ja -> "?" in SQL sonst undef)
  3136. my ($IsTimeSet,$IsAggrSet) = DbRep_checktimeaggr($hash);
  3137. Log3 ($name, 5, "DbRep $name - IsTimeSet: $IsTimeSet, IsAggrSet: $IsAggrSet");
  3138. # Timestampstring to Array
  3139. my @ts = split("\\|", $ts);
  3140. Log3 ($name, 5, "DbRep $name - Timestamp-Array: \n@ts");
  3141. #vorbereiten der DB-Abfrage, DB-Modell-abhaengig
  3142. if ($dbloghash->{MODEL} eq "POSTGRESQL") {
  3143. $selspec = "SUM(VALUE::numeric)";
  3144. } elsif ($dbloghash->{MODEL} eq "MYSQL") {
  3145. $selspec = "SUM(VALUE)";
  3146. } elsif ($dbloghash->{MODEL} eq "SQLITE") {
  3147. $selspec = "SUM(VALUE)";
  3148. } else {
  3149. $selspec = "SUM(VALUE)";
  3150. }
  3151. # SQL-Startzeit
  3152. my $st = [gettimeofday];
  3153. # DB-Abfrage zeilenweise für jeden Array-Eintrag
  3154. my $arrstr;
  3155. foreach my $row (@ts) {
  3156. my @a = split("#", $row);
  3157. my $runtime_string = $a[0];
  3158. my $runtime_string_first = $a[1];
  3159. my $runtime_string_next = $a[2];
  3160. if ($IsTimeSet || $IsAggrSet) {
  3161. $sql = DbRep_createSelectSql($hash,"history",$selspec,$device,$reading,"'$runtime_string_first'","'$runtime_string_next'",'');
  3162. } else {
  3163. $sql = DbRep_createSelectSql($hash,"history",$selspec,$device,$reading,undef,undef,'');
  3164. }
  3165. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  3166. eval{ $sth = $dbh->prepare($sql);
  3167. $sth->execute();
  3168. };
  3169. if ($@) {
  3170. $err = encode_base64($@,"");
  3171. Log3 ($name, 2, "DbRep $name - $@");
  3172. $dbh->disconnect;
  3173. return "$name|''|$device|$reading|''|$err|''";
  3174. }
  3175. # DB-Abfrage -> Ergebnis in @arr aufnehmen
  3176. my @line = $sth->fetchrow_array();
  3177. Log3 ($name, 5, "DbRep $name - SQL result: $line[0]") if($line[0]);
  3178. if(AttrVal($name, "aggregation", "") eq "hour") {
  3179. my @rsf = split(/[" "\|":"]/,$runtime_string_first);
  3180. $arrstr .= $runtime_string."#".$line[0]."#".$rsf[0]."_".$rsf[1]."|";
  3181. } else {
  3182. my @rsf = split(" ",$runtime_string_first);
  3183. $arrstr .= $runtime_string."#".$line[0]."#".$rsf[0]."|";
  3184. }
  3185. }
  3186. $sth->finish;
  3187. $dbh->disconnect;
  3188. # SQL-Laufzeit ermitteln
  3189. my $rt = tv_interval($st);
  3190. # Ergebnisse in Datenbank schreiben
  3191. my ($wrt,$irowdone);
  3192. if($prop =~ /writeToDB/) {
  3193. ($wrt,$irowdone,$err) = DbRep_OutputWriteToDB($name,$device,$reading,$arrstr,"sum");
  3194. if ($err) {
  3195. Log3 $hash->{NAME}, 2, "DbRep $name - $err";
  3196. $err = encode_base64($err,"");
  3197. return "$name|''|$device|$reading|''|$err|''";
  3198. }
  3199. $rt = $rt+$wrt;
  3200. }
  3201. # Daten müssen als Einzeiler zurückgegeben werden
  3202. $arrstr = encode_base64($arrstr,"");
  3203. # Background-Laufzeit ermitteln
  3204. my $brt = tv_interval($bst);
  3205. $rt = $rt.",".$brt;
  3206. return "$name|$arrstr|$device|$reading|$rt|0|$irowdone";
  3207. }
  3208. ####################################################################################################
  3209. # Auswertungsroutine der nichtblockierenden DB-Abfrage sumValue
  3210. ####################################################################################################
  3211. sub sumval_ParseDone($) {
  3212. my ($string) = @_;
  3213. my @a = split("\\|",$string);
  3214. my $hash = $defs{$a[0]};
  3215. my $name = $hash->{NAME};
  3216. my $arrstr = decode_base64($a[1]);
  3217. my $device = $a[2];
  3218. $device =~ s/[^A-Za-z\/\d_\.-]/\//g;
  3219. my $reading = $a[3];
  3220. $reading =~ s/[^A-Za-z\/\d_\.-]/\//g;
  3221. my $bt = $a[4];
  3222. my ($rt,$brt) = split(",", $bt);
  3223. my $err = $a[5]?decode_base64($a[5]):undef;
  3224. my $irowdone = $a[6];
  3225. my $reading_runtime_string;
  3226. if ($err) {
  3227. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  3228. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  3229. delete($hash->{HELPER}{RUNNING_PID});
  3230. return;
  3231. }
  3232. # only for this block because of warnings if details of readings are not set
  3233. no warnings 'uninitialized';
  3234. # Readingaufbereitung
  3235. readingsBeginUpdate($hash);
  3236. my @arr = split("\\|", $arrstr);
  3237. foreach my $row (@arr) {
  3238. my @a = split("#", $row);
  3239. my $runtime_string = $a[0];
  3240. my $c = $a[1];
  3241. my $rsf = $a[2]."__";
  3242. if (AttrVal($hash->{NAME}, "readingNameMap", "")) {
  3243. $reading_runtime_string = $rsf.AttrVal($hash->{NAME}, "readingNameMap", "")."__".$runtime_string;
  3244. } else {
  3245. my $ds = $device."__" if ($device);
  3246. my $rds = $reading."__" if ($reading);
  3247. $reading_runtime_string = $rsf.$ds.$rds."SUM__".$runtime_string;
  3248. }
  3249. ReadingsBulkUpdateValue ($hash, $reading_runtime_string, $c?sprintf("%.4f",$c):"-");
  3250. }
  3251. ReadingsBulkUpdateValue ($hash, "db_lines_processed", $irowdone) if($hash->{LASTCMD} =~ /writeToDB/);
  3252. ReadingsBulkUpdateTimeState($hash,$brt,$rt,"done");
  3253. readingsEndUpdate($hash, 1);
  3254. delete($hash->{HELPER}{RUNNING_PID});
  3255. return;
  3256. }
  3257. ####################################################################################################
  3258. # nichtblockierendes DB delete
  3259. ####################################################################################################
  3260. sub del_DoParse($) {
  3261. my ($string) = @_;
  3262. my ($name,$table,$device,$reading,$runtime_string_first,$runtime_string_next) = split("\\|", $string);
  3263. my $hash = $defs{$name};
  3264. my $dbloghash = $hash->{dbloghash};
  3265. my $dbconn = $dbloghash->{dbconn};
  3266. my $dbuser = $dbloghash->{dbuser};
  3267. my $dblogname = $dbloghash->{NAME};
  3268. my $dbpassword = $attr{"sec$dblogname"}{secret};
  3269. my ($dbh,$sql,$sth,$err,$rows);
  3270. # Background-Startzeit
  3271. my $bst = [gettimeofday];
  3272. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoCommit => 1, AutoInactiveDestroy => 1 });};
  3273. if ($@) {
  3274. $err = encode_base64($@,"");
  3275. Log3 ($name, 2, "DbRep $name - $@");
  3276. return "$name|''|''|$err|''|''|''";
  3277. }
  3278. # ist Zeiteingrenzung und/oder Aggregation gesetzt ? (wenn ja -> "?" in SQL sonst undef)
  3279. my ($IsTimeSet,$IsAggrSet) = DbRep_checktimeaggr($hash);
  3280. Log3 ($name, 5, "DbRep $name - IsTimeSet: $IsTimeSet, IsAggrSet: $IsAggrSet");
  3281. # SQL zusammenstellen für DB-Operation
  3282. if ($IsTimeSet || $IsAggrSet) {
  3283. $sql = DbRep_createDeleteSql($hash,$table,$device,$reading,$runtime_string_first,$runtime_string_next,'');
  3284. } else {
  3285. $sql = DbRep_createDeleteSql($hash,$table,$device,$reading,undef,undef,'');
  3286. }
  3287. $sth = $dbh->prepare($sql);
  3288. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  3289. # SQL-Startzeit
  3290. my $st = [gettimeofday];
  3291. eval {$sth->execute();};
  3292. if ($@) {
  3293. $err = encode_base64($@,"");
  3294. Log3 ($name, 2, "DbRep $name - $@");
  3295. $dbh->disconnect;
  3296. return "$name|''|''|$err|''|''|''";
  3297. }
  3298. $rows = $sth->rows;
  3299. $dbh->commit() if(!$dbh->{AutoCommit});
  3300. $dbh->disconnect;
  3301. # SQL-Laufzeit ermitteln
  3302. my $rt = tv_interval($st);
  3303. Log3 ($name, 5, "DbRep $name - Number of deleted rows: $rows");
  3304. # Background-Laufzeit ermitteln
  3305. my $brt = tv_interval($bst);
  3306. $rt = $rt.",".$brt;
  3307. return "$name|$rows|$rt|0|$table|$device|$reading";
  3308. }
  3309. ####################################################################################################
  3310. # Auswertungsroutine DB delete
  3311. ####################################################################################################
  3312. sub del_ParseDone($) {
  3313. my ($string) = @_;
  3314. my @a = split("\\|",$string);
  3315. my $hash = $defs{$a[0]};
  3316. my $name = $hash->{NAME};
  3317. my $rows = $a[1];
  3318. my $bt = $a[2];
  3319. my ($rt,$brt) = split(",", $bt);
  3320. my $err = $a[3]?decode_base64($a[3]):undef;
  3321. my $table = $a[4];
  3322. my $device = $a[5];
  3323. $device =~ s/[^A-Za-z\/\d_\.-]/\//g;
  3324. my $reading = $a[6];
  3325. $reading =~ s/[^A-Za-z\/\d_\.-]/\//g;
  3326. my $erread;
  3327. # Befehl nach Procedure ausführen
  3328. $erread = DbRep_afterproc($hash, "delEntries");
  3329. if ($err) {
  3330. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  3331. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  3332. delete($hash->{HELPER}{RUNNING_PID});
  3333. return;
  3334. }
  3335. # only for this block because of warnings if details of readings are not set
  3336. no warnings 'uninitialized';
  3337. my ($reading_runtime_string, $ds, $rds);
  3338. if (AttrVal($hash->{NAME}, "readingNameMap", "")) {
  3339. $reading_runtime_string = AttrVal($hash->{NAME}, "readingNameMap", "")."--DELETED_ROWS--";
  3340. } else {
  3341. $ds = $device."--" if ($device && $table ne "current");
  3342. $rds = $reading."--" if ($reading && $table ne "current");
  3343. $reading_runtime_string = $ds.$rds."--DELETED_ROWS_".uc($table)."--";
  3344. }
  3345. readingsBeginUpdate($hash);
  3346. ReadingsBulkUpdateValue ($hash, $reading_runtime_string, $rows);
  3347. $rows = ($table eq "current")?$rows:$ds.$rds.$rows;
  3348. Log3 ($name, 3, "DbRep $name - Entries of $hash->{DATABASE}.$table deleted: $rows");
  3349. my $state = $erread?$erread:"done";
  3350. ReadingsBulkUpdateTimeState($hash,$brt,$rt,$state);
  3351. readingsEndUpdate($hash, 1);
  3352. delete($hash->{HELPER}{RUNNING_PID});
  3353. return;
  3354. }
  3355. ####################################################################################################
  3356. # nichtblockierendes DB insert
  3357. ####################################################################################################
  3358. sub insert_Push($) {
  3359. my ($name) = @_;
  3360. my $hash = $defs{$name};
  3361. my $dbloghash = $hash->{dbloghash};
  3362. my $dbconn = $dbloghash->{dbconn};
  3363. my $dbuser = $dbloghash->{dbuser};
  3364. my $dblogname = $dbloghash->{NAME};
  3365. my $dbpassword = $attr{"sec$dblogname"}{secret};
  3366. my $utf8 = defined($hash->{UTF8})?$hash->{UTF8}:0;
  3367. my ($err,$sth);
  3368. # Background-Startzeit
  3369. my $bst = [gettimeofday];
  3370. my $dbh;
  3371. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoCommit => 1, AutoInactiveDestroy => 1, mysql_enable_utf8 => $utf8 });};
  3372. if ($@) {
  3373. $err = encode_base64($@,"");
  3374. Log3 ($name, 2, "DbRep $name - $@");
  3375. return "$name|''|''|$err";
  3376. }
  3377. # check ob PK verwendet wird, @usepkx?Anzahl der Felder im PK:0 wenn kein PK, $pkx?Namen der Felder:none wenn kein PK
  3378. my ($usepkh,$usepkc,$pkh,$pkc) = DbRep_checkUsePK($hash,$dbloghash,$dbh);
  3379. my $i_timestamp = $hash->{HELPER}{I_TIMESTAMP};
  3380. my $i_device = $hash->{HELPER}{I_DEVICE};
  3381. my $i_type = $hash->{HELPER}{I_TYPE};
  3382. my $i_event = $hash->{HELPER}{I_EVENT};
  3383. my $i_reading = $hash->{HELPER}{I_READING};
  3384. my $i_value = $hash->{HELPER}{I_VALUE};
  3385. my $i_unit = $hash->{HELPER}{I_UNIT} ? $hash->{HELPER}{I_UNIT} : " ";
  3386. # SQL zusammenstellen für DB-Operation
  3387. Log3 ($name, 5, "DbRep $name -> data to insert Timestamp: $i_timestamp, Device: $i_device, Type: $i_type, Event: $i_event, Reading: $i_reading, Value: $i_value, Unit: $i_unit");
  3388. # SQL-Startzeit
  3389. my $st = [gettimeofday];
  3390. # insert history mit/ohne primary key
  3391. if ($usepkh && $dbloghash->{MODEL} eq 'MYSQL') {
  3392. eval { $sth = $dbh->prepare("INSERT IGNORE INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  3393. } elsif ($usepkh && $dbloghash->{MODEL} eq 'SQLITE') {
  3394. eval { $sth = $dbh->prepare("INSERT OR IGNORE INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  3395. } elsif ($usepkh && $dbloghash->{MODEL} eq 'POSTGRESQL') {
  3396. eval { $sth = $dbh->prepare("INSERT INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?) ON CONFLICT DO NOTHING"); };
  3397. } else {
  3398. eval { $sth = $dbh->prepare("INSERT INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  3399. }
  3400. if ($@) {
  3401. $err = encode_base64($@,"");
  3402. Log3 ($name, 2, "DbRep $name - $@");
  3403. $dbh->disconnect();
  3404. return "$name|''|''|$err";
  3405. }
  3406. $dbh->begin_work();
  3407. eval {$sth->execute($i_timestamp, $i_device, $i_type, $i_event, $i_reading, $i_value, $i_unit);};
  3408. my $irow;
  3409. if ($@) {
  3410. $err = encode_base64($@,"");
  3411. Log3 ($name, 2, "DbRep $name - Insert new dataset into database failed".($usepkh?" (possible PK violation) ":": ")."$@");
  3412. $dbh->rollback();
  3413. $dbh->disconnect();
  3414. return "$name|''|''|$err";
  3415. } else {
  3416. $dbh->commit();
  3417. $irow = $sth->rows;
  3418. $dbh->disconnect();
  3419. }
  3420. # SQL-Laufzeit ermitteln
  3421. my $rt = tv_interval($st);
  3422. # Background-Laufzeit ermitteln
  3423. my $brt = tv_interval($bst);
  3424. $rt = $rt.",".$brt;
  3425. return "$name|$irow|$rt|0";
  3426. }
  3427. ####################################################################################################
  3428. # Auswertungsroutine DB insert
  3429. ####################################################################################################
  3430. sub insert_Done($) {
  3431. my ($string) = @_;
  3432. my @a = split("\\|",$string);
  3433. my $hash = $defs{$a[0]};
  3434. my $name = $hash->{NAME};
  3435. my $irow = $a[1];
  3436. my $bt = $a[2];
  3437. my ($rt,$brt) = split(",", $bt);
  3438. my $err = $a[3]?decode_base64($a[3]):undef;
  3439. my $i_timestamp = delete $hash->{HELPER}{I_TIMESTAMP};
  3440. my $i_device = delete $hash->{HELPER}{I_DEVICE};
  3441. my $i_type = delete $hash->{HELPER}{I_TYPE};
  3442. my $i_event = delete $hash->{HELPER}{I_EVENT};
  3443. my $i_reading = delete $hash->{HELPER}{I_READING};
  3444. my $i_value = delete $hash->{HELPER}{I_VALUE};
  3445. my $i_unit = delete $hash->{HELPER}{I_UNIT};
  3446. if ($err) {
  3447. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  3448. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  3449. delete($hash->{HELPER}{RUNNING_PID});
  3450. return;
  3451. }
  3452. # only for this block because of warnings if details of readings are not set
  3453. no warnings 'uninitialized';
  3454. readingsBeginUpdate($hash);
  3455. ReadingsBulkUpdateValue ($hash, "number_lines_inserted", $irow);
  3456. ReadingsBulkUpdateValue ($hash, "data_inserted", $i_timestamp.", ".$i_device.", ".$i_type.", ".$i_event.", ".$i_reading.", ".$i_value.", ".$i_unit);
  3457. ReadingsBulkUpdateTimeState($hash,$brt,$rt,"done");
  3458. readingsEndUpdate($hash, 1);
  3459. Log3 ($name, 5, "DbRep $name - Inserted into database $hash->{DATABASE} table 'history': Timestamp: $i_timestamp, Device: $i_device, Type: $i_type, Event: $i_event, Reading: $i_reading, Value: $i_value, Unit: $i_unit");
  3460. delete($hash->{HELPER}{RUNNING_PID});
  3461. return;
  3462. }
  3463. ####################################################################################################
  3464. # Current-Tabelle mit Device,Reading Kombinationen aus history auffüllen
  3465. ####################################################################################################
  3466. sub currentfillup_Push($) {
  3467. my ($string) = @_;
  3468. my ($name,$device,$reading,$runtime_string_first,$runtime_string_next) = split("\\|", $string);
  3469. my $hash = $defs{$name};
  3470. my $dbloghash = $hash->{dbloghash};
  3471. my $dbconn = $dbloghash->{dbconn};
  3472. my $dbuser = $dbloghash->{dbuser};
  3473. my $dblogname = $dbloghash->{NAME};
  3474. my $dbpassword = $attr{"sec$dblogname"}{secret};
  3475. my $utf8 = defined($hash->{UTF8})?$hash->{UTF8}:0;
  3476. my ($err,$sth,$sql,$devs,$danz,$ranz);
  3477. # Background-Startzeit
  3478. my $bst = [gettimeofday];
  3479. my $dbh;
  3480. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoCommit => 1, AutoInactiveDestroy => 1, mysql_enable_utf8 => $utf8 });};
  3481. if ($@) {
  3482. $err = encode_base64($@,"");
  3483. Log3 ($name, 2, "DbRep $name - $@");
  3484. return "$name|''|''|$err|''|''";
  3485. }
  3486. # check ob PK verwendet wird, @usepkx?Anzahl der Felder im PK:0 wenn kein PK, $pkx?Namen der Felder:none wenn kein PK
  3487. my ($usepkh,$usepkc,$pkh,$pkc) = DbRep_checkUsePK($hash,$dbloghash,$dbh);
  3488. # ist Zeiteingrenzung und/oder Aggregation gesetzt ? (wenn ja -> "?" in SQL sonst undef)
  3489. my ($IsTimeSet,$IsAggrSet) = DbRep_checktimeaggr($hash);
  3490. Log3 ($name, 5, "DbRep $name - IsTimeSet: $IsTimeSet, IsAggrSet: $IsAggrSet");
  3491. ($devs,$danz,$reading,$ranz) = DbRep_specsForSql($hash,$device,$reading);
  3492. # SQL-Startzeit
  3493. my $st = [gettimeofday];
  3494. # insert history mit/ohne primary key
  3495. # SQL zusammenstellen für DB-Operation
  3496. if ($usepkc && $dbloghash->{MODEL} eq 'MYSQL') {
  3497. $sql = "INSERT IGNORE INTO current (TIMESTAMP,DEVICE,READING) SELECT timestamp,device,reading FROM history where ";
  3498. $sql .= "DEVICE LIKE '$devs' AND " if($danz <= 1 && $devs !~ m(^%$) && $devs =~ m(\%));
  3499. $sql .= "DEVICE = '$devs' AND " if($danz <= 1 && $devs !~ m(\%));
  3500. $sql .= "DEVICE IN ($devs) AND " if($danz > 1);
  3501. $sql .= "READING LIKE '$reading' AND " if($ranz <= 1 && $reading !~ m(^%$) && $reading =~ m(\%));
  3502. $sql .= "READING = '$reading' AND " if($ranz <= 1 && $reading !~ m(\%));
  3503. $sql .= "READING IN ($reading) AND " if($ranz > 1);
  3504. if ($IsTimeSet) {
  3505. $sql .= "TIMESTAMP >= '$runtime_string_first' AND TIMESTAMP < '$runtime_string_next' ";
  3506. } else {
  3507. $sql .= "1 ";
  3508. }
  3509. $sql .= "group by timestamp,device,reading ;";
  3510. } elsif ($usepkc && $dbloghash->{MODEL} eq 'SQLITE') {
  3511. $sql = "INSERT OR IGNORE INTO current (TIMESTAMP,DEVICE,READING) SELECT timestamp,device,reading FROM history where ";
  3512. $sql .= "DEVICE LIKE '$devs' AND " if($danz <= 1 && $devs !~ m(^%$) && $devs =~ m(\%));
  3513. $sql .= "DEVICE = '$devs' AND " if($danz <= 1 && $devs !~ m(\%));
  3514. $sql .= "DEVICE IN ($devs) AND " if($danz > 1);
  3515. $sql .= "READING LIKE '$reading' AND " if($ranz <= 1 && $reading !~ m(^%$) && $reading =~ m(\%));
  3516. $sql .= "READING = '$reading' AND " if($ranz <= 1 && $reading !~ m(\%));
  3517. $sql .= "READING IN ($reading) AND " if($ranz > 1);
  3518. if ($IsTimeSet) {
  3519. $sql .= "TIMESTAMP >= '$runtime_string_first' AND TIMESTAMP < '$runtime_string_next' ";
  3520. } else {
  3521. $sql .= "1 ";
  3522. }
  3523. $sql .= "group by timestamp,device,reading ;";
  3524. } elsif ($usepkc && $dbloghash->{MODEL} eq 'POSTGRESQL') {
  3525. $sql = "INSERT INTO current (DEVICE,TIMESTAMP,READING) SELECT device, (array_agg(timestamp ORDER BY reading ASC))[1], reading FROM history where ";
  3526. $sql .= "DEVICE LIKE '$devs' AND " if($danz <= 1 && $devs !~ m(^%$) && $devs =~ m(\%));
  3527. $sql .= "DEVICE = '$devs' AND " if($danz <= 1 && $devs !~ m(\%));
  3528. $sql .= "DEVICE IN ($devs) AND " if($danz > 1);
  3529. $sql .= "READING LIKE '$reading' AND " if($ranz <= 1 && $reading !~ m(^%$) && $reading =~ m(\%));
  3530. $sql .= "READING = '$reading' AND " if($ranz <= 1 && $reading !~ m(\%));
  3531. $sql .= "READING IN ($reading) AND " if($ranz > 1);
  3532. if ($IsTimeSet) {
  3533. $sql .= "TIMESTAMP >= '$runtime_string_first' AND TIMESTAMP < '$runtime_string_next' ";
  3534. } else {
  3535. $sql .= "true ";
  3536. }
  3537. $sql .= "group by device,reading ON CONFLICT ($pkc) DO NOTHING; ";
  3538. } else {
  3539. if($dbloghash->{MODEL} ne 'POSTGRESQL') {
  3540. # MySQL und SQLite
  3541. $sql = "INSERT INTO current (TIMESTAMP,DEVICE,READING) SELECT timestamp,device,reading FROM history where ";
  3542. $sql .= "DEVICE LIKE '$devs' AND " if($danz <= 1 && $devs !~ m(^%$) && $devs =~ m(\%));
  3543. $sql .= "DEVICE = '$devs' AND " if($danz <= 1 && $devs !~ m(\%));
  3544. $sql .= "DEVICE IN ($devs) AND " if($danz > 1);
  3545. $sql .= "READING LIKE '$reading' AND " if($ranz <= 1 && $reading !~ m(^%$) && $reading =~ m(\%));
  3546. $sql .= "READING = '$reading' AND " if($ranz <= 1 && $reading !~ m(\%));
  3547. $sql .= "READING IN ($reading) AND " if($ranz > 1);
  3548. if ($IsTimeSet) {
  3549. $sql .= "TIMESTAMP >= '$runtime_string_first' AND TIMESTAMP < '$runtime_string_next' ";
  3550. } else {
  3551. $sql .= "1 ";
  3552. }
  3553. $sql .= "group by device,reading ;";
  3554. } else {
  3555. # PostgreSQL
  3556. $sql = "INSERT INTO current (DEVICE,TIMESTAMP,READING) SELECT device, (array_agg(timestamp ORDER BY reading ASC))[1], reading FROM history where ";
  3557. $sql .= "DEVICE LIKE '$devs' AND " if($danz <= 1 && $devs !~ m(^%$) && $devs =~ m(\%));
  3558. $sql .= "DEVICE = '$devs' AND " if($danz <= 1 && $devs !~ m(\%));
  3559. $sql .= "DEVICE IN ($devs) AND " if($danz > 1);
  3560. $sql .= "READING LIKE '$reading' AND " if($ranz <= 1 && $reading !~ m(^%$) && $reading =~ m(\%));
  3561. $sql .= "READING = '$reading' AND " if($ranz <= 1 && $reading !~ m(\%));
  3562. $sql .= "READING IN ($reading) AND " if($ranz > 1);
  3563. if ($IsTimeSet) {
  3564. $sql .= "TIMESTAMP >= '$runtime_string_first' AND TIMESTAMP < '$runtime_string_next' ";
  3565. } else {
  3566. $sql .= "true ";
  3567. }
  3568. $sql .= "group by device,reading;";
  3569. }
  3570. }
  3571. # Log SQL Statement
  3572. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  3573. eval { $sth = $dbh->prepare($sql); };
  3574. if ($@) {
  3575. $err = encode_base64($@,"");
  3576. Log3 ($name, 2, "DbRep $name - $@");
  3577. $dbh->disconnect();
  3578. return "$name|''|''|$err|''|''";
  3579. }
  3580. my $irow;
  3581. $dbh->begin_work();
  3582. eval {$sth->execute();};
  3583. if ($@) {
  3584. $err = encode_base64($@,"");
  3585. Log3 ($name, 2, "DbRep $name - Insert new dataset into database failed".($usepkh?" (possible PK violation) ":": ")."$@");
  3586. $dbh->rollback();
  3587. $dbh->disconnect();
  3588. return "$name|''|''|$err|''|''";
  3589. } else {
  3590. $dbh->commit();
  3591. $irow = $sth->rows;
  3592. $dbh->disconnect();
  3593. }
  3594. # SQL-Laufzeit ermitteln
  3595. my $rt = tv_interval($st);
  3596. # Background-Laufzeit ermitteln
  3597. my $brt = tv_interval($bst);
  3598. $rt = $rt.",".$brt;
  3599. return "$name|$irow|$rt|0|$device|$reading";
  3600. }
  3601. ####################################################################################################
  3602. # Auswertungsroutine Current-Tabelle auffüllen
  3603. ####################################################################################################
  3604. sub currentfillup_Done($) {
  3605. my ($string) = @_;
  3606. my @a = split("\\|",$string);
  3607. my $hash = $defs{$a[0]};
  3608. my $name = $hash->{NAME};
  3609. my $irow = $a[1];
  3610. my $bt = $a[2];
  3611. my ($rt,$brt) = split(",", $bt);
  3612. my $err = $a[3]?decode_base64($a[3]):undef;
  3613. my $device = $a[4];
  3614. my $reading = $a[5];
  3615. undef $device if ($device =~ m(^%$));
  3616. undef $reading if ($reading =~ m(^%$));
  3617. if ($err) {
  3618. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  3619. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  3620. delete($hash->{HELPER}{RUNNING_PID});
  3621. return;
  3622. }
  3623. # only for this block because of warnings if details of readings are not set
  3624. no warnings 'uninitialized';
  3625. my $rowstr;
  3626. $rowstr = $irow if(!$device && !$reading);
  3627. $rowstr = $irow." - limited by device: ".$device if($device && !$reading);
  3628. $rowstr = $irow." - limited by reading: ".$reading if(!$device && $reading);
  3629. $rowstr = $irow." - limited by device: ".$device." and reading: ".$reading if($device && $reading);
  3630. readingsBeginUpdate($hash);
  3631. ReadingsBulkUpdateValue($hash, "number_lines_inserted", $rowstr);
  3632. ReadingsBulkUpdateTimeState($hash,$brt,$rt,"done");
  3633. readingsEndUpdate($hash, 1);
  3634. Log3 ($name, 3, "DbRep $name - Table '$hash->{DATABASE}'.'current' filled up with rows: $rowstr");
  3635. delete($hash->{HELPER}{RUNNING_PID});
  3636. return;
  3637. }
  3638. ####################################################################################################
  3639. # nichtblockierendes DB deviceRename / readingRename
  3640. ####################################################################################################
  3641. sub change_Push($) {
  3642. my ($string) = @_;
  3643. my ($name,$device,$reading,$runtime_string_first,$runtime_string_next) = split("\\|", $string);
  3644. my $hash = $defs{$name};
  3645. my $dbloghash = $hash->{dbloghash};
  3646. my $dbconn = $dbloghash->{dbconn};
  3647. my $dbuser = $dbloghash->{dbuser};
  3648. my $dblogname = $dbloghash->{NAME};
  3649. my $dbpassword = $attr{"sec$dblogname"}{secret};
  3650. my $table = "history";
  3651. my ($dbh,$err,$sql);
  3652. # Background-Startzeit
  3653. my $bst = [gettimeofday];
  3654. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoCommit => 1, AutoInactiveDestroy => 1 });};
  3655. if ($@) {
  3656. $err = encode_base64($@,"");
  3657. Log3 ($name, 2, "DbRep $name - $@");
  3658. return "$name|''|''|$err";
  3659. }
  3660. my $renmode = $hash->{HELPER}{RENMODE};
  3661. # SQL-Startzeit
  3662. my $st = [gettimeofday];
  3663. my ($sth,$old,$new);
  3664. eval { $dbh->begin_work() if($dbh->{AutoCommit}); }; # Transaktion wenn gewünscht und autocommit ein
  3665. if ($@) {
  3666. Log3($name, 2, "DbRep $name -> Error start transaction - $@");
  3667. }
  3668. if ($renmode eq "devren") {
  3669. $old = delete $hash->{HELPER}{OLDDEV};
  3670. $new = delete $hash->{HELPER}{NEWDEV};
  3671. # SQL zusammenstellen für DB-Operation
  3672. Log3 ($name, 5, "DbRep $name -> Rename old device name \"$old\" to new device name \"$new\" in database $dblogname ");
  3673. # prepare DB operation
  3674. $old =~ s/'/''/g; # escape ' with ''
  3675. $new =~ s/'/''/g; # escape ' with ''
  3676. $sql = "UPDATE history SET TIMESTAMP=TIMESTAMP,DEVICE='$new' WHERE DEVICE='$old'; ";
  3677. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  3678. $sth = $dbh->prepare($sql) ;
  3679. } elsif ($renmode eq "readren") {
  3680. $old = delete $hash->{HELPER}{OLDREAD};
  3681. $new = delete $hash->{HELPER}{NEWREAD};
  3682. # SQL zusammenstellen für DB-Operation
  3683. Log3 ($name, 5, "DbRep $name -> Rename old reading name \"$old\" to new reading name \"$new\" in database $dblogname ");
  3684. # prepare DB operation
  3685. $old =~ s/'/''/g; # escape ' with ''
  3686. $new =~ s/'/''/g; # escape ' with ''
  3687. $sql = "UPDATE history SET TIMESTAMP=TIMESTAMP,READING='$new' WHERE READING='$old'; ";
  3688. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  3689. $sth = $dbh->prepare($sql) ;
  3690. }
  3691. $old =~ s/''/'/g; # escape back
  3692. $new =~ s/''/'/g; # escape back
  3693. my $urow;
  3694. eval { $sth->execute(); };
  3695. if ($@) {
  3696. $err = encode_base64($@,"");
  3697. my $m = ($renmode eq "devren")?"device":"reading";
  3698. Log3 ($name, 2, "DbRep $name - Failed to rename old $m name \"$old\" to new $m name \"$new\": $@");
  3699. $dbh->rollback() if(!$dbh->{AutoCommit});
  3700. $dbh->disconnect();
  3701. return "$name|''|''|$err";
  3702. } else {
  3703. $dbh->commit() if(!$dbh->{AutoCommit});
  3704. $urow = $sth->rows;
  3705. $dbh->disconnect();
  3706. }
  3707. # SQL-Laufzeit ermitteln
  3708. my $rt = tv_interval($st);
  3709. # Background-Laufzeit ermitteln
  3710. my $brt = tv_interval($bst);
  3711. $rt = $rt.",".$brt;
  3712. return "$name|$urow|$rt|0|$old|$new";
  3713. }
  3714. ####################################################################################################
  3715. # nichtblockierendes DB changeValue (Field VALUE)
  3716. ####################################################################################################
  3717. sub changeval_Push($) {
  3718. my ($string) = @_;
  3719. my ($name,$device,$reading,$runtime_string_first,$runtime_string_next,$ts) = split("\\§", $string);
  3720. my $hash = $defs{$name};
  3721. my $dbloghash = $hash->{dbloghash};
  3722. my $dbconn = $dbloghash->{dbconn};
  3723. my $dbuser = $dbloghash->{dbuser};
  3724. my $dblogname = $dbloghash->{NAME};
  3725. my $dbpassword = $attr{"sec$dblogname"}{secret};
  3726. my $table = "history";
  3727. my $complex = $hash->{HELPER}{COMPLEX}; # einfache oder komplexe Werteersetzung
  3728. my ($dbh,$err,$sql,$urow);
  3729. # Background-Startzeit
  3730. my $bst = [gettimeofday];
  3731. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoCommit => 1, AutoInactiveDestroy => 1 });};
  3732. if ($@) {
  3733. $err = encode_base64($@,"");
  3734. Log3 ($name, 2, "DbRep $name - $@");
  3735. return "$name|''|''|$err";
  3736. }
  3737. # ist Zeiteingrenzung und/oder Aggregation gesetzt ? (wenn ja -> "?" in SQL sonst undef)
  3738. my ($IsTimeSet,$IsAggrSet) = DbRep_checktimeaggr($hash);
  3739. Log3 ($name, 5, "DbRep $name - IsTimeSet: $IsTimeSet, IsAggrSet: $IsAggrSet");
  3740. # SQL-Startzeit
  3741. my $st = [gettimeofday];
  3742. my ($sth,$old,$new);
  3743. eval { $dbh->begin_work() if($dbh->{AutoCommit}); }; # Transaktion wenn gewünscht und autocommit ein
  3744. if ($@) {
  3745. Log3($name, 2, "DbRep $name -> Error start transaction - $@");
  3746. }
  3747. if (!$complex) {
  3748. $old = delete $hash->{HELPER}{OLDVAL};
  3749. $new = delete $hash->{HELPER}{NEWVAL};
  3750. # SQL zusammenstellen für DB-Operation
  3751. Log3 ($name, 5, "DbRep $name -> Change old value \"$old\" to new value \"$new\" in database $dblogname ");
  3752. # prepare DB operation
  3753. $old =~ s/'/''/g; # escape ' with ''
  3754. $new =~ s/'/''/g; # escape ' with ''
  3755. # SQL zusammenstellen für DB-Update
  3756. my $addon = $old =~ /%/?"WHERE VALUE LIKE '$old'":"WHERE VALUE='$old'";
  3757. if ($IsTimeSet) {
  3758. $sql = DbRep_createUpdateSql($hash,$table,"TIMESTAMP=TIMESTAMP,VALUE='$new' $addon",$device,$reading,"'$runtime_string_first'","'$runtime_string_next'",'');
  3759. } else {
  3760. $sql = DbRep_createUpdateSql($hash,$table,"TIMESTAMP=TIMESTAMP,VALUE='$new' $addon",$device,$reading,undef,undef,'');
  3761. }
  3762. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  3763. $sth = $dbh->prepare($sql) ;
  3764. $old =~ s/''/'/g; # escape back
  3765. $new =~ s/''/'/g; # escape back
  3766. eval { $sth->execute(); };
  3767. if ($@) {
  3768. $err = encode_base64($@,"");
  3769. Log3 ($name, 2, "DbRep $name - Failed to change old value \"$old\" to new value \"$new\": $@");
  3770. $dbh->rollback() if(!$dbh->{AutoCommit});
  3771. $dbh->disconnect();
  3772. return "$name|''|''|$err";
  3773. } else {
  3774. $dbh->commit() if(!$dbh->{AutoCommit});
  3775. $urow = $sth->rows;
  3776. }
  3777. } else {
  3778. $old = delete $hash->{HELPER}{OLDVAL};
  3779. $new = delete $hash->{HELPER}{NEWVAL};
  3780. $old =~ s/'/''/g; # escape ' with ''
  3781. # Timestampstring to Array
  3782. my @ts = split("\\|", $ts);
  3783. Log3 ($name, 5, "DbRep $name - Timestamp-Array: \n@ts");
  3784. # DB-Abfrage zeilenweise für jeden Array-Eintrag
  3785. $urow = 0;
  3786. my $selspec = "DEVICE,READING,TIMESTAMP,VALUE,UNIT";
  3787. my $addon = $old =~ /%/?"AND VALUE LIKE '$old'":"AND VALUE='$old'";
  3788. foreach my $row (@ts) {
  3789. my @a = split("#", $row);
  3790. my $runtime_string = $a[0];
  3791. my $runtime_string_first = $a[1];
  3792. my $runtime_string_next = $a[2];
  3793. if ($IsTimeSet || $IsAggrSet) {
  3794. $sql = DbRep_createSelectSql($hash,"history",$selspec,$device,$reading,"'$runtime_string_first'","'$runtime_string_next'",$addon);
  3795. } else {
  3796. $sql = DbRep_createSelectSql($hash,"history",$selspec,$device,$reading,undef,undef,$addon);
  3797. }
  3798. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  3799. eval{ $sth = $dbh->prepare($sql);
  3800. $sth->execute();
  3801. };
  3802. if ($@) {
  3803. $err = encode_base64($@,"");
  3804. Log3 ($name, 2, "DbRep $name - $@");
  3805. $dbh->disconnect;
  3806. return "$name|''|''|$err";
  3807. }
  3808. no warnings 'uninitialized';
  3809. # DEVICE _ESC_ READING _ESC_ DATE _ESC_ TIME _ESC_ VALUE _ESC_ UNIT
  3810. my @row_array = map { $_->[0]."_ESC_".$_->[1]."_ESC_".($_->[2] =~ s/ /_ESC_/r)."_ESC_".$_->[3]."_ESC_".$_->[4]."\n" } @{$sth->fetchall_arrayref()};
  3811. use warnings;
  3812. Log3 ($name, 4, "DbRep $name - Now change values of selected array ... ");
  3813. foreach my $upd (@row_array) {
  3814. # für jeden selektierten (zu ändernden) Datensatz Userfunktion anwenden und updaten
  3815. my ($device,$reading,$date,$time,$value,$unit) = ($upd =~ /^(.*)_ESC_(.*)_ESC_(.*)_ESC_(.*)_ESC_(.*)_ESC_(.*)$/);
  3816. my $oval = $value; # Selektkriterium für Update alter Valuewert
  3817. my $VALUE = $value;
  3818. my $UNIT = $unit;
  3819. eval $new;
  3820. if ($@) {
  3821. $err = encode_base64($@,"");
  3822. Log3 ($name, 2, "DbRep $name - $@");
  3823. $dbh->disconnect;
  3824. return "$name|''|''|$err";
  3825. }
  3826. $value = $VALUE if(defined $VALUE);
  3827. $unit = $UNIT if(defined $UNIT);
  3828. # Daten auf maximale Länge beschneiden (DbLog-Funktion !)
  3829. (undef,undef,undef,undef,$value,$unit) = DbLog_cutCol($hash->{dbloghash},"1","1","1","1",$value,$unit);
  3830. $value =~ s/'/''/g; # escape ' with ''
  3831. $unit =~ s/'/''/g; # escape ' with ''
  3832. # SQL zusammenstellen für DB-Update
  3833. $sql = "UPDATE history SET TIMESTAMP=TIMESTAMP,VALUE='$value',UNIT='$unit' WHERE TIMESTAMP = '$date $time' AND DEVICE = '$device' AND READING = '$reading' AND VALUE='$oval'";
  3834. Log3 ($name, 5, "DbRep $name - SQL execute: $sql");
  3835. $sth = $dbh->prepare($sql) ;
  3836. $value =~ s/''/'/g; # escape back
  3837. $unit =~ s/''/'/g; # escape back
  3838. eval { $sth->execute(); };
  3839. if ($@) {
  3840. $err = encode_base64($@,"");
  3841. Log3 ($name, 2, "DbRep $name - Failed to change old value \"$old\" to new value \"$new\": $@");
  3842. $dbh->rollback() if(!$dbh->{AutoCommit});
  3843. $dbh->disconnect();
  3844. return "$name|''|''|$err";
  3845. } else {
  3846. $dbh->commit() if(!$dbh->{AutoCommit});
  3847. $urow++;
  3848. }
  3849. }
  3850. }
  3851. }
  3852. $dbh->disconnect();
  3853. # SQL-Laufzeit ermitteln
  3854. my $rt = tv_interval($st);
  3855. # Background-Laufzeit ermitteln
  3856. my $brt = tv_interval($bst);
  3857. $rt = $rt.",".$brt;
  3858. return "$name|$urow|$rt|0|$old|$new";
  3859. }
  3860. ####################################################################################################
  3861. # Auswertungsroutine DB deviceRename/readingRename/changeValue
  3862. ####################################################################################################
  3863. sub change_Done($) {
  3864. my ($string) = @_;
  3865. my @a = split("\\|",$string);
  3866. my $hash = $defs{$a[0]};
  3867. my $name = $hash->{NAME};
  3868. my $urow = $a[1];
  3869. my $bt = $a[2];
  3870. my ($rt,$brt) = split(",", $bt);
  3871. my $err = $a[3]?decode_base64($a[3]):undef;
  3872. my $old = $a[4];
  3873. my $new = $a[5];
  3874. my $renmode = delete $hash->{HELPER}{RENMODE};
  3875. # Befehl nach Procedure ausführen
  3876. my $erread = DbRep_afterproc($hash, $renmode);
  3877. if ($err) {
  3878. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  3879. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  3880. delete($hash->{HELPER}{RUNNING_PID});
  3881. return;
  3882. }
  3883. # only for this block because of warnings if details of readings are not set
  3884. no warnings 'uninitialized';
  3885. readingsBeginUpdate($hash);
  3886. ReadingsBulkUpdateValue ($hash, "number_lines_updated", $urow);
  3887. if($renmode eq "devren") {
  3888. ReadingsBulkUpdateValue ($hash, "device_renamed", "old: ".$old." to new: ".$new) if($urow != 0);
  3889. ReadingsBulkUpdateValue ($hash, "device_not_renamed", "Warning - old: ".$old." not found, not renamed to new: ".$new)
  3890. if($urow == 0);
  3891. }
  3892. if($renmode eq "readren") {
  3893. ReadingsBulkUpdateValue ($hash, "reading_renamed", "old: ".$old." to new: ".$new) if($urow != 0);
  3894. ReadingsBulkUpdateValue ($hash, "reading_not_renamed", "Warning - old: ".$old." not found, not renamed to new: ".$new)
  3895. if ($urow == 0);
  3896. }
  3897. if($renmode eq "changeval") {
  3898. ReadingsBulkUpdateValue ($hash, "value_changed", "old: ".$old." to new: ".$new) if($urow != 0);
  3899. ReadingsBulkUpdateValue ($hash, "value_not_changed", "Warning - old: ".$old." not found, not changed to new: ".$new)
  3900. if ($urow == 0);
  3901. }
  3902. ReadingsBulkUpdateTimeState($hash,$brt,$rt,"done");
  3903. readingsEndUpdate($hash, 1);
  3904. if ($urow != 0) {
  3905. Log3 ($name, 3, "DbRep ".(($hash->{ROLE} eq "Agent")?"Agent ":"")."$name - DEVICE renamed in \"$hash->{DATABASE}\", old: \"$old\", new: \"$new\", number: $urow ") if($renmode eq "devren");
  3906. Log3 ($name, 3, "DbRep ".(($hash->{ROLE} eq "Agent")?"Agent ":"")."$name - READING renamed in \"$hash->{DATABASE}\", old: \"$old\", new: \"$new\", number: $urow ") if($renmode eq "readren");
  3907. Log3 ($name, 3, "DbRep ".(($hash->{ROLE} eq "Agent")?"Agent ":"")."$name - VALUE changed in \"$hash->{DATABASE}\", old: \"$old\", new: \"$new\", number: $urow ") if($renmode eq "changeval");
  3908. } else {
  3909. Log3 ($name, 3, "DbRep ".(($hash->{ROLE} eq "Agent")?"Agent ":"")."$name - WARNING - old device \"$old\" was not found in database \"$hash->{DATABASE}\" ") if($renmode eq "devren");
  3910. Log3 ($name, 3, "DbRep ".(($hash->{ROLE} eq "Agent")?"Agent ":"")."$name - WARNING - old reading \"$old\" was not found in database \"$hash->{DATABASE}\" ") if($renmode eq "readren");
  3911. Log3 ($name, 3, "DbRep ".(($hash->{ROLE} eq "Agent")?"Agent ":"")."$name - WARNING - old value \"$old\" not found in database \"$hash->{DATABASE}\" ") if($renmode eq "changeval");
  3912. }
  3913. delete($hash->{HELPER}{RUNNING_PID});
  3914. return;
  3915. }
  3916. ####################################################################################################
  3917. # nichtblockierende DB-Abfrage fetchrows
  3918. ####################################################################################################
  3919. sub fetchrows_DoParse($) {
  3920. my ($string) = @_;
  3921. my ($name,$table,$device,$reading,$runtime_string_first,$runtime_string_next) = split("\\|", $string);
  3922. my $hash = $defs{$name};
  3923. my $dbloghash = $hash->{dbloghash};
  3924. my $dbconn = $dbloghash->{dbconn};
  3925. my $dbuser = $dbloghash->{dbuser};
  3926. my $dblogname = $dbloghash->{NAME};
  3927. my $dbpassword = $attr{"sec$dblogname"}{secret};
  3928. my $limit = AttrVal($name, "limit", 1000);
  3929. my $utf8 = defined($hash->{UTF8})?$hash->{UTF8}:0;
  3930. my $fetchroute = AttrVal($name, "fetchRoute", "descent");
  3931. $fetchroute = ($fetchroute eq "descent")?"DESC":"ASC";
  3932. my ($err,$dbh,$sth,$sql,$rowlist,$nrows);
  3933. # Background-Startzeit
  3934. my $bst = [gettimeofday];
  3935. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1, mysql_enable_utf8 => $utf8 });};
  3936. if ($@) {
  3937. $err = encode_base64($@,"");
  3938. Log3 ($name, 2, "DbRep $name - $@");
  3939. return "$name|''|''|$err|''";
  3940. }
  3941. # ist Zeiteingrenzung und/oder Aggregation gesetzt ? (wenn ja -> "?" in SQL sonst undef)
  3942. my ($IsTimeSet,$IsAggrSet) = DbRep_checktimeaggr($hash);
  3943. Log3 ($name, 5, "DbRep $name - IsTimeSet: $IsTimeSet, IsAggrSet: $IsAggrSet");
  3944. # SQL zusammenstellen für DB-Abfrage
  3945. if ($IsTimeSet) {
  3946. $sql = DbRep_createSelectSql($hash,$table,"DEVICE,READING,TIMESTAMP,VALUE,UNIT",$device,$reading,"'$runtime_string_first'","'$runtime_string_next'","ORDER BY TIMESTAMP $fetchroute LIMIT ".($limit+1));
  3947. } else {
  3948. $sql = DbRep_createSelectSql($hash,$table,"DEVICE,READING,TIMESTAMP,VALUE,UNIT",$device,$reading,undef,undef,"ORDER BY TIMESTAMP $fetchroute LIMIT ".($limit+1));
  3949. }
  3950. $sth = $dbh->prepare($sql);
  3951. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  3952. # SQL-Startzeit
  3953. my $st = [gettimeofday];
  3954. eval{$sth->execute();};
  3955. if ($@) {
  3956. $err = encode_base64($@,"");
  3957. Log3 ($name, 2, "DbRep $name - $@");
  3958. $dbh->disconnect;
  3959. return "$name|''|''|$err|''";
  3960. }
  3961. no warnings 'uninitialized';
  3962. my @row_array = map { $_->[0]."_ESC_".$_->[1]."_ESC_".($_->[2] =~ s/ /_ESC_/r)."_ESC_".$_->[3]."_ESC_".$_->[4]."\n" } @{$sth->fetchall_arrayref()};
  3963. use warnings;
  3964. $nrows = $#row_array+1; # Anzahl der Ergebniselemente
  3965. pop @row_array if($nrows>$limit); # das zuviel selektierte Element wegpoppen wenn Limit überschritten
  3966. s/\|/_E#S#C_/g for @row_array; # escape Pipe "|"
  3967. if ($utf8) {
  3968. $rowlist = Encode::encode_utf8(join('|', @row_array));
  3969. } else {
  3970. $rowlist = join('|', @row_array);
  3971. }
  3972. Log3 ($name, 5, "DbRep $name -> row result list:\n$rowlist");
  3973. # SQL-Laufzeit ermitteln
  3974. my $rt = tv_interval($st);
  3975. $dbh->disconnect;
  3976. # Daten müssen als Einzeiler zurückgegeben werden
  3977. $rowlist = encode_base64($rowlist,"");
  3978. # Background-Laufzeit ermitteln
  3979. my $brt = tv_interval($bst);
  3980. $rt = $rt.",".$brt;
  3981. return "$name|$rowlist|$rt|0|$nrows";
  3982. }
  3983. ####################################################################################################
  3984. # Auswertungsroutine der nichtblockierenden DB-Abfrage fetchrows
  3985. ####################################################################################################
  3986. sub fetchrows_ParseDone($) {
  3987. my ($string) = @_;
  3988. my @a = split("\\|",$string);
  3989. my $hash = $defs{$a[0]};
  3990. my $rowlist = decode_base64($a[1]);
  3991. my $bt = $a[2];
  3992. my ($rt,$brt) = split(",", $bt);
  3993. my $err = $a[3]?decode_base64($a[3]):undef;
  3994. my $nrows = $a[4];
  3995. my $name = $hash->{NAME};
  3996. my $reading = AttrVal($name, "reading", undef);
  3997. my $limit = AttrVal($name, "limit", 1000);
  3998. my $color = "<html><span style=\"color: #".AttrVal($name, "fetchMarkDuplicates", "000000").";\">"; # Highlighting doppelter DB-Einträge
  3999. $color =~ s/#// if($color =~ /red|blue|brown|green|orange/);
  4000. my $ecolor = "</span></html>"; # Ende Highlighting
  4001. my @row;
  4002. my $reading_runtime_string;
  4003. if ($err) {
  4004. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  4005. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  4006. delete($hash->{HELPER}{RUNNING_PID});
  4007. return;
  4008. }
  4009. my @row_array = split("\\|", $rowlist);
  4010. s/_E#S#C_/\|/g for @row_array; # escaped Pipe return to "|"
  4011. Log3 ($name, 5, "DbRep $name - row_array decoded:\n @row_array");
  4012. # Readingaufbereitung
  4013. readingsBeginUpdate($hash);
  4014. my ($orow,$nrow,$oval,$nval);
  4015. my $dz = 1; # Index des Vorkommens im Selektionsarray
  4016. my $zs = ""; # Zusatz wenn device + Reading + Timestamp von folgenden DS gleich ist UND Value unterschiedlich
  4017. my $zsz = 1; # Zusatzzähler
  4018. foreach my $row (@row_array) {
  4019. my @a = split("_ESC_", $row, 6);
  4020. my $dev = $a[0];
  4021. my $rea = $a[1];
  4022. $a[3] =~ s/:/-/g; # substituieren unsupported characters ":" -> siehe fhem.pl
  4023. my $ts = $a[2]."_".$a[3];
  4024. my $val = $a[4];
  4025. my $unt = $a[5];
  4026. $val = $unt?$val." ".$unt:$val;
  4027. $nrow = $ts.$dev.$rea;
  4028. $nval = $val;
  4029. if($orow) {
  4030. if($orow.$oval eq $nrow.$val) {
  4031. $dz++;
  4032. $zs = "";
  4033. $zsz = 1;
  4034. } else {
  4035. # wenn device + Reading + Timestamp gleich ist UND Value unterschiedlich -> dann Zusatz an Reading hängen
  4036. if(($orow eq $nrow) && ($oval ne $val)) {
  4037. $zs = "_".$zsz;
  4038. $zsz++;
  4039. } else {
  4040. $zs = "";
  4041. $zsz = 1;
  4042. }
  4043. $dz = 1;
  4044. }
  4045. }
  4046. $orow = $nrow;
  4047. $oval = $val;
  4048. if ($reading && AttrVal($hash->{NAME}, "readingNameMap", "")) {
  4049. if($dz > 1 && AttrVal($name, "fetchMarkDuplicates", undef)) {
  4050. $reading_runtime_string = $ts."__".$color.$dz."__".AttrVal($hash->{NAME}, "readingNameMap", "").$zs.$ecolor;
  4051. } else {
  4052. $reading_runtime_string = $ts."__".$dz."__".AttrVal($hash->{NAME}, "readingNameMap", "").$zs;
  4053. }
  4054. } else {
  4055. if($dz > 1 && AttrVal($name, "fetchMarkDuplicates", undef)) {
  4056. $reading_runtime_string = $ts."__".$color.$dz."__".$dev."__".$rea.$zs.$ecolor;
  4057. } else {
  4058. $reading_runtime_string = $ts."__".$dz."__".$dev."__".$rea.$zs;
  4059. }
  4060. }
  4061. ReadingsBulkUpdateValue($hash, $reading_runtime_string, $val);
  4062. }
  4063. my $sfx = AttrVal("global", "language", "EN");
  4064. $sfx = ($sfx eq "EN" ? "" : "_$sfx");
  4065. ReadingsBulkUpdateValue($hash, "number_fetched_rows", ($nrows>$limit)?$nrows-1:$nrows);
  4066. ReadingsBulkUpdateTimeState($hash,$brt,$rt,($nrows-$limit>0)?
  4067. "<html>done - Warning: present rows exceed specified limit, adjust attribute <a href='https://fhem.de/commandref${sfx}.html#limit' target='_blank'>limit</a></html>":"done");
  4068. readingsEndUpdate($hash, 1);
  4069. delete($hash->{HELPER}{RUNNING_PID});
  4070. return;
  4071. }
  4072. ####################################################################################################
  4073. # DB-Abfrage delSeqDoublets
  4074. ####################################################################################################
  4075. sub delseqdoubl_DoParse($) {
  4076. my ($string) = @_;
  4077. my ($name,$opt,$device,$reading,$ts) = split("\\§", $string);
  4078. my $hash = $defs{$name};
  4079. my $dbloghash = $hash->{dbloghash};
  4080. my $dbconn = $dbloghash->{dbconn};
  4081. my $dbuser = $dbloghash->{dbuser};
  4082. my $dblogname = $dbloghash->{NAME};
  4083. my $dbpassword = $attr{"sec$dblogname"}{secret};
  4084. my $utf8 = defined($hash->{UTF8})?$hash->{UTF8}:0;
  4085. my $limit = AttrVal($name, "limit", 1000);
  4086. my $var = AttrVal($name, "seqDoubletsVariance", undef);
  4087. my $table = "history";
  4088. my ($err,$dbh,$sth,$sql,$rowlist,$nrows,$selspec,$st,$var1,$var2);
  4089. # Background-Startzeit
  4090. my $bst = [gettimeofday];
  4091. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1, mysql_enable_utf8 => $utf8 });};
  4092. if ($@) {
  4093. $err = encode_base64($@,"");
  4094. Log3 ($name, 2, "DbRep $name - $@");
  4095. return "$name|''|''|$err|''|$opt";
  4096. }
  4097. # Timestampstring to Array
  4098. my @ts = split("\\|", $ts);
  4099. Log3 ($name, 5, "DbRep $name - Timestamp-Array: \n@ts");
  4100. $selspec = "DEVICE,READING,TIMESTAMP,VALUE";
  4101. # SQL zusammenstellen für DB-Abfrage
  4102. $sql = DbRep_createSelectSql($hash,$table,$selspec,$device,$reading,"?","?","ORDER BY DEVICE,READING,TIMESTAMP ASC");
  4103. $sth = $dbh->prepare_cached($sql);
  4104. # DB-Abfrage zeilenweise für jeden Timearray-Eintrag
  4105. my @remain;
  4106. my @todel;
  4107. my $nremain = 0;
  4108. my $ntodel = 0;
  4109. my $ndel = 0;
  4110. my $rt = 0;
  4111. no warnings 'uninitialized';
  4112. foreach my $row (@ts) {
  4113. my @a = split("#", $row);
  4114. my $runtime_string = $a[0];
  4115. my $runtime_string_first = $a[1];
  4116. my $runtime_string_next = $a[2];
  4117. $runtime_string = encode_base64($runtime_string,"");
  4118. # SQL-Startzeit
  4119. $st = [gettimeofday];
  4120. # SQL zusammenstellen für Logausgabe
  4121. my $sql1 = DbRep_createSelectSql($hash,$table,$selspec,$device,$reading,"'$runtime_string_first'","'$runtime_string_next'",'');
  4122. Log3 ($name, 4, "DbRep $name - SQL execute: $sql1");
  4123. eval{$sth->execute($runtime_string_first, $runtime_string_next);};
  4124. if ($@) {
  4125. $err = encode_base64($@,"");
  4126. Log3 ($name, 2, "DbRep $name - $@");
  4127. $dbh->disconnect;
  4128. return "$name|''|''|$err|''|$opt";
  4129. }
  4130. # SQL-Laufzeit ermitteln
  4131. $rt = $rt+tv_interval($st);
  4132. # Beginn Löschlogik, Zusammenstellen der löschenden DS (warping)
  4133. # Array @sel -> die VERBLEIBENDEN Datensätze, @warp -> die zu löschenden Datensätze
  4134. my (@sel,@warp);
  4135. my ($or,$oor,$odev,$oread,$oval,$ooval,$ndev,$nread,$nval);
  4136. my $i = 0;
  4137. foreach my $nr (map { $_->[0]."_ESC_".$_->[1]."_ESC_".($_->[2] =~ s/ /_ESC_/r)."_ESC_".$_->[3] } @{$sth->fetchall_arrayref()}) {
  4138. ($ndev,$nread,undef,undef,$nval) = split("_ESC_", $nr); # Werte des aktuellen Elements
  4139. $or = pop @sel; # das letzte Element der Liste
  4140. ($odev,$oread,undef,undef,$oval) = split("_ESC_", $or); # Value des letzten Elements
  4141. if (looks_like_number($oval) && $var) { # Varianz +- falls $val numerischer Wert
  4142. $var1 = $oval + $var;
  4143. $var2 = $oval - $var;
  4144. } else {
  4145. undef $var1;
  4146. undef $var2;
  4147. }
  4148. $oor = pop @sel; # das vorletzte Element der Liste
  4149. $ooval = (split '_ESC_', $oor)[-1]; # Value des vorletzten Elements
  4150. if ($ndev.$nread ne $odev.$oread) {
  4151. $i = 0; # neues Device/Reading in einer Periode -> ooor soll erhalten bleiben
  4152. push (@sel,$oor) if($oor);
  4153. push (@sel,$or) if($or);
  4154. push (@sel,$nr);
  4155. } elsif ($i>=2 && ($ooval eq $oval && $oval eq $nval) || ($i>=2 && $var1 && $var2 && ($ooval <= $var1) && ($var2 <= $ooval) && ($nval <= $var1) && ($var2 <= $nval)) ) {
  4156. push (@sel,$oor);
  4157. push (@sel,$nr);
  4158. push (@warp,$or); # Array der zu löschenden Datensätze
  4159. if ($opt =~ /delete/ && $or) { # delete Datensätze
  4160. my ($dev,$read,$date,$time,$val) = split("_ESC_", $or);
  4161. my $dt = $date." ".$time;
  4162. chomp($val);
  4163. $dev =~ s/'/''/g; # escape ' with ''
  4164. $read =~ s/'/''/g; # escape ' with ''
  4165. $val =~ s/'/''/g; # escape ' with ''
  4166. $st = [gettimeofday];
  4167. my $dsql = "delete FROM $table where TIMESTAMP = '$dt' AND DEVICE = '$dev' AND READING = '$read' AND VALUE = '$val';";
  4168. my $sthd = $dbh->prepare($dsql);
  4169. Log3 ($name, 4, "DbRep $name - SQL execute: $dsql");
  4170. eval {$sthd->execute();};
  4171. if ($@) {
  4172. $err = encode_base64($@,"");
  4173. Log3 ($name, 2, "DbRep $name - $@");
  4174. $dbh->disconnect;
  4175. return "$name|''|''|$err|''|$opt";
  4176. }
  4177. $ndel = $ndel+$sthd->rows;
  4178. $dbh->commit() if(!$dbh->{AutoCommit});
  4179. $rt = $rt+tv_interval($st);
  4180. }
  4181. } else {
  4182. push (@sel,$oor) if($oor);
  4183. push (@sel,$or) if($or);
  4184. push (@sel,$nr);
  4185. }
  4186. $i++;
  4187. }
  4188. if(@sel && $opt =~ /adviceRemain/) {
  4189. # die verbleibenden Datensätze nach Ausführung (nur zur Anzeige)
  4190. push(@remain,@sel) if($#remain+1 < $limit);
  4191. }
  4192. if(@warp && $opt =~ /adviceDelete/) {
  4193. # die zu löschenden Datensätze (nur zur Anzeige)
  4194. push(@todel,@warp) if($#todel+1 < $limit);
  4195. }
  4196. $nremain = $nremain + $#sel+1 if(@sel);
  4197. $ntodel = $ntodel + $#warp+1 if(@warp);
  4198. my $sum = $nremain+$ntodel;
  4199. Log3 ($name, 3, "DbRep $name -> rows analyzed by \"$hash->{LASTCMD}\": $sum") if($sum && $opt =~ /advice/);
  4200. }
  4201. Log3 ($name, 3, "DbRep $name -> rows deleted by \"$hash->{LASTCMD}\": $ndel") if($ndel);
  4202. my $retn = ($opt =~ /adviceRemain/)?$nremain:($opt =~ /adviceDelete/)?$ntodel:$ndel;
  4203. my @retarray = ($opt =~ /adviceRemain/)?@remain:($opt =~ /adviceDelete/)?@todel:" ";
  4204. s/\|/_E#S#C_/g for @retarray; # escape Pipe "|"
  4205. if ($utf8 && @retarray) {
  4206. $rowlist = Encode::encode_utf8(join('|', @retarray));
  4207. } elsif(@retarray) {
  4208. $rowlist = join('|', @retarray);
  4209. } else {
  4210. $rowlist = 0;
  4211. }
  4212. use warnings;
  4213. Log3 ($name, 5, "DbRep $name -> row result list:\n$rowlist");
  4214. $dbh->disconnect;
  4215. # Daten müssen als Einzeiler zurückgegeben werden
  4216. $rowlist = encode_base64($rowlist,"");
  4217. # Background-Laufzeit ermitteln
  4218. my $brt = tv_interval($bst);
  4219. $rt = $rt.",".$brt;
  4220. return "$name|$rowlist|$rt|0|$retn|$opt";
  4221. }
  4222. ####################################################################################################
  4223. # Auswertungsroutine delSeqDoublets
  4224. ####################################################################################################
  4225. sub delseqdoubl_ParseDone($) {
  4226. my ($string) = @_;
  4227. my @a = split("\\|",$string);
  4228. my $hash = $defs{$a[0]};
  4229. my $rowlist = decode_base64($a[1]);
  4230. my $bt = $a[2];
  4231. my ($rt,$brt) = split(",", $bt);
  4232. my $err = $a[3]?decode_base64($a[3]):undef;
  4233. my $nrows = $a[4];
  4234. my $opt = $a[5];
  4235. my $name = $hash->{NAME};
  4236. my $reading = AttrVal($name, "reading", undef);
  4237. my $limit = AttrVal($name, "limit", 1000);
  4238. my @row;
  4239. my $l = 1;
  4240. my $reading_runtime_string;
  4241. my $erread;
  4242. if ($err) {
  4243. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  4244. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  4245. delete($hash->{HELPER}{RUNNING_PID});
  4246. return;
  4247. }
  4248. # Befehl nach Procedure ausführen
  4249. $erread = DbRep_afterproc($hash, "delSeq");
  4250. # Readingaufbereitung
  4251. readingsBeginUpdate($hash);
  4252. no warnings 'uninitialized';
  4253. if ($opt !~ /delete/ && $rowlist) {
  4254. my @row_array = split("\\|", $rowlist);
  4255. s/_E#S#C_/\|/g for @row_array; # escaped Pipe return to "|"
  4256. Log3 ($name, 5, "DbRep $name - row_array decoded: @row_array");
  4257. foreach my $row (@row_array) {
  4258. last if($l >= $limit);
  4259. my @a = split("_ESC_", $row, 5);
  4260. my $dev = $a[0];
  4261. my $rea = $a[1];
  4262. $a[3] =~ s/:/-/g; # substituieren unsupported characters ":" -> siehe fhem.pl
  4263. my $ts = $a[2]."_".$a[3];
  4264. my $val = $a[4];
  4265. if ($reading && AttrVal($hash->{NAME}, "readingNameMap", "")) {
  4266. $reading_runtime_string = $ts."__".AttrVal($hash->{NAME}, "readingNameMap", "") ;
  4267. } else {
  4268. $reading_runtime_string = $ts."__".$dev."__".$rea;
  4269. }
  4270. ReadingsBulkUpdateValue($hash, $reading_runtime_string, $val);
  4271. $l++;
  4272. }
  4273. }
  4274. use warnings;
  4275. my $sfx = AttrVal("global", "language", "EN");
  4276. $sfx = ($sfx eq "EN" ? "" : "_$sfx");
  4277. my $rnam = ($opt =~ /adviceRemain/)?"number_rows_to_remain":($opt =~ /adviceDelete/)?"number_rows_to_delete":"number_rows_deleted";
  4278. ReadingsBulkUpdateValue($hash, "$rnam", "$nrows");
  4279. ReadingsBulkUpdateTimeState($hash,$brt,$rt,($l >= $limit)?
  4280. "<html>done - Warning: not all items are shown, adjust attribute <a href='https://fhem.de/commandref${sfx}.html#limit' target='_blank'>limit</a> if you want see more</html>":"done");
  4281. readingsEndUpdate($hash, 1);
  4282. delete($hash->{HELPER}{RUNNING_PID});
  4283. return;
  4284. }
  4285. ####################################################################################################
  4286. # nichtblockierende DB-Funktion expfile
  4287. ####################################################################################################
  4288. sub expfile_DoParse($) {
  4289. my ($string) = @_;
  4290. my ($name, $device, $reading, $rsf, $file, $ts) = split("\\§", $string);
  4291. my $hash = $defs{$name};
  4292. my $dbloghash = $hash->{dbloghash};
  4293. my $dbconn = $dbloghash->{dbconn};
  4294. my $dbuser = $dbloghash->{dbuser};
  4295. my $dblogname = $dbloghash->{NAME};
  4296. my $dbpassword = $attr{"sec$dblogname"}{secret};
  4297. my $utf8 = defined($hash->{UTF8})?$hash->{UTF8}:0;
  4298. my ($dbh,$sth,$sql);
  4299. my $err=0;
  4300. # Background-Startzeit
  4301. my $bst = [gettimeofday];
  4302. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1, mysql_enable_utf8 => $utf8 });};
  4303. if ($@) {
  4304. $err = encode_base64($@,"");
  4305. Log3 ($name, 2, "DbRep $name - $@");
  4306. return "$name|''|''|$err|''|''|''";
  4307. }
  4308. $rsf =~ s/[:\s]/_/g;
  4309. my $outfile = $file?$file:AttrVal($name, "expimpfile", undef);
  4310. $outfile =~ s/%TSB/$rsf/g;
  4311. my @t = localtime;
  4312. $outfile = ResolveDateWildcards($outfile, @t);
  4313. if (open(FH, ">:utf8", "$outfile")) {
  4314. binmode (FH) if(!$utf8);
  4315. } else {
  4316. $err = encode_base64("could not open ".$outfile.": ".$!,"");
  4317. return "$name|''|''|$err|''|''|''";
  4318. }
  4319. # ist Zeiteingrenzung und/oder Aggregation gesetzt ? (wenn ja -> "?" in SQL sonst undef)
  4320. my ($IsTimeSet,$IsAggrSet) = DbRep_checktimeaggr($hash);
  4321. Log3 ($name, 5, "DbRep $name - IsTimeSet: $IsTimeSet, IsAggrSet: $IsAggrSet");
  4322. # Timestampstring to Array
  4323. my @ts = split("\\|", $ts);
  4324. Log3 ($name, 5, "DbRep $name - Timestamp-Array: \n@ts");
  4325. # SQL-Startzeit
  4326. my $st = [gettimeofday];
  4327. # DB-Abfrage zeilenweise für jeden Array-Eintrag
  4328. my $arrstr;
  4329. my $nrows = 0;
  4330. my $addon = "ORDER BY TIMESTAMP";
  4331. no warnings 'uninitialized';
  4332. foreach my $row (@ts) {
  4333. my @a = split("#", $row);
  4334. my $runtime_string = $a[0];
  4335. my $runtime_string_first = $a[1];
  4336. my $runtime_string_next = $a[2];
  4337. if ($IsTimeSet || $IsAggrSet) {
  4338. $sql = DbRep_createSelectSql($hash,"history","TIMESTAMP,DEVICE,TYPE,EVENT,READING,VALUE,UNIT",$device,$reading,"'$runtime_string_first'","'$runtime_string_next'",$addon);
  4339. } else {
  4340. $sql = DbRep_createSelectSql($hash,"history","TIMESTAMP,DEVICE,TYPE,EVENT,READING,VALUE,UNIT",$device,$reading,undef,undef,$addon);
  4341. }
  4342. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  4343. eval{ $sth = $dbh->prepare($sql);
  4344. $sth->execute();
  4345. };
  4346. if ($@) {
  4347. $err = encode_base64($@,"");
  4348. Log3 ($name, 2, "DbRep $name - $@");
  4349. $dbh->disconnect;
  4350. return "$name|''|''|$err|''|''|''";
  4351. }
  4352. while (my $row = $sth->fetchrow_arrayref) {
  4353. print FH DbRep_charfilter(join(',', map { s{"}{""}g; "\"$_\"";} @$row)), "\n";
  4354. Log3 ($name, 5, "DbRep $name -> write row: @$row");
  4355. # Anzahl der Datensätze
  4356. $nrows++;
  4357. }
  4358. }
  4359. close(FH);
  4360. # SQL-Laufzeit ermitteln
  4361. my $rt = tv_interval($st);
  4362. $sth->finish;
  4363. $dbh->disconnect;
  4364. # Background-Laufzeit ermitteln
  4365. my $brt = tv_interval($bst);
  4366. $rt = $rt.",".$brt;
  4367. return "$name|$nrows|$rt|$err|$device|$reading|$outfile";
  4368. }
  4369. ####################################################################################################
  4370. # Auswertungsroutine der nichtblockierenden DB-Funktion expfile
  4371. ####################################################################################################
  4372. sub expfile_ParseDone($) {
  4373. my ($string) = @_;
  4374. my @a = split("\\|",$string);
  4375. my $hash = $defs{$a[0]};
  4376. my $nrows = $a[1];
  4377. my $bt = $a[2];
  4378. my ($rt,$brt) = split(",", $bt);
  4379. my $err = $a[3]?decode_base64($a[3]):undef;
  4380. my $name = $hash->{NAME};
  4381. my $device = $a[4];
  4382. $device =~ s/[^A-Za-z\/\d_\.-]/\//g;
  4383. my $reading = $a[5];
  4384. $reading =~ s/[^A-Za-z\/\d_\.-]/\//g;
  4385. my $outfile = $a[6];
  4386. my $erread;
  4387. # Befehl nach Procedure ausführen
  4388. $erread = DbRep_afterproc($hash, "export");
  4389. if ($err) {
  4390. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  4391. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  4392. delete($hash->{HELPER}{RUNNING_PID});
  4393. return;
  4394. }
  4395. # only for this block because of warnings if details of readings are not set
  4396. no warnings 'uninitialized';
  4397. my $ds = $device." -- " if ($device);
  4398. my $rds = $reading." -- " if ($reading);
  4399. my $export_string = $ds.$rds." -- ROWS EXPORTED TO FILE -- ";
  4400. my $state = $erread?$erread:"done";
  4401. readingsBeginUpdate($hash);
  4402. ReadingsBulkUpdateValue ($hash, $export_string, $nrows);
  4403. ReadingsBulkUpdateTimeState($hash,$brt,$rt,$state);
  4404. readingsEndUpdate($hash, 1);
  4405. my $rows = $ds.$rds.$nrows;
  4406. Log3 ($name, 3, "DbRep $name - Number of exported datasets from $hash->{DATABASE} to file $outfile: ".$rows);
  4407. delete($hash->{HELPER}{RUNNING_PID});
  4408. return;
  4409. }
  4410. ####################################################################################################
  4411. # nichtblockierende DB-Funktion impfile
  4412. ####################################################################################################
  4413. sub impfile_Push($) {
  4414. my ($string) = @_;
  4415. my ($name, $rsf, $file) = split("\\|", $string);
  4416. my $hash = $defs{$name};
  4417. my $dbloghash = $hash->{dbloghash};
  4418. my $dbconn = $dbloghash->{dbconn};
  4419. my $dbuser = $dbloghash->{dbuser};
  4420. my $dblogname = $dbloghash->{NAME};
  4421. my $dbmodel = $hash->{dbloghash}{MODEL};
  4422. my $dbpassword = $attr{"sec$dblogname"}{secret};
  4423. my $utf8 = defined($hash->{UTF8})?$hash->{UTF8}:0;
  4424. my $err=0;
  4425. my $sth;
  4426. # Background-Startzeit
  4427. my $bst = [gettimeofday];
  4428. my $dbh;
  4429. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoCommit => 1, AutoInactiveDestroy => 1, mysql_enable_utf8 => $utf8 });};
  4430. if ($@) {
  4431. $err = encode_base64($@,"");
  4432. Log3 ($name, 2, "DbRep $name - $@");
  4433. return "$name|''|''|$err|''";
  4434. }
  4435. # check ob PK verwendet wird, @usepkx?Anzahl der Felder im PK:0 wenn kein PK, $pkx?Namen der Felder:none wenn kein PK
  4436. my ($usepkh,$usepkc,$pkh,$pkc) = DbRep_checkUsePK($hash,$dbloghash,$dbh);
  4437. $rsf =~ s/[:\s]/_/g;
  4438. my $infile = $file?$file:AttrVal($name, "expimpfile", undef);
  4439. $infile =~ s/%TSB/$rsf/g;
  4440. my @t = localtime;
  4441. $infile = ResolveDateWildcards($infile, @t);
  4442. if (open(FH, "<:utf8", "$infile")) {
  4443. binmode (FH) if(!$utf8);
  4444. } else {
  4445. $err = encode_base64("could not open ".$infile.": ".$!,"");
  4446. return "$name|''|''|$err|''";
  4447. }
  4448. # only for this block because of warnings if details inline is not set
  4449. no warnings 'uninitialized';
  4450. # SQL-Startzeit
  4451. my $st = [gettimeofday];
  4452. my $al;
  4453. # Datei zeilenweise einlesen und verarbeiten !
  4454. # Beispiel Inline:
  4455. # "2016-09-25 08:53:56","STP_5000","SMAUTILS","etotal: 11859.573","etotal","11859.573",""
  4456. # insert history mit/ohne primary key
  4457. if ($usepkh && $dbloghash->{MODEL} eq 'MYSQL') {
  4458. eval { $sth = $dbh->prepare_cached("INSERT IGNORE INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  4459. } elsif ($usepkh && $dbloghash->{MODEL} eq 'SQLITE') {
  4460. eval { $sth = $dbh->prepare_cached("INSERT OR IGNORE INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  4461. } elsif ($usepkh && $dbloghash->{MODEL} eq 'POSTGRESQL') {
  4462. eval { $sth = $dbh->prepare_cached("INSERT INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?) ON CONFLICT DO NOTHING"); };
  4463. } else {
  4464. eval { $sth = $dbh->prepare_cached("INSERT INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  4465. }
  4466. if ($@) {
  4467. $err = encode_base64($@,"");
  4468. Log3 ($name, 2, "DbRep $name - $@");
  4469. $dbh->disconnect();
  4470. return "$name|''|''|$err|''";
  4471. }
  4472. $dbh->begin_work();
  4473. my $irowdone = 0;
  4474. my $irowcount = 0;
  4475. my $warn = 0;
  4476. while (<FH>) {
  4477. $al = $_;
  4478. chomp $al;
  4479. my @alarr = split("\",\"", $al);
  4480. foreach(@alarr) {
  4481. tr/"//d;
  4482. }
  4483. my $i_timestamp = $alarr[0];
  4484. # $i_timestamp =~ tr/"//d;
  4485. my $i_device = $alarr[1];
  4486. my $i_type = $alarr[2];
  4487. my $i_event = $alarr[3];
  4488. my $i_reading = $alarr[4];
  4489. my $i_value = $alarr[5];
  4490. my $i_unit = $alarr[6] ? $alarr[6]: " ";
  4491. $irowcount++;
  4492. next if(!$i_timestamp); #leerer Datensatz
  4493. # check ob TIMESTAMP Format ok ?
  4494. my ($i_date, $i_time) = split(" ",$i_timestamp);
  4495. if ($i_date !~ /(\d{4})-(\d{2})-(\d{2})/ || $i_time !~ /(\d{2}):(\d{2}):(\d{2})/) {
  4496. $err = encode_base64("Format of date/time is not valid in row $irowcount of $infile. Must be format \"YYYY-MM-DD HH:MM:SS\" !","");
  4497. Log3 ($name, 2, "DbRep $name -> ERROR - Import from file $infile was not done. Invalid date/time field format in row $irowcount.");
  4498. close(FH);
  4499. $dbh->rollback;
  4500. return "$name|''|''|$err|''";
  4501. }
  4502. # Daten auf maximale Länge (entsprechend der Feldlänge in DbLog DB create-scripts) beschneiden wenn nicht SQLite
  4503. if ($dbmodel ne 'SQLITE') {
  4504. $i_device = substr($i_device,0, $dbrep_col{DEVICE});
  4505. $i_event = substr($i_event,0, $dbrep_col{EVENT});
  4506. $i_reading = substr($i_reading,0, $dbrep_col{READING});
  4507. $i_value = substr($i_value,0, $dbrep_col{VALUE});
  4508. $i_unit = substr($i_unit,0, $dbrep_col{UNIT}) if($i_unit);
  4509. }
  4510. Log3 ($name, 5, "DbRep $name -> data to insert Timestamp: $i_timestamp, Device: $i_device, Type: $i_type, Event: $i_event, Reading: $i_reading, Value: $i_value, Unit: $i_unit");
  4511. if($i_timestamp && $i_device && $i_reading) {
  4512. eval {$sth->execute($i_timestamp, $i_device, $i_type, $i_event, $i_reading, $i_value, $i_unit);};
  4513. if ($@) {
  4514. $err = encode_base64($@,"");
  4515. Log3 ($name, 2, "DbRep $name - Failed to insert new dataset into database: $@");
  4516. close(FH);
  4517. $dbh->rollback;
  4518. $dbh->disconnect;
  4519. return "$name|''|''|$err|''";
  4520. } else {
  4521. $irowdone++
  4522. }
  4523. } else {
  4524. my $c = !$i_timestamp?"field \"timestamp\" is empty":!$i_device?"field \"device\" is empty":"field \"reading\" is empty";
  4525. $err = encode_base64("format error in in row $irowcount of $infile - cause: $c","");
  4526. Log3 ($name, 2, "DbRep $name -> ERROR - Import of datasets NOT done. Formaterror in row $irowcount of $infile - cause: $c");
  4527. close(FH);
  4528. $dbh->rollback;
  4529. $dbh->disconnect;
  4530. return "$name|''|''|$err|''";
  4531. }
  4532. }
  4533. $dbh->commit;
  4534. $dbh->disconnect;
  4535. close(FH);
  4536. # SQL-Laufzeit ermitteln
  4537. my $rt = tv_interval($st);
  4538. # Background-Laufzeit ermitteln
  4539. my $brt = tv_interval($bst);
  4540. $rt = $rt.",".$brt;
  4541. return "$name|$irowdone|$rt|$err|$infile";
  4542. }
  4543. ####################################################################################################
  4544. # Auswertungsroutine der nichtblockierenden DB-Funktion impfile
  4545. ####################################################################################################
  4546. sub impfile_PushDone($) {
  4547. my ($string) = @_;
  4548. my @a = split("\\|",$string);
  4549. my $hash = $defs{$a[0]};
  4550. my $irowdone = $a[1];
  4551. my $bt = $a[2];
  4552. my ($rt,$brt) = split(",", $bt);
  4553. my $err = $a[3]?decode_base64($a[3]):undef;
  4554. my $name = $hash->{NAME};
  4555. my $infile = $a[4];
  4556. my $erread;
  4557. # Befehl nach Procedure ausführen
  4558. $erread = DbRep_afterproc($hash, "import");
  4559. if ($err) {
  4560. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  4561. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  4562. delete($hash->{HELPER}{RUNNING_PID});
  4563. return;
  4564. }
  4565. # only for this block because of warnings if details of readings are not set
  4566. no warnings 'uninitialized';
  4567. my $import_string = " -- ROWS IMPORTED FROM FILE -- ";
  4568. my $state = $erread?$erread:"done";
  4569. readingsBeginUpdate($hash);
  4570. ReadingsBulkUpdateValue ($hash, $import_string, $irowdone);
  4571. ReadingsBulkUpdateTimeState($hash,$brt,$rt,$state);
  4572. readingsEndUpdate($hash, 1);
  4573. Log3 ($name, 3, "DbRep $name - Number of imported datasets to $hash->{DATABASE} from file $infile: $irowdone");
  4574. delete($hash->{HELPER}{RUNNING_PID});
  4575. return;
  4576. }
  4577. ####################################################################################################
  4578. # nichtblockierende DB-Abfrage sqlCmd - generischer SQL-Befehl - name | opt | sqlcommand
  4579. ####################################################################################################
  4580. # set logdbrep sqlCmd select count(*) from history
  4581. # set logdbrep sqlCmd select DEVICE,count(*) from history group by DEVICE HAVING count(*) > 10000
  4582. sub sqlCmd_DoParse($) {
  4583. my ($string) = @_;
  4584. my ($name, $opt, $runtime_string_first, $runtime_string_next, $cmd) = split("\\|", $string);
  4585. my $hash = $defs{$name};
  4586. my $dbloghash = $hash->{dbloghash};
  4587. my $dbconn = $dbloghash->{dbconn};
  4588. my $dbuser = $dbloghash->{dbuser};
  4589. my $dblogname = $dbloghash->{NAME};
  4590. my $dbpassword = $attr{"sec$dblogname"}{secret};
  4591. my $utf8 = defined($hash->{UTF8})?$hash->{UTF8}:0;
  4592. my $srs = AttrVal($name, "sqlResultFieldSep", "|");
  4593. my $err;
  4594. # Background-Startzeit
  4595. my $bst = [gettimeofday];
  4596. my $dbh;
  4597. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoCommit => 1, AutoInactiveDestroy => 1, mysql_enable_utf8 => $utf8 });};
  4598. if ($@) {
  4599. $err = encode_base64($@,"");
  4600. Log3 ($name, 2, "DbRep $name - $@");
  4601. return "$name|''|$opt|$cmd|''|''|$err";
  4602. }
  4603. # only for this block because of warnings if details of readings are not set
  4604. no warnings 'uninitialized';
  4605. my $sql = ($cmd =~ m/\;$/)?$cmd:$cmd.";";
  4606. # Allow inplace replacement of keywords for timings (use time attribute syntax)
  4607. $sql =~ s/§timestamp_begin§/'$runtime_string_first'/g;
  4608. $sql =~ s/§timestamp_end§/'$runtime_string_next'/g;
  4609. # Debug "SQL :".$sql.":";
  4610. Log3($name, 4, "DbRep $name - SQL execute: $sql");
  4611. # SQL-Startzeit
  4612. my $st = [gettimeofday];
  4613. my ($sth,$r);
  4614. eval {$sth = $dbh->prepare($sql);
  4615. $r = $sth->execute();
  4616. };
  4617. if ($@) {
  4618. # error bei sql-execute
  4619. $err = encode_base64($@,"");
  4620. Log3 ($name, 2, "DbRep $name - ERROR - $@");
  4621. $dbh->disconnect;
  4622. return "$name|''|$opt|$sql|''|''|$err";
  4623. }
  4624. my @rows;
  4625. my $nrows = 0;
  4626. if($sql =~ m/^\s*(select|pragma|show)/is) {
  4627. while (my @line = $sth->fetchrow_array()) {
  4628. Log3 ($name, 4, "DbRep $name - SQL result: @line");
  4629. my $row = join("$srs", @line);
  4630. # join Delimiter "§" escapen
  4631. $row =~ s/§/|°escaped°|/g;
  4632. push(@rows, $row);
  4633. # Anzahl der Datensätze
  4634. $nrows++;
  4635. }
  4636. } else {
  4637. $nrows = $sth->rows;
  4638. eval {$dbh->commit() if(!$dbh->{AutoCommit});};
  4639. if ($@) {
  4640. $err = encode_base64($@,"");
  4641. Log3 ($name, 2, "DbRep $name - ERROR - $@");
  4642. $dbh->disconnect;
  4643. return "$name|''|$opt|$sql|''|''|$err";
  4644. }
  4645. push(@rows, $r);
  4646. my $com = (split(" ",$sql, 2))[0];
  4647. Log3 ($name, 3, "DbRep $name - Number of entries processed in db $hash->{DATABASE}: $nrows by $com");
  4648. }
  4649. $sth->finish;
  4650. # SQL-Laufzeit ermitteln
  4651. my $rt = tv_interval($st);
  4652. $dbh->disconnect;
  4653. # Daten müssen als Einzeiler zurückgegeben werden
  4654. my $rowstring = join("§", @rows);
  4655. $rowstring = encode_base64($rowstring,"");
  4656. # Background-Laufzeit ermitteln
  4657. my $brt = tv_interval($bst);
  4658. $rt = $rt.",".$brt;
  4659. return "$name|$rowstring|$opt|$sql|$nrows|$rt|$err";
  4660. }
  4661. ####################################################################################################
  4662. # Auswertungsroutine der nichtblockierenden DB-Abfrage sqlCmd
  4663. ####################################################################################################
  4664. sub sqlCmd_ParseDone($) {
  4665. my ($string) = @_;
  4666. my @a = split("\\|",$string);
  4667. my $hash = $defs{$a[0]};
  4668. my $name = $hash->{NAME};
  4669. my $rowstring = decode_base64($a[1]);
  4670. my $opt = $a[2];
  4671. my $cmd = $a[3];
  4672. my $nrows = $a[4];
  4673. my $bt = $a[5];
  4674. my ($rt,$brt) = split(",", $bt);
  4675. my $err = $a[6]?decode_base64($a[6]):undef;
  4676. my $srf = AttrVal($name, "sqlResultFormat", "separated");
  4677. my $srs = AttrVal($name, "sqlResultFieldSep", "|");
  4678. if ($err) {
  4679. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  4680. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  4681. delete($hash->{HELPER}{RUNNING_PID});
  4682. return;
  4683. }
  4684. Log3 ($name, 5, "DbRep $name - SQL result decoded: $rowstring") if($rowstring);
  4685. no warnings 'uninitialized';
  4686. # Readingaufbereitung
  4687. readingsBeginUpdate($hash);
  4688. ReadingsBulkUpdateValue ($hash, "sqlCmd", $cmd);
  4689. ReadingsBulkUpdateValue ($hash, "sqlResultNumRows", $nrows);
  4690. # Drop-Down Liste bisherige sqlCmd-Befehle füllen und in Key-File sichern
  4691. # my $hl = $hash->{HELPER}{SQLHIST};
  4692. my @sqlhist = split(",",$hash->{HELPER}{SQLHIST});
  4693. $cmd =~ s/\s/&nbsp;/g;
  4694. $cmd =~ s/,/<c>/g;
  4695. my $hlc = AttrVal($name, "sqlCmdHistoryLength", 0); # Anzahl der Einträge in Drop-Down Liste
  4696. if(!@sqlhist || (@sqlhist && !($cmd ~~ @sqlhist))) {
  4697. unshift @sqlhist,$cmd;
  4698. pop @sqlhist if(@sqlhist > $hlc);
  4699. my $hl = join(",",@sqlhist);
  4700. $hash->{HELPER}{SQLHIST} = $hl;
  4701. DbRep_setCmdFile($name."_sqlCmdList",$hl,$hash);
  4702. }
  4703. if ($srf eq "sline") {
  4704. $rowstring =~ s/§/]|[/g;
  4705. $rowstring =~ s/\|°escaped°\|/§/g;
  4706. ReadingsBulkUpdateValue ($hash, "SqlResult", $rowstring);
  4707. } elsif ($srf eq "table") {
  4708. my $res = "<html><table border=2 bordercolor='darkgreen' cellspacing=0>";
  4709. my @rows = split( /§/, $rowstring );
  4710. my $row;
  4711. foreach $row ( @rows ) {
  4712. $row =~ s/\|°escaped°\|/§/g;
  4713. $row =~ s/$srs/\|/g if($srs !~ /\|/);
  4714. $row =~ s/\|/<\/td><td style='padding-right:5px;padding-left:5px'>/g;
  4715. $res .= "<tr><td style='padding-right:5px;padding-left:5px'>".$row."</td></tr>";
  4716. }
  4717. $row .= $res."</table></html>";
  4718. ReadingsBulkUpdateValue ($hash,"SqlResult", $row);
  4719. } elsif ($srf eq "mline") {
  4720. my $res = "<html>";
  4721. my @rows = split( /§/, $rowstring );
  4722. my $row;
  4723. foreach $row ( @rows ) {
  4724. $row =~ s/\|°escaped°\|/§/g;
  4725. $res .= $row."<br>";
  4726. }
  4727. $row .= $res."</html>";
  4728. ReadingsBulkUpdateValue ($hash, "SqlResult", $row );
  4729. } elsif ($srf eq "separated") {
  4730. my @rows = split( /§/, $rowstring );
  4731. my $bigint = @rows;
  4732. my $numd = ceil(log10($bigint));
  4733. my $formatstr = sprintf('%%%d.%dd', $numd, $numd);
  4734. my $i = 0;
  4735. foreach my $row ( @rows ) {
  4736. $i++;
  4737. $row =~ s/\|°escaped°\|/§/g;
  4738. my $fi = sprintf($formatstr, $i);
  4739. ReadingsBulkUpdateValue ($hash, "SqlResultRow_".$fi, $row);
  4740. }
  4741. } elsif ($srf eq "json") {
  4742. my %result = ();
  4743. my @rows = split( /§/, $rowstring );
  4744. my $bigint = @rows;
  4745. my $numd = ceil(log10($bigint));
  4746. my $formatstr = sprintf('%%%d.%dd', $numd, $numd);
  4747. my $i = 0;
  4748. foreach my $row ( @rows ) {
  4749. $i++;
  4750. $row =~ s/\|°escaped°\|/§/g;
  4751. my $fi = sprintf($formatstr, $i);
  4752. $result{$fi} = $row;
  4753. }
  4754. my $json = toJSON(\%result); # at least fhem.pl 14348 2017-05-22 20:25:06Z
  4755. ReadingsBulkUpdateValue ($hash, "SqlResult", $json);
  4756. }
  4757. ReadingsBulkUpdateTimeState($hash,$brt,$rt,"done");
  4758. readingsEndUpdate($hash, 1);
  4759. delete($hash->{HELPER}{RUNNING_PID});
  4760. return;
  4761. }
  4762. ####################################################################################################
  4763. # nichtblockierende DB-Abfrage get db Metadaten
  4764. ####################################################################################################
  4765. sub dbmeta_DoParse($) {
  4766. my ($string) = @_;
  4767. my @a = split("\\|",$string);
  4768. my $name = $a[0];
  4769. my $hash = $defs{$name};
  4770. my $opt = $a[1];
  4771. my $dbloghash = $hash->{dbloghash};
  4772. my $dbconn = $dbloghash->{dbconn};
  4773. my $db = $hash->{DATABASE};
  4774. my $dbuser = $dbloghash->{dbuser};
  4775. my $dblogname = $dbloghash->{NAME};
  4776. my $dbpassword = $attr{"sec$dblogname"}{secret};
  4777. my $dbmodel = $dbloghash->{MODEL};
  4778. my $utf8 = defined($hash->{UTF8})?$hash->{UTF8}:0;
  4779. my ($dbh,$sth,$sql);
  4780. my $err;
  4781. # Background-Startzeit
  4782. my $bst = [gettimeofday];
  4783. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1, mysql_enable_utf8 => $utf8 });};
  4784. if ($@) {
  4785. $err = encode_base64($@,"");
  4786. Log3 ($name, 2, "DbRep $name - $@");
  4787. return "$name|''|''|''|$err";
  4788. }
  4789. # only for this block because of warnings if details of readings are not set
  4790. no warnings 'uninitialized';
  4791. # Liste der anzuzeigenden Parameter erzeugen, sonst alle ("%"), abhängig von $opt
  4792. my $param = AttrVal($name, "showVariables", "%") if($opt eq "dbvars");
  4793. $param = AttrVal($name, "showSvrInfo", "[A-Z_]") if($opt eq "svrinfo");
  4794. $param = AttrVal($name, "showStatus", "%") if($opt eq "dbstatus");
  4795. $param = "1" if($opt =~ /tableinfo|procinfo/); # Dummy-Eintrag für einen Schleifendurchlauf
  4796. my @parlist = split(",",$param);
  4797. # SQL-Startzeit
  4798. my $st = [gettimeofday];
  4799. my @row_array;
  4800. # due to incompatible changes made in MyQL 5.7.5, see http://johnemb.blogspot.de/2014/09/adding-or-removing-individual-sql-modes.html
  4801. if($dbmodel eq "MYSQL") {
  4802. eval {$dbh->do("SET sql_mode=(SELECT REPLACE(\@\@sql_mode,'ONLY_FULL_GROUP_BY',''));");};
  4803. }
  4804. if ($@) {
  4805. $err = encode_base64($@,"");
  4806. Log3 ($name, 2, "DbRep $name - $@");
  4807. $dbh->disconnect;
  4808. return "$name|''|''|''|$err";
  4809. }
  4810. if ($opt ne "svrinfo") {
  4811. foreach my $ple (@parlist) {
  4812. if ($opt eq "dbvars") {
  4813. $sql = "show variables like '$ple';";
  4814. } elsif ($opt eq "dbstatus") {
  4815. $sql = "show global status like '$ple';";
  4816. } elsif ($opt eq "tableinfo") {
  4817. $sql = "show Table Status from $db;";
  4818. } elsif ($opt eq "procinfo") {
  4819. $sql = "show full processlist;";
  4820. }
  4821. Log3($name, 4, "DbRep $name - SQL execute: $sql");
  4822. $sth = $dbh->prepare($sql);
  4823. eval {$sth->execute();};
  4824. if ($@) {
  4825. # error bei sql-execute
  4826. $err = encode_base64($@,"");
  4827. Log3 ($name, 2, "DbRep $name - $@");
  4828. $dbh->disconnect;
  4829. return "$name|''|''|''|$err";
  4830. } else {
  4831. # kein error bei sql-execute
  4832. if ($opt eq "tableinfo") {
  4833. $param = AttrVal($name, "showTableInfo", "[A-Z_]");
  4834. $param =~ s/,/\|/g;
  4835. $param =~ tr/%//d;
  4836. while ( my $line = $sth->fetchrow_hashref()) {
  4837. Log3 ($name, 5, "DbRep $name - SQL result: $line->{Name}, $line->{Version}, $line->{Row_format}, $line->{Rows}, $line->{Avg_row_length}, $line->{Data_length}, $line->{Max_data_length}, $line->{Index_length}, $line->{Data_free}, $line->{Auto_increment}, $line->{Create_time}, $line->{Check_time}, $line->{Collation}, $line->{Checksum}, $line->{Create_options}, $line->{Comment}");
  4838. if($line->{Name} =~ m/($param)/i) {
  4839. push(@row_array, $line->{Name}.".engine ".$line->{Engine}) if($line->{Engine});
  4840. push(@row_array, $line->{Name}.".version ".$line->{Version}) if($line->{Version});
  4841. push(@row_array, $line->{Name}.".row_format ".$line->{Row_format}) if($line->{Row_format});
  4842. push(@row_array, $line->{Name}.".number_of_rows ".$line->{Rows}) if($line->{Rows});
  4843. push(@row_array, $line->{Name}.".avg_row_length ".$line->{Avg_row_length}) if($line->{Avg_row_length});
  4844. push(@row_array, $line->{Name}.".data_length_MB ".sprintf("%.2f",$line->{Data_length}/1024/1024)) if($line->{Data_length});
  4845. push(@row_array, $line->{Name}.".max_data_length_MB ".sprintf("%.2f",$line->{Max_data_length}/1024/1024)) if($line->{Max_data_length});
  4846. push(@row_array, $line->{Name}.".index_length_MB ".sprintf("%.2f",$line->{Index_length}/1024/1024)) if($line->{Index_length});
  4847. push(@row_array, $line->{Name}.".data_index_length_MB ".sprintf("%.2f",($line->{Data_length}+$line->{Index_length})/1024/1024));
  4848. push(@row_array, $line->{Name}.".data_free_MB ".sprintf("%.2f",$line->{Data_free}/1024/1024)) if($line->{Data_free});
  4849. push(@row_array, $line->{Name}.".auto_increment ".$line->{Auto_increment}) if($line->{Auto_increment});
  4850. push(@row_array, $line->{Name}.".create_time ".$line->{Create_time}) if($line->{Create_time});
  4851. push(@row_array, $line->{Name}.".update_time ".$line->{Update_time}) if($line->{Update_time});
  4852. push(@row_array, $line->{Name}.".check_time ".$line->{Check_time}) if($line->{Check_time});
  4853. push(@row_array, $line->{Name}.".collation ".$line->{Collation}) if($line->{Collation});
  4854. push(@row_array, $line->{Name}.".checksum ".$line->{Checksum}) if($line->{Checksum});
  4855. push(@row_array, $line->{Name}.".create_options ".$line->{Create_options}) if($line->{Create_options});
  4856. push(@row_array, $line->{Name}.".comment ".$line->{Comment}) if($line->{Comment});
  4857. }
  4858. }
  4859. } elsif ($opt eq "procinfo") {
  4860. my $res = "<html><table border=2 bordercolor='darkgreen' cellspacing=0>";
  4861. $res .= "<tr><td style='padding-right:5px;padding-left:5px;font-weight:bold'>ID</td>";
  4862. $res .= "<td style='padding-right:5px;padding-left:5px;font-weight:bold'>USER</td>";
  4863. $res .= "<td style='padding-right:5px;padding-left:5px;font-weight:bold'>HOST</td>";
  4864. $res .= "<td style='padding-right:5px;padding-left:5px;font-weight:bold'>DB</td>";
  4865. $res .= "<td style='padding-right:5px;padding-left:5px;font-weight:bold'>CMD</td>";
  4866. $res .= "<td style='padding-right:5px;padding-left:5px;font-weight:bold'>TIME_Sec</td>";
  4867. $res .= "<td style='padding-right:5px;padding-left:5px;font-weight:bold'>STATE</td>";
  4868. $res .= "<td style='padding-right:5px;padding-left:5px;font-weight:bold'>INFO</td>";
  4869. $res .= "<td style='padding-right:5px;padding-left:5px;font-weight:bold'>PROGRESS</td></tr>";
  4870. while (my @line = $sth->fetchrow_array()) {
  4871. Log3 ($name, 4, "DbRep $name - SQL result: @line");
  4872. my $row = join("|", @line);
  4873. $row =~ tr/ A-Za-z0-9!"#$§%&'()*+,-.\/:;<=>?@[\]^_`{|}~//cd;
  4874. $row =~ s/\|/<\/td><td style='padding-right:5px;padding-left:5px'>/g;
  4875. $res .= "<tr><td style='padding-right:5px;padding-left:5px'>".$row."</td></tr>";
  4876. }
  4877. my $tab .= $res."</table></html>";
  4878. push(@row_array, "ProcessList ".$tab);
  4879. } else {
  4880. while (my @line = $sth->fetchrow_array()) {
  4881. Log3 ($name, 4, "DbRep $name - SQL result: @line");
  4882. my $row = join("§", @line);
  4883. $row =~ s/ /_/g;
  4884. @line = split("§", $row);
  4885. push(@row_array, $line[0]." ".$line[1]);
  4886. }
  4887. }
  4888. }
  4889. $sth->finish;
  4890. }
  4891. } else {
  4892. $param =~ s/,/\|/g;
  4893. $param =~ tr/%//d;
  4894. # Log3 ($name, 5, "DbRep $name - showDbInfo: $param");
  4895. if($dbmodel eq 'SQLITE') {
  4896. my $sf = $dbh->sqlite_db_filename();
  4897. if ($@) {
  4898. # error bei sql-execute
  4899. $err = encode_base64($@,"");
  4900. Log3 ($name, 2, "DbRep $name - $@");
  4901. $dbh->disconnect;
  4902. return "$name|''|''|''|$err";
  4903. } else {
  4904. # kein error bei sql-execute
  4905. my $key = "SQLITE_DB_FILENAME";
  4906. push(@row_array, $key." ".$sf) if($key =~ m/($param)/i);
  4907. }
  4908. my @a = split(' ',qx(du -m $hash->{DATABASE})) if ($^O =~ m/linux/i || $^O =~ m/unix/i);
  4909. my $key = "SQLITE_FILE_SIZE_MB";
  4910. push(@row_array, $key." ".$a[0]) if($key =~ m/($param)/i);
  4911. }
  4912. my $info;
  4913. while( my ($key,$value) = each(%GetInfoType) ) {
  4914. eval { $info = $dbh->get_info($GetInfoType{"$key"}) };
  4915. if ($@) {
  4916. $err = encode_base64($@,"");
  4917. Log3 ($name, 2, "DbRep $name - $@");
  4918. $dbh->disconnect;
  4919. return "$name|''|''|''|$err";
  4920. } else {
  4921. if($utf8) {
  4922. $info = Encode::encode_utf8($info) if($info);
  4923. }
  4924. push(@row_array, $key." ".$info) if($key =~ m/($param)/i);
  4925. }
  4926. }
  4927. }
  4928. # SQL-Laufzeit ermitteln
  4929. my $rt = tv_interval($st);
  4930. $dbh->disconnect;
  4931. my $rowlist = join('§', @row_array);
  4932. Log3 ($name, 5, "DbRep $name -> row_array: \n@row_array");
  4933. # Daten müssen als Einzeiler zurückgegeben werden
  4934. $rowlist = encode_base64($rowlist,"");
  4935. # Background-Laufzeit ermitteln
  4936. my $brt = tv_interval($bst);
  4937. $rt = $rt.",".$brt;
  4938. return "$name|$rowlist|$rt|$opt|0";
  4939. }
  4940. ####################################################################################################
  4941. # Auswertungsroutine der nichtblockierenden DB-Abfrage get db Metadaten
  4942. ####################################################################################################
  4943. sub dbmeta_ParseDone($) {
  4944. my ($string) = @_;
  4945. my @a = split("\\|",$string);
  4946. my $hash = $defs{$a[0]};
  4947. my $name = $hash->{NAME};
  4948. my $rowlist = decode_base64($a[1]);
  4949. my $bt = $a[2];
  4950. my $opt = $a[3];
  4951. my ($rt,$brt) = split(",", $bt);
  4952. my $err = $a[4]?decode_base64($a[4]):undef;
  4953. if ($err) {
  4954. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  4955. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  4956. delete($hash->{HELPER}{RUNNING_PID});
  4957. return;
  4958. }
  4959. # only for this block because of warnings if details of readings are not set
  4960. no warnings 'uninitialized';
  4961. # Readingaufbereitung
  4962. readingsBeginUpdate($hash);
  4963. my @row_array = split("§", $rowlist);
  4964. Log3 ($name, 5, "DbRep $name - SQL result decoded: \n@row_array") if(@row_array);
  4965. my $pre = "";
  4966. $pre = "VAR_" if($opt eq "dbvars");
  4967. $pre = "STAT_" if($opt eq "dbstatus");
  4968. $pre = "INFO_" if($opt eq "tableinfo");
  4969. foreach my $row (@row_array) {
  4970. my @a = split(" ", $row, 2);
  4971. my $k = $a[0];
  4972. my $v = $a[1];
  4973. ReadingsBulkUpdateValue ($hash, $pre.$k, $v);
  4974. }
  4975. ReadingsBulkUpdateTimeState($hash,$brt,$rt,"done");
  4976. readingsEndUpdate($hash, 1);
  4977. # InternalTimer(time+0.5, "browser_refresh", $hash, 0);
  4978. delete($hash->{HELPER}{RUNNING_PID});
  4979. return;
  4980. }
  4981. ####################################################################################################
  4982. # optimize Tables alle Datenbanken
  4983. ####################################################################################################
  4984. sub DbRep_optimizeTables($) {
  4985. my ($name) = @_;
  4986. my $hash = $defs{$name};
  4987. my $dbloghash = $hash->{dbloghash};
  4988. my $dbconn = $dbloghash->{dbconn};
  4989. my $dbuser = $dbloghash->{dbuser};
  4990. my $dblogname = $dbloghash->{NAME};
  4991. my $dbmodel = $dbloghash->{MODEL};
  4992. my $dbpassword = $attr{"sec$dblogname"}{secret};
  4993. my $dbname = $hash->{DATABASE};
  4994. my $value = 0;
  4995. my ($dbh,$sth,$query,$err,$r,$db_MB_start,$db_MB_end);
  4996. my (%db_tables,@tablenames);
  4997. # Background-Startzeit
  4998. my $bst = [gettimeofday];
  4999. # Verbindung mit DB
  5000. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1 });};
  5001. if ($@) {
  5002. $err = encode_base64($@,"");
  5003. Log3 ($name, 2, "DbRep $name - $@");
  5004. return "$name|''|$err|''|''";
  5005. }
  5006. # SQL-Startzeit
  5007. my $st = [gettimeofday];
  5008. if ($dbmodel =~ /MYSQL/) {
  5009. # Eigenschaften der vorhandenen Tabellen ermitteln (SHOW TABLE STATUS -> Rows sind nicht exakt !!)
  5010. $query = "SHOW TABLE STATUS FROM `$dbname`";
  5011. Log3 ($name, 5, "DbRep $name - current query: $query ");
  5012. Log3 ($name, 3, "DbRep $name - Searching for tables inside database $dbname....");
  5013. eval { $sth = $dbh->prepare($query);
  5014. $sth->execute;
  5015. };
  5016. if ($@) {
  5017. $err = encode_base64($@,"");
  5018. Log3 ($name, 2, "DbRep $name - Error executing: '".$query."' ! MySQL-Error: ".$@);
  5019. $sth->finish;
  5020. $dbh->disconnect;
  5021. return "$name|''|$err|''|''";
  5022. }
  5023. while ( $value = $sth->fetchrow_hashref()) {
  5024. # verbose 5 logging
  5025. Log3 ($name, 5, "DbRep $name - ......... Table definition found: .........");
  5026. foreach my $tk (sort(keys(%$value))) {
  5027. Log3 ($name, 5, "DbRep $name - $tk: $value->{$tk}") if(defined($value->{$tk}) && $tk ne "Rows");
  5028. }
  5029. Log3 ($name, 5, "DbRep $name - ......... Table definition END ............");
  5030. # check for old MySQL3-Syntax Type=xxx
  5031. if (defined $value->{Type}) {
  5032. # port old index type to index engine, so we can use the index Engine in the rest of the script
  5033. $value->{Engine} = $value->{Type};
  5034. }
  5035. $db_tables{$value->{Name}} = $value;
  5036. }
  5037. @tablenames = sort(keys(%db_tables));
  5038. if (@tablenames < 1) {
  5039. $err = "There are no tables inside database $dbname ! It doesn't make sense to backup an empty database. Skipping this one.";
  5040. Log3 ($name, 2, "DbRep $name - $err");
  5041. $err = encode_base64($@,"");
  5042. $sth->finish;
  5043. $dbh->disconnect;
  5044. return "$name|''|$err|''|''";
  5045. }
  5046. # Tabellen optimieren
  5047. $hash->{HELPER}{DBTABLES} = \%db_tables;
  5048. ($err,$db_MB_start,$db_MB_end) = DbRep_mysqlOptimizeTables($hash,$dbh,@tablenames);
  5049. if ($err) {
  5050. $err = encode_base64($err,"");
  5051. return "$name|''|$err|''|''";
  5052. }
  5053. }
  5054. if ($dbmodel =~ /SQLITE/) {
  5055. # Anfangsgröße ermitteln
  5056. $db_MB_start = (split(' ',qx(du -m $hash->{DATABASE})))[0] if ($^O =~ m/linux/i || $^O =~ m/unix/i);
  5057. Log3 ($name, 3, "DbRep $name - Size of database $dbname before optimize (MB): $db_MB_start");
  5058. $query ="VACUUM";
  5059. Log3 ($name, 5, "DbRep $name - current query: $query ");
  5060. Log3 ($name, 3, "DbRep $name - VACUUM database $dbname....");
  5061. eval {$sth = $dbh->prepare($query);
  5062. $r = $sth->execute();
  5063. };
  5064. if ($@) {
  5065. $err = encode_base64($@,"");
  5066. Log3 ($name, 2, "DbRep $name - Error executing: '".$query."' ! SQLite-Error: ".$@);
  5067. $sth->finish;
  5068. $dbh->disconnect;
  5069. return "$name|''|$err|''|''";
  5070. }
  5071. # Endgröße ermitteln
  5072. $db_MB_end = (split(' ',qx(du -m $hash->{DATABASE})))[0] if ($^O =~ m/linux/i || $^O =~ m/unix/i);
  5073. Log3 ($name, 3, "DbRep $name - Size of database $dbname after optimize (MB): $db_MB_end");
  5074. }
  5075. if ($dbmodel =~ /POSTGRESQL/) {
  5076. # Anfangsgröße ermitteln
  5077. $query = "SELECT pg_size_pretty(pg_database_size('$dbname'))";
  5078. Log3 ($name, 5, "DbRep $name - current query: $query ");
  5079. eval { $sth = $dbh->prepare($query);
  5080. $sth->execute;
  5081. };
  5082. if ($@) {
  5083. $err = encode_base64($@,"");
  5084. Log3 ($name, 2, "DbRep $name - Error executing: '".$query."' ! PostgreSQL-Error: ".$@);
  5085. $sth->finish;
  5086. $dbh->disconnect;
  5087. return "$name|''|$err|''|''";
  5088. }
  5089. $value = $sth->fetchrow();
  5090. $value =~ tr/MB//d;
  5091. $db_MB_start = sprintf("%.2f",$value);
  5092. Log3 ($name, 3, "DbRep $name - Size of database $dbname before optimize (MB): $db_MB_start");
  5093. Log3 ($name, 3, "DbRep $name - VACUUM database $dbname....");
  5094. $query = "vacuum history";
  5095. Log3 ($name, 5, "DbRep $name - current query: $query ");
  5096. eval {$sth = $dbh->prepare($query);
  5097. $sth->execute();
  5098. };
  5099. if ($@) {
  5100. $err = encode_base64($@,"");
  5101. Log3 ($name, 2, "DbRep $name - Error executing: '".$query."' ! PostgreSQL-Error: ".$@);
  5102. $sth->finish;
  5103. $dbh->disconnect;
  5104. return "$name|''|$err|''|''";
  5105. }
  5106. # Endgröße ermitteln
  5107. $query = "SELECT pg_size_pretty(pg_database_size('$dbname'))";
  5108. Log3 ($name, 5, "DbRep $name - current query: $query ");
  5109. eval { $sth = $dbh->prepare($query);
  5110. $sth->execute;
  5111. };
  5112. if ($@) {
  5113. $err = encode_base64($@,"");
  5114. Log3 ($name, 2, "DbRep $name - Error executing: '".$query."' ! PostgreSQL-Error: ".$@);
  5115. $sth->finish;
  5116. $dbh->disconnect;
  5117. return "$name|''|$err|''|''";
  5118. }
  5119. $value = $sth->fetchrow();
  5120. $value =~ tr/MB//d;
  5121. $db_MB_end = sprintf("%.2f",$value);
  5122. Log3 ($name, 3, "DbRep $name - Size of database $dbname after optimize (MB): $db_MB_end");
  5123. }
  5124. $sth->finish;
  5125. $dbh->disconnect;
  5126. # SQL-Laufzeit ermitteln
  5127. my $rt = tv_interval($st);
  5128. # Background-Laufzeit ermitteln
  5129. my $brt = tv_interval($bst);
  5130. $rt = $rt.",".$brt;
  5131. Log3 ($name, 3, "DbRep $name - Optimize tables of database $dbname finished, total time used: ".sprintf("%.0f",$brt)." sec.");
  5132. return "$name|$rt|''|$db_MB_start|$db_MB_end";
  5133. }
  5134. ####################################################################################################
  5135. # Auswertungsroutine optimize tables
  5136. ####################################################################################################
  5137. sub DbRep_OptimizeDone($) {
  5138. my ($string) = @_;
  5139. my @a = split("\\|",$string);
  5140. my $hash = $defs{$a[0]};
  5141. my $bt = $a[1];
  5142. my ($rt,$brt) = split(",", $bt);
  5143. my $err = $a[2]?decode_base64($a[2]):undef;
  5144. my $db_MB_start = $a[3];
  5145. my $db_MB_end = $a[4];
  5146. my $name = $hash->{NAME};
  5147. my $erread;
  5148. delete($hash->{HELPER}{RUNNING_OPTIMIZE});
  5149. if ($err) {
  5150. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  5151. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  5152. return;
  5153. }
  5154. # only for this block because of warnings if details of readings are not set
  5155. no warnings 'uninitialized';
  5156. readingsBeginUpdate($hash);
  5157. ReadingsBulkUpdateValue($hash, "SizeDbBegin_MB", $db_MB_start);
  5158. ReadingsBulkUpdateValue($hash, "SizeDbEnd_MB", $db_MB_end);
  5159. readingsEndUpdate($hash, 1);
  5160. # Befehl nach Procedure ausführen
  5161. $erread = DbRep_afterproc($hash, "optimize");
  5162. my $state = $erread?$erread:"optimize tables finished";
  5163. readingsBeginUpdate($hash);
  5164. ReadingsBulkUpdateTimeState($hash,$brt,undef,$state);
  5165. readingsEndUpdate($hash, 1);
  5166. Log3 ($name, 3, "DbRep $name - Optimize tables finished successfully. ");
  5167. return;
  5168. }
  5169. ####################################################################################################
  5170. # nicht blockierende Dump-Routine für MySQL (clientSide)
  5171. ####################################################################################################
  5172. sub mysql_DoDumpClientSide($) {
  5173. my ($name) = @_;
  5174. my $hash = $defs{$name};
  5175. my $dbloghash = $hash->{dbloghash};
  5176. my $dbconn = $dbloghash->{dbconn};
  5177. my $dbuser = $dbloghash->{dbuser};
  5178. my $dblogname = $dbloghash->{NAME};
  5179. my $dbpassword = $attr{"sec$dblogname"}{secret};
  5180. my $dbname = $hash->{DATABASE};
  5181. my $dump_path_def = $attr{global}{modpath}."/log/";
  5182. my $dump_path = AttrVal($name, "dumpDirLocal", $dump_path_def);
  5183. $dump_path = $dump_path."/" unless($dump_path =~ m/\/$/);
  5184. my $optimize_tables_beforedump = AttrVal($name, "optimizeTablesBeforeDump", 0);
  5185. my $memory_limit = AttrVal($name, "dumpMemlimit", 100000);
  5186. my $my_comment = AttrVal($name, "dumpComment", "");
  5187. my $dumpspeed = AttrVal($name, "dumpSpeed", 10000);
  5188. my $ebd = AttrVal($name, "executeBeforeProc", undef);
  5189. my $ead = AttrVal($name, "executeAfterProc", undef);
  5190. my $mysql_commentstring = "-- ";
  5191. my $character_set = "utf8";
  5192. my $repver = $hash->{VERSION};
  5193. my $sql_text = '';
  5194. my $sql_file = '';
  5195. my $dbpraefix = "";
  5196. my ($dbh,$sth,$tablename,$sql_create,$rct,$insert,$first_insert,$backupfile,$drc,$drh,$e,
  5197. $sql_daten,$inhalt,$filesize,$totalrecords,$status_start,$status_end,$err,$db_MB_start,$db_MB_end);
  5198. my (@ar,@tablerecords,@tablenames,@tables,@ergebnis);
  5199. my (%db_tables);
  5200. # Background-Startzeit
  5201. my $bst = [gettimeofday];
  5202. Log3 ($name, 3, "DbRep $name - Starting dump of database '$dbname'");
  5203. ##################### Beginn Dump ########################
  5204. ##############################################################
  5205. undef(%db_tables);
  5206. # Startzeit ermitteln
  5207. my ($Sekunden, $Minuten, $Stunden, $Monatstag, $Monat, $Jahr, $Wochentag, $Jahrestag, $Sommerzeit) = localtime(time);
  5208. $Jahr += 1900;
  5209. $Monat += 1;
  5210. $Jahrestag += 1;
  5211. my $CTIME_String = strftime "%Y-%m-%d %T",localtime(time);
  5212. my $time_stamp = $Jahr."_".sprintf("%02d",$Monat)."_".sprintf("%02d",$Monatstag)."_".sprintf("%02d",$Stunden)."_".sprintf("%02d",$Minuten);
  5213. my $starttime = sprintf("%02d",$Monatstag).".".sprintf("%02d",$Monat).".".$Jahr." ".sprintf("%02d",$Stunden).":".sprintf("%02d",$Minuten);
  5214. my $fieldlist = "";
  5215. # Verbindung mit DB
  5216. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1 });};
  5217. if ($@) {
  5218. $e = $@;
  5219. $err = encode_base64($e,"");
  5220. Log3 ($name, 2, "DbRep $name - $e");
  5221. return "$name|''|$err|''|''|''|''|''|''|''";
  5222. }
  5223. # SQL-Startzeit
  5224. my $st = [gettimeofday];
  5225. ##################### Mysql-Version ermitteln ########################
  5226. eval { $sth = $dbh->prepare("SELECT VERSION()");
  5227. $sth->execute;
  5228. };
  5229. if ($@) {
  5230. $e = $@;
  5231. $err = encode_base64($e,"");
  5232. Log3 ($name, 2, "DbRep $name - $e");
  5233. $dbh->disconnect;
  5234. return "$name|''|$err|''|''|''|''|''|''|''";
  5235. }
  5236. my @mysql_version = $sth->fetchrow;
  5237. my @v = split(/\./,$mysql_version[0]);
  5238. if($v[0] >= 5 || ($v[0] >= 4 && $v[1] >= 1) ) {
  5239. # mysql Version >= 4.1
  5240. $sth = $dbh->prepare("SET NAMES '".$character_set."'");
  5241. $sth->execute;
  5242. # get standard encoding of MySQl-Server
  5243. $sth = $dbh->prepare("SHOW VARIABLES LIKE 'character_set_connection'");
  5244. $sth->execute;
  5245. @ar = $sth->fetchrow;
  5246. $character_set = $ar[1];
  5247. } else {
  5248. # mysql Version < 4.1 -> no SET NAMES available
  5249. # get standard encoding of MySQl-Server
  5250. $sth = $dbh->prepare("SHOW VARIABLES LIKE 'character_set'");
  5251. $sth->execute;
  5252. @ar = $sth->fetchrow;
  5253. if (defined($ar[1])) { $character_set=$ar[1]; }
  5254. }
  5255. Log3 ($name, 3, "DbRep $name - Characterset of collection and backup file set to $character_set. ");
  5256. # Eigenschaften der vorhandenen Tabellen ermitteln (SHOW TABLE STATUS -> Rows sind nicht exakt !!)
  5257. undef(@tables);
  5258. undef(@tablerecords);
  5259. my %db_tables_views;
  5260. my $t = 0;
  5261. my $r = 0;
  5262. my $st_e = "\n";
  5263. my $value = 0;
  5264. my $engine = '';
  5265. my $query ="SHOW TABLE STATUS FROM `$dbname`";
  5266. Log3 ($name, 5, "DbRep $name - current query: $query ");
  5267. if ($dbpraefix ne "") {
  5268. $query.=" LIKE '$dbpraefix%'";
  5269. Log3 ($name, 3, "DbRep $name - Searching for tables inside database $dbname with prefix $dbpraefix....");
  5270. } else {
  5271. Log3 ($name, 3, "DbRep $name - Searching for tables inside database $dbname....");
  5272. }
  5273. eval { $sth = $dbh->prepare($query);
  5274. $sth->execute;
  5275. };
  5276. if ($@) {
  5277. $err = encode_base64($@,"");
  5278. Log3 ($name, 2, "DbRep $name - Error executing: '".$query."' ! MySQL-Error: ".$@);
  5279. $dbh->disconnect;
  5280. return "$name|''|$err|''|''|''|''|''|''|''";
  5281. }
  5282. while ( $value = $sth->fetchrow_hashref()) {
  5283. $value->{skip_data} = 0; #defaut -> backup data of table
  5284. # verbose 5 logging
  5285. Log3 ($name, 5, "DbRep $name - ......... Table definition found: .........");
  5286. foreach my $tk (sort(keys(%$value))) {
  5287. Log3 ($name, 5, "DbRep $name - $tk: $value->{$tk}") if(defined($value->{$tk}) && $tk ne "Rows");
  5288. }
  5289. Log3 ($name, 5, "DbRep $name - ......... Table definition END ............");
  5290. # decide if we need to skip the data while dumping (VIEWs and MEMORY)
  5291. # check for old MySQL3-Syntax Type=xxx
  5292. if (defined $value->{Type}) {
  5293. # port old index type to index engine, so we can use the index Engine in the rest of the script
  5294. $value->{Engine} = $value->{Type};
  5295. $engine = uc($value->{Type});
  5296. if ($engine eq "MEMORY") {
  5297. $value->{skip_data} = 1;
  5298. }
  5299. }
  5300. # check for > MySQL3 Engine = xxx
  5301. if (defined $value->{Engine}) {
  5302. $engine = uc($value->{Engine});
  5303. if ($engine eq "MEMORY") {
  5304. $value->{skip_data} = 1;
  5305. }
  5306. }
  5307. # check for Views - if it is a view the comment starts with "VIEW"
  5308. if (defined $value->{Comment} && uc(substr($value->{Comment},0,4)) eq 'VIEW') {
  5309. $value->{skip_data} = 1;
  5310. $value->{Engine} = 'VIEW';
  5311. $value->{Update_time} = '';
  5312. $db_tables_views{$value->{Name}} = $value;
  5313. } else {
  5314. $db_tables{$value->{Name}} = $value;
  5315. }
  5316. # cast indexes to int, cause they are used for builing the statusline
  5317. $value->{Rows} += 0;
  5318. $value->{Data_length} += 0;
  5319. $value->{Index_length} += 0;
  5320. }
  5321. $sth->finish;
  5322. @tablenames = sort(keys(%db_tables));
  5323. # add VIEW at the end as they need all tables to be created before
  5324. @tablenames = (@tablenames,sort(keys(%db_tables_views)));
  5325. %db_tables = (%db_tables,%db_tables_views);
  5326. $tablename = '';
  5327. if (@tablenames < 1) {
  5328. $err = "There are no tables inside database $dbname ! It doesn't make sense to backup an empty database. Skipping this one.";
  5329. Log3 ($name, 2, "DbRep $name - $err");
  5330. $err = encode_base64($@,"");
  5331. $dbh->disconnect;
  5332. return "$name|''|$err|''|''|''|''|''|''|''";
  5333. }
  5334. if($optimize_tables_beforedump) {
  5335. # Tabellen optimieren vor dem Dump
  5336. $hash->{HELPER}{DBTABLES} = \%db_tables;
  5337. ($err,$db_MB_start,$db_MB_end) = DbRep_mysqlOptimizeTables($hash,$dbh,@tablenames);
  5338. if ($err) {
  5339. $err = encode_base64($err,"");
  5340. return "$name|''|$err|''|''|''|''|''|''|''";
  5341. }
  5342. }
  5343. # Tabelleneigenschaften für SQL-File ermitteln
  5344. $st_e .= "-- TABLE-INFO\n";
  5345. foreach $tablename (@tablenames) {
  5346. my $dump_table = 1;
  5347. if ($dbpraefix ne "") {
  5348. if (substr($tablename,0,length($dbpraefix)) ne $dbpraefix) {
  5349. # exclude table from backup because it doesn't fit to praefix
  5350. $dump_table = 0;
  5351. }
  5352. }
  5353. if ($dump_table == 1) {
  5354. # how many rows
  5355. $sql_create = "SELECT count(*) FROM `$tablename`";
  5356. eval { $sth = $dbh->prepare($sql_create);
  5357. $sth->execute;
  5358. };
  5359. if ($@) {
  5360. $e = $@;
  5361. $err = "Fatal error sending Query '".$sql_create."' ! MySQL-Error: ".$e;
  5362. Log3 ($name, 2, "DbRep $name - $err");
  5363. $err = encode_base64($e,"");
  5364. $dbh->disconnect;
  5365. return "$name|''|$err|''|''|''|''|''|''|''";
  5366. }
  5367. $db_tables{$tablename}{Rows} = $sth->fetchrow;
  5368. $sth->finish;
  5369. $r += $db_tables{$tablename}{Rows};
  5370. push(@tables,$db_tables{$tablename}{Name}); # add tablename to backuped tables
  5371. $t++;
  5372. if (!defined $db_tables{$tablename}{Update_time}) {
  5373. $db_tables{$tablename}{Update_time} = 0;
  5374. }
  5375. $st_e .= $mysql_commentstring."TABLE: $db_tables{$tablename}{Name} | Rows: $db_tables{$tablename}{Rows} | Length: ".($db_tables{$tablename}{Data_length}+$db_tables{$tablename}{Index_length})." | Engine: $db_tables{$tablename}{Engine}\n";
  5376. if($db_tables{$tablename}{Name} eq "current") {
  5377. $drc = $db_tables{$tablename}{Rows};
  5378. }
  5379. if($db_tables{$tablename}{Name} eq "history") {
  5380. $drh = $db_tables{$tablename}{Rows};
  5381. }
  5382. }
  5383. }
  5384. $st_e .= "-- EOF TABLE-INFO";
  5385. Log3 ($name, 3, "DbRep $name - Found ".(@tables)." tables with $r records.");
  5386. # AUFBAU der Statuszeile in SQL-File:
  5387. # -- Status | tabellenzahl | datensaetze | Datenbankname | Kommentar | MySQLVersion | Charset | EXTINFO
  5388. #
  5389. $status_start = $mysql_commentstring."Status | Tables: $t | Rows: $r ";
  5390. $status_end = "| DB: $dbname | Comment: $my_comment | MySQL-Version: $mysql_version[0] ";
  5391. $status_end .= "| Charset: $character_set $st_e\n".
  5392. $mysql_commentstring."Dump created on $CTIME_String by DbRep-Version $repver\n".$mysql_commentstring;
  5393. $sql_text = $status_start.$status_end;
  5394. # neues SQL-Ausgabefile anlegen
  5395. ($sql_text,$first_insert,$sql_file,$backupfile,$err) = DbRep_NewDumpFilename($sql_text,$dump_path,$dbname,$time_stamp,$character_set);
  5396. if ($err) {
  5397. Log3 ($name, 2, "DbRep $name - $err");
  5398. $err = encode_base64($err,"");
  5399. return "$name|''|$err|''|''|''|''|''|''|''";
  5400. } else {
  5401. Log3 ($name, 5, "DbRep $name - New dumpfile $sql_file has been created.");
  5402. }
  5403. ##################### jede einzelne Tabelle dumpen ########################
  5404. $totalrecords = 0;
  5405. foreach $tablename (@tables) {
  5406. # first get CREATE TABLE Statement
  5407. if($dbpraefix eq "" || ($dbpraefix ne "" && substr($tablename,0,length($dbpraefix)) eq $dbpraefix)) {
  5408. Log3 ($name, 3, "DbRep $name - Dumping table $tablename (Type ".$db_tables{$tablename}{Engine}."):");
  5409. $a = "\n\n$mysql_commentstring\n$mysql_commentstring"."Table structure for table `$tablename`\n$mysql_commentstring\n";
  5410. if ($db_tables{$tablename}{Engine} ne 'VIEW' ) {
  5411. $a .= "DROP TABLE IF EXISTS `$tablename`;\n";
  5412. } else {
  5413. $a .= "DROP VIEW IF EXISTS `$tablename`;\n";
  5414. }
  5415. $sql_text .= $a;
  5416. $sql_create = "SHOW CREATE TABLE `$tablename`";
  5417. Log3 ($name, 5, "DbRep $name - current query: $sql_create ");
  5418. eval { $sth = $dbh->prepare($sql_create);
  5419. $sth->execute;
  5420. };
  5421. if ($@) {
  5422. $e = $@;
  5423. $err = "Fatal error sending Query '".$sql_create."' ! MySQL-Error: ".$e;
  5424. Log3 ($name, 2, "DbRep $name - $err");
  5425. $err = encode_base64($e,"");
  5426. $dbh->disconnect;
  5427. return "$name|''|$err|''|''|''|''|''|''|''";
  5428. }
  5429. @ergebnis = $sth->fetchrow;
  5430. $sth->finish;
  5431. $a = $ergebnis[1].";\n";
  5432. if (length($a) < 10) {
  5433. $err = "Fatal error! Couldn't read CREATE-Statement of table `$tablename`! This backup might be incomplete! Check your database for errors. MySQL-Error: ".$DBI::errstr;
  5434. Log3 ($name, 2, "DbRep $name - $err");
  5435. } else {
  5436. $sql_text .= $a;
  5437. # verbose 5 logging
  5438. Log3 ($name, 5, "DbRep $name - Create-SQL found:\n$a");
  5439. }
  5440. if ($db_tables{$tablename}{skip_data} == 0) {
  5441. $sql_text .= "\n$mysql_commentstring\n$mysql_commentstring"."Dumping data for table `$tablename`\n$mysql_commentstring\n";
  5442. $sql_text .= "/*!40000 ALTER TABLE `$tablename` DISABLE KEYS */;";
  5443. DbRep_WriteToDumpFile($sql_text,$sql_file);
  5444. $sql_text = "";
  5445. # build fieldlist
  5446. $fieldlist = "(";
  5447. $sql_create = "SHOW FIELDS FROM `$tablename`";
  5448. Log3 ($name, 5, "DbRep $name - current query: $sql_create ");
  5449. eval { $sth = $dbh->prepare($sql_create);
  5450. $sth->execute;
  5451. };
  5452. if ($@) {
  5453. $e = $@;
  5454. $err = "Fatal error sending Query '".$sql_create."' ! MySQL-Error: ".$e;
  5455. Log3 ($name, 2, "DbRep $name - $err");
  5456. $err = encode_base64($e,"");
  5457. $dbh->disconnect;
  5458. return "$name|''|$err|''|''|''|''|''|''|''";
  5459. }
  5460. while (@ar = $sth->fetchrow) {
  5461. $fieldlist .= "`".$ar[0]."`,";
  5462. }
  5463. $sth->finish;
  5464. # verbose 5 logging
  5465. Log3 ($name, 5, "DbRep $name - Fieldlist found: $fieldlist");
  5466. # remove trailing ',' and add ')'
  5467. $fieldlist = substr($fieldlist,0,length($fieldlist)-1).")";
  5468. # how many rows
  5469. $rct = $db_tables{$tablename}{Rows};
  5470. Log3 ($name, 5, "DbRep $name - Number entries of table $tablename: $rct");
  5471. # create insert Statements
  5472. for (my $ttt = 0; $ttt < $rct; $ttt += $dumpspeed) {
  5473. # default beginning for INSERT-String
  5474. $insert = "INSERT INTO `$tablename` $fieldlist VALUES (";
  5475. $first_insert = 0;
  5476. # get rows (parts)
  5477. $sql_daten = "SELECT * FROM `$tablename` LIMIT ".$ttt.",".$dumpspeed.";";
  5478. eval { $sth = $dbh->prepare($sql_daten);
  5479. $sth->execute;
  5480. };
  5481. if ($@) {
  5482. $e = $@;
  5483. $err = "Fatal error sending Query '".$sql_daten."' ! MySQL-Error: ".$e;
  5484. Log3 ($name, 2, "DbRep $name - $err");
  5485. $err = encode_base64($e,"");
  5486. $dbh->disconnect;
  5487. return "$name|''|$err|''|''|''|''|''|''|''";
  5488. }
  5489. while ( @ar = $sth->fetchrow) {
  5490. #Start the insert
  5491. if($first_insert == 0) {
  5492. $a = "\n$insert";
  5493. } else {
  5494. $a = "\n(";
  5495. }
  5496. # quote all values
  5497. foreach $inhalt(@ar) { $a .= $dbh->quote($inhalt).","; }
  5498. # remove trailing ',' and add end-sql
  5499. $a = substr($a,0, length($a)-1).");";
  5500. $sql_text .= $a;
  5501. if($memory_limit > 0 && length($sql_text) > $memory_limit) {
  5502. ($filesize,$err) = DbRep_WriteToDumpFile($sql_text,$sql_file);
  5503. # Log3 ($name, 5, "DbRep $name - Memory limit '$memory_limit' exceeded. Wrote to '$sql_file'. Filesize: '".DbRep_byteOutput($filesize)."'");
  5504. $sql_text = "";
  5505. }
  5506. }
  5507. $sth->finish;
  5508. }
  5509. $sql_text .= "\n/*!40000 ALTER TABLE `$tablename` ENABLE KEYS */;\n";
  5510. }
  5511. # write sql commands to file
  5512. ($filesize,$err) = DbRep_WriteToDumpFile($sql_text,$sql_file);
  5513. $sql_text = "";
  5514. if ($db_tables{$tablename}{skip_data} == 0) {
  5515. Log3 ($name, 3, "DbRep $name - $rct records inserted (size of backupfile: ".DbRep_byteOutput($filesize).")") if($filesize);
  5516. $totalrecords += $rct;
  5517. } else {
  5518. Log3 ($name, 3, "DbRep $name - Dumping structure of $tablename (Type ".$db_tables{$tablename}{Engine}." ) (size of backupfile: ".DbRep_byteOutput($filesize).")");
  5519. }
  5520. }
  5521. }
  5522. # end
  5523. DbRep_WriteToDumpFile("\nSET FOREIGN_KEY_CHECKS=1;\n",$sql_file);
  5524. ($filesize,$err) = DbRep_WriteToDumpFile($mysql_commentstring."EOB\n",$sql_file);
  5525. # Datenbankverbindung schliessen
  5526. $sth->finish() if (defined $sth);
  5527. $dbh->disconnect();
  5528. # SQL-Laufzeit ermitteln
  5529. my $rt = tv_interval($st);
  5530. # Dumpfile komprimieren wenn dumpCompress=1
  5531. my $compress = AttrVal($name,"dumpCompress",0);
  5532. if($compress) {
  5533. # $err nicht auswerten -> wenn compress fehlerhaft wird unkomprimiertes dumpfile verwendet
  5534. ($err,$backupfile) = DbRep_dumpCompress($hash,$backupfile);
  5535. my $fref = stat("$dump_path$backupfile");
  5536. if ($fref =~ /ARRAY/) {
  5537. $filesize = (@{stat("$dump_path$backupfile")})[7];
  5538. } else {
  5539. $filesize = (stat("$dump_path$backupfile"))[7];
  5540. }
  5541. }
  5542. # Dumpfile per FTP senden und versionieren
  5543. my ($ftperr,$ftpmsg,@ftpfd) = DbRep_sendftp($hash,$backupfile);
  5544. my $ftp = $ftperr?encode_base64($ftperr,""):$ftpmsg?encode_base64($ftpmsg,""):0;
  5545. my $ffd = join(", ", @ftpfd);
  5546. $ffd = $ffd?encode_base64($ffd,""):0;
  5547. # alte Dumpfiles löschen
  5548. my @fd = DbRep_deldumpfiles($hash,$backupfile);
  5549. my $bfd = join(", ", @fd );
  5550. $bfd = $bfd?encode_base64($bfd,""):0;
  5551. # Background-Laufzeit ermitteln
  5552. my $brt = tv_interval($bst);
  5553. $rt = $rt.",".$brt;
  5554. my $fsize = '';
  5555. if($filesize) {
  5556. $fsize = DbRep_byteOutput($filesize);
  5557. $fsize = encode_base64($fsize,"");
  5558. }
  5559. Log3 ($name, 3, "DbRep $name - Finished backup of database $dbname, total time used: ".sprintf("%.0f",$brt)." sec.");
  5560. return "$name|$rt|''|$dump_path$backupfile|$drc|$drh|$fsize|$ftp|$bfd|$ffd";
  5561. }
  5562. ####################################################################################################
  5563. # nicht blockierende Dump-Routine für MySQL (serverSide)
  5564. ####################################################################################################
  5565. sub mysql_DoDumpServerSide($) {
  5566. my ($name) = @_;
  5567. my $hash = $defs{$name};
  5568. my $dbloghash = $hash->{dbloghash};
  5569. my $dbconn = $dbloghash->{dbconn};
  5570. my $dbuser = $dbloghash->{dbuser};
  5571. my $dblogname = $dbloghash->{NAME};
  5572. my $dbpassword = $attr{"sec$dblogname"}{secret};
  5573. my $dbname = $hash->{DATABASE};
  5574. my $optimize_tables_beforedump = AttrVal($name, "optimizeTablesBeforeDump", 0);
  5575. my $dump_path_rem = AttrVal($name, "dumpDirRemote", "./");
  5576. $dump_path_rem = $dump_path_rem."/" unless($dump_path_rem =~ m/\/$/);
  5577. my $ebd = AttrVal($name, "executeBeforeProc", undef);
  5578. my $ead = AttrVal($name, "executeAfterProc", undef);
  5579. my $table = "history";
  5580. my ($dbh,$sth,$err,$db_MB_start,$db_MB_end,$drh);
  5581. my (%db_tables,@tablenames);
  5582. # Background-Startzeit
  5583. my $bst = [gettimeofday];
  5584. # Verbindung mit DB
  5585. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1 });};
  5586. if ($@) {
  5587. $err = encode_base64($@,"");
  5588. Log3 ($name, 2, "DbRep $name - $@");
  5589. return "$name|''|$err|''|''|''|''|''|''|''";
  5590. }
  5591. # Eigenschaften der vorhandenen Tabellen ermitteln (SHOW TABLE STATUS -> Rows sind nicht exakt !!)
  5592. my $value = 0;
  5593. my $query ="SHOW TABLE STATUS FROM `$dbname`";
  5594. Log3 ($name, 5, "DbRep $name - current query: $query ");
  5595. Log3 ($name, 3, "DbRep $name - Searching for tables inside database $dbname....");
  5596. eval { $sth = $dbh->prepare($query);
  5597. $sth->execute;
  5598. };
  5599. if ($@) {
  5600. $err = encode_base64($@,"");
  5601. Log3 ($name, 2, "DbRep $name - Error executing: '".$query."' ! MySQL-Error: ".$@);
  5602. $dbh->disconnect;
  5603. return "$name|''|$err|''|''|''|''|''|''|''";
  5604. }
  5605. while ( $value = $sth->fetchrow_hashref()) {
  5606. # verbose 5 logging
  5607. Log3 ($name, 5, "DbRep $name - ......... Table definition found: .........");
  5608. foreach my $tk (sort(keys(%$value))) {
  5609. Log3 ($name, 5, "DbRep $name - $tk: $value->{$tk}") if(defined($value->{$tk}) && $tk ne "Rows");
  5610. }
  5611. Log3 ($name, 5, "DbRep $name - ......... Table definition END ............");
  5612. # check for old MySQL3-Syntax Type=xxx
  5613. if (defined $value->{Type}) {
  5614. # port old index type to index engine, so we can use the index Engine in the rest of the script
  5615. $value->{Engine} = $value->{Type};
  5616. }
  5617. $db_tables{$value->{Name}} = $value;
  5618. }
  5619. $sth->finish;
  5620. @tablenames = sort(keys(%db_tables));
  5621. if (@tablenames < 1) {
  5622. $err = "There are no tables inside database $dbname ! It doesn't make sense to backup an empty database. Skipping this one.";
  5623. Log3 ($name, 2, "DbRep $name - $err");
  5624. $err = encode_base64($@,"");
  5625. $dbh->disconnect;
  5626. return "$name|''|$err|''|''|''|''|''|''|''";
  5627. }
  5628. if($optimize_tables_beforedump) {
  5629. # Tabellen optimieren vor dem Dump
  5630. $hash->{HELPER}{DBTABLES} = \%db_tables;
  5631. ($err,$db_MB_start,$db_MB_end) = DbRep_mysqlOptimizeTables($hash,$dbh,@tablenames);
  5632. if ($err) {
  5633. $err = encode_base64($err,"");
  5634. return "$name|''|$err|''|''|''|''|''|''|''";
  5635. }
  5636. }
  5637. Log3 ($name, 3, "DbRep $name - Starting dump of database '$dbname', table '$table'");
  5638. # Startzeit ermitteln
  5639. my ($Sekunden, $Minuten, $Stunden, $Monatstag, $Monat, $Jahr, $Wochentag, $Jahrestag, $Sommerzeit) = localtime(time);
  5640. $Jahr += 1900;
  5641. $Monat += 1;
  5642. $Jahrestag += 1;
  5643. my $time_stamp = $Jahr."_".sprintf("%02d",$Monat)."_".sprintf("%02d",$Monatstag)."_".sprintf("%02d",$Stunden)."_".sprintf("%02d",$Minuten);
  5644. my $bfile = $dbname."_".$table."_".$time_stamp.".csv";
  5645. Log3 ($name, 5, "DbRep $name - Use Outfile: $dump_path_rem$bfile");
  5646. # SQL-Startzeit
  5647. my $st = [gettimeofday];
  5648. my $sql = "SELECT * FROM history INTO OUTFILE '$dump_path_rem$bfile' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n'; ";
  5649. eval {$sth = $dbh->prepare($sql);
  5650. $drh = $sth->execute();
  5651. };
  5652. if ($@) {
  5653. # error bei sql-execute
  5654. $err = encode_base64($@,"");
  5655. Log3 ($name, 2, "DbRep $name - $@");
  5656. $dbh->disconnect;
  5657. return "$name|''|$err|''|''|''|''|''|''|''";
  5658. }
  5659. $sth->finish;
  5660. $dbh->disconnect;
  5661. # SQL-Laufzeit ermitteln
  5662. my $rt = tv_interval($st);
  5663. # Dumpfile komprimieren wenn dumpCompress=1
  5664. my $compress = AttrVal($name,"dumpCompress",0);
  5665. if($compress) {
  5666. # $err nicht auswerten -> wenn compress fehlerhaft wird unkomprimiertes dumpfile verwendet
  5667. ($err,$bfile) = DbRep_dumpCompress($hash,$bfile);
  5668. }
  5669. # Größe Dumpfile ermitteln ("dumpDirRemote" muß auf "dumpDirLocal" gemountet sein)
  5670. my $dump_path_def = $attr{global}{modpath}."/log/";
  5671. my $dump_path_loc = AttrVal($name,"dumpDirLocal", $dump_path_def);
  5672. $dump_path_loc = $dump_path_loc."/" unless($dump_path_loc =~ m/\/$/);
  5673. my $filesize;
  5674. my $fref = stat($dump_path_loc.$bfile);
  5675. if ($fref =~ /ARRAY/) {
  5676. $filesize = (@{stat($dump_path_loc.$bfile)})[7];
  5677. } else {
  5678. $filesize = (stat($dump_path_loc.$bfile))[7];
  5679. }
  5680. Log3 ($name, 3, "DbRep $name - Number of exported datasets: $drh");
  5681. Log3 ($name, 3, "DbRep $name - Size of backupfile: ".DbRep_byteOutput($filesize)) if($filesize);
  5682. # Dumpfile per FTP senden und versionieren
  5683. my ($ftperr,$ftpmsg,@ftpfd) = DbRep_sendftp($hash,$bfile);
  5684. my $ftp = $ftperr?encode_base64($ftperr,""):$ftpmsg?encode_base64($ftpmsg,""):0;
  5685. my $ffd = join(", ", @ftpfd);
  5686. $ffd = $ffd?encode_base64($ffd,""):0;
  5687. # alte Dumpfiles löschen
  5688. my @fd = DbRep_deldumpfiles($hash,$bfile);
  5689. my $bfd = join(", ", @fd );
  5690. $bfd = $bfd?encode_base64($bfd,""):0;
  5691. # Background-Laufzeit ermitteln
  5692. my $brt = tv_interval($bst);
  5693. my $fsize = '';
  5694. if($filesize) {
  5695. $fsize = DbRep_byteOutput($filesize);
  5696. $fsize = encode_base64($fsize,"");
  5697. }
  5698. $rt = $rt.",".$brt;
  5699. Log3 ($name, 3, "DbRep $name - Finished backup of database $dbname - total time used: ".sprintf("%.0f",$brt)." seconds");
  5700. return "$name|$rt|''|$dump_path_rem$bfile|n.a.|$drh|$fsize|$ftp|$bfd|$ffd";
  5701. }
  5702. ####################################################################################################
  5703. # Dump-Routine SQLite
  5704. ####################################################################################################
  5705. sub DbRep_sqliteDoDump($) {
  5706. my ($name) = @_;
  5707. my $hash = $defs{$name};
  5708. my $dbloghash = $hash->{dbloghash};
  5709. my $dbname = $hash->{DATABASE};
  5710. my $dbconn = $dbloghash->{dbconn};
  5711. my $dbuser = $dbloghash->{dbuser};
  5712. my $dblogname = $dbloghash->{NAME};
  5713. my $dbpassword = $attr{"sec$dblogname"}{secret};
  5714. my $dump_path_def = $attr{global}{modpath}."/log/";
  5715. my $dump_path = AttrVal($name, "dumpDirLocal", $dump_path_def);
  5716. $dump_path = $dump_path."/" unless($dump_path =~ m/\/$/);
  5717. my $optimize_tables_beforedump = AttrVal($name, "optimizeTablesBeforeDump", 0);
  5718. my $ebd = AttrVal($name, "executeBeforeProc", undef);
  5719. my $ead = AttrVal($name, "executeAfterProc", undef);
  5720. my ($dbh,$err,$db_MB,$r,$query,$sth);
  5721. # Background-Startzeit
  5722. my $bst = [gettimeofday];
  5723. # Verbindung mit DB
  5724. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1 });};
  5725. if ($@) {
  5726. $err = encode_base64($@,"");
  5727. Log3 ($name, 2, "DbRep $name - $@");
  5728. return "$name|''|$err|''|''|''|''|''|''|''";
  5729. }
  5730. if($optimize_tables_beforedump) {
  5731. # Vacuum vor Dump
  5732. # Anfangsgröße ermitteln
  5733. $db_MB = (split(' ',qx(du -m $dbname)))[0] if ($^O =~ m/linux/i || $^O =~ m/unix/i);
  5734. Log3 ($name, 3, "DbRep $name - Size of database $dbname before optimize (MB): $db_MB");
  5735. $query ="VACUUM";
  5736. Log3 ($name, 5, "DbRep $name - current query: $query ");
  5737. Log3 ($name, 3, "DbRep $name - VACUUM database $dbname....");
  5738. eval {$sth = $dbh->prepare($query);
  5739. $r = $sth->execute();
  5740. };
  5741. if ($@) {
  5742. $err = encode_base64($@,"");
  5743. Log3 ($name, 2, "DbRep $name - Error executing: '".$query."' ! SQLite-Error: ".$@);
  5744. $sth->finish;
  5745. $dbh->disconnect;
  5746. return "$name|''|$err|''|''|''|''|''|''|''";
  5747. }
  5748. # Endgröße ermitteln
  5749. $db_MB = (split(' ',qx(du -m $dbname)))[0] if ($^O =~ m/linux/i || $^O =~ m/unix/i);
  5750. Log3 ($name, 3, "DbRep $name - Size of database $dbname after optimize (MB): $db_MB");
  5751. }
  5752. $dbname = (split /[\/]/, $dbname)[-1];
  5753. Log3 ($name, 3, "DbRep $name - Starting dump of database '$dbname'");
  5754. # Startzeit ermitteln
  5755. my ($Sekunden, $Minuten, $Stunden, $Monatstag, $Monat, $Jahr, $Wochentag, $Jahrestag, $Sommerzeit) = localtime(time);
  5756. $Jahr += 1900;
  5757. $Monat += 1;
  5758. $Jahrestag += 1;
  5759. my $time_stamp = $Jahr."_".sprintf("%02d",$Monat)."_".sprintf("%02d",$Monatstag)."_".sprintf("%02d",$Stunden)."_".sprintf("%02d",$Minuten);
  5760. $dbname = (split /\./, $dbname)[0];
  5761. my $bfile = $dbname."_".$time_stamp.".sqlitebkp";
  5762. Log3 ($name, 5, "DbRep $name - Use Outfile: $dump_path$bfile");
  5763. # SQL-Startzeit
  5764. my $st = [gettimeofday];
  5765. eval { $dbh->sqlite_backup_to_file($dump_path.$bfile); };
  5766. if ($@) {
  5767. $err = encode_base64($@,"");
  5768. Log3 ($name, 2, "DbRep $name - $@");
  5769. $dbh->disconnect;
  5770. return "$name|''|$err|''|''|''|''|''|''|''";
  5771. }
  5772. $dbh->disconnect;
  5773. # SQL-Laufzeit ermitteln
  5774. my $rt = tv_interval($st);
  5775. # Dumpfile komprimieren
  5776. my $compress = AttrVal($name,"dumpCompress",0);
  5777. if($compress) {
  5778. # $err nicht auswerten -> wenn compress fehlerhaft wird unkomprimiertes dumpfile verwendet
  5779. ($err,$bfile) = DbRep_dumpCompress($hash,$bfile);
  5780. }
  5781. # Größe Dumpfile ermitteln
  5782. my @a = split(' ',qx(du $dump_path$bfile)) if ($^O =~ m/linux/i || $^O =~ m/unix/i);
  5783. my $filesize = ($a[0])?($a[0]*1024):"n.a.";
  5784. my $fsize = DbRep_byteOutput($filesize);
  5785. Log3 ($name, 3, "DbRep $name - Size of backupfile: ".$fsize);
  5786. # Dumpfile per FTP senden und versionieren
  5787. my ($ftperr,$ftpmsg,@ftpfd) = DbRep_sendftp($hash,$bfile);
  5788. my $ftp = $ftperr?encode_base64($ftperr,""):$ftpmsg?encode_base64($ftpmsg,""):0;
  5789. my $ffd = join(", ", @ftpfd);
  5790. $ffd = $ffd?encode_base64($ffd,""):0;
  5791. # alte Dumpfiles löschen
  5792. my @fd = DbRep_deldumpfiles($hash,$bfile);
  5793. my $bfd = join(", ", @fd );
  5794. $bfd = $bfd?encode_base64($bfd,""):0;
  5795. # Background-Laufzeit ermitteln
  5796. my $brt = tv_interval($bst);
  5797. $fsize = encode_base64($fsize,"");
  5798. $rt = $rt.",".$brt;
  5799. Log3 ($name, 3, "DbRep $name - Finished backup of database $dbname - total time used: ".sprintf("%.0f",$brt)." seconds");
  5800. return "$name|$rt|''|$dump_path$bfile|n.a.|n.a.|$fsize|$ftp|$bfd|$ffd";
  5801. }
  5802. ####################################################################################################
  5803. # Auswertungsroutine der nicht blockierenden DB-Funktion Dump
  5804. ####################################################################################################
  5805. sub DbRep_DumpDone($) {
  5806. my ($string) = @_;
  5807. my @a = split("\\|",$string);
  5808. my $hash = $defs{$a[0]};
  5809. my $bt = $a[1];
  5810. my ($rt,$brt) = split(",", $bt);
  5811. my $err = $a[2]?decode_base64($a[2]):undef;
  5812. my $bfile = $a[3];
  5813. my $drc = $a[4];
  5814. my $drh = $a[5];
  5815. my $fs = $a[6]?decode_base64($a[6]):undef;
  5816. my $ftp = $a[7]?decode_base64($a[7]):undef;
  5817. my $bfd = $a[8]?decode_base64($a[8]):undef;
  5818. my $ffd = $a[9]?decode_base64($a[9]):undef;
  5819. my $name = $hash->{NAME};
  5820. my $erread;
  5821. delete($hash->{HELPER}{RUNNING_BACKUP_CLIENT});
  5822. delete($hash->{HELPER}{RUNNING_BCKPREST_SERVER});
  5823. if ($err) {
  5824. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  5825. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  5826. return;
  5827. }
  5828. # only for this block because of warnings if details of readings are not set
  5829. no warnings 'uninitialized';
  5830. readingsBeginUpdate($hash);
  5831. ReadingsBulkUpdateValue($hash, "DumpFileCreated", $bfile);
  5832. ReadingsBulkUpdateValue($hash, "DumpFileCreatedSize", $fs);
  5833. ReadingsBulkUpdateValue($hash, "DumpFilesDeleted", $bfd);
  5834. ReadingsBulkUpdateValue($hash, "DumpRowsCurrent", $drc);
  5835. ReadingsBulkUpdateValue($hash, "DumpRowsHistory", $drh);
  5836. ReadingsBulkUpdateValue($hash, "FTP_Message", $ftp) if($ftp);
  5837. ReadingsBulkUpdateValue($hash, "FTP_DumpFilesDeleted", $ffd) if($ffd);
  5838. ReadingsBulkUpdateValue($hash, "background_processing_time", sprintf("%.4f",$brt));
  5839. readingsEndUpdate($hash, 1);
  5840. # Befehl nach Procedure ausführen
  5841. $erread = DbRep_afterproc($hash, "dump");
  5842. my $state = $erread?$erread:"Database backup finished";
  5843. readingsBeginUpdate($hash);
  5844. ReadingsBulkUpdateTimeState($hash,undef,undef,$state);
  5845. readingsEndUpdate($hash, 1);
  5846. Log3 ($name, 3, "DbRep $name - Database dump finished successfully. ");
  5847. return;
  5848. }
  5849. ####################################################################################################
  5850. # Dump-Routine SQLite
  5851. ####################################################################################################
  5852. sub DbRep_sqliteRepair($) {
  5853. my ($name) = @_;
  5854. my $hash = $defs{$name};
  5855. my $dbloghash = $hash->{dbloghash};
  5856. my $db = $hash->{DATABASE};
  5857. my $dbname = (split /[\/]/, $db)[-1];
  5858. my $dbpath = (split /$dbname/, $db)[0];
  5859. my $dblogname = $dbloghash->{NAME};
  5860. my $sqlfile = $dbpath."dump_all.sql";
  5861. my ($c,$clog,$ret,$err);
  5862. # Background-Startzeit
  5863. my $bst = [gettimeofday];
  5864. $c = "echo \".mode insert\n.output $sqlfile\n.dump\n.exit\" | sqlite3 $db; ";
  5865. $clog = $c;
  5866. $clog =~ s/\n/ /g;
  5867. Log3 ($name, 4, "DbRep $name - Systemcall: $clog");
  5868. $ret = system qq($c);
  5869. if($ret) {
  5870. $err = "Error in step \"dump corrupt database\" - see logfile";
  5871. $err = encode_base64($err,"");
  5872. return "$name|''|$err";
  5873. }
  5874. $c = "mv $db $db.corrupt";
  5875. $clog = $c;
  5876. $clog =~ s/\n/ /g;
  5877. Log3 ($name, 4, "DbRep $name - Systemcall: $clog");
  5878. $ret = system qq($c);
  5879. if($ret) {
  5880. $err = "Error in step \"move atabase to corrupt-db\" - see logfile";
  5881. $err = encode_base64($err,"");
  5882. return "$name|''|$err";
  5883. }
  5884. $c = "echo \".read $sqlfile\n.exit\" | sqlite3 $db;";
  5885. $clog = $c;
  5886. $clog =~ s/\n/ /g;
  5887. Log3 ($name, 4, "DbRep $name - Systemcall: $clog");
  5888. $ret = system qq($c);
  5889. if($ret) {
  5890. $err = "Error in step \"read dump to new database\" - see logfile";
  5891. $err = encode_base64($err,"");
  5892. return "$name|''|$err";
  5893. }
  5894. $c = "rm $sqlfile";
  5895. $clog = $c;
  5896. $clog =~ s/\n/ /g;
  5897. Log3 ($name, 4, "DbRep $name - Systemcall: $clog");
  5898. $ret = system qq($c);
  5899. if($ret) {
  5900. $err = "Error in step \"delete $sqlfile\" - see logfile";
  5901. $err = encode_base64($err,"");
  5902. return "$name|''|$err";
  5903. }
  5904. # Background-Laufzeit ermitteln
  5905. my $brt = tv_interval($bst);
  5906. return "$name|$brt|0";
  5907. }
  5908. ####################################################################################################
  5909. # Auswertungsroutine der nicht blockierenden DB-Funktion Dump
  5910. ####################################################################################################
  5911. sub DbRep_RepairDone($) {
  5912. my ($string) = @_;
  5913. my @a = split("\\|",$string);
  5914. my $hash = $defs{$a[0]};
  5915. my $brt = $a[1];
  5916. my $err = $a[2]?decode_base64($a[2]):undef;
  5917. my $dbloghash = $hash->{dbloghash};
  5918. my $name = $hash->{NAME};
  5919. my $erread;
  5920. delete($hash->{HELPER}{RUNNING_REPAIR});
  5921. # Datenbankverbindung in DbLog wieder öffenen
  5922. my $dbl = $dbloghash->{NAME};
  5923. CommandSet(undef,"$dbl reopen");
  5924. if ($err) {
  5925. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  5926. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  5927. return;
  5928. }
  5929. # only for this block because of warnings if details of readings are not set
  5930. no warnings 'uninitialized';
  5931. readingsBeginUpdate($hash);
  5932. ReadingsBulkUpdateValue($hash, "background_processing_time", sprintf("%.4f",$brt));
  5933. readingsEndUpdate($hash, 1);
  5934. # Befehl nach Procedure ausführen
  5935. $erread = DbRep_afterproc($hash, "repair");
  5936. my $state = $erread?$erread:"Repair finished $hash->{DATABASE}";
  5937. readingsBeginUpdate($hash);
  5938. ReadingsBulkUpdateTimeState($hash,undef,undef,$state);
  5939. readingsEndUpdate($hash, 1);
  5940. Log3 ($name, 3, "DbRep $name - Database repair $hash->{DATABASE} finished. - total time used: ".sprintf("%.0f",$brt)." seconds.");
  5941. return;
  5942. }
  5943. ####################################################################################################
  5944. # Restore SQLite
  5945. ####################################################################################################
  5946. sub DbRep_sqliteRestore ($) {
  5947. my ($string) = @_;
  5948. my ($name,$bfile) = split("\\|", $string);
  5949. my $hash = $defs{$name};
  5950. my $dbloghash = $hash->{dbloghash};
  5951. my $dbconn = $dbloghash->{dbconn};
  5952. my $dbuser = $dbloghash->{dbuser};
  5953. my $dblogname = $dbloghash->{NAME};
  5954. my $dbpassword = $attr{"sec$dblogname"}{secret};
  5955. my $dump_path_def = $attr{global}{modpath}."/log/";
  5956. my $dump_path = AttrVal($name, "dumpDirLocal", $dump_path_def);
  5957. $dump_path = $dump_path."/" unless($dump_path =~ m/\/$/);
  5958. my $ebd = AttrVal($name, "executeBeforeProc", undef);
  5959. my $ead = AttrVal($name, "executeAfterProc", undef);
  5960. my ($dbh,$err,$dbname);
  5961. # Background-Startzeit
  5962. my $bst = [gettimeofday];
  5963. # Verbindung mit DB
  5964. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1 });};
  5965. if ($@) {
  5966. $err = encode_base64($@,"");
  5967. Log3 ($name, 2, "DbRep $name - $@");
  5968. return "$name|''|$err|''|''";
  5969. }
  5970. eval { $dbname = $dbh->sqlite_db_filename(); };
  5971. if ($@) {
  5972. $err = encode_base64($@,"");
  5973. Log3 ($name, 2, "DbRep $name - $@");
  5974. $dbh->disconnect;
  5975. return "$name|''|$err|''|''";
  5976. }
  5977. $dbname = (split /[\/]/, $dbname)[-1];
  5978. # Dumpfile dekomprimieren wenn gzip
  5979. if($bfile =~ m/.*.gzip$/) {
  5980. ($err,$bfile) = DbRep_dumpUnCompress($hash,$bfile);
  5981. if ($err) {
  5982. $err = encode_base64($err,"");
  5983. $dbh->disconnect;
  5984. return "$name|''|$err|''|''";
  5985. }
  5986. }
  5987. Log3 ($name, 3, "DbRep $name - Starting restore of database '$dbname'");
  5988. # SQL-Startzeit
  5989. my $st = [gettimeofday];
  5990. eval { $dbh->sqlite_backup_from_file($dump_path.$bfile); };
  5991. if ($@) {
  5992. $err = encode_base64($@,"");
  5993. Log3 ($name, 2, "DbRep $name - $@");
  5994. $dbh->disconnect;
  5995. return "$name|''|$err|''|''";
  5996. }
  5997. $dbh->disconnect;
  5998. # SQL-Laufzeit ermitteln
  5999. my $rt = tv_interval($st);
  6000. # Background-Laufzeit ermitteln
  6001. my $brt = tv_interval($bst);
  6002. $rt = $rt.",".$brt;
  6003. Log3 ($name, 3, "DbRep $name - Restore of $dump_path$bfile into '$dbname' finished - total time used: ".sprintf("%.0f",$brt)." seconds.");
  6004. return "$name|$rt|''|$dump_path$bfile|n.a.";
  6005. }
  6006. ####################################################################################################
  6007. # Restore MySQL (serverSide)
  6008. ####################################################################################################
  6009. sub mysql_RestoreServerSide($) {
  6010. my ($string) = @_;
  6011. my ($name, $bfile) = split("\\|", $string);
  6012. my $hash = $defs{$name};
  6013. my $dbloghash = $hash->{dbloghash};
  6014. my $dbconn = $dbloghash->{dbconn};
  6015. my $dbuser = $dbloghash->{dbuser};
  6016. my $dblogname = $dbloghash->{NAME};
  6017. my $dbpassword = $attr{"sec$dblogname"}{secret};
  6018. my $dbname = $hash->{DATABASE};
  6019. my $dump_path_rem = AttrVal($name, "dumpDirRemote", "./");
  6020. $dump_path_rem = $dump_path_rem."/" unless($dump_path_rem =~ m/\/$/);
  6021. my $table = "history";
  6022. my ($dbh,$sth,$err,$drh);
  6023. # Background-Startzeit
  6024. my $bst = [gettimeofday];
  6025. # Verbindung mit DB
  6026. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1 });};
  6027. if ($@) {
  6028. $err = encode_base64($@,"");
  6029. Log3 ($name, 2, "DbRep $name - $@");
  6030. return "$name|''|$err|''|''";
  6031. }
  6032. # Dumpfile dekomprimieren wenn gzip
  6033. if($bfile =~ m/.*.gzip$/) {
  6034. ($err,$bfile) = DbRep_dumpUnCompress($hash,$bfile);
  6035. if ($err) {
  6036. $err = encode_base64($err,"");
  6037. $dbh->disconnect;
  6038. return "$name|''|$err|''|''";
  6039. }
  6040. }
  6041. Log3 ($name, 3, "DbRep $name - Starting restore of database '$dbname', table '$table'.");
  6042. # SQL-Startzeit
  6043. my $st = [gettimeofday];
  6044. my $sql = "LOAD DATA CONCURRENT INFILE '$dump_path_rem$bfile' IGNORE INTO TABLE $table FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n'; ";
  6045. eval {$sth = $dbh->prepare($sql);
  6046. $drh = $sth->execute();
  6047. };
  6048. if ($@) {
  6049. # error bei sql-execute
  6050. $err = encode_base64($@,"");
  6051. Log3 ($name, 2, "DbRep $name - $@");
  6052. $dbh->disconnect;
  6053. return "$name|''|$err|''|''";
  6054. }
  6055. $sth->finish;
  6056. $dbh->disconnect;
  6057. # SQL-Laufzeit ermitteln
  6058. my $rt = tv_interval($st);
  6059. # Background-Laufzeit ermitteln
  6060. my $brt = tv_interval($bst);
  6061. $rt = $rt.",".$brt;
  6062. Log3 ($name, 3, "DbRep $name - Restore of $dump_path_rem$bfile into '$dbname', '$table' finished - total time used: ".sprintf("%.0f",$brt)." seconds.");
  6063. return "$name|$rt|''|$dump_path_rem$bfile|n.a.";
  6064. }
  6065. ####################################################################################################
  6066. # Restore MySQL (ClientSide)
  6067. ####################################################################################################
  6068. sub mysql_RestoreClientSide($) {
  6069. my ($string) = @_;
  6070. my ($name, $bfile) = split("\\|", $string);
  6071. my $hash = $defs{$name};
  6072. my $dbloghash = $hash->{dbloghash};
  6073. my $dbconn = $dbloghash->{dbconn};
  6074. my $dbuser = $dbloghash->{dbuser};
  6075. my $dblogname = $dbloghash->{NAME};
  6076. my $dbpassword = $attr{"sec$dblogname"}{secret};
  6077. my $dbname = $hash->{DATABASE};
  6078. my $i_max = AttrVal($name, "dumpMemlimit", 100000); # max. Anzahl der Blockinserts
  6079. my $dump_path_def = $attr{global}{modpath}."/log/";
  6080. my $dump_path = AttrVal($name, "dumpDirLocal", $dump_path_def);
  6081. $dump_path = $dump_path."/" if($dump_path !~ /.*\/$/);
  6082. my ($dbh,$err,$v1,$v2,$e);
  6083. # Background-Startzeit
  6084. my $bst = [gettimeofday];
  6085. # Verbindung mit DB
  6086. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoCommit => 1 });};
  6087. if ($@) {
  6088. $e = $@;
  6089. $err = encode_base64($e,"");
  6090. Log3 ($name, 1, "DbRep $name - $e");
  6091. return "$name|''|$err|''|''";
  6092. }
  6093. # maximal mögliche Packetgröße ermitteln (in Bits) -> Umrechnen in max. Zeichen
  6094. my @row_ary;
  6095. my $sql = "show variables like 'max_allowed_packet'";
  6096. eval {@row_ary = $dbh->selectrow_array($sql);};
  6097. my $max_packets = $row_ary[1]; # Bits
  6098. $i_max = ($max_packets/8)-500; # Characters mit Sicherheitszuschlag
  6099. # Dumpfile dekomprimieren wenn gzip
  6100. if($bfile =~ m/.*.gzip$/) {
  6101. ($err,$bfile) = DbRep_dumpUnCompress($hash,$bfile);
  6102. if ($err) {
  6103. $err = encode_base64($err,"");
  6104. $dbh->disconnect;
  6105. return "$name|''|$err|''|''";
  6106. }
  6107. }
  6108. if(!open(FH, "<$dump_path$bfile")) {
  6109. $err = encode_base64("could not open ".$dump_path.$bfile.": ".$!,"");
  6110. return "$name|''|''|$err|''";
  6111. }
  6112. Log3 ($name, 3, "DbRep $name - Restore of database '$dbname' started. Sourcefile: $dump_path$bfile");
  6113. Log3 ($name, 3, "DbRep $name - Max packet lenght of insert statement: $i_max");
  6114. # SQL-Startzeit
  6115. my $st = [gettimeofday];
  6116. my $nc = 0; # Insert Zähler current
  6117. my $nh = 0; # Insert Zähler history
  6118. my $n = 0; # Insert Zähler
  6119. my $i = 0; # Array Zähler
  6120. my $tmp = '';
  6121. my $line = '';
  6122. my $base_query = '';
  6123. my $query = '';
  6124. while(<FH>) {
  6125. $tmp = $_;
  6126. chomp($tmp);
  6127. if(!$tmp || substr($tmp,0,2) eq "--") {
  6128. next;
  6129. }
  6130. $line .= $tmp;
  6131. if(substr($line,-1) eq ";") {
  6132. if($line !~ /^INSERT INTO.*$/) {
  6133. eval {$dbh->do($line);
  6134. };
  6135. if ($@) {
  6136. $e = $@;
  6137. $err = encode_base64($e,"");
  6138. Log3 ($name, 1, "DbRep $name - last query: $line");
  6139. Log3 ($name, 1, "DbRep $name - $e");
  6140. close(FH);
  6141. $dbh->disconnect;
  6142. return "$name|''|$err|''|''";
  6143. }
  6144. $line = '';
  6145. next;
  6146. }
  6147. if(!$base_query) {
  6148. $line =~ /INSERT INTO (.*) VALUES \((.*)\);/;
  6149. $v1 = $1;
  6150. $v2 = $2;
  6151. $base_query = qq{INSERT INTO $v1 VALUES };
  6152. $query = $base_query;
  6153. $nc++ if($base_query =~ /INSERT INTO `current`.*/);
  6154. $nh++ if($base_query =~ /INSERT INTO `history`.*/);
  6155. $query .= "," if($i);
  6156. $query .= "(".$v2.")";
  6157. $i++;
  6158. } else {
  6159. $line =~ /INSERT INTO (.*) VALUES \((.*)\);/;
  6160. $v1 = $1;
  6161. $v2 = $2;
  6162. my $ln = qq{INSERT INTO $v1 VALUES };
  6163. if($base_query eq $ln) {
  6164. $nc++ if($base_query =~ /INSERT INTO `current`.*/);
  6165. $nh++ if($base_query =~ /INSERT INTO `history`.*/);
  6166. $query .= "," if($i);
  6167. $query .= "(".$v2.")";
  6168. $i++;
  6169. } else {
  6170. $query = $query.";";
  6171. eval {$dbh->do($query);
  6172. };
  6173. if ($@) {
  6174. $e = $@;
  6175. $err = encode_base64($e,"");
  6176. Log3 ($name, 1, "DbRep $name - last query: $query");
  6177. Log3 ($name, 1, "DbRep $name - $e");
  6178. close(FH);
  6179. $dbh->disconnect;
  6180. return "$name|''|$err|''|''";
  6181. }
  6182. $i = 0;
  6183. $line =~ /INSERT INTO (.*) VALUES \((.*)\);/;
  6184. $v1 = $1;
  6185. $v2 = $2;
  6186. $base_query = qq{INSERT INTO $v1 VALUES };
  6187. $query = $base_query;
  6188. $query .= "(".$v2.")";
  6189. $nc++ if($base_query =~ /INSERT INTO `current`.*/);
  6190. $nh++ if($base_query =~ /INSERT INTO `history`.*/);
  6191. $i++;
  6192. }
  6193. }
  6194. if(length($query) >= $i_max) {
  6195. $query = $query.";";
  6196. eval {$dbh->do($query);
  6197. };
  6198. if ($@) {
  6199. $e = $@;
  6200. $err = encode_base64($e,"");
  6201. Log3 ($name, 1, "DbRep $name - last query: $query");
  6202. Log3 ($name, 1, "DbRep $name - $e");
  6203. close(FH);
  6204. $dbh->disconnect;
  6205. return "$name|''|$err|''|''";
  6206. }
  6207. $i = 0;
  6208. $query = '';
  6209. $base_query = '';
  6210. }
  6211. $line = '';
  6212. }
  6213. }
  6214. eval { $dbh->do($query) if($i);
  6215. };
  6216. if ($@) {
  6217. $e = $@;
  6218. $err = encode_base64($e,"");
  6219. Log3 ($name, 1, "DbRep $name - last query: $query");
  6220. Log3 ($name, 1, "DbRep $name - $e");
  6221. close(FH);
  6222. $dbh->disconnect;
  6223. return "$name|''|$err|''|''";
  6224. }
  6225. $dbh->disconnect;
  6226. close(FH);
  6227. # SQL-Laufzeit ermitteln
  6228. my $rt = tv_interval($st);
  6229. # Background-Laufzeit ermitteln
  6230. my $brt = tv_interval($bst);
  6231. $rt = $rt.",".$brt;
  6232. Log3 ($name, 3, "DbRep $name - Restore of '$dbname' finished - inserted history: $nh, inserted curent: $nc, time used: ".sprintf("%.0f",$brt)." seconds.");
  6233. return "$name|$rt|''|$dump_path$bfile|$nh|$nc";
  6234. }
  6235. ####################################################################################################
  6236. # Auswertungsroutine Restore
  6237. ####################################################################################################
  6238. sub DbRep_restoreDone($) {
  6239. my ($string) = @_;
  6240. my @a = split("\\|",$string);
  6241. my $hash = $defs{$a[0]};
  6242. my $bt = $a[1];
  6243. my ($rt,$brt) = split(",", $bt);
  6244. my $err = $a[2]?decode_base64($a[2]):undef;
  6245. my $bfile = $a[3];
  6246. my $drh = $a[4];
  6247. my $drc = $a[5];
  6248. my $name = $hash->{NAME};
  6249. my $erread;
  6250. delete($hash->{HELPER}{RUNNING_RESTORE});
  6251. if ($err) {
  6252. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  6253. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  6254. return;
  6255. }
  6256. readingsBeginUpdate($hash);
  6257. ReadingsBulkUpdateValue($hash, "RestoreRowsHistory", $drh) if($drh);
  6258. ReadingsBulkUpdateValue($hash, "RestoreRowsCurrent", $drc) if($drc);
  6259. readingsEndUpdate($hash, 1);
  6260. # Befehl nach Procedure ausführen
  6261. $erread = DbRep_afterproc($hash, "restore");
  6262. my $state = $erread?$erread:"Restore of $bfile finished";
  6263. readingsBeginUpdate($hash);
  6264. ReadingsBulkUpdateTimeState($hash,$brt,undef,$state);
  6265. readingsEndUpdate($hash, 1);
  6266. Log3 ($name, 3, "DbRep $name - Database restore finished successfully. ");
  6267. return;
  6268. }
  6269. ####################################################################################################
  6270. # Übertragung Datensätze in weitere DB
  6271. ####################################################################################################
  6272. sub DbRep_syncStandby($) {
  6273. my ($string) = @_;
  6274. my ($name,$device,$reading,$runtime_string_first,$runtime_string_next,$ts,$stbyname) = split("\\§", $string);
  6275. my $hash = $defs{$name};
  6276. my $table = "history";
  6277. my $utf8 = defined($hash->{UTF8})?$hash->{UTF8}:0;
  6278. my ($dbh,$dbhstby,$err,$sql,$irows,$irowdone);
  6279. # Quell-DB
  6280. my $dbloghash = $hash->{dbloghash};
  6281. my $dbconn = $dbloghash->{dbconn};
  6282. my $dbuser = $dbloghash->{dbuser};
  6283. my $dblogname = $dbloghash->{NAME};
  6284. my $dbpassword = $attr{"sec$dblogname"}{secret};
  6285. # Standby-DB
  6286. my $stbyhash = $defs{$stbyname};
  6287. my $stbyconn = $stbyhash->{dbconn};
  6288. my $stbyuser = $stbyhash->{dbuser};
  6289. my $stbypasswd = $attr{"sec$stbyname"}{secret};
  6290. my $stbyutf8 = defined($stbyhash->{UTF8})?$stbyhash->{UTF8}:0;
  6291. # Background-Startzeit
  6292. my $bst = [gettimeofday];
  6293. # Verbindung zur Quell-DB
  6294. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoCommit => 1, mysql_enable_utf8 => $utf8 });};
  6295. if ($@) {
  6296. $err = encode_base64($@,"");
  6297. Log3 ($name, 2, "DbRep $name - $@");
  6298. return "$name|''|''|$err";
  6299. }
  6300. # Verbindung zur Standby-DB
  6301. eval {$dbhstby = DBI->connect("dbi:$stbyconn", $stbyuser, $stbypasswd, { PrintError => 0, RaiseError => 1, AutoCommit => 1, mysql_enable_utf8 => $stbyutf8 });};
  6302. if ($@) {
  6303. $err = encode_base64($@,"");
  6304. Log3 ($name, 2, "DbRep $name - $@");
  6305. return "$name|''|''|$err";
  6306. }
  6307. # ist Zeiteingrenzung und/oder Aggregation gesetzt ? (wenn ja -> "?" in SQL sonst undef)
  6308. my ($IsTimeSet,$IsAggrSet) = DbRep_checktimeaggr($hash);
  6309. Log3 ($name, 5, "DbRep $name - IsTimeSet: $IsTimeSet, IsAggrSet: $IsAggrSet");
  6310. # SQL-Startzeit
  6311. my $st = [gettimeofday];
  6312. my ($sth,$old,$new);
  6313. eval { $dbh->begin_work() if($dbh->{AutoCommit}); }; # Transaktion wenn gewünscht und autocommit ein
  6314. if ($@) {
  6315. Log3($name, 2, "DbRep $name -> Error start transaction - $@");
  6316. }
  6317. # Timestampstring to Array
  6318. my @ts = split("\\|", $ts);
  6319. Log3 ($name, 5, "DbRep $name - Timestamp-Array: \n@ts");
  6320. # DB-Abfrage zeilenweise für jeden Array-Eintrag
  6321. $irows = 0;
  6322. $irowdone = 0;
  6323. my $selspec = "TIMESTAMP,DEVICE,TYPE,EVENT,READING,VALUE,UNIT";
  6324. my $addon = '';
  6325. foreach my $row (@ts) {
  6326. my @a = split("#", $row);
  6327. my $runtime_string = $a[0];
  6328. my $runtime_string_first = $a[1];
  6329. my $runtime_string_next = $a[2];
  6330. if ($IsTimeSet || $IsAggrSet) {
  6331. $sql = DbRep_createSelectSql($hash,"history",$selspec,$device,$reading,"'$runtime_string_first'","'$runtime_string_next'",$addon);
  6332. } else {
  6333. $sql = DbRep_createSelectSql($hash,"history",$selspec,$device,$reading,undef,undef,$addon);
  6334. }
  6335. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  6336. eval{ $sth = $dbh->prepare($sql);
  6337. $sth->execute();
  6338. };
  6339. if ($@) {
  6340. $err = encode_base64($@,"");
  6341. Log3 ($name, 2, "DbRep $name - $@");
  6342. $dbh->disconnect;
  6343. return "$name|''|''|$err";
  6344. }
  6345. no warnings 'uninitialized';
  6346. # DATE _ESC_ TIME _ESC_ DEVICE _ESC_ TYPE _ESC_ EVENT _ESC_ READING _ESC_ VALUE _ESC_ UNIT
  6347. my @row_array = map { ($_->[0] =~ s/ /_ESC_/r)."_ESC_".$_->[1]."_ESC_".$_->[2]."_ESC_".$_->[3]."_ESC_".$_->[4]."_ESC_".$_->[5]."_ESC_".$_->[6] } @{$sth->fetchall_arrayref()};
  6348. use warnings;
  6349. (undef,$irowdone,$err) = DbRep_WriteToDB($name,$dbhstby,$stbyhash,"0",@row_array) if(@row_array);
  6350. if ($err) {
  6351. Log3 ($name, 2, "DbRep $name - $err");
  6352. $err = encode_base64($err,"");
  6353. $dbh->disconnect;
  6354. $dbhstby->disconnect();
  6355. return "$name|''|''|$err";
  6356. }
  6357. $irows += $irowdone;
  6358. }
  6359. $dbh->disconnect();
  6360. $dbhstby->disconnect();
  6361. # SQL-Laufzeit ermitteln
  6362. my $rt = tv_interval($st);
  6363. # Background-Laufzeit ermitteln
  6364. my $brt = tv_interval($bst);
  6365. $rt = $rt.",".$brt;
  6366. return "$name|$irows|$rt|0";
  6367. }
  6368. ####################################################################################################
  6369. # Auswertungsroutine Übertragung Datensätze in weitere DB
  6370. ####################################################################################################
  6371. sub DbRep_syncStandbyDone($) {
  6372. my ($string) = @_;
  6373. my @a = split("\\|",$string);
  6374. my $hash = $defs{$a[0]};
  6375. my $name = $hash->{NAME};
  6376. my $irows = $a[1];
  6377. my $bt = $a[2];
  6378. my ($rt,$brt) = split(",", $bt);
  6379. my $err = $a[3]?decode_base64($a[3]):undef;
  6380. if ($err) {
  6381. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  6382. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  6383. delete($hash->{HELPER}{RUNNING_PID});
  6384. Log3 ($name, 4, "DbRep $name -> BlockingCall change_Done finished");
  6385. return;
  6386. }
  6387. # only for this block because of warnings if details of readings are not set
  6388. no warnings 'uninitialized';
  6389. readingsBeginUpdate($hash);
  6390. ReadingsBulkUpdateValue ($hash, "number_lines_inserted_Standby", $irows);
  6391. ReadingsBulkUpdateTimeState($hash,$brt,$rt,"done");
  6392. readingsEndUpdate($hash, 1);
  6393. # Befehl nach Procedure ausführen
  6394. my $erread = DbRep_afterproc($hash, "syncStandby");
  6395. delete($hash->{HELPER}{RUNNING_PID});
  6396. return;
  6397. }
  6398. ####################################################################################################
  6399. # reduceLog - Historische Werte ausduennen non-blocking > Forum #41089
  6400. #
  6401. # $ots - reduce Logs älter als: Attribut "timeOlderThan" oder "timestamp_begin"
  6402. # $nts - reduce Logs neuer als: Attribut "timeDiffToNow" oder "timestamp_end"
  6403. ####################################################################################################
  6404. sub DbRep_reduceLog($) {
  6405. my ($string) = @_;
  6406. my ($name,$d,$r,$nts,$ots) = split("\\|", $string);
  6407. my $hash = $defs{$name};
  6408. my $dbloghash = $hash->{dbloghash};
  6409. my $dbconn = $dbloghash->{dbconn};
  6410. my $dbuser = $dbloghash->{dbuser};
  6411. my $dblogname = $dbloghash->{NAME};
  6412. my $dbmodel = $dbloghash->{MODEL};
  6413. my $dbpassword = $attr{"sec$dblogname"}{secret};
  6414. my @a = @{$hash->{HELPER}{REDUCELOG}};
  6415. my $utf8 = defined($hash->{UTF8})?$hash->{UTF8}:0;
  6416. delete $hash->{HELPER}{REDUCELOG};
  6417. my ($ret,$row,$filter,$exclude,$c,$day,$hour,$lastHour,$updDate,$updHour,$average,$processingDay,$lastUpdH);
  6418. my (%hourlyKnown,%averageHash,@excludeRegex,@dayRows,@averageUpd,@averageUpdD);
  6419. my ($startTime,$currentHour,$currentDay,$deletedCount,$updateCount,$sum,$rowCount,$excludeCount) = (time(),99,0,0,0,0,0,0);
  6420. my ($dbh,$err,$brt);
  6421. Log3 ($name, 5, "DbRep $name -> Start DbLog_reduceLog");
  6422. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1, mysql_enable_utf8 => $utf8 });};
  6423. if ($@) {
  6424. $err = encode_base64($@,"");
  6425. Log3 ($name, 2, "DbRep $name - DbRep_reduceLog - $@");
  6426. return "$name|''|$err|''";
  6427. }
  6428. if ($a[-1] =~ /^EXCLUDE=(.+:.+)+/i) {
  6429. ($filter) = $a[-1] =~ /^EXCLUDE=(.+)/i;
  6430. @excludeRegex = split(',',$filter);
  6431. } elsif ($a[-1] =~ /^INCLUDE=.+:.+$/i) {
  6432. $filter = 1;
  6433. }
  6434. my ($idevs,$idanz,$ireading,$iranz,$edevs,$edanz,$ereading,$eranz) = DbRep_specsForSql($hash,$d,$r);
  6435. # ist Zeiteingrenzung und/oder Aggregation gesetzt ? (wenn ja -> "?" in SQL sonst undef)
  6436. my ($IsTimeSet,$IsAggrSet,$aggregation) = DbRep_checktimeaggr($hash);
  6437. Log3 ($name, 5, "DbRep $name - IsTimeSet: $IsTimeSet, IsAggrSet: $IsAggrSet");
  6438. my $sql;
  6439. my $table = "history";
  6440. my $selspec = "TIMESTAMP,DEVICE,'',READING,VALUE";
  6441. my $addon = "ORDER BY TIMESTAMP ASC";
  6442. if($filter) {
  6443. # Option EX/INCLUDE wurde angegeben
  6444. $sql = "SELECT TIMESTAMP,DEVICE,'',READING,VALUE FROM history WHERE "
  6445. .($a[-1] =~ /^INCLUDE=(.+):(.+)$/i ? "DEVICE like '$1' AND READING like '$2' AND " : '')
  6446. ."TIMESTAMP <= '$ots'".($nts?" AND TIMESTAMP >= '$nts' ":" ")."ORDER BY TIMESTAMP ASC";
  6447. } elsif ($IsTimeSet || $IsAggrSet) {
  6448. $sql = DbRep_createSelectSql($hash,$table,$selspec,$d,$r,"'$nts'","'$ots'",$addon);
  6449. } else {
  6450. $sql = DbRep_createSelectSql($hash,$table,$selspec,$d,$r,undef,undef,$addon);
  6451. }
  6452. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  6453. if (defined($a[2])) {
  6454. $average = ($a[2] =~ /average=day/i) ? "AVERAGE=DAY" : ($a[2] =~ /average/i) ? "AVERAGE=HOUR" : 0;
  6455. }
  6456. Log3 ($name, 3, "DbRep $name - reduce data older than: $ots, newer than: $nts");
  6457. Log3 ($name, 3, "DbRep $name - reduceLog requested with options: "
  6458. .(($average) ? "$average" : '')
  6459. .($filter ? ((($average && $filter) ? ", " : '').(uc((split('=',$a[-1]))[0]).'='.(split('=',$a[-1]))[1])) :
  6460. ((($idanz || $iranz) ? " INCLUDE -> " : '').($idanz ? "Devs: ".$idevs : '').($iranz ? " Readings: ".$ireading : '').(($edanz || $eranz) ? " EXCLUDE -> " : '').($edanz ? "Devs: ".$edevs : '').($eranz ? " Readings: ".$ereading : '')) ));
  6461. if ($ots) {
  6462. my ($sth_del, $sth_upd, $sth_delD, $sth_updD, $sth_get);
  6463. eval { $sth_del = $dbh->prepare_cached("DELETE FROM history WHERE (DEVICE=?) AND (READING=?) AND (TIMESTAMP=?) AND (VALUE=?)");
  6464. $sth_upd = $dbh->prepare_cached("UPDATE history SET TIMESTAMP=?, EVENT=?, VALUE=? WHERE (DEVICE=?) AND (READING=?) AND (TIMESTAMP=?) AND (VALUE=?)");
  6465. $sth_delD = $dbh->prepare_cached("DELETE FROM history WHERE (DEVICE=?) AND (READING=?) AND (TIMESTAMP=?)");
  6466. $sth_updD = $dbh->prepare_cached("UPDATE history SET TIMESTAMP=?, EVENT=?, VALUE=? WHERE (DEVICE=?) AND (READING=?) AND (TIMESTAMP=?)");
  6467. $sth_get = $dbh->prepare($sql);
  6468. };
  6469. if ($@) {
  6470. $err = encode_base64($@,"");
  6471. Log3 ($name, 2, "DbRep $name - DbRep_reduceLog - $@");
  6472. return "$name|''|$err|''";
  6473. }
  6474. eval { $sth_get->execute(); };
  6475. if ($@) {
  6476. $err = encode_base64($@,"");
  6477. Log3 ($name, 2, "DbRep $name - DbRep_reduceLog - $@");
  6478. return "$name|''|$err|''";
  6479. }
  6480. do {
  6481. $row = $sth_get->fetchrow_arrayref || ['0000-00-00 00:00:00','D','','R','V']; # || execute last-day dummy
  6482. $ret = 1;
  6483. ($day,$hour) = $row->[0] =~ /-(\d{2})\s(\d{2}):/;
  6484. $rowCount++ if($day != 00);
  6485. if ($day != $currentDay) {
  6486. if ($currentDay) { # false on first executed day
  6487. if (scalar @dayRows) {
  6488. ($lastHour) = $dayRows[-1]->[0] =~ /(.*\d+\s\d{2}):/;
  6489. $c = 0;
  6490. for my $delRow (@dayRows) {
  6491. $c++ if($day != 00 || $delRow->[0] !~ /$lastHour/);
  6492. }
  6493. if($c) {
  6494. $deletedCount += $c;
  6495. Log3 ($name, 3, "DbRep $name - reduceLog deleting $c records of day: $processingDay");
  6496. $dbh->{RaiseError} = 1;
  6497. $dbh->{PrintError} = 0;
  6498. eval {$dbh->begin_work() if($dbh->{AutoCommit});};
  6499. if ($@) {
  6500. Log3 ($name, 2, "DbRep $name - DbRep_reduceLog - $@");
  6501. }
  6502. eval {
  6503. my $i = 0;
  6504. my $k = 1;
  6505. my $th = ($#dayRows <= 2000)?100:($#dayRows <= 30000)?1000:10000;
  6506. for my $delRow (@dayRows) {
  6507. if($day != 00 || $delRow->[0] !~ /$lastHour/) {
  6508. Log3 ($name, 4, "DbRep $name - DELETE FROM history WHERE (DEVICE=$delRow->[1]) AND (READING=$delRow->[3]) AND (TIMESTAMP=$delRow->[0]) AND (VALUE=$delRow->[4])");
  6509. $sth_del->execute(($delRow->[1], $delRow->[3], $delRow->[0], $delRow->[4]));
  6510. $i++;
  6511. if($i == $th) {
  6512. my $prog = $k * $i;
  6513. Log3 ($name, 3, "DbRep $name - reduceLog deletion progress of day: $processingDay is: $prog");
  6514. $i = 0;
  6515. $k++;
  6516. }
  6517. }
  6518. }
  6519. };
  6520. if ($@) {
  6521. $err = $@;
  6522. Log3 ($name, 2, "DbRep $name - reduceLog ! FAILED ! for day $processingDay: $err");
  6523. eval {$dbh->rollback() if(!$dbh->{AutoCommit});};
  6524. if ($@) {
  6525. Log3 ($name, 2, "DbRep $name - DbRep_reduceLog - $@");
  6526. }
  6527. $ret = 0;
  6528. } else {
  6529. eval {$dbh->commit() if(!$dbh->{AutoCommit});};
  6530. if ($@) {
  6531. Log3 ($name, 2, "DbRep $name - DbRep_reduceLog - $@");
  6532. }
  6533. }
  6534. $dbh->{RaiseError} = 0;
  6535. $dbh->{PrintError} = 1;
  6536. }
  6537. @dayRows = ();
  6538. }
  6539. if ($ret && defined($a[2]) && $a[2] =~ /average/i) {
  6540. $dbh->{RaiseError} = 1;
  6541. $dbh->{PrintError} = 0;
  6542. eval {$dbh->begin_work() if($dbh->{AutoCommit});};
  6543. if ($@) {
  6544. Log3 ($name, 2, "DbRep $name - DbRep_reduceLog - $@");
  6545. }
  6546. eval {
  6547. push(@averageUpd, {%hourlyKnown}) if($day != 00);
  6548. $c = 0;
  6549. for my $hourHash (@averageUpd) { # Only count for logging...
  6550. for my $hourKey (keys %$hourHash) {
  6551. $c++ if ($hourHash->{$hourKey}->[0] && scalar(@{$hourHash->{$hourKey}->[4]}) > 1);
  6552. }
  6553. }
  6554. $updateCount += $c;
  6555. Log3 ($name, 3, "DbRep $name - reduceLog (hourly-average) updating $c records of day: $processingDay") if($c); # else only push to @averageUpdD
  6556. my $i = 0;
  6557. my $k = 1;
  6558. my $th = ($c <= 2000)?100:($c <= 30000)?1000:10000;
  6559. for my $hourHash (@averageUpd) {
  6560. for my $hourKey (keys %$hourHash) {
  6561. if ($hourHash->{$hourKey}->[0]) { # true if reading is a number
  6562. ($updDate,$updHour) = $hourHash->{$hourKey}->[0] =~ /(.*\d+)\s(\d{2}):/;
  6563. if (scalar(@{$hourHash->{$hourKey}->[4]}) > 1) { # true if reading has multiple records this hour
  6564. for (@{$hourHash->{$hourKey}->[4]}) { $sum += $_; }
  6565. $average = sprintf('%.3f', $sum/scalar(@{$hourHash->{$hourKey}->[4]}) );
  6566. $sum = 0;
  6567. Log3 ($name, 4, "DbRep $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]");
  6568. $sth_upd->execute(("$updDate $updHour:30:00", 'rl_av_h', $average, $hourHash->{$hourKey}->[1], $hourHash->{$hourKey}->[3], $hourHash->{$hourKey}->[0], $hourHash->{$hourKey}->[4]->[0]));
  6569. $i++;
  6570. if($i == $th) {
  6571. my $prog = $k * $i;
  6572. Log3 ($name, 3, "DbRep $name - reduceLog (hourly-average) updating progress of day: $processingDay is: $prog");
  6573. $i = 0;
  6574. $k++;
  6575. }
  6576. 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);
  6577. } else {
  6578. 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);
  6579. }
  6580. }
  6581. }
  6582. }
  6583. };
  6584. if ($@) {
  6585. $err = $@;
  6586. Log3 ($name, 2, "DbRep $name - reduceLog average=hour ! FAILED ! for day $processingDay: $err");
  6587. eval {$dbh->rollback() if(!$dbh->{AutoCommit});};
  6588. if ($@) {
  6589. Log3 ($name, 2, "DbRep $name - DbRep_reduceLog - $@");
  6590. }
  6591. @averageUpdD = ();
  6592. } else {
  6593. eval {$dbh->commit() if(!$dbh->{AutoCommit});};
  6594. if ($@) {
  6595. Log3 ($name, 2, "DbRep $name - DbRep_reduceLog - $@");
  6596. }
  6597. }
  6598. $dbh->{RaiseError} = 0;
  6599. $dbh->{PrintError} = 1;
  6600. @averageUpd = ();
  6601. }
  6602. if (defined($a[2]) && $a[2] =~ /average=day/i && scalar(@averageUpdD) && $day != 00) {
  6603. $dbh->{RaiseError} = 1;
  6604. $dbh->{PrintError} = 0;
  6605. eval {$dbh->begin_work() if($dbh->{AutoCommit});};
  6606. if ($@) {
  6607. Log3 ($name, 2, "DbRep $name - DbRep_reduceLog - $@");
  6608. }
  6609. eval {
  6610. for (@averageUpdD) {
  6611. push(@{$averageHash{$_->[3].$_->[4]}->{tedr}}, [$_->[0], $_->[1], $_->[3], $_->[4]]);
  6612. $averageHash{$_->[3].$_->[4]}->{sum} += $_->[2];
  6613. $averageHash{$_->[3].$_->[4]}->{date} = $_->[5];
  6614. }
  6615. $c = 0;
  6616. for (keys %averageHash) {
  6617. if(scalar @{$averageHash{$_}->{tedr}} == 1) {
  6618. delete $averageHash{$_};
  6619. } else {
  6620. $c += (scalar(@{$averageHash{$_}->{tedr}}) - 1);
  6621. }
  6622. }
  6623. $deletedCount += $c;
  6624. $updateCount += keys(%averageHash);
  6625. my ($id,$iu) = 0;
  6626. my ($kd,$ku) = 1;
  6627. my $thd = ($c <= 2000)?100:($c <= 30000)?1000:10000;
  6628. my $thu = ((keys %averageHash) <= 2000)?100:((keys %averageHash) <= 30000)?1000:10000;
  6629. Log3 ($name, 3, "DbRep $name - reduceLog (daily-average) updating ".(keys %averageHash).", deleting $c records of day: $processingDay") if(keys %averageHash);
  6630. for my $reading (keys %averageHash) {
  6631. $average = sprintf('%.3f', $averageHash{$reading}->{sum}/scalar(@{$averageHash{$reading}->{tedr}}));
  6632. $lastUpdH = pop @{$averageHash{$reading}->{tedr}};
  6633. for (@{$averageHash{$reading}->{tedr}}) {
  6634. Log3 ($name, 5, "DbRep $name - DELETE FROM history WHERE DEVICE='$_->[2]' AND READING='$_->[3]' AND TIMESTAMP='$_->[0]'");
  6635. $sth_delD->execute(($_->[2], $_->[3], $_->[0]));
  6636. $id++;
  6637. if($id == $thd) {
  6638. my $prog = $kd * $id;
  6639. Log3 ($name, 3, "DbRep $name - reduceLog (daily-average) deleting progress of day: $processingDay is: $prog");
  6640. $id = 0;
  6641. $kd++;
  6642. }
  6643. }
  6644. Log3 ($name, 4, "DbRep $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])");
  6645. $sth_updD->execute(($averageHash{$reading}->{date}." 12:00:00", 'rl_av_d', $average, $lastUpdH->[2], $lastUpdH->[3], $lastUpdH->[0]));
  6646. $iu++;
  6647. if($iu == $thu) {
  6648. my $prog = $ku * $id;
  6649. Log3 ($name, 3, "DbRep $name - reduceLog (daily-average) updating progress of day: $processingDay is: $prog");
  6650. $iu = 0;
  6651. $ku++;
  6652. }
  6653. }
  6654. };
  6655. if ($@) {
  6656. Log3 ($name, 3, "DbRep $name - reduceLog average=day ! FAILED ! for day $processingDay");
  6657. eval {$dbh->rollback() if(!$dbh->{AutoCommit});};
  6658. if ($@) {
  6659. Log3 ($name, 2, "DbRep $name - DbRep_reduceLog - $@");
  6660. }
  6661. } else {
  6662. eval {$dbh->commit() if(!$dbh->{AutoCommit});};
  6663. if ($@) {
  6664. Log3 ($name, 2, "DbRep $name - DbRep_reduceLog - $@");
  6665. }
  6666. }
  6667. $dbh->{RaiseError} = 0;
  6668. $dbh->{PrintError} = 1;
  6669. }
  6670. %averageHash = ();
  6671. %hourlyKnown = ();
  6672. @averageUpd = ();
  6673. @averageUpdD = ();
  6674. $currentHour = 99;
  6675. }
  6676. $currentDay = $day;
  6677. }
  6678. if ($hour != $currentHour) { # forget records from last hour, but remember these for average
  6679. if (defined($a[2]) && $a[2] =~ /average/i && keys(%hourlyKnown)) {
  6680. push(@averageUpd, {%hourlyKnown});
  6681. }
  6682. %hourlyKnown = ();
  6683. $currentHour = $hour;
  6684. }
  6685. if (defined $hourlyKnown{$row->[1].$row->[3]}) { # remember first readings for device per h, other can be deleted
  6686. push(@dayRows, [@$row]);
  6687. if (defined($a[2]) && $a[2] =~ /average/i && defined($row->[4]) && $row->[4] =~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ && $hourlyKnown{$row->[1].$row->[3]}->[0]) {
  6688. if ($hourlyKnown{$row->[1].$row->[3]}->[0]) {
  6689. push(@{$hourlyKnown{$row->[1].$row->[3]}->[4]}, $row->[4]);
  6690. }
  6691. }
  6692. } else {
  6693. $exclude = 0;
  6694. for (@excludeRegex) {
  6695. $exclude = 1 if("$row->[1]:$row->[3]" =~ /^$_$/);
  6696. }
  6697. if ($exclude) {
  6698. $excludeCount++ if($day != 00);
  6699. } else {
  6700. $hourlyKnown{$row->[1].$row->[3]} = (defined($row->[4]) && $row->[4] =~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/) ? [$row->[0],$row->[1],$row->[2],$row->[3],[$row->[4]]] : [0];
  6701. }
  6702. }
  6703. $processingDay = (split(' ',$row->[0]))[0];
  6704. } while( $day != 00 );
  6705. $brt = time() - $startTime;
  6706. my $result = "Rows processed: $rowCount, deleted: $deletedCount"
  6707. .((defined($a[2]) && $a[2] =~ /average/i)? ", updated: $updateCount" : '')
  6708. .(($excludeCount)? ", excluded: $excludeCount" : '');
  6709. Log3 ($name, 3, "DbRep $name - reduceLog finished. $result");
  6710. $ret = $result;
  6711. $ret = "reduceLog finished. $result";
  6712. } else {
  6713. $err = "reduceLog needs at least one of attributes \"timeOlderThan\", \"timeDiffToNow\", \"timestamp_begin\" or \"timestamp_end\" to be set";
  6714. Log3 ($name, 2, "DbRep $name - ERROR - $err");
  6715. $err = encode_base64($err,"");
  6716. return "$name|''|$err|''";
  6717. }
  6718. $dbh->disconnect();
  6719. $ret = encode_base64($ret,"");
  6720. Log3 ($name, 5, "DbRep $name -> DbRep_reduceLogNbl finished");
  6721. return "$name|$ret|0|$brt";
  6722. }
  6723. ####################################################################################################
  6724. # reduceLog non-blocking Rückkehrfunktion
  6725. ####################################################################################################
  6726. sub DbRep_reduceLogDone($) {
  6727. my ($string) = @_;
  6728. my @a = split("\\|",$string);
  6729. my $name = $a[0];
  6730. my $hash = $defs{$name};
  6731. my $ret = decode_base64($a[1]);
  6732. my $err = decode_base64($a[2]) if ($a[2]);
  6733. my $brt = $a[3];
  6734. my $dbloghash = $hash->{dbloghash};
  6735. my $erread;
  6736. delete $hash->{HELPER}{RUNNING_REDUCELOG};
  6737. if ($err) {
  6738. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  6739. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  6740. return;
  6741. }
  6742. # only for this block because of warnings if details of readings are not set
  6743. no warnings 'uninitialized';
  6744. readingsBeginUpdate($hash);
  6745. ReadingsBulkUpdateValue($hash, "background_processing_time", sprintf("%.2f",$brt));
  6746. ReadingsBulkUpdateValue($hash, "reduceLogState", $ret);
  6747. readingsEndUpdate($hash, 1);
  6748. # Befehl nach Procedure ausführen
  6749. $erread = DbRep_afterproc($hash, "reduceLog");
  6750. my $state = $erread?$erread:"reduceLog of $hash->{DATABASE} finished";
  6751. readingsBeginUpdate($hash);
  6752. ReadingsBulkUpdateTimeState($hash,undef,undef,$state);
  6753. readingsEndUpdate($hash, 1);
  6754. use warnings;
  6755. return;
  6756. }
  6757. ####################################################################################################
  6758. # Abbruchroutine Timeout reduceLog
  6759. ####################################################################################################
  6760. sub DbRep_reduceLogAborted(@) {
  6761. my ($hash,$cause) = @_;
  6762. my $name = $hash->{NAME};
  6763. my $dbh = $hash->{DBH};
  6764. my $erread;
  6765. $cause = $cause?$cause:"Timeout: process terminated";
  6766. Log3 ($name, 1, "DbRep $name - BlockingCall $hash->{HELPER}{RUNNING_REDUCELOG}{fn} pid:$hash->{HELPER}{RUNNING_REDUCELOG}{pid} $cause") if($hash->{HELPER}{RUNNING_REDUCELOG});
  6767. # Befehl nach Procedure ausführen
  6768. no warnings 'uninitialized';
  6769. $erread = DbRep_afterproc($hash, "reduceLog");
  6770. $erread = ", ".(split("but", $erread))[1] if($erread);
  6771. my $state = $cause.$erread;
  6772. $dbh->disconnect() if(defined($dbh));
  6773. ReadingsSingleUpdateValue ($hash, "state", $state, 1);
  6774. Log3 ($name, 2, "DbRep $name - Database reduceLog aborted due to \"$cause\" ");
  6775. delete($hash->{HELPER}{RUNNING_REDUCELOG});
  6776. return;
  6777. }
  6778. ####################################################################################################
  6779. # Abbruchroutine Timeout Restore
  6780. ####################################################################################################
  6781. sub DbRep_restoreAborted(@) {
  6782. my ($hash,$cause) = @_;
  6783. my $name = $hash->{NAME};
  6784. my $dbh = $hash->{DBH};
  6785. my $erread;
  6786. $cause = $cause?$cause:"Timeout: process terminated";
  6787. Log3 ($name, 1, "DbRep $name - BlockingCall $hash->{HELPER}{RUNNING_RESTORE}{fn} pid:$hash->{HELPER}{RUNNING_RESTORE}{pid} $cause") if($hash->{HELPER}{RUNNING_RESTORE});
  6788. # Befehl nach Procedure ausführen
  6789. no warnings 'uninitialized';
  6790. $erread = DbRep_afterproc($hash, "restore");
  6791. $erread = ", ".(split("but", $erread))[1] if($erread);
  6792. my $state = $cause.$erread;
  6793. $dbh->disconnect() if(defined($dbh));
  6794. ReadingsSingleUpdateValue ($hash, "state", $state, 1);
  6795. Log3 ($name, 2, "DbRep $name - Database restore aborted due to \"$cause\" ");
  6796. delete($hash->{HELPER}{RUNNING_RESTORE});
  6797. return;
  6798. }
  6799. ####################################################################################################
  6800. # Abbruchroutine Timeout DB-Abfrage
  6801. ####################################################################################################
  6802. sub DbRep_ParseAborted(@) {
  6803. my ($hash,$cause) = @_;
  6804. my $name = $hash->{NAME};
  6805. my $dbh = $hash->{DBH};
  6806. my $erread;
  6807. $cause = $cause?$cause:"Timeout: process terminated";
  6808. Log3 ($name, 1, "DbRep $name -> BlockingCall $hash->{HELPER}{RUNNING_PID}{fn} pid:$hash->{HELPER}{RUNNING_PID}{pid} $cause");
  6809. # Befehl nach Procedure ausführen
  6810. no warnings 'uninitialized';
  6811. $erread = DbRep_afterproc($hash, "command");
  6812. $erread = ", ".(split("but", $erread))[1] if($erread);
  6813. $dbh->disconnect() if(defined($dbh));
  6814. ReadingsSingleUpdateValue ($hash,"state",$cause, 1);
  6815. delete($hash->{HELPER}{RUNNING_PID});
  6816. return;
  6817. }
  6818. ####################################################################################################
  6819. # Abbruchroutine Timeout DB-Dump
  6820. ####################################################################################################
  6821. sub DbRep_DumpAborted(@) {
  6822. my ($hash,$cause) = @_;
  6823. my $name = $hash->{NAME};
  6824. my $dbh = $hash->{DBH};
  6825. my ($erread);
  6826. $cause = $cause?$cause:"Timeout: process terminated";
  6827. Log3 ($name, 1, "DbRep $name - BlockingCall $hash->{HELPER}{RUNNING_BACKUP_CLIENT}{fn} pid:$hash->{HELPER}{RUNNING_BACKUP_CLIENT}{pid} $cause") if($hash->{HELPER}{RUNNING_BACKUP_CLIENT});
  6828. Log3 ($name, 1, "DbRep $name - BlockingCall $hash->{HELPER}{RUNNING_BCKPREST_SERVER}{fn} pid:$hash->{HELPER}{RUNNING_BCKPREST_SERVER}{pid} $cause") if($hash->{HELPER}{RUNNING_BCKPREST_SERVER});
  6829. # Befehl nach Procedure ausführen
  6830. no warnings 'uninitialized';
  6831. $erread = DbRep_afterproc($hash, "dump");
  6832. $erread = ", ".(split("but", $erread))[1] if($erread);
  6833. my $state = $cause.$erread;
  6834. $dbh->disconnect() if(defined($dbh));
  6835. ReadingsSingleUpdateValue ($hash, "state", $state, 1);
  6836. Log3 ($name, 2, "DbRep $name - Database dump aborted due to \"$cause\" ");
  6837. delete($hash->{HELPER}{RUNNING_BACKUP_CLIENT});
  6838. delete($hash->{HELPER}{RUNNING_BCKPREST_SERVER});
  6839. return;
  6840. }
  6841. ####################################################################################################
  6842. # Abbruchroutine Timeout DB-Abfrage
  6843. ####################################################################################################
  6844. sub DbRep_OptimizeAborted(@) {
  6845. my ($hash,$cause) = @_;
  6846. my $name = $hash->{NAME};
  6847. my $dbh = $hash->{DBH};
  6848. my ($erread);
  6849. $cause = $cause?$cause:"Timeout: process terminated";
  6850. Log3 ($name, 1, "DbRep $name -> BlockingCall $hash->{HELPER}{RUNNING_OPTIMIZE}}{fn} pid:$hash->{HELPER}{RUNNING_OPTIMIZE}{pid} $cause");
  6851. # Befehl nach Procedure ausführen
  6852. no warnings 'uninitialized';
  6853. $erread = DbRep_afterproc($hash, "optimize");
  6854. $erread = ", ".(split("but", $erread))[1] if($erread);
  6855. my $state = $cause.$erread;
  6856. $dbh->disconnect() if(defined($dbh));
  6857. ReadingsSingleUpdateValue ($hash, "state", $state, 1);
  6858. Log3 ($name, 2, "DbRep $name - Database optimize aborted due to \"$cause\" ");
  6859. delete($hash->{HELPER}{RUNNING_OPTIMIZE});
  6860. return;
  6861. }
  6862. ####################################################################################################
  6863. # Abbruchroutine Repair SQlite
  6864. ####################################################################################################
  6865. sub DbRep_RepairAborted(@) {
  6866. my ($hash,$cause) = @_;
  6867. my $name = $hash->{NAME};
  6868. my $dbh = $hash->{DBH};
  6869. my $dbloghash = $hash->{dbloghash};
  6870. my $erread;
  6871. $cause = $cause?$cause:"Timeout: process terminated";
  6872. Log3 ($name, 1, "DbRep $name -> BlockingCall $hash->{HELPER}{RUNNING_REPAIR}{fn} pid:$hash->{HELPER}{RUNNING_REPAIR}{pid} $cause");
  6873. # Datenbankverbindung in DbLog wieder öffenen
  6874. my $dbl = $dbloghash->{NAME};
  6875. CommandSet(undef,"$dbl reopen");
  6876. # Befehl nach Procedure ausführen
  6877. no warnings 'uninitialized';
  6878. $erread = DbRep_afterproc($hash, "repair");
  6879. $erread = ", ".(split("but", $erread))[1] if($erread);
  6880. $dbh->disconnect() if(defined($dbh));
  6881. ReadingsSingleUpdateValue ($hash,"state",$cause, 1);
  6882. delete($hash->{HELPER}{RUNNING_REPAIR});
  6883. return;
  6884. }
  6885. ####################################################################################################
  6886. # SQL-Statement zusammenstellen für DB-Abfrage
  6887. ####################################################################################################
  6888. sub DbRep_createSelectSql($$$$$$$$) {
  6889. my ($hash,$table,$selspec,$device,$reading,$tf,$tn,$addon) = @_;
  6890. my $name = $hash->{NAME};
  6891. my $dbmodel = $hash->{dbloghash}{MODEL};
  6892. my $valfilter = AttrVal($name, "valueFilter", undef); # Wertefilter
  6893. my ($sql,$vf);
  6894. my $tnfull = 0;
  6895. my ($idevs,$idanz,$ireading,$iranz,$edevs,$edanz,$ereading,$eranz) = DbRep_specsForSql($hash,$device,$reading);
  6896. if($tn && $tn =~ /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/) {
  6897. $tnfull = 1;
  6898. }
  6899. if(defined $valfilter) {
  6900. if ($dbmodel eq "POSTGRESQL") {
  6901. $vf = "VALUE ~ '$valfilter' AND ";
  6902. } else {
  6903. $vf = "VALUE REGEXP '$valfilter' AND ";
  6904. }
  6905. }
  6906. $sql = "SELECT $selspec FROM $table where ";
  6907. # included devices
  6908. $sql .= "DEVICE LIKE '$idevs' AND " if($idanz <= 1 && $idevs !~ m(^%$) && $idevs =~ m(\%));
  6909. $sql .= "DEVICE = '$idevs' AND " if($idanz <= 1 && $idevs !~ m(\%));
  6910. $sql .= "DEVICE IN ($idevs) AND " if($idanz > 1);
  6911. # excluded devices
  6912. $sql .= "DEVICE NOT LIKE '$edevs' AND " if($edanz && $edanz == 1 && $edevs =~ m(\%));
  6913. $sql .= "DEVICE != '$edevs' AND " if($edanz && $edanz == 1 && $edevs !~ m(\%));
  6914. $sql .= "DEVICE NOT IN ($edevs) AND " if($edanz > 1);
  6915. # included readings
  6916. $sql .= "READING LIKE '$ireading' AND " if($iranz <= 1 && $ireading !~ m(^%$) && $ireading =~ m(\%));
  6917. $sql .= "READING = '$ireading' AND " if($iranz <= 1 && $ireading !~ m(\%));
  6918. $sql .= "READING IN ($ireading) AND " if($iranz > 1);
  6919. # excluded readings
  6920. $sql .= "READING NOT LIKE '$ereading' AND " if($eranz && $eranz == 1 && $ereading =~ m(\%));
  6921. $sql .= "READING != '$ereading' AND " if($eranz && $eranz == 1 && $ereading !~ m(\%));
  6922. $sql .= "READING NOT IN ($ereading) AND " if($eranz > 1);
  6923. # add valueFilter
  6924. $sql .= $vf if(defined $vf);
  6925. # Timestamp Filter
  6926. if (($tf && $tn)) {
  6927. $sql .= "TIMESTAMP >= $tf AND TIMESTAMP ".($tnfull?"<=":"<")." $tn ";
  6928. } else {
  6929. if ($dbmodel eq "POSTGRESQL") {
  6930. $sql .= "true ";
  6931. } else {
  6932. $sql .= "1 ";
  6933. }
  6934. }
  6935. $sql .= "$addon;";
  6936. return $sql;
  6937. }
  6938. ####################################################################################################
  6939. # SQL-Statement zusammenstellen für DB-Updates
  6940. ####################################################################################################
  6941. sub DbRep_createUpdateSql($$$$$$$$) {
  6942. my ($hash,$table,$selspec,$device,$reading,$tf,$tn,$addon) = @_;
  6943. my $name = $hash->{NAME};
  6944. my $dbmodel = $hash->{dbloghash}{MODEL};
  6945. my $valfilter = AttrVal($name, "valueFilter", undef); # Wertefilter
  6946. my ($sql,$vf);
  6947. my $tnfull = 0;
  6948. my ($idevs,$idanz,$ireading,$iranz,$edevs,$edanz,$ereading,$eranz) = DbRep_specsForSql($hash,$device,$reading);
  6949. if($tn =~ /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/) {
  6950. $tnfull = 1;
  6951. }
  6952. if(defined $valfilter) {
  6953. if ($dbmodel eq "POSTGRESQL") {
  6954. $vf = "VALUE ~ '$valfilter' AND ";
  6955. } else {
  6956. $vf = "VALUE REGEXP '$valfilter' AND ";
  6957. }
  6958. }
  6959. $sql = "UPDATE $table SET $selspec AND ";
  6960. # included devices
  6961. $sql .= "DEVICE LIKE '$idevs' AND " if($idanz <= 1 && $idevs !~ m(^%$) && $idevs =~ m(\%));
  6962. $sql .= "DEVICE = '$idevs' AND " if($idanz <= 1 && $idevs !~ m(\%));
  6963. $sql .= "DEVICE IN ($idevs) AND " if($idanz > 1);
  6964. # excluded devices
  6965. $sql .= "DEVICE NOT LIKE '$edevs' AND " if($edanz && $edanz == 1 && $edevs =~ m(\%));
  6966. $sql .= "DEVICE != '$edevs' AND " if($edanz && $edanz == 1 && $edevs !~ m(\%));
  6967. $sql .= "DEVICE NOT IN ($edevs) AND " if($edanz > 1);
  6968. # included readings
  6969. $sql .= "READING LIKE '$ireading' AND " if($iranz <= 1 && $ireading !~ m(^%$) && $ireading =~ m(\%));
  6970. $sql .= "READING = '$ireading' AND " if($iranz <= 1 && $ireading !~ m(\%));
  6971. $sql .= "READING IN ($ireading) AND " if($iranz > 1);
  6972. # excluded readings
  6973. $sql .= "READING NOT LIKE '$ereading' AND " if($eranz && $eranz == 1 && $ereading =~ m(\%));
  6974. $sql .= "READING != '$ereading' AND " if($eranz && $eranz == 1 && $ereading !~ m(\%));
  6975. $sql .= "READING NOT IN ($ereading) AND " if($eranz > 1);
  6976. # add valueFilter
  6977. $sql .= $vf if(defined $vf);
  6978. # Timestamp Filter
  6979. if (($tf && $tn)) {
  6980. $sql .= "TIMESTAMP >= $tf AND TIMESTAMP ".($tnfull?"<=":"<")." $tn ";
  6981. } else {
  6982. if ($dbmodel eq "POSTGRESQL") {
  6983. $sql .= "true ";
  6984. } else {
  6985. $sql .= "1 ";
  6986. }
  6987. }
  6988. $sql .= "$addon;";
  6989. return $sql;
  6990. }
  6991. ####################################################################################################
  6992. # SQL-Statement zusammenstellen für Löschvorgänge
  6993. ####################################################################################################
  6994. sub DbRep_createDeleteSql($$$$$$$) {
  6995. my ($hash,$table,$device,$reading,$tf,$tn,$addon) = @_;
  6996. my $name = $hash->{NAME};
  6997. my $dbmodel = $hash->{dbloghash}{MODEL};
  6998. my $valfilter = AttrVal($name, "valueFilter", undef); # Wertefilter
  6999. my ($sql,$vf);
  7000. my $tnfull = 0;
  7001. if($table eq "current") {
  7002. $sql = "delete FROM $table; ";
  7003. return $sql;
  7004. }
  7005. my ($idevs,$idanz,$ireading,$iranz,$edevs,$edanz,$ereading,$eranz) = DbRep_specsForSql($hash,$device,$reading);
  7006. if($tn =~ /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/) {
  7007. $tnfull = 1;
  7008. }
  7009. if(defined $valfilter) {
  7010. if ($dbmodel eq "POSTGRESQL") {
  7011. $vf = "VALUE ~ '$valfilter' AND ";
  7012. } else {
  7013. $vf = "VALUE REGEXP '$valfilter' AND ";
  7014. }
  7015. }
  7016. $sql = "delete FROM $table where ";
  7017. # included devices
  7018. $sql .= "DEVICE LIKE '$idevs' AND " if($idanz <= 1 && $idevs !~ m(^%$) && $idevs =~ m(\%));
  7019. $sql .= "DEVICE = '$idevs' AND " if($idanz <= 1 && $idevs !~ m(\%));
  7020. $sql .= "DEVICE IN ($idevs) AND " if($idanz > 1);
  7021. # excluded devices
  7022. $sql .= "DEVICE NOT LIKE '$edevs' AND " if($edanz && $edanz == 1 && $edevs =~ m(\%));
  7023. $sql .= "DEVICE != '$edevs' AND " if($edanz && $edanz == 1 && $edevs !~ m(\%));
  7024. $sql .= "DEVICE NOT IN ($edevs) AND " if($edanz > 1);
  7025. # included readings
  7026. $sql .= "READING LIKE '$ireading' AND " if($iranz <= 1 && $ireading !~ m(^%$) && $ireading =~ m(\%));
  7027. $sql .= "READING = '$ireading' AND " if($iranz <= 1 && $ireading !~ m(\%));
  7028. $sql .= "READING IN ($ireading) AND " if($iranz > 1);
  7029. # excluded readings
  7030. $sql .= "READING NOT LIKE '$ereading' AND " if($eranz && $eranz == 1 && $ereading =~ m(\%));
  7031. $sql .= "READING != '$ereading' AND " if($eranz && $eranz == 1 && $ereading !~ m(\%));
  7032. $sql .= "READING NOT IN ($ereading) AND " if($eranz > 1);
  7033. # add valueFilter
  7034. $sql .= $vf if(defined $vf);
  7035. # Timestamp Filter
  7036. if ($tf && $tn) {
  7037. $sql .= "TIMESTAMP >= '$tf' AND TIMESTAMP ".($tnfull?"<=":"<")." '$tn' $addon;";
  7038. } else {
  7039. if ($dbmodel eq "POSTGRESQL") {
  7040. $sql .= "true;";
  7041. } else {
  7042. $sql .= "1;";
  7043. }
  7044. }
  7045. return $sql;
  7046. }
  7047. ####################################################################################################
  7048. # Ableiten von Device, Reading-Spezifikationen
  7049. ####################################################################################################
  7050. sub DbRep_specsForSql($$$) {
  7051. my ($hash,$device,$reading) = @_;
  7052. my $name = $hash->{NAME};
  7053. ##### inkludierte / excludierte Devices und deren Anzahl ermitteln #####
  7054. my ($idevice,$edevice) = ('','');
  7055. my ($idevs,$edevs) = ('','');
  7056. my ($idanz,$edanz) = (0,0);
  7057. if($device =~ /EXCLUDE=/i) {
  7058. ($idevice,$edevice) = split(/EXCLUDE=/i,$device);
  7059. $idevice = $idevice?DbRep_trim($idevice):"%";
  7060. } else {
  7061. $idevice = $device;
  7062. }
  7063. # Devices inkludiert
  7064. my @idvspcs = devspec2array($idevice);
  7065. $idevs = join(",",@idvspcs);
  7066. $idevs =~ s/'/''/g; # escape ' with ''
  7067. $idanz = $#idvspcs+1;
  7068. if($idanz > 1) {
  7069. $idevs =~ s/,/','/g;
  7070. $idevs = "'".$idevs."'";
  7071. }
  7072. # Devices exkludiert
  7073. if($edevice) {
  7074. my @edvspcs = devspec2array($edevice);
  7075. $edevs = join(",",@edvspcs);
  7076. $edevs =~ s/'/''/g; # escape ' with ''
  7077. $edanz = $#edvspcs+1;
  7078. if($edanz > 1) {
  7079. $edevs =~ s/,/','/g;
  7080. $edevs = "'".$edevs."'";
  7081. }
  7082. }
  7083. Log3 $name, 5, "DbRep $name - Devices for operation - included: $idevs , excluded: $edevs";
  7084. ##### inkludierte / excludierte Readings und deren Anzahl ermitteln #####
  7085. my ($ireading,$ereading) = ('','');
  7086. my ($iranz,$eranz) = (0,0);
  7087. $reading =~ s/'/''/g; # escape ' with ''
  7088. if($reading =~ /EXCLUDE=/i) {
  7089. ($ireading,$ereading) = split(/EXCLUDE=/i,$reading);
  7090. $ireading = $ireading?DbRep_trim($ireading):"%";
  7091. } else {
  7092. $ireading = $reading;
  7093. }
  7094. # Readings inkludiert
  7095. my @ireads = split(",",$ireading);
  7096. $iranz = $#ireads+1;
  7097. if($iranz > 1) {
  7098. $ireading =~ s/,/','/g;
  7099. $ireading = "'".$ireading."'";
  7100. }
  7101. if($ereading) {
  7102. # Readings exkludiert
  7103. my @ereads = split(",",$ereading);
  7104. $eranz = $#ereads+1;
  7105. if($eranz > 1) {
  7106. $ereading =~ s/,/','/g;
  7107. $ereading = "'".$ereading."'";
  7108. }
  7109. }
  7110. Log3 $name, 5, "DbRep $name - Readings for operation - included: $ireading, excluded: $ereading ";
  7111. return ($idevs,$idanz,$ireading,$iranz,$edevs,$edanz,$ereading,$eranz);
  7112. }
  7113. ####################################################################################################
  7114. # Leerzeichen am Anfang / Ende eines strings entfernen
  7115. ####################################################################################################
  7116. sub DbRep_trim ($) {
  7117. my $str = shift;
  7118. $str =~ s/^\s+|\s+$//g;
  7119. return ($str);
  7120. }
  7121. ####################################################################################################
  7122. # Check ob Zeitgrenzen bzw. Aggregation gesetzt sind, evtl. übertseuern (je nach Funktion)
  7123. # Return "1" wenn Bedingung erfüllt, sonst "0"
  7124. ####################################################################################################
  7125. sub DbRep_checktimeaggr ($) {
  7126. my ($hash) = @_;
  7127. my $name = $hash->{NAME};
  7128. my $IsTimeSet = 0;
  7129. my $IsAggrSet = 0;
  7130. my $aggregation = AttrVal($name,"aggregation","no");
  7131. if ( AttrVal($name,"timestamp_begin",undef) || AttrVal($name,"timestamp_end",undef) ||
  7132. AttrVal($name,"timeDiffToNow",undef) || AttrVal($name,"timeOlderThan",undef) || AttrVal($name,"timeYearPeriod",undef) ) {
  7133. $IsTimeSet = 1;
  7134. }
  7135. if ($aggregation ne "no") {
  7136. $IsAggrSet = 1;
  7137. }
  7138. if($hash->{LASTCMD} =~ /delSeqDoublets/) {
  7139. $aggregation = ($aggregation eq "no")?"day":$aggregation; # wenn Aggregation "no", für delSeqDoublets immer "day" setzen
  7140. $IsAggrSet = 1;
  7141. }
  7142. if($hash->{LASTCMD} =~ /averageValue/ && AttrVal($name,"averageCalcForm","avgArithmeticMean") eq "avgDailyMeanGWS") {
  7143. $aggregation = "day"; # für Tagesmittelwertberechnung des deutschen Wetterdienstes immer "day"
  7144. $IsAggrSet = 1;
  7145. }
  7146. if($hash->{LASTCMD} =~ /delEntries|fetchrows|deviceRename|readingRename|tableCurrentFillup|reduceLog/) {
  7147. $IsAggrSet = 0;
  7148. $aggregation = "no";
  7149. }
  7150. if($hash->{LASTCMD} =~ /deviceRename|readingRename/) {
  7151. $IsTimeSet = 0;
  7152. }
  7153. if($hash->{LASTCMD} =~ /changeValue/) {
  7154. if($hash->{HELPER}{COMPLEX}) {
  7155. $IsAggrSet = 1;
  7156. $aggregation = "day";
  7157. } else {
  7158. $IsAggrSet = 0;
  7159. $aggregation = "no";
  7160. }
  7161. }
  7162. if($hash->{LASTCMD} =~ /syncStandby/ ) {
  7163. if($aggregation !~ /day|hour|week/) {
  7164. $aggregation = "day";
  7165. $IsAggrSet = 1;
  7166. }
  7167. }
  7168. return ($IsTimeSet,$IsAggrSet,$aggregation);
  7169. }
  7170. ####################################################################################################
  7171. # ReadingsSingleUpdate für Reading, Value, Event
  7172. ####################################################################################################
  7173. sub ReadingsSingleUpdateValue ($$$$) {
  7174. my ($hash,$reading,$val,$ev) = @_;
  7175. my $name = $hash->{NAME};
  7176. readingsSingleUpdate($hash, $reading, $val, $ev);
  7177. DbRep_userexit($name, $reading, $val);
  7178. return;
  7179. }
  7180. ####################################################################################################
  7181. # Readingsbulkupdate für Reading, Value
  7182. # readingsBeginUpdate und readingsEndUpdate muss vor/nach Funktionsaufruf gesetzt werden
  7183. ####################################################################################################
  7184. sub ReadingsBulkUpdateValue ($$$) {
  7185. my ($hash,$reading,$val) = @_;
  7186. my $name = $hash->{NAME};
  7187. readingsBulkUpdate($hash, $reading, $val);
  7188. DbRep_userexit($name, $reading, $val);
  7189. return;
  7190. }
  7191. ####################################################################################################
  7192. # Readingsbulkupdate für processing_time, state
  7193. # readingsBeginUpdate und readingsEndUpdate muss vor/nach Funktionsaufruf gesetzt werden
  7194. ####################################################################################################
  7195. sub ReadingsBulkUpdateTimeState ($$$$) {
  7196. my ($hash,$brt,$rt,$sval) = @_;
  7197. my $name = $hash->{NAME};
  7198. if(AttrVal($name, "showproctime", undef)) {
  7199. readingsBulkUpdate($hash, "background_processing_time", sprintf("%.4f",$brt)) if(defined($brt));
  7200. DbRep_userexit($name, "background_processing_time", sprintf("%.4f",$brt)) if(defined($brt));
  7201. readingsBulkUpdate($hash, "sql_processing_time", sprintf("%.4f",$rt)) if(defined($rt));
  7202. DbRep_userexit($name, "sql_processing_time", sprintf("%.4f",$rt)) if(defined($rt));
  7203. }
  7204. readingsBulkUpdate($hash, "state", $sval);
  7205. DbRep_userexit($name, "state", $sval);
  7206. return;
  7207. }
  7208. ####################################################################################################
  7209. # Anzeige von laufenden Blocking Prozessen
  7210. ####################################################################################################
  7211. sub DbRep_getblockinginfo($@) {
  7212. my ($hash) = @_;
  7213. my $name = $hash->{NAME};
  7214. my @rows;
  7215. our %BC_hash;
  7216. my $len = 99;
  7217. foreach my $h (values %BC_hash) {
  7218. next if($h->{terminated} || !$h->{pid});
  7219. my @allk = keys%{$h};
  7220. foreach my $k (@allk) {
  7221. Log3 ($name, 5, "DbRep $name -> $k : ".$h->{$k});
  7222. }
  7223. my $fn = (ref($h->{fn}) ? ref($h->{fn}) : $h->{fn});
  7224. my $arg = (ref($h->{arg}) ? ref($h->{arg}) : $h->{arg});
  7225. my $arg1 = substr($arg,0,$len);
  7226. $arg1 = $arg1."..." if(length($arg) > $len+1);
  7227. my $to = ($h->{timeout} ? $h->{timeout} : "N/A");
  7228. my $conn = ($h->{telnet} ? $h->{telnet} : "N/A");
  7229. push @rows, "$h->{pid}|ESCAPED|$fn|ESCAPED|$arg1|ESCAPED|$to|ESCAPED|$conn";
  7230. }
  7231. # Readingaufbereitung
  7232. readingsBeginUpdate($hash);
  7233. if(!@rows) {
  7234. ReadingsBulkUpdateTimeState($hash,undef,undef,"done - No BlockingCall processes running");
  7235. readingsEndUpdate($hash, 1);
  7236. return;
  7237. }
  7238. my $res = "<html><table border=2 bordercolor='darkgreen' cellspacing=0>";
  7239. $res .= "<tr><td style='padding-right:5px;padding-left:5px;font-weight:bold'>PID</td>";
  7240. $res .= "<td style='padding-right:5px;padding-left:5px;font-weight:bold'>FUNCTION</td>";
  7241. $res .= "<td style='padding-right:5px;padding-left:5px;font-weight:bold'>ARGUMENTS</td>";
  7242. $res .= "<td style='padding-right:5px;padding-left:5px;font-weight:bold'>TIMEOUT</td>";
  7243. $res .= "<td style='padding-right:5px;padding-left:5px;font-weight:bold'>CONNECTEDVIA</td></tr>";
  7244. foreach my $row (@rows) {
  7245. $row =~ s/\|ESCAPED\|/<\/td><td style='padding-right:5px;padding-left:5px'>/g;
  7246. $res .= "<tr><td style='padding-right:5px;padding-left:5px'>".$row."</td></tr>";
  7247. }
  7248. my $tab = $res."</table></html>";
  7249. ReadingsBulkUpdateValue ($hash,"BlockingInfo",$tab);
  7250. ReadingsBulkUpdateValue ($hash,"Blocking_Count",$#rows+1);
  7251. ReadingsBulkUpdateTimeState($hash,undef,undef,"done");
  7252. readingsEndUpdate($hash, 1);
  7253. return;
  7254. }
  7255. ####################################################################################################
  7256. # relative Zeitangaben als Sekunden normieren
  7257. #
  7258. # liefert die Attribute timeOlderThan, timeDiffToNow als Sekunden normiert zurück
  7259. ####################################################################################################
  7260. sub DbRep_normRelTime($) {
  7261. my ($hash) = @_;
  7262. my $name = $hash->{NAME};
  7263. my $tdtn = AttrVal($name, "timeDiffToNow", undef);
  7264. my $toth = AttrVal($name, "timeOlderThan", undef);
  7265. if($tdtn && $tdtn =~ /^\s*[ydhms]:(([\d]+.[\d]+)|[\d]+)\s*/) {
  7266. my ($y,$d,$h,$m,$s);
  7267. if($tdtn =~ /.*y:(([\d]+.[\d]+)|[\d]+).*/) {
  7268. $y = $tdtn;
  7269. $y =~ s/.*y:(([\d]+.[\d]+)|[\d]+).*/$1/e;
  7270. }
  7271. if($tdtn =~ /.*d:(([\d]+.[\d]+)|[\d]+).*/) {
  7272. $d = $tdtn;
  7273. $d =~ s/.*d:(([\d]+.[\d]+)|[\d]+).*/$1/e;
  7274. }
  7275. if($tdtn =~ /.*h:(([\d]+.[\d]+)|[\d]+).*/) {
  7276. $h = $tdtn;
  7277. $h =~ s/.*h:(([\d]+.[\d]+)|[\d]+).*/$1/e;
  7278. }
  7279. if($tdtn =~ /.*m:(([\d]+.[\d]+)|[\d]+).*/) {
  7280. $m = $tdtn;
  7281. $m =~ s/.*m:(([\d]+.[\d]+)|[\d]+).*/$1/e;
  7282. }
  7283. if($tdtn =~ /.*s:(([\d]+.[\d]+)|[\d]+).*/) {
  7284. $s = $tdtn;
  7285. $s =~ s/.*s:(([\d]+.[\d]+)|[\d]+).*/$1/e ;
  7286. }
  7287. no warnings 'uninitialized';
  7288. Log3($name, 4, "DbRep $name - timeDiffToNow - year: $y, day: $d, hour: $h, min: $m, sec: $s ");
  7289. use warnings;
  7290. $y = $y?($y*365*86400):0;
  7291. $d = $d?($d*86400):0;
  7292. $h = $h?($h*3600):0;
  7293. $m = $m?($m*60):0;
  7294. $s = $s?$s:0;
  7295. $tdtn = $y + $d + $h + $m + $s + 1; # one security second for correct create TimeArray
  7296. $tdtn = DbRep_corrRelTime($name,$tdtn,1);
  7297. }
  7298. if($toth && $toth =~ /^\s*[ydhms]:(([\d]+.[\d]+)|[\d]+)\s*/) {
  7299. my ($y,$d,$h,$m,$s);
  7300. if($toth =~ /.*y:(([\d]+.[\d]+)|[\d]+).*/) {
  7301. $y = $toth;
  7302. $y =~ s/.*y:(([\d]+.[\d]+)|[\d]+).*/$1/e;
  7303. }
  7304. if($toth =~ /.*d:(([\d]+.[\d]+)|[\d]+).*/) {
  7305. $d = $toth;
  7306. $d =~ s/.*d:(([\d]+.[\d]+)|[\d]+).*/$1/e;
  7307. }
  7308. if($toth =~ /.*h:(([\d]+.[\d]+)|[\d]+).*/) {
  7309. $h = $toth;
  7310. $h =~ s/.*h:(([\d]+.[\d]+)|[\d]+).*/$1/e;
  7311. }
  7312. if($toth =~ /.*m:(([\d]+.[\d]+)|[\d]+).*/) {
  7313. $m = $toth;
  7314. $m =~ s/.*m:(([\d]+.[\d]+)|[\d]+).*/$1/e;
  7315. }
  7316. if($toth =~ /.*s:(([\d]+.[\d]+)|[\d]+).*/) {
  7317. $s = $toth;
  7318. $s =~ s/.*s:(([\d]+.[\d]+)|[\d]+).*/$1/e ;
  7319. }
  7320. no warnings 'uninitialized';
  7321. Log3($name, 4, "DbRep $name - timeOlderThan - year: $y, day: $d, hour: $h, min: $m, sec: $s ");
  7322. use warnings;
  7323. $y = $y?($y*365*86400):0;
  7324. $d = $d?($d*86400):0;
  7325. $h = $h?($h*3600):0;
  7326. $m = $m?($m*60):0;
  7327. $s = $s?$s:0;
  7328. $toth = $y + $d + $h + $m + $s + 1; # one security second for correct create TimeArray
  7329. $toth = DbRep_corrRelTime($name,$toth,0);
  7330. }
  7331. return ($toth,$tdtn);
  7332. }
  7333. ####################################################################################################
  7334. # Korrektur Schaltjahr und Sommer/Winterzeit bei relativen Zeitangaben
  7335. ####################################################################################################
  7336. sub DbRep_corrRelTime($$$) {
  7337. my ($name,$tim,$tdtn) = @_;
  7338. my $hash = $defs{$name};
  7339. # year als Jahre seit 1900
  7340. # $mon als 0..11
  7341. my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);
  7342. my ($dsec,$dmin,$dhour,$dmday,$dmon,$dyear,$dwday,$dyday,$disdst);
  7343. if($tdtn) {
  7344. # timeDiffToNow
  7345. ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); # Startzeit Ableitung
  7346. ($dsec,$dmin,$dhour,$dmday,$dmon,$dyear,$dwday,$dyday,$disdst) = localtime(time-$tim); # Analyse Zieltimestamp timeDiffToNow
  7347. } else {
  7348. # timeOlderThan
  7349. ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time-$tim); # Startzeit Ableitung
  7350. my $mints = $hash->{HELPER}{MINTS}?$hash->{HELPER}{MINTS}:"1970-01-01 01:00:00"; # Timestamp des 1. Datensatzes verwenden falls ermittelt
  7351. my ($yyyy1, $mm1, $dd1, $hh1, $min1, $sec1) = ($mints =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/);
  7352. my $tsend = timelocal($sec1, $min1, $hh1, $dd1, $mm1-1, $yyyy1-1900);
  7353. ($dsec,$dmin,$dhour,$dmday,$dmon,$dyear,$dwday,$dyday,$disdst) = localtime($tsend); # Analyse Zieltimestamp timeOlderThan
  7354. }
  7355. $year += 1900;
  7356. $dyear += 1900;
  7357. my $k = $year - $dyear;
  7358. my $mg = ((int($mon)+1)+(($year-$dyear-1)*12)+(11-int($dmon)+1)); # Gesamtzahl der Monate des Bewertungszeitraumes
  7359. my $cly = 0; # Anzahl Schaltjahre innerhalb Beginn und Ende Auswertungszeitraum
  7360. my $fly = 0; # erstes Schaltjahr nach Start
  7361. my $lly = 0; # letzes Schaltjahr nach Start
  7362. while ($dyear+$k >= $dyear) {
  7363. my $ily = DbRep_IsLeapYear($name,$dyear+$k);
  7364. $cly++ if($ily);
  7365. $fly = $dyear+$k if($ily && !$fly);
  7366. $lly = $dyear+$k if($ily);
  7367. $k--;
  7368. }
  7369. # Log3($name, 4, "DbRep $name - countleapyear: $cly firstleapyear: $fly lastleapyear: $lly totalmonth: $mg isdaylight:$isdst destdaylight:$disdst");
  7370. if( ($fly <= $year && $mon > 1) && ($lly > $dyear || ($lly = $dyear && $dmon < 1)) ) {
  7371. $tim += $cly*86400;
  7372. # Log3($name, 4, "DbRep $name - leap year correction 1");
  7373. } else {
  7374. $tim += ($cly-1)*86400 if($cly);
  7375. # Log3($name, 4, "DbRep $name - leap year correction 2");
  7376. }
  7377. # Sommer/Winterzeitkorrektur
  7378. $tim += ($disdst-$isdst)*3600 if($disdst != $isdst);
  7379. return $tim;
  7380. }
  7381. ####################################################################################################
  7382. # liefert zurück ob übergebenes Jahr ein Schaltjahr ist ($ily = 1)
  7383. #
  7384. # Es gilt:
  7385. # - Wenn ein Jahr durch 4 teilbar ist, ist es ein Schaltjahr, aber
  7386. # - wenn es durch 100 teilbar ist, ist es kein schaltjahr, außer
  7387. # - es ist durch 400 teilbar, dann ist es ein schaltjahr
  7388. #
  7389. ####################################################################################################
  7390. sub DbRep_IsLeapYear($$) {
  7391. my ($name,$year) = @_;
  7392. my $ily = 0;
  7393. if ($year % 4 == 0 && $year % 100 != 0 || $year % 400 == 0) { # $year modulo 4 -> muß 0 sein
  7394. $ily = 1;
  7395. }
  7396. Log3($name, 4, "DbRep $name - Year $year is leap year") if($ily);
  7397. return $ily;
  7398. }
  7399. ###############################################################################
  7400. # Zeichencodierung für Fileexport filtern
  7401. ###############################################################################
  7402. sub DbRep_charfilter($) {
  7403. my ($txt) = @_;
  7404. # nur erwünschte Zeichen, Filtern von Steuerzeichen
  7405. $txt =~ tr/ A-Za-z0-9!"#$§%&'()*+,-.\/:;<=>?@[\\]^_`{|}~äöüÄÖÜ߀//cd;
  7406. return($txt);
  7407. }
  7408. ###################################################################################
  7409. # Befehl vor Procedure ausführen
  7410. ###################################################################################
  7411. sub DbRep_beforeproc ($$) {
  7412. my ($hash, $txt) = @_;
  7413. my $name = $hash->{NAME};
  7414. # Befehl vor Procedure ausführen
  7415. my $ebd = AttrVal($name, "executeBeforeProc", undef);
  7416. if($ebd) {
  7417. Log3 ($name, 3, "DbRep $name - execute command before $txt: '$ebd' ");
  7418. my $err = AnalyzeCommandChain(undef, $ebd);
  7419. if ($err) {
  7420. Log3 ($name, 2, "DbRep $name - command message before $txt: \"$err\" ");
  7421. my $erread = "Warning - message from command before $txt appeared";
  7422. ReadingsSingleUpdateValue ($hash, "before".$txt."_message", $err, 1);
  7423. ReadingsSingleUpdateValue ($hash, "state", $erread, 1);
  7424. }
  7425. }
  7426. return;
  7427. }
  7428. ###################################################################################
  7429. # Befehl nach Procedure ausführen
  7430. ###################################################################################
  7431. sub DbRep_afterproc ($$) {
  7432. my ($hash, $txt) = @_;
  7433. my $name = $hash->{NAME};
  7434. my $erread;
  7435. # Befehl nach Procedure ausführen
  7436. no warnings 'uninitialized';
  7437. my $ead = AttrVal($name, "executeAfterProc", undef);
  7438. if($ead) {
  7439. Log3 ($name, 4, "DbRep $name - execute command after $txt: '$ead' ");
  7440. my $err = AnalyzeCommandChain(undef, $ead);
  7441. if ($err) {
  7442. Log3 ($name, 2, "DbRep $name - command message after $txt: \"$err\" ");
  7443. ReadingsSingleUpdateValue ($hash, "after".$txt."_message", $err, 1);
  7444. $erread = "Warning - $txt finished, but command message after $txt appeared";
  7445. }
  7446. }
  7447. return $erread;
  7448. }
  7449. ##############################################################################################
  7450. # timestamp_begin, timestamp_end bei Einsatz datetime-Picker entsprechend
  7451. # den Anforderungen formatieren
  7452. ##############################################################################################
  7453. sub DbRep_formatpicker ($) {
  7454. my ($str) = @_;
  7455. if ($str =~ /^(\d{4})-(\d{2})-(\d{2})_(\d{2}):(\d{2})$/) {
  7456. # Anpassung für datetime-Picker Widget
  7457. $str =~ s/_/ /;
  7458. $str = $str.":00";
  7459. }
  7460. if ($str =~ /^(\d{4})-(\d{2})-(\d{2})_(\d{2}):(\d{2}):(\d{2})$/) {
  7461. # Anpassung für datetime-Picker Widget
  7462. $str =~ s/_/ /;
  7463. }
  7464. return $str;
  7465. }
  7466. ####################################################################################################
  7467. # userexit - Funktion um userspezifische Programmaufrufe nach Aktualisierung eines Readings
  7468. # zu ermöglichen, arbeitet OHNE Event abhängig vom Attr userExitFn
  7469. #
  7470. # Aufruf der <UserExitFn> mit $name,$reading,$value
  7471. ####################################################################################################
  7472. sub DbRep_userexit ($$$) {
  7473. my ($name,$reading,$value) = @_;
  7474. my $hash = $defs{$name};
  7475. return if(!$hash->{HELPER}{USEREXITFN});
  7476. if(!defined($reading)) {$reading = "";}
  7477. if(!defined($value)) {$value = "";}
  7478. $value =~ s/\\/\\\\/g; # escapen of chars for evaluation
  7479. $value =~ s/'/\\'/g;
  7480. my $re = $hash->{HELPER}{UEFN_REGEXP}?$hash->{HELPER}{UEFN_REGEXP}:".*:.*";
  7481. if("$reading:$value" =~ m/^$re$/ ) {
  7482. my @res;
  7483. my $cmd = $hash->{HELPER}{USEREXITFN}."('$name','$reading','$value')";
  7484. $cmd = "{".$cmd."}";
  7485. my $r = AnalyzeCommandChain(undef, $cmd);
  7486. }
  7487. return;
  7488. }
  7489. ####################################################################################################
  7490. # delete Readings before new operation
  7491. ####################################################################################################
  7492. sub DbRep_delread($;$$) {
  7493. # Readings löschen die nicht in der Ausnahmeliste (Attr readingPreventFromDel) stehen
  7494. my ($hash,$shutdown) = @_;
  7495. my $name = $hash->{NAME};
  7496. my @allrds = keys%{$defs{$name}{READINGS}};
  7497. if($shutdown) {
  7498. my $do = 0;
  7499. foreach my $key(@allrds) {
  7500. # Highlighted Readings löschen und save statefile wegen Inkompatibilitär beim Restart
  7501. if($key =~ /<html><span/) {
  7502. $do = 1;
  7503. delete($defs{$name}{READINGS}{$key});
  7504. }
  7505. }
  7506. WriteStatefile() if($do == 1);
  7507. return undef;
  7508. }
  7509. my @rdpfdel = split(",", $hash->{HELPER}{RDPFDEL}) if($hash->{HELPER}{RDPFDEL});
  7510. if(@rdpfdel) {
  7511. foreach my $key(@allrds) {
  7512. # Log3 ($name, 1, "DbRep $name - Reading Schlüssel: $key");
  7513. my $dodel = 1;
  7514. foreach my $rdpfdel(@rdpfdel) {
  7515. if($key =~ /$rdpfdel/ || $key eq "state") {
  7516. $dodel = 0;
  7517. }
  7518. }
  7519. if($dodel) {
  7520. delete($defs{$name}{READINGS}{$key});
  7521. }
  7522. }
  7523. } else {
  7524. foreach my $key(@allrds) {
  7525. # Log3 ($name, 1, "DbRep $name - Reading Schlüssel: $key");
  7526. delete($defs{$name}{READINGS}{$key}) if($key ne "state");
  7527. }
  7528. }
  7529. return undef;
  7530. }
  7531. ####################################################################################################
  7532. # erstellen neues SQL-File für Dumproutine
  7533. ####################################################################################################
  7534. sub DbRep_NewDumpFilename ($$$$$){
  7535. my ($sql_text,$dump_path,$dbname,$time_stamp,$character_set) = @_;
  7536. my $part = "";
  7537. my $sql_file = $dump_path.$dbname."_".$time_stamp.$part.".sql";
  7538. my $backupfile = $dbname."_".$time_stamp.$part.".sql";
  7539. $sql_text .= "/*!40101 SET NAMES '".$character_set."' */;\n";
  7540. $sql_text .= "SET FOREIGN_KEY_CHECKS=0;\n";
  7541. my ($filesize,$err) = DbRep_WriteToDumpFile($sql_text,$sql_file);
  7542. if($err) {
  7543. return (undef,undef,undef,undef,$err);
  7544. }
  7545. chmod(0777,$sql_file);
  7546. $sql_text = "";
  7547. my $first_insert = 0;
  7548. return ($sql_text,$first_insert,$sql_file,$backupfile,undef);
  7549. }
  7550. ####################################################################################################
  7551. # Schreiben DB-Dumps in SQL-File
  7552. ####################################################################################################
  7553. sub DbRep_WriteToDumpFile ($$) {
  7554. my ($inh,$sql_file) = @_;
  7555. my $filesize;
  7556. my $err = 0;
  7557. if(length($inh) > 0) {
  7558. unless(open(DATEI,">>$sql_file")) {
  7559. $err = "Can't open file '$sql_file' for write access";
  7560. return (undef,$err);
  7561. }
  7562. print DATEI $inh;
  7563. close(DATEI);
  7564. my $fref = stat($sql_file);
  7565. if ($fref =~ /ARRAY/) {
  7566. $filesize = (@{stat($sql_file)})[7];
  7567. } else {
  7568. $filesize = (stat($sql_file))[7];
  7569. }
  7570. }
  7571. return ($filesize,undef);
  7572. }
  7573. ####################################################################################################
  7574. # Filesize (Byte) umwandeln in KB bzw. MB
  7575. ####################################################################################################
  7576. sub DbRep_byteOutput ($) {
  7577. my $bytes = shift;
  7578. return if(!defined($bytes));
  7579. return $bytes if(!looks_like_number($bytes));
  7580. my $suffix = "Bytes";
  7581. if ($bytes >= 1024) { $suffix = "KB"; $bytes = sprintf("%.2f",($bytes/1024));};
  7582. if ($bytes >= 1024) { $suffix = "MB"; $bytes = sprintf("%.2f",($bytes/1024));};
  7583. my $ret = sprintf "%.2f",$bytes;
  7584. $ret.=' '.$suffix;
  7585. return $ret;
  7586. }
  7587. ####################################################################################################
  7588. # Schreibroutine in DbRep Keyvalue-File
  7589. ####################################################################################################
  7590. sub DbRep_setCmdFile($$$) {
  7591. my ($key,$value,$hash) = @_;
  7592. my $fName = $attr{global}{modpath}."/FHEM/FhemUtils/cacheDbRep";
  7593. my $param = {
  7594. FileName => $fName,
  7595. ForceType => "file",
  7596. };
  7597. my ($err, @old) = FileRead($param);
  7598. DbRep_createCmdFile($hash) if($err);
  7599. my @new;
  7600. my $fnd;
  7601. foreach my $l (@old) {
  7602. if($l =~ m/^$key:/) {
  7603. $fnd = 1;
  7604. push @new, "$key:$value" if(defined($value));
  7605. } else {
  7606. push @new, $l;
  7607. }
  7608. }
  7609. push @new, "$key:$value" if(!$fnd && defined($value));
  7610. return FileWrite($param, @new);
  7611. }
  7612. ####################################################################################################
  7613. # anlegen Keyvalue-File für DbRep wenn nicht vorhanden
  7614. ####################################################################################################
  7615. sub DbRep_createCmdFile ($) {
  7616. my ($hash) = @_;
  7617. my $fName = $attr{global}{modpath}."/FHEM/FhemUtils/cacheDbRep";
  7618. my $param = {
  7619. FileName => $fName,
  7620. ForceType => "file",
  7621. };
  7622. my @new;
  7623. push(@new, "# This file is auto generated from 93_DbRep.",
  7624. "# Please do not modify, move or delete it.",
  7625. "");
  7626. return FileWrite($param, @new);
  7627. }
  7628. ####################################################################################################
  7629. # Leseroutine aus DbRep Keyvalue-File
  7630. ####################################################################################################
  7631. sub DbRep_getCmdFile($) {
  7632. my ($key) = @_;
  7633. my $fName = $attr{global}{modpath}."/FHEM/FhemUtils/cacheDbRep";
  7634. my $param = {
  7635. FileName => $fName,
  7636. ForceType => "file",
  7637. };
  7638. my ($err, @l) = FileRead($param);
  7639. return ($err, undef) if($err);
  7640. for my $l (@l) {
  7641. return (undef, $1) if($l =~ m/^$key:(.*)/);
  7642. }
  7643. return (undef, undef);
  7644. }
  7645. ####################################################################################################
  7646. # Tabellenoptimierung MySQL
  7647. ####################################################################################################
  7648. sub DbRep_mysqlOptimizeTables ($$@) {
  7649. my ($hash,$dbh,@tablenames) = @_;
  7650. my $name = $hash->{NAME};
  7651. my $dbname = $hash->{DATABASE};
  7652. my $ret = 0;
  7653. my $opttbl = 0;
  7654. my $db_tables = $hash->{HELPER}{DBTABLES};
  7655. my ($engine,$tablename,$query,$sth,$value,$db_MB_start,$db_MB_end);
  7656. # Anfangsgröße ermitteln
  7657. $query = "SELECT sum( data_length + index_length ) / 1024 / 1024 FROM information_schema.TABLES where table_schema='$dbname' ";
  7658. Log3 ($name, 5, "DbRep $name - current query: $query ");
  7659. eval { $sth = $dbh->prepare($query);
  7660. $sth->execute;
  7661. };
  7662. if ($@) {
  7663. Log3 ($name, 2, "DbRep $name - Error executing: '".$query."' ! MySQL-Error: ".$@);
  7664. $sth->finish;
  7665. $dbh->disconnect;
  7666. return ($@,undef,undef);
  7667. }
  7668. $value = $sth->fetchrow();
  7669. $db_MB_start = sprintf("%.2f",$value);
  7670. Log3 ($name, 3, "DbRep $name - Size of database $dbname before optimize (MB): $db_MB_start");
  7671. Log3($name, 3, "DbRep $name - Optimizing tables");
  7672. foreach $tablename (@tablenames) {
  7673. #optimize table if engine supports optimization
  7674. $engine = '';
  7675. $engine = uc($db_tables->{$tablename}{Engine}) if($db_tables->{$tablename}{Engine});
  7676. if ($engine =~ /(MYISAM|BDB|INNODB|ARIA)/) {
  7677. Log3($name, 3, "DbRep $name - Optimizing table `$tablename` ($engine). It will take a while.");
  7678. my $sth_to = $dbh->prepare("OPTIMIZE TABLE `$tablename`");
  7679. $ret = $sth_to->execute;
  7680. if ($ret) {
  7681. Log3($name, 3, "DbRep $name - Table ".($opttbl+1)." `$tablename` optimized successfully.");
  7682. $opttbl++;
  7683. } else {
  7684. Log3($name, 2, "DbRep $name - Error while optimizing table $tablename. Continue with next table or backup.");
  7685. }
  7686. }
  7687. }
  7688. Log3($name, 3, "DbRep $name - $opttbl tables have been optimized.") if($opttbl > 0);
  7689. # Endgröße ermitteln
  7690. eval { $sth->execute; };
  7691. if ($@) {
  7692. Log3 ($name, 2, "DbRep $name - Error executing: '".$query."' ! MySQL-Error: ".$@);
  7693. $sth->finish;
  7694. $dbh->disconnect;
  7695. return ($@,undef,undef);
  7696. }
  7697. $value = $sth->fetchrow();
  7698. $db_MB_end = sprintf("%.2f",$value);
  7699. Log3 ($name, 3, "DbRep $name - Size of database $dbname after optimize (MB): $db_MB_end");
  7700. $sth->finish;
  7701. return (undef,$db_MB_start,$db_MB_end);
  7702. }
  7703. ####################################################################################################
  7704. # Dump-Files im dumpDirLocal löschen bis auf die letzten "n"
  7705. ####################################################################################################
  7706. sub DbRep_deldumpfiles ($$) {
  7707. my ($hash,$bfile) = @_;
  7708. my $name = $hash->{NAME};
  7709. my $dbloghash = $hash->{dbloghash};
  7710. my $dump_path_def = $attr{global}{modpath}."/log/";
  7711. my $dump_path_loc = AttrVal($name,"dumpDirLocal", $dump_path_def);
  7712. $dump_path_loc = $dump_path_loc."/" unless($dump_path_loc =~ m/\/$/);
  7713. my $dfk = AttrVal($name,"dumpFilesKeep", 3);
  7714. my $pfix = (split '\.', $bfile)[1];
  7715. my $dbname = (split '_', $bfile)[0];
  7716. my $file = $dbname."_.*".$pfix.".*"; # Files mit/ohne Endung "gzip" berücksichtigen
  7717. my @fd;
  7718. if(!opendir(DH, $dump_path_loc)) {
  7719. push(@fd, "No files deleted - Can't open path '$dump_path_loc'");
  7720. return @fd;
  7721. }
  7722. my @files = sort grep {/^$file$/} readdir(DH);
  7723. my $fref = stat("$dump_path_loc/$bfile");
  7724. if ($fref =~ /ARRAY/) {
  7725. @files = sort { (@{stat("$dump_path_loc/$a")})[9] cmp (@{stat("$dump_path_loc/$b")})[9] } @files
  7726. if(AttrVal("global", "archivesort", "alphanum") eq "timestamp");
  7727. } else {
  7728. @files = sort { (stat("$dump_path_loc/$a"))[9] cmp (stat("$dump_path_loc/$b"))[9] } @files
  7729. if(AttrVal("global", "archivesort", "alphanum") eq "timestamp");
  7730. }
  7731. closedir(DH);
  7732. Log3($name, 5, "DbRep $name - Dump files have been found in dumpDirLocal '$dump_path_loc': ".join(', ',@files) );
  7733. my $max = int(@files)-$dfk;
  7734. for(my $i = 0; $i < $max; $i++) {
  7735. push(@fd, $files[$i]);
  7736. Log3($name, 3, "DbRep $name - Deleting old dumpfile '$files[$i]' ");
  7737. unlink("$dump_path_loc/$files[$i]");
  7738. }
  7739. return @fd;
  7740. }
  7741. ####################################################################################################
  7742. # Dumpfile komprimieren
  7743. ####################################################################################################
  7744. sub DbRep_dumpCompress ($$) {
  7745. my ($hash,$bfile) = @_;
  7746. my $name = $hash->{NAME};
  7747. my $dump_path_def = $attr{global}{modpath}."/log/";
  7748. my $dump_path_loc = AttrVal($name,"dumpDirLocal", $dump_path_def);
  7749. $dump_path_loc =~ s/(\/$|\\$)//;
  7750. my $input = $dump_path_loc."/".$bfile;
  7751. my $output = $dump_path_loc."/".$bfile.".gzip";
  7752. Log3($name, 3, "DbRep $name - compress file $input");
  7753. my $stat = gzip $input => $output ,BinModeIn => 1;
  7754. if($GzipError) {
  7755. Log3($name, 2, "DbRep $name - gzip of $input failed: $GzipError");
  7756. return ($GzipError,$input);
  7757. }
  7758. Log3($name, 3, "DbRep $name - file compressed to output file: $output");
  7759. unlink("$input");
  7760. Log3($name, 3, "DbRep $name - input file deleted: $input");
  7761. return (undef,$bfile.".gzip");
  7762. }
  7763. ####################################################################################################
  7764. # Dumpfile dekomprimieren
  7765. ####################################################################################################
  7766. sub DbRep_dumpUnCompress ($$) {
  7767. my ($hash,$bfile) = @_;
  7768. my $name = $hash->{NAME};
  7769. my $dump_path_def = $attr{global}{modpath}."/log/";
  7770. my $dump_path_loc = AttrVal($name,"dumpDirLocal", $dump_path_def);
  7771. $dump_path_loc =~ s/(\/$|\\$)//;
  7772. my $input = $dump_path_loc."/".$bfile;
  7773. my $outfile = $bfile;
  7774. $outfile =~ s/\.gzip//;
  7775. my $output = $dump_path_loc."/".$outfile;
  7776. Log3($name, 3, "DbRep $name - uncompress file $input");
  7777. my $stat = gunzip $input => $output ,BinModeOut => 1;
  7778. if($GunzipError) {
  7779. Log3($name, 2, "DbRep $name - gunzip of $input failed: $GunzipError");
  7780. return ($GunzipError,$input);
  7781. }
  7782. Log3($name, 3, "DbRep $name - file uncompressed to output file: $output");
  7783. # Größe dekomprimiertes File ermitteln
  7784. my @a = split(' ',qx(du $output)) if ($^O =~ m/linux/i || $^O =~ m/unix/i);
  7785. my $filesize = ($a[0])?($a[0]*1024):undef;
  7786. my $fsize = DbRep_byteOutput($filesize);
  7787. Log3 ($name, 3, "DbRep $name - Size of uncompressed file: ".$fsize);
  7788. return (undef,$outfile);
  7789. }
  7790. ####################################################################################################
  7791. # erzeugtes Dump-File aus dumpDirLocal zum FTP-Server übertragen
  7792. ####################################################################################################
  7793. sub DbRep_sendftp ($$) {
  7794. my ($hash,$bfile) = @_;
  7795. my $name = $hash->{NAME};
  7796. my $dump_path_def = $attr{global}{modpath}."/log/";
  7797. my $dump_path_loc = AttrVal($name,"dumpDirLocal", $dump_path_def);
  7798. my $file = (split /[\/]/, $bfile)[-1];
  7799. my $ftpto = AttrVal($name,"ftpTimeout",30);
  7800. my $ftpUse = AttrVal($name,"ftpUse",0);
  7801. my $ftpuseSSL = AttrVal($name,"ftpUseSSL",0);
  7802. my $ftpDir = AttrVal($name,"ftpDir","/");
  7803. my $ftpPort = AttrVal($name,"ftpPort",21);
  7804. my $ftpServer = AttrVal($name,"ftpServer",undef);
  7805. my $ftpUser = AttrVal($name,"ftpUser","anonymous");
  7806. my $ftpPwd = AttrVal($name,"ftpPwd",undef);
  7807. my $ftpPassive = AttrVal($name,"ftpPassive",0);
  7808. my $ftpDebug = AttrVal($name,"ftpDebug",0);
  7809. my $fdfk = AttrVal($name,"ftpDumpFilesKeep", 3);
  7810. my $pfix = (split '\.', $bfile)[1];
  7811. my $dbname = (split '_', $bfile)[0];
  7812. my $ftpl = $dbname."_.*".$pfix.".*"; # Files mit/ohne Endung "gzip" berücksichtigen
  7813. my ($ftperr,$ftpmsg,$ftp);
  7814. # kein FTP verwenden oder möglich
  7815. return ($ftperr,$ftpmsg) if((!$ftpUse && !$ftpuseSSL) || !$bfile);
  7816. if(!$ftpServer) {
  7817. $ftperr = "FTP-Error: FTP-Server isn't set.";
  7818. Log3($name, 2, "DbRep $name - $ftperr");
  7819. return ($ftperr,undef);
  7820. }
  7821. if(!opendir(DH, $dump_path_loc)) {
  7822. $ftperr = "FTP-Error: Can't open path '$dump_path_loc'";
  7823. Log3($name, 2, "DbRep $name - $ftperr");
  7824. return ($ftperr,undef);
  7825. }
  7826. my $mod_ftpssl = 0;
  7827. my $mod_ftp = 0;
  7828. my $mod;
  7829. if ($ftpuseSSL) {
  7830. # FTP mit SSL soll genutzt werden
  7831. $mod = "Net::FTPSSL => e.g. with 'sudo cpan -i Net::FTPSSL' ";
  7832. eval { require Net::FTPSSL; };
  7833. if(!$@){
  7834. $mod_ftpssl = 1;
  7835. import Net::FTPSSL;
  7836. }
  7837. } else {
  7838. # nur FTP
  7839. $mod = "Net::FTP";
  7840. eval { require Net::FTP; };
  7841. if(!$@){
  7842. $mod_ftp = 1;
  7843. import Net::FTP;
  7844. }
  7845. }
  7846. if ($ftpuseSSL && $mod_ftpssl) {
  7847. # use ftp-ssl
  7848. my $enc = "E";
  7849. eval { $ftp = Net::FTPSSL->new($ftpServer, Port => $ftpPort, Timeout => $ftpto, Debug => $ftpDebug, Encryption => $enc) }
  7850. or $ftperr = "FTP-SSL-ERROR: Can't connect - $@";
  7851. } elsif (!$ftpuseSSL && $mod_ftp) {
  7852. # use plain ftp
  7853. eval { $ftp = Net::FTP->new($ftpServer, Port => $ftpPort, Timeout => $ftpto, Debug => $ftpDebug, Passive => $ftpPassive) }
  7854. or $ftperr = "FTP-Error: Can't connect - $@";
  7855. } else {
  7856. $ftperr = "FTP-Error: required module couldn't be loaded. You have to install it first: $mod.";
  7857. }
  7858. if ($ftperr) {
  7859. Log3($name, 2, "DbRep $name - $ftperr");
  7860. return ($ftperr,undef);
  7861. }
  7862. my $pwdstr = $ftpPwd?$ftpPwd:" ";
  7863. $ftp->login($ftpUser, $ftpPwd) or $ftperr = "FTP-Error: Couldn't login with user '$ftpUser' and password '$pwdstr' ";
  7864. if ($ftperr) {
  7865. Log3($name, 2, "DbRep $name - $ftperr");
  7866. return ($ftperr,undef);
  7867. }
  7868. $ftp->binary();
  7869. # FTP Verzeichnis setzen
  7870. $ftp->cwd($ftpDir) or $ftperr = "FTP-Error: Couldn't change directory to '$ftpDir' ";
  7871. if ($ftperr) {
  7872. Log3($name, 2, "DbRep $name - $ftperr");
  7873. return ($ftperr,undef);
  7874. }
  7875. $dump_path_loc =~ s/(\/$|\\$)//;
  7876. Log3($name, 3, "DbRep $name - FTP: transferring ".$dump_path_loc."/".$file);
  7877. $ftpmsg = $ftp->put($dump_path_loc."/".$file);
  7878. if (!$ftpmsg) {
  7879. $ftperr = "FTP-Error: Couldn't transfer ".$file." to ".$ftpServer." into dir ".$ftpDir;
  7880. Log3($name, 2, "DbRep $name - $ftperr");
  7881. } else {
  7882. $ftpmsg = "FTP: ".$file." transferred successfully to ".$ftpServer." into dir ".$ftpDir;
  7883. Log3($name, 3, "DbRep $name - $ftpmsg");
  7884. }
  7885. # Versionsverwaltung FTP-Verzeichnis
  7886. my (@ftl,@ftpfd);
  7887. if($ftpuseSSL) {
  7888. @ftl = sort grep {/^$ftpl$/} $ftp->nlst();
  7889. } else {
  7890. @ftl = sort grep {/^$ftpl$/} @{$ftp->ls()};
  7891. }
  7892. Log3($name, 5, "DbRep $name - FTP: filelist of \"$ftpDir\": @ftl");
  7893. my $max = int(@ftl)-$fdfk;
  7894. for(my $i = 0; $i < $max; $i++) {
  7895. push(@ftpfd, $ftl[$i]);
  7896. Log3($name, 3, "DbRep $name - FTP: deleting old dumpfile '$ftl[$i]' ");
  7897. $ftp->delete($ftl[$i]);
  7898. }
  7899. return ($ftperr,$ftpmsg,@ftpfd);
  7900. }
  7901. ####################################################################################################
  7902. # Test auf Daylight saving time
  7903. ####################################################################################################
  7904. sub DbRep_dsttest ($$$) {
  7905. my ($hash,$runtime,$aggsec) = @_;
  7906. my $name = $hash->{NAME};
  7907. my $dstchange = 0;
  7908. # der Wechsel der daylight saving time wird dadurch getestet, dass geprüft wird
  7909. # ob im Vergleich der aktuellen zur nächsten Selektionsperiode von "$aggsec (day, week, month)"
  7910. # ein Wechsel der daylight saving time vorliegt
  7911. my $dst = (localtime($runtime))[8]; # ermitteln daylight saving aktuelle runtime
  7912. my $time_str = localtime($runtime+$aggsec); # textual time representation
  7913. my $dst_new = (localtime($runtime+$aggsec))[8]; # ermitteln daylight saving nächste runtime
  7914. if ($dst != $dst_new) {
  7915. $dstchange = 1;
  7916. }
  7917. Log3 ($name, 5, "DbRep $name - Daylight savings changed: $dstchange (on $time_str)");
  7918. return $dstchange;
  7919. }
  7920. ####################################################################################################
  7921. # Counthash Untersuchung
  7922. # Logausgabe der Anzahl verarbeiteter Datensätze pro Zeitraum / Aggregation
  7923. # Rückgabe eines ncp-hash (no calc in period) mit den Perioden für die keine Differenz berechnet
  7924. # werden konnte weil nur ein Datensatz in der Periode zur Verfügung stand
  7925. ####################################################################################################
  7926. sub DbRep_calcount ($$) {
  7927. my ($hash,$ch) = @_;
  7928. my $name = $hash->{NAME};
  7929. my %ncp = ();
  7930. Log3 ($name, 4, "DbRep $name - count of values used for calc:");
  7931. foreach my $key (sort(keys%{$ch})) {
  7932. Log3 ($name, 4, "$key => ". $ch->{$key});
  7933. if($ch->{$key} eq "1") {
  7934. $ncp{"$key"} = " ||";
  7935. }
  7936. }
  7937. return \%ncp;
  7938. }
  7939. ####################################################################################################
  7940. # Funktionsergebnisse in Datenbank schreiben
  7941. ####################################################################################################
  7942. sub DbRep_OutputWriteToDB($$$$$) {
  7943. my ($name,$device,$reading,$arrstr,$optxt) = @_;
  7944. my $hash = $defs{$name};
  7945. my $dbloghash = $hash->{dbloghash};
  7946. my $dbconn = $dbloghash->{dbconn};
  7947. my $dbuser = $dbloghash->{dbuser};
  7948. my $dblogname = $dbloghash->{NAME};
  7949. my $dbmodel = $hash->{dbloghash}{MODEL};
  7950. my $DbLogType = AttrVal($hash->{dbloghash}{NAME}, "DbLogType", "History");
  7951. my $supk = AttrVal($hash->{dbloghash}{NAME}, "noSupportPK", 0);
  7952. my $dbpassword = $attr{"sec$dblogname"}{secret};
  7953. my $utf8 = defined($hash->{UTF8})?$hash->{UTF8}:0;
  7954. $device =~ s/[^A-Za-z\/\d_\.-]/\//g;
  7955. $reading =~ s/[^A-Za-z\/\d_\.-]/\//g;
  7956. my $type = "calculated";
  7957. my $event = "calculated";
  7958. my $unit = "";
  7959. my $wrt = 0;
  7960. my $irowdone = 0;
  7961. my ($dbh,$sth_ih,$sth_uh,$sth_ic,$sth_uc,$err,$timestamp,$value,$date,$time,$rsf,$aggr,@row_array);
  7962. if(!$hash->{dbloghash}{HELPER}{COLSET}) {
  7963. $err = "No result of \"$hash->{LASTCMD}\" to database written. Cause: column width in \"$hash->{DEF}\" isn't set";
  7964. return ($wrt,$irowdone,$err);
  7965. }
  7966. no warnings 'uninitialized';
  7967. (undef,undef,$aggr) = DbRep_checktimeaggr($hash);
  7968. $reading = $optxt."_".$aggr."_".AttrVal($name, "readingNameMap", $reading);
  7969. $type = $defs{$device}{TYPE} if($defs{$device}); # $type vom Device ableiten
  7970. if($optxt =~ /avg|sum/) {
  7971. my @arr = split("\\|", $arrstr);
  7972. foreach my $row (@arr) {
  7973. my @a = split("#", $row);
  7974. my $runtime_string = $a[0]; # Aggregations-Alias (nicht benötigt)
  7975. $value = defined($a[1])?sprintf("%.4f",$a[1]):undef;
  7976. $rsf = $a[2]; # Datum / Zeit für DB-Speicherung
  7977. ($date,$time) = split("_",$rsf);
  7978. $time =~ s/-/:/g if($time);
  7979. if($time !~ /^(\d{2}):(\d{2}):(\d{2})$/) {
  7980. if($aggr =~ /no|day|week|month/) {
  7981. $time = "23:59:58";
  7982. } elsif ($aggr =~ /hour/) {
  7983. $time = "$time:59:58";
  7984. }
  7985. }
  7986. if ($value) {
  7987. # Daten auf maximale Länge beschneiden (DbLog-Funktion !)
  7988. ($device,$type,$event,$reading,$value,$unit) = DbLog_cutCol($hash->{dbloghash},$device,$type,$event,$reading,$value,$unit);
  7989. push(@row_array, "$date $time|$device|$type|$event|$reading|$value|$unit");
  7990. }
  7991. }
  7992. }
  7993. if($optxt =~ /min|max|diff/) {
  7994. my %rh = split("§", $arrstr);
  7995. foreach my $key (sort(keys(%rh))) {
  7996. my @k = split("\\|",$rh{$key});
  7997. $rsf = $k[2]; # Datum / Zeit für DB-Speicherung
  7998. $value = defined($k[1])?sprintf("%.4f",$k[1]):undef;
  7999. ($date,$time) = split("_",$rsf);
  8000. $time =~ s/-/:/g if($time);
  8001. if($time !~ /^(\d{2}):(\d{2}):(\d{2})$/) {
  8002. if($aggr =~ /no|day|week|month/) {
  8003. $time = "23:59:58";
  8004. } elsif ($aggr =~ /hour/) {
  8005. $time = "$time:59:58";
  8006. }
  8007. }
  8008. if ($value) {
  8009. # Daten auf maximale Länge beschneiden (DbLog-Funktion !)
  8010. ($device,$type,$event,$reading,$value,$unit) = DbLog_cutCol($hash->{dbloghash},$device,$type,$event,$reading,$value,$unit);
  8011. push(@row_array, "$date $time|$device|$type|$event|$reading|$value|$unit");
  8012. }
  8013. }
  8014. }
  8015. if (@row_array) {
  8016. # Schreibzyklus aktivieren
  8017. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoCommit => 1, mysql_enable_utf8 => $utf8 });};
  8018. if ($@) {
  8019. $err = $@;
  8020. Log3 ($name, 2, "DbRep $name - $@");
  8021. return ($wrt,$irowdone,$err);
  8022. }
  8023. # check ob PK verwendet wird, @usepkx?Anzahl der Felder im PK:0 wenn kein PK, $pkx?Namen der Felder:none wenn kein PK
  8024. my ($usepkh,$usepkc,$pkh,$pkc);
  8025. if (!$supk) {
  8026. ($usepkh,$usepkc,$pkh,$pkc) = DbRep_checkUsePK($hash,$dbloghash,$dbh);
  8027. } else {
  8028. Log3 $hash->{NAME}, 5, "DbRep $name -> Primary Key usage suppressed by attribute noSupportPK in DbLog \"$dblogname\"";
  8029. }
  8030. if (lc($DbLogType) =~ m(history)) {
  8031. # insert history mit/ohne primary key
  8032. if ($usepkh && $dbloghash->{MODEL} eq 'MYSQL') {
  8033. eval { $sth_ih = $dbh->prepare_cached("INSERT IGNORE INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  8034. } elsif ($usepkh && $dbloghash->{MODEL} eq 'SQLITE') {
  8035. eval { $sth_ih = $dbh->prepare_cached("INSERT OR IGNORE INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  8036. } elsif ($usepkh && $dbloghash->{MODEL} eq 'POSTGRESQL') {
  8037. eval { $sth_ih = $dbh->prepare_cached("INSERT INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?) ON CONFLICT DO NOTHING"); };
  8038. } else {
  8039. eval { $sth_ih = $dbh->prepare_cached("INSERT INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  8040. }
  8041. if ($@) {
  8042. $err = $@;
  8043. Log3 ($name, 2, "DbRep $name - $@");
  8044. return ($wrt,$irowdone,$err);
  8045. }
  8046. # update history mit/ohne primary key
  8047. if ($usepkh && $hash->{MODEL} eq 'MYSQL') {
  8048. $sth_uh = $dbh->prepare("REPLACE INTO history (TYPE, EVENT, VALUE, UNIT, TIMESTAMP, DEVICE, READING) VALUES (?,?,?,?,?,?,?)");
  8049. } elsif ($usepkh && $hash->{MODEL} eq 'SQLITE') {
  8050. $sth_uh = $dbh->prepare("INSERT OR REPLACE INTO history (TYPE, EVENT, VALUE, UNIT, TIMESTAMP, DEVICE, READING) VALUES (?,?,?,?,?,?,?)");
  8051. } elsif ($usepkh && $hash->{MODEL} eq 'POSTGRESQL') {
  8052. $sth_uh = $dbh->prepare("INSERT INTO history (TYPE, EVENT, VALUE, UNIT, TIMESTAMP, DEVICE, READING) VALUES (?,?,?,?,?,?,?) ON CONFLICT ($pkc)
  8053. DO UPDATE SET TIMESTAMP=EXCLUDED.TIMESTAMP, DEVICE=EXCLUDED.DEVICE, TYPE=EXCLUDED.TYPE, EVENT=EXCLUDED.EVENT, READING=EXCLUDED.READING,
  8054. VALUE=EXCLUDED.VALUE, UNIT=EXCLUDED.UNIT");
  8055. } else {
  8056. $sth_uh = $dbh->prepare("UPDATE history SET TYPE=?, EVENT=?, VALUE=?, UNIT=? WHERE (TIMESTAMP=?) AND (DEVICE=?) AND (READING=?)");
  8057. }
  8058. }
  8059. if (lc($DbLogType) =~ m(current) ) {
  8060. # insert current mit/ohne primary key
  8061. if ($usepkc && $hash->{MODEL} eq 'MYSQL') {
  8062. eval { $sth_ic = $dbh->prepare("INSERT IGNORE INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  8063. } elsif ($usepkc && $hash->{MODEL} eq 'SQLITE') {
  8064. eval { $sth_ic = $dbh->prepare("INSERT OR IGNORE INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  8065. } elsif ($usepkc && $hash->{MODEL} eq 'POSTGRESQL') {
  8066. eval { $sth_ic = $dbh->prepare("INSERT INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?) ON CONFLICT DO NOTHING"); };
  8067. } else {
  8068. # old behavior
  8069. eval { $sth_ic = $dbh->prepare("INSERT INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  8070. }
  8071. if ($@) {
  8072. $err = $@;
  8073. Log3 ($name, 2, "DbRep $name - $@");
  8074. return ($wrt,$irowdone,$err);
  8075. }
  8076. # update current mit/ohne primary key
  8077. if ($usepkc && $hash->{MODEL} eq 'MYSQL') {
  8078. $sth_uc = $dbh->prepare("REPLACE INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)");
  8079. } elsif ($usepkc && $hash->{MODEL} eq 'SQLITE') {
  8080. $sth_uc = $dbh->prepare("INSERT OR REPLACE INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)");
  8081. } elsif ($usepkc && $hash->{MODEL} eq 'POSTGRESQL') {
  8082. $sth_uc = $dbh->prepare("INSERT INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?) ON CONFLICT ($pkc)
  8083. DO UPDATE SET TIMESTAMP=EXCLUDED.TIMESTAMP, DEVICE=EXCLUDED.DEVICE, TYPE=EXCLUDED.TYPE, EVENT=EXCLUDED.EVENT, READING=EXCLUDED.READING,
  8084. VALUE=EXCLUDED.VALUE, UNIT=EXCLUDED.UNIT");
  8085. } else {
  8086. $sth_uc = $dbh->prepare("UPDATE current SET TIMESTAMP=?, TYPE=?, EVENT=?, VALUE=?, UNIT=? WHERE (DEVICE=?) AND (READING=?)");
  8087. }
  8088. }
  8089. eval { $dbh->begin_work() if($dbh->{AutoCommit}); };
  8090. if ($@) {
  8091. Log3($name, 2, "DbRep $name -> Error start transaction for history - $@");
  8092. }
  8093. Log3 $hash->{NAME}, 4, "DbRep $name - data prepared to db write:";
  8094. # SQL-Startzeit
  8095. my $wst = [gettimeofday];
  8096. my $ihs = 0;
  8097. my $uhs = 0;
  8098. foreach my $row (@row_array) {
  8099. my @a = split("\\|",$row);
  8100. $timestamp = $a[0];
  8101. $device = $a[1];
  8102. $type = $a[2];
  8103. $event = $a[3];
  8104. $reading = $a[4];
  8105. $value = $a[5];
  8106. $unit = $a[6];
  8107. Log3 $hash->{NAME}, 4, "DbRep $name - $row";
  8108. eval {
  8109. # update oder insert history
  8110. if (lc($DbLogType) =~ m(history) ) {
  8111. my $rv_uh = $sth_uh->execute($type,$event,$value,$unit,$timestamp,$device,$reading);
  8112. if ($rv_uh == 0) {
  8113. $sth_ih->execute($timestamp,$device,$type,$event,$reading,$value,$unit);
  8114. $ihs++;
  8115. } else {
  8116. $uhs++;
  8117. }
  8118. }
  8119. # update oder insert current
  8120. if (lc($DbLogType) =~ m(current) ) {
  8121. my $rv_uc = $sth_uc->execute($timestamp,$type,$event,$value,$unit,$device,$reading);
  8122. if ($rv_uc == 0) {
  8123. $sth_ic->execute($timestamp,$device,$type,$event,$reading,$value,$unit);
  8124. }
  8125. }
  8126. };
  8127. if ($@) {
  8128. $err = $@;
  8129. Log3 ($name, 2, "DbRep $name - $@");
  8130. $dbh->rollback;
  8131. $dbh->disconnect;
  8132. $ihs = 0;
  8133. $uhs = 0;
  8134. return ($wrt,0,$err);
  8135. } else {
  8136. $irowdone++;
  8137. }
  8138. }
  8139. eval {$dbh->commit() if(!$dbh->{AutoCommit});};
  8140. $dbh->disconnect;
  8141. Log3 $hash->{NAME}, 3, "DbRep $name - number of lines updated in \"$dblogname\": $uhs";
  8142. Log3 $hash->{NAME}, 3, "DbRep $name - number of lines inserted into \"$dblogname\": $ihs";
  8143. # SQL-Laufzeit ermitteln
  8144. $wrt = tv_interval($wst);
  8145. }
  8146. return ($wrt,$irowdone,$err);
  8147. }
  8148. ####################################################################################################
  8149. # Werte eines Array in DB schreiben
  8150. # Übergabe-Array: $date_ESC_$time_ESC_$device_ESC_$type_ESC_$event_ESC_$reading_ESC_$value_ESC_$unit
  8151. # $histupd = 1 wenn history update, $histupd = 0 nur history insert
  8152. #
  8153. ####################################################################################################
  8154. sub DbRep_WriteToDB($$$@) {
  8155. my ($name,$dbh,$dbloghash,$histupd,@row_array) = @_;
  8156. my $hash = $defs{$name};
  8157. my $dblogname = $dbloghash->{NAME};
  8158. my $DbLogType = AttrVal($dbloghash->{NAME}, "DbLogType", "History");
  8159. my $supk = AttrVal($dbloghash->{NAME}, "noSupportPK", 0);
  8160. my $wrt = 0;
  8161. my $irowdone = 0;
  8162. my ($sth_ih,$sth_uh,$sth_ic,$sth_uc,$err);
  8163. # check ob PK verwendet wird, @usepkx?Anzahl der Felder im PK:0 wenn kein PK, $pkx?Namen der Felder:none wenn kein PK
  8164. my ($usepkh,$usepkc,$pkh,$pkc);
  8165. if (!$supk) {
  8166. ($usepkh,$usepkc,$pkh,$pkc) = DbRep_checkUsePK($hash,$dbloghash,$dbh);
  8167. } else {
  8168. Log3 $hash->{NAME}, 5, "DbRep $name -> Primary Key usage suppressed by attribute noSupportPK in DbLog \"$dblogname\"";
  8169. }
  8170. if (lc($DbLogType) =~ m(history)) {
  8171. # insert history mit/ohne primary key
  8172. if ($usepkh && $dbloghash->{MODEL} eq 'MYSQL') {
  8173. eval { $sth_ih = $dbh->prepare_cached("INSERT IGNORE INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  8174. } elsif ($usepkh && $dbloghash->{MODEL} eq 'SQLITE') {
  8175. eval { $sth_ih = $dbh->prepare_cached("INSERT OR IGNORE INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  8176. } elsif ($usepkh && $dbloghash->{MODEL} eq 'POSTGRESQL') {
  8177. eval { $sth_ih = $dbh->prepare_cached("INSERT INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?) ON CONFLICT DO NOTHING"); };
  8178. } else {
  8179. eval { $sth_ih = $dbh->prepare_cached("INSERT INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  8180. }
  8181. if ($@) {
  8182. $err = $@;
  8183. Log3 ($name, 2, "DbRep $name - $@");
  8184. return ($wrt,$irowdone,$err);
  8185. }
  8186. # update history mit/ohne primary key
  8187. if ($usepkh && $dbloghash->{MODEL} eq 'MYSQL') {
  8188. $sth_uh = $dbh->prepare("REPLACE INTO history (TYPE, EVENT, VALUE, UNIT, TIMESTAMP, DEVICE, READING) VALUES (?,?,?,?,?,?,?)");
  8189. } elsif ($usepkh && $dbloghash->{MODEL} eq 'SQLITE') {
  8190. $sth_uh = $dbh->prepare("INSERT OR REPLACE INTO history (TYPE, EVENT, VALUE, UNIT, TIMESTAMP, DEVICE, READING) VALUES (?,?,?,?,?,?,?)");
  8191. } elsif ($usepkh && $dbloghash->{MODEL} eq 'POSTGRESQL') {
  8192. $sth_uh = $dbh->prepare("INSERT INTO history (TYPE, EVENT, VALUE, UNIT, TIMESTAMP, DEVICE, READING) VALUES (?,?,?,?,?,?,?) ON CONFLICT ($pkc)
  8193. DO UPDATE SET TIMESTAMP=EXCLUDED.TIMESTAMP, DEVICE=EXCLUDED.DEVICE, TYPE=EXCLUDED.TYPE, EVENT=EXCLUDED.EVENT, READING=EXCLUDED.READING,
  8194. VALUE=EXCLUDED.VALUE, UNIT=EXCLUDED.UNIT");
  8195. } else {
  8196. $sth_uh = $dbh->prepare("UPDATE history SET TYPE=?, EVENT=?, VALUE=?, UNIT=? WHERE (TIMESTAMP=?) AND (DEVICE=?) AND (READING=?)");
  8197. }
  8198. }
  8199. if (lc($DbLogType) =~ m(current) ) {
  8200. # insert current mit/ohne primary key
  8201. if ($usepkc && $dbloghash->{MODEL} eq 'MYSQL') {
  8202. eval { $sth_ic = $dbh->prepare("INSERT IGNORE INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  8203. } elsif ($usepkc && $dbloghash->{MODEL} eq 'SQLITE') {
  8204. eval { $sth_ic = $dbh->prepare("INSERT OR IGNORE INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  8205. } elsif ($usepkc && $dbloghash->{MODEL} eq 'POSTGRESQL') {
  8206. eval { $sth_ic = $dbh->prepare("INSERT INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?) ON CONFLICT DO NOTHING"); };
  8207. } else {
  8208. # old behavior
  8209. eval { $sth_ic = $dbh->prepare("INSERT INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  8210. }
  8211. if ($@) {
  8212. $err = $@;
  8213. Log3 ($name, 2, "DbRep $name - $@");
  8214. return ($wrt,$irowdone,$err);
  8215. }
  8216. # update current mit/ohne primary key
  8217. if ($usepkc && $dbloghash->{MODEL} eq 'MYSQL') {
  8218. $sth_uc = $dbh->prepare("REPLACE INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)");
  8219. } elsif ($usepkc && $dbloghash->{MODEL} eq 'SQLITE') {
  8220. $sth_uc = $dbh->prepare("INSERT OR REPLACE INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)");
  8221. } elsif ($usepkc && $dbloghash->{MODEL} eq 'POSTGRESQL') {
  8222. $sth_uc = $dbh->prepare("INSERT INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?) ON CONFLICT ($pkc)
  8223. DO UPDATE SET TIMESTAMP=EXCLUDED.TIMESTAMP, DEVICE=EXCLUDED.DEVICE, TYPE=EXCLUDED.TYPE, EVENT=EXCLUDED.EVENT, READING=EXCLUDED.READING,
  8224. VALUE=EXCLUDED.VALUE, UNIT=EXCLUDED.UNIT");
  8225. } else {
  8226. $sth_uc = $dbh->prepare("UPDATE current SET TIMESTAMP=?, TYPE=?, EVENT=?, VALUE=?, UNIT=? WHERE (DEVICE=?) AND (READING=?)");
  8227. }
  8228. }
  8229. eval { $dbh->begin_work() if($dbh->{AutoCommit}); };
  8230. if ($@) {
  8231. Log3($name, 2, "DbRep $name -> Error start transaction for history - $@");
  8232. }
  8233. Log3 $hash->{NAME}, 5, "DbRep $name - data prepared to db write:";
  8234. # SQL-Startzeit
  8235. my $wst = [gettimeofday];
  8236. my $ihs = 0;
  8237. my $uhs = 0;
  8238. foreach my $row (@row_array) {
  8239. my ($date,$time,$device,$type,$event,$reading,$value,$unit) = ($row =~ /^(.*)_ESC_(.*)_ESC_(.*)_ESC_(.*)_ESC_(.*)_ESC_(.*)_ESC_(.*)_ESC_(.*)$/);
  8240. Log3 $hash->{NAME}, 5, "DbRep $name - $row";
  8241. my $timestamp = $date." ".$time;
  8242. eval {
  8243. # update oder insert history
  8244. if (lc($DbLogType) =~ m(history) ) {
  8245. my $rv_uh = 0;
  8246. if($histupd) {
  8247. $rv_uh = $sth_uh->execute($type,$event,$value,$unit,$timestamp,$device,$reading);
  8248. }
  8249. if ($rv_uh == 0) {
  8250. $sth_ih->execute($timestamp,$device,$type,$event,$reading,$value,$unit);
  8251. $ihs++;
  8252. } else {
  8253. $uhs++;
  8254. }
  8255. }
  8256. # update oder insert current
  8257. if (lc($DbLogType) =~ m(current) ) {
  8258. my $rv_uc = $sth_uc->execute($timestamp,$type,$event,$value,$unit,$device,$reading);
  8259. if ($rv_uc == 0) {
  8260. $sth_ic->execute($timestamp,$device,$type,$event,$reading,$value,$unit);
  8261. }
  8262. }
  8263. };
  8264. if ($@) {
  8265. $err = $@;
  8266. Log3 ($name, 2, "DbRep $name - $@");
  8267. $dbh->rollback;
  8268. $ihs = 0;
  8269. $uhs = 0;
  8270. return ($wrt,0,$err);
  8271. } else {
  8272. $irowdone++;
  8273. }
  8274. }
  8275. eval {$dbh->commit() if(!$dbh->{AutoCommit});};
  8276. Log3 $hash->{NAME}, 3, "DbRep $name - number of lines updated in \"$dblogname\": $uhs" if($uhs);
  8277. Log3 $hash->{NAME}, 3, "DbRep $name - number of lines inserted into \"$dblogname\": $ihs" if($ihs);
  8278. # SQL-Laufzeit ermitteln
  8279. $wrt = tv_interval($wst);
  8280. return ($wrt,$irowdone,$err);
  8281. }
  8282. ################################################################
  8283. # check ob primary key genutzt wird
  8284. ################################################################
  8285. sub DbRep_checkUsePK ($$$){
  8286. my ($hash,$dbloghash,$dbh) = @_;
  8287. my $name = $hash->{NAME};
  8288. my $dbconn = $dbloghash->{dbconn};
  8289. my $upkh = 0;
  8290. my $upkc = 0;
  8291. my (@pkh,@pkc);
  8292. my $db = (split("=",(split(";",$dbconn))[0]))[1];
  8293. eval {@pkh = $dbh->primary_key( undef, undef, 'history' );};
  8294. eval {@pkc = $dbh->primary_key( undef, undef, 'current' );};
  8295. my $pkh = (!@pkh || @pkh eq "")?"none":join(",",@pkh);
  8296. my $pkc = (!@pkc || @pkc eq "")?"none":join(",",@pkc);
  8297. $pkh =~ tr/"//d;
  8298. $pkc =~ tr/"//d;
  8299. $upkh = 1 if(@pkh && @pkh ne "none");
  8300. $upkc = 1 if(@pkc && @pkc ne "none");
  8301. Log3 $hash->{NAME}, 5, "DbRep $name -> Primary Key used in $db.history: $pkh";
  8302. Log3 $hash->{NAME}, 5, "DbRep $name -> Primary Key used in $db.current: $pkc";
  8303. return ($upkh,$upkc,$pkh,$pkc);
  8304. }
  8305. ################################################################
  8306. # extrahiert aus dem übergebenen Wert nur die Zahl
  8307. ################################################################
  8308. sub DbRep_numval ($){
  8309. my ($val) = @_;
  8310. return undef if(!defined($val));
  8311. $val = ($val =~ /(-?\d+(\.\d+)?)/ ? $1 : "");
  8312. return $val;
  8313. }
  8314. ####################################################################################################
  8315. # blockierende DB-Abfrage
  8316. # liefert Ergebnis sofort zurück, setzt keine Readings
  8317. ####################################################################################################
  8318. sub DbRep_dbValue($$) {
  8319. my ($name,$cmd) = @_;
  8320. my $hash = $defs{$name};
  8321. my $dbloghash = $hash->{dbloghash};
  8322. my $dbconn = $dbloghash->{dbconn};
  8323. my $dbuser = $dbloghash->{dbuser};
  8324. my $dblogname = $dbloghash->{NAME};
  8325. my $dbpassword = $attr{"sec$dblogname"}{secret};
  8326. my $utf8 = defined($hash->{UTF8})?$hash->{UTF8}:0;
  8327. my $srs = AttrVal($name, "sqlResultFieldSep", "|");
  8328. my ($err,$ret,$dbh);
  8329. readingsDelete($hash, "errortext");
  8330. ReadingsSingleUpdateValue ($hash, "state", "running", 1);
  8331. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoCommit => 1, AutoInactiveDestroy => 1, mysql_enable_utf8 => $utf8 });};
  8332. if ($@) {
  8333. $err = $@;
  8334. Log3 ($name, 2, "DbRep $name - $err");
  8335. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  8336. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  8337. return ($err);
  8338. }
  8339. my $sql = ($cmd =~ m/\;$/)?$cmd:$cmd.";";
  8340. # Ausgaben
  8341. Log3 ($name, 4, "DbRep $name - -------- New selection --------- ");
  8342. Log3 ($name, 4, "DbRep $name - Command: dbValue");
  8343. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  8344. # SQL-Startzeit
  8345. my $st = [gettimeofday];
  8346. my ($sth,$r);
  8347. eval {$sth = $dbh->prepare($sql);
  8348. $r = $sth->execute();
  8349. };
  8350. if ($@) {
  8351. $err = $@;
  8352. Log3 ($name, 2, "DbRep $name - $err");
  8353. $dbh->disconnect;
  8354. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  8355. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  8356. return ($err);
  8357. }
  8358. my $nrows = 0;
  8359. if($sql =~ m/^\s*(select|pragma|show)/is) {
  8360. while (my @line = $sth->fetchrow_array()) {
  8361. Log3 ($name, 4, "DbRep $name - SQL result: @line");
  8362. $ret .= join("$srs", @line);
  8363. $ret .= "\n";
  8364. # Anzahl der Datensätze
  8365. $nrows++;
  8366. }
  8367. } else {
  8368. $nrows = $sth->rows;
  8369. eval {$dbh->commit() if(!$dbh->{AutoCommit});};
  8370. if ($@) {
  8371. $err = $@;
  8372. Log3 ($name, 2, "DbRep $name - $err");
  8373. $dbh->disconnect;
  8374. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  8375. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  8376. return ($err);
  8377. }
  8378. $ret = $nrows;
  8379. }
  8380. $sth->finish;
  8381. $dbh->disconnect;
  8382. # SQL-Laufzeit ermitteln
  8383. my $rt = tv_interval($st);
  8384. my $com = (split(" ",$sql, 2))[0];
  8385. Log3 ($name, 4, "DbRep $name - Number of entries processed in db $hash->{DATABASE}: $nrows by $com");
  8386. # Readingaufbereitung
  8387. readingsBeginUpdate($hash);
  8388. ReadingsBulkUpdateTimeState($hash,undef,$rt,"done");
  8389. readingsEndUpdate($hash, 1);
  8390. return ($ret);
  8391. }
  8392. ####################################################################################################
  8393. # blockierende DB-Abfrage
  8394. # liefert den Wert eines Device:Readings des nächsmöglichen Logeintrags zum
  8395. # angegebenen Zeitpunkt
  8396. #
  8397. # Aufruf: DbReadingsVal("<dbrep-device>","<device:reading>","<timestamp>,"<default>")
  8398. ####################################################################################################
  8399. sub DbReadingsVal($$$$) {
  8400. my ($name, $devread, $ts, $default) = @_;
  8401. my $hash = $defs{$name};
  8402. my $dbmodel = $defs{$hash->{HELPER}{DBLOGDEVICE}}{MODEL};
  8403. my ($err,$ret,$sql);
  8404. unless(defined($defs{$name})) {
  8405. return ("DbRep-device \"$name\" doesn't exist.");
  8406. }
  8407. unless($defs{$name}{TYPE} eq "DbRep") {
  8408. return ("\"$name\" is not a DbRep-device but of type \"".$defs{$name}{TYPE}."\"");
  8409. }
  8410. unless($ts =~ /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/) {
  8411. return ("timestamp has not a valid format. Use \"YYYY-MM-DD hh:mm:ss\" as timestamp.");
  8412. }
  8413. my ($dev,$reading) = split(":",$devread);
  8414. unless($dev && $reading) {
  8415. return ("device:reading must be specified !");
  8416. }
  8417. if($dbmodel eq "MYSQL") {
  8418. $sql = "select value from (
  8419. ( select *, TIMESTAMPDIFF(SECOND, '$ts', timestamp) as diff from history
  8420. where device='$dev' and reading='$reading' and timestamp >= '$ts' order by timestamp asc limit 1
  8421. )
  8422. union
  8423. ( select *, TIMESTAMPDIFF(SECOND, timestamp, '$ts') as diff from history
  8424. where device='$dev' and reading='$reading' and timestamp < '$ts' order by timestamp desc limit 1
  8425. )
  8426. ) x order by diff limit 1;";
  8427. } elsif ($dbmodel eq "SQLITE") {
  8428. $sql = "select value from (
  8429. select value, (julianday(timestamp) - julianday('$ts')) * 86400.0 as diff from history
  8430. where device='MyWetter' and reading='temperature' and timestamp >= '$ts'
  8431. union
  8432. select value, (julianday('$ts') - julianday(timestamp)) * 86400.0 as diff from history
  8433. where device='MyWetter' and reading='temperature' and timestamp < '$ts'
  8434. )
  8435. x order by diff limit 1;";
  8436. } elsif ($dbmodel eq "POSTGRESQL") {
  8437. $sql = "select value from (
  8438. select value, EXTRACT(EPOCH FROM (timestamp - '$ts')) as diff from history
  8439. where device='MyWetter' and reading='temperature' and timestamp >= '$ts'
  8440. union
  8441. select value, EXTRACT(EPOCH FROM ('$ts' - timestamp)) as diff from history
  8442. where device='MyWetter' and reading='temperature' and timestamp < '$ts'
  8443. )
  8444. x order by diff limit 1;";
  8445. } else {
  8446. return ("DbReadingsVal is not implemented for $dbmodel");
  8447. }
  8448. $hash->{LASTCMD} = "dbValue $sql";
  8449. $ret = DbRep_dbValue($name,$sql);
  8450. $ret = $ret?$ret:$default;
  8451. return $ret;
  8452. }
  8453. ####################################################################################################
  8454. # Browser Refresh nach DB-Abfrage
  8455. ####################################################################################################
  8456. sub browser_refresh($) {
  8457. my ($hash) = @_;
  8458. RemoveInternalTimer($hash, "browser_refresh");
  8459. {FW_directNotify("#FHEMWEB:WEB", "location.reload('true')", "")};
  8460. # map { FW_directNotify("#FHEMWEB:$_", "location.reload(true)", "") } devspec2array("WEB.*");
  8461. return;
  8462. }
  8463. ####################################################################################################
  8464. # Test-Sub zu Testzwecken
  8465. ####################################################################################################
  8466. sub testexit ($) {
  8467. my ($hash) = @_;
  8468. my $name = $hash->{NAME};
  8469. if ( !DbRep_Connect($hash) ) {
  8470. Log3 ($name, 2, "DbRep $name - DB connect failed. Database down ? ");
  8471. ReadingsSingleUpdateValue ($hash, "state", "disconnected", 1);
  8472. return;
  8473. } else {
  8474. my $dbh = $hash->{DBH};
  8475. Log3 ($name, 3, "DbRep $name - --------------- FILE INFO --------------");
  8476. my $sqlfile = $dbh->sqlite_db_filename();
  8477. Log3 ($name, 3, "DbRep $name - FILE : $sqlfile ");
  8478. # # $dbh->table_info( $catalog, $schema, $table)
  8479. # my $sth = $dbh->table_info('', '%', '%');
  8480. # my $tables = $dbh->selectcol_arrayref($sth, {Columns => [3]});
  8481. # my $table = join ', ', @$tables;
  8482. # Log3 ($name, 3, "DbRep $name - SQL_TABLES : $table");
  8483. Log3 ($name, 3, "DbRep $name - --------------- PRAGMA --------------");
  8484. my @InfoTypes = ('sqlite_db_status');
  8485. foreach my $row (@InfoTypes) {
  8486. # my @linehash = $dbh->$row;
  8487. my $array= $dbh->$row ;
  8488. # push(@row_array, @array);
  8489. while ((my $key, my $val) = each %{$array}) {
  8490. Log3 ($name, 3, "DbRep $name - PRAGMA : $key : ".%{$val});
  8491. }
  8492. }
  8493. # $sth->finish;
  8494. $dbh->disconnect;
  8495. }
  8496. return;
  8497. }
  8498. 1;
  8499. =pod
  8500. =item helper
  8501. =item summary Reporting & Management content of DbLog-DB's. Content is depicted as readings
  8502. =item summary_DE Reporting & Management von DbLog-DB Content. Darstellung als Readings
  8503. =begin html
  8504. <a name="DbRep"></a>
  8505. <h3>DbRep</h3>
  8506. <ul>
  8507. <br>
  8508. The purpose of this module is browsing and managing the content of DbLog-databases. The searchresults can be evaluated concerning to various aggregations and the appropriate
  8509. Readings will be filled. The data selection will been done by declaration of device, reading and the time settings of selection-begin and selection-end. <br><br>
  8510. Almost all database operations are implemented nonblocking. If there are exceptions it will be suggested to.
  8511. Optional the execution time of SQL-statements in background can also be determined and provided as reading.
  8512. (refer to <a href="#DbRepattr">attributes</a>). <br>
  8513. All existing readings will be deleted when a new operation starts. By attribute "readingPreventFromDel" a comma separated list of readings which are should prevent
  8514. from deletion can be provided. <br><br>
  8515. Currently the following functions are provided: <br><br>
  8516. <ul><ul>
  8517. <li> Selection of all datasets within adjustable time limits. </li>
  8518. <li> Exposure of datasets of a Device/Reading-combination within adjustable time limits. </li>
  8519. <li> Selection of datasets by usage of dynamically calclated time limits at execution time. </li>
  8520. <li> Highlighting doublets when select and display datasets (fetchrows) </li>
  8521. <li> Calculation of quantity of datasets of a Device/Reading-combination within adjustable time limits and several aggregations. </li>
  8522. <li> The calculation of summary-, difference-, maximum-, minimum- and averageValues of numeric readings within adjustable time limits and several aggregations. </li>
  8523. <li> write back results of summary-, difference-, maximum-, minimum- and average calculation into the database </li>
  8524. <li> The deletion of datasets. The containment of deletion can be done by Device and/or Reading as well as fix or dynamically calculated time limits at execution time. </li>
  8525. <li> export of datasets to file (CSV-format). </li>
  8526. <li> import of datasets from file (CSV-Format). </li>
  8527. <li> rename of device/readings in datasets </li>
  8528. <li> change of reading values in the database (changeValue) </li>
  8529. <li> automatic rename of device names in datasets and other DbRep-definitions after FHEM "rename" command (see <a href="#DbRepAutoRename">DbRep-Agent</a>) </li>
  8530. <li> Execution of arbitrary user specific SQL-commands (non-blocking) </li>
  8531. <li> Execution of arbitrary user specific SQL-commands (blocking) for usage in user own code (dbValue) </li>
  8532. <li> creation of backups of the database in running state non-blocking (MySQL, SQLite) </li>
  8533. <li> transfer dumpfiles to a FTP server after backup incl. version control</li>
  8534. <li> restore of SQLite- and MySQL-Dumps non-blocking </li>
  8535. <li> optimize the connected database (optimizeTables, vacuum) </li>
  8536. <li> report of existing database processes (MySQL) </li>
  8537. <li> purge content of current-table </li>
  8538. <li> fill up the current-table with a (tunable) extract of the history-table</li>
  8539. <li> delete consecutive datasets with different timestamp but same values (clearing up consecutive doublets) </li>
  8540. <li> Repair of a corrupted SQLite database ("database disk image is malformed") </li>
  8541. <li> transmission of datasets from source database into another (Standby) database (syncStandby) </li>
  8542. <li> reduce the amount of datasets in database (reduceLog) </li>
  8543. </ul></ul>
  8544. <br>
  8545. To activate the function <b>Autorename</b> the attribute "role" has to be assigned to a defined DbRep-device. The standard role after DbRep definition is "Client".
  8546. Please read more in section <a href="#DbRepAutoRename">DbRep-Agent</a> about autorename function. <br><br>
  8547. DbRep provides a <b>UserExit</b> function. With this interface the user can execute own program code dependent from free
  8548. definable Reading/Value-combinations (Regex). The interface works without respectively independent from event
  8549. generation.
  8550. Further informations you can find as described at <a href="#DbRepattr">attribute</a> "userExitFn".
  8551. <br><br>
  8552. Once a DbRep-Device is defined, the function <b>DbReadingsVal</b> is provided.
  8553. With this function you can, similar to the well known ReadingsVal, get a reading value from database.
  8554. The function execution is carried out blocking.
  8555. The command syntax is: <br><br>
  8556. <ul>
  8557. <code>DbReadingsVal("&lt;name&gt;","&lt;device:reading&gt;","&lt;timestamp&gt;","&lt;default&gt;") </code> <br><br>
  8558. <b>Examples: </b><br>
  8559. $ret = DbReadingsVal("Rep.LogDB1","MyWetter:temperature","2018-01-13 08:00:00",""); <br>
  8560. attr &lt;name&gt; userReadings oldtemp {DbReadingsVal("Rep.LogDB1","MyWetter:temperature","2018-04-13 08:00:00","")}
  8561. <br><br>
  8562. <table>
  8563. <colgroup> <col width=5%> <col width=95%> </colgroup>
  8564. <tr><td> <b>&lt;name&gt;</b> </td><td>: name of the DbRep-Device to request </td></tr>
  8565. <tr><td> <b>&lt;device:reading&gt;</b> </td><td>: device:reading whose value is to deliver </td></tr>
  8566. <tr><td> <b>&lt;timestamp&gt;</b> </td><td>: timestamp of reading whose value is to deliver (*) in the form "YYYY-MM-DD hh:mm:ss" </td></tr>
  8567. <tr><td> <b>&lt;default&gt;</b> </td><td>: default value if no reading value can be retrieved </td></tr>
  8568. </table>
  8569. </ul>
  8570. <br>
  8571. (*) If no value can be retrieved at the &lt;timestamp&gt; exactly requested, the chronological most convenient reading
  8572. value is delivered back.
  8573. <br><br>
  8574. FHEM-Forum: <br>
  8575. <a href="https://forum.fhem.de/index.php/topic,53584.msg452567.html#msg452567">Modul 93_DbRep - Reporting and Management of database content (DbLog)</a>.<br><br>
  8576. <br>
  8577. </ul>
  8578. <b>Preparations </b> <br><br>
  8579. <ul>
  8580. The module requires the usage of a DbLog instance and the credentials of the database definition will be used. <br>
  8581. Only the content of table "history" will be included if isn't other is explained. <br><br>
  8582. Overview which other Perl-modules DbRep is using: <br><br>
  8583. Net::FTP (only if FTP-Transfer after database dump is used) <br>
  8584. Net::FTPSSL (only if FTP-Transfer with encoding after database dump is used) <br>
  8585. POSIX <br>
  8586. Time::HiRes <br>
  8587. Time::Local <br>
  8588. Scalar::Util <br>
  8589. DBI <br>
  8590. Color (FHEM-module) <br>
  8591. IO::Compress::Gzip <br>
  8592. IO::Uncompress::Gunzip <br>
  8593. Blocking (FHEM-module) <br><br>
  8594. Due to performance reason the following index should be created in addition: <br>
  8595. <code>
  8596. CREATE INDEX Report_Idx ON `history` (TIMESTAMP, READING) USING BTREE;
  8597. </code>
  8598. </ul>
  8599. <br>
  8600. <a name="DbRepdefine"></a>
  8601. <b>Definition</b>
  8602. <br>
  8603. <ul>
  8604. <code>
  8605. define &lt;name&gt; DbRep &lt;name of DbLog-instance&gt;
  8606. </code>
  8607. <br><br>
  8608. (&lt;name of DbLog-instance&gt; - name of the database instance which is wanted to analyze needs to be inserted)
  8609. </ul>
  8610. <br><br>
  8611. <a name="DbRepset"></a>
  8612. <b>Set </b>
  8613. <ul>
  8614. Currently following set-commands are included. They are used to trigger the evaluations and define the evaluation option option itself.
  8615. The criteria of searching database content and determine aggregation is carried out by setting several <a href="#DbRepattr">attributes</a>.
  8616. <br><br>
  8617. <ul><ul>
  8618. <li><b> averageValue [display | writeToDB]</b>
  8619. - calculates the average value of database column "VALUE" between period given by
  8620. timestamp-<a href="#DbRepattr">attributes</a> which are set.
  8621. The reading to evaluate must be specified by attribute "reading". <br>
  8622. By attribute "averageCalcForm" the calculation variant for average determination will be configured.
  8623. Is no or the option "display" specified, the results are only displayed. Using
  8624. option "writeToDB" the calculated results are stored in the database with a new reading
  8625. name. <br>
  8626. The new readingname is built of a prefix and the original reading name,
  8627. in which the original reading name can be replaced by the value of attribute "readingNameMap".
  8628. The prefix is made up of the creation function and the aggregation. <br>
  8629. The timestamp of the new stored readings is deviated from aggregation period,
  8630. unless no unique point of time of the result can be determined.
  8631. The field "EVENT" will be filled with "calculated".<br><br>
  8632. <ul>
  8633. <b>Example of building a new reading name from the original reading "totalpac":</b> <br>
  8634. avgam_day_totalpac <br>
  8635. # &lt;creation function&gt;_&lt;aggregation&gt;_&lt;original reading&gt; <br>
  8636. </ul>
  8637. <br>
  8638. Summarized the relevant attributes to control this function are: <br><br>
  8639. <ul>
  8640. <table>
  8641. <colgroup> <col width=5%> <col width=95%> </colgroup>
  8642. <tr><td> <b>averageCalcForm</b> </td><td>: choose the calculation variant for average determination </td></tr>
  8643. <tr><td> <b>device</b> </td><td>: include or exclude &lt;device&gt; from selection </td></tr>
  8644. <tr><td> <b>reading</b> </td><td>: include or exclude &lt;reading&gt; from selection </td></tr>
  8645. <tr><td> <b>time.*</b> </td><td>: a number of attributes to limit selection by time </td></tr>
  8646. <tr><td style="vertical-align:top"> <b>valueFilter</b> <td>: an additional REGEXP to control the record selection. The REGEXP is applied to the database field 'VALUE'. </td></tr>
  8647. </table>
  8648. </ul>
  8649. <br>
  8650. <br>
  8651. </li><br>
  8652. <li><b> cancelDump </b> - stops a running database dump. </li> <br>
  8653. <li><b> changeValue </b> - changes the saved value of readings.
  8654. If the selection is limited to particular device/reading-combinations by
  8655. <a href="#DbRepattr">attribute</a> "device" respectively "reading", it is considered as well
  8656. as possibly defined time limits by time attributes (time.*). <br>
  8657. If no limits are set, the whole database is scanned and the specified value will be
  8658. changed. <br><br>
  8659. <ul>
  8660. <b>Syntax: </b> <br>
  8661. set &lt;name&gt; changeValue "&lt;old string&gt;","&lt;new string&gt;" <br><br>
  8662. The strings have to be quoted and separated by comma.
  8663. A "string" can be: <br>
  8664. <table>
  8665. <colgroup> <col width=15%> <col width=85%> </colgroup>
  8666. <tr><td style="vertical-align:top"><b>&lt;old string&gt; :</b> <td><li>a simple string with/without spaces, e.g. "OL 12" </li>
  8667. <li>a string with usage of SQL-wildcard, e.g. "%OL%" </li> </td></tr>
  8668. <tr><td style="vertical-align:top"><b>&lt;new string&gt; :</b> <td><li>a simple string with/without spaces, e.g. "12 kWh" </li>
  8669. <li>Perl code embedded in "{}" with quotes, e.g. "{($VALUE,$UNIT) = split(" ",$VALUE)}".
  8670. The perl expression the variables $VALUE and $UNIT are committed to.
  8671. The variables are changable within the perl code. The returned value
  8672. of VALUE and UNIT are saved into the database field VALUE respectively
  8673. UNIT of the dataset. </li></td></tr>
  8674. </table>
  8675. <br>
  8676. <b>Examples: </b> <br>
  8677. set &lt;name&gt; changeValue "OL","12 OL" <br>
  8678. # the old field value "OL" is changed to "12 OL". <br><br>
  8679. set &lt;name&gt; changeValue "%OL%","12 OL" <br>
  8680. # contains the field VALUE the substring "OL", it is changed to "12 OL". <br><br>
  8681. set &lt;name&gt; changeValue "12 kWh","{($VALUE,$UNIT) = split(" ",$VALUE)}" <br>
  8682. # the old field value "12 kWh" is splitted to VALUE=12 and UNIT=kWh and saved into the database fields <br><br>
  8683. set &lt;name&gt; changeValue "24%","{$VALUE = (split(" ",$VALUE))[0]}" <br>
  8684. # if the old field value begins with "24", it is splitted and VALUE=24 is saved (e.g. "24 kWh")
  8685. <br><br>
  8686. Summarized the relevant attributes to control function changeValue are: <br><br>
  8687. <ul>
  8688. <table>
  8689. <colgroup> <col width=5%> <col width=95%> </colgroup>
  8690. <tr><td> <b>device</b> </td><td>: include or exclude &lt;device&gt; from selection </td></tr>
  8691. <tr><td> <b>reading</b> </td><td>: include or exclude &lt;reading&gt; from selection </td></tr>
  8692. <tr><td> <b>time.*</b> </td><td>: a number of attributes to limit selection by time </td></tr>
  8693. <tr><td> <b>executeBeforeProc</b> </td><td>: execute a FHEM command (or perl-routine) before start of changeValue </td></tr>
  8694. <tr><td> <b>executeAfterProc</b> </td><td>: execute a FHEM command (or perl-routine) after changeValue is finished </td></tr>
  8695. <tr><td style="vertical-align:top"> <b>valueFilter</b> <td>: an additional REGEXP to control the record selection. The REGEXP is applied to the database field 'VALUE'. </td></tr>
  8696. </table>
  8697. </ul>
  8698. <br>
  8699. <br>
  8700. <b>Note:</b> <br>
  8701. Even though the function itself is designed non-blocking, make sure the assigned DbLog-device
  8702. is operating in asynchronous mode to avoid FHEMWEB from blocking. <br><br>
  8703. </li> <br>
  8704. </ul>
  8705. <li><b> countEntries [history|current] </b> - provides the number of table entries (default: history) between time period set
  8706. by time.* -<a href="#DbRepattr">attributes</a> if set.
  8707. If time.* attributes not set, all entries of the table will be count.
  8708. The <a href="#DbRepattr">attributes</a> "device" and "reading" can be used to
  8709. limit the evaluation. <br>
  8710. By default the summary of all counted datasets, labeled by "ALLREADINGS", will be created.
  8711. If the attribute "countEntriesDetail" is set, the number of every reading
  8712. is reported additionally. <br><br>
  8713. The relevant attributes for this function are: <br><br>
  8714. <ul>
  8715. <table>
  8716. <colgroup> <col width=5%> <col width=95%> </colgroup>
  8717. <tr><td> <b>aggregation</b> </td><td>: aggregatiion/grouping of time intervals </td></tr>
  8718. <tr><td> <b>countEntriesDetail</b> </td><td>: detailed report the count of datasets (per reading) </td></tr>
  8719. <tr><td> <b>device</b> </td><td>: include or exclude &lt;device&gt; from selection </td></tr>
  8720. <tr><td> <b>reading</b> </td><td>: include or exclude &lt;reading&gt; from selection </td></tr>
  8721. <tr><td> <b>time.*</b> </td><td>: a number of attributes to limit selection by time </td></tr>
  8722. <tr><td style="vertical-align:top"> <b>valueFilter</b> <td>: an additional REGEXP to control the record selection. The REGEXP is applied to the database field 'VALUE'. </td></tr>
  8723. </table>
  8724. </ul>
  8725. <br>
  8726. </li> <br>
  8727. <li><b> delEntries </b> - deletes all database entries or only the database entries specified by <a href="#DbRepattr">attributes</a> Device and/or
  8728. Reading and the entered time period between "timestamp_begin", "timestamp_end" (if set) or "timeDiffToNow/timeOlderThan". <br><br>
  8729. <ul>
  8730. "timestamp_begin" is set <b>-&gt;</b> deletes db entries <b>from</b> this timestamp until current date/time <br>
  8731. "timestamp_end" is set <b>-&gt;</b> deletes db entries <b>until</b> this timestamp <br>
  8732. both Timestamps are set <b>-&gt;</b> deletes db entries <b>between</b> these timestamps <br>
  8733. "timeOlderThan" is set <b>-&gt;</b> delete entries <b>older</b> than current time minus "timeOlderThan" <br>
  8734. "timeDiffToNow" is set <b>-&gt;</b> delete db entries <b>from</b> current time minus "timeDiffToNow" until now <br>
  8735. <br>
  8736. Due to security reasons the attribute <a href="#DbRepattr">attribute</a> "allowDeletion" needs to be set to unlock the
  8737. delete-function. <br>
  8738. The relevant attributes to control function changeValue delEntries are: <br><br>
  8739. <ul>
  8740. <table>
  8741. <colgroup> <col width=5%> <col width=95%> </colgroup>
  8742. <tr><td> <b>allowDeletion</b> </td><td>: unlock the delete function </td></tr>
  8743. <tr><td> <b>device</b> </td><td>: include or exclude &lt;device&gt; from selection </td></tr>
  8744. <tr><td> <b>reading</b> </td><td>: include or exclude &lt;reading&gt; from selection </td></tr>
  8745. <tr><td> <b>time.*</b> </td><td>: a number of attributes to limit selection by time </td></tr>
  8746. <tr><td> <b>executeBeforeProc</b> </td><td>: execute a FHEM command (or perl-routine) before start of delEntries </td></tr>
  8747. <tr><td> <b>executeAfterProc</b> </td><td>: execute a FHEM command (or perl-routine) after delEntries is finished </td></tr>
  8748. </table>
  8749. </ul>
  8750. <br>
  8751. <br>
  8752. </li>
  8753. <br>
  8754. </ul>
  8755. <li><b> delSeqDoublets [adviceRemain | adviceDelete | delete]</b> - show respectively delete identical sequentially datasets.
  8756. Therefore Device,Reading and Value of the sequentially datasets are compared.
  8757. Not deleted are the first und the last dataset of a aggregation period (e.g. hour,day,week and so on) as
  8758. well as the datasets before or after a value change (database field VALUE). <br>
  8759. The <a href="#DbRepattr">attributes</a> to define the scope of aggregation,time period, device and reading are
  8760. considered. If attribute aggregation is not set or set to "no", it will change to the default aggregation
  8761. period "day". For datasets containing numerical values it is possible to determine a variance with <a href="#DbRepattr">attribute</a>
  8762. "seqDoubletsVariance". Up to this value consecutive numerical datasets are handled as identical and should be
  8763. deleted.
  8764. <br><br>
  8765. <ul>
  8766. <table>
  8767. <colgroup> <col width=5%> <col width=95%> </colgroup>
  8768. <tr><td> <b>adviceRemain</b> </td><td>: simulates the remaining datasets in database after delete-operation (nothing will be deleted !) </td></tr>
  8769. <tr><td> <b>adviceDelete</b> </td><td>: simulates the datasets to delete in database (nothing will be deleted !) </td></tr>
  8770. <tr><td> <b>delete</b> </td><td>: deletes the consecutive doublets (see example) </td></tr>
  8771. </table>
  8772. </ul>
  8773. <br>
  8774. Due to security reasons the attribute <a href="#DbRepattr">attribute</a> "allowDeletion" needs to be set for
  8775. execute the "delete" option. <br>
  8776. The amount of datasets to show by commands "delSeqDoublets adviceDelete", "delSeqDoublets adviceRemain" is
  8777. initially limited (default: 1000) and can be adjusted by <a href="#DbRepattr">attribute</a> "limit".
  8778. The adjustment of "limit" has no impact to the "delSeqDoublets delete" function, but affects <b>ONLY</b> the
  8779. display of the data. <br>
  8780. Before and after this "delSeqDoublets" it is possible to execute a FHEM command or Perl-script
  8781. (please see <a href="#DbRepattr">attributes</a> "executeBeforeProc" and "executeAfterProc").
  8782. <br><br>
  8783. <ul>
  8784. <b>Example</b> - the remaining datasets after executing delete-option are are marked as <b>bold</b>: <br><br>
  8785. <ul>
  8786. <b>2017-11-25_00-00-05__eg.az.fridge_Pwr__power 0 </b> <br>
  8787. 2017-11-25_00-02-26__eg.az.fridge_Pwr__power 0 <br>
  8788. 2017-11-25_00-04-33__eg.az.fridge_Pwr__power 0 <br>
  8789. 2017-11-25_01-06-10__eg.az.fridge_Pwr__power 0 <br>
  8790. <b>2017-11-25_01-08-21__eg.az.fridge_Pwr__power 0 </b> <br>
  8791. <b>2017-11-25_01-08-59__eg.az.fridge_Pwr__power 60.32 </b> <br>
  8792. <b>2017-11-25_01-11-21__eg.az.fridge_Pwr__power 56.26 </b> <br>
  8793. <b>2017-11-25_01-27-54__eg.az.fridge_Pwr__power 6.19 </b> <br>
  8794. <b>2017-11-25_01-28-51__eg.az.fridge_Pwr__power 0 </b> <br>
  8795. 2017-11-25_01-31-00__eg.az.fridge_Pwr__power 0 <br>
  8796. 2017-11-25_01-33-59__eg.az.fridge_Pwr__power 0 <br>
  8797. <b>2017-11-25_02-39-29__eg.az.fridge_Pwr__power 0 </b> <br>
  8798. <b>2017-11-25_02-41-18__eg.az.fridge_Pwr__power 105.28</b> <br>
  8799. <b>2017-11-25_02-41-26__eg.az.fridge_Pwr__power 61.52 </b> <br>
  8800. <b>2017-11-25_03-00-06__eg.az.fridge_Pwr__power 47.46 </b> <br>
  8801. <b>2017-11-25_03-00-33__eg.az.fridge_Pwr__power 0 </b> <br>
  8802. 2017-11-25_03-02-07__eg.az.fridge_Pwr__power 0 <br>
  8803. 2017-11-25_23-37-42__eg.az.fridge_Pwr__power 0 <br>
  8804. <b>2017-11-25_23-40-10__eg.az.fridge_Pwr__power 0 </b> <br>
  8805. <b>2017-11-25_23-42-24__eg.az.fridge_Pwr__power 1 </b> <br>
  8806. 2017-11-25_23-42-24__eg.az.fridge_Pwr__power 1 <br>
  8807. <b>2017-11-25_23-45-27__eg.az.fridge_Pwr__power 1 </b> <br>
  8808. <b>2017-11-25_23-47-07__eg.az.fridge_Pwr__power 0 </b> <br>
  8809. 2017-11-25_23-55-27__eg.az.fridge_Pwr__power 0 <br>
  8810. <b>2017-11-25_23-48-15__eg.az.fridge_Pwr__power 0 </b> <br>
  8811. <b>2017-11-25_23-50-21__eg.az.fridge_Pwr__power 59.1 </b> <br>
  8812. <b>2017-11-25_23-55-14__eg.az.fridge_Pwr__power 52.31 </b> <br>
  8813. <b>2017-11-25_23-58-09__eg.az.fridge_Pwr__power 51.73 </b> <br>
  8814. </ul>
  8815. </ul>
  8816. <br>
  8817. Summarized the relevant attributes to control this function are: <br><br>
  8818. <ul>
  8819. <table>
  8820. <colgroup> <col width=5%> <col width=95%> </colgroup>
  8821. <tr><td> <b>allowDeletion</b> </td><td>: needs to be set to execute the delete option </td></tr>
  8822. <tr><td> <b>aggregation</b> </td><td>: choose the aggregation period </td></tr>
  8823. <tr><td> <b>limit</b> </td><td>: limits ONLY the count of datasets to display </td></tr>
  8824. <tr><td> <b>device</b> </td><td>: include or exclude &lt;device&gt; from selection </td></tr>
  8825. <tr><td> <b>reading</b> </td><td>: include or exclude &lt;reading&gt; from selection </td></tr>
  8826. <tr><td> <b>executeBeforeProc</b> </td><td>: execute a FHEM command (or perl-routine) before start of delEntries </td></tr>
  8827. <tr><td> <b>executeAfterProc</b> </td><td>: execute a FHEM command (or perl-routine) after delEntries is finished </td></tr>
  8828. <tr><td> <b>seqDoubletsVariance</b> </td><td>: Up to this value consecutive numerical datasets are handled as identical and should be deleted </td></tr>
  8829. <tr><td> <b>time.*</b> </td><td>: a number of attributes to limit selection by time </td></tr>
  8830. <tr><td style="vertical-align:top"> <b>valueFilter</b> <td>: an additional REGEXP to control the record selection. The REGEXP is applied to the database field 'VALUE'. </td></tr>
  8831. </table>
  8832. </ul>
  8833. </li>
  8834. <br>
  8835. <br>
  8836. <li><b> deviceRename &lt;old_name&gt;,&lt;new_name&gt;</b> -
  8837. renames the device name of a device inside the connected database (Internal DATABASE).
  8838. The devicename will allways be changed in the <b>entire</b> database. Possibly set time limits or restrictions by
  8839. <a href="#DbRepattr">attributes</a> device and/or reading will not be considered. <br><br>
  8840. <ul>
  8841. <b>Example: </b> <br>
  8842. set &lt;name&gt; deviceRename ST_5000,ST5100 <br>
  8843. # The amount of renamed device names (datasets) will be displayed in reading "device_renamed". <br>
  8844. # If the device name to be renamed was not found in the database, a WARNUNG will appear in reading "device_not_renamed". <br>
  8845. # Appropriate entries will be written to Logfile if verbose >= 3 is set.
  8846. <br><br>
  8847. <b>Note:</b> <br>
  8848. Even though the function itself is designed non-blocking, make sure the assigned DbLog-device
  8849. is operating in asynchronous mode to avoid FHEMWEB from blocking. <br><br>
  8850. </li> <br>
  8851. </ul>
  8852. <li><b> diffValue [display | writeToDB]</b>
  8853. - calculates the difference of database column "VALUE" between period given by
  8854. <a href="#DbRepattr">attributes</a> "timestamp_begin", "timestamp_end" or "timeDiffToNow / timeOlderThan".
  8855. The reading to evaluate must be defined using attribute "reading".
  8856. This function is mostly reasonable if readingvalues are increasing permanently and don't write value-differences to the database.
  8857. The difference will be generated from the first available dataset (VALUE-Field) to the last available dataset between the
  8858. specified time linits/aggregation, in which a balanced difference value of the previous aggregation period will be transfered to the
  8859. following aggregation period in case this period contains a value. <br>
  8860. A possible counter overrun (restart with value "0") will be considered (compare <a href="#DbRepattr">attribute</a> "diffAccept"). <br><br>
  8861. If only one dataset will be found within the evalution period, the difference can be calculated only in combination with the balanced
  8862. difference of the previous aggregation period. In this case a logical inaccuracy according the assignment of the difference to the particular aggregation period
  8863. can be possible. Hence in warning in "state" will be placed and the reading "less_data_in_period" with a list of periods
  8864. with only one dataset found in it will be created.
  8865. <br><br>
  8866. <ul>
  8867. <b>Note: </b><br>
  8868. Within the evaluation respectively aggregation period (day, week, month, etc.) you should make available at least one dataset
  8869. at the beginning and one dataset at the end of each aggregation period to take the difference calculation as much as possible.
  8870. <br>
  8871. <br>
  8872. </ul>
  8873. Is no or the option "display" specified, the results are only displayed. Using
  8874. option "writeToDB" the calculation results are stored in the database with a new reading
  8875. name. <br>
  8876. The new readingname is built of a prefix and the original reading name,
  8877. in which the original reading name can be replaced by the value of attribute "readingNameMap".
  8878. The prefix is made up of the creation function and the aggregation. <br>
  8879. The timestamp of the new stored readings is deviated from aggregation period,
  8880. unless no unique point of time of the result can be determined.
  8881. The field "EVENT" will be filled with "calculated".<br><br>
  8882. <ul>
  8883. <b>Example of building a new reading name from the original reading "totalpac":</b> <br>
  8884. diff_day_totalpac <br>
  8885. # &lt;creation function&gt;_&lt;aggregation&gt;_&lt;original reading&gt; <br>
  8886. </ul>
  8887. <br>
  8888. Summarized the relevant attributes to control this function are: <br><br>
  8889. <ul>
  8890. <table>
  8891. <colgroup> <col width=5%> <col width=95%> </colgroup>
  8892. <tr><td> <b>aggregation</b> </td><td>: choose the aggregation period </td></tr>
  8893. <tr><td> <b>diffAccept</b> </td><td>: the maximum accepted difference between sequential records </td></tr>
  8894. <tr><td> <b>device</b> </td><td>: include or exclude &lt;device&gt; from selection </td></tr>
  8895. <tr><td> <b>reading</b> </td><td>: include or exclude &lt;reading&gt; from selection </td></tr>
  8896. <tr><td> <b>readingNameMap</b> </td><td>: rename the resulted reading name </td></tr>
  8897. <tr><td> <b>time.*</b> </td><td>: a number of attributes to limit selection by time </td></tr>
  8898. <tr><td style="vertical-align:top"> <b>valueFilter</b> <td>: an additional REGEXP to control the record selection. The REGEXP is applied to the database field 'VALUE'. </td></tr>
  8899. </table>
  8900. </ul>
  8901. <br>
  8902. <br>
  8903. </li><br>
  8904. <li><b> dumpMySQL [clientSide | serverSide]</b>
  8905. - creates a dump of the connected MySQL database. <br>
  8906. Depending from selected option the dump will be created on Client- or on Server-Side. <br>
  8907. The variants differs each other concerning the executing system, the creating location, the usage of
  8908. attributes, the function result and the needed hardware ressources. <br>
  8909. The option "clientSide" e.g. needs more powerful FHEM-Server hardware, but saves all available
  8910. tables inclusive possibly created views. <br>
  8911. With attribute "dumpCompress" a compression of dump file after creation can be switched on.
  8912. <br><br>
  8913. <ul>
  8914. <b><u>Option clientSide</u></b> <br>
  8915. The dump will be created by client (FHEM-Server) and will be saved in FHEM log-directory by
  8916. default.
  8917. The target directory can be set by <a href="#DbRepattr">attribute</a> "dumpDirLocal" and has to be
  8918. writable by the FHEM process. <br>
  8919. Before executing the dump a table optimization can be processed optionally (see attribute
  8920. "optimizeTablesBeforeDump") as well as a FHEM-command (attribute "executeBeforeProc").
  8921. After the dump a FHEM-command can be executed as well (see attribute "executeAfterProc"). <br><br>
  8922. <b>Note: <br>
  8923. To avoid FHEM from blocking, you have to operate DbLog in asynchronous mode if the table
  8924. optimization want to be used ! </b> <br><br>
  8925. By the <a href="#DbRepattr">attributes</a> "dumpMemlimit" and "dumpSpeed" the run-time behavior of the function can be
  8926. controlled to optimize the performance and demand of ressources. <br><br>
  8927. The attributes relevant for function "dumpMySQL clientSide" are: <br><br>
  8928. <ul>
  8929. <table>
  8930. <colgroup> <col width=5%> <col width=95%> </colgroup>
  8931. <tr><td> dumpComment </td><td>: User comment in head of dump file </td></tr>
  8932. <tr><td> dumpCompress </td><td>: compress of dump files after creation </td></tr>
  8933. <tr><td> dumpDirLocal </td><td>: the local destination directory for dump file creation </td></tr>
  8934. <tr><td> dumpMemlimit </td><td>: limits memory usage </td></tr>
  8935. <tr><td> dumpSpeed </td><td>: limits CPU utilization </td></tr>
  8936. <tr><td> dumpFilesKeep </td><td>: number of dump files to keep </td></tr>
  8937. <tr><td> executeBeforeProc </td><td>: execution of FHEM command (or perl-routine) before dump </td></tr>
  8938. <tr><td> executeAfterProc </td><td>: execution of FHEM command (or perl-routine) after dump </td></tr>
  8939. <tr><td> optimizeTablesBeforeDump </td><td>: table optimization before dump </td></tr>
  8940. </table>
  8941. </ul>
  8942. <br>
  8943. After a successfull finished dump the old dumpfiles are deleted and only the number of files
  8944. defined by attribute "dumpFilesKeep" (default: 3) remain in the target directory
  8945. "dumpDirLocal". If "dumpFilesKeep = 0" is set, all
  8946. dumpfiles (also the current created file), are deleted. This setting can be helpful, if FTP transmission is used
  8947. and the created dumps are only keep remain in the FTP destination directory. <br><br>
  8948. The <b>naming convention of dump files</b> is: &lt;dbname&gt;_&lt;date&gt;_&lt;time&gt;.sql[.gzip] <br><br>
  8949. To rebuild the database from a dump file the command: <br><br>
  8950. <ul>
  8951. set &lt;name&gt; restoreMySQL &lt;filename&gt; <br><br>
  8952. </ul>
  8953. can be used. <br><br>
  8954. The created dumpfile (uncompressed) can imported on the MySQL-Server by: <br><br>
  8955. <ul>
  8956. mysql -u &lt;user&gt; -p &lt;dbname&gt; < &lt;filename&gt;.sql <br><br>
  8957. </ul>
  8958. as well to restore the database from dump file. <br><br><br>
  8959. <b><u>Option serverSide</u></b> <br>
  8960. The dump will be created on the MySQL-Server and will be saved in its Home-directory
  8961. by default. <br>
  8962. The whole history-table (not the current-table) will be exported <b>CSV-formatted</b> without
  8963. any restrictions. <br>
  8964. Before executing the dump a table optimization can be processed optionally (see attribute
  8965. "optimizeTablesBeforeDump") as well as a FHEM-command (attribute "executeBeforeProc"). <br><br>
  8966. <b>Note: <br>
  8967. To avoid FHEM from blocking, you have to operate DbLog in asynchronous mode if the table
  8968. optimization want to be used ! </b> <br><br>
  8969. After the dump a FHEM-command can be executed as well (see attribute "executeAfterProc"). <br><br>
  8970. The attributes relevant for function "dumpMySQL serverSide" are: <br><br>
  8971. <ul>
  8972. <table>
  8973. <colgroup> <col width=5%> <col width=95%> </colgroup>
  8974. <tr><td> dumpDirRemote </td><td>: destination directory of dump file on remote server </td></tr>
  8975. <tr><td> dumpCompress </td><td>: compress of dump files after creation </td></tr>
  8976. <tr><td> dumpDirLocal </td><td>: the local mounted directory dumpDirRemote </td></tr>
  8977. <tr><td> dumpFilesKeep </td><td>: number of dump files to keep </td></tr>
  8978. <tr><td> executeBeforeProc </td><td>: execution of FHEM command (or perl-routine) before dump </td></tr>
  8979. <tr><td> executeAfterProc </td><td>: execution of FHEM command (or perl-routine) after dump </td></tr>
  8980. <tr><td> optimizeTablesBeforeDump </td><td>: table optimization before dump </td></tr>
  8981. </table>
  8982. </ul>
  8983. <br>
  8984. The target directory can be set by <a href="#DbRepattr">attribute</a> "dumpDirRemote".
  8985. It must be located on the MySQL-Host and has to be writable by the MySQL-server process. <br>
  8986. The used database user must have the "FILE"-privilege. <br><br>
  8987. <b>Note:</b> <br>
  8988. If the internal version management of DbRep should be used and the size of the created dumpfile be
  8989. reported, you have to mount the remote MySQL-Server directory "dumpDirRemote" on the client
  8990. and publish it to the DbRep-device by fill out the <a href="#DbRepattr">attribute</a>
  8991. "dumpDirLocal". <br>
  8992. Same is necessary if ftp transfer after dump is to be used (attribute "ftpUse" respectively "ftpUseSSL").
  8993. <br><br>
  8994. <ul>
  8995. <b>Example: </b> <br>
  8996. attr &lt;name&gt; dumpDirRemote /volume1/ApplicationBackup/dumps_FHEM/ <br>
  8997. attr &lt;name&gt; dumpDirLocal /sds1/backup/dumps_FHEM/ <br>
  8998. attr &lt;name&gt; dumpFilesKeep 2 <br><br>
  8999. # The dump will be created remote on the MySQL-Server in directory
  9000. '/volume1/ApplicationBackup/dumps_FHEM/'. <br>
  9001. # The internal version management searches in local mounted directory '/sds1/backup/dumps_FHEM/'
  9002. for present dumpfiles and deletes these files except the last two versions. <br>
  9003. <br>
  9004. </ul>
  9005. If the internal version management is used, after a successfull finished dump old dumpfiles will
  9006. be deleted and only the number of attribute "dumpFilesKeep" (default: 3) would remain in target
  9007. directory "dumpDirLocal" (the mounted "dumpDirRemote").
  9008. In that case FHEM needs write permissions to the directory "dumpDirLocal". <br><br>
  9009. The <b>naming convention of dump files</b> is: &lt;dbname&gt;_&lt;date&gt;_&lt;time&gt;.csv[.gzip] <br><br>
  9010. You can start a restore of table history from serverSide-Backup by command: <br><br>
  9011. <ul>
  9012. set &lt;name&gt; &lt;restoreMySQL&gt; &lt;filename&gt;.csv[.gzip] <br><br>
  9013. </ul>
  9014. <br><br>
  9015. <b><u>FTP-Transfer after Dump</u></b> <br>
  9016. If those possibility is be used, the <a href="#DbRepattr">attribute</a> "ftpUse" or
  9017. "ftpUseSSL" has to be set. The latter if encoding for FTP is to be used.
  9018. The module also carries the version control of dump files in FTP-destination by attribute
  9019. "ftpDumpFilesKeep". <br>
  9020. Further <a href="#DbRepattr">attributes</a> are: <br><br>
  9021. <ul>
  9022. <table>
  9023. <colgroup> <col width=5%> <col width=95%> </colgroup>
  9024. <tr><td> ftpUse </td><td>: FTP Transfer after dump will be switched on (without SSL encoding) </td></tr>
  9025. <tr><td> ftpUser </td><td>: User for FTP-server login, default: anonymous </td></tr>
  9026. <tr><td> ftpUseSSL </td><td>: FTP Transfer with SSL encoding after dump </td></tr>
  9027. <tr><td> ftpDebug </td><td>: debugging of FTP communication for diagnostics </td></tr>
  9028. <tr><td> ftpDir </td><td>: directory on FTP-server in which the file will be send into (default: "/") </td></tr>
  9029. <tr><td> ftpDumpFilesKeep </td><td>: leave the number of dump files in FTP-destination &lt;ftpDir&gt; (default: 3) </td></tr>
  9030. <tr><td> ftpPassive </td><td>: set if passive FTP is to be used </td></tr>
  9031. <tr><td> ftpPort </td><td>: FTP-Port, default: 21 </td></tr>
  9032. <tr><td> ftpPwd </td><td>: password of FTP-User, not set by default </td></tr>
  9033. <tr><td> ftpServer </td><td>: name or IP-address of FTP-server. <b>absolutely essential !</b> </td></tr>
  9034. <tr><td> ftpTimeout </td><td>: timeout of FTP-connection in seconds (default: 30). </td></tr>
  9035. </table>
  9036. </ul>
  9037. <br>
  9038. <br>
  9039. </ul>
  9040. </li><br>
  9041. <li><b> dumpSQLite </b> - creates a dump of the connected SQLite database. <br>
  9042. This function uses the SQLite Online Backup API and allow to create a consistent backup of the
  9043. database during the normal operation.
  9044. The dump will be saved in FHEM log-directory by default.
  9045. The target directory can be defined by <a href="#DbRepattr">attribute</a> "dumpDirLocal" and
  9046. has to be writable by the FHEM process. <br>
  9047. Before executing the dump a table optimization can be processed optionally (see attribute
  9048. "optimizeTablesBeforeDump").
  9049. <br><br>
  9050. <b>Note: <br>
  9051. To avoid FHEM from blocking, you have to operate DbLog in asynchronous mode if the table
  9052. optimization want to be used ! </b> <br><br>
  9053. Before and after the dump a FHEM-command can be executed (see attribute "executeBeforeProc",
  9054. "executeAfterProc"). <br><br>
  9055. The attributes relevant for function "dumpMySQL serverSide" are: <br><br>
  9056. <ul>
  9057. <table>
  9058. <colgroup> <col width=5%> <col width=95%> </colgroup>
  9059. <tr><td> dumpCompress </td><td>: compress of dump files after creation </td></tr>
  9060. <tr><td> dumpDirLocal </td><td>: the local mounted directory dumpDirRemote </td></tr>
  9061. <tr><td> dumpFilesKeep </td><td>: number of dump files to keep </td></tr>
  9062. <tr><td> executeBeforeProc </td><td>: execution of FHEM command (or perl-routine) before dump </td></tr>
  9063. <tr><td> executeAfterProc </td><td>: execution of FHEM command (or perl-routine) after dump </td></tr>
  9064. <tr><td> optimizeTablesBeforeDump </td><td>: table optimization before dump </td></tr>
  9065. </table>
  9066. </ul>
  9067. <br>
  9068. After a successfull finished dump the old dumpfiles are deleted and only the number of attribute
  9069. "dumpFilesKeep" (default: 3) remain in the target directory "dumpDirLocal". If "dumpFilesKeep = 0" is set, all
  9070. dumpfiles (also the current created file), are deleted. This setting can be helpful, if FTP transmission is used
  9071. and the created dumps are only keep remain in the FTP destination directory. <br><br>
  9072. The <b>naming convention of dump files</b> is: &lt;dbname&gt;_&lt;date&gt;_&lt;time&gt;.sqlitebkp[.gzip] <br><br>
  9073. The database can be restored by command "set &lt;name&gt; restoreSQLite &lt;filename&gt;" <br>
  9074. The created dump file can be transfered to a FTP-server. Please see explanations about FTP-
  9075. transfer in topic "dumpMySQL". <br><br>
  9076. </li><br>
  9077. <li><b> eraseReadings </b> - deletes all created readings in the device, except reading "state" and readings, which are
  9078. contained in exception list defined by attribute "readingPreventFromDel".
  9079. </li><br>
  9080. <li><b> exportToFile [&lt;file&gt;] </b>
  9081. - exports DB-entries to a file in CSV-format of time period specified by time attributes. <br>
  9082. Limitation of selections can be done by <a href="#DbRepattr">attributes</a> device and/or
  9083. reading.
  9084. The filename can be defined by <a href="#DbRepattr">attribute</a> "expimpfile". <br>
  9085. Optionally a file can be specified as a command option (/path/file) and overloads a possibly
  9086. defined attribute "expimpfile". The filename may contain wildcards as described
  9087. in attribute section of "expimpfile".
  9088. <br>
  9089. By setting attribute "aggregation" the export of datasets will be splitted into time slices
  9090. corresponding to the specified aggregation.
  9091. If, for example, "aggregation = month" is set, the data are selected in monthly packets and written
  9092. into the exportfile. Thereby the usage of main memory is optimized if very large amount of data
  9093. is exported and avoid the "died prematurely" error. <br><br>
  9094. The attributes relevant for this function are: <br><br>
  9095. <ul>
  9096. <table>
  9097. <colgroup> <col width=5%> <col width=95%> </colgroup>
  9098. <tr><td> <b>aggregation</b> </td><td>: determination of selection time slices </td></tr>
  9099. <tr><td> <b>device</b> </td><td>: include or exclude &lt;device&gt; from selection </td></tr>
  9100. <tr><td> <b>reading</b> </td><td>: include or exclude &lt;reading&gt; from selection </td></tr> <tr><td> <b>time.*</b> </td><td>: a number of attributes to limit selection by time </td></tr>
  9101. <tr><td> <b>executeBeforeProc</b> </td><td>: execution of FHEM command (or perl-routine) before export </td></tr>
  9102. <tr><td> <b>executeAfterProc</b> </td><td>: execution of FHEM command (or perl-routine) after export </td></tr>
  9103. <tr><td> <b>expimpfile</b> </td><td>: the name of exportfile </td></tr>
  9104. <tr><td> <b>time.*</b> </td><td>: a number of attributes to limit selection by time </td></tr>
  9105. <tr><td style="vertical-align:top"> <b>valueFilter</b> <td>: an additional REGEXP to control the record selection. The REGEXP is applied to the database field 'VALUE'. </td></tr>
  9106. </table>
  9107. </ul>
  9108. </li> <br>
  9109. <li><b> fetchrows [history|current] </b>
  9110. - provides <b>all</b> table entries (default: history)
  9111. of time period set by time <a href="#DbRepattr">attributes</a> respectively selection conditions
  9112. by attributes "device" and "reading".
  9113. An aggregation set will <b>not</b> be considered. <br>
  9114. The direction of data selection can be determined by <a href="#DbRepattr">attribute</a>
  9115. "fetchRoute". <br><br>
  9116. Every reading of result is composed of the dataset timestring , an index, the device name
  9117. and the reading name.
  9118. The function has the capability to reconize multiple occuring datasets (doublets).
  9119. Such doublets are marked by an index > 1. <br>
  9120. Doublets can be highlighted in terms of color by setting attribut e"fetchMarkDuplicates". <br><br>
  9121. <b>Note:</b> <br>
  9122. Highlighted readings are not displayed again after restart or rereadcfg because of they are not
  9123. saved in statefile. <br><br>
  9124. This attribute is preallocated with some colors, but can be changed by colorpicker-widget: <br><br>
  9125. <ul>
  9126. <code>
  9127. attr &lt;DbRep-Device&gt; widgetOverride fetchMarkDuplicates:colorpicker
  9128. </code>
  9129. </ul>
  9130. <br>
  9131. The readings of result are composed like the following sceme: <br><br>
  9132. <ul>
  9133. <b>Example:</b> <br>
  9134. 2017-10-22_03-04-43__1__SMA_Energymeter__Bezug_WirkP_Kosten_Diff <br>
  9135. # &lt;date&gt;_&lt;time&gt;__&lt;index&gt;__&lt;device&gt;__&lt;reading&gt;
  9136. </ul>
  9137. <br>
  9138. For a better overview the relevant attributes are listed here in a table: <br><br>
  9139. <ul>
  9140. <table>
  9141. <colgroup> <col width=5%> <col width=95%> </colgroup>
  9142. <tr><td> <b>fetchRoute</b> </td><td>: direction of selection read in database </td></tr>
  9143. <tr><td> <b>limit</b> </td><td>: limits the number of datasets to select and display </td></tr>
  9144. <tr><td> <b>fetchMarkDuplicates</b> </td><td>: Highlighting of found doublets </td></tr>
  9145. <tr><td> <b>device</b> </td><td>: include or exclude &lt;device&gt; from selection </td></tr>
  9146. <tr><td> <b>reading</b> </td><td>: include or exclude &lt;reading&gt; from selection </td></tr>
  9147. <tr><td> <b>time.*</b> </td><td>: A number of attributes to limit selection by time </td></tr>
  9148. <tr><td style="vertical-align:top"> <b>valueFilter</b> <td>: an additional REGEXP to control the record selection. The REGEXP is applied to the database field 'VALUE'. </td></tr>
  9149. </table>
  9150. </ul>
  9151. <br>
  9152. <br>
  9153. <b>Note:</b> <br>
  9154. Although the module is designed non-blocking, a huge number of selection result (huge number of rows)
  9155. can overwhelm the browser session respectively FHEMWEB.
  9156. Due to the sample space can be limited by <a href="#limit">attribute</a> "limit".
  9157. Of course ths attribute can be increased if your system capabilities allow a higher workload. <br><br>
  9158. </li> <br>
  9159. <li><b> insert </b> - use it to insert data ito table "history" manually. Input values for Date, Time and Value are mandatory. The database fields for Type and Event will be filled in with "manual" automatically and the values of Device, Reading will be get from set <a href="#DbRepattr">attributes</a>. <br><br>
  9160. <ul>
  9161. <b>input format: </b> Date,Time,Value,[Unit] <br>
  9162. # Unit is optional, attributes of device, reading must be set ! <br>
  9163. # If "Value=0" has to be inserted, use "Value = 0.0" to do it. <br><br>
  9164. <b>example:</b> 2016-08-01,23:00:09,TestValue,TestUnit <br>
  9165. # Spaces are NOT allowed in fieldvalues ! <br>
  9166. <br>
  9167. <b>Note: </b><br>
  9168. Please consider to insert AT LEAST two datasets into the intended time / aggregatiom period (day, week, month, etc.) because of
  9169. it's needed by function diffValue. Otherwise no difference can be calculated and diffValue will be print out "0" for the respective period !
  9170. <br>
  9171. <br>
  9172. </li>
  9173. </ul>
  9174. <li><b> importFromFile [&lt;file&gt;] </b>
  9175. - imports data in CSV format from file into database. <br>
  9176. The filename can be defined by <a href="#DbRepattr">attribute</a> "expimpfile". <br>
  9177. Optionally a file can be specified as a command option (/path/file) and overloads a possibly
  9178. defined attribute "expimpfile". The filename may contain wildcards as described
  9179. in attribute section of "expimpfile". <br><br>
  9180. <ul>
  9181. <b>dataset format: </b> <br>
  9182. "TIMESTAMP","DEVICE","TYPE","EVENT","READING","VALUE","UNIT" <br><br>
  9183. # The fields "TIMESTAMP","DEVICE","TYPE","EVENT","READING" and "VALUE" have to be set. The field "UNIT" is optional.
  9184. The file content will be imported transactional. That means all of the content will be imported or, in case of error, nothing of it.
  9185. If an extensive file will be used, DON'T set verbose = 5 because of a lot of datas would be written to the logfile in this case.
  9186. It could lead to blocking or overload FHEM ! <br><br>
  9187. <b>Example for a source dataset: </b> <br>
  9188. "2016-09-25 08:53:56","STP_5000","SMAUTILS","etotal: 11859.573","etotal","11859.573","" <br>
  9189. <br>
  9190. The attributes relevant for this function are: <br><br>
  9191. <ul>
  9192. <table>
  9193. <colgroup> <col width=5%> <col width=95%> </colgroup>
  9194. <tr><td> <b>executeBeforeProc</b> </td><td>: execution of FHEM command (or perl-routine) before import </td></tr>
  9195. <tr><td> <b>executeAfterProc</b> </td><td>: execution of FHEM command (or perl-routine) after import </td></tr>
  9196. <tr><td> <b>expimpfile</b> </td><td>: the name of exportfile </td></tr>
  9197. </table>
  9198. </ul>
  9199. </li> <br>
  9200. </ul>
  9201. <br>
  9202. <li><b> maxValue [display | writeToDB]</b>
  9203. - calculates the maximum value of database column "VALUE" between period given by
  9204. <a href="#DbRepattr">attributes</a> "timestamp_begin", "timestamp_end" or "timeDiffToNow / timeOlderThan".
  9205. The reading to evaluate must be defined using attribute "reading".
  9206. The evaluation contains the timestamp of the <b>last</b> appearing of the identified maximum value
  9207. within the given period. <br>
  9208. Is no or the option "display" specified, the results are only displayed. Using
  9209. option "writeToDB" the calculated results are stored in the database with a new reading
  9210. name. <br>
  9211. The new readingname is built of a prefix and the original reading name,
  9212. in which the original reading name can be replaced by the value of attribute "readingNameMap".
  9213. The prefix is made up of the creation function and the aggregation. <br>
  9214. The timestamp of the new stored readings is deviated from aggregation period,
  9215. unless no unique point of time of the result can be determined.
  9216. The field "EVENT" will be filled with "calculated".<br><br>
  9217. <ul>
  9218. <b>Example of building a new reading name from the original reading "totalpac":</b> <br>
  9219. max_day_totalpac <br>
  9220. # &lt;creation function&gt;_&lt;aggregation&gt;_&lt;original reading&gt; <br>
  9221. </ul>
  9222. <br>
  9223. Summarized the relevant attributes to control this function are: <br><br>
  9224. <ul>
  9225. <table>
  9226. <colgroup> <col width=5%> <col width=95%> </colgroup>
  9227. <tr><td> <b>aggregation</b> </td><td>: choose the aggregation period </td></tr>
  9228. <tr><td> <b>device</b> </td><td>: include or exclude &lt;device&gt; from selection </td></tr>
  9229. <tr><td> <b>reading</b> </td><td>: include or exclude &lt;reading&gt; from selection </td></tr>
  9230. <tr><td> <b>readingNameMap</b> </td><td>: rename the resulted readings </td></tr>
  9231. <tr><td> <b>time.*</b> </td><td>: a number of attributes to limit selection by time </td></tr>
  9232. <tr><td style="vertical-align:top"> <b>valueFilter</b> <td>: an additional REGEXP to control the record selection. The REGEXP is applied to the database field 'VALUE'. </td></tr>
  9233. </table>
  9234. </ul>
  9235. </li><br>
  9236. <li><b> minValue [display | writeToDB]</b>
  9237. - calculates the minimum value of database column "VALUE" between period given by
  9238. <a href="#DbRepattr">attributes</a> "timestamp_begin", "timestamp_end" or "timeDiffToNow / timeOlderThan".
  9239. The reading to evaluate must be defined using attribute "reading".
  9240. The evaluation contains the timestamp of the <b>first</b> appearing of the identified minimum
  9241. value within the given period. <br>
  9242. Is no or the option "display" specified, the results are only displayed. Using
  9243. option "writeToDB" the calculated results are stored in the database with a new reading
  9244. name. <br>
  9245. The new readingname is built of a prefix and the original reading name,
  9246. in which the original reading name can be replaced by the value of attribute "readingNameMap".
  9247. The prefix is made up of the creation function and the aggregation. <br>
  9248. The timestamp of the new stored readings is deviated from aggregation period,
  9249. unless no unique point of time of the result can be determined.
  9250. The field "EVENT" will be filled with "calculated".<br><br>
  9251. <ul>
  9252. <b>Example of building a new reading name from the original reading "totalpac":</b> <br>
  9253. min_day_totalpac <br>
  9254. # &lt;creation function&gt;_&lt;aggregation&gt;_&lt;original reading&gt; <br>
  9255. </ul>
  9256. <br>
  9257. Summarized the relevant attributes to control this function are: <br><br>
  9258. <ul>
  9259. <table>
  9260. <colgroup> <col width=5%> <col width=95%> </colgroup>
  9261. <tr><td> <b>aggregation</b> </td><td>: choose the aggregation period </td></tr>
  9262. <tr><td> <b>device</b> </td><td>: include or exclude &lt;device&gt; from selection </td></tr>
  9263. <tr><td> <b>reading</b> </td><td>: include or exclude &lt;reading&gt; from selection </td></tr>
  9264. <tr><td> <b>readingNameMap</b> </td><td>: rename the resulted readings </td></tr>
  9265. <tr><td> <b>time.*</b> </td><td>: a number of attributes to limit selection by time </td></tr>
  9266. <tr><td style="vertical-align:top"> <b>valueFilter</b> <td>: an additional REGEXP to control the record selection. The REGEXP is applied to the database field 'VALUE'. </td></tr>
  9267. </table>
  9268. </ul>
  9269. </li><br>
  9270. <li><b> optimizeTables </b> - optimize tables in the connected database (MySQL). <br>
  9271. Before and after an optimization it is possible to execute a FHEM command.
  9272. (please see <a href="#DbRepattr">attributes</a> "executeBeforeProc", "executeAfterProc")
  9273. <br><br>
  9274. <ul>
  9275. <b>Note:</b> <br>
  9276. Even though the function itself is designed non-blocking, make sure the assigned DbLog-device
  9277. is operating in asynchronous mode to avoid FHEMWEB from blocking. <br><br>
  9278. </li><br>
  9279. </ul>
  9280. <li><b> readingRename </b> - renames the reading name of a device inside the connected database (see Internal DATABASE).
  9281. The readingname will allways be changed in the <b>entire</b> database. Possibly set time limits or restrictions by
  9282. <a href="#DbRepattr">attributes</a> device and/or reading will not be considered. <br><br>
  9283. <ul>
  9284. <b>Example: </b> <br>
  9285. set &lt;name&gt; readingRename &lt;old reading name&gt;,&lt;new reading name&gt; <br>
  9286. # The amount of renamed reading names (datasets) will be displayed in reading "reading_renamed". <br>
  9287. # If the reading name to be renamed was not found in the database, a WARNUNG will appear in reading "reading_not_renamed". <br>
  9288. # Appropriate entries will be written to Logfile if verbose >= 3 is set.
  9289. <br><br>
  9290. <b>Note:</b> <br>
  9291. Even though the function itself is designed non-blocking, make sure the assigned DbLog-device
  9292. is operating in asynchronous mode to avoid FHEMWEB from blocking. <br><br>
  9293. </li> <br>
  9294. </ul>
  9295. <li><b> reduceLog [average[=day]] </b> <br>
  9296. Reduces historical records within the limits given by the "time.*"-attributes to one record
  9297. (the 1st) each hour per device & reading.
  9298. At least one of the "time.*"-attributes must be set (pls. see table below).
  9299. Each of missing time limit will be calculated by the module itself in this case.
  9300. <br><br>
  9301. With the optional argument 'average' not only the records will be reduced, but all numerical
  9302. values of an hour will be reduced to a single average.
  9303. With option 'average=day' all numerical values of a day are reduced to a single average
  9304. (implies 'average'). <br><br>
  9305. By setting attributes "device" and/or "reading" the records to be considered could be included
  9306. respectively excluded. Both containments delimit the selected data from database and may
  9307. reduce the system RAM load.
  9308. The reading "reduceLogState" contains the result of the last executed reducelog run. <br><br>
  9309. The relevant attributes for this function are: <br><br>
  9310. <ul>
  9311. <table>
  9312. <colgroup> <col width=5%> <col width=95%> </colgroup>
  9313. <tr><td> <b>executeBeforeProc</b> </td><td>: execution of FHEM command (or perl-routine) before reducelog </td></tr>
  9314. <tr><td> <b>executeAfterProc</b> </td><td>: execution of FHEM command (or perl-routine) after reducelog </td></tr>
  9315. <tr><td> <b>device</b> </td><td>: include or exclude &lt;device&gt; for selection </td></tr>
  9316. <tr><td> <b>reading</b> </td><td>: include or exclude &lt;reading&gt; for selection </td></tr>
  9317. <tr><td> <b>timeOlderThan</b> </td><td>: records <b>older</b> than this attribute will be reduced </td></tr>
  9318. <tr><td> <b>timestamp_end</b> </td><td>: records <b>older</b> than this attribute will be reduced </td></tr>
  9319. <tr><td> <b>timeDiffToNow</b> </td><td>: records <b>newer</b> than this attribute will be reduced </td></tr>
  9320. <tr><td> <b>timestamp_begin</b> </td><td>: records <b>newer</b> than this attribute will be reduced </td></tr>
  9321. <tr><td style="vertical-align:top"> <b>valueFilter</b> <td>: an additional REGEXP to control the record selection. The REGEXP is applied to the database field 'VALUE'. </td></tr>
  9322. </table>
  9323. </ul>
  9324. <br>
  9325. For compatibility reasons the reducelog command can optionally be completed with supplements "EXCLUDE"
  9326. respectively "INCLUDE" to exclude and/or include device/reading combinations from reducelog: <br><br>
  9327. <ul>
  9328. "reduceLog [average[=day]] [EXCLUDE=device1:reading1,device2:reading2,...] [INCLUDE=device:reading]" <br><br>
  9329. </ul>
  9330. This declaration is evaluated as Regex and overwrites the attributes "device" and "reading",
  9331. which won't be considered in this case. <br><br>
  9332. <ul>
  9333. <b>Examples: </b><br><br>
  9334. attr &lt;name&gt; timeOlderThan = d:200 <br>
  9335. set &lt;name&gt; reduceLog <br>
  9336. # Records older than 200 days are reduced to the first entry per hour of each device and reading.
  9337. <br>
  9338. <br>
  9339. attr &lt;name&gt; timeDiffToNow = d:200 <br>
  9340. set &lt;name&gt; reduceLog average=day <br>
  9341. # Records newer than 200 days are reduced to a single average a day of each device and reading.
  9342. <br>
  9343. <br>
  9344. attr &lt;name&gt; timeDiffToNow = d:30 <br>
  9345. attr &lt;name&gt; device = TYPE=SONOSPLAYER EXCLUDE=Sonos_Kueche <br>
  9346. attr &lt;name&gt; reading = room% EXCLUDE=roomNameAlias <br>
  9347. set &lt;name&gt; reduceLog <br>
  9348. # Records newer than 30 days which are contain devices from type SONOSPLAYER
  9349. (except device "Sonos_Kueche"), the readings beginning with "room" (except "roomNameAlias"),
  9350. are reduced to the first entry per hour of each device and reading. <br>
  9351. <br>
  9352. attr &lt;name&gt; timeDiffToNow = d:10 <br>
  9353. attr &lt;name&gt; timeOlderThan = d:5 <br>
  9354. attr &lt;name&gt; device = Luftdaten_remote <br>
  9355. set &lt;name&gt; reduceLog average <br>
  9356. # Records older than 5 and newer than 10 days and contain DEVICE "Luftdaten_remote" are
  9357. revised. Numerical values are reduced to the first entry per hour of each device and reading
  9358. <br>
  9359. <br>
  9360. </ul>
  9361. <b>Note:</b> <br>
  9362. Even though the function itself is non-blocking, you have to set the attached DbLog device into
  9363. the asynchronous mode (attr asyncMode = 1) to avoid a blocking situation of FHEM !. <br>
  9364. It is strongly recommended to check if the default INDEX 'Search_Idx' exists on the table
  9365. 'history'! <br><br>
  9366. </li> <br>
  9367. <li><b> repairSQLite </b> - repairs a corrupted SQLite database. <br>
  9368. A corruption is usally existent when the error message "database disk image is malformed"
  9369. appears in reading "state" of the connected DbLog-device.
  9370. If the command was started, the connected DbLog-device will firstly disconnected from the
  9371. database for 10 hours (36000 seconds) automatically (breakup time). After the repair is
  9372. finished, the DbLog-device will be connected to the (repaired) database immediately. <br>
  9373. As an argument the command can be completed by a differing breakup time (in seconds). <br>
  9374. The corrupted database is saved as &lt;database&gt;.corrupt in same directory. <br><br>
  9375. <ul>
  9376. <b>Example: </b><br>
  9377. set &lt;name&gt; repairSQLite <br>
  9378. # the database is trying to repair, breakup time is 10 hours <br>
  9379. set &lt;name&gt; repairSQLite 600 <br>
  9380. # the database is trying to repair, breakup time is 10 minutes
  9381. <br><br>
  9382. <b>Note:</b> <br>
  9383. It can't be guaranteed, that the repair attempt proceed successfully and no data loss will result.
  9384. Depending from corruption severity data loss may occur or the repair will fail even though
  9385. no error appears during the repair process. Please make sure a valid backup took place ! <br><br>
  9386. </li> <br>
  9387. </ul>
  9388. <li><b> restoreMySQL &lt;File&gt; </b> - restore a database from serverSide- or clientSide-Dump. <br>
  9389. The function provides a drop-down-list of files which can be used for restore. <br><br>
  9390. <b>Usage of serverSide-Dumps </b> <br>
  9391. The content of history-table will be restored from a serverSide-Dump.
  9392. Therefore the remote directory "dumpDirRemote" of the MySQL-Server has to be mounted on the
  9393. Client and make it usable to the DbRep-device by setting <a href="#DbRepattr">attribute</a>
  9394. "dumpDirLocal" to the appropriate value. <br>
  9395. All files with extension "csv[.gzip]" and if the filename is beginning with the name of the connected database
  9396. (see Internal DATABASE) are listed.
  9397. <br><br>
  9398. <b>Usage of clientSide-Dumps </b> <br>
  9399. All tables and views (if present) are restored.
  9400. The directory which contains the dump files has to be set by <a href="#DbRepattr">attribute</a>
  9401. "dumpDirLocal" to make it usable by the DbRep device. <br>
  9402. All files with extension "sql[.gzip]" and if the filename is beginning with the name of the connected database
  9403. (see Internal DATABASE) are listed. <br>
  9404. The restore speed depends of the server variable "<b>max_allowed_packet</b>". You can change
  9405. this variable in file my.cnf to adapt the speed. Please consider the need of sufficient ressources
  9406. (especially RAM).
  9407. <br><br>
  9408. The database user needs rights for database management, e.g.: <br>
  9409. CREATE, ALTER, INDEX, DROP, SHOW VIEW, CREATE VIEW
  9410. <br><br>
  9411. </li><br>
  9412. <li><b> restoreSQLite &lt;File&gt;.sqlitebkp[.gzip] </b> - restores a backup of SQLite database. <br>
  9413. The function provides a drop-down-list of files which can be used for restore.
  9414. The data stored in the current database are deleted respectively overwritten.
  9415. All files with extension "sqlitebkp[.gzip]" and if the filename is beginning with the name of the connected database
  9416. will are listed. <br><br>
  9417. </li><br>
  9418. <li><b> sqlCmd </b> - executes an arbitrary user specific command. <br>
  9419. If the command contains a operation to delete data, the <a href="#DbRepattr">attribute</a>
  9420. "allowDeletion" has to be set for security reason. <br>
  9421. The statement doesn't consider limitations by attributes "device", "reading", "time.*"
  9422. respectively "aggregation". <br>
  9423. If the <a href="#DbRepattr">attribute</a> "timestamp_begin" respectively "timestamp_end"
  9424. is assumed in the statement, it is possible to use placeholder "<b>§timestamp_begin§</b>" respectively
  9425. "<b>§timestamp_end§</b>" on suitable place. <br><br>
  9426. If you want update a dataset, you have to add "TIMESTAMP=TIMESTAMP" to the update-statement to avoid changing the
  9427. original timestamp. <br><br>
  9428. <ul>
  9429. <b>Examples of SQL-statements: </b> <br><br>
  9430. <ul>
  9431. <li>set &lt;name&gt; sqlCmd select DEVICE, count(*) from history where TIMESTAMP >= "2017-01-06 00:00:00" group by DEVICE having count(*) > 800 </li>
  9432. <li>set &lt;name&gt; sqlCmd select DEVICE, count(*) from history where TIMESTAMP >= "2017-05-06 00:00:00" group by DEVICE </li>
  9433. <li>set &lt;name&gt; sqlCmd select DEVICE, count(*) from history where TIMESTAMP >= §timestamp_begin§ group by DEVICE </li>
  9434. <li>set &lt;name&gt; sqlCmd select * from history where DEVICE like "Te%t" order by `TIMESTAMP` desc </li>
  9435. <li>set &lt;name&gt; sqlCmd select * from history where `TIMESTAMP` > "2017-05-09 18:03:00" order by `TIMESTAMP` desc </li>
  9436. <li>set &lt;name&gt; sqlCmd select * from current order by `TIMESTAMP` desc </li>
  9437. <li>set &lt;name&gt; sqlCmd select sum(VALUE) as 'Einspeisung am 04.05.2017', count(*) as 'Anzahl' FROM history where `READING` = "Einspeisung_WirkP_Zaehler_Diff" and TIMESTAMP between '2017-05-04' AND '2017-05-05' </li>
  9438. <li>set &lt;name&gt; sqlCmd delete from current </li>
  9439. <li>set &lt;name&gt; sqlCmd delete from history where TIMESTAMP < "2016-05-06 00:00:00" </li>
  9440. <li>set &lt;name&gt; sqlCmd update history set TIMESTAMP=TIMESTAMP,VALUE='Val' WHERE VALUE='TestValue' </li>
  9441. <li>set &lt;name&gt; sqlCmd select * from history where DEVICE = "Test" </li>
  9442. <li>set &lt;name&gt; sqlCmd insert into history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES ('2017-05-09 17:00:14','Test','manuell','manuell','Tes§e','TestValue','°C') </li>
  9443. </ul>
  9444. <br>
  9445. The result of the statement will be shown in <a href="#DbRepReadings">Reading</a> "SqlResult".
  9446. The formatting of result can be choosen by <a href="#DbRepattr">attribute</a> "sqlResultFormat", as well as the used
  9447. field separator can be determined by <a href="#DbRepattr">attribute</a> "sqlResultFieldSep". <br><br>
  9448. The module provides a command history once a sqlCmd command was executed successfully.
  9449. To use this option, activate the attribute "sqlCmdHistoryLength" with list lenght you want. <br><br>
  9450. For a better overview the relevant attributes for sqlCmd are listed in a table: <br><br>
  9451. <ul>
  9452. <table>
  9453. <colgroup> <col width=5%> <col width=95%> </colgroup>
  9454. <tr><td> <b>allowDeletion</b> </td><td>: activates capabilty to delete datasets </td></tr>
  9455. <tr><td> <b>sqlResultFormat</b> </td><td>: determines presentation style of command result </td></tr>
  9456. <tr><td> <b>sqlResultFieldSep</b> </td><td>: choice of a useful field separator for result </td></tr>
  9457. <tr><td> <b>sqlCmdHistoryLength</b> </td><td>: activates command history and length </td></tr>
  9458. </table>
  9459. </ul>
  9460. <br>
  9461. <br>
  9462. <b>Note:</b> <br>
  9463. Even though the module works non-blocking regarding to database operations, a huge
  9464. sample space (number of rows/readings) could block the browser session respectively
  9465. FHEMWEB.
  9466. If you are unsure about the result of the statement, you should preventively add a limit to
  9467. the statement. <br><br>
  9468. </li><br>
  9469. </ul>
  9470. <li><b> sqlCmdHistory </b> - If history is activated by <a href="#DbRepattr">attribute</a> "sqlCmdHistoryLength", an already
  9471. successfully executed sqlCmd-command can be repeated from a drop-down list. <br>
  9472. By execution of the last list entry, "__purge_historylist__", the list itself can be deleted. <br>
  9473. If the statement contains "," this character is displayed as "&lt;c&gt;" in the history
  9474. list due to technical restrictions. <br>
  9475. </li><br>
  9476. <li><b> sqlSpecial </b> - This function provides a drop-down list with a selection of prepared reportings. <br>
  9477. The statements result is depicted in reading "SqlResult".
  9478. The result can be formatted by <a href="#DbRepattr">attribute</a> "sqlResultFormat",
  9479. a well as the used field separator by <a href="#DbRepattr">attribute</a> "sqlResultFieldSep".
  9480. <br><br>
  9481. The relevant attributes for this function are: <br><br>
  9482. <ul>
  9483. <table>
  9484. <colgroup> <col width=5%> <col width=95%> </colgroup>
  9485. <tr><td> <b>sqlResultFormat</b> </td><td>: determines the formatting of the result </td></tr>
  9486. <tr><td> <b>sqlResultFieldSep</b> </td><td>: determines the used field separator in statement result </td></tr>
  9487. </table>
  9488. </ul>
  9489. <br>
  9490. The following predefined reportings are selectable: <br><br>
  9491. <ul>
  9492. <table>
  9493. <colgroup> <col width=5%> <col width=95%> </colgroup>
  9494. <tr><td> <b>50mostFreqLogsLast2days </b> </td><td>: reports the 50 most occuring log entries of the last 2 days </td></tr>
  9495. <tr><td> <b>allDevCount </b> </td><td>: all devices occuring in database and their quantity </td></tr>
  9496. <tr><td> <b>allDevReadCount </b> </td><td>: all device/reading combinations occuring in database and their quantity </td></tr>
  9497. </table>
  9498. </ul>
  9499. </li><br><br>
  9500. <li><b> sumValue [display | writeToDB]</b>
  9501. - calculates the summary of database column "VALUE" between period given by
  9502. <a href="#DbRepattr">attributes</a> "timestamp_begin", "timestamp_end" or
  9503. "timeDiffToNow / timeOlderThan". The reading to evaluate must be defined using attribute
  9504. "reading". Using this function is mostly reasonable if value-differences of readings
  9505. are written to the database. <br>
  9506. Is no or the option "display" specified, the results are only displayed. Using
  9507. option "writeToDB" the calculation results are stored in the database with a new reading
  9508. name. <br>
  9509. The new readingname is built of a prefix and the original reading name,
  9510. in which the original reading name can be replaced by the value of attribute "readingNameMap".
  9511. The prefix is made up of the creation function and the aggregation. <br>
  9512. The timestamp of the new stored readings is deviated from aggregation period,
  9513. unless no unique point of time of the result can be determined.
  9514. The field "EVENT" will be filled with "calculated".<br><br>
  9515. <ul>
  9516. <b>Example of building a new reading name from the original reading "totalpac":</b> <br>
  9517. sum_day_totalpac <br>
  9518. # &lt;creation function&gt;_&lt;aggregation&gt;_&lt;original reading&gt; <br>
  9519. </ul>
  9520. <br>
  9521. Summarized the relevant attributes to control this function are: <br><br>
  9522. <ul>
  9523. <table>
  9524. <colgroup> <col width=5%> <col width=95%> </colgroup>
  9525. <tr><td> <b>aggregation</b> </td><td>: choose the aggregation period </td></tr>
  9526. <tr><td> <b>device</b> </td><td>: include or exclude &lt;device&gt; from selection </td></tr>
  9527. <tr><td> <b>reading</b> </td><td>: include or exclude &lt;reading&gt; from selection </td></tr>
  9528. <tr><td> <b>readingNameMap</b> </td><td>: rename the resulted readings </td></tr>
  9529. <tr><td> <b>time.*</b> </td><td>: a number of attributes to limit selection by time </td></tr>
  9530. <tr><td style="vertical-align:top"> <b>valueFilter</b> <td>: an additional REGEXP to control the record selection. The REGEXP is applied to the database field 'VALUE'. </td></tr>
  9531. </table>
  9532. </ul>
  9533. </li> <br>
  9534. <li><b> syncStandby &lt;DbLog-Device Standby&gt; </b>
  9535. - datasets of the connected database (source) are transmitted into another database
  9536. (Standby-database). <br>
  9537. Here the "&lt;DbLog-Device Standby&gt;" is the DbLog-Device what is connected to the
  9538. Standby-database. <br><br>
  9539. All the datasets which are determined by timestamp-<a href="#limit">attributes</a>
  9540. or respectively the attributes "device", "reading" are transmitted. <br>
  9541. The datasets are transmitted in time slices accordingly to the adjusted aggregation.
  9542. If the attribute "aggregation" has value "no" or "month", the datasets are transmitted
  9543. automatically in daily time slices into standby-database.
  9544. Source- and Standby-database can be of different types.
  9545. <br><br>
  9546. The relevant attributes to control the syncStandby function are: <br><br>
  9547. <ul>
  9548. <table>
  9549. <colgroup> <col width=5%> <col width=95%> </colgroup>
  9550. <tr><td> <b>aggregation</b> </td><td>: adjustment of time slices for data transmission (hour,day,week) </td></tr>
  9551. <tr><td> <b>device</b> </td><td>: include or exclude &lt;device&gt; for transmission </td></tr>
  9552. <tr><td> <b>reading</b> </td><td>: include or exclude &lt;reading&gt; for transmission </td></tr>
  9553. <tr><td> <b>time.*</b> </td><td>: a number of attributes to limit selection by time </td></tr>
  9554. <tr><td style="vertical-align:top"> <b>valueFilter</b> <td>: an additional REGEXP to control the record selection. The REGEXP is applied to the database field 'VALUE'. </td></tr>
  9555. </table>
  9556. </ul>
  9557. </li> <br>
  9558. <li><b> tableCurrentFillup </b> - the current-table will be filled u with an extract of the history-table.
  9559. The <a href="#DbRepattr">attributes</a> for limiting time and device, reading are considered.
  9560. Thereby the content of the extract can be affected. In the associated DbLog-device the attribute "DbLogType" should be set to
  9561. "SampleFill/History". </li> <br>
  9562. <li><b> tableCurrentPurge </b> - deletes the content of current-table. There are no limits, e.g. by attributes "timestamp_begin", "timestamp_end", device, reading
  9563. and so on, considered. </li> <br>
  9564. <li><b> vacuum </b> - optimize tables in the connected database (SQLite, PostgreSQL). <br>
  9565. Before and after an optimization it is possible to execute a FHEM command.
  9566. (please see <a href="#DbRepattr">attributes</a> "executeBeforeProc", "executeAfterProc")
  9567. <br><br>
  9568. <ul>
  9569. <b>Note:</b> <br>
  9570. Even though the function itself is designed non-blocking, make sure the assigned DbLog-device
  9571. is operating in asynchronous mode to avoid FHEM from blocking. <br><br>
  9572. </li>
  9573. </ul><br>
  9574. <br>
  9575. </ul></ul>
  9576. <b>For all evaluation variants (except sqlCmd,deviceRename,readingRename) applies: </b> <br>
  9577. In addition to the needed reading the device can be complemented to restrict the datasets for reporting / function.
  9578. If no time limit attribute is set but aggregation is set, the period from the oldest dataset in database to the current
  9579. date/time will be used as selection criterion. If the oldest dataset wasn't identified, then '1970-01-01 01:00:00' is used
  9580. as start date (see get &lt;name&gt; "minTimestamp" also).
  9581. If both time limit attribute and aggregation isn't set, the selection on database is runnung without timestamp criterion.
  9582. <br><br>
  9583. <b>Note: </b> <br>
  9584. If you are in detail view it could be necessary to refresh the browser to see the result of operation as soon in DeviceOverview section "state = done" will be shown.
  9585. <br><br>
  9586. </ul>
  9587. <a name="DbRepget"></a>
  9588. <b>Get </b>
  9589. <ul>
  9590. The get-commands of DbRep provide to retrieve some metadata of the used database instance.
  9591. Those are for example adjusted server parameter, server variables, datadasestatus- and table informations. THe available get-functions depending of
  9592. the used database type. So for SQLite curently only "get svrinfo" is usable. The functions nativ are delivering a lot of outpit values.
  9593. They can be limited by function specific <a href="#DbRepattr">attributes</a>. The filter has to be setup by a comma separated list.
  9594. SQL-Wildcard (%) can be used to setup the list arguments.
  9595. <br><br>
  9596. <b>Note: </b> <br>
  9597. After executing a get-funktion in detail view please make a browser refresh to see the results !
  9598. <br><br>
  9599. <ul><ul>
  9600. <li><b> blockinginfo </b> - list the current system wide running background processes (BlockingCalls) together with their informations.
  9601. If character string is too long (e.g. arguments) it is reported shortened.
  9602. </li>
  9603. <br><br>
  9604. <li><b> dbstatus </b> - lists global informations about MySQL server status (e.g. informations related to cache, threads, bufferpools, etc. ).
  9605. Initially all available informations are reported. Using the <a href="#DbRepattr">attribute</a> "showStatus" the quantity of
  9606. results can be limited to show only the desired values. Further detailed informations of items meaning are
  9607. explained <a href=http://dev.mysql.com/doc/refman/5.7/en/server-status-variables.html>there</a>. <br>
  9608. <br><ul>
  9609. <b>Example</b> <br>
  9610. get &lt;name&gt; dbstatus <br>
  9611. attr &lt;name&gt; showStatus %uptime%,%qcache% <br>
  9612. # Only readings containing "uptime" and "qcache" in name will be created
  9613. </li>
  9614. <br><br>
  9615. </ul>
  9616. <li><b> dbValue &lt;SQL-statement&gt;</b> -
  9617. Executes the specified SQL-statement in <b>blocking</b> manner. Because of its mode of operation
  9618. this function is particular convenient for user own perl scripts. <br>
  9619. The input accepts multi line commands and delivers multi line results as well.
  9620. If several fields are selected and passed back, the fieds are separated by the separator defined
  9621. by <a href="#DbRepattr">attribute</a> "sqlResultFieldSep" (default "|"). Several result lines
  9622. are separated by newline ("\n"). <br>
  9623. This function only set/update status readings, the userExitFn function isn't called.
  9624. <br>
  9625. <br><ul>
  9626. <b>Examples for use in FHEMWEB</b> <br>
  9627. {fhem("get &lt;name&gt; dbValue select device,count(*) from history where timestamp > '2018-04-01' group by device")} <br>
  9628. get &lt;name&gt; dbValue select device,count(*) from history where timestamp > '2018-04-01' group by device <br>
  9629. {CommandGet(undef,"Rep.LogDB1 dbValue select device,count(*) from history where timestamp > '2018-04-01' group by device")} <br>
  9630. </ul>
  9631. <br><br>
  9632. If you create a little routine in 99_myUtils, for example:
  9633. <br>
  9634. <pre>
  9635. sub dbval($$) {
  9636. my ($name,$cmd) = @_;
  9637. my $ret = CommandGet(undef,"$name dbValue $cmd");
  9638. return $ret;
  9639. }
  9640. </pre>
  9641. it can be accessed with e.g. those calls:
  9642. <br><br>
  9643. <ul>
  9644. <b>Examples:</b> <br>
  9645. {dbval("&lt;name&gt;","select count(*) from history")} <br>
  9646. $ret = dbval("&lt;name&gt;","select count(*) from history"); <br>
  9647. </ul>
  9648. </li>
  9649. <br><br>
  9650. <li><b> dbvars </b> - lists global informations about MySQL system variables. Included are e.g. readings related to InnoDB-Home, datafile path,
  9651. memory- or cache-parameter and so on. The Output reports initially all available informations. Using the
  9652. <a href="#DbRepattr">attribute</a> "showVariables" the quantity of results can be limited to show only the desired values.
  9653. Further detailed informations of items meaning are explained
  9654. <a href=http://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html>there</a>. <br>
  9655. <br><ul>
  9656. <b>Example</b> <br>
  9657. get &lt;name&gt; dbvars <br>
  9658. attr &lt;name&gt; showVariables %version%,%query_cache% <br>
  9659. # Only readings containing "version" and "query_cache" in name will be created
  9660. </li>
  9661. <br><br>
  9662. </ul>
  9663. <li><b> minTimestamp </b> - Identifies the oldest timestamp in the database (will be executed implicitely at FHEM start).
  9664. The timestamp is used as begin of data selection if no time attribut is set to determine the
  9665. start date.
  9666. </li>
  9667. <br><br>
  9668. <li><b> procinfo </b> - reports the existing database processes in a summary table (only MySQL). <br>
  9669. Typically only the own processes of the connection user (set in DbLog configuration file) will be
  9670. reported. If all precesses have to be reported, the global "PROCESS" right has to be granted to the
  9671. user. <br>
  9672. As of MariaDB 5.3 for particular SQL-Statements a progress reporting will be provided
  9673. (table row "PROGRESS"). So you can track, for instance, the degree of processing during an index
  9674. creation. <br>
  9675. Further informations can be found
  9676. <a href=https://mariadb.com/kb/en/mariadb/show-processlist/>there</a>. <br>
  9677. </li>
  9678. <br><br>
  9679. <li><b> svrinfo </b> - common database server informations, e.g. DBMS-version, server address and port and so on. The quantity of elements to get depends
  9680. on the database type. Using the <a href="#DbRepattr">attribute</a> "showSvrInfo" the quantity of results can be limited to show only
  9681. the desired values. Further detailed informations of items meaning are explained
  9682. <a href=https://msdn.microsoft.com/en-us/library/ms711681(v=vs.85).aspx>there</a>. <br>
  9683. <br><ul>
  9684. <b>Example</b> <br>
  9685. get &lt;name&gt; svrinfo <br>
  9686. attr &lt;name&gt; showSvrInfo %SQL_CATALOG_TERM%,%NAME% <br>
  9687. # Only readings containing "SQL_CATALOG_TERM" and "NAME" in name will be created
  9688. </li>
  9689. <br><br>
  9690. </ul>
  9691. <li><b> tableinfo </b> - access detailed informations about tables in MySQL database which is connected by the DbRep-device.
  9692. All available tables in the connected database will be selected by default.
  9693. Using the<a href="#DbRepattr">attribute</a> "showTableInfo" the results can be limited to tables you want to show.
  9694. Further detailed informations of items meaning are explained <a href=http://dev.mysql.com/doc/refman/5.7/en/show-table-status.html>there</a>. <br>
  9695. <br><ul>
  9696. <b>Example</b> <br>
  9697. get &lt;name&gt; tableinfo <br>
  9698. attr &lt;name&gt; showTableInfo current,history <br>
  9699. # Only informations related to tables "current" and "history" are going to be created
  9700. </li>
  9701. <br><br>
  9702. </ul>
  9703. <li><b> versionNotes [hints | rel | &lt;key&gt;] </b> -
  9704. Shows realease informations and/or hints about the module. It contains only main release
  9705. informations for module users. <br>
  9706. If no options are specified, both release informations and hints will be shown. "rel" shows
  9707. only release informations and "hints" shows only hints. By the &lt;key&gt;-specification only
  9708. the hint with the specified number is shown.
  9709. </li>
  9710. <br>
  9711. </ul></ul>
  9712. </ul>
  9713. <a name="DbRepattr"></a>
  9714. <b>Attributes</b>
  9715. <br>
  9716. <ul>
  9717. Using the module specific attributes you are able to define the scope of evaluation and the aggregation. <br>
  9718. The listed attrbutes are not completely relevant for every function of the module. The help of set/get-commands
  9719. contain explicitly which attributes are relevant for the specific command. <br><br>
  9720. <b>Note for SQL-Wildcard Usage:</b> <br>
  9721. Within the attribute values of "device" and "reading" you may use SQL-Wildcard "%", Character "_" is not supported as a wildcard.
  9722. The character "%" stands for any characters. <br>
  9723. This rule is valid to all functions <b>except</b> "insert", "importFromFile" and "deviceRename". <br>
  9724. The function "insert" doesn't allow setting the mentioned attributes containing the wildcard "%". <br>
  9725. In readings the wildcard character "%" will be replaced by "/" to meet the rules of allowed characters in readings.
  9726. <br><br>
  9727. <ul><ul>
  9728. <a name="aggregation"></a>
  9729. <li><b>aggregation </b> - Aggregation of Device/Reading-selections. Possible is hour, day, week, month or "no".
  9730. Delivers e.g. the count of database entries for a day (countEntries), Summation of
  9731. difference values of a reading (sumValue) and so on. Using aggregation "no" (default) an
  9732. aggregation don't happens but the output contaims all values of Device/Reading in the defined time period. </li> <br>
  9733. <a name="allowDeletion"></a>
  9734. <li><b>allowDeletion </b> - unlocks the delete-function </li> <br>
  9735. <a name="averageCalcForm"></a>
  9736. <li><b>averageCalcForm </b> - specifies the calculation variant of average peak by "averageValue". <br><br>
  9737. At the moment the following methods are implemented: <br><br>
  9738. <ul>
  9739. <table>
  9740. <colgroup> <col width=20%> <col width=80%> </colgroup>
  9741. <tr><td><b>avgArithmeticMean :</b> </td><td>the arithmetic average is calculated (default) </td></tr>
  9742. <tr><td style="vertical-align:top"><b>avgDailyMeanGWS :</b> <td>calculates the daily medium temperature according the
  9743. specifications of german weather service (pls. see "get &lt;name&gt; versionNotes 2"). <br>
  9744. This variant uses aggregation "day" automatically. </td></tr>
  9745. <tr><td><b>avgTimeWeightMean :</b> </td><td>calculates a time weighted average mean value is calculated </td></tr>
  9746. </table>
  9747. </ul>
  9748. </li><br>
  9749. <a name="countEntriesDetail"></a>
  9750. <li><b>countEntriesDetail </b> - If set, the function countEntries creates a detailed report of counted datasets of
  9751. every reading. By default only the summary of counted datasets is reported.
  9752. </li> <br>
  9753. <a name="device"></a>
  9754. <li><b>device </b> - Selection of particular or several devices. <br>
  9755. You can specify a list of devices separated by "," or use device specifications (devspec). <br>
  9756. Inside of lists or device specifications a SQL wildcard (%) will be evaluated as a normal
  9757. ASCII-character.
  9758. The device names are derived from device specification and the active devices in FHEM before
  9759. SQL selection will be carried out. <br>
  9760. If the the device, list or device specification is prepended by "EXCLUDE=",
  9761. the devices are excluded from database selection.
  9762. </li> <br>
  9763. <ul>
  9764. <b>Examples:</b> <br>
  9765. <code>attr &lt;name&gt; device TYPE=DbRep</code> <br>
  9766. # select datasets of active present devices with Type "DbRep" <br>
  9767. <code>attr &lt;name&gt; device MySTP_5000</code> <br>
  9768. # select datasets of device "MySTP_5000" <br>
  9769. <code>attr &lt;name&gt; device SMA.*</code> <br>
  9770. # select datasets of devices starting with "SMA" <br>
  9771. <code>attr &lt;name&gt; device SMA_Energymeter,MySTP_5000</code> <br>
  9772. # select datasets of devices "SMA_Energymeter" and "MySTP_5000" <br>
  9773. <code>attr &lt;name&gt; device %5000</code> <br>
  9774. # select datasets of devices ending with "5000" <br>
  9775. <code>attr &lt;name&gt; device TYPE=SSCam EXCLUDE=SDS1_SVS </code> <br>
  9776. # select datasets of devices TYPE SSCam but exclude device "SDS1_SVS" <br>
  9777. <code>attr &lt;name&gt; device EXCLUDE=SDS1_SVS </code> <br>
  9778. # select all devices except device "SDS1_SVS" <br>
  9779. <code>attr &lt;name&gt; device EXCLUDE=TYPE=SSCam </code> <br>
  9780. # select all devices except devices of TYPE "SSCam" <br>
  9781. </ul>
  9782. <br>
  9783. If you need more information about device specifications, execute
  9784. "get &lt;name&gt; versionNotes 3".
  9785. <br><br>
  9786. <a name="diffAccept"></a>
  9787. <li><b>diffAccept </b> - valid for function diffValue. diffAccept determines the threshold, up to that a calaculated
  9788. difference between two straight sequently datasets should be commenly accepted
  9789. (default = 20). <br>
  9790. Hence faulty DB entries with a disproportional high difference value will be eliminated and
  9791. don't tamper the result.
  9792. If a threshold overrun happens, the reading "diff_overrun_limit_&lt;diffLimit&gt;" will be
  9793. generated (&lt;diffLimit&gt; will be substituted with the present prest attribute value). <br>
  9794. The reading contains a list of relevant pair of values. Using verbose=3 this list will also
  9795. be reported in the FHEM logfile.
  9796. </li><br>
  9797. <ul>
  9798. Example report in logfile if threshold of diffAccept=10 overruns: <br><br>
  9799. DbRep Rep.STP5000.etotal -> data ignored while calc diffValue due to threshold overrun (diffAccept = 10): <br>
  9800. 2016-04-09 08:50:50 0.0340 -> 2016-04-09 12:42:01 13.3440 <br><br>
  9801. # The first dataset with a value of 0.0340 is untypical low compared to the next value of 13.3440 and results a untypical
  9802. high difference value. <br>
  9803. # Now you have to decide if the (second) dataset should be deleted, ignored of the attribute diffAccept should be adjusted.
  9804. </ul><br>
  9805. <a name="disable"></a>
  9806. <li><b>disable </b> - deactivates the module </li> <br>
  9807. <a name="dumpComment"></a>
  9808. <li><b>dumpComment </b> - User-comment. It will be included in the header of the created dumpfile by
  9809. command "dumpMySQL clientSide". </li> <br>
  9810. <a name="dumpCompress"></a>
  9811. <li><b>dumpCompress </b> - if set, the dump files are compressed after operation of "dumpMySQL" bzw. "dumpSQLite" </li> <br>
  9812. <a name="dumpDirLocal"></a>
  9813. <li><b>dumpDirLocal </b> - Target directory of database dumps by command "dumpMySQL clientSide"
  9814. (default: "{global}{modpath}/log/" on the FHEM-Server). <br>
  9815. In this directory also the internal version administration searches for old backup-files
  9816. and deletes them if the number exceeds attribute "dumpFilesKeep".
  9817. The attribute is also relevant to publish a local mounted directory "dumpDirRemote" to
  9818. DbRep. </li> <br>
  9819. <a name="dumpDirRemote"></a>
  9820. <li><b>dumpDirRemote </b> - Target directory of database dumps by command "dumpMySQL serverSide"
  9821. (default: the Home-directory of MySQL-Server on the MySQL-Host). </li> <br>
  9822. <a name="dumpMemlimit"></a>
  9823. <li><b>dumpMemlimit </b> - tolerable memory consumption for the SQL-script during generation period (default: 100000 characters).
  9824. Please adjust this parameter if you may notice memory bottlenecks and performance problems based
  9825. on it on your specific hardware. </li> <br>
  9826. <a name="dumpSpeed"></a>
  9827. <li><b>dumpSpeed </b> - Number of Lines which will be selected in source database with one select by dump-command
  9828. "dumpMySQL ClientSide" (default: 10000).
  9829. This parameter impacts the run-time and consumption of resources directly. </li> <br>
  9830. <a name="dumpFilesKeep"></a>
  9831. <li><b>dumpFilesKeep </b> - The specified number of dumpfiles remain in the dump directory (default: 3).
  9832. If there more (older) files has been found, these files will be deleted after a new database dump
  9833. was created successfully.
  9834. The global attrubute "archivesort" will be considered. </li> <br>
  9835. <a name="executeAfterProc"></a>
  9836. <li><b>executeAfterProc </b> - you can specify a FHEM command or perl function which should be executed
  9837. <b>after command execution</b>. <br>
  9838. Perl functions have to be enclosed in {} .<br><br>
  9839. <ul>
  9840. <b>Example:</b> <br><br>
  9841. attr &lt;name&gt; executeAfterProc set og_gz_westfenster off; <br>
  9842. attr &lt;name&gt; executeAfterProc {adump ("&lt;name&gt;")} <br><br>
  9843. # "adump" is a function defined in 99_myUtils.pm e.g.: <br>
  9844. <pre>
  9845. sub adump {
  9846. my ($name) = @_;
  9847. my $hash = $defs{$name};
  9848. # own function, e.g.
  9849. Log3($name, 3, "DbRep $name -> Dump finished");
  9850. return;
  9851. }
  9852. </pre>
  9853. </ul>
  9854. </li>
  9855. <a name="executeBeforeProc"></a>
  9856. <li><b>executeBeforeProc </b> - you can specify a FHEM command or perl function which should be executed
  9857. <b>before command execution</b>. <br>
  9858. Perl functions have to be enclosed in {} .<br><br>
  9859. <ul>
  9860. <b>Example:</b> <br><br>
  9861. attr &lt;name&gt; executeBeforeProc set og_gz_westfenster on; <br>
  9862. attr &lt;name&gt; executeBeforeProc {bdump ("&lt;name&gt;")} <br><br>
  9863. # "bdump" is a function defined in 99_myUtils.pm e.g.: <br>
  9864. <pre>
  9865. sub bdump {
  9866. my ($name) = @_;
  9867. my $hash = $defs{$name};
  9868. # own function, e.g.
  9869. Log3($name, 3, "DbRep $name -> Dump starts now");
  9870. return;
  9871. }
  9872. </pre>
  9873. </ul>
  9874. </li>
  9875. <a name="expimpfile"></a>
  9876. <li><b>expimpfile </b> - Path/filename for data export/import. <br><br>
  9877. The filename may contain wildcards which are replaced by corresponding values
  9878. (see subsequent table).
  9879. Furthermore filename can contain %-wildcards of the POSIX strftime function of the underlying OS (see your
  9880. strftime manual). <br>
  9881. <ul>
  9882. <table>
  9883. <colgroup> <col width=5%> <col width=95%> </colgroup>
  9884. <tr><td> %L </td><td>: is replaced by the value of global logdir attribute </td></tr>
  9885. <tr><td> %TSB </td><td>: is replaced by the (calculated) value of the timestamp_begin attribute </td></tr>
  9886. <tr><td> </td><td> </td></tr>
  9887. <tr><td> </td><td> <b>Common used POSIX-wildcards are:</b> </td></tr>
  9888. <tr><td> %d </td><td>: day of month (01..31) </td></tr>
  9889. <tr><td> %m </td><td>: month (01..12) </td></tr>
  9890. <tr><td> %Y </td><td>: year (1970...) </td></tr>
  9891. <tr><td> %w </td><td>: day of week (0..6); 0 represents Sunday </td></tr>
  9892. <tr><td> %j </td><td>: day of year (001..366) </td></tr>
  9893. <tr><td> %U </td><td>: week number of year with Sunday as first day of week (00..53) </td></tr>
  9894. <tr><td> %W </td><td>: week number of year with Monday as first day of week (00..53) </td></tr>
  9895. </table>
  9896. </ul>
  9897. </li> <br>
  9898. <ul>
  9899. <b>Examples:</b> <br>
  9900. <code>attr &lt;name&gt; expimpfile /sds1/backup/exptest_%TSB.csv </code> <br>
  9901. <code>attr &lt;name&gt; expimpfile /sds1/backup/exptest_%Y-%m-%d.csv </code> <br>
  9902. </ul>
  9903. <br>
  9904. <a name="DbRep_expimpfile"></a>
  9905. About POSIX wildcard usage please see also explanations in
  9906. <a href="https://fhem.de/commandref.html#FileLog">Filelog</a>. <br>
  9907. <br><br>
  9908. <a name="fastStart"></a>
  9909. <li><b>fastStart </b> - Usually every DbRep device is making a short connect to its database when FHEM is started to
  9910. retrieve some important informations and the reading "state" switches to "connected" on success.
  9911. If this attrbute is set, the initial database connection is executed not till then the
  9912. DbRep device is processing its first command. <br>
  9913. While the reading "state" is remaining in state "initialized" after FHEM-start.
  9914. This approach my be helpful if a lot of DbRep devices are defined.
  9915. </li> <br>
  9916. <a name="fetchMarkDuplicates"></a>
  9917. <li><b>fetchMarkDuplicates </b>
  9918. - Highlighting of multiple occuring datasets in result of "fetchrows" command </li> <br>
  9919. <a name="fetchRoute"></a>
  9920. <li><b>fetchRoute [descent | ascent] </b> - specify the direction of data selection of the fetchrows-command. <br><br>
  9921. <ul>
  9922. <b>descent</b> - the data are read descent (default). If
  9923. amount of datasets specified by attribut "limit" is exceeded,
  9924. the newest x datasets are shown. <br><br>
  9925. <b>ascent</b> - the data are read ascent . If
  9926. amount of datasets specified by attribut "limit" is exceeded,
  9927. the oldest x datasets are shown. <br>
  9928. </ul>
  9929. </li> <br><br>
  9930. <a name="ftpUse"></a>
  9931. <li><b>ftpUse </b> - FTP Transfer after dump will be switched on (without SSL encoding). The created
  9932. database backup file will be transfered non-blocking to the FTP-Server (Attribut "ftpServer").
  9933. </li> <br>
  9934. <a name="ftpUseSSL"></a>
  9935. <li><b>ftpUseSSL </b> - FTP Transfer with SSL encoding after dump. The created database backup file will be transfered
  9936. non-blocking to the FTP-Server (Attribut "ftpServer"). </li> <br>
  9937. <a name="ftpUser"></a>
  9938. <li><b>ftpUser </b> - User for FTP-server login, default: "anonymous". </li> <br>
  9939. <a name="ftpDebug"></a>
  9940. <li><b>ftpDebug </b> - debugging of FTP communication for diagnostics. </li> <br>
  9941. <a name="ftpDir"></a>
  9942. <li><b>ftpDir </b> - directory on FTP-server in which the file will be send into (default: "/"). </li> <br>
  9943. <a name="ftpDumpFilesKeep"></a>
  9944. <li><b>ftpDumpFilesKeep </b> - leave the number of dump files in FTP-destination &lt;ftpDir&gt; (default: 3). Are there more
  9945. (older) dump files present, these files are deleted after a new dump was transfered successfully. </li> <br>
  9946. <a name="ftpPassive"></a>
  9947. <li><b>ftpPassive </b> - set if passive FTP is to be used </li> <br>
  9948. <a name="ftpPort"></a>
  9949. <li><b>ftpPort </b> - FTP-Port, default: 21 </li> <br>
  9950. <a name="ftpPwd"></a>
  9951. <li><b>ftpPwd </b> - password of FTP-User, is not set by default </li> <br>
  9952. <a name="ftpServer"></a>
  9953. <li><b>ftpServer </b> - name or IP-address of FTP-server. <b>absolutely essential !</b> </li> <br>
  9954. <a name="ftpTimeout"></a>
  9955. <li><b>ftpTimeout </b> - timeout of FTP-connection in seconds (default: 30). </li> <br>
  9956. <a name="limit"></a>
  9957. <li><b>limit </b> - limits the number of selected datasets by the "fetchrows", or the shown datasets of "delSeqDoublets adviceDelete",
  9958. "delSeqDoublets adviceRemain" commands (default: 1000).
  9959. This limitation should prevent the browser session from overload and
  9960. avoids FHEMWEB from blocking. Please change the attribut according your requirements or change the
  9961. selection criteria (decrease evaluation period). </li> <br>
  9962. <a name="optimizeTablesBeforeDump"></a>
  9963. <li><b>optimizeTablesBeforeDump </b> - if set to "1", the database tables will be optimized before executing the dump
  9964. (default: 0).
  9965. Thereby the backup run-time time will be extended. <br><br>
  9966. <ul>
  9967. <b>Note</b> <br>
  9968. The table optimizing cause locking the tables and therefore to blocking of
  9969. FHEM if DbLog isn't working in asynchronous mode (DbLog-attribute "asyncMode") !
  9970. <br>
  9971. </ul>
  9972. </li> <br>
  9973. <a name="reading"></a>
  9974. <li><b>reading </b> - Selection of particular or several readings.
  9975. More than one reading can be specified by a comma separated list. <br>
  9976. If SQL wildcard (%) is set in a list, it will be evaluated as a normal ASCII-character. <br>
  9977. If the reading or the reading-list is prepended by "EXCLUDE=", those readings are excluded
  9978. from database selection.
  9979. </li> <br>
  9980. <ul>
  9981. <b>Examples:</b> <br>
  9982. <code>attr &lt;name&gt; reading etotal</code> <br>
  9983. <code>attr &lt;name&gt; reading et%</code> <br>
  9984. <code>attr &lt;name&gt; reading etotal,etoday</code> <br>
  9985. <code>attr &lt;name&gt; reading etotal,etoday EXCLUDE=state </code> <br>
  9986. <code>attr &lt;name&gt; reading etotal,etoday EXCLUDE=Einspeisung% </code> <br>
  9987. </ul>
  9988. <br><br>
  9989. <a name="readingNameMap"></a>
  9990. <li><b>readingNameMap </b> - the name of the analyzed reading can be overwritten for output </li> <br>
  9991. <a name="role"></a>
  9992. <li><b>role </b> - the role of the DbRep-device. Standard role is "Client". <br>
  9993. <a name="DbRep_role"></a>
  9994. The role "Agent" is described in section <a href="#DbRepAutoRename">DbRep-Agent</a>.
  9995. </li> <br>
  9996. <a name="readingPreventFromDel"></a>
  9997. <li><b>readingPreventFromDel </b> - comma separated list of readings which are should prevent from deletion when a
  9998. new operation starts </li> <br>
  9999. <a name="seqDoubletsVariance"></a>
  10000. <li><b>seqDoubletsVariance </b> - accepted variance (+/-) for the command "set &lt;name&gt; delSeqDoublets". <br>
  10001. The value of this attribute describes the variance up to it consecutive numeric values (VALUE) of
  10002. datasets are handled as identical and should be deleted. "seqDoubletsVariance" is an absolut numerical value,
  10003. which is used as a positive as well as a negative variance. </li> <br>
  10004. <ul>
  10005. <b>Examples:</b> <br>
  10006. <code>attr &lt;name&gt; seqDoubletsVariance 0.0014 </code> <br>
  10007. <code>attr &lt;name&gt; seqDoubletsVariance 1.45 </code> <br>
  10008. </ul>
  10009. <br><br>
  10010. <a name="showproctime"></a>
  10011. <li><b>showproctime </b> - if set, the reading "sql_processing_time" shows the required execution time (in seconds)
  10012. for the sql-requests. This is not calculated for a single sql-statement, but the summary
  10013. of all sql-statements necessara for within an executed DbRep-function in background. </li> <br>
  10014. <a name="showStatus"></a>
  10015. <li><b>showStatus </b> - limits the sample space of command "get &lt;name&gt; dbstatus". SQL-Wildcard (%) can be used. </li> <br>
  10016. <ul>
  10017. <b>Example: </b><br>
  10018. attr &lt;name&gt; showStatus %uptime%,%qcache% <br>
  10019. # Only readings with containing "uptime" and "qcache" in name will be shown <br>
  10020. </ul><br>
  10021. <a name="showVariables"></a>
  10022. <li><b>showVariables </b> - limits the sample space of command "get &lt;name&gt; dbvars". SQL-Wildcard (%) can be used. </li> <br>
  10023. <ul>
  10024. <b>Example: </b><br>
  10025. attr &lt;name&gt; showVariables %version%,%query_cache% <br>
  10026. # Only readings with containing "version" and "query_cache" in name will be shown <br>
  10027. </ul><br>
  10028. <a name="showSvrInfo"></a>
  10029. <li><b>showSvrInfo </b> - limits the sample space of command "get &lt;name&gt; svrinfo". SQL-Wildcard (%) can be used. </li> <br>
  10030. <ul>
  10031. <b>Example: </b><br>
  10032. attr &lt;name&gt; showSvrInfo %SQL_CATALOG_TERM%,%NAME% <br>
  10033. # Only readings with containing "SQL_CATALOG_TERM" and "NAME" in name will be shown <br>
  10034. </ul><br>
  10035. <a name="showTableInfo"></a>
  10036. <li><b>showTableInfo </b> - limits the tablename which is selected by command "get &lt;name&gt; tableinfo". SQL-Wildcard
  10037. (%) can be used. </li> <br>
  10038. <ul>
  10039. <b>Example: </b><br>
  10040. attr &lt;name&gt; showTableInfo current,history <br>
  10041. # Only informations about tables "current" and "history" will be shown <br>
  10042. </ul><br>
  10043. <a name="sqlCmdHistoryLength"></a>
  10044. <li><b>sqlCmdHistoryLength </b>
  10045. - activates the command history of "sqlCmd" and determines the length of it </li> <br>
  10046. <a name="sqlResultFieldSep"></a>
  10047. <li><b>sqlResultFieldSep </b> - determines the used field separator (default: "|") in the result of some sql-commands. </li> <br>
  10048. <a name="sqlResultFormat"></a>
  10049. <li><b>sqlResultFormat </b> - determines the formatting of the "set &lt;name&gt; sqlCmd" command result.
  10050. Possible options are: <br><br>
  10051. <ul>
  10052. <b>separated </b> - every line of the result will be generated sequentially in a single
  10053. reading. (default) <br><br>
  10054. <b>mline </b> - the result will be generated as multiline in
  10055. Reading SqlResult.
  10056. <br><br>
  10057. <b>sline </b> - the result will be generated as singleline in
  10058. Reading SqlResult.
  10059. Datasets are separated by "]|[". <br><br>
  10060. <b>table </b> - the result will be generated as an table in
  10061. Reading SqlResult. <br><br>
  10062. <b>json </b> - creates the Reading SqlResult as a JSON
  10063. coded hash.
  10064. Every hash-element consists of the serial number of the dataset (key)
  10065. and its value. </li> <br><br>
  10066. To process the result, you may use a userExitFn in 99_myUtils for example: <br>
  10067. <pre>
  10068. sub resfromjson {
  10069. my ($name,$reading,$value) = @_;
  10070. my $hash = $defs{$name};
  10071. if ($reading eq "SqlResult") {
  10072. # only reading SqlResult contains JSON encoded data
  10073. my $data = decode_json($value);
  10074. foreach my $k (keys(%$data)) {
  10075. # use your own processing from here for every hash-element
  10076. # e.g. output of every element that contains "Cam"
  10077. my $ke = $data->{$k};
  10078. if($ke =~ m/Cam/i) {
  10079. my ($res1,$res2) = split("\\|", $ke);
  10080. Log3($name, 1, "$name - extract element $k by userExitFn: ".$res1." ".$res2);
  10081. }
  10082. }
  10083. }
  10084. return;
  10085. }
  10086. </pre>
  10087. </ul>
  10088. <br>
  10089. <a name="timeYearPeriod"></a>
  10090. <li><b>timeYearPeriod </b> - By this attribute an annual time period will be determined for database data selection.
  10091. The time limits are calculated dynamically during execution time. Every time an annual period is determined.
  10092. Periods of less than a year are not possible to set. <br>
  10093. This attribute is particularly intended to make reports synchronous to an account period, e.g. of an energy- or gas provider.
  10094. </li> <br>
  10095. <ul>
  10096. <b>Example:</b> <br><br>
  10097. attr &lt;name&gt; timeYearPeriod 06-25 06-24 <br><br>
  10098. # evaluates the database within the time limits 25. june AAAA and 24. june BBBB. <br>
  10099. # The year AAAA respectively year BBBB is calculated dynamically depending of the current date. <br>
  10100. # If the current date >= 25. june and =< 31. december, than AAAA = current year and BBBB = current year+1 <br>
  10101. # If the current date >= 01. january und =< 24. june, than AAAA = current year-1 and BBBB = current year
  10102. </ul>
  10103. <br><br>
  10104. <a name="timestamp_begin"></a>
  10105. <li><b>timestamp_begin </b> - begin of data selection </li> <br>
  10106. The format of timestamp is as used with DbLog "YYYY-MM-DD HH:MM:SS". For the attributes "timestamp_begin", "timestamp_end"
  10107. you can also use one of the following entries. The timestamp-attribute will be dynamically set to: <br><br>
  10108. <ul>
  10109. <b>current_year_begin</b> : matches "&lt;current year&gt;-01-01 00:00:00" <br>
  10110. <b>current_year_end</b> : matches "&lt;current year&gt;-12-31 23:59:59" <br>
  10111. <b>previous_year_begin</b> : matches "&lt;previous year&gt;-01-01 00:00:00" <br>
  10112. <b>previous_year_end</b> : matches "&lt;previous year&gt;-12-31 23:59:59" <br>
  10113. <b>current_month_begin</b> : matches "&lt;current month first day&gt; 00:00:00" <br>
  10114. <b>current_month_end</b> : matches "&lt;current month last day&gt; 23:59:59" <br>
  10115. <b>previous_month_begin</b> : matches "&lt;previous month first day&gt; 00:00:00" <br>
  10116. <b>previous_month_end</b> : matches "&lt;previous month last day&gt; 23:59:59" <br>
  10117. <b>current_week_begin</b> : matches "&lt;first day of current week&gt; 00:00:00" <br>
  10118. <b>current_week_end</b> : matches "&lt;last day of current week&gt; 23:59:59" <br>
  10119. <b>previous_week_begin</b> : matches "&lt;first day of previous week&gt; 00:00:00" <br>
  10120. <b>previous_week_end</b> : matches "&lt;last day of previous week&gt; 23:59:59" <br>
  10121. <b>current_day_begin</b> : matches "&lt;current day&gt; 00:00:00" <br>
  10122. <b>current_day_end</b> : matches "&lt;current day&gt; 23:59:59" <br>
  10123. <b>previous_day_begin</b> : matches "&lt;previous day&gt; 00:00:00" <br>
  10124. <b>previous_day_end</b> : matches "&lt;previous day&gt; 23:59:59" <br>
  10125. <b>current_hour_begin</b> : matches "&lt;current hour&gt;:00:00" <br>
  10126. <b>current_hour_end</b> : matches "&lt;current hour&gt;:59:59" <br>
  10127. <b>previous_hour_begin</b> : matches "&lt;previous hour&gt;:00:00" <br>
  10128. <b>previous_hour_end</b> : matches "&lt;previous hour&gt;:59:59" <br>
  10129. </ul>
  10130. <br><br>
  10131. <a name="timestamp_end"></a>
  10132. <li><b>timestamp_end </b> - end of data selection. If not set the current date/time combination will be used. </li> <br>
  10133. The format of timestamp is as used with DbLog "YYYY-MM-DD HH:MM:SS". For the attributes "timestamp_begin", "timestamp_end"
  10134. you can also use one of the following entries. The timestamp-attribute will be dynamically set to: <br><br>
  10135. <ul>
  10136. <b>current_year_begin</b> : matches "&lt;current year&gt;-01-01 00:00:00" <br>
  10137. <b>current_year_end</b> : matches "&lt;current year&gt;-12-31 23:59:59" <br>
  10138. <b>previous_year_begin</b> : matches "&lt;previous year&gt;-01-01 00:00:00" <br>
  10139. <b>previous_year_end</b> : matches "&lt;previous year&gt;-12-31 23:59:59" <br>
  10140. <b>current_month_begin</b> : matches "&lt;current month first day&gt; 00:00:00" <br>
  10141. <b>current_month_end</b> : matches "&lt;current month last day&gt; 23:59:59" <br>
  10142. <b>previous_month_begin</b> : matches "&lt;previous month first day&gt; 00:00:00" <br>
  10143. <b>previous_month_end</b> : matches "&lt;previous month last day&gt; 23:59:59" <br>
  10144. <b>current_week_begin</b> : matches "&lt;first day of current week&gt; 00:00:00" <br>
  10145. <b>current_week_end</b> : matches "&lt;last day of current week&gt; 23:59:59" <br>
  10146. <b>previous_week_begin</b> : matches "&lt;first day of previous week&gt; 00:00:00" <br>
  10147. <b>previous_week_end</b> : matches "&lt;last day of previous week&gt; 23:59:59" <br>
  10148. <b>current_day_begin</b> : matches "&lt;current day&gt; 00:00:00" <br>
  10149. <b>current_day_end</b> : matches "&lt;current day&gt; 23:59:59" <br>
  10150. <b>previous_day_begin</b> : matches "&lt;previous day&gt; 00:00:00" <br>
  10151. <b>previous_day_end</b> : matches "&lt;previous day&gt; 23:59:59" <br>
  10152. <b>current_hour_begin</b> : matches "&lt;current hour&gt;:00:00" <br>
  10153. <b>current_hour_end</b> : matches "&lt;current hour&gt;:59:59" <br>
  10154. <b>previous_hour_begin</b> : matches "&lt;previous hour&gt;:00:00" <br>
  10155. <b>previous_hour_end</b> : matches "&lt;previous hour&gt;:59:59" <br> </ul><br>
  10156. Make sure that "timestamp_begin" < "timestamp_end" is fulfilled. <br><br>
  10157. <ul>
  10158. <b>Example:</b> <br><br>
  10159. attr &lt;name&gt; timestamp_begin current_year_begin <br>
  10160. attr &lt;name&gt; timestamp_end current_year_end <br><br>
  10161. # Analyzes the database between the time limits of the current year. <br>
  10162. </ul>
  10163. <br><br>
  10164. <b>Note </b> <br>
  10165. If the attribute "timeDiffToNow" will be set, the attributes "timestamp_begin" respectively "timestamp_end" will be deleted if they were set before.
  10166. The setting of "timestamp_begin" respectively "timestamp_end" causes the deletion of attribute "timeDiffToNow" if it was set before as well.
  10167. <br><br>
  10168. <a name="timeDiffToNow"></a>
  10169. <li><b>timeDiffToNow </b> - the <b>begin time </b> of data selection will be set to the timestamp <b>"&lt;current time&gt; -
  10170. &lt;timeDiffToNow&gt;"</b> dynamically (e.g. if set to 86400, the last 24 hours are considered by data
  10171. selection). The time period will be calculated dynamically at execution time. </li> <br>
  10172. <ul>
  10173. <b>Examples for input format:</b> <br>
  10174. <code>attr &lt;name&gt; timeDiffToNow 86400</code> <br>
  10175. # the start time is set to "current time - 86400 seconds" <br>
  10176. <code>attr &lt;name&gt; timeDiffToNow d:2 h:3 m:2 s:10</code> <br>
  10177. # the start time is set to "current time - 2 days 3 hours 2 minutes 10 seconds" <br>
  10178. <code>attr &lt;name&gt; timeDiffToNow m:600</code> <br>
  10179. # the start time is set to "current time - 600 minutes" gesetzt <br>
  10180. <code>attr &lt;name&gt; timeDiffToNow h:2.5</code> <br>
  10181. # the start time is set to "current time - 2,5 hours" <br>
  10182. <code>attr &lt;name&gt; timeDiffToNow y:1 h:2.5</code> <br>
  10183. # the start time is set to "current time - 1 year and 2,5 hours" <br>
  10184. <code>attr &lt;name&gt; timeDiffToNow y:1.5</code> <br>
  10185. # the start time is set to "current time - 1.5 years" <br>
  10186. </ul>
  10187. <br>
  10188. If both attributes "timeDiffToNow" and "timeOlderThan" are set, the selection
  10189. period will be calculated between of these timestamps dynamically.
  10190. <br><br>
  10191. <a name="timeOlderThan"></a>
  10192. <li><b>timeOlderThan </b> - the <b>end time</b> of data selection will be set to the timestamp <b>"&lt;aktuelle Zeit&gt; -
  10193. &lt;timeOlderThan&gt;"</b> dynamically. Always the datasets up to timestamp
  10194. "&lt;current time&gt; - &lt;timeOlderThan&gt;" will be considered (e.g. if set to
  10195. 86400, all datasets older than one day are considered). The time period will be calculated dynamically at
  10196. execution time. </li> <br>
  10197. <ul>
  10198. <b>Examples for input format:</b> <br>
  10199. <code>attr &lt;name&gt; timeOlderThan 86400</code> <br>
  10200. # the selection end time is set to "current time - 86400 seconds" <br>
  10201. <code>attr &lt;name&gt; timeOlderThan d:2 h:3 m:2 s:10</code> <br>
  10202. # the selection end time is set to "current time - 2 days 3 hours 2 minutes 10 seconds" <br>
  10203. <code>attr &lt;name&gt; timeOlderThan m:600</code> <br>
  10204. # the selection end time is set to "current time - 600 minutes" gesetzt <br>
  10205. <code>attr &lt;name&gt; timeOlderThan h:2.5</code> <br>
  10206. # the selection end time is set to "current time - 2,5 hours" <br>
  10207. <code>attr &lt;name&gt; timeOlderThan y:1 h:2.5</code> <br>
  10208. # the selection end time is set to "current time - 1 year and 2,5 hours" <br>
  10209. <code>attr &lt;name&gt; timeOlderThan y:1.5</code> <br>
  10210. # the selection end time is set to "current time - 1.5 years" <br>
  10211. </ul>
  10212. <br>
  10213. If both attributes "timeDiffToNow" and "timeOlderThan" are set, the selection
  10214. period will be calculated between of these timestamps dynamically.
  10215. <br><br>
  10216. <a name="timeout"></a>
  10217. <li><b>timeout </b> - set the timeout-value for Blocking-Call Routines in background in seconds (default 86400) </li> <br>
  10218. <a name="userExitFn"></a>
  10219. <li><b>userExitFn </b> - provides an interface to execute user specific program code. <br>
  10220. To activate the interfaace at first you should implement the subroutine which will be
  10221. called by the interface in your 99_myUtls.pm as shown in by the example: <br>
  10222. <pre>
  10223. sub UserFunction {
  10224. my ($name,$reading,$value) = @_;
  10225. my $hash = $defs{$name};
  10226. ...
  10227. # e.g. output transfered data
  10228. Log3 $name, 1, "UserExitFn $name called - transfer parameter are Reading: $reading, Value: $value " ;
  10229. ...
  10230. return;
  10231. }
  10232. </pre>
  10233. The interface activation takes place by setting the subroutine name into the attribute.
  10234. Optional you may set a Reading:Value combination (Regex) as argument. If no Regex is
  10235. specified, all value combinations will be evaluated as "true" (related to .*:.*).
  10236. <br><br>
  10237. <ul>
  10238. <b>Example:</b> <br>
  10239. attr <device> userExitFn UserFunction .*:.* <br>
  10240. # "UserFunction" is the name of subroutine in 99_myUtils.pm.
  10241. </ul>
  10242. <br>
  10243. The interface works generally without and independent from Events.
  10244. If the attribute is set, after every reading generation the Regex will be evaluated.
  10245. If the evaluation was "true", set subroutine will be called.
  10246. For further processing following parameters will be forwarded to the function: <br><br>
  10247. <ul>
  10248. <li>$name - the name of the DbRep-Device </li>
  10249. <li>$reading - the name of the created reading </li>
  10250. <li>$value - the value of the reading </li>
  10251. </ul>
  10252. </li>
  10253. <br><br>
  10254. <a name="valueFilter"></a>
  10255. <li><b>valueFilter </b> - Regular expression (REGEXP) to filter datasets within particular functions. The REGEXP is
  10256. applied to a particular field or to the whole selected dataset (inclusive Device, Reading and
  10257. so on).
  10258. Please consider the explanations within the set-commands. Further information is available
  10259. with command "get &lt;name&gt; versionNotes 4". </li> <br>
  10260. </ul>
  10261. </ul></ul>
  10262. <a name="DbRepReadings"></a>
  10263. <b>Readings</b>
  10264. <br>
  10265. <ul>
  10266. Regarding to the selected operation the reasults will be shown as readings. At the beginning of a new operation all old readings will be deleted to avoid
  10267. that unsuitable or invalid readings would remain.<br><br>
  10268. In addition the following readings will be created: <br><br>
  10269. <ul><ul>
  10270. <li><b>state </b> - contains the current state of evaluation. If warnings are occured (state = Warning) compare Readings
  10271. "diff_overrun_limit_&lt;diffLimit&gt;" and "less_data_in_period" </li> <br>
  10272. <li><b>errortext </b> - description about the reason of an error state </li> <br>
  10273. <li><b>background_processing_time </b> - the processing time spent for operations in background/forked operation </li> <br>
  10274. <li><b>sql_processing_time </b> - the processing time wasted for all sql-statements used for an operation </li> <br>
  10275. <li><b>diff_overrun_limit_&lt;diffLimit&gt;</b> - contains a list of pairs of datasets which have overrun the threshold (&lt;diffLimit&gt;)
  10276. of calculated difference each other determined by attribute "diffAccept" (default=20). </li> <br>
  10277. <li><b>less_data_in_period </b> - contains a list of time periods within only one dataset was found. The difference calculation considers
  10278. the last value of the aggregation period before the current one. Valid for function "diffValue". </li> <br>
  10279. <li><b>SqlResult </b> - result of the last executed sqlCmd-command. The formatting can be specified
  10280. by <a href="#DbRepattr">attribute</a> "sqlResultFormat" </li> <br>
  10281. <li><b>sqlCmd </b> - contains the last executed sqlCmd-command </li> <br>
  10282. </ul></ul>
  10283. <br><br>
  10284. </ul>
  10285. <a name="DbRepAutoRename"></a>
  10286. <b>DbRep Agent - automatic change of device names in databases and DbRep-definitions after FHEM "rename" command</b>
  10287. <br>
  10288. <ul>
  10289. By the attribute "role" the role of DbRep-device will be configured. The standard role is "Client". If the role has changed to "Agent", the DbRep device
  10290. react automatically on renaming devices in your FHEM installation. The DbRep device is now called DbRep-Agent. <br><br>
  10291. By the DbRep-Agent the following features are activated when a FHEM-device has being renamed: <br><br>
  10292. <ul><ul>
  10293. <li> in the database connected to the DbRep-Agent (Internal Database) dataset containing the old device name will be searched and renamed to the
  10294. to the new device name in <b>all</b> affected datasets. </li> <br>
  10295. <li> in the DbLog-Device assigned to the DbRep-Agent the definition will be changed to substitute the old device name by the new one. Thereby the logging of
  10296. the renamed device will be going on in the database. </li> <br>
  10297. <li> in other existing DbRep-definitions with Type "Client" a possibly set attribute "device = old device name" will be changed to "device = new device name".
  10298. Because of that, reporting definitions will be kept consistent automatically if devices are renamed in FHEM. </li> <br>
  10299. </ul></ul>
  10300. The following restrictions take place if a DbRep device was changed to an Agent by setting attribute "role" to "Agent". These conditions will be activated
  10301. and checked: <br><br>
  10302. <ul><ul>
  10303. <li> within a FHEM installation only one DbRep-Agent can be configured for every defined DbLog-database. That means, if more than one DbLog-database is present,
  10304. you could define same numbers of DbRep-Agents as well as DbLog-devices are defined. </li> <br>
  10305. <li> after changing to DbRep-Agent role only the set-command "renameDevice" will be available and as well as a reduced set of module specific attributes will be
  10306. permitted. If a DbRep-device of privious type "Client" has changed an Agent, furthermore not permitted attributes will be deleted if set. </li> <br>
  10307. </ul></ul>
  10308. All activities like database changes and changes of other DbRep-definitions will be logged in FHEM Logfile with verbose=3. In order that the renameDevice
  10309. function don't running into timeout set the timeout attribute to an appropriate value, especially if there are databases with huge datasets to evaluate.
  10310. As well as all the other database operations of this module, the autorename operation will be executed nonblocking. <br><br>
  10311. <ul>
  10312. <b>Example </b> of definition of a DbRep-device as an Agent: <br><br>
  10313. <code>
  10314. define Rep.Agent DbRep LogDB <br>
  10315. attr Rep.Agent devStateIcon connected:10px-kreis-gelb .*disconnect:10px-kreis-rot .*done:10px-kreis-gruen <br>
  10316. attr Rep.Agent icon security <br>
  10317. attr Rep.Agent role Agent <br>
  10318. attr Rep.Agent room DbLog <br>
  10319. attr Rep.Agent showproctime 1 <br>
  10320. attr Rep.Agent stateFormat { ReadingsVal("$name","state", undef) eq "running" ? "renaming" : ReadingsVal("$name","state", undef). " &raquo;; ProcTime: ".ReadingsVal("$name","sql_processing_time", undef)." sec"} <br>
  10321. attr Rep.Agent timeout 86400 <br>
  10322. </code>
  10323. <br>
  10324. </ul>
  10325. <b>Note:</b> <br>
  10326. Even though the function itself is designed non-blocking, make sure the assigned DbLog-device
  10327. is operating in asynchronous mode to avoid FHEMWEB from blocking. <br><br>
  10328. </ul>
  10329. =end html
  10330. =begin html_DE
  10331. <a name="DbRep"></a>
  10332. <h3>DbRep</h3>
  10333. <ul>
  10334. <br>
  10335. Zweck des Moduls ist es, den Inhalt von DbLog-Datenbanken nach bestimmten Kriterien zu durchsuchen, zu managen, das Ergebnis hinsichtlich verschiedener
  10336. Aggregationen auszuwerten und als Readings darzustellen. Die Abgrenzung der zu berücksichtigenden Datenbankinhalte erfolgt durch die Angabe von Device, Reading und
  10337. die Zeitgrenzen für Auswertungsbeginn bzw. Auswertungsende. <br><br>
  10338. Fast alle Datenbankoperationen werden nichtblockierend ausgeführt. Auf Ausnahmen wird hingewiesen.
  10339. Die Ausführungszeit der (SQL)-Hintergrundoperationen kann optional ebenfalls als Reading bereitgestellt
  10340. werden (siehe <a href="#DbRepattr">Attribute</a>). <br>
  10341. Alle vorhandenen Readings werden vor einer neuen Operation gelöscht. Durch das Attribut "readingPreventFromDel" kann eine Komma separierte Liste von Readings
  10342. angegeben werden die nicht gelöscht werden sollen. <br><br>
  10343. Aktuell werden folgende Operationen unterstützt: <br><br>
  10344. <ul><ul>
  10345. <li> Selektion aller Datensätze innerhalb einstellbarer Zeitgrenzen </li>
  10346. <li> Darstellung der Datensätze einer Device/Reading-Kombination innerhalb einstellbarer Zeitgrenzen. </li>
  10347. <li> Selektion der Datensätze unter Verwendung von dynamisch berechneter Zeitgrenzen zum Ausführungszeitpunkt. </li>
  10348. <li> Dubletten-Hervorhebung bei Datensatzanzeige (fetchrows) </li>
  10349. <li> Berechnung der Anzahl von Datensätzen einer Device/Reading-Kombination unter Berücksichtigung von Zeitgrenzen
  10350. und verschiedenen Aggregationen. </li>
  10351. <li> Die Berechnung von Summen-, Differenz-, Maximum-, Minimum- und Durchschnittswerten numerischer Readings
  10352. in Zeitgrenzen und verschiedenen Aggregationen. </li>
  10353. <li> Speichern von Summen-, Differenz- , Maximum- , Minimum- und Durchschnittswertberechnungen in der Datenbank </li>
  10354. <li> Löschung von Datensätzen. Die Eingrenzung der Löschung kann durch Device und/oder Reading sowie fixer oder
  10355. dynamisch berechneter Zeitgrenzen zum Ausführungszeitpunkt erfolgen. </li>
  10356. <li> Export von Datensätzen in ein File im CSV-Format </li>
  10357. <li> Import von Datensätzen aus File im CSV-Format </li>
  10358. <li> Umbenennen von Device/Readings in Datenbanksätzen </li>
  10359. <li> Ändern von Reading-Werten (VALUES) in der Datenbank (changeValue) </li>
  10360. <li> automatisches Umbenennen von Device-Namen in Datenbanksätzen und DbRep-Definitionen nach FHEM "rename"
  10361. Befehl (siehe <a href="#DbRepAutoRename">DbRep-Agent</a>) </li>
  10362. <li> Ausführen von beliebigen Benutzer spezifischen SQL-Kommandos (non-blocking) </li>
  10363. <li> Ausführen von beliebigen Benutzer spezifischen SQL-Kommandos (blocking) zur Verwendung in eigenem Code (dbValue) </li>
  10364. <li> Backups der FHEM-Datenbank im laufenden Betrieb erstellen (MySQL, SQLite) </li>
  10365. <li> senden des Dumpfiles zu einem FTP-Server nach dem Backup incl. Versionsverwaltung </li>
  10366. <li> Restore von SQLite- und MySQL-Dumps </li>
  10367. <li> Optimierung der angeschlossenen Datenbank (optimizeTables, vacuum) </li>
  10368. <li> Ausgabe der existierenden Datenbankprozesse (MySQL) </li>
  10369. <li> leeren der current-Tabelle </li>
  10370. <li> Auffüllen der current-Tabelle mit einem (einstellbaren) Extrakt der history-Tabelle</li>
  10371. <li> Bereinigung sequentiell aufeinander folgender Datensätze mit unterschiedlichen Zeitstempel aber gleichen Werten (sequentielle Dublettenbereinigung) </li>
  10372. <li> Reparatur einer korrupten SQLite Datenbank ("database disk image is malformed") </li>
  10373. <li> Übertragung von Datensätzen aus der Quelldatenbank in eine andere (Standby) Datenbank (syncStandby) </li>
  10374. <li> Reduktion der Anzahl von Datensätzen in der Datenbank (reduceLog) </li>
  10375. </ul></ul>
  10376. <br>
  10377. Zur Aktivierung der Funktion <b>Autorename</b> wird dem definierten DbRep-Device mit dem Attribut "role" die Rolle "Agent" zugewiesen. Die Standardrolle nach Definition
  10378. ist "Client". Mehr ist dazu im Abschnitt <a href="#DbRepAutoRename">DbRep-Agent</a> beschrieben. <br><br>
  10379. DbRep stellt dem Nutzer einen <b>UserExit</b> zur Verfügung. Über diese Schnittstelle kann der Nutzer in Abhängigkeit von
  10380. frei definierbaren Reading/Value-Kombinationen (Regex) eigenen Code zur Ausführung bringen. Diese Schnittstelle arbeitet
  10381. unabhängig von einer Eventgenerierung. Weitere Informationen dazu ist unter <a href="#DbRepattr">Attribut</a>
  10382. "userExitFn" beschrieben. <br><br>
  10383. Sobald ein DbRep-Device definiert ist, wird die Funktion <b>DbReadingsVal</b> zur Verfügung gestellt.
  10384. Mit dieser Funktion läßt sich, ähnlich dem allgemeinen ReadingsVal, der Wert eines Readings aus der Datenbank abrufen.
  10385. Die Funktionsausführung erfolgt blockierend.
  10386. Die Befehlssyntax ist: <br><br>
  10387. <ul>
  10388. <code>DbReadingsVal("&lt;name&gt;","&lt;device:reading&gt;","&lt;timestamp&gt;","&lt;default&gt;") </code> <br><br>
  10389. <b>Beispiele: </b><br>
  10390. $ret = DbReadingsVal("Rep.LogDB1","MyWetter:temperature","2018-01-13 08:00:00",""); <br>
  10391. attr &lt;name&gt; userReadings oldtemp {DbReadingsVal("Rep.LogDB1","MyWetter:temperature","2018-04-13 08:00:00","")}
  10392. <br><br>
  10393. <table>
  10394. <colgroup> <col width=5%> <col width=95%> </colgroup>
  10395. <tr><td> <b>&lt;name&gt;</b> </td><td>: Name des abzufragenden DbRep-Device </td></tr>
  10396. <tr><td> <b>&lt;device:reading&gt;</b> </td><td>: Device:Reading dessen Wert geliefert werden soll </td></tr>
  10397. <tr><td> <b>&lt;timestamp&gt;</b> </td><td>: Zeitpunkt des zu liefernden Readingwertes (*) in der Form "YYYY-MM-DD hh:mm:ss" </td></tr>
  10398. <tr><td> <b>&lt;default&gt;</b> </td><td>: Defaultwert falls kein Readingwert ermittelt werden konnte </td></tr>
  10399. </table>
  10400. </ul>
  10401. <br>
  10402. (*) Es wird der zeitlich zu &lt;timestamp&gt; passendste Readingwert zurück geliefert, falls kein Wert exakt zu dem
  10403. angegebenen Zeitpunkt geloggt wurde.
  10404. <br><br>
  10405. FHEM-Forum: <br>
  10406. <a href="https://forum.fhem.de/index.php/topic,53584.msg452567.html#msg452567">Modul 93_DbRep - Reporting und Management von Datenbankinhalten (DbLog)</a>. <br><br>
  10407. FHEM-Wiki: <br>
  10408. <a href="https://wiki.fhem.de/wiki/DbRep_-_Reporting_und_Management_von_DbLog-Datenbankinhalten">DbRep - Reporting und Management von DbLog-Datenbankinhalten</a>. <br><br>
  10409. <br>
  10410. </ul>
  10411. <b>Voraussetzungen </b> <br><br>
  10412. <ul>
  10413. Das Modul setzt den Einsatz einer oder mehrerer DbLog-Instanzen voraus. Es werden die Zugangsdaten dieser
  10414. Datenbankdefinition genutzt. <br>
  10415. Es werden nur Inhalte der Tabelle "history" berücksichtigt wenn nichts anderes beschrieben ist. <br><br>
  10416. Überblick welche anderen Perl-Module DbRep verwendet: <br><br>
  10417. Net::FTP (nur wenn FTP-Transfer nach Datenbank-Dump genutzt wird) <br>
  10418. Net::FTPSSL (nur wenn FTP-Transfer mit Verschlüsselung nach Datenbank-Dump genutzt wird) <br>
  10419. POSIX <br>
  10420. Time::HiRes <br>
  10421. Time::Local <br>
  10422. Scalar::Util <br>
  10423. DBI <br>
  10424. Color (FHEM-Modul) <br>
  10425. IO::Compress::Gzip <br>
  10426. IO::Uncompress::Gunzip <br>
  10427. Blocking (FHEM-Modul) <br><br>
  10428. Aus Performancegründen sollte zusätzlich folgender Index erstellt werden: <br>
  10429. <code>
  10430. CREATE INDEX Report_Idx ON `history` (TIMESTAMP, READING) USING BTREE;
  10431. </code>
  10432. </ul>
  10433. <br>
  10434. <a name="DbRepdefine"></a>
  10435. <b>Definition</b>
  10436. <br>
  10437. <ul>
  10438. <code>
  10439. define &lt;name&gt; DbRep &lt;Name der DbLog-Instanz&gt;
  10440. </code>
  10441. <br><br>
  10442. (&lt;Name der DbLog-Instanz&gt; - es wird der Name der auszuwertenden DBLog-Datenbankdefinition angegeben <b>nicht</b> der Datenbankname selbst)
  10443. </ul>
  10444. <br><br>
  10445. <a name="DbRepset"></a>
  10446. <b>Set </b>
  10447. <ul>
  10448. Zur Zeit gibt es folgende Set-Kommandos. Über sie werden die Auswertungen angestoßen und definieren selbst die Auswertungsvariante.
  10449. Nach welchen Kriterien die Datenbankinhalte durchsucht werden und die Aggregation erfolgt, wird durch <a href="#DbRepattr">Attribute</a> gesteuert.
  10450. <br><br>
  10451. <ul><ul>
  10452. <li><b> averageValue [display | writeToDB]</b>
  10453. - berechnet einen Durchschnittswert des Datenbankfelds "VALUE" in den
  10454. gegebenen Zeitgrenzen ( siehe <a href="#DbRepattr">Attribute</a>).
  10455. Es muss das auszuwertende Reading über das <a href="#DbRepattr">Attribut</a> "reading"
  10456. angegeben sein. <br>
  10457. Mit dem Attribut "averageCalcForm" wird die Berechnungsvariante zur Mittelwertermittlung definiert.
  10458. Ist keine oder die Option "display" angegeben, werden die Ergebnisse nur angezeigt. Mit
  10459. der Option "writeToDB" werden die Berechnungsergebnisse mit einem neuen Readingnamen
  10460. in der Datenbank gespeichert. <br>
  10461. Der neue Readingname wird aus einem Präfix und dem originalen Readingnamen gebildet,
  10462. wobei der originale Readingname durch das Attribut "readingNameMap" ersetzt werden kann.
  10463. Der Präfix setzt sich aus der Bildungsfunktion und der Aggregation zusammen. <br>
  10464. Der Timestamp der neuen Readings in der Datenbank wird von der eingestellten Aggregationsperiode
  10465. abgeleitet, sofern kein eindeutiger Zeitpunkt des Ergebnisses bestimmt werden kann.
  10466. Das Feld "EVENT" wird mit "calculated" gefüllt.<br><br>
  10467. <ul>
  10468. <b>Beispiel neuer Readingname gebildet aus dem Originalreading "totalpac":</b> <br>
  10469. avgam_day_totalpac <br>
  10470. # &lt;Bildungsfunktion&gt;_&lt;Aggregation&gt;_&lt;Originalreading&gt; <br>
  10471. </ul>
  10472. <br>
  10473. Zusammengefasst sind die zur Steuerung dieser Funktion relevanten Attribute: <br><br>
  10474. <ul>
  10475. <table>
  10476. <colgroup> <col width=5%> <col width=95%> </colgroup>
  10477. <tr><td> <b>aggregation</b> </td><td>: Auswahl einer Aggregationsperiode </td></tr>
  10478. <tr><td> <b>averageCalcForm</b> </td><td>: Auswahl der Berechnungsvariante für den Durchschnitt</td></tr>
  10479. <tr><td> <b>device</b> </td><td>: einschließen oder ausschließen von Datensätzen die &lt;device&gt; enthalten </td></tr>
  10480. <tr><td> <b>reading</b> </td><td>: einschließen oder ausschließen von Datensätzen die &lt;reading&gt; enthalten </td></tr>
  10481. <tr><td> <b>readingNameMap</b> </td><td>: die entstehenden Ergebnisreadings werden partiell umbenannt </td></tr>
  10482. <tr><td> <b>time.*</b> </td><td>: eine Reihe von Attributen zur Zeitabgrenzung </td></tr>
  10483. <tr><td style="vertical-align:top"> <b>valueFilter</b> <td>: ein zusätzliches REGEXP um die Datenselektion zu steuern. Der REGEXP wird auf das Datenbankfeld 'VALUE' angewendet. </td></tr>
  10484. </table>
  10485. </ul>
  10486. <br>
  10487. <br>
  10488. </li> <br>
  10489. <li><b> cancelDump </b> - bricht einen laufenden Datenbankdump ab. </li> <br>
  10490. <li><b> changeValue </b> - ändert den gespeicherten Wert eines Readings.
  10491. Ist die Selektion auf bestimmte Device/Reading-Kombinationen durch die
  10492. <a href="#DbRepattr">Attribute</a> "device" bzw. "reading" beschränkt, werden sie genauso
  10493. berücksichtigt wie gesetzte Zeitgrenzen (Attribute time.*). <br>
  10494. Fehlen diese Beschränkungen, wird die gesamte Datenbank durchsucht und der angegebene Wert
  10495. geändert. <br><br>
  10496. <ul>
  10497. <b>Syntax: </b> <br>
  10498. set &lt;name&gt; changeValue "&lt;alter String&gt;","&lt;neuer String&gt;" <br><br>
  10499. Die Strings werden in Doppelstrich eingeschlossen und durch Komma getrennt.
  10500. Dabei kann "String" sein: <br>
  10501. <table>
  10502. <colgroup> <col width=15%> <col width=85%> </colgroup>
  10503. <tr><td style="vertical-align:top"><b>&lt;old string&gt; :</b> <td><li>ein einfacher String mit/ohne Leerzeichen, z.B. "OL 12" </li>
  10504. <li>ein String mit Verwendung von SQL-Wildcard, z.B. "%OL%" </li> </td></tr>
  10505. <tr><td style="vertical-align:top"><b>&lt;new string&gt; :</b> <td><li>ein einfacher String mit/ohne Leerzeichen, z.B. "12 kWh" </li>
  10506. <li>Perl Code eingeschlossen in "{}" inkl. Quotes, z.B. "{($VALUE,$UNIT) = split(" ",$VALUE)}".
  10507. Dem Perl-Ausdruck werden die Variablen $VALUE und $UNIT übergeben. Sie können innerhalb
  10508. des Perl-Code geändert werden. Der zurückgebene Wert von $VALUE und $UNIT wird in dem Feld
  10509. VALUE bzw. UNIT des Datensatzes gespeichert. </li></td></tr>
  10510. </table>
  10511. <br>
  10512. <b>Beispiele: </b> <br>
  10513. set &lt;name&gt; changeValue "OL","12 OL" <br>
  10514. # der alte Feldwert "OL" wird in "12 OL" geändert. <br><br>
  10515. set &lt;name&gt; changeValue "%OL%","12 OL" <br>
  10516. # enthält das Feld VALUE den Teilstring "OL", wird es in "12 OL" geändert. <br><br>
  10517. set &lt;name&gt; changeValue "12 kWh","{($VALUE,$UNIT) = split(" ",$VALUE)}" <br>
  10518. # der alte Feldwert "12 kWh" wird in VALUE=12 und UNIT=kWh gesplittet und in den Datenbankfeldern gespeichert <br><br>
  10519. set &lt;name&gt; changeValue "24%","{$VALUE = (split(" ",$VALUE))[0]}" <br>
  10520. # beginnt der alte Feldwert mit "24", wird er gesplittet und VALUE=24 gespeichert (z.B. "24 kWh")
  10521. <br><br>
  10522. Zusammengefasst sind die zur Steuerung von changeValue relevanten Attribute: <br><br>
  10523. <ul>
  10524. <table>
  10525. <colgroup> <col width=5%> <col width=95%> </colgroup>
  10526. <tr><td> <b>device</b> </td><td>: einschließen oder ausschließen von Datensätzen die &lt;device&gt; enthalten </td></tr>
  10527. <tr><td> <b>aggregation</b> </td><td>: Auswahl einer Aggregationsperiode </td></tr>
  10528. <tr><td> <b>reading</b> </td><td>: einschließen oder ausschließen von Datensätzen die &lt;reading&gt; enthalten </td></tr>
  10529. <tr><td> <b>time.*</b> </td><td>: eine Reihe von Attributen zur Zeitabgrenzung </td></tr>
  10530. <tr><td> <b>executeBeforeProc</b> </td><td>: ausführen FHEM Kommando (oder perl-Routine) vor Start changeValue </td></tr>
  10531. <tr><td> <b>executeAfterProc</b> </td><td>: ausführen FHEM Kommando (oder perl-Routine) nach Ende changeValue </td></tr>
  10532. <tr><td style="vertical-align:top"> <b>valueFilter</b> <td>: ein zusätzliches REGEXP um die Datenselektion zu steuern. Der REGEXP wird auf das Datenbankfeld 'VALUE' angewendet. </td></tr>
  10533. </table>
  10534. </ul>
  10535. <br>
  10536. <br>
  10537. <b>Hinweis:</b> <br>
  10538. Obwohl die Funktion selbst non-blocking ausgelegt ist, sollte das zugeordnete DbLog-Device
  10539. im asynchronen Modus betrieben werden um ein Blockieren von FHEMWEB zu vermeiden (Tabellen-Lock). <br><br>
  10540. </li> <br>
  10541. </ul>
  10542. <li><b> countEntries [history | current] </b>
  10543. - liefert die Anzahl der Tabelleneinträge (default: history) in den gegebenen
  10544. Zeitgrenzen (siehe <a href="#DbRepattr">Attribute</a>).
  10545. Sind die Timestamps nicht gesetzt, werden alle Einträge der Tabelle gezählt.
  10546. Beschränkungen durch die <a href="#DbRepattr">Attribute</a> Device bzw. Reading
  10547. gehen in die Selektion mit ein. <br>
  10548. Standardmäßig wird die Summe aller Datensätze, gekennzeichnet mit "ALLREADINGS", erstellt.
  10549. Ist das Attribut "countEntriesDetail" gesetzt, wird die Anzahl jedes einzelnen Readings
  10550. zusätzlich ausgegeben. <br><br>
  10551. Die für diese Funktion relevanten Attribute sind: <br><br>
  10552. <ul>
  10553. <table>
  10554. <colgroup> <col width=5%> <col width=95%> </colgroup>
  10555. <tr><td> <b>aggregation</b> </td><td>: Zusammenfassung/Gruppierung von Zeitintervallen </td></tr>
  10556. <tr><td> <b>countEntriesDetail</b> </td><td>: detaillierte Ausgabe der Datensatzanzahl </td></tr>
  10557. <tr><td> <b>device</b> </td><td>: einschließen oder ausschließen von Datensätzen die &lt;device&gt; enthalten </td></tr>
  10558. <tr><td> <b>reading</b> </td><td>: einschließen oder ausschließen von Datensätzen die &lt;reading&gt; enthalten </td></tr>
  10559. <tr><td> <b>time.*</b> </td><td>: eine Reihe von Attributen zur Zeitabgrenzung </td></tr>
  10560. <tr><td> <b>readingNameMap</b> </td><td>: die entstehenden Ergebnisreadings werden partiell umbenannt </td></tr>
  10561. <tr><td style="vertical-align:top"> <b>valueFilter</b> <td>: ein zusätzliches REGEXP um die Datenselektion zu steuern. Der REGEXP wird auf das Datenbankfeld 'VALUE' angewendet. </td></tr>
  10562. </table>
  10563. </ul>
  10564. <br>
  10565. </li> <br>
  10566. <li><b> delEntries </b> - löscht alle oder die durch die <a href="#DbRepattr">Attribute</a> device und/oder
  10567. reading definierten Datenbankeinträge. Die Eingrenzung über Timestamps erfolgt
  10568. folgendermaßen: <br><br>
  10569. <ul>
  10570. "timestamp_begin" gesetzt <b>-&gt;</b> gelöscht werden DB-Einträge <b>ab</b> diesem Zeitpunkt bis zum aktuellen Datum/Zeit <br>
  10571. "timestamp_end" gesetzt <b>-&gt;</b> gelöscht werden DB-Einträge <b>bis</b> bis zu diesem Zeitpunkt <br>
  10572. beide Timestamps gesetzt <b>-&gt;</b> gelöscht werden DB-Einträge <b>zwischen</b> diesen Zeitpunkten <br>
  10573. "timeOlderThan" gesetzt <b>-&gt;</b> gelöscht werden DB-Einträge <b>älter</b> als aktuelle Zeit minus "timeOlderThan" <br>
  10574. "timeDiffToNow" gesetzt <b>-&gt;</b> gelöscht werden DB-Einträge <b>ab</b> aktueller Zeit minus "timeDiffToNow" bis jetzt <br>
  10575. </ul>
  10576. <br>
  10577. Aus Sicherheitsgründen muss das <a href="#DbRepattr">Attribut</a> "allowDeletion"
  10578. gesetzt sein um die Löschfunktion freizuschalten. <br><br>
  10579. Die zur Steuerung von delEntries relevanten Attribute: <br><br>
  10580. <ul>
  10581. <table>
  10582. <colgroup> <col width=5%> <col width=95%> </colgroup>
  10583. <tr><td> <b>allowDeletion</b> </td><td>: Freischaltung der Löschfunktion </td></tr>
  10584. <tr><td> <b>device</b> </td><td>: einschließen oder ausschließen von Datensätzen die &lt;device&gt; enthalten </td></tr>
  10585. <tr><td> <b>reading</b> </td><td>: einschließen oder ausschließen von Datensätzen die &lt;reading&gt; enthalten </td></tr>
  10586. <tr><td> <b>readingNameMap</b> </td><td>: die entstehenden Ergebnisreadings werden partiell umbenannt </td></tr>
  10587. <tr><td> <b>time.*</b> </td><td>: eine Reihe von Attributen zur Zeitabgrenzung </td></tr>
  10588. <tr><td> <b>executeBeforeProc</b> </td><td>: ausführen FHEM Kommando (oder perl-Routine) vor Start delEntries </td></tr>
  10589. <tr><td> <b>executeAfterProc</b> </td><td>: ausführen FHEM Kommando (oder perl-Routine) nach Ende delEntries </td></tr>
  10590. <tr><td style="vertical-align:top"> <b>valueFilter</b> <td>: ein zusätzliches REGEXP um die Datenselektion zu steuern. Der REGEXP wird auf das Datenbankfeld 'VALUE' angewendet. </td></tr>
  10591. </table>
  10592. </ul>
  10593. <br>
  10594. </li>
  10595. <br>
  10596. <li><b> delSeqDoublets [adviceRemain | adviceDelete | delete]</b> - zeigt bzw. löscht aufeinander folgende identische Datensätze.
  10597. Dazu wird Device,Reading und Value ausgewertet. Nicht gelöscht werden der erste und der letzte Datensatz
  10598. einer Aggregationsperiode (z.B. hour, day, week usw.) sowie die Datensätze vor oder nach einem Wertewechsel
  10599. (Datenbankfeld VALUE). <br>
  10600. Die <a href="#DbRepattr">Attribute</a> zur Aggregation,Zeit-,Device- und Reading-Abgrenzung werden dabei
  10601. berücksichtigt. Ist das Attribut "aggregation" nicht oder auf "no" gesetzt, wird als Standard die Aggregation
  10602. "day" verwendet. Für Datensätze mit numerischen Werten kann mit dem <a href="#DbRepattr">Attribut</a>
  10603. "seqDoubletsVariance" eine Abweichung eingestellt werden, bis zu der aufeinander folgende numerische Werte als
  10604. identisch angesehen und gelöscht werden sollen.
  10605. <br><br>
  10606. <ul>
  10607. <table>
  10608. <colgroup> <col width=5%> <col width=95%> </colgroup>
  10609. <tr><td> <b>adviceRemain</b> </td><td>: simuliert die nach der Operation in der DB verbleibenden Datensätze (es wird nichts gelöscht !) </td></tr>
  10610. <tr><td> <b>adviceDelete</b> </td><td>: simuliert die zu löschenden Datensätze (es wird nichts gelöscht !) </td></tr>
  10611. <tr><td> <b>delete</b> </td><td>: löscht die sequentiellen Dubletten (siehe Beispiel) </td></tr>
  10612. </table>
  10613. </ul>
  10614. <br>
  10615. Aus Sicherheitsgründen muss das <a href="#DbRepattr">Attribut</a> "allowDeletion" für die "delete" Option
  10616. gesetzt sein. <br>
  10617. Die Anzahl der anzuzeigenden Datensätze der Kommandos "delSeqDoublets adviceDelete", "delSeqDoublets adviceRemain" ist
  10618. zunächst begrenzt (default 1000) und kann durch das <a href="#DbRepattr">Attribut</a> "limit" angepasst werden.
  10619. Die Einstellung von "limit" hat keinen Einfluss auf die "delSeqDoublets delete" Funktion, sondern beeinflusst <b>NUR</b> die
  10620. Anzeige der Daten. <br>
  10621. Vor und nach der Ausführung von "delSeqDoublets" kann ein FHEM-Kommando bzw. Perl-Routine ausgeführt werden.
  10622. (siehe <a href="#DbRepattr">Attribute</a> "executeBeforeProc", "executeAfterProc")
  10623. <br><br>
  10624. <ul>
  10625. <b>Beispiel</b> - die nach Verwendung der delete-Option in der DB verbleibenden Datensätze sind <b>fett</b>
  10626. gekennzeichnet:<br><br>
  10627. <ul>
  10628. <b>2017-11-25_00-00-05__eg.az.fridge_Pwr__power 0 </b> <br>
  10629. 2017-11-25_00-02-26__eg.az.fridge_Pwr__power 0 <br>
  10630. 2017-11-25_00-04-33__eg.az.fridge_Pwr__power 0 <br>
  10631. 2017-11-25_01-06-10__eg.az.fridge_Pwr__power 0 <br>
  10632. <b>2017-11-25_01-08-21__eg.az.fridge_Pwr__power 0 </b> <br>
  10633. <b>2017-11-25_01-08-59__eg.az.fridge_Pwr__power 60.32 </b> <br>
  10634. <b>2017-11-25_01-11-21__eg.az.fridge_Pwr__power 56.26 </b> <br>
  10635. <b>2017-11-25_01-27-54__eg.az.fridge_Pwr__power 6.19 </b> <br>
  10636. <b>2017-11-25_01-28-51__eg.az.fridge_Pwr__power 0 </b> <br>
  10637. 2017-11-25_01-31-00__eg.az.fridge_Pwr__power 0 <br>
  10638. 2017-11-25_01-33-59__eg.az.fridge_Pwr__power 0 <br>
  10639. <b>2017-11-25_02-39-29__eg.az.fridge_Pwr__power 0 </b> <br>
  10640. <b>2017-11-25_02-41-18__eg.az.fridge_Pwr__power 105.28</b> <br>
  10641. <b>2017-11-25_02-41-26__eg.az.fridge_Pwr__power 61.52 </b> <br>
  10642. <b>2017-11-25_03-00-06__eg.az.fridge_Pwr__power 47.46 </b> <br>
  10643. <b>2017-11-25_03-00-33__eg.az.fridge_Pwr__power 0 </b> <br>
  10644. 2017-11-25_03-02-07__eg.az.fridge_Pwr__power 0 <br>
  10645. 2017-11-25_23-37-42__eg.az.fridge_Pwr__power 0 <br>
  10646. <b>2017-11-25_23-40-10__eg.az.fridge_Pwr__power 0 </b> <br>
  10647. <b>2017-11-25_23-42-24__eg.az.fridge_Pwr__power 1 </b> <br>
  10648. 2017-11-25_23-42-24__eg.az.fridge_Pwr__power 1 <br>
  10649. <b>2017-11-25_23-45-27__eg.az.fridge_Pwr__power 1 </b> <br>
  10650. <b>2017-11-25_23-47-07__eg.az.fridge_Pwr__power 0 </b> <br>
  10651. 2017-11-25_23-55-27__eg.az.fridge_Pwr__power 0 <br>
  10652. <b>2017-11-25_23-48-15__eg.az.fridge_Pwr__power 0 </b> <br>
  10653. <b>2017-11-25_23-50-21__eg.az.fridge_Pwr__power 59.1 </b> <br>
  10654. <b>2017-11-25_23-55-14__eg.az.fridge_Pwr__power 52.31 </b> <br>
  10655. <b>2017-11-25_23-58-09__eg.az.fridge_Pwr__power 51.73 </b> <br>
  10656. </ul>
  10657. </ul>
  10658. <br>
  10659. Zusammengefasst sind die zur Steuerung dieser Funktion relevanten Attribute: <br><br>
  10660. <ul>
  10661. <table>
  10662. <colgroup> <col width=5%> <col width=95%> </colgroup>
  10663. <tr><td> <b>allowDeletion</b> </td><td>: needs to be set to execute the delete option </td></tr>
  10664. <tr><td> <b>aggregation</b> </td><td>: Auswahl einer Aggregationsperiode </td></tr>
  10665. <tr><td> <b>device</b> </td><td>: einschließen oder ausschließen von Datensätzen die &lt;device&gt; enthalten </td></tr>
  10666. <tr><td> <b>reading</b> </td><td>: einschließen oder ausschließen von Datensätzen die &lt;reading&gt; enthalten </td></tr>
  10667. <tr><td> <b>readingNameMap</b> </td><td>: die entstehenden Ergebnisreadings werden partiell umbenannt </td></tr>
  10668. <tr><td> <b>seqDoubletsVariance</b> </td><td>: bis zu diesem Wert werden aufeinander folgende numerische Datensätze als identisch angesehen und werden gelöscht </td></tr>
  10669. <tr><td> <b>time.*</b> </td><td>: eine Reihe von Attributen zur Zeitabgrenzung </td></tr>
  10670. <tr><td style="vertical-align:top"> <b>valueFilter</b> <td>: ein zusätzliches REGEXP um die Datenselektion zu steuern. Der REGEXP wird auf das Datenbankfeld 'VALUE' angewendet. </td></tr>
  10671. </table>
  10672. </ul>
  10673. <br>
  10674. <br>
  10675. </li>
  10676. <li><b> deviceRename &lt;old_name&gt;,&lt;new_name&gt;</b> -
  10677. benennt den Namen eines Device innerhalb der angeschlossenen Datenbank (Internal DATABASE) um.
  10678. Der Gerätename wird immer in der <b>gesamten</b> Datenbank umgesetzt. Eventuell gesetzte
  10679. Zeitgrenzen oder Beschränkungen durch die <a href="#DbRepattr">Attribute</a> Device bzw.
  10680. Reading werden nicht berücksichtigt. <br><br>
  10681. <ul>
  10682. <b>Beispiel: </b><br>
  10683. set &lt;name&gt; deviceRename ST_5000,ST5100 <br>
  10684. # Die Anzahl der umbenannten Device-Datensätze wird im Reading "device_renamed" ausgegeben. <br>
  10685. # Wird der umzubenennende Gerätename in der Datenbank nicht gefunden, wird eine WARNUNG im Reading "device_not_renamed" ausgegeben. <br>
  10686. # Entsprechende Einträge erfolgen auch im Logfile mit verbose=3
  10687. <br><br>
  10688. <b>Hinweis:</b> <br>
  10689. Obwohl die Funktion selbst non-blocking ausgelegt ist, sollte das zugeordnete DbLog-Device
  10690. im asynchronen Modus betrieben werden um ein Blockieren von FHEMWEB zu vermeiden (Tabellen-Lock). <br><br>
  10691. </li> <br>
  10692. </ul>
  10693. <li><b> diffValue [display | writeToDB] </b>
  10694. - berechnet den Differenzwert des Datenbankfelds "VALUE" in den Zeitgrenzen (Attribute) "timestamp_begin", "timestamp_end" bzw "timeDiffToNow / timeOlderThan".
  10695. Es muss das auszuwertende Reading im Attribut "reading" angegeben sein.
  10696. Diese Funktion ist z.B. zur Auswertung von Eventloggings sinnvoll, deren Werte sich fortlaufend erhöhen und keine Wertdifferenzen wegschreiben. <br>
  10697. Es wird immer die Differenz aus dem Value-Wert des ersten verfügbaren Datensatzes und dem Value-Wert des letzten verfügbaren Datensatzes innerhalb der angegebenen
  10698. Zeitgrenzen/Aggregation gebildet, wobei ein Übertragswert der Vorperiode (Aggregation) zur darauf folgenden Aggregationsperiode
  10699. berücksichtigt wird sofern diese einen Value-Wert enhtält. <br>
  10700. Dabei wird ein Zählerüberlauf (Neubeginn bei 0) mit berücksichtigt (vergleiche <a href="#DbRepattr">Attribut</a> "diffAccept"). <br>
  10701. Wird in einer auszuwertenden Zeit- bzw. Aggregationsperiode nur ein Datensatz gefunden, kann die Differenz in Verbindung mit dem
  10702. Differenzübertrag der Vorperiode berechnet werden. in diesem Fall kann es zu einer logischen Ungenauigkeit in der Zuordnung der Differenz
  10703. zu der Aggregationsperiode kommen. Deswegen wird eine Warnung im "state" und das
  10704. Reading "less_data_in_period" mit einer Liste der betroffenen Perioden wird erzeugt. <br><br>
  10705. <ul>
  10706. <b>Hinweis: </b><br>
  10707. Im Auswertungs- bzw. Aggregationszeitraum (Tag, Woche, Monat, etc.) sollten dem Modul pro Periode mindestens ein Datensatz
  10708. zu Beginn und ein Datensatz gegen Ende des Aggregationszeitraumes zur Verfügung stehen um eine möglichst genaue Auswertung
  10709. der Differenzwerte vornehmen zu können.
  10710. <br>
  10711. <br>
  10712. </ul>
  10713. Ist keine oder die Option "display" angegeben, werden die Ergebnisse nur angezeigt. Mit
  10714. der Option "writeToDB" werden die Berechnungsergebnisse mit einem neuen Readingnamen
  10715. in der Datenbank gespeichert. <br>
  10716. Der neue Readingname wird aus einem Präfix und dem originalen Readingnamen gebildet,
  10717. wobei der originale Readingname durch das Attribut "readingNameMap" ersetzt werden kann.
  10718. Der Präfix setzt sich aus der Bildungsfunktion und der Aggregation zusammen. <br>
  10719. Der Timestamp der neuen Readings in der Datenbank wird von der eingestellten Aggregationsperiode
  10720. abgeleitet, sofern kein eindeutiger Zeitpunkt des Ergebnisses bestimmt werden kann.
  10721. Das Feld "EVENT" wird mit "calculated" gefüllt.<br><br>
  10722. <ul>
  10723. <b>Beispiel neuer Readingname gebildet aus dem Originalreading "totalpac":</b> <br>
  10724. diff_day_totalpac <br>
  10725. # &lt;Bildungsfunktion&gt;_&lt;Aggregation&gt;_&lt;Originalreading&gt; <br>
  10726. </ul>
  10727. <br>
  10728. Zusammengefasst sind die zur Steuerung dieser Funktion relevanten Attribute: <br><br>
  10729. <ul>
  10730. <table>
  10731. <colgroup> <col width=5%> <col width=95%> </colgroup>
  10732. <tr><td> <b>aggregation</b> </td><td>: Auswahl einer Aggregationsperiode </td></tr>
  10733. <tr><td> <b>device</b> </td><td>: einschließen oder ausschließen von Datensätzen die &lt;device&gt; enthalten </td></tr>
  10734. <tr><td> <b>reading</b> </td><td>: einschließen oder ausschließen von Datensätzen die &lt;reading&gt; enthalten </td></tr>
  10735. <tr><td> <b>readingNameMap</b> </td><td>: die entstehenden Ergebnisreadings werden partiell umbenannt </td></tr>
  10736. <tr><td> <b>seqDoubletsVariance</b> </td><td>: bis zu diesem Wert werden aufeinander folgende numerische Datensätze als identisch angesehen und werden gelöscht </td></tr>
  10737. <tr><td> <b>time.*</b> </td><td>: eine Reihe von Attributen zur Zeitabgrenzung </td></tr>
  10738. <tr><td style="vertical-align:top"> <b>valueFilter</b> <td>: ein zusätzliches REGEXP um die Datenselektion zu steuern. Der REGEXP wird auf das Datenbankfeld 'VALUE' angewendet. </td></tr>
  10739. </table>
  10740. </ul>
  10741. <br>
  10742. </li> <br>
  10743. <li><b> dumpMySQL [clientSide | serverSide]</b>
  10744. - erstellt einen Dump der angeschlossenen MySQL-Datenbank. <br>
  10745. Abhängig von der ausgewählten Option wird der Dump auf der Client- bzw. Serverseite erstellt. <br>
  10746. Die Varianten unterscheiden sich hinsichtlich des ausführenden Systems, des Erstellungsortes, der
  10747. Attributverwendung, des erzielten Ergebnisses und der benötigten Hardwareressourcen. <br>
  10748. Die Option "clientSide" benötigt z.B. eine leistungsfähigere Hardware des FHEM-Servers, sichert aber alle
  10749. Tabellen inklusive eventuell angelegter Views. <br>
  10750. Mit dem Attribut "dumpCompress" kann eine Komprimierung der erstellten Dumpfiles eingeschaltet werden.
  10751. <br><br>
  10752. <ul>
  10753. <b><u>Option clientSide</u></b> <br>
  10754. Der Dump wird durch den Client (FHEM-Rechner) erstellt und per default im log-Verzeichnis des Clients
  10755. gespeichert.
  10756. Das Zielverzeichnis kann mit dem <a href="#DbRepattr">Attribut</a> "dumpDirLocal" verändert werden und muß auf
  10757. dem Client durch FHEM beschreibbar sein. <br>
  10758. Vor dem Dump kann eine Tabellenoptimierung (Attribut "optimizeTablesBeforeDump") oder ein FHEM-Kommando
  10759. (Attribut "executeBeforeProc") optional zugeschaltet werden.
  10760. Nach dem Dump kann ebenfalls ein FHEM-Kommando (siehe Attribut "executeAfterProc") ausgeführt werden. <br><br>
  10761. <b>Achtung ! <br>
  10762. Um ein Blockieren von FHEM zu vermeiden, muß DbLog im asynchronen Modus betrieben werden wenn die
  10763. Tabellenoptimierung verwendet wird ! </b> <br><br>
  10764. Über die <a href="#DbRepattr">Attribute</a> "dumpMemlimit" und "dumpSpeed" kann das Laufzeitverhalten der
  10765. Funktion beeinflusst werden um eine Optimierung bezüglich Performance und Ressourcenbedarf zu erreichen. <br><br>
  10766. Die für "dumpMySQL clientSide" relevanten Attribute sind: <br><br>
  10767. <ul>
  10768. <table>
  10769. <colgroup> <col width=5%> <col width=95%> </colgroup>
  10770. <tr><td> dumpComment </td><td>: User-Kommentar im Dumpfile </td></tr>
  10771. <tr><td> dumpCompress </td><td>: Komprimierung des Dumpfiles nach der Erstellung </td></tr>
  10772. <tr><td> dumpDirLocal </td><td>: das lokale Zielverzeichnis für die Erstellung des Dump </td></tr>
  10773. <tr><td> dumpMemlimit </td><td>: Begrenzung der Speicherverwendung </td></tr>
  10774. <tr><td> dumpSpeed </td><td>: Begrenzung die CPU-Belastung </td></tr>
  10775. <tr><td> dumpFilesKeep </td><td>: Anzahl der aufzubwahrenden Dumpfiles </td></tr>
  10776. <tr><td> executeBeforeProc </td><td>: ausführen FHEM Kommando (oder perl-Routine) vor dem Dump </td></tr>
  10777. <tr><td> executeAfterProc </td><td>: ausführen FHEM Kommando (oder perl-Routine) nach dem Dump </td></tr>
  10778. <tr><td> optimizeTablesBeforeDump </td><td>: Tabelloptimierung vor dem Dump ausführen </td></tr>
  10779. </table>
  10780. </ul>
  10781. <br>
  10782. Nach einem erfolgreichen Dump werden alte Dumpfiles gelöscht und nur die Anzahl Files, definiert durch
  10783. das Attribut "dumpFilesKeep" (default: 3), verbleibt im Zielverzeichnis "dumpDirLocal". Falls "dumpFilesKeep = 0"
  10784. gesetzt ist, werden alle Dumpfiles (auch das aktuell erstellte File), gelöscht.
  10785. Diese Einstellung kann sinnvoll sein, wenn FTP aktiviert ist
  10786. und die erzeugten Dumps nur im FTP-Zielverzeichnis erhalten bleiben sollen. <br><br>
  10787. Die <b>Namenskonvention der Dumpfiles</b> ist: &lt;dbname&gt;_&lt;date&gt;_&lt;time&gt;.sql[.gzip] <br><br>
  10788. Um die Datenbank aus dem Dumpfile wiederherzustellen kann das Kommmando: <br><br>
  10789. <ul>
  10790. set &lt;name&gt; restoreMySQL &lt;filename&gt; <br><br>
  10791. </ul>
  10792. verwendet werden. <br><br>
  10793. Das erzeugte Dumpfile (unkomprimiert) kann ebenfalls mit: <br><br>
  10794. <ul>
  10795. mysql -u &lt;user&gt; -p &lt;dbname&gt; < &lt;filename&gt;.sql <br><br>
  10796. </ul>
  10797. auf dem MySQL-Server ausgeführt werden um die Datenbank aus dem Dump wiederherzustellen. <br><br>
  10798. <br>
  10799. <b><u>Option serverSide</u></b> <br>
  10800. Der Dump wird durch den MySQL-Server erstellt und per default im Home-Verzeichnis des MySQL-Servers
  10801. gespeichert. <br>
  10802. Es wird die gesamte history-Tabelle (nicht current-Tabelle) <b>im CSV-Format</b> ohne
  10803. Einschränkungen exportiert. <br>
  10804. Vor dem Dump kann eine Tabellenoptimierung (Attribut "optimizeTablesBeforeDump")
  10805. optional zugeschaltet werden . <br><br>
  10806. <b>Achtung ! <br>
  10807. Um ein Blockieren von FHEM zu vermeiden, muß DbLog im asynchronen Modus betrieben werden wenn die
  10808. Tabellenoptimierung verwendet wird ! </b> <br><br>
  10809. Vor und nach dem Dump kann ein FHEM-Kommando (siehe Attribute "executeBeforeProc", "executeAfterProc") ausgeführt
  10810. werden. <br><br>
  10811. Die für "dumpMySQL serverSide" relevanten Attribute sind: <br><br>
  10812. <ul>
  10813. <table>
  10814. <colgroup> <col width=5%> <col width=95%> </colgroup>
  10815. <tr><td> dumpDirRemote </td><td>: das Erstellungsverzeichnis des Dumpfile auf dem entfernten Server </td></tr>
  10816. <tr><td> dumpCompress </td><td>: Komprimierung des Dumpfiles nach der Erstellung </td></tr>
  10817. <tr><td> dumpDirLocal </td><td>: Directory des lokal gemounteten dumpDirRemote-Verzeichnisses </td></tr>
  10818. <tr><td> dumpFilesKeep </td><td>: Anzahl der aufzubwahrenden Dumpfiles </td></tr>
  10819. <tr><td> executeBeforeProc </td><td>: ausführen FHEM Kommando (oder perl-Routine) vor dem Dump </td></tr>
  10820. <tr><td> executeAfterProc </td><td>: ausführen FHEM Kommando (oder perl-Routine) nach dem Dump </td></tr>
  10821. <tr><td> optimizeTablesBeforeDump </td><td>: Tabelloptimierung vor dem Dump ausführen </td></tr>
  10822. </table>
  10823. </ul>
  10824. <br>
  10825. Das Zielverzeichnis kann mit dem <a href="#DbRepattr">Attribut</a> "dumpDirRemote" verändert werden.
  10826. Es muß sich auf dem MySQL-Host gefinden und durch den MySQL-Serverprozess beschreibbar sein. <br>
  10827. Der verwendete Datenbankuser benötigt das "FILE"-Privileg. <br><br>
  10828. <b>Hinweis:</b> <br>
  10829. Soll die interne Versionsverwaltung und die Dumpfilekompression des Moduls genutzt, sowie die Größe des erzeugten
  10830. Dumpfiles ausgegeben werden, ist das Verzeichnis "dumpDirRemote" des MySQL-Servers auf dem Client zu mounten
  10831. und im <a href="#DbRepattr">Attribut</a> "dumpDirLocal" dem DbRep-Device bekannt zu machen. <br>
  10832. Gleiches gilt wenn der FTP-Transfer nach dem Dump genutzt werden soll (Attribut "ftpUse" bzw. "ftpUseSSL").
  10833. <br><br>
  10834. <ul>
  10835. <b>Beispiel: </b> <br>
  10836. attr &lt;name&gt; dumpDirRemote /volume1/ApplicationBackup/dumps_FHEM/ <br>
  10837. attr &lt;name&gt; dumpDirLocal /sds1/backup/dumps_FHEM/ <br>
  10838. attr &lt;name&gt; dumpFilesKeep 2 <br><br>
  10839. # Der Dump wird remote auf dem MySQL-Server im Verzeichnis '/volume1/ApplicationBackup/dumps_FHEM/'
  10840. erstellt. <br>
  10841. # Die interne Versionsverwaltung sucht im lokal gemounteten Verzeichnis '/sds1/backup/dumps_FHEM/'
  10842. vorhandene Dumpfiles und löscht diese bis auf die zwei letzten Versionen. <br>
  10843. <br>
  10844. </ul>
  10845. Wird die interne Versionsverwaltung genutzt, werden nach einem erfolgreichen Dump alte Dumpfiles gelöscht
  10846. und nur die Anzahl "dumpFilesKeep" (default: 3) verbleibt im Zielverzeichnis "dumpDirRemote".
  10847. FHEM benötigt in diesem Fall Schreibrechte auf dem Verzeichnis "dumpDirLocal". <br><br>
  10848. Die <b>Namenskonvention der Dumpfiles</b> ist: &lt;dbname&gt;_&lt;date&gt;_&lt;time&gt;.csv[.gzip] <br><br>
  10849. Ein Restore der Datenbank aus diesem Backup kann durch den Befehl: <br><br>
  10850. <ul>
  10851. set &lt;name&gt; &lt;restoreMySQL&gt; &lt;filename&gt;.csv[.gzip] <br><br>
  10852. </ul>
  10853. gestartet werden. <br><br>
  10854. <b><u>FTP Transfer nach Dump</u></b> <br>
  10855. Wenn diese Möglichkeit genutzt werden soll, ist das <a href="#DbRepattr">Attribut</a> "ftpUse" oder
  10856. "ftpUseSSL" zu setzen. Letzteres gilt wenn eine verschlüsselte Übertragung genutzt werden soll. <br>
  10857. Das Modul übernimmt ebenfalls die Versionierung der Dumpfiles im FTP-Zielverzeichnis mit Hilfe des Attributes
  10858. "ftpDumpFilesKeep".
  10859. Für die FTP-Übertragung relevante <a href="#DbRepattr">Attribute</a> sind: <br><br>
  10860. <ul>
  10861. <table>
  10862. <colgroup> <col width=5%> <col width=95%> </colgroup>
  10863. <tr><td> ftpUse </td><td>: FTP Transfer nach dem Dump wird eingeschaltet (ohne SSL Verschlüsselung) </td></tr>
  10864. <tr><td> ftpUser </td><td>: User zur Anmeldung am FTP-Server, default: anonymous </td></tr>
  10865. <tr><td> ftpUseSSL </td><td>: FTP Transfer mit SSL Verschlüsselung nach dem Dump wird eingeschaltet </td></tr>
  10866. <tr><td> ftpDebug </td><td>: Debugging des FTP Verkehrs zur Fehlersuche </td></tr>
  10867. <tr><td> ftpDir </td><td>: Verzeichnis auf dem FTP-Server in welches das File übertragen werden soll (default: "/") </td></tr>
  10868. <tr><td> ftpDumpFilesKeep </td><td>: Es wird die angegebene Anzahl Dumpfiles im &lt;ftpDir&gt; belassen (default: 3) </td></tr>
  10869. <tr><td> ftpPassive </td><td>: setzen wenn passives FTP verwendet werden soll </td></tr>
  10870. <tr><td> ftpPort </td><td>: FTP-Port, default: 21 </td></tr>
  10871. <tr><td> ftpPwd </td><td>: Passwort des FTP-Users, default nicht gesetzt </td></tr>
  10872. <tr><td> ftpServer </td><td>: Name oder IP-Adresse des FTP-Servers. <b>notwendig !</b> </td></tr>
  10873. <tr><td> ftpTimeout </td><td>: Timeout für die FTP-Verbindung in Sekunden (default: 30). </td></tr>
  10874. </table>
  10875. </ul>
  10876. <br>
  10877. <br>
  10878. </ul>
  10879. </li><br>
  10880. <li><b> dumpSQLite </b> - erstellt einen Dump der angeschlossenen SQLite-Datenbank. <br>
  10881. Diese Funktion nutzt die SQLite Online Backup API und ermöglicht es konsistente Backups der SQLite-DB
  10882. in laufenden Betrieb zu erstellen.
  10883. Der Dump wird per default im log-Verzeichnis des FHEM-Rechners gespeichert.
  10884. Das Zielverzeichnis kann mit dem <a href="#DbRepattr">Attribut</a> "dumpDirLocal" verändert werden und muß
  10885. durch FHEM beschreibbar sein.
  10886. Vor dem Dump kann optional eine Tabellenoptimierung (Attribut "optimizeTablesBeforeDump")
  10887. zugeschaltet werden.
  10888. <br><br>
  10889. <b>Achtung ! <br>
  10890. Um ein Blockieren von FHEM zu vermeiden, muß DbLog im asynchronen Modus betrieben werden wenn die
  10891. Tabellenoptimierung verwendet wird ! </b> <br><br>
  10892. Vor und nach dem Dump kann ein FHEM-Kommando (siehe Attribute "executeBeforeProc", "executeAfterProc")
  10893. ausgeführt werden. <br><br>
  10894. Die für diese Funktion relevanten Attribute sind: <br><br>
  10895. <ul>
  10896. <table>
  10897. <colgroup> <col width=5%> <col width=95%> </colgroup>
  10898. <tr><td> dumpCompress </td><td>: Komprimierung des Dumpfiles nach der Erstellung </td></tr>
  10899. <tr><td> dumpDirLocal </td><td>: Directory des lokal gemounteten dumpDirRemote-Verzeichnisses </td></tr>
  10900. <tr><td> dumpFilesKeep </td><td>: Anzahl der aufzubwahrenden Dumpfiles </td></tr>
  10901. <tr><td> executeBeforeProc </td><td>: ausführen FHEM Kommando (oder perl-Routine) vor dem Dump </td></tr>
  10902. <tr><td> executeAfterProc </td><td>: ausführen FHEM Kommando (oder perl-Routine) nach dem Dump </td></tr>
  10903. <tr><td> optimizeTablesBeforeDump </td><td>: Tabelloptimierung vor dem Dump ausführen </td></tr>
  10904. </table>
  10905. </ul>
  10906. <br>
  10907. Nach einem erfolgreichen Dump werden alte Dumpfiles gelöscht und nur die Anzahl Files, definiert durch das
  10908. Attribut "dumpFilesKeep" (default: 3), verbleibt im Zielverzeichnis "dumpDirLocal". Falls "dumpFilesKeep = 0" gesetzt, werden
  10909. alle Dumpfiles (auch das aktuell erstellte File), gelöscht. Diese Einstellung kann sinnvoll sein, wenn FTP aktiviert ist
  10910. und die erzeugten Dumps nur im FTP-Zielverzeichnis erhalten bleiben sollen. <br><br>
  10911. Die <b>Namenskonvention der Dumpfiles</b> ist: &lt;dbname&gt;_&lt;date&gt;_&lt;time&gt;.sqlitebkp[.gzip] <br><br>
  10912. Die Datenbank kann mit "set &lt;name&gt; restoreSQLite &lt;Filename&gt;" wiederhergestellt
  10913. werden. <br>
  10914. Das erstellte Dumpfile kann auf einen FTP-Server übertragen werden. Siehe dazu die Erläuterungen
  10915. unter "dumpMySQL". <br><br>
  10916. </li><br>
  10917. <li><b> eraseReadings </b> - Löscht alle angelegten Readings im Device, außer dem Reading "state" und Readings, die in der
  10918. Ausnahmeliste definiert mit Attribut "readingPreventFromDel" enthalten sind.
  10919. </li><br>
  10920. <li><b> exportToFile [&lt;File&gt;] </b>
  10921. - exportiert DB-Einträge im CSV-Format in den gegebenen Zeitgrenzen. <br>
  10922. Einschränkungen durch die <a href="#DbRepattr">Attribute</a> "device" bzw. "reading" gehen in die Selektion mit ein.
  10923. Der Dateiname wird durch das <a href="#DbRepattr">Attribut</a> "expimpfile" bestimmt. <br>
  10924. Alternativ kann die Datei (/Pfad/Datei) als Kommando-Option angegeben werden und übersteuert ein
  10925. eventuell gesetztes Attribut "expimpfile". Der Dateiname kann Wildcards enthalten (siehe Attribut "expimpfile").
  10926. <br>
  10927. Durch das Attribut "aggregation" wird der Export der Datensätze in Zeitscheiben der angegebenen Aggregation
  10928. vorgenommen. Ist z.B. "aggregation = month" gesetzt, werden die Daten in monatlichen Paketen selektiert und in
  10929. das Exportfile geschrieben. Dadurch wird die Hauptspeicherverwendung optimiert wenn sehr große Datenmengen
  10930. exportiert werden sollen und vermeidet den "died prematurely" Abbruchfehler. <br><br>
  10931. Die für diese Funktion relevanten Attribute sind: <br><br>
  10932. <ul>
  10933. <table>
  10934. <colgroup> <col width=5%> <col width=95%> </colgroup>
  10935. <tr><td> <b>aggregation</b> </td><td>: Festlegung der Selektionspaketierung </td></tr>
  10936. <tr><td> <b>device</b> </td><td>: einschließen oder ausschließen von Datensätzen die &lt;device&gt; enthalten </td></tr>
  10937. <tr><td> <b>reading</b> </td><td>: einschließen oder ausschließen von Datensätzen die &lt;reading&gt; enthalten </td></tr>
  10938. <tr><td> <b>executeBeforeProc</b> </td><td>: FHEM Kommando (oder perl-Routine) vor dem Export ausführen </td></tr>
  10939. <tr><td> <b>executeAfterProc</b> </td><td>: FHEM Kommando (oder perl-Routine) nach dem Export ausführen </td></tr>
  10940. <tr><td> <b>expimpfile</b> </td><td>: der Name des Exportfiles </td></tr>
  10941. <tr><td> <b>time.*</b> </td><td>: eine Reihe von Attributen zur Zeitabgrenzung </td></tr>
  10942. <tr><td style="vertical-align:top"> <b>valueFilter</b> <td>: ein zusätzliches REGEXP um die Datenselektion zu steuern. Der REGEXP wird auf das Datenbankfeld 'VALUE' angewendet. </td></tr>
  10943. </table>
  10944. </ul>
  10945. </li><br>
  10946. <li><b> fetchrows [history|current] </b>
  10947. - liefert <b>alle</b> Tabelleneinträge (default: history)
  10948. in den gegebenen Zeitgrenzen bzw. Selektionsbedingungen durch die <a href="#DbRepattr">Attribute</a>
  10949. "device" und "reading".
  10950. Eine evtl. gesetzte Aggregation wird dabei <b>nicht</b> berücksichtigt. <br>
  10951. Die Leserichtung in der Datenbank kann durch das <a href="#DbRepattr">Attribut</a>
  10952. "fetchRoute" bestimmt werden. <br><br>
  10953. Jedes Ergebnisreading setzt sich aus dem Timestring des Datensatzes, einem Index, dem Device
  10954. und dem Reading zusammen.
  10955. Die Funktion fetchrows ist in der Lage, mehrfach vorkommende Datensätze (Dubletten) zu erkennen.
  10956. Solche Dubletten sind mit einem Index > 1 gekennzeichnet. <br>
  10957. Dubletten können mit dem Attribut "fetchMarkDuplicates" farblich hervorgehoben werden. <br><br>
  10958. <b>Hinweis:</b> <br>
  10959. Hervorgehobene Readings werden nach einem Restart bzw. nach rereadcfg nicht mehr angezeigt da
  10960. sie nicht im statefile gesichert werden (Verletzung erlaubter Readingnamen durch Formatierung).
  10961. <br><br>
  10962. Dieses Attribut ist mit einigen Farben vorbelegt, kann aber mit dem colorpicker-Widget
  10963. überschrieben werden: <br><br>
  10964. <ul>
  10965. <code>
  10966. attr &lt;name&gt; widgetOverride fetchMarkDuplicates:colorpicker
  10967. </code>
  10968. </ul>
  10969. <br>
  10970. Die Ergebnisreadings von fetchrows sind nach folgendem Schema aufgebaut: <br><br>
  10971. <ul>
  10972. <b>Beispiel:</b> <br>
  10973. 2017-10-22_03-04-43__1__SMA_Energymeter__Bezug_WirkP_Kosten_Diff <br>
  10974. # &lt;Datum&gt;_&lt;Zeit&gt;__&lt;Index&gt;__&lt;Device&gt;__&lt;Reading&gt;
  10975. </ul>
  10976. <br>
  10977. Zur besseren Übersicht sind die zur Steuerung von fetchrows relevanten Attribute hier noch einmal
  10978. dargestellt: <br><br>
  10979. <ul>
  10980. <table>
  10981. <colgroup> <col width=5%> <col width=95%> </colgroup>
  10982. <tr><td> <b>fetchRoute</b> </td><td>: Leserichtung der Selektion innerhalb der Datenbank </td></tr>
  10983. <tr><td> <b>limit</b> </td><td>: begrenzt die Anzahl zu selektierenden bzw. anzuzeigenden Datensätze </td></tr>
  10984. <tr><td> <b>fetchMarkDuplicates</b> </td><td>: Hervorhebung von gefundenen Dubletten </td></tr>
  10985. <tr><td> <b>device</b> </td><td>: einschließen oder ausschließen von Datensätzen die &lt;device&gt; enthalten </td></tr>
  10986. <tr><td> <b>reading</b> </td><td>: einschließen oder ausschließen von Datensätzen die &lt;reading&gt; enthalten </td></tr>
  10987. <tr><td> <b>time.*</b> </td><td>: eine Reihe von Attributen zur Zeitabgrenzung </td></tr>
  10988. <tr><td style="vertical-align:top"> <b>valueFilter</b> <td>: filtert die anzuzeigenden Datensätze mit einem regulären Ausdruck. Der Regex wird auf Werte des DAtenbankfelder 'VALUE' angewendet. </td></tr>
  10989. </table>
  10990. </ul>
  10991. <br>
  10992. <br>
  10993. <b>Hinweis:</b> <br>
  10994. Auch wenn das Modul bezüglich der Datenbankabfrage nichtblockierend arbeitet, kann eine
  10995. zu große Ergebnismenge (Anzahl Zeilen bzw. Readings) die Browsersesssion bzw. FHEMWEB
  10996. blockieren. Aus diesem Grund wird die Ergebnismenge mit dem
  10997. <a href="#limit">Attribut</a> "limit" begrenzt. Bei Bedarf kann dieses Attribut
  10998. geändert werden, falls eine Anpassung der Selektionsbedingungen nicht möglich oder
  10999. gewünscht ist. <br><br>
  11000. </li> <br>
  11001. <li><b> insert </b> - Manuelles Einfügen eines Datensatzes in die Tabelle "history". Obligatorisch sind Eingabewerte für Datum, Zeit und Value.
  11002. Die Werte für die DB-Felder Type bzw. Event werden mit "manual" gefüllt, sowie die Werte für Device, Reading aus den gesetzten <a href="#DbRepattr">Attributen </a> genommen. <br><br>
  11003. <ul>
  11004. <b>Eingabeformat: </b> Datum,Zeit,Value,[Unit] <br>
  11005. # Unit ist optional, Attribute "reading" und "device" müssen gesetzt sein <br>
  11006. # Soll "Value=0" eingefügt werden, ist "Value = 0.0" zu verwenden. <br><br>
  11007. <b>Beispiel: </b> 2016-08-01,23:00:09,TestValue,TestUnit <br>
  11008. # Es sind KEINE Leerzeichen im Feldwert erlaubt !<br>
  11009. <br>
  11010. <b>Hinweis: </b><br>
  11011. Bei der Eingabe ist darauf zu achten dass im beabsichtigten Aggregationszeitraum (Tag, Woche, Monat, etc.) MINDESTENS zwei
  11012. Datensätze für die Funktion diffValue zur Verfügung stehen. Ansonsten kann keine Differenz berechnet werden und diffValue
  11013. gibt in diesem Fall "0" in der betroffenen Periode aus !
  11014. <br>
  11015. <br>
  11016. </li>
  11017. </ul>
  11018. <li><b> importFromFile [&lt;File&gt;] </b>
  11019. - importiert Datensätze im CSV-Format aus einer Datei in die Datenbank. <br>
  11020. Der Dateiname wird durch das <a href="#DbRepattr">Attribut</a> "expimpfile" bestimmt. <br>
  11021. Alternativ kann die Datei (/Pfad/Datei) als Kommando-Option angegeben werden und übersteuert ein
  11022. eventuell gesetztes Attribut "expimpfile". Der Dateiname kann Wildcards enthalten (siehe
  11023. Attribut "expimpfile"). <br><br>
  11024. <ul>
  11025. <b>Datensatzformat: </b> <br>
  11026. "TIMESTAMP","DEVICE","TYPE","EVENT","READING","VALUE","UNIT" <br><br>
  11027. # Die Felder "TIMESTAMP","DEVICE","TYPE","EVENT","READING" und "VALUE" müssen gesetzt sein. Das Feld "UNIT" ist optional.
  11028. Der Fileinhalt wird als Transaktion importiert, d.h. es wird der Inhalt des gesamten Files oder, im Fehlerfall, kein Datensatz des Files importiert.
  11029. Wird eine umfangreiche Datei mit vielen Datensätzen importiert, sollte KEIN verbose=5 gesetzt werden. Es würden in diesem Fall sehr viele Sätze in
  11030. das Logfile geschrieben werden was FHEM blockieren oder überlasten könnte. <br><br>
  11031. <b>Beispiel: </b> <br>
  11032. "2016-09-25 08:53:56","STP_5000","SMAUTILS","etotal: 11859.573","etotal","11859.573","" <br>
  11033. <br>
  11034. Die für diese Funktion relevanten Attribute sind: <br><br>
  11035. <ul>
  11036. <table>
  11037. <colgroup> <col width=5%> <col width=95%> </colgroup>
  11038. <tr><td> <b>executeBeforeProc</b> </td><td>: FHEM Kommando (oder perl-Routine) vor dem Import ausführen </td></tr>
  11039. <tr><td> <b>executeAfterProc</b> </td><td>: FHEM Kommando (oder perl-Routine) nach dem Import ausführen </td></tr>
  11040. <tr><td> <b>expimpfile</b> </td><td>: der Name des Importfiles </td></tr>
  11041. </table>
  11042. </ul>
  11043. </li> <br>
  11044. </ul>
  11045. <br>
  11046. <li><b> maxValue [display | writeToDB] </b>
  11047. - berechnet den Maximalwert des Datenbankfelds "VALUE" in den Zeitgrenzen
  11048. (Attribute) "timestamp_begin", "timestamp_end" bzw. "timeDiffToNow / timeOlderThan".
  11049. Es muss das auszuwertende Reading über das <a href="#DbRepattr">Attribut</a> "reading"
  11050. angegeben sein.
  11051. Die Auswertung enthält den Zeitstempel des ermittelten Maximumwertes innerhalb der
  11052. Aggregation bzw. Zeitgrenzen.
  11053. Im Reading wird der Zeitstempel des <b>letzten</b> Auftretens vom Maximalwert ausgegeben
  11054. falls dieser Wert im Intervall mehrfach erreicht wird. <br>
  11055. Ist keine oder die Option "display" angegeben, werden die Ergebnisse nur angezeigt. Mit
  11056. der Option "writeToDB" werden die Berechnungsergebnisse mit einem neuen Readingnamen
  11057. in der Datenbank gespeichert. <br>
  11058. Der neue Readingname wird aus einem Präfix und dem originalen Readingnamen gebildet,
  11059. wobei der originale Readingname durch das Attribut "readingNameMap" ersetzt werden kann.
  11060. Der Präfix setzt sich aus der Bildungsfunktion und der Aggregation zusammen. <br>
  11061. Der Timestamp der neuen Readings in der Datenbank wird von der eingestellten Aggregationsperiode
  11062. abgeleitet, sofern kein eindeutiger Zeitpunkt des Ergebnisses bestimmt werden kann.
  11063. Das Feld "EVENT" wird mit "calculated" gefüllt.<br><br>
  11064. <ul>
  11065. <b>Beispiel neuer Readingname gebildet aus dem Originalreading "totalpac":</b> <br>
  11066. max_day_totalpac <br>
  11067. # &lt;Bildungsfunktion&gt;_&lt;Aggregation&gt;_&lt;Originalreading&gt; <br>
  11068. </ul>
  11069. <br>
  11070. Zusammengefasst sind die zur Steuerung dieser Funktion relevanten Attribute: <br><br>
  11071. <ul>
  11072. <table>
  11073. <colgroup> <col width=5%> <col width=95%> </colgroup>
  11074. <tr><td> <b>aggregation</b> </td><td>: Auswahl einer Aggregationsperiode </td></tr>
  11075. <tr><td> <b>limit</b> </td><td>: die Anzahl der ANZUZEIGENDEN Datensätz wird beschränkt </td></tr>
  11076. <tr><td> <b>device</b> </td><td>: einschließen oder ausschließen von Datensätzen die &lt;device&gt; enthalten </td></tr>
  11077. <tr><td> <b>reading</b> </td><td>: einschließen oder ausschließen von Datensätzen die &lt;reading&gt; enthalten </td></tr>
  11078. <tr><td> <b>readingNameMap</b> </td><td>: die entstehenden Ergebnisreadings werden partiell umbenannt </td></tr>
  11079. <tr><td> <b>time.*</b> </td><td>: eine Reihe von Attributen zur Zeitabgrenzung </td></tr>
  11080. <tr><td style="vertical-align:top"> <b>valueFilter</b> <td>: ein zusätzliches REGEXP um die Datenselektion zu steuern. Der REGEXP wird auf das Datenbankfeld 'VALUE' angewendet. </td></tr>
  11081. </table>
  11082. </ul>
  11083. <br>
  11084. </li> <br>
  11085. <li><b> minValue [display | writeToDB]</b>
  11086. - berechnet den Minimalwert des Datenbankfelds "VALUE" in den Zeitgrenzen
  11087. (Attribute) "timestamp_begin", "timestamp_end" bzw. "timeDiffToNow / timeOlderThan".
  11088. Es muss das auszuwertende Reading über das <a href="#DbRepattr">Attribut</a> "reading"
  11089. angegeben sein.
  11090. Die Auswertung enthält den Zeitstempel des ermittelten Minimumwertes innerhalb der
  11091. Aggregation bzw. Zeitgrenzen.
  11092. Im Reading wird der Zeitstempel des <b>ersten</b> Auftretens vom Minimalwert ausgegeben
  11093. falls dieser Wert im Intervall mehrfach erreicht wird. <br>
  11094. Ist keine oder die Option "display" angegeben, werden die Ergebnisse nur angezeigt. Mit
  11095. der Option "writeToDB" werden die Berechnungsergebnisse mit einem neuen Readingnamen
  11096. in der Datenbank gespeichert. <br>
  11097. Der neue Readingname wird aus einem Präfix und dem originalen Readingnamen gebildet,
  11098. wobei der originale Readingname durch das Attribut "readingNameMap" ersetzt werden kann.
  11099. Der Präfix setzt sich aus der Bildungsfunktion und der Aggregation zusammen. <br>
  11100. Der Timestamp der neuen Readings in der Datenbank wird von der eingestellten Aggregationsperiode
  11101. abgeleitet, sofern kein eindeutiger Zeitpunkt des Ergebnisses bestimmt werden kann.
  11102. Das Feld "EVENT" wird mit "calculated" gefüllt.<br><br>
  11103. <ul>
  11104. <b>Beispiel neuer Readingname gebildet aus dem Originalreading "totalpac":</b> <br>
  11105. min_day_totalpac <br>
  11106. # &lt;Bildungsfunktion&gt;_&lt;Aggregation&gt;_&lt;Originalreading&gt; <br>
  11107. </ul>
  11108. <br>
  11109. Zusammengefasst sind die zur Steuerung dieser Funktion relevanten Attribute: <br><br>
  11110. <ul>
  11111. <table>
  11112. <colgroup> <col width=5%> <col width=95%> </colgroup>
  11113. <tr><td> <b>aggregation</b> </td><td>: Auswahl einer Aggregationsperiode </td></tr>
  11114. <tr><td> <b>limit</b> </td><td>: die Anzahl der ANZUZEIGENDEN Datensätz wird beschränkt </td></tr>
  11115. <tr><td> <b>device</b> </td><td>: einschließen oder ausschließen von Datensätzen die &lt;device&gt; enthalten </td></tr>
  11116. <tr><td> <b>reading</b> </td><td>: einschließen oder ausschließen von Datensätzen die &lt;reading&gt; enthalten </td></tr>
  11117. <tr><td> <b>readingNameMap</b> </td><td>: die entstehenden Ergebnisreadings werden partiell umbenannt </td></tr>
  11118. <tr><td> <b>time.*</b> </td><td>: eine Reihe von Attributen zur Zeitabgrenzung </td></tr>
  11119. <tr><td style="vertical-align:top"> <b>valueFilter</b> <td>: ein zusätzliches REGEXP um die Datenselektion zu steuern. Der REGEXP wird auf das Datenbankfeld 'VALUE' angewendet. </td></tr>
  11120. </table>
  11121. </ul>
  11122. <br>
  11123. </li> <br>
  11124. <li><b> optimizeTables </b> - optimiert die Tabellen in der angeschlossenen Datenbank (MySQL). <br>
  11125. Vor und nach der Optimierung kann ein FHEM-Kommando ausgeführt werden.
  11126. (siehe <a href="#DbRepattr">Attribute</a> "executeBeforeProc", "executeAfterProc")
  11127. <br><br>
  11128. <ul>
  11129. <b>Hinweis:</b> <br>
  11130. Obwohl die Funktion selbst non-blocking ausgelegt ist, muß das zugeordnete DbLog-Device
  11131. im asynchronen Modus betrieben werden um ein Blockieren von FHEMWEB zu vermeiden. <br><br>
  11132. </li>
  11133. </ul><br>
  11134. <li><b> readingRename </b> - benennt den Namen eines Readings innerhalb der angeschlossenen Datenbank (siehe Internal DATABASE) um.
  11135. Der Readingname wird immer in der <b>gesamten</b> Datenbank umgesetzt. Eventuell
  11136. gesetzte Zeitgrenzen oder Beschränkungen durch die <a href="#DbRepattr">Attribute</a>
  11137. Device bzw. Reading werden nicht berücksichtigt. <br><br>
  11138. <ul>
  11139. <b>Beispiel: </b><br>
  11140. set &lt;name&gt; readingRename &lt;alter Readingname&gt;,&lt;neuer Readingname&gt; <br>
  11141. # Die Anzahl der umbenannten Device-Datensätze wird im Reading "reading_renamed"
  11142. ausgegeben. <br>
  11143. # Wird der umzubenennende Readingname in der Datenbank nicht gefunden, wird eine
  11144. WARNUNG im Reading "reading_not_renamed" ausgegeben. <br>
  11145. # Entsprechende Einträge erfolgen auch im Logfile mit verbose=3.
  11146. <br><br>
  11147. <b>Hinweis:</b> <br>
  11148. Obwohl die Funktion selbst non-blocking ausgelegt ist, sollte das zugeordnete DbLog-Device
  11149. im asynchronen Modus betrieben werden um ein Blockieren von FHEMWEB zu vermeiden (Tabellen-Lock). <br><br>
  11150. </li> <br>
  11151. </ul>
  11152. <li><b> reduceLog [average[=day]] </b> <br>
  11153. Reduziert historische Datensätze innerhalb der durch die "time.*"-Attribute bestimmten
  11154. Zeitgrenzen auf einen Eintrag (den ersten) pro Stunde je Device & Reading.
  11155. Es muss mindestens eines der "time.*"-Attribute gesetzt sein (siehe Tabelle unten).
  11156. Die jeweils fehlende Zeitabgrenzung wird in diesem Fall durch das Modul errechnet.
  11157. <br><br>
  11158. Durch die optionale Angabe von 'average' wird nicht nur die Datenbank bereinigt, sondern
  11159. alle numerischen Werte einer Stunde werden auf einen einzigen Mittelwert reduziert.
  11160. Mit der Option 'average=day' werden alle numerischen Werte eines Tages auf einen einzigen
  11161. Mittelwert reduziert (impliziert 'average'). <br><br>
  11162. Mit den Attributen "device" und "reading" können die zu berücksichtigenden Datensätze eingeschlossen
  11163. bzw. ausgeschlossen werden. Beide Eingrenzungen reduzieren die selektierten Daten und verringern den
  11164. Ressourcenbedarf.
  11165. Das Reading "reduceLogState" enthält das Ausführungsergebnis des letzten reduceLog-Befehls. <br><br>
  11166. Die für diese Funktion relevanten Attribute sind: <br><br>
  11167. <ul>
  11168. <table>
  11169. <colgroup> <col width=5%> <col width=95%> </colgroup>
  11170. <tr><td> <b>executeBeforeProc</b> </td><td>: FHEM Kommando (oder perl-Routine) vor dem Export ausführen </td></tr>
  11171. <tr><td> <b>executeAfterProc</b> </td><td>: FHEM Kommando (oder perl-Routine) nach dem Export ausführen </td></tr>
  11172. <tr><td> <b>device</b> </td><td>: einschließen oder ausschließen von Datensätzen die &lt;device&gt; enthalten </td></tr>
  11173. <tr><td> <b>reading</b> </td><td>: einschließen oder ausschließen von Datensätzen die &lt;reading&gt; enthalten </td></tr>
  11174. <tr><td> <b>timeOlderThan</b> </td><td>: es werden Datenbankeinträge <b>älter</b> als dieses Attribut reduziert </td></tr>
  11175. <tr><td> <b>timestamp_end</b> </td><td>: es werden Datenbankeinträge <b>älter</b> als dieses Attribut reduziert </td></tr>
  11176. <tr><td> <b>timeDiffToNow</b> </td><td>: es werden Datenbankeinträge <b>neuer</b> als dieses Attribut reduziert </td></tr>
  11177. <tr><td> <b>timestamp_begin</b> </td><td>: es werden Datenbankeinträge <b>neuer</b> als dieses Attribut reduziert </td></tr>
  11178. <tr><td style="vertical-align:top"> <b>valueFilter</b> <td>: ein zusätzliches REGEXP um die Datenselektion zu steuern. Der REGEXP wird auf das Datenbankfeld 'VALUE' angewendet. </td></tr>
  11179. </table>
  11180. </ul>
  11181. <br>
  11182. Aus Kompatibilitätsgründen kann der Befehl optional durch die Zusätze "EXCLUDE" bzw. "INCLUDE"
  11183. ergänzt werden um device/reading Kombinationen von reduceLog auszuschließen bzw. einzuschließen: <br><br>
  11184. <ul>
  11185. "reduceLog [average[=day]] [EXCLUDE=device1:reading1,device2:reading2,...] [INCLUDE=device:reading]" <br><br>
  11186. </ul>
  11187. Diese Angabe wird als Regex ausgewertet und überschreibt die Attribute "device" und "reading",
  11188. die in diesem Fall nicht beachtet werden. <br><br>
  11189. <ul>
  11190. <b>Beispiele: </b><br><br>
  11191. attr &lt;name&gt; timeOlderThan = d:200 <br>
  11192. set &lt;name&gt; reduceLog <br>
  11193. # Datensätze die älter als 200 Tage sind, werden auf den ersten Eintrag pro Stunde je Device & Reading
  11194. reduziert. <br>
  11195. <br>
  11196. attr &lt;name&gt; timeDiffToNow = d:200 <br>
  11197. set &lt;name&gt; reduceLog average=day <br>
  11198. # Datensätze die neuer als 200 Tage sind, werden auf einen Eintrag pro Tag je Device & Reading
  11199. reduziert. <br>
  11200. <br>
  11201. attr &lt;name&gt; timeDiffToNow = d:30 <br>
  11202. attr &lt;name&gt; device = TYPE=SONOSPLAYER EXCLUDE=Sonos_Kueche <br>
  11203. attr &lt;name&gt; reading = room% EXCLUDE=roomNameAlias <br>
  11204. set &lt;name&gt; reduceLog <br>
  11205. # Datensätze die neuer als 30 Tage sind, die Devices vom Typ SONOSPLAYER sind
  11206. (außer Device "Sonos_Kueche"), die Readings mit "room" beginnen (außer "roomNameAlias"),
  11207. werden auf den ersten Eintrag pro Stunde je Device & Reading reduziert. <br>
  11208. <br>
  11209. attr &lt;name&gt; timeDiffToNow = d:10 <br>
  11210. attr &lt;name&gt; timeOlderThan = d:5 <br>
  11211. attr &lt;name&gt; device = Luftdaten_remote <br>
  11212. set &lt;name&gt; reduceLog average <br>
  11213. # Datensätze die älter als 5 und neuer als 10 Tage sind und DEVICE "Luftdaten_remote" enthalten,
  11214. werden bereinigt. Numerische Werte einer Stunde werden auf einen Mittelwert reduziert <br>
  11215. <br>
  11216. </ul>
  11217. <b>Hinweis:</b> <br>
  11218. Obwohl die Funktion selbst non-blocking ausgelegt ist, sollte das zugeordnete DbLog-Device
  11219. im asynchronen Modus betrieben werden um ein Blockieren von FHEMWEB zu vermeiden
  11220. (Tabellen-Lock). <br>
  11221. Weiterhin wird dringend empfohlen den standard INDEX 'Search_Idx' in der Tabelle 'history'
  11222. anzulegen ! <br>
  11223. Die Abarbeitung dieses Befehls dauert unter Umständen (ohne INDEX) extrem lange. <br><br>
  11224. </li> <br>
  11225. <li><b> repairSQLite </b> - repariert eine korrupte SQLite-Datenbank. <br>
  11226. Eine Korruption liegt im Allgemeinen vor wenn die Fehlermitteilung "database disk image is malformed"
  11227. im state des DbLog-Devices erscheint.
  11228. Wird dieses Kommando gestartet, wird das angeschlossene DbLog-Device zunächst automatisch für 10 Stunden
  11229. (36000 Sekunden) von der Datenbank getrennt (Trennungszeit). Nach Abschluss der Reparatur erfolgt
  11230. wieder eine sofortige Neuverbindung zur reparierten Datenbank. <br>
  11231. Dem Befehl kann eine abweichende Trennungszeit (in Sekunden) als Argument angegeben werden. <br>
  11232. Die korrupte Datenbank wird als &lt;database&gt;.corrupt im gleichen Verzeichnis gespeichert. <br><br>
  11233. <ul>
  11234. <b>Beispiel: </b><br>
  11235. set &lt;name&gt; repairSQLite <br>
  11236. # Die Datenbank wird repariert, Trennungszeit beträgt 10 Stunden <br>
  11237. set &lt;name&gt; repairSQLite 600 <br>
  11238. # Die Datenbank wird repariert, Trennungszeit beträgt 10 Minuten
  11239. <br><br>
  11240. <b>Hinweis:</b> <br>
  11241. Es ist nicht garantiert, dass die Reparatur erfolgreich verläuft und keine Daten verloren gehen.
  11242. Je nach Schwere der Korruption kann Datenverlust auftreten oder die Reparatur scheitern, auch wenn
  11243. kein Fehler im Ablauf signalisiert wird. Ein Backup der Datenbank sollte unbedingt vorhanden
  11244. sein ! <br><br>
  11245. </li> <br>
  11246. </ul>
  11247. <li><b> restoreMySQL &lt;File&gt; </b> - stellt die Datenbank aus einem serverSide- oder clientSide-Dump wieder her. <br>
  11248. Die Funktion stellt über eine Drop-Down Liste eine Dateiauswahl für den Restore zur Verfügung. <br><br>
  11249. <b>Verwendung eines serverSide-Dumps </b> <br>
  11250. Es wird der Inhalt der history-Tabelle aus einem serverSide-Dump wiederhergestellt.
  11251. Dazu ist das Verzeichnis "dumpDirRemote" des MySQL-Servers auf dem Client zu mounten
  11252. und im <a href="#DbRepattr">Attribut</a> "dumpDirLocal" dem DbRep-Device bekannt zu machen. <br>
  11253. Es werden alle Files mit der Endung "csv[.gzip]" und deren Name mit der
  11254. verbundenen Datenbank beginnt (siehe Internal DATABASE), aufgelistet.
  11255. <br><br>
  11256. <b>Verwendung eines clientSide-Dumps </b> <br>
  11257. Es werden alle Tabellen und eventuell vorhandenen Views wiederhergestellt.
  11258. Das Verzeichnis, in dem sich die Dump-Files befinden, ist im <a href="#DbRepattr">Attribut</a> "dumpDirLocal" dem
  11259. DbRep-Device bekannt zu machen. <br>
  11260. Es werden alle Files mit der Endung "sql[.gzip]" und deren Name mit der
  11261. verbundenen Datenbank beginnt (siehe Internal DATABASE), aufgelistet. <br>
  11262. Die Geschwindigkeit des Restores ist abhängig von der Servervariable "<b>max_allowed_packet</b>". Durch Veränderung
  11263. dieser Variable im File my.cnf kann die Geschwindigkeit angepasst werden. Auf genügend verfügbare Ressourcen (insbesondere
  11264. RAM) ist dabei zu achten. <br><br>
  11265. Der Datenbankuser benötigt Rechte zum Tabellenmanagement, z.B.: <br>
  11266. CREATE, ALTER, INDEX, DROP, SHOW VIEW, CREATE VIEW
  11267. <br><br>
  11268. </li><br>
  11269. <li><b> restoreSQLite &lt;File&gt;.sqlitebkp[.gzip] </b> - stellt das Backup einer SQLite-Datenbank wieder her. <br>
  11270. Die Funktion stellt über eine Drop-Down Liste die für den Restore zur Verfügung stehenden Dateien
  11271. zur Verfügung. Die aktuell in der Zieldatenbank enthaltenen Daten werden gelöscht bzw.
  11272. überschrieben.
  11273. Es werden alle Files mit der Endung "sqlitebkp[.gzip]" und deren Name mit dem Namen der
  11274. verbundenen Datenbank beginnt, aufgelistet . <br><br>
  11275. </li><br>
  11276. <li><b> sqlCmd </b> - führt ein beliebiges benutzerspezifisches Kommando aus. <br>
  11277. Enthält dieses Kommando eine Delete-Operation, muss zur Sicherheit das
  11278. <a href="#DbRepattr">Attribut</a> "allowDeletion" gesetzt sein. <br>
  11279. Bei der Ausführung dieses Kommandos werden keine Einschränkungen durch gesetzte Attribute
  11280. "device", "reading", "time.*" bzw. "aggregation" berücksichtigt. <br>
  11281. Sollen die im Modul gesetzten <a href="#DbRepattr">Attribute</a> "timestamp_begin" bzw.
  11282. "timestamp_end" im Statement berücksichtigt werden, können die Platzhalter
  11283. "<b>§timestamp_begin§</b>" bzw. "<b>§timestamp_end§</b>" dafür verwendet werden. <br><br>
  11284. Soll ein Datensatz upgedated werden, ist dem Statement "TIMESTAMP=TIMESTAMP" hinzuzufügen um eine Änderung des
  11285. originalen Timestamps zu verhindern. <br><br>
  11286. <ul>
  11287. <b>Beispiele für Statements: </b> <br><br>
  11288. <ul>
  11289. <li>set &lt;name&gt; sqlCmd select DEVICE, count(*) from history where TIMESTAMP >= "2017-01-06 00:00:00" group by DEVICE having count(*) > 800 </li>
  11290. <li>set &lt;name&gt; sqlCmd select DEVICE, count(*) from history where TIMESTAMP >= "2017-05-06 00:00:00" group by DEVICE </li>
  11291. <li>set &lt;name&gt; sqlCmd select DEVICE, count(*) from history where TIMESTAMP >= §timestamp_begin§ group by DEVICE </li>
  11292. <li>set &lt;name&gt; sqlCmd select * from history where DEVICE like "Te%t" order by `TIMESTAMP` desc </li>
  11293. <li>set &lt;name&gt; sqlCmd select * from history where `TIMESTAMP` > "2017-05-09 18:03:00" order by `TIMESTAMP` desc </li>
  11294. <li>set &lt;name&gt; sqlCmd select * from current order by `TIMESTAMP` desc </li>
  11295. <li>set &lt;name&gt; sqlCmd select sum(VALUE) as 'Einspeisung am 04.05.2017', count(*) as 'Anzahl' FROM history where `READING` = "Einspeisung_WirkP_Zaehler_Diff" and TIMESTAMP between '2017-05-04' AND '2017-05-05' </li>
  11296. <li>set &lt;name&gt; sqlCmd delete from current </li>
  11297. <li>set &lt;name&gt; sqlCmd delete from history where TIMESTAMP < "2016-05-06 00:00:00" </li>
  11298. <li>set &lt;name&gt; sqlCmd update history set TIMESTAMP=TIMESTAMP,VALUE='Val' WHERE VALUE='TestValue' </li>
  11299. <li>set &lt;name&gt; sqlCmd select * from history where DEVICE = "Test" </li>
  11300. <li>set &lt;name&gt; sqlCmd insert into history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES ('2017-05-09 17:00:14','Test','manuell','manuell','Tes§e','TestValue','°C') </li>
  11301. </ul>
  11302. <br>
  11303. Das Ergebnis des Statements wird im <a href="#DbRepReadings">Reading</a> "SqlResult" dargestellt.
  11304. Die Ergebnis-Formatierung kann durch das <a href="#DbRepattr">Attribut</a> "sqlResultFormat" ausgewählt, sowie der verwendete
  11305. Feldtrenner durch das <a href="#DbRepattr">Attribut</a> "sqlResultFieldSep" festgelegt werden. <br><br>
  11306. Das Modul stellt optional eine Kommando-Historie zur Verfügung sobald ein SQL-Kommando erfolgreich
  11307. ausgeführt wurde.
  11308. Um diese Option zu nutzen, ist das Attribut "sqlCmdHistoryLength" mit der gewünschten Listenlänge
  11309. zu aktivieren. <br><br>
  11310. Zur besseren Übersicht sind die zur Steuerung von sqlCmd relevanten Attribute hier noch einmal
  11311. dargestellt: <br><br>
  11312. <ul>
  11313. <table>
  11314. <colgroup> <col width=5%> <col width=95%> </colgroup>
  11315. <tr><td> <b>allowDeletion</b> </td><td>: aktiviert Löschmöglichkeit </td></tr>
  11316. <tr><td> <b>sqlResultFormat</b> </td><td>: legt die Darstellung des Kommandoergebnis fest </td></tr>
  11317. <tr><td> <b>sqlResultFieldSep</b> </td><td>: Auswahl Feldtrenner im Ergebnis </td></tr>
  11318. <tr><td> <b>sqlCmdHistoryLength</b> </td><td>: Aktivierung Kommando-Historie und deren Umfang</td></tr>
  11319. </table>
  11320. </ul>
  11321. <br>
  11322. <br>
  11323. <b>Hinweis:</b> <br>
  11324. Auch wenn das Modul bezüglich der Datenbankabfrage nichtblockierend arbeitet, kann eine
  11325. zu große Ergebnismenge (Anzahl Zeilen bzw. Readings) die Browsersesssion bzw. FHEMWEB
  11326. blockieren. Wenn man sich unsicher ist, sollte man vorsorglich dem Statement ein Limit
  11327. hinzufügen. <br><br>
  11328. </li> <br>
  11329. </ul>
  11330. <li><b> sqlCmdHistory </b> - Wenn mit dem <a href="#DbRepattr">Attribut</a> "sqlCmdHistoryLength" aktiviert, kann
  11331. aus einer Liste ein bereits erfolgreich ausgeführtes sqlCmd-Kommando wiederholt werden. <br>
  11332. Mit Ausführung des letzten Eintrags der Liste, "__purge_historylist__", kann die Liste gelöscht
  11333. werden. <br>
  11334. Falls das Statement "," enthält, wird dieses Zeichen aus technischen Gründen in der
  11335. History-Liste als "&lt;c&gt;" dargestellt. <br>
  11336. </li><br>
  11337. <li><b> sqlSpecial </b> - Die Funktion bietet eine Drop-Downliste mit einer Auswahl vorbereiter Auswertungen
  11338. an. <br>
  11339. Das Ergebnis des Statements wird im Reading "SqlResult" dargestellt.
  11340. Die Ergebnis-Formatierung kann durch das <a href="#DbRepattr">Attribut</a> "sqlResultFormat"
  11341. ausgewählt, sowie der verwendete Feldtrenner durch das <a href="#DbRepattr">Attribut</a>
  11342. "sqlResultFieldSep" festgelegt werden. <br><br>
  11343. Die für diese Funktion relevanten Attribute sind: <br><br>
  11344. <ul>
  11345. <table>
  11346. <colgroup> <col width=5%> <col width=95%> </colgroup>
  11347. <tr><td> <b>sqlResultFormat</b> </td><td>: Optionen der Ergebnisformatierung </td></tr>
  11348. <tr><td> <b>sqlResultFieldSep</b> </td><td>: Auswahl des Trennzeichens zwischen Ergebnisfeldern </td></tr>
  11349. </table>
  11350. </ul>
  11351. <br>
  11352. Es sind die folgenden vordefinierte Auswertungen auswählbar: <br><br>
  11353. <ul>
  11354. <table>
  11355. <colgroup> <col width=5%> <col width=95%> </colgroup>
  11356. <tr><td> <b>50mostFreqLogsLast2days </b> </td><td>: ermittelt die 50 am häufigsten vorkommenden Loggingeinträge der letzten 2 Tage </td></tr>
  11357. <tr><td> <b>allDevCount </b> </td><td>: alle in der Datenbank vorkommenden Devices und deren Anzahl </td></tr>
  11358. <tr><td> <b>allDevReadCount </b> </td><td>: alle in der Datenbank vorkommenden Device/Reading-Kombinationen und deren Anzahl</td></tr>
  11359. </table>
  11360. </ul>
  11361. </li><br><br>
  11362. <li><b> sumValue [display | writeToDB]</b>
  11363. - Berechnet die Summenwerte des Datenbankfelds "VALUE" in den Zeitgrenzen
  11364. (Attribute) "timestamp_begin", "timestamp_end" bzw. "timeDiffToNow / timeOlderThan".
  11365. Es muss das auszuwertende Reading im <a href="#DbRepattr">Attribut</a> "reading"
  11366. angegeben sein. Diese Funktion ist sinnvoll wenn fortlaufend Wertedifferenzen eines
  11367. Readings in die Datenbank geschrieben werden. <br>
  11368. Ist keine oder die Option "display" angegeben, werden die Ergebnisse nur angezeigt. Mit
  11369. der Option "writeToDB" werden die Berechnungsergebnisse mit einem neuen Readingnamen
  11370. in der Datenbank gespeichert. <br>
  11371. Der neue Readingname wird aus einem Präfix und dem originalen Readingnamen gebildet,
  11372. wobei der originale Readingname durch das Attribut "readingNameMap" ersetzt werden kann.
  11373. Der Präfix setzt sich aus der Bildungsfunktion und der Aggregation zusammen. <br>
  11374. Der Timestamp der neuen Readings in der Datenbank wird von der eingestellten Aggregationsperiode
  11375. abgeleitet, sofern kein eindeutiger Zeitpunkt des Ergebnisses bestimmt werden kann.
  11376. Das Feld "EVENT" wird mit "calculated" gefüllt.<br><br>
  11377. <ul>
  11378. <b>Beispiel neuer Readingname gebildet aus dem Originalreading "totalpac":</b> <br>
  11379. sum_day_totalpac <br>
  11380. # &lt;Bildungsfunktion&gt;_&lt;Aggregation&gt;_&lt;Originalreading&gt; <br>
  11381. </ul>
  11382. <br>
  11383. Zusammengefasst sind die zur Steuerung dieser Funktion relevanten Attribute: <br><br>
  11384. <ul>
  11385. <table>
  11386. <colgroup> <col width=5%> <col width=95%> </colgroup>
  11387. <tr><td> <b>aggregation</b> </td><td>: Auswahl einer Aggregationsperiode </td></tr>
  11388. <tr><td> <b>limit</b> </td><td>: die Anzahl der ANZUZEIGENDEN Datensätz wird beschränkt </td></tr>
  11389. <tr><td> <b>device</b> </td><td>: einschließen oder ausschließen von Datensätzen die &lt;device&gt; enthalten </td></tr>
  11390. <tr><td> <b>reading</b> </td><td>: einschließen oder ausschließen von Datensätzen die &lt;reading&gt; enthalten </td></tr>
  11391. <tr><td> <b>readingNameMap</b> </td><td>: die entstehenden Ergebnisreadings werden partiell umbenannt </td></tr>
  11392. <tr><td> <b>time.*</b> </td><td>: eine Reihe von Attributen zur Zeitabgrenzung </td></tr>
  11393. <tr><td style="vertical-align:top"> <b>valueFilter</b> <td>: ein zusätzliches REGEXP um die Datenselektion zu steuern. Der REGEXP wird auf das Datenbankfeld 'VALUE' angewendet. </td></tr>
  11394. </table>
  11395. </ul>
  11396. <br>
  11397. </li> <br>
  11398. <li><b> syncStandby &lt;DbLog-Device Standby&gt; </b>
  11399. - Es werden die Datensätze aus der angeschlossenen Datenbank (Quelle) direkt in eine weitere
  11400. Datenbank (Standby-Datenbank) übertragen.
  11401. Dabei ist "&lt;DbLog-Device Standby&gt;" das DbLog-Device, welches mit der Standby-Datenbank
  11402. verbunden ist. <br><br>
  11403. Es werden alle Datensätze übertragen, die durch Timestamp-<a href="#limit">Attribute</a>
  11404. bzw. die Attribute "device", "reading" bestimmt sind. <br>
  11405. Die Datensätze werden dabei in Zeitscheiben entsprechend der eingestellten Aggregation übertragen.
  11406. Hat das Attribut "aggregation" den Wert "no" oder "month", werden die Datensätze automatisch
  11407. in Tageszeitscheiben zur Standby-Datenbank übertragen.
  11408. Quell- und Standby-Datenbank können unterschiedlichen Typs sein.
  11409. <br><br>
  11410. Die zur Steuerung der syncStandby Funktion relevanten Attribute sind: <br><br>
  11411. <ul>
  11412. <table>
  11413. <colgroup> <col width=5%> <col width=95%> </colgroup>
  11414. <tr><td> <b>aggregation</b> </td><td>: Einstellung der Zeitscheiben zur Übertragung (hour,day,week) </td></tr>
  11415. <tr><td> <b>device</b> </td><td>: einschließen oder ausschließen von Datensätzen die &lt;device&gt; enthalten </td></tr>
  11416. <tr><td> <b>reading</b> </td><td>: einschließen oder ausschließen von Datensätzen die &lt;reading&gt; enthalten </td></tr>
  11417. <tr><td> <b>time.*</b> </td><td>: Attribute zur Zeitabgrenzung der zu übertragenden Datensätze. </td></tr>
  11418. <tr><td style="vertical-align:top"> <b>valueFilter</b> <td>: ein zusätzliches REGEXP um die Datenselektion zu steuern. Der REGEXP wird auf das Datenbankfeld 'VALUE' angewendet. </td></tr>
  11419. </table>
  11420. </ul>
  11421. <br>
  11422. </li> <br>
  11423. <li><b> tableCurrentFillup </b> - Die current-Tabelle wird mit einem Extrakt der history-Tabelle aufgefüllt.
  11424. Die <a href="#DbRepattr">Attribute</a> zur Zeiteinschränkung bzw. device, reading werden ausgewertet.
  11425. Dadurch kann der Inhalt des Extrakts beeinflusst werden. Im zugehörigen DbLog-Device sollte sollte das Attribut
  11426. "DbLogType=SampleFill/History" gesetzt sein. </li> <br>
  11427. <li><b> tableCurrentPurge </b> - löscht den Inhalt der current-Tabelle. Es werden keine Limitierungen, z.B. durch die Attribute "timestamp_begin",
  11428. "timestamp_end", device, reading, usw. , ausgewertet. </li> <br>
  11429. <li><b> vacuum </b> - optimiert die Tabellen in der angeschlossenen Datenbank (SQLite, PostgreSQL). <br>
  11430. Vor und nach der Optimierung kann ein FHEM-Kommando ausgeführt werden.
  11431. (siehe <a href="#DbRepattr">Attribute</a> "executeBeforeProc", "executeAfterProc")
  11432. <br><br>
  11433. <ul>
  11434. <b>Hinweis:</b> <br>
  11435. Obwohl die Funktion selbst non-blocking ausgelegt ist, muß das zugeordnete DbLog-Device
  11436. im asynchronen Modus betrieben werden um ein Blockieren von FHEM zu vermeiden. <br><br>
  11437. </li>
  11438. </ul><br>
  11439. <br>
  11440. </ul></ul>
  11441. <b>Für alle Auswertungsvarianten (Ausnahme sqlCmd,deviceRename,readingRename) gilt: </b> <br>
  11442. Zusätzlich zu dem auszuwertenden Reading kann das Device mit angegeben werden um das Reporting nach diesen Kriterien einzuschränken.
  11443. Sind keine Zeitgrenzen-Attribute angegeben jedoch das Aggregations-Attribut gesetzt, wird der Zeitstempel des ältesten
  11444. Datensatzes in der Datenbank als Startdatum und das aktuelle Datum/die aktuelle Zeit als Zeitgrenze genutzt.
  11445. Konnte der älteste Datensatz in der Datenbank nicht ermittelt werden, wird '1970-01-01 01:00:00' als Selektionsstart
  11446. genutzt (siehe get &lt;name&gt; minTimestamp).
  11447. Sind weder Zeitgrenzen-Attribute noch Aggregation angegeben, wird die Datenselektion ohne Timestamp-Einschränkungen
  11448. ausgeführt.
  11449. <br><br>
  11450. <b>Hinweis: </b> <br>
  11451. In der Detailansicht kann ein Browserrefresh nötig sein um die Operationsergebnisse zu sehen sobald im DeviceOverview "state = done" angezeigt wird.
  11452. <br><br>
  11453. </ul>
  11454. <a name="DbRepget"></a>
  11455. <b>Get </b>
  11456. <ul>
  11457. Die Get-Kommandos von DbRep dienen dazu eine Reihe von Metadaten der verwendeten Datenbankinstanz abzufragen.
  11458. Dies sind zum Beispiel eingestellte Serverparameter, Servervariablen, Datenbankstatus- und Tabelleninformationen. Die verfügbaren get-Funktionen
  11459. sind von dem verwendeten Datenbanktyp abhängig. So ist für SQLite z.Zt. nur "svrinfo" verfügbar. Die Funktionen liefern nativ sehr viele Ausgabewerte,
  11460. die über über funktionsspezifische <a href="#DbRepattr">Attribute</a> abgrenzbar sind. Der Filter ist als kommaseparierte Liste anzuwenden.
  11461. Dabei kann SQL-Wildcard (%) verwendet werden.
  11462. <br><br>
  11463. <b>Hinweis: </b> <br>
  11464. Nach der Ausführung einer get-Funktion in der Detailsicht einen Browserrefresh durchführen um die Ergebnisse zu sehen !
  11465. <br><br>
  11466. <ul><ul>
  11467. <li><b> blockinginfo </b> - Listet die aktuell systemweit laufenden Hintergrundprozesse (BlockingCalls) mit ihren Informationen auf.
  11468. Zu lange Zeichenketten (z.B. Argumente) werden gekürzt ausgeschrieben.
  11469. </li>
  11470. <br><br>
  11471. <li><b> dbstatus </b> - Listet globale Informationen zum MySQL Serverstatus (z.B. Informationen zum Cache, Threads, Bufferpools, etc. ).
  11472. Es werden zunächst alle verfügbaren Informationen berichtet. Mit dem <a href="#DbRepattr">Attribut</a> "showStatus" kann die
  11473. Ergebnismenge eingeschränkt werden, um nur gewünschte Ergebnisse abzurufen. Detailinformationen zur Bedeutung der einzelnen Readings
  11474. sind <a href=http://dev.mysql.com/doc/refman/5.7/en/server-status-variables.html>hier</a> verfügbar. <br>
  11475. <br><ul>
  11476. <b>Bespiel</b> <br>
  11477. get &lt;name&gt; dbstatus <br>
  11478. attr &lt;name&gt; showStatus %uptime%,%qcache% <br>
  11479. # Es werden nur Readings erzeugt die im Namen "uptime" und "qcache" enthalten
  11480. </li>
  11481. <br><br>
  11482. </ul>
  11483. <li><b> dbValue &lt;SQL-Statement&gt;</b> -
  11484. Führt das angegebene SQL-Statement <b>blockierend</b> aus. Diese Funktion ist durch ihre Arbeitsweise
  11485. speziell für den Einsatz in benutzerspezifischen Scripten geeignet. <br>
  11486. Die Eingabe akzeptiert Mehrzeiler und gibt ebenso mehrzeilige Ergebisse zurück.
  11487. Werden mehrere Felder selektiert und zurückgegeben, erfolgt die Feldtrennung mit dem Trenner
  11488. des <a href="#DbRepattr">Attributes</a> "sqlResultFieldSep" (default "|"). Mehrere Ergebniszeilen
  11489. werden mit Newline ("\n") separiert. <br>
  11490. Diese Funktion setzt/aktualisiert nur Statusreadings, die Funktion im Attribut "userExitFn"
  11491. wird nicht aufgerufen.
  11492. <br>
  11493. <br><ul>
  11494. <b>Bespiele zur Nutzung im FHEMWEB</b> <br>
  11495. {fhem("get &lt;name&gt; dbValue select device,count(*) from history where timestamp > '2018-04-01' group by device")} <br>
  11496. get &lt;name&gt; dbValue select device,count(*) from history where timestamp > '2018-04-01' group by device <br>
  11497. {CommandGet(undef,"Rep.LogDB1 dbValue select device,count(*) from history where timestamp > '2018-04-01' group by device")} <br>
  11498. </ul>
  11499. <br><br>
  11500. Erstellt man eine kleine Routine in 99_myUtils, wie z.B.:
  11501. <br>
  11502. <pre>
  11503. sub dbval($$) {
  11504. my ($name,$cmd) = @_;
  11505. my $ret = CommandGet(undef,"$name dbValue $cmd");
  11506. return $ret;
  11507. }
  11508. </pre>
  11509. kann dbValue vereinfacht verwendet werden mit Aufrufen wie:
  11510. <br><br>
  11511. <ul>
  11512. <b>Bespiele</b> <br>
  11513. {dbval("&lt;name&gt;","select count(*) from history")} <br>
  11514. oder <br>
  11515. $ret = dbval("&lt;name&gt;","select count(*) from history"); <br>
  11516. </ul>
  11517. </li>
  11518. <br><br>
  11519. <li><b> dbvars </b> - Zeigt die globalen Werte der MySQL Systemvariablen. Enthalten sind zum Beispiel Angaben zum InnoDB-Home, dem Datafile-Pfad,
  11520. Memory- und Cache-Parameter, usw. Die Ausgabe listet zunächst alle verfügbaren Informationen auf. Mit dem
  11521. <a href="#DbRepattr">Attribut</a> "showVariables" kann die Ergebnismenge eingeschränkt werden um nur gewünschte Ergebnisse
  11522. abzurufen. Weitere Informationen zur Bedeutung der ausgegebenen Variablen sind
  11523. <a href=http://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html>hier</a> verfügbar. <br>
  11524. <br><ul>
  11525. <b>Bespiel</b> <br>
  11526. get &lt;name&gt; dbvars <br>
  11527. attr &lt;name&gt; showVariables %version%,%query_cache% <br>
  11528. # Es werden nur Readings erzeugt die im Namen "version" und "query_cache" enthalten
  11529. </li>
  11530. <br><br>
  11531. </ul>
  11532. <li><b> minTimestamp </b> - Ermittelt den Zeitstempel des ältesten Datensatzes in der Datenbank (wird implizit beim Start von
  11533. FHEM ausgeführt).
  11534. Der Zeitstempel wird als Selektionsbeginn verwendet wenn kein Zeitattribut den Selektionsbeginn
  11535. festlegt.
  11536. </li>
  11537. <br><br>
  11538. <li><b> procinfo </b> - Listet die existierenden Datenbank-Prozesse in einer Tabelle auf (nur MySQL). <br>
  11539. Typischerweise werden nur die Prozesse des Verbindungsusers (angegeben in DbLog-Konfiguration)
  11540. ausgegeben. Sollen alle Prozesse angezeigt werden, ist dem User das globale Recht "PROCESS"
  11541. einzuräumen. <br>
  11542. Für bestimmte SQL-Statements wird seit MariaDB 5.3 ein Fortschrittsreporting (Spalte "PROGRESS")
  11543. ausgegeben. Zum Beispiel kann der Abarbeitungsgrad bei der Indexerstellung verfolgt werden. <br>
  11544. Weitere Informationen sind
  11545. <a href=https://mariadb.com/kb/en/mariadb/show-processlist/>hier</a> verfügbar. <br>
  11546. </li>
  11547. <br><br>
  11548. <li><b> svrinfo </b> - allgemeine Datenbankserver-Informationen wie z.B. die DBMS-Version, Serveradresse und Port usw. Die Menge der Listenelemente
  11549. ist vom Datenbanktyp abhängig. Mit dem <a href="#DbRepattr">Attribut</a> "showSvrInfo" kann die Ergebnismenge eingeschränkt werden.
  11550. Weitere Erläuterungen zu den gelieferten Informationen sind
  11551. <a href=https://msdn.microsoft.com/en-us/library/ms711681(v=vs.85).aspx>hier</a> zu finden. <br>
  11552. <br><ul>
  11553. <b>Bespiel</b> <br>
  11554. get &lt;name&gt; svrinfo <br>
  11555. attr &lt;name&gt; showSvrInfo %SQL_CATALOG_TERM%,%NAME% <br>
  11556. # Es werden nur Readings erzeugt die im Namen "SQL_CATALOG_TERM" und "NAME" enthalten
  11557. </li>
  11558. <br><br>
  11559. </ul>
  11560. <li><b> tableinfo </b> - ruft Tabelleninformationen aus der mit dem DbRep-Device verbundenen Datenbank ab (MySQL).
  11561. Es werden per default alle in der verbundenen Datenbank angelegten Tabellen ausgewertet.
  11562. Mit dem <a href="#DbRepattr">Attribut</a> "showTableInfo" können die Ergebnisse eingeschränkt werden. Erläuterungen zu den erzeugten
  11563. Readings sind <a href=http://dev.mysql.com/doc/refman/5.7/en/show-table-status.html>hier</a> zu finden. <br>
  11564. <br><ul>
  11565. <b>Bespiel</b> <br>
  11566. get &lt;name&gt; tableinfo <br>
  11567. attr &lt;name&gt; showTableInfo current,history <br>
  11568. # Es werden nur Information der Tabellen "current" und "history" angezeigt
  11569. </li>
  11570. <br><br>
  11571. </ul>
  11572. <li><b> versionNotes [hints | rel | &lt;key&gt;] </b> -
  11573. Zeigt Release Informationen und/oder Hinweise zum Modul an. Es sind nur Release Informationen mit
  11574. Bedeutung für den Modulnutzer enthalten. <br>
  11575. Sind keine Optionen angegben, werden sowohl Release Informationen als auch Hinweise angezeigt.
  11576. "rel" zeigt nur Release Informationen und "hints" nur Hinweise an. Mit der &lt;key&gt;-Angabe
  11577. wird der Hinweis mit der angegebenen Nummer angezeigt.
  11578. </li>
  11579. <br>
  11580. </ul></ul>
  11581. </ul>
  11582. <a name="DbRepattr"></a>
  11583. <b>Attribute</b>
  11584. <br>
  11585. <ul>
  11586. Über die modulspezifischen Attribute wird die Abgrenzung der Auswertung und die Aggregation der Werte gesteuert. <br>
  11587. Die hier aufgeführten Attribute sind nicht für jede Funktion des Moduls bedeutsam. In der Hilfe zu den set/get-Kommandos
  11588. wird explizit angegeben, welche Attribute für das jeweilige Kommando relevant sind. <br><br>
  11589. <b>Hinweis zur SQL-Wildcard Verwendung:</b> <br>
  11590. Innerhalb der Attribut-Werte für "device" und "reading" kann SQL-Wildcards "%" angegeben werden.
  11591. Dabei wird "%" als Platzhalter für beliebig viele Zeichen verwendet.
  11592. Das Zeichen "_" wird nicht als SQL-Wildcard supported. <br>
  11593. Dies gilt für alle Funktionen <b>ausser</b> "insert", "importFromFile" und "deviceRename". <br>
  11594. Die Funktion "insert" erlaubt nicht, dass die genannten Attribute das Wildcard "%" enthalten. Character "_" wird als normales Zeichen gewertet.<br>
  11595. In Ergebnis-Readings wird das Wildcardzeichen "%" durch "/" ersetzt um die Regeln für erlaubte Zeichen in Readings einzuhalten.
  11596. <br><br>
  11597. <ul><ul>
  11598. <a name="aggregation"></a>
  11599. <li><b>aggregation </b> - Zusammenfassung der Device/Reading-Selektionen in Stunden,Tage,Kalenderwochen,Kalendermonaten
  11600. oder "no". <br>
  11601. Liefert z.B. die Anzahl der DB-Einträge am Tag (countEntries), Summierung von
  11602. Differenzwerten eines Readings (sumValue), usw. <br>
  11603. Mit Aggregation "no" (default) erfolgt keine Zusammenfassung in einem Zeitraum, sondern die
  11604. Ausgabe wird aus allen Werten einer Device/Reading-Kombination zwischen den definierten
  11605. Zeiträumen ermittelt. </li> <br>
  11606. <a name="allowDeletion"></a>
  11607. <li><b>allowDeletion </b> - schaltet die Löschfunktion des Moduls frei </li> <br>
  11608. <a name="averageCalcForm"></a>
  11609. <li><b>averageCalcForm </b> - legt die Berechnungsvariante für die Ermittlung des Durchschnittswertes mit "averageValue"
  11610. fest.<br><br>
  11611. Zur Zeit sind folgende Varianten implementiert: <br><br>
  11612. <ul>
  11613. <table>
  11614. <colgroup> <col width=20%> <col width=80%> </colgroup>
  11615. <tr><td> <b>avgArithmeticMean :</b> </td><td>es wird der arithmetische Mittelwert berechnet (default) </td></tr>
  11616. <tr><td style="vertical-align:top"> <b>avgDailyMeanGWS :</b> <td>berechnet die Tagesmitteltemperatur entsprechend den
  11617. Vorschriften des deutschen Wetterdienstes (siehe "get &lt;name&gt; versionNotes 2"). <br>
  11618. Diese Variante verwendet automatisch die Aggregation "day". </td></tr>
  11619. <tr><td> <b>avgTimeWeightMean :</b> </td><td>berechnet den zeitgewichteten Mittelwert </td></tr>
  11620. </table>
  11621. </ul>
  11622. </li><br>
  11623. <a name="countEntriesDetail"></a>
  11624. <li><b>countEntriesDetail </b> - Wenn gesetzt, erstellt die Funktion "countEntries" eine detallierte Ausgabe der Datensatzzahl
  11625. pro Reading und Zeitintervall.
  11626. Standardmäßig wird nur die Summe aller selektierten Datensätze ausgegeben.
  11627. </li> <br>
  11628. <a name="device"></a>
  11629. <li><b>device </b> - Abgrenzung der DB-Selektionen auf ein bestimmtes oder mehrere Devices. <br>
  11630. Es können Geräte-Spezifikationen (devspec) angegeben werden. <br>
  11631. Innerhalb von Geräte-Spezifikationen wird SQL-Wildcard (%) als normales ASCII-Zeichen gewertet.
  11632. Die Devicenamen werden vor der Selektion aus der Geräte-Spezifikationen und den aktuell in FHEM
  11633. vorhandenen Devices abgeleitet. <br>
  11634. Wird dem Device bzw. der Device-Liste oder Geräte-Spezifikation ein "EXCLUDE=" vorangestellt,
  11635. werden diese Devices von der Selektion ausgeschlossen.
  11636. </li> <br>
  11637. <ul>
  11638. <b>Beispiele:</b> <br>
  11639. <code>attr &lt;name&gt; device TYPE=DbRep </code> <br>
  11640. <code>attr &lt;name&gt; device MySTP_5000 </code> <br>
  11641. <code>attr &lt;name&gt; device SMA.*,MySTP.* </code> <br>
  11642. <code>attr &lt;name&gt; device SMA_Energymeter,MySTP_5000 </code> <br>
  11643. <code>attr &lt;name&gt; device %5000 </code> <br>
  11644. <code>attr &lt;name&gt; device TYPE=SSCam EXCLUDE=SDS1_SVS </code> <br>
  11645. <code>attr &lt;name&gt; device EXCLUDE=SDS1_SVS </code> <br>
  11646. <code>attr &lt;name&gt; device EXCLUDE=TYPE=SSCam </code> <br>
  11647. </ul>
  11648. <br>
  11649. Falls weitere Informationen zu Geräte-Spezifikationen benötigt werden, bitte
  11650. "get &lt;name&gt; versionNotes 3" ausführen.
  11651. <br><br>
  11652. <a name="diffAccept"></a>
  11653. <li><b>diffAccept </b> - gilt für Funktion diffValue. diffAccept legt fest bis zu welchem Schwellenwert eine berechnete positive Werte-Differenz
  11654. zwischen zwei unmittelbar aufeinander folgenden Datensätzen akzeptiert werden soll (Standard ist 20). <br>
  11655. Damit werden fehlerhafte DB-Einträge mit einem unverhältnismäßig hohen Differenzwert von der Berechnung ausgeschlossen und
  11656. verfälschen nicht das Ergebnis. Sollten Schwellenwertüberschreitungen vorkommen, wird das Reading "diff_overrun_limit_&lt;diffLimit&gt;"
  11657. erstellt. (&lt;diffLimit&gt; wird dabei durch den aktuellen Attributwert ersetzt)
  11658. Es enthält eine Liste der relevanten Wertepaare. Mit verbose 3 werden diese Datensätze ebenfalls im Logfile protokolliert.
  11659. </li> <br>
  11660. <ul>
  11661. Beispiel Ausgabe im Logfile beim Überschreiten von diffAccept=10: <br><br>
  11662. DbRep Rep.STP5000.etotal -> data ignored while calc diffValue due to threshold overrun (diffAccept = 10): <br>
  11663. 2016-04-09 08:50:50 0.0340 -> 2016-04-09 12:42:01 13.3440 <br><br>
  11664. # Der erste Datensatz mit einem Wert von 0.0340 ist untypisch gering zum nächsten Wert 13.3440 und führt zu einem zu hohen
  11665. Differenzwert. <br>
  11666. # Es ist zu entscheiden ob der Datensatz gelöscht, ignoriert, oder das Attribut diffAccept angepasst werden sollte.
  11667. </ul><br>
  11668. <a name="disable"></a>
  11669. <li><b>disable </b> - deaktiviert das Modul </li> <br>
  11670. <a name="dumpComment"></a>
  11671. <li><b>dumpComment </b> - User-Kommentar. Er wird im Kopf des durch den Befehl "dumpMyQL clientSide" erzeugten Dumpfiles
  11672. eingetragen. </li> <br>
  11673. <a name="dumpCompress"></a>
  11674. <li><b>dumpCompress </b> - wenn gesetzt, werden die Dumpfiles nach "dumpMySQL" bzw. "dumpSQLite" komprimiert </li> <br>
  11675. <a name="dumpDirLocal"></a>
  11676. <li><b>dumpDirLocal </b> - Zielverzeichnis für die Erstellung von Dumps mit "dumpMySQL clientSide".
  11677. default: "{global}{modpath}/log/" auf dem FHEM-Server. <br>
  11678. Ebenfalls werden in diesem Verzeichnis alte Backup-Files durch die interne Versionsverwaltung von
  11679. "dumpMySQL" gesucht und gelöscht wenn die gefundene Anzahl den Attributwert "dumpFilesKeep"
  11680. überschreitet. Das Attribut dient auch dazu ein lokal gemountetes Verzeichnis "dumpDirRemote"
  11681. DbRep bekannt zu machen. </li> <br>
  11682. <a name="dumpDirRemote"></a>
  11683. <li><b>dumpDirRemote </b> - Zielverzeichnis für die Erstellung von Dumps mit "dumpMySQL serverSide".
  11684. default: das Home-Dir des MySQL-Servers auf dem MySQL-Host </li> <br>
  11685. <a name="dumpMemlimit"></a>
  11686. <li><b>dumpMemlimit </b> - erlaubter Speicherverbrauch für das Dump SQL-Script zur Generierungszeit (default: 100000 Zeichen).
  11687. Bitte den Parameter anpassen, falls es zu Speicherengpässen und damit verbundenen Performanceproblemen
  11688. kommen sollte. </li> <br>
  11689. <a name="dumpSpeed"></a>
  11690. <li><b>dumpSpeed </b> - Anzahl der abgerufenen Zeilen aus der Quelldatenbank (default: 10000) pro Select durch "dumpMySQL ClientSide".
  11691. Dieser Parameter hat direkten Einfluß auf die Laufzeit und den Ressourcenverbrauch zur Laufzeit. </li> <br>
  11692. <a name="dumpFilesKeep"></a>
  11693. <li><b>dumpFilesKeep </b> - Es wird die angegebene Anzahl Dumpfiles im Dumpdir belassen (default: 3). Sind mehr (ältere) Dumpfiles
  11694. vorhanden, werden diese gelöscht nachdem ein neuer Dump erfolgreich erstellt wurde. Das globale
  11695. Attribut "archivesort" wird berücksichtigt. </li> <br>
  11696. <a name="executeAfterProc"></a>
  11697. <li><b>executeAfterProc </b> - Es kann ein FHEM-Kommando oder eine Perl-Funktion angegeben werden welche <b>nach der
  11698. Befehlsabarbeitung</b> ausgeführt werden soll. <br>
  11699. Funktionen sind in {} einzuschließen.<br><br>
  11700. <ul>
  11701. <b>Beispiel:</b> <br><br>
  11702. attr &lt;name&gt; executeAfterProc set og_gz_westfenster off; <br>
  11703. attr &lt;name&gt; executeAfterProc {adump ("&lt;name&gt;")} <br><br>
  11704. # "adump" ist eine in 99_myUtils definierte Funktion. <br>
  11705. <pre>
  11706. sub adump {
  11707. my ($name) = @_;
  11708. my $hash = $defs{$name};
  11709. # die eigene Funktion, z.B.
  11710. Log3($name, 3, "DbRep $name -> Dump ist beendet");
  11711. return;
  11712. }
  11713. </pre>
  11714. </ul>
  11715. </li>
  11716. <a name="executeBeforeProc"></a>
  11717. <li><b>executeBeforeProc </b> - Es kann ein FHEM-Kommando oder eine Perl-Funktion angegeben werden welche <b>vor der
  11718. Befehlsabarbeitung</b> ausgeführt werden soll. <br>
  11719. Funktionen sind in {} einzuschließen.<br><br>
  11720. <ul>
  11721. <b>Beispiel:</b> <br><br>
  11722. attr &lt;name&gt; executeBeforeProc set og_gz_westfenster on; <br>
  11723. attr &lt;name&gt; executeBeforeProc {bdump ("&lt;name&gt;")} <br><br>
  11724. # "bdump" ist eine in 99_myUtils definierte Funktion. <br>
  11725. <pre>
  11726. sub bdump {
  11727. my ($name) = @_;
  11728. my $hash = $defs{$name};
  11729. # die eigene Funktion, z.B.
  11730. Log3($name, 3, "DbRep $name -> Dump startet");
  11731. return;
  11732. }
  11733. </pre>
  11734. </ul>
  11735. </li>
  11736. <a name="expimpfile"></a>
  11737. <li><b>expimpfile </b> - Pfad/Dateiname für Export/Import in/aus einem File. <br><br>
  11738. Der Dateiname kann Platzhalter enthalten die gemäß der nachfolgenden Tabelle ersetzt werden.
  11739. Weiterhin können %-wildcards der POSIX strftime-Funktion des darunterliegenden OS enthalten
  11740. sein (siehe auch strftime Beschreibung). <br>
  11741. <br>
  11742. <ul>
  11743. <table>
  11744. <colgroup> <col width=5%> <col width=95%> </colgroup>
  11745. <tr><td> %L </td><td>: wird ersetzt durch den Wert des global logdir Attributs </td></tr>
  11746. <tr><td> %TSB </td><td>: wird ersetzt durch den (berechneten) Wert des timestamp_begin Attributs </td></tr>
  11747. <tr><td> </td><td> </td></tr>
  11748. <tr><td> </td><td> <b>Allgemein gebräuchliche POSIX-Wildcards sind:</b> </td></tr>
  11749. <tr><td> %d </td><td>: Tag des Monats (01..31) </td></tr>
  11750. <tr><td> %m </td><td>: Monat (01..12) </td></tr>
  11751. <tr><td> %Y </td><td>: Jahr (1970...) </td></tr>
  11752. <tr><td> %w </td><td>: Wochentag (0..6); beginnend mit Sonntag (0) </td></tr>
  11753. <tr><td> %j </td><td>: Tag des Jahres (001..366) </td></tr>
  11754. <tr><td> %U </td><td>: Wochennummer des Jahres, wobei Wochenbeginn = Sonntag (00..53) </td></tr>
  11755. <tr><td> %W </td><td>: Wochennummer des Jahres, wobei Wochenbeginn = Montag (00..53) </td></tr>
  11756. </table>
  11757. </ul>
  11758. </li> <br>
  11759. <ul>
  11760. <b>Beispiele:</b> <br>
  11761. <code>attr &lt;name&gt; expimpfile /sds1/backup/exptest_%TSB.csv </code> <br>
  11762. <code>attr &lt;name&gt; expimpfile /sds1/backup/exptest_%Y-%m-%d.csv </code> <br>
  11763. </ul>
  11764. <br>
  11765. <a name="DbRep_expimpfile"></a>
  11766. Zur POSIX Wildcardverwendung siehe auch die Erläuterungen zu <a href="#FileLog">Filelog</a>.
  11767. <br><br>
  11768. <a name="fastStart"></a>
  11769. <li><b>fastStart </b> - Normalerweise verbindet sich jedes DbRep-Device beim FHEM-Start kurz mit seiner Datenbank um
  11770. benötigte Informationen abzurufen und das Reading "state" springt bei Erfolg auf "connected".
  11771. Ist dieses Attribut gesetzt, erfolgt die initiale Datenbankverbindung erst dann wenn das
  11772. DbRep-Device sein erstes Kommando ausführt. <br>
  11773. Das Reading "state" verbleibt nach FHEM-Start solange im Status "initialized". Diese Einstellung
  11774. kann hilfreich sein wenn viele DbRep-Devices definiert sind.
  11775. </li> <br>
  11776. <a name="fetchMarkDuplicates"></a>
  11777. <li><b>fetchMarkDuplicates </b>
  11778. - Markierung von mehrfach vorkommenden Datensätzen im Ergebnis des "fetchrows" Kommandos </li> <br>
  11779. <a name="fetchRoute"></a>
  11780. <li><b>fetchRoute [descent | ascent] </b> - bestimmt die Leserichtung des fetchrows-Befehl. <br><br>
  11781. <ul>
  11782. <b>descent</b> - die Datensätze werden absteigend gelesen (default). Wird
  11783. die durch das Attribut "limit" festgelegte Anzahl der Datensätze
  11784. überschritten, werden die neuesten x Datensätze angezeigt. <br><br>
  11785. <b>ascent</b> - die Datensätze werden aufsteigend gelesen. Wird
  11786. die durch das Attribut "limit" festgelegte Anzahl der Datensätze
  11787. überschritten, werden die ältesten x Datensätze angezeigt. <br>
  11788. </ul>
  11789. </li> <br><br>
  11790. <a name="ftpUse"></a>
  11791. <li><b>ftpUse </b> - FTP Transfer nach einem Dump wird eingeschaltet (ohne SSL Verschlüsselung). Das erzeugte
  11792. Datenbank Backupfile wird non-blocking zum angegebenen FTP-Server (Attribut "ftpServer")
  11793. übertragen. </li> <br>
  11794. <a name="ftpUseSSL"></a>
  11795. <li><b>ftpUseSSL </b> - FTP Transfer mit SSL Verschlüsselung nach einem Dump wird eingeschaltet. Das erzeugte
  11796. Datenbank Backupfile wird non-blocking zum angegebenen FTP-Server (Attribut "ftpServer")
  11797. übertragen. </li> <br>
  11798. <a name="ftpUser"></a>
  11799. <li><b>ftpUser </b> - User zur Anmeldung am FTP-Server nach einem Dump, default: "anonymous". </li> <br>
  11800. <a name="ftpDebug"></a>
  11801. <li><b>ftpDebug </b> - Debugging der FTP Kommunikation zur Fehlersuche. </li> <br>
  11802. <a name="ftpDir"></a>
  11803. <li><b>ftpDir </b> - Verzeichnis des FTP-Servers in welches das File nach einem Dump übertragen werden soll
  11804. (default: "/"). </li> <br>
  11805. <a name="ftpDumpFilesKeep"></a>
  11806. <li><b>ftpDumpFilesKeep </b> - Es wird die angegebene Anzahl Dumpfiles im &lt;ftpDir&gt; belassen (default: 3). Sind mehr
  11807. (ältere) Dumpfiles vorhanden, werden diese gelöscht nachdem ein neuer Dump erfolgreich
  11808. übertragen wurde. </li> <br>
  11809. <a name="ftpPassive"></a>
  11810. <li><b>ftpPassive </b> - setzen wenn passives FTP verwendet werden soll </li> <br>
  11811. <a name="ftpPort"></a>
  11812. <li><b>ftpPort </b> - FTP-Port, default: 21 </li> <br>
  11813. <a name="ftpPwd"></a>
  11814. <li><b>ftpPwd </b> - Passwort des FTP-Users, default nicht gesetzt </li> <br>
  11815. <a name="ftpServer"></a>
  11816. <li><b>ftpServer </b> - Name oder IP-Adresse des FTP-Servers zur Übertragung von Files nach einem Dump. </li> <br>
  11817. <a name="ftpTimeout"></a>
  11818. <li><b>ftpTimeout </b> - Timeout für eine FTP-Verbindung in Sekunden (default: 30). </li> <br>
  11819. <a name="limit"></a>
  11820. <li><b>limit </b> - begrenzt die Anzahl der resultierenden Datensätze im select-Statement von "fetchrows", bzw. der anzuzeigenden Datensätze
  11821. der Kommandos "delSeqDoublets adviceDelete", "delSeqDoublets adviceRemain" (default 1000).
  11822. Diese Limitierung soll eine Überlastung der Browsersession und ein
  11823. blockieren von FHEMWEB verhindern. Bei Bedarf entsprechend ändern bzw. die
  11824. Selektionskriterien (Zeitraum der Auswertung) anpassen. </li> <br>
  11825. <a name="optimizeTablesBeforeDump"></a>
  11826. <li><b>optimizeTablesBeforeDump </b> - wenn "1", wird vor dem Datenbankdump eine Tabellenoptimierung ausgeführt (default: 0).
  11827. Dadurch verlängert sich die Laufzeit des Dump. <br><br>
  11828. <ul>
  11829. <b>Hinweis </b> <br>
  11830. Die Tabellenoptimierung führt zur Sperrung der Tabellen und damit zur Blockierung von
  11831. FHEM falls DbLog nicht im asynchronen Modus (DbLog-Attribut "asyncMode") betrieben wird !
  11832. <br>
  11833. </ul>
  11834. </li> <br>
  11835. <a name="reading"></a>
  11836. <li><b>reading </b> - Abgrenzung der DB-Selektionen auf ein bestimmtes oder mehrere Readings sowie exkludieren von
  11837. Readings.
  11838. Mehrere Readings werden als Komma separierte Liste angegeben. <br>
  11839. SQL Wildcard (%) wird in einer Liste als normales ASCII-Zeichen gewertet. <br>
  11840. Wird dem Reading bzw. der Reading-Liste ein "EXCLUDE=" vorangestellt, werden diese Readings
  11841. von der Selektion ausgeschlossen.
  11842. </li> <br>
  11843. <ul>
  11844. <b>Beispiele:</b> <br>
  11845. <code>attr &lt;name&gt; reading etotal</code> <br>
  11846. <code>attr &lt;name&gt; reading et%</code> <br>
  11847. <code>attr &lt;name&gt; reading etotal,etoday</code> <br>
  11848. <code>attr &lt;name&gt; reading etotal,etoday EXCLUDE=state </code> <br>
  11849. <code>attr &lt;name&gt; reading etotal,etoday EXCLUDE=Einspeisung% </code> <br>
  11850. </ul>
  11851. <br><br>
  11852. <a name="readingNameMap"></a>
  11853. <li><b>readingNameMap </b> - der Name des ausgewerteten Readings wird mit diesem String für die Anzeige überschrieben </li> <br>
  11854. <a name="readingPreventFromDel"></a>
  11855. <li><b>readingPreventFromDel </b> - Komma separierte Liste von Readings die vor einer neuen Operation nicht gelöscht
  11856. werden sollen </li> <br>
  11857. <a name="role"></a>
  11858. <li><b>role </b> - die Rolle des DbRep-Device. Standard ist "Client". Die Rolle "Agent" ist im Abschnitt
  11859. "DbRep-Agent" beschrieben. <br>
  11860. <a name="DbRep_role"></a>
  11861. Siehe auch Abschnitt <a href="#DbRepAutoRename">DbRep-Agent</a>.
  11862. </li> <br>
  11863. <a name="seqDoubletsVariance"></a>
  11864. <li><b>seqDoubletsVariance </b> - akzeptierte Abweichung (+/-) für das Kommando "set &lt;name&gt; delSeqDoublets". <br>
  11865. Der Wert des Attributs beschreibt die Abweichung bis zu der aufeinanderfolgende numerische
  11866. Werte (VALUE) von Datensätze als gleich angesehen und gelöscht werden sollen.
  11867. "seqDoubletsVariance" ist ein absoluter Zahlenwert,
  11868. der sowohl als positive als auch negative Abweichung verwendet wird. </li> <br>
  11869. <ul>
  11870. <b>Beispiele:</b> <br>
  11871. <code>attr &lt;name&gt; seqDoubletsVariance 0.0014 </code> <br>
  11872. <code>attr &lt;name&gt; seqDoubletsVariance 1.45 </code> <br>
  11873. </ul>
  11874. <br><br>
  11875. <a name="showproctime"></a>
  11876. <li><b>showproctime </b> - wenn gesetzt, zeigt das Reading "sql_processing_time" die benötigte Abarbeitungszeit (in Sekunden)
  11877. für die SQL-Ausführung der durchgeführten Funktion. Dabei wird nicht ein einzelnes
  11878. SQl-Statement, sondern die Summe aller notwendigen SQL-Abfragen innerhalb der jeweiligen
  11879. Funktion betrachtet. </li> <br>
  11880. <a name="showStatus"></a>
  11881. <li><b>showStatus </b> - grenzt die Ergebnismenge des Befehls "get &lt;name&gt; dbstatus" ein. Es können SQL-Wildcard (%) verwendet werden. </li> <br>
  11882. <ul>
  11883. <b>Bespiel: </b> <br>
  11884. attr &lt;name&gt; showStatus %uptime%,%qcache% <br>
  11885. # Es werden nur Readings erzeugt die im Namen "uptime" und "qcache" enthalten <br>
  11886. </ul><br>
  11887. <a name="showVariables"></a>
  11888. <li><b>showVariables </b> - grenzt die Ergebnismenge des Befehls "get &lt;name&gt; dbvars" ein. Es können SQL-Wildcard (%) verwendet werden. </li> <br>
  11889. <ul>
  11890. <b>Bespiel: </b> <br>
  11891. attr &lt;name&gt; showVariables %version%,%query_cache% <br>
  11892. # Es werden nur Readings erzeugt die im Namen "version" und "query_cache" enthalten <br>
  11893. </ul><br>
  11894. <a name="showSvrInfo"></a>
  11895. <li><b>showSvrInfo </b> - grenzt die Ergebnismenge des Befehls "get &lt;name&gt; svrinfo" ein. Es können SQL-Wildcard (%) verwendet werden. </li> <br>
  11896. <ul>
  11897. <b>Bespiel: </b> <br>
  11898. attr &lt;name&gt; showSvrInfo %SQL_CATALOG_TERM%,%NAME% <br>
  11899. # Es werden nur Readings erzeugt die im Namen "SQL_CATALOG_TERM" und "NAME" enthalten <br>
  11900. </ul><br>
  11901. <a name="showTableInfo"></a>
  11902. <li><b>showTableInfo </b> - grenzt die Ergebnismenge des Befehls "get &lt;name&gt; tableinfo" ein. Es können SQL-Wildcard (%) verwendet werden. </li> <br>
  11903. <ul>
  11904. <b>Bespiel: </b> <br>
  11905. attr &lt;name&gt; showTableInfo current,history <br>
  11906. # Es werden nur Information der Tabellen "current" und "history" angezeigt <br>
  11907. </ul><br>
  11908. <a name="sqlResultFieldSep"></a>
  11909. <li><b>sqlResultFieldSep </b> - legt den verwendeten Feldseparator (default: "|") im Ergebnis des Kommandos
  11910. "set ... sqlCmd" fest. </li> <br>
  11911. <a name="sqlCmdHistoryLength"></a>
  11912. <li><b>sqlCmdHistoryLength </b>
  11913. - aktiviert die Kommandohistorie von "sqlCmd" und legt deren Länge fest </li> <br>
  11914. <a name="sqlResultFormat"></a>
  11915. <li><b>sqlResultFormat </b> - legt die Formatierung des Ergebnisses des Kommandos "set &lt;name&gt; sqlCmd" fest.
  11916. Mögliche Optionen sind: <br><br>
  11917. <ul>
  11918. <b>separated </b> - die Ergebniszeilen werden als einzelne Readings fortlaufend
  11919. generiert. (default)<br><br>
  11920. <b>mline </b> - das Ergebnis wird als Mehrzeiler im Reading
  11921. SqlResult dargestellt. <br><br>
  11922. <b>sline </b> - das Ergebnis wird als Singleline im Reading
  11923. SqlResult dargestellt. Satztrenner ist"]|[". <br><br>
  11924. <b>table </b> - das Ergebnis wird als Tabelle im Reading
  11925. SqlResult dargestellt. <br><br>
  11926. <b>json </b> - erzeugt das Reading SqlResult als
  11927. JSON-kodierten Hash.
  11928. Jedes Hash-Element (Ergebnissatz) setzt sich aus der laufenden Nummer
  11929. des Datensatzes (Key) und dessen Wert zusammen. </li><br><br>
  11930. Die Weiterverarbeitung des Ergebnisses kann z.B. mit der folgenden userExitFn in 99_myUtils.pm erfolgen: <br>
  11931. <pre>
  11932. sub resfromjson {
  11933. my ($name,$reading,$value) = @_;
  11934. my $hash = $defs{$name};
  11935. if ($reading eq "SqlResult") {
  11936. # nur Reading SqlResult enthält JSON-kodierte Daten
  11937. my $data = decode_json($value);
  11938. foreach my $k (keys(%$data)) {
  11939. # ab hier eigene Verarbeitung für jedes Hash-Element
  11940. # z.B. Ausgabe jedes Element welches "Cam" enthält
  11941. my $ke = $data->{$k};
  11942. if($ke =~ m/Cam/i) {
  11943. my ($res1,$res2) = split("\\|", $ke);
  11944. Log3($name, 1, "$name - extract element $k by userExitFn: ".$res1." ".$res2);
  11945. }
  11946. }
  11947. }
  11948. return;
  11949. }
  11950. </pre>
  11951. </ul><br>
  11952. <a name="timeYearPeriod"></a>
  11953. <li><b>timeYearPeriod </b> - Mit Hilfe dieses Attributes wird eine jährliche Zeitperiode für die Datenbankselektion bestimmt.
  11954. Die Zeitgrenzen werden zur Ausführungszeit dynamisch berechnet. Es wird immer eine Jahresperiode
  11955. bestimmt. Eine unterjährige Angabe ist nicht möglich. <br>
  11956. Dieses Attribut ist vor allem dazu gedacht Auswertungen synchron zu einer Abrechnungsperiode, z.B. der eines
  11957. Energie- oder Gaslieferanten, anzufertigen.
  11958. </li> <br>
  11959. <ul>
  11960. <b>Beispiel:</b> <br><br>
  11961. attr &lt;name&gt; timeYearPeriod 06-25 06-24 <br><br>
  11962. # wertet die Datenbank in den Zeitgrenzen 25. Juni AAAA bis 24. Juni BBBB aus. <br>
  11963. # Das Jahr AAAA bzw. BBBB wird in Abhängigkeit des aktuellen Datums errechnet. <br>
  11964. # Ist das aktuelle Datum >= 25. Juni und =< 31. Dezember, dann ist AAAA = aktuelles Jahr und BBBB = aktuelles Jahr+1 <br>
  11965. # Ist das aktuelle Datum >= 01. Januar und =< 24. Juni, dann ist AAAA = aktuelles Jahr-1 und BBBB = aktuelles Jahr
  11966. </ul>
  11967. <br><br>
  11968. <a name="timestamp_begin"></a>
  11969. <li><b>timestamp_begin </b> - der zeitliche Beginn für die Datenselektion </li> <br>
  11970. Das Format von Timestamp ist "YYYY-MM-DD HH:MM:SS". Für die Attribute "timestamp_begin", "timestamp_end"
  11971. kann ebenso eine der folgenden Eingaben verwendet werden. Dabei wird das timestamp-Attribut dynamisch belegt: <br><br>
  11972. <ul>
  11973. <b>current_year_begin</b> : entspricht "&lt;aktuelles Jahr&gt;-01-01 00:00:00" <br>
  11974. <b>current_year_end</b> : entspricht "&lt;aktuelles Jahr&gt;-12-31 23:59:59" <br>
  11975. <b>previous_year_begin</b> : entspricht "&lt;vorheriges Jahr&gt;-01-01 00:00:00" <br>
  11976. <b>previous_year_end</b> : entspricht "&lt;vorheriges Jahr&gt;-12-31 23:59:59" <br>
  11977. <b>current_month_begin</b> : entspricht "&lt;aktueller Monat erster Tag&gt; 00:00:00" <br>
  11978. <b>current_month_end</b> : entspricht "&lt;aktueller Monat letzter Tag&gt; 23:59:59" <br>
  11979. <b>previous_month_begin</b> : entspricht "&lt;Vormonat erster Tag&gt; 00:00:00" <br>
  11980. <b>previous_month_end</b> : entspricht "&lt;Vormonat letzter Tag&gt; 23:59:59" <br>
  11981. <b>current_week_begin</b> : entspricht "&lt;erster Tag der akt. Woche&gt; 00:00:00" <br>
  11982. <b>current_week_end</b> : entspricht "&lt;letzter Tag der akt. Woche&gt; 23:59:59" <br>
  11983. <b>previous_week_begin</b> : entspricht "&lt;erster Tag Vorwoche&gt; 00:00:00" <br>
  11984. <b>previous_week_end</b> : entspricht "&lt;letzter Tag Vorwoche&gt; 23:59:59" <br>
  11985. <b>current_day_begin</b> : entspricht "&lt;aktueller Tag&gt; 00:00:00" <br>
  11986. <b>current_day_end</b> : entspricht "&lt;aktueller Tag&gt; 23:59:59" <br>
  11987. <b>previous_day_begin</b> : entspricht "&lt;Vortag&gt; 00:00:00" <br>
  11988. <b>previous_day_end</b> : entspricht "&lt;Vortag&gt; 23:59:59" <br>
  11989. <b>current_hour_begin</b> : entspricht "&lt;aktuelle Stunde&gt;:00:00" <br>
  11990. <b>current_hour_end</b> : entspricht "&lt;aktuelle Stunde&gt;:59:59" <br>
  11991. <b>previous_hour_begin</b> : entspricht "&lt;vorherige Stunde&gt;:00:00" <br>
  11992. <b>previous_hour_end</b> : entspricht "&lt;vorherige Stunde&gt;:59:59" <br>
  11993. </ul><br>
  11994. <a name="timestamp_end"></a>
  11995. <li><b>timestamp_end </b> - das zeitliche Ende für die Datenselektion. Wenn nicht gesetzt wird immer die aktuelle
  11996. Datum/Zeit-Kombi für das Ende der Selektion eingesetzt. </li> <br>
  11997. Das Format von Timestamp ist "YYYY-MM-DD HH:MM:SS". Für die Attribute "timestamp_begin", "timestamp_end"
  11998. kann ebenso eine der folgenden Eingaben verwendet werden. Dabei wird das timestamp-Attribut dynamisch belegt: <br><br>
  11999. <ul>
  12000. <b>current_year_begin</b> : entspricht "&lt;aktuelles Jahr&gt;-01-01 00:00:00" <br>
  12001. <b>current_year_end</b> : entspricht "&lt;aktuelles Jahr&gt;-12-31 23:59:59" <br>
  12002. <b>previous_year_begin</b> : entspricht "&lt;vorheriges Jahr&gt;-01-01 00:00:00" <br>
  12003. <b>previous_year_end</b> : entspricht "&lt;vorheriges Jahr&gt;-12-31 23:59:59" <br>
  12004. <b>current_month_begin</b> : entspricht "&lt;aktueller Monat erster Tag&gt; 00:00:00" <br>
  12005. <b>current_month_end</b> : entspricht "&lt;aktueller Monat letzter Tag&gt; 23:59:59" <br>
  12006. <b>previous_month_begin</b> : entspricht "&lt;Vormonat erster Tag&gt; 00:00:00" <br>
  12007. <b>previous_month_end</b> : entspricht "&lt;Vormonat letzter Tag&gt; 23:59:59" <br>
  12008. <b>current_week_begin</b> : entspricht "&lt;erster Tag der akt. Woche&gt; 00:00:00" <br>
  12009. <b>current_week_end</b> : entspricht "&lt;letzter Tag der akt. Woche&gt; 23:59:59" <br>
  12010. <b>previous_week_begin</b> : entspricht "&lt;erster Tag Vorwoche&gt; 00:00:00" <br>
  12011. <b>previous_week_end</b> : entspricht "&lt;letzter Tag Vorwoche&gt; 23:59:59" <br>
  12012. <b>current_day_begin</b> : entspricht "&lt;aktueller Tag&gt; 00:00:00" <br>
  12013. <b>current_day_end</b> : entspricht "&lt;aktueller Tag&gt; 23:59:59" <br>
  12014. <b>previous_day_begin</b> : entspricht "&lt;Vortag&gt; 00:00:00" <br>
  12015. <b>previous_day_end</b> : entspricht "&lt;Vortag&gt; 23:59:59" <br>
  12016. <b>current_hour_begin</b> : entspricht "&lt;aktuelle Stunde&gt;:00:00" <br>
  12017. <b>current_hour_end</b> : entspricht "&lt;aktuelle Stunde&gt;:59:59" <br>
  12018. <b>previous_hour_begin</b> : entspricht "&lt;vorherige Stunde&gt;:00:00" <br>
  12019. <b>previous_hour_end</b> : entspricht "&lt;vorherige Stunde&gt;:59:59" <br>
  12020. </ul><br>
  12021. Natürlich sollte man immer darauf achten dass "timestamp_begin" < "timestamp_end" ist. <br><br>
  12022. <ul>
  12023. <b>Beispiel:</b> <br><br>
  12024. attr &lt;name&gt; timestamp_begin current_year_begin <br>
  12025. attr &lt;name&gt; timestamp_end current_year_end <br><br>
  12026. # Wertet die Datenbank in den Zeitgrenzen des aktuellen Jahres aus. <br>
  12027. </ul>
  12028. <br><br>
  12029. <b>Hinweis </b> <br>
  12030. Wird das Attribut "timeDiffToNow" gesetzt, werden die eventuell gesetzten anderen Zeit-Attribute
  12031. ("timestamp_begin","timestamp_end","timeYearPeriod") gelöscht.
  12032. Das Setzen von "timestamp_begin" bzw. "timestamp_end" bedingt die Löschung von anderen Zeit-Attribute falls sie vorher
  12033. gesetzt waren.
  12034. <br><br>
  12035. <a name="timeDiffToNow"></a>
  12036. <li><b>timeDiffToNow </b> - der <b>Selektionsbeginn</b> wird auf den Zeitpunkt <b>"&lt;aktuelle Zeit&gt; - &lt;timeDiffToNow&gt;"</b>
  12037. gesetzt (z.b. werden die letzten 24 Stunden in die Selektion eingehen wenn das Attribut auf "86400" gesetzt
  12038. wurde). Die Timestampermittlung erfolgt dynamisch zum Ausführungszeitpunkt. </li> <br>
  12039. <ul>
  12040. <b>Eingabeformat Beispiel:</b> <br>
  12041. <code>attr &lt;name&gt; timeDiffToNow 86400</code> <br>
  12042. # die Startzeit wird auf "aktuelle Zeit - 86400 Sekunden" gesetzt <br>
  12043. <code>attr &lt;name&gt; timeDiffToNow d:2 h:3 m:2 s:10</code> <br>
  12044. # die Startzeit wird auf "aktuelle Zeit - 2 Tage 3 Stunden 2 Minuten 10 Sekunden" gesetzt <br>
  12045. <code>attr &lt;name&gt; timeDiffToNow m:600</code> <br>
  12046. # die Startzeit wird auf "aktuelle Zeit - 600 Minuten" gesetzt <br>
  12047. <code>attr &lt;name&gt; timeDiffToNow h:2.5</code> <br>
  12048. # die Startzeit wird auf "aktuelle Zeit - 2,5 Stunden" gesetzt <br>
  12049. <code>attr &lt;name&gt; timeDiffToNow y:1 h:2.5</code> <br>
  12050. # die Startzeit wird auf "aktuelle Zeit - 1 Jahr und 2,5 Stunden" gesetzt <br>
  12051. <code>attr &lt;name&gt; timeDiffToNow y:1.5</code> <br>
  12052. # die Startzeit wird auf "aktuelle Zeit - 1,5 Jahre gesetzt <br>
  12053. </ul>
  12054. <br>
  12055. Sind die Attribute "timeDiffToNow" und "timeOlderThan" gleichzeitig gesetzt, wird der
  12056. Selektionszeitraum zwischen diesen Zeitpunkten dynamisch kalkuliert.
  12057. <br><br>
  12058. <a name="timeOlderThan"></a>
  12059. <li><b>timeOlderThan </b> - das <b>Selektionsende</b> wird auf den Zeitpunkt <b>"&lt;aktuelle Zeit&gt; - &lt;timeOlderThan&gt;"</b>
  12060. gesetzt. Dadurch werden alle Datensätze bis zu dem Zeitpunkt "&lt;aktuelle
  12061. Zeit&gt; - &lt;timeOlderThan&gt;" berücksichtigt (z.b. wenn auf 86400 gesetzt, werden alle
  12062. Datensätze die älter als ein Tag sind berücksichtigt). Die Timestampermittlung erfolgt
  12063. dynamisch zum Ausführungszeitpunkt. </li> <br>
  12064. <ul>
  12065. <b>Eingabeformat Beispiel:</b> <br>
  12066. <code>attr &lt;name&gt; timeOlderThan 86400</code> <br>
  12067. # das Selektionsende wird auf "aktuelle Zeit - 86400 Sekunden" gesetzt <br>
  12068. <code>attr &lt;name&gt; timeOlderThan d:2 h:3 m:2 s:10</code> <br>
  12069. # das Selektionsende wird auf "aktuelle Zeit - 2 Tage 3 Stunden 2 Minuten 10 Sekunden" gesetzt <br>
  12070. <code>attr &lt;name&gt; timeOlderThan m:600</code> <br>
  12071. # das Selektionsende wird auf "aktuelle Zeit - 600 Minuten" gesetzt <br>
  12072. <code>attr &lt;name&gt; timeOlderThan h:2.5</code> <br>
  12073. # das Selektionsende wird auf "aktuelle Zeit - 2,5 Stunden" gesetzt <br>
  12074. <code>attr &lt;name&gt; timeOlderThan y:1 h:2.5</code> <br>
  12075. # das Selektionsende wird auf "aktuelle Zeit - 1 Jahr und 2,5 Stunden" gesetzt <br>
  12076. <code>attr &lt;name&gt; timeOlderThan y:1.5</code> <br>
  12077. # das Selektionsende wird auf "aktuelle Zeit - 1,5 Jahre gesetzt <br>
  12078. </ul>
  12079. <br>
  12080. Sind die Attribute "timeDiffToNow" und "timeOlderThan" gleichzeitig gesetzt, wird der
  12081. Selektionszeitraum zwischen diesen Zeitpunkten dynamisch kalkuliert.
  12082. <br><br>
  12083. <a name="timeout"></a>
  12084. <li><b>timeout </b> - das Attribut setzt den Timeout-Wert für die Blocking-Call Routinen in Sekunden
  12085. (Default: 86400) </li> <br>
  12086. <a name="userExitFn"></a>
  12087. <li><b>userExitFn </b> - stellt eine Schnittstelle zur Ausführung eigenen Usercodes zur Verfügung. <br>
  12088. Um die Schnittstelle zu aktivieren, wird zunächst die aufzurufende Subroutine in
  12089. 99_myUtls.pm nach folgendem Muster erstellt: <br>
  12090. <pre>
  12091. sub UserFunction {
  12092. my ($name,$reading,$value) = @_;
  12093. my $hash = $defs{$name};
  12094. ...
  12095. # z.B. übergebene Daten loggen
  12096. Log3 $name, 1, "UserExitFn $name called - transfer parameter are Reading: $reading, Value: $value " ;
  12097. ...
  12098. return;
  12099. }
  12100. </pre>
  12101. Die Aktivierung der Schnittstelle erfogt durch Setzen des Funktionsnamens im Attribut.
  12102. Optional kann ein Reading:Value Regex als Argument angegeben werden. Wird kein Regex
  12103. angegeben, werden alle Wertekombinationen als "wahr" gewertet (entspricht .*:.*).
  12104. <br><br>
  12105. <ul>
  12106. <b>Beispiel:</b> <br>
  12107. attr <device> userExitFn UserFunction .*:.* <br>
  12108. # "UserFunction" ist die Subroutine in 99_myUtils.pm.
  12109. </ul>
  12110. <br>
  12111. Grundsätzlich arbeitet die Schnittstelle OHNE Eventgenerierung bzw. benötigt zur Funktion keinen
  12112. Event. Sofern das Attribut gesetzt ist, erfolgt Die Regexprüfung NACH der Erstellung eines
  12113. Readings. Ist die Prüfung WAHR, wird die angegebene Funktion aufgerufen.
  12114. Zur Weiterverarbeitung werden der aufgerufenenen Funktion folgende Variablen übergeben: <br><br>
  12115. <ul>
  12116. <li>$name - der Name des DbRep-Devices </li>
  12117. <li>$reading - der Namen des erstellen Readings </li>
  12118. <li>$value - der Wert des Readings </li>
  12119. </ul>
  12120. </li>
  12121. <br>
  12122. <br>
  12123. <a name="valueFilter"></a>
  12124. <li><b>valueFilter </b> - Regulärer Ausdruck (REGEXP) zur Filterung von Datensätzen innerhalb bestimmter Funktionen.
  12125. Der REGEXP wird auf ein bestimmtes Feld oder den gesamten selektierten Datensatz (inkl. Device,
  12126. Reading usw.) angewendet.
  12127. Bitte beachten sie die Erläuterungen zu den entsprechenden Set-Kommandos. Weitere Informationen
  12128. sind mit "get &lt;name&gt; versionNotes 4" verfügbar. </li> <br>
  12129. </ul></ul>
  12130. </ul>
  12131. <a name="DbRepReadings"></a>
  12132. <b>Readings</b>
  12133. <br>
  12134. <ul>
  12135. Abhängig von der ausgeführten DB-Operation werden die Ergebnisse in entsprechenden Readings dargestellt. Zu Beginn einer neuen Operation
  12136. werden alle alten Readings einer vorangegangenen Operation gelöscht um den Verbleib unpassender bzw. ungültiger Readings zu vermeiden.
  12137. <br><br>
  12138. Zusätzlich werden folgende Readings erzeugt (Auswahl): <br><br>
  12139. <ul><ul>
  12140. <li><b>state </b> - enthält den aktuellen Status der Auswertung. Wenn Warnungen auftraten (state = Warning) vergleiche Readings
  12141. "diff_overrun_limit_&lt;diffLimit&gt;" und "less_data_in_period" </li> <br>
  12142. <li><b>errortext </b> - Grund eines Fehlerstatus </li> <br>
  12143. <li><b>background_processing_time </b> - die gesamte Prozesszeit die im Hintergrund/Blockingcall verbraucht wird </li> <br>
  12144. <li><b>diff_overrun_limit_&lt;diffLimit&gt;</b> - enthält eine Liste der Wertepaare die eine durch das Attribut "diffAccept" festgelegte Differenz
  12145. &lt;diffLimit&gt; (Standard: 20) überschreiten. Gilt für Funktion "diffValue". </li> <br>
  12146. <li><b>less_data_in_period </b> - enthält eine Liste der Zeitperioden in denen nur ein einziger Datensatz gefunden wurde. Die
  12147. Differenzberechnung berücksichtigt den letzten Wert der Vorperiode. Gilt für Funktion "diffValue". </li> <br>
  12148. <li><b>sql_processing_time </b> - der Anteil der Prozesszeit die für alle SQL-Statements der ausgeführten
  12149. Operation verbraucht wird </li> <br>
  12150. <li><b>SqlResult </b> - Ergebnis des letzten sqlCmd-Kommandos. Die Formatierung erfolgt entsprechend
  12151. des <a href="#DbRepattr">Attributes</a> "sqlResultFormat" </li> <br>
  12152. <li><b>sqlCmd </b> - das letzte ausgeführte sqlCmd-Kommando </li> <br>
  12153. </ul></ul>
  12154. <br>
  12155. </ul>
  12156. <a name="DbRepAutoRename"></a>
  12157. <b>DbRep Agent - automatisches Ändern von Device-Namen in Datenbanken und DbRep-Definitionen nach FHEM "rename" Kommando</b>
  12158. <br>
  12159. <ul>
  12160. Mit dem Attribut "role" wird die Rolle des DbRep-Device festgelegt. Die Standardrolle ist "Client". Mit der Änderung der Rolle in "Agent" wird das Device
  12161. veranlasst auf Umbenennungen von Geräten in der FHEM Installation zu reagieren. <br><br>
  12162. Durch den DbRep-Agenten werden folgende Features aktiviert wenn ein Gerät in FHEM mit "rename" umbenannt wird: <br><br>
  12163. <ul><ul>
  12164. <li> in der dem DbRep-Agenten zugeordneten Datenbank (Internal Database) wird nach Datensätzen mit dem alten Gerätenamen gesucht und dieser Gerätename in
  12165. <b>allen</b> betroffenen Datensätzen in den neuen Namen geändert. </li> <br>
  12166. <li> in dem DbRep-Agenten zugeordneten DbLog-Device wird in der Definition das alte durch das umbenannte Device ersetzt. Dadurch erfolgt ein weiteres Logging
  12167. des umbenannten Device in der Datenbank. </li> <br>
  12168. <li> in den existierenden DbRep-Definitionen vom Typ "Client" wird ein evtl. gesetztes Attribut "device = alter Devicename" in "device = neuer Devicename"
  12169. geändert. Dadurch werden Auswertungsdefinitionen bei Geräteumbenennungen automatisch konstistent gehalten. </li> <br>
  12170. </ul></ul>
  12171. Mit der Änderung in einen Agenten sind folgende Restriktionen verbunden die mit dem Setzen des Attributes "role = Agent" eingeschaltet
  12172. und geprüft werden: <br><br>
  12173. <ul><ul>
  12174. <li> es kann nur einen Agenten pro Datenbank in der FHEM-Installation geben. Ist mehr als eine Datenbank mit DbLog definiert, können
  12175. ebenso viele DbRep-Agenten eingerichtet werden </li> <br>
  12176. <li> mit der Umwandlung in einen Agenten wird nur noch das Set-Komando "renameDevice" verfügbar sein sowie nur ein eingeschränkter Satz von DbRep-spezifischen
  12177. Attributen zugelassen. Wird ein DbRep-Device vom bisherigen Typ "Client" in einen Agenten geändert, werden evtl. gesetzte und nun nicht mehr zugelassene
  12178. Attribute glöscht. </li> <br>
  12179. </ul></ul>
  12180. Die Aktivitäten wie Datenbankänderungen bzw. Änderungen an anderen DbRep-Definitionen werden im Logfile mit verbose=3 protokolliert. Damit die renameDevice-Funktion
  12181. bei großen Datenbanken nicht in ein timeout läuft, sollte das Attribut "timeout" entsprechend dimensioniert werden. Wie alle Datenbankoperationen des Moduls
  12182. wird auch das Autorename nonblocking ausgeführt. <br><br>
  12183. <ul>
  12184. <b>Beispiel </b> für die Definition eines DbRep-Device als Agent: <br><br>
  12185. <code>
  12186. define Rep.Agent DbRep LogDB <br>
  12187. attr Rep.Agent devStateIcon connected:10px-kreis-gelb .*disconnect:10px-kreis-rot .*done:10px-kreis-gruen <br>
  12188. attr Rep.Agent icon security <br>
  12189. attr Rep.Agent role Agent <br>
  12190. attr Rep.Agent room DbLog <br>
  12191. attr Rep.Agent showproctime 1 <br>
  12192. attr Rep.Agent stateFormat { ReadingsVal("$name","state", undef) eq "running" ? "renaming" : ReadingsVal("$name","state", undef). " &raquo;; ProcTime: ".ReadingsVal("$name","sql_processing_time", undef)." sec"} <br>
  12193. attr Rep.Agent timeout 86400 <br>
  12194. </code>
  12195. <br>
  12196. </ul>
  12197. <b>Hinweis:</b> <br>
  12198. Obwohl die Funktion selbst non-blocking ausgelegt ist, sollte das zugeordnete DbLog-Device
  12199. im asynchronen Modus betrieben werden um ein Blockieren von FHEMWEB zu vermeiden (Tabellen-Lock). <br><br>
  12200. </ul>
  12201. =end html_DE
  12202. =cu