filter out duplicate incoming calls
[auerswald-callnotifier.git] / src / callnotifier / CallMonitor.php
index ebc9a50062828a93d974e77f64fea2a51d7cfed6..b8f904d0c46cf9247ccde621bc5a7e5ce8ad90db 100644 (file)
@@ -49,7 +49,7 @@ class CallMonitor
             $call->type = CallMonitor_Call::OUTGOING;
         }
 
-        $this->handleParams($call, $msg);
+        $this->handleParams($msg, $call, $callId);
     }
 
 
@@ -59,9 +59,29 @@ class CallMonitor
 
         switch ($msg->type) {
         case EDSS1_Message::INFORMATION:
-            $this->handleParams($call, $msg);
+            $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;
 
@@ -74,7 +94,7 @@ class CallMonitor
         }
     }
 
-    protected function handleParams($call, $msg)
+    protected function handleParams($msg, $call, $callId)
     {
         foreach ($msg->parameters as $param) {
             switch ($param->type) {
@@ -87,7 +107,18 @@ class CallMonitor
                 $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;
+                }
             }
         }
     }