1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JarvisPHP\Plugins\Radio_plugin; |
4
|
|
|
|
5
|
|
|
use JarvisPHP\Core\JarvisSession; |
6
|
|
|
use JarvisPHP\Core\JarvisPHP; |
7
|
|
|
use JarvisPHP\Core\JarvisLanguage; |
8
|
|
|
use JarvisPHP\Core\JarvisTTS; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Radio_plugin |
12
|
|
|
* |
13
|
|
|
* @author Stefano Bianchini |
14
|
|
|
*/ |
15
|
|
|
class Radio_plugin implements \JarvisPHP\Core\JarvisPluginInterface { |
16
|
|
|
/** |
17
|
|
|
* Priority of plugin |
18
|
|
|
* @var int |
19
|
|
|
*/ |
20
|
|
|
var $priority = 5; |
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* the behaviour of plugin |
24
|
|
|
* @param string $command |
25
|
|
|
*/ |
26
|
|
|
function answer($command) { |
|
|
|
|
27
|
|
|
|
28
|
|
|
$start_vlc = false; |
29
|
|
|
$stream_url=''; |
30
|
|
|
$answer='nothing done'; |
31
|
|
|
|
32
|
|
|
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); |
33
|
|
|
|
34
|
|
View Code Duplication |
if(preg_match(JarvisLanguage::translate('preg_match_relax_music',get_called_class()), $command)) { |
|
|
|
|
35
|
|
|
$stream_url = 'http://pub4.radiotunes.com:80/radiotunes_relaxation_aacplus.flv'; |
36
|
|
|
JarvisPHP::getLogger()->debug('Playing relaxation'); |
37
|
|
|
$start_vlc = true; |
38
|
|
|
} |
39
|
|
View Code Duplication |
if(preg_match(JarvisLanguage::translate('preg_match_lounge_music',get_called_class()), $command)) { |
|
|
|
|
40
|
|
|
$stream_url = 'http://pub4.radiotunes.com:80/radiotunes_smoothlounge_aacplus.flv'; |
41
|
|
|
JarvisPHP::getLogger()->debug('Playing lounge'); |
42
|
|
|
$start_vlc = true; |
43
|
|
|
} |
44
|
|
View Code Duplication |
if(preg_match(JarvisLanguage::translate('preg_match_jazz_music',get_called_class()), $command)) { |
|
|
|
|
45
|
|
|
$stream_url = 'http://pub4.radiotunes.com:80/radiotunes_smoothjazz_aacplus.flv'; |
46
|
|
|
JarvisPHP::getLogger()->debug('Playing jazz'); |
47
|
|
|
$start_vlc = true; |
48
|
|
|
} |
49
|
|
|
if(preg_match(JarvisLanguage::translate('preg_match_stop',get_called_class()), $command)) { |
50
|
|
|
$result = @socket_connect($socket, 'localhost', 9999); |
51
|
|
View Code Duplication |
if ($result === false) { |
|
|
|
|
52
|
|
|
//Error handling |
53
|
|
|
JarvisPHP::getLogger()->warn('Failed to connect!'); |
54
|
|
|
$answer = 'Failed to connect!'; |
55
|
|
|
} |
56
|
|
|
else { |
57
|
|
|
$vlc_remote_command = 'stop'.chr(13); |
|
|
|
|
58
|
|
|
$vlc_remote_command = 'quit'.chr(13); |
59
|
|
|
socket_write($socket, $vlc_remote_command, strlen($vlc_remote_command)); |
60
|
|
|
socket_close($socket); |
61
|
|
|
JarvisPHP::getLogger()->debug('Radio stopped'); |
62
|
|
|
$answer = 'Radio stopped'; |
63
|
|
|
} |
64
|
|
|
$start_vlc = false; |
65
|
|
|
} |
66
|
|
|
if($start_vlc) { |
67
|
|
|
//Start Vlc on port 9999 |
68
|
|
|
//exec('vlc -I rc --rc-host localhost:9999'); |
69
|
|
|
@exec('/usr/bin/nohup /usr/bin/vlc -I rc --rc-host localhost:9999 &'); |
|
|
|
|
70
|
|
|
|
71
|
|
|
sleep(1); |
72
|
|
|
|
73
|
|
|
$result = @socket_connect($socket, 'localhost', 9999); |
74
|
|
View Code Duplication |
if ($result === false) { |
|
|
|
|
75
|
|
|
//Error handling |
76
|
|
|
JarvisPHP::getLogger()->warn('Failed to connect!'); |
77
|
|
|
$answer = 'Failed to connect!'; |
78
|
|
|
} |
79
|
|
|
else { |
80
|
|
|
$vlc_remote_command = 'add '.$stream_url.chr(13); |
81
|
|
|
socket_write($socket, $vlc_remote_command, strlen($vlc_remote_command)); |
82
|
|
|
socket_close($socket); |
83
|
|
|
JarvisPHP::getLogger()->debug('Radio started'); |
84
|
|
|
$answer = 'Radio started'; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$response = new \JarvisPHP\Core\JarvisResponse($answer, JarvisPHP::getRealClassName(get_called_class()), true); |
89
|
|
|
$response->send(); |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get plugin's priority |
95
|
|
|
* @return int |
96
|
|
|
*/ |
97
|
|
|
function getPriority() { |
|
|
|
|
98
|
|
|
return $this->priority; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Is it the right plugin for the command? |
103
|
|
|
* @param string $command |
104
|
|
|
* @return boolean |
105
|
|
|
*/ |
106
|
|
|
function isLikely($command) { |
|
|
|
|
107
|
|
|
return preg_match(JarvisLanguage::translate('preg_match_activate_plugin',get_called_class()), $command); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Does the plugin need a session? |
112
|
|
|
* @return boolean |
113
|
|
|
*/ |
114
|
|
|
function hasSession() { |
|
|
|
|
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.