Plugin.pm 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. $prefs->setChange(sub {
  29. $_[2]->send_loudness;
  30. }, 'loudness');
  31. sub initPlugin {
  32. my $class = shift;
  33. # enable the following to test the firmware downloading code without a SqueezeliteESP32 player
  34. # require Plugins::SqueezeESP32::FirmwareHelper;
  35. # Plugins::SqueezeESP32::FirmwareHelper::init();
  36. if ( main::WEBUI ) {
  37. require Plugins::SqueezeESP32::PlayerSettings;
  38. Plugins::SqueezeESP32::PlayerSettings->new;
  39. }
  40. $class->SUPER::initPlugin(@_);
  41. # no name can be a subset of others due to a bug in addPlayerClass
  42. Slim::Networking::Slimproto::addPlayerClass($class, 100, 'squeezeesp32-basic', { client => 'Plugins::SqueezeESP32::Player', display => 'Plugins::SqueezeESP32::Graphics' });
  43. Slim::Networking::Slimproto::addPlayerClass($class, 101, 'squeezeesp32-graphic', { client => 'Plugins::SqueezeESP32::Player', display => 'Slim::Display::NoDisplay' });
  44. main::INFOLOG && $log->is_info && $log->info("Added class 100 and 101 for SqueezeESP32");
  45. # register a command to set the EQ - without saving the values! Send params as single comma separated list of values
  46. Slim::Control::Request::addDispatch(['squeezeesp32', 'seteq', '_eq'], [1, 0, 0, \&setEQ]);
  47. Slim::Control::Request::addDispatch(['squeezeesp32', 'setld', '_ld'], [1, 0, 0, \&setLD]);
  48. # Note for some forgetful know-it-all: we need to wrap the callback to make it unique. Otherwise subscriptions would overwrite each other.
  49. Slim::Control::Request::subscribe( sub { onNotification(@_) }, [ ['newmetadata'] ] );
  50. Slim::Control::Request::subscribe( sub { onNotification(@_) }, [ ['playlist'], ['open', 'newsong'] ]);
  51. Slim::Control::Request::subscribe( \&onStopClear, [ ['playlist'], ['stop', 'clear'] ]);
  52. }
  53. sub onStopClear {
  54. my $request = shift;
  55. my $client = $request->client || return;
  56. if ($client->isa('Plugins::SqueezeESP32::Player')) {
  57. $client->clear_artwork(0, $request->getRequestString());
  58. }
  59. }
  60. sub onNotification {
  61. my $request = shift;
  62. my $client = $request->client || return;
  63. foreach my $player ($client->syncGroupActiveMembers) {
  64. next unless $player->isa('Plugins::SqueezeESP32::Player');
  65. $player->update_artwork;
  66. }
  67. }
  68. sub setEQ {
  69. my $request = shift;
  70. # check this is the correct command.
  71. if ($request->isNotCommand([['squeezeesp32'],['seteq']])) {
  72. $request->setStatusBadDispatch();
  73. return;
  74. }
  75. # get our parameters
  76. my $client = $request->client();
  77. my @eqParams = split(/,/, $request->getParam('_eq') || '');
  78. for (my $x = 0; $x < 10; $x++) {
  79. $eqParams[$x] ||= 0;
  80. }
  81. $client->send_equalizer(\@eqParams);
  82. }
  83. sub setLD {
  84. my $request = shift;
  85. # check this is the correct command.
  86. if ($request->isNotCommand([['squeezeesp32'],['setld']])) {
  87. $request->setStatusBadDispatch();
  88. return;
  89. }
  90. # get our parameters
  91. my $client = $request->client();
  92. my $loudness = $request->getParam('_ld') || 0;
  93. $client->send_loudness($loudness);
  94. }
  95. 1;