Plugin.pm 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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' => 'PLUGIN_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->migrateClient(2, sub {
  21. my ($cprefs, $client) = @_;
  22. $cprefs->set('artwork', undef) if $cprefs->get('artwork') && ref $cprefs->get('artwork') ne 'HASH';
  23. 1;
  24. });
  25. $prefs->setChange(sub {
  26. $_[2]->send_equalizer;
  27. }, 'equalizer');
  28. sub initPlugin {
  29. my $class = shift;
  30. # enable the following to test the firmware downloading code without a SqueezeliteESP32 player
  31. # require Plugins::SqueezeESP32::FirmwareHelper;
  32. # Plugins::SqueezeESP32::FirmwareHelper::init();
  33. if ( main::WEBUI ) {
  34. require Plugins::SqueezeESP32::PlayerSettings;
  35. Plugins::SqueezeESP32::PlayerSettings->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. }
  49. sub onStopClear {
  50. my $request = shift;
  51. my $client = $request->client || return;
  52. if ($client->isa('Plugins::SqueezeESP32::Player')) {
  53. $client->clear_artwork(0, $request->getRequestString());
  54. }
  55. }
  56. sub onNotification {
  57. my $request = shift;
  58. my $client = $request->client || return;
  59. foreach my $player ($client->syncGroupActiveMembers) {
  60. next unless $player->isa('Plugins::SqueezeESP32::Player');
  61. $player->update_artwork;
  62. }
  63. }
  64. sub setEQ {
  65. my $request = shift;
  66. # check this is the correct command.
  67. if ($request->isNotCommand([['squeezeesp32'],['seteq']])) {
  68. $request->setStatusBadDispatch();
  69. return;
  70. }
  71. # get our parameters
  72. my $client = $request->client();
  73. my @eqParams = split(/,/, $request->getParam('_eq') || '');
  74. for (my $x = 0; $x < 10; $x++) {
  75. $eqParams[$x] ||= 0;
  76. }
  77. $client->send_equalizer(\@eqParams);
  78. }
  79. 1;