93_DbRep.pm 653 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246
  1. ##########################################################################################################
  2. # $Id: 93_DbRep.pm 17518 2018-10-12 16:00:37Z 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. #
  31. ###########################################################################################################################
  32. #
  33. # Definition: define <name> DbRep <DbLog-Device>
  34. #
  35. # This module uses credentials of the DbLog-Device
  36. #
  37. ###########################################################################################################################
  38. package main;
  39. use strict;
  40. use warnings;
  41. # Versions History intern
  42. our %DbRep_vNotesIntern = (
  43. "8.2.3" => "07.10.2018 check availability of DbLog-device at definition time of DbRep-device ",
  44. "8.2.2" => "07.10.2018 DbRep_getMinTs changed, fix don't get the real min timestamp in rare cases ",
  45. "8.2.1" => "07.10.2018 \$hash->{dbloghash}{HELPER}{REOPEN_RUNS_UNTIL} contains the time until DB is closed ",
  46. "8.2.0" => "05.10.2018 direct help for attributes ",
  47. "8.1.0" => "02.10.2018 new get versionNotes command ",
  48. "8.0.1" => "20.09.2018 DbRep_getMinTs improved",
  49. "8.0.0" => "11.09.2018 get filesize in DbRep_WriteToDumpFile corrected, restoreMySQL for clientSide dumps, minor fixes ",
  50. "7.20.0" => "04.09.2018 deviceRename can operate a Device name with blank, e.g. 'current balance' as old device name ",
  51. "7.19.0" => "25.08.2018 attribute 'valueFilter' to filter datasets in fetchrows ",
  52. "7.18.2" => "02.08.2018 fix in fetchrow function (forum:#89886), fix highlighting ",
  53. "7.18.1" => "03.06.2018 commandref revised ",
  54. "7.18.0" => "02.06.2018 possible use of y:(\\d) for timeDiffToNow, timeOlderThan , minor fixes of timeOlderThan, delEntries considers executeBeforeDump,executeAfterDump ",
  55. "7.17.3" => "30.04.2017 writeToDB - readingname can be replaced by the value of attribute 'readingNameMap' ",
  56. "7.17.2" => "22.04.2017 fix don't writeToDB if device name contain '.' only, minor fix in DbReadingsVal ",
  57. "7.17.1" => "20.04.2017 fix '§' is deleted by carfilter ",
  58. "7.17.0" => "17.04.2018 new function DbReadingsVal ",
  59. "7.16.0" => "13.04.2018 new function dbValue (blocking) ",
  60. "7.15.2" => "12.04.2018 fix in setting MODEL, prevent fhem from crash if wrong timestamp '0000-00-00' found in db ",
  61. "7.15.1" => "11.04.2018 sqlCmd accept widget textField-long, Internal MODEL is set ",
  62. "7.15.0" => "24.03.2018 new command sqlSpecial ",
  63. "7.14.8" => "21.03.2018 fix no save into database if value=0 (DbRep_OutputWriteToDB) ",
  64. "7.14.7" => "21.03.2018 exportToFile,importFromFile can use file as an argument and executeBeforeDump, executeAfterDump is considered ",
  65. "7.14.6" => "18.03.2018 attribute expimpfile can use some kinds of wildcards (exportToFile, importFromFile adapted) ",
  66. "7.14.5" => "17.03.2018 perl warnings of DbLog \$dn,\$dt,\$evt,\$rd in changeval_Push & complex ",
  67. "7.14.4" => "11.03.2018 increased timeout of BlockingCall in DbRep_firstconnect ",
  68. "7.14.3" => "07.03.2018 DbRep_firstconnect changed - get lowest timestamp in database, DbRep_Connect deleted ",
  69. "7.14.2" => "04.03.2018 fix perl warning ",
  70. "7.14.1" => "01.03.2018 currentfillup_Push bugfix for PostgreSQL ",
  71. "7.14.0" => "26.02.2018 syncStandby ",
  72. "7.13.3" => "25.02.2018 commandref revised (forum:#84953) ",
  73. "7.13.2" => "24.02.2018 DbRep_firstconnect changed, bug fix in DbRep_collaggstr for aggregation = month ",
  74. "7.13.1" => "20.02.2018 commandref revised ",
  75. "7.13.0" => "17.02.2018 changeValue can handle perl code {} as 'new string' ",
  76. "7.12.0" => "16.02.2018 compression of dumpfile, restore of compressed files possible ",
  77. "7.11.0" => "12.02.2018 new command 'repairSQLite' to repair a corrupted SQLite database ",
  78. "7.10.0" => "10.02.2018 bugfix delete attr timeYearPeriod if set other time attributes, new 'changeValue' command ",
  79. "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 ",
  80. "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 ",
  81. "7.8.0" => "04.02.2018 new command 'eraseReadings' ",
  82. "7.7.1" => "03.02.2018 minor fix in DbRep_firstconnect if IsDisabled ",
  83. "7.7.0" => "29.01.2018 attribute 'averageCalcForm', calculation sceme 'avgDailyMeanGWS', 'avgArithmeticMean' for averageValue ",
  84. "7.6.1" => "27.01.2018 new attribute 'sqlCmdHistoryLength' and 'fetchMarkDuplicates' for highlighting multiple datasets by fetchrows ",
  85. "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) ",
  86. "7.5.5" => "25.01.2018 minor change in delSeqDoublets ",
  87. "7.5.4" => "24.01.2018 delseqdoubl_DoParse reviewed to optimize memory usage, executeBeforeDump executeAfterDump now available for 'delSeqDoublets' ",
  88. "7.5.3" => "23.01.2018 new attribute 'ftpDumpFilesKeep', version management added to FTP-usage ",
  89. "7.5.2" => "23.01.2018 fix typo DumpRowsCurrrent, dumpFilesKeep can be set to '0', commandref revised ",
  90. "7.5.1" => "20.01.2018 DbRep_DumpDone changed to create background_processing_time before execute 'executeAfterProc' Commandref updated ",
  91. "7.5.0" => "16.01.2018 DbRep_OutputWriteToDB, set options display/writeToDB for (max|min|sum|average|diff)Value ",
  92. "7.4.1" => "14.01.2018 fix old dumpfiles not deleted by dumpMySQL clientSide ",
  93. "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 ",
  94. "7.3.1" => "08.01.2018 fix syntax error for perl < 5.20 ",
  95. "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) ",
  96. "7.2.1" => "04.01.2018 bugfix month out of range that causes fhem crash ",
  97. "7.2.0" => "27.12.2017 new attribute 'seqDoubletsVariance' ",
  98. "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 ",
  99. "7.0.0" => "18.12.2017 don't set \$runtime_string_first,\$runtime_string_next,\$ts if time/aggregation-attributes not set, change_Push redesigned, new command get blockinginfo, identify if reopen is running on dblog-device and postpone the set-command ",
  100. "6.4.3" => "17.12.2017 bugfix in delSeqDoublets, fetchrows if datasets contain characters like \"' <blank> and s.o. ",
  101. "6.4.2" => "15.12.2017 change 'delSeqDoublets' to respect attribute 'limit' (adviceDelete,adviceRemain), commandref revised ",
  102. "6.4.1" => "13.12.2017 new Attribute 'sqlResultFieldSep' for field separate options of sqlCmd result ",
  103. "6.4.0" => "10.12.2017 prepare module for usage of datetime picker widget (Forum:#35736) ",
  104. "6.3.2" => "05.12.2017 make direction of fetchrows switchable ASC <-> DESC by attribute fetchRoute ",
  105. "6.3.1" => "04.12.2017 fix DBD::mysql::st execute failed: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'DEVELfhem.history.TIMESTAMP' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by Forum:https://forum.fhem.de/index.php/topic,65860.msg725595.html#msg725595 , fix currentfillup_Push PostgreSQL -> use \$runtime_string_next as Timestring during current insert ",
  106. "6.3.0" => "04.12.2017 support addition format d:xx h:xx m:xx s:xx for attributes timeDiffToNow, timeOlderThan ",
  107. "6.2.3" => "04.12.2017 fix localtime(time); (current time deduction) in DbRep_createTimeArray ",
  108. "6.2.2" => "01.12.2017 support all aggregations for delSeqDoublets, better output filesize when mysql dump finished ",
  109. "6.2.1" => "30.11.2017 support delSeqDoublets without device,reading is set and support device-devspec, reading list, minor fixes in delSeqDoublets ",
  110. "6.2.0" => "29.11.2017 enhanced command delSeqDoublets by 'delete' ",
  111. "6.1.0" => "29.11.2017 new command delSeqDoublets (adviceRemain,adviceDelete), add Option to LASTCMD ",
  112. "6.0.0" => "18.11.2017 FTP transfer dumpfile after dump, delete old dumpfiles within Blockingcall (avoid freezes) commandref revised, minor fixes ",
  113. "5.8.6" => "30.10.2017 don't limit attr reading, device if the attr contains a list ",
  114. "5.8.5" => "19.10.2017 filter unwanted characters in 'procinfo'-result ",
  115. "5.8.4" => "17.10.2017 DbRep_createSelectSql, DbRep_createDeleteSql, currentfillup_Push switch to devspec ",
  116. "5.8.3" => "16.10.2017 change to use DbRep_createSelectSql: minValue,diffValue - DbRep_createDeleteSql: delEntries ",
  117. "5.8.2" => "15.10.2017 sub DbRep_createTimeArray ",
  118. "5.8.1" => "15.10.2017 change to use DbRep_createSelectSql: sumValue,averageValue,exportToFile,maxValue ",
  119. "5.8.0" => "15.10.2017 adapt DbRep_createSelectSql for better performance if time/aggregation not set, can set table as flexible argument for countEntries, fetchrows (default: history), minor fixes ",
  120. "5.7.1" => "13.10.2017 tableCurrentFillup fix for PostgreSQL, commandref revised ",
  121. "5.7.0" => "09.10.2017 tableCurrentPurge, tableCurrentFillup ",
  122. "5.6.4" => "05.10.2017 abortFn's adapted to use abortArg (Forum:77472) ",
  123. "5.6.3" => "01.10.2017 fix crash of fhem due to wrong rmday-calculation if month is changed, Forum:#77328 ",
  124. "5.6.2" => "28.08.2017 commandref revised ",
  125. "5.6.1" => "18.07.2017 commandref revised, minor fixes ",
  126. "5.6.0" => "17.07.2017 default timeout changed to 86400, new get-command 'procinfo' (MySQL) ",
  127. "5.5.2" => "16.07.2017 dbmeta_DoParse -> show variables (no global) ",
  128. "5.5.1" => "16.07.2017 wrong text output in state when restoreMySQL was aborted by timeout ",
  129. "5.5.0" => "10.07.2017 replace \$hash->{dbloghash}{DBMODEL} by \$hash->{dbloghash}{MODEL} (DbLog was changed) ",
  130. "5.4.0" => "03.07.2017 restoreMySQL - restore of csv-files (from dumpServerSide), RestoreRowsHistory/ DumpRowsHistory, Commandref revised ",
  131. "5.3.1" => "28.06.2017 vacuum for SQLite added, readings enhanced for optimizeTables / vacuum, commandref revised ",
  132. "5.3.0" => "26.06.2017 change of DbRep_mysqlOptimizeTables, new command optimizeTables ",
  133. "5.2.1" => "25.06.2017 bugfix in sqlCmd_DoParse (PRAGMA, UTF8, SHOW) ",
  134. "5.2.0" => "14.06.2017 UTF-8 support for MySQL (fetchrows, srvinfo, expfile, impfile, insert) ",
  135. "5.1.0" => "13.06.2017 column 'UNIT' added to fetchrow result ",
  136. "5.0.6" => "13.06.2017 add Aria engine to DbRep_mysqlOptimizeTables ",
  137. "5.0.5" => "12.06.2017 bugfixes in DbRep_DumpAborted, some changes in dumpMySQL, optimizeTablesBeforeDump added to mysql_DoDumpServerSide, new reading DumpFileCreatedSize ",
  138. "5.0.4" => "09.06.2017 some improvements and changes of mysql_DoDump, commandref revised, new attributes executeBeforeDump, executeAfterDump ",
  139. "5.0.3" => "07.06.2017 mysql_DoDumpServerSide added ",
  140. "5.0.2" => "06.06.2017 little improvements in mysql_DoDumpClientSide ",
  141. "5.0.1" => "05.06.2017 dependencies between dumpMemlimit and dumpSpeed created, enhanced verbose 5 logging ",
  142. "5.0.0" => "04.06.2017 MySQL Dump nonblocking added ",
  143. "4.16.1" => "22.05.2017 encode json without JSON module, requires at least fhem.pl 14348 2017-05-22 20:25:06Z ",
  144. "4.16.0" => "22.05.2017 format json as option of sqlResultFormat, state will never be deleted in 'DbRep_delread' ",
  145. "4.15.1" => "20.05.2017 correction of commandref ",
  146. "4.15.0" => "17.05.2017 SUM(VALUE),AVG(VALUE) recreated for PostgreSQL, Code reviewed and optimized ",
  147. "4.14.2" => "16.05.2017 SQL-Statements optimized for Wildcard '%' usage if used, Wildcard '_' isn't supported furthermore, \"averageValue\", \"sumValue\", \"maxValue\", \"minValue\", \"countEntries\" performance optimized, commandref revised ",
  148. "4.14.1" => "16.05.2017 limitation of fetchrows result datasets to 1000 by attr limit ",
  149. "4.14.0" => "15.05.2017 UserExitFn added as separate sub (DbRep_userexit) and attr userExitFn defined, new subs ReadingsBulkUpdateTimeState, ReadingsBulkUpdateValue, ReadingsSingleUpdateValue, commandref revised ",
  150. "4.13.7" => "11.05.2017 attribute sqlResultSingleFormat became sqlResultFormat, sqlResultSingle deleted and sqlCmd contains now all format possibilities (separated,mline,sline,table), commandref revised ",
  151. "4.13.6" => "10.05.2017 minor changes ",
  152. "4.13.5" => "09.05.2017 cover dbh prepare in eval to avoid crash (sqlResult_DoParse) ",
  153. "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 ",
  154. "4.13.3" => "09.05.2017 flexible format of reading SqlResultRow_xxx for proper and sort sequence ",
  155. "4.13.2" => "09.05.2017 sqlResult, sqlResultSingle are able to execute delete, insert, update commands error corrections ",
  156. "4.13.1" => "09.05.2017 change substitution in sqlResult, sqlResult_DoParse ",
  157. "4.13.0" => "09.05.2017 acceptance of viegener change with some corrections (separating lines with ]|[ in Singleline) ",
  158. "4.12.3" => "07.05.2017 New sets sqlSelect execute arbitrary sql command returning each row as single reading (fields separated with |) allowing replacement of timestamp values according to attribute definition --> §timestamp_begin§ etc and sqlSelectSingle for executing an sql command returning a single reading (separating lines with §) ",
  159. "4.12.2" => "17.04.2017 DbRep_checkUsePK changed ",
  160. "4.12.1" => "07.04.2017 get tableinfo changed for MySQL ",
  161. "4.12.0" => "31.03.2017 support of primary key for insert functions ",
  162. "4.11.4" => "29.03.2017 bugfix timestamp in minValue, maxValue if VALUE contains more than one numeric value (like in sysmon) ",
  163. "4.11.3" => "26.03.2017 usage of daylight saving time changed to avoid wrong selection when wintertime switch to summertime, minor bug fixes ",
  164. "4.11.2" => "16.03.2017 bugfix in func dbmeta_DoParse (SQLITE_DB_FILENAME) ",
  165. "4.11.1" => "28.02.2017 commandref completed ",
  166. "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 ",
  167. "4.10.3" => "01.02.2017 rename reading 'diff-overrun_limit-' to 'diff_overrun_limit_', DbRep_collaggstr day aggregation changed back from 4.7.5 change ",
  168. "4.10.2" => "16.01.2017 bugfix uninitialized value \$renmode if RenameAgent ",
  169. "4.10.1" => "30.11.2016 bugfix importFromFile format problem if UNIT-field wasn't set ",
  170. "4.10.0" => "28.12.2016 del_DoParse changed to use Wildcards, del_ParseDone changed to use readingNameMap ",
  171. "4.9.0" => "23.12.2016 function readingRename added ",
  172. "4.8.6" => "17.12.2016 new bugfix group by-clause due to incompatible changes made in MyQL 5.7.5 (Forum #msg541103) ",
  173. "4.8.5" => "16.12.2016 bugfix group by-clause due to Forum #msg540610 ",
  174. "4.8.4" => "13.12.2016 added 'group by ...,table_schema' to select in dbmeta_DoParse due to Forum #msg539228, commandref adapted, changed 'not_enough_data_in_period' to 'less_data_in_period' ",
  175. "4.8.3" => "12.12.2016 balance diff to next period if value of period is 0 between two periods with values ",
  176. "4.8.2" => "10.12.2016 bugfix negativ diff if balanced ",
  177. "4.8.1" => "10.12.2016 added balance diff to diffValue, a difference between the last value of an old aggregation period to the first value of a new aggregation period will be take over now ",
  178. "4.8.0" => "09.12.2016 diffValue selection chenged to 'between' ",
  179. "4.7.7" => "08.12.2016 code review ",
  180. "4.7.6" => "07.12.2016 DbRep version as internal, check if perl module DBI is installed ",
  181. "4.7.5" => "05.12.2016 DbRep_collaggstr day aggregation changed ",
  182. "4.7.4" => "28.11.2016 sub DbRep_calcount changed due to Forum #msg529312 ",
  183. "4.7.3" => "20.11.2016 new diffValue function made suitable to SQLite ",
  184. "4.7.2" => "20.11.2016 commandref adapted, state = Warnings adapted ",
  185. "4.7.1" => "17.11.2016 changed fieldlength to DbLog new standard, diffValue state Warnings due to several situations and generate readings not_enough_data_in_period, diff-overrun_limit ",
  186. "4.7.0" => "16.11.2016 sub diffValue changed due to Forum #msg520154, attr diffAccept added, diffValue now able to calculate if counter was going to 0 ",
  187. "4.6.0" => "31.10.2016 bugfix calc issue due to daylight saving time end (winter time) ",
  188. "4.5.1" => "18.10.2016 get svrinfo contains SQLite database file size (MB), modified timeout routine ",
  189. "4.5.0" => "17.10.2016 get data of dbstatus, dbvars, tableinfo, svrinfo (database dependend) ",
  190. "4.4.0" => "13.10.2016 get function prepared ",
  191. "4.3.0" => "11.10.2016 Preparation of get metadata ",
  192. "4.2.0" => "10.10.2016 allow SQL-Wildcards (% _) in attr reading & attr device ",
  193. "4.1.3" => "09.10.2016 bugfix delEntries running on SQLite ",
  194. "4.1.2" => "08.10.2016 old device in DEF of connected DbLog device will substitute by renamed device if it is present in DEF ",
  195. "4.1.1" => "06.10.2016 NotifyFn is getting events from global AND own device, set is reduced if ROLE=Agent, english commandref enhanced ",
  196. "4.1.0" => "05.10.2016 DbRep_Attr changed ",
  197. "4.0.0" => "04.10.2016 Internal/Attribute ROLE added, sub DbRep_firstconnect changed NotifyFN activated to start deviceRename if ROLE=Agent ",
  198. "3.13.0" => "03.10.2016 added deviceRename to rename devices in database, new Internal DATABASE ",
  199. "3.12.0" => "02.10.2016 function minValue added ",
  200. "3.11.1" => "30.09.2016 bugfix include first and next day in calculation if Timestamp is exactly 'YYYY-MM-DD 00:00:00' ",
  201. "3.11.0" => "29.09.2016 maxValue calculation moved to background to reduce FHEM-load ",
  202. "3.10.1" => "28.09.2016 sub impFile -> changed \$dbh->{AutoCommit} = 0 to \$dbh->begin_work ",
  203. "3.10.0" => "27.09.2016 diffValue calculation moved to background to reduce FHEM-load, new reading background_processing_time ",
  204. "3.9.1" => "27.09.2016 Internal 'LASTCMD' added ",
  205. "3.9.0" => "26.09.2016 new function importFromFile to import data from file (CSV format) ",
  206. "3.8.0" => "16.09.2016 new attr readingPreventFromDel to prevent readings from deletion when a new operation starts ",
  207. "3.7.3" => "11.09.2016 changed format of diffValue-reading if no value was selected ",
  208. "3.7.2" => "04.09.2016 problem in diffValue fixed if if no value was selected ",
  209. "3.7.1" => "31.08.2016 Reading 'errortext' added, commandref continued, exportToFile changed, diffValue changed to fix wrong timestamp if error occur ",
  210. "3.7.0" => "30.08.2016 exportToFile added exports data to file (CSV format) ",
  211. "3.6.0" => "29.08.2016 plausibility checks of database column character length ",
  212. "3.5.2" => "21.08.2016 fit to new commandref style ",
  213. "3.5.1" => "20.08.2016 commandref continued ",
  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 timestmp attribute ",
  216. "3.4.3" => "09.08.2016 fields for input using 'insert' changed to 'date,time,value,unit'. Attributes device, reading will be used to complete dataset, now more informations available about faulty datasets in arithmetic operations ",
  217. "3.4.2" => "05.08.2016 commandref complemented, fieldlength used in function 'insert' trimmed to 32 ",
  218. "3.4.1" => "04.08.2016 check of numeric value type in functions maxvalue, diffvalue ",
  219. "3.4.0" => "03.08.2016 function 'insert' added ",
  220. "3.3.3" => "16.07.2016 bugfix of aggregation=week if month start is 01 and month end is 12 AND the last week of december is '01' like in 2014 (checked in version 11804) ",
  221. "3.3.2" => "16.07.2016 readings completed with begin of selection range to ensure valid reading order, also done if readingNameMap is set ",
  222. "3.3.1" => "15.07.2016 function 'diffValue' changed, write '-' if no value ",
  223. "3.3.0" => "12.07.2016 function 'diffValue' added ",
  224. "3.2.1" => "12.07.2016 DbRep_Notify prepared, switched from readingsSingleUpdate to readingsBulkUpdate ",
  225. "3.2.0" => "11.07.2016 handling of db-errors is relocated to blockingcall-subs (checked in version 11785) ",
  226. "3.1.1" => "10.07.2016 state turns to initialized and connected after attr 'disabled' is switched from '1' to '0' ",
  227. "3.1.0" => "09.07.2016 new Attr 'timeDiffToNow' and change subs according to that ",
  228. "3.0.0" => "04.07.2016 no selection if timestamp isn't set and aggregation isn't set with fetchrows, delEntries ",
  229. "2.9.9" => "03.07.2016 english version of commandref completed ",
  230. "2.9.8" => "01.07.2016 changed fetchrows_ParseDone to handle readingvalues with whitespaces correctly ",
  231. "2.9.7" => "30.06.2016 moved {DBLOGDEVICE} to {HELPER}{DBLOGDEVICE} ",
  232. "2.9.6" => "30.06.2016 sql-call changed for countEntries, averageValue, sumValue avoiding problems if no timestamp is set and aggregation is set ",
  233. "2.9.5" => "30.06.2016 format of readingnames changed again (substitute ':' with '-' in time) ",
  234. "2.9.4" => "30.06.2016 change readingmap to readingNameMap, prove of unsupported characters added ",
  235. "2.9.3" => "27.06.2016 format of readingnames changed avoiding some problems after restart and splitting ",
  236. "2.9.2" => "27.06.2016 use Time::Local added, DbRep_firstconnect added ",
  237. "2.9.1" => "26.06.2016 german commandref added ",
  238. "2.9.0" => "25.06.2016 attributes showproctime, timeout added ",
  239. "2.8.1" => "24.06.2016 sql-creation of sumValue, maxValue, fetchrows changed main-routine changed ",
  240. "2.8.0" => "24.06.2016 function averageValue changed to nonblocking function ",
  241. "2.7.1" => "24.06.2016 changed blockingcall routines, changed to unique abort-function ",
  242. "2.7.0" => "23.06.2016 changed function countEntries to nonblocking ",
  243. "2.6.3" => "22.06.2016 abort-routines changed, dbconnect-routines changed ",
  244. "2.6.2" => "21.06.2016 aggregation week corrected ",
  245. "2.6.1" => "20.06.2016 routine maxval_ParseDone corrected ",
  246. "2.6.0" => "31.05.2016 maxValue changed to nonblocking function ",
  247. "2.5.3" => "31.05.2016 function delEntries changed ",
  248. "2.5.2" => "31.05.2016 ping check changed, DbRep_Connect changed ",
  249. "2.5.1" => "30.05.2016 sleep in nb-functions deleted ",
  250. "2.5.0" => "30.05.2016 changed to use own \$dbh with DbLog-credentials, function sumValue, fetchrows ",
  251. "2.4.2" => "29.05.2016 function sumValue changed ",
  252. "2.4.1" => "29.05.2016 function fetchrow changed ",
  253. "2.4.0" => "29.05.2016 changed to nonblocking function for sumValue ",
  254. "2.3.0" => "28.05.2016 changed sumValue to 'prepare' with placeholders ",
  255. "2.2.0" => "27.05.2016 changed fetchrow and delEntries function to 'prepare' with placeholders added nonblocking function for delEntries ",
  256. "2.1.0" => "25.05.2016 codechange ",
  257. "2.0.0" => "24.05.2016 added nonblocking function for fetchrow ",
  258. "1.2.0" => "21.05.2016 function and attribute for delEntries added ",
  259. "1.1.0" => "20.05.2016 change result-format of 'count', move runtime-counter to sub DbRep_collaggstr ",
  260. "1.0.0" => "19.05.2016 Initial"
  261. );
  262. # Versions History extern:
  263. our %DbRep_vNotesExtern = (
  264. "8.2.2" => "07.10.2018 fix don't get the real min timestamp in rare cases ",
  265. "8.2.0" => "05.10.2018 direct help for attributes ",
  266. "8.1.0" => "01.10.2018 new get versionNotes command ",
  267. "8.0.0" => "11.09.2018 get filesize in DbRep_WriteToDumpFile corrected, restoreMySQL for clientSide dumps, minor fixes ",
  268. "7.20.0" => "04.09.2018 deviceRename can operate a Device name with blank, e.g. 'current balance' as old device name ",
  269. "7.19.0" => "25.08.2018 attribute 'valueFilter' to filter datasets in fetchrows ",
  270. "7.18.2" => "02.08.2018 fix in fetchrow function (forum:#89886), fix highlighting ",
  271. "7.18.0" => "02.06.2018 possible use of y:(\\d) for timeDiffToNow, timeOlderThan , minor fixes of timeOlderThan, delEntries considers executeBeforeDump,executeAfterDump ",
  272. "7.17.3" => "30.04.2017 writeToDB - readingname can be replaced by the value of attribute 'readingNameMap' ",
  273. "7.17.0" => "17.04.2018 new function DbReadingsVal ",
  274. "7.16.0" => "13.04.2018 new function dbValue (blocking) ",
  275. "7.15.2" => "12.04.2018 fix in setting MODEL, prevent fhem from crash if wrong timestamp '0000-00-00' found in db ",
  276. "7.15.1" => "11.04.2018 sqlCmd accept widget textField-long, Internal MODEL is set ",
  277. "7.15.0" => "24.03.2018 new command sqlSpecial ",
  278. "7.14.7" => "21.03.2018 exportToFile,importFromFile can use file as an argument and executeBeforeDump, executeAfterDump is considered ",
  279. "7.14.6" => "18.03.2018 attribute expimpfile can use some kinds of wildcards (exportToFile, importFromFile adapted) ",
  280. "7.14.3" => "07.03.2018 DbRep_firstconnect changed - get lowest timestamp in database, DbRep_Connect deleted ",
  281. "7.14.0" => "26.02.2018 new syncStandby command",
  282. "7.12.0" => "16.02.2018 compression of dumpfile, restore of compressed files possible ",
  283. "7.11.0" => "12.02.2018 new command 'repairSQLite' to repair a corrupted SQLite database ",
  284. "7.10.0" => "10.02.2018 bugfix delete attr timeYearPeriod if set other time attributes, new 'changeValue' command ",
  285. "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 ",
  286. "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 ",
  287. "7.8.0" => "04.02.2018 new command 'eraseReadings' ",
  288. "7.7.1" => "03.02.2018 minor fix in DbRep_firstconnect if IsDisabled ",
  289. "7.7.0" => "29.01.2018 attribute 'averageCalcForm', calculation sceme 'avgDailyMeanGWS', 'avgArithmeticMean' for averageValue ",
  290. "7.6.1" => "27.01.2018 new attribute 'sqlCmdHistoryLength' and 'fetchMarkDuplicates' for highlighting multiple datasets by fetchrows ",
  291. "7.5.3" => "23.01.2018 new attribute 'ftpDumpFilesKeep', version management added to FTP-usage ",
  292. "7.4.1" => "14.01.2018 fix old dumpfiles not deleted by dumpMySQL clientSide ",
  293. "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 ",
  294. "7.3.1" => "08.01.2018 fix syntax error for perl < 5.20 ",
  295. "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 ",
  296. "6.4.1" => "13.12.2017 new Attribute 'sqlResultFieldSep' for field separate options of sqlCmd result ",
  297. "6.4.0" => "10.12.2017 prepare module for usage of datetime picker widget (Forum:#35736) ",
  298. "6.1.0" => "29.11.2017 new command delSeqDoublets (adviceRemain,adviceDelete), add Option to LASTCMD ",
  299. "6.0.0" => "18.11.2017 FTP transfer dumpfile after dump, delete old dumpfiles within Blockingcall (avoid freezes) commandref revised, minor fixes ",
  300. "5.6.4" => "05.10.2017 abortFn's adapted to use abortArg (Forum:77472) ",
  301. "5.6.3" => "01.10.2017 fix crash of fhem due to wrong rmday-calculation if month is changed, Forum:#77328 ",
  302. "5.6.0" => "17.07.2017 default timeout changed to 86400, new get-command 'procinfo' (MySQL) ",
  303. "5.4.0" => "03.07.2017 restoreMySQL - restore of csv-files (from dumpServerSide), RestoreRowsHistory/ DumpRowsHistory, Commandref revised ",
  304. "5.3.1" => "28.06.2017 vacuum for SQLite added, readings enhanced for optimizeTables / vacuum, commandref revised ",
  305. "5.3.0" => "26.06.2017 change of DbRep_mysqlOptimizeTables, new command optimizeTables ",
  306. "5.0.6" => "13.06.2017 add Aria engine to DbRep_mysqlOptimizeTables ",
  307. "5.0.3" => "07.06.2017 mysql_DoDumpServerSide added ",
  308. "5.0.1" => "05.06.2017 dependencies between dumpMemlimit and dumpSpeed created, enhanced verbose 5 logging ",
  309. "5.0.0" => "04.06.2017 MySQL Dump nonblocking added ",
  310. "4.16.1" => "22.05.2017 encode json without JSON module, requires at least fhem.pl 14348 2017-05-22 20:25:06Z ",
  311. "4.14.1" => "16.05.2017 limitation of fetchrows result datasets to 1000 by attr limit ",
  312. "4.14.0" => "15.05.2017 UserExitFn added as separate sub (DbRep_userexit) and attr userExitFn defined, new subs ReadingsBulkUpdateTimeState, ReadingsBulkUpdateValue, ReadingsSingleUpdateValue, commandref revised ",
  313. "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 ",
  314. "4.13.2" => "09.05.2017 sqlResult, sqlResultSingle are able to execute delete, insert, update commands error corrections ",
  315. "4.12.0" => "31.03.2017 support of primary key for insert functions ",
  316. "4.11.4" => "29.03.2017 bugfix timestamp in minValue, maxValue if VALUE contains more than one numeric value (like in sysmon) ",
  317. "4.11.3" => "26.03.2017 usage of daylight saving time changed to avoid wrong selection when wintertime switch to summertime, minor bug fixes ",
  318. "4.11.2" => "16.03.2017 bugfix in func dbmeta_DoParse (SQLITE_DB_FILENAME) ",
  319. "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 ",
  320. "4.10.2" => "16.01.2017 bugfix uninitialized value \$renmode if RenameAgent ",
  321. "4.10.1" => "30.11.2016 bugfix importFromFile format problem if UNIT-field wasn't set ",
  322. "4.9.0" => "23.12.2016 function readingRename added ",
  323. "4.8.6" => "17.12.2016 new bugfix group by-clause due to incompatible changes made in MyQL 5.7.5 (Forum #msg541103) ",
  324. "4.8.5" => "16.12.2016 bugfix group by-clause due to Forum #msg540610 ",
  325. "4.7.6" => "07.12.2016 DbRep version as internal, check if perl module DBI is installed ",
  326. "4.7.4" => "28.11.2016 sub DbRep_calcount changed due to Forum #msg529312 ",
  327. "4.7.3" => "20.11.2016 new diffValue function made suitable to SQLite ",
  328. "4.6.0" => "31.10.2016 bugfix calc issue due to daylight saving time end (winter time) ",
  329. "4.5.1" => "18.10.2016 get svrinfo contains SQLite database file size (MB), modified timeout routine ",
  330. "4.2.0" => "10.10.2016 allow SQL-Wildcards (% _) in attr reading & attr device ",
  331. "4.1.3" => "09.10.2016 bugfix delEntries running on SQLite ",
  332. "3.13.0" => "03.10.2016 added deviceRename to rename devices in database, new Internal DATABASE ",
  333. "3.12.0" => "02.10.2016 function minValue added ",
  334. "3.11.1" => "30.09.2016 bugfix include first and next day in calculation if Timestamp is exactly 'YYYY-MM-DD 00:00:00' ",
  335. "3.9.0" => "26.09.2016 new function importFromFile to import data from file (CSV format) ",
  336. "3.8.0" => "16.09.2016 new attr readingPreventFromDel to prevent readings from deletion when a new operation starts ",
  337. "3.7.2" => "04.09.2016 problem in diffValue fixed if if no value was selected ",
  338. "3.7.1" => "31.08.2016 Reading 'errortext' added, commandref continued, exportToFile changed, diffValue changed to fix wrong timestamp if error occur ",
  339. "3.7.0" => "30.08.2016 exportToFile added exports data to file (CSV format) ",
  340. "3.5.0" => "18.08.2016 new attribute timeOlderThan ",
  341. "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 ",
  342. "3.4.0" => "03.08.2016 function 'insert' added ",
  343. "3.3.1" => "15.07.2016 function 'diffValue' changed, write '-' if no value ",
  344. "3.3.0" => "12.07.2016 function 'diffValue' added ",
  345. "3.1.1" => "10.07.2016 state turns to initialized and connected after attr 'disabled' is switched from '1' to '0' ",
  346. "3.1.0" => "09.07.2016 new Attr 'timeDiffToNow' and change subs according to that ",
  347. "3.0.0" => "04.07.2016 no selection if timestamp isn't set and aggregation isn't set with fetchrows, delEntries ",
  348. "2.9.8" => "01.07.2016 changed fetchrows_ParseDone to handle readingvalues with whitespaces correctly ",
  349. "2.9.5" => "30.06.2016 format of readingnames changed again (substitute ':' with '-' in time) ",
  350. "2.9.4" => "30.06.2016 change readingmap to readingNameMap, prove of unsupported characters added ",
  351. "2.9.3" => "27.06.2016 format of readingnames changed avoiding some problems after restart and splitting ",
  352. "2.9.0" => "25.06.2016 attributes showproctime, timeout added ",
  353. "2.8.0" => "24.06.2016 function averageValue changed to nonblocking function ",
  354. "2.7.0" => "23.06.2016 changed function countEntries to nonblocking ",
  355. "2.6.2" => "21.06.2016 aggregation week corrected ",
  356. "2.6.1" => "20.06.2016 routine maxval_ParseDone corrected ",
  357. "2.6.0" => "31.05.2016 maxValue changed to nonblocking function ",
  358. "2.4.0" => "29.05.2016 changed to nonblocking function for sumValue ",
  359. "2.0.0" => "24.05.2016 added nonblocking function for fetchrow ",
  360. "1.2.0" => "21.05.2016 function and attribute for delEntries added ",
  361. "1.0.0" => "19.05.2016 Initial"
  362. );
  363. # Hint Hash
  364. our %DbRep_vHintsExt = (
  365. "2" => "<a href='https://www.dwd.de/DE/leistungen/klimadatendeutschland/beschreibung_tagesmonatswerte.html'>Rules</a> of german weather service for calculation of average temperatures. ",
  366. "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"
  367. );
  368. use POSIX qw(strftime);
  369. use Time::HiRes qw(gettimeofday tv_interval);
  370. use Scalar::Util qw(looks_like_number);
  371. eval "use DBI;1" or my $DbRepMMDBI = "DBI";
  372. use DBI::Const::GetInfoType;
  373. use Blocking;
  374. use Color; # colorpicker Widget
  375. use Time::Local;
  376. use Encode qw(encode_utf8);
  377. use IO::Compress::Gzip qw(gzip $GzipError);
  378. use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
  379. # no if $] >= 5.018000, warnings => 'experimental';
  380. no if $] >= 5.017011, warnings => 'experimental::smartmatch';
  381. sub DbRep_Main($$;$);
  382. sub DbLog_cutCol($$$$$$$); # DbLog-Funktion nutzen um Daten auf maximale Länge beschneiden
  383. my $DbRepVersion = "8.0.1";
  384. my %dbrep_col = ("DEVICE" => 64,
  385. "TYPE" => 64,
  386. "EVENT" => 512,
  387. "READING" => 64,
  388. "VALUE" => 128,
  389. "UNIT" => 32
  390. );
  391. ###################################################################################
  392. # DbRep_Initialize
  393. ###################################################################################
  394. sub DbRep_Initialize($) {
  395. my ($hash) = @_;
  396. $hash->{DefFn} = "DbRep_Define";
  397. $hash->{UndefFn} = "DbRep_Undef";
  398. $hash->{ShutdownFn} = "DbRep_Shutdown";
  399. $hash->{NotifyFn} = "DbRep_Notify";
  400. $hash->{SetFn} = "DbRep_Set";
  401. $hash->{GetFn} = "DbRep_Get";
  402. $hash->{AttrFn} = "DbRep_Attr";
  403. $hash->{FW_deviceOverview} = 1;
  404. $hash->{AttrList} = "disable:1,0 ".
  405. "reading ".
  406. "allowDeletion:1,0 ".
  407. "averageCalcForm:avgArithmeticMean,avgDailyMeanGWS,avgTimeWeightMean ".
  408. "device " .
  409. "dumpComment ".
  410. "dumpCompress:1,0 ".
  411. "dumpDirLocal ".
  412. "dumpDirRemote ".
  413. "dumpMemlimit ".
  414. "dumpSpeed ".
  415. "dumpFilesKeep:0,1,2,3,4,5,6,7,8,9,10 ".
  416. "executeBeforeProc ".
  417. "executeAfterProc ".
  418. "expimpfile ".
  419. "fetchRoute:ascent,descent ".
  420. "fetchMarkDuplicates:red,blue,brown,green,orange ".
  421. "ftpDebug:1,0 ".
  422. "ftpDir ".
  423. "ftpDumpFilesKeep:1,2,3,4,5,6,7,8,9,10 ".
  424. "ftpPassive:1,0 ".
  425. "ftpPwd ".
  426. "ftpPort ".
  427. "ftpServer ".
  428. "ftpTimeout ".
  429. "ftpUse:1,0 ".
  430. "ftpUser ".
  431. "ftpUseSSL:1,0 ".
  432. "aggregation:hour,day,week,month,no ".
  433. "diffAccept ".
  434. "limit ".
  435. "optimizeTablesBeforeDump:1,0 ".
  436. "readingNameMap ".
  437. "readingPreventFromDel ".
  438. "role:Client,Agent ".
  439. "seqDoubletsVariance ".
  440. "showproctime:1,0 ".
  441. "showSvrInfo ".
  442. "showVariables ".
  443. "showStatus ".
  444. "showTableInfo ".
  445. "sqlCmdHistoryLength:0,5,10,15,20,25,30,35,40,45,50 ".
  446. "sqlResultFormat:separated,mline,sline,table,json ".
  447. "sqlResultFieldSep:|,:,\/ ".
  448. "timeYearPeriod ".
  449. "timestamp_begin ".
  450. "timestamp_end ".
  451. "timeDiffToNow ".
  452. "timeOlderThan ".
  453. "timeout ".
  454. "userExitFn ".
  455. "valueFilter ".
  456. $readingFnAttributes;
  457. # Umbenennen von existierenden Attrbuten
  458. # $hash->{AttrRenameMap} = { "reading" => "readingFilter",
  459. # "device" => "deviceFilter",
  460. # };
  461. return undef;
  462. }
  463. ###################################################################################
  464. # DbRep_Define
  465. ###################################################################################
  466. sub DbRep_Define($@) {
  467. # define <name> DbRep <DbLog-Device>
  468. # ($hash) [1] [2]
  469. #
  470. my ($hash, $def) = @_;
  471. my $name = $hash->{NAME};
  472. return "Error: Perl module ".$DbRepMMDBI." is missing. Install it on Debian with: sudo apt-get install libdbi-perl" if($DbRepMMDBI);
  473. my @a = split("[ \t][ \t]*", $def);
  474. if(!$a[2]) {
  475. return "You need to specify more parameters.\n". "Format: define <name> DbRep <DbLog-Device>";
  476. } elsif (!$defs{$a[2]}) {
  477. return "The specified DbLog-Device \"$a[2]\" doesn't exist.";
  478. }
  479. $hash->{LASTCMD} = " ";
  480. $hash->{ROLE} = AttrVal($name, "role", "Client");
  481. $hash->{MODEL} = $hash->{ROLE};
  482. $hash->{HELPER}{DBLOGDEVICE} = $a[2];
  483. $hash->{VERSION} = (reverse sort(keys %DbRep_vNotesIntern))[0];
  484. $hash->{NOTIFYDEV} = "global,".$name; # nur Events dieser Devices an DbRep_Notify weiterleiten
  485. my $dbconn = $defs{$a[2]}{dbconn};
  486. $hash->{DATABASE} = (split(/;|=/, $dbconn))[1];
  487. $hash->{UTF8} = defined($defs{$a[2]}{UTF8})?$defs{$a[2]}{UTF8}:0;
  488. my ($err,$hl) = DbRep_getCmdFile($name."_sqlCmdList");
  489. if(!$err) {
  490. $hash->{HELPER}{SQLHIST} = $hl;
  491. Log3 ($name, 4, "DbRep $name - history sql commandlist read from file ".$attr{global}{modpath}."/FHEM/FhemUtils/cacheDbRep");
  492. }
  493. RemoveInternalTimer($hash);
  494. InternalTimer(gettimeofday()+int(rand(45)), 'DbRep_firstconnect', $hash, 0);
  495. Log3 ($name, 4, "DbRep $name - initialized");
  496. ReadingsSingleUpdateValue ($hash, 'state', 'initialized', 1);
  497. return undef;
  498. }
  499. ###################################################################################
  500. # DbRep_Set
  501. ###################################################################################
  502. sub DbRep_Set($@) {
  503. my ($hash, @a) = @_;
  504. return "\"set X\" needs at least an argument" if ( @a < 2 );
  505. my $name = $a[0];
  506. my $opt = $a[1];
  507. my $prop = $a[2];
  508. my $dbh = $hash->{DBH};
  509. my $dblogdevice = $hash->{HELPER}{DBLOGDEVICE};
  510. $hash->{dbloghash} = $defs{$dblogdevice};
  511. my $dbmodel = $hash->{dbloghash}{MODEL};
  512. my $dbname = $hash->{DATABASE};
  513. my $sd ="";
  514. my (@bkps,$dir);
  515. $dir = AttrVal($name, "dumpDirLocal", "./"); # 'dumpDirRemote' (Backup-Verz. auf dem MySQL-Server) muß gemountet sein und in 'dumpDirLocal' eingetragen sein
  516. $dir = $dir."/" unless($dir =~ m/\/$/);
  517. opendir(DIR,$dir);
  518. if ($dbmodel =~ /MYSQL/) {
  519. $dbname = $hash->{DATABASE};
  520. $sd = $dbname.".*(csv|sql)";
  521. } elsif ($dbmodel =~ /SQLITE/) {
  522. $dbname = $hash->{DATABASE};
  523. $dbname = (split /[\/]/, $dbname)[-1];
  524. $dbname = (split /\./, $dbname)[0];
  525. $sd = $dbname."_.*.sqlitebkp";
  526. }
  527. while (my $file = readdir(DIR)) {
  528. next unless (-f "$dir/$file");
  529. next unless ($file =~ /^$sd/);
  530. push @bkps,$file;
  531. }
  532. closedir(DIR);
  533. my $cj = @bkps?join(",",reverse(sort @bkps)):" ";
  534. # Drop-Down Liste bisherige Befehle in "sqlCmd" erstellen
  535. my $hl = $hash->{HELPER}{SQLHIST}.",___purge_historylist___" if($hash->{HELPER}{SQLHIST});
  536. my $setlist = "Unknown argument $opt, choose one of ".
  537. "eraseReadings:noArg ".
  538. (($hash->{ROLE} ne "Agent")?"sumValue:display,writeToDB ":"").
  539. (($hash->{ROLE} ne "Agent")?"averageValue:display,writeToDB ":"").
  540. (($hash->{ROLE} ne "Agent")?"changeValue ":"").
  541. (($hash->{ROLE} ne "Agent")?"delEntries:noArg ":"").
  542. (($hash->{ROLE} ne "Agent")?"delSeqDoublets:adviceRemain,adviceDelete,delete ":"").
  543. "deviceRename ".
  544. (($hash->{ROLE} ne "Agent")?"readingRename ":"").
  545. (($hash->{ROLE} ne "Agent")?"exportToFile ":"").
  546. (($hash->{ROLE} ne "Agent")?"importFromFile ":"").
  547. (($hash->{ROLE} ne "Agent")?"maxValue:display,writeToDB ":"").
  548. (($hash->{ROLE} ne "Agent")?"minValue:display,writeToDB ":"").
  549. (($hash->{ROLE} ne "Agent")?"fetchrows:history,current ":"").
  550. (($hash->{ROLE} ne "Agent")?"diffValue:display,writeToDB ":"").
  551. (($hash->{ROLE} ne "Agent")?"insert ":"").
  552. (($hash->{ROLE} ne "Agent")?"sqlCmd ":"").
  553. (($hash->{ROLE} ne "Agent" && $hl)?"sqlCmdHistory:".$hl." ":"").
  554. (($hash->{ROLE} ne "Agent")?"sqlSpecial:50mostFreqLogsLast2days,allDevCount,allDevReadCount ":"").
  555. (($hash->{ROLE} ne "Agent")?"syncStandby ":"").
  556. (($hash->{ROLE} ne "Agent")?"tableCurrentFillup:noArg ":"").
  557. (($hash->{ROLE} ne "Agent")?"tableCurrentPurge:noArg ":"").
  558. (($hash->{ROLE} ne "Agent" && $dbmodel =~ /MYSQL/ )?"dumpMySQL:clientSide,serverSide ":"").
  559. (($hash->{ROLE} ne "Agent" && $dbmodel =~ /SQLITE/ )?"dumpSQLite:noArg ":"").
  560. (($hash->{ROLE} ne "Agent" && $dbmodel =~ /SQLITE/ )?"repairSQLite ":"").
  561. (($hash->{ROLE} ne "Agent" && $dbmodel =~ /MYSQL/ )?"optimizeTables:noArg ":"").
  562. (($hash->{ROLE} ne "Agent" && $dbmodel =~ /SQLITE|POSTGRESQL/ )?"vacuum:noArg ":"").
  563. (($hash->{ROLE} ne "Agent" && $dbmodel =~ /MYSQL/)?"restoreMySQL:".$cj." ":"").
  564. (($hash->{ROLE} ne "Agent" && $dbmodel =~ /SQLITE/)?"restoreSQLite:".$cj." ":"").
  565. (($hash->{ROLE} ne "Agent")?"countEntries:history,current ":"");
  566. return if(IsDisabled($name));
  567. if ($opt =~ /eraseReadings/) {
  568. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  569. # Readings löschen die nicht in der Ausnahmeliste (Attr readingPreventFromDel) stehen
  570. DbRep_delread($hash);
  571. return undef;
  572. }
  573. if ($opt eq "dumpMySQL" && $hash->{ROLE} ne "Agent") {
  574. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  575. if ($prop eq "serverSide") {
  576. Log3 ($name, 3, "DbRep $name - ################################################################");
  577. Log3 ($name, 3, "DbRep $name - ### New database serverSide dump ###");
  578. Log3 ($name, 3, "DbRep $name - ################################################################");
  579. } else {
  580. Log3 ($name, 3, "DbRep $name - ################################################################");
  581. Log3 ($name, 3, "DbRep $name - ### New database clientSide dump ###");
  582. Log3 ($name, 3, "DbRep $name - ################################################################");
  583. }
  584. # Befehl vor Procedure ausführen
  585. DbRep_beforeproc($hash, "dump");
  586. DbRep_Main($hash,$opt,$prop);
  587. return undef;
  588. }
  589. if ($opt eq "dumpSQLite" && $hash->{ROLE} ne "Agent") {
  590. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  591. Log3 ($name, 3, "DbRep $name - ################################################################");
  592. Log3 ($name, 3, "DbRep $name - ### New SQLite dump ###");
  593. Log3 ($name, 3, "DbRep $name - ################################################################");
  594. # Befehl vor Procedure ausführen
  595. DbRep_beforeproc($hash, "dump");
  596. DbRep_Main($hash,$opt,$prop);
  597. return undef;
  598. }
  599. if ($opt eq "repairSQLite" && $hash->{ROLE} ne "Agent") {
  600. $prop = $prop?$prop:36000;
  601. if($prop) {
  602. unless($prop =~ /^(\d+)$/) { return " The Value of $opt is not valid. Use only figures 0-9 without decimal places !";};
  603. # unless ($aVal =~ /^[0-9]+$/) { return " The Value of $aName is not valid. Use only figures 0-9 without decimal places !";}
  604. }
  605. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  606. Log3 ($name, 3, "DbRep $name - ################################################################");
  607. Log3 ($name, 3, "DbRep $name - ### New SQLite repair attempt ###");
  608. Log3 ($name, 3, "DbRep $name - ################################################################");
  609. Log3 ($name, 3, "DbRep $name - start repair attempt of database ".$hash->{DATABASE});
  610. # closetime Datenbank
  611. my $dbloghash = $hash->{dbloghash};
  612. my $dbl = $dbloghash->{NAME};
  613. CommandSet(undef,"$dbl reopen $prop");
  614. # Befehl vor Procedure ausführen
  615. DbRep_beforeproc($hash, "repair");
  616. DbRep_Main($hash,$opt);
  617. return undef;
  618. }
  619. if ($opt =~ /restoreMySQL|restoreSQLite/ && $hash->{ROLE} ne "Agent") {
  620. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  621. Log3 ($name, 3, "DbRep $name - ################################################################");
  622. Log3 ($name, 3, "DbRep $name - ### New database Restore/Recovery ###");
  623. Log3 ($name, 3, "DbRep $name - ################################################################");
  624. # Befehl vor Procedure ausführen
  625. DbRep_beforeproc($hash, "restore");
  626. DbRep_Main($hash,$opt,$prop);
  627. return undef;
  628. }
  629. if ($opt =~ /optimizeTables|vacuum/ && $hash->{ROLE} ne "Agent") {
  630. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  631. Log3 ($name, 3, "DbRep $name - ################################################################");
  632. Log3 ($name, 3, "DbRep $name - ### New optimize table / vacuum execution ###");
  633. Log3 ($name, 3, "DbRep $name - ################################################################");
  634. # Befehl vor Procedure ausführen
  635. DbRep_beforeproc($hash, "optimize");
  636. DbRep_Main($hash,$opt);
  637. return undef;
  638. }
  639. if ($opt =~ m/delSeqDoublets/ && $hash->{ROLE} ne "Agent") {
  640. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  641. if ($prop =~ /delete/ && !AttrVal($hash->{NAME}, "allowDeletion", 0)) {
  642. return " Set attribute 'allowDeletion' if you want to allow deletion of any database entries. Use it with care !";
  643. }
  644. DbRep_beforeproc($hash, "delSeq");
  645. DbRep_Main($hash,$opt,$prop);
  646. return undef;
  647. }
  648. if ($hash->{HELPER}{RUNNING_BACKUP_CLIENT}) {
  649. $setlist = "Unknown argument $opt, choose one of ".
  650. (($hash->{ROLE} ne "Agent")?"cancelDump:noArg ":"");
  651. }
  652. if ($hash->{HELPER}{RUNNING_REPAIR}) {
  653. $setlist = "Unknown argument $opt, choose one of ".
  654. (($hash->{ROLE} ne "Agent")?"cancelRepair:noArg ":"");
  655. }
  656. if ($hash->{HELPER}{RUNNING_RESTORE}) {
  657. $setlist = "Unknown argument $opt, choose one of ".
  658. (($hash->{ROLE} ne "Agent")?"cancelRestore:noArg ":"");
  659. }
  660. if ($opt eq "cancelDump" && $hash->{ROLE} ne "Agent") {
  661. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  662. BlockingKill($hash->{HELPER}{RUNNING_BACKUP_CLIENT});
  663. Log3 ($name, 3, "DbRep $name -> running Dump has been canceled");
  664. ReadingsSingleUpdateValue ($hash, "state", "Dump canceled", 1);
  665. return undef;
  666. }
  667. if ($opt eq "cancelRepair" && $hash->{ROLE} ne "Agent") {
  668. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  669. BlockingKill($hash->{HELPER}{RUNNING_REPAIR});
  670. Log3 ($name, 3, "DbRep $name -> running Repair has been canceled");
  671. ReadingsSingleUpdateValue ($hash, "state", "Repair canceled", 1);
  672. return undef;
  673. }
  674. if ($opt eq "cancelRestore" && $hash->{ROLE} ne "Agent") {
  675. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  676. BlockingKill($hash->{HELPER}{RUNNING_RESTORE});
  677. Log3 ($name, 3, "DbRep $name -> running Restore has been canceled");
  678. ReadingsSingleUpdateValue ($hash, "state", "Restore canceled", 1);
  679. return undef;
  680. }
  681. #######################################################################################################
  682. ## keine Aktionen außer die über diesem Eintrag solange Reopen xxxx im DbLog-Device läuft
  683. #######################################################################################################
  684. if ($hash->{dbloghash}{HELPER}{REOPEN_RUNS} && $opt !~ /\?/) {
  685. my $ro = $hash->{dbloghash}{HELPER}{REOPEN_RUNS_UNTIL};
  686. Log3 ($name, 3, "DbRep $name - connection $dblogdevice to db $dbname is closed until $ro - $opt postponed");
  687. ReadingsSingleUpdateValue ($hash, "state", "connection $dblogdevice to $dbname is closed until $ro - $opt postponed", 1);
  688. return;
  689. }
  690. #######################################################################################################
  691. if ($opt =~ /countEntries/ && $hash->{ROLE} ne "Agent") {
  692. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  693. my $table = $prop?$prop:"history";
  694. DbRep_Main($hash,$opt,$table);
  695. } elsif ($opt =~ /fetchrows/ && $hash->{ROLE} ne "Agent") {
  696. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  697. my $table = $prop?$prop:"history";
  698. DbRep_Main($hash,$opt,$table);
  699. } elsif ($opt =~ m/(max|min|sum|average|diff)Value/ && $hash->{ROLE} ne "Agent") {
  700. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  701. if (!AttrVal($hash->{NAME}, "reading", "")) {
  702. return " The attribute reading to analyze is not set !";
  703. }
  704. if ($prop && $prop =~ /writeToDB/) {
  705. if (!AttrVal($hash->{NAME}, "device", "") || AttrVal($hash->{NAME}, "device", "") =~ /[%*:=,]/ || AttrVal($hash->{NAME}, "reading", "") =~ /[,\s]/) {
  706. return "<html>If you want write results back to database, attributes \"device\" and \"reading\" must be set.<br>
  707. 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>
  708. The \"reading\" to evaluate has to be a single reading and no list.</html>";
  709. }
  710. }
  711. DbRep_Main($hash,$opt,$prop);
  712. } elsif ($opt =~ m/delEntries|tableCurrentPurge/ && $hash->{ROLE} ne "Agent") {
  713. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  714. if (!AttrVal($hash->{NAME}, "allowDeletion", undef)) {
  715. return " Set attribute 'allowDeletion' if you want to allow deletion of any database entries. Use it with care !";
  716. }
  717. DbRep_beforeproc($hash, "delEntries");
  718. DbRep_Main($hash,$opt);
  719. } elsif ($opt =~ m/tableCurrentFillup/ && $hash->{ROLE} ne "Agent") {
  720. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  721. DbRep_Main($hash,$opt);
  722. } elsif ($opt eq "deviceRename") {
  723. shift @a;
  724. shift @a;
  725. $prop = join(" ",@a); # Device Name kann Leerzeichen enthalten
  726. Log3 ($name, 1, "DbRep $name - a: @a");
  727. my ($olddev, $newdev) = split(",",$prop);
  728. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  729. if (!$olddev || !$newdev) {return "Both entries \"old device name\", \"new device name\" are needed. Use \"set $name deviceRename olddevname,newdevname\" ";}
  730. $hash->{HELPER}{OLDDEV} = $olddev;
  731. $hash->{HELPER}{NEWDEV} = $newdev;
  732. $hash->{HELPER}{RENMODE} = "devren";
  733. DbRep_Main($hash,$opt);
  734. } elsif ($opt eq "readingRename") {
  735. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  736. my ($oldread, $newread) = split(",",$prop);
  737. if (!$oldread || !$newread) {return "Both entries \"old reading name\", \"new reading name\" are needed. Use \"set $name readingRename oldreadingname,newreadingname\" ";}
  738. $hash->{HELPER}{OLDREAD} = $oldread;
  739. $hash->{HELPER}{NEWREAD} = $newread;
  740. $hash->{HELPER}{RENMODE} = "readren";
  741. DbRep_Main($hash,$opt);
  742. } elsif ($opt eq "insert" && $hash->{ROLE} ne "Agent") {
  743. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  744. if ($prop) {
  745. if (!AttrVal($hash->{NAME}, "device", "") || !AttrVal($hash->{NAME}, "reading", "") ) {
  746. return "One or both of attributes \"device\", \"reading\" are not set. It's mandatory to set both to complete dataset for manual insert !";
  747. }
  748. # Attribute device & reading dürfen kein SQL-Wildcard % enthalten
  749. return "One or both of attributes \"device\", \"reading\" containing SQL wildcard \"%\". Wildcards are not allowed in function manual insert !"
  750. if(AttrVal($hash->{NAME},"device","") =~ m/%/ || AttrVal($hash->{NAME},"reading","") =~ m/%/ );
  751. my ($i_date, $i_time, $i_value, $i_unit) = split(",",$prop);
  752. 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)>' ";}
  753. unless ($i_date =~ /(\d{4})-(\d{2})-(\d{2})/) {return "Input for date is not valid. Use format YYYY-MM-DD !";}
  754. unless ($i_time =~ /(\d{2}):(\d{2}):(\d{2})/) {return "Input for time is not valid. Use format HH:MM:SS !";}
  755. my $i_timestamp = $i_date." ".$i_time;
  756. my ($yyyy, $mm, $dd, $hh, $min, $sec) = ($i_timestamp =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/);
  757. eval { my $ts = timelocal($sec, $min, $hh, $dd, $mm-1, $yyyy-1900); };
  758. if ($@) {
  759. my @l = split (/at/, $@);
  760. return " Timestamp is out of range - $l[0]";
  761. }
  762. my $i_device = AttrVal($hash->{NAME}, "device", "");
  763. my $i_reading = AttrVal($hash->{NAME}, "reading", "");
  764. # Daten auf maximale Länge (entsprechend der Feldlänge in DbLog DB create-scripts) beschneiden wenn nicht SQLite
  765. if ($dbmodel ne 'SQLITE') {
  766. $i_device = substr($i_device,0, $dbrep_col{DEVICE});
  767. $i_reading = substr($i_reading,0, $dbrep_col{READING});
  768. $i_value = substr($i_value,0, $dbrep_col{VALUE});
  769. $i_unit = substr($i_unit,0, $dbrep_col{UNIT}) if($i_unit);
  770. }
  771. $hash->{HELPER}{I_TIMESTAMP} = $i_timestamp;
  772. $hash->{HELPER}{I_DEVICE} = $i_device;
  773. $hash->{HELPER}{I_READING} = $i_reading;
  774. $hash->{HELPER}{I_VALUE} = $i_value;
  775. $hash->{HELPER}{I_UNIT} = $i_unit;
  776. $hash->{HELPER}{I_TYPE} = my $i_type = "manual";
  777. $hash->{HELPER}{I_EVENT} = my $i_event = "manual";
  778. } else {
  779. return "Data to insert to table 'history' are needed like this pattern: 'Date,Time,Value,[Unit]'. \"Unit\" is optional. Spaces are not allowed !";
  780. }
  781. DbRep_Main($hash,$opt);
  782. } elsif ($opt eq "exportToFile" && $hash->{ROLE} ne "Agent") {
  783. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  784. my $f = $prop if($prop);
  785. if (!AttrVal($hash->{NAME}, "expimpfile", "") && !$f) {
  786. return "\"$opt\" needs a file as an argument or the attribute \"expimpfile\" (path and filename) to be set !";
  787. }
  788. DbRep_Main($hash,$opt,$f);
  789. } elsif ($opt eq "importFromFile" && $hash->{ROLE} ne "Agent") {
  790. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  791. my $f = $prop if($prop);
  792. if (!AttrVal($hash->{NAME}, "expimpfile", "") && !$f) {
  793. return "\"$opt\" needs a file as an argument or the attribute \"expimpfile\" (path and filename) to be set !";
  794. }
  795. DbRep_Main($hash,$opt,$f);
  796. } elsif ($opt =~ /sqlCmd|sqlSpecial|sqlCmdHistory/) {
  797. return "\"set $opt\" needs at least an argument" if ( @a < 3 );
  798. # remove arg 0, 1 to get SQL command
  799. my $sqlcmd;
  800. if($opt eq "sqlSpecial") {
  801. $sqlcmd = $prop;
  802. }
  803. if($opt eq "sqlCmd") {
  804. my @cmd = @a;
  805. shift @cmd; shift @cmd;
  806. $sqlcmd = join(" ", @cmd);
  807. $sqlcmd =~ tr/ A-Za-z0-9!"#$§%&'()*+,-.\/:;<=>?@[\\]^_`{|}~äöüÄÖÜ߀/ /cs;
  808. }
  809. if($opt eq "sqlCmdHistory") {
  810. $prop =~ tr/ A-Za-z0-9!"#$%&'()*+,-.\/:;<=>?@[\\]^_`{|}~äöüÄÖÜ߀/ /cs;
  811. $prop =~ s/<c>/,/g;
  812. $sqlcmd = $prop;
  813. if($sqlcmd eq "___purge_historylist___") {
  814. delete($hash->{HELPER}{SQLHIST});
  815. DbRep_setCmdFile($name."_sqlCmdList","",$hash); # Löschen der sql History Liste im DbRep-Keyfile
  816. return "SQL command historylist of $name deleted.";
  817. }
  818. }
  819. $hash->{LASTCMD} = $sqlcmd?"$opt $sqlcmd":"$opt";
  820. if ($sqlcmd =~ m/^\s*delete/is && !AttrVal($hash->{NAME}, "allowDeletion", undef)) {
  821. return "Attribute 'allowDeletion = 1' is needed for command '$sqlcmd'. Use it with care !";
  822. }
  823. DbRep_Main($hash,$opt,$sqlcmd);
  824. } elsif ($opt =~ /changeValue/) {
  825. shift @a;
  826. shift @a;
  827. $prop = join(" ", @a);
  828. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  829. unless($prop =~ m/^\s*(".*",".*")\s*$/) {return "Both entries \"old string\", \"new string\" are needed. Use \"set $name changeValue \"old string\",\"new string\" (use quotes)";}
  830. my $complex = 0;
  831. my ($oldval,$newval) = ($prop =~ /^\s*"(.*?)","(.*?)"\s*$/);
  832. if($newval =~ m/[{}]/) {
  833. if($newval =~ m/^\s*(\{.*\})\s*$/s) {
  834. $newval = $1;
  835. $complex = 1;
  836. my %specials = (
  837. "%VALUE" => $name,
  838. "%UNIT" => $name,
  839. );
  840. $newval = EvalSpecials($newval, %specials);
  841. } else {
  842. return "The expression of \"new string\" has to be included in \"{ }\" ";
  843. }
  844. }
  845. $hash->{HELPER}{COMPLEX} = $complex;
  846. $hash->{HELPER}{OLDVAL} = $oldval;
  847. $hash->{HELPER}{NEWVAL} = $newval;
  848. $hash->{HELPER}{RENMODE} = "changeval";
  849. DbRep_beforeproc($hash, "changeval");
  850. DbRep_Main($hash,$opt);
  851. } elsif ($opt =~ m/syncStandby/ && $hash->{ROLE} ne "Agent") {
  852. unless($prop) {return "A DbLog-device (standby) is needed to sync. Use \"set $name syncStandby <DbLog-standby name>\" ";}
  853. if(!exists($defs{$prop}) || $defs{$prop}->{TYPE} ne "DbLog") {
  854. return "The device \"$prop\" doesn't exist or is not a DbLog-device. ";
  855. }
  856. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  857. DbRep_Main($hash,$opt,$prop);
  858. } else {
  859. return "$setlist";
  860. }
  861. return undef;
  862. }
  863. ###################################################################################
  864. # DbRep_Get
  865. ###################################################################################
  866. sub DbRep_Get($@) {
  867. my ($hash, @a) = @_;
  868. return "\"get X\" needs at least an argument" if ( @a < 2 );
  869. my $name = $a[0];
  870. my $opt = $a[1];
  871. my $prop = $a[2];
  872. my $dbh = $hash->{DBH};
  873. my $dblogdevice = $hash->{HELPER}{DBLOGDEVICE};
  874. $hash->{dbloghash} = $defs{$dblogdevice};
  875. my $dbmodel = $hash->{dbloghash}{MODEL};
  876. my $dbname = $hash->{DATABASE};
  877. my $to = AttrVal($name, "timeout", "86400");
  878. my $getlist = "Unknown argument $opt, choose one of ".
  879. "svrinfo:noArg ".
  880. "blockinginfo:noArg ".
  881. "minTimestamp:noArg ".
  882. "dbValue ".
  883. (($dbmodel eq "MYSQL")?"dbstatus:noArg ":"").
  884. (($dbmodel eq "MYSQL")?"tableinfo:noArg ":"").
  885. (($dbmodel eq "MYSQL")?"procinfo:noArg ":"").
  886. (($dbmodel eq "MYSQL")?"dbvars:noArg ":"").
  887. "versionNotes:noArg "
  888. ;
  889. return if(IsDisabled($name));
  890. if ($hash->{dbloghash}{HELPER}{REOPEN_RUNS} && $opt !~ /\?|procinfo|blockinginfo/) {
  891. my $ro = $hash->{dbloghash}{HELPER}{REOPEN_RUNS_UNTIL};
  892. Log3 ($name, 3, "DbRep $name - connection $dblogdevice to db $dbname is closed until $ro - $opt postponed");
  893. ReadingsSingleUpdateValue ($hash, "state", "connection $dblogdevice to $dbname is closed until $ro - $opt postponed", 1);
  894. return;
  895. }
  896. if ($opt =~ /dbvars|dbstatus|tableinfo|procinfo/) {
  897. return "Dump is running - try again later !" if($hash->{HELPER}{RUNNING_BACKUP_CLIENT});
  898. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  899. return "The operation \"$opt\" isn't available with database type $dbmodel" if ($dbmodel ne 'MYSQL');
  900. ReadingsSingleUpdateValue ($hash, "state", "running", 1);
  901. DbRep_delread($hash); # Readings löschen die nicht in der Ausnahmeliste (Attr readingPreventFromDel) stehen
  902. $hash->{HELPER}{RUNNING_PID} = BlockingCall("dbmeta_DoParse", "$name|$opt", "dbmeta_ParseDone", $to, "DbRep_ParseAborted", $hash);
  903. } elsif ($opt eq "svrinfo") {
  904. return "Dump is running - try again later !" if($hash->{HELPER}{RUNNING_BACKUP_CLIENT});
  905. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  906. DbRep_delread($hash);
  907. ReadingsSingleUpdateValue ($hash, "state", "running", 1);
  908. $hash->{HELPER}{RUNNING_PID} = BlockingCall("dbmeta_DoParse", "$name|$opt", "dbmeta_ParseDone", $to, "DbRep_ParseAborted", $hash);
  909. } elsif ($opt eq "blockinginfo") {
  910. return "Dump is running - try again later !" if($hash->{HELPER}{RUNNING_BACKUP_CLIENT});
  911. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  912. DbRep_delread($hash);
  913. ReadingsSingleUpdateValue ($hash, "state", "running", 1);
  914. DbRep_getblockinginfo($hash);
  915. } elsif ($opt eq "minTimestamp") {
  916. return "Dump is running - try again later !" if($hash->{HELPER}{RUNNING_BACKUP_CLIENT});
  917. $hash->{LASTCMD} = $prop?"$opt $prop":"$opt";
  918. DbRep_delread($hash);
  919. ReadingsSingleUpdateValue ($hash, "state", "running", 1);
  920. DbRep_firstconnect($hash);
  921. } elsif ($opt =~ /dbValue/) {
  922. return "get \"$opt\" needs at least an argument" if ( @a < 3 );
  923. # remove arg 0, 1 to get SQL command
  924. my @cmd = @a;
  925. shift @cmd; shift @cmd;
  926. my $sqlcmd = join(" ",@cmd);
  927. $sqlcmd =~ tr/ A-Za-z0-9!"#$§%&'()*+,-.\/:;<=>?@[\\]^_`{|}~äöüÄÖÜ߀/ /cs;
  928. $hash->{LASTCMD} = $sqlcmd?"$opt $sqlcmd":"$opt";
  929. if ($sqlcmd =~ m/^\s*delete/is && !AttrVal($hash->{NAME}, "allowDeletion", undef)) {
  930. return "Attribute 'allowDeletion = 1' is needed for command '$sqlcmd'. Use it with care !";
  931. }
  932. my ($err,$ret) = DbRep_dbValue($name,$sqlcmd);
  933. return $err?$err:$ret;
  934. } elsif ($opt =~ /versionNotes/) {
  935. my $header = "<b>Module release information table</b><br>";
  936. my $header1 = "<b>Helpful hints</b><br>";
  937. # Ausgabetabelle erstellen
  938. my ($ret,$val0,$val1);
  939. $ret = "<html>";
  940. $ret .= sprintf("<div class=\"makeTable wide\"; style=\"text-align:left\">$header <br>");
  941. $ret .= "<table class=\"block wide internals\">";
  942. $ret .= "<tbody>";
  943. $ret .= "<tr class=\"even\">";
  944. my $i = 0;
  945. foreach my $key (reverse sort(keys %DbRep_vNotesExtern)) {
  946. ($val0,$val1) = split(/\s/,$DbRep_vNotesExtern{$key},2);
  947. $ret .= sprintf("<td style=\"vertical-align:top\"><b>$key</b> </td><td style=\"vertical-align:top\">$val0 </td><td>$val1</td>" );
  948. $ret .= "</tr>";
  949. $i++;
  950. if ($i & 1) {
  951. # $i ist ungerade
  952. $ret .= "<tr class=\"odd\">";
  953. } else {
  954. $ret .= "<tr class=\"even\">";
  955. }
  956. }
  957. $ret .= "</tr>";
  958. $ret .= "</tbody>";
  959. $ret .= "</table>";
  960. $ret .= "</div>";
  961. $ret .= sprintf("<div class=\"makeTable wide\"; style=\"text-align:left\">$header1 <br>");
  962. $ret .= "<table class=\"block wide internals\">";
  963. $ret .= "<tbody>";
  964. $ret .= "<tr class=\"even\">";
  965. $i = 0;
  966. foreach my $key (reverse sort(keys %DbRep_vHintsExt)) {
  967. $val0 = $DbRep_vHintsExt{$key};
  968. $ret .= sprintf("<td style=\"vertical-align:top\"><b>$key</b> </td><td style=\"vertical-align:top\">$val0</td>" );
  969. $ret .= "</tr>";
  970. $i++;
  971. if ($i & 1) {
  972. # $i ist ungerade
  973. $ret .= "<tr class=\"odd\">";
  974. } else {
  975. $ret .= "<tr class=\"even\">";
  976. }
  977. }
  978. $ret .= "</tr>";
  979. $ret .= "</tbody>";
  980. $ret .= "</table>";
  981. $ret .= "</div>";
  982. $ret .= "</html>";
  983. return $ret;
  984. } else {
  985. return "$getlist";
  986. }
  987. return undef;
  988. }
  989. ###################################################################################
  990. # DbRep_Attr
  991. ###################################################################################
  992. sub DbRep_Attr($$$$) {
  993. my ($cmd,$name,$aName,$aVal) = @_;
  994. my $hash = $defs{$name};
  995. $hash->{dbloghash} = $defs{$hash->{HELPER}{DBLOGDEVICE}};
  996. my $dbmodel = $hash->{dbloghash}{MODEL};
  997. my $do;
  998. # $cmd can be "del" or "set"
  999. # $name is device name
  1000. # aName and aVal are Attribute name and value
  1001. # nicht erlaubte / nicht setzbare Attribute wenn role = Agent
  1002. my @agentnoattr = qw(aggregation
  1003. allowDeletion
  1004. dumpDirLocal
  1005. reading
  1006. readingNameMap
  1007. readingPreventFromDel
  1008. device
  1009. diffAccept
  1010. executeBeforeProc
  1011. executeAfterProc
  1012. expimpfile
  1013. ftpUse
  1014. ftpUser
  1015. ftpUseSSL
  1016. ftpDebug
  1017. ftpDir
  1018. ftpPassive
  1019. ftpPort
  1020. ftpPwd
  1021. ftpServer
  1022. ftpTimeout
  1023. dumpMemlimit
  1024. dumpComment
  1025. dumpSpeed
  1026. optimizeTablesBeforeDump
  1027. seqDoubletsVariance
  1028. sqlCmdHistoryLength
  1029. timeYearPeriod
  1030. timestamp_begin
  1031. timestamp_end
  1032. timeDiffToNow
  1033. timeOlderThan
  1034. sqlResultFormat
  1035. );
  1036. if ($aName eq "disable") {
  1037. if($cmd eq "set") {
  1038. $do = ($aVal) ? 1 : 0;
  1039. }
  1040. $do = 0 if($cmd eq "del");
  1041. my $val = ($do == 1 ? "disabled" : "initialized");
  1042. ReadingsSingleUpdateValue ($hash, "state", $val, 1);
  1043. if ($do == 0) {
  1044. RemoveInternalTimer($hash);
  1045. InternalTimer(time+5, 'DbRep_firstconnect', $hash, 0);
  1046. } else {
  1047. my $dbh = $hash->{DBH};
  1048. $dbh->disconnect() if($dbh);
  1049. }
  1050. }
  1051. if ($cmd eq "set" && $hash->{ROLE} eq "Agent") {
  1052. foreach (@agentnoattr) {
  1053. return ("Attribute $aName is not usable due to role of $name is \"$hash->{ROLE}\" ") if ($_ eq $aName);
  1054. }
  1055. }
  1056. if ($aName eq "readingPreventFromDel") {
  1057. if($cmd eq "set") {
  1058. 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.";}
  1059. $hash->{HELPER}{RDPFDEL} = $aVal;
  1060. } else {
  1061. delete $hash->{HELPER}{RDPFDEL} if($hash->{HELPER}{RDPFDEL});
  1062. }
  1063. }
  1064. if ($aName eq "sqlCmdHistoryLength") {
  1065. if($cmd eq "set") {
  1066. $do = ($aVal) ? 1 : 0;
  1067. }
  1068. $do = 0 if($cmd eq "del");
  1069. if ($do == 0) {
  1070. delete($hash->{HELPER}{SQLHIST});
  1071. DbRep_setCmdFile($name."_sqlCmdList","",$hash); # Löschen der sql History Liste im DbRep-Keyfile
  1072. }
  1073. }
  1074. if ($aName eq "userExitFn") {
  1075. if($cmd eq "set") {
  1076. if(!$aVal) {return "Usage of $aName is wrong. The function has to be specified as \"<UserExitFn> [reading:value]\" ";}
  1077. my @a = split(/ /,$aVal,2);
  1078. $hash->{HELPER}{USEREXITFN} = $a[0];
  1079. $hash->{HELPER}{UEFN_REGEXP} = $a[1] if($a[1]);
  1080. } else {
  1081. delete $hash->{HELPER}{USEREXITFN} if($hash->{HELPER}{USEREXITFN});
  1082. delete $hash->{HELPER}{UEFN_REGEXP} if($hash->{HELPER}{UEFN_REGEXP});
  1083. }
  1084. }
  1085. if ($aName eq "role") {
  1086. if($cmd eq "set") {
  1087. if ($aVal eq "Agent") {
  1088. # check ob bereits ein Agent für die angeschlossene Datenbank existiert -> DbRep-Device kann dann keine Agent-Rolle einnehmen
  1089. foreach(devspec2array("TYPE=DbRep")) {
  1090. my $devname = $_;
  1091. next if($devname eq $name);
  1092. my $devrole = $defs{$_}{ROLE};
  1093. my $devdb = $defs{$_}{DATABASE};
  1094. if ($devrole eq "Agent" && $devdb eq $hash->{DATABASE}) { return "There is already an Agent device: $devname defined for database $hash->{DATABASE} !"; }
  1095. }
  1096. # nicht erlaubte Attribute löschen falls gesetzt
  1097. foreach (@agentnoattr) {
  1098. delete($attr{$name}{$_});
  1099. }
  1100. $attr{$name}{icon} = "security";
  1101. }
  1102. $do = $aVal;
  1103. } else {
  1104. $do = "Client";
  1105. }
  1106. $hash->{ROLE} = $do;
  1107. $hash->{MODEL} = $hash->{ROLE};
  1108. delete($attr{$name}{icon}) if($do eq "Client");
  1109. }
  1110. if ($cmd eq "set") {
  1111. if ($aName =~ /valueFilter/) {
  1112. eval { "Hallo" =~ m/$aVal/ };
  1113. return "Bad regexp: $@" if($@);
  1114. }
  1115. if ($aName =~ /seqDoubletsVariance/) {
  1116. unless (looks_like_number($aVal)) { return " The Value of $aName is not valid. Only figures are allowed !";}
  1117. }
  1118. if ($aName eq "timeYearPeriod") {
  1119. # 06-01 02-28
  1120. unless ($aVal =~ /^(\d{2})-(\d{2}) (\d{2})-(\d{2})$/ )
  1121. { return "The Value of \"$aName\" isn't valid. Set the account period as \"MM-DD MM-DD\".";}
  1122. my ($mm1, $dd1, $mm2, $dd2) = ($aVal =~ /^(\d{2})-(\d{2}) (\d{2})-(\d{2})$/);
  1123. my (undef,undef,undef,$mday,$mon,$year1,undef,undef,undef) = localtime(time); # Istzeit Ableitung
  1124. my $year2 = $year1;
  1125. # a b c d
  1126. # 06-01 02-28 , wenn c < a && $mon < a -> Jahr(a)-1, sonst Jahr(c)+1
  1127. my $c = ($mon+1).$mday;
  1128. my $e = $mm2.$dd2;
  1129. if ($mm2 <= $mm1 && $c <= $e) {
  1130. $year1--;
  1131. } else {
  1132. $year2++;
  1133. }
  1134. eval { my $t1 = timelocal(00, 00, 00, $dd1, $mm1-1, $year1-1900);
  1135. my $t2 = timelocal(00, 00, 00, $dd2, $mm2-1, $year2-1900); };
  1136. if ($@) {
  1137. my @l = split (/at/, $@);
  1138. return " The Value of $aName is out of range - $l[0]";
  1139. }
  1140. delete($attr{$name}{timestamp_begin}) if ($attr{$name}{timestamp_begin});
  1141. delete($attr{$name}{timestamp_end}) if ($attr{$name}{timestamp_end});
  1142. delete($attr{$name}{timeDiffToNow}) if ($attr{$name}{timeDiffToNow});
  1143. delete($attr{$name}{timeOlderThan}) if ($attr{$name}{timeOlderThan});
  1144. return undef;
  1145. }
  1146. if ($aName eq "timestamp_begin" || $aName eq "timestamp_end") {
  1147. my ($a,$b,$c) = split('_',$aVal);
  1148. if ($a =~ /^current$|^previous$/ && $b =~ /^hour$|^day$|^week$|^month$|^year$/ && $c =~ /^begin$|^end$/) {
  1149. delete($attr{$name}{timeDiffToNow}) if ($attr{$name}{timeDiffToNow});
  1150. delete($attr{$name}{timeOlderThan}) if ($attr{$name}{timeOlderThan});
  1151. return undef;
  1152. }
  1153. $aVal = DbRep_formatpicker($aVal);
  1154. unless ($aVal =~ /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/)
  1155. {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\" !";}
  1156. my ($yyyy, $mm, $dd, $hh, $min, $sec) = ($aVal =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/);
  1157. eval { my $epoch_seconds_begin = timelocal($sec, $min, $hh, $dd, $mm-1, $yyyy-1900); };
  1158. if ($@) {
  1159. my @l = split (/at/, $@);
  1160. return " The Value of $aName is out of range - $l[0]";
  1161. }
  1162. delete($attr{$name}{timeDiffToNow}) if ($attr{$name}{timeDiffToNow});
  1163. delete($attr{$name}{timeOlderThan}) if ($attr{$name}{timeOlderThan});
  1164. }
  1165. if ($aName =~ /ftpTimeout|timeout|diffAccept/) {
  1166. unless ($aVal =~ /^[0-9]+$/) { return " The Value of $aName is not valid. Use only figures 0-9 without decimal places !";}
  1167. }
  1168. if ($aName eq "readingNameMap") {
  1169. unless ($aVal =~ m/^[A-Za-z\d_\.-]+$/) { return " Unsupported character in $aName found. Use only A-Z a-z _ . -";}
  1170. }
  1171. if ($aName eq "timeDiffToNow") {
  1172. unless ($aVal =~ /^[0-9]+$/ || $aVal =~ /^\s*[ydhms]:([\d]+)\s*/ && $aVal !~ /.*,.*/ )
  1173. { 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 !";}
  1174. delete($attr{$name}{timestamp_begin}) if ($attr{$name}{timestamp_begin});
  1175. delete($attr{$name}{timestamp_end}) if ($attr{$name}{timestamp_end});
  1176. delete($attr{$name}{timeOlderThan}) if ($attr{$name}{timeOlderThan});
  1177. delete($attr{$name}{timeYearPeriod}) if ($attr{$name}{timeYearPeriod});
  1178. }
  1179. if ($aName eq "timeOlderThan") {
  1180. unless ($aVal =~ /^[0-9]+$/ || $aVal =~ /^\s*[ydhms]:([\d]+)\s*/ && $aVal !~ /.*,.*/ )
  1181. { 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 !";}
  1182. delete($attr{$name}{timestamp_begin}) if ($attr{$name}{timestamp_begin});
  1183. delete($attr{$name}{timestamp_end}) if ($attr{$name}{timestamp_end});
  1184. delete($attr{$name}{timeDiffToNow}) if ($attr{$name}{timeDiffToNow});
  1185. delete($attr{$name}{timeYearPeriod}) if ($attr{$name}{timeYearPeriod});
  1186. }
  1187. if ($aName eq "dumpMemlimit" || $aName eq "dumpSpeed") {
  1188. unless ($aVal =~ /^[0-9]+$/) { return "The Value of $aName is not valid. Use only figures 0-9 without decimal places.";}
  1189. my $dml = AttrVal($name, "dumpMemlimit", 100000);
  1190. my $ds = AttrVal($name, "dumpSpeed", 10000);
  1191. if($aName eq "dumpMemlimit") {
  1192. unless($aVal >= (10 * $ds)) {return "The Value of $aName has to be at least '10 x dumpSpeed' ! ";}
  1193. }
  1194. if($aName eq "dumpSpeed") {
  1195. unless($aVal <= ($dml / 10)) {return "The Value of $aName mustn't be greater than 'dumpMemlimit / 10' ! ";}
  1196. }
  1197. }
  1198. if ($aName eq "ftpUse") {
  1199. delete($attr{$name}{ftpUseSSL});
  1200. }
  1201. if ($aName eq "ftpUseSSL") {
  1202. delete($attr{$name}{ftpUse});
  1203. }
  1204. if ($aName eq "reading" || $aName eq "device") {
  1205. if ($dbmodel && $dbmodel ne 'SQLITE') {
  1206. my $attrname = uc($aName);
  1207. if ($dbmodel eq 'POSTGRESQL' && $aVal !~ m/,/) {
  1208. return "Length of \"$aName\" is too big. Maximum length for database type $dbmodel is $dbrep_col{$attrname}" if(length($aVal) > $dbrep_col{$attrname});
  1209. } elsif ($dbmodel eq 'MYSQL' && $aVal !~ m/,/) {
  1210. return "Length of \"$aName\" is too big. Maximum length for database type $dbmodel is $dbrep_col{$attrname}" if(length($aVal) > $dbrep_col{$attrname});
  1211. }
  1212. }
  1213. }
  1214. }
  1215. return undef;
  1216. }
  1217. ###################################################################################
  1218. # DbRep_Notify Eventverarbeitung
  1219. ###################################################################################
  1220. sub DbRep_Notify($$) {
  1221. # Es werden nur die Events von Geräten verarbeitet die im Hash $hash->{NOTIFYDEV} gelistet sind (wenn definiert).
  1222. # Dadurch kann die Menge der Events verringert werden. In sub DbRep_Define angeben.
  1223. # Beispiele:
  1224. # $hash->{NOTIFYDEV} = "global";
  1225. # $hash->{NOTIFYDEV} = "global,Definition_A,Definition_B";
  1226. my ($own_hash, $dev_hash) = @_;
  1227. my $myName = $own_hash->{NAME}; # Name des eigenen Devices
  1228. my $devName = $dev_hash->{NAME}; # Device welches Events erzeugt hat
  1229. return if(IsDisabled($myName)); # Return if the module is disabled
  1230. my $events = deviceEvents($dev_hash,0);
  1231. return if(!$events);
  1232. foreach my $event (@{$events}) {
  1233. $event = "" if(!defined($event));
  1234. my @evl = split("[ \t][ \t]*", $event);
  1235. # if ($devName = $myName && $evl[0] =~ /done/) {
  1236. # InternalTimer(time+1, "browser_refresh", $own_hash, 0);
  1237. # }
  1238. if ($own_hash->{ROLE} eq "Agent") {
  1239. # wenn Rolle "Agent" Verbeitung von RENAMED Events
  1240. next if ($event !~ /RENAMED/);
  1241. my $strucChanged;
  1242. # altes in neues device in der DEF des angeschlossenen DbLog-device ändern (neues device loggen)
  1243. my $dblog_name = $own_hash->{dbloghash}{NAME}; # Name des an den DbRep-Agenten angeschlossenen DbLog-Dev
  1244. my $dblog_hash = $defs{$dblog_name};
  1245. if ( $dblog_hash->{DEF} =~ m/( |\(|\|)$evl[1]( |\)|\||:)/ ) {
  1246. $dblog_hash->{DEF} =~ s/$evl[1]/$evl[2]/;
  1247. $dblog_hash->{REGEXP} =~ s/$evl[1]/$evl[2]/;
  1248. # Definitionsänderung wurde vorgenommen
  1249. $strucChanged = 1;
  1250. Log3 ($myName, 3, "DbRep Agent $myName - $dblog_name substituted in DEF, old: \"$evl[1]\", new: \"$evl[2]\" ");
  1251. }
  1252. # DEVICE innerhalb angeschlossener Datenbank umbenennen
  1253. Log3 ($myName, 4, "DbRep Agent $myName - Evt RENAMED rec - old device: $evl[1], new device: $evl[2] -> start deviceRename in DB: $own_hash->{DATABASE} ");
  1254. $own_hash->{HELPER}{OLDDEV} = $evl[1];
  1255. $own_hash->{HELPER}{NEWDEV} = $evl[2];
  1256. $own_hash->{HELPER}{RENMODE} = "devren";
  1257. DbRep_Main($own_hash,"deviceRename");
  1258. # die Attribute "device" in allen DbRep-Devices mit der Datenbank = DB des Agenten von alten Device in neues Device ändern
  1259. foreach(devspec2array("TYPE=DbRep")) {
  1260. my $repname = $_;
  1261. next if($_ eq $myName);
  1262. my $repattrdevice = $attr{$_}{device};
  1263. next if(!$repattrdevice);
  1264. my $repdb = $defs{$_}{DATABASE};
  1265. if ($repattrdevice eq $evl[1] && $repdb eq $own_hash->{DATABASE}) {
  1266. $attr{$_}{device} = $evl[2];
  1267. # Definitionsänderung wurde vorgenommen
  1268. $strucChanged = 1;
  1269. Log3 ($myName, 3, "DbRep Agent $myName - $_ attr device changed, old: \"$evl[1]\", new: \"$evl[2]\" ");
  1270. }
  1271. }
  1272. # if ($strucChanged) {CommandSave("","")};
  1273. }
  1274. }
  1275. return;
  1276. }
  1277. ###################################################################################
  1278. # DbRep_Undef
  1279. ###################################################################################
  1280. sub DbRep_Undef($$) {
  1281. my ($hash, $arg) = @_;
  1282. RemoveInternalTimer($hash);
  1283. my $dbh = $hash->{DBH};
  1284. $dbh->disconnect() if(defined($dbh));
  1285. BlockingKill($hash->{HELPER}{RUNNING_PID}) if (exists($hash->{HELPER}{RUNNING_PID}));
  1286. BlockingKill($hash->{HELPER}{RUNNING_BACKUP_CLIENT}) if (exists($hash->{HELPER}{RUNNING_BACKUP_CLIENT}));
  1287. BlockingKill($hash->{HELPER}{RUNNING_RESTORE}) if (exists($hash->{HELPER}{RUNNING_RESTORE}));
  1288. BlockingKill($hash->{HELPER}{RUNNING_BCKPREST_SERVER}) if (exists($hash->{HELPER}{RUNNING_BCKPREST_SERVER}));
  1289. BlockingKill($hash->{HELPER}{RUNNING_OPTIMIZE}) if (exists($hash->{HELPER}{RUNNING_OPTIMIZE}));
  1290. BlockingKill($hash->{HELPER}{RUNNING_REPAIR}) if (exists($hash->{HELPER}{RUNNING_REPAIR}));
  1291. DbRep_delread($hash,1);
  1292. return undef;
  1293. }
  1294. ###################################################################################
  1295. # DbRep_Shutdown
  1296. ###################################################################################
  1297. sub DbRep_Shutdown($) {
  1298. my ($hash) = @_;
  1299. my $dbh = $hash->{DBH};
  1300. $dbh->disconnect() if(defined($dbh));
  1301. DbRep_delread($hash,1);
  1302. RemoveInternalTimer($hash);
  1303. return undef;
  1304. }
  1305. ###################################################################################
  1306. # First Init DB Connect
  1307. # Verbindung zur DB aufbauen und den Timestamp des ältesten
  1308. # Datensatzes ermitteln
  1309. ###################################################################################
  1310. sub DbRep_firstconnect($) {
  1311. my ($hash) = @_;
  1312. my $name = $hash->{NAME};
  1313. my $to = "120";
  1314. my $dbloghash = $hash->{dbloghash};
  1315. my $dbconn = $dbloghash->{dbconn};
  1316. my $dbuser = $dbloghash->{dbuser};
  1317. RemoveInternalTimer($hash, "DbRep_firstconnect");
  1318. return if(IsDisabled($name));
  1319. if ($init_done == 1) {
  1320. Log3 ($name, 3, "DbRep $name - Connectiontest to database $dbconn with user $dbuser") if($hash->{LASTCMD} ne "minTimestamp");
  1321. $hash->{HELPER}{RUNNING_PID} = BlockingCall("DbRep_getMinTs", "$name", "DbRep_getMinTsDone", $to, "DbRep_getMinTsAborted", $hash);
  1322. $hash->{HELPER}{RUNNING_PID}{loglevel} = 5 if($hash->{HELPER}{RUNNING_PID}); # Forum #77057
  1323. } else {
  1324. InternalTimer(time+1, "DbRep_firstconnect", $hash, 0);
  1325. }
  1326. return;
  1327. }
  1328. ####################################################################################################
  1329. # den ältesten Datensatz (Timestamp) in der DB bestimmen
  1330. ####################################################################################################
  1331. sub DbRep_getMinTs($) {
  1332. my ($name) = @_;
  1333. my $hash = $defs{$name};
  1334. my $dbloghash = $hash->{dbloghash};
  1335. my $dbconn = $dbloghash->{dbconn};
  1336. my $dbuser = $dbloghash->{dbuser};
  1337. my $dblogname = $dbloghash->{NAME};
  1338. my $dbpassword = $attr{"sec$dblogname"}{secret};
  1339. my $mintsdef = "1970-01-01 01:00:00";
  1340. my ($dbh,$sql,$err,$mints);
  1341. # Background-Startzeit
  1342. my $bst = [gettimeofday];
  1343. eval { $dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1 }); };
  1344. if ($@) {
  1345. $err = encode_base64($@,"");
  1346. Log3 ($name, 2, "DbRep $name - $@");
  1347. return "$name|''|''|$err";
  1348. }
  1349. # SQL-Startzeit
  1350. my $st = [gettimeofday];
  1351. eval { $mints = $dbh->selectrow_array("SELECT min(TIMESTAMP) FROM history;"); };
  1352. # eval { $mints = $dbh->selectrow_array("select TIMESTAMP from history limit 1;"); };
  1353. # eval { $mints = $dbh->selectrow_array("select TIMESTAMP from history order by TIMESTAMP limit 1;"); };
  1354. $dbh->disconnect;
  1355. # SQL-Laufzeit ermitteln
  1356. my $rt = tv_interval($st);
  1357. $mints = $mints?encode_base64($mints,""):encode_base64($mintsdef,"");
  1358. # Background-Laufzeit ermitteln
  1359. my $brt = tv_interval($bst);
  1360. $rt = $rt.",".$brt;
  1361. return "$name|$mints|$rt|0";
  1362. }
  1363. ####################################################################################################
  1364. # Auswertungsroutine den ältesten Datensatz (Timestamp) in der DB bestimmen
  1365. ####################################################################################################
  1366. sub DbRep_getMinTsDone($) {
  1367. my ($string) = @_;
  1368. my @a = split("\\|",$string);
  1369. my $hash = $defs{$a[0]};
  1370. my $name = $hash->{NAME};
  1371. my $mints = decode_base64($a[1]);
  1372. my $bt = $a[2];
  1373. my ($rt,$brt) = split(",", $bt);
  1374. my $err = $a[3]?decode_base64($a[3]):undef;
  1375. my $dblogdevice = $hash->{HELPER}{DBLOGDEVICE};
  1376. $hash->{dbloghash} = $defs{$dblogdevice};
  1377. my $dbconn = $hash->{dbloghash}{dbconn};
  1378. if ($err) {
  1379. readingsBeginUpdate($hash);
  1380. ReadingsBulkUpdateValue ($hash, "errortext", $err);
  1381. ReadingsBulkUpdateValue ($hash, "state", "disconnected");
  1382. readingsEndUpdate($hash, 1);
  1383. delete($hash->{HELPER}{RUNNING_PID});
  1384. Log3 ($name, 2, "DbRep $name - DB connect failed. Make sure credentials of database $hash->{DATABASE} are valid and database is reachable.");
  1385. return;
  1386. }
  1387. my $state = ($hash->{LASTCMD} eq "minTimestamp")?"done":"connected";
  1388. $state = "invalid timestamp \"$mints\" found in database - please delete it" if($mints =~ /^0000-00-00.*$/);
  1389. readingsBeginUpdate($hash);
  1390. ReadingsBulkUpdateValue ($hash, "timestamp_oldest_dataset", $mints) if($hash->{LASTCMD} eq "minTimestamp");
  1391. ReadingsBulkUpdateTimeState($hash,$brt,$rt,$state);
  1392. readingsEndUpdate($hash, 1);
  1393. Log3 ($name, 4, "DbRep $name - Connectiontest to db $dbconn successful") if($hash->{LASTCMD} ne "minTimestamp");
  1394. $hash->{HELPER}{MINTS} = $mints;
  1395. delete($hash->{HELPER}{RUNNING_PID});
  1396. return;
  1397. }
  1398. ####################################################################################################
  1399. # Abbruchroutine den ältesten Datensatz (Timestamp) in der DB bestimmen
  1400. ####################################################################################################
  1401. sub DbRep_getMinTsAborted(@) {
  1402. my ($hash,$cause) = @_;
  1403. my $name = $hash->{NAME};
  1404. $cause = $cause?$cause:"Timeout: process terminated";
  1405. Log3 ($name, 1, "DbRep $name -> BlockingCall $hash->{HELPER}{RUNNING_PID}{fn} pid:$hash->{HELPER}{RUNNING_PID}{pid} $cause");
  1406. readingsBeginUpdate($hash);
  1407. ReadingsBulkUpdateValue ($hash, "errortext", $cause);
  1408. ReadingsBulkUpdateValue ($hash, "state", "disconnected");
  1409. readingsEndUpdate($hash, 1);
  1410. delete($hash->{HELPER}{RUNNING_PID});
  1411. return;
  1412. }
  1413. ################################################################################################################
  1414. # Hauptroutine
  1415. ################################################################################################################
  1416. sub DbRep_Main($$;$) {
  1417. my ($hash,$opt,$prop) = @_;
  1418. my $name = $hash->{NAME};
  1419. my $to = AttrVal($name, "timeout", "86400");
  1420. my $reading = AttrVal($name, "reading", "%");
  1421. my $device = AttrVal($name, "device", "%");
  1422. my $dbloghash = $hash->{dbloghash};
  1423. my $dbmodel = $dbloghash->{MODEL};
  1424. # Entkommentieren für Testroutine im Vordergrund
  1425. # testexit($hash);
  1426. return if( ($hash->{HELPER}{RUNNING_BACKUP_CLIENT} ||
  1427. $hash->{HELPER}{RUNNING_BCKPREST_SERVER} ||
  1428. $hash->{HELPER}{RUNNING_RESTORE} ||
  1429. $hash->{HELPER}{RUNNING_REPAIR} ||
  1430. $hash->{HELPER}{RUNNING_OPTIMIZE}) &&
  1431. $opt !~ /dumpMySQL|restoreMySQL|dumpSQLite|restoreSQLite|optimizeTables|vacuum|repairSQLite/ );
  1432. # Readings löschen die nicht in der Ausnahmeliste (Attr readingPreventFromDel) stehen
  1433. DbRep_delread($hash);
  1434. if ($opt =~ /dumpMySQL|dumpSQLite/) {
  1435. BlockingKill($hash->{HELPER}{RUNNING_BACKUP_CLIENT}) if (exists($hash->{HELPER}{RUNNING_BACKUP_CLIENT}));
  1436. BlockingKill($hash->{HELPER}{RUNNING_BCKPREST_SERVER}) if (exists($hash->{HELPER}{RUNNING_BCKPREST_SERVER}));
  1437. BlockingKill($hash->{HELPER}{RUNNING_OPTIMIZE}) if (exists($hash->{HELPER}{RUNNING_OPTIMIZE}));
  1438. if($dbmodel =~ /MYSQL/) {
  1439. if ($prop eq "serverSide") {
  1440. $hash->{HELPER}{RUNNING_BCKPREST_SERVER} = BlockingCall("mysql_DoDumpServerSide", "$name", "DbRep_DumpDone", $to, "DbRep_DumpAborted", $hash);
  1441. ReadingsSingleUpdateValue ($hash, "state", "serverSide Dump is running - be patient and see Logfile !", 1);
  1442. } else {
  1443. $hash->{HELPER}{RUNNING_BACKUP_CLIENT} = BlockingCall("mysql_DoDumpClientSide", "$name", "DbRep_DumpDone", $to, "DbRep_DumpAborted", $hash);
  1444. ReadingsSingleUpdateValue ($hash, "state", "clientSide Dump is running - be patient and see Logfile !", 1);
  1445. }
  1446. }
  1447. if($dbmodel =~ /SQLITE/) {
  1448. $hash->{HELPER}{RUNNING_BACKUP_CLIENT} = BlockingCall("DbRep_sqliteDoDump", "$name", "DbRep_DumpDone", $to, "DbRep_DumpAborted", $hash);
  1449. ReadingsSingleUpdateValue ($hash, "state", "SQLite Dump is running - be patient and see Logfile !", 1);
  1450. }
  1451. return;
  1452. }
  1453. if ($opt =~ /restoreMySQL/) {
  1454. BlockingKill($hash->{HELPER}{RUNNING_RESTORE}) if (exists($hash->{HELPER}{RUNNING_RESTORE}));
  1455. BlockingKill($hash->{HELPER}{RUNNING_OPTIMIZE}) if (exists($hash->{HELPER}{RUNNING_OPTIMIZE}));
  1456. if($prop =~ /csv/) {
  1457. $hash->{HELPER}{RUNNING_RESTORE} = BlockingCall("mysql_RestoreServerSide", "$name|$prop", "DbRep_restoreDone", $to, "DbRep_restoreAborted", $hash);
  1458. } elsif ($prop =~ /sql/) {
  1459. $hash->{HELPER}{RUNNING_RESTORE} = BlockingCall("mysql_RestoreClientSide", "$name|$prop", "DbRep_restoreDone", $to, "DbRep_restoreAborted", $hash);
  1460. } else {
  1461. ReadingsSingleUpdateValue ($hash, "state", "restore database error - unknown fileextension \"$prop\"", 1);
  1462. }
  1463. ReadingsSingleUpdateValue ($hash, "state", "restore database is running - be patient and see Logfile !", 1);
  1464. return;
  1465. }
  1466. if ($opt =~ /restoreSQLite/) {
  1467. BlockingKill($hash->{HELPER}{RUNNING_RESTORE}) if (exists($hash->{HELPER}{RUNNING_RESTORE}));
  1468. BlockingKill($hash->{HELPER}{RUNNING_OPTIMIZE}) if (exists($hash->{HELPER}{RUNNING_OPTIMIZE}));
  1469. $hash->{HELPER}{RUNNING_RESTORE} = BlockingCall("DbRep_sqliteRestore", "$name|$prop", "DbRep_restoreDone", $to, "DbRep_restoreAborted", $hash);
  1470. ReadingsSingleUpdateValue ($hash, "state", "restore database is running - be patient and see Logfile !", 1);
  1471. return;
  1472. }
  1473. if ($opt =~ /optimizeTables|vacuum/) {
  1474. BlockingKill($hash->{HELPER}{RUNNING_OPTIMIZE}) if (exists($hash->{HELPER}{RUNNING_OPTIMIZE}));
  1475. BlockingKill($hash->{HELPER}{RUNNING_RESTORE}) if (exists($hash->{HELPER}{RUNNING_RESTORE}));
  1476. $hash->{HELPER}{RUNNING_OPTIMIZE} = BlockingCall("DbRep_optimizeTables", "$name", "DbRep_OptimizeDone", $to, "DbRep_OptimizeAborted", $hash);
  1477. ReadingsSingleUpdateValue ($hash, "state", "optimize tables is running - be patient and see Logfile !", 1);
  1478. return;
  1479. }
  1480. if ($opt =~ /repairSQLite/) {
  1481. BlockingKill($hash->{HELPER}{RUNNING_BACKUP_CLIENT}) if (exists($hash->{HELPER}{RUNNING_BACKUP_CLIENT}));
  1482. BlockingKill($hash->{HELPER}{RUNNING_OPTIMIZE}) if (exists($hash->{HELPER}{RUNNING_OPTIMIZE}));
  1483. BlockingKill($hash->{HELPER}{RUNNING_REPAIR}) if (exists($hash->{HELPER}{RUNNING_REPAIR}));
  1484. $hash->{HELPER}{RUNNING_REPAIR} = BlockingCall("DbRep_sqliteRepair", "$name", "DbRep_RepairDone", $to, "DbRep_RepairAborted", $hash);
  1485. ReadingsSingleUpdateValue ($hash, "state", "repair database is running - be patient and see Logfile !", 1);
  1486. return;
  1487. }
  1488. if (exists($hash->{HELPER}{RUNNING_PID}) && $hash->{ROLE} ne "Agent") {
  1489. Log3 ($name, 3, "DbRep $name - WARNING - old process $hash->{HELPER}{RUNNING_PID}{pid} will be killed now to start a new BlockingCall");
  1490. BlockingKill($hash->{HELPER}{RUNNING_PID});
  1491. }
  1492. ReadingsSingleUpdateValue ($hash, "state", "running", 1);
  1493. # only for this block because of warnings if details of readings are not set
  1494. no warnings 'uninitialized';
  1495. # Ausgaben und Zeitmanipulationen
  1496. Log3 ($name, 4, "DbRep $name - -------- New selection --------- ");
  1497. Log3 ($name, 4, "DbRep $name - Command: $opt $prop");
  1498. # zentrales Timestamp-Array und Zeitgrenzen bereitstellen
  1499. my ($epoch_seconds_begin,$epoch_seconds_end,$runtime_string_first,$runtime_string_next);
  1500. my $ts = "no_aggregation"; # Dummy für eine Select-Schleife wenn != $IsTimeSet || $IsAggrSet
  1501. my ($IsTimeSet,$IsAggrSet,$aggregation) = DbRep_checktimeaggr($hash);
  1502. if($IsTimeSet || $IsAggrSet) {
  1503. ($epoch_seconds_begin,$epoch_seconds_end,$runtime_string_first,$runtime_string_next,$ts) = DbRep_createTimeArray($hash,$aggregation,$opt);
  1504. } else {
  1505. Log3 ($name, 4, "DbRep $name - Timestamp begin human readable: not set") if($opt !~ /tableCurrentPurge/);
  1506. Log3 ($name, 4, "DbRep $name - Timestamp end human readable: not set") if($opt !~ /tableCurrentPurge/);
  1507. }
  1508. Log3 ($name, 4, "DbRep $name - Aggregation: $aggregation") if($opt !~ /tableCurrentPurge|tableCurrentFillup|fetchrows|insert/);
  1509. ##### Funktionsaufrufe #####
  1510. if ($opt eq "sumValue") {
  1511. $hash->{HELPER}{RUNNING_PID} = BlockingCall("sumval_DoParse", "$name§$device§$reading§$prop§$ts", "sumval_ParseDone", $to, "DbRep_ParseAborted", $hash);
  1512. } elsif ($opt =~ m/countEntries/) {
  1513. my $table = $prop;
  1514. $hash->{HELPER}{RUNNING_PID} = BlockingCall("count_DoParse", "$name§$table§$device§$reading§$ts", "count_ParseDone", $to, "DbRep_ParseAborted", $hash);
  1515. } elsif ($opt eq "averageValue") {
  1516. Log3 ($name, 4, "DbRep $name - averageValue calculation sceme: ".AttrVal($name,"averageCalcForm","avgArithmeticMean"));
  1517. $hash->{HELPER}{RUNNING_PID} = BlockingCall("averval_DoParse", "$name§$device§$reading§$prop§$ts", "averval_ParseDone", $to, "DbRep_ParseAborted", $hash);
  1518. } elsif ($opt eq "fetchrows") {
  1519. my $table = $prop;
  1520. $hash->{HELPER}{RUNNING_PID} = BlockingCall("fetchrows_DoParse", "$name|$table|$device|$reading|$runtime_string_first|$runtime_string_next", "fetchrows_ParseDone", $to, "DbRep_ParseAborted", $hash);
  1521. } elsif ($opt =~ /delSeqDoublets/) {
  1522. my $cmd = $prop?$prop:"adviceRemain";
  1523. $hash->{HELPER}{RUNNING_PID} = BlockingCall("delseqdoubl_DoParse", "$name§$cmd§$device§$reading§$ts", "delseqdoubl_ParseDone", $to, "DbRep_ParseAborted", $hash);
  1524. } elsif ($opt eq "exportToFile") {
  1525. my $file = $prop;
  1526. DbRep_beforeproc($hash, "export");
  1527. $hash->{HELPER}{RUNNING_PID} = BlockingCall("expfile_DoParse", "$name§$device§$reading§$runtime_string_first§$file§$ts", "expfile_ParseDone", $to, "DbRep_ParseAborted", $hash);
  1528. } elsif ($opt eq "importFromFile") {
  1529. my $file = $prop;
  1530. DbRep_beforeproc($hash, "import");
  1531. $hash->{HELPER}{RUNNING_PID} = BlockingCall("impfile_Push", "$name|$runtime_string_first|$file", "impfile_PushDone", $to, "DbRep_ParseAborted", $hash);
  1532. } elsif ($opt eq "maxValue") {
  1533. $hash->{HELPER}{RUNNING_PID} = BlockingCall("maxval_DoParse", "$name§$device§$reading§$prop§$ts", "maxval_ParseDone", $to, "DbRep_ParseAborted", $hash);
  1534. } elsif ($opt eq "minValue") {
  1535. $hash->{HELPER}{RUNNING_PID} = BlockingCall("minval_DoParse", "$name§$device§$reading§$prop§$ts", "minval_ParseDone", $to, "DbRep_ParseAborted", $hash);
  1536. } elsif ($opt eq "delEntries") {
  1537. $hash->{HELPER}{RUNNING_PID} = BlockingCall("del_DoParse", "$name|history|$device|$reading|$runtime_string_first|$runtime_string_next", "del_ParseDone", $to, "DbRep_ParseAborted", $hash);
  1538. } elsif ($opt eq "tableCurrentPurge") {
  1539. undef $runtime_string_first;
  1540. undef $runtime_string_next;
  1541. $hash->{HELPER}{RUNNING_PID} = BlockingCall("del_DoParse", "$name|current|$device|$reading|$runtime_string_first|$runtime_string_next", "del_ParseDone", $to, "DbRep_ParseAborted", $hash);
  1542. } elsif ($opt eq "tableCurrentFillup") {
  1543. $hash->{HELPER}{RUNNING_PID} = BlockingCall("currentfillup_Push", "$name|$device|$reading|$runtime_string_first|$runtime_string_next", "currentfillup_Done", $to, "DbRep_ParseAborted", $hash);
  1544. } elsif ($opt eq "diffValue") {
  1545. $hash->{HELPER}{RUNNING_PID} = BlockingCall("diffval_DoParse", "$name§$device§$reading§$prop§$ts", "diffval_ParseDone", $to, "DbRep_ParseAborted", $hash);
  1546. } elsif ($opt eq "insert") {
  1547. $hash->{HELPER}{RUNNING_PID} = BlockingCall("insert_Push", "$name", "insert_Done", $to, "DbRep_ParseAborted", $hash);
  1548. } elsif ($opt =~ /deviceRename|readingRename/) {
  1549. $hash->{HELPER}{RUNNING_PID} = BlockingCall("change_Push", "$name|$device|$reading|$runtime_string_first|$runtime_string_next", "change_Done", $to, "DbRep_ParseAborted", $hash);
  1550. } elsif ($opt =~ /changeValue/) {
  1551. $hash->{HELPER}{RUNNING_PID} = BlockingCall("changeval_Push", "$name§$device§$reading§$runtime_string_first§$runtime_string_next§$ts", "change_Done", $to, "DbRep_ParseAborted", $hash);
  1552. } elsif ($opt =~ /sqlCmd|sqlSpecial/ ) {
  1553. # Execute a generic sql command or special sql
  1554. if ($opt =~ /sqlSpecial/) {
  1555. if($prop eq "50mostFreqLogsLast2days") {
  1556. $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/);
  1557. $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/);
  1558. $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/);
  1559. } elsif ($prop eq "allDevReadCount") {
  1560. $prop = "select device, reading, count(*) from history group by DEVICE, READING;";
  1561. } elsif ($prop eq "allDevCount") {
  1562. $prop = "select device, count(*) from history group by DEVICE;";
  1563. }
  1564. }
  1565. $hash->{HELPER}{RUNNING_PID} = BlockingCall("sqlCmd_DoParse", "$name|$opt|$runtime_string_first|$runtime_string_next|$prop", "sqlCmd_ParseDone", $to, "DbRep_ParseAborted", $hash);
  1566. } elsif ($opt =~ /syncStandby/ ) {
  1567. # Befehl vor Procedure ausführen
  1568. DbRep_beforeproc($hash, "syncStandby");
  1569. $hash->{HELPER}{RUNNING_PID} = BlockingCall("DbRep_syncStandby", "$name§$device§$reading§$runtime_string_first§$runtime_string_next§$ts§$prop", "DbRep_syncStandbyDone", $to, "DbRep_ParseAborted", $hash);
  1570. }
  1571. $hash->{HELPER}{RUNNING_PID}{loglevel} = 5 if($hash->{HELPER}{RUNNING_PID}); # Forum #77057
  1572. return;
  1573. }
  1574. ################################################################################################################
  1575. # Create zentrales Timsstamp-Array
  1576. ################################################################################################################
  1577. sub DbRep_createTimeArray($$$) {
  1578. my ($hash,$aggregation,$opt) = @_;
  1579. my $name = $hash->{NAME};
  1580. # year als Jahre seit 1900
  1581. # $mon als 0..11
  1582. # $time = timelocal( $sec, $min, $hour, $mday, $mon, $year );
  1583. my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); # Istzeit Ableitung
  1584. my ($tsbegin,$tsend,$dim,$tsub,$tadd);
  1585. my ($rsec,$rmin,$rhour,$rmday,$rmon,$ryear);
  1586. # absolute Auswertungszeiträume statische und dynamische (Beginn / Ende) berechnen
  1587. if($hash->{HELPER}{MINTS} && $hash->{HELPER}{MINTS} =~ m/0000-00-00/) {
  1588. Log3 ($name, 1, "DbRep $name - ERROR - wrong timestamp \"$hash->{HELPER}{MINTS}\" found in database. Please delete it !");
  1589. delete $hash->{HELPER}{MINTS};
  1590. }
  1591. my $mints = $hash->{HELPER}{MINTS}?$hash->{HELPER}{MINTS}:"1970-01-01 01:00:00"; # Timestamp des 1. Datensatzes verwenden falls ermittelt
  1592. $tsbegin = AttrVal($name, "timestamp_begin", $mints);
  1593. $tsbegin = DbRep_formatpicker($tsbegin);
  1594. $tsend = AttrVal($name, "timestamp_end", strftime "%Y-%m-%d %H:%M:%S", localtime(time));
  1595. $tsend = DbRep_formatpicker($tsend);
  1596. if ( my $tap = AttrVal($name, "timeYearPeriod", undef)) {
  1597. # a b c d
  1598. # 06-01 02-28 , wenn c < a && $mon < a -> Jahr(a)-1, sonst Jahr(c)+1
  1599. my $ybp = $year+1900;
  1600. my $yep = $year+1900;
  1601. $tap =~ qr/^(\d{2})-(\d{2}) (\d{2})-(\d{2})$/p;
  1602. my $mbp = $1;
  1603. my $dbp = $2;
  1604. my $mep = $3;
  1605. my $dep = $4;
  1606. my $c = ($mon+1).$mday;
  1607. my $e = $mep.$dep;
  1608. if ($mep <= $mbp && $c <= $e) {
  1609. $ybp--;
  1610. } else {
  1611. $yep++;
  1612. }
  1613. $tsbegin = "$ybp-$mbp-$dbp 00:00:00";
  1614. $tsend = "$yep-$mep-$dep 23:59:59";
  1615. }
  1616. if (AttrVal($name,"timestamp_begin","") eq "current_year_begin" ||
  1617. AttrVal($name,"timestamp_end","") eq "current_year_begin") {
  1618. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,0,1,0,$year)) if(AttrVal($name,"timestamp_begin","") eq "current_year_begin");
  1619. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,0,1,0,$year)) if(AttrVal($name,"timestamp_end","") eq "current_year_begin");
  1620. }
  1621. if (AttrVal($name, "timestamp_begin", "") eq "current_year_end" ||
  1622. AttrVal($name, "timestamp_end", "") eq "current_year_end") {
  1623. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,23,31,11,$year)) if(AttrVal($name,"timestamp_begin","") eq "current_year_end");
  1624. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,23,31,11,$year)) if(AttrVal($name,"timestamp_end","") eq "current_year_end");
  1625. }
  1626. if (AttrVal($name, "timestamp_begin", "") eq "previous_year_begin" ||
  1627. AttrVal($name, "timestamp_end", "") eq "previous_year_begin") {
  1628. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,0,1,0,$year-1)) if(AttrVal($name, "timestamp_begin", "") eq "previous_year_begin");
  1629. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,0,1,0,$year-1)) if(AttrVal($name, "timestamp_end", "") eq "previous_year_begin");
  1630. }
  1631. if (AttrVal($name, "timestamp_begin", "") eq "previous_year_end" ||
  1632. AttrVal($name, "timestamp_end", "") eq "previous_year_end") {
  1633. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,23,31,11,$year-1)) if(AttrVal($name, "timestamp_begin", "") eq "previous_year_end");
  1634. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,23,31,11,$year-1)) if(AttrVal($name, "timestamp_end", "") eq "previous_year_end");
  1635. }
  1636. if (AttrVal($name, "timestamp_begin", "") eq "current_month_begin" ||
  1637. AttrVal($name, "timestamp_end", "") eq "current_month_begin") {
  1638. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,0,1,$mon,$year)) if(AttrVal($name, "timestamp_begin", "") eq "current_month_begin");
  1639. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,0,1,$mon,$year)) if(AttrVal($name, "timestamp_end", "") eq "current_month_begin");
  1640. }
  1641. if (AttrVal($name, "timestamp_begin", "") eq "current_month_end" ||
  1642. AttrVal($name, "timestamp_end", "") eq "current_month_end") {
  1643. $dim = $mon-1?30+(($mon+1)*3%7<4):28+!($year%4||$year%400*!($year%100));
  1644. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,23,$dim,$mon,$year)) if(AttrVal($name, "timestamp_begin", "") eq "current_month_end");
  1645. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,23,$dim,$mon,$year)) if(AttrVal($name, "timestamp_end", "") eq "current_month_end");
  1646. }
  1647. if (AttrVal($name, "timestamp_begin", "") eq "previous_month_begin" ||
  1648. AttrVal($name, "timestamp_end", "") eq "previous_month_begin") {
  1649. $ryear = ($mon-1<0)?$year-1:$year;
  1650. $rmon = ($mon-1<0)?11:$mon-1;
  1651. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,0,1,$rmon,$ryear)) if(AttrVal($name, "timestamp_begin", "") eq "previous_month_begin");
  1652. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,0,1,$rmon,$ryear)) if(AttrVal($name, "timestamp_end", "") eq "previous_month_begin");
  1653. }
  1654. if (AttrVal($name, "timestamp_begin", "") eq "previous_month_end" ||
  1655. AttrVal($name, "timestamp_end", "") eq "previous_month_end") {
  1656. $ryear = ($mon-1<0)?$year-1:$year;
  1657. $rmon = ($mon-1<0)?11:$mon-1;
  1658. $dim = $rmon-1?30+(($rmon+1)*3%7<4):28+!($ryear%4||$ryear%400*!($ryear%100));
  1659. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,23,$dim,$rmon,$ryear)) if(AttrVal($name, "timestamp_begin", "") eq "previous_month_end");
  1660. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,23,$dim,$rmon,$ryear)) if(AttrVal($name, "timestamp_end", "") eq "previous_month_end");
  1661. }
  1662. if (AttrVal($name, "timestamp_begin", "") eq "current_week_begin" ||
  1663. AttrVal($name, "timestamp_end", "") eq "current_week_begin") {
  1664. $tsub = 0 if($wday == 1); # wenn Start am "Mo" keine Korrektur
  1665. $tsub = 86400 if($wday == 2); # wenn Start am "Di" dann Korrektur -1 Tage
  1666. $tsub = 172800 if($wday == 3); # wenn Start am "Mi" dann Korrektur -2 Tage
  1667. $tsub = 259200 if($wday == 4); # wenn Start am "Do" dann Korrektur -3 Tage
  1668. $tsub = 345600 if($wday == 5); # wenn Start am "Fr" dann Korrektur -4 Tage
  1669. $tsub = 432000 if($wday == 6); # wenn Start am "Sa" dann Korrektur -5 Tage
  1670. $tsub = 518400 if($wday == 0); # wenn Start am "So" dann Korrektur -6 Tage
  1671. ($rsec,$rmin,$rhour,$rmday,$rmon,$ryear) = localtime(time-$tsub);
  1672. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,0,$rmday,$rmon,$ryear)) if(AttrVal($name, "timestamp_begin", "") eq "current_week_begin");
  1673. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,0,$rmday,$rmon,$ryear)) if(AttrVal($name, "timestamp_end", "") eq "current_week_begin");
  1674. }
  1675. if (AttrVal($name, "timestamp_begin", "") eq "current_week_end" ||
  1676. AttrVal($name, "timestamp_end", "") eq "current_week_end") {
  1677. $tadd = 518400 if($wday == 1); # wenn Start am "Mo" dann Korrektur +6 Tage
  1678. $tadd = 432000 if($wday == 2); # wenn Start am "Di" dann Korrektur +5 Tage
  1679. $tadd = 345600 if($wday == 3); # wenn Start am "Mi" dann Korrektur +4 Tage
  1680. $tadd = 259200 if($wday == 4); # wenn Start am "Do" dann Korrektur +3 Tage
  1681. $tadd = 172800 if($wday == 5); # wenn Start am "Fr" dann Korrektur +2 Tage
  1682. $tadd = 86400 if($wday == 6); # wenn Start am "Sa" dann Korrektur +1 Tage
  1683. $tadd = 0 if($wday == 0); # wenn Start am "So" keine Korrektur
  1684. ($rsec,$rmin,$rhour,$rmday,$rmon,$ryear) = localtime(time+$tadd);
  1685. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,23,$rmday,$rmon,$ryear)) if(AttrVal($name, "timestamp_begin", "") eq "current_week_end");
  1686. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,23,$rmday,$rmon,$ryear)) if(AttrVal($name, "timestamp_end", "") eq "current_week_end");
  1687. }
  1688. if (AttrVal($name, "timestamp_begin", "") eq "previous_week_begin" ||
  1689. AttrVal($name, "timestamp_end", "") eq "previous_week_begin") {
  1690. $tsub = 604800 if($wday == 1); # wenn Start am "Mo" dann Korrektur -7 Tage
  1691. $tsub = 691200 if($wday == 2); # wenn Start am "Di" dann Korrektur -8 Tage
  1692. $tsub = 777600 if($wday == 3); # wenn Start am "Mi" dann Korrektur -9 Tage
  1693. $tsub = 864000 if($wday == 4); # wenn Start am "Do" dann Korrektur -10 Tage
  1694. $tsub = 950400 if($wday == 5); # wenn Start am "Fr" dann Korrektur -11 Tage
  1695. $tsub = 1036800 if($wday == 6); # wenn Start am "Sa" dann Korrektur -12 Tage
  1696. $tsub = 1123200 if($wday == 0); # wenn Start am "So" dann Korrektur -13 Tage
  1697. ($rsec,$rmin,$rhour,$rmday,$rmon,$ryear) = localtime(time-$tsub);
  1698. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,0,$rmday,$rmon,$ryear)) if(AttrVal($name, "timestamp_begin", "") eq "previous_week_begin");
  1699. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,0,$rmday,$rmon,$ryear)) if(AttrVal($name, "timestamp_end", "") eq "previous_week_begin");
  1700. }
  1701. if (AttrVal($name, "timestamp_begin", "") eq "previous_week_end" ||
  1702. AttrVal($name, "timestamp_end", "") eq "previous_week_end") {
  1703. $tsub = 86400 if($wday == 1); # wenn Start am "Mo" dann Korrektur -1 Tage
  1704. $tsub = 172800 if($wday == 2); # wenn Start am "Di" dann Korrektur -2 Tage
  1705. $tsub = 259200 if($wday == 3); # wenn Start am "Mi" dann Korrektur -3 Tage
  1706. $tsub = 345600 if($wday == 4); # wenn Start am "Do" dann Korrektur -4 Tage
  1707. $tsub = 432000 if($wday == 5); # wenn Start am "Fr" dann Korrektur -5 Tage
  1708. $tsub = 518400 if($wday == 6); # wenn Start am "Sa" dann Korrektur -6 Tage
  1709. $tsub = 604800 if($wday == 0); # wenn Start am "So" dann Korrektur -7 Tage
  1710. ($rsec,$rmin,$rhour,$rmday,$rmon,$ryear) = localtime(time-$tsub);
  1711. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,23,$rmday,$rmon,$ryear)) if(AttrVal($name, "timestamp_begin", "") eq "previous_week_end");
  1712. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,23,$rmday,$rmon,$ryear)) if(AttrVal($name, "timestamp_end", "") eq "previous_week_end");
  1713. }
  1714. if (AttrVal($name, "timestamp_begin", "") eq "current_day_begin" ||
  1715. AttrVal($name, "timestamp_end", "") eq "current_day_begin") {
  1716. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,0,$mday,$mon,$year)) if(AttrVal($name, "timestamp_begin", "") eq "current_day_begin");
  1717. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,0,$mday,$mon,$year)) if(AttrVal($name, "timestamp_end", "") eq "current_day_begin");
  1718. }
  1719. if (AttrVal($name, "timestamp_begin", "") eq "current_day_end" ||
  1720. AttrVal($name, "timestamp_end", "") eq "current_day_end") {
  1721. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,23,$mday,$mon,$year)) if(AttrVal($name, "timestamp_begin", "") eq "current_day_end");
  1722. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,23,$mday,$mon,$year)) if(AttrVal($name, "timestamp_end", "") eq "current_day_end");
  1723. }
  1724. if (AttrVal($name, "timestamp_begin", "") eq "previous_day_begin" ||
  1725. AttrVal($name, "timestamp_end", "") eq "previous_day_begin") {
  1726. $rmday = $mday-1;
  1727. $rmon = $mon;
  1728. $ryear = $year;
  1729. if($rmday<1) {
  1730. $rmon--;
  1731. if ($rmon<0) {
  1732. $rmon=11;
  1733. $ryear--;
  1734. }
  1735. $rmday = $rmon-1?30+(($rmon+1)*3%7<4):28+!($ryear%4||$ryear%400*!($ryear%100)); # Achtung: Monat als 1...12 (statt 0...11)
  1736. }
  1737. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,0,$rmday,$rmon,$ryear)) if(AttrVal($name, "timestamp_begin", "") eq "previous_day_begin");
  1738. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,0,$rmday,$rmon,$ryear)) if(AttrVal($name, "timestamp_end", "") eq "previous_day_begin");
  1739. }
  1740. if (AttrVal($name, "timestamp_begin", "") eq "previous_day_end" ||
  1741. AttrVal($name, "timestamp_end", "") eq "previous_day_end") {
  1742. $rmday = $mday-1;
  1743. $rmon = $mon;
  1744. $ryear = $year;
  1745. if($rmday<1) {
  1746. $rmon--;
  1747. if ($rmon<0) {
  1748. $rmon=11;
  1749. $ryear--;
  1750. }
  1751. $rmday = $rmon-1?30+(($rmon+1)*3%7<4):28+!($ryear%4||$ryear%400*!($ryear%100)); # Achtung: Monat als 1...12 (statt 0...11)
  1752. }
  1753. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,23,$rmday,$rmon,$ryear)) if(AttrVal($name, "timestamp_begin", "") eq "previous_day_end");
  1754. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,23,$rmday,$rmon,$ryear)) if(AttrVal($name, "timestamp_end", "") eq "previous_day_end");
  1755. }
  1756. if (AttrVal($name, "timestamp_begin", "") eq "current_hour_begin" ||
  1757. AttrVal($name, "timestamp_end", "") eq "current_hour_begin") {
  1758. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,$hour,$mday,$mon,$year)) if(AttrVal($name, "timestamp_begin", "") eq "current_hour_begin");
  1759. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,$hour,$mday,$mon,$year)) if(AttrVal($name, "timestamp_end", "") eq "current_hour_begin");
  1760. }
  1761. if (AttrVal($name, "timestamp_begin", "") eq "current_hour_end" ||
  1762. AttrVal($name, "timestamp_end", "") eq "current_hour_end") {
  1763. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,$hour,$mday,$mon,$year)) if(AttrVal($name, "timestamp_begin", "") eq "current_hour_end");
  1764. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,$hour,$mday,$mon,$year)) if(AttrVal($name, "timestamp_end", "") eq "current_hour_end");
  1765. }
  1766. if (AttrVal($name, "timestamp_begin", "") eq "previous_hour_begin" ||
  1767. AttrVal($name, "timestamp_end", "") eq "previous_hour_begin") {
  1768. $rhour = $hour-1;
  1769. $rmday = $mday;
  1770. $rmon = $mon;
  1771. $ryear = $year;
  1772. if($rhour<0) {
  1773. $rhour = 23;
  1774. $rmday = $mday-1;
  1775. if($rmday<1) {
  1776. $rmon--;
  1777. if ($rmon<0) {
  1778. $rmon=11;
  1779. $ryear--;
  1780. }
  1781. $rmday = $rmon-1?30+(($rmon+1)*3%7<4):28+!($ryear%4||$ryear%400*!($ryear%100)); # Achtung: Monat als 1...12 (statt 0...11)
  1782. }
  1783. }
  1784. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,$rhour,$rmday,$rmon,$ryear)) if(AttrVal($name, "timestamp_begin", "") eq "previous_hour_begin");
  1785. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(0,0,$rhour,$rmday,$rmon,$ryear)) if(AttrVal($name, "timestamp_end", "") eq "previous_hour_begin");
  1786. }
  1787. if (AttrVal($name, "timestamp_begin", "") eq "previous_hour_end" ||
  1788. AttrVal($name, "timestamp_end", "") eq "previous_hour_end") {
  1789. $rhour = $hour-1;
  1790. $rmday = $mday;
  1791. $rmon = $mon;
  1792. $ryear = $year;
  1793. if($rhour<0) {
  1794. $rhour = 23;
  1795. $rmday = $mday-1;
  1796. if($rmday<1) {
  1797. $rmon--;
  1798. if ($rmon<0) {
  1799. $rmon=11;
  1800. $ryear--;
  1801. }
  1802. $rmday = $rmon-1?30+(($rmon+1)*3%7<4):28+!($ryear%4||$ryear%400*!($ryear%100)); # Achtung: Monat als 1...12 (statt 0...11)
  1803. }
  1804. }
  1805. $tsbegin = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,$rhour,$rmday,$rmon,$ryear)) if(AttrVal($name, "timestamp_begin", "") eq "previous_hour_end");
  1806. $tsend = strftime "%Y-%m-%d %T",localtime(timelocal(59,59,$rhour,$rmday,$rmon,$ryear)) if(AttrVal($name, "timestamp_end", "") eq "previous_hour_end");
  1807. }
  1808. # extrahieren der Einzelwerte von Datum/Zeit Beginn
  1809. my ($yyyy1, $mm1, $dd1, $hh1, $min1, $sec1) = ($tsbegin =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/);
  1810. # extrahieren der Einzelwerte von Datum/Zeit Ende
  1811. my ($yyyy2, $mm2, $dd2, $hh2, $min2, $sec2) = ($tsend =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/);
  1812. # relative Auswertungszeit Beginn berücksichtigen # Umwandeln in Epochesekunden Beginn
  1813. my $epoch_seconds_begin = timelocal($sec1, $min1, $hh1, $dd1, $mm1-1, $yyyy1-1900) if($tsbegin);
  1814. my ($timeolderthan,$timedifftonow) = DbRep_normRelTime($hash);
  1815. if($timedifftonow) {
  1816. $epoch_seconds_begin = time() - $timedifftonow;
  1817. Log3 ($name, 4, "DbRep $name - Time difference to current time for calculating Timestamp begin: $timedifftonow sec");
  1818. } elsif ($timeolderthan) {
  1819. my $mints = $hash->{HELPER}{MINTS}?$hash->{HELPER}{MINTS}:"1970-01-01 01:00:00"; # Timestamp des 1. Datensatzes verwenden falls ermittelt
  1820. $mints =~ /^(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+)$/;
  1821. $epoch_seconds_begin = timelocal($6, $5, $4, $3, $2-1, $1-1900);
  1822. }
  1823. my $tsbegin_string = strftime "%Y-%m-%d %H:%M:%S", localtime($epoch_seconds_begin);
  1824. Log3 ($name, 5, "DbRep $name - Timestamp begin epocheseconds: $epoch_seconds_begin") if($opt !~ /tableCurrentPurge/);
  1825. Log3 ($name, 4, "DbRep $name - Timestamp begin human readable: $tsbegin_string") if($opt !~ /tableCurrentPurge/);
  1826. # relative Auswertungszeit Ende berücksichtigen # Umwandeln in Epochesekunden Endezeit
  1827. my $epoch_seconds_end = timelocal($sec2, $min2, $hh2, $dd2, $mm2-1, $yyyy2-1900);
  1828. $epoch_seconds_end = $timeolderthan ? (time() - $timeolderthan) : $epoch_seconds_end;
  1829. #$epoch_seconds_end = AttrVal($name, "timeOlderThan", undef) ?
  1830. # (time() - AttrVal($name, "timeOlderThan", undef)) : $epoch_seconds_end;
  1831. Log3 ($name, 4, "DbRep $name - Time difference to current time for calculating Timestamp end: $timeolderthan sec") if(AttrVal($name, "timeOlderThan", undef));
  1832. my $tsend_string = strftime "%Y-%m-%d %H:%M:%S", localtime($epoch_seconds_end);
  1833. Log3 ($name, 5, "DbRep $name - Timestamp end epocheseconds: $epoch_seconds_end") if($opt !~ /tableCurrentPurge/);
  1834. Log3 ($name, 4, "DbRep $name - Timestamp end human readable: $tsend_string") if($opt !~ /tableCurrentPurge/);
  1835. # Erstellung Wertehash für Aggregationen
  1836. my $runtime = $epoch_seconds_begin; # Schleifenlaufzeit auf Beginn der Zeitselektion setzen
  1837. my $runtime_string; # Datum/Zeit im SQL-Format für Readingname Teilstring
  1838. my $runtime_string_first; # Datum/Zeit Auswertungsbeginn im SQL-Format für SQL-Statement
  1839. my $runtime_string_next; # Datum/Zeit + Periode (Granularität) für Auswertungsende im SQL-Format
  1840. my $reading_runtime_string; # zusammengesetzter Readingname+Aggregation für Update
  1841. my $tsstr = strftime "%H:%M:%S", localtime($runtime); # für Berechnung Tagesverschieber / Stundenverschieber
  1842. my $testr = strftime "%H:%M:%S", localtime($epoch_seconds_end); # für Berechnung Tagesverschieber / Stundenverschieber
  1843. my $dsstr = strftime "%Y-%m-%d", localtime($runtime); # für Berechnung Tagesverschieber / Stundenverschieber
  1844. my $destr = strftime "%Y-%m-%d", localtime($epoch_seconds_end); # für Berechnung Tagesverschieber / Stundenverschieber
  1845. my $msstr = strftime "%m", localtime($runtime); # Startmonat für Berechnung Monatsverschieber
  1846. my $mestr = strftime "%m", localtime($epoch_seconds_end); # Endemonat für Berechnung Monatsverschieber
  1847. my $ysstr = strftime "%Y", localtime($runtime); # Startjahr für Berechnung Monatsverschieber
  1848. my $yestr = strftime "%Y", localtime($epoch_seconds_end); # Endejahr für Berechnung Monatsverschieber
  1849. my $wd = strftime "%a", localtime($runtime); # Wochentag des aktuellen Startdatum/Zeit
  1850. my $wdadd = 604800 if($wd eq "Mo"); # wenn Start am "Mo" dann nächste Grenze +7 Tage
  1851. $wdadd = 518400 if($wd eq "Di"); # wenn Start am "Di" dann nächste Grenze +6 Tage
  1852. $wdadd = 432000 if($wd eq "Mi"); # wenn Start am "Mi" dann nächste Grenze +5 Tage
  1853. $wdadd = 345600 if($wd eq "Do"); # wenn Start am "Do" dann nächste Grenze +4 Tage
  1854. $wdadd = 259200 if($wd eq "Fr"); # wenn Start am "Fr" dann nächste Grenze +3 Tage
  1855. $wdadd = 172800 if($wd eq "Sa"); # wenn Start am "Sa" dann nächste Grenze +2 Tage
  1856. $wdadd = 86400 if($wd eq "So"); # wenn Start am "So" dann nächste Grenze +1 Tage
  1857. Log3 ($name, 5, "DbRep $name - weekday of start for selection: $wd -> wdadd: $wdadd") if($wdadd);
  1858. my $aggsec;
  1859. if ($aggregation eq "hour") {
  1860. $aggsec = 3600;
  1861. } elsif ($aggregation eq "day") {
  1862. $aggsec = 86400;
  1863. } elsif ($aggregation eq "week") {
  1864. $aggsec = 604800;
  1865. } elsif ($aggregation eq "month") {
  1866. $aggsec = 2678400; # Initialwert, wird in DbRep_collaggstr für jeden Monat berechnet
  1867. } elsif ($aggregation eq "no") {
  1868. $aggsec = 1;
  1869. } else {
  1870. return;
  1871. }
  1872. my %cv = (
  1873. tsstr => $tsstr,
  1874. testr => $testr,
  1875. dsstr => $dsstr,
  1876. destr => $destr,
  1877. msstr => $msstr,
  1878. mestr => $mestr,
  1879. ysstr => $ysstr,
  1880. yestr => $yestr,
  1881. aggsec => $aggsec,
  1882. aggregation => $aggregation,
  1883. epoch_seconds_end => $epoch_seconds_end,
  1884. wdadd => $wdadd
  1885. );
  1886. $hash->{HELPER}{CV} = \%cv;
  1887. my $ts; # für Erstellung Timestamp-Array zur nonblocking SQL-Abarbeitung
  1888. my $i = 1; # Schleifenzähler -> nur Indikator für ersten Durchlauf -> anderer $runtime_string_first
  1889. my $ll; # loopindikator, wenn 1 = loopausstieg
  1890. # Aufbau Timestampstring mit Zeitgrenzen entsprechend Aggregation
  1891. while (!$ll) {
  1892. # collect aggregation strings
  1893. ($runtime,$runtime_string,$runtime_string_first,$runtime_string_next,$ll) = DbRep_collaggstr($hash,$runtime,$i,$runtime_string_next);
  1894. $ts .= $runtime_string."#".$runtime_string_first."#".$runtime_string_next."|";
  1895. $i++;
  1896. }
  1897. return ($epoch_seconds_begin,$epoch_seconds_end,$runtime_string_first,$runtime_string_next,$ts);
  1898. }
  1899. ####################################################################################################
  1900. # Zusammenstellung Aggregationszeiträume
  1901. ####################################################################################################
  1902. sub DbRep_collaggstr($$$$) {
  1903. my ($hash,$runtime,$i,$runtime_string_next) = @_;
  1904. my $name = $hash->{NAME};
  1905. my $runtime_string; # Datum/Zeit im SQL-Format für Readingname Teilstring
  1906. my $runtime_string_first; # Datum/Zeit Auswertungsbeginn im SQL-Format für SQL-Statement
  1907. my $ll; # loopindikator, wenn 1 = loopausstieg
  1908. my $runtime_orig; # orig. runtime als Grundlage für Addition mit $aggsec
  1909. my $tsstr = $hash->{HELPER}{CV}{tsstr}; # für Berechnung Tagesverschieber / Stundenverschieber
  1910. my $testr = $hash->{HELPER}{CV}{testr}; # für Berechnung Tagesverschieber / Stundenverschieber
  1911. my $dsstr = $hash->{HELPER}{CV}{dsstr}; # für Berechnung Tagesverschieber / Stundenverschieber
  1912. my $destr = $hash->{HELPER}{CV}{destr}; # für Berechnung Tagesverschieber / Stundenverschieber
  1913. my $msstr = $hash->{HELPER}{CV}{msstr}; # Startmonat für Berechnung Monatsverschieber
  1914. my $mestr = $hash->{HELPER}{CV}{mestr}; # Endemonat für Berechnung Monatsverschieber
  1915. my $ysstr = $hash->{HELPER}{CV}{ysstr}; # Startjahr für Berechnung Monatsverschieber
  1916. my $yestr = $hash->{HELPER}{CV}{yestr}; # Endejahr für Berechnung Monatsverschieber
  1917. my $aggregation = $hash->{HELPER}{CV}{aggregation}; # Aggregation
  1918. my $aggsec = $hash->{HELPER}{CV}{aggsec}; # laufende Aggregationssekunden
  1919. my $epoch_seconds_end = $hash->{HELPER}{CV}{epoch_seconds_end};
  1920. my $wdadd = $hash->{HELPER}{CV}{wdadd}; # Ergänzungstage. Starttag + Ergänzungstage = der folgende Montag (für week-Aggregation)
  1921. # only for this block because of warnings if some values not set
  1922. no warnings 'uninitialized';
  1923. # keine Aggregation (all between timestamps)
  1924. if ($aggregation eq "no") {
  1925. $runtime_string = "no_aggregation"; # für Readingname
  1926. $runtime_string_first = strftime "%Y-%m-%d %H:%M:%S", localtime($runtime);
  1927. $runtime_string_next = strftime "%Y-%m-%d %H:%M:%S", localtime($epoch_seconds_end);
  1928. $ll = 1;
  1929. }
  1930. # Monatsaggregation
  1931. if ($aggregation eq "month") {
  1932. $runtime_orig = $runtime;
  1933. # Hilfsrechnungen
  1934. my $rm = strftime "%m", localtime($runtime); # Monat des aktuell laufenden Startdatums d. SQL-Select
  1935. my $ry = strftime "%Y", localtime($runtime); # Jahr des aktuell laufenden Startdatums d. SQL-Select
  1936. my $dim = $rm-2?30+($rm*3%7<4):28+!($ry%4||$ry%400*!($ry%100)); # Anzahl Tage des aktuell laufenden Monats
  1937. Log3 ($name, 5, "DbRep $name - act year: $ry, act month: $rm, days in month: $dim, endyear: $yestr, endmonth: $mestr");
  1938. $aggsec = $dim * 86400;
  1939. $runtime = $runtime+3600 if(DbRep_dsttest($hash,$runtime,$aggsec) && (strftime "%m", localtime($runtime)) > 6); # Korrektur Winterzeitumstellung (Uhr wurde 1 Stunde zurück gestellt)
  1940. $runtime_string = strftime "%Y-%m", localtime($runtime); # für Readingname
  1941. if ($i==1) {
  1942. # nur im ersten Durchlauf
  1943. $runtime_string_first = strftime "%Y-%m-%d %H:%M:%S", localtime($runtime_orig);
  1944. }
  1945. if ($ysstr == $yestr && $msstr == $mestr || $ry == $yestr && $rm == $mestr) {
  1946. $runtime_string_first = strftime "%Y-%m-01", localtime($runtime) if($i>1);
  1947. $runtime_string_next = strftime "%Y-%m-%d %H:%M:%S", localtime($epoch_seconds_end);
  1948. $ll=1;
  1949. } else {
  1950. if(($runtime) > $epoch_seconds_end) {
  1951. #$runtime_string_first = strftime "%Y-%m-01", localtime($runtime) if($i>11); # ausgebaut 24.02.2018
  1952. $runtime_string_first = strftime "%Y-%m-01", localtime($runtime);
  1953. $runtime_string_next = strftime "%Y-%m-%d %H:%M:%S", localtime($epoch_seconds_end);
  1954. $ll=1;
  1955. } else {
  1956. $runtime_string_first = strftime "%Y-%m-01", localtime($runtime) if($i>1);
  1957. $runtime_string_next = strftime "%Y-%m-01", localtime($runtime+($dim*86400));
  1958. }
  1959. }
  1960. my ($yyyy1, $mm1, $dd1) = ($runtime_string_next =~ /(\d+)-(\d+)-(\d+)/);
  1961. $runtime = timelocal("00", "00", "00", "01", $mm1-1, $yyyy1-1900);
  1962. # neue Beginnzeit in Epoche-Sekunden
  1963. $runtime = $runtime_orig+$aggsec;
  1964. }
  1965. # Wochenaggregation
  1966. if ($aggregation eq "week") {
  1967. $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)
  1968. $runtime_orig = $runtime;
  1969. my $w = strftime "%V", localtime($runtime); # Wochennummer des aktuellen Startdatum/Zeit
  1970. $runtime_string = "week_".$w; # für Readingname
  1971. my $ms = strftime "%m", localtime($runtime); # Startmonat (01-12)
  1972. my $me = strftime "%m", localtime($epoch_seconds_end); # Endemonat (01-12)
  1973. if ($i==1) {
  1974. # nur im ersten Schleifendurchlauf
  1975. $runtime_string_first = strftime "%Y-%m-%d %H:%M:%S", localtime($runtime);
  1976. # Korrektur $runtime_orig für Berechnung neue Beginnzeit für nächsten Durchlauf
  1977. my ($yyyy1, $mm1, $dd1) = ($runtime_string_first =~ /(\d+)-(\d+)-(\d+)/);
  1978. $runtime = timelocal("00", "00", "00", $dd1, $mm1-1, $yyyy1-1900);
  1979. $runtime = $runtime+3600 if(DbRep_dsttest($hash,$runtime,$aggsec) && (strftime "%m", localtime($runtime)) > 6); # Korrektur Winterzeitumstellung (Uhr wurde 1 Stunde zurück gestellt)
  1980. $runtime = $runtime+$wdadd;
  1981. $runtime_orig = $runtime-$aggsec;
  1982. # die Woche Beginn ist gleich der Woche vom Ende Auswertung
  1983. if((strftime "%V", localtime($epoch_seconds_end)) eq ($w) && ($ms+$me != 13)) {
  1984. $runtime_string_next = strftime "%Y-%m-%d %H:%M:%S", localtime($epoch_seconds_end);
  1985. $ll=1;
  1986. } else {
  1987. $runtime_string_next = strftime "%Y-%m-%d", localtime($runtime);
  1988. }
  1989. } else {
  1990. # weitere Durchläufe
  1991. if(($runtime+$aggsec) > $epoch_seconds_end) {
  1992. $runtime_string_first = strftime "%Y-%m-%d", localtime($runtime_orig);
  1993. $runtime_string_next = strftime "%Y-%m-%d %H:%M:%S", localtime($epoch_seconds_end);
  1994. $ll=1;
  1995. } else {
  1996. $runtime_string_first = strftime "%Y-%m-%d", localtime($runtime_orig) ;
  1997. $runtime_string_next = strftime "%Y-%m-%d", localtime($runtime+$aggsec);
  1998. }
  1999. }
  2000. # neue Beginnzeit in Epoche-Sekunden
  2001. $runtime = $runtime_orig+$aggsec;
  2002. }
  2003. # Tagesaggregation
  2004. if ($aggregation eq "day") {
  2005. $runtime_string = strftime "%Y-%m-%d", localtime($runtime); # für Readingname
  2006. $runtime_string_first = strftime "%Y-%m-%d %H:%M:%S", localtime($runtime) if($i==1);
  2007. $runtime_string_first = strftime "%Y-%m-%d", localtime($runtime) if($i>1);
  2008. $runtime = $runtime+3600 if(DbRep_dsttest($hash,$runtime,$aggsec) && (strftime "%m", localtime($runtime)) > 6); # Korrektur Winterzeitumstellung (Uhr wurde 1 Stunde zurück gestellt)
  2009. if((($tsstr gt $testr) ? $runtime : ($runtime+$aggsec)) > $epoch_seconds_end) {
  2010. $runtime_string_first = strftime "%Y-%m-%d", localtime($runtime);
  2011. $runtime_string_first = strftime "%Y-%m-%d %H:%M:%S", localtime($runtime) if( $dsstr eq $destr);
  2012. $runtime_string_next = strftime "%Y-%m-%d %H:%M:%S", localtime($epoch_seconds_end);
  2013. $ll=1;
  2014. } else {
  2015. $runtime_string_next = strftime "%Y-%m-%d", localtime($runtime+$aggsec);
  2016. }
  2017. Log3 ($name, 5, "DbRep $name - runtime_string: $runtime_string, runtime_string_first: $runtime_string_first, runtime_string_next: $runtime_string_next");
  2018. # neue Beginnzeit in Epoche-Sekunden
  2019. $runtime = $runtime+$aggsec;
  2020. }
  2021. # Stundenaggregation
  2022. if ($aggregation eq "hour") {
  2023. $runtime_string = strftime "%Y-%m-%d_%H", localtime($runtime); # für Readingname
  2024. $runtime_string_first = strftime "%Y-%m-%d %H:%M:%S", localtime($runtime) if($i==1);
  2025. $runtime = $runtime+3600 if(DbRep_dsttest($hash,$runtime,$aggsec) && (strftime "%m", localtime($runtime)) > 6); # Korrektur Winterzeitumstellung (Uhr wurde 1 Stunde zurück gestellt)
  2026. $runtime_string_first = strftime "%Y-%m-%d %H", localtime($runtime) if($i>1);
  2027. my @a = split (":",$tsstr);
  2028. my $hs = $a[0];
  2029. my $msstr = $a[1].":".$a[2];
  2030. @a = split (":",$testr);
  2031. my $he = $a[0];
  2032. my $mestr = $a[1].":".$a[2];
  2033. if((($msstr gt $mestr) ? $runtime : ($runtime+$aggsec)) > $epoch_seconds_end) {
  2034. $runtime_string_first = strftime "%Y-%m-%d %H", localtime($runtime);
  2035. $runtime_string_first = strftime "%Y-%m-%d %H:%M:%S", localtime($runtime) if( $dsstr eq $destr && $hs eq $he);
  2036. $runtime_string_next = strftime "%Y-%m-%d %H:%M:%S", localtime($epoch_seconds_end);
  2037. $ll=1;
  2038. } else {
  2039. $runtime_string_next = strftime "%Y-%m-%d %H", localtime($runtime+$aggsec);
  2040. }
  2041. # neue Beginnzeit in Epoche-Sekunden
  2042. $runtime = $runtime+$aggsec;
  2043. }
  2044. return ($runtime,$runtime_string,$runtime_string_first,$runtime_string_next,$ll);
  2045. }
  2046. ####################################################################################################
  2047. # nichtblockierende DB-Abfrage averageValue
  2048. ####################################################################################################
  2049. sub averval_DoParse($) {
  2050. my ($string) = @_;
  2051. my ($name,$device,$reading,$prop,$ts) = split("\\§", $string);
  2052. my $hash = $defs{$name};
  2053. my $dbloghash = $hash->{dbloghash};
  2054. my $dbconn = $dbloghash->{dbconn};
  2055. my $dbuser = $dbloghash->{dbuser};
  2056. my $dblogname = $dbloghash->{NAME};
  2057. my $dbpassword = $attr{"sec$dblogname"}{secret};
  2058. my $acf = AttrVal($name, "averageCalcForm", "avgArithmeticMean"); # Festlegung Berechnungsschema f. Mittelwert
  2059. my $qlf = "avg";
  2060. my ($dbh,$sql,$sth,$err,$selspec,$addon);
  2061. # Background-Startzeit
  2062. my $bst = [gettimeofday];
  2063. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1 });};
  2064. if ($@) {
  2065. $err = encode_base64($@,"");
  2066. Log3 ($name, 2, "DbRep $name - $@");
  2067. return "$name|''|$device|$reading|''|$err|''";
  2068. }
  2069. # only for this block because of warnings if details of readings are not set
  2070. no warnings 'uninitialized';
  2071. # ist Zeiteingrenzung und/oder Aggregation gesetzt ? (wenn ja -> "?" in SQL sonst undef)
  2072. my ($IsTimeSet,$IsAggrSet) = DbRep_checktimeaggr($hash);
  2073. Log3 ($name, 5, "DbRep $name - IsTimeSet: $IsTimeSet, IsAggrSet: $IsAggrSet");
  2074. # Timestampstring to Array
  2075. my @ts = split("\\|", $ts);
  2076. Log3 ($name, 5, "DbRep $name - Timestamp-Array: \n@ts");
  2077. if($acf eq "avgArithmeticMean") {
  2078. # arithmetischer Mittelwert
  2079. # vorbereiten der DB-Abfrage, DB-Modell-abhaengig
  2080. $addon = '';
  2081. if ($dbloghash->{MODEL} eq "POSTGRESQL") {
  2082. $selspec = "AVG(VALUE::numeric)";
  2083. } elsif ($dbloghash->{MODEL} eq "MYSQL") {
  2084. $selspec = "AVG(VALUE)";
  2085. } elsif ($dbloghash->{MODEL} eq "SQLITE") {
  2086. $selspec = "AVG(VALUE)";
  2087. } else {
  2088. $selspec = "AVG(VALUE)";
  2089. }
  2090. $qlf = "avgam";
  2091. } elsif ($acf eq "avgDailyMeanGWS") {
  2092. # Tagesmittelwert Temperaturen nach Schema des deutschen Wetterdienstes
  2093. # 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;
  2094. $addon = "ORDER BY TIMESTAMP DESC LIMIT 1";
  2095. $selspec = "VALUE";
  2096. $qlf = "avgdmgws";
  2097. } elsif ($acf eq "avgTimeWeightMean") {
  2098. $addon = "ORDER BY TIMESTAMP ASC";
  2099. $selspec = "TIMESTAMP,VALUE";
  2100. $qlf = "avgtwm";
  2101. }
  2102. # SQL-Startzeit
  2103. my $st = [gettimeofday];
  2104. # DB-Abfrage zeilenweise für jeden Array-Eintrag
  2105. my $arrstr;
  2106. foreach my $row (@ts) {
  2107. my @a = split("#", $row);
  2108. my $runtime_string = $a[0];
  2109. my $runtime_string_first = $a[1];
  2110. my $runtime_string_next = $a[2];
  2111. if($acf eq "avgArithmeticMean") {
  2112. # arithmetischer Mittelwert (Standard)
  2113. #
  2114. if ($IsTimeSet || $IsAggrSet) {
  2115. $sql = DbRep_createSelectSql($hash,"history",$selspec,$device,$reading,"'$runtime_string_first'","'$runtime_string_next'",$addon);
  2116. } else {
  2117. $sql = DbRep_createSelectSql($hash,"history",$selspec,$device,$reading,undef,undef,$addon);
  2118. }
  2119. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  2120. eval{ $sth = $dbh->prepare($sql);
  2121. $sth->execute();
  2122. };
  2123. if ($@) {
  2124. $err = encode_base64($@,"");
  2125. Log3 ($name, 2, "DbRep $name - $@");
  2126. $dbh->disconnect;
  2127. return "$name|''|$device|$reading|''|$err|''";
  2128. }
  2129. my @line = $sth->fetchrow_array();
  2130. Log3 ($name, 5, "DbRep $name - SQL result: $line[0]") if($line[0]);
  2131. if(AttrVal($name, "aggregation", "") eq "hour") {
  2132. my @rsf = split(/[" "\|":"]/,$runtime_string_first);
  2133. $arrstr .= $runtime_string."#".$line[0]."#".$rsf[0]."_".$rsf[1]."|";
  2134. } else {
  2135. my @rsf = split(" ",$runtime_string_first);
  2136. $arrstr .= $runtime_string."#".$line[0]."#".$rsf[0]."|";
  2137. }
  2138. } elsif ($acf eq "avgDailyMeanGWS") {
  2139. # Berechnung des Tagesmittelwertes (Temperatur) nach der Vorschrift des deutschen Wetterdienstes
  2140. # Berechnung der Tagesmittel aus 24 Stundenwerten, Bezugszeit für einen Tag i.d.R. 23:51 UTC des
  2141. # Vortages bis 23:50 UTC, d.h. 00:51 bis 23:50 MEZ
  2142. # Wenn mehr als 3 Stundenwerte fehlen -> Berechnung aus den 4 Hauptterminen (00, 06, 12, 18 UTC),
  2143. # d.h. 01, 07, 13, 19 MEZ
  2144. # https://www.dwd.de/DE/leistungen/klimadatendeutschland/beschreibung_tagesmonatswerte.html
  2145. #
  2146. my $sum = 0;
  2147. my $anz = 0; # Anzahl der Messwerte am Tag
  2148. my($t01,$t07,$t13,$t19); # Temperaturen der Haupttermine
  2149. my ($bdate,undef) = split(" ",$runtime_string_first);
  2150. for my $i (0..23) {
  2151. my $bsel = $bdate." ".sprintf("%02d",$i).":00:00";
  2152. my $esel = ($i<23)?$bdate." ".sprintf("%02d",$i).":59:59":$runtime_string_next;
  2153. $sql = DbRep_createSelectSql($hash,"history",$selspec,$device,$reading,"'$bsel'","'$esel'",$addon);
  2154. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  2155. eval{ $sth = $dbh->prepare($sql);
  2156. $sth->execute();
  2157. };
  2158. if ($@) {
  2159. $err = encode_base64($@,"");
  2160. Log3 ($name, 2, "DbRep $name - $@");
  2161. $dbh->disconnect;
  2162. return "$name|''|$device|$reading|''|$err|''";
  2163. }
  2164. my $val = $sth->fetchrow_array();
  2165. Log3 ($name, 5, "DbRep $name - SQL result: $val") if($val);
  2166. $val = DbRep_numval ($val); # nichtnumerische Zeichen eliminieren
  2167. if(defined($val) && looks_like_number($val)) {
  2168. $sum += $val;
  2169. $t01 = $val if($val && $i == 00); # Wert f. Stunde 01 ist zw. letzter Wert vor 01
  2170. $t07 = $val if($val && $i == 06);
  2171. $t13 = $val if($val && $i == 12);
  2172. $t19 = $val if($val && $i == 18);
  2173. $anz++;
  2174. }
  2175. }
  2176. if($anz >= 21) {
  2177. $sum = $sum/24;
  2178. } elsif ($anz >= 4 && $t01 && $t07 && $t13 && $t19) {
  2179. $sum = ($t01+$t07+$t13+$t19)/4;
  2180. } else {
  2181. $sum = "insufficient values";
  2182. }
  2183. if(AttrVal($name, "aggregation", "") eq "hour") {
  2184. my @rsf = split(/[" "\|":"]/,$runtime_string_first);
  2185. $arrstr .= $runtime_string."#".$sum."#".$rsf[0]."_".$rsf[1]."|";
  2186. } else {
  2187. my @rsf = split(" ",$runtime_string_first);
  2188. $arrstr .= $runtime_string."#".$sum."#".$rsf[0]."|";
  2189. }
  2190. } elsif ($acf eq "avgTimeWeightMean") {
  2191. # zeitgewichteten Mittelwert berechnen
  2192. # http://massmatics.de/merkzettel/#!837:Gewichteter_Mittelwert
  2193. #
  2194. # $tsum = timestamp letzter Messpunkt - timestamp erster Messpunkt
  2195. # $t1 = timestamp wert1
  2196. # $t2 = timestamp wert2
  2197. # $dt = $t2 - $t1
  2198. # $t1 = $t2
  2199. # .....
  2200. # (val1*$dt/$tsum) + (val2*$dt/$tsum) + .... + (valn*$dt/$tsum)
  2201. #
  2202. # gesamte Zeitspanne $tsum zwischen ersten und letzten Datensatz der Zeitscheibe ermitteln
  2203. my ($tsum,$tf,$tl,$tn,$to,$dt,$val,$val1);
  2204. my $sum = 0;
  2205. my $addonf = 'ORDER BY TIMESTAMP ASC LIMIT 1';
  2206. my $addonl = 'ORDER BY TIMESTAMP DESC LIMIT 1';
  2207. my $sqlf = DbRep_createSelectSql($hash,"history","TIMESTAMP",$device,$reading,"'$runtime_string_first'","'$runtime_string_next'",$addonf);
  2208. my $sqll = DbRep_createSelectSql($hash,"history","TIMESTAMP",$device,$reading,"'$runtime_string_first'","'$runtime_string_next'",$addonl);
  2209. eval { $tf = ($dbh->selectrow_array($sqlf))[0];
  2210. $tl = ($dbh->selectrow_array($sqll))[0];
  2211. };
  2212. if ($@) {
  2213. $err = encode_base64($@,"");
  2214. Log3 ($name, 2, "DbRep $name - $@");
  2215. $dbh->disconnect;
  2216. return "$name|''|$device|$reading|''|$err|''";
  2217. }
  2218. if(!$tf || !$tl) {
  2219. # kein Start- und/oder Ende Timestamp in Zeitscheibe vorhanden -> keine Werteberechnung möglich
  2220. $sum = "insufficient values";
  2221. } else {
  2222. my ($yyyyf, $mmf, $ddf, $hhf, $minf, $secf) = ($tf =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/);
  2223. my ($yyyyl, $mml, $ddl, $hhl, $minl, $secl) = ($tl =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/);
  2224. $tsum = (timelocal($secl, $minl, $hhl, $ddl, $mml-1, $yyyyl-1900))-(timelocal($secf, $minf, $hhf, $ddf, $mmf-1, $yyyyf-1900));
  2225. if ($IsTimeSet || $IsAggrSet) {
  2226. $sql = DbRep_createSelectSql($hash,"history",$selspec,$device,$reading,"'$runtime_string_first'","'$runtime_string_next'",$addon);
  2227. } else {
  2228. $sql = DbRep_createSelectSql($hash,"history",$selspec,$device,$reading,undef,undef,$addon);
  2229. }
  2230. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  2231. eval{ $sth = $dbh->prepare($sql);
  2232. $sth->execute();
  2233. };
  2234. if ($@) {
  2235. $err = encode_base64($@,"");
  2236. Log3 ($name, 2, "DbRep $name - $@");
  2237. $dbh->disconnect;
  2238. return "$name|''|$device|$reading|''|$err|''";
  2239. }
  2240. my @twm_array = map { $_->[0]."_ESC_".$_->[1] } @{$sth->fetchall_arrayref()};
  2241. foreach my $twmrow (@twm_array) {
  2242. ($tn,$val) = split("_ESC_",$twmrow);
  2243. $val = DbRep_numval ($val); # nichtnumerische Zeichen eliminieren
  2244. my ($yyyyt1, $mmt1, $ddt1, $hht1, $mint1, $sect1) = ($tn =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/);
  2245. $tn = timelocal($sect1, $mint1, $hht1, $ddt1, $mmt1-1, $yyyyt1-1900);
  2246. if(!$to) {
  2247. $val1 = $val;
  2248. $to = $tn;
  2249. next;
  2250. }
  2251. $dt = $tn - $to;
  2252. $sum += $val1*($dt/$tsum);
  2253. $val1 = $val;
  2254. $to = $tn;
  2255. Log3 ($name, 5, "DbRep $name - data element: $twmrow");
  2256. Log3 ($name, 5, "DbRep $name - time sum: $tsum, delta time: $dt, value: $val1, twm: ".$val1*($dt/$tsum));
  2257. }
  2258. }
  2259. if(AttrVal($name, "aggregation", "") eq "hour") {
  2260. my @rsf = split(/[" "\|":"]/,$runtime_string_first);
  2261. $arrstr .= $runtime_string."#".$sum."#".$rsf[0]."_".$rsf[1]."|";
  2262. } else {
  2263. my @rsf = split(" ",$runtime_string_first);
  2264. $arrstr .= $runtime_string."#".$sum."#".$rsf[0]."|";
  2265. }
  2266. }
  2267. }
  2268. $sth->finish;
  2269. $dbh->disconnect;
  2270. # SQL-Laufzeit ermitteln
  2271. my $rt = tv_interval($st);
  2272. # Ergebnisse in Datenbank schreiben
  2273. my ($wrt,$irowdone);
  2274. if($prop =~ /writeToDB/) {
  2275. ($wrt,$irowdone,$err) = DbRep_OutputWriteToDB($name,$device,$reading,$arrstr,$qlf);
  2276. if ($err) {
  2277. Log3 $hash->{NAME}, 2, "DbRep $name - $err";
  2278. $err = encode_base64($err,"");
  2279. return "$name|''|$device|$reading|''|$err|''";
  2280. }
  2281. $rt = $rt+$wrt;
  2282. }
  2283. # Daten müssen als Einzeiler zurückgegeben werden
  2284. $arrstr = encode_base64($arrstr,"");
  2285. # Background-Laufzeit ermitteln
  2286. my $brt = tv_interval($bst);
  2287. $rt = $rt.",".$brt;
  2288. return "$name|$arrstr|$device|$reading|$rt|0|$irowdone";
  2289. }
  2290. ####################################################################################################
  2291. # Auswertungsroutine der nichtblockierenden DB-Abfrage averageValue
  2292. ####################################################################################################
  2293. sub averval_ParseDone($) {
  2294. my ($string) = @_;
  2295. my @a = split("\\|",$string);
  2296. my $hash = $defs{$a[0]};
  2297. my $name = $hash->{NAME};
  2298. my $arrstr = decode_base64($a[1]);
  2299. my $device = $a[2];
  2300. $device =~ s/[^A-Za-z\/\d_\.-]/\//g;
  2301. my $reading = $a[3];
  2302. $reading =~ s/[^A-Za-z\/\d_\.-]/\//g;
  2303. my $bt = $a[4];
  2304. my ($rt,$brt) = split(",", $bt);
  2305. my $err = $a[5]?decode_base64($a[5]):undef;
  2306. my $irowdone = $a[6];
  2307. my $reading_runtime_string;
  2308. if ($err) {
  2309. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  2310. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  2311. delete($hash->{HELPER}{RUNNING_PID});
  2312. return;
  2313. }
  2314. # only for this block because of warnings if details of readings are not set
  2315. no warnings 'uninitialized';
  2316. my $acf = AttrVal($name, "averageCalcForm", "avgArithmeticMean");
  2317. if($acf eq "avgArithmeticMean") {
  2318. $acf = "AM"
  2319. } elsif ($acf eq "avgDailyMeanGWS") {
  2320. $acf = "DMGWS";
  2321. } elsif ($acf eq "avgTimeWeightMean") {
  2322. $acf = "TWM";
  2323. }
  2324. # Readingaufbereitung
  2325. readingsBeginUpdate($hash);
  2326. my @arr = split("\\|", $arrstr);
  2327. foreach my $row (@arr) {
  2328. my @a = split("#", $row);
  2329. my $runtime_string = $a[0];
  2330. my $c = $a[1];
  2331. my $rsf = $a[2]."__";
  2332. if (AttrVal($hash->{NAME}, "readingNameMap", "")) {
  2333. $reading_runtime_string = $rsf.AttrVal($hash->{NAME}, "readingNameMap", "")."__".$runtime_string;
  2334. } else {
  2335. my $ds = $device."__" if ($device);
  2336. my $rds = $reading."__" if ($reading);
  2337. $reading_runtime_string = $rsf.$ds.$rds."AVG".$acf."__".$runtime_string;
  2338. }
  2339. if($acf eq "DMGWS") {
  2340. ReadingsBulkUpdateValue ($hash, $reading_runtime_string, looks_like_number($c)?sprintf("%.1f",$c):$c);
  2341. } else {
  2342. ReadingsBulkUpdateValue ($hash, $reading_runtime_string, $c?sprintf("%.4f",$c):"-");
  2343. }
  2344. }
  2345. ReadingsBulkUpdateValue ($hash, "db_lines_processed", $irowdone) if($hash->{LASTCMD} =~ /writeToDB/);
  2346. ReadingsBulkUpdateTimeState($hash,$brt,$rt,"done");
  2347. readingsEndUpdate($hash, 1);
  2348. delete($hash->{HELPER}{RUNNING_PID});
  2349. return;
  2350. }
  2351. ####################################################################################################
  2352. # nichtblockierende DB-Abfrage count
  2353. ####################################################################################################
  2354. sub count_DoParse($) {
  2355. my ($string) = @_;
  2356. my ($name,$table,$device,$reading,$ts) = split("\\§", $string);
  2357. my $hash = $defs{$name};
  2358. my $dbloghash = $hash->{dbloghash};
  2359. my $dbconn = $dbloghash->{dbconn};
  2360. my $dbuser = $dbloghash->{dbuser};
  2361. my $dblogname = $dbloghash->{NAME};
  2362. my $dbpassword = $attr{"sec$dblogname"}{secret};
  2363. my ($dbh,$sql,$sth,$err,$selspec);
  2364. # Background-Startzeit
  2365. my $bst = [gettimeofday];
  2366. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1 });};
  2367. if ($@) {
  2368. $err = encode_base64($@,"");
  2369. Log3 ($name, 2, "DbRep $name - $@");
  2370. return "$name|''|$device|$reading|''|$err|$table";
  2371. }
  2372. # only for this block because of warnings if details of readings are not set
  2373. no warnings 'uninitialized';
  2374. # ist Zeiteingrenzung und/oder Aggregation gesetzt ? (wenn ja -> "?" in SQL sonst undef)
  2375. my ($IsTimeSet,$IsAggrSet,$aggregation) = DbRep_checktimeaggr($hash);
  2376. Log3 ($name, 5, "DbRep $name - IsTimeSet: $IsTimeSet, IsAggrSet: $IsAggrSet");
  2377. # Timestampstring to Array
  2378. my @ts = split("\\|", $ts);
  2379. Log3 ($name, 5, "DbRep $name - Timestamp-Array: \n@ts");
  2380. # SQL-Startzeit
  2381. my $st = [gettimeofday];
  2382. # DB-Abfrage zeilenweise für jeden Timearray-Eintrag
  2383. my $arrstr;
  2384. foreach my $row (@ts) {
  2385. my @a = split("#", $row);
  2386. my $runtime_string = $a[0];
  2387. my $runtime_string_first = $a[1];
  2388. my $runtime_string_next = $a[2];
  2389. if ($IsTimeSet || $IsAggrSet) {
  2390. $sql = DbRep_createSelectSql($hash,$table,"COUNT(*)",$device,$reading,"'$runtime_string_first'","'$runtime_string_next'",'');
  2391. } else {
  2392. $sql = DbRep_createSelectSql($hash,$table,"COUNT(*)",$device,$reading,undef,undef,'');
  2393. }
  2394. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  2395. eval{ $sth = $dbh->prepare($sql);
  2396. $sth->execute();
  2397. };
  2398. if ($@) {
  2399. $err = encode_base64($@,"");
  2400. Log3 ($name, 2, "DbRep $name - $@");
  2401. $dbh->disconnect;
  2402. return "$name|''|$device|$reading|''|$err|$table";
  2403. }
  2404. # DB-Abfrage -> Ergebnis in @arr aufnehmen
  2405. my @line = $sth->fetchrow_array();
  2406. Log3 ($name, 5, "DbRep $name - SQL result: $line[0]") if($line[0]);
  2407. if($aggregation eq "hour") {
  2408. my @rsf = split(/[" "\|":"]/,$runtime_string_first);
  2409. $arrstr .= $runtime_string."#".$line[0]."#".$rsf[0]."_".$rsf[1]."|";
  2410. } else {
  2411. my @rsf = split(" ",$runtime_string_first);
  2412. $arrstr .= $runtime_string."#".$line[0]."#".$rsf[0]."|";
  2413. }
  2414. }
  2415. $sth->finish;
  2416. $dbh->disconnect;
  2417. # SQL-Laufzeit ermitteln
  2418. my $rt = tv_interval($st);
  2419. # Daten müssen als Einzeiler zurückgegeben werden
  2420. $arrstr = encode_base64($arrstr,"");
  2421. # Background-Laufzeit ermitteln
  2422. my $brt = tv_interval($bst);
  2423. $rt = $rt.",".$brt;
  2424. return "$name|$arrstr|$device|$reading|$rt|0|$table";
  2425. }
  2426. ####################################################################################################
  2427. # Auswertungsroutine der nichtblockierenden DB-Abfrage count
  2428. ####################################################################################################
  2429. sub count_ParseDone($) {
  2430. my ($string) = @_;
  2431. my @a = split("\\|",$string);
  2432. my $hash = $defs{$a[0]};
  2433. my $name = $hash->{NAME};
  2434. my $arrstr = decode_base64($a[1]);
  2435. my $device = $a[2];
  2436. $device =~ s/[^A-Za-z\/\d_\.-]/\//g;
  2437. my $reading = $a[3];
  2438. $reading =~ s/[^A-Za-z\/\d_\.-]/\//g;
  2439. my $bt = $a[4];
  2440. my ($rt,$brt) = split(",", $bt);
  2441. my $err = $a[5]?decode_base64($a[5]):undef;
  2442. my $table = $a[6];
  2443. my $reading_runtime_string;
  2444. if ($err) {
  2445. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  2446. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  2447. delete($hash->{HELPER}{RUNNING_PID});
  2448. return;
  2449. }
  2450. Log3 ($name, 5, "DbRep $name - SQL result decoded: $arrstr") if($arrstr);
  2451. # only for this block because of warnings if details of readings are not set
  2452. no warnings 'uninitialized';
  2453. # Readingaufbereitung
  2454. readingsBeginUpdate($hash);
  2455. my @arr = split("\\|", $arrstr);
  2456. foreach my $row (@arr) {
  2457. my @a = split("#", $row);
  2458. my $runtime_string = $a[0];
  2459. my $c = $a[1];
  2460. my $rsf = $a[2]."__";
  2461. if (AttrVal($hash->{NAME}, "readingNameMap", "")) {
  2462. $reading_runtime_string = $rsf.AttrVal($hash->{NAME}, "readingNameMap", "")."__".$runtime_string;
  2463. } else {
  2464. my $ds = $device."__" if ($device);
  2465. my $rds = $reading."__" if ($reading);
  2466. $reading_runtime_string = $rsf.$ds.$rds."COUNT_".$table."__".$runtime_string;
  2467. }
  2468. ReadingsBulkUpdateValue ($hash, $reading_runtime_string, $c?$c:"-");
  2469. }
  2470. ReadingsBulkUpdateTimeState($hash,$brt,$rt,"done");
  2471. readingsEndUpdate($hash, 1);
  2472. delete($hash->{HELPER}{RUNNING_PID});
  2473. return;
  2474. }
  2475. ####################################################################################################
  2476. # nichtblockierende DB-Abfrage maxValue
  2477. ####################################################################################################
  2478. sub maxval_DoParse($) {
  2479. my ($string) = @_;
  2480. my ($name,$device,$reading,$prop,$ts) = split("\\§", $string);
  2481. my $hash = $defs{$name};
  2482. my $dbloghash = $hash->{dbloghash};
  2483. my $dbconn = $dbloghash->{dbconn};
  2484. my $dbuser = $dbloghash->{dbuser};
  2485. my $dblogname = $dbloghash->{NAME};
  2486. my $dbpassword = $attr{"sec$dblogname"}{secret};
  2487. my ($dbh,$sql,$sth,$err);
  2488. # Background-Startzeit
  2489. my $bst = [gettimeofday];
  2490. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1 });};
  2491. if ($@) {
  2492. $err = encode_base64($@,"");
  2493. Log3 ($name, 2, "DbRep $name - $@");
  2494. return "$name|''|$device|$reading|''|$err|''";
  2495. }
  2496. # only for this block because of warnings if details of readings are not set
  2497. no warnings 'uninitialized';
  2498. # ist Zeiteingrenzung und/oder Aggregation gesetzt ? (wenn ja -> "?" in SQL sonst undef)
  2499. my ($IsTimeSet,$IsAggrSet) = DbRep_checktimeaggr($hash);
  2500. Log3 ($name, 5, "DbRep $name - IsTimeSet: $IsTimeSet, IsAggrSet: $IsAggrSet");
  2501. # Timestampstring to Array
  2502. my @ts = split("\\|", $ts);
  2503. Log3 ($name, 5, "DbRep $name - Timestamp-Array: \n@ts");
  2504. # SQL-Startzeit
  2505. my $st = [gettimeofday];
  2506. # DB-Abfrage zeilenweise für jeden Array-Eintrag
  2507. my @row_array;
  2508. foreach my $row (@ts) {
  2509. my @a = split("#", $row);
  2510. my $runtime_string = $a[0];
  2511. my $runtime_string_first = $a[1];
  2512. my $runtime_string_next = $a[2];
  2513. $runtime_string = encode_base64($runtime_string,"");
  2514. if ($IsTimeSet || $IsAggrSet) {
  2515. $sql = DbRep_createSelectSql($hash,"history","VALUE,TIMESTAMP",$device,$reading,"'$runtime_string_first'","'$runtime_string_next'","ORDER BY TIMESTAMP");
  2516. } else {
  2517. $sql = DbRep_createSelectSql($hash,"history","VALUE,TIMESTAMP",$device,$reading,undef,undef,"ORDER BY TIMESTAMP");
  2518. }
  2519. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  2520. eval{ $sth = $dbh->prepare($sql);
  2521. $sth->execute();
  2522. };
  2523. if ($@) {
  2524. $err = encode_base64($@,"");
  2525. Log3 ($name, 2, "DbRep $name - $@");
  2526. $dbh->disconnect;
  2527. return "$name|''|$device|$reading|''|$err|''";
  2528. }
  2529. my @array= map { $runtime_string." ".$_ -> [0]." ".$_ -> [1]."\n" } @{ $sth->fetchall_arrayref() };
  2530. if(!@array) {
  2531. if(AttrVal($name, "aggregation", "") eq "hour") {
  2532. my @rsf = split(/[" "\|":"]/,$runtime_string_first);
  2533. @array = ($runtime_string." "."0"." ".$rsf[0]."_".$rsf[1]."\n");
  2534. } else {
  2535. my @rsf = split(" ",$runtime_string_first);
  2536. @array = ($runtime_string." "."0"." ".$rsf[0]."\n");
  2537. }
  2538. }
  2539. push(@row_array, @array);
  2540. }
  2541. $sth->finish;
  2542. $dbh->disconnect;
  2543. # SQL-Laufzeit ermitteln
  2544. my $rt = tv_interval($st);
  2545. Log3 ($name, 5, "DbRep $name -> raw data of row_array result:\n @row_array");
  2546. #---------- Berechnung Ergebnishash maxValue ------------------------
  2547. my $i = 1;
  2548. my %rh = ();
  2549. my ($lastruntimestring,$row_max_time,$max_value);
  2550. foreach my $row (@row_array) {
  2551. my @a = split("[ \t][ \t]*", $row);
  2552. my $runtime_string = decode_base64($a[0]);
  2553. $lastruntimestring = $runtime_string if ($i == 1);
  2554. my $value = $a[1];
  2555. $a[-1] =~ s/:/-/g if($a[-1]); # substituieren unsupported characters -> siehe fhem.pl
  2556. my $timestamp = ($a[-1]&&$a[-2])?$a[-2]."_".$a[-1]:$a[-1];
  2557. # Leerzeichen am Ende $timestamp entfernen
  2558. $timestamp =~ s/\s+$//g;
  2559. # Test auf $value = "numeric"
  2560. if (!looks_like_number($value)) {
  2561. Log3 ($name, 2, "DbRep $name - ERROR - value isn't numeric in maxValue function. Faulty dataset was \nTIMESTAMP: $timestamp, DEVICE: $device, READING: $reading, VALUE: $value.");
  2562. $err = encode_base64("Value isn't numeric. Faulty dataset was - TIMESTAMP: $timestamp, VALUE: $value", "");
  2563. return "$name|''|$device|$reading|''|$err|''";
  2564. }
  2565. Log3 ($name, 5, "DbRep $name - Runtimestring: $runtime_string, DEVICE: $device, READING: $reading, TIMESTAMP: $timestamp, VALUE: $value");
  2566. if ($runtime_string eq $lastruntimestring) {
  2567. if (!defined($max_value) || $value >= $max_value) {
  2568. $max_value = $value;
  2569. $row_max_time = $timestamp;
  2570. $rh{$runtime_string} = $runtime_string."|".$max_value."|".$row_max_time;
  2571. }
  2572. } else {
  2573. # neuer Zeitabschnitt beginnt, ersten Value-Wert erfassen
  2574. $lastruntimestring = $runtime_string;
  2575. undef $max_value;
  2576. if (!defined($max_value) || $value >= $max_value) {
  2577. $max_value = $value;
  2578. $row_max_time = $timestamp;
  2579. $rh{$runtime_string} = $runtime_string."|".$max_value."|".$row_max_time;
  2580. }
  2581. }
  2582. $i++;
  2583. }
  2584. #---------------------------------------------------------------------------------------------
  2585. Log3 ($name, 5, "DbRep $name - result of maxValue calculation before encoding:");
  2586. foreach my $key (sort(keys(%rh))) {
  2587. Log3 ($name, 5, "runtimestring Key: $key, value: ".$rh{$key});
  2588. }
  2589. # Ergebnishash als Einzeiler zurückgeben bzw. Übergabe Schreibroutine
  2590. my $rows = join('§', %rh);
  2591. # Ergebnisse in Datenbank schreiben
  2592. my ($wrt,$irowdone);
  2593. if($prop =~ /writeToDB/) {
  2594. ($wrt,$irowdone,$err) = DbRep_OutputWriteToDB($name,$device,$reading,$rows,"max");
  2595. if ($err) {
  2596. Log3 $hash->{NAME}, 2, "DbRep $name - $err";
  2597. $err = encode_base64($err,"");
  2598. return "$name|''|$device|$reading|''|$err|''";
  2599. }
  2600. $rt = $rt+$wrt;
  2601. }
  2602. my $rowlist = encode_base64($rows,"");
  2603. # Background-Laufzeit ermitteln
  2604. my $brt = tv_interval($bst);
  2605. $rt = $rt.",".$brt;
  2606. return "$name|$rowlist|$device|$reading|$rt|0|$irowdone";
  2607. }
  2608. ####################################################################################################
  2609. # Auswertungsroutine der nichtblockierenden DB-Abfrage maxValue
  2610. ####################################################################################################
  2611. sub maxval_ParseDone($) {
  2612. my ($string) = @_;
  2613. my @a = split("\\|",$string);
  2614. my $hash = $defs{$a[0]};
  2615. my $name = $hash->{NAME};
  2616. my $rowlist = decode_base64($a[1]);
  2617. my $device = $a[2];
  2618. $device =~ s/[^A-Za-z\/\d_\.-]/\//g;
  2619. my $reading = $a[3];
  2620. $reading =~ s/[^A-Za-z\/\d_\.-]/\//g;
  2621. my $bt = $a[4];
  2622. my ($rt,$brt) = split(",", $bt);
  2623. my $err = $a[5]?decode_base64($a[5]):undef;
  2624. my $irowdone = $a[6];
  2625. my $reading_runtime_string;
  2626. if ($err) {
  2627. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  2628. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  2629. delete($hash->{HELPER}{RUNNING_PID});
  2630. return;
  2631. }
  2632. my %rh = split("§", $rowlist);
  2633. Log3 ($name, 5, "DbRep $name - result of maxValue calculation after decoding:");
  2634. foreach my $key (sort(keys(%rh))) {
  2635. Log3 ($name, 5, "DbRep $name - runtimestring Key: $key, value: ".$rh{$key});
  2636. }
  2637. # Readingaufbereitung
  2638. readingsBeginUpdate($hash);
  2639. # only for this block because of warnings if details of readings are not set
  2640. no warnings 'uninitialized';
  2641. foreach my $key (sort(keys(%rh))) {
  2642. my @k = split("\\|",$rh{$key});
  2643. my $rsf = $k[2]."__" if($k[2]);
  2644. if (AttrVal($hash->{NAME}, "readingNameMap", "")) {
  2645. $reading_runtime_string = $rsf.AttrVal($hash->{NAME}, "readingNameMap", "")."__".$k[0];
  2646. } else {
  2647. my $ds = $device."__" if ($device);
  2648. my $rds = $reading."__" if ($reading);
  2649. $reading_runtime_string = $rsf.$ds.$rds."MAX__".$k[0];
  2650. }
  2651. my $rv = $k[1];
  2652. ReadingsBulkUpdateValue ($hash, $reading_runtime_string, defined($rv)?sprintf("%.4f",$rv):"-");
  2653. }
  2654. ReadingsBulkUpdateValue ($hash, "db_lines_processed", $irowdone) if($hash->{LASTCMD} =~ /writeToDB/);
  2655. ReadingsBulkUpdateTimeState($hash,$brt,$rt,"done");
  2656. readingsEndUpdate($hash, 1);
  2657. delete($hash->{HELPER}{RUNNING_PID});
  2658. return;
  2659. }
  2660. ####################################################################################################
  2661. # nichtblockierende DB-Abfrage minValue
  2662. ####################################################################################################
  2663. sub minval_DoParse($) {
  2664. my ($string) = @_;
  2665. my ($name,$device,$reading,$prop,$ts) = split("\\§", $string);
  2666. my $hash = $defs{$name};
  2667. my $dbloghash = $hash->{dbloghash};
  2668. my $dbconn = $dbloghash->{dbconn};
  2669. my $dbuser = $dbloghash->{dbuser};
  2670. my $dblogname = $dbloghash->{NAME};
  2671. my $dbpassword = $attr{"sec$dblogname"}{secret};
  2672. my ($dbh,$sql,$sth,$err);
  2673. # Background-Startzeit
  2674. my $bst = [gettimeofday];
  2675. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1 });};
  2676. if ($@) {
  2677. $err = encode_base64($@,"");
  2678. Log3 ($name, 2, "DbRep $name - $@");
  2679. return "$name|''|$device|$reading|''|$err|''";
  2680. }
  2681. # only for this block because of warnings if details of readings are not set
  2682. no warnings 'uninitialized';
  2683. # ist Zeiteingrenzung und/oder Aggregation gesetzt ? (wenn ja -> "?" in SQL sonst undef)
  2684. my ($IsTimeSet,$IsAggrSet) = DbRep_checktimeaggr($hash);
  2685. Log3 ($name, 5, "DbRep $name - IsTimeSet: $IsTimeSet, IsAggrSet: $IsAggrSet");
  2686. # Timestampstring to Array
  2687. my @ts = split("\\|", $ts);
  2688. Log3 ($name, 5, "DbRep $name - Timestamp-Array: \n@ts");
  2689. # SQL-Startzeit
  2690. my $st = [gettimeofday];
  2691. # DB-Abfrage zeilenweise für jeden Array-Eintrag
  2692. my @row_array;
  2693. foreach my $row (@ts) {
  2694. my @a = split("#", $row);
  2695. my $runtime_string = $a[0];
  2696. my $runtime_string_first = $a[1];
  2697. my $runtime_string_next = $a[2];
  2698. $runtime_string = encode_base64($runtime_string,"");
  2699. if ($IsTimeSet || $IsAggrSet) {
  2700. $sql = DbRep_createSelectSql($hash,"history","VALUE,TIMESTAMP",$device,$reading,"'$runtime_string_first'","'$runtime_string_next'","ORDER BY TIMESTAMP");
  2701. } else {
  2702. $sql = DbRep_createSelectSql($hash,"history","VALUE,TIMESTAMP",$device,$reading,undef,undef,"ORDER BY TIMESTAMP");
  2703. }
  2704. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  2705. eval{ $sth = $dbh->prepare($sql);
  2706. $sth->execute();
  2707. };
  2708. if ($@) {
  2709. $err = encode_base64($@,"");
  2710. Log3 ($name, 2, "DbRep $name - $@");
  2711. $dbh->disconnect;
  2712. return "$name|''|$device|$reading|''|$err|''";
  2713. }
  2714. my @array= map { $runtime_string." ".$_ -> [0]." ".$_ -> [1]."\n" } @{ $sth->fetchall_arrayref() };
  2715. if(!@array) {
  2716. if(AttrVal($name, "aggregation", "") eq "hour") {
  2717. my @rsf = split(/[" "\|":"]/,$runtime_string_first);
  2718. @array = ($runtime_string." "."0"." ".$rsf[0]."_".$rsf[1]."\n");
  2719. } else {
  2720. my @rsf = split(" ",$runtime_string_first);
  2721. @array = ($runtime_string." "."0"." ".$rsf[0]."\n");
  2722. }
  2723. }
  2724. push(@row_array, @array);
  2725. }
  2726. $sth->finish;
  2727. $dbh->disconnect;
  2728. # SQL-Laufzeit ermitteln
  2729. my $rt = tv_interval($st);
  2730. Log3 ($name, 5, "DbRep $name -> raw data of row_array result:\n @row_array");
  2731. #---------- Berechnung Ergebnishash minValue ------------------------
  2732. my $i = 1;
  2733. my %rh = ();
  2734. my $lastruntimestring;
  2735. my $row_min_time;
  2736. my ($min_value,$value);
  2737. foreach my $row (@row_array) {
  2738. my @a = split("[ \t][ \t]*", $row);
  2739. my $runtime_string = decode_base64($a[0]);
  2740. $lastruntimestring = $runtime_string if ($i == 1);
  2741. $value = $a[1];
  2742. $min_value = $a[1] if ($i == 1);
  2743. $a[-1] =~ s/:/-/g if($a[-1]); # substituieren unsupported characters -> siehe fhem.pl
  2744. my $timestamp = ($a[-1]&&$a[-2])?$a[-2]."_".$a[-1]:$a[-1];
  2745. # Leerzeichen am Ende $timestamp entfernen
  2746. $timestamp =~ s/\s+$//g;
  2747. # Test auf $value = "numeric"
  2748. if (!looks_like_number($value)) {
  2749. # $a[-1] =~ s/\s+$//g;
  2750. Log3 ($name, 2, "DbRep $name - ERROR - value isn't numeric in minValue function. Faulty dataset was \nTIMESTAMP: $timestamp, DEVICE: $device, READING: $reading, VALUE: $value.");
  2751. $err = encode_base64("Value isn't numeric. Faulty dataset was - TIMESTAMP: $timestamp, VALUE: $value", "");
  2752. return "$name|''|$device|$reading|''|$err|''";
  2753. }
  2754. Log3 ($name, 5, "DbRep $name - Runtimestring: $runtime_string, DEVICE: $device, READING: $reading, TIMESTAMP: $timestamp, VALUE: $value");
  2755. $rh{$runtime_string} = $runtime_string."|".$min_value."|".$timestamp if ($i == 1); # minValue des ersten SQL-Statements in hash einfügen
  2756. if ($runtime_string eq $lastruntimestring) {
  2757. if (!defined($min_value) || $value < $min_value) {
  2758. $min_value = $value;
  2759. $row_min_time = $timestamp;
  2760. $rh{$runtime_string} = $runtime_string."|".$min_value."|".$row_min_time;
  2761. }
  2762. } else {
  2763. # neuer Zeitabschnitt beginnt, ersten Value-Wert erfassen
  2764. $lastruntimestring = $runtime_string;
  2765. $min_value = $value;
  2766. $row_min_time = $timestamp;
  2767. $rh{$runtime_string} = $runtime_string."|".$min_value."|".$row_min_time;
  2768. }
  2769. $i++;
  2770. }
  2771. #---------------------------------------------------------------------------------------------
  2772. Log3 ($name, 5, "DbRep $name - result of minValue calculation before encoding:");
  2773. foreach my $key (sort(keys(%rh))) {
  2774. Log3 ($name, 5, "runtimestring Key: $key, value: ".$rh{$key});
  2775. }
  2776. # Ergebnishash als Einzeiler zurückgeben bzw. an Schreibroutine übergeben
  2777. my $rows = join('§', %rh);
  2778. # Ergebnisse in Datenbank schreiben
  2779. my ($wrt,$irowdone);
  2780. if($prop =~ /writeToDB/) {
  2781. ($wrt,$irowdone,$err) = DbRep_OutputWriteToDB($name,$device,$reading,$rows,"min");
  2782. if ($err) {
  2783. Log3 $hash->{NAME}, 2, "DbRep $name - $err";
  2784. $err = encode_base64($err,"");
  2785. return "$name|''|$device|$reading|''|$err|''";
  2786. }
  2787. $rt = $rt+$wrt;
  2788. }
  2789. my $rowlist = encode_base64($rows,"");
  2790. # Background-Laufzeit ermitteln
  2791. my $brt = tv_interval($bst);
  2792. $rt = $rt.",".$brt;
  2793. return "$name|$rowlist|$device|$reading|$rt|0|$irowdone";
  2794. }
  2795. ####################################################################################################
  2796. # Auswertungsroutine der nichtblockierenden DB-Abfrage minValue
  2797. ####################################################################################################
  2798. sub minval_ParseDone($) {
  2799. my ($string) = @_;
  2800. my @a = split("\\|",$string);
  2801. my $hash = $defs{$a[0]};
  2802. my $name = $hash->{NAME};
  2803. my $rowlist = decode_base64($a[1]);
  2804. my $device = $a[2];
  2805. $device =~ s/[^A-Za-z\/\d_\.-]/\//g;
  2806. my $reading = $a[3];
  2807. $reading =~ s/[^A-Za-z\/\d_\.-]/\//g;
  2808. my $bt = $a[4];
  2809. my ($rt,$brt) = split(",", $bt);
  2810. my $err = $a[5]?decode_base64($a[5]):undef;
  2811. my $irowdone = $a[6];
  2812. my $reading_runtime_string;
  2813. if ($err) {
  2814. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  2815. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  2816. delete($hash->{HELPER}{RUNNING_PID});
  2817. return;
  2818. }
  2819. my %rh = split("§", $rowlist);
  2820. Log3 ($name, 5, "DbRep $name - result of minValue calculation after decoding:");
  2821. foreach my $key (sort(keys(%rh))) {
  2822. Log3 ($name, 5, "DbRep $name - runtimestring Key: $key, value: ".$rh{$key});
  2823. }
  2824. # Readingaufbereitung
  2825. readingsBeginUpdate($hash);
  2826. # only for this block because of warnings if details of readings are not set
  2827. no warnings 'uninitialized';
  2828. foreach my $key (sort(keys(%rh))) {
  2829. my @k = split("\\|",$rh{$key});
  2830. my $rsf = $k[2]."__" if($k[2]);
  2831. if (AttrVal($hash->{NAME}, "readingNameMap", "")) {
  2832. $reading_runtime_string = $rsf.AttrVal($hash->{NAME}, "readingNameMap", "")."__".$k[0];
  2833. } else {
  2834. my $ds = $device."__" if ($device);
  2835. my $rds = $reading."__" if ($reading);
  2836. $reading_runtime_string = $rsf.$ds.$rds."MIN__".$k[0];
  2837. }
  2838. my $rv = $k[1];
  2839. ReadingsBulkUpdateValue ($hash, $reading_runtime_string, defined($rv)?sprintf("%.4f",$rv):"-");
  2840. }
  2841. ReadingsBulkUpdateValue ($hash, "db_lines_processed", $irowdone) if($hash->{LASTCMD} =~ /writeToDB/);
  2842. ReadingsBulkUpdateTimeState($hash,$brt,$rt,"done");
  2843. readingsEndUpdate($hash, 1);
  2844. delete($hash->{HELPER}{RUNNING_PID});
  2845. return;
  2846. }
  2847. ####################################################################################################
  2848. # nichtblockierende DB-Abfrage diffValue
  2849. ####################################################################################################
  2850. sub diffval_DoParse($) {
  2851. my ($string) = @_;
  2852. my ($name,$device,$reading,$prop,$ts) = split("\\§", $string);
  2853. my $hash = $defs{$name};
  2854. my $dbloghash = $hash->{dbloghash};
  2855. my $dbconn = $dbloghash->{dbconn};
  2856. my $dbuser = $dbloghash->{dbuser};
  2857. my $dblogname = $dbloghash->{NAME};
  2858. my $dbmodel = $dbloghash->{MODEL};
  2859. my $dbpassword = $attr{"sec$dblogname"}{secret};
  2860. my ($dbh,$sql,$sth,$err,$selspec);
  2861. # Background-Startzeit
  2862. my $bst = [gettimeofday];
  2863. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1 });};
  2864. if ($@) {
  2865. $err = encode_base64($@,"");
  2866. Log3 ($name, 2, "DbRep $name - $@");
  2867. return "$name|''|$device|$reading|''|''|''|$err|''";
  2868. }
  2869. # only for this block because of warnings if details of readings are not set
  2870. no warnings 'uninitialized';
  2871. # ist Zeiteingrenzung und/oder Aggregation gesetzt ? (wenn ja -> "?" in SQL sonst undef)
  2872. my ($IsTimeSet,$IsAggrSet) = DbRep_checktimeaggr($hash);
  2873. Log3 ($name, 5, "DbRep $name - IsTimeSet: $IsTimeSet, IsAggrSet: $IsAggrSet");
  2874. # Timestampstring to Array
  2875. my @ts = split("\\|", $ts);
  2876. Log3 ($name, 5, "DbRep $name - Timestamp-Array: \n@ts");
  2877. #vorbereiten der DB-Abfrage, DB-Modell-abhaengig
  2878. if($dbmodel eq "MYSQL") {
  2879. $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 ";
  2880. } else {
  2881. $selspec = "TIMESTAMP,VALUE";
  2882. }
  2883. # SQL-Startzeit
  2884. my $st = [gettimeofday];
  2885. # DB-Abfrage zeilenweise für jeden Array-Eintrag
  2886. my @row_array;
  2887. my @array;
  2888. foreach my $row (@ts) {
  2889. my @a = split("#", $row);
  2890. my $runtime_string = $a[0];
  2891. my $runtime_string_first = $a[1];
  2892. my $runtime_string_next = $a[2];
  2893. $runtime_string = encode_base64($runtime_string,"");
  2894. if($dbmodel eq "MYSQL") {
  2895. eval {$dbh->do("set \@V:= 0, \@diff:= 0, \@diffTotal:= 0, \@RB:= 1;");}; # @\RB = Resetbit wenn neues Selektionsintervall beginnt
  2896. }
  2897. if ($@) {
  2898. $err = encode_base64($@,"");
  2899. Log3 ($name, 2, "DbRep $name - $@");
  2900. $dbh->disconnect;
  2901. return "$name|''|$device|$reading|''|''|''|$err|''";
  2902. }
  2903. if ($IsTimeSet || $IsAggrSet) {
  2904. $sql = DbRep_createSelectSql($hash,"history",$selspec,$device,$reading,"'$runtime_string_first'","'$runtime_string_next'",'');
  2905. } else {
  2906. $sql = DbRep_createSelectSql($hash,"history",$selspec,$device,$reading,undef,undef,'');
  2907. }
  2908. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  2909. eval{ $sth = $dbh->prepare($sql);
  2910. $sth->execute();
  2911. };
  2912. if ($@) {
  2913. $err = encode_base64($@,"");
  2914. Log3 ($name, 2, "DbRep $name - $@");
  2915. $dbh->disconnect;
  2916. return "$name|''|$device|$reading|''|''|''|$err|''";
  2917. } else {
  2918. if($dbmodel eq "MYSQL") {
  2919. @array = map { $runtime_string." ".$_ -> [0]." ".$_ -> [1]." ".$_ -> [2]."\n" } @{ $sth->fetchall_arrayref() };
  2920. } else {
  2921. @array = map { $runtime_string." ".$_ -> [0]." ".$_ -> [1]."\n" } @{ $sth->fetchall_arrayref() };
  2922. if (@array) {
  2923. my @sp;
  2924. my $dse = 0;
  2925. my $vold;
  2926. my @sqlite_array;
  2927. foreach my $row (@array) {
  2928. @sp = split("[ \t][ \t]*", $row, 4);
  2929. my $runtime_string = $sp[0];
  2930. my $timestamp = $sp[2]?$sp[1]." ".$sp[2]:$sp[1];
  2931. my $vnew = $sp[3];
  2932. $vnew =~ tr/\n//d;
  2933. $dse = ($vold && (($vnew-$vold) > 0))?($vnew-$vold):0;
  2934. @sp = $runtime_string." ".$timestamp." ".$vnew." ".$dse."\n";
  2935. $vold = $vnew;
  2936. push(@sqlite_array, @sp);
  2937. }
  2938. @array = @sqlite_array;
  2939. }
  2940. }
  2941. if(!@array) {
  2942. if(AttrVal($name, "aggregation", "") eq "hour") {
  2943. my @rsf = split(/[" "\|":"]/,$runtime_string_first);
  2944. @array = ($runtime_string." ".$rsf[0]."_".$rsf[1]."\n");
  2945. } else {
  2946. my @rsf = split(" ",$runtime_string_first);
  2947. @array = ($runtime_string." ".$rsf[0]."\n");
  2948. }
  2949. }
  2950. push(@row_array, @array);
  2951. }
  2952. }
  2953. # SQL-Laufzeit ermitteln
  2954. my $rt = tv_interval($st);
  2955. $dbh->disconnect;
  2956. Log3 ($name, 5, "DbRep $name - raw data of row_array result:\n @row_array");
  2957. my $difflimit = AttrVal($name, "diffAccept", "20"); # legt fest, bis zu welchem Wert Differenzen akzeptiert werden (Ausreißer eliminieren)
  2958. # Berechnung diffValue aus Selektionshash
  2959. my %rh = (); # Ergebnishash, wird alle Ergebniszeilen enthalten
  2960. my %ch = (); # counthash, enthält die Anzahl der verarbeiteten Datasets pro runtime_string
  2961. my $lastruntimestring;
  2962. my $i = 1;
  2963. my $lval; # immer der letzte Wert von $value
  2964. my $rslval; # runtimestring von lval
  2965. my $uediff; # Übertragsdifferenz (Differenz zwischen letzten Wert einer Aggregationsperiode und dem ersten Wert der Folgeperiode)
  2966. my $diff_current; # Differenzwert des aktuellen Datasets
  2967. my $diff_before; # Differenzwert vorheriger Datensatz
  2968. my $rejectstr; # String der ignorierten Differenzsätze
  2969. my $diff_total; # Summenwert aller berücksichtigten Teildifferenzen
  2970. my $max = ($#row_array)+1; # Anzahl aller Listenelemente
  2971. Log3 ($name, 5, "DbRep $name - data of row_array result assigned to fields:\n");
  2972. foreach my $row (@row_array) {
  2973. my @a = split("[ \t][ \t]*", $row, 6);
  2974. my $runtime_string = decode_base64($a[0]);
  2975. $lastruntimestring = $runtime_string if ($i == 1);
  2976. my $timestamp = $a[2]?$a[1]."_".$a[2]:$a[1];
  2977. my $value = $a[3]?$a[3]:0;
  2978. my $diff = $a[4]?sprintf("%.4f",$a[4]):0;
  2979. # if ($uediff) {
  2980. # $diff = $diff + $uediff;
  2981. # Log3 ($name, 4, "DbRep $name - balance difference of $uediff between $rslval and $runtime_string");
  2982. # $uediff = 0;
  2983. # }
  2984. # Leerzeichen am Ende $timestamp entfernen
  2985. $timestamp =~ s/\s+$//g;
  2986. # Test auf $value = "numeric"
  2987. if (!looks_like_number($value)) {
  2988. $a[3] =~ s/\s+$//g;
  2989. Log3 ($name, 2, "DbRep $name - ERROR - value isn't numeric in diffValue function. Faulty dataset was \nTIMESTAMP: $timestamp, DEVICE: $device, READING: $reading, VALUE: $value.");
  2990. $err = encode_base64("Value isn't numeric. Faulty dataset was - TIMESTAMP: $timestamp, VALUE: $value", "");
  2991. return "$name|''|$device|$reading|''|''|''|$err|''";
  2992. }
  2993. Log3 ($name, 5, "DbRep $name - Runtimestring: $runtime_string, DEVICE: $device, READING: $reading, \nTIMESTAMP: $timestamp, VALUE: $value, DIFF: $diff");
  2994. # String ignorierter Zeilen erzeugen
  2995. $diff_current = $timestamp." ".$diff;
  2996. if($diff > $difflimit) {
  2997. $rejectstr .= $diff_before." -> ".$diff_current."\n";
  2998. }
  2999. $diff_before = $diff_current;
  3000. # Ergebnishash erzeugen
  3001. if ($runtime_string eq $lastruntimestring) {
  3002. if ($i == 1) {
  3003. $diff_total = $diff?$diff:0 if($diff <= $difflimit);
  3004. $rh{$runtime_string} = $runtime_string."|".$diff_total."|".$timestamp;
  3005. $ch{$runtime_string} = 1 if($value);
  3006. $lval = $value;
  3007. $rslval = $runtime_string;
  3008. }
  3009. if ($diff) {
  3010. if($diff <= $difflimit) {
  3011. $diff_total = $diff_total+$diff;
  3012. }
  3013. $rh{$runtime_string} = $runtime_string."|".$diff_total."|".$timestamp;
  3014. $ch{$runtime_string}++ if($value && $i > 1);
  3015. $lval = $value;
  3016. $rslval = $runtime_string;
  3017. }
  3018. } else {
  3019. # neuer Zeitabschnitt beginnt, ersten Value-Wert erfassen und Übertragsdifferenz bilden
  3020. $lastruntimestring = $runtime_string;
  3021. $i = 1;
  3022. $uediff = $value - $lval if($value > $lval);
  3023. $diff = $uediff;
  3024. $lval = $value if($value); # Übetrag über Perioden mit value = 0 hinweg !
  3025. $rslval = $runtime_string;
  3026. Log3 ($name, 4, "DbRep $name - balance difference of $uediff between $rslval and $runtime_string");
  3027. $diff_total = $diff?$diff:0 if($diff <= $difflimit);
  3028. $rh{$runtime_string} = $runtime_string."|".$diff_total."|".$timestamp;
  3029. $ch{$runtime_string} = 1 if($value);
  3030. $uediff = 0;
  3031. }
  3032. $i++;
  3033. }
  3034. Log3 ($name, 4, "DbRep $name - result of diffValue calculation before encoding:");
  3035. foreach my $key (sort(keys(%rh))) {
  3036. Log3 ($name, 4, "runtimestring Key: $key, value: ".$rh{$key});
  3037. }
  3038. my $ncp = DbRep_calcount($hash,\%ch);
  3039. my ($ncps,$ncpslist);
  3040. if(%$ncp) {
  3041. Log3 ($name, 3, "DbRep $name - time/aggregation periods containing only one dataset -> no diffValue calc was possible in period:");
  3042. foreach my $key (sort(keys%{$ncp})) {
  3043. Log3 ($name, 3, $key) ;
  3044. }
  3045. $ncps = join('§', %$ncp);
  3046. $ncpslist = encode_base64($ncps,"");
  3047. }
  3048. # Ergebnishash als Einzeiler zurückgeben
  3049. # ignorierte Zeilen ($diff > $difflimit)
  3050. my $rowsrej = encode_base64($rejectstr,"") if($rejectstr);
  3051. # Ergebnishash
  3052. my $rows = join('§', %rh);
  3053. # Ergebnisse in Datenbank schreiben
  3054. my ($wrt,$irowdone);
  3055. if($prop =~ /writeToDB/) {
  3056. ($wrt,$irowdone,$err) = DbRep_OutputWriteToDB($name,$device,$reading,$rows,"diff");
  3057. if ($err) {
  3058. Log3 $hash->{NAME}, 2, "DbRep $name - $err";
  3059. $err = encode_base64($err,"");
  3060. return "$name|''|$device|$reading|''|''|''|$err|''";
  3061. }
  3062. $rt = $rt+$wrt;
  3063. }
  3064. my $rowlist = encode_base64($rows,"");
  3065. # Background-Laufzeit ermitteln
  3066. my $brt = tv_interval($bst);
  3067. $rt = $rt.",".$brt;
  3068. return "$name|$rowlist|$device|$reading|$rt|$rowsrej|$ncpslist|0|$irowdone";
  3069. }
  3070. ####################################################################################################
  3071. # Auswertungsroutine der nichtblockierenden DB-Abfrage diffValue
  3072. ####################################################################################################
  3073. sub diffval_ParseDone($) {
  3074. my ($string) = @_;
  3075. my @a = split("\\|",$string);
  3076. my $hash = $defs{$a[0]};
  3077. my $name = $hash->{NAME};
  3078. my $rowlist = decode_base64($a[1]);
  3079. my $device = $a[2];
  3080. $device =~ s/[^A-Za-z\/\d_\.-]/\//g;
  3081. my $reading = $a[3];
  3082. $reading =~ s/[^A-Za-z\/\d_\.-]/\//g;
  3083. my $bt = $a[4];
  3084. my ($rt,$brt) = split(",", $bt);
  3085. my $rowsrej = $a[5]?decode_base64($a[5]):undef; # String von Datensätzen die nicht berücksichtigt wurden (diff Schwellenwert Überschreitung)
  3086. my $ncpslist = decode_base64($a[6]); # Hash von Perioden die nicht kalkuliert werden konnten "no calc in period"
  3087. my $err = $a[7]?decode_base64($a[7]):undef;
  3088. my $irowdone = $a[8];
  3089. my $reading_runtime_string;
  3090. my $difflimit = AttrVal($name, "diffAccept", "20"); # legt fest, bis zu welchem Wert Differenzen akzeptoert werden (Ausreißer eliminieren)AttrVal($name, "diffAccept", "20");
  3091. if ($err) {
  3092. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  3093. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  3094. delete($hash->{HELPER}{RUNNING_PID});
  3095. return;
  3096. }
  3097. # only for this block because of warnings if details of readings are not set
  3098. no warnings 'uninitialized';
  3099. # Auswertung hashes für state-Warning
  3100. $rowsrej =~ s/_/ /g;
  3101. Log3 ($name, 3, "DbRep $name -> data ignored while calc diffValue due to threshold overrun (diffAccept = $difflimit): \n$rowsrej")
  3102. if($rowsrej);
  3103. $rowsrej =~ s/\n/ \|\| /g;
  3104. my %ncp = split("§", $ncpslist);
  3105. my $ncpstr;
  3106. if(%ncp) {
  3107. foreach my $ncpkey (sort(keys(%ncp))) {
  3108. $ncpstr .= $ncpkey." || ";
  3109. }
  3110. }
  3111. # Readingaufbereitung
  3112. my %rh = split("§", $rowlist);
  3113. Log3 ($name, 4, "DbRep $name - result of diffValue calculation after decoding:");
  3114. foreach my $key (sort(keys(%rh))) {
  3115. Log3 ($name, 4, "DbRep $name - runtimestring Key: $key, value: ".$rh{$key});
  3116. }
  3117. readingsBeginUpdate($hash);
  3118. foreach my $key (sort(keys(%rh))) {
  3119. my @k = split("\\|",$rh{$key});
  3120. my $rts = $k[2]."__";
  3121. $rts =~ s/:/-/g; # substituieren unsupported characters -> siehe fhem.pl
  3122. if (AttrVal($hash->{NAME}, "readingNameMap", "")) {
  3123. $reading_runtime_string = $rts.AttrVal($hash->{NAME}, "readingNameMap", "")."__".$k[0];
  3124. } else {
  3125. my $ds = $device."__" if ($device);
  3126. my $rds = $reading."__" if ($reading);
  3127. $reading_runtime_string = $rts.$ds.$rds."DIFF__".$k[0];
  3128. }
  3129. my $rv = $k[1];
  3130. ReadingsBulkUpdateValue ($hash, $reading_runtime_string, $rv?sprintf("%.4f",$rv):"-");
  3131. }
  3132. ReadingsBulkUpdateValue ($hash, "db_lines_processed", $irowdone) if($hash->{LASTCMD} =~ /writeToDB/);
  3133. ReadingsBulkUpdateValue ($hash, "diff_overrun_limit_".$difflimit, $rowsrej) if($rowsrej);
  3134. ReadingsBulkUpdateValue ($hash, "less_data_in_period", $ncpstr) if($ncpstr);
  3135. ReadingsBulkUpdateTimeState($hash,$brt,$rt,($ncpstr||$rowsrej)?"Warning":"done");
  3136. readingsEndUpdate($hash, 1);
  3137. delete($hash->{HELPER}{RUNNING_PID});
  3138. return;
  3139. }
  3140. ####################################################################################################
  3141. # nichtblockierende DB-Abfrage sumValue
  3142. ####################################################################################################
  3143. sub sumval_DoParse($) {
  3144. my ($string) = @_;
  3145. my ($name,$device,$reading,$prop,$ts) = split("\\§", $string);
  3146. my $hash = $defs{$name};
  3147. my $dbloghash = $hash->{dbloghash};
  3148. my $dbconn = $dbloghash->{dbconn};
  3149. my $dbuser = $dbloghash->{dbuser};
  3150. my $dblogname = $dbloghash->{NAME};
  3151. my $dbpassword = $attr{"sec$dblogname"}{secret};
  3152. my ($dbh,$sql,$sth,$err,$selspec);
  3153. # Background-Startzeit
  3154. my $bst = [gettimeofday];
  3155. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1 });};
  3156. if ($@) {
  3157. $err = encode_base64($@,"");
  3158. Log3 ($name, 2, "DbRep $name - $@");
  3159. return "$name|''|$device|$reading|''|$err|''";
  3160. }
  3161. # only for this block because of warnings if details of readings are not set
  3162. no warnings 'uninitialized';
  3163. # ist Zeiteingrenzung und/oder Aggregation gesetzt ? (wenn ja -> "?" in SQL sonst undef)
  3164. my ($IsTimeSet,$IsAggrSet) = DbRep_checktimeaggr($hash);
  3165. Log3 ($name, 5, "DbRep $name - IsTimeSet: $IsTimeSet, IsAggrSet: $IsAggrSet");
  3166. # Timestampstring to Array
  3167. my @ts = split("\\|", $ts);
  3168. Log3 ($name, 5, "DbRep $name - Timestamp-Array: \n@ts");
  3169. #vorbereiten der DB-Abfrage, DB-Modell-abhaengig
  3170. if ($dbloghash->{MODEL} eq "POSTGRESQL") {
  3171. $selspec = "SUM(VALUE::numeric)";
  3172. } elsif ($dbloghash->{MODEL} eq "MYSQL") {
  3173. $selspec = "SUM(VALUE)";
  3174. } elsif ($dbloghash->{MODEL} eq "SQLITE") {
  3175. $selspec = "SUM(VALUE)";
  3176. } else {
  3177. $selspec = "SUM(VALUE)";
  3178. }
  3179. # SQL-Startzeit
  3180. my $st = [gettimeofday];
  3181. # DB-Abfrage zeilenweise für jeden Array-Eintrag
  3182. my $arrstr;
  3183. foreach my $row (@ts) {
  3184. my @a = split("#", $row);
  3185. my $runtime_string = $a[0];
  3186. my $runtime_string_first = $a[1];
  3187. my $runtime_string_next = $a[2];
  3188. if ($IsTimeSet || $IsAggrSet) {
  3189. $sql = DbRep_createSelectSql($hash,"history",$selspec,$device,$reading,"'$runtime_string_first'","'$runtime_string_next'",'');
  3190. } else {
  3191. $sql = DbRep_createSelectSql($hash,"history",$selspec,$device,$reading,undef,undef,'');
  3192. }
  3193. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  3194. eval{ $sth = $dbh->prepare($sql);
  3195. $sth->execute();
  3196. };
  3197. if ($@) {
  3198. $err = encode_base64($@,"");
  3199. Log3 ($name, 2, "DbRep $name - $@");
  3200. $dbh->disconnect;
  3201. return "$name|''|$device|$reading|''|$err|''";
  3202. }
  3203. # DB-Abfrage -> Ergebnis in @arr aufnehmen
  3204. my @line = $sth->fetchrow_array();
  3205. Log3 ($name, 5, "DbRep $name - SQL result: $line[0]") if($line[0]);
  3206. if(AttrVal($name, "aggregation", "") eq "hour") {
  3207. my @rsf = split(/[" "\|":"]/,$runtime_string_first);
  3208. $arrstr .= $runtime_string."#".$line[0]."#".$rsf[0]."_".$rsf[1]."|";
  3209. } else {
  3210. my @rsf = split(" ",$runtime_string_first);
  3211. $arrstr .= $runtime_string."#".$line[0]."#".$rsf[0]."|";
  3212. }
  3213. }
  3214. $sth->finish;
  3215. $dbh->disconnect;
  3216. # SQL-Laufzeit ermitteln
  3217. my $rt = tv_interval($st);
  3218. # Ergebnisse in Datenbank schreiben
  3219. my ($wrt,$irowdone);
  3220. if($prop =~ /writeToDB/) {
  3221. ($wrt,$irowdone,$err) = DbRep_OutputWriteToDB($name,$device,$reading,$arrstr,"sum");
  3222. if ($err) {
  3223. Log3 $hash->{NAME}, 2, "DbRep $name - $err";
  3224. $err = encode_base64($err,"");
  3225. return "$name|''|$device|$reading|''|$err|''";
  3226. }
  3227. $rt = $rt+$wrt;
  3228. }
  3229. # Daten müssen als Einzeiler zurückgegeben werden
  3230. $arrstr = encode_base64($arrstr,"");
  3231. # Background-Laufzeit ermitteln
  3232. my $brt = tv_interval($bst);
  3233. $rt = $rt.",".$brt;
  3234. return "$name|$arrstr|$device|$reading|$rt|0|$irowdone";
  3235. }
  3236. ####################################################################################################
  3237. # Auswertungsroutine der nichtblockierenden DB-Abfrage sumValue
  3238. ####################################################################################################
  3239. sub sumval_ParseDone($) {
  3240. my ($string) = @_;
  3241. my @a = split("\\|",$string);
  3242. my $hash = $defs{$a[0]};
  3243. my $name = $hash->{NAME};
  3244. my $arrstr = decode_base64($a[1]);
  3245. my $device = $a[2];
  3246. $device =~ s/[^A-Za-z\/\d_\.-]/\//g;
  3247. my $reading = $a[3];
  3248. $reading =~ s/[^A-Za-z\/\d_\.-]/\//g;
  3249. my $bt = $a[4];
  3250. my ($rt,$brt) = split(",", $bt);
  3251. my $err = $a[5]?decode_base64($a[5]):undef;
  3252. my $irowdone = $a[6];
  3253. my $reading_runtime_string;
  3254. if ($err) {
  3255. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  3256. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  3257. delete($hash->{HELPER}{RUNNING_PID});
  3258. return;
  3259. }
  3260. # only for this block because of warnings if details of readings are not set
  3261. no warnings 'uninitialized';
  3262. # Readingaufbereitung
  3263. readingsBeginUpdate($hash);
  3264. my @arr = split("\\|", $arrstr);
  3265. foreach my $row (@arr) {
  3266. my @a = split("#", $row);
  3267. my $runtime_string = $a[0];
  3268. my $c = $a[1];
  3269. my $rsf = $a[2]."__";
  3270. if (AttrVal($hash->{NAME}, "readingNameMap", "")) {
  3271. $reading_runtime_string = $rsf.AttrVal($hash->{NAME}, "readingNameMap", "")."__".$runtime_string;
  3272. } else {
  3273. my $ds = $device."__" if ($device);
  3274. my $rds = $reading."__" if ($reading);
  3275. $reading_runtime_string = $rsf.$ds.$rds."SUM__".$runtime_string;
  3276. }
  3277. ReadingsBulkUpdateValue ($hash, $reading_runtime_string, $c?sprintf("%.4f",$c):"-");
  3278. }
  3279. ReadingsBulkUpdateValue ($hash, "db_lines_processed", $irowdone) if($hash->{LASTCMD} =~ /writeToDB/);
  3280. ReadingsBulkUpdateTimeState($hash,$brt,$rt,"done");
  3281. readingsEndUpdate($hash, 1);
  3282. delete($hash->{HELPER}{RUNNING_PID});
  3283. return;
  3284. }
  3285. ####################################################################################################
  3286. # nichtblockierendes DB delete
  3287. ####################################################################################################
  3288. sub del_DoParse($) {
  3289. my ($string) = @_;
  3290. my ($name,$table,$device,$reading,$runtime_string_first,$runtime_string_next) = split("\\|", $string);
  3291. my $hash = $defs{$name};
  3292. my $dbloghash = $hash->{dbloghash};
  3293. my $dbconn = $dbloghash->{dbconn};
  3294. my $dbuser = $dbloghash->{dbuser};
  3295. my $dblogname = $dbloghash->{NAME};
  3296. my $dbpassword = $attr{"sec$dblogname"}{secret};
  3297. my ($dbh,$sql,$sth,$err,$rows);
  3298. # Background-Startzeit
  3299. my $bst = [gettimeofday];
  3300. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoCommit => 1, AutoInactiveDestroy => 1 });};
  3301. if ($@) {
  3302. $err = encode_base64($@,"");
  3303. Log3 ($name, 2, "DbRep $name - $@");
  3304. return "$name|''|''|$err|''|''|''";
  3305. }
  3306. # ist Zeiteingrenzung und/oder Aggregation gesetzt ? (wenn ja -> "?" in SQL sonst undef)
  3307. my ($IsTimeSet,$IsAggrSet) = DbRep_checktimeaggr($hash);
  3308. Log3 ($name, 5, "DbRep $name - IsTimeSet: $IsTimeSet, IsAggrSet: $IsAggrSet");
  3309. # SQL zusammenstellen für DB-Operation
  3310. if ($IsTimeSet || $IsAggrSet) {
  3311. $sql = DbRep_createDeleteSql($hash,$table,$device,$reading,$runtime_string_first,$runtime_string_next,'');
  3312. } else {
  3313. $sql = DbRep_createDeleteSql($hash,$table,$device,$reading,undef,undef,'');
  3314. }
  3315. $sth = $dbh->prepare($sql);
  3316. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  3317. # SQL-Startzeit
  3318. my $st = [gettimeofday];
  3319. eval {$sth->execute();};
  3320. if ($@) {
  3321. $err = encode_base64($@,"");
  3322. Log3 ($name, 2, "DbRep $name - $@");
  3323. $dbh->disconnect;
  3324. return "$name|''|''|$err|''|''|''";
  3325. }
  3326. $rows = $sth->rows;
  3327. $dbh->commit() if(!$dbh->{AutoCommit});
  3328. $dbh->disconnect;
  3329. # SQL-Laufzeit ermitteln
  3330. my $rt = tv_interval($st);
  3331. Log3 ($name, 5, "DbRep $name - Number of deleted rows: $rows");
  3332. # Background-Laufzeit ermitteln
  3333. my $brt = tv_interval($bst);
  3334. $rt = $rt.",".$brt;
  3335. return "$name|$rows|$rt|0|$table|$device|$reading";
  3336. }
  3337. ####################################################################################################
  3338. # Auswertungsroutine DB delete
  3339. ####################################################################################################
  3340. sub del_ParseDone($) {
  3341. my ($string) = @_;
  3342. my @a = split("\\|",$string);
  3343. my $hash = $defs{$a[0]};
  3344. my $name = $hash->{NAME};
  3345. my $rows = $a[1];
  3346. my $bt = $a[2];
  3347. my ($rt,$brt) = split(",", $bt);
  3348. my $err = $a[3]?decode_base64($a[3]):undef;
  3349. my $table = $a[4];
  3350. my $device = $a[5];
  3351. $device =~ s/[^A-Za-z\/\d_\.-]/\//g;
  3352. my $reading = $a[6];
  3353. $reading =~ s/[^A-Za-z\/\d_\.-]/\//g;
  3354. my $erread;
  3355. # Befehl nach Procedure ausführen
  3356. $erread = DbRep_afterproc($hash, "delEntries");
  3357. if ($err) {
  3358. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  3359. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  3360. delete($hash->{HELPER}{RUNNING_PID});
  3361. return;
  3362. }
  3363. # only for this block because of warnings if details of readings are not set
  3364. no warnings 'uninitialized';
  3365. my ($reading_runtime_string, $ds, $rds);
  3366. if (AttrVal($hash->{NAME}, "readingNameMap", "")) {
  3367. $reading_runtime_string = AttrVal($hash->{NAME}, "readingNameMap", "")."--DELETED_ROWS--";
  3368. } else {
  3369. $ds = $device."--" if ($device && $table ne "current");
  3370. $rds = $reading."--" if ($reading && $table ne "current");
  3371. $reading_runtime_string = $ds.$rds."--DELETED_ROWS_".uc($table)."--";
  3372. }
  3373. readingsBeginUpdate($hash);
  3374. ReadingsBulkUpdateValue ($hash, $reading_runtime_string, $rows);
  3375. $rows = ($table eq "current")?$rows:$ds.$rds.$rows;
  3376. Log3 ($name, 3, "DbRep $name - Entries of $hash->{DATABASE}.$table deleted: $rows");
  3377. my $state = $erread?$erread:"done";
  3378. ReadingsBulkUpdateTimeState($hash,$brt,$rt,$state);
  3379. readingsEndUpdate($hash, 1);
  3380. delete($hash->{HELPER}{RUNNING_PID});
  3381. return;
  3382. }
  3383. ####################################################################################################
  3384. # nichtblockierendes DB insert
  3385. ####################################################################################################
  3386. sub insert_Push($) {
  3387. my ($name) = @_;
  3388. my $hash = $defs{$name};
  3389. my $dbloghash = $hash->{dbloghash};
  3390. my $dbconn = $dbloghash->{dbconn};
  3391. my $dbuser = $dbloghash->{dbuser};
  3392. my $dblogname = $dbloghash->{NAME};
  3393. my $dbpassword = $attr{"sec$dblogname"}{secret};
  3394. my $utf8 = defined($hash->{UTF8})?$hash->{UTF8}:0;
  3395. my ($err,$sth);
  3396. # Background-Startzeit
  3397. my $bst = [gettimeofday];
  3398. my $dbh;
  3399. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoCommit => 1, AutoInactiveDestroy => 1, mysql_enable_utf8 => $utf8 });};
  3400. if ($@) {
  3401. $err = encode_base64($@,"");
  3402. Log3 ($name, 2, "DbRep $name - $@");
  3403. return "$name|''|''|$err";
  3404. }
  3405. # check ob PK verwendet wird, @usepkx?Anzahl der Felder im PK:0 wenn kein PK, $pkx?Namen der Felder:none wenn kein PK
  3406. my ($usepkh,$usepkc,$pkh,$pkc) = DbRep_checkUsePK($hash,$dbloghash,$dbh);
  3407. my $i_timestamp = $hash->{HELPER}{I_TIMESTAMP};
  3408. my $i_device = $hash->{HELPER}{I_DEVICE};
  3409. my $i_type = $hash->{HELPER}{I_TYPE};
  3410. my $i_event = $hash->{HELPER}{I_EVENT};
  3411. my $i_reading = $hash->{HELPER}{I_READING};
  3412. my $i_value = $hash->{HELPER}{I_VALUE};
  3413. my $i_unit = $hash->{HELPER}{I_UNIT} ? $hash->{HELPER}{I_UNIT} : " ";
  3414. # SQL zusammenstellen für DB-Operation
  3415. 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");
  3416. # SQL-Startzeit
  3417. my $st = [gettimeofday];
  3418. # insert history mit/ohne primary key
  3419. if ($usepkh && $dbloghash->{MODEL} eq 'MYSQL') {
  3420. eval { $sth = $dbh->prepare("INSERT IGNORE INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  3421. } elsif ($usepkh && $dbloghash->{MODEL} eq 'SQLITE') {
  3422. eval { $sth = $dbh->prepare("INSERT OR IGNORE INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  3423. } elsif ($usepkh && $dbloghash->{MODEL} eq 'POSTGRESQL') {
  3424. eval { $sth = $dbh->prepare("INSERT INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?) ON CONFLICT DO NOTHING"); };
  3425. } else {
  3426. eval { $sth = $dbh->prepare("INSERT INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  3427. }
  3428. if ($@) {
  3429. $err = encode_base64($@,"");
  3430. Log3 ($name, 2, "DbRep $name - $@");
  3431. $dbh->disconnect();
  3432. return "$name|''|''|$err";
  3433. }
  3434. $dbh->begin_work();
  3435. eval {$sth->execute($i_timestamp, $i_device, $i_type, $i_event, $i_reading, $i_value, $i_unit);};
  3436. my $irow;
  3437. if ($@) {
  3438. $err = encode_base64($@,"");
  3439. Log3 ($name, 2, "DbRep $name - Insert new dataset into database failed".($usepkh?" (possible PK violation) ":": ")."$@");
  3440. $dbh->rollback();
  3441. $dbh->disconnect();
  3442. return "$name|''|''|$err";
  3443. } else {
  3444. $dbh->commit();
  3445. $irow = $sth->rows;
  3446. $dbh->disconnect();
  3447. }
  3448. # SQL-Laufzeit ermitteln
  3449. my $rt = tv_interval($st);
  3450. # Background-Laufzeit ermitteln
  3451. my $brt = tv_interval($bst);
  3452. $rt = $rt.",".$brt;
  3453. return "$name|$irow|$rt|0";
  3454. }
  3455. ####################################################################################################
  3456. # Auswertungsroutine DB insert
  3457. ####################################################################################################
  3458. sub insert_Done($) {
  3459. my ($string) = @_;
  3460. my @a = split("\\|",$string);
  3461. my $hash = $defs{$a[0]};
  3462. my $name = $hash->{NAME};
  3463. my $irow = $a[1];
  3464. my $bt = $a[2];
  3465. my ($rt,$brt) = split(",", $bt);
  3466. my $err = $a[3]?decode_base64($a[3]):undef;
  3467. my $i_timestamp = delete $hash->{HELPER}{I_TIMESTAMP};
  3468. my $i_device = delete $hash->{HELPER}{I_DEVICE};
  3469. my $i_type = delete $hash->{HELPER}{I_TYPE};
  3470. my $i_event = delete $hash->{HELPER}{I_EVENT};
  3471. my $i_reading = delete $hash->{HELPER}{I_READING};
  3472. my $i_value = delete $hash->{HELPER}{I_VALUE};
  3473. my $i_unit = delete $hash->{HELPER}{I_UNIT};
  3474. if ($err) {
  3475. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  3476. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  3477. delete($hash->{HELPER}{RUNNING_PID});
  3478. return;
  3479. }
  3480. # only for this block because of warnings if details of readings are not set
  3481. no warnings 'uninitialized';
  3482. readingsBeginUpdate($hash);
  3483. ReadingsBulkUpdateValue ($hash, "number_lines_inserted", $irow);
  3484. ReadingsBulkUpdateValue ($hash, "data_inserted", $i_timestamp.", ".$i_device.", ".$i_type.", ".$i_event.", ".$i_reading.", ".$i_value.", ".$i_unit);
  3485. ReadingsBulkUpdateTimeState($hash,$brt,$rt,"done");
  3486. readingsEndUpdate($hash, 1);
  3487. 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");
  3488. delete($hash->{HELPER}{RUNNING_PID});
  3489. return;
  3490. }
  3491. ####################################################################################################
  3492. # Current-Tabelle mit Device,Reading Kombinationen aus history auffüllen
  3493. ####################################################################################################
  3494. sub currentfillup_Push($) {
  3495. my ($string) = @_;
  3496. my ($name,$device,$reading,$runtime_string_first,$runtime_string_next) = split("\\|", $string);
  3497. my $hash = $defs{$name};
  3498. my $dbloghash = $hash->{dbloghash};
  3499. my $dbconn = $dbloghash->{dbconn};
  3500. my $dbuser = $dbloghash->{dbuser};
  3501. my $dblogname = $dbloghash->{NAME};
  3502. my $dbpassword = $attr{"sec$dblogname"}{secret};
  3503. my $utf8 = defined($hash->{UTF8})?$hash->{UTF8}:0;
  3504. my ($err,$sth,$sql,$devs,$danz,$ranz);
  3505. # Background-Startzeit
  3506. my $bst = [gettimeofday];
  3507. my $dbh;
  3508. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoCommit => 1, AutoInactiveDestroy => 1, mysql_enable_utf8 => $utf8 });};
  3509. if ($@) {
  3510. $err = encode_base64($@,"");
  3511. Log3 ($name, 2, "DbRep $name - $@");
  3512. return "$name|''|''|$err|''|''";
  3513. }
  3514. # check ob PK verwendet wird, @usepkx?Anzahl der Felder im PK:0 wenn kein PK, $pkx?Namen der Felder:none wenn kein PK
  3515. my ($usepkh,$usepkc,$pkh,$pkc) = DbRep_checkUsePK($hash,$dbloghash,$dbh);
  3516. # ist Zeiteingrenzung und/oder Aggregation gesetzt ? (wenn ja -> "?" in SQL sonst undef)
  3517. my ($IsTimeSet,$IsAggrSet) = DbRep_checktimeaggr($hash);
  3518. Log3 ($name, 5, "DbRep $name - IsTimeSet: $IsTimeSet, IsAggrSet: $IsAggrSet");
  3519. ($devs,$danz,$reading,$ranz) = DbRep_specsForSql($hash,$device,$reading);
  3520. # SQL-Startzeit
  3521. my $st = [gettimeofday];
  3522. # insert history mit/ohne primary key
  3523. # SQL zusammenstellen für DB-Operation
  3524. if ($usepkc && $dbloghash->{MODEL} eq 'MYSQL') {
  3525. $sql = "INSERT IGNORE INTO current (TIMESTAMP,DEVICE,READING) SELECT timestamp,device,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 .= "1 ";
  3536. }
  3537. $sql .= "group by timestamp,device,reading ;";
  3538. } elsif ($usepkc && $dbloghash->{MODEL} eq 'SQLITE') {
  3539. $sql = "INSERT OR IGNORE INTO current (TIMESTAMP,DEVICE,READING) SELECT timestamp,device,reading FROM history where ";
  3540. $sql .= "DEVICE LIKE '$devs' AND " if($danz <= 1 && $devs !~ m(^%$) && $devs =~ m(\%));
  3541. $sql .= "DEVICE = '$devs' AND " if($danz <= 1 && $devs !~ m(\%));
  3542. $sql .= "DEVICE IN ($devs) AND " if($danz > 1);
  3543. $sql .= "READING LIKE '$reading' AND " if($ranz <= 1 && $reading !~ m(^%$) && $reading =~ m(\%));
  3544. $sql .= "READING = '$reading' AND " if($ranz <= 1 && $reading !~ m(\%));
  3545. $sql .= "READING IN ($reading) AND " if($ranz > 1);
  3546. if ($IsTimeSet) {
  3547. $sql .= "TIMESTAMP >= '$runtime_string_first' AND TIMESTAMP < '$runtime_string_next' ";
  3548. } else {
  3549. $sql .= "1 ";
  3550. }
  3551. $sql .= "group by timestamp,device,reading ;";
  3552. } elsif ($usepkc && $dbloghash->{MODEL} eq 'POSTGRESQL') {
  3553. $sql = "INSERT INTO current (DEVICE,TIMESTAMP,READING) SELECT device, (array_agg(timestamp ORDER BY reading ASC))[1], reading FROM history where ";
  3554. $sql .= "DEVICE LIKE '$devs' AND " if($danz <= 1 && $devs !~ m(^%$) && $devs =~ m(\%));
  3555. $sql .= "DEVICE = '$devs' AND " if($danz <= 1 && $devs !~ m(\%));
  3556. $sql .= "DEVICE IN ($devs) AND " if($danz > 1);
  3557. $sql .= "READING LIKE '$reading' AND " if($ranz <= 1 && $reading !~ m(^%$) && $reading =~ m(\%));
  3558. $sql .= "READING = '$reading' AND " if($ranz <= 1 && $reading !~ m(\%));
  3559. $sql .= "READING IN ($reading) AND " if($ranz > 1);
  3560. if ($IsTimeSet) {
  3561. $sql .= "TIMESTAMP >= '$runtime_string_first' AND TIMESTAMP < '$runtime_string_next' ";
  3562. } else {
  3563. $sql .= "true ";
  3564. }
  3565. $sql .= "group by device,reading ON CONFLICT ($pkc) DO NOTHING; ";
  3566. } else {
  3567. if($dbloghash->{MODEL} ne 'POSTGRESQL') {
  3568. # MySQL und SQLite
  3569. $sql = "INSERT INTO current (TIMESTAMP,DEVICE,READING) SELECT timestamp,device,reading FROM history where ";
  3570. $sql .= "DEVICE LIKE '$devs' AND " if($danz <= 1 && $devs !~ m(^%$) && $devs =~ m(\%));
  3571. $sql .= "DEVICE = '$devs' AND " if($danz <= 1 && $devs !~ m(\%));
  3572. $sql .= "DEVICE IN ($devs) AND " if($danz > 1);
  3573. $sql .= "READING LIKE '$reading' AND " if($ranz <= 1 && $reading !~ m(^%$) && $reading =~ m(\%));
  3574. $sql .= "READING = '$reading' AND " if($ranz <= 1 && $reading !~ m(\%));
  3575. $sql .= "READING IN ($reading) AND " if($ranz > 1);
  3576. if ($IsTimeSet) {
  3577. $sql .= "TIMESTAMP >= '$runtime_string_first' AND TIMESTAMP < '$runtime_string_next' ";
  3578. } else {
  3579. $sql .= "1 ";
  3580. }
  3581. $sql .= "group by device,reading ;";
  3582. } else {
  3583. # PostgreSQL
  3584. $sql = "INSERT INTO current (DEVICE,TIMESTAMP,READING) SELECT device, (array_agg(timestamp ORDER BY reading ASC))[1], reading FROM history where ";
  3585. $sql .= "DEVICE LIKE '$devs' AND " if($danz <= 1 && $devs !~ m(^%$) && $devs =~ m(\%));
  3586. $sql .= "DEVICE = '$devs' AND " if($danz <= 1 && $devs !~ m(\%));
  3587. $sql .= "DEVICE IN ($devs) AND " if($danz > 1);
  3588. $sql .= "READING LIKE '$reading' AND " if($ranz <= 1 && $reading !~ m(^%$) && $reading =~ m(\%));
  3589. $sql .= "READING = '$reading' AND " if($ranz <= 1 && $reading !~ m(\%));
  3590. $sql .= "READING IN ($reading) AND " if($ranz > 1);
  3591. if ($IsTimeSet) {
  3592. $sql .= "TIMESTAMP >= '$runtime_string_first' AND TIMESTAMP < '$runtime_string_next' ";
  3593. } else {
  3594. $sql .= "true ";
  3595. }
  3596. $sql .= "group by device,reading;";
  3597. }
  3598. }
  3599. # Log SQL Statement
  3600. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  3601. eval { $sth = $dbh->prepare($sql); };
  3602. if ($@) {
  3603. $err = encode_base64($@,"");
  3604. Log3 ($name, 2, "DbRep $name - $@");
  3605. $dbh->disconnect();
  3606. return "$name|''|''|$err|''|''";
  3607. }
  3608. my $irow;
  3609. $dbh->begin_work();
  3610. eval {$sth->execute();};
  3611. if ($@) {
  3612. $err = encode_base64($@,"");
  3613. Log3 ($name, 2, "DbRep $name - Insert new dataset into database failed".($usepkh?" (possible PK violation) ":": ")."$@");
  3614. $dbh->rollback();
  3615. $dbh->disconnect();
  3616. return "$name|''|''|$err|''|''";
  3617. } else {
  3618. $dbh->commit();
  3619. $irow = $sth->rows;
  3620. $dbh->disconnect();
  3621. }
  3622. # SQL-Laufzeit ermitteln
  3623. my $rt = tv_interval($st);
  3624. # Background-Laufzeit ermitteln
  3625. my $brt = tv_interval($bst);
  3626. $rt = $rt.",".$brt;
  3627. return "$name|$irow|$rt|0|$device|$reading";
  3628. }
  3629. ####################################################################################################
  3630. # Auswertungsroutine Current-Tabelle auffüllen
  3631. ####################################################################################################
  3632. sub currentfillup_Done($) {
  3633. my ($string) = @_;
  3634. my @a = split("\\|",$string);
  3635. my $hash = $defs{$a[0]};
  3636. my $name = $hash->{NAME};
  3637. my $irow = $a[1];
  3638. my $bt = $a[2];
  3639. my ($rt,$brt) = split(",", $bt);
  3640. my $err = $a[3]?decode_base64($a[3]):undef;
  3641. my $device = $a[4];
  3642. my $reading = $a[5];
  3643. undef $device if ($device =~ m(^%$));
  3644. undef $reading if ($reading =~ m(^%$));
  3645. if ($err) {
  3646. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  3647. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  3648. delete($hash->{HELPER}{RUNNING_PID});
  3649. return;
  3650. }
  3651. # only for this block because of warnings if details of readings are not set
  3652. no warnings 'uninitialized';
  3653. my $rowstr;
  3654. $rowstr = $irow if(!$device && !$reading);
  3655. $rowstr = $irow." - limited by device: ".$device if($device && !$reading);
  3656. $rowstr = $irow." - limited by reading: ".$reading if(!$device && $reading);
  3657. $rowstr = $irow." - limited by device: ".$device." and reading: ".$reading if($device && $reading);
  3658. readingsBeginUpdate($hash);
  3659. ReadingsBulkUpdateValue($hash, "number_lines_inserted", $rowstr);
  3660. ReadingsBulkUpdateTimeState($hash,$brt,$rt,"done");
  3661. readingsEndUpdate($hash, 1);
  3662. Log3 ($name, 3, "DbRep $name - Table '$hash->{DATABASE}'.'current' filled up with rows: $rowstr");
  3663. delete($hash->{HELPER}{RUNNING_PID});
  3664. return;
  3665. }
  3666. ####################################################################################################
  3667. # nichtblockierendes DB deviceRename / readingRename
  3668. ####################################################################################################
  3669. sub change_Push($) {
  3670. my ($string) = @_;
  3671. my ($name,$device,$reading,$runtime_string_first,$runtime_string_next) = split("\\|", $string);
  3672. my $hash = $defs{$name};
  3673. my $dbloghash = $hash->{dbloghash};
  3674. my $dbconn = $dbloghash->{dbconn};
  3675. my $dbuser = $dbloghash->{dbuser};
  3676. my $dblogname = $dbloghash->{NAME};
  3677. my $dbpassword = $attr{"sec$dblogname"}{secret};
  3678. my $table = "history";
  3679. my ($dbh,$err,$sql);
  3680. # Background-Startzeit
  3681. my $bst = [gettimeofday];
  3682. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoCommit => 1, AutoInactiveDestroy => 1 });};
  3683. if ($@) {
  3684. $err = encode_base64($@,"");
  3685. Log3 ($name, 2, "DbRep $name - $@");
  3686. return "$name|''|''|$err";
  3687. }
  3688. my $renmode = $hash->{HELPER}{RENMODE};
  3689. # SQL-Startzeit
  3690. my $st = [gettimeofday];
  3691. my ($sth,$old,$new);
  3692. eval { $dbh->begin_work() if($dbh->{AutoCommit}); }; # Transaktion wenn gewünscht und autocommit ein
  3693. if ($@) {
  3694. Log3($name, 2, "DbRep $name -> Error start transaction - $@");
  3695. }
  3696. if ($renmode eq "devren") {
  3697. $old = delete $hash->{HELPER}{OLDDEV};
  3698. $new = delete $hash->{HELPER}{NEWDEV};
  3699. # SQL zusammenstellen für DB-Operation
  3700. Log3 ($name, 5, "DbRep $name -> Rename old device name \"$old\" to new device name \"$new\" in database $dblogname ");
  3701. # prepare DB operation
  3702. $old =~ s/'/''/g; # escape ' with ''
  3703. $new =~ s/'/''/g; # escape ' with ''
  3704. $sql = "UPDATE history SET TIMESTAMP=TIMESTAMP,DEVICE='$new' WHERE DEVICE='$old'; ";
  3705. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  3706. $sth = $dbh->prepare($sql) ;
  3707. } elsif ($renmode eq "readren") {
  3708. $old = delete $hash->{HELPER}{OLDREAD};
  3709. $new = delete $hash->{HELPER}{NEWREAD};
  3710. # SQL zusammenstellen für DB-Operation
  3711. Log3 ($name, 5, "DbRep $name -> Rename old reading name \"$old\" to new reading name \"$new\" in database $dblogname ");
  3712. # prepare DB operation
  3713. $old =~ s/'/''/g; # escape ' with ''
  3714. $new =~ s/'/''/g; # escape ' with ''
  3715. $sql = "UPDATE history SET TIMESTAMP=TIMESTAMP,READING='$new' WHERE READING='$old'; ";
  3716. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  3717. $sth = $dbh->prepare($sql) ;
  3718. }
  3719. $old =~ s/''/'/g; # escape back
  3720. $new =~ s/''/'/g; # escape back
  3721. my $urow;
  3722. eval { $sth->execute(); };
  3723. if ($@) {
  3724. $err = encode_base64($@,"");
  3725. my $m = ($renmode eq "devren")?"device":"reading";
  3726. Log3 ($name, 2, "DbRep $name - Failed to rename old $m name \"$old\" to new $m name \"$new\": $@");
  3727. $dbh->rollback() if(!$dbh->{AutoCommit});
  3728. $dbh->disconnect();
  3729. return "$name|''|''|$err";
  3730. } else {
  3731. $dbh->commit() if(!$dbh->{AutoCommit});
  3732. $urow = $sth->rows;
  3733. $dbh->disconnect();
  3734. }
  3735. # SQL-Laufzeit ermitteln
  3736. my $rt = tv_interval($st);
  3737. # Background-Laufzeit ermitteln
  3738. my $brt = tv_interval($bst);
  3739. $rt = $rt.",".$brt;
  3740. return "$name|$urow|$rt|0|$old|$new";
  3741. }
  3742. ####################################################################################################
  3743. # nichtblockierendes DB deviceRename / readingRename
  3744. ####################################################################################################
  3745. sub changeval_Push($) {
  3746. my ($string) = @_;
  3747. my ($name,$device,$reading,$runtime_string_first,$runtime_string_next,$ts) = split("\\§", $string);
  3748. my $hash = $defs{$name};
  3749. my $dbloghash = $hash->{dbloghash};
  3750. my $dbconn = $dbloghash->{dbconn};
  3751. my $dbuser = $dbloghash->{dbuser};
  3752. my $dblogname = $dbloghash->{NAME};
  3753. my $dbpassword = $attr{"sec$dblogname"}{secret};
  3754. my $table = "history";
  3755. my $complex = $hash->{HELPER}{COMPLEX}; # einfache oder komplexe Werteersetzung
  3756. my ($dbh,$err,$sql,$urow);
  3757. # Background-Startzeit
  3758. my $bst = [gettimeofday];
  3759. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoCommit => 1, AutoInactiveDestroy => 1 });};
  3760. if ($@) {
  3761. $err = encode_base64($@,"");
  3762. Log3 ($name, 2, "DbRep $name - $@");
  3763. return "$name|''|''|$err";
  3764. }
  3765. # ist Zeiteingrenzung und/oder Aggregation gesetzt ? (wenn ja -> "?" in SQL sonst undef)
  3766. my ($IsTimeSet,$IsAggrSet) = DbRep_checktimeaggr($hash);
  3767. Log3 ($name, 5, "DbRep $name - IsTimeSet: $IsTimeSet, IsAggrSet: $IsAggrSet");
  3768. # SQL-Startzeit
  3769. my $st = [gettimeofday];
  3770. my ($sth,$old,$new);
  3771. eval { $dbh->begin_work() if($dbh->{AutoCommit}); }; # Transaktion wenn gewünscht und autocommit ein
  3772. if ($@) {
  3773. Log3($name, 2, "DbRep $name -> Error start transaction - $@");
  3774. }
  3775. if (!$complex) {
  3776. $old = delete $hash->{HELPER}{OLDVAL};
  3777. $new = delete $hash->{HELPER}{NEWVAL};
  3778. # SQL zusammenstellen für DB-Operation
  3779. Log3 ($name, 5, "DbRep $name -> Change old value \"$old\" to new value \"$new\" in database $dblogname ");
  3780. # prepare DB operation
  3781. $old =~ s/'/''/g; # escape ' with ''
  3782. $new =~ s/'/''/g; # escape ' with ''
  3783. # SQL zusammenstellen für DB-Update
  3784. my $addon = $old =~ /%/?"WHERE VALUE LIKE '$old'":"WHERE VALUE='$old'";
  3785. if ($IsTimeSet) {
  3786. $sql = DbRep_createUpdateSql($hash,$table,"TIMESTAMP=TIMESTAMP,VALUE='$new' $addon",$device,$reading,"'$runtime_string_first'","'$runtime_string_next'",'');
  3787. } else {
  3788. $sql = DbRep_createUpdateSql($hash,$table,"TIMESTAMP=TIMESTAMP,VALUE='$new' $addon",$device,$reading,undef,undef,'');
  3789. }
  3790. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  3791. $sth = $dbh->prepare($sql) ;
  3792. $old =~ s/''/'/g; # escape back
  3793. $new =~ s/''/'/g; # escape back
  3794. eval { $sth->execute(); };
  3795. if ($@) {
  3796. $err = encode_base64($@,"");
  3797. Log3 ($name, 2, "DbRep $name - Failed to change old value \"$old\" to new value \"$new\": $@");
  3798. $dbh->rollback() if(!$dbh->{AutoCommit});
  3799. $dbh->disconnect();
  3800. return "$name|''|''|$err";
  3801. } else {
  3802. $dbh->commit() if(!$dbh->{AutoCommit});
  3803. $urow = $sth->rows;
  3804. }
  3805. } else {
  3806. $old = delete $hash->{HELPER}{OLDVAL};
  3807. $new = delete $hash->{HELPER}{NEWVAL};
  3808. $old =~ s/'/''/g; # escape ' with ''
  3809. # Timestampstring to Array
  3810. my @ts = split("\\|", $ts);
  3811. Log3 ($name, 5, "DbRep $name - Timestamp-Array: \n@ts");
  3812. # DB-Abfrage zeilenweise für jeden Array-Eintrag
  3813. $urow = 0;
  3814. my $selspec = "DEVICE,READING,TIMESTAMP,VALUE,UNIT";
  3815. my $addon = $old =~ /%/?"AND VALUE LIKE '$old'":"AND VALUE='$old'";
  3816. foreach my $row (@ts) {
  3817. my @a = split("#", $row);
  3818. my $runtime_string = $a[0];
  3819. my $runtime_string_first = $a[1];
  3820. my $runtime_string_next = $a[2];
  3821. if ($IsTimeSet || $IsAggrSet) {
  3822. $sql = DbRep_createSelectSql($hash,"history",$selspec,$device,$reading,"'$runtime_string_first'","'$runtime_string_next'",$addon);
  3823. } else {
  3824. $sql = DbRep_createSelectSql($hash,"history",$selspec,$device,$reading,undef,undef,$addon);
  3825. }
  3826. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  3827. eval{ $sth = $dbh->prepare($sql);
  3828. $sth->execute();
  3829. };
  3830. if ($@) {
  3831. $err = encode_base64($@,"");
  3832. Log3 ($name, 2, "DbRep $name - $@");
  3833. $dbh->disconnect;
  3834. return "$name|''|''|$err";
  3835. }
  3836. no warnings 'uninitialized';
  3837. # DEVICE _ESC_ READING _ESC_ DATE _ESC_ TIME _ESC_ VALUE _ESC_ UNIT
  3838. my @row_array = map { $_->[0]."_ESC_".$_->[1]."_ESC_".($_->[2] =~ s/ /_ESC_/r)."_ESC_".$_->[3]."_ESC_".$_->[4]."\n" } @{$sth->fetchall_arrayref()};
  3839. use warnings;
  3840. Log3 ($name, 4, "DbRep $name - Now change values of selected array ... ");
  3841. foreach my $upd (@row_array) {
  3842. # für jeden selektierten (zu ändernden) Datensatz Userfunktion anwenden und updaten
  3843. my ($device,$reading,$date,$time,$value,$unit) = ($upd =~ /^(.*)_ESC_(.*)_ESC_(.*)_ESC_(.*)_ESC_(.*)_ESC_(.*)$/);
  3844. my $oval = $value; # Selektkriterium für Update alter Valuewert
  3845. my $VALUE = $value;
  3846. my $UNIT = $unit;
  3847. eval $new;
  3848. if ($@) {
  3849. $err = encode_base64($@,"");
  3850. Log3 ($name, 2, "DbRep $name - $@");
  3851. $dbh->disconnect;
  3852. return "$name|''|''|$err";
  3853. }
  3854. $value = $VALUE if(defined $VALUE);
  3855. $unit = $UNIT if(defined $UNIT);
  3856. # Daten auf maximale Länge beschneiden (DbLog-Funktion !)
  3857. (undef,undef,undef,undef,$value,$unit) = DbLog_cutCol($hash->{dbloghash},"1","1","1","1",$value,$unit);
  3858. $value =~ s/'/''/g; # escape ' with ''
  3859. $unit =~ s/'/''/g; # escape ' with ''
  3860. # SQL zusammenstellen für DB-Update
  3861. $sql = "UPDATE history SET TIMESTAMP=TIMESTAMP,VALUE='$value',UNIT='$unit' WHERE TIMESTAMP = '$date $time' AND DEVICE = '$device' AND READING = '$reading' AND VALUE='$oval'";
  3862. Log3 ($name, 5, "DbRep $name - SQL execute: $sql");
  3863. $sth = $dbh->prepare($sql) ;
  3864. $value =~ s/''/'/g; # escape back
  3865. $unit =~ s/''/'/g; # escape back
  3866. eval { $sth->execute(); };
  3867. if ($@) {
  3868. $err = encode_base64($@,"");
  3869. Log3 ($name, 2, "DbRep $name - Failed to change old value \"$old\" to new value \"$new\": $@");
  3870. $dbh->rollback() if(!$dbh->{AutoCommit});
  3871. $dbh->disconnect();
  3872. return "$name|''|''|$err";
  3873. } else {
  3874. $dbh->commit() if(!$dbh->{AutoCommit});
  3875. $urow++;
  3876. }
  3877. }
  3878. }
  3879. }
  3880. $dbh->disconnect();
  3881. # SQL-Laufzeit ermitteln
  3882. my $rt = tv_interval($st);
  3883. # Background-Laufzeit ermitteln
  3884. my $brt = tv_interval($bst);
  3885. $rt = $rt.",".$brt;
  3886. return "$name|$urow|$rt|0|$old|$new";
  3887. }
  3888. ####################################################################################################
  3889. # Auswertungsroutine DB deviceRename/readingRename/changeValue
  3890. ####################################################################################################
  3891. sub change_Done($) {
  3892. my ($string) = @_;
  3893. my @a = split("\\|",$string);
  3894. my $hash = $defs{$a[0]};
  3895. my $name = $hash->{NAME};
  3896. my $urow = $a[1];
  3897. my $bt = $a[2];
  3898. my ($rt,$brt) = split(",", $bt);
  3899. my $err = $a[3]?decode_base64($a[3]):undef;
  3900. my $old = $a[4];
  3901. my $new = $a[5];
  3902. my $renmode = delete $hash->{HELPER}{RENMODE};
  3903. # Befehl nach Procedure ausführen
  3904. my $erread = DbRep_afterproc($hash, $renmode);
  3905. if ($err) {
  3906. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  3907. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  3908. delete($hash->{HELPER}{RUNNING_PID});
  3909. return;
  3910. }
  3911. # only for this block because of warnings if details of readings are not set
  3912. no warnings 'uninitialized';
  3913. readingsBeginUpdate($hash);
  3914. ReadingsBulkUpdateValue ($hash, "number_lines_updated", $urow);
  3915. if($renmode eq "devren") {
  3916. ReadingsBulkUpdateValue ($hash, "device_renamed", "old: ".$old." to new: ".$new) if($urow != 0);
  3917. ReadingsBulkUpdateValue ($hash, "device_not_renamed", "Warning - old: ".$old." not found, not renamed to new: ".$new)
  3918. if($urow == 0);
  3919. }
  3920. if($renmode eq "readren") {
  3921. ReadingsBulkUpdateValue ($hash, "reading_renamed", "old: ".$old." to new: ".$new) if($urow != 0);
  3922. ReadingsBulkUpdateValue ($hash, "reading_not_renamed", "Warning - old: ".$old." not found, not renamed to new: ".$new)
  3923. if ($urow == 0);
  3924. }
  3925. if($renmode eq "changeval") {
  3926. ReadingsBulkUpdateValue ($hash, "value_changed", "old: ".$old." to new: ".$new) if($urow != 0);
  3927. ReadingsBulkUpdateValue ($hash, "value_not_changed", "Warning - old: ".$old." not found, not changed to new: ".$new)
  3928. if ($urow == 0);
  3929. }
  3930. ReadingsBulkUpdateTimeState($hash,$brt,$rt,"done");
  3931. readingsEndUpdate($hash, 1);
  3932. if ($urow != 0) {
  3933. 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");
  3934. 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");
  3935. 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");
  3936. } else {
  3937. 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");
  3938. 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");
  3939. Log3 ($name, 3, "DbRep ".(($hash->{ROLE} eq "Agent")?"Agent ":"")."$name - WARNING - old value \"$old\" not found in database \"$hash->{DATABASE}\" ") if($renmode eq "changeval");
  3940. }
  3941. delete($hash->{HELPER}{RUNNING_PID});
  3942. return;
  3943. }
  3944. ####################################################################################################
  3945. # nichtblockierende DB-Abfrage fetchrows
  3946. ####################################################################################################
  3947. sub fetchrows_DoParse($) {
  3948. my ($string) = @_;
  3949. my ($name,$table,$device,$reading,$runtime_string_first,$runtime_string_next) = split("\\|", $string);
  3950. my $hash = $defs{$name};
  3951. my $dbloghash = $hash->{dbloghash};
  3952. my $dbconn = $dbloghash->{dbconn};
  3953. my $dbuser = $dbloghash->{dbuser};
  3954. my $dblogname = $dbloghash->{NAME};
  3955. my $dbpassword = $attr{"sec$dblogname"}{secret};
  3956. my $limit = AttrVal($name, "limit", 1000);
  3957. my $utf8 = defined($hash->{UTF8})?$hash->{UTF8}:0;
  3958. my $fetchroute = AttrVal($name, "fetchRoute", "descent");
  3959. my $valfilter = AttrVal($name, "valueFilter", undef); # nur Anzeige von Datensätzen die "valueFilter" enthalten
  3960. $fetchroute = ($fetchroute eq "descent")?"DESC":"ASC";
  3961. my ($err,$dbh,$sth,$sql,$rowlist,$nrows);
  3962. # Background-Startzeit
  3963. my $bst = [gettimeofday];
  3964. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1, mysql_enable_utf8 => $utf8 });};
  3965. if ($@) {
  3966. $err = encode_base64($@,"");
  3967. Log3 ($name, 2, "DbRep $name - $@");
  3968. return "$name|''|''|$err|''";
  3969. }
  3970. # ist Zeiteingrenzung und/oder Aggregation gesetzt ? (wenn ja -> "?" in SQL sonst undef)
  3971. my ($IsTimeSet,$IsAggrSet) = DbRep_checktimeaggr($hash);
  3972. Log3 ($name, 5, "DbRep $name - IsTimeSet: $IsTimeSet, IsAggrSet: $IsAggrSet");
  3973. # SQL zusammenstellen für DB-Abfrage
  3974. if ($IsTimeSet) {
  3975. $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));
  3976. } else {
  3977. $sql = DbRep_createSelectSql($hash,$table,"DEVICE,READING,TIMESTAMP,VALUE,UNIT",$device,$reading,undef,undef,"ORDER BY TIMESTAMP $fetchroute LIMIT ".($limit+1));
  3978. }
  3979. $sth = $dbh->prepare($sql);
  3980. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  3981. # SQL-Startzeit
  3982. my $st = [gettimeofday];
  3983. eval{$sth->execute();};
  3984. if ($@) {
  3985. $err = encode_base64($@,"");
  3986. Log3 ($name, 2, "DbRep $name - $@");
  3987. $dbh->disconnect;
  3988. return "$name|''|''|$err|''";
  3989. }
  3990. no warnings 'uninitialized';
  3991. my @row_array = map { $_->[0]."_ESC_".$_->[1]."_ESC_".($_->[2] =~ s/ /_ESC_/r)."_ESC_".$_->[3]."_ESC_".$_->[4]."\n" } @{$sth->fetchall_arrayref()};
  3992. # eventuell gesetzten Datensatz-Filter anwenden
  3993. if($valfilter) {
  3994. my @fiarr;
  3995. foreach my $row (@row_array) {
  3996. next if($row !~ /$valfilter/);
  3997. push @fiarr,$row;
  3998. }
  3999. @row_array = @fiarr;
  4000. }
  4001. use warnings;
  4002. $nrows = $#row_array+1; # Anzahl der Ergebniselemente
  4003. pop @row_array if($nrows>$limit); # das zuviel selektierte Element wegpoppen wenn Limit überschritten
  4004. s/\|/_E#S#C_/g for @row_array; # escape Pipe "|"
  4005. if ($utf8) {
  4006. $rowlist = Encode::encode_utf8(join('|', @row_array));
  4007. } else {
  4008. $rowlist = join('|', @row_array);
  4009. }
  4010. Log3 ($name, 5, "DbRep $name -> row result list:\n$rowlist");
  4011. # SQL-Laufzeit ermitteln
  4012. my $rt = tv_interval($st);
  4013. $dbh->disconnect;
  4014. # Daten müssen als Einzeiler zurückgegeben werden
  4015. $rowlist = encode_base64($rowlist,"");
  4016. # Background-Laufzeit ermitteln
  4017. my $brt = tv_interval($bst);
  4018. $rt = $rt.",".$brt;
  4019. return "$name|$rowlist|$rt|0|$nrows";
  4020. }
  4021. ####################################################################################################
  4022. # Auswertungsroutine der nichtblockierenden DB-Abfrage fetchrows
  4023. ####################################################################################################
  4024. sub fetchrows_ParseDone($) {
  4025. my ($string) = @_;
  4026. my @a = split("\\|",$string);
  4027. my $hash = $defs{$a[0]};
  4028. my $rowlist = decode_base64($a[1]);
  4029. my $bt = $a[2];
  4030. my ($rt,$brt) = split(",", $bt);
  4031. my $err = $a[3]?decode_base64($a[3]):undef;
  4032. my $nrows = $a[4];
  4033. my $name = $hash->{NAME};
  4034. my $reading = AttrVal($name, "reading", undef);
  4035. my $limit = AttrVal($name, "limit", 1000);
  4036. my $color = "<html><span style=\"color: #".AttrVal($name, "fetchMarkDuplicates", "000000").";\">"; # Highlighting doppelter DB-Einträge
  4037. $color =~ s/#// if($color =~ /red|blue|brown|green|orange/);
  4038. my $ecolor = "</span></html>"; # Ende Highlighting
  4039. my @row;
  4040. my $reading_runtime_string;
  4041. if ($err) {
  4042. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  4043. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  4044. delete($hash->{HELPER}{RUNNING_PID});
  4045. return;
  4046. }
  4047. my @row_array = split("\\|", $rowlist);
  4048. s/_E#S#C_/\|/g for @row_array; # escaped Pipe return to "|"
  4049. Log3 ($name, 5, "DbRep $name - row_array decoded: @row_array");
  4050. # Readingaufbereitung
  4051. readingsBeginUpdate($hash);
  4052. my ($orow,$nrow,$oval,$nval);
  4053. my $dz = 1; # Index des Vorkommens im Selektionsarray
  4054. my $zs = ""; # Zusatz wenn device + Reading + Timestamp von folgenden DS gleich ist UND Value unterschiedlich
  4055. my $zsz = 1; # Zusatzzähler
  4056. foreach my $row (@row_array) {
  4057. my @a = split("_ESC_", $row, 6);
  4058. my $dev = $a[0];
  4059. my $rea = $a[1];
  4060. $a[3] =~ s/:/-/g; # substituieren unsupported characters ":" -> siehe fhem.pl
  4061. my $ts = $a[2]."_".$a[3];
  4062. my $val = $a[4];
  4063. my $unt = $a[5];
  4064. $val = $unt?$val." ".$unt:$val;
  4065. $nrow = $ts.$dev.$rea;
  4066. $nval = $val;
  4067. if($orow) {
  4068. if($orow.$oval eq $nrow.$val) {
  4069. $dz++;
  4070. $zs = "";
  4071. $zsz = 1;
  4072. } else {
  4073. # wenn device + Reading + Timestamp gleich ist UND Value unterschiedlich -> dann Zusatz an Reading hängen
  4074. if(($orow eq $nrow) && ($oval ne $val)) {
  4075. $zs = "_".$zsz;
  4076. $zsz++;
  4077. } else {
  4078. $zs = "";
  4079. $zsz = 1;
  4080. }
  4081. $dz = 1;
  4082. }
  4083. }
  4084. $orow = $nrow;
  4085. $oval = $val;
  4086. if ($reading && AttrVal($hash->{NAME}, "readingNameMap", "")) {
  4087. if($dz > 1 && AttrVal($name, "fetchMarkDuplicates", undef)) {
  4088. $reading_runtime_string = $ts."__".$color.$dz."__".AttrVal($hash->{NAME}, "readingNameMap", "").$zs.$ecolor;
  4089. } else {
  4090. $reading_runtime_string = $ts."__".$dz."__".AttrVal($hash->{NAME}, "readingNameMap", "").$zs;
  4091. }
  4092. } else {
  4093. if($dz > 1 && AttrVal($name, "fetchMarkDuplicates", undef)) {
  4094. $reading_runtime_string = $ts."__".$color.$dz."__".$dev."__".$rea.$zs.$ecolor;
  4095. } else {
  4096. $reading_runtime_string = $ts."__".$dz."__".$dev."__".$rea.$zs;
  4097. }
  4098. }
  4099. ReadingsBulkUpdateValue($hash, $reading_runtime_string, $val);
  4100. }
  4101. my $sfx = AttrVal("global", "language", "EN");
  4102. $sfx = ($sfx eq "EN" ? "" : "_$sfx");
  4103. ReadingsBulkUpdateValue($hash, "number_fetched_rows", ($nrows>$limit)?$nrows-1:$nrows);
  4104. ReadingsBulkUpdateTimeState($hash,$brt,$rt,($nrows-$limit>0)?
  4105. "<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");
  4106. readingsEndUpdate($hash, 1);
  4107. delete($hash->{HELPER}{RUNNING_PID});
  4108. return;
  4109. }
  4110. ####################################################################################################
  4111. # DB-Abfrage delSeqDoublets
  4112. ####################################################################################################
  4113. sub delseqdoubl_DoParse($) {
  4114. my ($string) = @_;
  4115. my ($name,$opt,$device,$reading,$ts) = split("\\§", $string);
  4116. my $hash = $defs{$name};
  4117. my $dbloghash = $hash->{dbloghash};
  4118. my $dbconn = $dbloghash->{dbconn};
  4119. my $dbuser = $dbloghash->{dbuser};
  4120. my $dblogname = $dbloghash->{NAME};
  4121. my $dbpassword = $attr{"sec$dblogname"}{secret};
  4122. my $utf8 = defined($hash->{UTF8})?$hash->{UTF8}:0;
  4123. my $limit = AttrVal($name, "limit", 1000);
  4124. my $var = AttrVal($name, "seqDoubletsVariance", undef);
  4125. my $table = "history";
  4126. my ($err,$dbh,$sth,$sql,$rowlist,$nrows,$selspec,$st,$var1,$var2);
  4127. # Background-Startzeit
  4128. my $bst = [gettimeofday];
  4129. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1, mysql_enable_utf8 => $utf8 });};
  4130. if ($@) {
  4131. $err = encode_base64($@,"");
  4132. Log3 ($name, 2, "DbRep $name - $@");
  4133. return "$name|''|''|$err|''|$opt";
  4134. }
  4135. # Timestampstring to Array
  4136. my @ts = split("\\|", $ts);
  4137. Log3 ($name, 5, "DbRep $name - Timestamp-Array: \n@ts");
  4138. $selspec = "DEVICE,READING,TIMESTAMP,VALUE";
  4139. # SQL zusammenstellen für DB-Abfrage
  4140. $sql = DbRep_createSelectSql($hash,$table,$selspec,$device,$reading,"?","?","ORDER BY DEVICE,READING,TIMESTAMP ASC");
  4141. $sth = $dbh->prepare_cached($sql);
  4142. # DB-Abfrage zeilenweise für jeden Timearray-Eintrag
  4143. my @remain;
  4144. my @todel;
  4145. my $nremain = 0;
  4146. my $ntodel = 0;
  4147. my $ndel = 0;
  4148. my $rt = 0;
  4149. no warnings 'uninitialized';
  4150. foreach my $row (@ts) {
  4151. my @a = split("#", $row);
  4152. my $runtime_string = $a[0];
  4153. my $runtime_string_first = $a[1];
  4154. my $runtime_string_next = $a[2];
  4155. $runtime_string = encode_base64($runtime_string,"");
  4156. # SQL-Startzeit
  4157. $st = [gettimeofday];
  4158. # SQL zusammenstellen für Logausgabe
  4159. my $sql1 = DbRep_createSelectSql($hash,$table,$selspec,$device,$reading,"'$runtime_string_first'","'$runtime_string_next'",'');
  4160. Log3 ($name, 4, "DbRep $name - SQL execute: $sql1");
  4161. eval{$sth->execute($runtime_string_first, $runtime_string_next);};
  4162. if ($@) {
  4163. $err = encode_base64($@,"");
  4164. Log3 ($name, 2, "DbRep $name - $@");
  4165. $dbh->disconnect;
  4166. return "$name|''|''|$err|''|$opt";
  4167. }
  4168. # SQL-Laufzeit ermitteln
  4169. $rt = $rt+tv_interval($st);
  4170. # Beginn Löschlogik, Zusammenstellen der löschenden DS (warping)
  4171. # Array @sel -> die VERBLEIBENDEN Datensätze, @warp -> die zu löschenden Datensätze
  4172. my (@sel,@warp);
  4173. my ($or,$oor,$odev,$oread,$oval,$ooval,$ndev,$nread,$nval);
  4174. my $i = 0;
  4175. foreach my $nr (map { $_->[0]."_ESC_".$_->[1]."_ESC_".($_->[2] =~ s/ /_ESC_/r)."_ESC_".$_->[3] } @{$sth->fetchall_arrayref()}) {
  4176. ($ndev,$nread,undef,undef,$nval) = split("_ESC_", $nr); # Werte des aktuellen Elements
  4177. $or = pop @sel; # das letzte Element der Liste
  4178. ($odev,$oread,undef,undef,$oval) = split("_ESC_", $or); # Value des letzten Elements
  4179. if (looks_like_number($oval) && $var) { # Varianz +- falls $val numerischer Wert
  4180. $var1 = $oval + $var;
  4181. $var2 = $oval - $var;
  4182. } else {
  4183. undef $var1;
  4184. undef $var2;
  4185. }
  4186. $oor = pop @sel; # das vorletzte Element der Liste
  4187. $ooval = (split '_ESC_', $oor)[-1]; # Value des vorletzten Elements
  4188. if ($ndev.$nread ne $odev.$oread) {
  4189. $i = 0; # neues Device/Reading in einer Periode -> ooor soll erhalten bleiben
  4190. push (@sel,$oor) if($oor);
  4191. push (@sel,$or) if($or);
  4192. push (@sel,$nr);
  4193. } elsif ($i>=2 && ($ooval eq $oval && $oval eq $nval) || ($i>=2 && $var1 && $var2 && ($ooval <= $var1) && ($var2 <= $ooval) && ($nval <= $var1) && ($var2 <= $nval)) ) {
  4194. push (@sel,$oor);
  4195. push (@sel,$nr);
  4196. push (@warp,$or); # Array der zu löschenden Datensätze
  4197. if ($opt =~ /delete/ && $or) { # delete Datensätze
  4198. my ($dev,$read,$date,$time,$val) = split("_ESC_", $or);
  4199. my $dt = $date." ".$time;
  4200. chomp($val);
  4201. $dev =~ s/'/''/g; # escape ' with ''
  4202. $read =~ s/'/''/g; # escape ' with ''
  4203. $val =~ s/'/''/g; # escape ' with ''
  4204. $st = [gettimeofday];
  4205. my $dsql = "delete FROM $table where TIMESTAMP = '$dt' AND DEVICE = '$dev' AND READING = '$read' AND VALUE = '$val';";
  4206. my $sthd = $dbh->prepare($dsql);
  4207. Log3 ($name, 4, "DbRep $name - SQL execute: $dsql");
  4208. eval {$sthd->execute();};
  4209. if ($@) {
  4210. $err = encode_base64($@,"");
  4211. Log3 ($name, 2, "DbRep $name - $@");
  4212. $dbh->disconnect;
  4213. return "$name|''|''|$err|''|$opt";
  4214. }
  4215. $ndel = $ndel+$sthd->rows;
  4216. $dbh->commit() if(!$dbh->{AutoCommit});
  4217. $rt = $rt+tv_interval($st);
  4218. }
  4219. } else {
  4220. push (@sel,$oor) if($oor);
  4221. push (@sel,$or) if($or);
  4222. push (@sel,$nr);
  4223. }
  4224. $i++;
  4225. }
  4226. if(@sel && $opt =~ /adviceRemain/) {
  4227. # die verbleibenden Datensätze nach Ausführung (nur zur Anzeige)
  4228. push(@remain,@sel) if($#remain+1 < $limit);
  4229. }
  4230. if(@warp && $opt =~ /adviceDelete/) {
  4231. # die zu löschenden Datensätze (nur zur Anzeige)
  4232. push(@todel,@warp) if($#todel+1 < $limit);
  4233. }
  4234. $nremain = $nremain + $#sel+1 if(@sel);
  4235. $ntodel = $ntodel + $#warp+1 if(@warp);
  4236. my $sum = $nremain+$ntodel;
  4237. Log3 ($name, 3, "DbRep $name -> rows analyzed by \"$hash->{LASTCMD}\": $sum") if($sum && $opt =~ /advice/);
  4238. }
  4239. Log3 ($name, 3, "DbRep $name -> rows deleted by \"$hash->{LASTCMD}\": $ndel") if($ndel);
  4240. my $retn = ($opt =~ /adviceRemain/)?$nremain:($opt =~ /adviceDelete/)?$ntodel:$ndel;
  4241. my @retarray = ($opt =~ /adviceRemain/)?@remain:($opt =~ /adviceDelete/)?@todel:" ";
  4242. s/\|/_E#S#C_/g for @retarray; # escape Pipe "|"
  4243. if ($utf8 && @retarray) {
  4244. $rowlist = Encode::encode_utf8(join('|', @retarray));
  4245. } elsif(@retarray) {
  4246. $rowlist = join('|', @retarray);
  4247. } else {
  4248. $rowlist = 0;
  4249. }
  4250. use warnings;
  4251. Log3 ($name, 5, "DbRep $name -> row result list:\n$rowlist");
  4252. $dbh->disconnect;
  4253. # Daten müssen als Einzeiler zurückgegeben werden
  4254. $rowlist = encode_base64($rowlist,"");
  4255. # Background-Laufzeit ermitteln
  4256. my $brt = tv_interval($bst);
  4257. $rt = $rt.",".$brt;
  4258. return "$name|$rowlist|$rt|0|$retn|$opt";
  4259. }
  4260. ####################################################################################################
  4261. # Auswertungsroutine delSeqDoublets
  4262. ####################################################################################################
  4263. sub delseqdoubl_ParseDone($) {
  4264. my ($string) = @_;
  4265. my @a = split("\\|",$string);
  4266. my $hash = $defs{$a[0]};
  4267. my $rowlist = decode_base64($a[1]);
  4268. my $bt = $a[2];
  4269. my ($rt,$brt) = split(",", $bt);
  4270. my $err = $a[3]?decode_base64($a[3]):undef;
  4271. my $nrows = $a[4];
  4272. my $opt = $a[5];
  4273. my $name = $hash->{NAME};
  4274. my $reading = AttrVal($name, "reading", undef);
  4275. my $limit = AttrVal($name, "limit", 1000);
  4276. my @row;
  4277. my $l = 1;
  4278. my $reading_runtime_string;
  4279. my $erread;
  4280. if ($err) {
  4281. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  4282. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  4283. delete($hash->{HELPER}{RUNNING_PID});
  4284. return;
  4285. }
  4286. # Befehl nach Procedure ausführen
  4287. $erread = DbRep_afterproc($hash, "delSeq");
  4288. # Readingaufbereitung
  4289. readingsBeginUpdate($hash);
  4290. no warnings 'uninitialized';
  4291. if ($opt !~ /delete/ && $rowlist) {
  4292. my @row_array = split("\\|", $rowlist);
  4293. s/_E#S#C_/\|/g for @row_array; # escaped Pipe return to "|"
  4294. Log3 ($name, 5, "DbRep $name - row_array decoded: @row_array");
  4295. foreach my $row (@row_array) {
  4296. last if($l >= $limit);
  4297. my @a = split("_ESC_", $row, 5);
  4298. my $dev = $a[0];
  4299. my $rea = $a[1];
  4300. $a[3] =~ s/:/-/g; # substituieren unsupported characters ":" -> siehe fhem.pl
  4301. my $ts = $a[2]."_".$a[3];
  4302. my $val = $a[4];
  4303. if ($reading && AttrVal($hash->{NAME}, "readingNameMap", "")) {
  4304. $reading_runtime_string = $ts."__".AttrVal($hash->{NAME}, "readingNameMap", "") ;
  4305. } else {
  4306. $reading_runtime_string = $ts."__".$dev."__".$rea;
  4307. }
  4308. ReadingsBulkUpdateValue($hash, $reading_runtime_string, $val);
  4309. $l++;
  4310. }
  4311. }
  4312. use warnings;
  4313. my $sfx = AttrVal("global", "language", "EN");
  4314. $sfx = ($sfx eq "EN" ? "" : "_$sfx");
  4315. my $rnam = ($opt =~ /adviceRemain/)?"number_rows_to_remain":($opt =~ /adviceDelete/)?"number_rows_to_delete":"number_rows_deleted";
  4316. ReadingsBulkUpdateValue($hash, "$rnam", "$nrows");
  4317. ReadingsBulkUpdateTimeState($hash,$brt,$rt,($l >= $limit)?
  4318. "<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");
  4319. readingsEndUpdate($hash, 1);
  4320. delete($hash->{HELPER}{RUNNING_PID});
  4321. return;
  4322. }
  4323. ####################################################################################################
  4324. # nichtblockierende DB-Funktion expfile
  4325. ####################################################################################################
  4326. sub expfile_DoParse($) {
  4327. my ($string) = @_;
  4328. my ($name, $device, $reading, $rsf, $file, $ts) = split("\\§", $string);
  4329. my $hash = $defs{$name};
  4330. my $dbloghash = $hash->{dbloghash};
  4331. my $dbconn = $dbloghash->{dbconn};
  4332. my $dbuser = $dbloghash->{dbuser};
  4333. my $dblogname = $dbloghash->{NAME};
  4334. my $dbpassword = $attr{"sec$dblogname"}{secret};
  4335. my $utf8 = defined($hash->{UTF8})?$hash->{UTF8}:0;
  4336. my ($dbh,$sth,$sql);
  4337. my $err=0;
  4338. # Background-Startzeit
  4339. my $bst = [gettimeofday];
  4340. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1, mysql_enable_utf8 => $utf8 });};
  4341. if ($@) {
  4342. $err = encode_base64($@,"");
  4343. Log3 ($name, 2, "DbRep $name - $@");
  4344. return "$name|''|''|$err|''|''|''";
  4345. }
  4346. $rsf =~ s/[:\s]/_/g;
  4347. my $outfile = $file?$file:AttrVal($name, "expimpfile", undef);
  4348. $outfile =~ s/%TSB/$rsf/g;
  4349. my @t = localtime;
  4350. $outfile = ResolveDateWildcards($outfile, @t);
  4351. if (open(FH, ">:utf8", "$outfile")) {
  4352. binmode (FH) if(!$utf8);
  4353. } else {
  4354. $err = encode_base64("could not open ".$outfile.": ".$!,"");
  4355. return "$name|''|''|$err|''|''|''";
  4356. }
  4357. # ist Zeiteingrenzung und/oder Aggregation gesetzt ? (wenn ja -> "?" in SQL sonst undef)
  4358. my ($IsTimeSet,$IsAggrSet) = DbRep_checktimeaggr($hash);
  4359. Log3 ($name, 5, "DbRep $name - IsTimeSet: $IsTimeSet, IsAggrSet: $IsAggrSet");
  4360. # Timestampstring to Array
  4361. my @ts = split("\\|", $ts);
  4362. Log3 ($name, 5, "DbRep $name - Timestamp-Array: \n@ts");
  4363. # SQL-Startzeit
  4364. my $st = [gettimeofday];
  4365. # DB-Abfrage zeilenweise für jeden Array-Eintrag
  4366. my $arrstr;
  4367. my $nrows = 0;
  4368. my $addon = "ORDER BY TIMESTAMP";
  4369. no warnings 'uninitialized';
  4370. foreach my $row (@ts) {
  4371. my @a = split("#", $row);
  4372. my $runtime_string = $a[0];
  4373. my $runtime_string_first = $a[1];
  4374. my $runtime_string_next = $a[2];
  4375. if ($IsTimeSet || $IsAggrSet) {
  4376. $sql = DbRep_createSelectSql($hash,"history","TIMESTAMP,DEVICE,TYPE,EVENT,READING,VALUE,UNIT",$device,$reading,"'$runtime_string_first'","'$runtime_string_next'",$addon);
  4377. } else {
  4378. $sql = DbRep_createSelectSql($hash,"history","TIMESTAMP,DEVICE,TYPE,EVENT,READING,VALUE,UNIT",$device,$reading,undef,undef,$addon);
  4379. }
  4380. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  4381. eval{ $sth = $dbh->prepare($sql);
  4382. $sth->execute();
  4383. };
  4384. if ($@) {
  4385. $err = encode_base64($@,"");
  4386. Log3 ($name, 2, "DbRep $name - $@");
  4387. $dbh->disconnect;
  4388. return "$name|''|''|$err|''|''|''";
  4389. }
  4390. while (my $row = $sth->fetchrow_arrayref) {
  4391. print FH DbRep_charfilter(join(',', map { s{"}{""}g; "\"$_\"";} @$row)), "\n";
  4392. Log3 ($name, 5, "DbRep $name -> write row: @$row");
  4393. # Anzahl der Datensätze
  4394. $nrows++;
  4395. }
  4396. }
  4397. close(FH);
  4398. # SQL-Laufzeit ermitteln
  4399. my $rt = tv_interval($st);
  4400. $sth->finish;
  4401. $dbh->disconnect;
  4402. # Background-Laufzeit ermitteln
  4403. my $brt = tv_interval($bst);
  4404. $rt = $rt.",".$brt;
  4405. return "$name|$nrows|$rt|$err|$device|$reading|$outfile";
  4406. }
  4407. ####################################################################################################
  4408. # Auswertungsroutine der nichtblockierenden DB-Funktion expfile
  4409. ####################################################################################################
  4410. sub expfile_ParseDone($) {
  4411. my ($string) = @_;
  4412. my @a = split("\\|",$string);
  4413. my $hash = $defs{$a[0]};
  4414. my $nrows = $a[1];
  4415. my $bt = $a[2];
  4416. my ($rt,$brt) = split(",", $bt);
  4417. my $err = $a[3]?decode_base64($a[3]):undef;
  4418. my $name = $hash->{NAME};
  4419. my $device = $a[4];
  4420. $device =~ s/[^A-Za-z\/\d_\.-]/\//g;
  4421. my $reading = $a[5];
  4422. $reading =~ s/[^A-Za-z\/\d_\.-]/\//g;
  4423. my $outfile = $a[6];
  4424. my $erread;
  4425. # Befehl nach Procedure ausführen
  4426. $erread = DbRep_afterproc($hash, "export");
  4427. if ($err) {
  4428. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  4429. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  4430. delete($hash->{HELPER}{RUNNING_PID});
  4431. return;
  4432. }
  4433. # only for this block because of warnings if details of readings are not set
  4434. no warnings 'uninitialized';
  4435. my $ds = $device." -- " if ($device);
  4436. my $rds = $reading." -- " if ($reading);
  4437. my $export_string = $ds.$rds." -- ROWS EXPORTED TO FILE -- ";
  4438. my $state = $erread?$erread:"done";
  4439. readingsBeginUpdate($hash);
  4440. ReadingsBulkUpdateValue ($hash, $export_string, $nrows);
  4441. ReadingsBulkUpdateTimeState($hash,$brt,$rt,$state);
  4442. readingsEndUpdate($hash, 1);
  4443. my $rows = $ds.$rds.$nrows;
  4444. Log3 ($name, 3, "DbRep $name - Number of exported datasets from $hash->{DATABASE} to file $outfile: ".$rows);
  4445. delete($hash->{HELPER}{RUNNING_PID});
  4446. return;
  4447. }
  4448. ####################################################################################################
  4449. # nichtblockierende DB-Funktion impfile
  4450. ####################################################################################################
  4451. sub impfile_Push($) {
  4452. my ($string) = @_;
  4453. my ($name, $rsf, $file) = split("\\|", $string);
  4454. my $hash = $defs{$name};
  4455. my $dbloghash = $hash->{dbloghash};
  4456. my $dbconn = $dbloghash->{dbconn};
  4457. my $dbuser = $dbloghash->{dbuser};
  4458. my $dblogname = $dbloghash->{NAME};
  4459. my $dbmodel = $hash->{dbloghash}{MODEL};
  4460. my $dbpassword = $attr{"sec$dblogname"}{secret};
  4461. my $utf8 = defined($hash->{UTF8})?$hash->{UTF8}:0;
  4462. my $err=0;
  4463. my $sth;
  4464. # Background-Startzeit
  4465. my $bst = [gettimeofday];
  4466. my $dbh;
  4467. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoCommit => 1, AutoInactiveDestroy => 1, mysql_enable_utf8 => $utf8 });};
  4468. if ($@) {
  4469. $err = encode_base64($@,"");
  4470. Log3 ($name, 2, "DbRep $name - $@");
  4471. return "$name|''|''|$err|''";
  4472. }
  4473. # check ob PK verwendet wird, @usepkx?Anzahl der Felder im PK:0 wenn kein PK, $pkx?Namen der Felder:none wenn kein PK
  4474. my ($usepkh,$usepkc,$pkh,$pkc) = DbRep_checkUsePK($hash,$dbloghash,$dbh);
  4475. $rsf =~ s/[:\s]/_/g;
  4476. my $infile = $file?$file:AttrVal($name, "expimpfile", undef);
  4477. $infile =~ s/%TSB/$rsf/g;
  4478. my @t = localtime;
  4479. $infile = ResolveDateWildcards($infile, @t);
  4480. if (open(FH, "<:utf8", "$infile")) {
  4481. binmode (FH) if(!$utf8);
  4482. } else {
  4483. $err = encode_base64("could not open ".$infile.": ".$!,"");
  4484. return "$name|''|''|$err|''";
  4485. }
  4486. # only for this block because of warnings if details inline is not set
  4487. no warnings 'uninitialized';
  4488. # SQL-Startzeit
  4489. my $st = [gettimeofday];
  4490. my $al;
  4491. # Datei zeilenweise einlesen und verarbeiten !
  4492. # Beispiel Inline:
  4493. # "2016-09-25 08:53:56","STP_5000","SMAUTILS","etotal: 11859.573","etotal","11859.573",""
  4494. # insert history mit/ohne primary key
  4495. if ($usepkh && $dbloghash->{MODEL} eq 'MYSQL') {
  4496. eval { $sth = $dbh->prepare_cached("INSERT IGNORE INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  4497. } elsif ($usepkh && $dbloghash->{MODEL} eq 'SQLITE') {
  4498. eval { $sth = $dbh->prepare_cached("INSERT OR IGNORE INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  4499. } elsif ($usepkh && $dbloghash->{MODEL} eq 'POSTGRESQL') {
  4500. eval { $sth = $dbh->prepare_cached("INSERT INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?) ON CONFLICT DO NOTHING"); };
  4501. } else {
  4502. eval { $sth = $dbh->prepare_cached("INSERT INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  4503. }
  4504. if ($@) {
  4505. $err = encode_base64($@,"");
  4506. Log3 ($name, 2, "DbRep $name - $@");
  4507. $dbh->disconnect();
  4508. return "$name|''|''|$err|''";
  4509. }
  4510. $dbh->begin_work();
  4511. my $irowdone = 0;
  4512. my $irowcount = 0;
  4513. my $warn = 0;
  4514. while (<FH>) {
  4515. $al = $_;
  4516. chomp $al;
  4517. my @alarr = split("\",\"", $al);
  4518. foreach(@alarr) {
  4519. tr/"//d;
  4520. }
  4521. my $i_timestamp = $alarr[0];
  4522. # $i_timestamp =~ tr/"//d;
  4523. my $i_device = $alarr[1];
  4524. my $i_type = $alarr[2];
  4525. my $i_event = $alarr[3];
  4526. my $i_reading = $alarr[4];
  4527. my $i_value = $alarr[5];
  4528. my $i_unit = $alarr[6] ? $alarr[6]: " ";
  4529. $irowcount++;
  4530. next if(!$i_timestamp); #leerer Datensatz
  4531. # check ob TIMESTAMP Format ok ?
  4532. my ($i_date, $i_time) = split(" ",$i_timestamp);
  4533. if ($i_date !~ /(\d{4})-(\d{2})-(\d{2})/ || $i_time !~ /(\d{2}):(\d{2}):(\d{2})/) {
  4534. $err = encode_base64("Format of date/time is not valid in row $irowcount of $infile. Must be format \"YYYY-MM-DD HH:MM:SS\" !","");
  4535. Log3 ($name, 2, "DbRep $name -> ERROR - Import from file $infile was not done. Invalid date/time field format in row $irowcount.");
  4536. close(FH);
  4537. $dbh->rollback;
  4538. return "$name|''|''|$err|''";
  4539. }
  4540. # Daten auf maximale Länge (entsprechend der Feldlänge in DbLog DB create-scripts) beschneiden wenn nicht SQLite
  4541. if ($dbmodel ne 'SQLITE') {
  4542. $i_device = substr($i_device,0, $dbrep_col{DEVICE});
  4543. $i_event = substr($i_event,0, $dbrep_col{EVENT});
  4544. $i_reading = substr($i_reading,0, $dbrep_col{READING});
  4545. $i_value = substr($i_value,0, $dbrep_col{VALUE});
  4546. $i_unit = substr($i_unit,0, $dbrep_col{UNIT}) if($i_unit);
  4547. }
  4548. 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");
  4549. if($i_timestamp && $i_device && $i_reading) {
  4550. eval {$sth->execute($i_timestamp, $i_device, $i_type, $i_event, $i_reading, $i_value, $i_unit);};
  4551. if ($@) {
  4552. $err = encode_base64($@,"");
  4553. Log3 ($name, 2, "DbRep $name - Failed to insert new dataset into database: $@");
  4554. close(FH);
  4555. $dbh->rollback;
  4556. $dbh->disconnect;
  4557. return "$name|''|''|$err|''";
  4558. } else {
  4559. $irowdone++
  4560. }
  4561. } else {
  4562. my $c = !$i_timestamp?"field \"timestamp\" is empty":!$i_device?"field \"device\" is empty":"field \"reading\" is empty";
  4563. $err = encode_base64("format error in in row $irowcount of $infile - cause: $c","");
  4564. Log3 ($name, 2, "DbRep $name -> ERROR - Import of datasets NOT done. Formaterror in row $irowcount of $infile - cause: $c");
  4565. close(FH);
  4566. $dbh->rollback;
  4567. $dbh->disconnect;
  4568. return "$name|''|''|$err|''";
  4569. }
  4570. }
  4571. $dbh->commit;
  4572. $dbh->disconnect;
  4573. close(FH);
  4574. # SQL-Laufzeit ermitteln
  4575. my $rt = tv_interval($st);
  4576. # Background-Laufzeit ermitteln
  4577. my $brt = tv_interval($bst);
  4578. $rt = $rt.",".$brt;
  4579. return "$name|$irowdone|$rt|$err|$infile";
  4580. }
  4581. ####################################################################################################
  4582. # Auswertungsroutine der nichtblockierenden DB-Funktion impfile
  4583. ####################################################################################################
  4584. sub impfile_PushDone($) {
  4585. my ($string) = @_;
  4586. my @a = split("\\|",$string);
  4587. my $hash = $defs{$a[0]};
  4588. my $irowdone = $a[1];
  4589. my $bt = $a[2];
  4590. my ($rt,$brt) = split(",", $bt);
  4591. my $err = $a[3]?decode_base64($a[3]):undef;
  4592. my $name = $hash->{NAME};
  4593. my $infile = $a[4];
  4594. my $erread;
  4595. # Befehl nach Procedure ausführen
  4596. $erread = DbRep_afterproc($hash, "import");
  4597. if ($err) {
  4598. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  4599. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  4600. delete($hash->{HELPER}{RUNNING_PID});
  4601. return;
  4602. }
  4603. # only for this block because of warnings if details of readings are not set
  4604. no warnings 'uninitialized';
  4605. my $import_string = " -- ROWS IMPORTED FROM FILE -- ";
  4606. my $state = $erread?$erread:"done";
  4607. readingsBeginUpdate($hash);
  4608. ReadingsBulkUpdateValue ($hash, $import_string, $irowdone);
  4609. ReadingsBulkUpdateTimeState($hash,$brt,$rt,$state);
  4610. readingsEndUpdate($hash, 1);
  4611. Log3 ($name, 3, "DbRep $name - Number of imported datasets to $hash->{DATABASE} from file $infile: $irowdone");
  4612. delete($hash->{HELPER}{RUNNING_PID});
  4613. return;
  4614. }
  4615. ####################################################################################################
  4616. # nichtblockierende DB-Abfrage sqlCmd - generischer SQL-Befehl - name | opt | sqlcommand
  4617. ####################################################################################################
  4618. # set logdbrep sqlCmd select count(*) from history
  4619. # set logdbrep sqlCmd select DEVICE,count(*) from history group by DEVICE HAVING count(*) > 10000
  4620. sub sqlCmd_DoParse($) {
  4621. my ($string) = @_;
  4622. my ($name, $opt, $runtime_string_first, $runtime_string_next, $cmd) = split("\\|", $string);
  4623. my $hash = $defs{$name};
  4624. my $dbloghash = $hash->{dbloghash};
  4625. my $dbconn = $dbloghash->{dbconn};
  4626. my $dbuser = $dbloghash->{dbuser};
  4627. my $dblogname = $dbloghash->{NAME};
  4628. my $dbpassword = $attr{"sec$dblogname"}{secret};
  4629. my $utf8 = defined($hash->{UTF8})?$hash->{UTF8}:0;
  4630. my $srs = AttrVal($name, "sqlResultFieldSep", "|");
  4631. my $err;
  4632. # Background-Startzeit
  4633. my $bst = [gettimeofday];
  4634. my $dbh;
  4635. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoCommit => 1, AutoInactiveDestroy => 1, mysql_enable_utf8 => $utf8 });};
  4636. if ($@) {
  4637. $err = encode_base64($@,"");
  4638. Log3 ($name, 2, "DbRep $name - $@");
  4639. return "$name|''|$opt|$cmd|''|''|$err";
  4640. }
  4641. # only for this block because of warnings if details of readings are not set
  4642. no warnings 'uninitialized';
  4643. my $sql = ($cmd =~ m/\;$/)?$cmd:$cmd.";";
  4644. # Allow inplace replacement of keywords for timings (use time attribute syntax)
  4645. $sql =~ s/§timestamp_begin§/'$runtime_string_first'/g;
  4646. $sql =~ s/§timestamp_end§/'$runtime_string_next'/g;
  4647. # Debug "SQL :".$sql.":";
  4648. Log3($name, 4, "DbRep $name - SQL execute: $sql");
  4649. # SQL-Startzeit
  4650. my $st = [gettimeofday];
  4651. my ($sth,$r);
  4652. eval {$sth = $dbh->prepare($sql);
  4653. $r = $sth->execute();
  4654. };
  4655. if ($@) {
  4656. # error bei sql-execute
  4657. $err = encode_base64($@,"");
  4658. Log3 ($name, 2, "DbRep $name - ERROR - $@");
  4659. $dbh->disconnect;
  4660. return "$name|''|$opt|$sql|''|''|$err";
  4661. }
  4662. my @rows;
  4663. my $nrows = 0;
  4664. if($sql =~ m/^\s*(select|pragma|show)/is) {
  4665. while (my @line = $sth->fetchrow_array()) {
  4666. Log3 ($name, 4, "DbRep $name - SQL result: @line");
  4667. my $row = join("$srs", @line);
  4668. # join Delimiter "§" escapen
  4669. $row =~ s/§/|°escaped°|/g;
  4670. push(@rows, $row);
  4671. # Anzahl der Datensätze
  4672. $nrows++;
  4673. }
  4674. } else {
  4675. $nrows = $sth->rows;
  4676. eval {$dbh->commit() if(!$dbh->{AutoCommit});};
  4677. if ($@) {
  4678. $err = encode_base64($@,"");
  4679. Log3 ($name, 2, "DbRep $name - ERROR - $@");
  4680. $dbh->disconnect;
  4681. return "$name|''|$opt|$sql|''|''|$err";
  4682. }
  4683. push(@rows, $r);
  4684. my $com = (split(" ",$sql, 2))[0];
  4685. Log3 ($name, 3, "DbRep $name - Number of entries processed in db $hash->{DATABASE}: $nrows by $com");
  4686. }
  4687. $sth->finish;
  4688. # SQL-Laufzeit ermitteln
  4689. my $rt = tv_interval($st);
  4690. $dbh->disconnect;
  4691. # Daten müssen als Einzeiler zurückgegeben werden
  4692. my $rowstring = join("§", @rows);
  4693. $rowstring = encode_base64($rowstring,"");
  4694. # Background-Laufzeit ermitteln
  4695. my $brt = tv_interval($bst);
  4696. $rt = $rt.",".$brt;
  4697. return "$name|$rowstring|$opt|$sql|$nrows|$rt|$err";
  4698. }
  4699. ####################################################################################################
  4700. # Auswertungsroutine der nichtblockierenden DB-Abfrage sqlCmd
  4701. ####################################################################################################
  4702. sub sqlCmd_ParseDone($) {
  4703. my ($string) = @_;
  4704. my @a = split("\\|",$string);
  4705. my $hash = $defs{$a[0]};
  4706. my $name = $hash->{NAME};
  4707. my $rowstring = decode_base64($a[1]);
  4708. my $opt = $a[2];
  4709. my $cmd = $a[3];
  4710. my $nrows = $a[4];
  4711. my $bt = $a[5];
  4712. my ($rt,$brt) = split(",", $bt);
  4713. my $err = $a[6]?decode_base64($a[6]):undef;
  4714. my $srf = AttrVal($name, "sqlResultFormat", "separated");
  4715. my $srs = AttrVal($name, "sqlResultFieldSep", "|");
  4716. if ($err) {
  4717. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  4718. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  4719. delete($hash->{HELPER}{RUNNING_PID});
  4720. return;
  4721. }
  4722. Log3 ($name, 5, "DbRep $name - SQL result decoded: $rowstring") if($rowstring);
  4723. no warnings 'uninitialized';
  4724. # Readingaufbereitung
  4725. readingsBeginUpdate($hash);
  4726. ReadingsBulkUpdateValue ($hash, "sqlCmd", $cmd);
  4727. ReadingsBulkUpdateValue ($hash, "sqlResultNumRows", $nrows);
  4728. # Drop-Down Liste bisherige sqlCmd-Befehle füllen und in Key-File sichern
  4729. # my $hl = $hash->{HELPER}{SQLHIST};
  4730. my @sqlhist = split(",",$hash->{HELPER}{SQLHIST});
  4731. $cmd =~ s/\s/&nbsp;/g;
  4732. $cmd =~ s/,/<c>/g;
  4733. my $hlc = AttrVal($name, "sqlCmdHistoryLength", 0); # Anzahl der Einträge in Drop-Down Liste
  4734. if(!@sqlhist || (@sqlhist && !($cmd ~~ @sqlhist))) {
  4735. unshift @sqlhist,$cmd;
  4736. pop @sqlhist if(@sqlhist > $hlc);
  4737. my $hl = join(",",@sqlhist);
  4738. $hash->{HELPER}{SQLHIST} = $hl;
  4739. DbRep_setCmdFile($name."_sqlCmdList",$hl,$hash);
  4740. }
  4741. if ($srf eq "sline") {
  4742. $rowstring =~ s/§/]|[/g;
  4743. $rowstring =~ s/\|°escaped°\|/§/g;
  4744. ReadingsBulkUpdateValue ($hash, "SqlResult", $rowstring);
  4745. } elsif ($srf eq "table") {
  4746. my $res = "<html><table border=2 bordercolor='darkgreen' cellspacing=0>";
  4747. my @rows = split( /§/, $rowstring );
  4748. my $row;
  4749. foreach $row ( @rows ) {
  4750. $row =~ s/\|°escaped°\|/§/g;
  4751. $row =~ s/$srs/\|/g if($srs !~ /\|/);
  4752. $row =~ s/\|/<\/td><td style='padding-right:5px;padding-left:5px'>/g;
  4753. $res .= "<tr><td style='padding-right:5px;padding-left:5px'>".$row."</td></tr>";
  4754. }
  4755. $row .= $res."</table></html>";
  4756. ReadingsBulkUpdateValue ($hash,"SqlResult", $row);
  4757. } elsif ($srf eq "mline") {
  4758. my $res = "<html>";
  4759. my @rows = split( /§/, $rowstring );
  4760. my $row;
  4761. foreach $row ( @rows ) {
  4762. $row =~ s/\|°escaped°\|/§/g;
  4763. $res .= $row."<br>";
  4764. }
  4765. $row .= $res."</html>";
  4766. ReadingsBulkUpdateValue ($hash, "SqlResult", $row );
  4767. } elsif ($srf eq "separated") {
  4768. my @rows = split( /§/, $rowstring );
  4769. my $bigint = @rows;
  4770. my $numd = ceil(log10($bigint));
  4771. my $formatstr = sprintf('%%%d.%dd', $numd, $numd);
  4772. my $i = 0;
  4773. foreach my $row ( @rows ) {
  4774. $i++;
  4775. $row =~ s/\|°escaped°\|/§/g;
  4776. my $fi = sprintf($formatstr, $i);
  4777. ReadingsBulkUpdateValue ($hash, "SqlResultRow_".$fi, $row);
  4778. }
  4779. } elsif ($srf eq "json") {
  4780. my %result = ();
  4781. my @rows = split( /§/, $rowstring );
  4782. my $bigint = @rows;
  4783. my $numd = ceil(log10($bigint));
  4784. my $formatstr = sprintf('%%%d.%dd', $numd, $numd);
  4785. my $i = 0;
  4786. foreach my $row ( @rows ) {
  4787. $i++;
  4788. $row =~ s/\|°escaped°\|/§/g;
  4789. my $fi = sprintf($formatstr, $i);
  4790. $result{$fi} = $row;
  4791. }
  4792. my $json = toJSON(\%result); # at least fhem.pl 14348 2017-05-22 20:25:06Z
  4793. ReadingsBulkUpdateValue ($hash, "SqlResult", $json);
  4794. }
  4795. ReadingsBulkUpdateTimeState($hash,$brt,$rt,"done");
  4796. readingsEndUpdate($hash, 1);
  4797. delete($hash->{HELPER}{RUNNING_PID});
  4798. return;
  4799. }
  4800. ####################################################################################################
  4801. # nichtblockierende DB-Abfrage get db Metadaten
  4802. ####################################################################################################
  4803. sub dbmeta_DoParse($) {
  4804. my ($string) = @_;
  4805. my @a = split("\\|",$string);
  4806. my $name = $a[0];
  4807. my $hash = $defs{$name};
  4808. my $opt = $a[1];
  4809. my $dbloghash = $hash->{dbloghash};
  4810. my $dbconn = $dbloghash->{dbconn};
  4811. my $db = $hash->{DATABASE};
  4812. my $dbuser = $dbloghash->{dbuser};
  4813. my $dblogname = $dbloghash->{NAME};
  4814. my $dbpassword = $attr{"sec$dblogname"}{secret};
  4815. my $dbmodel = $dbloghash->{MODEL};
  4816. my $utf8 = defined($hash->{UTF8})?$hash->{UTF8}:0;
  4817. my ($dbh,$sth,$sql);
  4818. my $err;
  4819. # Background-Startzeit
  4820. my $bst = [gettimeofday];
  4821. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1, mysql_enable_utf8 => $utf8 });};
  4822. if ($@) {
  4823. $err = encode_base64($@,"");
  4824. Log3 ($name, 2, "DbRep $name - $@");
  4825. return "$name|''|''|''|$err";
  4826. }
  4827. # only for this block because of warnings if details of readings are not set
  4828. no warnings 'uninitialized';
  4829. # Liste der anzuzeigenden Parameter erzeugen, sonst alle ("%"), abhängig von $opt
  4830. my $param = AttrVal($name, "showVariables", "%") if($opt eq "dbvars");
  4831. $param = AttrVal($name, "showSvrInfo", "[A-Z_]") if($opt eq "svrinfo");
  4832. $param = AttrVal($name, "showStatus", "%") if($opt eq "dbstatus");
  4833. $param = "1" if($opt =~ /tableinfo|procinfo/); # Dummy-Eintrag für einen Schleifendurchlauf
  4834. my @parlist = split(",",$param);
  4835. # SQL-Startzeit
  4836. my $st = [gettimeofday];
  4837. my @row_array;
  4838. # due to incompatible changes made in MyQL 5.7.5, see http://johnemb.blogspot.de/2014/09/adding-or-removing-individual-sql-modes.html
  4839. if($dbmodel eq "MYSQL") {
  4840. eval {$dbh->do("SET sql_mode=(SELECT REPLACE(\@\@sql_mode,'ONLY_FULL_GROUP_BY',''));");};
  4841. }
  4842. if ($@) {
  4843. $err = encode_base64($@,"");
  4844. Log3 ($name, 2, "DbRep $name - $@");
  4845. $dbh->disconnect;
  4846. return "$name|''|''|''|$err";
  4847. }
  4848. if ($opt ne "svrinfo") {
  4849. foreach my $ple (@parlist) {
  4850. if ($opt eq "dbvars") {
  4851. $sql = "show variables like '$ple';";
  4852. } elsif ($opt eq "dbstatus") {
  4853. $sql = "show global status like '$ple';";
  4854. } elsif ($opt eq "tableinfo") {
  4855. $sql = "show Table Status from $db;";
  4856. } elsif ($opt eq "procinfo") {
  4857. $sql = "show full processlist;";
  4858. }
  4859. Log3($name, 4, "DbRep $name - SQL execute: $sql");
  4860. $sth = $dbh->prepare($sql);
  4861. eval {$sth->execute();};
  4862. if ($@) {
  4863. # error bei sql-execute
  4864. $err = encode_base64($@,"");
  4865. Log3 ($name, 2, "DbRep $name - $@");
  4866. $dbh->disconnect;
  4867. return "$name|''|''|''|$err";
  4868. } else {
  4869. # kein error bei sql-execute
  4870. if ($opt eq "tableinfo") {
  4871. $param = AttrVal($name, "showTableInfo", "[A-Z_]");
  4872. $param =~ s/,/\|/g;
  4873. $param =~ tr/%//d;
  4874. while ( my $line = $sth->fetchrow_hashref()) {
  4875. 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}");
  4876. if($line->{Name} =~ m/($param)/i) {
  4877. push(@row_array, $line->{Name}.".engine ".$line->{Engine}) if($line->{Engine});
  4878. push(@row_array, $line->{Name}.".version ".$line->{Version}) if($line->{Version});
  4879. push(@row_array, $line->{Name}.".row_format ".$line->{Row_format}) if($line->{Row_format});
  4880. push(@row_array, $line->{Name}.".number_of_rows ".$line->{Rows}) if($line->{Rows});
  4881. push(@row_array, $line->{Name}.".avg_row_length ".$line->{Avg_row_length}) if($line->{Avg_row_length});
  4882. push(@row_array, $line->{Name}.".data_length_MB ".sprintf("%.2f",$line->{Data_length}/1024/1024)) if($line->{Data_length});
  4883. push(@row_array, $line->{Name}.".max_data_length_MB ".sprintf("%.2f",$line->{Max_data_length}/1024/1024)) if($line->{Max_data_length});
  4884. push(@row_array, $line->{Name}.".index_length_MB ".sprintf("%.2f",$line->{Index_length}/1024/1024)) if($line->{Index_length});
  4885. push(@row_array, $line->{Name}.".data_index_length_MB ".sprintf("%.2f",($line->{Data_length}+$line->{Index_length})/1024/1024));
  4886. push(@row_array, $line->{Name}.".data_free_MB ".sprintf("%.2f",$line->{Data_free}/1024/1024)) if($line->{Data_free});
  4887. push(@row_array, $line->{Name}.".auto_increment ".$line->{Auto_increment}) if($line->{Auto_increment});
  4888. push(@row_array, $line->{Name}.".create_time ".$line->{Create_time}) if($line->{Create_time});
  4889. push(@row_array, $line->{Name}.".update_time ".$line->{Update_time}) if($line->{Update_time});
  4890. push(@row_array, $line->{Name}.".check_time ".$line->{Check_time}) if($line->{Check_time});
  4891. push(@row_array, $line->{Name}.".collation ".$line->{Collation}) if($line->{Collation});
  4892. push(@row_array, $line->{Name}.".checksum ".$line->{Checksum}) if($line->{Checksum});
  4893. push(@row_array, $line->{Name}.".create_options ".$line->{Create_options}) if($line->{Create_options});
  4894. push(@row_array, $line->{Name}.".comment ".$line->{Comment}) if($line->{Comment});
  4895. }
  4896. }
  4897. } elsif ($opt eq "procinfo") {
  4898. my $res = "<html><table border=2 bordercolor='darkgreen' cellspacing=0>";
  4899. $res .= "<tr><td style='padding-right:5px;padding-left:5px;font-weight:bold'>ID</td>";
  4900. $res .= "<td style='padding-right:5px;padding-left:5px;font-weight:bold'>USER</td>";
  4901. $res .= "<td style='padding-right:5px;padding-left:5px;font-weight:bold'>HOST</td>";
  4902. $res .= "<td style='padding-right:5px;padding-left:5px;font-weight:bold'>DB</td>";
  4903. $res .= "<td style='padding-right:5px;padding-left:5px;font-weight:bold'>CMD</td>";
  4904. $res .= "<td style='padding-right:5px;padding-left:5px;font-weight:bold'>TIME_Sec</td>";
  4905. $res .= "<td style='padding-right:5px;padding-left:5px;font-weight:bold'>STATE</td>";
  4906. $res .= "<td style='padding-right:5px;padding-left:5px;font-weight:bold'>INFO</td>";
  4907. $res .= "<td style='padding-right:5px;padding-left:5px;font-weight:bold'>PROGRESS</td></tr>";
  4908. while (my @line = $sth->fetchrow_array()) {
  4909. Log3 ($name, 4, "DbRep $name - SQL result: @line");
  4910. my $row = join("|", @line);
  4911. $row =~ tr/ A-Za-z0-9!"#$§%&'()*+,-.\/:;<=>?@[\]^_`{|}~//cd;
  4912. $row =~ s/\|/<\/td><td style='padding-right:5px;padding-left:5px'>/g;
  4913. $res .= "<tr><td style='padding-right:5px;padding-left:5px'>".$row."</td></tr>";
  4914. }
  4915. my $tab .= $res."</table></html>";
  4916. push(@row_array, "ProcessList ".$tab);
  4917. } else {
  4918. while (my @line = $sth->fetchrow_array()) {
  4919. Log3 ($name, 4, "DbRep $name - SQL result: @line");
  4920. my $row = join("§", @line);
  4921. $row =~ s/ /_/g;
  4922. @line = split("§", $row);
  4923. push(@row_array, $line[0]." ".$line[1]);
  4924. }
  4925. }
  4926. }
  4927. $sth->finish;
  4928. }
  4929. } else {
  4930. $param =~ s/,/\|/g;
  4931. $param =~ tr/%//d;
  4932. # Log3 ($name, 5, "DbRep $name - showDbInfo: $param");
  4933. if($dbmodel eq 'SQLITE') {
  4934. my $sf = $dbh->sqlite_db_filename();
  4935. if ($@) {
  4936. # error bei sql-execute
  4937. $err = encode_base64($@,"");
  4938. Log3 ($name, 2, "DbRep $name - $@");
  4939. $dbh->disconnect;
  4940. return "$name|''|''|''|$err";
  4941. } else {
  4942. # kein error bei sql-execute
  4943. my $key = "SQLITE_DB_FILENAME";
  4944. push(@row_array, $key." ".$sf) if($key =~ m/($param)/i);
  4945. }
  4946. my @a = split(' ',qx(du -m $hash->{DATABASE})) if ($^O =~ m/linux/i || $^O =~ m/unix/i);
  4947. my $key = "SQLITE_FILE_SIZE_MB";
  4948. push(@row_array, $key." ".$a[0]) if($key =~ m/($param)/i);
  4949. }
  4950. my $info;
  4951. while( my ($key,$value) = each(%GetInfoType) ) {
  4952. eval { $info = $dbh->get_info($GetInfoType{"$key"}) };
  4953. if ($@) {
  4954. $err = encode_base64($@,"");
  4955. Log3 ($name, 2, "DbRep $name - $@");
  4956. $dbh->disconnect;
  4957. return "$name|''|''|''|$err";
  4958. } else {
  4959. if($utf8) {
  4960. $info = Encode::encode_utf8($info) if($info);
  4961. }
  4962. push(@row_array, $key." ".$info) if($key =~ m/($param)/i);
  4963. }
  4964. }
  4965. }
  4966. # SQL-Laufzeit ermitteln
  4967. my $rt = tv_interval($st);
  4968. $dbh->disconnect;
  4969. my $rowlist = join('§', @row_array);
  4970. Log3 ($name, 5, "DbRep $name -> row_array: \n@row_array");
  4971. # Daten müssen als Einzeiler zurückgegeben werden
  4972. $rowlist = encode_base64($rowlist,"");
  4973. # Background-Laufzeit ermitteln
  4974. my $brt = tv_interval($bst);
  4975. $rt = $rt.",".$brt;
  4976. return "$name|$rowlist|$rt|$opt|0";
  4977. }
  4978. ####################################################################################################
  4979. # Auswertungsroutine der nichtblockierenden DB-Abfrage get db Metadaten
  4980. ####################################################################################################
  4981. sub dbmeta_ParseDone($) {
  4982. my ($string) = @_;
  4983. my @a = split("\\|",$string);
  4984. my $hash = $defs{$a[0]};
  4985. my $name = $hash->{NAME};
  4986. my $rowlist = decode_base64($a[1]);
  4987. my $bt = $a[2];
  4988. my $opt = $a[3];
  4989. my ($rt,$brt) = split(",", $bt);
  4990. my $err = $a[4]?decode_base64($a[4]):undef;
  4991. if ($err) {
  4992. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  4993. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  4994. delete($hash->{HELPER}{RUNNING_PID});
  4995. return;
  4996. }
  4997. # only for this block because of warnings if details of readings are not set
  4998. no warnings 'uninitialized';
  4999. # Readingaufbereitung
  5000. readingsBeginUpdate($hash);
  5001. my @row_array = split("§", $rowlist);
  5002. Log3 ($name, 5, "DbRep $name - SQL result decoded: \n@row_array") if(@row_array);
  5003. my $pre = "";
  5004. $pre = "VAR_" if($opt eq "dbvars");
  5005. $pre = "STAT_" if($opt eq "dbstatus");
  5006. $pre = "INFO_" if($opt eq "tableinfo");
  5007. foreach my $row (@row_array) {
  5008. my @a = split(" ", $row, 2);
  5009. my $k = $a[0];
  5010. my $v = $a[1];
  5011. ReadingsBulkUpdateValue ($hash, $pre.$k, $v);
  5012. }
  5013. ReadingsBulkUpdateTimeState($hash,$brt,$rt,"done");
  5014. readingsEndUpdate($hash, 1);
  5015. # InternalTimer(time+0.5, "browser_refresh", $hash, 0);
  5016. delete($hash->{HELPER}{RUNNING_PID});
  5017. return;
  5018. }
  5019. ####################################################################################################
  5020. # optimize Tables alle Datenbanken
  5021. ####################################################################################################
  5022. sub DbRep_optimizeTables($) {
  5023. my ($name) = @_;
  5024. my $hash = $defs{$name};
  5025. my $dbloghash = $hash->{dbloghash};
  5026. my $dbconn = $dbloghash->{dbconn};
  5027. my $dbuser = $dbloghash->{dbuser};
  5028. my $dblogname = $dbloghash->{NAME};
  5029. my $dbmodel = $dbloghash->{MODEL};
  5030. my $dbpassword = $attr{"sec$dblogname"}{secret};
  5031. my $dbname = $hash->{DATABASE};
  5032. my $value = 0;
  5033. my ($dbh,$sth,$query,$err,$r,$db_MB_start,$db_MB_end);
  5034. my (%db_tables,@tablenames);
  5035. # Background-Startzeit
  5036. my $bst = [gettimeofday];
  5037. # Verbindung mit DB
  5038. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1 });};
  5039. if ($@) {
  5040. $err = encode_base64($@,"");
  5041. Log3 ($name, 2, "DbRep $name - $@");
  5042. return "$name|''|$err|''|''";
  5043. }
  5044. # SQL-Startzeit
  5045. my $st = [gettimeofday];
  5046. if ($dbmodel =~ /MYSQL/) {
  5047. # Eigenschaften der vorhandenen Tabellen ermitteln (SHOW TABLE STATUS -> Rows sind nicht exakt !!)
  5048. $query = "SHOW TABLE STATUS FROM `$dbname`";
  5049. Log3 ($name, 5, "DbRep $name - current query: $query ");
  5050. Log3 ($name, 3, "DbRep $name - Searching for tables inside database $dbname....");
  5051. eval { $sth = $dbh->prepare($query);
  5052. $sth->execute;
  5053. };
  5054. if ($@) {
  5055. $err = encode_base64($@,"");
  5056. Log3 ($name, 2, "DbRep $name - Error executing: '".$query."' ! MySQL-Error: ".$@);
  5057. $sth->finish;
  5058. $dbh->disconnect;
  5059. return "$name|''|$err|''|''";
  5060. }
  5061. while ( $value = $sth->fetchrow_hashref()) {
  5062. # verbose 5 logging
  5063. Log3 ($name, 5, "DbRep $name - ......... Table definition found: .........");
  5064. foreach my $tk (sort(keys(%$value))) {
  5065. Log3 ($name, 5, "DbRep $name - $tk: $value->{$tk}") if(defined($value->{$tk}) && $tk ne "Rows");
  5066. }
  5067. Log3 ($name, 5, "DbRep $name - ......... Table definition END ............");
  5068. # check for old MySQL3-Syntax Type=xxx
  5069. if (defined $value->{Type}) {
  5070. # port old index type to index engine, so we can use the index Engine in the rest of the script
  5071. $value->{Engine} = $value->{Type};
  5072. }
  5073. $db_tables{$value->{Name}} = $value;
  5074. }
  5075. @tablenames = sort(keys(%db_tables));
  5076. if (@tablenames < 1) {
  5077. $err = "There are no tables inside database $dbname ! It doesn't make sense to backup an empty database. Skipping this one.";
  5078. Log3 ($name, 2, "DbRep $name - $err");
  5079. $err = encode_base64($@,"");
  5080. $sth->finish;
  5081. $dbh->disconnect;
  5082. return "$name|''|$err|''|''";
  5083. }
  5084. # Tabellen optimieren
  5085. $hash->{HELPER}{DBTABLES} = \%db_tables;
  5086. ($err,$db_MB_start,$db_MB_end) = DbRep_mysqlOptimizeTables($hash,$dbh,@tablenames);
  5087. if ($err) {
  5088. $err = encode_base64($err,"");
  5089. return "$name|''|$err|''|''";
  5090. }
  5091. }
  5092. if ($dbmodel =~ /SQLITE/) {
  5093. # Anfangsgröße ermitteln
  5094. $db_MB_start = (split(' ',qx(du -m $hash->{DATABASE})))[0] if ($^O =~ m/linux/i || $^O =~ m/unix/i);
  5095. Log3 ($name, 3, "DbRep $name - Size of database $dbname before optimize (MB): $db_MB_start");
  5096. $query ="VACUUM";
  5097. Log3 ($name, 5, "DbRep $name - current query: $query ");
  5098. Log3 ($name, 3, "DbRep $name - VACUUM database $dbname....");
  5099. eval {$sth = $dbh->prepare($query);
  5100. $r = $sth->execute();
  5101. };
  5102. if ($@) {
  5103. $err = encode_base64($@,"");
  5104. Log3 ($name, 2, "DbRep $name - Error executing: '".$query."' ! SQLite-Error: ".$@);
  5105. $sth->finish;
  5106. $dbh->disconnect;
  5107. return "$name|''|$err|''|''";
  5108. }
  5109. # Endgröße ermitteln
  5110. $db_MB_end = (split(' ',qx(du -m $hash->{DATABASE})))[0] if ($^O =~ m/linux/i || $^O =~ m/unix/i);
  5111. Log3 ($name, 3, "DbRep $name - Size of database $dbname after optimize (MB): $db_MB_end");
  5112. }
  5113. if ($dbmodel =~ /POSTGRESQL/) {
  5114. # Anfangsgröße ermitteln
  5115. $query = "SELECT pg_size_pretty(pg_database_size('$dbname'))";
  5116. Log3 ($name, 5, "DbRep $name - current query: $query ");
  5117. eval { $sth = $dbh->prepare($query);
  5118. $sth->execute;
  5119. };
  5120. if ($@) {
  5121. $err = encode_base64($@,"");
  5122. Log3 ($name, 2, "DbRep $name - Error executing: '".$query."' ! PostgreSQL-Error: ".$@);
  5123. $sth->finish;
  5124. $dbh->disconnect;
  5125. return "$name|''|$err|''|''";
  5126. }
  5127. $value = $sth->fetchrow();
  5128. $value =~ tr/MB//d;
  5129. $db_MB_start = sprintf("%.2f",$value);
  5130. Log3 ($name, 3, "DbRep $name - Size of database $dbname before optimize (MB): $db_MB_start");
  5131. Log3 ($name, 3, "DbRep $name - VACUUM database $dbname....");
  5132. $query = "vacuum history";
  5133. Log3 ($name, 5, "DbRep $name - current query: $query ");
  5134. eval {$sth = $dbh->prepare($query);
  5135. $sth->execute();
  5136. };
  5137. if ($@) {
  5138. $err = encode_base64($@,"");
  5139. Log3 ($name, 2, "DbRep $name - Error executing: '".$query."' ! PostgreSQL-Error: ".$@);
  5140. $sth->finish;
  5141. $dbh->disconnect;
  5142. return "$name|''|$err|''|''";
  5143. }
  5144. # Endgröße ermitteln
  5145. $query = "SELECT pg_size_pretty(pg_database_size('$dbname'))";
  5146. Log3 ($name, 5, "DbRep $name - current query: $query ");
  5147. eval { $sth = $dbh->prepare($query);
  5148. $sth->execute;
  5149. };
  5150. if ($@) {
  5151. $err = encode_base64($@,"");
  5152. Log3 ($name, 2, "DbRep $name - Error executing: '".$query."' ! PostgreSQL-Error: ".$@);
  5153. $sth->finish;
  5154. $dbh->disconnect;
  5155. return "$name|''|$err|''|''";
  5156. }
  5157. $value = $sth->fetchrow();
  5158. $value =~ tr/MB//d;
  5159. $db_MB_end = sprintf("%.2f",$value);
  5160. Log3 ($name, 3, "DbRep $name - Size of database $dbname after optimize (MB): $db_MB_end");
  5161. }
  5162. $sth->finish;
  5163. $dbh->disconnect;
  5164. # SQL-Laufzeit ermitteln
  5165. my $rt = tv_interval($st);
  5166. # Background-Laufzeit ermitteln
  5167. my $brt = tv_interval($bst);
  5168. $rt = $rt.",".$brt;
  5169. Log3 ($name, 3, "DbRep $name - Optimize tables of database $dbname finished, total time used: ".sprintf("%.0f",$brt)." sec.");
  5170. return "$name|$rt|''|$db_MB_start|$db_MB_end";
  5171. }
  5172. ####################################################################################################
  5173. # Auswertungsroutine optimize tables
  5174. ####################################################################################################
  5175. sub DbRep_OptimizeDone($) {
  5176. my ($string) = @_;
  5177. my @a = split("\\|",$string);
  5178. my $hash = $defs{$a[0]};
  5179. my $bt = $a[1];
  5180. my ($rt,$brt) = split(",", $bt);
  5181. my $err = $a[2]?decode_base64($a[2]):undef;
  5182. my $db_MB_start = $a[3];
  5183. my $db_MB_end = $a[4];
  5184. my $name = $hash->{NAME};
  5185. my $erread;
  5186. delete($hash->{HELPER}{RUNNING_OPTIMIZE});
  5187. if ($err) {
  5188. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  5189. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  5190. return;
  5191. }
  5192. # only for this block because of warnings if details of readings are not set
  5193. no warnings 'uninitialized';
  5194. readingsBeginUpdate($hash);
  5195. ReadingsBulkUpdateValue($hash, "SizeDbBegin_MB", $db_MB_start);
  5196. ReadingsBulkUpdateValue($hash, "SizeDbEnd_MB", $db_MB_end);
  5197. readingsEndUpdate($hash, 1);
  5198. # Befehl nach Procedure ausführen
  5199. $erread = DbRep_afterproc($hash, "optimize");
  5200. my $state = $erread?$erread:"optimize tables finished";
  5201. readingsBeginUpdate($hash);
  5202. ReadingsBulkUpdateTimeState($hash,$brt,undef,$state);
  5203. readingsEndUpdate($hash, 1);
  5204. Log3 ($name, 3, "DbRep $name - Optimize tables finished successfully. ");
  5205. return;
  5206. }
  5207. ####################################################################################################
  5208. # nicht blockierende Dump-Routine für MySQL (clientSide)
  5209. ####################################################################################################
  5210. sub mysql_DoDumpClientSide($) {
  5211. my ($name) = @_;
  5212. my $hash = $defs{$name};
  5213. my $dbloghash = $hash->{dbloghash};
  5214. my $dbconn = $dbloghash->{dbconn};
  5215. my $dbuser = $dbloghash->{dbuser};
  5216. my $dblogname = $dbloghash->{NAME};
  5217. my $dbpassword = $attr{"sec$dblogname"}{secret};
  5218. my $dbname = $hash->{DATABASE};
  5219. my $dump_path_def = $attr{global}{modpath}."/log/";
  5220. my $dump_path = AttrVal($name, "dumpDirLocal", $dump_path_def);
  5221. $dump_path = $dump_path."/" unless($dump_path =~ m/\/$/);
  5222. my $optimize_tables_beforedump = AttrVal($name, "optimizeTablesBeforeDump", 0);
  5223. my $memory_limit = AttrVal($name, "dumpMemlimit", 100000);
  5224. my $my_comment = AttrVal($name, "dumpComment", "");
  5225. my $dumpspeed = AttrVal($name, "dumpSpeed", 10000);
  5226. my $ebd = AttrVal($name, "executeBeforeProc", undef);
  5227. my $ead = AttrVal($name, "executeAfterProc", undef);
  5228. my $mysql_commentstring = "-- ";
  5229. my $character_set = "utf8";
  5230. my $repver = $hash->{VERSION};
  5231. my $sql_text = '';
  5232. my $sql_file = '';
  5233. my $dbpraefix = "";
  5234. my ($dbh,$sth,$tablename,$sql_create,$rct,$insert,$first_insert,$backupfile,$drc,$drh,$e,
  5235. $sql_daten,$inhalt,$filesize,$totalrecords,$status_start,$status_end,$err,$db_MB_start,$db_MB_end);
  5236. my (@ar,@tablerecords,@tablenames,@tables,@ergebnis);
  5237. my (%db_tables);
  5238. # Background-Startzeit
  5239. my $bst = [gettimeofday];
  5240. Log3 ($name, 3, "DbRep $name - Starting dump of database '$dbname'");
  5241. ##################### Beginn Dump ########################
  5242. ##############################################################
  5243. undef(%db_tables);
  5244. # Startzeit ermitteln
  5245. my ($Sekunden, $Minuten, $Stunden, $Monatstag, $Monat, $Jahr, $Wochentag, $Jahrestag, $Sommerzeit) = localtime(time);
  5246. $Jahr += 1900;
  5247. $Monat += 1;
  5248. $Jahrestag += 1;
  5249. my $CTIME_String = strftime "%Y-%m-%d %T",localtime(time);
  5250. my $time_stamp = $Jahr."_".sprintf("%02d",$Monat)."_".sprintf("%02d",$Monatstag)."_".sprintf("%02d",$Stunden)."_".sprintf("%02d",$Minuten);
  5251. my $starttime = sprintf("%02d",$Monatstag).".".sprintf("%02d",$Monat).".".$Jahr." ".sprintf("%02d",$Stunden).":".sprintf("%02d",$Minuten);
  5252. my $fieldlist = "";
  5253. # Verbindung mit DB
  5254. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1 });};
  5255. if ($@) {
  5256. $e = $@;
  5257. $err = encode_base64($e,"");
  5258. Log3 ($name, 2, "DbRep $name - $e");
  5259. return "$name|''|$err|''|''|''|''|''|''|''";
  5260. }
  5261. # SQL-Startzeit
  5262. my $st = [gettimeofday];
  5263. ##################### Mysql-Version ermitteln ########################
  5264. eval { $sth = $dbh->prepare("SELECT VERSION()");
  5265. $sth->execute;
  5266. };
  5267. if ($@) {
  5268. $e = $@;
  5269. $err = encode_base64($e,"");
  5270. Log3 ($name, 2, "DbRep $name - $e");
  5271. $dbh->disconnect;
  5272. return "$name|''|$err|''|''|''|''|''|''|''";
  5273. }
  5274. my @mysql_version = $sth->fetchrow;
  5275. my @v = split(/\./,$mysql_version[0]);
  5276. if($v[0] >= 5 || ($v[0] >= 4 && $v[1] >= 1) ) {
  5277. # mysql Version >= 4.1
  5278. $sth = $dbh->prepare("SET NAMES '".$character_set."'");
  5279. $sth->execute;
  5280. # get standard encoding of MySQl-Server
  5281. $sth = $dbh->prepare("SHOW VARIABLES LIKE 'character_set_connection'");
  5282. $sth->execute;
  5283. @ar = $sth->fetchrow;
  5284. $character_set = $ar[1];
  5285. } else {
  5286. # mysql Version < 4.1 -> no SET NAMES available
  5287. # get standard encoding of MySQl-Server
  5288. $sth = $dbh->prepare("SHOW VARIABLES LIKE 'character_set'");
  5289. $sth->execute;
  5290. @ar = $sth->fetchrow;
  5291. if (defined($ar[1])) { $character_set=$ar[1]; }
  5292. }
  5293. Log3 ($name, 3, "DbRep $name - Characterset of collection and backup file set to $character_set. ");
  5294. # Eigenschaften der vorhandenen Tabellen ermitteln (SHOW TABLE STATUS -> Rows sind nicht exakt !!)
  5295. undef(@tables);
  5296. undef(@tablerecords);
  5297. my %db_tables_views;
  5298. my $t = 0;
  5299. my $r = 0;
  5300. my $st_e = "\n";
  5301. my $value = 0;
  5302. my $engine = '';
  5303. my $query ="SHOW TABLE STATUS FROM `$dbname`";
  5304. Log3 ($name, 5, "DbRep $name - current query: $query ");
  5305. if ($dbpraefix ne "") {
  5306. $query.=" LIKE '$dbpraefix%'";
  5307. Log3 ($name, 3, "DbRep $name - Searching for tables inside database $dbname with prefix $dbpraefix....");
  5308. } else {
  5309. Log3 ($name, 3, "DbRep $name - Searching for tables inside database $dbname....");
  5310. }
  5311. eval { $sth = $dbh->prepare($query);
  5312. $sth->execute;
  5313. };
  5314. if ($@) {
  5315. $err = encode_base64($@,"");
  5316. Log3 ($name, 2, "DbRep $name - Error executing: '".$query."' ! MySQL-Error: ".$@);
  5317. $dbh->disconnect;
  5318. return "$name|''|$err|''|''|''|''|''|''|''";
  5319. }
  5320. while ( $value = $sth->fetchrow_hashref()) {
  5321. $value->{skip_data} = 0; #defaut -> backup data of table
  5322. # verbose 5 logging
  5323. Log3 ($name, 5, "DbRep $name - ......... Table definition found: .........");
  5324. foreach my $tk (sort(keys(%$value))) {
  5325. Log3 ($name, 5, "DbRep $name - $tk: $value->{$tk}") if(defined($value->{$tk}) && $tk ne "Rows");
  5326. }
  5327. Log3 ($name, 5, "DbRep $name - ......... Table definition END ............");
  5328. # decide if we need to skip the data while dumping (VIEWs and MEMORY)
  5329. # check for old MySQL3-Syntax Type=xxx
  5330. if (defined $value->{Type}) {
  5331. # port old index type to index engine, so we can use the index Engine in the rest of the script
  5332. $value->{Engine} = $value->{Type};
  5333. $engine = uc($value->{Type});
  5334. if ($engine eq "MEMORY") {
  5335. $value->{skip_data} = 1;
  5336. }
  5337. }
  5338. # check for > MySQL3 Engine = xxx
  5339. if (defined $value->{Engine}) {
  5340. $engine = uc($value->{Engine});
  5341. if ($engine eq "MEMORY") {
  5342. $value->{skip_data} = 1;
  5343. }
  5344. }
  5345. # check for Views - if it is a view the comment starts with "VIEW"
  5346. if (defined $value->{Comment} && uc(substr($value->{Comment},0,4)) eq 'VIEW') {
  5347. $value->{skip_data} = 1;
  5348. $value->{Engine} = 'VIEW';
  5349. $value->{Update_time} = '';
  5350. $db_tables_views{$value->{Name}} = $value;
  5351. } else {
  5352. $db_tables{$value->{Name}} = $value;
  5353. }
  5354. # cast indexes to int, cause they are used for builing the statusline
  5355. $value->{Rows} += 0;
  5356. $value->{Data_length} += 0;
  5357. $value->{Index_length} += 0;
  5358. }
  5359. $sth->finish;
  5360. @tablenames = sort(keys(%db_tables));
  5361. # add VIEW at the end as they need all tables to be created before
  5362. @tablenames = (@tablenames,sort(keys(%db_tables_views)));
  5363. %db_tables = (%db_tables,%db_tables_views);
  5364. $tablename = '';
  5365. if (@tablenames < 1) {
  5366. $err = "There are no tables inside database $dbname ! It doesn't make sense to backup an empty database. Skipping this one.";
  5367. Log3 ($name, 2, "DbRep $name - $err");
  5368. $err = encode_base64($@,"");
  5369. $dbh->disconnect;
  5370. return "$name|''|$err|''|''|''|''|''|''|''";
  5371. }
  5372. if($optimize_tables_beforedump) {
  5373. # Tabellen optimieren vor dem Dump
  5374. $hash->{HELPER}{DBTABLES} = \%db_tables;
  5375. ($err,$db_MB_start,$db_MB_end) = DbRep_mysqlOptimizeTables($hash,$dbh,@tablenames);
  5376. if ($err) {
  5377. $err = encode_base64($err,"");
  5378. return "$name|''|$err|''|''|''|''|''|''|''";
  5379. }
  5380. }
  5381. # Tabelleneigenschaften für SQL-File ermitteln
  5382. $st_e .= "-- TABLE-INFO\n";
  5383. foreach $tablename (@tablenames) {
  5384. my $dump_table = 1;
  5385. if ($dbpraefix ne "") {
  5386. if (substr($tablename,0,length($dbpraefix)) ne $dbpraefix) {
  5387. # exclude table from backup because it doesn't fit to praefix
  5388. $dump_table = 0;
  5389. }
  5390. }
  5391. if ($dump_table == 1) {
  5392. # how many rows
  5393. $sql_create = "SELECT count(*) FROM `$tablename`";
  5394. eval { $sth = $dbh->prepare($sql_create);
  5395. $sth->execute;
  5396. };
  5397. if ($@) {
  5398. $e = $@;
  5399. $err = "Fatal error sending Query '".$sql_create."' ! MySQL-Error: ".$e;
  5400. Log3 ($name, 2, "DbRep $name - $err");
  5401. $err = encode_base64($e,"");
  5402. $dbh->disconnect;
  5403. return "$name|''|$err|''|''|''|''|''|''|''";
  5404. }
  5405. $db_tables{$tablename}{Rows} = $sth->fetchrow;
  5406. $sth->finish;
  5407. $r += $db_tables{$tablename}{Rows};
  5408. push(@tables,$db_tables{$tablename}{Name}); # add tablename to backuped tables
  5409. $t++;
  5410. if (!defined $db_tables{$tablename}{Update_time}) {
  5411. $db_tables{$tablename}{Update_time} = 0;
  5412. }
  5413. $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";
  5414. if($db_tables{$tablename}{Name} eq "current") {
  5415. $drc = $db_tables{$tablename}{Rows};
  5416. }
  5417. if($db_tables{$tablename}{Name} eq "history") {
  5418. $drh = $db_tables{$tablename}{Rows};
  5419. }
  5420. }
  5421. }
  5422. $st_e .= "-- EOF TABLE-INFO";
  5423. Log3 ($name, 3, "DbRep $name - Found ".(@tables)." tables with $r records.");
  5424. # AUFBAU der Statuszeile in SQL-File:
  5425. # -- Status | tabellenzahl | datensaetze | Datenbankname | Kommentar | MySQLVersion | Charset | EXTINFO
  5426. #
  5427. $status_start = $mysql_commentstring."Status | Tables: $t | Rows: $r ";
  5428. $status_end = "| DB: $dbname | Comment: $my_comment | MySQL-Version: $mysql_version[0] ";
  5429. $status_end .= "| Charset: $character_set $st_e\n".
  5430. $mysql_commentstring."Dump created on $CTIME_String by DbRep-Version $repver\n".$mysql_commentstring;
  5431. $sql_text = $status_start.$status_end;
  5432. # neues SQL-Ausgabefile anlegen
  5433. ($sql_text,$first_insert,$sql_file,$backupfile,$err) = DbRep_NewDumpFilename($sql_text,$dump_path,$dbname,$time_stamp,$character_set);
  5434. if ($err) {
  5435. Log3 ($name, 2, "DbRep $name - $err");
  5436. $err = encode_base64($err,"");
  5437. return "$name|''|$err|''|''|''|''|''|''|''";
  5438. } else {
  5439. Log3 ($name, 5, "DbRep $name - New dumpfile $sql_file has been created.");
  5440. }
  5441. ##################### jede einzelne Tabelle dumpen ########################
  5442. $totalrecords = 0;
  5443. foreach $tablename (@tables) {
  5444. # first get CREATE TABLE Statement
  5445. if($dbpraefix eq "" || ($dbpraefix ne "" && substr($tablename,0,length($dbpraefix)) eq $dbpraefix)) {
  5446. Log3 ($name, 3, "DbRep $name - Dumping table $tablename (Type ".$db_tables{$tablename}{Engine}."):");
  5447. $a = "\n\n$mysql_commentstring\n$mysql_commentstring"."Table structure for table `$tablename`\n$mysql_commentstring\n";
  5448. if ($db_tables{$tablename}{Engine} ne 'VIEW' ) {
  5449. $a .= "DROP TABLE IF EXISTS `$tablename`;\n";
  5450. } else {
  5451. $a .= "DROP VIEW IF EXISTS `$tablename`;\n";
  5452. }
  5453. $sql_text .= $a;
  5454. $sql_create = "SHOW CREATE TABLE `$tablename`";
  5455. Log3 ($name, 5, "DbRep $name - current query: $sql_create ");
  5456. eval { $sth = $dbh->prepare($sql_create);
  5457. $sth->execute;
  5458. };
  5459. if ($@) {
  5460. $e = $@;
  5461. $err = "Fatal error sending Query '".$sql_create."' ! MySQL-Error: ".$e;
  5462. Log3 ($name, 2, "DbRep $name - $err");
  5463. $err = encode_base64($e,"");
  5464. $dbh->disconnect;
  5465. return "$name|''|$err|''|''|''|''|''|''|''";
  5466. }
  5467. @ergebnis = $sth->fetchrow;
  5468. $sth->finish;
  5469. $a = $ergebnis[1].";\n";
  5470. if (length($a) < 10) {
  5471. $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;
  5472. Log3 ($name, 2, "DbRep $name - $err");
  5473. } else {
  5474. $sql_text .= $a;
  5475. # verbose 5 logging
  5476. Log3 ($name, 5, "DbRep $name - Create-SQL found:\n$a");
  5477. }
  5478. if ($db_tables{$tablename}{skip_data} == 0) {
  5479. $sql_text .= "\n$mysql_commentstring\n$mysql_commentstring"."Dumping data for table `$tablename`\n$mysql_commentstring\n";
  5480. $sql_text .= "/*!40000 ALTER TABLE `$tablename` DISABLE KEYS */;";
  5481. DbRep_WriteToDumpFile($sql_text,$sql_file);
  5482. $sql_text = "";
  5483. # build fieldlist
  5484. $fieldlist = "(";
  5485. $sql_create = "SHOW FIELDS FROM `$tablename`";
  5486. Log3 ($name, 5, "DbRep $name - current query: $sql_create ");
  5487. eval { $sth = $dbh->prepare($sql_create);
  5488. $sth->execute;
  5489. };
  5490. if ($@) {
  5491. $e = $@;
  5492. $err = "Fatal error sending Query '".$sql_create."' ! MySQL-Error: ".$e;
  5493. Log3 ($name, 2, "DbRep $name - $err");
  5494. $err = encode_base64($e,"");
  5495. $dbh->disconnect;
  5496. return "$name|''|$err|''|''|''|''|''|''|''";
  5497. }
  5498. while (@ar = $sth->fetchrow) {
  5499. $fieldlist .= "`".$ar[0]."`,";
  5500. }
  5501. $sth->finish;
  5502. # verbose 5 logging
  5503. Log3 ($name, 5, "DbRep $name - Fieldlist found: $fieldlist");
  5504. # remove trailing ',' and add ')'
  5505. $fieldlist = substr($fieldlist,0,length($fieldlist)-1).")";
  5506. # how many rows
  5507. $rct = $db_tables{$tablename}{Rows};
  5508. Log3 ($name, 5, "DbRep $name - Number entries of table $tablename: $rct");
  5509. # create insert Statements
  5510. for (my $ttt = 0; $ttt < $rct; $ttt += $dumpspeed) {
  5511. # default beginning for INSERT-String
  5512. $insert = "INSERT INTO `$tablename` $fieldlist VALUES (";
  5513. $first_insert = 0;
  5514. # get rows (parts)
  5515. $sql_daten = "SELECT * FROM `$tablename` LIMIT ".$ttt.",".$dumpspeed.";";
  5516. eval { $sth = $dbh->prepare($sql_daten);
  5517. $sth->execute;
  5518. };
  5519. if ($@) {
  5520. $e = $@;
  5521. $err = "Fatal error sending Query '".$sql_daten."' ! MySQL-Error: ".$e;
  5522. Log3 ($name, 2, "DbRep $name - $err");
  5523. $err = encode_base64($e,"");
  5524. $dbh->disconnect;
  5525. return "$name|''|$err|''|''|''|''|''|''|''";
  5526. }
  5527. while ( @ar = $sth->fetchrow) {
  5528. #Start the insert
  5529. if($first_insert == 0) {
  5530. $a = "\n$insert";
  5531. } else {
  5532. $a = "\n(";
  5533. }
  5534. # quote all values
  5535. foreach $inhalt(@ar) { $a .= $dbh->quote($inhalt).","; }
  5536. # remove trailing ',' and add end-sql
  5537. $a = substr($a,0, length($a)-1).");";
  5538. $sql_text .= $a;
  5539. if($memory_limit > 0 && length($sql_text) > $memory_limit) {
  5540. ($filesize,$err) = DbRep_WriteToDumpFile($sql_text,$sql_file);
  5541. # Log3 ($name, 5, "DbRep $name - Memory limit '$memory_limit' exceeded. Wrote to '$sql_file'. Filesize: '".DbRep_byteOutput($filesize)."'");
  5542. $sql_text = "";
  5543. }
  5544. }
  5545. $sth->finish;
  5546. }
  5547. $sql_text .= "\n/*!40000 ALTER TABLE `$tablename` ENABLE KEYS */;\n";
  5548. }
  5549. # write sql commands to file
  5550. ($filesize,$err) = DbRep_WriteToDumpFile($sql_text,$sql_file);
  5551. $sql_text = "";
  5552. if ($db_tables{$tablename}{skip_data} == 0) {
  5553. Log3 ($name, 3, "DbRep $name - $rct records inserted (size of backupfile: ".DbRep_byteOutput($filesize).")") if($filesize);
  5554. $totalrecords += $rct;
  5555. } else {
  5556. Log3 ($name, 3, "DbRep $name - Dumping structure of $tablename (Type ".$db_tables{$tablename}{Engine}." ) (size of backupfile: ".DbRep_byteOutput($filesize).")");
  5557. }
  5558. }
  5559. }
  5560. # end
  5561. DbRep_WriteToDumpFile("\nSET FOREIGN_KEY_CHECKS=1;\n",$sql_file);
  5562. ($filesize,$err) = DbRep_WriteToDumpFile($mysql_commentstring."EOB\n",$sql_file);
  5563. # Datenbankverbindung schliessen
  5564. $sth->finish() if (defined $sth);
  5565. $dbh->disconnect();
  5566. # SQL-Laufzeit ermitteln
  5567. my $rt = tv_interval($st);
  5568. # Dumpfile komprimieren wenn dumpCompress=1
  5569. my $compress = AttrVal($name,"dumpCompress",0);
  5570. if($compress) {
  5571. # $err nicht auswerten -> wenn compress fehlerhaft wird unkomprimiertes dumpfile verwendet
  5572. ($err,$backupfile) = DbRep_dumpCompress($hash,$backupfile);
  5573. my $fref = stat("$dump_path$backupfile");
  5574. if ($fref =~ /ARRAY/) {
  5575. $filesize = (@{stat("$dump_path$backupfile")})[7];
  5576. } else {
  5577. $filesize = (stat("$dump_path$backupfile"))[7];
  5578. }
  5579. }
  5580. # Dumpfile per FTP senden und versionieren
  5581. my ($ftperr,$ftpmsg,@ftpfd) = DbRep_sendftp($hash,$backupfile);
  5582. my $ftp = $ftperr?encode_base64($ftperr,""):$ftpmsg?encode_base64($ftpmsg,""):0;
  5583. my $ffd = join(", ", @ftpfd);
  5584. $ffd = $ffd?encode_base64($ffd,""):0;
  5585. # alte Dumpfiles löschen
  5586. my @fd = DbRep_deldumpfiles($hash,$backupfile);
  5587. my $bfd = join(", ", @fd );
  5588. $bfd = $bfd?encode_base64($bfd,""):0;
  5589. # Background-Laufzeit ermitteln
  5590. my $brt = tv_interval($bst);
  5591. $rt = $rt.",".$brt;
  5592. my $fsize = '';
  5593. if($filesize) {
  5594. $fsize = DbRep_byteOutput($filesize);
  5595. $fsize = encode_base64($fsize,"");
  5596. }
  5597. Log3 ($name, 3, "DbRep $name - Finished backup of database $dbname, total time used: ".sprintf("%.0f",$brt)." sec.");
  5598. return "$name|$rt|''|$dump_path$backupfile|$drc|$drh|$fsize|$ftp|$bfd|$ffd";
  5599. }
  5600. ####################################################################################################
  5601. # nicht blockierende Dump-Routine für MySQL (serverSide)
  5602. ####################################################################################################
  5603. sub mysql_DoDumpServerSide($) {
  5604. my ($name) = @_;
  5605. my $hash = $defs{$name};
  5606. my $dbloghash = $hash->{dbloghash};
  5607. my $dbconn = $dbloghash->{dbconn};
  5608. my $dbuser = $dbloghash->{dbuser};
  5609. my $dblogname = $dbloghash->{NAME};
  5610. my $dbpassword = $attr{"sec$dblogname"}{secret};
  5611. my $dbname = $hash->{DATABASE};
  5612. my $optimize_tables_beforedump = AttrVal($name, "optimizeTablesBeforeDump", 0);
  5613. my $dump_path_rem = AttrVal($name, "dumpDirRemote", "./");
  5614. $dump_path_rem = $dump_path_rem."/" unless($dump_path_rem =~ m/\/$/);
  5615. my $ebd = AttrVal($name, "executeBeforeProc", undef);
  5616. my $ead = AttrVal($name, "executeAfterProc", undef);
  5617. my $table = "history";
  5618. my ($dbh,$sth,$err,$db_MB_start,$db_MB_end,$drh);
  5619. my (%db_tables,@tablenames);
  5620. # Background-Startzeit
  5621. my $bst = [gettimeofday];
  5622. # Verbindung mit DB
  5623. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1 });};
  5624. if ($@) {
  5625. $err = encode_base64($@,"");
  5626. Log3 ($name, 2, "DbRep $name - $@");
  5627. return "$name|''|$err|''|''|''|''|''|''|''";
  5628. }
  5629. # Eigenschaften der vorhandenen Tabellen ermitteln (SHOW TABLE STATUS -> Rows sind nicht exakt !!)
  5630. my $value = 0;
  5631. my $query ="SHOW TABLE STATUS FROM `$dbname`";
  5632. Log3 ($name, 5, "DbRep $name - current query: $query ");
  5633. Log3 ($name, 3, "DbRep $name - Searching for tables inside database $dbname....");
  5634. eval { $sth = $dbh->prepare($query);
  5635. $sth->execute;
  5636. };
  5637. if ($@) {
  5638. $err = encode_base64($@,"");
  5639. Log3 ($name, 2, "DbRep $name - Error executing: '".$query."' ! MySQL-Error: ".$@);
  5640. $dbh->disconnect;
  5641. return "$name|''|$err|''|''|''|''|''|''|''";
  5642. }
  5643. while ( $value = $sth->fetchrow_hashref()) {
  5644. # verbose 5 logging
  5645. Log3 ($name, 5, "DbRep $name - ......... Table definition found: .........");
  5646. foreach my $tk (sort(keys(%$value))) {
  5647. Log3 ($name, 5, "DbRep $name - $tk: $value->{$tk}") if(defined($value->{$tk}) && $tk ne "Rows");
  5648. }
  5649. Log3 ($name, 5, "DbRep $name - ......... Table definition END ............");
  5650. # check for old MySQL3-Syntax Type=xxx
  5651. if (defined $value->{Type}) {
  5652. # port old index type to index engine, so we can use the index Engine in the rest of the script
  5653. $value->{Engine} = $value->{Type};
  5654. }
  5655. $db_tables{$value->{Name}} = $value;
  5656. }
  5657. $sth->finish;
  5658. @tablenames = sort(keys(%db_tables));
  5659. if (@tablenames < 1) {
  5660. $err = "There are no tables inside database $dbname ! It doesn't make sense to backup an empty database. Skipping this one.";
  5661. Log3 ($name, 2, "DbRep $name - $err");
  5662. $err = encode_base64($@,"");
  5663. $dbh->disconnect;
  5664. return "$name|''|$err|''|''|''|''|''|''|''";
  5665. }
  5666. if($optimize_tables_beforedump) {
  5667. # Tabellen optimieren vor dem Dump
  5668. $hash->{HELPER}{DBTABLES} = \%db_tables;
  5669. ($err,$db_MB_start,$db_MB_end) = DbRep_mysqlOptimizeTables($hash,$dbh,@tablenames);
  5670. if ($err) {
  5671. $err = encode_base64($err,"");
  5672. return "$name|''|$err|''|''|''|''|''|''|''";
  5673. }
  5674. }
  5675. Log3 ($name, 3, "DbRep $name - Starting dump of database '$dbname', table '$table'");
  5676. # Startzeit ermitteln
  5677. my ($Sekunden, $Minuten, $Stunden, $Monatstag, $Monat, $Jahr, $Wochentag, $Jahrestag, $Sommerzeit) = localtime(time);
  5678. $Jahr += 1900;
  5679. $Monat += 1;
  5680. $Jahrestag += 1;
  5681. my $time_stamp = $Jahr."_".sprintf("%02d",$Monat)."_".sprintf("%02d",$Monatstag)."_".sprintf("%02d",$Stunden)."_".sprintf("%02d",$Minuten);
  5682. my $bfile = $dbname."_".$table."_".$time_stamp.".csv";
  5683. Log3 ($name, 5, "DbRep $name - Use Outfile: $dump_path_rem$bfile");
  5684. # SQL-Startzeit
  5685. my $st = [gettimeofday];
  5686. my $sql = "SELECT * FROM history INTO OUTFILE '$dump_path_rem$bfile' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n'; ";
  5687. eval {$sth = $dbh->prepare($sql);
  5688. $drh = $sth->execute();
  5689. };
  5690. if ($@) {
  5691. # error bei sql-execute
  5692. $err = encode_base64($@,"");
  5693. Log3 ($name, 2, "DbRep $name - $@");
  5694. $dbh->disconnect;
  5695. return "$name|''|$err|''|''|''|''|''|''|''";
  5696. }
  5697. $sth->finish;
  5698. $dbh->disconnect;
  5699. # SQL-Laufzeit ermitteln
  5700. my $rt = tv_interval($st);
  5701. # Dumpfile komprimieren wenn dumpCompress=1
  5702. my $compress = AttrVal($name,"dumpCompress",0);
  5703. if($compress) {
  5704. # $err nicht auswerten -> wenn compress fehlerhaft wird unkomprimiertes dumpfile verwendet
  5705. ($err,$bfile) = DbRep_dumpCompress($hash,$bfile);
  5706. }
  5707. # Größe Dumpfile ermitteln ("dumpDirRemote" muß auf "dumpDirLocal" gemountet sein)
  5708. my $dump_path_def = $attr{global}{modpath}."/log/";
  5709. my $dump_path_loc = AttrVal($name,"dumpDirLocal", $dump_path_def);
  5710. $dump_path_loc = $dump_path_loc."/" unless($dump_path_loc =~ m/\/$/);
  5711. my $filesize;
  5712. my $fref = stat($dump_path_loc.$bfile);
  5713. if ($fref =~ /ARRAY/) {
  5714. $filesize = (@{stat($dump_path_loc.$bfile)})[7];
  5715. } else {
  5716. $filesize = (stat($dump_path_loc.$bfile))[7];
  5717. }
  5718. Log3 ($name, 3, "DbRep $name - Number of exported datasets: $drh");
  5719. Log3 ($name, 3, "DbRep $name - Size of backupfile: ".DbRep_byteOutput($filesize)) if($filesize);
  5720. # Dumpfile per FTP senden und versionieren
  5721. my ($ftperr,$ftpmsg,@ftpfd) = DbRep_sendftp($hash,$bfile);
  5722. my $ftp = $ftperr?encode_base64($ftperr,""):$ftpmsg?encode_base64($ftpmsg,""):0;
  5723. my $ffd = join(", ", @ftpfd);
  5724. $ffd = $ffd?encode_base64($ffd,""):0;
  5725. # alte Dumpfiles löschen
  5726. my @fd = DbRep_deldumpfiles($hash,$bfile);
  5727. my $bfd = join(", ", @fd );
  5728. $bfd = $bfd?encode_base64($bfd,""):0;
  5729. # Background-Laufzeit ermitteln
  5730. my $brt = tv_interval($bst);
  5731. my $fsize = '';
  5732. if($filesize) {
  5733. $fsize = DbRep_byteOutput($filesize);
  5734. $fsize = encode_base64($fsize,"");
  5735. }
  5736. $rt = $rt.",".$brt;
  5737. Log3 ($name, 3, "DbRep $name - Finished backup of database $dbname - total time used: ".sprintf("%.0f",$brt)." seconds");
  5738. return "$name|$rt|''|$dump_path_rem$bfile|n.a.|$drh|$fsize|$ftp|$bfd|$ffd";
  5739. }
  5740. ####################################################################################################
  5741. # Dump-Routine SQLite
  5742. ####################################################################################################
  5743. sub DbRep_sqliteDoDump($) {
  5744. my ($name) = @_;
  5745. my $hash = $defs{$name};
  5746. my $dbloghash = $hash->{dbloghash};
  5747. my $dbname = $hash->{DATABASE};
  5748. my $dbconn = $dbloghash->{dbconn};
  5749. my $dbuser = $dbloghash->{dbuser};
  5750. my $dblogname = $dbloghash->{NAME};
  5751. my $dbpassword = $attr{"sec$dblogname"}{secret};
  5752. my $dump_path_def = $attr{global}{modpath}."/log/";
  5753. my $dump_path = AttrVal($name, "dumpDirLocal", $dump_path_def);
  5754. $dump_path = $dump_path."/" unless($dump_path =~ m/\/$/);
  5755. my $optimize_tables_beforedump = AttrVal($name, "optimizeTablesBeforeDump", 0);
  5756. my $ebd = AttrVal($name, "executeBeforeProc", undef);
  5757. my $ead = AttrVal($name, "executeAfterProc", undef);
  5758. my ($dbh,$err,$db_MB,$r,$query,$sth);
  5759. # Background-Startzeit
  5760. my $bst = [gettimeofday];
  5761. # Verbindung mit DB
  5762. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1 });};
  5763. if ($@) {
  5764. $err = encode_base64($@,"");
  5765. Log3 ($name, 2, "DbRep $name - $@");
  5766. return "$name|''|$err|''|''|''|''|''|''|''";
  5767. }
  5768. if($optimize_tables_beforedump) {
  5769. # Vacuum vor Dump
  5770. # Anfangsgröße ermitteln
  5771. $db_MB = (split(' ',qx(du -m $dbname)))[0] if ($^O =~ m/linux/i || $^O =~ m/unix/i);
  5772. Log3 ($name, 3, "DbRep $name - Size of database $dbname before optimize (MB): $db_MB");
  5773. $query ="VACUUM";
  5774. Log3 ($name, 5, "DbRep $name - current query: $query ");
  5775. Log3 ($name, 3, "DbRep $name - VACUUM database $dbname....");
  5776. eval {$sth = $dbh->prepare($query);
  5777. $r = $sth->execute();
  5778. };
  5779. if ($@) {
  5780. $err = encode_base64($@,"");
  5781. Log3 ($name, 2, "DbRep $name - Error executing: '".$query."' ! SQLite-Error: ".$@);
  5782. $sth->finish;
  5783. $dbh->disconnect;
  5784. return "$name|''|$err|''|''|''|''|''|''|''";
  5785. }
  5786. # Endgröße ermitteln
  5787. $db_MB = (split(' ',qx(du -m $dbname)))[0] if ($^O =~ m/linux/i || $^O =~ m/unix/i);
  5788. Log3 ($name, 3, "DbRep $name - Size of database $dbname after optimize (MB): $db_MB");
  5789. }
  5790. $dbname = (split /[\/]/, $dbname)[-1];
  5791. Log3 ($name, 3, "DbRep $name - Starting dump of database '$dbname'");
  5792. # Startzeit ermitteln
  5793. my ($Sekunden, $Minuten, $Stunden, $Monatstag, $Monat, $Jahr, $Wochentag, $Jahrestag, $Sommerzeit) = localtime(time);
  5794. $Jahr += 1900;
  5795. $Monat += 1;
  5796. $Jahrestag += 1;
  5797. my $time_stamp = $Jahr."_".sprintf("%02d",$Monat)."_".sprintf("%02d",$Monatstag)."_".sprintf("%02d",$Stunden)."_".sprintf("%02d",$Minuten);
  5798. $dbname = (split /\./, $dbname)[0];
  5799. my $bfile = $dbname."_".$time_stamp.".sqlitebkp";
  5800. Log3 ($name, 5, "DbRep $name - Use Outfile: $dump_path$bfile");
  5801. # SQL-Startzeit
  5802. my $st = [gettimeofday];
  5803. eval { $dbh->sqlite_backup_to_file($dump_path.$bfile); };
  5804. if ($@) {
  5805. $err = encode_base64($@,"");
  5806. Log3 ($name, 2, "DbRep $name - $@");
  5807. $dbh->disconnect;
  5808. return "$name|''|$err|''|''|''|''|''|''|''";
  5809. }
  5810. $dbh->disconnect;
  5811. # SQL-Laufzeit ermitteln
  5812. my $rt = tv_interval($st);
  5813. # Dumpfile komprimieren
  5814. my $compress = AttrVal($name,"dumpCompress",0);
  5815. if($compress) {
  5816. # $err nicht auswerten -> wenn compress fehlerhaft wird unkomprimiertes dumpfile verwendet
  5817. ($err,$bfile) = DbRep_dumpCompress($hash,$bfile);
  5818. }
  5819. # Größe Dumpfile ermitteln
  5820. my @a = split(' ',qx(du $dump_path$bfile)) if ($^O =~ m/linux/i || $^O =~ m/unix/i);
  5821. my $filesize = ($a[0])?($a[0]*1024):"n.a.";
  5822. my $fsize = DbRep_byteOutput($filesize);
  5823. Log3 ($name, 3, "DbRep $name - Size of backupfile: ".$fsize);
  5824. # Dumpfile per FTP senden und versionieren
  5825. my ($ftperr,$ftpmsg,@ftpfd) = DbRep_sendftp($hash,$bfile);
  5826. my $ftp = $ftperr?encode_base64($ftperr,""):$ftpmsg?encode_base64($ftpmsg,""):0;
  5827. my $ffd = join(", ", @ftpfd);
  5828. $ffd = $ffd?encode_base64($ffd,""):0;
  5829. # alte Dumpfiles löschen
  5830. my @fd = DbRep_deldumpfiles($hash,$bfile);
  5831. my $bfd = join(", ", @fd );
  5832. $bfd = $bfd?encode_base64($bfd,""):0;
  5833. # Background-Laufzeit ermitteln
  5834. my $brt = tv_interval($bst);
  5835. $fsize = encode_base64($fsize,"");
  5836. $rt = $rt.",".$brt;
  5837. Log3 ($name, 3, "DbRep $name - Finished backup of database $dbname - total time used: ".sprintf("%.0f",$brt)." seconds");
  5838. return "$name|$rt|''|$dump_path$bfile|n.a.|n.a.|$fsize|$ftp|$bfd|$ffd";
  5839. }
  5840. ####################################################################################################
  5841. # Auswertungsroutine der nicht blockierenden DB-Funktion Dump
  5842. ####################################################################################################
  5843. sub DbRep_DumpDone($) {
  5844. my ($string) = @_;
  5845. my @a = split("\\|",$string);
  5846. my $hash = $defs{$a[0]};
  5847. my $bt = $a[1];
  5848. my ($rt,$brt) = split(",", $bt);
  5849. my $err = $a[2]?decode_base64($a[2]):undef;
  5850. my $bfile = $a[3];
  5851. my $drc = $a[4];
  5852. my $drh = $a[5];
  5853. my $fs = $a[6]?decode_base64($a[6]):undef;
  5854. my $ftp = $a[7]?decode_base64($a[7]):undef;
  5855. my $bfd = $a[8]?decode_base64($a[8]):undef;
  5856. my $ffd = $a[9]?decode_base64($a[9]):undef;
  5857. my $name = $hash->{NAME};
  5858. my $erread;
  5859. delete($hash->{HELPER}{RUNNING_BACKUP_CLIENT});
  5860. delete($hash->{HELPER}{RUNNING_BCKPREST_SERVER});
  5861. if ($err) {
  5862. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  5863. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  5864. return;
  5865. }
  5866. # only for this block because of warnings if details of readings are not set
  5867. no warnings 'uninitialized';
  5868. readingsBeginUpdate($hash);
  5869. ReadingsBulkUpdateValue($hash, "DumpFileCreated", $bfile);
  5870. ReadingsBulkUpdateValue($hash, "DumpFileCreatedSize", $fs);
  5871. ReadingsBulkUpdateValue($hash, "DumpFilesDeleted", $bfd);
  5872. ReadingsBulkUpdateValue($hash, "DumpRowsCurrent", $drc);
  5873. ReadingsBulkUpdateValue($hash, "DumpRowsHistory", $drh);
  5874. ReadingsBulkUpdateValue($hash, "FTP_Message", $ftp) if($ftp);
  5875. ReadingsBulkUpdateValue($hash, "FTP_DumpFilesDeleted", $ffd) if($ffd);
  5876. ReadingsBulkUpdateValue($hash, "background_processing_time", sprintf("%.4f",$brt));
  5877. readingsEndUpdate($hash, 1);
  5878. # Befehl nach Procedure ausführen
  5879. $erread = DbRep_afterproc($hash, "dump");
  5880. my $state = $erread?$erread:"Database backup finished";
  5881. readingsBeginUpdate($hash);
  5882. ReadingsBulkUpdateTimeState($hash,undef,undef,$state);
  5883. readingsEndUpdate($hash, 1);
  5884. Log3 ($name, 3, "DbRep $name - Database dump finished successfully. ");
  5885. return;
  5886. }
  5887. ####################################################################################################
  5888. # Dump-Routine SQLite
  5889. ####################################################################################################
  5890. sub DbRep_sqliteRepair($) {
  5891. my ($name) = @_;
  5892. my $hash = $defs{$name};
  5893. my $dbloghash = $hash->{dbloghash};
  5894. my $db = $hash->{DATABASE};
  5895. my $dbname = (split /[\/]/, $db)[-1];
  5896. my $dbpath = (split /$dbname/, $db)[0];
  5897. my $dblogname = $dbloghash->{NAME};
  5898. my $sqlfile = $dbpath."dump_all.sql";
  5899. my ($c,$clog,$ret,$err);
  5900. # Background-Startzeit
  5901. my $bst = [gettimeofday];
  5902. $c = "echo \".mode insert\n.output $sqlfile\n.dump\n.exit\" | sqlite3 $db; ";
  5903. $clog = $c;
  5904. $clog =~ s/\n/ /g;
  5905. Log3 ($name, 4, "DbRep $name - Systemcall: $clog");
  5906. $ret = system qq($c);
  5907. if($ret) {
  5908. $err = "Error in step \"dump corrupt database\" - see logfile";
  5909. $err = encode_base64($err,"");
  5910. return "$name|''|$err";
  5911. }
  5912. $c = "mv $db $db.corrupt";
  5913. $clog = $c;
  5914. $clog =~ s/\n/ /g;
  5915. Log3 ($name, 4, "DbRep $name - Systemcall: $clog");
  5916. $ret = system qq($c);
  5917. if($ret) {
  5918. $err = "Error in step \"move atabase to corrupt-db\" - see logfile";
  5919. $err = encode_base64($err,"");
  5920. return "$name|''|$err";
  5921. }
  5922. $c = "echo \".read $sqlfile\n.exit\" | sqlite3 $db;";
  5923. $clog = $c;
  5924. $clog =~ s/\n/ /g;
  5925. Log3 ($name, 4, "DbRep $name - Systemcall: $clog");
  5926. $ret = system qq($c);
  5927. if($ret) {
  5928. $err = "Error in step \"read dump to new database\" - see logfile";
  5929. $err = encode_base64($err,"");
  5930. return "$name|''|$err";
  5931. }
  5932. $c = "rm $sqlfile";
  5933. $clog = $c;
  5934. $clog =~ s/\n/ /g;
  5935. Log3 ($name, 4, "DbRep $name - Systemcall: $clog");
  5936. $ret = system qq($c);
  5937. if($ret) {
  5938. $err = "Error in step \"delete $sqlfile\" - see logfile";
  5939. $err = encode_base64($err,"");
  5940. return "$name|''|$err";
  5941. }
  5942. # Background-Laufzeit ermitteln
  5943. my $brt = tv_interval($bst);
  5944. return "$name|$brt|0";
  5945. }
  5946. ####################################################################################################
  5947. # Auswertungsroutine der nicht blockierenden DB-Funktion Dump
  5948. ####################################################################################################
  5949. sub DbRep_RepairDone($) {
  5950. my ($string) = @_;
  5951. my @a = split("\\|",$string);
  5952. my $hash = $defs{$a[0]};
  5953. my $brt = $a[1];
  5954. my $err = $a[2]?decode_base64($a[2]):undef;
  5955. my $dbloghash = $hash->{dbloghash};
  5956. my $name = $hash->{NAME};
  5957. my $erread;
  5958. delete($hash->{HELPER}{RUNNING_REPAIR});
  5959. # Datenbankverbindung in DbLog wieder öffenen
  5960. my $dbl = $dbloghash->{NAME};
  5961. CommandSet(undef,"$dbl reopen");
  5962. if ($err) {
  5963. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  5964. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  5965. return;
  5966. }
  5967. # only for this block because of warnings if details of readings are not set
  5968. no warnings 'uninitialized';
  5969. readingsBeginUpdate($hash);
  5970. ReadingsBulkUpdateValue($hash, "background_processing_time", sprintf("%.4f",$brt));
  5971. readingsEndUpdate($hash, 1);
  5972. # Befehl nach Procedure ausführen
  5973. $erread = DbRep_afterproc($hash, "repair");
  5974. my $state = $erread?$erread:"Repair finished $hash->{DATABASE}";
  5975. readingsBeginUpdate($hash);
  5976. ReadingsBulkUpdateTimeState($hash,undef,undef,$state);
  5977. readingsEndUpdate($hash, 1);
  5978. Log3 ($name, 3, "DbRep $name - Database repair $hash->{DATABASE} finished. - total time used: ".sprintf("%.0f",$brt)." seconds.");
  5979. return;
  5980. }
  5981. ####################################################################################################
  5982. # Restore SQLite
  5983. ####################################################################################################
  5984. sub DbRep_sqliteRestore ($) {
  5985. my ($string) = @_;
  5986. my ($name,$bfile) = split("\\|", $string);
  5987. my $hash = $defs{$name};
  5988. my $dbloghash = $hash->{dbloghash};
  5989. my $dbconn = $dbloghash->{dbconn};
  5990. my $dbuser = $dbloghash->{dbuser};
  5991. my $dblogname = $dbloghash->{NAME};
  5992. my $dbpassword = $attr{"sec$dblogname"}{secret};
  5993. my $dump_path_def = $attr{global}{modpath}."/log/";
  5994. my $dump_path = AttrVal($name, "dumpDirLocal", $dump_path_def);
  5995. $dump_path = $dump_path."/" unless($dump_path =~ m/\/$/);
  5996. my $ebd = AttrVal($name, "executeBeforeProc", undef);
  5997. my $ead = AttrVal($name, "executeAfterProc", undef);
  5998. my ($dbh,$err,$dbname);
  5999. # Background-Startzeit
  6000. my $bst = [gettimeofday];
  6001. # Verbindung mit DB
  6002. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1 });};
  6003. if ($@) {
  6004. $err = encode_base64($@,"");
  6005. Log3 ($name, 2, "DbRep $name - $@");
  6006. return "$name|''|$err|''|''";
  6007. }
  6008. eval { $dbname = $dbh->sqlite_db_filename(); };
  6009. if ($@) {
  6010. $err = encode_base64($@,"");
  6011. Log3 ($name, 2, "DbRep $name - $@");
  6012. $dbh->disconnect;
  6013. return "$name|''|$err|''|''";
  6014. }
  6015. $dbname = (split /[\/]/, $dbname)[-1];
  6016. # Dumpfile dekomprimieren wenn gzip
  6017. if($bfile =~ m/.*.gzip$/) {
  6018. ($err,$bfile) = DbRep_dumpUnCompress($hash,$bfile);
  6019. if ($err) {
  6020. $err = encode_base64($err,"");
  6021. $dbh->disconnect;
  6022. return "$name|''|$err|''|''";
  6023. }
  6024. }
  6025. Log3 ($name, 3, "DbRep $name - Starting restore of database '$dbname'");
  6026. # SQL-Startzeit
  6027. my $st = [gettimeofday];
  6028. eval { $dbh->sqlite_backup_from_file($dump_path.$bfile); };
  6029. if ($@) {
  6030. $err = encode_base64($@,"");
  6031. Log3 ($name, 2, "DbRep $name - $@");
  6032. $dbh->disconnect;
  6033. return "$name|''|$err|''|''";
  6034. }
  6035. $dbh->disconnect;
  6036. # SQL-Laufzeit ermitteln
  6037. my $rt = tv_interval($st);
  6038. # Background-Laufzeit ermitteln
  6039. my $brt = tv_interval($bst);
  6040. $rt = $rt.",".$brt;
  6041. Log3 ($name, 3, "DbRep $name - Restore of $dump_path$bfile into '$dbname' finished - total time used: ".sprintf("%.0f",$brt)." seconds.");
  6042. return "$name|$rt|''|$dump_path$bfile|n.a.";
  6043. }
  6044. ####################################################################################################
  6045. # Restore MySQL (serverSide)
  6046. ####################################################################################################
  6047. sub mysql_RestoreServerSide($) {
  6048. my ($string) = @_;
  6049. my ($name, $bfile) = split("\\|", $string);
  6050. my $hash = $defs{$name};
  6051. my $dbloghash = $hash->{dbloghash};
  6052. my $dbconn = $dbloghash->{dbconn};
  6053. my $dbuser = $dbloghash->{dbuser};
  6054. my $dblogname = $dbloghash->{NAME};
  6055. my $dbpassword = $attr{"sec$dblogname"}{secret};
  6056. my $dbname = $hash->{DATABASE};
  6057. my $dump_path_rem = AttrVal($name, "dumpDirRemote", "./");
  6058. $dump_path_rem = $dump_path_rem."/" unless($dump_path_rem =~ m/\/$/);
  6059. my $table = "history";
  6060. my ($dbh,$sth,$err,$drh);
  6061. # Background-Startzeit
  6062. my $bst = [gettimeofday];
  6063. # Verbindung mit DB
  6064. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoInactiveDestroy => 1 });};
  6065. if ($@) {
  6066. $err = encode_base64($@,"");
  6067. Log3 ($name, 2, "DbRep $name - $@");
  6068. return "$name|''|$err|''|''";
  6069. }
  6070. # Dumpfile dekomprimieren wenn gzip
  6071. if($bfile =~ m/.*.gzip$/) {
  6072. ($err,$bfile) = DbRep_dumpUnCompress($hash,$bfile);
  6073. if ($err) {
  6074. $err = encode_base64($err,"");
  6075. $dbh->disconnect;
  6076. return "$name|''|$err|''|''";
  6077. }
  6078. }
  6079. Log3 ($name, 3, "DbRep $name - Starting restore of database '$dbname', table '$table'.");
  6080. # SQL-Startzeit
  6081. my $st = [gettimeofday];
  6082. my $sql = "LOAD DATA CONCURRENT INFILE '$dump_path_rem$bfile' IGNORE INTO TABLE $table FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n'; ";
  6083. eval {$sth = $dbh->prepare($sql);
  6084. $drh = $sth->execute();
  6085. };
  6086. if ($@) {
  6087. # error bei sql-execute
  6088. $err = encode_base64($@,"");
  6089. Log3 ($name, 2, "DbRep $name - $@");
  6090. $dbh->disconnect;
  6091. return "$name|''|$err|''|''";
  6092. }
  6093. $sth->finish;
  6094. $dbh->disconnect;
  6095. # SQL-Laufzeit ermitteln
  6096. my $rt = tv_interval($st);
  6097. # Background-Laufzeit ermitteln
  6098. my $brt = tv_interval($bst);
  6099. $rt = $rt.",".$brt;
  6100. Log3 ($name, 3, "DbRep $name - Restore of $dump_path_rem$bfile into '$dbname', '$table' finished - total time used: ".sprintf("%.0f",$brt)." seconds.");
  6101. return "$name|$rt|''|$dump_path_rem$bfile|n.a.";
  6102. }
  6103. ####################################################################################################
  6104. # Restore MySQL (ClientSide)
  6105. ####################################################################################################
  6106. sub mysql_RestoreClientSide($) {
  6107. my ($string) = @_;
  6108. my ($name, $bfile) = split("\\|", $string);
  6109. my $hash = $defs{$name};
  6110. my $dbloghash = $hash->{dbloghash};
  6111. my $dbconn = $dbloghash->{dbconn};
  6112. my $dbuser = $dbloghash->{dbuser};
  6113. my $dblogname = $dbloghash->{NAME};
  6114. my $dbpassword = $attr{"sec$dblogname"}{secret};
  6115. my $dbname = $hash->{DATABASE};
  6116. my $i_max = AttrVal($name, "dumpMemlimit", 100000); # max. Anzahl der Blockinserts
  6117. my $dump_path_def = $attr{global}{modpath}."/log/";
  6118. my $dump_path = AttrVal($name, "dumpDirLocal", $dump_path_def);
  6119. $dump_path = $dump_path."/" if($dump_path !~ /.*\/$/);
  6120. my ($dbh,$err,$v1,$v2,$e);
  6121. # Background-Startzeit
  6122. my $bst = [gettimeofday];
  6123. # Verbindung mit DB
  6124. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoCommit => 1 });};
  6125. if ($@) {
  6126. $e = $@;
  6127. $err = encode_base64($e,"");
  6128. Log3 ($name, 1, "DbRep $name - $e");
  6129. return "$name|''|$err|''|''";
  6130. }
  6131. # maximal mögliche Packetgröße ermitteln (in Bits) -> Umrechnen in max. Zeichen
  6132. my @row_ary;
  6133. my $sql = "show variables like 'max_allowed_packet'";
  6134. eval {@row_ary = $dbh->selectrow_array($sql);};
  6135. my $max_packets = $row_ary[1]; # Bits
  6136. $i_max = ($max_packets/8)-500; # Characters mit Sicherheitszuschlag
  6137. # Dumpfile dekomprimieren wenn gzip
  6138. if($bfile =~ m/.*.gzip$/) {
  6139. ($err,$bfile) = DbRep_dumpUnCompress($hash,$bfile);
  6140. if ($err) {
  6141. $err = encode_base64($err,"");
  6142. $dbh->disconnect;
  6143. return "$name|''|$err|''|''";
  6144. }
  6145. }
  6146. if(!open(FH, "<$dump_path$bfile")) {
  6147. $err = encode_base64("could not open ".$dump_path.$bfile.": ".$!,"");
  6148. return "$name|''|''|$err|''";
  6149. }
  6150. Log3 ($name, 3, "DbRep $name - Restore of database '$dbname' started. Sourcefile: $dump_path$bfile");
  6151. Log3 ($name, 3, "DbRep $name - Max packet lenght of insert statement: $i_max");
  6152. # SQL-Startzeit
  6153. my $st = [gettimeofday];
  6154. my $nc = 0; # Insert Zähler current
  6155. my $nh = 0; # Insert Zähler history
  6156. my $n = 0; # Insert Zähler
  6157. my $i = 0; # Array Zähler
  6158. my $tmp = '';
  6159. my $line = '';
  6160. my $base_query = '';
  6161. my $query = '';
  6162. while(<FH>) {
  6163. $tmp = $_;
  6164. chomp($tmp);
  6165. if(!$tmp || substr($tmp,0,2) eq "--") {
  6166. next;
  6167. }
  6168. $line .= $tmp;
  6169. if(substr($line,-1) eq ";") {
  6170. if($line !~ /^INSERT INTO.*$/) {
  6171. eval {$dbh->do($line);
  6172. };
  6173. if ($@) {
  6174. $e = $@;
  6175. $err = encode_base64($e,"");
  6176. Log3 ($name, 1, "DbRep $name - last query: $line");
  6177. Log3 ($name, 1, "DbRep $name - $e");
  6178. close(FH);
  6179. $dbh->disconnect;
  6180. return "$name|''|$err|''|''";
  6181. }
  6182. $line = '';
  6183. next;
  6184. }
  6185. if(!$base_query) {
  6186. $line =~ /INSERT INTO (.*) VALUES \((.*)\);/;
  6187. $v1 = $1;
  6188. $v2 = $2;
  6189. $base_query = qq{INSERT INTO $v1 VALUES };
  6190. $query = $base_query;
  6191. $nc++ if($base_query =~ /INSERT INTO `current`.*/);
  6192. $nh++ if($base_query =~ /INSERT INTO `history`.*/);
  6193. $query .= "," if($i);
  6194. $query .= "(".$v2.")";
  6195. $i++;
  6196. } else {
  6197. $line =~ /INSERT INTO (.*) VALUES \((.*)\);/;
  6198. $v1 = $1;
  6199. $v2 = $2;
  6200. my $ln = qq{INSERT INTO $v1 VALUES };
  6201. if($base_query eq $ln) {
  6202. $nc++ if($base_query =~ /INSERT INTO `current`.*/);
  6203. $nh++ if($base_query =~ /INSERT INTO `history`.*/);
  6204. $query .= "," if($i);
  6205. $query .= "(".$v2.")";
  6206. $i++;
  6207. } else {
  6208. $query = $query.";";
  6209. eval {$dbh->do($query);
  6210. };
  6211. if ($@) {
  6212. $e = $@;
  6213. $err = encode_base64($e,"");
  6214. Log3 ($name, 1, "DbRep $name - last query: $query");
  6215. Log3 ($name, 1, "DbRep $name - $e");
  6216. close(FH);
  6217. $dbh->disconnect;
  6218. return "$name|''|$err|''|''";
  6219. }
  6220. $i = 0;
  6221. $line =~ /INSERT INTO (.*) VALUES \((.*)\);/;
  6222. $v1 = $1;
  6223. $v2 = $2;
  6224. $base_query = qq{INSERT INTO $v1 VALUES };
  6225. $query = $base_query;
  6226. $query .= "(".$v2.")";
  6227. $nc++ if($base_query =~ /INSERT INTO `current`.*/);
  6228. $nh++ if($base_query =~ /INSERT INTO `history`.*/);
  6229. $i++;
  6230. }
  6231. }
  6232. if(length($query) >= $i_max) {
  6233. $query = $query.";";
  6234. eval {$dbh->do($query);
  6235. };
  6236. if ($@) {
  6237. $e = $@;
  6238. $err = encode_base64($e,"");
  6239. Log3 ($name, 1, "DbRep $name - last query: $query");
  6240. Log3 ($name, 1, "DbRep $name - $e");
  6241. close(FH);
  6242. $dbh->disconnect;
  6243. return "$name|''|$err|''|''";
  6244. }
  6245. $i = 0;
  6246. $query = '';
  6247. $base_query = '';
  6248. }
  6249. $line = '';
  6250. }
  6251. }
  6252. eval { $dbh->do($query) if($i);
  6253. };
  6254. if ($@) {
  6255. $e = $@;
  6256. $err = encode_base64($e,"");
  6257. Log3 ($name, 1, "DbRep $name - last query: $query");
  6258. Log3 ($name, 1, "DbRep $name - $e");
  6259. close(FH);
  6260. $dbh->disconnect;
  6261. return "$name|''|$err|''|''";
  6262. }
  6263. $dbh->disconnect;
  6264. close(FH);
  6265. # SQL-Laufzeit ermitteln
  6266. my $rt = tv_interval($st);
  6267. # Background-Laufzeit ermitteln
  6268. my $brt = tv_interval($bst);
  6269. $rt = $rt.",".$brt;
  6270. Log3 ($name, 3, "DbRep $name - Restore of '$dbname' finished - inserted history: $nh, inserted curent: $nc, time used: ".sprintf("%.0f",$brt)." seconds.");
  6271. return "$name|$rt|''|$dump_path$bfile|$nh|$nc";
  6272. }
  6273. ####################################################################################################
  6274. # Auswertungsroutine Restore
  6275. ####################################################################################################
  6276. sub DbRep_restoreDone($) {
  6277. my ($string) = @_;
  6278. my @a = split("\\|",$string);
  6279. my $hash = $defs{$a[0]};
  6280. my $bt = $a[1];
  6281. my ($rt,$brt) = split(",", $bt);
  6282. my $err = $a[2]?decode_base64($a[2]):undef;
  6283. my $bfile = $a[3];
  6284. my $drh = $a[4];
  6285. my $drc = $a[5];
  6286. my $name = $hash->{NAME};
  6287. my $erread;
  6288. delete($hash->{HELPER}{RUNNING_RESTORE});
  6289. if ($err) {
  6290. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  6291. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  6292. return;
  6293. }
  6294. readingsBeginUpdate($hash);
  6295. ReadingsBulkUpdateValue($hash, "RestoreRowsHistory", $drh) if($drh);
  6296. ReadingsBulkUpdateValue($hash, "RestoreRowsCurrent", $drc) if($drc);
  6297. readingsEndUpdate($hash, 1);
  6298. # Befehl nach Procedure ausführen
  6299. $erread = DbRep_afterproc($hash, "restore");
  6300. my $state = $erread?$erread:"Restore of $bfile finished";
  6301. readingsBeginUpdate($hash);
  6302. ReadingsBulkUpdateTimeState($hash,$brt,undef,$state);
  6303. readingsEndUpdate($hash, 1);
  6304. Log3 ($name, 3, "DbRep $name - Database restore finished successfully. ");
  6305. return;
  6306. }
  6307. ####################################################################################################
  6308. # Übertragung Datensätze in weitere DB
  6309. ####################################################################################################
  6310. sub DbRep_syncStandby($) {
  6311. my ($string) = @_;
  6312. my ($name,$device,$reading,$runtime_string_first,$runtime_string_next,$ts,$stbyname) = split("\\§", $string);
  6313. my $hash = $defs{$name};
  6314. my $table = "history";
  6315. my $utf8 = defined($hash->{UTF8})?$hash->{UTF8}:0;
  6316. my ($dbh,$dbhstby,$err,$sql,$irows,$irowdone);
  6317. # Quell-DB
  6318. my $dbloghash = $hash->{dbloghash};
  6319. my $dbconn = $dbloghash->{dbconn};
  6320. my $dbuser = $dbloghash->{dbuser};
  6321. my $dblogname = $dbloghash->{NAME};
  6322. my $dbpassword = $attr{"sec$dblogname"}{secret};
  6323. # Standby-DB
  6324. my $stbyhash = $defs{$stbyname};
  6325. my $stbyconn = $stbyhash->{dbconn};
  6326. my $stbyuser = $stbyhash->{dbuser};
  6327. my $stbypasswd = $attr{"sec$stbyname"}{secret};
  6328. my $stbyutf8 = defined($stbyhash->{UTF8})?$stbyhash->{UTF8}:0;
  6329. # Background-Startzeit
  6330. my $bst = [gettimeofday];
  6331. # Verbindung zur Quell-DB
  6332. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoCommit => 1, mysql_enable_utf8 => $utf8 });};
  6333. if ($@) {
  6334. $err = encode_base64($@,"");
  6335. Log3 ($name, 2, "DbRep $name - $@");
  6336. return "$name|''|''|$err";
  6337. }
  6338. # Verbindung zur Standby-DB
  6339. eval {$dbhstby = DBI->connect("dbi:$stbyconn", $stbyuser, $stbypasswd, { PrintError => 0, RaiseError => 1, AutoCommit => 1, mysql_enable_utf8 => $stbyutf8 });};
  6340. if ($@) {
  6341. $err = encode_base64($@,"");
  6342. Log3 ($name, 2, "DbRep $name - $@");
  6343. return "$name|''|''|$err";
  6344. }
  6345. # ist Zeiteingrenzung und/oder Aggregation gesetzt ? (wenn ja -> "?" in SQL sonst undef)
  6346. my ($IsTimeSet,$IsAggrSet) = DbRep_checktimeaggr($hash);
  6347. Log3 ($name, 5, "DbRep $name - IsTimeSet: $IsTimeSet, IsAggrSet: $IsAggrSet");
  6348. # SQL-Startzeit
  6349. my $st = [gettimeofday];
  6350. my ($sth,$old,$new);
  6351. eval { $dbh->begin_work() if($dbh->{AutoCommit}); }; # Transaktion wenn gewünscht und autocommit ein
  6352. if ($@) {
  6353. Log3($name, 2, "DbRep $name -> Error start transaction - $@");
  6354. }
  6355. # Timestampstring to Array
  6356. my @ts = split("\\|", $ts);
  6357. Log3 ($name, 5, "DbRep $name - Timestamp-Array: \n@ts");
  6358. # DB-Abfrage zeilenweise für jeden Array-Eintrag
  6359. $irows = 0;
  6360. $irowdone = 0;
  6361. my $selspec = "TIMESTAMP,DEVICE,TYPE,EVENT,READING,VALUE,UNIT";
  6362. my $addon = '';
  6363. foreach my $row (@ts) {
  6364. my @a = split("#", $row);
  6365. my $runtime_string = $a[0];
  6366. my $runtime_string_first = $a[1];
  6367. my $runtime_string_next = $a[2];
  6368. if ($IsTimeSet || $IsAggrSet) {
  6369. $sql = DbRep_createSelectSql($hash,"history",$selspec,$device,$reading,"'$runtime_string_first'","'$runtime_string_next'",$addon);
  6370. } else {
  6371. $sql = DbRep_createSelectSql($hash,"history",$selspec,$device,$reading,undef,undef,$addon);
  6372. }
  6373. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  6374. eval{ $sth = $dbh->prepare($sql);
  6375. $sth->execute();
  6376. };
  6377. if ($@) {
  6378. $err = encode_base64($@,"");
  6379. Log3 ($name, 2, "DbRep $name - $@");
  6380. $dbh->disconnect;
  6381. return "$name|''|''|$err";
  6382. }
  6383. no warnings 'uninitialized';
  6384. # DATE _ESC_ TIME _ESC_ DEVICE _ESC_ TYPE _ESC_ EVENT _ESC_ READING _ESC_ VALUE _ESC_ UNIT
  6385. my @row_array = map { ($_->[0] =~ s/ /_ESC_/r)."_ESC_".$_->[1]."_ESC_".$_->[2]."_ESC_".$_->[3]."_ESC_".$_->[4]."_ESC_".$_->[5]."_ESC_".$_->[6] } @{$sth->fetchall_arrayref()};
  6386. use warnings;
  6387. (undef,$irowdone,$err) = DbRep_WriteToDB($name,$dbhstby,$stbyhash,"0",@row_array) if(@row_array);
  6388. if ($err) {
  6389. Log3 ($name, 2, "DbRep $name - $err");
  6390. $err = encode_base64($err,"");
  6391. $dbh->disconnect;
  6392. $dbhstby->disconnect();
  6393. return "$name|''|''|$err";
  6394. }
  6395. $irows += $irowdone;
  6396. }
  6397. $dbh->disconnect();
  6398. $dbhstby->disconnect();
  6399. # SQL-Laufzeit ermitteln
  6400. my $rt = tv_interval($st);
  6401. # Background-Laufzeit ermitteln
  6402. my $brt = tv_interval($bst);
  6403. $rt = $rt.",".$brt;
  6404. return "$name|$irows|$rt|0";
  6405. }
  6406. ####################################################################################################
  6407. # Auswertungsroutine Übertragung Datensätze in weitere DB
  6408. ####################################################################################################
  6409. sub DbRep_syncStandbyDone($) {
  6410. my ($string) = @_;
  6411. my @a = split("\\|",$string);
  6412. my $hash = $defs{$a[0]};
  6413. my $name = $hash->{NAME};
  6414. my $irows = $a[1];
  6415. my $bt = $a[2];
  6416. my ($rt,$brt) = split(",", $bt);
  6417. my $err = $a[3]?decode_base64($a[3]):undef;
  6418. if ($err) {
  6419. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  6420. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  6421. delete($hash->{HELPER}{RUNNING_PID});
  6422. Log3 ($name, 4, "DbRep $name -> BlockingCall change_Done finished");
  6423. return;
  6424. }
  6425. # only for this block because of warnings if details of readings are not set
  6426. no warnings 'uninitialized';
  6427. readingsBeginUpdate($hash);
  6428. ReadingsBulkUpdateValue ($hash, "number_lines_inserted_Standby", $irows);
  6429. ReadingsBulkUpdateTimeState($hash,$brt,$rt,"done");
  6430. readingsEndUpdate($hash, 1);
  6431. # Befehl nach Procedure ausführen
  6432. my $erread = DbRep_afterproc($hash, "syncStandby");
  6433. delete($hash->{HELPER}{RUNNING_PID});
  6434. return;
  6435. }
  6436. ####################################################################################################
  6437. # Abbruchroutine Timeout Restore
  6438. ####################################################################################################
  6439. sub DbRep_restoreAborted(@) {
  6440. my ($hash,$cause) = @_;
  6441. my $name = $hash->{NAME};
  6442. my $dbh = $hash->{DBH};
  6443. my $erread;
  6444. $cause = $cause?$cause:"Timeout: process terminated";
  6445. Log3 ($name, 1, "DbRep $name - BlockingCall $hash->{HELPER}{RUNNING_RESTORE}{fn} pid:$hash->{HELPER}{RUNNING_RESTORE}{pid} $cause") if($hash->{HELPER}{RUNNING_RESTORE});
  6446. # Befehl nach Procedure ausführen
  6447. no warnings 'uninitialized';
  6448. $erread = DbRep_afterproc($hash, "restore");
  6449. $erread = ", ".(split("but", $erread))[1] if($erread);
  6450. my $state = $cause.$erread;
  6451. $dbh->disconnect() if(defined($dbh));
  6452. ReadingsSingleUpdateValue ($hash, "state", $state, 1);
  6453. Log3 ($name, 2, "DbRep $name - Database restore aborted by \"$cause\" ");
  6454. delete($hash->{HELPER}{RUNNING_RESTORE});
  6455. return;
  6456. }
  6457. ####################################################################################################
  6458. # Abbruchroutine Timeout DB-Abfrage
  6459. ####################################################################################################
  6460. sub DbRep_ParseAborted(@) {
  6461. my ($hash,$cause) = @_;
  6462. my $name = $hash->{NAME};
  6463. my $dbh = $hash->{DBH};
  6464. my $erread;
  6465. $cause = $cause?$cause:"Timeout: process terminated";
  6466. Log3 ($name, 1, "DbRep $name -> BlockingCall $hash->{HELPER}{RUNNING_PID}{fn} pid:$hash->{HELPER}{RUNNING_PID}{pid} $cause");
  6467. # Befehl nach Procedure ausführen
  6468. no warnings 'uninitialized';
  6469. $erread = DbRep_afterproc($hash, "command");
  6470. $erread = ", ".(split("but", $erread))[1] if($erread);
  6471. $dbh->disconnect() if(defined($dbh));
  6472. ReadingsSingleUpdateValue ($hash,"state",$cause, 1);
  6473. delete($hash->{HELPER}{RUNNING_PID});
  6474. return;
  6475. }
  6476. ####################################################################################################
  6477. # Abbruchroutine Timeout DB-Dump
  6478. ####################################################################################################
  6479. sub DbRep_DumpAborted(@) {
  6480. my ($hash,$cause) = @_;
  6481. my $name = $hash->{NAME};
  6482. my $dbh = $hash->{DBH};
  6483. my ($erread);
  6484. $cause = $cause?$cause:"Timeout: process terminated";
  6485. 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});
  6486. 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});
  6487. # Befehl nach Procedure ausführen
  6488. no warnings 'uninitialized';
  6489. $erread = DbRep_afterproc($hash, "dump");
  6490. $erread = ", ".(split("but", $erread))[1] if($erread);
  6491. my $state = $cause.$erread;
  6492. $dbh->disconnect() if(defined($dbh));
  6493. ReadingsSingleUpdateValue ($hash, "state", $state, 1);
  6494. Log3 ($name, 2, "DbRep $name - Database dump aborted by \"$cause\" ");
  6495. delete($hash->{HELPER}{RUNNING_BACKUP_CLIENT});
  6496. delete($hash->{HELPER}{RUNNING_BCKPREST_SERVER});
  6497. return;
  6498. }
  6499. ####################################################################################################
  6500. # Abbruchroutine Timeout DB-Abfrage
  6501. ####################################################################################################
  6502. sub DbRep_OptimizeAborted(@) {
  6503. my ($hash,$cause) = @_;
  6504. my $name = $hash->{NAME};
  6505. my $dbh = $hash->{DBH};
  6506. my ($erread);
  6507. $cause = $cause?$cause:"Timeout: process terminated";
  6508. Log3 ($name, 1, "DbRep $name -> BlockingCall $hash->{HELPER}{RUNNING_OPTIMIZE}}{fn} pid:$hash->{HELPER}{RUNNING_OPTIMIZE}{pid} $cause");
  6509. # Befehl nach Procedure ausführen
  6510. no warnings 'uninitialized';
  6511. $erread = DbRep_afterproc($hash, "optimize");
  6512. $erread = ", ".(split("but", $erread))[1] if($erread);
  6513. my $state = $cause.$erread;
  6514. $dbh->disconnect() if(defined($dbh));
  6515. ReadingsSingleUpdateValue ($hash, "state", $state, 1);
  6516. Log3 ($name, 2, "DbRep $name - Database optimize aborted by \"$cause\" ");
  6517. delete($hash->{HELPER}{RUNNING_OPTIMIZE});
  6518. return;
  6519. }
  6520. ####################################################################################################
  6521. # Abbruchroutine Repair SQlite
  6522. ####################################################################################################
  6523. sub DbRep_RepairAborted(@) {
  6524. my ($hash,$cause) = @_;
  6525. my $name = $hash->{NAME};
  6526. my $dbh = $hash->{DBH};
  6527. my $dbloghash = $hash->{dbloghash};
  6528. my $erread;
  6529. $cause = $cause?$cause:"Timeout: process terminated";
  6530. Log3 ($name, 1, "DbRep $name -> BlockingCall $hash->{HELPER}{RUNNING_REPAIR}{fn} pid:$hash->{HELPER}{RUNNING_REPAIR}{pid} $cause");
  6531. # Datenbankverbindung in DbLog wieder öffenen
  6532. my $dbl = $dbloghash->{NAME};
  6533. CommandSet(undef,"$dbl reopen");
  6534. # Befehl nach Procedure ausführen
  6535. no warnings 'uninitialized';
  6536. $erread = DbRep_afterproc($hash, "repair");
  6537. $erread = ", ".(split("but", $erread))[1] if($erread);
  6538. $dbh->disconnect() if(defined($dbh));
  6539. ReadingsSingleUpdateValue ($hash,"state",$cause, 1);
  6540. delete($hash->{HELPER}{RUNNING_REPAIR});
  6541. return;
  6542. }
  6543. ####################################################################################################
  6544. # SQL-Statement zusammenstellen für DB-Abfrage
  6545. ####################################################################################################
  6546. sub DbRep_createSelectSql($$$$$$$$) {
  6547. my ($hash,$table,$selspec,$device,$reading,$tf,$tn,$addon) = @_;
  6548. my $name = $hash->{NAME};
  6549. my $dbmodel = $hash->{dbloghash}{MODEL};
  6550. my ($sql,$devs,$danz,$ranz);
  6551. my $tnfull = 0;
  6552. ($devs,$danz,$reading,$ranz) = DbRep_specsForSql($hash,$device,$reading);
  6553. if($tn && $tn =~ /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/) {
  6554. $tnfull = 1;
  6555. }
  6556. $sql = "SELECT $selspec FROM $table where ";
  6557. $sql .= "DEVICE LIKE '$devs' AND " if($danz <= 1 && $devs !~ m(^%$) && $devs =~ m(\%));
  6558. $sql .= "DEVICE = '$devs' AND " if($danz <= 1 && $devs !~ m(\%));
  6559. $sql .= "DEVICE IN ($devs) AND " if($danz > 1);
  6560. $sql .= "READING LIKE '$reading' AND " if($ranz <= 1 && $reading !~ m(^%$) && $reading =~ m(\%));
  6561. $sql .= "READING = '$reading' AND " if($ranz <= 1 && $reading !~ m(\%));
  6562. $sql .= "READING IN ($reading) AND " if($ranz > 1);
  6563. if (($tf && $tn)) {
  6564. $sql .= "TIMESTAMP >= $tf AND TIMESTAMP ".($tnfull?"<=":"<")." $tn ";
  6565. } else {
  6566. if ($dbmodel eq "POSTGRESQL") {
  6567. $sql .= "true ";
  6568. } else {
  6569. $sql .= "1 ";
  6570. }
  6571. }
  6572. $sql .= "$addon;";
  6573. return $sql;
  6574. }
  6575. ####################################################################################################
  6576. # SQL-Statement zusammenstellen für DB-Updates
  6577. ####################################################################################################
  6578. sub DbRep_createUpdateSql($$$$$$$$) {
  6579. my ($hash,$table,$selspec,$device,$reading,$tf,$tn,$addon) = @_;
  6580. my $name = $hash->{NAME};
  6581. my $dbmodel = $hash->{dbloghash}{MODEL};
  6582. my ($sql,$devs,$danz,$ranz);
  6583. my $tnfull = 0;
  6584. ($devs,$danz,$reading,$ranz) = DbRep_specsForSql($hash,$device,$reading);
  6585. if($tn =~ /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/) {
  6586. $tnfull = 1;
  6587. }
  6588. $sql = "UPDATE $table SET $selspec AND ";
  6589. $sql .= "DEVICE LIKE '$devs' AND " if($danz <= 1 && $devs !~ m(^%$) && $devs =~ m(\%));
  6590. $sql .= "DEVICE = '$devs' AND " if($danz <= 1 && $devs !~ m(\%));
  6591. $sql .= "DEVICE IN ($devs) AND " if($danz > 1);
  6592. $sql .= "READING LIKE '$reading' AND " if($ranz <= 1 && $reading !~ m(^%$) && $reading =~ m(\%));
  6593. $sql .= "READING = '$reading' AND " if($ranz <= 1 && $reading !~ m(\%));
  6594. $sql .= "READING IN ($reading) AND " if($ranz > 1);
  6595. if (($tf && $tn)) {
  6596. $sql .= "TIMESTAMP >= $tf AND TIMESTAMP ".($tnfull?"<=":"<")." $tn ";
  6597. } else {
  6598. if ($dbmodel eq "POSTGRESQL") {
  6599. $sql .= "true ";
  6600. } else {
  6601. $sql .= "1 ";
  6602. }
  6603. }
  6604. $sql .= "$addon;";
  6605. return $sql;
  6606. }
  6607. ####################################################################################################
  6608. # SQL-Statement zusammenstellen für Löschvorgänge
  6609. ####################################################################################################
  6610. sub DbRep_createDeleteSql($$$$$$$) {
  6611. my ($hash,$table,$device,$reading,$tf,$tn,$addon) = @_;
  6612. my $name = $hash->{NAME};
  6613. my $dbmodel = $hash->{dbloghash}{MODEL};
  6614. my ($sql,$devs,$danz,$ranz);
  6615. my $tnfull = 0;
  6616. if($table eq "current") {
  6617. $sql = "delete FROM $table; ";
  6618. return $sql;
  6619. }
  6620. ($devs,$danz,$reading,$ranz) = DbRep_specsForSql($hash,$device,$reading);
  6621. if($tn =~ /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/) {
  6622. $tnfull = 1;
  6623. }
  6624. $sql = "delete FROM $table where ";
  6625. $sql .= "DEVICE LIKE '$devs' AND " if($danz <= 1 && $devs !~ m(^%$) && $devs =~ m(\%));
  6626. $sql .= "DEVICE = '$devs' AND " if($danz <= 1 && $devs !~ m(\%));
  6627. $sql .= "DEVICE IN ($devs) AND " if($danz > 1);
  6628. $sql .= "READING LIKE '$reading' AND " if($ranz <= 1 && $reading !~ m(^%$) && $reading =~ m(\%));
  6629. $sql .= "READING = '$reading' AND " if($ranz <= 1 && $reading !~ m(\%));
  6630. $sql .= "READING IN ($reading) AND " if($ranz > 1);
  6631. if ($tf && $tn) {
  6632. $sql .= "TIMESTAMP >= '$tf' AND TIMESTAMP ".($tnfull?"<=":"<")." '$tn' $addon;";
  6633. } else {
  6634. if ($dbmodel eq "POSTGRESQL") {
  6635. $sql .= "true;";
  6636. } else {
  6637. $sql .= "1;";
  6638. }
  6639. }
  6640. return $sql;
  6641. }
  6642. ####################################################################################################
  6643. # Ableiten von Device, Reading-Spezifikationen
  6644. ####################################################################################################
  6645. sub DbRep_specsForSql($$$) {
  6646. my ($hash,$device,$reading) = @_;
  6647. my $name = $hash->{NAME};
  6648. my @dvspcs = devspec2array($device);
  6649. my $devs = join(",",@dvspcs);
  6650. $devs =~ s/'/''/g; # escape ' with ''
  6651. my $danz = $#dvspcs+1;
  6652. if ($danz > 1) {
  6653. $devs =~ s/,/','/g;
  6654. $devs = "'".$devs."'";
  6655. }
  6656. Log3 $name, 5, "DbRep $name - Device specifications use for select: $devs";
  6657. $reading =~ s/'/''/g; # escape ' with ''
  6658. my @reads = split(",",$reading);
  6659. my $ranz = $#reads+1;
  6660. if ($ranz > 1) {
  6661. $reading =~ s/,/','/g;
  6662. $reading = "'".$reading."'";
  6663. }
  6664. Log3 $name, 5, "DbRep $name - Reading specification use for select: $reading";
  6665. return ($devs,$danz,$reading,$ranz);
  6666. }
  6667. ####################################################################################################
  6668. # Check ob Zeitgrenzen bzw. Aggregation gesetzt sind, evtl. übertseuern (je nach Funktion)
  6669. # Return "1" wenn Bedingung erfüllt, sonst "0"
  6670. ####################################################################################################
  6671. sub DbRep_checktimeaggr ($) {
  6672. my ($hash) = @_;
  6673. my $name = $hash->{NAME};
  6674. my $IsTimeSet = 0;
  6675. my $IsAggrSet = 0;
  6676. my $aggregation = AttrVal($name,"aggregation","no");
  6677. if ( AttrVal($name,"timestamp_begin",undef) || AttrVal($name,"timestamp_end",undef) ||
  6678. AttrVal($name,"timeDiffToNow",undef) || AttrVal($name,"timeOlderThan",undef) || AttrVal($name,"timeYearPeriod",undef) ) {
  6679. $IsTimeSet = 1;
  6680. }
  6681. if ($aggregation ne "no") {
  6682. $IsAggrSet = 1;
  6683. }
  6684. if($hash->{LASTCMD} =~ /delSeqDoublets/) {
  6685. $aggregation = ($aggregation eq "no")?"day":$aggregation; # wenn Aggregation "no", für delSeqDoublets immer "day" setzen
  6686. $IsAggrSet = 1;
  6687. }
  6688. if($hash->{LASTCMD} =~ /averageValue/ && AttrVal($name,"averageCalcForm","avgArithmeticMean") eq "avgDailyMeanGWS") {
  6689. $aggregation = "day"; # für Tagesmittelwertberechnung des deutschen Wetterdienstes immer "day"
  6690. $IsAggrSet = 1;
  6691. }
  6692. if($hash->{LASTCMD} =~ /delEntries|fetchrows|deviceRename|readingRename|tableCurrentFillup/) {
  6693. $IsAggrSet = 0;
  6694. $aggregation = "no";
  6695. }
  6696. if($hash->{LASTCMD} =~ /deviceRename|readingRename/) {
  6697. $IsTimeSet = 0;
  6698. }
  6699. if($hash->{LASTCMD} =~ /changeValue/) {
  6700. if($hash->{HELPER}{COMPLEX}) {
  6701. $IsAggrSet = 1;
  6702. $aggregation = "day";
  6703. } else {
  6704. $IsAggrSet = 0;
  6705. $aggregation = "no";
  6706. }
  6707. }
  6708. if($hash->{LASTCMD} =~ /syncStandby/ ) {
  6709. if($aggregation !~ /day|hour|week/) {
  6710. $aggregation = "day";
  6711. $IsAggrSet = 1;
  6712. }
  6713. }
  6714. return ($IsTimeSet,$IsAggrSet,$aggregation);
  6715. }
  6716. ####################################################################################################
  6717. # ReadingsSingleUpdate für Reading, Value, Event
  6718. ####################################################################################################
  6719. sub ReadingsSingleUpdateValue ($$$$) {
  6720. my ($hash,$reading,$val,$ev) = @_;
  6721. my $name = $hash->{NAME};
  6722. readingsSingleUpdate($hash, $reading, $val, $ev);
  6723. DbRep_userexit($name, $reading, $val);
  6724. return;
  6725. }
  6726. ####################################################################################################
  6727. # Readingsbulkupdate für Reading, Value
  6728. # readingsBeginUpdate und readingsEndUpdate muss vor/nach Funktionsaufruf gesetzt werden
  6729. ####################################################################################################
  6730. sub ReadingsBulkUpdateValue ($$$) {
  6731. my ($hash,$reading,$val) = @_;
  6732. my $name = $hash->{NAME};
  6733. readingsBulkUpdate($hash, $reading, $val);
  6734. DbRep_userexit($name, $reading, $val);
  6735. return;
  6736. }
  6737. ####################################################################################################
  6738. # Readingsbulkupdate für processing_time, state
  6739. # readingsBeginUpdate und readingsEndUpdate muss vor/nach Funktionsaufruf gesetzt werden
  6740. ####################################################################################################
  6741. sub ReadingsBulkUpdateTimeState ($$$$) {
  6742. my ($hash,$brt,$rt,$sval) = @_;
  6743. my $name = $hash->{NAME};
  6744. if(AttrVal($name, "showproctime", undef)) {
  6745. readingsBulkUpdate($hash, "background_processing_time", sprintf("%.4f",$brt)) if(defined($brt));
  6746. DbRep_userexit($name, "background_processing_time", sprintf("%.4f",$brt)) if(defined($brt));
  6747. readingsBulkUpdate($hash, "sql_processing_time", sprintf("%.4f",$rt)) if(defined($rt));
  6748. DbRep_userexit($name, "sql_processing_time", sprintf("%.4f",$rt)) if(defined($rt));
  6749. }
  6750. readingsBulkUpdate($hash, "state", $sval);
  6751. DbRep_userexit($name, "state", $sval);
  6752. return;
  6753. }
  6754. ####################################################################################################
  6755. # Anzeige von laufenden Blocking Prozessen
  6756. ####################################################################################################
  6757. sub DbRep_getblockinginfo($@) {
  6758. my ($hash) = @_;
  6759. my $name = $hash->{NAME};
  6760. my @rows;
  6761. our %BC_hash;
  6762. my $len = 99;
  6763. foreach my $h (values %BC_hash) {
  6764. next if($h->{terminated} || !$h->{pid});
  6765. my @allk = keys%{$h};
  6766. foreach my $k (@allk) {
  6767. Log3 ($name, 5, "DbRep $name -> $k : ".$h->{$k});
  6768. }
  6769. my $fn = (ref($h->{fn}) ? ref($h->{fn}) : $h->{fn});
  6770. my $arg = (ref($h->{arg}) ? ref($h->{arg}) : $h->{arg});
  6771. my $arg1 = substr($arg,0,$len);
  6772. $arg1 = $arg1."..." if(length($arg) > $len+1);
  6773. my $to = ($h->{timeout} ? $h->{timeout} : "N/A");
  6774. my $conn = ($h->{telnet} ? $h->{telnet} : "N/A");
  6775. push @rows, "$h->{pid}|ESCAPED|$fn|ESCAPED|$arg1|ESCAPED|$to|ESCAPED|$conn";
  6776. }
  6777. # Readingaufbereitung
  6778. readingsBeginUpdate($hash);
  6779. if(!@rows) {
  6780. ReadingsBulkUpdateTimeState($hash,undef,undef,"done - No BlockingCall processes running");
  6781. readingsEndUpdate($hash, 1);
  6782. return;
  6783. }
  6784. my $res = "<html><table border=2 bordercolor='darkgreen' cellspacing=0>";
  6785. $res .= "<tr><td style='padding-right:5px;padding-left:5px;font-weight:bold'>PID</td>";
  6786. $res .= "<td style='padding-right:5px;padding-left:5px;font-weight:bold'>FUNCTION</td>";
  6787. $res .= "<td style='padding-right:5px;padding-left:5px;font-weight:bold'>ARGUMENTS</td>";
  6788. $res .= "<td style='padding-right:5px;padding-left:5px;font-weight:bold'>TIMEOUT</td>";
  6789. $res .= "<td style='padding-right:5px;padding-left:5px;font-weight:bold'>CONNECTEDVIA</td></tr>";
  6790. foreach my $row (@rows) {
  6791. $row =~ s/\|ESCAPED\|/<\/td><td style='padding-right:5px;padding-left:5px'>/g;
  6792. $res .= "<tr><td style='padding-right:5px;padding-left:5px'>".$row."</td></tr>";
  6793. }
  6794. my $tab = $res."</table></html>";
  6795. ReadingsBulkUpdateValue ($hash,"BlockingInfo",$tab);
  6796. ReadingsBulkUpdateValue ($hash,"Blocking_Count",$#rows+1);
  6797. ReadingsBulkUpdateTimeState($hash,undef,undef,"done");
  6798. readingsEndUpdate($hash, 1);
  6799. return;
  6800. }
  6801. ####################################################################################################
  6802. # relative Zeitangaben als Sekunden normieren
  6803. #
  6804. # liefert die Attribute timeOlderThan, timeDiffToNow als Sekunden normiert zurück
  6805. ####################################################################################################
  6806. sub DbRep_normRelTime($) {
  6807. my ($hash) = @_;
  6808. my $name = $hash->{NAME};
  6809. my $tdtn = AttrVal($name, "timeDiffToNow", undef);
  6810. my $toth = AttrVal($name, "timeOlderThan", undef);
  6811. if($tdtn && $tdtn =~ /^\s*[ydhms]:(([\d]+.[\d]+)|[\d]+)\s*/) {
  6812. my ($y,$d,$h,$m,$s);
  6813. if($tdtn =~ /.*y:(([\d]+.[\d]+)|[\d]+).*/) {
  6814. $y = $tdtn;
  6815. $y =~ s/.*y:(([\d]+.[\d]+)|[\d]+).*/$1/e;
  6816. }
  6817. if($tdtn =~ /.*d:(([\d]+.[\d]+)|[\d]+).*/) {
  6818. $d = $tdtn;
  6819. $d =~ s/.*d:(([\d]+.[\d]+)|[\d]+).*/$1/e;
  6820. }
  6821. if($tdtn =~ /.*h:(([\d]+.[\d]+)|[\d]+).*/) {
  6822. $h = $tdtn;
  6823. $h =~ s/.*h:(([\d]+.[\d]+)|[\d]+).*/$1/e;
  6824. }
  6825. if($tdtn =~ /.*m:(([\d]+.[\d]+)|[\d]+).*/) {
  6826. $m = $tdtn;
  6827. $m =~ s/.*m:(([\d]+.[\d]+)|[\d]+).*/$1/e;
  6828. }
  6829. if($tdtn =~ /.*s:(([\d]+.[\d]+)|[\d]+).*/) {
  6830. $s = $tdtn;
  6831. $s =~ s/.*s:(([\d]+.[\d]+)|[\d]+).*/$1/e ;
  6832. }
  6833. no warnings 'uninitialized';
  6834. Log3($name, 4, "DbRep $name - timeDiffToNow - year: $y, day: $d, hour: $h, min: $m, sec: $s ");
  6835. use warnings;
  6836. $y = $y?($y*365*86400):0;
  6837. $d = $d?($d*86400):0;
  6838. $h = $h?($h*3600):0;
  6839. $m = $m?($m*60):0;
  6840. $s = $s?$s:0;
  6841. $tdtn = $y + $d + $h + $m + $s + 1; # one security second for correct create TimeArray
  6842. $tdtn = DbRep_corrRelTime($name,$tdtn,1);
  6843. }
  6844. if($toth && $toth =~ /^\s*[ydhms]:(([\d]+.[\d]+)|[\d]+)\s*/) {
  6845. my ($y,$d,$h,$m,$s);
  6846. if($toth =~ /.*y:(([\d]+.[\d]+)|[\d]+).*/) {
  6847. $y = $toth;
  6848. $y =~ s/.*y:(([\d]+.[\d]+)|[\d]+).*/$1/e;
  6849. }
  6850. if($toth =~ /.*d:(([\d]+.[\d]+)|[\d]+).*/) {
  6851. $d = $toth;
  6852. $d =~ s/.*d:(([\d]+.[\d]+)|[\d]+).*/$1/e;
  6853. }
  6854. if($toth =~ /.*h:(([\d]+.[\d]+)|[\d]+).*/) {
  6855. $h = $toth;
  6856. $h =~ s/.*h:(([\d]+.[\d]+)|[\d]+).*/$1/e;
  6857. }
  6858. if($toth =~ /.*m:(([\d]+.[\d]+)|[\d]+).*/) {
  6859. $m = $toth;
  6860. $m =~ s/.*m:(([\d]+.[\d]+)|[\d]+).*/$1/e;
  6861. }
  6862. if($toth =~ /.*s:(([\d]+.[\d]+)|[\d]+).*/) {
  6863. $s = $toth;
  6864. $s =~ s/.*s:(([\d]+.[\d]+)|[\d]+).*/$1/e ;
  6865. }
  6866. no warnings 'uninitialized';
  6867. Log3($name, 4, "DbRep $name - timeOlderThan - year: $y, day: $d, hour: $h, min: $m, sec: $s ");
  6868. use warnings;
  6869. $y = $y?($y*365*86400):0;
  6870. $d = $d?($d*86400):0;
  6871. $h = $h?($h*3600):0;
  6872. $m = $m?($m*60):0;
  6873. $s = $s?$s:0;
  6874. $toth = $y + $d + $h + $m + $s + 1; # one security second for correct create TimeArray
  6875. $toth = DbRep_corrRelTime($name,$toth,0);
  6876. }
  6877. return ($toth,$tdtn);
  6878. }
  6879. ####################################################################################################
  6880. # Korrektur Schaltjahr und Sommer/Winterzeit bei relativen Zeitangaben
  6881. ####################################################################################################
  6882. sub DbRep_corrRelTime($$$) {
  6883. my ($name,$tim,$tdtn) = @_;
  6884. my $hash = $defs{$name};
  6885. # year als Jahre seit 1900
  6886. # $mon als 0..11
  6887. my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);
  6888. my ($dsec,$dmin,$dhour,$dmday,$dmon,$dyear,$dwday,$dyday,$disdst);
  6889. if($tdtn) {
  6890. # timeDiffToNow
  6891. ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); # Startzeit Ableitung
  6892. ($dsec,$dmin,$dhour,$dmday,$dmon,$dyear,$dwday,$dyday,$disdst) = localtime(time-$tim); # Analyse Zieltimestamp timeDiffToNow
  6893. } else {
  6894. # timeOlderThan
  6895. ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time-$tim); # Startzeit Ableitung
  6896. my $mints = $hash->{HELPER}{MINTS}?$hash->{HELPER}{MINTS}:"1970-01-01 01:00:00"; # Timestamp des 1. Datensatzes verwenden falls ermittelt
  6897. my ($yyyy1, $mm1, $dd1, $hh1, $min1, $sec1) = ($mints =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/);
  6898. my $tsend = timelocal($sec1, $min1, $hh1, $dd1, $mm1-1, $yyyy1-1900);
  6899. ($dsec,$dmin,$dhour,$dmday,$dmon,$dyear,$dwday,$dyday,$disdst) = localtime($tsend); # Analyse Zieltimestamp timeOlderThan
  6900. }
  6901. $year += 1900;
  6902. $dyear += 1900;
  6903. my $k = $year - $dyear;
  6904. my $mg = ((int($mon)+1)+(($year-$dyear-1)*12)+(11-int($dmon)+1)); # Gesamtzahl der Monate des Bewertungszeitraumes
  6905. my $cly = 0; # Anzahl Schaltjahre innerhalb Beginn und Ende Auswertungszeitraum
  6906. my $fly = 0; # erstes Schaltjahr nach Start
  6907. my $lly = 0; # letzes Schaltjahr nach Start
  6908. while ($dyear+$k >= $dyear) {
  6909. my $ily = DbRep_IsLeapYear($name,$dyear+$k);
  6910. $cly++ if($ily);
  6911. $fly = $dyear+$k if($ily && !$fly);
  6912. $lly = $dyear+$k if($ily);
  6913. $k--;
  6914. }
  6915. # Log3($name, 4, "DbRep $name - countleapyear: $cly firstleapyear: $fly lastleapyear: $lly totalmonth: $mg isdaylight:$isdst destdaylight:$disdst");
  6916. if( ($fly <= $year && $mon > 1) && ($lly > $dyear || ($lly = $dyear && $dmon < 1)) ) {
  6917. $tim += $cly*86400;
  6918. # Log3($name, 4, "DbRep $name - leap year correction 1");
  6919. } else {
  6920. $tim += ($cly-1)*86400 if($cly);
  6921. # Log3($name, 4, "DbRep $name - leap year correction 2");
  6922. }
  6923. # Sommer/Winterzeitkorrektur
  6924. $tim += ($disdst-$isdst)*3600 if($disdst != $isdst);
  6925. return $tim;
  6926. }
  6927. ####################################################################################################
  6928. # liefert zurück ob übergebenes Jahr ein Schaltjahr ist ($ily = 1)
  6929. #
  6930. # Es gilt:
  6931. # - Wenn ein Jahr durch 4 teilbar ist, ist es ein Schaltjahr, aber
  6932. # - wenn es durch 100 teilbar ist, ist es kein schaltjahr, außer
  6933. # - es ist durch 400 teilbar, dann ist es ein schaltjahr
  6934. #
  6935. ####################################################################################################
  6936. sub DbRep_IsLeapYear($$) {
  6937. my ($name,$year) = @_;
  6938. my $ily = 0;
  6939. if ($year % 4 == 0 && $year % 100 != 0 || $year % 400 == 0) { # $year modulo 4 -> muß 0 sein
  6940. $ily = 1;
  6941. }
  6942. Log3($name, 4, "DbRep $name - Year $year is leap year") if($ily);
  6943. return $ily;
  6944. }
  6945. ###############################################################################
  6946. # Zeichencodierung für Fileexport filtern
  6947. ###############################################################################
  6948. sub DbRep_charfilter($) {
  6949. my ($txt) = @_;
  6950. # nur erwünschte Zeichen, Filtern von Steuerzeichen
  6951. $txt =~ tr/ A-Za-z0-9!"#$§%&'()*+,-.\/:;<=>?@[\\]^_`{|}~äöüÄÖÜ߀//cd;
  6952. return($txt);
  6953. }
  6954. ###################################################################################
  6955. # Befehl vor Procedure ausführen
  6956. ###################################################################################
  6957. sub DbRep_beforeproc ($$) {
  6958. my ($hash, $txt) = @_;
  6959. my $name = $hash->{NAME};
  6960. # Befehl vor Procedure ausführen
  6961. my $ebd = AttrVal($name, "executeBeforeProc", undef);
  6962. if($ebd) {
  6963. Log3 ($name, 3, "DbRep $name - execute command before $txt: '$ebd' ");
  6964. my $err = AnalyzeCommandChain(undef, $ebd);
  6965. if ($err) {
  6966. Log3 ($name, 2, "DbRep $name - command message before $txt: \"$err\" ");
  6967. my $erread = "Warning - message from command before $txt appeared";
  6968. ReadingsSingleUpdateValue ($hash, "before".$txt."_message", $err, 1);
  6969. ReadingsSingleUpdateValue ($hash, "state", $erread, 1);
  6970. }
  6971. }
  6972. return;
  6973. }
  6974. ###################################################################################
  6975. # Befehl nach Procedure ausführen
  6976. ###################################################################################
  6977. sub DbRep_afterproc ($$) {
  6978. my ($hash, $txt) = @_;
  6979. my $name = $hash->{NAME};
  6980. my $erread;
  6981. # Befehl nach Procedure ausführen
  6982. no warnings 'uninitialized';
  6983. my $ead = AttrVal($name, "executeAfterProc", undef);
  6984. if($ead) {
  6985. Log3 ($name, 4, "DbRep $name - execute command after $txt: '$ead' ");
  6986. my $err = AnalyzeCommandChain(undef, $ead);
  6987. if ($err) {
  6988. Log3 ($name, 2, "DbRep $name - command message after $txt: \"$err\" ");
  6989. ReadingsSingleUpdateValue ($hash, "after".$txt."_message", $err, 1);
  6990. $erread = "Warning - $txt finished, but command message after $txt appeared";
  6991. }
  6992. }
  6993. return $erread;
  6994. }
  6995. ##############################################################################################
  6996. # timestamp_begin, timestamp_end bei Einsatz datetime-Picker entsprechend
  6997. # den Anforderungen formatieren
  6998. ##############################################################################################
  6999. sub DbRep_formatpicker ($) {
  7000. my ($str) = @_;
  7001. if ($str =~ /^(\d{4})-(\d{2})-(\d{2})_(\d{2}):(\d{2})$/) {
  7002. # Anpassung für datetime-Picker Widget
  7003. $str =~ s/_/ /;
  7004. $str = $str.":00";
  7005. }
  7006. if ($str =~ /^(\d{4})-(\d{2})-(\d{2})_(\d{2}):(\d{2}):(\d{2})$/) {
  7007. # Anpassung für datetime-Picker Widget
  7008. $str =~ s/_/ /;
  7009. }
  7010. return $str;
  7011. }
  7012. ####################################################################################################
  7013. # userexit - Funktion um userspezifische Programmaufrufe nach Aktualisierung eines Readings
  7014. # zu ermöglichen, arbeitet OHNE Event abhängig vom Attr userExitFn
  7015. #
  7016. # Aufruf der <UserExitFn> mit $name,$reading,$value
  7017. ####################################################################################################
  7018. sub DbRep_userexit ($$$) {
  7019. my ($name,$reading,$value) = @_;
  7020. my $hash = $defs{$name};
  7021. return if(!$hash->{HELPER}{USEREXITFN});
  7022. if(!defined($reading)) {$reading = "";}
  7023. if(!defined($value)) {$value = "";}
  7024. $value =~ s/\\/\\\\/g; # escapen of chars for evaluation
  7025. $value =~ s/'/\\'/g;
  7026. my $re = $hash->{HELPER}{UEFN_REGEXP}?$hash->{HELPER}{UEFN_REGEXP}:".*:.*";
  7027. if("$reading:$value" =~ m/^$re$/ ) {
  7028. my @res;
  7029. my $cmd = $hash->{HELPER}{USEREXITFN}."('$name','$reading','$value')";
  7030. $cmd = "{".$cmd."}";
  7031. my $r = AnalyzeCommandChain(undef, $cmd);
  7032. }
  7033. return;
  7034. }
  7035. ####################################################################################################
  7036. # delete Readings before new operation
  7037. ####################################################################################################
  7038. sub DbRep_delread($;$$) {
  7039. # Readings löschen die nicht in der Ausnahmeliste (Attr readingPreventFromDel) stehen
  7040. my ($hash,$shutdown) = @_;
  7041. my $name = $hash->{NAME};
  7042. my @allrds = keys%{$defs{$name}{READINGS}};
  7043. if($shutdown) {
  7044. my $do = 0;
  7045. foreach my $key(@allrds) {
  7046. # Highlighted Readings löschen und save statefile wegen Inkompatibilitär beim Restart
  7047. if($key =~ /<html><span/) {
  7048. $do = 1;
  7049. delete($defs{$name}{READINGS}{$key});
  7050. }
  7051. }
  7052. WriteStatefile() if($do == 1);
  7053. return undef;
  7054. }
  7055. my @rdpfdel = split(",", $hash->{HELPER}{RDPFDEL}) if($hash->{HELPER}{RDPFDEL});
  7056. if(@rdpfdel) {
  7057. foreach my $key(@allrds) {
  7058. # Log3 ($name, 1, "DbRep $name - Reading Schlüssel: $key");
  7059. my $dodel = 1;
  7060. foreach my $rdpfdel(@rdpfdel) {
  7061. if($key =~ /$rdpfdel/ || $key eq "state") {
  7062. $dodel = 0;
  7063. }
  7064. }
  7065. if($dodel) {
  7066. delete($defs{$name}{READINGS}{$key});
  7067. }
  7068. }
  7069. } else {
  7070. foreach my $key(@allrds) {
  7071. # Log3 ($name, 1, "DbRep $name - Reading Schlüssel: $key");
  7072. delete($defs{$name}{READINGS}{$key}) if($key ne "state");
  7073. }
  7074. }
  7075. return undef;
  7076. }
  7077. ####################################################################################################
  7078. # erstellen neues SQL-File für Dumproutine
  7079. ####################################################################################################
  7080. sub DbRep_NewDumpFilename ($$$$$){
  7081. my ($sql_text,$dump_path,$dbname,$time_stamp,$character_set) = @_;
  7082. my $part = "";
  7083. my $sql_file = $dump_path.$dbname."_".$time_stamp.$part.".sql";
  7084. my $backupfile = $dbname."_".$time_stamp.$part.".sql";
  7085. $sql_text .= "/*!40101 SET NAMES '".$character_set."' */;\n";
  7086. $sql_text .= "SET FOREIGN_KEY_CHECKS=0;\n";
  7087. my ($filesize,$err) = DbRep_WriteToDumpFile($sql_text,$sql_file);
  7088. if($err) {
  7089. return (undef,undef,undef,undef,$err);
  7090. }
  7091. chmod(0777,$sql_file);
  7092. $sql_text = "";
  7093. my $first_insert = 0;
  7094. return ($sql_text,$first_insert,$sql_file,$backupfile,undef);
  7095. }
  7096. ####################################################################################################
  7097. # Schreiben DB-Dumps in SQL-File
  7098. ####################################################################################################
  7099. sub DbRep_WriteToDumpFile ($$) {
  7100. my ($inh,$sql_file) = @_;
  7101. my $filesize;
  7102. my $err = 0;
  7103. if(length($inh) > 0) {
  7104. unless(open(DATEI,">>$sql_file")) {
  7105. $err = "Can't open file '$sql_file' for write access";
  7106. return (undef,$err);
  7107. }
  7108. print DATEI $inh;
  7109. close(DATEI);
  7110. my $fref = stat($sql_file);
  7111. if ($fref =~ /ARRAY/) {
  7112. $filesize = (@{stat($sql_file)})[7];
  7113. } else {
  7114. $filesize = (stat($sql_file))[7];
  7115. }
  7116. }
  7117. return ($filesize,undef);
  7118. }
  7119. ####################################################################################################
  7120. # Filesize (Byte) umwandeln in KB bzw. MB
  7121. ####################################################################################################
  7122. sub DbRep_byteOutput ($) {
  7123. my $bytes = shift;
  7124. return if(!defined($bytes));
  7125. return $bytes if(!looks_like_number($bytes));
  7126. my $suffix = "Bytes";
  7127. if ($bytes >= 1024) { $suffix = "KB"; $bytes = sprintf("%.2f",($bytes/1024));};
  7128. if ($bytes >= 1024) { $suffix = "MB"; $bytes = sprintf("%.2f",($bytes/1024));};
  7129. my $ret = sprintf "%.2f",$bytes;
  7130. $ret.=' '.$suffix;
  7131. return $ret;
  7132. }
  7133. ####################################################################################################
  7134. # Schreibroutine in DbRep Keyvalue-File
  7135. ####################################################################################################
  7136. sub DbRep_setCmdFile($$$) {
  7137. my ($key,$value,$hash) = @_;
  7138. my $fName = $attr{global}{modpath}."/FHEM/FhemUtils/cacheDbRep";
  7139. my $param = {
  7140. FileName => $fName,
  7141. ForceType => "file",
  7142. };
  7143. my ($err, @old) = FileRead($param);
  7144. DbRep_createCmdFile($hash) if($err);
  7145. my @new;
  7146. my $fnd;
  7147. foreach my $l (@old) {
  7148. if($l =~ m/^$key:/) {
  7149. $fnd = 1;
  7150. push @new, "$key:$value" if(defined($value));
  7151. } else {
  7152. push @new, $l;
  7153. }
  7154. }
  7155. push @new, "$key:$value" if(!$fnd && defined($value));
  7156. return FileWrite($param, @new);
  7157. }
  7158. ####################################################################################################
  7159. # anlegen Keyvalue-File für DbRep wenn nicht vorhanden
  7160. ####################################################################################################
  7161. sub DbRep_createCmdFile ($) {
  7162. my ($hash) = @_;
  7163. my $fName = $attr{global}{modpath}."/FHEM/FhemUtils/cacheDbRep";
  7164. my $param = {
  7165. FileName => $fName,
  7166. ForceType => "file",
  7167. };
  7168. my @new;
  7169. push(@new, "# This file is auto generated from 93_DbRep.",
  7170. "# Please do not modify, move or delete it.",
  7171. "");
  7172. return FileWrite($param, @new);
  7173. }
  7174. ####################################################################################################
  7175. # Leseroutine aus DbRep Keyvalue-File
  7176. ####################################################################################################
  7177. sub DbRep_getCmdFile($) {
  7178. my ($key) = @_;
  7179. my $fName = $attr{global}{modpath}."/FHEM/FhemUtils/cacheDbRep";
  7180. my $param = {
  7181. FileName => $fName,
  7182. ForceType => "file",
  7183. };
  7184. my ($err, @l) = FileRead($param);
  7185. return ($err, undef) if($err);
  7186. for my $l (@l) {
  7187. return (undef, $1) if($l =~ m/^$key:(.*)/);
  7188. }
  7189. return (undef, undef);
  7190. }
  7191. ####################################################################################################
  7192. # Tabellenoptimierung MySQL
  7193. ####################################################################################################
  7194. sub DbRep_mysqlOptimizeTables ($$@) {
  7195. my ($hash,$dbh,@tablenames) = @_;
  7196. my $name = $hash->{NAME};
  7197. my $dbname = $hash->{DATABASE};
  7198. my $ret = 0;
  7199. my $opttbl = 0;
  7200. my $db_tables = $hash->{HELPER}{DBTABLES};
  7201. my ($engine,$tablename,$query,$sth,$value,$db_MB_start,$db_MB_end);
  7202. # Anfangsgröße ermitteln
  7203. $query = "SELECT sum( data_length + index_length ) / 1024 / 1024 FROM information_schema.TABLES where table_schema='$dbname' ";
  7204. Log3 ($name, 5, "DbRep $name - current query: $query ");
  7205. eval { $sth = $dbh->prepare($query);
  7206. $sth->execute;
  7207. };
  7208. if ($@) {
  7209. Log3 ($name, 2, "DbRep $name - Error executing: '".$query."' ! MySQL-Error: ".$@);
  7210. $sth->finish;
  7211. $dbh->disconnect;
  7212. return ($@,undef,undef);
  7213. }
  7214. $value = $sth->fetchrow();
  7215. $db_MB_start = sprintf("%.2f",$value);
  7216. Log3 ($name, 3, "DbRep $name - Size of database $dbname before optimize (MB): $db_MB_start");
  7217. Log3($name, 3, "DbRep $name - Optimizing tables");
  7218. foreach $tablename (@tablenames) {
  7219. #optimize table if engine supports optimization
  7220. $engine = '';
  7221. $engine = uc($db_tables->{$tablename}{Engine}) if($db_tables->{$tablename}{Engine});
  7222. if ($engine =~ /(MYISAM|BDB|INNODB|ARIA)/) {
  7223. Log3($name, 3, "DbRep $name - Optimizing table `$tablename` ($engine). It will take a while.");
  7224. my $sth_to = $dbh->prepare("OPTIMIZE TABLE `$tablename`");
  7225. $ret = $sth_to->execute;
  7226. if ($ret) {
  7227. Log3($name, 3, "DbRep $name - Table ".($opttbl+1)." `$tablename` optimized successfully.");
  7228. $opttbl++;
  7229. } else {
  7230. Log3($name, 2, "DbRep $name - Error while optimizing table $tablename. Continue with next table or backup.");
  7231. }
  7232. }
  7233. }
  7234. Log3($name, 3, "DbRep $name - $opttbl tables have been optimized.") if($opttbl > 0);
  7235. # Endgröße ermitteln
  7236. eval { $sth->execute; };
  7237. if ($@) {
  7238. Log3 ($name, 2, "DbRep $name - Error executing: '".$query."' ! MySQL-Error: ".$@);
  7239. $sth->finish;
  7240. $dbh->disconnect;
  7241. return ($@,undef,undef);
  7242. }
  7243. $value = $sth->fetchrow();
  7244. $db_MB_end = sprintf("%.2f",$value);
  7245. Log3 ($name, 3, "DbRep $name - Size of database $dbname after optimize (MB): $db_MB_end");
  7246. $sth->finish;
  7247. return (undef,$db_MB_start,$db_MB_end);
  7248. }
  7249. ####################################################################################################
  7250. # Dump-Files im dumpDirLocal löschen bis auf die letzten "n"
  7251. ####################################################################################################
  7252. sub DbRep_deldumpfiles ($$) {
  7253. my ($hash,$bfile) = @_;
  7254. my $name = $hash->{NAME};
  7255. my $dbloghash = $hash->{dbloghash};
  7256. my $dump_path_def = $attr{global}{modpath}."/log/";
  7257. my $dump_path_loc = AttrVal($name,"dumpDirLocal", $dump_path_def);
  7258. $dump_path_loc = $dump_path_loc."/" unless($dump_path_loc =~ m/\/$/);
  7259. my $dfk = AttrVal($name,"dumpFilesKeep", 3);
  7260. my $pfix = (split '\.', $bfile)[1];
  7261. my $dbname = (split '_', $bfile)[0];
  7262. my $file = $dbname."_.*".$pfix.".*"; # Files mit/ohne Endung "gzip" berücksichtigen
  7263. my @fd;
  7264. if(!opendir(DH, $dump_path_loc)) {
  7265. push(@fd, "No files deleted - Can't open path '$dump_path_loc'");
  7266. return @fd;
  7267. }
  7268. my @files = sort grep {/^$file$/} readdir(DH);
  7269. my $fref = stat("$dump_path_loc/$bfile");
  7270. if ($fref =~ /ARRAY/) {
  7271. @files = sort { (@{stat("$dump_path_loc/$a")})[9] cmp (@{stat("$dump_path_loc/$b")})[9] } @files
  7272. if(AttrVal("global", "archivesort", "alphanum") eq "timestamp");
  7273. } else {
  7274. @files = sort { (stat("$dump_path_loc/$a"))[9] cmp (stat("$dump_path_loc/$b"))[9] } @files
  7275. if(AttrVal("global", "archivesort", "alphanum") eq "timestamp");
  7276. }
  7277. closedir(DH);
  7278. Log3($name, 5, "DbRep $name - Dump files have been found in dumpDirLocal '$dump_path_loc': ".join(', ',@files) );
  7279. my $max = int(@files)-$dfk;
  7280. for(my $i = 0; $i < $max; $i++) {
  7281. push(@fd, $files[$i]);
  7282. Log3($name, 3, "DbRep $name - Deleting old dumpfile '$files[$i]' ");
  7283. unlink("$dump_path_loc/$files[$i]");
  7284. }
  7285. return @fd;
  7286. }
  7287. ####################################################################################################
  7288. # Dumpfile komprimieren
  7289. ####################################################################################################
  7290. sub DbRep_dumpCompress ($$) {
  7291. my ($hash,$bfile) = @_;
  7292. my $name = $hash->{NAME};
  7293. my $dump_path_def = $attr{global}{modpath}."/log/";
  7294. my $dump_path_loc = AttrVal($name,"dumpDirLocal", $dump_path_def);
  7295. $dump_path_loc =~ s/(\/$|\\$)//;
  7296. my $input = $dump_path_loc."/".$bfile;
  7297. my $output = $dump_path_loc."/".$bfile.".gzip";
  7298. Log3($name, 3, "DbRep $name - compress file $input");
  7299. my $stat = gzip $input => $output ,BinModeIn => 1;
  7300. if($GzipError) {
  7301. Log3($name, 2, "DbRep $name - gzip of $input failed: $GzipError");
  7302. return ($GzipError,$input);
  7303. }
  7304. Log3($name, 3, "DbRep $name - file compressed to output file: $output");
  7305. unlink("$input");
  7306. Log3($name, 3, "DbRep $name - input file deleted: $input");
  7307. return (undef,$bfile.".gzip");
  7308. }
  7309. ####################################################################################################
  7310. # Dumpfile dekomprimieren
  7311. ####################################################################################################
  7312. sub DbRep_dumpUnCompress ($$) {
  7313. my ($hash,$bfile) = @_;
  7314. my $name = $hash->{NAME};
  7315. my $dump_path_def = $attr{global}{modpath}."/log/";
  7316. my $dump_path_loc = AttrVal($name,"dumpDirLocal", $dump_path_def);
  7317. $dump_path_loc =~ s/(\/$|\\$)//;
  7318. my $input = $dump_path_loc."/".$bfile;
  7319. my $outfile = $bfile;
  7320. $outfile =~ s/\.gzip//;
  7321. my $output = $dump_path_loc."/".$outfile;
  7322. Log3($name, 3, "DbRep $name - uncompress file $input");
  7323. my $stat = gunzip $input => $output ,BinModeOut => 1;
  7324. if($GunzipError) {
  7325. Log3($name, 2, "DbRep $name - gunzip of $input failed: $GunzipError");
  7326. return ($GunzipError,$input);
  7327. }
  7328. Log3($name, 3, "DbRep $name - file uncompressed to output file: $output");
  7329. # Größe dekomprimiertes File ermitteln
  7330. my @a = split(' ',qx(du $output)) if ($^O =~ m/linux/i || $^O =~ m/unix/i);
  7331. my $filesize = ($a[0])?($a[0]*1024):undef;
  7332. my $fsize = DbRep_byteOutput($filesize);
  7333. Log3 ($name, 3, "DbRep $name - Size of uncompressed file: ".$fsize);
  7334. return (undef,$outfile);
  7335. }
  7336. ####################################################################################################
  7337. # erzeugtes Dump-File aus dumpDirLocal zum FTP-Server übertragen
  7338. ####################################################################################################
  7339. sub DbRep_sendftp ($$) {
  7340. my ($hash,$bfile) = @_;
  7341. my $name = $hash->{NAME};
  7342. my $dump_path_def = $attr{global}{modpath}."/log/";
  7343. my $dump_path_loc = AttrVal($name,"dumpDirLocal", $dump_path_def);
  7344. my $file = (split /[\/]/, $bfile)[-1];
  7345. my $ftpto = AttrVal($name,"ftpTimeout",30);
  7346. my $ftpUse = AttrVal($name,"ftpUse",0);
  7347. my $ftpuseSSL = AttrVal($name,"ftpUseSSL",0);
  7348. my $ftpDir = AttrVal($name,"ftpDir","/");
  7349. my $ftpPort = AttrVal($name,"ftpPort",21);
  7350. my $ftpServer = AttrVal($name,"ftpServer",undef);
  7351. my $ftpUser = AttrVal($name,"ftpUser","anonymous");
  7352. my $ftpPwd = AttrVal($name,"ftpPwd",undef);
  7353. my $ftpPassive = AttrVal($name,"ftpPassive",0);
  7354. my $ftpDebug = AttrVal($name,"ftpDebug",0);
  7355. my $fdfk = AttrVal($name,"ftpDumpFilesKeep", 3);
  7356. my $pfix = (split '\.', $bfile)[1];
  7357. my $dbname = (split '_', $bfile)[0];
  7358. my $ftpl = $dbname."_.*".$pfix.".*"; # Files mit/ohne Endung "gzip" berücksichtigen
  7359. my ($ftperr,$ftpmsg,$ftp);
  7360. # kein FTP verwenden oder möglich
  7361. return ($ftperr,$ftpmsg) if((!$ftpUse && !$ftpuseSSL) || !$bfile);
  7362. if(!$ftpServer) {
  7363. $ftperr = "FTP-Error: FTP-Server isn't set.";
  7364. Log3($name, 2, "DbRep $name - $ftperr");
  7365. return ($ftperr,undef);
  7366. }
  7367. if(!opendir(DH, $dump_path_loc)) {
  7368. $ftperr = "FTP-Error: Can't open path '$dump_path_loc'";
  7369. Log3($name, 2, "DbRep $name - $ftperr");
  7370. return ($ftperr,undef);
  7371. }
  7372. my $mod_ftpssl = 0;
  7373. my $mod_ftp = 0;
  7374. my $mod;
  7375. if ($ftpuseSSL) {
  7376. # FTP mit SSL soll genutzt werden
  7377. $mod = "Net::FTPSSL => e.g. with 'sudo cpan -i Net::FTPSSL' ";
  7378. eval { require Net::FTPSSL; };
  7379. if(!$@){
  7380. $mod_ftpssl = 1;
  7381. import Net::FTPSSL;
  7382. }
  7383. } else {
  7384. # nur FTP
  7385. $mod = "Net::FTP";
  7386. eval { require Net::FTP; };
  7387. if(!$@){
  7388. $mod_ftp = 1;
  7389. import Net::FTP;
  7390. }
  7391. }
  7392. if ($ftpuseSSL && $mod_ftpssl) {
  7393. # use ftp-ssl
  7394. my $enc = "E";
  7395. eval { $ftp = Net::FTPSSL->new($ftpServer, Port => $ftpPort, Timeout => $ftpto, Debug => $ftpDebug, Encryption => $enc) }
  7396. or $ftperr = "FTP-SSL-ERROR: Can't connect - $@";
  7397. } elsif (!$ftpuseSSL && $mod_ftp) {
  7398. # use plain ftp
  7399. eval { $ftp = Net::FTP->new($ftpServer, Port => $ftpPort, Timeout => $ftpto, Debug => $ftpDebug, Passive => $ftpPassive) }
  7400. or $ftperr = "FTP-Error: Can't connect - $@";
  7401. } else {
  7402. $ftperr = "FTP-Error: required module couldn't be loaded. You have to install it first: $mod.";
  7403. }
  7404. if ($ftperr) {
  7405. Log3($name, 2, "DbRep $name - $ftperr");
  7406. return ($ftperr,undef);
  7407. }
  7408. my $pwdstr = $ftpPwd?$ftpPwd:" ";
  7409. $ftp->login($ftpUser, $ftpPwd) or $ftperr = "FTP-Error: Couldn't login with user '$ftpUser' and password '$pwdstr' ";
  7410. if ($ftperr) {
  7411. Log3($name, 2, "DbRep $name - $ftperr");
  7412. return ($ftperr,undef);
  7413. }
  7414. $ftp->binary();
  7415. # FTP Verzeichnis setzen
  7416. $ftp->cwd($ftpDir) or $ftperr = "FTP-Error: Couldn't change directory to '$ftpDir' ";
  7417. if ($ftperr) {
  7418. Log3($name, 2, "DbRep $name - $ftperr");
  7419. return ($ftperr,undef);
  7420. }
  7421. $dump_path_loc =~ s/(\/$|\\$)//;
  7422. Log3($name, 3, "DbRep $name - FTP: transferring ".$dump_path_loc."/".$file);
  7423. $ftpmsg = $ftp->put($dump_path_loc."/".$file);
  7424. if (!$ftpmsg) {
  7425. $ftperr = "FTP-Error: Couldn't transfer ".$file." to ".$ftpServer." into dir ".$ftpDir;
  7426. Log3($name, 2, "DbRep $name - $ftperr");
  7427. } else {
  7428. $ftpmsg = "FTP: ".$file." transferred successfully to ".$ftpServer." into dir ".$ftpDir;
  7429. Log3($name, 3, "DbRep $name - $ftpmsg");
  7430. }
  7431. # Versionsverwaltung FTP-Verzeichnis
  7432. my (@ftl,@ftpfd);
  7433. if($ftpuseSSL) {
  7434. @ftl = sort grep {/^$ftpl$/} $ftp->nlst();
  7435. } else {
  7436. @ftl = sort grep {/^$ftpl$/} @{$ftp->ls()};
  7437. }
  7438. Log3($name, 5, "DbRep $name - FTP: filelist of \"$ftpDir\": @ftl");
  7439. my $max = int(@ftl)-$fdfk;
  7440. for(my $i = 0; $i < $max; $i++) {
  7441. push(@ftpfd, $ftl[$i]);
  7442. Log3($name, 3, "DbRep $name - FTP: deleting old dumpfile '$ftl[$i]' ");
  7443. $ftp->delete($ftl[$i]);
  7444. }
  7445. return ($ftperr,$ftpmsg,@ftpfd);
  7446. }
  7447. ####################################################################################################
  7448. # Test auf Daylight saving time
  7449. ####################################################################################################
  7450. sub DbRep_dsttest ($$$) {
  7451. my ($hash,$runtime,$aggsec) = @_;
  7452. my $name = $hash->{NAME};
  7453. my $dstchange = 0;
  7454. # der Wechsel der daylight saving time wird dadurch getestet, dass geprüft wird
  7455. # ob im Vergleich der aktuellen zur nächsten Selektionsperiode von "$aggsec (day, week, month)"
  7456. # ein Wechsel der daylight saving time vorliegt
  7457. my $dst = (localtime($runtime))[8]; # ermitteln daylight saving aktuelle runtime
  7458. my $time_str = localtime($runtime+$aggsec); # textual time representation
  7459. my $dst_new = (localtime($runtime+$aggsec))[8]; # ermitteln daylight saving nächste runtime
  7460. if ($dst != $dst_new) {
  7461. $dstchange = 1;
  7462. }
  7463. Log3 ($name, 5, "DbRep $name - Daylight savings changed: $dstchange (on $time_str)");
  7464. return $dstchange;
  7465. }
  7466. ####################################################################################################
  7467. # Counthash Untersuchung
  7468. # Logausgabe der Anzahl verarbeiteter Datensätze pro Zeitraum / Aggregation
  7469. # Rückgabe eines ncp-hash (no calc in period) mit den Perioden für die keine Differenz berechnet
  7470. # werden konnte weil nur ein Datensatz in der Periode zur Verfügung stand
  7471. ####################################################################################################
  7472. sub DbRep_calcount ($$) {
  7473. my ($hash,$ch) = @_;
  7474. my $name = $hash->{NAME};
  7475. my %ncp = ();
  7476. Log3 ($name, 4, "DbRep $name - count of values used for calc:");
  7477. foreach my $key (sort(keys%{$ch})) {
  7478. Log3 ($name, 4, "$key => ". $ch->{$key});
  7479. if($ch->{$key} eq "1") {
  7480. $ncp{"$key"} = " ||";
  7481. }
  7482. }
  7483. return \%ncp;
  7484. }
  7485. ####################################################################################################
  7486. # Funktionsergebnisse in Datenbank schreiben
  7487. ####################################################################################################
  7488. sub DbRep_OutputWriteToDB($$$$$) {
  7489. my ($name,$device,$reading,$arrstr,$optxt) = @_;
  7490. my $hash = $defs{$name};
  7491. my $dbloghash = $hash->{dbloghash};
  7492. my $dbconn = $dbloghash->{dbconn};
  7493. my $dbuser = $dbloghash->{dbuser};
  7494. my $dblogname = $dbloghash->{NAME};
  7495. my $dbmodel = $hash->{dbloghash}{MODEL};
  7496. my $DbLogType = AttrVal($hash->{dbloghash}{NAME}, "DbLogType", "History");
  7497. my $supk = AttrVal($hash->{dbloghash}{NAME}, "noSupportPK", 0);
  7498. my $dbpassword = $attr{"sec$dblogname"}{secret};
  7499. my $utf8 = defined($hash->{UTF8})?$hash->{UTF8}:0;
  7500. $device =~ s/[^A-Za-z\/\d_\.-]/\//g;
  7501. $reading =~ s/[^A-Za-z\/\d_\.-]/\//g;
  7502. my $type = "calculated";
  7503. my $event = "calculated";
  7504. my $unit = "";
  7505. my $wrt = 0;
  7506. my $irowdone = 0;
  7507. my ($dbh,$sth_ih,$sth_uh,$sth_ic,$sth_uc,$err,$timestamp,$value,$date,$time,$rsf,$aggr,@row_array);
  7508. if(!$hash->{dbloghash}{HELPER}{COLSET}) {
  7509. $err = "No result of \"$hash->{LASTCMD}\" to database written. Cause: column width in \"$hash->{DEF}\" isn't set";
  7510. return ($wrt,$irowdone,$err);
  7511. }
  7512. no warnings 'uninitialized';
  7513. (undef,undef,$aggr) = DbRep_checktimeaggr($hash);
  7514. $reading = $optxt."_".$aggr."_".AttrVal($name, "readingNameMap", $reading);
  7515. $type = $defs{$device}{TYPE} if($defs{$device}); # $type vom Device ableiten
  7516. if($optxt =~ /avg|sum/) {
  7517. my @arr = split("\\|", $arrstr);
  7518. foreach my $row (@arr) {
  7519. my @a = split("#", $row);
  7520. my $runtime_string = $a[0]; # Aggregations-Alias (nicht benötigt)
  7521. $value = defined($a[1])?sprintf("%.4f",$a[1]):undef;
  7522. $rsf = $a[2]; # Datum / Zeit für DB-Speicherung
  7523. ($date,$time) = split("_",$rsf);
  7524. $time =~ s/-/:/g if($time);
  7525. if($time !~ /^(\d{2}):(\d{2}):(\d{2})$/) {
  7526. if($aggr =~ /no|day|week|month/) {
  7527. $time = "23:59:58";
  7528. } elsif ($aggr =~ /hour/) {
  7529. $time = "$time:59:58";
  7530. }
  7531. }
  7532. if ($value) {
  7533. # Daten auf maximale Länge beschneiden (DbLog-Funktion !)
  7534. ($device,$type,$event,$reading,$value,$unit) = DbLog_cutCol($hash->{dbloghash},$device,$type,$event,$reading,$value,$unit);
  7535. push(@row_array, "$date $time|$device|$type|$event|$reading|$value|$unit");
  7536. }
  7537. }
  7538. }
  7539. if($optxt =~ /min|max|diff/) {
  7540. my %rh = split("§", $arrstr);
  7541. foreach my $key (sort(keys(%rh))) {
  7542. my @k = split("\\|",$rh{$key});
  7543. $rsf = $k[2]; # Datum / Zeit für DB-Speicherung
  7544. $value = defined($k[1])?sprintf("%.4f",$k[1]):undef;
  7545. ($date,$time) = split("_",$rsf);
  7546. $time =~ s/-/:/g if($time);
  7547. if($time !~ /^(\d{2}):(\d{2}):(\d{2})$/) {
  7548. if($aggr =~ /no|day|week|month/) {
  7549. $time = "23:59:58";
  7550. } elsif ($aggr =~ /hour/) {
  7551. $time = "$time:59:58";
  7552. }
  7553. }
  7554. if ($value) {
  7555. # Daten auf maximale Länge beschneiden (DbLog-Funktion !)
  7556. ($device,$type,$event,$reading,$value,$unit) = DbLog_cutCol($hash->{dbloghash},$device,$type,$event,$reading,$value,$unit);
  7557. push(@row_array, "$date $time|$device|$type|$event|$reading|$value|$unit");
  7558. }
  7559. }
  7560. }
  7561. if (@row_array) {
  7562. # Schreibzyklus aktivieren
  7563. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoCommit => 1, mysql_enable_utf8 => $utf8 });};
  7564. if ($@) {
  7565. $err = $@;
  7566. Log3 ($name, 2, "DbRep $name - $@");
  7567. return ($wrt,$irowdone,$err);
  7568. }
  7569. # check ob PK verwendet wird, @usepkx?Anzahl der Felder im PK:0 wenn kein PK, $pkx?Namen der Felder:none wenn kein PK
  7570. my ($usepkh,$usepkc,$pkh,$pkc);
  7571. if (!$supk) {
  7572. ($usepkh,$usepkc,$pkh,$pkc) = DbRep_checkUsePK($hash,$dbloghash,$dbh);
  7573. } else {
  7574. Log3 $hash->{NAME}, 5, "DbRep $name -> Primary Key usage suppressed by attribute noSupportPK in DbLog \"$dblogname\"";
  7575. }
  7576. if (lc($DbLogType) =~ m(history)) {
  7577. # insert history mit/ohne primary key
  7578. if ($usepkh && $dbloghash->{MODEL} eq 'MYSQL') {
  7579. eval { $sth_ih = $dbh->prepare_cached("INSERT IGNORE INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  7580. } elsif ($usepkh && $dbloghash->{MODEL} eq 'SQLITE') {
  7581. eval { $sth_ih = $dbh->prepare_cached("INSERT OR IGNORE INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  7582. } elsif ($usepkh && $dbloghash->{MODEL} eq 'POSTGRESQL') {
  7583. eval { $sth_ih = $dbh->prepare_cached("INSERT INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?) ON CONFLICT DO NOTHING"); };
  7584. } else {
  7585. eval { $sth_ih = $dbh->prepare_cached("INSERT INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  7586. }
  7587. if ($@) {
  7588. $err = $@;
  7589. Log3 ($name, 2, "DbRep $name - $@");
  7590. return ($wrt,$irowdone,$err);
  7591. }
  7592. # update history mit/ohne primary key
  7593. if ($usepkh && $hash->{MODEL} eq 'MYSQL') {
  7594. $sth_uh = $dbh->prepare("REPLACE INTO history (TYPE, EVENT, VALUE, UNIT, TIMESTAMP, DEVICE, READING) VALUES (?,?,?,?,?,?,?)");
  7595. } elsif ($usepkh && $hash->{MODEL} eq 'SQLITE') {
  7596. $sth_uh = $dbh->prepare("INSERT OR REPLACE INTO history (TYPE, EVENT, VALUE, UNIT, TIMESTAMP, DEVICE, READING) VALUES (?,?,?,?,?,?,?)");
  7597. } elsif ($usepkh && $hash->{MODEL} eq 'POSTGRESQL') {
  7598. $sth_uh = $dbh->prepare("INSERT INTO history (TYPE, EVENT, VALUE, UNIT, TIMESTAMP, DEVICE, READING) VALUES (?,?,?,?,?,?,?) ON CONFLICT ($pkc)
  7599. DO UPDATE SET TIMESTAMP=EXCLUDED.TIMESTAMP, DEVICE=EXCLUDED.DEVICE, TYPE=EXCLUDED.TYPE, EVENT=EXCLUDED.EVENT, READING=EXCLUDED.READING,
  7600. VALUE=EXCLUDED.VALUE, UNIT=EXCLUDED.UNIT");
  7601. } else {
  7602. $sth_uh = $dbh->prepare("UPDATE history SET TYPE=?, EVENT=?, VALUE=?, UNIT=? WHERE (TIMESTAMP=?) AND (DEVICE=?) AND (READING=?)");
  7603. }
  7604. }
  7605. if (lc($DbLogType) =~ m(current) ) {
  7606. # insert current mit/ohne primary key
  7607. if ($usepkc && $hash->{MODEL} eq 'MYSQL') {
  7608. eval { $sth_ic = $dbh->prepare("INSERT IGNORE INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  7609. } elsif ($usepkc && $hash->{MODEL} eq 'SQLITE') {
  7610. eval { $sth_ic = $dbh->prepare("INSERT OR IGNORE INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  7611. } elsif ($usepkc && $hash->{MODEL} eq 'POSTGRESQL') {
  7612. eval { $sth_ic = $dbh->prepare("INSERT INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?) ON CONFLICT DO NOTHING"); };
  7613. } else {
  7614. # old behavior
  7615. eval { $sth_ic = $dbh->prepare("INSERT INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  7616. }
  7617. if ($@) {
  7618. $err = $@;
  7619. Log3 ($name, 2, "DbRep $name - $@");
  7620. return ($wrt,$irowdone,$err);
  7621. }
  7622. # update current mit/ohne primary key
  7623. if ($usepkc && $hash->{MODEL} eq 'MYSQL') {
  7624. $sth_uc = $dbh->prepare("REPLACE INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)");
  7625. } elsif ($usepkc && $hash->{MODEL} eq 'SQLITE') {
  7626. $sth_uc = $dbh->prepare("INSERT OR REPLACE INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)");
  7627. } elsif ($usepkc && $hash->{MODEL} eq 'POSTGRESQL') {
  7628. $sth_uc = $dbh->prepare("INSERT INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?) ON CONFLICT ($pkc)
  7629. DO UPDATE SET TIMESTAMP=EXCLUDED.TIMESTAMP, DEVICE=EXCLUDED.DEVICE, TYPE=EXCLUDED.TYPE, EVENT=EXCLUDED.EVENT, READING=EXCLUDED.READING,
  7630. VALUE=EXCLUDED.VALUE, UNIT=EXCLUDED.UNIT");
  7631. } else {
  7632. $sth_uc = $dbh->prepare("UPDATE current SET TIMESTAMP=?, TYPE=?, EVENT=?, VALUE=?, UNIT=? WHERE (DEVICE=?) AND (READING=?)");
  7633. }
  7634. }
  7635. eval { $dbh->begin_work() if($dbh->{AutoCommit}); };
  7636. if ($@) {
  7637. Log3($name, 2, "DbRep $name -> Error start transaction for history - $@");
  7638. }
  7639. Log3 $hash->{NAME}, 4, "DbRep $name - data prepared to db write:";
  7640. # SQL-Startzeit
  7641. my $wst = [gettimeofday];
  7642. my $ihs = 0;
  7643. my $uhs = 0;
  7644. foreach my $row (@row_array) {
  7645. my @a = split("\\|",$row);
  7646. $timestamp = $a[0];
  7647. $device = $a[1];
  7648. $type = $a[2];
  7649. $event = $a[3];
  7650. $reading = $a[4];
  7651. $value = $a[5];
  7652. $unit = $a[6];
  7653. Log3 $hash->{NAME}, 4, "DbRep $name - $row";
  7654. eval {
  7655. # update oder insert history
  7656. if (lc($DbLogType) =~ m(history) ) {
  7657. my $rv_uh = $sth_uh->execute($type,$event,$value,$unit,$timestamp,$device,$reading);
  7658. if ($rv_uh == 0) {
  7659. $sth_ih->execute($timestamp,$device,$type,$event,$reading,$value,$unit);
  7660. $ihs++;
  7661. } else {
  7662. $uhs++;
  7663. }
  7664. }
  7665. # update oder insert current
  7666. if (lc($DbLogType) =~ m(current) ) {
  7667. my $rv_uc = $sth_uc->execute($timestamp,$type,$event,$value,$unit,$device,$reading);
  7668. if ($rv_uc == 0) {
  7669. $sth_ic->execute($timestamp,$device,$type,$event,$reading,$value,$unit);
  7670. }
  7671. }
  7672. };
  7673. if ($@) {
  7674. $err = $@;
  7675. Log3 ($name, 2, "DbRep $name - $@");
  7676. $dbh->rollback;
  7677. $dbh->disconnect;
  7678. $ihs = 0;
  7679. $uhs = 0;
  7680. return ($wrt,0,$err);
  7681. } else {
  7682. $irowdone++;
  7683. }
  7684. }
  7685. eval {$dbh->commit() if(!$dbh->{AutoCommit});};
  7686. $dbh->disconnect;
  7687. Log3 $hash->{NAME}, 3, "DbRep $name - number of lines updated in \"$dblogname\": $uhs";
  7688. Log3 $hash->{NAME}, 3, "DbRep $name - number of lines inserted into \"$dblogname\": $ihs";
  7689. # SQL-Laufzeit ermitteln
  7690. $wrt = tv_interval($wst);
  7691. }
  7692. return ($wrt,$irowdone,$err);
  7693. }
  7694. ####################################################################################################
  7695. # Werte eines Array in DB schreiben
  7696. # Übergabe-Array: $date_ESC_$time_ESC_$device_ESC_$type_ESC_$event_ESC_$reading_ESC_$value_ESC_$unit
  7697. # $histupd = 1 wenn history update, $histupd = 0 nur history insert
  7698. #
  7699. ####################################################################################################
  7700. sub DbRep_WriteToDB($$$@) {
  7701. my ($name,$dbh,$dbloghash,$histupd,@row_array) = @_;
  7702. my $hash = $defs{$name};
  7703. my $dblogname = $dbloghash->{NAME};
  7704. my $DbLogType = AttrVal($dbloghash->{NAME}, "DbLogType", "History");
  7705. my $supk = AttrVal($dbloghash->{NAME}, "noSupportPK", 0);
  7706. my $wrt = 0;
  7707. my $irowdone = 0;
  7708. my ($sth_ih,$sth_uh,$sth_ic,$sth_uc,$err);
  7709. # check ob PK verwendet wird, @usepkx?Anzahl der Felder im PK:0 wenn kein PK, $pkx?Namen der Felder:none wenn kein PK
  7710. my ($usepkh,$usepkc,$pkh,$pkc);
  7711. if (!$supk) {
  7712. ($usepkh,$usepkc,$pkh,$pkc) = DbRep_checkUsePK($hash,$dbloghash,$dbh);
  7713. } else {
  7714. Log3 $hash->{NAME}, 5, "DbRep $name -> Primary Key usage suppressed by attribute noSupportPK in DbLog \"$dblogname\"";
  7715. }
  7716. if (lc($DbLogType) =~ m(history)) {
  7717. # insert history mit/ohne primary key
  7718. if ($usepkh && $dbloghash->{MODEL} eq 'MYSQL') {
  7719. eval { $sth_ih = $dbh->prepare_cached("INSERT IGNORE INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  7720. } elsif ($usepkh && $dbloghash->{MODEL} eq 'SQLITE') {
  7721. eval { $sth_ih = $dbh->prepare_cached("INSERT OR IGNORE INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  7722. } elsif ($usepkh && $dbloghash->{MODEL} eq 'POSTGRESQL') {
  7723. eval { $sth_ih = $dbh->prepare_cached("INSERT INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?) ON CONFLICT DO NOTHING"); };
  7724. } else {
  7725. eval { $sth_ih = $dbh->prepare_cached("INSERT INTO history (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  7726. }
  7727. if ($@) {
  7728. $err = $@;
  7729. Log3 ($name, 2, "DbRep $name - $@");
  7730. return ($wrt,$irowdone,$err);
  7731. }
  7732. # update history mit/ohne primary key
  7733. if ($usepkh && $dbloghash->{MODEL} eq 'MYSQL') {
  7734. $sth_uh = $dbh->prepare("REPLACE INTO history (TYPE, EVENT, VALUE, UNIT, TIMESTAMP, DEVICE, READING) VALUES (?,?,?,?,?,?,?)");
  7735. } elsif ($usepkh && $dbloghash->{MODEL} eq 'SQLITE') {
  7736. $sth_uh = $dbh->prepare("INSERT OR REPLACE INTO history (TYPE, EVENT, VALUE, UNIT, TIMESTAMP, DEVICE, READING) VALUES (?,?,?,?,?,?,?)");
  7737. } elsif ($usepkh && $dbloghash->{MODEL} eq 'POSTGRESQL') {
  7738. $sth_uh = $dbh->prepare("INSERT INTO history (TYPE, EVENT, VALUE, UNIT, TIMESTAMP, DEVICE, READING) VALUES (?,?,?,?,?,?,?) ON CONFLICT ($pkc)
  7739. DO UPDATE SET TIMESTAMP=EXCLUDED.TIMESTAMP, DEVICE=EXCLUDED.DEVICE, TYPE=EXCLUDED.TYPE, EVENT=EXCLUDED.EVENT, READING=EXCLUDED.READING,
  7740. VALUE=EXCLUDED.VALUE, UNIT=EXCLUDED.UNIT");
  7741. } else {
  7742. $sth_uh = $dbh->prepare("UPDATE history SET TYPE=?, EVENT=?, VALUE=?, UNIT=? WHERE (TIMESTAMP=?) AND (DEVICE=?) AND (READING=?)");
  7743. }
  7744. }
  7745. if (lc($DbLogType) =~ m(current) ) {
  7746. # insert current mit/ohne primary key
  7747. if ($usepkc && $dbloghash->{MODEL} eq 'MYSQL') {
  7748. eval { $sth_ic = $dbh->prepare("INSERT IGNORE INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  7749. } elsif ($usepkc && $dbloghash->{MODEL} eq 'SQLITE') {
  7750. eval { $sth_ic = $dbh->prepare("INSERT OR IGNORE INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  7751. } elsif ($usepkc && $dbloghash->{MODEL} eq 'POSTGRESQL') {
  7752. eval { $sth_ic = $dbh->prepare("INSERT INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?) ON CONFLICT DO NOTHING"); };
  7753. } else {
  7754. # old behavior
  7755. eval { $sth_ic = $dbh->prepare("INSERT INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)"); };
  7756. }
  7757. if ($@) {
  7758. $err = $@;
  7759. Log3 ($name, 2, "DbRep $name - $@");
  7760. return ($wrt,$irowdone,$err);
  7761. }
  7762. # update current mit/ohne primary key
  7763. if ($usepkc && $dbloghash->{MODEL} eq 'MYSQL') {
  7764. $sth_uc = $dbh->prepare("REPLACE INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)");
  7765. } elsif ($usepkc && $dbloghash->{MODEL} eq 'SQLITE') {
  7766. $sth_uc = $dbh->prepare("INSERT OR REPLACE INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?)");
  7767. } elsif ($usepkc && $dbloghash->{MODEL} eq 'POSTGRESQL') {
  7768. $sth_uc = $dbh->prepare("INSERT INTO current (TIMESTAMP, DEVICE, TYPE, EVENT, READING, VALUE, UNIT) VALUES (?,?,?,?,?,?,?) ON CONFLICT ($pkc)
  7769. DO UPDATE SET TIMESTAMP=EXCLUDED.TIMESTAMP, DEVICE=EXCLUDED.DEVICE, TYPE=EXCLUDED.TYPE, EVENT=EXCLUDED.EVENT, READING=EXCLUDED.READING,
  7770. VALUE=EXCLUDED.VALUE, UNIT=EXCLUDED.UNIT");
  7771. } else {
  7772. $sth_uc = $dbh->prepare("UPDATE current SET TIMESTAMP=?, TYPE=?, EVENT=?, VALUE=?, UNIT=? WHERE (DEVICE=?) AND (READING=?)");
  7773. }
  7774. }
  7775. eval { $dbh->begin_work() if($dbh->{AutoCommit}); };
  7776. if ($@) {
  7777. Log3($name, 2, "DbRep $name -> Error start transaction for history - $@");
  7778. }
  7779. Log3 $hash->{NAME}, 5, "DbRep $name - data prepared to db write:";
  7780. # SQL-Startzeit
  7781. my $wst = [gettimeofday];
  7782. my $ihs = 0;
  7783. my $uhs = 0;
  7784. foreach my $row (@row_array) {
  7785. my ($date,$time,$device,$type,$event,$reading,$value,$unit) = ($row =~ /^(.*)_ESC_(.*)_ESC_(.*)_ESC_(.*)_ESC_(.*)_ESC_(.*)_ESC_(.*)_ESC_(.*)$/);
  7786. Log3 $hash->{NAME}, 5, "DbRep $name - $row";
  7787. my $timestamp = $date." ".$time;
  7788. eval {
  7789. # update oder insert history
  7790. if (lc($DbLogType) =~ m(history) ) {
  7791. my $rv_uh = 0;
  7792. if($histupd) {
  7793. $rv_uh = $sth_uh->execute($type,$event,$value,$unit,$timestamp,$device,$reading);
  7794. }
  7795. if ($rv_uh == 0) {
  7796. $sth_ih->execute($timestamp,$device,$type,$event,$reading,$value,$unit);
  7797. $ihs++;
  7798. } else {
  7799. $uhs++;
  7800. }
  7801. }
  7802. # update oder insert current
  7803. if (lc($DbLogType) =~ m(current) ) {
  7804. my $rv_uc = $sth_uc->execute($timestamp,$type,$event,$value,$unit,$device,$reading);
  7805. if ($rv_uc == 0) {
  7806. $sth_ic->execute($timestamp,$device,$type,$event,$reading,$value,$unit);
  7807. }
  7808. }
  7809. };
  7810. if ($@) {
  7811. $err = $@;
  7812. Log3 ($name, 2, "DbRep $name - $@");
  7813. $dbh->rollback;
  7814. $ihs = 0;
  7815. $uhs = 0;
  7816. return ($wrt,0,$err);
  7817. } else {
  7818. $irowdone++;
  7819. }
  7820. }
  7821. eval {$dbh->commit() if(!$dbh->{AutoCommit});};
  7822. Log3 $hash->{NAME}, 3, "DbRep $name - number of lines updated in \"$dblogname\": $uhs" if($uhs);
  7823. Log3 $hash->{NAME}, 3, "DbRep $name - number of lines inserted into \"$dblogname\": $ihs" if($ihs);
  7824. # SQL-Laufzeit ermitteln
  7825. $wrt = tv_interval($wst);
  7826. return ($wrt,$irowdone,$err);
  7827. }
  7828. ################################################################
  7829. # check ob primary key genutzt wird
  7830. ################################################################
  7831. sub DbRep_checkUsePK ($$$){
  7832. my ($hash,$dbloghash,$dbh) = @_;
  7833. my $name = $hash->{NAME};
  7834. my $dbconn = $dbloghash->{dbconn};
  7835. my $upkh = 0;
  7836. my $upkc = 0;
  7837. my (@pkh,@pkc);
  7838. my $db = (split("=",(split(";",$dbconn))[0]))[1];
  7839. eval {@pkh = $dbh->primary_key( undef, undef, 'history' );};
  7840. eval {@pkc = $dbh->primary_key( undef, undef, 'current' );};
  7841. my $pkh = (!@pkh || @pkh eq "")?"none":join(",",@pkh);
  7842. my $pkc = (!@pkc || @pkc eq "")?"none":join(",",@pkc);
  7843. $pkh =~ tr/"//d;
  7844. $pkc =~ tr/"//d;
  7845. $upkh = 1 if(@pkh && @pkh ne "none");
  7846. $upkc = 1 if(@pkc && @pkc ne "none");
  7847. Log3 $hash->{NAME}, 5, "DbRep $name -> Primary Key used in $db.history: $pkh";
  7848. Log3 $hash->{NAME}, 5, "DbRep $name -> Primary Key used in $db.current: $pkc";
  7849. return ($upkh,$upkc,$pkh,$pkc);
  7850. }
  7851. ################################################################
  7852. # extrahiert aus dem übergebenen Wert nur die Zahl
  7853. ################################################################
  7854. sub DbRep_numval ($){
  7855. my ($val) = @_;
  7856. return undef if(!defined($val));
  7857. $val = ($val =~ /(-?\d+(\.\d+)?)/ ? $1 : "");
  7858. return $val;
  7859. }
  7860. ####################################################################################################
  7861. # blockierende DB-Abfrage
  7862. # liefert Ergebnis sofort zurück, setzt keine Readings
  7863. ####################################################################################################
  7864. sub DbRep_dbValue($$) {
  7865. my ($name,$cmd) = @_;
  7866. my $hash = $defs{$name};
  7867. my $dbloghash = $hash->{dbloghash};
  7868. my $dbconn = $dbloghash->{dbconn};
  7869. my $dbuser = $dbloghash->{dbuser};
  7870. my $dblogname = $dbloghash->{NAME};
  7871. my $dbpassword = $attr{"sec$dblogname"}{secret};
  7872. my $utf8 = defined($hash->{UTF8})?$hash->{UTF8}:0;
  7873. my $srs = AttrVal($name, "sqlResultFieldSep", "|");
  7874. my ($err,$ret,$dbh);
  7875. readingsDelete($hash, "errortext");
  7876. ReadingsSingleUpdateValue ($hash, "state", "running", 1);
  7877. eval {$dbh = DBI->connect("dbi:$dbconn", $dbuser, $dbpassword, { PrintError => 0, RaiseError => 1, AutoCommit => 1, AutoInactiveDestroy => 1, mysql_enable_utf8 => $utf8 });};
  7878. if ($@) {
  7879. $err = $@;
  7880. Log3 ($name, 2, "DbRep $name - $err");
  7881. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  7882. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  7883. return ($err);
  7884. }
  7885. my $sql = ($cmd =~ m/\;$/)?$cmd:$cmd.";";
  7886. # Ausgaben
  7887. Log3 ($name, 4, "DbRep $name - -------- New selection --------- ");
  7888. Log3 ($name, 4, "DbRep $name - Command: dbValue");
  7889. Log3 ($name, 4, "DbRep $name - SQL execute: $sql");
  7890. # SQL-Startzeit
  7891. my $st = [gettimeofday];
  7892. my ($sth,$r);
  7893. eval {$sth = $dbh->prepare($sql);
  7894. $r = $sth->execute();
  7895. };
  7896. if ($@) {
  7897. $err = $@;
  7898. Log3 ($name, 2, "DbRep $name - $err");
  7899. $dbh->disconnect;
  7900. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  7901. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  7902. return ($err);
  7903. }
  7904. my $nrows = 0;
  7905. if($sql =~ m/^\s*(select|pragma|show)/is) {
  7906. while (my @line = $sth->fetchrow_array()) {
  7907. Log3 ($name, 4, "DbRep $name - SQL result: @line");
  7908. $ret .= join("$srs", @line);
  7909. $ret .= "\n";
  7910. # Anzahl der Datensätze
  7911. $nrows++;
  7912. }
  7913. } else {
  7914. $nrows = $sth->rows;
  7915. eval {$dbh->commit() if(!$dbh->{AutoCommit});};
  7916. if ($@) {
  7917. $err = $@;
  7918. Log3 ($name, 2, "DbRep $name - $err");
  7919. $dbh->disconnect;
  7920. ReadingsSingleUpdateValue ($hash, "errortext", $err, 1);
  7921. ReadingsSingleUpdateValue ($hash, "state", "error", 1);
  7922. return ($err);
  7923. }
  7924. $ret = $nrows;
  7925. }
  7926. $sth->finish;
  7927. $dbh->disconnect;
  7928. # SQL-Laufzeit ermitteln
  7929. my $rt = tv_interval($st);
  7930. my $com = (split(" ",$sql, 2))[0];
  7931. Log3 ($name, 4, "DbRep $name - Number of entries processed in db $hash->{DATABASE}: $nrows by $com");
  7932. # Readingaufbereitung
  7933. readingsBeginUpdate($hash);
  7934. ReadingsBulkUpdateTimeState($hash,undef,$rt,"done");
  7935. readingsEndUpdate($hash, 1);
  7936. return ($ret);
  7937. }
  7938. ####################################################################################################
  7939. # blockierende DB-Abfrage
  7940. # liefert den Wert eines Device:Readings des nächsmöglichen Logeintrags zum
  7941. # angegebenen Zeitpunkt
  7942. #
  7943. # Aufruf: DbReadingsVal("<dbrep-device>","<device:reading>","<timestamp>,"<default>")
  7944. ####################################################################################################
  7945. sub DbReadingsVal($$$$) {
  7946. my ($name, $devread, $ts, $default) = @_;
  7947. my $hash = $defs{$name};
  7948. my $dbmodel = $defs{$hash->{HELPER}{DBLOGDEVICE}}{MODEL};
  7949. my ($err,$ret,$sql);
  7950. unless(defined($defs{$name})) {
  7951. return ("DbRep-device \"$name\" doesn't exist.");
  7952. }
  7953. unless($defs{$name}{TYPE} eq "DbRep") {
  7954. return ("\"$name\" is not a DbRep-device but of type \"".$defs{$name}{TYPE}."\"");
  7955. }
  7956. unless($ts =~ /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/) {
  7957. return ("timestamp has not a valid format. Use \"YYYY-MM-DD hh:mm:ss\" as timestamp.");
  7958. }
  7959. my ($dev,$reading) = split(":",$devread);
  7960. unless($dev && $reading) {
  7961. return ("device:reading must be specified !");
  7962. }
  7963. if($dbmodel eq "MYSQL") {
  7964. $sql = "select value from (
  7965. ( select *, TIMESTAMPDIFF(SECOND, '$ts', timestamp) as diff from history
  7966. where device='$dev' and reading='$reading' and timestamp >= '$ts' order by timestamp asc limit 1
  7967. )
  7968. union
  7969. ( select *, TIMESTAMPDIFF(SECOND, timestamp, '$ts') as diff from history
  7970. where device='$dev' and reading='$reading' and timestamp < '$ts' order by timestamp desc limit 1
  7971. )
  7972. ) x order by diff limit 1;";
  7973. } elsif ($dbmodel eq "SQLITE") {
  7974. $sql = "select value from (
  7975. select value, (julianday(timestamp) - julianday('$ts')) * 86400.0 as diff from history
  7976. where device='MyWetter' and reading='temperature' and timestamp >= '$ts'
  7977. union
  7978. select value, (julianday('$ts') - julianday(timestamp)) * 86400.0 as diff from history
  7979. where device='MyWetter' and reading='temperature' and timestamp < '$ts'
  7980. )
  7981. x order by diff limit 1;";
  7982. } elsif ($dbmodel eq "POSTGRESQL") {
  7983. $sql = "select value from (
  7984. select value, EXTRACT(EPOCH FROM (timestamp - '$ts')) as diff from history
  7985. where device='MyWetter' and reading='temperature' and timestamp >= '$ts'
  7986. union
  7987. select value, EXTRACT(EPOCH FROM ('$ts' - timestamp)) as diff from history
  7988. where device='MyWetter' and reading='temperature' and timestamp < '$ts'
  7989. )
  7990. x order by diff limit 1;";
  7991. } else {
  7992. return ("DbReadingsVal is not implemented for $dbmodel");
  7993. }
  7994. $hash->{LASTCMD} = "dbValue $sql";
  7995. $ret = DbRep_dbValue($name,$sql);
  7996. $ret = $ret?$ret:$default;
  7997. return $ret;
  7998. }
  7999. ####################################################################################################
  8000. # Browser Refresh nach DB-Abfrage
  8001. ####################################################################################################
  8002. sub browser_refresh($) {
  8003. my ($hash) = @_;
  8004. RemoveInternalTimer($hash, "browser_refresh");
  8005. {FW_directNotify("#FHEMWEB:WEB", "location.reload('true')", "")};
  8006. # map { FW_directNotify("#FHEMWEB:$_", "location.reload(true)", "") } devspec2array("WEB.*");
  8007. return;
  8008. }
  8009. ####################################################################################################
  8010. # Test-Sub zu Testzwecken
  8011. ####################################################################################################
  8012. sub testexit ($) {
  8013. my ($hash) = @_;
  8014. my $name = $hash->{NAME};
  8015. if ( !DbRep_Connect($hash) ) {
  8016. Log3 ($name, 2, "DbRep $name - DB connect failed. Database down ? ");
  8017. ReadingsSingleUpdateValue ($hash, "state", "disconnected", 1);
  8018. return;
  8019. } else {
  8020. my $dbh = $hash->{DBH};
  8021. Log3 ($name, 3, "DbRep $name - --------------- FILE INFO --------------");
  8022. my $sqlfile = $dbh->sqlite_db_filename();
  8023. Log3 ($name, 3, "DbRep $name - FILE : $sqlfile ");
  8024. # # $dbh->table_info( $catalog, $schema, $table)
  8025. # my $sth = $dbh->table_info('', '%', '%');
  8026. # my $tables = $dbh->selectcol_arrayref($sth, {Columns => [3]});
  8027. # my $table = join ', ', @$tables;
  8028. # Log3 ($name, 3, "DbRep $name - SQL_TABLES : $table");
  8029. Log3 ($name, 3, "DbRep $name - --------------- PRAGMA --------------");
  8030. my @InfoTypes = ('sqlite_db_status');
  8031. foreach my $row (@InfoTypes) {
  8032. # my @linehash = $dbh->$row;
  8033. my $array= $dbh->$row ;
  8034. # push(@row_array, @array);
  8035. while ((my $key, my $val) = each %{$array}) {
  8036. Log3 ($name, 3, "DbRep $name - PRAGMA : $key : ".%{$val});
  8037. }
  8038. }
  8039. # $sth->finish;
  8040. $dbh->disconnect;
  8041. }
  8042. return;
  8043. }
  8044. 1;
  8045. =pod
  8046. =item helper
  8047. =item summary Reporting & Management content of DbLog-DB's. Content is depicted as readings
  8048. =item summary_DE Reporting & Management von DbLog-DB Content. Darstellung als Readings
  8049. =begin html
  8050. <a name="DbRep"></a>
  8051. <h3>DbRep</h3>
  8052. <ul>
  8053. <br>
  8054. 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
  8055. 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>
  8056. Almost all database operations are implemented nonblocking. If there are exceptions it will be suggested to.
  8057. Optional the execution time of SQL-statements in background can also be determined and provided as reading.
  8058. (refer to <a href="#DbRepattr">attributes</a>). <br>
  8059. All existing readings will be deleted when a new operation starts. By attribute "readingPreventFromDel" a comma separated list of readings which are should prevent
  8060. from deletion can be provided. <br><br>
  8061. Currently the following functions are provided: <br><br>
  8062. <ul><ul>
  8063. <li> Selection of all datasets within adjustable time limits. </li>
  8064. <li> Exposure of datasets of a Device/Reading-combination within adjustable time limits. </li>
  8065. <li> Selection of datasets by usage of dynamically calclated time limits at execution time. </li>
  8066. <li> Highlighting doublets when select and display datasets (fetchrows) </li>
  8067. <li> Calculation of quantity of datasets of a Device/Reading-combination within adjustable time limits and several aggregations. </li>
  8068. <li> The calculation of summary-, difference-, maximum-, minimum- and averageValues of numeric readings within adjustable time limits and several aggregations. </li>
  8069. <li> write back results of summary-, difference-, maximum-, minimum- and average calculation into the database </li>
  8070. <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>
  8071. <li> export of datasets to file (CSV-format). </li>
  8072. <li> import of datasets from file (CSV-Format). </li>
  8073. <li> rename of device/readings in datasets </li>
  8074. <li> change of reading values in the database (changeValue) </li>
  8075. <li> automatic rename of device names in datasets and other DbRep-definitions after FHEM "rename" command (see <a href="#DbRepAutoRename">DbRep-Agent</a>) </li>
  8076. <li> Execution of arbitrary user specific SQL-commands (non-blocking) </li>
  8077. <li> Execution of arbitrary user specific SQL-commands (blocking) for usage in user own code (dbValue) </li>
  8078. <li> creation of backups of the database in running state non-blocking (MySQL, SQLite) </li>
  8079. <li> transfer dumpfiles to a FTP server after backup incl. version control</li>
  8080. <li> restore of SQLite- and MySQL-Dumps non-blocking </li>
  8081. <li> optimize the connected database (optimizeTables, vacuum) </li>
  8082. <li> report of existing database processes (MySQL) </li>
  8083. <li> purge content of current-table </li>
  8084. <li> fill up the current-table with a (tunable) extract of the history-table</li>
  8085. <li> delete consecutive datasets with different timestamp but same values (clearing up consecutive doublets) </li>
  8086. <li> Repair of a corrupted SQLite database ("database disk image is malformed") </li>
  8087. <li> transmission of datasets from source database into another (Standby) database (syncStandby) </li>
  8088. </ul></ul>
  8089. <br>
  8090. 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".
  8091. Please read more in section <a href="#DbRepAutoRename">DbRep-Agent</a> about autorename function. <br><br>
  8092. DbRep provides a <b>UserExit</b> function. With this interface the user can execute own program code dependent from free
  8093. definable Reading/Value-combinations (Regex). The interface works without respectively independent from event
  8094. generation.
  8095. Further informations you can find as described at <a href="#DbRepattr">attribute</a> "userExitFn".
  8096. <br><br>
  8097. Once a DbRep-Device is defined, the function <b>DbReadingsVal</b> is provided.
  8098. With this function you can, similar to the well known ReadingsVal, get a reading value from database.
  8099. The function execution is carried out blocking.
  8100. The command syntax is: <br><br>
  8101. <ul>
  8102. <code>DbReadingsVal("&lt;name&gt;","&lt;device:reading&gt;","&lt;timestamp&gt;","&lt;default&gt;") </code> <br><br>
  8103. <b>Examples: </b><br>
  8104. $ret = DbReadingsVal("Rep.LogDB1","MyWetter:temperature","2018-01-13 08:00:00",""); <br>
  8105. attr &lt;name&gt; userReadings oldtemp {DbReadingsVal("Rep.LogDB1","MyWetter:temperature","2018-04-13 08:00:00","")}
  8106. <br><br>
  8107. <table>
  8108. <colgroup> <col width=5%> <col width=95%> </colgroup>
  8109. <tr><td> <b>&lt;name&gt;</b> </td><td>: name of the DbRep-Device to request </td></tr>
  8110. <tr><td> <b>&lt;device:reading&gt;</b> </td><td>: device:reading whose value is to deliver </td></tr>
  8111. <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>
  8112. <tr><td> <b>&lt;default&gt;</b> </td><td>: default value if no reading value can be retrieved </td></tr>
  8113. </table>
  8114. </ul>
  8115. <br>
  8116. (*) If no value can be retrieved at the &lt;timestamp&gt; exactly requested, the chronological most convenient reading
  8117. value is delivered back.
  8118. <br><br>
  8119. FHEM-Forum: <br>
  8120. <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>
  8121. <br>
  8122. </ul>
  8123. <b>Preparations </b> <br><br>
  8124. <ul>
  8125. The module requires the usage of a DbLog instance and the credentials of the database definition will be used. <br>
  8126. Only the content of table "history" will be included if isn't other is explained. <br><br>
  8127. Overview which other Perl-modules DbRep is using: <br><br>
  8128. Net::FTP (only if FTP-Transfer after database dump is used) <br>
  8129. Net::FTPSSL (only if FTP-Transfer with encoding after database dump is used) <br>
  8130. POSIX <br>
  8131. Time::HiRes <br>
  8132. Time::Local <br>
  8133. Scalar::Util <br>
  8134. DBI <br>
  8135. Color (FHEM-module) <br>
  8136. IO::Compress::Gzip <br>
  8137. IO::Uncompress::Gunzip <br>
  8138. Blocking (FHEM-module) <br><br>
  8139. Due to performance reason the following index should be created in addition: <br>
  8140. <code>
  8141. CREATE INDEX Report_Idx ON `history` (TIMESTAMP, READING) USING BTREE;
  8142. </code>
  8143. </ul>
  8144. <br>
  8145. <a name="DbRepdefine"></a>
  8146. <b>Definition</b>
  8147. <br>
  8148. <ul>
  8149. <code>
  8150. define &lt;name&gt; DbRep &lt;name of DbLog-instance&gt;
  8151. </code>
  8152. <br><br>
  8153. (&lt;name of DbLog-instance&gt; - name of the database instance which is wanted to analyze needs to be inserted)
  8154. </ul>
  8155. <br><br>
  8156. <a name="DbRepset"></a>
  8157. <b>Set </b>
  8158. <ul>
  8159. Currently following set-commands are included. They are used to trigger the evaluations and define the evaluation option option itself.
  8160. The criteria of searching database content and determine aggregation is carried out by setting several <a href="#DbRepattr">attributes</a>.
  8161. <br><br>
  8162. <ul><ul>
  8163. <li><b> averageValue [display | writeToDB]</b>
  8164. - calculates the average value of database column "VALUE" between period given by
  8165. timestamp-<a href="#DbRepattr">attributes</a> which are set.
  8166. The reading to evaluate must be specified by attribute "reading". <br>
  8167. By attribute "averageCalcForm" the calculation variant for average determination will be configured.
  8168. Is no or the option "display" specified, the results are only displayed. Using
  8169. option "writeToDB" the calculated results are stored in the database with a new reading
  8170. name. <br>
  8171. The new readingname is built of a prefix and the original reading name,
  8172. in which the original reading name can be replaced by the value of attribute "readingNameMap".
  8173. The prefix is made up of the creation function and the aggregation. <br>
  8174. The timestamp of the new stored readings is deviated from aggregation period,
  8175. unless no unique point of time of the result can be determined.
  8176. The field "EVENT" will be filled with "calculated".<br><br>
  8177. <ul>
  8178. <b>Example of building a new reading name from the original reading "totalpac":</b> <br>
  8179. avgam_day_totalpac <br>
  8180. # &lt;creation function&gt;_&lt;aggregation&gt;_&lt;original reading&gt; <br>
  8181. </ul>
  8182. </li><br>
  8183. <li><b> cancelDump </b> - stops a running database dump. </li> <br>
  8184. <li><b> changeValue </b> - changes the saved value of readings.
  8185. If the selection is limited to particular device/reading-combinations by
  8186. <a href="#DbRepattr">attribute</a> "device" respectively "reading", it is considered as well
  8187. as possibly defined time limits by time attributes (time.*). <br>
  8188. If no limits are set, the whole database is scanned and the specified value will be
  8189. changed. <br><br>
  8190. <ul>
  8191. <b>Syntax: </b> <br>
  8192. set &lt;name&gt; changeValue "&lt;old string&gt;","&lt;new string&gt;" <br><br>
  8193. The strings have to be quoted and separated by comma.
  8194. A "string" can be: <br>
  8195. <pre>
  8196. &lt;old string&gt; : * a simple string with/without spaces, e.g. "OL 12"
  8197. * a string with usage of SQL-wildcard, e.g. "%OL%"
  8198. &lt;new string&gt; : * a simple string with/without spaces, e.g. "12 kWh"
  8199. * Perl code embedded in "{}" with quotes, e.g. "{($VALUE,$UNIT) = split(" ",$VALUE)}".
  8200. The perl expression the variables $VALUE and $UNIT are committed to. The variables are changable within
  8201. the perl code. The returned value of VALUE and UNIT are saved into the database field
  8202. VALUE respectively UNIT of the dataset.
  8203. </pre>
  8204. <b>Examples: </b> <br>
  8205. set &lt;name&gt; changeValue "OL","12 OL" <br>
  8206. # the old field value "OL" is changed to "12 OL". <br><br>
  8207. set &lt;name&gt; changeValue "%OL%","12 OL" <br>
  8208. # contains the field VALUE the substring "OL", it is changed to "12 OL". <br><br>
  8209. set &lt;name&gt; changeValue "12 kWh","{($VALUE,$UNIT) = split(" ",$VALUE)}" <br>
  8210. # the old field value "12 kWh" is splitted to VALUE=12 and UNIT=kWh and saved into the database fields <br><br>
  8211. set &lt;name&gt; changeValue "24%","{$VALUE = (split(" ",$VALUE))[0]}" <br>
  8212. # if the old field value begins with "24", it is splitted and VALUE=24 is saved (e.g. "24 kWh")
  8213. <br><br>
  8214. Summarized the relevant attributes to control function changeValue are: <br><br>
  8215. <ul>
  8216. <table>
  8217. <colgroup> <col width=5%> <col width=95%> </colgroup>
  8218. <tr><td> <b>device</b> </td><td>: selection only of datasets which contain &lt;device&gt; </td></tr>
  8219. <tr><td> <b>reading</b> </td><td>: selection only of datasets which contain &lt;reading&gt; </td></tr>
  8220. <tr><td> <b>time.*</b> </td><td>: a number of attributes to limit selection by time </td></tr>
  8221. <tr><td> <b>executeBeforeProc</b> </td><td>: execute a FHEM command (or perl-routine) before start of changeValue </td></tr>
  8222. <tr><td> <b>executeAfterProc</b> </td><td>: execute a FHEM command (or perl-routine) after changeValue is finished </td></tr>
  8223. </table>
  8224. </ul>
  8225. <br>
  8226. <br>
  8227. <b>Note:</b> <br>
  8228. Even though the function itself is designed non-blocking, make sure the assigned DbLog-device
  8229. is operating in asynchronous mode to avoid FHEMWEB from blocking. <br><br>
  8230. </li> <br>
  8231. </ul>
  8232. <li><b> countEntries [history|current] </b> - provides the number of table-entries (default: history) between period set
  8233. by timestamp-<a href="#DbRepattr">attributes</a> if set.
  8234. If timestamp-attributes are not set, all entries of the table will be count.
  8235. The <a href="#DbRepattr">attributes</a> "device" and "reading" can be used to
  8236. limit the evaluation. </li> <br>
  8237. <li><b> delEntries </b> - deletes all database entries or only the database entries specified by <a href="#DbRepattr">attributes</a> Device and/or
  8238. Reading and the entered time period between "timestamp_begin", "timestamp_end" (if set) or "timeDiffToNow/timeOlderThan". <br><br>
  8239. <ul>
  8240. "timestamp_begin" is set <b>-&gt;</b> deletes db entries <b>from</b> this timestamp until current date/time <br>
  8241. "timestamp_end" is set <b>-&gt;</b> deletes db entries <b>until</b> this timestamp <br>
  8242. both Timestamps are set <b>-&gt;</b> deletes db entries <b>between</b> these timestamps <br>
  8243. "timeOlderThan" is set <b>-&gt;</b> delete entries <b>older</b> than current time minus "timeOlderThan" <br>
  8244. "timeDiffToNow" is set <b>-&gt;</b> delete db entries <b>from</b> current time minus "timeDiffToNow" until now <br>
  8245. <br>
  8246. Due to security reasons the attribute <a href="#DbRepattr">attribute</a> "allowDeletion" needs to be set to unlock the
  8247. delete-function. <br>
  8248. The relevant attributes to control function changeValue delEntries are: <br><br>
  8249. <ul>
  8250. <table>
  8251. <colgroup> <col width=5%> <col width=95%> </colgroup>
  8252. <tr><td> <b>allowDeletion</b> </td><td>: unlock the delete function </td></tr>
  8253. <tr><td> <b>device</b> </td><td>: selection only of datasets which contain &lt;device&gt; </td></tr>
  8254. <tr><td> <b>reading</b> </td><td>: selection only of datasets which contain &lt;reading&gt; </td></tr>
  8255. <tr><td> <b>time.*</b> </td><td>: a number of attributes to limit selection by time </td></tr>
  8256. <tr><td> <b>executeBeforeProc</b> </td><td>: execute a FHEM command (or perl-routine) before start of delEntries </td></tr>
  8257. <tr><td> <b>executeAfterProc</b> </td><td>: execute a FHEM command (or perl-routine) after delEntries is finished </td></tr>
  8258. </table>
  8259. </ul>
  8260. <br>
  8261. <br>
  8262. </li>
  8263. <br>
  8264. </ul>
  8265. <li><b> delSeqDoublets [adviceRemain | adviceDelete | delete]</b> - show respectively delete identical sequentially datasets.
  8266. Therefore Device,Reading and Value of the sequentially datasets are compared.
  8267. Not deleted are the first und the last dataset of a aggregation period (e.g. hour,day,week and so on) as
  8268. well as the datasets before or after a value change (database field VALUE). <br>
  8269. The <a href="#DbRepattr">attributes</a> to define the scope of aggregation,time period, device and reading are
  8270. considered. If attribute aggregation is not set or set to "no", it will change to the default aggregation
  8271. period "day". For datasets containing numerical values it is possible to determine a variance with <a href="#DbRepattr">attribute</a>
  8272. "seqDoubletsVariance". Up to this value consecutive numerical datasets are handled as identical and should be
  8273. deleted.
  8274. <br><br>
  8275. <ul>
  8276. <table>
  8277. <colgroup> <col width=5%> <col width=95%> </colgroup>
  8278. <tr><td> <b>adviceRemain</b> </td><td>: simulates the remaining datasets in database after delete-operation (nothing will be deleted !) </td></tr>
  8279. <tr><td> <b>adviceDelete</b> </td><td>: simulates the datasets to delete in database (nothing will be deleted !) </td></tr>
  8280. <tr><td> <b>delete</b> </td><td>: deletes the consecutive doublets (see example) </td></tr>
  8281. </table>
  8282. </ul>
  8283. <br>
  8284. Due to security reasons the attribute <a href="#DbRepattr">attribute</a> "allowDeletion" needs to be set for
  8285. execute the "delete" option. <br>
  8286. The amount of datasets to show by commands "delSeqDoublets adviceDelete", "delSeqDoublets adviceRemain" is
  8287. initially limited (default: 1000) and can be adjusted by <a href="#DbRepattr">attribute</a> "limit".
  8288. The adjustment of "limit" has no impact to the "delSeqDoublets delete" function, but affects <b>ONLY</b> the
  8289. display of the data. <br>
  8290. Before and after this "delSeqDoublets" it is possible to execute a FHEM command or Perl-script
  8291. (please see <a href="#DbRepattr">attributes</a> "executeBeforeProc" and "executeAfterProc").
  8292. <br><br>
  8293. <ul>
  8294. <b>Example</b> - the remaining datasets after executing delete-option are are marked as <b>bold</b>: <br><br>
  8295. <ul>
  8296. <b>2017-11-25_00-00-05__eg.az.fridge_Pwr__power 0 </b> <br>
  8297. 2017-11-25_00-02-26__eg.az.fridge_Pwr__power 0 <br>
  8298. 2017-11-25_00-04-33__eg.az.fridge_Pwr__power 0 <br>
  8299. 2017-11-25_01-06-10__eg.az.fridge_Pwr__power 0 <br>
  8300. <b>2017-11-25_01-08-21__eg.az.fridge_Pwr__power 0 </b> <br>
  8301. <b>2017-11-25_01-08-59__eg.az.fridge_Pwr__power 60.32 </b> <br>
  8302. <b>2017-11-25_01-11-21__eg.az.fridge_Pwr__power 56.26 </b> <br>
  8303. <b>2017-11-25_01-27-54__eg.az.fridge_Pwr__power 6.19 </b> <br>
  8304. <b>2017-11-25_01-28-51__eg.az.fridge_Pwr__power 0 </b> <br>
  8305. 2017-11-25_01-31-00__eg.az.fridge_Pwr__power 0 <br>
  8306. 2017-11-25_01-33-59__eg.az.fridge_Pwr__power 0 <br>
  8307. <b>2017-11-25_02-39-29__eg.az.fridge_Pwr__power 0 </b> <br>
  8308. <b>2017-11-25_02-41-18__eg.az.fridge_Pwr__power 105.28</b> <br>
  8309. <b>2017-11-25_02-41-26__eg.az.fridge_Pwr__power 61.52 </b> <br>
  8310. <b>2017-11-25_03-00-06__eg.az.fridge_Pwr__power 47.46 </b> <br>
  8311. <b>2017-11-25_03-00-33__eg.az.fridge_Pwr__power 0 </b> <br>
  8312. 2017-11-25_03-02-07__eg.az.fridge_Pwr__power 0 <br>
  8313. 2017-11-25_23-37-42__eg.az.fridge_Pwr__power 0 <br>
  8314. <b>2017-11-25_23-40-10__eg.az.fridge_Pwr__power 0 </b> <br>
  8315. <b>2017-11-25_23-42-24__eg.az.fridge_Pwr__power 1 </b> <br>
  8316. 2017-11-25_23-42-24__eg.az.fridge_Pwr__power 1 <br>
  8317. <b>2017-11-25_23-45-27__eg.az.fridge_Pwr__power 1 </b> <br>
  8318. <b>2017-11-25_23-47-07__eg.az.fridge_Pwr__power 0 </b> <br>
  8319. 2017-11-25_23-55-27__eg.az.fridge_Pwr__power 0 <br>
  8320. <b>2017-11-25_23-48-15__eg.az.fridge_Pwr__power 0 </b> <br>
  8321. <b>2017-11-25_23-50-21__eg.az.fridge_Pwr__power 59.1 </b> <br>
  8322. <b>2017-11-25_23-55-14__eg.az.fridge_Pwr__power 52.31 </b> <br>
  8323. <b>2017-11-25_23-58-09__eg.az.fridge_Pwr__power 51.73 </b> <br>
  8324. </ul>
  8325. </ul>
  8326. </li>
  8327. <br>
  8328. <br>
  8329. <li><b> deviceRename </b> - renames the device name of a device inside the connected database (Internal DATABASE).
  8330. The devicename will allways be changed in the <b>entire</b> database. Possibly set time limits or restrictions by
  8331. <a href="#DbRepattr">attributes</a> device and/or reading will not be considered. <br><br>
  8332. <ul>
  8333. <b>Example: </b> <br>
  8334. set &lt;name&gt; deviceRename ST_5000,ST5100 <br>
  8335. # The amount of renamed device names (datasets) will be displayed in reading "device_renamed". <br>
  8336. # If the device name to be renamed was not found in the database, a WARNUNG will appear in reading "device_not_renamed". <br>
  8337. # Appropriate entries will be written to Logfile if verbose >= 3 is set.
  8338. <br><br>
  8339. <b>Note:</b> <br>
  8340. Even though the function itself is designed non-blocking, make sure the assigned DbLog-device
  8341. is operating in asynchronous mode to avoid FHEMWEB from blocking. <br><br>
  8342. </li> <br>
  8343. </ul>
  8344. <li><b> diffValue [display | writeToDB]</b>
  8345. - calculates the difference of database column "VALUE" between period given by
  8346. <a href="#DbRepattr">attributes</a> "timestamp_begin", "timestamp_end" or "timeDiffToNow / timeOlderThan".
  8347. The reading to evaluate must be defined using attribute "reading".
  8348. This function is mostly reasonable if readingvalues are increasing permanently and don't write value-differences to the database.
  8349. The difference will be generated from the first available dataset (VALUE-Field) to the last available dataset between the
  8350. specified time linits/aggregation, in which a balanced difference value of the previous aggregation period will be transfered to the
  8351. following aggregation period in case this period contains a value. <br>
  8352. An possible counter overrun (restart with value "0") will be considered (compare <a href="#DbRepattr">attribute</a> "diffAccept"). <br><br>
  8353. If only one dataset will be found within the evalution period, the difference can be calculated only in combination with the balanced
  8354. difference of the previous aggregation period. In this case a logical inaccuracy according the assignment of the difference to the particular aggregation period
  8355. can be possible. Hence in warning in "state" will be placed and the reading "less_data_in_period" with a list of periods
  8356. with only one dataset found in it will be created.
  8357. <br><br>
  8358. <ul>
  8359. <b>Note: </b><br>
  8360. Within the evaluation respectively aggregation period (day, week, month, etc.) you should make available at least one dataset
  8361. at the beginning and one dataset at the end of each aggregation period to take the difference calculation as much as possible.
  8362. <br>
  8363. <br>
  8364. </ul>
  8365. Is no or the option "display" specified, the results are only displayed. Using
  8366. option "writeToDB" the calculation results are stored in the database with a new reading
  8367. name. <br>
  8368. The new readingname is built of a prefix and the original reading name,
  8369. in which the original reading name can be replaced by the value of attribute "readingNameMap".
  8370. The prefix is made up of the creation function and the aggregation. <br>
  8371. The timestamp of the new stored readings is deviated from aggregation period,
  8372. unless no unique point of time of the result can be determined.
  8373. The field "EVENT" will be filled with "calculated".<br><br>
  8374. <ul>
  8375. <b>Example of building a new reading name from the original reading "totalpac":</b> <br>
  8376. diff_day_totalpac <br>
  8377. # &lt;creation function&gt;_&lt;aggregation&gt;_&lt;original reading&gt; <br>
  8378. </ul>
  8379. </li><br>
  8380. <li><b> dumpMySQL [clientSide | serverSide]</b>
  8381. - creates a dump of the connected MySQL database. <br>
  8382. Depending from selected option the dump will be created on Client- or on Server-Side. <br>
  8383. The variants differs each other concerning the executing system, the creating location, the usage of
  8384. attributes, the function result and the needed hardware ressources. <br>
  8385. The option "clientSide" e.g. needs more powerful FHEM-Server hardware, but saves all available
  8386. tables inclusive possibly created views. <br>
  8387. With attribute "dumpCompress" a compression of dump file after creation can be switched on.
  8388. <br><br>
  8389. <ul>
  8390. <b><u>Option clientSide</u></b> <br>
  8391. The dump will be created by client (FHEM-Server) and will be saved in FHEM log-directory by
  8392. default.
  8393. The target directory can be set by <a href="#DbRepattr">attribute</a> "dumpDirLocal" and has to be
  8394. writable by the FHEM process. <br>
  8395. Before executing the dump a table optimization can be processed optionally (see attribute
  8396. "optimizeTablesBeforeDump") as well as a FHEM-command (attribute "executeBeforeProc").
  8397. After the dump a FHEM-command can be executed as well (see attribute "executeAfterProc"). <br><br>
  8398. <b>Note: <br>
  8399. To avoid FHEM from blocking, you have to operate DbLog in asynchronous mode if the table
  8400. optimization want to be used ! </b> <br><br>
  8401. By the <a href="#DbRepattr">attributes</a> "dumpMemlimit" and "dumpSpeed" the run-time behavior of the function can be
  8402. controlled to optimize the performance and demand of ressources. <br><br>
  8403. The attributes relevant for function "dumpMySQL clientSide" are: <br><br>
  8404. <ul>
  8405. <table>
  8406. <colgroup> <col width=5%> <col width=95%> </colgroup>
  8407. <tr><td> dumpComment </td><td>: User comment in head of dump file </td></tr>
  8408. <tr><td> dumpCompress </td><td>: compress of dump files after creation </td></tr>
  8409. <tr><td> dumpDirLocal </td><td>: the local destination directory for dump file creation </td></tr>
  8410. <tr><td> dumpMemlimit </td><td>: limits memory usage </td></tr>
  8411. <tr><td> dumpSpeed </td><td>: limits CPU utilization </td></tr>
  8412. <tr><td> dumpFilesKeep </td><td>: number of dump files to keep </td></tr>
  8413. <tr><td> executeBeforeProc </td><td>: execution of FHEM command (or perl-routine) before dump </td></tr>
  8414. <tr><td> executeAfterProc </td><td>: execution of FHEM command (or perl-routine) after dump </td></tr>
  8415. <tr><td> optimizeTablesBeforeDump </td><td>: table optimization before dump </td></tr>
  8416. </table>
  8417. </ul>
  8418. <br>
  8419. After a successfull finished dump the old dumpfiles are deleted and only the number of files
  8420. defined by attribute "dumpFilesKeep" (default: 3) remain in the target directory
  8421. "dumpDirLocal". If "dumpFilesKeep = 0" is set, all
  8422. dumpfiles (also the current created file), are deleted. This setting can be helpful, if FTP transmission is used
  8423. and the created dumps are only keep remain in the FTP destination directory. <br><br>
  8424. The <b>naming convention of dump files</b> is: &lt;dbname&gt;_&lt;date&gt;_&lt;time&gt;.sql[.gzip] <br><br>
  8425. To rebuild the database from a dump file the command: <br><br>
  8426. <ul>
  8427. set &lt;name&gt; restoreMySQL &lt;filename&gt; <br><br>
  8428. </ul>
  8429. can be used. <br><br>
  8430. The created dumpfile (uncompressed) can imported on the MySQL-Server by: <br><br>
  8431. <ul>
  8432. mysql -u &lt;user&gt; -p &lt;dbname&gt; < &lt;filename&gt;.sql <br><br>
  8433. </ul>
  8434. as well to restore the database from dump file. <br><br><br>
  8435. <b><u>Option serverSide</u></b> <br>
  8436. The dump will be created on the MySQL-Server and will be saved in its Home-directory
  8437. by default. <br>
  8438. The whole history-table (not the current-table) will be exported <b>CSV-formatted</b> without
  8439. any restrictions. <br>
  8440. Before executing the dump a table optimization can be processed optionally (see attribute
  8441. "optimizeTablesBeforeDump") as well as a FHEM-command (attribute "executeBeforeProc"). <br><br>
  8442. <b>Note: <br>
  8443. To avoid FHEM from blocking, you have to operate DbLog in asynchronous mode if the table
  8444. optimization want to be used ! </b> <br><br>
  8445. After the dump a FHEM-command can be executed as well (see attribute "executeAfterProc"). <br><br>
  8446. The attributes relevant for function "dumpMySQL serverSide" are: <br><br>
  8447. <ul>
  8448. <table>
  8449. <colgroup> <col width=5%> <col width=95%> </colgroup>
  8450. <tr><td> dumpDirRemote </td><td>: destination directory of dump file on remote server </td></tr>
  8451. <tr><td> dumpCompress </td><td>: compress of dump files after creation </td></tr>
  8452. <tr><td> dumpDirLocal </td><td>: the local mounted directory dumpDirRemote </td></tr>
  8453. <tr><td> dumpFilesKeep </td><td>: number of dump files to keep </td></tr>
  8454. <tr><td> executeBeforeProc </td><td>: execution of FHEM command (or perl-routine) before dump </td></tr>
  8455. <tr><td> executeAfterProc </td><td>: execution of FHEM command (or perl-routine) after dump </td></tr>
  8456. <tr><td> optimizeTablesBeforeDump </td><td>: table optimization before dump </td></tr>
  8457. </table>
  8458. </ul>
  8459. <br>
  8460. The target directory can be set by <a href="#DbRepattr">attribute</a> "dumpDirRemote".
  8461. It must be located on the MySQL-Host and has to be writable by the MySQL-server process. <br>
  8462. The used database user must have the "FILE"-privilege. <br><br>
  8463. <b>Note:</b> <br>
  8464. If the internal version management of DbRep should be used and the size of the created dumpfile be
  8465. reported, you have to mount the remote MySQL-Server directory "dumpDirRemote" on the client
  8466. and publish it to the DbRep-device by fill out the <a href="#DbRepattr">attribute</a>
  8467. "dumpDirLocal". <br>
  8468. Same is necessary if ftp transfer after dump is to be used (attribute "ftpUse" respectively "ftpUseSSL").
  8469. <br><br>
  8470. <ul>
  8471. <b>Example: </b> <br>
  8472. attr &lt;name&gt; dumpDirRemote /volume1/ApplicationBackup/dumps_FHEM/ <br>
  8473. attr &lt;name&gt; dumpDirLocal /sds1/backup/dumps_FHEM/ <br>
  8474. attr &lt;name&gt; dumpFilesKeep 2 <br><br>
  8475. # The dump will be created remote on the MySQL-Server in directory
  8476. '/volume1/ApplicationBackup/dumps_FHEM/'. <br>
  8477. # The internal version management searches in local mounted directory '/sds1/backup/dumps_FHEM/'
  8478. for present dumpfiles and deletes these files except the last two versions. <br>
  8479. <br>
  8480. </ul>
  8481. If the internal version management is used, after a successfull finished dump old dumpfiles will
  8482. be deleted and only the number of attribute "dumpFilesKeep" (default: 3) would remain in target
  8483. directory "dumpDirLocal" (the mounted "dumpDirRemote").
  8484. In that case FHEM needs write permissions to the directory "dumpDirLocal". <br><br>
  8485. The <b>naming convention of dump files</b> is: &lt;dbname&gt;_&lt;date&gt;_&lt;time&gt;.csv[.gzip] <br><br>
  8486. You can start a restore of table history from serverSide-Backup by command: <br><br>
  8487. <ul>
  8488. set &lt;name&gt; &lt;restoreMySQL&gt; &lt;filename&gt;.csv[.gzip] <br><br>
  8489. </ul>
  8490. <br><br>
  8491. <b><u>FTP-Transfer after Dump</u></b> <br>
  8492. If those possibility is be used, the <a href="#DbRepattr">attribute</a> "ftpUse" or
  8493. "ftpUseSSL" has to be set. The latter if encoding for FTP is to be used.
  8494. The module also carries the version control of dump files in FTP-destination by attribute
  8495. "ftpDumpFilesKeep". <br>
  8496. Further <a href="#DbRepattr">attributes</a> are: <br><br>
  8497. <ul>
  8498. <table>
  8499. <colgroup> <col width=5%> <col width=95%> </colgroup>
  8500. <tr><td> ftpUse </td><td>: FTP Transfer after dump will be switched on (without SSL encoding) </td></tr>
  8501. <tr><td> ftpUser </td><td>: User for FTP-server login, default: anonymous </td></tr>
  8502. <tr><td> ftpUseSSL </td><td>: FTP Transfer with SSL encoding after dump </td></tr>
  8503. <tr><td> ftpDebug </td><td>: debugging of FTP communication for diagnostics </td></tr>
  8504. <tr><td> ftpDir </td><td>: directory on FTP-server in which the file will be send into (default: "/") </td></tr>
  8505. <tr><td> ftpDumpFilesKeep </td><td>: leave the number of dump files in FTP-destination &lt;ftpDir&gt; (default: 3) </td></tr>
  8506. <tr><td> ftpPassive </td><td>: set if passive FTP is to be used </td></tr>
  8507. <tr><td> ftpPort </td><td>: FTP-Port, default: 21 </td></tr>
  8508. <tr><td> ftpPwd </td><td>: password of FTP-User, not set by default </td></tr>
  8509. <tr><td> ftpServer </td><td>: name or IP-address of FTP-server. <b>absolutely essential !</b> </td></tr>
  8510. <tr><td> ftpTimeout </td><td>: timeout of FTP-connection in seconds (default: 30). </td></tr>
  8511. </table>
  8512. </ul>
  8513. <br>
  8514. <br>
  8515. </ul>
  8516. </li><br>
  8517. <li><b> dumpSQLite </b> - creates a dump of the connected SQLite database. <br>
  8518. This function uses the SQLite Online Backup API and allow to create a consistent backup of the
  8519. database during the normal operation.
  8520. The dump will be saved in FHEM log-directory by default.
  8521. The target directory can be defined by <a href="#DbRepattr">attribute</a> "dumpDirLocal" and
  8522. has to be writable by the FHEM process. <br>
  8523. Before executing the dump a table optimization can be processed optionally (see attribute
  8524. "optimizeTablesBeforeDump").
  8525. <br><br>
  8526. <b>Note: <br>
  8527. To avoid FHEM from blocking, you have to operate DbLog in asynchronous mode if the table
  8528. optimization want to be used ! </b> <br><br>
  8529. Before and after the dump a FHEM-command can be executed (see attribute "executeBeforeProc",
  8530. "executeAfterProc"). <br><br>
  8531. The attributes relevant for function "dumpMySQL serverSide" are: <br><br>
  8532. <ul>
  8533. <table>
  8534. <colgroup> <col width=5%> <col width=95%> </colgroup>
  8535. <tr><td> dumpCompress </td><td>: compress of dump files after creation </td></tr>
  8536. <tr><td> dumpDirLocal </td><td>: the local mounted directory dumpDirRemote </td></tr>
  8537. <tr><td> dumpFilesKeep </td><td>: number of dump files to keep </td></tr>
  8538. <tr><td> executeBeforeProc </td><td>: execution of FHEM command (or perl-routine) before dump </td></tr>
  8539. <tr><td> executeAfterProc </td><td>: execution of FHEM command (or perl-routine) after dump </td></tr>
  8540. <tr><td> optimizeTablesBeforeDump </td><td>: table optimization before dump </td></tr>
  8541. </table>
  8542. </ul>
  8543. <br>
  8544. After a successfull finished dump the old dumpfiles are deleted and only the number of attribute
  8545. "dumpFilesKeep" (default: 3) remain in the target directory "dumpDirLocal". If "dumpFilesKeep = 0" is set, all
  8546. dumpfiles (also the current created file), are deleted. This setting can be helpful, if FTP transmission is used
  8547. and the created dumps are only keep remain in the FTP destination directory. <br><br>
  8548. The <b>naming convention of dump files</b> is: &lt;dbname&gt;_&lt;date&gt;_&lt;time&gt;.sqlitebkp[.gzip] <br><br>
  8549. The database can be restored by command "set &lt;name&gt; restoreSQLite &lt;filename&gt;" <br>
  8550. The created dump file can be transfered to a FTP-server. Please see explanations about FTP-
  8551. transfer in topic "dumpMySQL". <br><br>
  8552. </li><br>
  8553. <li><b> eraseReadings </b> - deletes all created readings in the device, except reading "state" and readings, which are
  8554. contained in exception list defined by attribute "readingPreventFromDel".
  8555. </li><br>
  8556. <li><b> exportToFile [&lt;file&gt;] </b>
  8557. - exports DB-entries to a file in CSV-format of time period specified by time attributes. <br>
  8558. Limitation of selections can be done by <a href="#DbRepattr">attributes</a> device and/or
  8559. reading.
  8560. The filename can be defined by <a href="#DbRepattr">attribute</a> "expimpfile". <br>
  8561. Optionally a file can be specified as a command option (/path/file) and overloads a possibly
  8562. defined attribute "expimpfile". The filename may contain wildcards as described
  8563. in attribute section of "expimpfile".
  8564. <br>
  8565. By setting attribute "aggregation" the export of datasets will be splitted into time slices
  8566. corresponding to the specified aggregation.
  8567. If, for example, "aggregation = month" is set, the data are selected in monthly packets and written
  8568. into the exportfile. Thereby the usage of main memory is optimized if very large amount of data
  8569. is exported and avoid the "died prematurely" error. <br><br>
  8570. The attributes relevant for this function are: <br><br>
  8571. <ul>
  8572. <table>
  8573. <colgroup> <col width=5%> <col width=95%> </colgroup>
  8574. <tr><td> <b>aggregation</b> </td><td>: determination of selection time slices </td></tr>
  8575. <tr><td> <b>device</b> </td><td>: select only datasets which are contain &lt;device&gt; </td></tr>
  8576. <tr><td> <b>reading</b> </td><td>: select only datasets which are contain &lt;reading&gt; </td></tr>
  8577. <tr><td> <b>executeBeforeProc</b> </td><td>: execution of FHEM command (or perl-routine) before export </td></tr>
  8578. <tr><td> <b>executeAfterProc</b> </td><td>: execution of FHEM command (or perl-routine) after export </td></tr>
  8579. <tr><td> <b>expimpfile</b> </td><td>: the name of exportfile </td></tr>
  8580. <tr><td> <b>time.*</b> </td><td>: a number of attributes to limit selection by time </td></tr>
  8581. </table>
  8582. </ul>
  8583. </li> <br>
  8584. <li><b> fetchrows [history|current] </b>
  8585. - provides <b>all</b> table entries (default: history)
  8586. of time period set by time <a href="#DbRepattr">attributes</a> respectively selection conditions
  8587. by attributes "device" and "reading".
  8588. An aggregation set will <b>not</b> be considered. <br>
  8589. The direction of data selection can be determined by <a href="#DbRepattr">attribute</a>
  8590. "fetchRoute". <br><br>
  8591. Every reading of result is composed of the dataset timestring , an index, the device name
  8592. and the reading name.
  8593. The function has the capability to reconize multiple occuring datasets (doublets).
  8594. Such doublets are marked by an index > 1. <br>
  8595. Doublets can be highlighted in terms of color by setting attribut e"fetchMarkDuplicates". <br><br>
  8596. <b>Note:</b> <br>
  8597. Highlighted readings are not displayed again after restart or rereadcfg because of they are not
  8598. saved in statefile. <br><br>
  8599. This attribute is preallocated with some colors, but can be changed by colorpicker-widget: <br><br>
  8600. <ul>
  8601. <code>
  8602. attr &lt;DbRep-Device&gt; widgetOverride fetchMarkDuplicates:colorpicker
  8603. </code>
  8604. </ul>
  8605. <br>
  8606. The readings of result are composed like the following sceme: <br><br>
  8607. <ul>
  8608. <b>Example:</b> <br>
  8609. 2017-10-22_03-04-43__1__SMA_Energymeter__Bezug_WirkP_Kosten_Diff <br>
  8610. # &lt;date&gt;_&lt;time&gt;__&lt;index&gt;__&lt;device&gt;__&lt;reading&gt;
  8611. </ul>
  8612. <br>
  8613. For a better overview the relevant attributes are listed here in a table: <br><br>
  8614. <ul>
  8615. <table>
  8616. <colgroup> <col width=5%> <col width=95%> </colgroup>
  8617. <tr><td> <b>fetchRoute</b> </td><td>: direction of selection read in database </td></tr>
  8618. <tr><td> <b>limit</b> </td><td>: limits the number of datasets to select and display </td></tr>
  8619. <tr><td> <b>fetchMarkDuplicates</b> </td><td>: Highlighting of found doublets </td></tr>
  8620. <tr><td> <b>device</b> </td><td>: select only datasets which are contain &lt;device&gt; </td></tr>
  8621. <tr><td> <b>reading</b> </td><td>: select only datasets which are contain &lt;reading&gt; </td></tr>
  8622. <tr><td> <b>time.*</b> </td><td>: A number of attributes to limit selection by time </td></tr>
  8623. <tr><td> <b>valueFilter</b> </td><td>: filter datasets of database field "VALUE" by a regular expression </td></tr>
  8624. </table>
  8625. </ul>
  8626. <br>
  8627. <br>
  8628. <b>Note:</b> <br>
  8629. Although the module is designed non-blocking, a huge number of selection result (huge number of rows)
  8630. can overwhelm the browser session respectively FHEMWEB.
  8631. Due to the sample space can be limited by <a href="#limit">attribute</a> "limit".
  8632. Of course ths attribute can be increased if your system capabilities allow a higher workload. <br><br>
  8633. </li> <br>
  8634. <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>
  8635. <ul>
  8636. <b>input format: </b> Date,Time,Value,[Unit] <br>
  8637. # Unit is optional, attributes of device, reading must be set ! <br>
  8638. # If "Value=0" has to be inserted, use "Value = 0.0" to do it. <br><br>
  8639. <b>example:</b> 2016-08-01,23:00:09,TestValue,TestUnit <br>
  8640. # Spaces are NOT allowed in fieldvalues ! <br>
  8641. <br>
  8642. <b>Note: </b><br>
  8643. Please consider to insert AT LEAST two datasets into the intended time / aggregatiom period (day, week, month, etc.) because of
  8644. it's needed by function diffValue. Otherwise no difference can be calculated and diffValue will be print out "0" for the respective period !
  8645. <br>
  8646. <br>
  8647. </li>
  8648. </ul>
  8649. <li><b> importFromFile [&lt;file&gt;] </b>
  8650. - imports data in CSV format from file into database. <br>
  8651. The filename can be defined by <a href="#DbRepattr">attribute</a> "expimpfile". <br>
  8652. Optionally a file can be specified as a command option (/path/file) and overloads a possibly
  8653. defined attribute "expimpfile". The filename may contain wildcards as described
  8654. in attribute section of "expimpfile". <br><br>
  8655. <ul>
  8656. <b>dataset format: </b> <br>
  8657. "TIMESTAMP","DEVICE","TYPE","EVENT","READING","VALUE","UNIT" <br><br>
  8658. # The fields "TIMESTAMP","DEVICE","TYPE","EVENT","READING" and "VALUE" have to be set. The field "UNIT" is optional.
  8659. The file content will be imported transactional. That means all of the content will be imported or, in case of error, nothing of it.
  8660. 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.
  8661. It could lead to blocking or overload FHEM ! <br><br>
  8662. <b>Example for a source dataset: </b> <br>
  8663. "2016-09-25 08:53:56","STP_5000","SMAUTILS","etotal: 11859.573","etotal","11859.573","" <br>
  8664. <br>
  8665. The attributes relevant for this function are: <br><br>
  8666. <ul>
  8667. <table>
  8668. <colgroup> <col width=5%> <col width=95%> </colgroup>
  8669. <tr><td> <b>executeBeforeProc</b> </td><td>: execution of FHEM command (or perl-routine) before import </td></tr>
  8670. <tr><td> <b>executeAfterProc</b> </td><td>: execution of FHEM command (or perl-routine) after import </td></tr>
  8671. <tr><td> <b>expimpfile</b> </td><td>: the name of exportfile </td></tr>
  8672. </table>
  8673. </ul>
  8674. </li> <br>
  8675. </ul>
  8676. <br>
  8677. <li><b> maxValue [display | writeToDB]</b>
  8678. - calculates the maximum value of database column "VALUE" between period given by
  8679. <a href="#DbRepattr">attributes</a> "timestamp_begin", "timestamp_end" or "timeDiffToNow / timeOlderThan".
  8680. The reading to evaluate must be defined using attribute "reading".
  8681. The evaluation contains the timestamp of the <b>last</b> appearing of the identified maximum value
  8682. within the given period. <br>
  8683. Is no or the option "display" specified, the results are only displayed. Using
  8684. option "writeToDB" the calculated results are stored in the database with a new reading
  8685. name. <br>
  8686. The new readingname is built of a prefix and the original reading name,
  8687. in which the original reading name can be replaced by the value of attribute "readingNameMap".
  8688. The prefix is made up of the creation function and the aggregation. <br>
  8689. The timestamp of the new stored readings is deviated from aggregation period,
  8690. unless no unique point of time of the result can be determined.
  8691. The field "EVENT" will be filled with "calculated".<br><br>
  8692. <ul>
  8693. <b>Example of building a new reading name from the original reading "totalpac":</b> <br>
  8694. max_day_totalpac <br>
  8695. # &lt;creation function&gt;_&lt;aggregation&gt;_&lt;original reading&gt; <br>
  8696. </ul>
  8697. </li><br>
  8698. <li><b> minValue [display | writeToDB]</b>
  8699. - calculates the minimum value of database column "VALUE" between period given by
  8700. <a href="#DbRepattr">attributes</a> "timestamp_begin", "timestamp_end" or "timeDiffToNow / timeOlderThan".
  8701. The reading to evaluate must be defined using attribute "reading".
  8702. The evaluation contains the timestamp of the <b>first</b> appearing of the identified minimum
  8703. value within the given period. <br>
  8704. Is no or the option "display" specified, the results are only displayed. Using
  8705. option "writeToDB" the calculated results are stored in the database with a new reading
  8706. name. <br>
  8707. The new readingname is built of a prefix and the original reading name,
  8708. in which the original reading name can be replaced by the value of attribute "readingNameMap".
  8709. The prefix is made up of the creation function and the aggregation. <br>
  8710. The timestamp of the new stored readings is deviated from aggregation period,
  8711. unless no unique point of time of the result can be determined.
  8712. The field "EVENT" will be filled with "calculated".<br><br>
  8713. <ul>
  8714. <b>Example of building a new reading name from the original reading "totalpac":</b> <br>
  8715. min_day_totalpac <br>
  8716. # &lt;creation function&gt;_&lt;aggregation&gt;_&lt;original reading&gt; <br>
  8717. </ul>
  8718. </li><br>
  8719. <li><b> optimizeTables </b> - optimize tables in the connected database (MySQL). <br>
  8720. Before and after an optimization it is possible to execute a FHEM command.
  8721. (please see <a href="#DbRepattr">attributes</a> "executeBeforeProc", "executeAfterProc")
  8722. <br><br>
  8723. <ul>
  8724. <b>Note:</b> <br>
  8725. Even though the function itself is designed non-blocking, make sure the assigned DbLog-device
  8726. is operating in asynchronous mode to avoid FHEMWEB from blocking. <br><br>
  8727. </li><br>
  8728. </ul>
  8729. <li><b> readingRename </b> - renames the reading name of a device inside the connected database (see Internal DATABASE).
  8730. The readingname will allways be changed in the <b>entire</b> database. Possibly set time limits or restrictions by
  8731. <a href="#DbRepattr">attributes</a> device and/or reading will not be considered. <br><br>
  8732. <ul>
  8733. <b>Example: </b> <br>
  8734. set &lt;name&gt; readingRename &lt;old reading name&gt;,&lt;new reading name&gt; <br>
  8735. # The amount of renamed reading names (datasets) will be displayed in reading "reading_renamed". <br>
  8736. # If the reading name to be renamed was not found in the database, a WARNUNG will appear in reading "reading_not_renamed". <br>
  8737. # Appropriate entries will be written to Logfile if verbose >= 3 is set.
  8738. <br><br>
  8739. <b>Note:</b> <br>
  8740. Even though the function itself is designed non-blocking, make sure the assigned DbLog-device
  8741. is operating in asynchronous mode to avoid FHEMWEB from blocking. <br><br>
  8742. </li> <br>
  8743. </ul>
  8744. <li><b> repairSQLite </b> - repairs a corrupted SQLite database. <br>
  8745. A corruption is usally existent when the error message "database disk image is malformed"
  8746. appears in reading "state" of the connected DbLog-device.
  8747. If the command was started, the connected DbLog-device will firstly disconnected from the
  8748. database for 10 hours (36000 seconds) automatically (breakup time). After the repair is
  8749. finished, the DbLog-device will be connected to the (repaired) database immediately. <br>
  8750. As an argument the command can be completed by a differing breakup time (in seconds). <br>
  8751. The corrupted database is saved as &lt;database&gt;.corrupt in same directory. <br><br>
  8752. <ul>
  8753. <b>Example: </b><br>
  8754. set &lt;name&gt; repairSQLite <br>
  8755. # the database is trying to repair, breakup time is 10 hours <br>
  8756. set &lt;name&gt; repairSQLite 600 <br>
  8757. # the database is trying to repair, breakup time is 10 minutes
  8758. <br><br>
  8759. <b>Note:</b> <br>
  8760. It can't be guaranteed, that the repair attempt proceed successfully and no data loss will result.
  8761. Depending from corruption severity data loss may occur or the repair will fail even though
  8762. no error appears during the repair process. Please make sure a valid backup took place ! <br><br>
  8763. </li> <br>
  8764. </ul>
  8765. <li><b> restoreMySQL &lt;File&gt; </b> - restore a database from serverSide- or clientSide-Dump. <br>
  8766. The function provides a drop-down-list of files which can be used for restore. <br><br>
  8767. <b>Usage of serverSide-Dumps </b> <br>
  8768. The content of history-table will be restored from a serverSide-Dump.
  8769. Therefore the remote directory "dumpDirRemote" of the MySQL-Server has to be mounted on the
  8770. Client and make it usable to the DbRep-device by setting <a href="#DbRepattr">attribute</a>
  8771. "dumpDirLocal" to the appropriate value. <br>
  8772. All files with extension "csv[.gzip]" and if the filename is beginning with the name of the connected database
  8773. (see Internal DATABASE) are listed.
  8774. <br><br>
  8775. <b>Usage of clientSide-Dumps </b> <br>
  8776. All tables and views (if present) are restored.
  8777. The directory which contains the dump files has to be set by <a href="#DbRepattr">attribute</a>
  8778. "dumpDirLocal" to make it usable by the DbRep device. <br>
  8779. All files with extension "sql[.gzip]" and if the filename is beginning with the name of the connected database
  8780. (see Internal DATABASE) are listed. <br>
  8781. The restore speed depends of the server variable "<b>max_allowed_packet</b>". You can change
  8782. this variable in file my.cnf to adapt the speed. Please consider the need of sufficient ressources
  8783. (especially RAM).
  8784. <br><br>
  8785. The database user needs rights for database management, e.g.: <br>
  8786. CREATE, ALTER, INDEX, DROP, SHOW VIEW, CREATE VIEW
  8787. <br><br>
  8788. </li><br>
  8789. <li><b> restoreSQLite &lt;File&gt;.sqlitebkp[.gzip] </b> - restores a backup of SQLite database. <br>
  8790. The function provides a drop-down-list of files which can be used for restore.
  8791. The data stored in the current database are deleted respectively overwritten.
  8792. All files with extension "sqlitebkp[.gzip]" and if the filename is beginning with the name of the connected database
  8793. will are listed. <br><br>
  8794. </li><br>
  8795. <li><b> sqlCmd </b> - executes an arbitrary user specific command. <br>
  8796. If the command contains a operation to delete data, the <a href="#DbRepattr">attribute</a>
  8797. "allowDeletion" has to be set for security reason. <br>
  8798. The statement doesn't consider limitations by attributes "device", "reading", "time.*"
  8799. respectively "aggregation". <br>
  8800. If the <a href="#DbRepattr">attribute</a> "timestamp_begin" respectively "timestamp_end"
  8801. is assumed in the statement, it is possible to use placeholder "<b>§timestamp_begin§</b>" respectively
  8802. "<b>§timestamp_end§</b>" on suitable place. <br><br>
  8803. If you want update a dataset, you have to add "TIMESTAMP=TIMESTAMP" to the update-statement to avoid changing the
  8804. original timestamp. <br><br>
  8805. <ul>
  8806. <b>Examples of SQL-statements: </b> <br><br>
  8807. <ul>
  8808. <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>
  8809. <li>set &lt;name&gt; sqlCmd select DEVICE, count(*) from history where TIMESTAMP >= "2017-05-06 00:00:00" group by DEVICE </li>
  8810. <li>set &lt;name&gt; sqlCmd select DEVICE, count(*) from history where TIMESTAMP >= §timestamp_begin§ group by DEVICE </li>
  8811. <li>set &lt;name&gt; sqlCmd select * from history where DEVICE like "Te%t" order by `TIMESTAMP` desc </li>
  8812. <li>set &lt;name&gt; sqlCmd select * from history where `TIMESTAMP` > "2017-05-09 18:03:00" order by `TIMESTAMP` desc </li>
  8813. <li>set &lt;name&gt; sqlCmd select * from current order by `TIMESTAMP` desc </li>
  8814. <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>
  8815. <li>set &lt;name&gt; sqlCmd delete from current </li>
  8816. <li>set &lt;name&gt; sqlCmd delete from history where TIMESTAMP < "2016-05-06 00:00:00" </li>
  8817. <li>set &lt;name&gt; sqlCmd update history set TIMESTAMP=TIMESTAMP,VALUE='Val' WHERE VALUE='TestValue' </li>
  8818. <li>set &lt;name&gt; sqlCmd select * from history where DEVICE = "Test" </li>
  8819. <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>
  8820. </ul>
  8821. <br>
  8822. The result of the statement will be shown in <a href="#DbRepReadings">Reading</a> "SqlResult".
  8823. The formatting of result can be choosen by <a href="#DbRepattr">attribute</a> "sqlResultFormat", as well as the used
  8824. field separator can be determined by <a href="#DbRepattr">attribute</a> "sqlResultFieldSep". <br><br>
  8825. The module provides a command history once a sqlCmd command was executed successfully.
  8826. To use this option, activate the attribute "sqlCmdHistoryLength" with list lenght you want. <br><br>
  8827. For a better overview the relevant attributes for sqlCmd are listed in a table: <br><br>
  8828. <ul>
  8829. <table>
  8830. <colgroup> <col width=5%> <col width=95%> </colgroup>
  8831. <tr><td> <b>allowDeletion</b> </td><td>: activates capabilty to delete datasets </td></tr>
  8832. <tr><td> <b>sqlResultFormat</b> </td><td>: determines presentation style of command result </td></tr>
  8833. <tr><td> <b>sqlResultFieldSep</b> </td><td>: choice of a useful field separator for result </td></tr>
  8834. <tr><td> <b>sqlCmdHistoryLength</b> </td><td>: activates command history and length </td></tr>
  8835. </table>
  8836. </ul>
  8837. <br>
  8838. <br>
  8839. <b>Note:</b> <br>
  8840. Even though the module works non-blocking regarding to database operations, a huge
  8841. sample space (number of rows/readings) could block the browser session respectively
  8842. FHEMWEB.
  8843. If you are unsure about the result of the statement, you should preventively add a limit to
  8844. the statement. <br><br>
  8845. </li><br>
  8846. </ul>
  8847. <li><b> sqlCmdHistory </b> - If history is activated by <a href="#DbRepattr">attribute</a> "sqlCmdHistoryLength", an already
  8848. successfully executed sqlCmd-command can be repeated from a drop-down list. <br>
  8849. By execution of the last list entry, "__purge_historylist__", the list itself can be deleted. <br>
  8850. If the statement contains "," this character is displayed as "&lt;c&gt;" in the history
  8851. list due to technical restrictions. <br>
  8852. </li><br>
  8853. <li><b> sqlSpecial </b> - This function provides a drop-down list with a selection of prepared reportings. <br>
  8854. The statements result is depicted in reading "SqlResult".
  8855. The result can be formatted by <a href="#DbRepattr">attribute</a> "sqlResultFormat",
  8856. a well as the used field separator by <a href="#DbRepattr">attribute</a> "sqlResultFieldSep".
  8857. <br><br>
  8858. The relevant attributes for this function are: <br><br>
  8859. <ul>
  8860. <table>
  8861. <colgroup> <col width=5%> <col width=95%> </colgroup>
  8862. <tr><td> <b>sqlResultFormat</b> </td><td>: determines the formatting of the result </td></tr>
  8863. <tr><td> <b>sqlResultFieldSep</b> </td><td>: determines the used field separator in statement result </td></tr>
  8864. </table>
  8865. </ul>
  8866. <br>
  8867. The following predefined reportings are selectable: <br><br>
  8868. <ul>
  8869. <table>
  8870. <colgroup> <col width=5%> <col width=95%> </colgroup>
  8871. <tr><td> <b>50mostFreqLogsLast2days </b> </td><td>: reports the 50 most occuring log entries of the last 2 days </td></tr>
  8872. <tr><td> <b>allDevCount </b> </td><td>: all devices occuring in database and their quantity </td></tr>
  8873. <tr><td> <b>allDevReadCount </b> </td><td>: all device/reading combinations occuring in database and their quantity </td></tr>
  8874. </table>
  8875. </ul>
  8876. </li><br><br>
  8877. <li><b> sumValue [display | writeToDB]</b>
  8878. - calculates the summary of database column "VALUE" between period given by
  8879. <a href="#DbRepattr">attributes</a> "timestamp_begin", "timestamp_end" or
  8880. "timeDiffToNow / timeOlderThan". The reading to evaluate must be defined using attribute
  8881. "reading". Using this function is mostly reasonable if value-differences of readings
  8882. are written to the database. <br>
  8883. Is no or the option "display" specified, the results are only displayed. Using
  8884. option "writeToDB" the calculation results are stored in the database with a new reading
  8885. name. <br>
  8886. The new readingname is built of a prefix and the original reading name,
  8887. in which the original reading name can be replaced by the value of attribute "readingNameMap".
  8888. The prefix is made up of the creation function and the aggregation. <br>
  8889. The timestamp of the new stored readings is deviated from aggregation period,
  8890. unless no unique point of time of the result can be determined.
  8891. The field "EVENT" will be filled with "calculated".<br><br>
  8892. <ul>
  8893. <b>Example of building a new reading name from the original reading "totalpac":</b> <br>
  8894. sum_day_totalpac <br>
  8895. # &lt;creation function&gt;_&lt;aggregation&gt;_&lt;original reading&gt; <br>
  8896. </li> <br>
  8897. </ul>
  8898. <br>
  8899. <li><b> syncStandby &lt;DbLog-Device Standby&gt; </b>
  8900. - datasets of the connected database (source) are transmitted into another database
  8901. (Standby-database). <br>
  8902. Here the "&lt;DbLog-Device Standby&gt;" is the DbLog-Device what is connected to the
  8903. Standby-database. <br><br>
  8904. All the datasets which are determined by timestamp-<a href="#limit">attributes</a>
  8905. or respectively the attributes "device", "reading" are transmitted. <br>
  8906. The datasets are transmitted in time slices accordingly to the adjusted aggregation.
  8907. If the attribute "aggregation" has value "no" or "month", the datasets are transmitted
  8908. automatically in daily time slices into standby-database.
  8909. Source- and Standby-database can be of different types.
  8910. <br><br>
  8911. The relevant attributes to control the syncStandby function are: <br><br>
  8912. <ul>
  8913. <table>
  8914. <colgroup> <col width=5%> <col width=95%> </colgroup>
  8915. <tr><td> <b>aggregation</b> </td><td>: adjustment of time slices for data transmission (hour,day,week) </td></tr>
  8916. <tr><td> <b>device</b> </td><td>: transmit only datasets which are contain &lt;device&gt; </td></tr>
  8917. <tr><td> <b>reading</b> </td><td>: transmit only datasets which are contain &lt;reading&gt; </td></tr>
  8918. <tr><td> <b>time.*</b> </td><td>: A number of attributes to limit selection by time </td></tr>
  8919. </table>
  8920. </ul>
  8921. <br>
  8922. <br>
  8923. </li> <br>
  8924. <li><b> tableCurrentFillup </b> - the current-table will be filled u with an extract of the history-table.
  8925. The <a href="#DbRepattr">attributes</a> for limiting time and device, reading are considered.
  8926. Thereby the content of the extract can be affected. In the associated DbLog-device the attribute "DbLogType" should be set to
  8927. "SampleFill/History". </li> <br>
  8928. <li><b> tableCurrentPurge </b> - deletes the content of current-table. There are no limits, e.g. by attributes "timestamp_begin", "timestamp_end", device, reading
  8929. and so on, considered. </li> <br>
  8930. <li><b> vacuum </b> - optimize tables in the connected database (SQLite, PostgreSQL). <br>
  8931. Before and after an optimization it is possible to execute a FHEM command.
  8932. (please see <a href="#DbRepattr">attributes</a> "executeBeforeProc", "executeAfterProc")
  8933. <br><br>
  8934. <ul>
  8935. <b>Note:</b> <br>
  8936. Even though the function itself is designed non-blocking, make sure the assigned DbLog-device
  8937. is operating in asynchronous mode to avoid FHEM from blocking. <br><br>
  8938. </li>
  8939. </ul><br>
  8940. <br>
  8941. </ul></ul>
  8942. <b>For all evaluation variants (except sqlCmd,deviceRename,readingRename) applies: </b> <br>
  8943. In addition to the needed reading the device can be complemented to restrict the datasets for reporting / function.
  8944. If no time limit attribute is set but aggregation is set, the period from the oldest dataset in database to the current
  8945. date/time will be used as selection criterion. If the oldest dataset wasn't identified, then '1970-01-01 01:00:00' is used
  8946. as start date (see get &lt;name&gt; "minTimestamp" also).
  8947. If both time limit attribute and aggregation isn't set, the selection on database is runnung without timestamp criterion.
  8948. <br><br>
  8949. <b>Note: </b> <br>
  8950. 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.
  8951. <br><br>
  8952. </ul>
  8953. <a name="DbRepget"></a>
  8954. <b>Get </b>
  8955. <ul>
  8956. The get-commands of DbRep provide to retrieve some metadata of the used database instance.
  8957. Those are for example adjusted server parameter, server variables, datadasestatus- and table informations. THe available get-functions depending of
  8958. the used database type. So for SQLite curently only "get svrinfo" is usable. The functions nativ are delivering a lot of outpit values.
  8959. They can be limited by function specific <a href="#DbRepattr">attributes</a>. The filter has to be setup by a comma separated list.
  8960. SQL-Wildcard (%) can be used to setup the list arguments.
  8961. <br><br>
  8962. <b>Note: </b> <br>
  8963. After executing a get-funktion in detail view please make a browser refresh to see the results !
  8964. <br><br>
  8965. <ul><ul>
  8966. <li><b> blockinginfo </b> - list the current system wide running background processes (BlockingCalls) together with their informations.
  8967. If character string is too long (e.g. arguments) it is reported shortened.
  8968. </li>
  8969. <br><br>
  8970. <li><b> dbstatus </b> - lists global informations about MySQL server status (e.g. informations related to cache, threads, bufferpools, etc. ).
  8971. Initially all available informations are reported. Using the <a href="#DbRepattr">attribute</a> "showStatus" the quantity of
  8972. results can be limited to show only the desired values. Further detailed informations of items meaning are
  8973. explained <a href=http://dev.mysql.com/doc/refman/5.7/en/server-status-variables.html>there</a>. <br>
  8974. <br><ul>
  8975. <b>Example</b> <br>
  8976. get &lt;name&gt; dbstatus <br>
  8977. attr &lt;name&gt; showStatus %uptime%,%qcache% <br>
  8978. # Only readings containing "uptime" and "qcache" in name will be created
  8979. </li>
  8980. <br><br>
  8981. </ul>
  8982. <li><b> dbValue &lt;SQL-statement&gt;</b> -
  8983. Executes the specified SQL-statement in <b>blocking</b> manner. Because of its mode of operation
  8984. this function is particular convenient for user own perl scripts. <br>
  8985. The input accepts multi line commands and delivers multi line results as well.
  8986. If several fields are selected and passed back, the fieds are separated by the separator defined
  8987. by <a href="#DbRepattr">attribute</a> "sqlResultFieldSep" (default "|"). Several result lines
  8988. are separated by newline ("\n"). <br>
  8989. This function only set/update status readings, the userExitFn function isn't called.
  8990. <br>
  8991. <br><ul>
  8992. <b>Examples for use in FHEMWEB</b> <br>
  8993. {fhem("get &lt;name&gt; dbValue select device,count(*) from history where timestamp > '2018-04-01' group by device")} <br>
  8994. get &lt;name&gt; dbValue select device,count(*) from history where timestamp > '2018-04-01' group by device <br>
  8995. {CommandGet(undef,"Rep.LogDB1 dbValue select device,count(*) from history where timestamp > '2018-04-01' group by device")} <br>
  8996. </ul>
  8997. <br><br>
  8998. If you create a little routine in 99_myUtils, for example:
  8999. <br>
  9000. <pre>
  9001. sub dbval($$) {
  9002. my ($name,$cmd) = @_;
  9003. my $ret = CommandGet(undef,"$name dbValue $cmd");
  9004. return $ret;
  9005. }
  9006. </pre>
  9007. it can be accessed with e.g. those calls:
  9008. <br><br>
  9009. <ul>
  9010. <b>Examples:</b> <br>
  9011. {dbval("&lt;name&gt;","select count(*) from history")} <br>
  9012. $ret = dbval("&lt;name&gt;","select count(*) from history"); <br>
  9013. </ul>
  9014. </li>
  9015. <br><br>
  9016. <li><b> dbvars </b> - lists global informations about MySQL system variables. Included are e.g. readings related to InnoDB-Home, datafile path,
  9017. memory- or cache-parameter and so on. The Output reports initially all available informations. Using the
  9018. <a href="#DbRepattr">attribute</a> "showVariables" the quantity of results can be limited to show only the desired values.
  9019. Further detailed informations of items meaning are explained
  9020. <a href=http://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html>there</a>. <br>
  9021. <br><ul>
  9022. <b>Example</b> <br>
  9023. get &lt;name&gt; dbvars <br>
  9024. attr &lt;name&gt; showVariables %version%,%query_cache% <br>
  9025. # Only readings containing "version" and "query_cache" in name will be created
  9026. </li>
  9027. <br><br>
  9028. </ul>
  9029. <li><b> minTimestamp </b> - Identifies the oldest timestamp in the database (will be executed implicitely at FHEM start).
  9030. The timestamp is used as begin of data selection if no time attribut is set to determine the
  9031. start date.
  9032. </li>
  9033. <br><br>
  9034. <li><b> procinfo </b> - reports the existing database processes in a summary table (only MySQL). <br>
  9035. Typically only the own processes of the connection user (set in DbLog configuration file) will be
  9036. reported. If all precesses have to be reported, the global "PROCESS" right has to be granted to the
  9037. user. <br>
  9038. As of MariaDB 5.3 for particular SQL-Statements a progress reporting will be provided
  9039. (table row "PROGRESS"). So you can track, for instance, the degree of processing during an index
  9040. creation. <br>
  9041. Further informations can be found
  9042. <a href=https://mariadb.com/kb/en/mariadb/show-processlist/>there</a>. <br>
  9043. </li>
  9044. <br><br>
  9045. <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
  9046. on the database type. Using the <a href="#DbRepattr">attribute</a> "showSvrInfo" the quantity of results can be limited to show only
  9047. the desired values. Further detailed informations of items meaning are explained
  9048. <a href=https://msdn.microsoft.com/en-us/library/ms711681(v=vs.85).aspx>there</a>. <br>
  9049. <br><ul>
  9050. <b>Example</b> <br>
  9051. get &lt;name&gt; svrinfo <br>
  9052. attr &lt;name&gt; showSvrInfo %SQL_CATALOG_TERM%,%NAME% <br>
  9053. # Only readings containing "SQL_CATALOG_TERM" and "NAME" in name will be created
  9054. </li>
  9055. <br><br>
  9056. </ul>
  9057. <li><b> tableinfo </b> - access detailed informations about tables in MySQL database which is connected by the DbRep-device.
  9058. All available tables in the connected database will be selected by default.
  9059. Using the<a href="#DbRepattr">attribute</a> "showTableInfo" the results can be limited to tables you want to show.
  9060. 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>
  9061. <br><ul>
  9062. <b>Example</b> <br>
  9063. get &lt;name&gt; tableinfo <br>
  9064. attr &lt;name&gt; showTableInfo current,history <br>
  9065. # Only informations related to tables "current" and "history" are going to be created
  9066. </li>
  9067. <br><br>
  9068. </ul>
  9069. <br>
  9070. </ul></ul>
  9071. </ul>
  9072. <a name="DbRepattr"></a>
  9073. <b>Attributes</b>
  9074. <br>
  9075. <ul>
  9076. Using the module specific attributes you are able to define the scope of evaluation and the aggregation. <br><br>
  9077. <b>Note for SQL-Wildcard Usage:</b> <br>
  9078. Within the attribute values of "device" and "reading" you may use SQL-Wildcard "%", Character "_" is not supported as a wildcard.
  9079. The character "%" stands for any characters. <br>
  9080. This rule is valid to all functions <b>except</b> "insert", "importFromFile" and "deviceRename". <br>
  9081. The function "insert" doesn't allow setting the mentioned attributes containing the wildcard "%". <br>
  9082. In readings the wildcard character "%" will be replaced by "/" to meet the rules of allowed characters in readings.
  9083. <br><br>
  9084. <ul><ul>
  9085. <a name="aggregation"></a>
  9086. <li><b>aggregation </b> - Aggregation of Device/Reading-selections. Possible is hour, day, week, month or "no".
  9087. Delivers e.g. the count of database entries for a day (countEntries), Summation of
  9088. difference values of a reading (sumValue) and so on. Using aggregation "no" (default) an
  9089. aggregation don't happens but the output contaims all values of Device/Reading in the defined time period. </li> <br>
  9090. <a name="allowDeletion"></a>
  9091. <li><b>allowDeletion </b> - unlocks the delete-function </li> <br>
  9092. <a name="averageCalcForm"></a>
  9093. <li><b>averageCalcForm </b> - specifies the calculation variant of average peak by "averageValue". <br><br>
  9094. At the moment the following methods are implemented: <br><br>
  9095. <ul>
  9096. <table>
  9097. <colgroup> <col width=20%> <col width=80%> </colgroup>
  9098. <tr><td><b>avgArithmeticMean :</b> </td><td>the arithmetic average is calculated (default) </td></tr>
  9099. <tr><td><b>avgDailyMeanGWS :</b> </td><td>calculates the daily medium temperature according the
  9100. specifications of german weather service (pls. see helpful hints by get versionNotes). <br>
  9101. This variant uses aggregation "day" automatically. </td></tr>
  9102. <tr><td><b>avgTimeWeightMean :</b> </td><td>calculates a time weighted average mean value is calculated </td></tr>
  9103. </table>
  9104. </ul>
  9105. </li><br>
  9106. <a name="device"></a>
  9107. <li><b>device </b> - Selection of a particular device. <br>
  9108. You can specify device specifications (devspec). <br>
  9109. Inside of device specifications a SQL wildcard (%) will be evaluated as a normal ASCII-character.
  9110. The device names are derived from device specification and the active devices in FHEM before
  9111. SQL selection will be carried out. </li> <br>
  9112. <ul>
  9113. <b>Examples:</b> <br>
  9114. <code>attr &lt;name&gt; device TYPE=DbRep</code> <br>
  9115. # select datasets of active present devices with Type "DbRep" <br>
  9116. <code>attr &lt;name&gt; device MySTP_5000</code> <br>
  9117. # select datasets of device "MySTP_5000" <br>
  9118. <code>attr &lt;name&gt; device SMA.*</code> <br>
  9119. # select datasets of devices starting with "SMA" <br>
  9120. <code>attr &lt;name&gt; device SMA_Energymeter,MySTP_5000</code> <br>
  9121. # select datasets of devices "SMA_Energymeter" and "MySTP_5000" <br>
  9122. <code>attr &lt;name&gt; device %5000</code> <br>
  9123. # select datasets of devices ending with "5000" <br>
  9124. </ul>
  9125. <br>
  9126. <a name="DbRep_device"></a>
  9127. Please see also <a href="#devspec">device specifications (devspec)</a>.
  9128. <br><br>
  9129. <a name="diffAccept"></a>
  9130. <li><b>diffAccept </b> - valid for function diffValue. diffAccept determines the threshold, up to that a calaculated
  9131. difference between two straight sequently datasets should be commenly accepted
  9132. (default = 20). <br>
  9133. Hence faulty DB entries with a disproportional high difference value will be eliminated and
  9134. don't tamper the result.
  9135. If a threshold overrun happens, the reading "diff_overrun_limit_&lt;diffLimit&gt;" will be
  9136. generated (&lt;diffLimit&gt; will be substituted with the present prest attribute value). <br>
  9137. The reading contains a list of relevant pair of values. Using verbose=3 this list will also
  9138. be reported in the FHEM logfile.
  9139. </li><br>
  9140. <ul>
  9141. Example report in logfile if threshold of diffAccept=10 overruns: <br><br>
  9142. DbRep Rep.STP5000.etotal -> data ignored while calc diffValue due to threshold overrun (diffAccept = 10): <br>
  9143. 2016-04-09 08:50:50 0.0340 -> 2016-04-09 12:42:01 13.3440 <br><br>
  9144. # The first dataset with a value of 0.0340 is untypical low compared to the next value of 13.3440 and results a untypical
  9145. high difference value. <br>
  9146. # Now you have to decide if the (second) dataset should be deleted, ignored of the attribute diffAccept should be adjusted.
  9147. </ul><br>
  9148. <a name="disable"></a>
  9149. <li><b>disable </b> - deactivates the module </li> <br>
  9150. <a name="dumpComment"></a>
  9151. <li><b>dumpComment </b> - User-comment. It will be included in the header of the created dumpfile by
  9152. command "dumpMySQL clientSide". </li> <br>
  9153. <a name="dumpCompress"></a>
  9154. <li><b>dumpCompress </b> - if set, the dump files are compressed after operation of "dumpMySQL" bzw. "dumpSQLite" </li> <br>
  9155. <a name="dumpDirLocal"></a>
  9156. <li><b>dumpDirLocal </b> - Target directory of database dumps by command "dumpMySQL clientSide"
  9157. (default: "{global}{modpath}/log/" on the FHEM-Server). <br>
  9158. In this directory also the internal version administration searches for old backup-files
  9159. and deletes them if the number exceeds attribute "dumpFilesKeep".
  9160. The attribute is also relevant to publish a local mounted directory "dumpDirRemote" to
  9161. DbRep. </li> <br>
  9162. <a name="dumpDirRemote"></a>
  9163. <li><b>dumpDirRemote </b> - Target directory of database dumps by command "dumpMySQL serverSide"
  9164. (default: the Home-directory of MySQL-Server on the MySQL-Host). </li> <br>
  9165. <a name="dumpMemlimit"></a>
  9166. <li><b>dumpMemlimit </b> - tolerable memory consumption for the SQL-script during generation period (default: 100000 characters).
  9167. Please adjust this parameter if you may notice memory bottlenecks and performance problems based
  9168. on it on your specific hardware. </li> <br>
  9169. <a name="dumpSpeed"></a>
  9170. <li><b>dumpSpeed </b> - Number of Lines which will be selected in source database with one select by dump-command
  9171. "dumpMySQL ClientSide" (default: 10000).
  9172. This parameter impacts the run-time and consumption of resources directly. </li> <br>
  9173. <a name="dumpFilesKeep"></a>
  9174. <li><b>dumpFilesKeep </b> - The specified number of dumpfiles remain in the dump directory (default: 3).
  9175. If there more (older) files has been found, these files will be deleted after a new database dump
  9176. was created successfully.
  9177. The global attrubute "archivesort" will be considered. </li> <br>
  9178. <a name="executeAfterProc"></a>
  9179. <li><b>executeAfterProc </b> - you can specify a FHEM-command which should be executed <b>after dump</b>. <br>
  9180. Perl functions have to be enclosed in {} .<br><br>
  9181. <ul>
  9182. <b>Example:</b> <br><br>
  9183. attr &lt;name&gt; executeAfterProc set og_gz_westfenster off; <br>
  9184. attr &lt;name&gt; executeAfterProc {adump ("&lt;name&gt;")} <br><br>
  9185. # "adump" is a function defined in 99_myUtils.pm e.g.: <br>
  9186. <pre>
  9187. sub adump {
  9188. my ($name) = @_;
  9189. my $hash = $defs{$name};
  9190. # own function, e.g.
  9191. Log3($name, 3, "DbRep $name -> Dump finished");
  9192. return;
  9193. }
  9194. </pre>
  9195. </ul>
  9196. </li>
  9197. <a name="executeBeforeProc"></a>
  9198. <li><b>executeBeforeProc </b> - you can specify a FHEM-command which should be executed <b>before dump</b>. <br>
  9199. Perl functions have to be enclosed in {} .<br><br>
  9200. <ul>
  9201. <b>Example:</b> <br><br>
  9202. attr &lt;name&gt; executeBeforeProc set og_gz_westfenster on; <br>
  9203. attr &lt;name&gt; executeBeforeProc {bdump ("&lt;name&gt;")} <br><br>
  9204. # "bdump" is a function defined in 99_myUtils.pm e.g.: <br>
  9205. <pre>
  9206. sub bdump {
  9207. my ($name) = @_;
  9208. my $hash = $defs{$name};
  9209. # own function, e.g.
  9210. Log3($name, 3, "DbRep $name -> Dump starts now");
  9211. return;
  9212. }
  9213. </pre>
  9214. </ul>
  9215. </li>
  9216. <a name="expimpfile"></a>
  9217. <li><b>expimpfile </b> - Path/filename for data export/import. <br><br>
  9218. The filename may contain wildcards which are replaced by corresponding values
  9219. (see subsequent table).
  9220. Furthermore filename can contain %-wildcards of the POSIX strftime function of the underlying OS (see your
  9221. strftime manual). <br>
  9222. <ul>
  9223. <table>
  9224. <colgroup> <col width=5%> <col width=95%> </colgroup>
  9225. <tr><td> %L </td><td>: is replaced by the value of global logdir attribute </td></tr>
  9226. <tr><td> %TSB </td><td>: is replaced by the (calculated) value of the timestamp_begin attribute </td></tr>
  9227. <tr><td> </td><td> </td></tr>
  9228. <tr><td> </td><td> <b>Common used POSIX-wildcards are:</b> </td></tr>
  9229. <tr><td> %d </td><td>: day of month (01..31) </td></tr>
  9230. <tr><td> %m </td><td>: month (01..12) </td></tr>
  9231. <tr><td> %Y </td><td>: year (1970...) </td></tr>
  9232. <tr><td> %w </td><td>: day of week (0..6); 0 represents Sunday </td></tr>
  9233. <tr><td> %j </td><td>: day of year (001..366) </td></tr>
  9234. <tr><td> %U </td><td>: week number of year with Sunday as first day of week (00..53) </td></tr>
  9235. <tr><td> %W </td><td>: week number of year with Monday as first day of week (00..53) </td></tr>
  9236. </table>
  9237. </ul>
  9238. </li> <br>
  9239. <ul>
  9240. <b>Examples:</b> <br>
  9241. <code>attr &lt;name&gt; expimpfile /sds1/backup/exptest_%TSB.csv </code> <br>
  9242. <code>attr &lt;name&gt; expimpfile /sds1/backup/exptest_%Y-%m-%d.csv </code> <br>
  9243. </ul>
  9244. <br>
  9245. <a name="DbRep_expimpfile"></a>
  9246. About POSIX wildcard usage please see also explanations in
  9247. <a href="https://fhem.de/commandref.html#FileLog">Filelog</a>. <br>
  9248. <br><br>
  9249. <a name="fetchMarkDuplicates"></a>
  9250. <li><b>fetchMarkDuplicates </b>
  9251. - Highlighting of multiple occuring datasets in result of "fetchrows" command </li> <br>
  9252. <a name="fetchRoute"></a>
  9253. <li><b>fetchRoute [descent | ascent] </b> - specify the direction of data selection of the fetchrows-command. <br><br>
  9254. <ul>
  9255. <b>descent</b> - the data are read descent (default). If
  9256. amount of datasets specified by attribut "limit" is exceeded,
  9257. the newest x datasets are shown. <br><br>
  9258. <b>ascent</b> - the data are read ascent . If
  9259. amount of datasets specified by attribut "limit" is exceeded,
  9260. the oldest x datasets are shown. <br>
  9261. </ul>
  9262. </li> <br><br>
  9263. <a name="ftpUse"></a>
  9264. <li><b>ftpUse </b> - FTP Transfer after dump will be switched on (without SSL encoding). The created
  9265. database backup file will be transfered non-blocking to the FTP-Server (Attribut "ftpServer").
  9266. </li> <br>
  9267. <a name="ftpUseSSL"></a>
  9268. <li><b>ftpUseSSL </b> - FTP Transfer with SSL encoding after dump. The created database backup file will be transfered
  9269. non-blocking to the FTP-Server (Attribut "ftpServer"). </li> <br>
  9270. <a name="ftpUser"></a>
  9271. <li><b>ftpUser </b> - User for FTP-server login, default: "anonymous". </li> <br>
  9272. <a name="ftpDebug"></a>
  9273. <li><b>ftpDebug </b> - debugging of FTP communication for diagnostics. </li> <br>
  9274. <a name="ftpDir"></a>
  9275. <li><b>ftpDir </b> - directory on FTP-server in which the file will be send into (default: "/"). </li> <br>
  9276. <a name="ftpDumpFilesKeep"></a>
  9277. <li><b>ftpDumpFilesKeep </b> - leave the number of dump files in FTP-destination &lt;ftpDir&gt; (default: 3). Are there more
  9278. (older) dump files present, these files are deleted after a new dump was transfered successfully. </li> <br>
  9279. <a name="ftpPassive"></a>
  9280. <li><b>ftpPassive </b> - set if passive FTP is to be used </li> <br>
  9281. <a name="ftpPort"></a>
  9282. <li><b>ftpPort </b> - FTP-Port, default: 21 </li> <br>
  9283. <a name="ftpPwd"></a>
  9284. <li><b>ftpPwd </b> - password of FTP-User, is not set by default </li> <br>
  9285. <a name="ftpServer"></a>
  9286. <li><b>ftpServer </b> - name or IP-address of FTP-server. <b>absolutely essential !</b> </li> <br>
  9287. <a name="ftpTimeout"></a>
  9288. <li><b>ftpTimeout </b> - timeout of FTP-connection in seconds (default: 30). </li> <br>
  9289. <a name="limit"></a>
  9290. <li><b>limit </b> - limits the number of selected datasets by the "fetchrows", or the shown datasets of "delSeqDoublets adviceDelete",
  9291. "delSeqDoublets adviceRemain" commands (default: 1000).
  9292. This limitation should prevent the browser session from overload and
  9293. avoids FHEMWEB from blocking. Please change the attribut according your requirements or change the
  9294. selection criteria (decrease evaluation period). </li> <br>
  9295. <a name="optimizeTablesBeforeDump"></a>
  9296. <li><b>optimizeTablesBeforeDump </b> - if set to "1", the database tables will be optimized before executing the dump
  9297. (default: 0).
  9298. Thereby the backup run-time time will be extended. <br><br>
  9299. <ul>
  9300. <b>Note</b> <br>
  9301. The table optimizing cause locking the tables and therefore to blocking of
  9302. FHEM if DbLog isn't working in asynchronous mode (DbLog-attribute "asyncMode") !
  9303. <br>
  9304. </ul>
  9305. </li> <br>
  9306. <a name="reading"></a>
  9307. <li><b>reading </b> - Selection of a particular reading.
  9308. More than one reading are specified as a comma separated list. <br>
  9309. If SQL wildcard (%) is set in a list, it will be evaluated as a normal ASCII-character. <br>
  9310. </li> <br>
  9311. <ul>
  9312. <b>Examples:</b> <br>
  9313. <code>attr &lt;name&gt; reading etotal</code> <br>
  9314. <code>attr &lt;name&gt; reading et%</code> <br>
  9315. <code>attr &lt;name&gt; reading etotal,etoday</code> <br>
  9316. </ul>
  9317. <br><br>
  9318. <a name="readingNameMap"></a>
  9319. <li><b>readingNameMap </b> - the name of the analyzed reading can be overwritten for output </li> <br>
  9320. <a name="role"></a>
  9321. <li><b>role </b> - the role of the DbRep-device. Standard role is "Client". <br>
  9322. <a name="DbRep_role"></a>
  9323. The role "Agent" is described in section <a href="#DbRepAutoRename">DbRep-Agent</a>.
  9324. </li> <br>
  9325. <a name="readingPreventFromDel"></a>
  9326. <li><b>readingPreventFromDel </b> - comma separated list of readings which are should prevent from deletion when a
  9327. new operation starts </li> <br>
  9328. <a name="seqDoubletsVariance"></a>
  9329. <li><b>seqDoubletsVariance </b> - accepted variance (+/-) for the command "set &lt;name&gt; delSeqDoublets". <br>
  9330. The value of this attribute describes the variance up to it consecutive numeric values (VALUE) of
  9331. datasets are handled as identical and should be deleted. "seqDoubletsVariance" is an absolut numerical value,
  9332. which is used as a positive as well as a negative variance. </li> <br>
  9333. <ul>
  9334. <b>Examples:</b> <br>
  9335. <code>attr &lt;name&gt; seqDoubletsVariance 0.0014 </code> <br>
  9336. <code>attr &lt;name&gt; seqDoubletsVariance 1.45 </code> <br>
  9337. </ul>
  9338. <br><br>
  9339. <a name="showproctime"></a>
  9340. <li><b>showproctime </b> - if set, the reading "sql_processing_time" shows the required execution time (in seconds)
  9341. for the sql-requests. This is not calculated for a single sql-statement, but the summary
  9342. of all sql-statements necessara for within an executed DbRep-function in background. </li> <br>
  9343. <a name="showStatus"></a>
  9344. <li><b>showStatus </b> - limits the sample space of command "get &lt;name&gt; dbstatus". SQL-Wildcard (%) can be used. </li> <br>
  9345. <ul>
  9346. <b>Example: </b><br>
  9347. attr &lt;name&gt; showStatus %uptime%,%qcache% <br>
  9348. # Only readings with containing "uptime" and "qcache" in name will be shown <br>
  9349. </ul><br>
  9350. <a name="showVariables"></a>
  9351. <li><b>showVariables </b> - limits the sample space of command "get &lt;name&gt; dbvars". SQL-Wildcard (%) can be used. </li> <br>
  9352. <ul>
  9353. <b>Example: </b><br>
  9354. attr &lt;name&gt; showVariables %version%,%query_cache% <br>
  9355. # Only readings with containing "version" and "query_cache" in name will be shown <br>
  9356. </ul><br>
  9357. <a name="showSvrInfo"></a>
  9358. <li><b>showSvrInfo </b> - limits the sample space of command "get &lt;name&gt; svrinfo". SQL-Wildcard (%) can be used. </li> <br>
  9359. <ul>
  9360. <b>Example: </b><br>
  9361. attr &lt;name&gt; showSvrInfo %SQL_CATALOG_TERM%,%NAME% <br>
  9362. # Only readings with containing "SQL_CATALOG_TERM" and "NAME" in name will be shown <br>
  9363. </ul><br>
  9364. <a name="showTableInfo"></a>
  9365. <li><b>showTableInfo </b> - limits the tablename which is selected by command "get &lt;name&gt; tableinfo". SQL-Wildcard
  9366. (%) can be used. </li> <br>
  9367. <ul>
  9368. <b>Example: </b><br>
  9369. attr &lt;name&gt; showTableInfo current,history <br>
  9370. # Only informations about tables "current" and "history" will be shown <br>
  9371. </ul><br>
  9372. <a name="sqlCmdHistoryLength"></a>
  9373. <li><b>sqlCmdHistoryLength </b>
  9374. - activates the command history of "sqlCmd" and determines the length of it </li> <br>
  9375. <a name="sqlResultFieldSep"></a>
  9376. <li><b>sqlResultFieldSep </b> - determines the used field separator (default: "|") in the result of some sql-commands. </li> <br>
  9377. <a name="sqlResultFormat"></a>
  9378. <li><b>sqlResultFormat </b> - determines the formatting of the "set &lt;name&gt; sqlCmd" command result.
  9379. Possible options are: <br><br>
  9380. <ul>
  9381. <b>separated </b> - every line of the result will be generated sequentially in a single
  9382. reading. (default) <br><br>
  9383. <b>mline </b> - the result will be generated as multiline in
  9384. Reading SqlResult.
  9385. <br><br>
  9386. <b>sline </b> - the result will be generated as singleline in
  9387. Reading SqlResult.
  9388. Datasets are separated by "]|[". <br><br>
  9389. <b>table </b> - the result will be generated as an table in
  9390. Reading SqlResult. <br><br>
  9391. <b>json </b> - creates the Reading SqlResult as a JSON
  9392. coded hash.
  9393. Every hash-element consists of the serial number of the dataset (key)
  9394. and its value. </li> <br><br>
  9395. To process the result, you may use a userExitFn in 99_myUtils for example: <br>
  9396. <pre>
  9397. sub resfromjson {
  9398. my ($name,$reading,$value) = @_;
  9399. my $hash = $defs{$name};
  9400. if ($reading eq "SqlResult") {
  9401. # only reading SqlResult contains JSON encoded data
  9402. my $data = decode_json($value);
  9403. foreach my $k (keys(%$data)) {
  9404. # use your own processing from here for every hash-element
  9405. # e.g. output of every element that contains "Cam"
  9406. my $ke = $data->{$k};
  9407. if($ke =~ m/Cam/i) {
  9408. my ($res1,$res2) = split("\\|", $ke);
  9409. Log3($name, 1, "$name - extract element $k by userExitFn: ".$res1." ".$res2);
  9410. }
  9411. }
  9412. }
  9413. return;
  9414. }
  9415. </pre>
  9416. </ul>
  9417. <br>
  9418. <a name="timeYearPeriod"></a>
  9419. <li><b>timeYearPeriod </b> - By this attribute an annual time period will be determined for database data selection.
  9420. The time limits are calculated dynamically during execution time. Every time an annual period is determined.
  9421. Periods of less than a year are not possible to set. <br>
  9422. This attribute is particularly intended to make reports synchronous to an account period, e.g. of an energy- or gas provider.
  9423. </li> <br>
  9424. <ul>
  9425. <b>Example:</b> <br><br>
  9426. attr &lt;name&gt; timeYearPeriod 06-25 06-24 <br><br>
  9427. # evaluates the database within the time limits 25. june AAAA and 24. june BBBB. <br>
  9428. # The year AAAA respectively year BBBB is calculated dynamically depending of the current date. <br>
  9429. # If the current date >= 25. june and =< 31. december, than AAAA = current year and BBBB = current year+1 <br>
  9430. # If the current date >= 01. january und =< 24. june, than AAAA = current year-1 and BBBB = current year
  9431. </ul>
  9432. <br><br>
  9433. <a name="timestamp_begin"></a>
  9434. <li><b>timestamp_begin </b> - begin of data selection (*) </li> <br>
  9435. <a name="timestamp_end"></a>
  9436. <li><b>timestamp_end </b> - end of data selection. If not set the current date/time combination will be used. (*) </li> <br>
  9437. (*) The format of timestamp is as used with DbLog "YYYY-MM-DD HH:MM:SS". For the attributes "timestamp_begin", "timestamp_end"
  9438. you can also use one of the following entries. The timestamp-attribute will be dynamically set to: <br><br>
  9439. <ul>
  9440. <b>current_year_begin</b> : matches "&lt;current year&gt;-01-01 00:00:00" <br>
  9441. <b>current_year_end</b> : matches "&lt;current year&gt;-12-31 23:59:59" <br>
  9442. <b>previous_year_begin</b> : matches "&lt;previous year&gt;-01-01 00:00:00" <br>
  9443. <b>previous_year_end</b> : matches "&lt;previous year&gt;-12-31 23:59:59" <br>
  9444. <b>current_month_begin</b> : matches "&lt;current month first day&gt; 00:00:00" <br>
  9445. <b>current_month_end</b> : matches "&lt;current month last day&gt; 23:59:59" <br>
  9446. <b>previous_month_begin</b> : matches "&lt;previous month first day&gt; 00:00:00" <br>
  9447. <b>previous_month_end</b> : matches "&lt;previous month last day&gt; 23:59:59" <br>
  9448. <b>current_week_begin</b> : matches "&lt;first day of current week&gt; 00:00:00" <br>
  9449. <b>current_week_end</b> : matches "&lt;last day of current week&gt; 23:59:59" <br>
  9450. <b>previous_week_begin</b> : matches "&lt;first day of previous week&gt; 00:00:00" <br>
  9451. <b>previous_week_end</b> : matches "&lt;last day of previous week&gt; 23:59:59" <br>
  9452. <b>current_day_begin</b> : matches "&lt;current day&gt; 00:00:00" <br>
  9453. <b>current_day_end</b> : matches "&lt;current day&gt; 23:59:59" <br>
  9454. <b>previous_day_begin</b> : matches "&lt;previous day&gt; 00:00:00" <br>
  9455. <b>previous_day_end</b> : matches "&lt;previous day&gt; 23:59:59" <br>
  9456. <b>current_hour_begin</b> : matches "&lt;current hour&gt;:00:00" <br>
  9457. <b>current_hour_end</b> : matches "&lt;current hour&gt;:59:59" <br>
  9458. <b>previous_hour_begin</b> : matches "&lt;previous hour&gt;:00:00" <br>
  9459. <b>previous_hour_end</b> : matches "&lt;previous hour&gt;:59:59" <br> </ul><br>
  9460. Make sure that "timestamp_begin" < "timestamp_end" is fulfilled. <br><br>
  9461. <ul>
  9462. <b>Example:</b> <br><br>
  9463. attr &lt;name&gt; timestamp_begin current_year_begin <br>
  9464. attr &lt;name&gt; timestamp_end current_year_end <br><br>
  9465. # Analyzes the database between the time limits of the current year. <br>
  9466. </ul>
  9467. <br><br>
  9468. <b>Note </b> <br>
  9469. If the attribute "timeDiffToNow" will be set, the attributes "timestamp_begin" respectively "timestamp_end" will be deleted if they were set before.
  9470. The setting of "timestamp_begin" respectively "timestamp_end" causes the deletion of attribute "timeDiffToNow" if it was set before as well.
  9471. <br><br>
  9472. <a name="timeDiffToNow"></a>
  9473. <li><b>timeDiffToNow </b> - the <b>begin</b> of data selection will be set to the timestamp <b>"&lt;current time&gt; -
  9474. &lt;timeDiffToNow&gt;"</b> dynamically (e.g. if set to 86400, the last 24 hours are considered by data
  9475. selection). The time period will be calculated dynamically at execution time. </li> <br>
  9476. <ul>
  9477. <b>Examples for input format:</b> <br>
  9478. <code>attr &lt;name&gt; timeDiffToNow 86400</code> <br>
  9479. # the start time is set to "current time - 86400 seconds" <br>
  9480. <code>attr &lt;name&gt; timeDiffToNow d:2 h:3 m:2 s:10</code> <br>
  9481. # the start time is set to "current time - 2 days 3 hours 2 minutes 10 seconds" <br>
  9482. <code>attr &lt;name&gt; timeDiffToNow m:600</code> <br>
  9483. # the start time is set to "current time - 600 minutes" gesetzt <br>
  9484. <code>attr &lt;name&gt; timeDiffToNow h:2.5</code> <br>
  9485. # the start time is set to "current time - 2,5 hours" <br>
  9486. <code>attr &lt;name&gt; timeDiffToNow y:1 h:2.5</code> <br>
  9487. # the start time is set to "current time - 1 year and 2,5 hours" <br>
  9488. <code>attr &lt;name&gt; timeDiffToNow y:1.5</code> <br>
  9489. # the start time is set to "current time - 1.5 years" <br>
  9490. </ul>
  9491. <br><br>
  9492. <a name="timeOlderThan"></a>
  9493. <li><b>timeOlderThan </b> - the <b>end</b> of data selection will be set to the timestamp <b>"&lt;aktuelle Zeit&gt; -
  9494. &lt;timeOlderThan&gt;"</b> dynamically. Always the datasets up to timestamp
  9495. "&lt;current time&gt; - &lt;timeOlderThan&gt;" will be considered (e.g. if set to
  9496. 86400, all datasets older than one day are considered). The time period will be calculated dynamically at
  9497. execution time. <br>
  9498. The valid input format for attribute "timeOlderThan" is identical to attribute "timeDiffToNow". </li> <br>
  9499. <a name="timeout"></a>
  9500. <li><b>timeout </b> - set the timeout-value for Blocking-Call Routines in background in seconds (default 86400) </li> <br>
  9501. <a name="userExitFn"></a>
  9502. <li><b>userExitFn </b> - provides an interface to execute user specific program code. <br>
  9503. To activate the interfaace at first you should implement the subroutine which will be
  9504. called by the interface in your 99_myUtls.pm as shown in by the example: <br>
  9505. <pre>
  9506. sub UserFunction {
  9507. my ($name,$reading,$value) = @_;
  9508. my $hash = $defs{$name};
  9509. ...
  9510. # e.g. output transfered data
  9511. Log3 $name, 1, "UserExitFn $name called - transfer parameter are Reading: $reading, Value: $value " ;
  9512. ...
  9513. return;
  9514. }
  9515. </pre>
  9516. The interface activation takes place by setting the subroutine name into the attribute.
  9517. Optional you may set a Reading:Value combination (Regex) as argument. If no Regex is
  9518. specified, all value combinations will be evaluated as "true" (related to .*:.*).
  9519. <br><br>
  9520. <ul>
  9521. <b>Example:</b> <br>
  9522. attr <device> userExitFn UserFunction .*:.* <br>
  9523. # "UserFunction" is the name of subroutine in 99_myUtils.pm.
  9524. </ul>
  9525. <br>
  9526. The interface works generally without and independent from Events.
  9527. If the attribute is set, after every reading generation the Regex will be evaluated.
  9528. If the evaluation was "true", set subroutine will be called.
  9529. For further processing following parameters will be forwarded to the function: <br><br>
  9530. <ul>
  9531. <li>$name - the name of the DbRep-Device </li>
  9532. <li>$reading - the name of the created reading </li>
  9533. <li>$value - the value of the reading </li>
  9534. </ul>
  9535. </li>
  9536. <br><br>
  9537. <a name="valueFilter"></a>
  9538. <li><b>valueFilter </b> - Regular expression to filter datasets within particular functions. The regex is
  9539. applied to the whole selected dataset (inclusive Device, Reading and so on).
  9540. Please compare to explanations of relevant set-commands. </li> <br>
  9541. </ul>
  9542. </ul></ul>
  9543. <a name="DbRepReadings"></a>
  9544. <b>Readings</b>
  9545. <br>
  9546. <ul>
  9547. 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
  9548. that unsuitable or invalid readings would remain.<br><br>
  9549. In addition the following readings will be created: <br><br>
  9550. <ul><ul>
  9551. <li><b>state </b> - contains the current state of evaluation. If warnings are occured (state = Warning) compare Readings
  9552. "diff_overrun_limit_&lt;diffLimit&gt;" and "less_data_in_period" </li> <br>
  9553. <li><b>errortext </b> - description about the reason of an error state </li> <br>
  9554. <li><b>background_processing_time </b> - the processing time spent for operations in background/forked operation </li> <br>
  9555. <li><b>sql_processing_time </b> - the processing time wasted for all sql-statements used for an operation </li> <br>
  9556. <li><b>diff_overrun_limit_&lt;diffLimit&gt;</b> - contains a list of pairs of datasets which have overrun the threshold (&lt;diffLimit&gt;)
  9557. of calculated difference each other determined by attribute "diffAccept" (default=20). </li> <br>
  9558. <li><b>less_data_in_period </b> - contains a list of time periods within only one dataset was found. The difference calculation considers
  9559. the last value of the aggregation period before the current one. Valid for function "diffValue". </li> <br>
  9560. <li><b>SqlResult </b> - result of the last executed sqlCmd-command. The formatting can be specified
  9561. by <a href="#DbRepattr">attribute</a> "sqlResultFormat" </li> <br>
  9562. <li><b>sqlCmd </b> - contains the last executed sqlCmd-command </li> <br>
  9563. </ul></ul>
  9564. <br><br>
  9565. </ul>
  9566. <a name="DbRepAutoRename"></a>
  9567. <b>DbRep Agent - automatic change of device names in databases and DbRep-definitions after FHEM "rename" command</b>
  9568. <br>
  9569. <ul>
  9570. 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
  9571. react automatically on renaming devices in your FHEM installation. The DbRep device is now called DbRep-Agent. <br><br>
  9572. By the DbRep-Agent the following features are activated when a FHEM-device has being renamed: <br><br>
  9573. <ul><ul>
  9574. <li> in the database connected to the DbRep-Agent (Internal Database) dataset containing the old device name will be searched and renamed to the
  9575. to the new device name in <b>all</b> affected datasets. </li> <br>
  9576. <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
  9577. the renamed device will be going on in the database. </li> <br>
  9578. <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".
  9579. Because of that, reporting definitions will be kept consistent automatically if devices are renamed in FHEM. </li> <br>
  9580. </ul></ul>
  9581. 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
  9582. and checked: <br><br>
  9583. <ul><ul>
  9584. <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,
  9585. you could define same numbers of DbRep-Agents as well as DbLog-devices are defined. </li> <br>
  9586. <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
  9587. permitted. If a DbRep-device of privious type "Client" has changed an Agent, furthermore not permitted attributes will be deleted if set. </li> <br>
  9588. </ul></ul>
  9589. 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
  9590. function don't running into timeout set the timeout attribute to an appropriate value, especially if there are databases with huge datasets to evaluate.
  9591. As well as all the other database operations of this module, the autorename operation will be executed nonblocking. <br><br>
  9592. <ul>
  9593. <b>Example </b> of definition of a DbRep-device as an Agent: <br><br>
  9594. <code>
  9595. define Rep.Agent DbRep LogDB <br>
  9596. attr Rep.Agent devStateIcon connected:10px-kreis-gelb .*disconnect:10px-kreis-rot .*done:10px-kreis-gruen <br>
  9597. attr Rep.Agent icon security <br>
  9598. attr Rep.Agent role Agent <br>
  9599. attr Rep.Agent room DbLog <br>
  9600. attr Rep.Agent showproctime 1 <br>
  9601. 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>
  9602. attr Rep.Agent timeout 86400 <br>
  9603. </code>
  9604. <br>
  9605. </ul>
  9606. <b>Note:</b> <br>
  9607. Even though the function itself is designed non-blocking, make sure the assigned DbLog-device
  9608. is operating in asynchronous mode to avoid FHEMWEB from blocking. <br><br>
  9609. </ul>
  9610. =end html
  9611. =begin html_DE
  9612. <a name="DbRep"></a>
  9613. <h3>DbRep</h3>
  9614. <ul>
  9615. <br>
  9616. Zweck des Moduls ist es, den Inhalt von DbLog-Datenbanken nach bestimmten Kriterien zu durchsuchen, zu managen, das Ergebnis hinsichtlich verschiedener
  9617. Aggregationen auszuwerten und als Readings darzustellen. Die Abgrenzung der zu berücksichtigenden Datenbankinhalte erfolgt durch die Angabe von Device, Reading und
  9618. die Zeitgrenzen für Auswertungsbeginn bzw. Auswertungsende. <br><br>
  9619. Fast alle Datenbankoperationen werden nichtblockierend ausgeführt. Auf Ausnahmen wird hingewiesen.
  9620. Die Ausführungszeit der (SQL)-Hintergrundoperationen kann optional ebenfalls als Reading bereitgestellt
  9621. werden (siehe <a href="#DbRepattr">Attribute</a>). <br>
  9622. Alle vorhandenen Readings werden vor einer neuen Operation gelöscht. Durch das Attribut "readingPreventFromDel" kann eine Komma separierte Liste von Readings
  9623. angegeben werden die nicht gelöscht werden sollen. <br><br>
  9624. Aktuell werden folgende Operationen unterstützt: <br><br>
  9625. <ul><ul>
  9626. <li> Selektion aller Datensätze innerhalb einstellbarer Zeitgrenzen </li>
  9627. <li> Darstellung der Datensätze einer Device/Reading-Kombination innerhalb einstellbarer Zeitgrenzen. </li>
  9628. <li> Selektion der Datensätze unter Verwendung von dynamisch berechneter Zeitgrenzen zum Ausführungszeitpunkt. </li>
  9629. <li> Dubletten-Hervorhebung bei Datensatzanzeige (fetchrows) </li>
  9630. <li> Berechnung der Anzahl von Datensätzen einer Device/Reading-Kombination unter Berücksichtigung von Zeitgrenzen
  9631. und verschiedenen Aggregationen. </li>
  9632. <li> Die Berechnung von Summen-, Differenz-, Maximum-, Minimum- und Durchschnittswerten numerischer Readings
  9633. in Zeitgrenzen und verschiedenen Aggregationen. </li>
  9634. <li> Speichern von Summen-, Differenz- , Maximum- , Minimum- und Durchschnittswertberechnungen in der Datenbank </li>
  9635. <li> Löschung von Datensätzen. Die Eingrenzung der Löschung kann durch Device und/oder Reading sowie fixer oder
  9636. dynamisch berechneter Zeitgrenzen zum Ausführungszeitpunkt erfolgen. </li>
  9637. <li> Export von Datensätzen in ein File im CSV-Format </li>
  9638. <li> Import von Datensätzen aus File im CSV-Format </li>
  9639. <li> Umbenennen von Device/Readings in Datenbanksätzen </li>
  9640. <li> Ändern von Reading-Werten (VALUES) in der Datenbank (changeValue) </li>
  9641. <li> automatisches Umbenennen von Device-Namen in Datenbanksätzen und DbRep-Definitionen nach FHEM "rename"
  9642. Befehl (siehe <a href="#DbRepAutoRename">DbRep-Agent</a>) </li>
  9643. <li> Ausführen von beliebigen Benutzer spezifischen SQL-Kommandos (non-blocking) </li>
  9644. <li> Ausführen von beliebigen Benutzer spezifischen SQL-Kommandos (blocking) zur Verwendung in eigenem Code (dbValue) </li>
  9645. <li> Backups der FHEM-Datenbank im laufenden Betrieb erstellen (MySQL, SQLite) </li>
  9646. <li> senden des Dumpfiles zu einem FTP-Server nach dem Backup incl. Versionsverwaltung </li>
  9647. <li> Restore von SQLite- und MySQL-Dumps </li>
  9648. <li> Optimierung der angeschlossenen Datenbank (optimizeTables, vacuum) </li>
  9649. <li> Ausgabe der existierenden Datenbankprozesse (MySQL) </li>
  9650. <li> leeren der current-Tabelle </li>
  9651. <li> Auffüllen der current-Tabelle mit einem (einstellbaren) Extrakt der history-Tabelle</li>
  9652. <li> Bereinigung sequentiell aufeinander folgender Datensätze mit unterschiedlichen Zeitstempel aber gleichen Werten (sequentielle Dublettenbereinigung) </li>
  9653. <li> Reparatur einer korrupten SQLite Datenbank ("database disk image is malformed") </li>
  9654. <li> Übertragung von Datensätzen aus der Quelldatenbank in eine andere (Standby) Datenbank (syncStandby) </li>
  9655. </ul></ul>
  9656. <br>
  9657. Zur Aktivierung der Funktion <b>Autorename</b> wird dem definierten DbRep-Device mit dem Attribut "role" die Rolle "Agent" zugewiesen. Die Standardrolle nach Definition
  9658. ist "Client". Mehr ist dazu im Abschnitt <a href="#DbRepAutoRename">DbRep-Agent</a> beschrieben. <br><br>
  9659. DbRep stellt dem Nutzer einen <b>UserExit</b> zur Verfügung. Über diese Schnittstelle kann der Nutzer in Abhängigkeit von
  9660. frei definierbaren Reading/Value-Kombinationen (Regex) eigenen Code zur Ausführung bringen. Diese Schnittstelle arbeitet
  9661. unabhängig von einer Eventgenerierung. Weitere Informationen dazu ist unter <a href="#DbRepattr">Attribut</a>
  9662. "userExitFn" beschrieben. <br><br>
  9663. Sobald ein DbRep-Device definiert ist, wird die Funktion <b>DbReadingsVal</b> zur Verfügung gestellt.
  9664. Mit dieser Funktion läßt sich, ähnlich dem allgemeinen ReadingsVal, der Wert eines Readings aus der Datenbank abrufen.
  9665. Die Funktionsausführung erfolgt blockierend.
  9666. Die Befehlssyntax ist: <br><br>
  9667. <ul>
  9668. <code>DbReadingsVal("&lt;name&gt;","&lt;device:reading&gt;","&lt;timestamp&gt;","&lt;default&gt;") </code> <br><br>
  9669. <b>Beispiele: </b><br>
  9670. $ret = DbReadingsVal("Rep.LogDB1","MyWetter:temperature","2018-01-13 08:00:00",""); <br>
  9671. attr &lt;name&gt; userReadings oldtemp {DbReadingsVal("Rep.LogDB1","MyWetter:temperature","2018-04-13 08:00:00","")}
  9672. <br><br>
  9673. <table>
  9674. <colgroup> <col width=5%> <col width=95%> </colgroup>
  9675. <tr><td> <b>&lt;name&gt;</b> </td><td>: Name des abzufragenden DbRep-Device </td></tr>
  9676. <tr><td> <b>&lt;device:reading&gt;</b> </td><td>: Device:Reading dessen Wert geliefert werden soll </td></tr>
  9677. <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>
  9678. <tr><td> <b>&lt;default&gt;</b> </td><td>: Defaultwert falls kein Readingwert ermittelt werden konnte </td></tr>
  9679. </table>
  9680. </ul>
  9681. <br>
  9682. (*) Es wird der zeitlich zu &lt;timestamp&gt; passendste Readingwert zurück geliefert, falls kein Wert exakt zu dem
  9683. angegebenen Zeitpunkt geloggt wurde.
  9684. <br><br>
  9685. FHEM-Forum: <br>
  9686. <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>
  9687. FHEM-Wiki: <br>
  9688. <a href="https://wiki.fhem.de/wiki/DbRep_-_Reporting_und_Management_von_DbLog-Datenbankinhalten">DbRep - Reporting und Management von DbLog-Datenbankinhalten</a>. <br><br>
  9689. <br>
  9690. </ul>
  9691. <b>Voraussetzungen </b> <br><br>
  9692. <ul>
  9693. Das Modul setzt den Einsatz einer oder mehrerer DbLog-Instanzen voraus. Es werden die Zugangsdaten dieser
  9694. Datenbankdefinition genutzt. <br>
  9695. Es werden nur Inhalte der Tabelle "history" berücksichtigt wenn nichts anderes beschrieben ist. <br><br>
  9696. Überblick welche anderen Perl-Module DbRep verwendet: <br><br>
  9697. Net::FTP (nur wenn FTP-Transfer nach Datenbank-Dump genutzt wird) <br>
  9698. Net::FTPSSL (nur wenn FTP-Transfer mit Verschlüsselung nach Datenbank-Dump genutzt wird) <br>
  9699. POSIX <br>
  9700. Time::HiRes <br>
  9701. Time::Local <br>
  9702. Scalar::Util <br>
  9703. DBI <br>
  9704. Color (FHEM-Modul) <br>
  9705. IO::Compress::Gzip <br>
  9706. IO::Uncompress::Gunzip <br>
  9707. Blocking (FHEM-Modul) <br><br>
  9708. Aus Performancegründen sollten zusätzlich folgender Index erstellt werden: <br>
  9709. <code>
  9710. CREATE INDEX Report_Idx ON `history` (TIMESTAMP, READING) USING BTREE;
  9711. </code>
  9712. </ul>
  9713. <br>
  9714. <a name="DbRepdefine"></a>
  9715. <b>Definition</b>
  9716. <br>
  9717. <ul>
  9718. <code>
  9719. define &lt;name&gt; DbRep &lt;Name der DbLog-Instanz&gt;
  9720. </code>
  9721. <br><br>
  9722. (&lt;Name der DbLog-Instanz&gt; - es wird der Name der auszuwertenden DBLog-Datenbankdefinition angegeben <b>nicht</b> der Datenbankname selbst)
  9723. </ul>
  9724. <br><br>
  9725. <a name="DbRepset"></a>
  9726. <b>Set </b>
  9727. <ul>
  9728. Zur Zeit gibt es folgende Set-Kommandos. Über sie werden die Auswertungen angestoßen und definieren selbst die Auswertungsvariante.
  9729. Nach welchen Kriterien die Datenbankinhalte durchsucht werden und die Aggregation erfolgt, wird durch <a href="#DbRepattr">Attribute</a> gesteuert.
  9730. <br><br>
  9731. <ul><ul>
  9732. <li><b> averageValue [display | writeToDB]</b>
  9733. - berechnet einen Durchschnittswert des Datenbankfelds "VALUE" in den
  9734. gegebenen Zeitgrenzen ( siehe <a href="#DbRepattr">Attribute</a>).
  9735. Es muss das auszuwertende Reading über das <a href="#DbRepattr">Attribut</a> "reading"
  9736. angegeben sein. <br>
  9737. Mit dem Attribut "averageCalcForm" wird die Berechnungsvariante zur Mittelwertermittlung definiert.
  9738. Ist keine oder die Option "display" angegeben, werden die Ergebnisse nur angezeigt. Mit
  9739. der Option "writeToDB" werden die Berechnungsergebnisse mit einem neuen Readingnamen
  9740. in der Datenbank gespeichert. <br>
  9741. Der neue Readingname wird aus einem Präfix und dem originalen Readingnamen gebildet,
  9742. wobei der originale Readingname durch das Attribut "readingNameMap" ersetzt werden kann.
  9743. Der Präfix setzt sich aus der Bildungsfunktion und der Aggregation zusammen. <br>
  9744. Der Timestamp der neuen Readings in der Datenbank wird von der eingestellten Aggregationsperiode
  9745. abgeleitet, sofern kein eindeutiger Zeitpunkt des Ergebnisses bestimmt werden kann.
  9746. Das Feld "EVENT" wird mit "calculated" gefüllt.<br><br>
  9747. <ul>
  9748. <b>Beispiel neuer Readingname gebildet aus dem Originalreading "totalpac":</b> <br>
  9749. avgam_day_totalpac <br>
  9750. # &lt;Bildungsfunktion&gt;_&lt;Aggregation&gt;_&lt;Originalreading&gt; <br>
  9751. </li> <br>
  9752. </ul>
  9753. <li><b> cancelDump </b> - bricht einen laufenden Datenbankdump ab. </li> <br>
  9754. <li><b> changeValue </b> - ändert den gespeicherten Wert eines Readings.
  9755. Ist die Selektion auf bestimmte Device/Reading-Kombinationen durch die
  9756. <a href="#DbRepattr">Attribute</a> "device" bzw. "reading" beschränkt, werden sie genauso
  9757. berücksichtigt wie gesetzte Zeitgrenzen (Attribute time.*). <br>
  9758. Fehlen diese Beschränkungen, wird die gesamte Datenbank durchsucht und der angegebene Wert
  9759. geändert. <br><br>
  9760. <ul>
  9761. <b>Syntax: </b> <br>
  9762. set &lt;name&gt; changeValue "&lt;alter String&gt;","&lt;neuer String&gt;" <br><br>
  9763. Die Strings werden in Doppelstrich eingeschlossen und durch Komma getrennt.
  9764. Dabei kann "String" sein: <br>
  9765. <pre>
  9766. &lt;alter String&gt; : * ein einfacher String mit/ohne Leerzeichen, z.B. "OL 12"
  9767. * ein String mit Verwendung von SQL-Wildcard, z.B. "%OL%"
  9768. &lt;neuer String&gt; : * ein einfacher String mit/ohne Leerzeichen, z.B. "12 kWh"
  9769. * Perl Code eingeschlossen in "{}" inkl. Quotes, z.B. "{($VALUE,$UNIT) = split(" ",$VALUE)}".
  9770. Dem Perl-Ausdruck werden die Variablen $VALUE und $UNIT übergeben. Sie können innerhalb
  9771. des Perl-Code geändert werden. Der zurückgebene Wert von $VALUE und $UNIT wird in dem Feld
  9772. VALUE bzw. UNIT des Datensatzes gespeichert.
  9773. </pre>
  9774. <b>Beispiele: </b> <br>
  9775. set &lt;name&gt; changeValue "OL","12 OL" <br>
  9776. # der alte Feldwert "OL" wird in "12 OL" geändert. <br><br>
  9777. set &lt;name&gt; changeValue "%OL%","12 OL" <br>
  9778. # enthält das Feld VALUE den Teilstring "OL", wird es in "12 OL" geändert. <br><br>
  9779. set &lt;name&gt; changeValue "12 kWh","{($VALUE,$UNIT) = split(" ",$VALUE)}" <br>
  9780. # der alte Feldwert "12 kWh" wird in VALUE=12 und UNIT=kWh gesplittet und in den Datenbankfeldern gespeichert <br><br>
  9781. set &lt;name&gt; changeValue "24%","{$VALUE = (split(" ",$VALUE))[0]}" <br>
  9782. # beginnt der alte Feldwert mit "24", wird er gesplittet und VALUE=24 gespeichert (z.B. "24 kWh")
  9783. <br><br>
  9784. Zusammengefasst sind die zur Steuerung von changeValue relevanten Attribute: <br><br>
  9785. <ul>
  9786. <table>
  9787. <colgroup> <col width=5%> <col width=95%> </colgroup>
  9788. <tr><td> <b>device</b> </td><td>: Selektion nur von Datensätzen die &lt;device&gt; enthalten </td></tr>
  9789. <tr><td> <b>reading</b> </td><td>: Selektion nur von Datensätzen die &lt;reading&gt; enthalten </td></tr>
  9790. <tr><td> <b>time.*</b> </td><td>: eine Reihe von Attributen zur Zeitabgrenzung </td></tr>
  9791. <tr><td> <b>executeBeforeProc</b> </td><td>: ausführen FHEM Kommando (oder perl-Routine) vor Start changeValue </td></tr>
  9792. <tr><td> <b>executeAfterProc</b> </td><td>: ausführen FHEM Kommando (oder perl-Routine) nach Ende changeValue </td></tr>
  9793. </table>
  9794. </ul>
  9795. <br>
  9796. <br>
  9797. <b>Hinweis:</b> <br>
  9798. Obwohl die Funktion selbst non-blocking ausgelegt ist, sollte das zugeordnete DbLog-Device
  9799. im asynchronen Modus betrieben werden um ein Blockieren von FHEMWEB zu vermeiden (Tabellen-Lock). <br><br>
  9800. </li> <br>
  9801. </ul>
  9802. <li><b> countEntries [history | current] </b>
  9803. - liefert die Anzahl der Tabelleneinträge (default: history) in den gegebenen
  9804. Zeitgrenzen (siehe <a href="#DbRepattr">Attribute</a>).
  9805. Sind die Timestamps nicht gesetzt werden alle Einträge gezählt.
  9806. Beschränkungen durch die <a href="#DbRepattr">Attribute</a> Device bzw. Reading
  9807. gehen in die Selektion mit ein. </li> <br>
  9808. <li><b> delEntries </b> - löscht alle oder die durch die <a href="#DbRepattr">Attribute</a> device und/oder
  9809. reading definierten Datenbankeinträge. Die Eingrenzung über Timestamps erfolgt
  9810. folgendermaßen: <br><br>
  9811. <ul>
  9812. "timestamp_begin" gesetzt <b>-&gt;</b> gelöscht werden DB-Einträge <b>ab</b> diesem Zeitpunkt bis zum aktuellen Datum/Zeit <br>
  9813. "timestamp_end" gesetzt <b>-&gt;</b> gelöscht werden DB-Einträge <b>bis</b> bis zu diesem Zeitpunkt <br>
  9814. beide Timestamps gesetzt <b>-&gt;</b> gelöscht werden DB-Einträge <b>zwischen</b> diesen Zeitpunkten <br>
  9815. "timeOlderThan" gesetzt <b>-&gt;</b> gelöscht werden DB-Einträge <b>älter</b> als aktuelle Zeit minus "timeOlderThan" <br>
  9816. "timeDiffToNow" gesetzt <b>-&gt;</b> gelöscht werden DB-Einträge <b>ab</b> aktueller Zeit minus "timeDiffToNow" bis jetzt <br>
  9817. <br>
  9818. Aus Sicherheitsgründen muss das <a href="#DbRepattr">Attribut</a> "allowDeletion"
  9819. gesetzt sein um die Löschfunktion freizuschalten. <br><br>
  9820. Die zur Steuerung von delEntries relevanten Attribute: <br><br>
  9821. <ul>
  9822. <table>
  9823. <colgroup> <col width=5%> <col width=95%> </colgroup>
  9824. <tr><td> <b>allowDeletion</b> </td><td>: Freischaltung der Löschfunktion </td></tr>
  9825. <tr><td> <b>device</b> </td><td>: Selektion nur von Datensätzen die &lt;device&gt; enthalten </td></tr>
  9826. <tr><td> <b>reading</b> </td><td>: Selektion nur von Datensätzen die &lt;reading&gt; enthalten </td></tr>
  9827. <tr><td> <b>time.*</b> </td><td>: eine Reihe von Attributen zur Zeitabgrenzung </td></tr>
  9828. <tr><td> <b>executeBeforeProc</b> </td><td>: ausführen FHEM Kommando (oder perl-Routine) vor Start delEntries </td></tr>
  9829. <tr><td> <b>executeAfterProc</b> </td><td>: ausführen FHEM Kommando (oder perl-Routine) nach Ende delEntries </td></tr>
  9830. </table>
  9831. </ul>
  9832. <br>
  9833. <br>
  9834. </li>
  9835. <br>
  9836. </ul>
  9837. <li><b> delSeqDoublets [adviceRemain | adviceDelete | delete]</b> - zeigt bzw. löscht aufeinander folgende identische Datensätze.
  9838. Dazu wird Device,Reading und Value ausgewertet. Nicht gelöscht werden der erste und der letzte Datensatz
  9839. einer Aggregationsperiode (z.B. hour, day, week usw.) sowie die Datensätze vor oder nach einem Wertewechsel
  9840. (Datenbankfeld VALUE). <br>
  9841. Die <a href="#DbRepattr">Attribute</a> zur Aggregation,Zeit-,Device- und Reading-Abgrenzung werden dabei
  9842. berücksichtigt. Ist das Attribut "aggregation" nicht oder auf "no" gesetzt, wird als Standard die Aggregation
  9843. "day" verwendet. Für Datensätze mit numerischen Werten kann mit dem <a href="#DbRepattr">Attribut</a>
  9844. "seqDoubletsVariance" eine Abweichung eingestellt werden, bis zu der aufeinander folgende numerische Werte als
  9845. identisch angesehen und gelöscht werden sollen.
  9846. <br><br>
  9847. <ul>
  9848. <table>
  9849. <colgroup> <col width=5%> <col width=95%> </colgroup>
  9850. <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>
  9851. <tr><td> <b>adviceDelete</b> </td><td>: simuliert die zu löschenden Datensätze (es wird nichts gelöscht !) </td></tr>
  9852. <tr><td> <b>delete</b> </td><td>: löscht die sequentiellen Dubletten (siehe Beispiel) </td></tr>
  9853. </table>
  9854. </ul>
  9855. <br>
  9856. Aus Sicherheitsgründen muss das <a href="#DbRepattr">Attribut</a> "allowDeletion" für die "delete" Option
  9857. gesetzt sein. <br>
  9858. Die Anzahl der anzuzeigenden Datensätze der Kommandos "delSeqDoublets adviceDelete", "delSeqDoublets adviceRemain" ist
  9859. zunächst begrenzt (default 1000) und kann durch das <a href="#DbRepattr">Attribut</a> "limit" angepasst werden.
  9860. Die Einstellung von "limit" hat keinen Einfluss auf die "delSeqDoublets delete" Funktion, sondern beeinflusst <b>NUR</b> die
  9861. Anzeige der Daten. <br>
  9862. Vor und nach der Ausführung von "delSeqDoublets" kann ein FHEM-Kommando bzw. Perl-Routine ausgeführt werden.
  9863. (siehe <a href="#DbRepattr">Attribute</a> "executeBeforeProc", "executeAfterProc")
  9864. <br><br>
  9865. <ul>
  9866. <b>Beispiel</b> - die nach Verwendung der delete-Option in der DB verbleibenden Datensätze sind <b>fett</b>
  9867. gekennzeichnet:<br><br>
  9868. <ul>
  9869. <b>2017-11-25_00-00-05__eg.az.fridge_Pwr__power 0 </b> <br>
  9870. 2017-11-25_00-02-26__eg.az.fridge_Pwr__power 0 <br>
  9871. 2017-11-25_00-04-33__eg.az.fridge_Pwr__power 0 <br>
  9872. 2017-11-25_01-06-10__eg.az.fridge_Pwr__power 0 <br>
  9873. <b>2017-11-25_01-08-21__eg.az.fridge_Pwr__power 0 </b> <br>
  9874. <b>2017-11-25_01-08-59__eg.az.fridge_Pwr__power 60.32 </b> <br>
  9875. <b>2017-11-25_01-11-21__eg.az.fridge_Pwr__power 56.26 </b> <br>
  9876. <b>2017-11-25_01-27-54__eg.az.fridge_Pwr__power 6.19 </b> <br>
  9877. <b>2017-11-25_01-28-51__eg.az.fridge_Pwr__power 0 </b> <br>
  9878. 2017-11-25_01-31-00__eg.az.fridge_Pwr__power 0 <br>
  9879. 2017-11-25_01-33-59__eg.az.fridge_Pwr__power 0 <br>
  9880. <b>2017-11-25_02-39-29__eg.az.fridge_Pwr__power 0 </b> <br>
  9881. <b>2017-11-25_02-41-18__eg.az.fridge_Pwr__power 105.28</b> <br>
  9882. <b>2017-11-25_02-41-26__eg.az.fridge_Pwr__power 61.52 </b> <br>
  9883. <b>2017-11-25_03-00-06__eg.az.fridge_Pwr__power 47.46 </b> <br>
  9884. <b>2017-11-25_03-00-33__eg.az.fridge_Pwr__power 0 </b> <br>
  9885. 2017-11-25_03-02-07__eg.az.fridge_Pwr__power 0 <br>
  9886. 2017-11-25_23-37-42__eg.az.fridge_Pwr__power 0 <br>
  9887. <b>2017-11-25_23-40-10__eg.az.fridge_Pwr__power 0 </b> <br>
  9888. <b>2017-11-25_23-42-24__eg.az.fridge_Pwr__power 1 </b> <br>
  9889. 2017-11-25_23-42-24__eg.az.fridge_Pwr__power 1 <br>
  9890. <b>2017-11-25_23-45-27__eg.az.fridge_Pwr__power 1 </b> <br>
  9891. <b>2017-11-25_23-47-07__eg.az.fridge_Pwr__power 0 </b> <br>
  9892. 2017-11-25_23-55-27__eg.az.fridge_Pwr__power 0 <br>
  9893. <b>2017-11-25_23-48-15__eg.az.fridge_Pwr__power 0 </b> <br>
  9894. <b>2017-11-25_23-50-21__eg.az.fridge_Pwr__power 59.1 </b> <br>
  9895. <b>2017-11-25_23-55-14__eg.az.fridge_Pwr__power 52.31 </b> <br>
  9896. <b>2017-11-25_23-58-09__eg.az.fridge_Pwr__power 51.73 </b> <br>
  9897. </ul>
  9898. </ul>
  9899. </li>
  9900. <br>
  9901. <br>
  9902. <li><b> deviceRename </b> - benennt den Namen eines Device innerhalb der angeschlossenen Datenbank (Internal
  9903. DATABASE) um.
  9904. Der Gerätename wird immer in der <b>gesamten</b> Datenbank umgesetzt. Eventuell gesetzte
  9905. Zeitgrenzen oder Beschränkungen durch die <a href="#DbRepattr">Attribute</a> Device bzw.
  9906. Reading werden nicht berücksichtigt. <br><br>
  9907. <ul>
  9908. <b>Beispiel: </b><br>
  9909. set &lt;name&gt; deviceRename ST_5000,ST5100 <br>
  9910. # Die Anzahl der umbenannten Device-Datensätze wird im Reading "device_renamed" ausgegeben. <br>
  9911. # Wird der umzubenennende Gerätename in der Datenbank nicht gefunden, wird eine WARNUNG im Reading "device_not_renamed" ausgegeben. <br>
  9912. # Entsprechende Einträge erfolgen auch im Logfile mit verbose=3
  9913. <br><br>
  9914. <b>Hinweis:</b> <br>
  9915. Obwohl die Funktion selbst non-blocking ausgelegt ist, sollte das zugeordnete DbLog-Device
  9916. im asynchronen Modus betrieben werden um ein Blockieren von FHEMWEB zu vermeiden (Tabellen-Lock). <br><br>
  9917. </li> <br>
  9918. </ul>
  9919. <li><b> diffValue [display | writeToDB] </b>
  9920. - berechnet den Differenzwert des Datenbankfelds "VALUE" in den Zeitgrenzen (Attribute) "timestamp_begin", "timestamp_end" bzw "timeDiffToNow / timeOlderThan".
  9921. Es muss das auszuwertende Reading im Attribut "reading" angegeben sein.
  9922. Diese Funktion ist z.B. zur Auswertung von Eventloggings sinnvoll, deren Werte sich fortlaufend erhöhen und keine Wertdifferenzen wegschreiben. <br>
  9923. 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
  9924. Zeitgrenzen/Aggregation gebildet, wobei ein Übertragswert der Vorperiode (Aggregation) zur darauf folgenden Aggregationsperiode
  9925. berücksichtigt wird sofern diese einen Value-Wert enhtält. <br>
  9926. Dabei wird ein Zählerüberlauf (Neubeginn bei 0) mit berücksichtigt (vergleiche <a href="#DbRepattr">Attribut</a> "diffAccept"). <br>
  9927. Wird in einer auszuwertenden Zeit- bzw. Aggregationsperiode nur ein Datensatz gefunden, kann die Differenz in Verbindung mit dem
  9928. Differenzübertrag der Vorperiode berechnet werden. in diesem Fall kann es zu einer logischen Ungenauigkeit in der Zuordnung der Differenz
  9929. zu der Aggregationsperiode kommen. Deswegen wird eine Warnung im "state" und das
  9930. Reading "less_data_in_period" mit einer Liste der betroffenen Perioden wird erzeugt. <br><br>
  9931. <ul>
  9932. <b>Hinweis: </b><br>
  9933. Im Auswertungs- bzw. Aggregationszeitraum (Tag, Woche, Monat, etc.) sollten dem Modul pro Periode mindestens ein Datensatz
  9934. zu Beginn und ein Datensatz gegen Ende des Aggregationszeitraumes zur Verfügung stehen um eine möglichst genaue Auswertung
  9935. der Differenzwerte vornehmen zu können.
  9936. <br>
  9937. <br>
  9938. </ul>
  9939. Ist keine oder die Option "display" angegeben, werden die Ergebnisse nur angezeigt. Mit
  9940. der Option "writeToDB" werden die Berechnungsergebnisse mit einem neuen Readingnamen
  9941. in der Datenbank gespeichert. <br>
  9942. Der neue Readingname wird aus einem Präfix und dem originalen Readingnamen gebildet,
  9943. wobei der originale Readingname durch das Attribut "readingNameMap" ersetzt werden kann.
  9944. Der Präfix setzt sich aus der Bildungsfunktion und der Aggregation zusammen. <br>
  9945. Der Timestamp der neuen Readings in der Datenbank wird von der eingestellten Aggregationsperiode
  9946. abgeleitet, sofern kein eindeutiger Zeitpunkt des Ergebnisses bestimmt werden kann.
  9947. Das Feld "EVENT" wird mit "calculated" gefüllt.<br><br>
  9948. <ul>
  9949. <b>Beispiel neuer Readingname gebildet aus dem Originalreading "totalpac":</b> <br>
  9950. diff_day_totalpac <br>
  9951. # &lt;Bildungsfunktion&gt;_&lt;Aggregation&gt;_&lt;Originalreading&gt; <br>
  9952. </li> <br>
  9953. </ul>
  9954. <li><b> dumpMySQL [clientSide | serverSide]</b>
  9955. - erstellt einen Dump der angeschlossenen MySQL-Datenbank. <br>
  9956. Abhängig von der ausgewählten Option wird der Dump auf der Client- bzw. Serverseite erstellt. <br>
  9957. Die Varianten unterscheiden sich hinsichtlich des ausführenden Systems, des Erstellungsortes, der
  9958. Attributverwendung, des erzielten Ergebnisses und der benötigten Hardwareressourcen. <br>
  9959. Die Option "clientSide" benötigt z.B. eine leistungsfähigere Hardware des FHEM-Servers, sichert aber alle
  9960. Tabellen inklusive eventuell angelegter Views. <br>
  9961. Mit dem Attribut "dumpCompress" kann eine Komprimierung der erstellten Dumpfiles eingeschaltet werden.
  9962. <br><br>
  9963. <ul>
  9964. <b><u>Option clientSide</u></b> <br>
  9965. Der Dump wird durch den Client (FHEM-Rechner) erstellt und per default im log-Verzeichnis des Clients
  9966. gespeichert.
  9967. Das Zielverzeichnis kann mit dem <a href="#DbRepattr">Attribut</a> "dumpDirLocal" verändert werden und muß auf
  9968. dem Client durch FHEM beschreibbar sein. <br>
  9969. Vor dem Dump kann eine Tabellenoptimierung (Attribut "optimizeTablesBeforeDump") oder ein FHEM-Kommando
  9970. (Attribut "executeBeforeProc") optional zugeschaltet werden.
  9971. Nach dem Dump kann ebenfalls ein FHEM-Kommando (siehe Attribut "executeAfterProc") ausgeführt werden. <br><br>
  9972. <b>Achtung ! <br>
  9973. Um ein Blockieren von FHEM zu vermeiden, muß DbLog im asynchronen Modus betrieben werden wenn die
  9974. Tabellenoptimierung verwendet wird ! </b> <br><br>
  9975. Über die <a href="#DbRepattr">Attribute</a> "dumpMemlimit" und "dumpSpeed" kann das Laufzeitverhalten der
  9976. Funktion beeinflusst werden um eine Optimierung bezüglich Performance und Ressourcenbedarf zu erreichen. <br><br>
  9977. Die für "dumpMySQL clientSide" relevanten Attribute sind: <br><br>
  9978. <ul>
  9979. <table>
  9980. <colgroup> <col width=5%> <col width=95%> </colgroup>
  9981. <tr><td> dumpComment </td><td>: User-Kommentar im Dumpfile </td></tr>
  9982. <tr><td> dumpCompress </td><td>: Komprimierung des Dumpfiles nach der Erstellung </td></tr>
  9983. <tr><td> dumpDirLocal </td><td>: das lokale Zielverzeichnis für die Erstellung des Dump </td></tr>
  9984. <tr><td> dumpMemlimit </td><td>: Begrenzung der Speicherverwendung </td></tr>
  9985. <tr><td> dumpSpeed </td><td>: Begrenzung die CPU-Belastung </td></tr>
  9986. <tr><td> dumpFilesKeep </td><td>: Anzahl der aufzubwahrenden Dumpfiles </td></tr>
  9987. <tr><td> executeBeforeProc </td><td>: ausführen FHEM Kommando (oder perl-Routine) vor dem Dump </td></tr>
  9988. <tr><td> executeAfterProc </td><td>: ausführen FHEM Kommando (oder perl-Routine) nach dem Dump </td></tr>
  9989. <tr><td> optimizeTablesBeforeDump </td><td>: Tabelloptimierung vor dem Dump ausführen </td></tr>
  9990. </table>
  9991. </ul>
  9992. <br>
  9993. Nach einem erfolgreichen Dump werden alte Dumpfiles gelöscht und nur die Anzahl Files, definiert durch
  9994. das Attribut "dumpFilesKeep" (default: 3), verbleibt im Zielverzeichnis "dumpDirLocal". Falls "dumpFilesKeep = 0"
  9995. gesetzt ist, werden alle Dumpfiles (auch das aktuell erstellte File), gelöscht.
  9996. Diese Einstellung kann sinnvoll sein, wenn FTP aktiviert ist
  9997. und die erzeugten Dumps nur im FTP-Zielverzeichnis erhalten bleiben sollen. <br><br>
  9998. Die <b>Namenskonvention der Dumpfiles</b> ist: &lt;dbname&gt;_&lt;date&gt;_&lt;time&gt;.sql[.gzip] <br><br>
  9999. Um die Datenbank aus dem Dumpfile wiederherzustellen kann das Kommmando: <br><br>
  10000. <ul>
  10001. set &lt;name&gt; restoreMySQL &lt;filename&gt; <br><br>
  10002. </ul>
  10003. verwendet werden. <br><br>
  10004. Das erzeugte Dumpfile (unkomprimiert) kann ebenfalls mit: <br><br>
  10005. <ul>
  10006. mysql -u &lt;user&gt; -p &lt;dbname&gt; < &lt;filename&gt;.sql <br><br>
  10007. </ul>
  10008. auf dem MySQL-Server ausgeführt werden um die Datenbank aus dem Dump wiederherzustellen. <br><br>
  10009. <br>
  10010. <b><u>Option serverSide</u></b> <br>
  10011. Der Dump wird durch den MySQL-Server erstellt und per default im Home-Verzeichnis des MySQL-Servers
  10012. gespeichert. <br>
  10013. Es wird die gesamte history-Tabelle (nicht current-Tabelle) <b>im CSV-Format</b> ohne
  10014. Einschränkungen exportiert. <br>
  10015. Vor dem Dump kann eine Tabellenoptimierung (Attribut "optimizeTablesBeforeDump")
  10016. optional zugeschaltet werden . <br><br>
  10017. <b>Achtung ! <br>
  10018. Um ein Blockieren von FHEM zu vermeiden, muß DbLog im asynchronen Modus betrieben werden wenn die
  10019. Tabellenoptimierung verwendet wird ! </b> <br><br>
  10020. Vor und nach dem Dump kann ein FHEM-Kommando (siehe Attribute "executeBeforeProc", "executeAfterProc") ausgeführt
  10021. werden. <br><br>
  10022. Die für "dumpMySQL serverSide" relevanten Attribute sind: <br><br>
  10023. <ul>
  10024. <table>
  10025. <colgroup> <col width=5%> <col width=95%> </colgroup>
  10026. <tr><td> dumpDirRemote </td><td>: das Erstellungsverzeichnis des Dumpfile auf dem entfernten Server </td></tr>
  10027. <tr><td> dumpCompress </td><td>: Komprimierung des Dumpfiles nach der Erstellung </td></tr>
  10028. <tr><td> dumpDirLocal </td><td>: Directory des lokal gemounteten dumpDirRemote-Verzeichnisses </td></tr>
  10029. <tr><td> dumpFilesKeep </td><td>: Anzahl der aufzubwahrenden Dumpfiles </td></tr>
  10030. <tr><td> executeBeforeProc </td><td>: ausführen FHEM Kommando (oder perl-Routine) vor dem Dump </td></tr>
  10031. <tr><td> executeAfterProc </td><td>: ausführen FHEM Kommando (oder perl-Routine) nach dem Dump </td></tr>
  10032. <tr><td> optimizeTablesBeforeDump </td><td>: Tabelloptimierung vor dem Dump ausführen </td></tr>
  10033. </table>
  10034. </ul>
  10035. <br>
  10036. Das Zielverzeichnis kann mit dem <a href="#DbRepattr">Attribut</a> "dumpDirRemote" verändert werden.
  10037. Es muß sich auf dem MySQL-Host gefinden und durch den MySQL-Serverprozess beschreibbar sein. <br>
  10038. Der verwendete Datenbankuser benötigt das "FILE"-Privileg. <br><br>
  10039. <b>Hinweis:</b> <br>
  10040. Soll die interne Versionsverwaltung und die Dumpfilekompression des Moduls genutzt, sowie die Größe des erzeugten
  10041. Dumpfiles ausgegeben werden, ist das Verzeichnis "dumpDirRemote" des MySQL-Servers auf dem Client zu mounten
  10042. und im <a href="#DbRepattr">Attribut</a> "dumpDirLocal" dem DbRep-Device bekannt zu machen. <br>
  10043. Gleiches gilt wenn der FTP-Transfer nach dem Dump genutzt werden soll (Attribut "ftpUse" bzw. "ftpUseSSL").
  10044. <br><br>
  10045. <ul>
  10046. <b>Beispiel: </b> <br>
  10047. attr &lt;name&gt; dumpDirRemote /volume1/ApplicationBackup/dumps_FHEM/ <br>
  10048. attr &lt;name&gt; dumpDirLocal /sds1/backup/dumps_FHEM/ <br>
  10049. attr &lt;name&gt; dumpFilesKeep 2 <br><br>
  10050. # Der Dump wird remote auf dem MySQL-Server im Verzeichnis '/volume1/ApplicationBackup/dumps_FHEM/'
  10051. erstellt. <br>
  10052. # Die interne Versionsverwaltung sucht im lokal gemounteten Verzeichnis '/sds1/backup/dumps_FHEM/'
  10053. vorhandene Dumpfiles und löscht diese bis auf die zwei letzten Versionen. <br>
  10054. <br>
  10055. </ul>
  10056. Wird die interne Versionsverwaltung genutzt, werden nach einem erfolgreichen Dump alte Dumpfiles gelöscht
  10057. und nur die Anzahl "dumpFilesKeep" (default: 3) verbleibt im Zielverzeichnis "dumpDirRemote".
  10058. FHEM benötigt in diesem Fall Schreibrechte auf dem Verzeichnis "dumpDirLocal". <br><br>
  10059. Die <b>Namenskonvention der Dumpfiles</b> ist: &lt;dbname&gt;_&lt;date&gt;_&lt;time&gt;.csv[.gzip] <br><br>
  10060. Ein Restore der Datenbank aus diesem Backup kann durch den Befehl: <br><br>
  10061. <ul>
  10062. set &lt;name&gt; &lt;restoreMySQL&gt; &lt;filename&gt;.csv[.gzip] <br><br>
  10063. </ul>
  10064. gestartet werden. <br><br>
  10065. <b><u>FTP Transfer nach Dump</u></b> <br>
  10066. Wenn diese Möglichkeit genutzt werden soll, ist das <a href="#DbRepattr">Attribut</a> "ftpUse" oder
  10067. "ftpUseSSL" zu setzen. Letzteres gilt wenn eine verschlüsselte Übertragung genutzt werden soll. <br>
  10068. Das Modul übernimmt ebenfalls die Versionierung der Dumpfiles im FTP-Zielverzeichnis mit Hilfe des Attributes
  10069. "ftpDumpFilesKeep".
  10070. Für die FTP-Übertragung relevante <a href="#DbRepattr">Attribute</a> sind: <br><br>
  10071. <ul>
  10072. <table>
  10073. <colgroup> <col width=5%> <col width=95%> </colgroup>
  10074. <tr><td> ftpUse </td><td>: FTP Transfer nach dem Dump wird eingeschaltet (ohne SSL Verschlüsselung) </td></tr>
  10075. <tr><td> ftpUser </td><td>: User zur Anmeldung am FTP-Server, default: anonymous </td></tr>
  10076. <tr><td> ftpUseSSL </td><td>: FTP Transfer mit SSL Verschlüsselung nach dem Dump wird eingeschaltet </td></tr>
  10077. <tr><td> ftpDebug </td><td>: Debugging des FTP Verkehrs zur Fehlersuche </td></tr>
  10078. <tr><td> ftpDir </td><td>: Verzeichnis auf dem FTP-Server in welches das File übertragen werden soll (default: "/") </td></tr>
  10079. <tr><td> ftpDumpFilesKeep </td><td>: Es wird die angegebene Anzahl Dumpfiles im &lt;ftpDir&gt; belassen (default: 3) </td></tr>
  10080. <tr><td> ftpPassive </td><td>: setzen wenn passives FTP verwendet werden soll </td></tr>
  10081. <tr><td> ftpPort </td><td>: FTP-Port, default: 21 </td></tr>
  10082. <tr><td> ftpPwd </td><td>: Passwort des FTP-Users, default nicht gesetzt </td></tr>
  10083. <tr><td> ftpServer </td><td>: Name oder IP-Adresse des FTP-Servers. <b>notwendig !</b> </td></tr>
  10084. <tr><td> ftpTimeout </td><td>: Timeout für die FTP-Verbindung in Sekunden (default: 30). </td></tr>
  10085. </table>
  10086. </ul>
  10087. <br>
  10088. <br>
  10089. </ul>
  10090. </li><br>
  10091. <li><b> dumpSQLite </b> - erstellt einen Dump der angeschlossenen SQLite-Datenbank. <br>
  10092. Diese Funktion nutzt die SQLite Online Backup API und ermöglicht es konsistente Backups der SQLite-DB
  10093. in laufenden Betrieb zu erstellen.
  10094. Der Dump wird per default im log-Verzeichnis des FHEM-Rechners gespeichert.
  10095. Das Zielverzeichnis kann mit dem <a href="#DbRepattr">Attribut</a> "dumpDirLocal" verändert werden und muß
  10096. durch FHEM beschreibbar sein.
  10097. Vor dem Dump kann optional eine Tabellenoptimierung (Attribut "optimizeTablesBeforeDump")
  10098. zugeschaltet werden.
  10099. <br><br>
  10100. <b>Achtung ! <br>
  10101. Um ein Blockieren von FHEM zu vermeiden, muß DbLog im asynchronen Modus betrieben werden wenn die
  10102. Tabellenoptimierung verwendet wird ! </b> <br><br>
  10103. Vor und nach dem Dump kann ein FHEM-Kommando (siehe Attribute "executeBeforeProc", "executeAfterProc")
  10104. ausgeführt werden. <br><br>
  10105. Die für diese Funktion relevanten Attribute sind: <br><br>
  10106. <ul>
  10107. <table>
  10108. <colgroup> <col width=5%> <col width=95%> </colgroup>
  10109. <tr><td> dumpCompress </td><td>: Komprimierung des Dumpfiles nach der Erstellung </td></tr>
  10110. <tr><td> dumpDirLocal </td><td>: Directory des lokal gemounteten dumpDirRemote-Verzeichnisses </td></tr>
  10111. <tr><td> dumpFilesKeep </td><td>: Anzahl der aufzubwahrenden Dumpfiles </td></tr>
  10112. <tr><td> executeBeforeProc </td><td>: ausführen FHEM Kommando (oder perl-Routine) vor dem Dump </td></tr>
  10113. <tr><td> executeAfterProc </td><td>: ausführen FHEM Kommando (oder perl-Routine) nach dem Dump </td></tr>
  10114. <tr><td> optimizeTablesBeforeDump </td><td>: Tabelloptimierung vor dem Dump ausführen </td></tr>
  10115. </table>
  10116. </ul>
  10117. <br>
  10118. Nach einem erfolgreichen Dump werden alte Dumpfiles gelöscht und nur die Anzahl Files, definiert durch das
  10119. Attribut "dumpFilesKeep" (default: 3), verbleibt im Zielverzeichnis "dumpDirLocal". Falls "dumpFilesKeep = 0" gesetzt, werden
  10120. alle Dumpfiles (auch das aktuell erstellte File), gelöscht. Diese Einstellung kann sinnvoll sein, wenn FTP aktiviert ist
  10121. und die erzeugten Dumps nur im FTP-Zielverzeichnis erhalten bleiben sollen. <br><br>
  10122. Die <b>Namenskonvention der Dumpfiles</b> ist: &lt;dbname&gt;_&lt;date&gt;_&lt;time&gt;.sqlitebkp[.gzip] <br><br>
  10123. Die Datenbank kann mit "set &lt;name&gt; restoreSQLite &lt;Filename&gt;" wiederhergestellt
  10124. werden. <br>
  10125. Das erstellte Dumpfile kann auf einen FTP-Server übertragen werden. Siehe dazu die Erläuterungen
  10126. unter "dumpMySQL". <br><br>
  10127. </li><br>
  10128. <li><b> eraseReadings </b> - Löscht alle angelegten Readings im Device, außer dem Reading "state" und Readings, die in der
  10129. Ausnahmeliste definiert mit Attribut "readingPreventFromDel" enthalten sind.
  10130. </li><br>
  10131. <li><b> exportToFile [&lt;File&gt;] </b>
  10132. - exportiert DB-Einträge im CSV-Format in den gegebenen Zeitgrenzen. <br>
  10133. Einschränkungen durch die <a href="#DbRepattr">Attribute</a> "device" bzw. "reading" gehen in die Selektion mit ein.
  10134. Der Dateiname wird durch das <a href="#DbRepattr">Attribut</a> "expimpfile" bestimmt. <br>
  10135. Alternativ kann die Datei (/Pfad/Datei) als Kommando-Option angegeben werden und übersteuert ein
  10136. eventuell gesetztes Attribut "expimpfile". Der Dateiname kann Wildcards enthalten (siehe Attribut "expimpfile").
  10137. <br>
  10138. Durch das Attribut "aggregation" wird der Export der Datensätze in Zeitscheiben der angegebenen Aggregation
  10139. vorgenommen. Ist z.B. "aggregation = month" gesetzt, werden die Daten in monatlichen Paketen selektiert und in
  10140. das Exportfile geschrieben. Dadurch wird die Hauptspeicherverwendung optimiert wenn sehr große Datenmengen
  10141. exportiert werden sollen und vermeidet den "died prematurely" Abbruchfehler. <br><br>
  10142. Die für diese Funktion relevanten Attribute sind: <br><br>
  10143. <ul>
  10144. <table>
  10145. <colgroup> <col width=5%> <col width=95%> </colgroup>
  10146. <tr><td> <b>aggregation</b> </td><td>: Festlegung der Selektionspaketierung </td></tr>
  10147. <tr><td> <b>device</b> </td><td>: Einschränkung des Exports auf ein bestimmtes Device </td></tr>
  10148. <tr><td> <b>reading</b> </td><td>: Einschränkung des Exports auf ein bestimmtes Reading </td></tr>
  10149. <tr><td> <b>executeBeforeProc</b> </td><td>: FHEM Kommando (oder perl-Routine) vor dem Export ausführen </td></tr>
  10150. <tr><td> <b>executeAfterProc</b> </td><td>: FHEM Kommando (oder perl-Routine) nach dem Export ausführen </td></tr>
  10151. <tr><td> <b>expimpfile</b> </td><td>: der Name des Exportfiles </td></tr>
  10152. <tr><td> <b>time.*</b> </td><td>: eine Reihe von Attributen zur Zeitabgrenzung </td></tr>
  10153. </table>
  10154. </ul>
  10155. </li><br>
  10156. <li><b> fetchrows [history|current] </b>
  10157. - liefert <b>alle</b> Tabelleneinträge (default: history)
  10158. in den gegebenen Zeitgrenzen bzw. Selektionsbedingungen durch die <a href="#DbRepattr">Attribute</a>
  10159. "device" und "reading".
  10160. Eine evtl. gesetzte Aggregation wird dabei <b>nicht</b> berücksichtigt. <br>
  10161. Die Leserichtung in der Datenbank kann durch das <a href="#DbRepattr">Attribut</a>
  10162. "fetchRoute" bestimmt werden. <br><br>
  10163. Jedes Ergebnisreading setzt sich aus dem Timestring des Datensatzes, einem Index, dem Device
  10164. und dem Reading zusammen.
  10165. Die Funktion fetchrows ist in der Lage mehrfach vorkommende Datensätze (Dubletten) zu erkennen.
  10166. Solche Dubletten sind mit einem Index > 1 gekennzeichnet. <br>
  10167. Dubletten können mit dem Attribut "fetchMarkDuplicates" farblich hervorgehoben werden. <br><br>
  10168. <b>Hinweis:</b> <br>
  10169. Hervorgehobene Readings werden nach einem Restart bzw. nach rereadcfg nicht mehr angezeigt da
  10170. sie nicht im statefile gesichert werden (Verletzung erlaubter Readingnamen durch Formatierung).
  10171. <br><br>
  10172. Dieses Attribut ist mit einigen Farben vorbelegt, kann aber mit dem colorpicker-Widget
  10173. überschrieben werden: <br><br>
  10174. <ul>
  10175. <code>
  10176. attr &lt;name&gt; widgetOverride fetchMarkDuplicates:colorpicker
  10177. </code>
  10178. </ul>
  10179. <br>
  10180. Die Ergebnisreadings von fetchrows sind nach folgendem Schema aufgebaut: <br><br>
  10181. <ul>
  10182. <b>Beispiel:</b> <br>
  10183. 2017-10-22_03-04-43__1__SMA_Energymeter__Bezug_WirkP_Kosten_Diff <br>
  10184. # &lt;Datum&gt;_&lt;Zeit&gt;__&lt;Index&gt;__&lt;Device&gt;__&lt;Reading&gt;
  10185. </ul>
  10186. <br>
  10187. Zur besseren Übersicht sind die zur Steuerung von fetchrows relevanten Attribute hier noch einmal
  10188. dargestellt: <br><br>
  10189. <ul>
  10190. <table>
  10191. <colgroup> <col width=5%> <col width=95%> </colgroup>
  10192. <tr><td> <b>fetchRoute</b> </td><td>: Leserichtung des Selekts innerhalb der Datenbank </td></tr>
  10193. <tr><td> <b>limit</b> </td><td>: begrenzt die Anzahl zu selektierenden bzw. anzuzeigenden Datensätze </td></tr>
  10194. <tr><td> <b>fetchMarkDuplicates</b> </td><td>: Hervorhebung von gefundenen Dubletten </td></tr>
  10195. <tr><td> <b>device</b> </td><td>: Selektion nur von Datensätzen die &lt;device&gt; enthalten </td></tr>
  10196. <tr><td> <b>reading</b> </td><td>: Selektion nur von Datensätzen die &lt;reading&gt; enthalten </td></tr>
  10197. <tr><td> <b>time.*</b> </td><td>: eine Reihe von Attributen zur Zeitabgrenzung </td></tr>
  10198. <tr><td> <b>valueFilter</b> </td><td>: filtert Datensätze des Datenbankfeldes "VALUE" mit einem regulären Ausdruck </td></tr>
  10199. </table>
  10200. </ul>
  10201. <br>
  10202. <br>
  10203. <b>Hinweis:</b> <br>
  10204. Auch wenn das Modul bezüglich der Datenbankabfrage nichtblockierend arbeitet, kann eine
  10205. zu große Ergebnismenge (Anzahl Zeilen bzw. Readings) die Browsersesssion bzw. FHEMWEB
  10206. blockieren. Aus diesem Grund wird die Ergebnismenge mit dem
  10207. <a href="#limit">Attribut</a> "limit" begrenzt. Bei Bedarf kann dieses Attribut
  10208. geändert werden, falls eine Anpassung der Selektionsbedingungen nicht möglich oder
  10209. gewünscht ist. <br><br>
  10210. </li> <br>
  10211. <li><b> insert </b> - Manuelles Einfügen eines Datensatzes in die Tabelle "history". Obligatorisch sind Eingabewerte für Datum, Zeit und Value.
  10212. 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>
  10213. <ul>
  10214. <b>Eingabeformat: </b> Datum,Zeit,Value,[Unit] <br>
  10215. # Unit ist optional, Attribute "reading" und "device" müssen gesetzt sein <br>
  10216. # Soll "Value=0" eingefügt werden, ist "Value = 0.0" zu verwenden. <br><br>
  10217. <b>Beispiel: </b> 2016-08-01,23:00:09,TestValue,TestUnit <br>
  10218. # Es sind KEINE Leerzeichen im Feldwert erlaubt !<br>
  10219. <br>
  10220. <b>Hinweis: </b><br>
  10221. Bei der Eingabe ist darauf zu achten dass im beabsichtigten Aggregationszeitraum (Tag, Woche, Monat, etc.) MINDESTENS zwei
  10222. Datensätze für die Funktion diffValue zur Verfügung stehen. Ansonsten kann keine Differenz berechnet werden und diffValue
  10223. gibt in diesem Fall "0" in der betroffenen Periode aus !
  10224. <br>
  10225. <br>
  10226. </li>
  10227. </ul>
  10228. <li><b> importFromFile [&lt;File&gt;] </b>
  10229. - importiert Datensätze im CSV-Format aus einer Datei in die Datenbank. <br>
  10230. Der Dateiname wird durch das <a href="#DbRepattr">Attribut</a> "expimpfile" bestimmt. <br>
  10231. Alternativ kann die Datei (/Pfad/Datei) als Kommando-Option angegeben werden und übersteuert ein
  10232. eventuell gesetztes Attribut "expimpfile". Der Dateiname kann Wildcards enthalten (siehe
  10233. Attribut "expimpfile"). <br><br>
  10234. <ul>
  10235. <b>Datensatzformat: </b> <br>
  10236. "TIMESTAMP","DEVICE","TYPE","EVENT","READING","VALUE","UNIT" <br><br>
  10237. # Die Felder "TIMESTAMP","DEVICE","TYPE","EVENT","READING" und "VALUE" müssen gesetzt sein. Das Feld "UNIT" ist optional.
  10238. Der Fileinhalt wird als Transaktion importiert, d.h. es wird der Inhalt des gesamten Files oder, im Fehlerfall, kein Datensatz des Files importiert.
  10239. 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
  10240. das Logfile geschrieben werden was FHEM blockieren oder überlasten könnte. <br><br>
  10241. <b>Beispiel: </b> <br>
  10242. "2016-09-25 08:53:56","STP_5000","SMAUTILS","etotal: 11859.573","etotal","11859.573","" <br>
  10243. <br>
  10244. Die für diese Funktion relevanten Attribute sind: <br><br>
  10245. <ul>
  10246. <table>
  10247. <colgroup> <col width=5%> <col width=95%> </colgroup>
  10248. <tr><td> <b>executeBeforeProc</b> </td><td>: FHEM Kommando (oder perl-Routine) vor dem Import ausführen </td></tr>
  10249. <tr><td> <b>executeAfterProc</b> </td><td>: FHEM Kommando (oder perl-Routine) nach dem Import ausführen </td></tr>
  10250. <tr><td> <b>expimpfile</b> </td><td>: der Name des Importfiles </td></tr>
  10251. </table>
  10252. </ul>
  10253. </li> <br>
  10254. </ul>
  10255. <br>
  10256. <li><b> maxValue [display | writeToDB] </b>
  10257. - berechnet den Maximalwert des Datenbankfelds "VALUE" in den Zeitgrenzen
  10258. (Attribute) "timestamp_begin", "timestamp_end" bzw. "timeDiffToNow / timeOlderThan".
  10259. Es muss das auszuwertende Reading über das <a href="#DbRepattr">Attribut</a> "reading"
  10260. angegeben sein.
  10261. Die Auswertung enthält den Zeitstempel des ermittelten Maximumwertes innerhalb der
  10262. Aggregation bzw. Zeitgrenzen.
  10263. Im Reading wird der Zeitstempel des <b>letzten</b> Auftretens vom Maximalwert ausgegeben
  10264. falls dieser Wert im Intervall mehrfach erreicht wird. <br>
  10265. Ist keine oder die Option "display" angegeben, werden die Ergebnisse nur angezeigt. Mit
  10266. der Option "writeToDB" werden die Berechnungsergebnisse mit einem neuen Readingnamen
  10267. in der Datenbank gespeichert. <br>
  10268. Der neue Readingname wird aus einem Präfix und dem originalen Readingnamen gebildet,
  10269. wobei der originale Readingname durch das Attribut "readingNameMap" ersetzt werden kann.
  10270. Der Präfix setzt sich aus der Bildungsfunktion und der Aggregation zusammen. <br>
  10271. Der Timestamp der neuen Readings in der Datenbank wird von der eingestellten Aggregationsperiode
  10272. abgeleitet, sofern kein eindeutiger Zeitpunkt des Ergebnisses bestimmt werden kann.
  10273. Das Feld "EVENT" wird mit "calculated" gefüllt.<br><br>
  10274. <ul>
  10275. <b>Beispiel neuer Readingname gebildet aus dem Originalreading "totalpac":</b> <br>
  10276. max_day_totalpac <br>
  10277. # &lt;Bildungsfunktion&gt;_&lt;Aggregation&gt;_&lt;Originalreading&gt; <br>
  10278. </li> <br>
  10279. </ul>
  10280. <li><b> minValue [display | writeToDB]</b>
  10281. - berechnet den Minimalwert des Datenbankfelds "VALUE" in den Zeitgrenzen
  10282. (Attribute) "timestamp_begin", "timestamp_end" bzw. "timeDiffToNow / timeOlderThan".
  10283. Es muss das auszuwertende Reading über das <a href="#DbRepattr">Attribut</a> "reading"
  10284. angegeben sein.
  10285. Die Auswertung enthält den Zeitstempel des ermittelten Minimumwertes innerhalb der
  10286. Aggregation bzw. Zeitgrenzen.
  10287. Im Reading wird der Zeitstempel des <b>ersten</b> Auftretens vom Minimalwert ausgegeben
  10288. falls dieser Wert im Intervall mehrfach erreicht wird. <br>
  10289. Ist keine oder die Option "display" angegeben, werden die Ergebnisse nur angezeigt. Mit
  10290. der Option "writeToDB" werden die Berechnungsergebnisse mit einem neuen Readingnamen
  10291. in der Datenbank gespeichert. <br>
  10292. Der neue Readingname wird aus einem Präfix und dem originalen Readingnamen gebildet,
  10293. wobei der originale Readingname durch das Attribut "readingNameMap" ersetzt werden kann.
  10294. Der Präfix setzt sich aus der Bildungsfunktion und der Aggregation zusammen. <br>
  10295. Der Timestamp der neuen Readings in der Datenbank wird von der eingestellten Aggregationsperiode
  10296. abgeleitet, sofern kein eindeutiger Zeitpunkt des Ergebnisses bestimmt werden kann.
  10297. Das Feld "EVENT" wird mit "calculated" gefüllt.<br><br>
  10298. <ul>
  10299. <b>Beispiel neuer Readingname gebildet aus dem Originalreading "totalpac":</b> <br>
  10300. min_day_totalpac <br>
  10301. # &lt;Bildungsfunktion&gt;_&lt;Aggregation&gt;_&lt;Originalreading&gt; <br>
  10302. </li> <br>
  10303. </ul>
  10304. <li><b> optimizeTables </b> - optimiert die Tabellen in der angeschlossenen Datenbank (MySQL). <br>
  10305. Vor und nach der Optimierung kann ein FHEM-Kommando ausgeführt werden.
  10306. (siehe <a href="#DbRepattr">Attribute</a> "executeBeforeProc", "executeAfterProc")
  10307. <br><br>
  10308. <ul>
  10309. <b>Hinweis:</b> <br>
  10310. Obwohl die Funktion selbst non-blocking ausgelegt ist, muß das zugeordnete DbLog-Device
  10311. im asynchronen Modus betrieben werden um ein Blockieren von FHEMWEB zu vermeiden. <br><br>
  10312. </li>
  10313. </ul><br>
  10314. <li><b> readingRename </b> - benennt den Namen eines Readings innerhalb der angeschlossenen Datenbank (siehe Internal DATABASE) um.
  10315. Der Readingname wird immer in der <b>gesamten</b> Datenbank umgesetzt. Eventuell
  10316. gesetzte Zeitgrenzen oder Beschränkungen durch die <a href="#DbRepattr">Attribute</a>
  10317. Device bzw. Reading werden nicht berücksichtigt. <br><br>
  10318. <ul>
  10319. <b>Beispiel: </b><br>
  10320. set &lt;name&gt; readingRename &lt;alter Readingname&gt;,&lt;neuer Readingname&gt; <br>
  10321. # Die Anzahl der umbenannten Device-Datensätze wird im Reading "reading_renamed"
  10322. ausgegeben. <br>
  10323. # Wird der umzubenennende Readingname in der Datenbank nicht gefunden, wird eine
  10324. WARNUNG im Reading "reading_not_renamed" ausgegeben. <br>
  10325. # Entsprechende Einträge erfolgen auch im Logfile mit verbose=3.
  10326. <br><br>
  10327. <b>Hinweis:</b> <br>
  10328. Obwohl die Funktion selbst non-blocking ausgelegt ist, sollte das zugeordnete DbLog-Device
  10329. im asynchronen Modus betrieben werden um ein Blockieren von FHEMWEB zu vermeiden (Tabellen-Lock). <br><br>
  10330. </li> <br>
  10331. </ul>
  10332. <li><b> repairSQLite </b> - repariert eine korrupte SQLite-Datenbank. <br>
  10333. Eine Korruption liegt im Allgemeinen vor wenn die Fehlermitteilung "database disk image is malformed"
  10334. im state des DbLog-Devices erscheint.
  10335. Wird dieses Kommando gestartet, wird das angeschlossene DbLog-Device zunächst automatisch für 10 Stunden
  10336. (36000 Sekunden) von der Datenbank getrennt (Trennungszeit). Nach Abschluss der Reparatur erfolgt
  10337. wieder eine sofortige Neuverbindung zur reparierten Datenbank. <br>
  10338. Dem Befehl kann eine abweichende Trennungszeit (in Sekunden) als Argument angegeben werden. <br>
  10339. Die korrupte Datenbank wird als &lt;database&gt;.corrupt im gleichen Verzeichnis gespeichert. <br><br>
  10340. <ul>
  10341. <b>Beispiel: </b><br>
  10342. set &lt;name&gt; repairSQLite <br>
  10343. # Die Datenbank wird repariert, Trennungszeit beträgt 10 Stunden <br>
  10344. set &lt;name&gt; repairSQLite 600 <br>
  10345. # Die Datenbank wird repariert, Trennungszeit beträgt 10 Minuten
  10346. <br><br>
  10347. <b>Hinweis:</b> <br>
  10348. Es ist nicht garantiert, dass die Reparatur erfolgreich verläuft und keine Daten verloren gehen.
  10349. Je nach Schwere der Korruption kann Datenverlust auftreten oder die Reparatur scheitern, auch wenn
  10350. kein Fehler im Ablauf signalisiert wird. Ein Backup der Datenbank sollte unbedingt vorhanden
  10351. sein ! <br><br>
  10352. </li> <br>
  10353. </ul>
  10354. <li><b> restoreMySQL &lt;File&gt; </b> - stellt die Datenbank aus einem serverSide- oder clientSide-Dump wieder her. <br>
  10355. Die Funktion stellt über eine Drop-Down Liste eine Dateiauswahl für den Restore zur Verfügung. <br><br>
  10356. <b>Verwendung eines serverSide-Dumps </b> <br>
  10357. Es wird der Inhalt der history-Tabelle aus einem serverSide-Dump wiederhergestellt.
  10358. Dazu ist das Verzeichnis "dumpDirRemote" des MySQL-Servers auf dem Client zu mounten
  10359. und im <a href="#DbRepattr">Attribut</a> "dumpDirLocal" dem DbRep-Device bekannt zu machen. <br>
  10360. Es werden alle Files mit der Endung "csv[.gzip]" und deren Name mit der
  10361. verbundenen Datenbank beginnt (siehe Internal DATABASE), aufgelistet.
  10362. <br><br>
  10363. <b>Verwendung eines clientSide-Dumps </b> <br>
  10364. Es werden alle Tabellen und eventuell vorhandenen Views wiederhergestellt.
  10365. Das Verzeichnis, in dem sich die Dump-Files befinden, ist im <a href="#DbRepattr">Attribut</a> "dumpDirLocal" dem
  10366. DbRep-Device bekannt zu machen. <br>
  10367. Es werden alle Files mit der Endung "sql[.gzip]" und deren Name mit der
  10368. verbundenen Datenbank beginnt (siehe Internal DATABASE), aufgelistet. <br>
  10369. Die Geschwindigkeit des Restores ist abhängig von der Servervariable "<b>max_allowed_packet</b>". Durch Veränderung
  10370. dieser Variable im File my.cnf kann die Geschwindigkeit angepasst werden. Auf genügend verfügbare Ressourcen (insbesondere
  10371. RAM) ist dabei zu achten. <br><br>
  10372. Der Datenbankuser benötigt Rechte zum Tabellenmanagement, z.B.: <br>
  10373. CREATE, ALTER, INDEX, DROP, SHOW VIEW, CREATE VIEW
  10374. <br><br>
  10375. </li><br>
  10376. <li><b> restoreSQLite &lt;File&gt;.sqlitebkp[.gzip] </b> - stellt das Backup einer SQLite-Datenbank wieder her. <br>
  10377. Die Funktion stellt über eine Drop-Down Liste die für den Restore zur Verfügung stehenden Dateien
  10378. zur Verfügung. Die aktuell in der Zieldatenbank enthaltenen Daten werden gelöscht bzw.
  10379. überschrieben.
  10380. Es werden alle Files mit der Endung "sqlitebkp[.gzip]" und deren Name mit dem Namen der
  10381. verbundenen Datenbank beginnt, aufgelistet . <br><br>
  10382. </li><br>
  10383. <li><b> sqlCmd </b> - führt ein beliebiges Benutzer spezifisches Kommando aus. <br>
  10384. Enthält dieses Kommando eine Delete-Operation, muss zur Sicherheit das
  10385. <a href="#DbRepattr">Attribut</a> "allowDeletion" gesetzt sein. <br>
  10386. Bei der Ausführung dieses Kommandos werden keine Einschränkungen durch gesetzte Attribute
  10387. "device", "reading", "time.*" bzw. "aggregation" berücksichtigt. <br>
  10388. Sollen die im Modul gesetzten <a href="#DbRepattr">Attribute</a> "timestamp_begin" bzw.
  10389. "timestamp_end" im Statement berücksichtigt werden, können die Platzhalter
  10390. "<b>§timestamp_begin§</b>" bzw. "<b>§timestamp_end§</b>" dafür verwendet werden. <br><br>
  10391. Soll ein Datensatz upgedated werden, ist dem Statement "TIMESTAMP=TIMESTAMP" hinzuzufügen um eine Änderung des
  10392. originalen Timestamps zu verhindern. <br><br>
  10393. <ul>
  10394. <b>Beispiele für Statements: </b> <br><br>
  10395. <ul>
  10396. <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>
  10397. <li>set &lt;name&gt; sqlCmd select DEVICE, count(*) from history where TIMESTAMP >= "2017-05-06 00:00:00" group by DEVICE </li>
  10398. <li>set &lt;name&gt; sqlCmd select DEVICE, count(*) from history where TIMESTAMP >= §timestamp_begin§ group by DEVICE </li>
  10399. <li>set &lt;name&gt; sqlCmd select * from history where DEVICE like "Te%t" order by `TIMESTAMP` desc </li>
  10400. <li>set &lt;name&gt; sqlCmd select * from history where `TIMESTAMP` > "2017-05-09 18:03:00" order by `TIMESTAMP` desc </li>
  10401. <li>set &lt;name&gt; sqlCmd select * from current order by `TIMESTAMP` desc </li>
  10402. <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>
  10403. <li>set &lt;name&gt; sqlCmd delete from current </li>
  10404. <li>set &lt;name&gt; sqlCmd delete from history where TIMESTAMP < "2016-05-06 00:00:00" </li>
  10405. <li>set &lt;name&gt; sqlCmd update history set TIMESTAMP=TIMESTAMP,VALUE='Val' WHERE VALUE='TestValue' </li>
  10406. <li>set &lt;name&gt; sqlCmd select * from history where DEVICE = "Test" </li>
  10407. <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>
  10408. </ul>
  10409. <br>
  10410. Das Ergebnis des Statements wird im <a href="#DbRepReadings">Reading</a> "SqlResult" dargestellt.
  10411. Die Ergebnis-Formatierung kann durch das <a href="#DbRepattr">Attribut</a> "sqlResultFormat" ausgewählt, sowie der verwendete
  10412. Feldtrenner durch das <a href="#DbRepattr">Attribut</a> "sqlResultFieldSep" festgelegt werden. <br><br>
  10413. Das Modul stellt optional eine Kommando-Historie zur Verfügung sobald ein SQL-Kommando erfolgreich
  10414. ausgeführt wurde.
  10415. Um diese Option zu nutzen, ist das Attribut "sqlCmdHistoryLength" mit der gewünschten Listenlänge
  10416. zu aktivieren. <br><br>
  10417. Zur besseren Übersicht sind die zur Steuerung von sqlCmd relevanten Attribute hier noch einmal
  10418. dargestellt: <br><br>
  10419. <ul>
  10420. <table>
  10421. <colgroup> <col width=5%> <col width=95%> </colgroup>
  10422. <tr><td> <b>allowDeletion</b> </td><td>: aktiviert Löschmöglichkeit </td></tr>
  10423. <tr><td> <b>sqlResultFormat</b> </td><td>: legt die Darstellung des Kommandoergebnis fest </td></tr>
  10424. <tr><td> <b>sqlResultFieldSep</b> </td><td>: Auswahl Feldtrenner im Ergebnis </td></tr>
  10425. <tr><td> <b>sqlCmdHistoryLength</b> </td><td>: Aktivierung Kommando-Historie und deren Umfang</td></tr>
  10426. </table>
  10427. </ul>
  10428. <br>
  10429. <br>
  10430. <b>Hinweis:</b> <br>
  10431. Auch wenn das Modul bezüglich der Datenbankabfrage nichtblockierend arbeitet, kann eine
  10432. zu große Ergebnismenge (Anzahl Zeilen bzw. Readings) die Browsersesssion bzw. FHEMWEB
  10433. blockieren. Wenn man sich unsicher ist, sollte man vorsorglich dem Statement ein Limit
  10434. hinzufügen. <br><br>
  10435. </li> <br>
  10436. </ul>
  10437. <li><b> sqlCmdHistory </b> - Wenn mit dem <a href="#DbRepattr">Attribut</a> "sqlCmdHistoryLength" aktiviert, kann
  10438. aus einer Liste ein bereits erfolgreich ausgeführtes sqlCmd-Kommando wiederholt werden. <br>
  10439. Mit Ausführung des letzten Eintrags der Liste, "__purge_historylist__", kann die Liste gelöscht
  10440. werden. <br>
  10441. Falls das Statement "," enthält, wird dieses Zeichen aus technischen Gründen in der
  10442. History-Liste als "&lt;c&gt;" dargestellt. <br>
  10443. </li><br>
  10444. <li><b> sqlSpecial </b> - Die Funktion bietet eine Drop-Downliste mit einer Auswahl vorbereiter Auswertungen
  10445. an. <br>
  10446. Das Ergebnis des Statements wird im Reading "SqlResult" dargestellt.
  10447. Die Ergebnis-Formatierung kann durch das <a href="#DbRepattr">Attribut</a> "sqlResultFormat"
  10448. ausgewählt, sowie der verwendete Feldtrenner durch das <a href="#DbRepattr">Attribut</a>
  10449. "sqlResultFieldSep" festgelegt werden. <br><br>
  10450. Die für diese Funktion relevanten Attribute sind: <br><br>
  10451. <ul>
  10452. <table>
  10453. <colgroup> <col width=5%> <col width=95%> </colgroup>
  10454. <tr><td> <b>sqlResultFormat</b> </td><td>: Optionen der Ergebnisformatierung </td></tr>
  10455. <tr><td> <b>sqlResultFieldSep</b> </td><td>: Auswahl des Trennzeichens zwischen Ergebnisfeldern </td></tr>
  10456. </table>
  10457. </ul>
  10458. <br>
  10459. Es sind die folgenden vordefinierte Auswertungen auswählbar: <br><br>
  10460. <ul>
  10461. <table>
  10462. <colgroup> <col width=5%> <col width=95%> </colgroup>
  10463. <tr><td> <b>50mostFreqLogsLast2days </b> </td><td>: ermittelt die 50 am häufigsten vorkommenden Loggingeinträge der letzten 2 Tage </td></tr>
  10464. <tr><td> <b>allDevCount </b> </td><td>: alle in der Datenbank vorkommenden Devices und deren Anzahl </td></tr>
  10465. <tr><td> <b>allDevReadCount </b> </td><td>: alle in der Datenbank vorkommenden Device/Reading-Kombinationen und deren Anzahl</td></tr>
  10466. </table>
  10467. </ul>
  10468. </li><br><br>
  10469. <li><b> sumValue [display | writeToDB]</b>
  10470. - Berechnet die Summenwerte des Datenbankfelds "VALUE" in den Zeitgrenzen
  10471. (Attribute) "timestamp_begin", "timestamp_end" bzw. "timeDiffToNow / timeOlderThan".
  10472. Es muss das auszuwertende Reading im <a href="#DbRepattr">Attribut</a> "reading"
  10473. angegeben sein. Diese Funktion ist sinnvoll wenn fortlaufend Wertedifferenzen eines
  10474. Readings in die Datenbank geschrieben werden. <br>
  10475. Ist keine oder die Option "display" angegeben, werden die Ergebnisse nur angezeigt. Mit
  10476. der Option "writeToDB" werden die Berechnungsergebnisse mit einem neuen Readingnamen
  10477. in der Datenbank gespeichert. <br>
  10478. Der neue Readingname wird aus einem Präfix und dem originalen Readingnamen gebildet,
  10479. wobei der originale Readingname durch das Attribut "readingNameMap" ersetzt werden kann.
  10480. Der Präfix setzt sich aus der Bildungsfunktion und der Aggregation zusammen. <br>
  10481. Der Timestamp der neuen Readings in der Datenbank wird von der eingestellten Aggregationsperiode
  10482. abgeleitet, sofern kein eindeutiger Zeitpunkt des Ergebnisses bestimmt werden kann.
  10483. Das Feld "EVENT" wird mit "calculated" gefüllt.<br><br>
  10484. <ul>
  10485. <b>Beispiel neuer Readingname gebildet aus dem Originalreading "totalpac":</b> <br>
  10486. sum_day_totalpac <br>
  10487. # &lt;Bildungsfunktion&gt;_&lt;Aggregation&gt;_&lt;Originalreading&gt; <br>
  10488. </li> <br>
  10489. </ul>
  10490. <br>
  10491. <li><b> syncStandby &lt;DbLog-Device Standby&gt; </b>
  10492. - Es werden die Datensätze aus der angeschlossenen Datenbank (Quelle) direkt in eine weitere
  10493. Datenbank (Standby-Datenbank) übertragen.
  10494. Dabei ist "&lt;DbLog-Device Standby&gt;" das DbLog-Device, welches mit der Standby-Datenbank
  10495. verbunden ist. <br><br>
  10496. Es werden alle Datensätze übertragen, die durch Timestamp-<a href="#limit">Attribute</a>
  10497. bzw. die Attribute "device", "reading" bestimmt sind. <br>
  10498. Die Datensätze werden dabei in Zeitscheiben entsprechend der eingestellten Aggregation übertragen.
  10499. Hat das Attribut "aggregation" den Wert "no" oder "month", werden die Datensätze automatisch
  10500. in Tageszeitscheiben zur Standby-Datenbank übertragen.
  10501. Quell- und Standby-Datenbank können unterschiedlichen Typs sein.
  10502. <br><br>
  10503. Die zur Steuerung der syncStandby Funktion relevanten Attribute sind: <br><br>
  10504. <ul>
  10505. <table>
  10506. <colgroup> <col width=5%> <col width=95%> </colgroup>
  10507. <tr><td> <b>aggregation</b> </td><td>: Einstellung der Zeitscheiben zur Übertragung (hour,day,week) </td></tr>
  10508. <tr><td> <b>device</b> </td><td>: Übertragung nur von Datensätzen die &lt;device&gt; enthalten </td></tr>
  10509. <tr><td> <b>reading</b> </td><td>: Übertragung nur von Datensätzen die &lt;reading&gt; enthalten </td></tr>
  10510. <tr><td> <b>time.*</b> </td><td>: Attribute zur Zeitabgrenzung der zu übertragenden Datensätze. </td></tr>
  10511. </table>
  10512. </ul>
  10513. <br>
  10514. <br>
  10515. </li> <br>
  10516. <li><b> tableCurrentFillup </b> - Die current-Tabelle wird mit einem Extrakt der history-Tabelle aufgefüllt.
  10517. Die <a href="#DbRepattr">Attribute</a> zur Zeiteinschränkung bzw. device, reading werden ausgewertet.
  10518. Dadurch kann der Inhalt des Extrakts beeinflusst werden. Im zugehörigen DbLog-Device sollte sollte das Attribut
  10519. "DbLogType=SampleFill/History" gesetzt sein. </li> <br>
  10520. <li><b> tableCurrentPurge </b> - löscht den Inhalt der current-Tabelle. Es werden keine Limitierungen, z.B. durch die Attribute "timestamp_begin",
  10521. "timestamp_end", device, reading, usw. , ausgewertet. </li> <br>
  10522. <li><b> vacuum </b> - optimiert die Tabellen in der angeschlossenen Datenbank (SQLite, PostgreSQL). <br>
  10523. Vor und nach der Optimierung kann ein FHEM-Kommando ausgeführt werden.
  10524. (siehe <a href="#DbRepattr">Attribute</a> "executeBeforeProc", "executeAfterProc")
  10525. <br><br>
  10526. <ul>
  10527. <b>Hinweis:</b> <br>
  10528. Obwohl die Funktion selbst non-blocking ausgelegt ist, muß das zugeordnete DbLog-Device
  10529. im asynchronen Modus betrieben werden um ein Blockieren von FHEM zu vermeiden. <br><br>
  10530. </li>
  10531. </ul><br>
  10532. <br>
  10533. </ul></ul>
  10534. <b>Für alle Auswertungsvarianten (Ausnahme sqlCmd,deviceRename,readingRename) gilt: </b> <br>
  10535. Zusätzlich zu dem auszuwertenden Reading kann das Device mit angegeben werden um das Reporting nach diesen Kriterien einzuschränken.
  10536. Sind keine Zeitgrenzen-Attribute angegeben jedoch das Aggregations-Attribut gesetzt, wird der Zeitstempel des ältesten
  10537. Datensatzes in der Datenbank als Startdatum und das aktuelle Datum/die aktuelle Zeit als Zeitgrenze genutzt.
  10538. Konnte der älteste Datensatz in der Datenbank nicht ermittelt werden, wird '1970-01-01 01:00:00' als Selektionsstart
  10539. genutzt (siehe get &lt;name&gt; minTimestamp).
  10540. Sind weder Zeitgrenzen-Attribute noch Aggregation angegeben, wird die Datenselektion ohne Timestamp-Einschränkungen
  10541. ausgeführt.
  10542. <br><br>
  10543. <b>Hinweis: </b> <br>
  10544. In der Detailansicht kann ein Browserrefresh nötig sein um die Operationsergebnisse zu sehen sobald im DeviceOverview "state = done" angezeigt wird.
  10545. <br><br>
  10546. </ul>
  10547. <a name="DbRepget"></a>
  10548. <b>Get </b>
  10549. <ul>
  10550. Die Get-Kommandos von DbRep dienen dazu eine Reihe von Metadaten der verwendeten Datenbankinstanz abzufragen.
  10551. Dies sind zum Beispiel eingestellte Serverparameter, Servervariablen, Datenbankstatus- und Tabelleninformationen. Die verfügbaren get-Funktionen
  10552. 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,
  10553. die über über funktionsspezifische <a href="#DbRepattr">Attribute</a> abgrenzbar sind. Der Filter ist als kommaseparierte Liste anzuwenden.
  10554. Dabei kann SQL-Wildcard (%) verwendet werden.
  10555. <br><br>
  10556. <b>Hinweis: </b> <br>
  10557. Nach der Ausführung einer get-Funktion in der Detailsicht einen Browserrefresh durchführen um die Ergebnisse zu sehen !
  10558. <br><br>
  10559. <ul><ul>
  10560. <li><b> blockinginfo </b> - Listet die aktuell systemweit laufenden Hintergrundprozesse (BlockingCalls) mit ihren Informationen auf.
  10561. Zu lange Zeichenketten (z.B. Argumente) werden gekürzt ausgeschrieben.
  10562. </li>
  10563. <br><br>
  10564. <li><b> dbstatus </b> - Listet globale Informationen zum MySQL Serverstatus (z.B. Informationen zum Cache, Threads, Bufferpools, etc. ).
  10565. Es werden zunächst alle verfügbaren Informationen berichtet. Mit dem <a href="#DbRepattr">Attribut</a> "showStatus" kann die
  10566. Ergebnismenge eingeschränkt werden, um nur gewünschte Ergebnisse abzurufen. Detailinformationen zur Bedeutung der einzelnen Readings
  10567. sind <a href=http://dev.mysql.com/doc/refman/5.7/en/server-status-variables.html>hier</a> verfügbar. <br>
  10568. <br><ul>
  10569. <b>Bespiel</b> <br>
  10570. get &lt;name&gt; dbstatus <br>
  10571. attr &lt;name&gt; showStatus %uptime%,%qcache% <br>
  10572. # Es werden nur Readings erzeugt die im Namen "uptime" und "qcache" enthalten
  10573. </li>
  10574. <br><br>
  10575. </ul>
  10576. <li><b> dbValue &lt;SQL-Statement&gt;</b> -
  10577. Führt das angegebene SQL-Statement <b>blockierend</b> aus. Diese Funktion ist durch ihre Arbeitsweise
  10578. speziell für den Einsatz in usereigenen Scripten geeignet. <br>
  10579. Die Eingabe akzeptiert Mehrzeiler und gibt ebenso mehrzeilige Ergebisse zurück.
  10580. Werden mehrere Felder selektiert und zurückgegeben, erfolgt die Feldtrennung mit dem Trenner
  10581. des <a href="#DbRepattr">Attributes</a> "sqlResultFieldSep" (default "|"). Mehrere Ergebniszeilen
  10582. werden mit Newline ("\n") separiert. <br>
  10583. Diese Funktion setzt/aktualisiert nur Statusreadings, die Funktion im Attribut "userExitFn"
  10584. wird nicht aufgerufen.
  10585. <br>
  10586. <br><ul>
  10587. <b>Bespiele zur Nutzung im FHEMWEB</b> <br>
  10588. {fhem("get &lt;name&gt; dbValue select device,count(*) from history where timestamp > '2018-04-01' group by device")} <br>
  10589. get &lt;name&gt; dbValue select device,count(*) from history where timestamp > '2018-04-01' group by device <br>
  10590. {CommandGet(undef,"Rep.LogDB1 dbValue select device,count(*) from history where timestamp > '2018-04-01' group by device")} <br>
  10591. </ul>
  10592. <br><br>
  10593. Erstellt man eine kleine Routine in 99_myUtils, wie z.B.:
  10594. <br>
  10595. <pre>
  10596. sub dbval($$) {
  10597. my ($name,$cmd) = @_;
  10598. my $ret = CommandGet(undef,"$name dbValue $cmd");
  10599. return $ret;
  10600. }
  10601. </pre>
  10602. kann dbValue vereinfacht verwendet werden mit Aufrufen wie:
  10603. <br><br>
  10604. <ul>
  10605. <b>Bespiele</b> <br>
  10606. {dbval("&lt;name&gt;","select count(*) from history")} <br>
  10607. oder <br>
  10608. $ret = dbval("&lt;name&gt;","select count(*) from history"); <br>
  10609. </ul>
  10610. </li>
  10611. <br><br>
  10612. <li><b> dbvars </b> - Zeigt die globalen Werte der MySQL Systemvariablen. Enthalten sind zum Beispiel Angaben zum InnoDB-Home, dem Datafile-Pfad,
  10613. Memory- und Cache-Parameter, usw. Die Ausgabe listet zunächst alle verfügbaren Informationen auf. Mit dem
  10614. <a href="#DbRepattr">Attribut</a> "showVariables" kann die Ergebnismenge eingeschränkt werden um nur gewünschte Ergebnisse
  10615. abzurufen. Weitere Informationen zur Bedeutung der ausgegebenen Variablen sind
  10616. <a href=http://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html>hier</a> verfügbar. <br>
  10617. <br><ul>
  10618. <b>Bespiel</b> <br>
  10619. get &lt;name&gt; dbvars <br>
  10620. attr &lt;name&gt; showVariables %version%,%query_cache% <br>
  10621. # Es werden nur Readings erzeugt die im Namen "version" und "query_cache" enthalten
  10622. </li>
  10623. <br><br>
  10624. </ul>
  10625. <li><b> minTimestamp </b> - Ermittelt den Zeitstempel des ältesten Datensatzes in der Datenbank (wird implizit beim Start von
  10626. FHEM ausgeführt).
  10627. Der Zeitstempel wird als Selektionsbeginn verwendet wenn kein Zeitattribut den Selektionsbeginn
  10628. festlegt.
  10629. </li>
  10630. <br><br>
  10631. <li><b> procinfo </b> - Listet die existierenden Datenbank-Prozesse in einer Tabelle auf (nur MySQL). <br>
  10632. Typischerweise werden nur die Prozesse des Verbindungsusers (angegeben in DbLog-Konfiguration)
  10633. ausgegeben. Sollen alle Prozesse angezeigt werden, ist dem User das globale Recht "PROCESS"
  10634. einzuräumen. <br>
  10635. Für bestimmte SQL-Statements wird seit MariaDB 5.3 ein Fortschrittsreporting (Spalte "PROGRESS")
  10636. ausgegeben. Zum Beispiel kann der Abarbeitungsgrad bei der Indexerstellung verfolgt werden. <br>
  10637. Weitere Informationen sind
  10638. <a href=https://mariadb.com/kb/en/mariadb/show-processlist/>hier</a> verfügbar. <br>
  10639. </li>
  10640. <br><br>
  10641. <li><b> svrinfo </b> - allgemeine Datenbankserver-Informationen wie z.B. die DBMS-Version, Serveradresse und Port usw. Die Menge der Listenelemente
  10642. ist vom Datenbanktyp abhängig. Mit dem <a href="#DbRepattr">Attribut</a> "showSvrInfo" kann die Ergebnismenge eingeschränkt werden.
  10643. Weitere Erläuterungen zu den gelieferten Informationen sind
  10644. <a href=https://msdn.microsoft.com/en-us/library/ms711681(v=vs.85).aspx>hier</a> zu finden. <br>
  10645. <br><ul>
  10646. <b>Bespiel</b> <br>
  10647. get &lt;name&gt; svrinfo <br>
  10648. attr &lt;name&gt; showSvrInfo %SQL_CATALOG_TERM%,%NAME% <br>
  10649. # Es werden nur Readings erzeugt die im Namen "SQL_CATALOG_TERM" und "NAME" enthalten
  10650. </li>
  10651. <br><br>
  10652. </ul>
  10653. <li><b> tableinfo </b> - ruft Tabelleninformationen aus der mit dem DbRep-Device verbundenen Datenbank ab (MySQL).
  10654. Es werden per default alle in der verbundenen Datenbank angelegten Tabellen ausgewertet.
  10655. Mit dem <a href="#DbRepattr">Attribut</a> "showTableInfo" können die Ergebnisse eingeschränkt werden. Erläuterungen zu den erzeugten
  10656. Readings sind <a href=http://dev.mysql.com/doc/refman/5.7/en/show-table-status.html>hier</a> zu finden. <br>
  10657. <br><ul>
  10658. <b>Bespiel</b> <br>
  10659. get &lt;name&gt; tableinfo <br>
  10660. attr &lt;name&gt; showTableInfo current,history <br>
  10661. # Es werden nur Information der Tabellen "current" und "history" angezeigt
  10662. </li>
  10663. <br><br>
  10664. </ul>
  10665. <br>
  10666. </ul></ul>
  10667. </ul>
  10668. <a name="DbRepattr"></a>
  10669. <b>Attribute</b>
  10670. <br>
  10671. <ul>
  10672. Über die modulspezifischen Attribute wird die Abgrenzung der Auswertung und die Aggregation der Werte gesteuert. <br><br>
  10673. <b>Hinweis zur SQL-Wildcard Verwendung:</b> <br>
  10674. Innerhalb der Attribut-Werte für "device" und "reading" kann SQL-Wildcards "%" angegeben werden.
  10675. Dabei wird "%" als Platzhalter für beliebig viele Zeichen verwendet.
  10676. Das Zeichen "_" wird nicht als SQL-Wildcard supported. <br>
  10677. Dies gilt für alle Funktionen <b>ausser</b> "insert", "importFromFile" und "deviceRename". <br>
  10678. Die Funktion "insert" erlaubt nicht, dass die genannten Attribute das Wildcard "%" enthalten. Character "_" wird als normales Zeichen gewertet.<br>
  10679. In Ergebnis-Readings wird das Wildcardzeichen "%" durch "/" ersetzt um die Regeln für erlaubte Zeichen in Readings einzuhalten.
  10680. <br><br>
  10681. <ul><ul>
  10682. <a name="aggregation"></a>
  10683. <li><b>aggregation </b> - Zusammenfassung der Device/Reading-Selektionen in Stunden,Tage,Kalenderwochen,Kalendermonaten
  10684. oder "no". <br>
  10685. Liefert z.B. die Anzahl der DB-Einträge am Tag (countEntries), Summierung von
  10686. Differenzwerten eines Readings (sumValue), usw. <br>
  10687. Mit Aggregation "no" (default) erfolgt keine Zusammenfassung in einem Zeitraum, sondern die
  10688. Ausgabe wird aus allen Werten einer Device/Reading-Kombination zwischen den definierten
  10689. Zeiträumen ermittelt. </li> <br>
  10690. <a name="allowDeletion"></a>
  10691. <li><b>allowDeletion </b> - schaltet die Löschfunktion des Moduls frei </li> <br>
  10692. <a name="averageCalcForm"></a>
  10693. <li><b>averageCalcForm </b> - legt die Berechnungsvariante für die Ermittlung des Durchschnittswertes mit "averageValue"
  10694. fest.<br><br>
  10695. Zur Zeit sind folgende Varianten implementiert: <br><br>
  10696. <ul>
  10697. <table>
  10698. <colgroup> <col width=20%> <col width=80%> </colgroup>
  10699. <tr><td> <b>avgArithmeticMean :</b> </td><td>es wird der arithmetische Mittelwert berechnet (default) </td></tr>
  10700. <tr><td> <b>avgDailyMeanGWS :</b> </td><td>berechnet die Tagesmitteltemperatur entsprechend den
  10701. Vorschriften des deutschen Wetterdienstes (siehe "helpful hints" mit Funktion get versionNotes). <br>
  10702. Diese Variante verwendet automatisch die Aggregation "day". </td></tr>
  10703. <tr><td> <b>avgTimeWeightMean :</b> </td><td>berechnet den zeitgewichteten Mittelwert </td></tr>
  10704. </table>
  10705. </ul>
  10706. </li><br>
  10707. <a name="device"></a>
  10708. <li><b>device </b> - Abgrenzung der DB-Selektionen auf ein bestimmtes Device. <br>
  10709. Es können Geräte-Spezifikationen (devspec) angegeben werden. <br>
  10710. Innerhalb von Geräte-Spezifikationen wird SQL-Wildcard (%) als normales ASCII-Zeichen gewertet.
  10711. Die Devicenamen werden vor der Selektion aus der Geräte-Spezifikationen und den aktuell in FHEM
  10712. vorhandenen Devices abgeleitet. </li> <br>
  10713. <ul>
  10714. <b>Beispiele:</b> <br>
  10715. <code>attr &lt;name&gt; device TYPE=DbRep</code> <br>
  10716. <code>attr &lt;name&gt; device MySTP_5000</code> <br>
  10717. <code>attr &lt;name&gt; device SMA.*,MySTP.*</code> <br>
  10718. <code>attr &lt;name&gt; device SMA_Energymeter,MySTP_5000</code> <br>
  10719. <code>attr &lt;name&gt; device %5000</code> <br>
  10720. </ul>
  10721. <br>
  10722. <a name="DbRep_device"></a>
  10723. Siehe <a href="#devspec">Geräte-Spezifikationen (devspec)</a>.
  10724. <br><br>
  10725. <a name="diffAccept"></a>
  10726. <li><b>diffAccept </b> - gilt für Funktion diffValue. diffAccept legt fest bis zu welchem Schwellenwert eine berechnete positive Werte-Differenz
  10727. zwischen zwei unmittelbar aufeinander folgenden Datensätzen akzeptiert werden soll (Standard ist 20). <br>
  10728. Damit werden fehlerhafte DB-Einträge mit einem unverhältnismäßig hohen Differenzwert von der Berechnung ausgeschlossen und
  10729. verfälschen nicht das Ergebnis. Sollten Schwellenwertüberschreitungen vorkommen, wird das Reading "diff_overrun_limit_&lt;diffLimit&gt;"
  10730. erstellt. (&lt;diffLimit&gt; wird dabei durch den aktuellen Attributwert ersetzt)
  10731. Es enthält eine Liste der relevanten Wertepaare. Mit verbose 3 werden diese Datensätze ebenfalls im Logfile protokolliert.
  10732. </li> <br>
  10733. <ul>
  10734. Beispiel Ausgabe im Logfile beim Überschreiten von diffAccept=10: <br><br>
  10735. DbRep Rep.STP5000.etotal -> data ignored while calc diffValue due to threshold overrun (diffAccept = 10): <br>
  10736. 2016-04-09 08:50:50 0.0340 -> 2016-04-09 12:42:01 13.3440 <br><br>
  10737. # 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
  10738. Differenzwert. <br>
  10739. # Es ist zu entscheiden ob der Datensatz gelöscht, ignoriert, oder das Attribut diffAccept angepasst werden sollte.
  10740. </ul><br>
  10741. <a name="disable"></a>
  10742. <li><b>disable </b> - deaktiviert das Modul </li> <br>
  10743. <a name="dumpComment"></a>
  10744. <li><b>dumpComment </b> - User-Kommentar. Er wird im Kopf des durch den Befehl "dumpMyQL clientSide" erzeugten Dumpfiles
  10745. eingetragen. </li> <br>
  10746. <a name="dumpCompress"></a>
  10747. <li><b>dumpCompress </b> - wenn gesetzt, werden die Dumpfiles nach "dumpMySQL" bzw. "dumpSQLite" komprimiert </li> <br>
  10748. <a name="dumpDirLocal"></a>
  10749. <li><b>dumpDirLocal </b> - Zielverzeichnis für die Erstellung von Dumps mit "dumpMySQL clientSide".
  10750. default: "{global}{modpath}/log/" auf dem FHEM-Server. <br>
  10751. Ebenfalls werden in diesem Verzeichnis alte Backup-Files durch die interne Versionsverwaltung von
  10752. "dumpMySQL" gesucht und gelöscht wenn die gefundene Anzahl den Attributwert "dumpFilesKeep"
  10753. überschreitet. Das Attribut dient auch dazu ein lokal gemountetes Verzeichnis "dumpDirRemote"
  10754. DbRep bekannt zu machen. </li> <br>
  10755. <a name="dumpDirRemote"></a>
  10756. <li><b>dumpDirRemote </b> - Zielverzeichnis für die Erstellung von Dumps mit "dumpMySQL serverSide".
  10757. default: das Home-Dir des MySQL-Servers auf dem MySQL-Host </li> <br>
  10758. <a name="dumpMemlimit"></a>
  10759. <li><b>dumpMemlimit </b> - erlaubter Speicherverbrauch für das Dump SQL-Script zur Generierungszeit (default: 100000 Zeichen).
  10760. Bitte den Parameter anpassen, falls es zu Speicherengpässen und damit verbundenen Performanceproblemen
  10761. kommen sollte. </li> <br>
  10762. <a name="dumpSpeed"></a>
  10763. <li><b>dumpSpeed </b> - Anzahl der abgerufenen Zeilen aus der Quelldatenbank (default: 10000) pro Select durch "dumpMySQL ClientSide".
  10764. Dieser Parameter hat direkten Einfluß auf die Laufzeit und den Ressourcenverbrauch zur Laufzeit. </li> <br>
  10765. <a name="dumpFilesKeep"></a>
  10766. <li><b>dumpFilesKeep </b> - Es wird die angegebene Anzahl Dumpfiles im Dumpdir belassen (default: 3). Sind mehr (ältere) Dumpfiles
  10767. vorhanden, werden diese gelöscht nachdem ein neuer Dump erfolgreich erstellt wurde. Das globale
  10768. Attribut "archivesort" wird berücksichtigt. </li> <br>
  10769. <a name="executeAfterProc"></a>
  10770. <li><b>executeAfterProc </b> - Es kann ein FHEM-Kommando angegeben werden welches <b>nach dem Dump</b> ausgeführt werden soll. <br>
  10771. Funktionen sind in {} einzuschließen.<br><br>
  10772. <ul>
  10773. <b>Beispiel:</b> <br><br>
  10774. attr &lt;name&gt; executeAfterProc set og_gz_westfenster off; <br>
  10775. attr &lt;name&gt; executeAfterProc {adump ("&lt;name&gt;")} <br><br>
  10776. # "adump" ist eine in 99_myUtils definierte Funktion. <br>
  10777. <pre>
  10778. sub adump {
  10779. my ($name) = @_;
  10780. my $hash = $defs{$name};
  10781. # die eigene Funktion, z.B.
  10782. Log3($name, 3, "DbRep $name -> Dump ist beendet");
  10783. return;
  10784. }
  10785. </pre>
  10786. </ul>
  10787. </li>
  10788. <a name="executeBeforeProc"></a>
  10789. <li><b>executeBeforeProc </b> - Es kann ein FHEM-Kommando angegeben werden welches <b>vor dem Dump</b> ausgeführt werden soll. <br>
  10790. Funktionen sind in {} einzuschließen.<br><br>
  10791. <ul>
  10792. <b>Beispiel:</b> <br><br>
  10793. attr &lt;name&gt; executeBeforeProc set og_gz_westfenster on; <br>
  10794. attr &lt;name&gt; executeBeforeProc {bdump ("&lt;name&gt;")} <br><br>
  10795. # "bdump" ist eine in 99_myUtils definierte Funktion. <br>
  10796. <pre>
  10797. sub bdump {
  10798. my ($name) = @_;
  10799. my $hash = $defs{$name};
  10800. # die eigene Funktion, z.B.
  10801. Log3($name, 3, "DbRep $name -> Dump startet");
  10802. return;
  10803. }
  10804. </pre>
  10805. </ul>
  10806. </li>
  10807. <a name="expimpfile"></a>
  10808. <li><b>expimpfile </b> - Pfad/Dateiname für Export/Import in/aus einem File. <br><br>
  10809. Der Dateiname kann Platzhalter enthalten die gemäß der nachfolgenden Tabelle ersetzt werden.
  10810. Weiterhin können %-wildcards der POSIX strftime-Funktion des darunterliegenden OS enthalten
  10811. sein (siehe auch strftime Beschreibung). <br>
  10812. <br>
  10813. <ul>
  10814. <table>
  10815. <colgroup> <col width=5%> <col width=95%> </colgroup>
  10816. <tr><td> %L </td><td>: wird ersetzt durch den Wert des global logdir Attributs </td></tr>
  10817. <tr><td> %TSB </td><td>: wird ersetzt durch den (berechneten) Wert des timestamp_begin Attributs </td></tr>
  10818. <tr><td> </td><td> </td></tr>
  10819. <tr><td> </td><td> <b>Allgemein gebräuchliche POSIX-Wildcards sind:</b> </td></tr>
  10820. <tr><td> %d </td><td>: Tag des Monats (01..31) </td></tr>
  10821. <tr><td> %m </td><td>: Monat (01..12) </td></tr>
  10822. <tr><td> %Y </td><td>: Jahr (1970...) </td></tr>
  10823. <tr><td> %w </td><td>: Wochentag (0..6); beginnend mit Sonntag (0) </td></tr>
  10824. <tr><td> %j </td><td>: Tag des Jahres (001..366) </td></tr>
  10825. <tr><td> %U </td><td>: Wochennummer des Jahres, wobei Wochenbeginn = Sonntag (00..53) </td></tr>
  10826. <tr><td> %W </td><td>: Wochennummer des Jahres, wobei Wochenbeginn = Montag (00..53) </td></tr>
  10827. </table>
  10828. </ul>
  10829. </li> <br>
  10830. <ul>
  10831. <b>Beispiele:</b> <br>
  10832. <code>attr &lt;name&gt; expimpfile /sds1/backup/exptest_%TSB.csv </code> <br>
  10833. <code>attr &lt;name&gt; expimpfile /sds1/backup/exptest_%Y-%m-%d.csv </code> <br>
  10834. </ul>
  10835. <br>
  10836. <a name="DbRep_expimpfile"></a>
  10837. Zur POSIX Wildcardverwendung siehe auch die Erläuterungen zu <a href="#FileLog">Filelog</a>.
  10838. <br><br>
  10839. <a name="fetchMarkDuplicates"></a>
  10840. <li><b>fetchMarkDuplicates </b>
  10841. - Markierung von mehrfach vorkommenden Datensätzen im Ergebnis des "fetchrows" Kommandos </li> <br>
  10842. <a name="fetchRoute"></a>
  10843. <li><b>fetchRoute [descent | ascent] </b> - bestimmt die Leserichtung des fetchrows-Befehl. <br><br>
  10844. <ul>
  10845. <b>descent</b> - die Datensätze werden absteigend gelesen (default). Wird
  10846. die durch das Attribut "limit" festgelegte Anzahl der Datensätze
  10847. überschritten, werden die neuesten x Datensätze angezeigt. <br><br>
  10848. <b>ascent</b> - die Datensätze werden aufsteigend gelesen. Wird
  10849. die durch das Attribut "limit" festgelegte Anzahl der Datensätze
  10850. überschritten, werden die ältesten x Datensätze angezeigt. <br>
  10851. </ul>
  10852. </li> <br><br>
  10853. <a name="ftpUse"></a>
  10854. <li><b>ftpUse </b> - FTP Transfer nach einem Dump wird eingeschaltet (ohne SSL Verschlüsselung). Das erzeugte
  10855. Datenbank Backupfile wird non-blocking zum angegebenen FTP-Server (Attribut "ftpServer")
  10856. übertragen. </li> <br>
  10857. <a name="ftpUseSSL"></a>
  10858. <li><b>ftpUseSSL </b> - FTP Transfer mit SSL Verschlüsselung nach einem Dump wird eingeschaltet. Das erzeugte
  10859. Datenbank Backupfile wird non-blocking zum angegebenen FTP-Server (Attribut "ftpServer")
  10860. übertragen. </li> <br>
  10861. <a name="ftpUser"></a>
  10862. <li><b>ftpUser </b> - User zur Anmeldung am FTP-Server nach einem Dump, default: "anonymous". </li> <br>
  10863. <a name="ftpDebug"></a>
  10864. <li><b>ftpDebug </b> - Debugging der FTP Kommunikation zur Fehlersuche. </li> <br>
  10865. <a name="ftpDir"></a>
  10866. <li><b>ftpDir </b> - Verzeichnis des FTP-Servers in welches das File nach einem Dump übertragen werden soll
  10867. (default: "/"). </li> <br>
  10868. <a name="ftpDumpFilesKeep"></a>
  10869. <li><b>ftpDumpFilesKeep </b> - Es wird die angegebene Anzahl Dumpfiles im &lt;ftpDir&gt; belassen (default: 3). Sind mehr
  10870. (ältere) Dumpfiles vorhanden, werden diese gelöscht nachdem ein neuer Dump erfolgreich
  10871. übertragen wurde. </li> <br>
  10872. <a name="ftpPassive"></a>
  10873. <li><b>ftpPassive </b> - setzen wenn passives FTP verwendet werden soll </li> <br>
  10874. <a name="ftpPort"></a>
  10875. <li><b>ftpPort </b> - FTP-Port, default: 21 </li> <br>
  10876. <a name="ftpPwd"></a>
  10877. <li><b>ftpPwd </b> - Passwort des FTP-Users, default nicht gesetzt </li> <br>
  10878. <a name="ftpServer"></a>
  10879. <li><b>ftpServer </b> - Name oder IP-Adresse des FTP-Servers zur Übertragung von Files nach einem Dump. </li> <br>
  10880. <a name="ftpTimeout"></a>
  10881. <li><b>ftpTimeout </b> - Timeout für eine FTP-Verbindung in Sekunden (default: 30). </li> <br>
  10882. <a name="limit"></a>
  10883. <li><b>limit </b> - begrenzt die Anzahl der resultierenden Datensätze im select-Statement von "fetchrows", bzw. der anzuzeigenden Datensätze
  10884. der Kommandos "delSeqDoublets adviceDelete", "delSeqDoublets adviceRemain" (default 1000).
  10885. Diese Limitierung soll eine Überlastung der Browsersession und ein
  10886. blockieren von FHEMWEB verhindern. Bei Bedarf entsprechend ändern bzw. die
  10887. Selektionskriterien (Zeitraum der Auswertung) anpassen. </li> <br>
  10888. <a name="optimizeTablesBeforeDump"></a>
  10889. <li><b>optimizeTablesBeforeDump </b> - wenn "1", wird vor dem Datenbankdump eine Tabellenoptimierung ausgeführt (default: 0).
  10890. Dadurch verlängert sich die Laufzeit des Dump. <br><br>
  10891. <ul>
  10892. <b>Hinweis </b> <br>
  10893. Die Tabellenoptimierung führt zur Sperrung der Tabellen und damit zur Blockierung von
  10894. FHEM falls DbLog nicht im asynchronen Modus (DbLog-Attribut "asyncMode") betrieben wird !
  10895. <br>
  10896. </ul>
  10897. </li> <br>
  10898. <a name="reading"></a>
  10899. <li><b>reading </b> - Abgrenzung der DB-Selektionen auf ein bestimmtes oder mehrere Readings.
  10900. Mehrere Readings werden als Komma separierte Liste angegeben. <br>
  10901. SQL Wildcard (%) wird in einer Liste als normales ASCII-Zeichen gewertet. <br>
  10902. </li> <br>
  10903. <ul>
  10904. <b>Beispiele:</b> <br>
  10905. <code>attr &lt;name&gt; reading etotal</code> <br>
  10906. <code>attr &lt;name&gt; reading et%</code> <br>
  10907. <code>attr &lt;name&gt; reading etotal,etoday</code> <br>
  10908. </ul>
  10909. <br><br>
  10910. <a name="readingNameMap"></a>
  10911. <li><b>readingNameMap </b> - der Name des ausgewerteten Readings wird mit diesem String für die Anzeige überschrieben </li> <br>
  10912. <a name="readingPreventFromDel"></a>
  10913. <li><b>readingPreventFromDel </b> - Komma separierte Liste von Readings die vor einer neuen Operation nicht gelöscht
  10914. werden sollen </li> <br>
  10915. <a name="role"></a>
  10916. <li><b>role </b> - die Rolle des DbRep-Device. Standard ist "Client". Die Rolle "Agent" ist im Abschnitt
  10917. "DbRep-Agent" beschrieben. <br>
  10918. <a name="DbRep_role"></a>
  10919. Siehe auch Abschnitt <a href="#DbRepAutoRename">DbRep-Agent</a>.
  10920. </li> <br>
  10921. <a name="seqDoubletsVariance"></a>
  10922. <li><b>seqDoubletsVariance </b> - akzeptierte Abweichung (+/-) für das Kommando "set &lt;name&gt; delSeqDoublets". <br>
  10923. Der Wert des Attributs beschreibt die Abweichung bis zu der aufeinanderfolgende numerische
  10924. Werte (VALUE) von Datensätze als gleich angesehen und gelöscht werden sollen.
  10925. "seqDoubletsVariance" ist ein absoluter Zahlenwert,
  10926. der sowohl als positive als auch negative Abweichung verwendet wird. </li> <br>
  10927. <ul>
  10928. <b>Beispiele:</b> <br>
  10929. <code>attr &lt;name&gt; seqDoubletsVariance 0.0014 </code> <br>
  10930. <code>attr &lt;name&gt; seqDoubletsVariance 1.45 </code> <br>
  10931. </ul>
  10932. <br><br>
  10933. <a name="showproctime"></a>
  10934. <li><b>showproctime </b> - wenn gesetzt, zeigt das Reading "sql_processing_time" die benötigte Abarbeitungszeit (in Sekunden)
  10935. für die SQL-Ausführung der durchgeführten Funktion. Dabei wird nicht ein einzelnes
  10936. SQl-Statement, sondern die Summe aller notwendigen SQL-Abfragen innerhalb der jeweiligen
  10937. Funktion betrachtet. </li> <br>
  10938. <a name="showStatus"></a>
  10939. <li><b>showStatus </b> - grenzt die Ergebnismenge des Befehls "get &lt;name&gt; dbstatus" ein. Es können SQL-Wildcard (%) verwendet werden. </li> <br>
  10940. <ul>
  10941. <b>Bespiel: </b> <br>
  10942. attr &lt;name&gt; showStatus %uptime%,%qcache% <br>
  10943. # Es werden nur Readings erzeugt die im Namen "uptime" und "qcache" enthalten <br>
  10944. </ul><br>
  10945. <a name="showVariables"></a>
  10946. <li><b>showVariables </b> - grenzt die Ergebnismenge des Befehls "get &lt;name&gt; dbvars" ein. Es können SQL-Wildcard (%) verwendet werden. </li> <br>
  10947. <ul>
  10948. <b>Bespiel: </b> <br>
  10949. attr &lt;name&gt; showVariables %version%,%query_cache% <br>
  10950. # Es werden nur Readings erzeugt die im Namen "version" und "query_cache" enthalten <br>
  10951. </ul><br>
  10952. <a name="showSvrInfo"></a>
  10953. <li><b>showSvrInfo </b> - grenzt die Ergebnismenge des Befehls "get &lt;name&gt; svrinfo" ein. Es können SQL-Wildcard (%) verwendet werden. </li> <br>
  10954. <ul>
  10955. <b>Bespiel: </b> <br>
  10956. attr &lt;name&gt; showSvrInfo %SQL_CATALOG_TERM%,%NAME% <br>
  10957. # Es werden nur Readings erzeugt die im Namen "SQL_CATALOG_TERM" und "NAME" enthalten <br>
  10958. </ul><br>
  10959. <a name="showTableInfo"></a>
  10960. <li><b>showTableInfo </b> - grenzt die Ergebnismenge des Befehls "get &lt;name&gt; tableinfo" ein. Es können SQL-Wildcard (%) verwendet werden. </li> <br>
  10961. <ul>
  10962. <b>Bespiel: </b> <br>
  10963. attr &lt;name&gt; showTableInfo current,history <br>
  10964. # Es werden nur Information der Tabellen "current" und "history" angezeigt <br>
  10965. </ul><br>
  10966. <a name="sqlResultFieldSep"></a>
  10967. <li><b>sqlResultFieldSep </b> - legt den verwendeten Feldseparator (default: "|") im Ergebnis des Kommandos
  10968. "set ... sqlCmd" fest. </li> <br>
  10969. <a name="sqlCmdHistoryLength"></a>
  10970. <li><b>sqlCmdHistoryLength </b>
  10971. - aktiviert die Kommandohistorie von "sqlCmd" und legt deren Länge fest </li> <br>
  10972. <a name="sqlResultFormat"></a>
  10973. <li><b>sqlResultFormat </b> - legt die Formatierung des Ergebnisses des Kommandos "set &lt;name&gt; sqlCmd" fest.
  10974. Mögliche Optionen sind: <br><br>
  10975. <ul>
  10976. <b>separated </b> - die Ergebniszeilen werden als einzelne Readings fortlaufend
  10977. generiert. (default)<br><br>
  10978. <b>mline </b> - das Ergebnis wird als Mehrzeiler im Reading
  10979. SqlResult dargestellt. <br><br>
  10980. <b>sline </b> - das Ergebnis wird als Singleline im Reading
  10981. SqlResult dargestellt. Satztrenner ist"]|[". <br><br>
  10982. <b>table </b> - das Ergebnis wird als Tabelle im Reading
  10983. SqlResult dargestellt. <br><br>
  10984. <b>json </b> - erzeugt das Reading SqlResult als
  10985. JSON-kodierten Hash.
  10986. Jedes Hash-Element (Ergebnissatz) setzt sich aus der laufenden Nummer
  10987. des Datensatzes (Key) und dessen Wert zusammen. </li><br><br>
  10988. Die Weiterverarbeitung des Ergebnisses kann z.B. mit der folgenden userExitFn in 99_myUtils.pm erfolgen: <br>
  10989. <pre>
  10990. sub resfromjson {
  10991. my ($name,$reading,$value) = @_;
  10992. my $hash = $defs{$name};
  10993. if ($reading eq "SqlResult") {
  10994. # nur Reading SqlResult enthält JSON-kodierte Daten
  10995. my $data = decode_json($value);
  10996. foreach my $k (keys(%$data)) {
  10997. # ab hier eigene Verarbeitung für jedes Hash-Element
  10998. # z.B. Ausgabe jedes Element welches "Cam" enthält
  10999. my $ke = $data->{$k};
  11000. if($ke =~ m/Cam/i) {
  11001. my ($res1,$res2) = split("\\|", $ke);
  11002. Log3($name, 1, "$name - extract element $k by userExitFn: ".$res1." ".$res2);
  11003. }
  11004. }
  11005. }
  11006. return;
  11007. }
  11008. </pre>
  11009. </ul><br>
  11010. <a name="timeYearPeriod"></a>
  11011. <li><b>timeYearPeriod </b> - Mit Hilfe dieses Attributes wird eine jährliche Zeitperiode für die Datenbankselektion bestimmt.
  11012. Die Zeitgrenzen werden zur Ausführungszeit dynamisch berechnet. Es wird immer eine Jahresperiode
  11013. bestimmt. Eine unterjährige Angabe ist nicht möglich. <br>
  11014. Dieses Attribut ist vor allem dazu gedacht Auswertungen synchron zu einer Abrechnungsperiode, z.B. der eines
  11015. Energie- oder Gaslieferanten, anzufertigen.
  11016. </li> <br>
  11017. <ul>
  11018. <b>Beispiel:</b> <br><br>
  11019. attr &lt;name&gt; timeYearPeriod 06-25 06-24 <br><br>
  11020. # wertet die Datenbank in den Zeitgrenzen 25. Juni AAAA bis 24. Juni BBBB aus. <br>
  11021. # Das Jahr AAAA bzw. BBBB wird in Abhängigkeit des aktuellen Datums errechnet. <br>
  11022. # Ist das aktuelle Datum >= 25. Juni und =< 31. Dezember, dann ist AAAA = aktuelles Jahr und BBBB = aktuelles Jahr+1 <br>
  11023. # Ist das aktuelle Datum >= 01. Januar und =< 24. Juni, dann ist AAAA = aktuelles Jahr-1 und BBBB = aktuelles Jahr
  11024. </ul>
  11025. <br><br>
  11026. <a name="timestamp_begin"></a>
  11027. <li><b>timestamp_begin </b> - der zeitliche Beginn für die Datenselektion (*) </li> <br>
  11028. <a name="timestamp_end"></a>
  11029. <li><b>timestamp_end </b> - das zeitliche Ende für die Datenselektion. Wenn nicht gesetzt wird immer die aktuelle
  11030. Datum/Zeit-Kombi für das Ende der Selektion eingesetzt. (*) </li> <br>
  11031. (*) Das Format von Timestamp ist wie in DbLog "YYYY-MM-DD HH:MM:SS". Für die Attribute "timestamp_begin", "timestamp_end"
  11032. kann ebenso eine der folgenden Eingaben verwendet werden. Dabei wird das timestamp-Attribut dynamisch belegt: <br><br>
  11033. <ul>
  11034. <b>current_year_begin</b> : entspricht "&lt;aktuelles Jahr&gt;-01-01 00:00:00" <br>
  11035. <b>current_year_end</b> : entspricht "&lt;aktuelles Jahr&gt;-12-31 23:59:59" <br>
  11036. <b>previous_year_begin</b> : entspricht "&lt;vorheriges Jahr&gt;-01-01 00:00:00" <br>
  11037. <b>previous_year_end</b> : entspricht "&lt;vorheriges Jahr&gt;-12-31 23:59:59" <br>
  11038. <b>current_month_begin</b> : entspricht "&lt;aktueller Monat erster Tag&gt; 00:00:00" <br>
  11039. <b>current_month_end</b> : entspricht "&lt;aktueller Monat letzter Tag&gt; 23:59:59" <br>
  11040. <b>previous_month_begin</b> : entspricht "&lt;Vormonat erster Tag&gt; 00:00:00" <br>
  11041. <b>previous_month_end</b> : entspricht "&lt;Vormonat letzter Tag&gt; 23:59:59" <br>
  11042. <b>current_week_begin</b> : entspricht "&lt;erster Tag der akt. Woche&gt; 00:00:00" <br>
  11043. <b>current_week_end</b> : entspricht "&lt;letzter Tag der akt. Woche&gt; 23:59:59" <br>
  11044. <b>previous_week_begin</b> : entspricht "&lt;erster Tag Vorwoche&gt; 00:00:00" <br>
  11045. <b>previous_week_end</b> : entspricht "&lt;letzter Tag Vorwoche&gt; 23:59:59" <br>
  11046. <b>current_day_begin</b> : entspricht "&lt;aktueller Tag&gt; 00:00:00" <br>
  11047. <b>current_day_end</b> : entspricht "&lt;aktueller Tag&gt; 23:59:59" <br>
  11048. <b>previous_day_begin</b> : entspricht "&lt;Vortag&gt; 00:00:00" <br>
  11049. <b>previous_day_end</b> : entspricht "&lt;Vortag&gt; 23:59:59" <br>
  11050. <b>current_hour_begin</b> : entspricht "&lt;aktuelle Stunde&gt;:00:00" <br>
  11051. <b>current_hour_end</b> : entspricht "&lt;aktuelle Stunde&gt;:59:59" <br>
  11052. <b>previous_hour_begin</b> : entspricht "&lt;vorherige Stunde&gt;:00:00" <br>
  11053. <b>previous_hour_end</b> : entspricht "&lt;vorherige Stunde&gt;:59:59" <br>
  11054. </ul><br>
  11055. Natürlich sollte man immer darauf achten dass "timestamp_begin" < "timestamp_end" ist. <br><br>
  11056. <ul>
  11057. <b>Beispiel:</b> <br><br>
  11058. attr &lt;name&gt; timestamp_begin current_year_begin <br>
  11059. attr &lt;name&gt; timestamp_end current_year_end <br><br>
  11060. # Wertet die Datenbank in den Zeitgrenzen des aktuellen Jahres aus. <br>
  11061. </ul>
  11062. <br><br>
  11063. <b>Hinweis </b> <br>
  11064. Wird das Attribut "timeDiffToNow" gesetzt, werden die eventuell gesetzten anderen Zeit-Attribute
  11065. ("timestamp_begin","timestamp_end","timeYearPeriod") gelöscht.
  11066. Das Setzen von "timestamp_begin" bzw. "timestamp_end" bedingt die Löschung von anderen Zeit-Attribute falls sie vorher
  11067. gesetzt waren.
  11068. <br><br>
  11069. <a name="timeDiffToNow"></a>
  11070. <li><b>timeDiffToNow </b> - der <b>Selektionsbeginn</b> wird auf den Zeitpunkt <b>"&lt;aktuelle Zeit&gt; - &lt;timeDiffToNow&gt;"</b>
  11071. gesetzt (z.b. werden die letzten 24 Stunden in die Selektion eingehen wenn das Attribut auf "86400" gesetzt
  11072. wurde). Die Timestampermittlung erfolgt dynamisch zum Ausführungszeitpunkt. </li> <br>
  11073. <ul>
  11074. <b>Eingabeformat Beispiel:</b> <br>
  11075. <code>attr &lt;name&gt; timeDiffToNow 86400</code> <br>
  11076. # die Startzeit wird auf "aktuelle Zeit - 86400 Sekunden" gesetzt <br>
  11077. <code>attr &lt;name&gt; timeDiffToNow d:2 h:3 m:2 s:10</code> <br>
  11078. # die Startzeit wird auf "aktuelle Zeit - 2 Tage 3 Stunden 2 Minuten 10 Sekunden" gesetzt <br>
  11079. <code>attr &lt;name&gt; timeDiffToNow m:600</code> <br>
  11080. # die Startzeit wird auf "aktuelle Zeit - 600 Minuten" gesetzt <br>
  11081. <code>attr &lt;name&gt; timeDiffToNow h:2.5</code> <br>
  11082. # die Startzeit wird auf "aktuelle Zeit - 2,5 Stunden" gesetzt <br>
  11083. <code>attr &lt;name&gt; timeDiffToNow y:1 h:2.5</code> <br>
  11084. # die Startzeit wird auf "aktuelle Zeit - 1 Jahr und 2,5 Stunden" gesetzt <br>
  11085. <code>attr &lt;name&gt; timeDiffToNow y:1.5</code> <br>
  11086. # die Startzeit wird auf "aktuelle Zeit - 1,5 Jahre gesetzt <br>
  11087. </ul>
  11088. <br><br>
  11089. <a name="timeOlderThan"></a>
  11090. <li><b>timeOlderThan </b> - das <b>Selektionsende</b> wird auf den Zeitpunkt <b>"&lt;aktuelle Zeit&gt; - &lt;timeOlderThan&gt;"</b>
  11091. gesetzt. Dadurch werden alle Datensätze bis zu dem Zeitpunkt "&lt;aktuelle
  11092. Zeit&gt; - &lt;timeOlderThan&gt;" berücksichtigt (z.b. wenn auf 86400 gesetzt, werden alle
  11093. Datensätze die älter als ein Tag sind berücksichtigt). Die Timestampermittlung erfolgt
  11094. dynamisch zum Ausführungszeitpunkt. <br>
  11095. Es gelten die gleichen Eingabeformate wie für das Attribut "timeDiffToNow". </li> <br>
  11096. <a name="timeout"></a>
  11097. <li><b>timeout </b> - das Attribut setzt den Timeout-Wert für die Blocking-Call Routinen in Sekunden
  11098. (Default: 86400) </li> <br>
  11099. <a name="userExitFn"></a>
  11100. <li><b>userExitFn </b> - stellt eine Schnittstelle zur Ausführung eigenen Usercodes zur Verfügung. <br>
  11101. Um die Schnittstelle zu aktivieren, wird zunächst die aufzurufende Subroutine in
  11102. 99_myUtls.pm nach folgendem Muster erstellt: <br>
  11103. <pre>
  11104. sub UserFunction {
  11105. my ($name,$reading,$value) = @_;
  11106. my $hash = $defs{$name};
  11107. ...
  11108. # z.B. übergebene Daten loggen
  11109. Log3 $name, 1, "UserExitFn $name called - transfer parameter are Reading: $reading, Value: $value " ;
  11110. ...
  11111. return;
  11112. }
  11113. </pre>
  11114. Die Aktivierung der Schnittstelle erfogt durch Setzen des Funktionsnamens im Attribut.
  11115. Optional kann ein Reading:Value Regex als Argument angegeben werden. Wird kein Regex
  11116. angegeben, werden alle Wertekombinationen als "wahr" gewertet (entspricht .*:.*).
  11117. <br><br>
  11118. <ul>
  11119. <b>Beispiel:</b> <br>
  11120. attr <device> userExitFn UserFunction .*:.* <br>
  11121. # "UserFunction" ist die Subroutine in 99_myUtils.pm.
  11122. </ul>
  11123. <br>
  11124. Grundsätzlich arbeitet die Schnittstelle OHNE Eventgenerierung bzw. benötigt zur Funktion keinen
  11125. Event. Sofern das Attribut gesetzt ist, erfolgt Die Regexprüfung NACH der Erstellung eines
  11126. Readings. Ist die Prüfung WAHR, wird die angegebene Funktion aufgerufen.
  11127. Zur Weiterverarbeitung werden der aufgerufenenen Funktion folgende Variablen übergeben: <br><br>
  11128. <ul>
  11129. <li>$name - der Name des DbRep-Devices </li>
  11130. <li>$reading - der Namen des erstellen Readings </li>
  11131. <li>$value - der Wert des Readings </li>
  11132. </ul>
  11133. </li>
  11134. <br>
  11135. <br>
  11136. <a name="valueFilter"></a>
  11137. <li><b>valueFilter </b> - Regulärer Ausdruck zur Filterung von Datensätzen innerhalb bestimmter Funktionen. Der
  11138. Regex auf den gesamten selektierten Datensatz (inkl. Device, Reading usw.) angewendet.
  11139. Bitte vergleichen sie die Erläuterungen zu den entsprechenden Set-Kommandos. </li> <br>
  11140. </ul></ul>
  11141. </ul>
  11142. <a name="DbRepReadings"></a>
  11143. <b>Readings</b>
  11144. <br>
  11145. <ul>
  11146. Abhängig von der ausgeführten DB-Operation werden die Ergebnisse in entsprechenden Readings dargestellt. Zu Beginn einer neuen Operation
  11147. werden alle alten Readings einer vorangegangenen Operation gelöscht um den Verbleib unpassender bzw. ungültiger Readings zu vermeiden.
  11148. <br><br>
  11149. Zusätzlich werden folgende Readings erzeugt (Auswahl): <br><br>
  11150. <ul><ul>
  11151. <li><b>state </b> - enthält den aktuellen Status der Auswertung. Wenn Warnungen auftraten (state = Warning) vergleiche Readings
  11152. "diff_overrun_limit_&lt;diffLimit&gt;" und "less_data_in_period" </li> <br>
  11153. <li><b>errortext </b> - Grund eines Fehlerstatus </li> <br>
  11154. <li><b>background_processing_time </b> - die gesamte Prozesszeit die im Hintergrund/Blockingcall verbraucht wird </li> <br>
  11155. <li><b>diff_overrun_limit_&lt;diffLimit&gt;</b> - enthält eine Liste der Wertepaare die eine durch das Attribut "diffAccept" festgelegte Differenz
  11156. &lt;diffLimit&gt; (Standard: 20) überschreiten. Gilt für Funktion "diffValue". </li> <br>
  11157. <li><b>less_data_in_period </b> - enthält eine Liste der Zeitperioden in denen nur ein einziger Datensatz gefunden wurde. Die
  11158. Differenzberechnung berücksichtigt den letzten Wert der Vorperiode. Gilt für Funktion "diffValue". </li> <br>
  11159. <li><b>sql_processing_time </b> - der Anteil der Prozesszeit die für alle SQL-Statements der ausgeführten
  11160. Operation verbraucht wird </li> <br>
  11161. <li><b>SqlResult </b> - Ergebnis des letzten sqlCmd-Kommandos. Die Formatierung erfolgt entsprechend
  11162. des <a href="#DbRepattr">Attributes</a> "sqlResultFormat" </li> <br>
  11163. <li><b>sqlCmd </b> - das letzte ausgeführte sqlCmd-Kommando </li> <br>
  11164. </ul></ul>
  11165. <br>
  11166. </ul>
  11167. <a name="DbRepAutoRename"></a>
  11168. <b>DbRep Agent - automatisches Ändern von Device-Namen in Datenbanken und DbRep-Definitionen nach FHEM "rename" Kommando</b>
  11169. <br>
  11170. <ul>
  11171. 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
  11172. veranlasst auf Umbenennungen von Geräten in der FHEM Installation zu reagieren. <br><br>
  11173. Durch den DbRep-Agenten werden folgende Features aktiviert wenn ein Gerät in FHEM mit "rename" umbenannt wird: <br><br>
  11174. <ul><ul>
  11175. <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
  11176. <b>allen</b> betroffenen Datensätzen in den neuen Namen geändert. </li> <br>
  11177. <li> in dem DbRep-Agenten zugeordneten DbLog-Device wird in der Definition das alte durch das umbenannte Device ersetzt. Dadurch erfolgt ein weiteres Logging
  11178. des umbenannten Device in der Datenbank. </li> <br>
  11179. <li> in den existierenden DbRep-Definitionen vom Typ "Client" wird ein evtl. gesetztes Attribut "device = alter Devicename" in "device = neuer Devicename"
  11180. geändert. Dadurch werden Auswertungsdefinitionen bei Geräteumbenennungen automatisch konstistent gehalten. </li> <br>
  11181. </ul></ul>
  11182. Mit der Änderung in einen Agenten sind folgende Restriktionen verbunden die mit dem Setzen des Attributes "role = Agent" eingeschaltet
  11183. und geprüft werden: <br><br>
  11184. <ul><ul>
  11185. <li> es kann nur einen Agenten pro Datenbank in der FHEM-Installation geben. Ist mehr als eine Datenbank mit DbLog definiert, können
  11186. ebenso viele DbRep-Agenten eingerichtet werden </li> <br>
  11187. <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
  11188. Attributen zugelassen. Wird ein DbRep-Device vom bisherigen Typ "Client" in einen Agenten geändert, werden evtl. gesetzte und nun nicht mehr zugelassene
  11189. Attribute glöscht. </li> <br>
  11190. </ul></ul>
  11191. Die Aktivitäten wie Datenbankänderungen bzw. Änderungen an anderen DbRep-Definitionen werden im Logfile mit verbose=3 protokolliert. Damit die renameDevice-Funktion
  11192. bei großen Datenbanken nicht in ein timeout läuft, sollte das Attribut "timeout" entsprechend dimensioniert werden. Wie alle Datenbankoperationen des Moduls
  11193. wird auch das Autorename nonblocking ausgeführt. <br><br>
  11194. <ul>
  11195. <b>Beispiel </b> für die Definition eines DbRep-Device als Agent: <br><br>
  11196. <code>
  11197. define Rep.Agent DbRep LogDB <br>
  11198. attr Rep.Agent devStateIcon connected:10px-kreis-gelb .*disconnect:10px-kreis-rot .*done:10px-kreis-gruen <br>
  11199. attr Rep.Agent icon security <br>
  11200. attr Rep.Agent role Agent <br>
  11201. attr Rep.Agent room DbLog <br>
  11202. attr Rep.Agent showproctime 1 <br>
  11203. 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>
  11204. attr Rep.Agent timeout 86400 <br>
  11205. </code>
  11206. <br>
  11207. </ul>
  11208. <b>Hinweis:</b> <br>
  11209. Obwohl die Funktion selbst non-blocking ausgelegt ist, sollte das zugeordnete DbLog-Device
  11210. im asynchronen Modus betrieben werden um ein Blockieren von FHEMWEB zu vermeiden (Tabellen-Lock). <br><br>
  11211. </ul>
  11212. =end html_DE
  11213. =cu