diff --git a/fhem/FHEM/72_FB_CALLLIST.pm b/fhem/FHEM/72_FB_CALLLIST.pm index d6bd1b382..f2e17236c 100755 --- a/fhem/FHEM/72_FB_CALLLIST.pm +++ b/fhem/FHEM/72_FB_CALLLIST.pm @@ -749,20 +749,70 @@ sub FB_CALLLIST_makeTable($$$$) } ##################################### -# creating the call list as html string or json array -sub FB_CALLLIST_list2html($;$) +# get formated list items to display +sub FB_CALLLIST_getListItems($) { - my ($hash, $to_json) = @_; + my ($hash) = @_; + + my $name = $hash->{NAME}; + my @result; + + if(exists($hash->{helper}{DATA}) and (scalar keys %{$hash->{helper}{DATA}}) > 0) + { + my $count = 0; + + my @list = sort { (AttrVal($name, "list-order","descending") eq "descending") ? $b <=> $a : $a <=> $b } keys %{$hash->{helper}{DATA}}; + + if(AttrVal($hash->{NAME}, "list-type", "all") eq "missed-calls") + { + @list = grep { !$hash->{helper}{DATA}{$_}{running_call} } @list; + } + + if(AttrVal($hash->{NAME}, "list-type", "all") eq "completed") + { + @list = grep { !$hash->{helper}{DATA}{$_}{running_call} } @list; + } + + foreach my $index (@list) + { + $count++; + my $data = \%{$hash->{helper}{DATA}{$index}}; + + my $number = $data->{external_number}; + + my $line = { + index => $index, + line => $count, # internal line identifier for JavaScript, must be present + row => $count, # column "row" to display or not + state => FB_CALLLIST_returnCallState($hash, $index), + timestamp => strftime(AttrVal($name, "time-format-string", "%a, %d %b %Y %H:%M:%S"), localtime($index)), + name => ($data->{external_name} eq "unknown" ? "-" : $data->{external_name}), + number => $number, + external => ($data->{external_connection} ? ((exists($hash->{helper}{EXTERNAL_MAP}) and exists($hash->{helper}{EXTERNAL_MAP}{$data->{external_connection}})) ? $hash->{helper}{EXTERNAL_MAP}{$data->{external_connection}} : $data->{external_connection} ) : "-"), + internal => ((exists($hash->{helper}{INTERNAL_FILTER}) and exists($hash->{helper}{INTERNAL_FILTER}{$data->{internal_number}})) ? $hash->{helper}{INTERNAL_FILTER}{$data->{internal_number}} : $data->{internal_number} ), + connection => ($data->{internal_connection} ? ((exists($hash->{helper}{CONNECTION_MAP}) and exists($hash->{helper}{CONNECTION_MAP}{$data->{internal_connection}})) ? $hash->{helper}{CONNECTION_MAP}{$data->{internal_connection}} : $data->{internal_connection} ) : "-"), + duration => FB_CALLLIST_formatDuration($hash, $index) + }; + + push @result, $line; + } + } + + return @result; +} + +##################################### +# creating the call list as html string +sub FB_CALLLIST_list2html($) +{ + my ($hash) = @_; return undef if( !$hash ); my $name = $hash->{NAME}; my $alias = AttrVal($hash->{NAME}, "alias", $hash->{NAME}); - my $create_readings = AttrVal($hash->{NAME}, "create-readings","0"); - my $count = 0; my $td_style = 'style="padding-left:6px;padding-right:6px;"'; - my @json_output = (); my $line; my $old_locale = setlocale(LC_ALL); @@ -791,57 +841,20 @@ sub FB_CALLLIST_list2html($;$) $ret .= FB_CALLLIST_returnOrderedHTMLOutput($hash, FB_CALLLIST_returnTableHeader($hash), 'class="fbcalllist header"','') if(AttrVal($name, "no-table-header", "0") eq "0"); - readingsBeginUpdate($hash) if($to_json and $create_readings eq "1"); + my @item_list = FB_CALLLIST_getListItems($hash); - if(exists($hash->{helper}{DATA}) and (scalar keys %{$hash->{helper}{DATA}}) > 0) - { - my @json_list; - - my @list = sort { (AttrVal($name, "list-order","descending") eq "descending") ? $b <=> $a : $a <=> $b } keys %{$hash->{helper}{DATA}}; - - if(AttrVal($hash->{NAME}, "list-type", "all") eq "missed-calls") + if(@item_list > 0) + { + foreach $line (@item_list) { - @list = grep { !$hash->{helper}{DATA}{$_}{running_call} } @list; - } - - if(AttrVal($hash->{NAME}, "list-type", "all") eq "completed") - { - @list = grep { !$hash->{helper}{DATA}{$_}{running_call} } @list; - } - - foreach my $index (@list) - { - $count++; - my $data = \%{$hash->{helper}{DATA}{$index}}; - - my $number = $data->{external_number}; - - $line = { - index => $index, - line => $count, - row => $count, - state => FB_CALLLIST_returnCallState($hash, $index), - timestamp => strftime(AttrVal($name, "time-format-string", "%a, %d %b %Y %H:%M:%S"), localtime($index)), - name => ($data->{external_name} eq "unknown" ? "-" : $data->{external_name}), - number => $number, - external => ($data->{external_connection} ? ((exists($hash->{helper}{EXTERNAL_MAP}) and exists($hash->{helper}{EXTERNAL_MAP}{$data->{external_connection}})) ? $hash->{helper}{EXTERNAL_MAP}{$data->{external_connection}} : $data->{external_connection} ) : "-"), - internal => ((exists($hash->{helper}{INTERNAL_FILTER}) and exists($hash->{helper}{INTERNAL_FILTER}{$data->{internal_number}})) ? $hash->{helper}{INTERNAL_FILTER}{$data->{internal_number}} : $data->{internal_number} ), - connection => ($data->{internal_connection} ? ((exists($hash->{helper}{CONNECTION_MAP}) and exists($hash->{helper}{CONNECTION_MAP}{$data->{internal_connection}})) ? $hash->{helper}{CONNECTION_MAP}{$data->{internal_connection}} : $data->{internal_connection} ) : "-"), - duration => FB_CALLLIST_formatDuration($hash, $index) - }; - - FB_CALLLIST_updateReadings($hash, $line) if($to_json and $create_readings eq "1"); - if(defined(my $cmd = AttrVal($name, "number-cmd", undef))) { - $cmd =~ s/\$NUMBER/$number/g; + $cmd =~ s/\$NUMBER/$line->{number}/g; - $line->{number} = ''.$number.""; + $line->{number} = ''.$line->{number}.""; } - - push @json_output, FB_CALLLIST_returnOrderedJSONOutput($hash, $line); - $ret .= FB_CALLLIST_returnOrderedHTMLOutput($hash, $line, 'number="'.$count.'" class="fbcalllist '.($count % 2 == 1 ? "odd" : "even").'"', 'class="fbcalllist" '.$td_style); + $ret .= FB_CALLLIST_returnOrderedHTMLOutput($hash, $line, 'number="'.$line->{line}.'" class="fbcalllist '.($line->{line} % 2 == 1 ? "odd" : "even").'"', 'class="fbcalllist" '.$td_style); } } else @@ -865,29 +878,82 @@ sub FB_CALLLIST_list2html($;$) $ret .= ""; $ret .= ""; + + setlocale(LC_ALL, $old_locale); + + + return $ret; +} + + +##################################### +# creating the call list as json array +sub FB_CALLLIST_list2json($) +{ + my ($hash) = @_; + + return undef if( !$hash ); + + my $name = $hash->{NAME}; + my $create_readings = AttrVal($hash->{NAME}, "create-readings","0"); + my @json_output = (); + + my $old_locale = setlocale(LC_ALL); + + if(AttrVal($name, "language", "en") eq "de") + { + setlocale(LC_ALL, "de_DE.utf8"); + } + else + { + setlocale(LC_ALL, "en_US.utf8"); + } + + my @item_list = FB_CALLLIST_getListItems($hash); + + readingsBeginUpdate($hash) if($create_readings eq "1"); + + if(@item_list > 0) + { + foreach my $line (@item_list) + { + FB_CALLLIST_updateReadings($hash, $line) if($create_readings eq "1"); + + if(defined(my $cmd = AttrVal($name, "number-cmd", undef))) + { + $cmd =~ s/\$NUMBER/$line->{number}/g; + + $line->{number} = ''.$line->{number}.""; + } + + push @json_output, FB_CALLLIST_returnOrderedJSONOutput($hash, $line); + } + } + setlocale(LC_ALL, $old_locale); # delete old readings - if($to_json and $create_readings eq "1") + if($create_readings eq "1") { - readingsBulkUpdate($hash, "numberOfCalls", $count, 1); + readingsBulkUpdate($hash, "numberOfCalls", scalar @item_list, 1); my @delete_readings; - for my $reading (grep { /^(\d+)-/ and ($1 > $count) } keys %{$hash->{READINGS}}) + for my $reading (grep { /^(\d+)-/ and ($1 > @item_list) } keys %{$hash->{READINGS}}) { readingsBulkUpdate($hash, $reading, ""); push @delete_readings, $reading; } - readingsEndUpdate($hash, 1) if($to_json and $create_readings eq "1"); + readingsEndUpdate($hash, 1) if($create_readings eq "1"); map { delete($hash->{READINGS}{$_}) } @delete_readings; } - return ($to_json ? @json_output : $ret); + return @json_output; } + ##################################### # format duration in seconds into hh:mm:ss sub FB_CALLLIST_formatDuration($$) @@ -1121,7 +1187,7 @@ sub FB_CALLLIST_updateFhemWebClients($) return undef unless($init_done); - if(my @list = FB_CALLLIST_list2html($hash, 1)) + if(my @list = FB_CALLLIST_list2json($hash)) { Log3 $name, 5, "FB_CALLLIST ($name) - inform all FHEMWEB clients";