2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-02-01 01:09:47 +00:00

55_GDS - some code improvements

git-svn-id: https://svn.fhem.de/fhem/trunk@6417 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
betateilchen 2014-08-18 19:03:28 +00:00
parent 5bb558316d
commit f9f71cf54c

View File

@ -35,7 +35,6 @@ use Net::FTP;
use List::MoreUtils 'first_index';
use XML::Simple;
use HttpUtils;
use Blocking;
require LWP::UserAgent;
my ($bulaList, $cmapList, %rmapList, $fmapList, %bula2bulaShort, %bulaShort2dwd, %dwd2Dir, %dwd2Name,
@ -71,6 +70,22 @@ sub GDS_Initialize($) {
fillMappingTables($hash);
initDropdownLists($hash);
createIndexFile($hash);
if($name){
(undef, $found) = retrieveFile($hash,"conditions");
if($found){
$sList = getListStationsDropdown($hash)
} else {
Log3($name, 2, "GDS $name: No datafile (conditions) found");
}
(undef, $found) = retrieveFile($hash,"alerts");
if($found){
($aList, undef) = buildCAPList($hash);
} else {
Log3($name, 2, "GDS $name: No datafile (alerts) found");
}
}
}
sub GDS_Define($$$) {
@ -98,8 +113,18 @@ sub GDS_Define($$$) {
initDropdownLists($hash);
createIndexFile($hash);
BlockingCall("nb_defRead", $hash);
(undef, $found) = retrieveFile($hash,"conditions");
if($found){
$sList = getListStationsDropdown($hash)
} else {
Log3($name, 2, "GDS $name: No datafile (conditions) found");
}
retrieveFile($hash,"alerts");
if($found){
($aList, undef) = buildCAPList($hash);
} else {
Log3($name, 3, "GDS $name: No datafile (alerts) found");
}
readingsSingleUpdate($hash, '_tzOffset', _calctz(time,localtime(time))*3600, 0);
readingsSingleUpdate($hash, 'state', 'active',1);
@ -148,7 +173,14 @@ sub GDS_Set($@) {
}
when("rereadcfg"){
BlockingCall("nb_setReread",$hash);
eval {
retrieveFile($hash,"conditions");
$sList = getListStationsDropdown($hash);
};
eval {
retrieveFile($hash,"alerts");
($aList, undef) = buildCAPList($hash);
};
break;
}
@ -199,26 +231,26 @@ sub GDS_Get($@) {
when("conditionsmap"){
# retrieve map: current conditions
BlockingCall("nb_retrieveFile", "$name|$command|$parameter");
retrieveFile($hash,$command,$parameter);
break;
}
when("forecastsmap"){
# retrieve map: forecasts
BlockingCall("nb_retrieveFile", "$name|$command|$parameter");
retrieveFile($hash,$command,$parameter);
break;
}
when("warningsmap"){
# retrieve map: warnings
BlockingCall("nb_retrieveFile", "$name|$command|$parameter");
retrieveFile($hash,$command,$parameter);
break;
}
when("radarmap"){
# retrieve map: radar
$parameter = ucfirst($parameter);
BlockingCall("nb_retrieveFile", "$name|$command|$parameter|$rmapList{$parameter}");
retrieveFile($hash,$command,$parameter,$rmapList{$parameter});
break;
}
@ -258,7 +290,9 @@ sub GDS_Get($@) {
}
when("rereadcfg"){
BlockingCall("nb_getReread",$hash);
retrieveFile($hash,"conditions");
retrieveFile($hash,"alerts");
initDropdownLists($hash);
break;
}
@ -268,10 +302,12 @@ sub GDS_Get($@) {
" VHDL32 = preliminary | VHDL33 = cancel VHDL32\n".
sepLine(31)."+".sepLine(38);
for ($vhdl=30; $vhdl <=33; $vhdl++){
nb_retrieveFile("$name|$command|$parameter|$vhdl|1");
(undef, $found) = retrieveFile($hash, $command, $parameter, $vhdl,1);
if($found){
$result .= retrieveTextWarn($hash);
$result .= "\n".sepLine(70);
}
}
$result .= "\n\n";
break;
}
@ -324,44 +360,6 @@ sub GDS_GetUpdate($) {
#
####################################################################################################
sub nb_getReread($) {
my ($hash) = @_;
my $name = $hash->{NAME};
Log3($name, 4, "GDS $name: nb_getReread started");
nb_retrieveFile("$name|conditions");
nb_retrieveFile("$name|alerts");
initDropdownLists($hash);
Log3($name, 4, "GDS $name: nb_getReread finished");
return;
}
sub nb_setReread($) {
my ($hash) = @_;
my $name = $hash->{NAME};
Log3($name, 4, "GDS $name: nb_setReread started");
eval {
nb_retrieveFile("$name|conditions");
$sList = getListStationsDropdown($hash);
};
eval {
nb_retrieveFile("$name|alerts");
($aList, undef) = buildCAPList($hash);
};
Log3($name, 4, "GDS $name: nb_setReread finished");
return;
}
sub nb_defRead($) {
my ($hash) = @_;
my $name = $hash->{NAME};
Log3($name, 4, "GDS $name: nb_defRead started");
nb_retrieveFile("$name|conditions");
$sList = getListStationsDropdown($hash);
nb_retrieveFile("$name|alerts");
($aList, undef) = buildCAPList($hash);
Log3($name, 4, "GDS $name: nb_defRead finished");
return;
}
sub getHelp(){
return "Use one of the following commands:\n".
@ -420,7 +418,7 @@ sub getListCapStations($$){
# prüfen, ob CSV schon vorhanden,
# falls nicht: vom Server holen
if (!-e $tempDir."caplist.csv"){
nb_retrieveFile("$name|$command");
retrieveFile($hash, $command);
}
# CSV öffnen und parsen
@ -592,6 +590,7 @@ sub decodeCAPData($$){
readingsBulkUpdate($hash, $k, latin1ToUtf8($v)) if(defined($v)); }
readingsEndUpdate($hash, 1);
eval {readingsSingleUpdate($hash, 'a_eventCode_AREA_COLOR_hex', _rgbd2h(ReadingsVal($name, 'a_eventCode_AREA_COLOR', '')),0);};
return;
}
@ -672,7 +671,7 @@ sub retrieveConditions($$@){
Log3($name, 4, "GDS $name: Retrieving conditions data");
nb_retrieveFile("$name|conditions|||1");
($dataFile, $found) = retrieveFile($hash,"conditions",undef,undef,1);
open WXDATA, $tempDir.$name."_conditions";
while (chomp($line = <WXDATA>)) {
map {s/\r//g;} ($line);
@ -735,12 +734,11 @@ sub retrieveConditions($$@){
return ;
}
sub xxx_retrieveFile($$;$$$){
sub retrieveFile($$;$$$){
#
# request = type, e.g. alerts, conditions, warnings
# parameter = additional selector, e.g. Bundesland
#
Debug "retrieveFile started!";
my ($hash, $request, $parameter, $parameter2, $useFtp) = @_;
my $name = $hash->{NAME};
my $user = $hash->{helper}{USER};
@ -880,153 +878,6 @@ Debug "retrieveFile started!";
return ($dataFile, $found);
}
sub nb_retrieveFile($){
#
# request = type, e.g. alerts, conditions, warnings
# parameter = additional selector, e.g. Bundesland
#
my ($string) = @_;
my ($name, $request, $parameter, $parameter2, $useFtp) = split("\\|",$string);
my $hash = $defs{$name};
my $user = $hash->{helper}{USER};
my $pass = $hash->{helper}{PASS};
my $proxyName = AttrVal($name, "gdsProxyName", "");
my $proxyType = AttrVal($name, "gdsProxyType", "");
my $passive = AttrVal($name, "gdsPassiveFtp", 0);
my $debug = AttrVal($name, "gdsDebug",0);
Log3($name, 4, "GDS $name: nb_retrievFile started");
my ($dwd, $dir, $ftp, @files, $dataFile, $targetFile, $found, $readingName);
my $urlString = "ftp://$user:$pass\@ftp-outgoing2.dwd.de/";
my $ua;
eval { $ua = LWP::UserAgent->new; };
if(!defined($ua)) {
Log3($name, 1, "GDS $name: LWP not available!");
readingsSingleUpdate($hash, 'LWP error', 'LWP not available!',1);
return;
}
$ua->timeout(10);
$ua->env_proxy;
given($request){
when("capstations"){
$dir = "gds/help/";
$dwd = "legend_warnings_CAP_WarnCellsID.csv";
$targetFile = $tempDir.$request.".csv";
break;
}
when("conditionsmap"){
$dir = "gds/specials/observations/maps/germany/";
$dwd = $parameter."*";
$targetFile = $tempDir.$name."_".$request.".jpg";
break;
}
when("forecastsmap"){
$dir = "gds/specials/forecasts/maps/germany/";
$dwd = $parameter."*";
$targetFile = $tempDir.$name."_".$request.".jpg";
break;
}
when("warningsmap"){
if(length($parameter) != 2){
$parameter = $bula2bulaShort{lc($parameter)};
}
$dwd = "Schilder".$dwd2Dir{$bulaShort2dwd{lc($parameter)}}.".jpg";
$dir = "gds/specials/warnings/maps/";
$targetFile = $tempDir.$name."_".$request.".jpg";
break;
}
when("radarmap"){
$dir = "gds/specials/radar/".$parameter2;
$dwd = "Webradar_".$parameter."*";
$targetFile = $tempDir.$name."_".$request.".jpg";
break;
}
when("alerts"){
$dir = "gds/specials/warnings/xml/PVW/";
$dwd = "Z_CAP*";
$targetFile = $tempDir.$name."_".$request;
break;
}
when("conditions"){
$useFtp = 1;
$dir = "gds/specials/observations/tables/germany/";
$dwd = "*";
$targetFile = $tempDir.$name."_".$request;
break;
}
when("warnings"){
$useFtp = 1;
if(length($parameter) != 2){
$parameter = $bula2bulaShort{lc($parameter)};
}
$dwd = $bulaShort2dwd{lc($parameter)};
$dir = $dwd2Dir{$dwd};
$dwd = "VHDL".$parameter2."_".$dwd."*";
$dir = "gds/specials/warnings/".$dir."/";
$targetFile = $tempDir.$name."_".$request;
break;
}
}
Log3($name, 4, "GDS $name: searching for $dir".$dwd." on DWD server");
$urlString .= $dir;
$found = 0;
eval {
$ftp = Net::FTP->new( "ftp-outgoing2.dwd.de",
Debug => 0,
Timeout => 10,
Passive => $passive,
FirewallType => $proxyType,
Firewall => $proxyName);
Log3($name, 4, "GDS $name: ftp connection established.");
if(defined($ftp)){
$ftp->login($user, $pass);
$ftp->cwd("$dir");
@files = undef;
@files = $ftp->ls($dwd);
if(@files){
Log3($name, 4, "GDS $name: filelist found.");
@files = sort(@files);
$dataFile = $files[-1];
$urlString .= $dataFile;
Log3($name, 4, "GDS $name: retrieving $dataFile");
if($useFtp){
Log3($name, 4, "GDS $name: using FTP for retrieval");
$ftp->get($files[-1], $targetFile);
} else {
Log3($name, 4, "GDS $name: using HTTP for retrieval");
$ua->get($urlString,':content_file' => $targetFile);
}
$found = 1;
} else {
Log3($name, 4, "GDS $name: filelist not found.");
$found = 0;
}
$ftp->quit;
}
Log3($name, 4, "GDS $name: updating readings.");
readingsBeginUpdate($hash);
readingsBulkUpdate($hash, "_dataSource", "Quelle: Deutscher Wetterdienst");
readingsBulkUpdate($hash, "_dF_".$request, $dataFile) if(AttrVal($name, "gdsDebug", 0));
readingsEndUpdate($hash, 1);
};
return ($dataFile, $found);
}
sub getListStationsDropdown($){
my ($hash) = @_;
my $name = $hash->{NAME};