Plugin.pm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. # Add menu item to extras
  53. Slim::Buttons::Home::addSubMenu('PLUGINS', 'PLUGIN_SQUEEZEESP32', { 'useMode' => 'squeezeesp32_mode', });
  54. }
  55. sub onStopClear {
  56. my $request = shift;
  57. my $client = $request->client || return;
  58. if ($client->isa('Plugins::SqueezeESP32::Player')) {
  59. $client->clear_artwork(0, $request->getRequestString());
  60. }
  61. }
  62. sub onNotification {
  63. my $request = shift;
  64. my $client = $request->client || return;
  65. foreach my $player ($client->syncGroupActiveMembers) {
  66. next unless $player->isa('Plugins::SqueezeESP32::Player');
  67. $player->update_artwork;
  68. }
  69. }
  70. sub setEQ {
  71. my $request = shift;
  72. # check this is the correct command.
  73. if ($request->isNotCommand([['squeezeesp32'],['seteq']])) {
  74. $request->setStatusBadDispatch();
  75. return;
  76. }
  77. # get our parameters
  78. my $client = $request->client();
  79. my @eqParams = split(/,/, $request->getParam('_eq') || '');
  80. for (my $x = 0; $x < 10; $x++) {
  81. $eqParams[$x] ||= 0;
  82. }
  83. $client->send_equalizer(\@eqParams);
  84. }
  85. sub setLD {
  86. my $request = shift;
  87. # check this is the correct command.
  88. if ($request->isNotCommand([['squeezeesp32'],['setld']])) {
  89. $request->setStatusBadDispatch();
  90. return;
  91. }
  92. # get our parameters
  93. my $client = $request->client();
  94. my $loudness = $request->getParam('_ld') || 0;
  95. $client->send_loudness($loudness);
  96. }
  97. 1;