From 7cdf798089f61db30d1cdddd3599566fa3feb060 Mon Sep 17 00:00:00 2001 From: betateilchen Date: Thu, 21 Dec 2017 17:12:46 +0000 Subject: [PATCH] 55_bshGezeiten.pm: updated git-svn-id: https://svn.fhem.de/fhem/trunk@15663 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/contrib/betateilchen/55_bshGezeiten.pm | 205 ++++++++++++++++++-- 1 file changed, 192 insertions(+), 13 deletions(-) diff --git a/fhem/contrib/betateilchen/55_bshGezeiten.pm b/fhem/contrib/betateilchen/55_bshGezeiten.pm index b5d7c24e4..b4e691209 100644 --- a/fhem/contrib/betateilchen/55_bshGezeiten.pm +++ b/fhem/contrib/betateilchen/55_bshGezeiten.pm @@ -1,22 +1,55 @@ ############################################## # $Id$ + +=for comment +############################################## +# +# 55_bshGezeiten.pm written by betateilchen +# +############################################## +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +# +############################################## +=cut + package main; use strict; use warnings; +use feature qw/switch/; + use HTML::TreeBuilder::XPath; sub bshGezeiten_Initialize($) { my ($hash) = @_; -# $hash->{SetFn} = "bshG_Set"; $hash->{DefFn} = "bshG_Define"; $hash->{UndefFn} = "bshG_Undefine"; - $hash->{AttrList} = "bsh_skipOutdated:1,0 " . + $hash->{AttrFn} = "bshG_Attr"; + $hash->{AttrList} = "bsh_language:en,de ". + "bsh_numEntries ". + "bsh_skipOutdated:1,0 " . + "bsh_timeZone:legal,UTC,MEZ ". + "disabled:0,1 ". $readingFnAttributes; } -################################### +#http://www.bsh.de/cgi-bin/gezeiten/was_tab_e.pl?ort=DE__743P&zone=Legal+time+%B9&niveau=KN sub bshG_Define($$) { my ($hash, $def) = @_; @@ -36,13 +69,56 @@ sub bshG_Undefine($) { RemoveInternalTimer($hash); return; } - + +sub bshG_Attr(@) { + my @a = @_; + my $hash = $defs{$a[1]}; + my ($cmd, $name, $attrName, $attrValue) = @a; + $attrValue //= ''; + + given($attrName){ + when("bsh_timeZone"){ + if ($cmd eq 'del'){ + delete $attr{$name}{$attrName}; + $attrValue = "Gesetzliche+Zeit"; + } else { + $attrValue = "Gesetzliche+Zeit" if ($attrValue eq 'legal'); + } + $hash->{'.url'} =~ s/&zone=.*&/&zone=$attrValue&/; + RemoveInternalTimer($hash,'_bsh_pegel'); + _bsh_pegel($hash); + return; + } + + when("bsh_numEntries"){ + CommandDeleteReading(undef,"$name tidalInfo.*"); + } + + when("bsh_skipOutdated"){ + CommandDeleteReading(undef,"$name tidalInfo.*"); + } + + default {$attr{$name}{$attrName} = $attrValue;} + } + + RemoveInternalTimer($hash,'_bsh_decode'); + RemoveInternalTimer($hash,'_bsh_pegel'); + if(IsDisabled($name)) { + readingsSingleUpdate($hash, 'state', 'disabled', 0); + } else { + readingsSingleUpdate($hash, 'state', 'active', 0); +# InternalTimer(gettimeofday()+1, "_bsh_pegel", $hash, 0); + _bsh_pegel($hash); + } + return; +} + +# -------------------------------------------------- + sub _bsh_pegel($) { my ($hash) = @_; my $name = $hash->{NAME}; -# my $next = computeAlignTime("01:00", "00:01"); -# $hash->{TRIGGERTIME} = $next; -# $hash->{TRIGGERTIME_FMT} = FmtDateTime($next); + Log3($hash,4,"$name: starting data retrieval"); InternalTimer(gettimeofday()+3600, "_bsh_pegel", $hash, 0); HttpUtils_NonblockingGet({ hash => $hash, @@ -63,13 +139,14 @@ sub _bsh_pegel_cb($){ sub _bsh_decode($) { my ($hash) = @_; my $name = $hash->{NAME}; + Log3($hash,4,"$name: starting data decoder"); RemoveInternalTimer($hash,'_bsh_decode'); InternalTimer(gettimeofday()+60, "_bsh_decode", $hash, 0); - + my $tree= HTML::TreeBuilder::XPath->new; $tree->parse($hash->{'.content'}); my @ort = $tree->findvalues(q{//strong}); - my (undef,undef,$ort) = split(/ /,$ort[1],3); + my $ort = (split(/ /,$ort[1],3))[2]; $ort = latin1ToUtf8($ort); my @values = $tree->findvalues(q{//table//tr}); my $counter = 0; @@ -80,6 +157,7 @@ sub _bsh_decode($) { #Sa16.12.HW03:09 4.0�m my $d = substr($v,2,6); my $w = substr($v,8,2); + $w =~ s/NW/LW/ if (lc(AttrVal($name,'bsh_language',"en")) eq 'en'); my $t = substr($v,10,5); my $h = substr($v,15,4); if (AttrVal($name,'bsh_skipOutdated',1)) { @@ -88,14 +166,115 @@ sub _bsh_decode($) { my $x = time_str2num "$year-$month1-$day1 $hour1:$min1:00"; next if time > $x; } - $counter++; - readingsBulkUpdate($hash, sprintf('Meldung.%02s',$counter), "$w $d $t $h"); - last if ($counter == 8); + $counter++; + readingsBulkUpdate($hash, sprintf('tidalInfo.%02s',$counter), "$w $d $t $h"); + last if ($counter == AttrVal($name,'bsh_numEntries',8)); } - readingsBulkUpdate($hash,'Pegelname',$ort); + readingsBulkUpdateIfChanged($hash,'stationName',$ort); readingsBulkUpdate($hash,'state','active'); readingsEndUpdate($hash,1); } 1; + +# commandref documentation +=pod +=item device +=item summary show times of high and low water +=item summary_DE Gezeitenvorhersage des BSH +=begin html + + +

bshGezeiten

+ + +=end html +=begin html_DE + + +

bshGezeiten

+ +=end html_DE +=cut +