call echo logger
authorChristian Weiske <cweiske@cweiske.de>
Tue, 31 Jul 2012 05:49:52 +0000 (07:49 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Tue, 31 Jul 2012 05:49:52 +0000 (07:49 +0200)
src/callnotifier/CLI.php
src/callnotifier/CallMonitor.php
src/callnotifier/CallMonitor/Call.php [new file with mode: 0644]
src/callnotifier/EDSS1/Parameter.php
src/callnotifier/Log.php
src/callnotifier/Logger/CallEcho.php [new file with mode: 0644]

index 5fdde92b98e08a7ef09cf9de4fe0ce376faff9ad..6ec9a7e40938664dbcabe6e91b738597e95dd0f0 100644 (file)
@@ -31,6 +31,9 @@ class CLI
                 $debugLogger->edss1MsgOnly = true;
             }
         }
+        $log->addLogger(
+            new Logger_CallEcho(), array('incomingCall', 'finishedCall')
+        );
 
         $callMonitor = new CallMonitor($this->config, $log);
 
index a910e9f13008d55a2398821d89c349e3a74871d5..9abdba80c959cf61b8ab65c38f699ded6aa5389b 100644 (file)
@@ -4,9 +4,15 @@ namespace callnotifier;
 /**
  * Watches EDSS1 messages for calls. Keeps an internal call state
  * and notifies loggers of incoming and finished calls.
+ *
+ * Notifications:
+ * - incomingCall
+ * - finishedCall
  */
 class CallMonitor
 {
+    protected $currentCalls = array();
+
     public function __construct($config, $log)
     {
         $this->config = $config;
@@ -15,8 +21,58 @@ class CallMonitor
 
     public function handle(EDSS1_Message $msg)
     {
+        $callId = $msg->callRef;
+        if (!array_key_exists($callId, $this->currentCalls)) {
+            $this->handleNew($callId, $msg);
+        } else {
+            $this->handleExisting($callId, $msg);
+        }
+    }
+
+    protected function handleNew($callId, EDSS1_Message $msg)
+    {
+        if ($msg->type != EDSS1_Message::SETUP) {
+            return;
+        }
+        $this->currentCalls[$callId] = new CallMonitor_Call();
+        $this->handleSetup($callId, $msg);
     }
 
+
+    protected function handleSetup($callId, EDSS1_Message $msg)
+    {
+        $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;
+            }
+        }
+    }
+
+
+    protected function handleExisting($callId, EDSS1_Message $msg)
+    {
+        $call = $this->currentCalls[$callId];
+
+        switch ($msg->type) {
+        case EDSS1_Message::CALL_PROCEEDING:
+            $this->log->log('incomingCall', array('call' => $call));
+            break;
+
+        case EDSS1_Message::RELEASE:
+        case EDSS1_Message::RELEASE_COMPLETE:
+            $call->end = time();
+            $this->log->log('finishedCall', array('call' => $call));
+            unset($this->currentCalls[$callId]);
+            break;
+        }
+    }
 }
 
 ?>
diff --git a/src/callnotifier/CallMonitor/Call.php b/src/callnotifier/CallMonitor/Call.php
new file mode 100644 (file)
index 0000000..3ebd251
--- /dev/null
@@ -0,0 +1,34 @@
+<?php
+namespace callnotifier;
+
+class CallMonitor_Call
+{
+    /**
+     * Telephone number of caller
+     *
+     * @var string
+     */
+    public $from;
+
+    /**
+     * Telephone number of called person
+     *
+     * @var string
+     */
+    public $to;
+
+    /**
+     * Time when the call started, unix timestamp
+     *
+     * @var int
+     */
+    public $start;
+
+    /**
+     * Time when the call ended, unix timestamp
+     */
+    public $end;
+
+}
+
+?>
index 0f20427ccf17325a06cc0cd9a4088bd656f668f8..87bc3c3482adb60d05b31cff7c41e8fb9d762977 100644 (file)
@@ -6,10 +6,10 @@ namespace callnotifier;
  */
 class EDSS1_Parameter
 {
-    const CALLING_PARTY_NUMBER = "\x6C";
-    const CALLED_PARTY_NUMBER = "\x70";
-    const CONNECTED_NUMBER = "\x4C";
-    const KEYPAD = "\x2C";
+    const CALLING_PARTY_NUMBER = 0x6C;
+    const CALLED_PARTY_NUMBER = 0x70;
+    const CONNECTED_NUMBER = 0x4C;
+    const KEYPAD = 0x2C;
 
     public $type;
     public $length;
index 9f7a6546857d2031dbc5cef01bb2be746993f048..8a6b79a9ada81bd2c03766218c0e99243ed8dc20 100644 (file)
@@ -13,6 +13,8 @@ class Log
     protected $logger = array(
         'msgData' => array(),
         'edss1msg' => array(),
+        'incomingCall' => array(),
+        'finishedCall' => array(),
     );
 
     /**
diff --git a/src/callnotifier/Logger/CallEcho.php b/src/callnotifier/Logger/CallEcho.php
new file mode 100644 (file)
index 0000000..fe8b9b4
--- /dev/null
@@ -0,0 +1,33 @@
+<?php
+namespace callnotifier;
+
+class Logger_CallEcho implements Logger
+{
+    public function log($type, $arData)
+    {
+        switch ($type) {
+        case 'incomingCall':
+            $this->displayIncoming($arData['call']);
+            break;
+        case 'finishedCall':
+            $this->displayFinished($arData['call']);
+            break;
+        }
+    }
+
+
+    protected function displayIncoming(CallMonitor_Call $call)
+    {
+        echo 'Incoming call from ' . $call->from
+            . ' to ' . $call->to . "\n";
+    }
+
+    protected function displayFinished(CallMonitor_Call $call)
+    {
+        echo 'Finished call from ' . $call->from
+            . ' to ' . $call->to
+            . ', length ' . date('H:i:s', $call->end - $call->start - 3600)
+            . "\n";
+    }
+}
+?>