From 127b8b32f42db6ad08f9bfd71887f7c47212319d Mon Sep 17 00:00:00 2001 From: Junior Date: Tue, 2 Jul 2019 08:49:32 -0400 Subject: [PATCH] Switch to using the settings table for current volume instead of /tmp/curvol --- functions.php | 23 +++++++++++++++++------ scripts/homeaudio_togglemute.pl | 22 +++++++++++++++++++--- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/functions.php b/functions.php index 0b37186..efc29a2 100644 --- a/functions.php +++ b/functions.php @@ -5,17 +5,28 @@ function killPlayingSong() { } function setSystemVolume($newvol = null) { + global $globaldbh; + if ( is_null($newvol) ) return false; system("HOME=/home/web && /usr/bin/amixer -q sset Master " . $newvol); - file_put_contents("/tmp/curvol", $newvol); + $query = "UPDATE " . SETTINGSTABLE . " SET value=:value WHERE parameter='CURVOLUME'"; + $fields = array(); + $fields[':value'] = $newvol; + $sth = $globaldbh->prepare($query); + $sth->execute($fields); return true; } function getSystemVolume() { - $curvol = file_get_contents("/tmp/curvol"); - //$curvol = trim(shell_exec("HOME=/home/web && /usr/bin/amixer sget Master | grep \"Front Left:\" | awk '{print $4}'")); + global $globaldbh; + + $query = "SELECT value FROM " . SETTINGSTABLE . " WHERE parameter='CURVOLUME'"; + $sth = $globaldbh->prepare($query); + $sth->execute(); + if ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) { + $curvol = $row['value']; + } else { + $curvol = '0'; + } return $curvol; } - - -?> diff --git a/scripts/homeaudio_togglemute.pl b/scripts/homeaudio_togglemute.pl index fbeff46..ffba28f 100755 --- a/scripts/homeaudio_togglemute.pl +++ b/scripts/homeaudio_togglemute.pl @@ -1,9 +1,17 @@ #!/usr/bin/perl use Time::HiRes qw(usleep); +use DBI; +use DBD::mysql; +use Config::INI::Reader; + sub trim { my $s = shift; $s =~ s/^\s+|\s+$//g; return $s }; -$maxvolume = 32768; +my $confcontents = Config::INI::Reader->read_file('/etc/homeaudio.ini'); +my $Config = $confcontents->{_}; + +$dsn = "DBI:mysql:" . $Config->{DBNAME} . ":" . $Config->{DBHOST}; +$dbh = DBI->connect($dsn, $Config->{DBUSER}, $Config->{DBPASS}, {RaiseError=>1}); if ( $ARGV[0] eq "-show" ) { $showmute = 1; @@ -13,8 +21,16 @@ if ( $ARGV[0] eq "-show" ) { $sound = `/usr/bin/amixer sget Master`; $mute = trim(`/usr/bin/amixer sget Master | grep "Front Left:" | awk '{print \$6}'`); -$curvol = int(`cat /tmp/curvol`); -$stepval = int($maxvolume * 0.1); +$sth = $dbh->prepare("SELECT value FROM settings WHERE parameter='CURVOLUME'"); +$sth->execute(); +if ( $sth->rows > 0 ) { + @result = $sth->fetchrow_array(); + $curvol = $result[0]; +} else { + $curvol = 0; +} +$sth->finish(); +$stepval = int($curvol * 0.1); @mixer = split(/\n/, $sound); #if ( index($mixer[@mixer-1], "[on]") > 0 ) { if ( $mute eq "[on]" ) {