16c95b0f6f6ea5845634632929ba0cf7eab9fc7b
[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(
40             new CallMonitor_Detailler_LDAP(
41                 array(
42                     'host' => 'ldap.home.cweiske.de',
43                     'basedn' => 'ou=adressbuch,dc=cweiske,dc=de',
44                     'binddn' => 'cn=readonly,ou=users,dc=cweiske,dc=de',
45                     'bindpw' => 'readonly'
46                 )
47             )
48         );
49         $callMonitor->addDetailler(
50             new CallMonitor_Detailler_OpenGeoDb(
51                 'mysql:host=dojo;dbname=opengeodb',
52                 'opengeodb-read',
53                 'opengeodb'
54             )
55         );
56
57         $handler = new MessageHandler($this->config, $log, $callMonitor);
58
59         if ($this->config->replayFile !== null) {
60             $sourceClass = 'callnotifier\Source_File';
61         } else {
62             $sourceClass = 'callnotifier\Source_Remote';
63         }
64         $source = new $sourceClass($this->config, $handler);
65         $source->run();
66     }
67
68     public function setupCli()
69     {
70         $p = new \Console_CommandLine();
71         $p->description = 'Notifies about incoming calls on an Auerswald COMpact 3000';
72         $p->version = '0.0.1';
73
74         $p->addOption(
75             'host',
76             array(
77                 'short_name'  => '-h',
78                 'long_name'   => '--host',
79                 'description' => 'IP of COMpact 3000',
80                 'help_name'   => 'IP',
81                 'action'      => 'StoreString'
82             )
83         );
84
85         $p->addOption(
86             'dumpFile',
87             array(
88                 'long_name'   => '--dump',
89                 'description' => 'dump messages into file for later replay',
90                 'help_name'   => 'FILE',
91                 'action'      => 'StoreString'
92             )
93         );
94         $p->addOption(
95             'replayFile',
96             array(
97                 'long_name'   => '--replay',
98                 'description' => "Replay messages from file instead from network",
99                 'help_name'   => 'FILE',
100                 'action'      => 'StoreString'
101             )
102         );
103
104         $p->addOption(
105             'debug',
106             array(
107                 'short_name'  => '-d',
108                 'long_name'   => '--debug',
109                 'description' => "Debug mode: Echo all received messages and events",
110                 'action'      => 'StoreTrue'
111             )
112         );
113         $p->addOption(
114             'debugEdss1',
115             array(
116                 'short_name'  => '-e',
117                 'long_name'   => '--debug-edss1',
118                 'description' => "Debug mode: Show EDSS1 messages only",
119                 'action'      => 'StoreTrue'
120             )
121         );
122
123         $this->cliParser = $p;
124     }
125
126     protected function fillConfig($config, $result)
127     {
128         $config->setIfNotEmpty('host', $result->options['host']);
129         $config->setIfNotEmpty('dumpFile', $result->options['dumpFile']);
130         $config->setIfNotEmpty('replayFile', $result->options['replayFile']);
131     }
132 }
133
134 ?>