PlayerSettings.pm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package Plugins::SqueezeESP32::PlayerSettings;
  2. use strict;
  3. use base qw(Slim::Web::Settings);
  4. use JSON::XS::VersionOneAndTwo;
  5. use List::Util qw(first min max);
  6. use Slim::Utils::Log;
  7. use Slim::Utils::Prefs;
  8. my $sprefs = preferences('server');
  9. my $prefs = preferences('plugin.squeezeesp32');
  10. my $log = logger('plugin.squeezeesp32');
  11. sub name {
  12. return Slim::Web::HTTP::CSRF->protectName('PLUGIN_SQUEEZEESP32_PLAYERSETTINGS');
  13. }
  14. sub needsClient {
  15. return 1;
  16. }
  17. sub validFor {
  18. my ($class, $client) = @_;
  19. return $client->model eq 'squeezeesp32';
  20. }
  21. sub page {
  22. return Slim::Web::HTTP::CSRF->protectURI('plugins/SqueezeESP32/settings/player.html');
  23. }
  24. sub prefs {
  25. my ($class, $client) = @_;
  26. my @prefs;
  27. push @prefs, qw(width small_VU) if $client->displayWidth;
  28. return ($prefs->client($client), @prefs);
  29. }
  30. sub handler {
  31. my ($class, $client, $paramRef, $callback, @args) = @_;
  32. my ($cprefs, @prefs) = $class->prefs($client);
  33. if ($paramRef->{'saveSettings'}) {
  34. if ($client->displayWidth) {
  35. $cprefs->set('small_VU', $paramRef->{'pref_small_VU'} || 15);
  36. require Plugins::SqueezeESP32::Graphics;
  37. my $spectrum = Plugins::SqueezeESP32::Graphics::sanitizeSpectrum({
  38. scale => $paramRef->{'pref_spectrum_scale'},
  39. small => {
  40. size => $paramRef->{'pref_spectrum_small_size'},
  41. band => $paramRef->{'pref_spectrum_small_band'}
  42. },
  43. full => {
  44. band => $paramRef->{'pref_spectrum_full_band'}
  45. },
  46. });
  47. $cprefs->set('spectrum', $spectrum);
  48. my $artwork = {
  49. enable => $paramRef->{'pref_artwork_enable'} eq 'on',
  50. x => $paramRef->{'pref_artwork_x'} || 0,
  51. y => $paramRef->{'pref_artwork_y'} || 0,
  52. };
  53. $cprefs->set('artwork', $artwork);
  54. $client->display->modes($client->display->build_modes);
  55. # the display update will be done below, after all is completed
  56. # force update or disable artwork
  57. if ($artwork->{'enable'}) {
  58. $client->update_artwork(1);
  59. } else {
  60. $client->config_artwork();
  61. }
  62. }
  63. if ($client->can('depth') && $client->depth == 16) {
  64. my $equalizer = $cprefs->get('equalizer');
  65. foreach (0 .. 9) {
  66. $equalizer->[$_] = min($client->maxBass, max($client->minBass, $paramRef->{"pref_equalizer.$_"} || 0))
  67. }
  68. $equalizer = [ splice(@$equalizer, 0, 10) ];
  69. $cprefs->set('equalizer', $equalizer);
  70. $client->update_tones($equalizer);
  71. }
  72. }
  73. if ($client->displayWidth) {
  74. # the Settings super class can't handle anything but scalar values
  75. # we need to populate the $paramRef for the other prefs manually
  76. $paramRef->{'pref_spectrum'} = $cprefs->get('spectrum');
  77. $paramRef->{'pref_artwork'} = $cprefs->get('artwork');
  78. }
  79. $paramRef->{'pref_equalizer'} = $cprefs->get('equalizer') if $client->can('depth') && $client->depth == 16;
  80. $paramRef->{'player_ip'} = $client->ip;
  81. require Plugins::SqueezeESP32::FirmwareHelper;
  82. Plugins::SqueezeESP32::FirmwareHelper::initFirmwareDownload($client, sub {
  83. my ($currentFWInfo, $newFWUrl, $customFwUrl) = @_;
  84. $currentFWInfo ||= {};
  85. my $newFWInfo = Plugins::SqueezeESP32::FirmwareHelper::getFirmwareTag($newFWUrl) || {};
  86. if ($paramRef->{installUpdate} || $paramRef->{installCustomUpdate}) {
  87. my $http = Slim::Networking::SimpleAsyncHTTP->new(sub {
  88. main::INFOLOG && $log->is_info && $log->info("Firmware update triggered");
  89. }, sub {
  90. main::INFOLOG && $log->is_info && $log->info("Failed to trigger firmware update");
  91. main::DEBUGLOG && $log->is_debug && $log->debug(Data::Dump::dump(@_));
  92. })->post(sprintf('http://%s/config.json', $client->ip), to_json({
  93. timestamp => int(Time::HiRes::time() * 1000) * 1,
  94. config => {
  95. fwurl => {
  96. value => $paramRef->{installCustomUpdate} ? $customFwUrl : $newFWUrl,
  97. type => 33
  98. }
  99. }
  100. }));
  101. }
  102. else {
  103. if ($currentFWInfo->{version} && $newFWInfo->{version} && $currentFWInfo->{version} > $newFWInfo->{version}) {
  104. main::INFOLOG && $log->is_info && $log->info("There's an update for your SqueezeESP32 player: $newFWUrl");
  105. $paramRef->{fwUpdateAvailable} = sprintf($client->string('PLUGIN_SQUEEZEESP32_FIRMWARE_AVAILABLE'), $newFWInfo->{version}, $currentFWInfo->{version});
  106. }
  107. if ($customFwUrl) {
  108. main::INFOLOG && $log->is_info && $log->info("There's a custom firmware for your SqueezeESP32 player: $customFwUrl");
  109. $paramRef->{fwCustomUpdateAvailable} = 'PLUGIN_SQUEEZEESP32_CUSTOM_FIRMWARE_AVAILABLE';
  110. }
  111. }
  112. $callback->( $client, $paramRef, $class->SUPER::handler($client, $paramRef), @args );
  113. });
  114. return;
  115. }
  116. 1;