2
0

Plugin.pm 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. use Plugins::SqueezeESP32::FirmwareHelper;
  8. my $prefs = preferences('plugin.squeezeesp32');
  9. my $log = Slim::Utils::Log->addLogCategory({
  10. 'category' => 'plugin.squeezeesp32',
  11. 'defaultLevel' => 'INFO',
  12. 'description' => 'PLUGIN_SQUEEZEESP32',
  13. });
  14. # migrate 'eq' pref, as that's a reserved word and could cause problems in the future
  15. $prefs->migrateClient(1, sub {
  16. my ($cprefs, $client) = @_;
  17. $cprefs->set('equalizer', $cprefs->get('eq'));
  18. $cprefs->remove('eq');
  19. 1;
  20. });
  21. $prefs->migrateClient(2, sub {
  22. my ($cprefs, $client) = @_;
  23. $cprefs->set('artwork', undef) if $cprefs->get('artwork') && ref $cprefs->get('artwork') ne 'HASH';
  24. 1;
  25. });
  26. $prefs->setChange(sub {
  27. $_[2]->send_equalizer;
  28. }, 'equalizer');
  29. sub initPlugin {
  30. my $class = shift;
  31. if ( main::WEBUI ) {
  32. require Plugins::SqueezeESP32::PlayerSettings;
  33. Plugins::SqueezeESP32::PlayerSettings->new;
  34. # require Plugins::SqueezeESP32::Settings;
  35. # Plugins::SqueezeESP32::Settings->new;
  36. }
  37. $class->SUPER::initPlugin(@_);
  38. # no name can be a subset of others due to a bug in addPlayerClass
  39. Slim::Networking::Slimproto::addPlayerClass($class, 100, 'squeezeesp32-basic', { client => 'Plugins::SqueezeESP32::Player', display => 'Plugins::SqueezeESP32::Graphics' });
  40. Slim::Networking::Slimproto::addPlayerClass($class, 101, 'squeezeesp32-graphic', { client => 'Plugins::SqueezeESP32::Player', display => 'Slim::Display::NoDisplay' });
  41. main::INFOLOG && $log->is_info && $log->info("Added class 100 and 101 for SqueezeESP32");
  42. # register a command to set the EQ - without saving the values! Send params as single comma separated list of values
  43. Slim::Control::Request::addDispatch(['squeezeesp32', 'seteq', '_eq'], [1, 0, 0, \&setEQ]);
  44. # Note for some forgetful know-it-all: we need to wrap the callback to make it unique. Otherwise subscriptions would overwrite each other.
  45. Slim::Control::Request::subscribe( sub { onNotification(@_) }, [ ['newmetadata'] ] );
  46. Slim::Control::Request::subscribe( sub { onNotification(@_) }, [ ['playlist'], ['open', 'newsong'] ]);
  47. Slim::Control::Request::subscribe( \&onStopClear, [ ['playlist'], ['stop', 'clear'] ]);
  48. Plugins::SqueezeESP32::FirmwareHelper->init();
  49. }
  50. sub onStopClear {
  51. my $request = shift;
  52. my $client = $request->client || return;
  53. if ($client->isa('Plugins::SqueezeESP32::Player')) {
  54. $client->clear_artwork(0, $request->getRequestString());
  55. }
  56. }
  57. sub onNotification {
  58. my $request = shift;
  59. my $client = $request->client || return;
  60. foreach my $player ($client->syncGroupActiveMembers) {
  61. next unless $player->isa('Plugins::SqueezeESP32::Player');
  62. $player->update_artwork;
  63. }
  64. }
  65. sub setEQ {
  66. my $request = shift;
  67. # check this is the correct command.
  68. if ($request->isNotCommand([['squeezeesp32'],['seteq']])) {
  69. $request->setStatusBadDispatch();
  70. return;
  71. }
  72. # get our parameters
  73. my $client = $request->client();
  74. my @eqParams = split(/,/, $request->getParam('_eq') || '');
  75. for (my $x = 0; $x < 10; $x++) {
  76. $eqParams[$x] ||= 0;
  77. }
  78. $client->send_equalizer(\@eqParams);
  79. }
  80. 1;