sleep before reconnecting
[auerswald-callnotifier.git] / src / callnotifier / CLI.php
1 <?php
2 namespace callnotifier;
3
4
5 class CLI
6 {
7     protected $cliParser;
8
9     public function __construct()
10     {
11         $this->setupCli();
12     }
13
14     public function run()
15     {
16         $config = new Config();
17         try {
18             $result = $this->cliParser->parse();
19         } catch (\Exception $exc) {
20             $this->cliParser->displayError($exc->getMessage());
21         }
22
23         $this->fillConfig($config, $result);
24
25         $log = new Log();
26         if ($result->options['debug'] || $result->options['debugEdss1']) {
27             $debugLogger = new Logger_Debug();
28             $log->addLogger($debugLogger, '*');
29             if ($result->options['debugEdss1']) {
30                 $debugLogger->edss1MsgOnly = true;
31             }
32         }
33
34         $callMonitor = new CallMonitor($config, $log);
35
36         $configFile = $this->getConfigFile();
37         if ($configFile !== null) {
38             include $configFile;
39         }
40
41         $handler = new MessageHandler($config, $log, $callMonitor);
42
43         if ($config->replayFile !== null) {
44             $sourceClass = 'callnotifier\Source_File';
45         } else {
46             $sourceClass = 'callnotifier\Source_Remote';
47         }
48
49         try {
50             $source = new $sourceClass($config, $handler);
51             $source->run();
52         } catch (\Exception $e) {
53             $msg = 'Callnotifier error!' . "\n"
54                 . 'Code: ' . $e->getCode() . "\n"
55                 . 'Message: ' . $e->getMessage() . "\n";
56             file_put_contents('php://stderr', $msg);
57             exit(1);
58         }
59     }
60
61     public function setupCli()
62     {
63         $p = new \Console_CommandLine();
64         $p->description = 'Notifies about incoming calls on an Auerswald COMpact 3000';
65         $p->version = '0.0.1';
66
67         $p->addOption(
68             'host',
69             array(
70                 'short_name'  => '-h',
71                 'long_name'   => '--host',
72                 'description' => 'IP of COMpact 3000',
73                 'help_name'   => 'IP',
74                 'action'      => 'StoreString'
75             )
76         );
77
78         $p->addOption(
79             'dumpFile',
80             array(
81                 'long_name'   => '--dump',
82                 'description' => 'Dump messages into file for later replay',
83                 'help_name'   => 'FILE',
84                 'action'      => 'StoreString'
85             )
86         );
87         $p->addOption(
88             'replayFile',
89             array(
90                 'long_name'   => '--replay',
91                 'description' => "Replay messages from file instead from network",
92                 'help_name'   => 'FILE',
93                 'action'      => 'StoreString'
94             )
95         );
96
97         $p->addOption(
98             'debug',
99             array(
100                 'short_name'  => '-d',
101                 'long_name'   => '--debug',
102                 'description' => "Debug mode: Echo all received messages and events",
103                 'action'      => 'StoreTrue'
104             )
105         );
106         $p->addOption(
107             'debugEdss1',
108             array(
109                 'short_name'  => '-e',
110                 'long_name'   => '--debug-edss1',
111                 'description' => "Debug mode: Show EDSS1 messages only",
112                 'action'      => 'StoreTrue'
113             )
114         );
115
116         $this->cliParser = $p;
117     }
118
119     protected function fillConfig($config, $result)
120     {
121         $config->setIfNotEmpty('host', $result->options['host']);
122         $config->setIfNotEmpty('dumpFile', $result->options['dumpFile']);
123         $config->setIfNotEmpty('replayFile', $result->options['replayFile']);
124     }
125
126     /**
127      * Finds the path to the configuration file.
128      *
129      * The following locations are tried:
130      * - Git checkout: data/callnotifier.config.php
131      * - ~/.config/callnotifier.config.php
132      * - /etc/callnotifier.config.php
133      *
134      * @return string Full path of config file or NULL if no file found
135      */
136     protected function getConfigFile()
137     {
138         if (basename(dirname(__DIR__)) == 'src'
139             && file_exists(__DIR__ . '/../../data/callnotifier.config.php')
140         ) {
141             return __DIR__ . '/../../data/callnotifier.config.php';
142         }
143
144         if (isset($_ENV['HOME'])) {
145             $file = $_ENV['HOME'] . '/.config/callnotifier.config.php';
146             if (file_exists($file)) {
147                 return $file;
148             }
149         }
150
151         $file = '/etc/callnotifier.config.php';
152         if (file_exists($file)) {
153             return $file;
154         }
155
156         return null;
157     }
158 }
159
160 ?>