App-MPDJ
view release on metacpan or search on metacpan
lib/App/MPDJ.pm view on Meta::CPAN
'syslog|s=s' => { DEFAULT => '' },
'conlog|l=s' => { DEFAULT => '' },
'help|h' => { ACTION => \&help, },
'version|V' => { ACTION => \&version, });
$self->_getopt(@args); # to get --conf option, if any
my @configs =
$self->config->get('conf') || ('/etc/mpdj.conf', "$ENV{HOME}/.mpdjrc");
foreach my $config (@configs) {
if (-e $config) {
say "Loading config ($config)" if $self->config->get('conlog');
$self->config->file($config);
} else {
say "Config file skipped ($config)" if $self->config->get('conlog');
}
}
$self->_getopt(@args); # to override config file
}
sub _getopt {
my ($self, @args) = @_;
$self->config->getopt([@args]); # do not consume @args
if (@{ $self->{config_errors} }) {
foreach my $err (@{ $self->{config_errors} }) {
printf STDERR @$err;
print STDERR "\n";
}
$self->help;
}
}
sub connect {
my ($self) = @_;
$self->{mpd} = Net::MPD->connect($self->config->get('mpd'));
}
sub execute {
my ($self) = @_;
local @SIG{qw( INT TERM HUP )} = sub {
$self->log->notice('Exiting');
exit 0;
};
my @loggers;
push @loggers,
([ 'Screen', min_level => $self->config->get('conlog'), newline => 1 ])
if $self->config->get('conlog');
push @loggers,
([ 'Syslog', min_level => $self->config->get('syslog'), ident => 'mpdj' ])
if $self->config->get('syslog');
$self->{log} = Log::Dispatch->new(outputs => \@loggers);
if ($self->config->get('daemon')) {
$self->log->notice('Forking to background');
Proc::Daemon::Init;
}
$self->connect;
$self->configure;
$self->mpd->subscribe('mpdj');
$self->update_cache;
while (1) {
$self->log->debug('Waiting');
my @changes =
$self->mpd->idle(qw(database player playlist message options));
$self->mpd->update_status();
foreach my $subsystem (@changes) {
my $function = $subsystem . '_changed';
$self->$function();
}
}
}
sub configure {
my ($self) = @_;
$self->log->notice('Configuring MPD server');
$self->mpd->repeat(0);
$self->mpd->random(0);
if ($self->config->get('calls-freq')) {
my $now = time;
$self->{last_call} = $now - $now % $self->config->get('calls-freq');
$self->log->notice("Set last call to $self->{last_call}");
}
}
sub update_cache {
my ($self) = @_;
$self->log->notice('Updating music and calls cache...');
foreach my $category (('music', 'calls')) {
@{ $self->{$category} } = grep { $_->{type} eq 'file' }
$self->mpd->list_all($self->config->get("${category}-path"));
my $total = scalar(@{ $self->{$category} });
if ($total) {
$self->log->notice(sprintf 'Total %s available: %d', $category, $total);
} else {
$self->log->warning(
"No $category available. Path should be mpd path not file system.");
}
}
}
sub remove_old_songs {
my ($self) = @_;
( run in 2.069 seconds using v1.01-cache-2.11-cpan-d8267643d1d )