PlayerSettings.pm 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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);
  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. for my $i (0 .. $#{$equalizer}) {
  66. $equalizer->[$i] = $paramRef->{"pref_equalizer.$i"} || 0;
  67. }
  68. $cprefs->set('equalizer', $equalizer);
  69. $client->update_tones($equalizer);
  70. }
  71. }
  72. if ($client->displayWidth) {
  73. # the Settings super class can't handle anything but scalar values
  74. # we need to populate the $paramRef for the other prefs manually
  75. $paramRef->{'pref_spectrum'} = $cprefs->get('spectrum');
  76. $paramRef->{'pref_artwork'} = $cprefs->get('artwork');
  77. }
  78. $paramRef->{'pref_equalizer'} = $cprefs->get('equalizer') if $client->can('depth') && $client->depth == 16;
  79. $paramRef->{'player_ip'} = $client->ip;
  80. Plugins::SqueezeESP32::FirmwareHelper::initFirmwareDownload($client, sub {
  81. my ($currentFWInfo, $newFWUrl, $customFwUrl) = @_;
  82. $currentFWInfo ||= {};
  83. my $newFWInfo = Plugins::SqueezeESP32::FirmwareHelper::getFirmwareTag($newFWUrl) || {};
  84. if ($paramRef->{installUpdate} || $paramRef->{installCustomUpdate}) {
  85. my $http = Slim::Networking::SimpleAsyncHTTP->new(sub {
  86. main::INFOLOG && $log->is_info && $log->info("Firmware update triggered");
  87. }, sub {
  88. main::INFOLOG && $log->is_info && $log->info("Failed to trigger firmware update");
  89. main::DEBUGLOG && $log->is_debug && $log->debug(Data::Dump::dump(@_));
  90. })->post(sprintf('http://%s/config.json', $client->ip), to_json({
  91. timestamp => int(Time::HiRes::time() * 1000) * 1,
  92. config => {
  93. fwurl => {
  94. value => $paramRef->{installCustomUpdate} ? $customFwUrl : $newFWUrl,
  95. type => 33
  96. }
  97. }
  98. }));
  99. }
  100. else {
  101. if ($currentFWInfo->{version} && $newFWInfo->{version} && $currentFWInfo->{version} > $newFWInfo->{version}) {
  102. main::INFOLOG && $log->is_info && $log->info("There's an update for your SqueezeESP32 player: $newFWUrl");
  103. $paramRef->{fwUpdateAvailable} = sprintf($client->string('PLUGIN_SQUEEZEESP32_FIRMWARE_AVAILABLE'), $newFWInfo->{version}, $currentFWInfo->{version});
  104. }
  105. if ($customFwUrl) {
  106. main::INFOLOG && $log->is_info && $log->info("There's a custom firmware for your SqueezeESP32 player: $customFwUrl");
  107. $paramRef->{fwCustomUpdateAvailable} = 'PLUGIN_SQUEEZEESP32_CUSTOM_FIRMWARE_AVAILABLE';
  108. }
  109. }
  110. $callback->( $client, $paramRef, $class->SUPER::handler($client, $paramRef), @args );
  111. });
  112. return;
  113. }
  114. 1;