2 namespace callnotifier;
9 * Array of logger object arrays.
10 * Key is the notification type, value is an array of logger objects
11 * that want to get notified about the type.
15 protected $logger = array(
17 'edss1msg' => array(),
21 public function __construct($config)
23 $this->config = $config;
30 * @param Logger $logger Logger object to register
31 * @param array|string $types Single notification type or array of such
32 * types. "*" means "register for all types".
36 public function addLogger(Logger $logger, $types)
39 $types = array_keys($this->logger);
41 $types = (array)$types;
43 foreach ($types as $type) {
44 if (!isset($this->logger[$type])) {
45 throw new \Exception('Unknown log type: ' . $type);
47 $this->logger[$type][] = $logger;
51 public function handle($msg)
53 if ($this->config->dumpFile !== null) {
56 if (substr($msg, 0, 9) != '[DKANPROT') {
57 //unknown message type
60 $regex = '#^\\[DKANPROT-([^ ]+) ([0-9]+)\\] (.*)$#';
61 if (!preg_match($regex, $msg, $matches)) {
62 //message should always be that way
65 list(, $type, $someid, $details) = $matches;
75 if ($type == 'Debug') {
76 $this->parseEDSS1($details);
81 * Example string: "T02: 00 A3 06 0A 08 01 01 5A FF 0A"
83 * @param string $details Detail string of a debug message
86 protected function parseEDSS1($details)
88 if ($details{0} != 'T' && $details{0} != 'N') {
89 //we only want byte data
92 if (substr($details, 16, 4) != ' 08 ') {
93 //only E-DSS-1, no other packets
97 $bytestring = substr($details, 5);
98 $bytes = static::getBytesFromHexString($bytestring);
100 $mp = new EDSS1_Parser();
101 $msg = $mp->parse($bytes);
103 $this->log('edss1msg', array('msg' => $msg));
106 protected function log($type, $arData)
108 if (!isset($this->logger[$type])) {
109 throw new \Exception('Unknown log type: ' . $type);
112 if (count($this->logger[$type])) {
113 foreach ($this->logger[$type] as $logger) {
114 $logger->log($type, $arData);
119 public static function getBytesFromHexString($bytestring)
122 foreach (explode(' ', $bytestring) as $strbyte) {
123 $bytes .= chr(hexdec($strbyte));
128 protected function prepareDump()
130 if ($this->config->dumpFile === null) {
133 $this->dumpHdl = fopen($this->config->dumpFile, 'w');
134 if (!$this->dumpHdl) {
135 throw new Exception('Cannot open replay file for reading');
139 protected function dump($msg)
141 fwrite($this->dumpHdl, $msg . "\n");