begin work on call detaillers (load name from ldap, load location from opengeodb)
[auerswald-callnotifier.git] / src / callnotifier / CLI.php
1 <?php
2 namespace callnotifier;
3
4
5 class CLI
6 {
7     protected $cliParser;
8     protected $config;
9
10     public function __construct()
11     {
12         $this->setupCli();
13     }
14
15     public function run()
16     {
17         $this->config = new Config();
18         try {
19             $result = $this->cliParser->parse();
20         } catch (\Exception $exc) {
21             $this->cliParser->displayError($exc->getMessage());
22         }
23
24         $this->fillConfig($this->config, $result);
25
26         $log = new Log();
27         if ($result->options['debug'] || $result->options['debugEdss1']) {
28             $debugLogger = new Logger_Debug();
29             $log->addLogger($debugLogger, '*');
30             if ($result->options['debugEdss1']) {
31                 $debugLogger->edss1MsgOnly = true;
32             }
33         }
34         $log->addLogger(
35             new Logger_CallEcho(), array('startingCall', 'finishedCall')
36         );
37
38         $callMonitor = new CallMonitor($this->config, $log);
39         $callMonitor->addDetailler(new CallMonitor_Detailler_LDAP());
40         $callMonitor->addDetailler(new CallMonitor_Detailler_OpenGeoDb());
41
42         $handler = new MessageHandler($this->config, $log, $callMonitor);
43
44         if ($this->config->replayFile !== null) {
45             $sourceClass = 'callnotifier\Source_File';
46         } else {
47             $sourceClass = 'callnotifier\Source_Remote';
48         }
49         $source = new $sourceClass($this->config, $handler);
50         $source->run();
51     }
52
53     public function setupCli()
54     {
55         $p = new \Console_CommandLine();
56         $p->description = 'Notifies about incoming calls on an Auerswald COMpact 3000';
57         $p->version = '0.0.1';
58
59         $p->addOption(
60             'host',
61             array(
62                 'short_name'  => '-h',
63                 'long_name'   => '--host',
64                 'description' => 'IP of COMpact 3000',
65                 'help_name'   => 'IP',
66                 'action'      => 'StoreString'
67             )
68         );
69
70         $p->addOption(
71             'dumpFile',
72             array(
73                 'long_name'   => '--dump',
74                 'description' => 'dump messages into file for later replay',
75                 'help_name'   => 'FILE',
76                 'action'      => 'StoreString'
77             )
78         );
79         $p->addOption(
80             'replayFile',
81             array(
82                 'long_name'   => '--replay',
83                 'description' => "Replay messages from file instead from network",
84                 'help_name'   => 'FILE',
85                 'action'      => 'StoreString'
86             )
87         );
88
89         $p->addOption(
90             'debug',
91             array(
92                 'short_name'  => '-d',
93                 'long_name'   => '--debug',
94                 'description' => "Debug mode: Echo all received messages and events",
95                 'action'      => 'StoreTrue'
96             )
97         );
98         $p->addOption(
99             'debugEdss1',
100             array(
101                 'short_name'  => '-e',
102                 'long_name'   => '--debug-edss1',
103                 'description' => "Debug mode: Show EDSS1 messages only",
104                 'action'      => 'StoreTrue'
105             )
106         );
107
108         $this->cliParser = $p;
109     }
110
111     protected function fillConfig($config, $result)
112     {
113         $config->setIfNotEmpty('host', $result->options['host']);
114         $config->setIfNotEmpty('dumpFile', $result->options['dumpFile']);
115         $config->setIfNotEmpty('replayFile', $result->options['replayFile']);
116     }
117 }
118
119 ?>