|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright 2015 Brian Smith <[email protected]>. |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
6
|
|
|
* you may not use this file except in compliance with the License. |
|
7
|
|
|
* You may obtain a copy of the License at |
|
8
|
|
|
* |
|
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
10
|
|
|
* |
|
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14
|
|
|
* See the License for the specific language governing permissions and |
|
15
|
|
|
* limitations under the License. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace phparia\Client; |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
use Devristo\Phpws\Client\WebSocket; |
|
22
|
|
|
use phparia\Api\Applications; |
|
23
|
|
|
use phparia\Api\Asterisk; |
|
24
|
|
|
use phparia\Api\Bridges; |
|
25
|
|
|
use phparia\Api\Channels; |
|
26
|
|
|
use phparia\Api\DeviceStates; |
|
27
|
|
|
use phparia\Api\Endpoints; |
|
28
|
|
|
use phparia\Api\Events; |
|
29
|
|
|
use phparia\Api\Mailboxes; |
|
30
|
|
|
use phparia\Api\Playbacks; |
|
31
|
|
|
use phparia\Api\Recordings; |
|
32
|
|
|
use phparia\Api\Sounds; |
|
33
|
|
|
use phparia\Events\Event; |
|
34
|
|
|
use React\EventLoop; |
|
35
|
|
|
use Zend\Log\LoggerInterface; |
|
36
|
|
|
|
|
37
|
|
|
class Phparia |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* @var WebSocket |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $wsClient; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var EventLoop\LoopInterface |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $eventLoop; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var LoggerInterface |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $logger; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var AriClient |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $ariClient; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var AmiClient |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $amiClient; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var string |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $stasisApplicationName; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param LoggerInterface $logger |
|
71
|
|
|
*/ |
|
72
|
78 |
|
public function __construct(LoggerInterface $logger) |
|
73
|
|
|
{ |
|
74
|
78 |
|
$this->logger = $logger; |
|
75
|
78 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Connect to ARI and optionally AMI |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $ariAddress |
|
81
|
|
|
* @param string|null $amiAddress |
|
82
|
|
|
*/ |
|
83
|
78 |
|
public function connect($ariAddress, $amiAddress = null) |
|
84
|
|
|
{ |
|
85
|
78 |
|
$this->eventLoop = EventLoop\Factory::create(); |
|
86
|
78 |
|
$this->ariClient = new AriClient($this->eventLoop, $this->logger); |
|
87
|
78 |
|
$this->ariClient->connect($ariAddress); |
|
88
|
78 |
|
$this->wsClient = $this->ariClient->getWsClient(); |
|
89
|
78 |
|
$this->stasisApplicationName = $this->ariClient->getStasisApplicationName(); |
|
90
|
|
|
|
|
91
|
78 |
|
if ($amiAddress !== null) { |
|
92
|
78 |
|
$this->amiClient = new AmiClient($this->ariClient->getWsClient(), $this->eventLoop, $this->logger); |
|
93
|
78 |
|
$this->amiClient |
|
94
|
78 |
|
->connect($amiAddress) |
|
95
|
78 |
|
->done(); |
|
96
|
78 |
|
} |
|
97
|
78 |
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Connect and start the event loop |
|
101
|
|
|
*/ |
|
102
|
19 |
|
public function run() |
|
103
|
|
|
{ |
|
104
|
19 |
|
$this->wsClient->open(); |
|
105
|
19 |
|
$this->eventLoop->run(); |
|
106
|
9 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Disconnect and stop the event loop |
|
110
|
|
|
*/ |
|
111
|
9 |
|
public function stop() |
|
112
|
|
|
{ |
|
113
|
9 |
|
$this->wsClient->close(); |
|
114
|
9 |
|
$this->eventLoop->stop(); |
|
115
|
9 |
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param callable|callable $callback |
|
119
|
|
|
*/ |
|
120
|
19 |
|
public function onStasisStart(callable $callback) |
|
121
|
|
|
{ |
|
122
|
19 |
|
$this->wsClient->on(Event::STASIS_START, $callback); |
|
123
|
19 |
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param callable|callable $callback |
|
127
|
|
|
*/ |
|
128
|
|
|
public function onStasisEnd(callable $callback) |
|
129
|
|
|
{ |
|
130
|
|
|
$this->wsClient->on(Event::STASIS_END, $callback); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @return WebSocket |
|
135
|
|
|
*/ |
|
136
|
1 |
|
public function getWsClient() |
|
137
|
|
|
{ |
|
138
|
1 |
|
return $this->wsClient; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @return EventLoop\LoopInterface |
|
143
|
|
|
*/ |
|
144
|
1 |
|
public function getEventLoop() |
|
145
|
|
|
{ |
|
146
|
1 |
|
return $this->eventLoop; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @return LoggerInterface |
|
151
|
|
|
*/ |
|
152
|
2 |
|
public function getLogger() |
|
153
|
|
|
{ |
|
154
|
2 |
|
return $this->logger; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @return AriClient |
|
159
|
|
|
*/ |
|
160
|
20 |
|
public function getAriClient() |
|
161
|
|
|
{ |
|
162
|
20 |
|
return $this->ariClient; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @return AmiClient |
|
167
|
|
|
*/ |
|
168
|
1 |
|
public function getAmiClient() |
|
169
|
|
|
{ |
|
170
|
1 |
|
return $this->amiClient; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @return string |
|
175
|
|
|
*/ |
|
176
|
26 |
|
public function getStasisApplicationName() |
|
177
|
|
|
{ |
|
178
|
26 |
|
return $this->stasisApplicationName; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @return Applications |
|
183
|
|
|
*/ |
|
184
|
12 |
|
public function applications() |
|
185
|
|
|
{ |
|
186
|
12 |
|
return $this->ariClient->applications(); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @return Asterisk |
|
191
|
|
|
*/ |
|
192
|
7 |
|
public function asterisk() |
|
193
|
|
|
{ |
|
194
|
7 |
|
return $this->ariClient->asterisk(); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @return Bridges |
|
199
|
|
|
*/ |
|
200
|
30 |
|
public function bridges() |
|
201
|
|
|
{ |
|
202
|
30 |
|
return $this->ariClient->bridges(); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @return Channels |
|
207
|
|
|
*/ |
|
208
|
20 |
|
public function channels() |
|
209
|
|
|
{ |
|
210
|
20 |
|
return $this->ariClient->channels(); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @return DeviceStates |
|
215
|
|
|
*/ |
|
216
|
2 |
|
public function deviceStates() |
|
217
|
|
|
{ |
|
218
|
2 |
|
return $this->ariClient->deviceStates(); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @return Endpoints |
|
223
|
|
|
*/ |
|
224
|
2 |
|
public function endPoints() |
|
225
|
|
|
{ |
|
226
|
2 |
|
return $this->ariClient->endPoints(); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* @return Events |
|
231
|
|
|
*/ |
|
232
|
2 |
|
public function events() |
|
233
|
|
|
{ |
|
234
|
2 |
|
return $this->ariClient->events(); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* @return Mailboxes |
|
239
|
|
|
*/ |
|
240
|
1 |
|
public function mailboxes() |
|
241
|
|
|
{ |
|
242
|
1 |
|
return $this->ariClient->mailboxes(); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* @return Playbacks |
|
247
|
|
|
*/ |
|
248
|
5 |
|
public function playbacks() |
|
249
|
|
|
{ |
|
250
|
5 |
|
return $this->ariClient->playbacks(); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* @return Recordings |
|
255
|
|
|
*/ |
|
256
|
6 |
|
public function recordings() |
|
257
|
|
|
{ |
|
258
|
6 |
|
return $this->ariClient->recordings(); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* @return Sounds |
|
263
|
|
|
*/ |
|
264
|
3 |
|
public function sounds() |
|
265
|
|
|
{ |
|
266
|
3 |
|
return $this->ariClient->sounds(); |
|
267
|
1 |
|
} |
|
268
|
|
|
|
|
269
|
|
|
} |