From 49fa4ce71d6bc8659584917c6b7fabcf90fa0bc0 Mon Sep 17 00:00:00 2001 From: nasseeder1 Date: Fri, 23 Aug 2019 20:08:48 +0000 Subject: [PATCH] 93_DbRep: new attribute fetchValueFn to change displayed values of database field VALUE got by fetchrows command git-svn-id: https://svn.fhem.de/fhem/trunk@20048 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 2 + fhem/FHEM/93_DbRep.pm | 109 +++++++++++++++++++++++++++++++++++------- 2 files changed, 93 insertions(+), 18 deletions(-) diff --git a/fhem/CHANGED b/fhem/CHANGED index ffb0829b4..02fc1a0ad 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -1,5 +1,7 @@ # Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # Do not insert empty lines here, update check depends on it. + - feature: 93_DbRep: new attribute fetchValueFn to change displayed values of + database field VALUE got by fetchrows command - feature: 93_DbLog: new attribute dbSchema to add database schema to tables Forum: #102679 - feature: 76_SMAInverter: V2.13.0, attr target-serial/target-susyid are set diff --git a/fhem/FHEM/93_DbRep.pm b/fhem/FHEM/93_DbRep.pm index 8bffddbf1..7bf2f2d84 100644 --- a/fhem/FHEM/93_DbRep.pm +++ b/fhem/FHEM/93_DbRep.pm @@ -58,6 +58,8 @@ no if $] >= 5.017011, warnings => 'experimental::smartmatch'; # Version History intern our %DbRep_vNotesIntern = ( + "8.22.0" => "23.08.2019 new attr fetchValueFn. When fetching the database content, manipulate the VALUE-field before create reading ", + "8.21.2" => "14.08.2019 commandRef revised ", "8.21.1" => "31.05.2019 syncStandby considers executeBeforeProc, commandRef revised ", "8.21.0" => "28.04.2019 implement FHEM command \"dbReadingsVal\" ", "8.20.1" => "28.04.2019 set index verbose changed, check index \"Report_Idx\" in getInitData ", @@ -153,6 +155,8 @@ our %DbRep_vNotesIntern = ( # Version History extern: our %DbRep_vNotesExtern = ( + "8.22.0" => "23.08.2019 A new attribute \"fetchValueFn\" is provided. When fetching the database content, you are able to manipulate ". + "the value displayed from the VALUE database field before create the appropriate reading. ", "8.21.0" => "28.04.2019 FHEM command \"dbReadingsVal\" implemented.", "8.20.0" => "27.04.2019 With the new set \"index\" command it is now possible to list and (re)create the indexes which are ". "needed for DbLog and/or DbRep operation.", @@ -340,6 +344,7 @@ sub DbRep_Initialize($) { "fastStart:1,0 ". "fetchRoute:ascent,descent ". "fetchMarkDuplicates:red,blue,brown,green,orange ". + "fetchValueFn:textField-long ". "ftpDebug:1,0 ". "ftpDir ". "ftpDumpFilesKeep:1,2,3,4,5,6,7,8,9,10 ". @@ -1143,6 +1148,21 @@ sub DbRep_Attr($$$$) { } } + if ($aName eq "fetchValueFn") { + if($cmd eq "set") { + my $VALUE = "Hello"; + # Funktion aus Attr validieren + if( $aVal =~ m/^\s*(\{.*\})\s*$/s ) { + $aVal = $1; + } else { + $aVal = ""; + } + return "Your function does not match the form \"{}\"" if(!$aVal); + eval $aVal; + return "Bad function: $@" if($@); + } + } + if ($aName eq "sqlCmdHistoryLength") { if($cmd eq "set") { $do = ($aVal) ? 1 : 0; @@ -4874,6 +4894,7 @@ sub fetchrows_ParseDone($) { my $name = $hash->{NAME}; my $reading = AttrVal($name, "reading", undef); my $limit = AttrVal($name, "limit", 1000); + my $fvfn = AttrVal($name, "fetchValueFn", undef); my $color = ""; # Highlighting doppelter DB-Einträge $color =~ s/#// if($color =~ /red|blue|brown|green|orange/); my $ecolor = ""; # Ende Highlighting @@ -4945,6 +4966,19 @@ sub fetchrows_ParseDone($) { $reading_runtime_string = $ts."__".$dz."__".$dev."__".$rea.$zs; } } + + if($fvfn) { + my $VALUE = $val; + if( $fvfn =~ m/^\s*(\{.*\})\s*$/s ) { + $fvfn = $1; + } else { + $fvfn = ""; + } + if ($fvfn) { + eval $fvfn; + $val = $VALUE if(!$@); + } + } ReadingsBulkUpdateValue($hash, $reading_runtime_string, $val); } @@ -9543,13 +9577,11 @@ sub DbRep_delread($;$$) { # Highlighted Readings löschen und save statefile wegen Inkompatibilitär beim Restart if($key =~ / 5.9 und zu lang nach der neuen Festlegung if($do == 0 && $featurelevel > 5.9 && !goodReadingName($key)) { $do = 1; - # delete($defs{$name}{READINGS}{$key}); readingsDelete($hash,$key); } } @@ -11624,7 +11656,8 @@ return; Every reading of result is composed of the dataset timestring , an index, the device name and the reading name. The function has the capability to reconize multiple occuring datasets (doublets). - Such doublets are marked by an index > 1.
+ Such doublets are marked by an index > 1. Optional a Unique-Index is appended if + datasets with identical timestamp, device and reading but different value are existing.
Doublets can be highlighted in terms of color by setting attribut e"fetchMarkDuplicates".

Note:
@@ -11644,8 +11677,8 @@ return;
    Example:
    - 2017-10-22_03-04-43__1__SMA_Energymeter__Bezug_WirkP_Kosten_Diff
    - # <date>_<time>__<index>__<device>__<reading> + 2017-10-22_03-04-43__1__SMA_Energymeter__Bezug_WirkP_Kosten_Diff__[1]
    + # <date>_<time>__<index>__<device>__<reading>__[Unique-Index]

@@ -11654,10 +11687,11 @@ return;
    - - - + + + + @@ -11674,7 +11708,7 @@ return;
  • index <Option> - - Reports the existing indexes in the database or creates the needed indexes. + - Reports the existing indexes in the database or creates the index which is needed. If the index is already created, it will be renewed (dropped and new created)

    The possible options are:

    @@ -11690,6 +11724,9 @@ return;
  • fetchRoute : direction of selection read in database
    limit : limits the number of datasets to select and display
    fetchMarkDuplicates : Highlighting of found doublets
    device : include or exclude <device> from selection
    fetchRoute : direction of selection read in database
    fetchMarkDuplicates : Highlighting of found doublets
    fetchValueFn : the displayed value of the VALUE database field can be changed by a function before the reading is created
    limit : limits the number of datasets to select and display
    reading : include or exclude <reading> from selection
    time.* : A number of attributes to limit selection by time
    valueFilter : an additional REGEXP to control the record selection. The REGEXP is applied to the database field 'VALUE'.

+ + Note:
+ The used database user needs the ALTER and INDEX privilege.

@@ -12679,6 +12716,21 @@ sub bdump {

+ + +
  • fetchValueFn - When fetching the database content, you are able to manipulate the value fetched from the + VALUE database field before create the appropriate reading. You have to insert a Perl + function which is enclosed in {} .
    + The value of the database field VALUE is provided in variable $VALUE.

    + +
      + Example:
      + attr <name> fetchValueFn { $VALUE =~ s/^.*Used:\s(.*)\sMB,.*/$1." MB"/e }
      + + # From a long line a specific pattern is extracted and will be displayed als VALUE instead + the whole line +
    +


  • ftpUse - FTP Transfer after dump will be switched on (without SSL encoding). The created @@ -14037,10 +14089,12 @@ sub bdump { Die Leserichtung in der Datenbank kann durch das Attribut "fetchRoute" bestimmt werden.

    - Jedes Ergebnisreading setzt sich aus dem Timestring des Datensatzes, einem Index, dem Device - und dem Reading zusammen. + Jedes Ergebnisreading setzt sich aus dem Timestring des Datensatzes, einem Dubletten-Index, + dem Device und dem Reading zusammen. Die Funktion fetchrows ist in der Lage, mehrfach vorkommende Datensätze (Dubletten) zu erkennen. - Solche Dubletten sind mit einem Index > 1 gekennzeichnet.
    + Solche Dubletten sind mit einem Dubletten-Index > 1 gekennzeichnet. Optional wird noch ein + Unique-Index angehängt, wenn Datensätze mit identischem Timestamp, Device und Reading aber + unterschiedlichem Value vorhanden sind.
    Dubletten können mit dem Attribut "fetchMarkDuplicates" farblich hervorgehoben werden.

    Hinweis:
    @@ -14062,8 +14116,8 @@ sub bdump {
      Beispiel:
      - 2017-10-22_03-04-43__1__SMA_Energymeter__Bezug_WirkP_Kosten_Diff
      - # <Datum>_<Zeit>__<Index>__<Device>__<Reading> + 2017-10-22_03-04-43__1__SMA_Energymeter__Bezug_WirkP_Kosten_Diff__[1]
      + # <Datum>_<Zeit>__<Dubletten-Index>__<Device>__<Reading>__[Unique-Index]

    @@ -14073,10 +14127,11 @@ sub bdump {
      - - - + + + + @@ -14098,7 +14153,7 @@ sub bdump { - Listet die in der Datenbank vorhandenen Indexe auf bzw. legt die benötigten Indexe an. Ist ein Index bereits angelegt, wird er erneuert (gelöscht und erneut angelegt)

      - Die möglichen Optionen sind:

      + Die möglichen Optionen sind:

      fetchRoute : Leserichtung der Selektion innerhalb der Datenbank
      limit : begrenzt die Anzahl zu selektierenden bzw. anzuzeigenden Datensätze
      fetchMarkDuplicates : Hervorhebung von gefundenen Dubletten
      device : einschließen oder ausschließen von Datensätzen die <device> enthalten
      fetchRoute : Leserichtung der Selektion innerhalb der Datenbank
      fetchMarkDuplicates : Hervorhebung von gefundenen Dubletten
      fetchValueFn : der angezeigte Wert des VALUE Datenbankfeldes kann mit einer Funktion vor der Readingerstellung geändert werden
      limit : begrenzt die Anzahl zu selektierenden bzw. anzuzeigenden Datensätze
      reading : einschließen oder ausschließen von Datensätzen die <reading> enthalten
      time.* : eine Reihe von Attributen zur Zeitabgrenzung
      valueFilter : filtert die anzuzeigenden Datensätze mit einem regulären Ausdruck (Datenbank spezifischer REGEXP). Der REGEXP wird auf Werte des Datenbankfeldes 'VALUE' angewendet.
      @@ -14111,6 +14166,9 @@ sub bdump {

    + + Hinweis:
    + Der verwendete Datenbank-Nutzer benötigt das ALTER und INDEX Privileg.

  • @@ -15117,6 +15175,21 @@ sub bdump {

    + + +
  • fetchValueFn - Der angezeigte Wert des Datenbankfeldes VALUE kann vor der Erstellung des entsprechenden + Readings geändert werden. Das Attribut muss eine Perl Funktion eingeschlossen in {} + enthalten.
    + Der Wert des Datenbankfeldes VALUE wird in der Variable $VALUE zur Verfügung gestellt.

    + +
      + Beispiel:
      + attr <name> fetchValueFn { $VALUE =~ s/^.*Used:\s(.*)\sMB,.*/$1." MB"/e }
      + + # Von einer langen Ausgabe wird ein spezifisches Zeichenmuster extrahiert und als VALUE + anstatt der gesamten Zeile im Reading angezeigt. +
    +


  • ftpUse - FTP Transfer nach einem Dump wird eingeschaltet (ohne SSL Verschlüsselung). Das erzeugte