Plugin.pm 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package Plugins::SqueezeESP32::Plugin;
  2. use strict;
  3. use base qw(Slim::Plugin::Base);
  4. use Slim::Utils::Prefs;
  5. use Slim::Utils::Log;
  6. use Slim::Web::ImageProxy;
  7. my $prefs = preferences('plugin.squeezeesp32');
  8. my $log = Slim::Utils::Log->addLogCategory({
  9. 'category' => 'plugin.squeezeesp32',
  10. 'defaultLevel' => 'INFO',
  11. 'description' => Slim::Utils::Strings::string('SqueezeESP32'),
  12. });
  13. # migrate 'eq' pref, as that's a reserved word and could cause problems in the future
  14. $prefs->migrateClient(1, sub {
  15. my ($cprefs, $client) = @_;
  16. $cprefs->set('equalizer', $cprefs->get('eq'));
  17. $cprefs->remove('eq');
  18. 1;
  19. });
  20. $prefs->setChange(sub {
  21. send_equalizer($_[2]);
  22. }, 'equalizer');
  23. sub initPlugin {
  24. my $class = shift;
  25. if ( main::WEBUI ) {
  26. require Plugins::SqueezeESP32::PlayerSettings;
  27. Plugins::SqueezeESP32::PlayerSettings->new;
  28. # require Plugins::SqueezeESP32::Settings;
  29. # Plugins::SqueezeESP32::Settings->new;
  30. }
  31. $class->SUPER::initPlugin(@_);
  32. Slim::Networking::Slimproto::addPlayerClass($class, 100, 'squeezeesp32', { client => 'Plugins::SqueezeESP32::Player', display => 'Plugins::SqueezeESP32::Graphics' });
  33. main::INFOLOG && $log->is_info && $log->info("Added class 100 for SqueezeESP32");
  34. # Note for some forgetful know-it-all: we need to wrap the callback to make it unique. Otherwise subscriptions would overwrite each other.
  35. Slim::Control::Request::subscribe( sub { onNotification(@_) }, [ ['newmetadata'] ] );
  36. Slim::Control::Request::subscribe( sub { onNotification(@_) }, [ ['playlist'], ['open', 'newsong'] ]);
  37. Slim::Control::Request::subscribe( \&onStopClear, [ ['playlist'], ['stop', 'clear'] ]);
  38. # the custom player class is only initialized if it has a display - thus we need to listen to connect events in order to initializes other player prefs
  39. Slim::Control::Request::subscribe( \&onPlayer,[ ['client'], [ 'new', 'reconnect' ] ] );
  40. }
  41. sub onStopClear {
  42. my $request = shift;
  43. my $client = $request->client || return;
  44. if ($client->isa('Plugins::SqueezeESP32::Player')) {
  45. $client->clear_artwork($request);
  46. }
  47. }
  48. sub onPlayer {
  49. my $request = shift;
  50. my $client = $request->client || return;
  51. if ($client->model eq 'squeezeesp32') {
  52. main::INFOLOG && $log->is_info && $log->info("SqueezeESP player connected: " . $client->id);
  53. $prefs->client($client)->init( {
  54. equalizer => [(0) x 10],
  55. } );
  56. send_equalizer($client);
  57. }
  58. }
  59. sub onNotification {
  60. my $request = shift;
  61. my $client = $request->client || return;
  62. if ($client->isa('Plugins::SqueezeESP32::Player')) {
  63. $client->update_artwork();
  64. }
  65. }
  66. sub send_equalizer {
  67. my ($client) = @_;
  68. if ($client->model eq 'squeezeesp32') {
  69. my $equalizer = $prefs->client($client)->get('equalizer') || [(0) x 10];
  70. my $size = @$equalizer;
  71. my $data = pack("c[$size]", @{$equalizer});
  72. $client->sendFrame( eqlz => \$data );
  73. }
  74. }
  75. 1;