|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright 2015 Brian Smith <[email protected]>. |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace phparia\Client; |
|
20
|
|
|
|
|
21
|
|
|
use Clue\React\Ami\ActionSender; |
|
22
|
|
|
use Clue\React\Ami\Client; |
|
23
|
|
|
use Clue\React\Ami\Factory; |
|
24
|
|
|
use Clue\React\Ami\Protocol\Event; |
|
25
|
|
|
use Devristo\Phpws\Client\WebSocket; |
|
26
|
|
|
use React\EventLoop\LoopInterface; |
|
27
|
|
|
use Zend\Log\LoggerInterface; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* AMI Client to get events not supported in ARI |
|
31
|
|
|
* |
|
32
|
|
|
* @author Brian Smith <[email protected]> |
|
33
|
|
|
*/ |
|
34
|
|
|
class AmiClient |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* @var Client |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $amiClient = null; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var WebSocket |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $wsClient; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var LoopInterface |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $eventLoop; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var LoggerInterface |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $logger; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var ActionSender |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $actionSender = null; |
|
60
|
|
|
|
|
61
|
97 |
|
public function __construct(WebSocket $wsClient, LoopInterface $eventLoop, LoggerInterface $logger) |
|
62
|
|
|
{ |
|
63
|
97 |
|
$this->wsClient = $wsClient; |
|
64
|
97 |
|
$this->eventLoop = $eventLoop; |
|
65
|
97 |
|
$this->logger = $logger; |
|
66
|
97 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Connect to AMI and start emitting events. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $address Example uaername:password@localhost:5038 |
|
72
|
|
|
* @return \React\Promise\Promise |
|
73
|
|
|
*/ |
|
74
|
97 |
|
public function connect($address) |
|
75
|
|
|
{ |
|
76
|
97 |
|
$factory = new Factory($this->eventLoop); |
|
77
|
|
|
|
|
78
|
97 |
|
return $factory->createClient($address) |
|
79
|
|
|
->then(function (Client $client) { |
|
80
|
37 |
|
$this->amiClient = $client; |
|
81
|
37 |
|
$this->actionSender = new ActionSender($client); |
|
82
|
37 |
|
$this->actionSender->events(true); |
|
83
|
|
|
$client->on('close', function () { |
|
84
|
|
|
$this->logger->debug('AMI connection closed'); |
|
85
|
37 |
|
}); |
|
86
|
|
|
$client->on('event', function (Event $event) { |
|
87
|
37 |
|
$this->wsClient->emit($event->getName(), (array)$event); |
|
88
|
37 |
|
}); |
|
89
|
97 |
|
}, function (\Exception $e) { |
|
90
|
|
|
$this->logger->err('Connection error: '.$e->getMessage()); |
|
91
|
97 |
|
}); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return Client |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getClient() |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->amiClient; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return ActionSender |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getActionSender() |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->actionSender; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
|