fix docblock
[auerswald-callnotifier.git] / src / callnotifier / CallMonitor.php
index 9abdba80c959cf61b8ab65c38f699ded6aa5389b..428136fb5dff0df2757f249542e941677a95e9c0 100644 (file)
@@ -6,7 +6,7 @@ namespace callnotifier;
  * and notifies loggers of incoming and finished calls.
  *
  * Notifications:
- * - incomingCall
+ * - startingCall
  * - finishedCall
  */
 class CallMonitor
@@ -43,16 +43,13 @@ class CallMonitor
     {
         $call = $this->currentCalls[$callId];
         $call->start = time();
-        foreach ($msg->parameters as $param) {
-            switch ($param->type) {
-            case EDSS1_Parameter::CALLING_PARTY_NUMBER:
-                $call->from = $param->number;
-                break;
-            case EDSS1_Parameter::CALLED_PARTY_NUMBER:
-                $call->to = $param->number;
-                break;
-            }
+        if ($msg->tei == 127) {
+            $call->type = CallMonitor_Call::INCOMING;
+        } else {
+            $call->type = CallMonitor_Call::OUTGOING;
         }
+
+        $this->handleParams($msg, $call, $callId);
     }
 
 
@@ -61,8 +58,31 @@ class CallMonitor
         $call = $this->currentCalls[$callId];
 
         switch ($msg->type) {
-        case EDSS1_Message::CALL_PROCEEDING:
-            $this->log->log('incomingCall', array('call' => $call));
+        case EDSS1_Message::INFORMATION:
+            $this->handleParams($msg, $call, $callId);
+            break;
+        case EDSS1_Message::ALERTING:
+            if ($call->type == CallMonitor_Call::OUTGOING) {
+                /**
+                 * There may be two alerts: One from the telephone to the
+                 * switchboard, and one from the switchboard to the target.
+                 *
+                 * The alert from the switchboard to the target call is
+                 * sent first, so we can remove the call from the telephone
+                 * to the switchboard.
+                 */
+                $bFound = false;
+                foreach ($this->currentCalls as $otherCallId => $otherCall) {
+                    if ($otherCallId != $callId && $otherCall->to == $call->to) {
+                        $bFound = true;
+                        break;
+                    }
+                }
+                if ($bFound) {
+                    unset($this->currentCalls[$otherCallId]);
+                }
+            }
+            $this->log->log('startingCall', array('call' => $call));
             break;
 
         case EDSS1_Message::RELEASE:
@@ -73,6 +93,44 @@ class CallMonitor
             break;
         }
     }
+
+    protected function handleParams($msg, $call, $callId)
+    {
+        foreach ($msg->parameters as $param) {
+            switch ($param->type) {
+            case EDSS1_Parameter::CALLING_PARTY_NUMBER:
+                $call->from = $this->getFullNumber(
+                    $param->number, $param->numberType
+                );
+                break;
+            case EDSS1_Parameter::CALLED_PARTY_NUMBER:
+                $call->to = $this->getFullNumber(
+                    $param->number, $param->numberType
+                );
+                if ($call->type == CallMonitor_Call::INCOMING
+                    && $param->numberType != EDSS1_Parameter_Names::NUMBER_SUBSCRIBER
+                ) {
+                    //only keep incoming calls that arrive at the switchboard,
+                    // not the ones from the switchboard to the telephones
+                    unset($this->currentCalls[$callId]);
+                }
+                break;
+            case EDSS1_Parameter::KEYPAD:
+                if ($call->to === null) {
+                    $call->to = $param->data;
+                }
+            }
+        }
+    }
+
+
+    protected function getFullNumber($number, $type)
+    {
+        if ($type == EDSS1_Parameter_Names::NUMBER_NATIONAL) {
+            return '0' . $number;
+        }
+        return $number;
+    }
 }
 
 ?>