fix copied error message
[auerswald-callnotifier.git] / src / callnotifier / MessageHandler.php
index b4ed6455706a882129527b05d4d1b8fd94dcae50..78a726de60ada2566d71b3bc4263a07e11b0844a 100644 (file)
@@ -6,10 +6,12 @@ class MessageHandler
     protected $dumpHdl;
 
 
-    public function __construct($config)
+    public function __construct($config, $log, $callMonitor)
     {
         $this->config = $config;
         $this->prepareDump();
+        $this->log = $log;
+        $this->callMonitor = $callMonitor;
     }
 
     public function handle($msg)
@@ -27,18 +29,56 @@ class MessageHandler
             return false;
         }
         list(, $type, $someid, $details) = $matches;
+        $this->log->log(
+            'msgData',
+            array(
+                'type' => $type,
+                'id' => $someid,
+                'details' => $details
+            )
+        );
 
-        if ($type != 'Info') {
-            //we only want info messages
-            var_dump($type . ': ' . $details);
+        if ($type == 'Debug') {
+            $msg = $this->parseEDSS1($details);
+            if (is_object($msg)) {
+                $this->log->log('edss1msg', array('msg' => $msg));
+                $this->callMonitor->handle($msg);
+            }
+        }
+    }
+
+    /**
+     * Example string: "T02: 00 A3 06 0A 08 01 01 5A FF 0A"
+     *
+     * @param string $details Detail string of a debug message
+     *
+     * @return EDSS1_Message The retrieved message, NULL if none.
+     */
+    protected function parseEDSS1($details)
+    {
+        if ($details{0} != 'T' && $details{0} != 'N') {
+            //we only want byte data
+            return;
+        }
+        if (substr($details, 16, 4) != ' 08 ') {
+            //only E-DSS-1, no other packets
             return;
         }
-        //Vegw/Ets-Cref:[0xffef]/[0x64] - VEGW_SETUP from upper layer to internal destination: CGPN[**22]->CDPN[41], 
-        var_dump($details);
-        $regex = '#CGPN\\[([^\\]]+)\\]->CDPN\\[([^\\]]+)\\]#';
-        if (preg_match($regex, $details, $matches)) {
-            var_dump('a call!', $matches);
+        
+        $bytestring = substr($details, 5);
+        $bytes = static::getBytesFromHexString($bytestring);
+
+        $mp = new EDSS1_Parser();
+        return $mp->parse($bytes);
+    }
+
+    public static function getBytesFromHexString($bytestring)
+    {
+        $bytes = '';
+        foreach (explode(' ', $bytestring) as $strbyte) {
+            $bytes .= chr(hexdec($strbyte));
         }
+        return $bytes;
     }
 
     protected function prepareDump()
@@ -48,7 +88,7 @@ class MessageHandler
         }
         $this->dumpHdl = fopen($this->config->dumpFile, 'w');
         if (!$this->dumpHdl) {
-            throw new Exception('Cannot open replay file for reading');
+            throw new Exception('Cannot open dump file for writing');
         }
     }