diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2022-07-18 16:29:20 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2022-07-18 16:29:20 +0300 |
commit | 9b40fbdd31f9366cf4fba4db04e85aa26e7c8a04 (patch) | |
tree | b5f328c7968a70ed1e0041b1ed67ad543fc2f5c3 /localwebsite/handlers/ModemHandler.php | |
parent | 36c90356aa291b65661b523e2f79375a72d588b0 (diff) |
lws: sms sending, inbox/outbox view, verbose modem info
Diffstat (limited to 'localwebsite/handlers/ModemHandler.php')
-rw-r--r-- | localwebsite/handlers/ModemHandler.php | 128 |
1 files changed, 112 insertions, 16 deletions
diff --git a/localwebsite/handlers/ModemHandler.php b/localwebsite/handlers/ModemHandler.php index 88fe463..a874a94 100644 --- a/localwebsite/handlers/ModemHandler.php +++ b/localwebsite/handlers/ModemHandler.php @@ -1,5 +1,9 @@ <?php +use libphonenumber\NumberParseException; +use libphonenumber\PhoneNumberFormat; +use libphonenumber\PhoneNumberUtil; + class ModemHandler extends RequestHandler { @@ -34,11 +38,39 @@ class ModemHandler extends RequestHandler ajax_ok([ 'html' => $this->tpl->render('modem_data.twig', [ 'loading' => false, + 'modem' => $id, 'modem_data' => $modem_data ]) ]); } + public function GET_verbose_page() { + global $config; + + list($modem) = $this->input('modem'); + if (!$modem) + $modem = array_key_first($config['modems']); + + list($signal, $status, $traffic, $device, $dialup_conn) = self::getModemData( + $config['modems'][$modem]['ip'], + $config['modems'][$modem]['legacy_token_auth'], + true); + + $data = [ + ['Signal', $signal], + ['Connection', $status], + ['Traffic', $traffic], + ['Device info', $device], + ['Dialup connection', $dialup_conn] + ]; + $this->tpl->set([ + 'data' => $data, + 'modem_name' => $config['modems'][$modem]['label'], + ]); + $this->tpl->set_title('Подробная информация о модеме '.$modem); + $this->tpl->render_page('modem_verbose_page.twig'); + } + public function GET_routing_smallhome_page() { global $config; @@ -122,41 +154,105 @@ class ModemHandler extends RequestHandler $this->tpl->render_page('routing_dhcp_page.twig'); } - public function GET_sms_page() { + public function GET_sms() { global $config; - list($selected) = $this->input('modem'); + list($selected, $is_outbox, $error, $sent) = $this->input('modem, b:outbox, error, b:sent'); if (!$selected) $selected = array_key_first($config['modems']); $cfg = $config['modems'][$selected]; $e3372 = new E3372($cfg['ip'], $cfg['legacy_token_auth']); - $messages = $e3372->getSMSList(); + $messages = $e3372->getSMSList(1, 20, $is_outbox); $this->tpl->set([ 'modems_list' => array_keys($config['modems']), 'modems' => $config['modems'], 'selected_modem' => $selected, - 'messages' => $messages - ]); - $this->tpl->set_title('Модемы: SMS-сообщения'); + 'messages' => $messages, + 'is_outbox' => $is_outbox, + 'error' => $error, + 'is_sent' => $sent + ]); + + $direction = $is_outbox ? 'исходящие' : 'входящие'; + $this->tpl->set_title('SMS-сообщения ('.$direction.', '.$selected.')'); $this->tpl->render_page('sms_page.twig'); } - protected static function getModemData(string $ip, bool $need_auth = true): array { + public function POST_sms() { + global $config; + + list($selected, $is_outbox, $phone, $text) = $this->input('modem, b:outbox, phone, text'); + if (!$selected) + $selected = array_key_first($config['modems']); + + $return_url = '/sms/?modem='.$selected; + if ($is_outbox) + $return_url .= '&outbox=1'; + + $go_back = function(?string $error = null) use ($return_url) { + if (!is_null($error)) + $return_url .= '&error='.urlencode($error); + else + $return_url .= '&sent=1'; + redirect($return_url); + }; + + $phone = preg_replace('/\s+/', '', $phone); + $country = null; + if (!startsWith($phone, '+')) + $country = 'RU'; + + $phoneUtil = PhoneNumberUtil::getInstance(); + try { + $number = $phoneUtil->parse($phone, $country); + } catch (NumberParseException $e) { + debugError(__METHOD__.': failed to parse number '.$phone.': '.$e->getMessage()); + $go_back('Неверный номер ('.$e->getMessage().')'); + return; + } + + if (!$phoneUtil->isValidNumber($number)) { + $go_back('Неверный номер'); + return; + } + + $phone = $phoneUtil->format($number, PhoneNumberFormat::E164); + + $cfg = $config['modems'][$selected]; + $e3372 = new E3372($cfg['ip'], $cfg['legacy_token_auth']); + + $result = $e3372->sendSMS($phone, $text); + debugLog($result); + + $go_back(); + } + + protected static function getModemData(string $ip, + bool $need_auth = true, + bool $get_raw_data = false): array { $modem = new E3372($ip, $need_auth); + $signal = $modem->getDeviceSignal(); $status = $modem->getMonitoringStatus(); $traffic = $modem->getTrafficStats(); - return [ - 'type' => e3372::getNetworkTypeLabel($status['CurrentNetworkType']), - 'level' => $status['SignalIcon'] ?? 0, - 'rssi' => $signal['rssi'], - 'sinr' => $signal['sinr'], - 'connected_time' => secondsToTime($traffic['CurrentConnectTime']), - 'downloaded' => bytesToUnitsLabel(gmp_init($traffic['CurrentDownload'])), - 'uploaded' => bytesToUnitsLabel(gmp_init($traffic['CurrentUpload'])), - ]; + + if ($get_raw_data) { + $device_info = $modem->getDeviceInformation(); + $dialup_conn = $modem->getDialupConnection(); + return [$signal, $status, $traffic, $device_info, $dialup_conn]; + } else { + return [ + 'type' => e3372::getNetworkTypeLabel($status['CurrentNetworkType']), + 'level' => $status['SignalIcon'] ?? 0, + 'rssi' => $signal['rssi'], + 'sinr' => $signal['sinr'], + 'connected_time' => secondsToTime($traffic['CurrentConnectTime']), + 'downloaded' => bytesToUnitsLabel(gmp_init($traffic['CurrentDownload'])), + 'uploaded' => bytesToUnitsLabel(gmp_init($traffic['CurrentUpload'])), + ]; + } } protected static function getCurrentSmallHomeUpstream() { |