forked from krkeegan/misterhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMp3Player.pm
127 lines (85 loc) · 2.94 KB
/
Mp3Player.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
=head1 B<Mp3Player>
=head2 SYNOPSIS
NONE
=head2 DESCRIPTION
Provides an object-based interface for all MP3 players. Code for the different players should be moved into this object eventually (no need for four mutually exclusive common code modules.)
=head2 INHERITS
B<Generic_Item>
=head2 METHODS
=over
=item B<UnDoc>
=cut
#!/usr/bin/perl
use strict;
package Mp3Player;
@Mp3Player::ISA = ('Generic_Item');
my @mp3player_object_list;
sub new
{
my ($class, $address) = @_;
my $self = {address => $address};
bless $self, $class;
push(@mp3player_object_list, $self);
push(@{$$self{states}}, 'play', 'pause', 'stop', 'next song', 'previous song', 'shuffle', 'repeat', 'volume up', 'volume down', 'clear list', 'random song');
return $self;
}
sub default_setstate
{
my ($self, $state) = @_;
my $address = $self->{address} || 'localhost';
print "Mp3Player set: " . $self->{address} . " to " . $state . "\n" if $::Debug{mp3};
# NOTE: -1 is returned by the object to suppress the state change on failure.
# X10 and Insteon controllers need to work like this (cm11 doesn't.)
# 0 = failure goes to -1 for mh fail code;
# 1 = success goes to 0, which mh sees as a success. (?)
return mp3_player_control($state, $self->{address}) - 1;
}
sub play {
my $self = shift;
print "\n\n\nPlaying: $_[0]\n\n\n";
return &mp3_player_control("play \"$_[0]\"", $self->{address});
}
sub mp3_player_control
{
my ($command, $host) = @_;
my $result;
if ($command =~ /^play "(.+)"$/i) {
eval "\$result = &::mp3_play(qq|$1|, '$host')";
}
elsif ($command =~ /^station "(.+)"$/i) {
eval "\$result = &::mp3_radio_play(qq|$1|, '$host')";
}
elsif ($command =~ /^queue "(.+)"$/i) {
eval "\$result = &::mp3_queue(qq|$1|, '$host')";
}
elsif ($command eq 'clear list') {
eval "\$result = &::mp3_clear()";
}
else {
eval "\$result = &::mp3_control('$command', '$host')";
}
if ($@) {
warn "mp3_control: eval error: $@\n";
return 0; # Failed to evaluate
}
elsif (!$result) {
warn "mp3_control: command failed: $command";
return 0; # Failed to execute
}
else {
return 1; # Success!
}
}
1;
=back
=head2 INI PARAMETERS
NONE
=head2 AUTHOR
UNK
=head2 SEE ALSO
NONE
=head2 LICENSE
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
=cut