diff --git a/fhem/CHANGED b/fhem/CHANGED index 4667a9f10..8d74f20a3 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: 98_MediaList.pm: added sorting setting function, + default sort by filename - feature: 10_pilight_ctrl: support protocol heitech - feature: 91_sequence: add delay and omit name options (Forum #75925) - bugfix: 74_AMADDevice: fix a lot of Bugs diff --git a/fhem/FHEM/98_MediaList.pm b/fhem/FHEM/98_MediaList.pm index cc33b0965..e76cc20c6 100644 --- a/fhem/FHEM/98_MediaList.pm +++ b/fhem/FHEM/98_MediaList.pm @@ -82,6 +82,7 @@ sub MediaList_Initialize($) "Playlist_Del" => { "count" => "1"}, #Arg: TrackNr "Playlist_Empty" => { "count" => "0", "args" => "noArg"}, #Leeren # "Playlist_Drop" => { "count" => "1"} #Loeschen, erst relevant wenn abgespeicherte Playlist + "SortBy" => { "count" => "1", "args" => "File,Title"} ); } @@ -227,6 +228,18 @@ sub MediaList_Set($@) MediaList_OnPlayPressed($hash, $par); } + + # sortiere Playlist nach Kriterien, zb. File, Title, Artist, etc + if($cmd eq "SortBy") { + return "sort criteria required: ". $sets{$cmd}{"args"} if (!$par); + + my $json = ReadingsVal($me, "currentdir_playlist", "[]"); + return "no currentdir_playlist available, please select one" if($json eq "[]"); + + $json = MediaList_playlist_sort($json, $par, "asc"); + readingsSingleUpdate($hash, "currentdir_playlist", $json, 1); + return undef; + } } ########################################################################### @@ -430,6 +443,8 @@ sub MediaList_done_playlistinfo($) { delete($hash->{helper}{RUNNING_PID}); + $playlist = MediaList_playlist_sort($playlist, "File", "asc"); # sortiere Playlist per default nach Dateinamen + my @data = @{JSON::XS->new->decode($playlist)}; for(my $j=0; $j<=$#data; $j++) { $playlistduration += $data[$j]->{Time} @@ -705,6 +720,39 @@ sub MediaList_readingsSingleUpdateByName($$$) { readingsSingleUpdate($defs{$devName}, $readingName, $readingVal, 1); } +#################################### +# die PlayList sortieren, Parameter +# 1. Hash +# 2. SortiTem: Filename, Title +# 3. order: asc, desc +#################################### +sub MediaList_playlist_sort { + my ($json, $SortItem, $order) = @_; + my @t; + my @sortdata; + + my @data = @{JSON::XS->new->decode($json)}; +# Log3 undef, 1, "JSON: ".Dumper($json); +# Log3 undef, 1, "DATA: ".Dumper(@data); + + for(my $j=0; $j<=$#data; $j++) { + push(@t, $data[$j]->{$SortItem}); + } + + @t = sort(@t); + @t = reverse @t if($order eq "desc"); + + for(my $i=0; $i<=$#t; $i++) { + for(my $j=0; $j<=$#data; $j++) { + if($t[$i] eq $data[$j]->{$SortItem}) { + push(@sortdata, $data[$j]); + } + } + } + + return JSON::XS->new->encode(\@sortdata); +} + #################################### # CrawlerRoutine zur Navigation im @@ -795,6 +843,7 @@ sub MediaList_Crawl($$) { Note: this module needs the following additional modules: