mirror of
https://github.com/fhem/fhem-mirror.git
synced 2025-01-31 18:59:33 +00:00
FB_CALLLIST: internal code makeover, split HTML and JSON generation into separate functions
git-svn-id: https://svn.fhem.de/fhem/trunk@13524 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
parent
a16c842ad2
commit
69a1b3f1dc
@ -749,20 +749,70 @@ sub FB_CALLLIST_makeTable($$$$)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#####################################
|
#####################################
|
||||||
# creating the call list as html string or json array
|
# get formated list items to display
|
||||||
sub FB_CALLLIST_list2html($;$)
|
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 );
|
return undef if( !$hash );
|
||||||
|
|
||||||
my $name = $hash->{NAME};
|
my $name = $hash->{NAME};
|
||||||
my $alias = AttrVal($hash->{NAME}, "alias", $hash->{NAME});
|
my $alias = AttrVal($hash->{NAME}, "alias", $hash->{NAME});
|
||||||
|
|
||||||
my $create_readings = AttrVal($hash->{NAME}, "create-readings","0");
|
my $create_readings = AttrVal($hash->{NAME}, "create-readings","0");
|
||||||
my $count = 0;
|
|
||||||
my $td_style = 'style="padding-left:6px;padding-right:6px;"';
|
my $td_style = 'style="padding-left:6px;padding-right:6px;"';
|
||||||
my @json_output = ();
|
|
||||||
my $line;
|
my $line;
|
||||||
|
|
||||||
my $old_locale = setlocale(LC_ALL);
|
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");
|
$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)
|
if(@item_list > 0)
|
||||||
{
|
{
|
||||||
my @json_list;
|
foreach $line (@item_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")
|
|
||||||
{
|
{
|
||||||
@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)))
|
if(defined(my $cmd = AttrVal($name, "number-cmd", undef)))
|
||||||
{
|
{
|
||||||
$cmd =~ s/\$NUMBER/$number/g;
|
$cmd =~ s/\$NUMBER/$line->{number}/g;
|
||||||
|
|
||||||
$line->{number} = '<a href=\'#\' onclick="FW_cmd(FW_root+\'?XHR=1&cmd='.urlEncode($cmd).'\');return false;">'.$number."</a>";
|
$line->{number} = '<a href=\'#\' onclick="FW_cmd(FW_root+\'?XHR=1&cmd='.urlEncode($cmd).'\');return false;">'.$line->{number}."</a>";
|
||||||
}
|
}
|
||||||
|
|
||||||
push @json_output, FB_CALLLIST_returnOrderedJSONOutput($hash, $line);
|
$ret .= FB_CALLLIST_returnOrderedHTMLOutput($hash, $line, 'number="'.$line->{line}.'" class="fbcalllist '.($line->{line} % 2 == 1 ? "odd" : "even").'"', 'class="fbcalllist" '.$td_style);
|
||||||
|
|
||||||
$ret .= FB_CALLLIST_returnOrderedHTMLOutput($hash, $line, 'number="'.$count.'" class="fbcalllist '.($count % 2 == 1 ? "odd" : "even").'"', 'class="fbcalllist" '.$td_style);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -865,29 +878,82 @@ sub FB_CALLLIST_list2html($;$)
|
|||||||
|
|
||||||
$ret .= "</table></div>";
|
$ret .= "</table></div>";
|
||||||
$ret .= "</td></tr></table>";
|
$ret .= "</td></tr></table>";
|
||||||
|
|
||||||
|
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} = '<a href=\'#\' onclick="FW_cmd(FW_root+\'?XHR=1&cmd='.urlEncode($cmd).'\');return false;">'.$line->{number}."</a>";
|
||||||
|
}
|
||||||
|
|
||||||
|
push @json_output, FB_CALLLIST_returnOrderedJSONOutput($hash, $line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
setlocale(LC_ALL, $old_locale);
|
setlocale(LC_ALL, $old_locale);
|
||||||
|
|
||||||
# delete old readings
|
# 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;
|
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, "");
|
readingsBulkUpdate($hash, $reading, "");
|
||||||
push @delete_readings, $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;
|
map { delete($hash->{READINGS}{$_}) } @delete_readings;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ($to_json ? @json_output : $ret);
|
return @json_output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#####################################
|
#####################################
|
||||||
# format duration in seconds into hh:mm:ss
|
# format duration in seconds into hh:mm:ss
|
||||||
sub FB_CALLLIST_formatDuration($$)
|
sub FB_CALLLIST_formatDuration($$)
|
||||||
@ -1121,7 +1187,7 @@ sub FB_CALLLIST_updateFhemWebClients($)
|
|||||||
|
|
||||||
return undef unless($init_done);
|
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";
|
Log3 $name, 5, "FB_CALLLIST ($name) - inform all FHEMWEB clients";
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user