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($msg, $call, $callId);
56 protected function handleExisting($callId, EDSS1_Message $msg)
58 $call = $this->currentCalls[$callId];
61 case EDSS1_Message::INFORMATION:
62 $this->handleParams($msg, $call, $callId);
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($msg, $call, $callId)
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
110 if ($call->type == CallMonitor_Call::INCOMING
111 && $param->numberType != EDSS1_Parameter_Names::NUMBER_SUBSCRIBER
113 //only keep incoming calls that arrive at the switchboard,
114 // not the ones from the switchboard to the telephones
115 unset($this->currentCalls[$callId]);
118 case EDSS1_Parameter::KEYPAD:
119 if ($call->to === null) {
120 $call->to = $param->data;
127 protected function getFullNumber($number, $type)
129 if ($type == EDSS1_Parameter_Names::NUMBER_NATIONAL) {
130 return '0' . $number;