2 namespace callnotifier;
5 * Watches EDSS1 messages for calls. Keeps an internal call state
6 * and notifies loggers of incoming and finished calls.
14 protected $currentCalls = array();
16 public function __construct($config, $log)
18 $this->config = $config;
22 public function handle(EDSS1_Message $msg)
24 $callId = $msg->callRef;
25 if (!array_key_exists($callId, $this->currentCalls)) {
26 $this->handleNew($callId, $msg);
28 $this->handleExisting($callId, $msg);
32 protected function handleNew($callId, EDSS1_Message $msg)
34 if ($msg->type != EDSS1_Message::SETUP) {
37 $this->currentCalls[$callId] = new CallMonitor_Call();
38 $this->handleSetup($callId, $msg);
42 protected function handleSetup($callId, EDSS1_Message $msg)
44 $call = $this->currentCalls[$callId];
45 $call->start = time();
46 if ($msg->tei == 127) {
47 $call->type = CallMonitor_Call::INCOMING;
49 $call->type = CallMonitor_Call::OUTGOING;
52 $this->handleParams($call, $msg);
56 protected function handleExisting($callId, EDSS1_Message $msg)
58 $call = $this->currentCalls[$callId];
61 case EDSS1_Message::INFORMATION:
62 $this->handleParams($call, $msg);
64 case EDSS1_Message::ALERTING:
65 if ($call->type == CallMonitor_Call::OUTGOING) {
67 * There may be two alerts: One from the telephone to the
68 * switchboard, and one from the switchboard to the target.
70 * The alert from the switchboard to the target call is
71 * sent first, so we can remove the call from the telephone
75 foreach ($this->currentCalls as $otherCallId => $otherCall) {
76 if ($otherCallId != $callId && $otherCall->to == $call->to) {
82 unset($this->currentCalls[$otherCallId]);
85 $this->log->log('startingCall', array('call' => $call));
88 case EDSS1_Message::RELEASE:
89 case EDSS1_Message::RELEASE_COMPLETE:
91 $this->log->log('finishedCall', array('call' => $call));
92 unset($this->currentCalls[$callId]);
97 protected function handleParams($call, $msg)
99 foreach ($msg->parameters as $param) {
100 switch ($param->type) {
101 case EDSS1_Parameter::CALLING_PARTY_NUMBER:
102 $call->from = $this->getFullNumber(
103 $param->number, $param->numberType
106 case EDSS1_Parameter::CALLED_PARTY_NUMBER:
107 $call->to = $this->getFullNumber(
108 $param->number, $param->numberType
116 protected function getFullNumber($number, $type)
118 if ($type == EDSS1_Parameter_Names::NUMBER_NATIONAL) {
119 return '0' . $number;