Plugin.pm 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package Plugins::SqueezeESP32::Plugin;
  2. use strict;
  3. use base qw(Slim::Plugin::Base);
  4. use Digest::MD5 qw(md5);
  5. use List::Util qw(min);
  6. use Slim::Utils::Prefs;
  7. use Slim::Utils::Log;
  8. use Slim::Web::ImageProxy;
  9. my $prefs = preferences('plugin.squeezeesp32');
  10. my $log = Slim::Utils::Log->addLogCategory({
  11. 'category' => 'plugin.squeezeesp32',
  12. 'defaultLevel' => 'INFO',
  13. 'description' => Slim::Utils::Strings::string('SqueezeESP32'),
  14. });
  15. sub initPlugin {
  16. my $class = shift;
  17. if ( main::WEBUI ) {
  18. require Plugins::SqueezeESP32::PlayerSettings;
  19. Plugins::SqueezeESP32::PlayerSettings->new;
  20. # require Plugins::SqueezeESP32::Settings;
  21. # Plugins::SqueezeESP32::Settings->new;
  22. }
  23. $class->SUPER::initPlugin(@_);
  24. Slim::Networking::Slimproto::addPlayerClass($class, 100, 'squeezeesp32', { client => 'Plugins::SqueezeESP32::Player', display => 'Plugins::SqueezeESP32::Graphics' });
  25. $log->info("Added class 100 for SqueezeESP32");
  26. Slim::Control::Request::subscribe(\&update_artwork, [ ['newmetadata'] ] );
  27. Slim::Control::Request::subscribe(\&update_artwork, [ ['playlist'], ['open', 'newsong'] ]);
  28. }
  29. sub update_artwork {
  30. my $request = shift;
  31. my $client = $request->client;
  32. my $cprefs = $prefs->client($client);
  33. my $artwork = $cprefs->get('artwork');
  34. return unless $client->model eq 'squeezeesp32' && $artwork->{'enable'};
  35. my $reqstr = $request->getRequestString();
  36. #my $path = $request->getParam('_path');
  37. my $s = $artwork->{'y'} >= 32 ? $cprefs->get('height') - $artwork->{'y'} : 32;
  38. $s = min($s, $cprefs->get('width') - $artwork->{'x'});
  39. my $path = 'music/current/cover_' . $s . 'x' . $s . '_o.jpg';
  40. my $body = Slim::Web::Graphics::artworkRequest($client, $path, undef, \&send_artwork, undef, HTTP::Response->new);
  41. send_artwork($client, undef, \$body) if $body;
  42. $log->info("artwork update notification $reqstr with $path");
  43. }
  44. sub send_artwork {
  45. my ($client, $params, $dataref) = @_;
  46. # I'm not sure why we are called so often, so only send when needed
  47. my $md5 = md5($$dataref);
  48. return if $client->pluginData('artwork_md5') eq $md5;
  49. $client->pluginData('artwork', $dataref);
  50. $client->pluginData('artwork_md5', $md5);
  51. my $artwork = $prefs->client($client)->get('artwork');
  52. my $length = length $$dataref;
  53. my $offset = 0;
  54. $log->info("got resized artwork (length: ", length $$dataref, ")");
  55. my $header = pack('Nnn', $length, $artwork->{'x'}, $artwork->{'y'});
  56. while ($length > 0) {
  57. $length = 1280 if $length > 1280;
  58. $log->info("sending grfa $length");
  59. my $data = $header . pack('N', $offset) . substr( $$dataref, 0, $length, '' );
  60. $client->sendFrame( grfa => \$data );
  61. $offset += $length;
  62. $length = length $$dataref;
  63. }
  64. }
  65. 1;