diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2024-02-17 02:48:57 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2024-02-17 02:48:57 +0300 |
commit | b7f1d55c9b4de4d21b11e5615a5dc8be0d4e883c (patch) | |
tree | df3cba57518e21590d579b014867611002d92de5 /localwebsite/handlers | |
parent | c4ace358182d1f58724336714490e3caac6b60df (diff) | |
parent | 05c85757b8e2340441057d9ddfde2e9649ae8676 (diff) |
Merge branch 'website-python-rewrite'
Diffstat (limited to 'localwebsite/handlers')
-rw-r--r-- | localwebsite/handlers/InverterHandler.php | 104 | ||||
-rw-r--r-- | localwebsite/handlers/MiscHandler.php | 42 | ||||
-rw-r--r-- | localwebsite/handlers/ModemHandler.php | 170 |
3 files changed, 0 insertions, 316 deletions
diff --git a/localwebsite/handlers/InverterHandler.php b/localwebsite/handlers/InverterHandler.php deleted file mode 100644 index 5fa269f..0000000 --- a/localwebsite/handlers/InverterHandler.php +++ /dev/null @@ -1,104 +0,0 @@ -<?php - -class InverterHandler extends RequestHandler -{ - - public function __construct() { - parent::__construct(); - $this->tpl->add_static('inverter.js'); - } - - public function GET_status_page() { - $inv = $this->getClient(); - - $status = jsonDecode($inv->exec('get-status'))['data']; - $rated = jsonDecode($inv->exec('get-rated'))['data']; - - $this->tpl->set([ - 'status' => $status, - 'rated' => $rated, - 'html' => $this->renderStatusHtml($status, $rated) - ]); - $this->tpl->set_title('Инвертор'); - $this->tpl->render_page('inverter_page.twig'); - } - - public function GET_set_osp() { - list($osp) = $this->input('e:value(=sub|sbu)'); - $inv = $this->getClient(); - try { - $inv->exec('set-output-source-priority', [strtoupper($osp)]); - } catch (Exception $e) { - die('Ошибка: '.jsonDecode($e->getMessage())['message']); - } - redirect('/inverter/'); - } - - public function GET_status_ajax() { - $inv = $this->getClient(); - $status = jsonDecode($inv->exec('get-status'))['data']; - $rated = jsonDecode($inv->exec('get-rated'))['data']; - ajax_ok(['html' => $this->renderStatusHtml($status, $rated)]); - } - - protected function renderStatusHtml(array $status, array $rated) { - $power_direction = strtolower($status['battery_power_direction']); - $power_direction = preg_replace('/ge$/', 'ging', $power_direction); - - $charging_rate = ''; - if ($power_direction == 'charging') - $charging_rate = sprintf(' @ %s %s', - $status['battery_charge_current']['value'], - $status['battery_charge_current']['unit']); - else if ($power_direction == 'discharging') - $charging_rate = sprintf(' @ %s %s', - $status['battery_discharge_current']['value'], - $status['battery_discharge_current']['unit']); - - $html = sprintf('<b>Battery:</b> %s %s', - $status['battery_voltage']['value'], - $status['battery_voltage']['unit']); - $html .= sprintf(' (%s%s, ', - $status['battery_capacity']['value'], - $status['battery_capacity']['unit']); - $html .= sprintf('%s%s)', - $power_direction, - $charging_rate); - - $html .= "\n".sprintf('<b>Load:</b> %s %s', - $status['ac_output_active_power']['value'], - $status['ac_output_active_power']['unit']); - $html .= sprintf(' (%s%%)', - $status['output_load_percent']['value']); - - if ($status['pv1_input_power']['value'] > 0) - $html .= "\n".sprintf('<b>Input power:</b> %s %s', - $status['pv1_input_power']['value'], - $status['pv1_input_power']['unit']); - - if ($status['grid_voltage']['value'] > 0 or $status['grid_freq']['value'] > 0) { - $html .= "\n".sprintf('<b>AC input:</b> %s %s', - $status['grid_voltage']['value'], - $status['grid_voltage']['unit']); - $html .= sprintf(', %s %s', - $status['grid_freq']['value'], - $status['grid_freq']['unit']); - } - - $html .= "\n".sprintf('<b>Priority:</b> %s', - $rated['output_source_priority']); - - return nl2br($html); - } - - protected function getClient(): InverterdClient { - global $config; - if (isset($_GET['alt']) && $_GET['alt'] == 1) - $config['inverterd_host'] = '192.168.5.223'; - $inv = new InverterdClient($config['inverterd_host'], $config['inverterd_port']); - $inv->setFormat('json'); - return $inv; - } - - -} diff --git a/localwebsite/handlers/MiscHandler.php b/localwebsite/handlers/MiscHandler.php index 4c5a25e..efaca22 100644 --- a/localwebsite/handlers/MiscHandler.php +++ b/localwebsite/handlers/MiscHandler.php @@ -3,17 +3,6 @@ class MiscHandler extends RequestHandler { - public function GET_main() { - global $config; - $this->tpl->set_title('Главная'); - $this->tpl->set([ - 'grafana_sensors_url' => $config['grafana_sensors_url'], - 'grafana_inverter_url' => $config['grafana_inverter_url'], - 'cameras' => $config['cam_list']['labels'] - ]); - $this->tpl->render_page('index.twig'); - } - public function GET_sensors_page() { global $config; @@ -30,29 +19,6 @@ class MiscHandler extends RequestHandler $this->tpl->render_page('sensors.twig'); } - public function GET_pump_page() { - global $config; - - if (isset($_GET['alt']) && $_GET['alt'] == 1) - $config['pump_host'] = '192.168.5.223'; - - list($set) = $this->input('set'); - $client = new GPIORelaydClient($config['pump_host'], $config['pump_port']); - - if ($set == GPIORelaydClient::STATUS_ON || $set == GPIORelaydClient::STATUS_OFF) { - $client->setStatus($set); - redirect('/pump/'); - } - - $status = $client->getStatus(); - - $this->tpl->set([ - 'status' => $status - ]); - $this->tpl->set_title('Насос'); - $this->tpl->render_page('pump.twig'); - } - public function GET_cams() { global $config; @@ -160,12 +126,4 @@ class MiscHandler extends RequestHandler } } - public function GET_debug() { - print_r($_SERVER); - } - - public function GET_phpinfo() { - phpinfo(); - } - } diff --git a/localwebsite/handlers/ModemHandler.php b/localwebsite/handlers/ModemHandler.php index 23e4c9a..94ad75b 100644 --- a/localwebsite/handlers/ModemHandler.php +++ b/localwebsite/handlers/ModemHandler.php @@ -7,71 +7,6 @@ use libphonenumber\PhoneNumberUtil; class ModemHandler extends RequestHandler { - public function __construct() - { - parent::__construct(); - $this->tpl->add_static('modem.js'); - } - - public function GET_status_page() { - global $config; - - $this->tpl->set([ - 'modems' => $config['modems'], - 'js_modems' => array_keys($config['modems']), - ]); - - $this->tpl->set_title('Состояние модемов'); - $this->tpl->render_page('modem_status_page.twig'); - } - - public function GET_status_get_ajax() { - global $config; - list($id) = $this->input('id'); - if (!isset($config['modems'][$id])) - ajax_error('invalid modem id: '.$id); - - $modem_data = self::getModemData( - $config['modems'][$id]['ip'], - $config['modems'][$id]['legacy_token_auth']); - - 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; @@ -160,111 +95,6 @@ class ModemHandler extends RequestHandler $this->tpl->render_page('routing_dhcp_page.twig'); } - public function GET_sms() { - global $config; - - 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(1, 20, $is_outbox); - - $this->tpl->set([ - 'modems_list' => array_keys($config['modems']), - 'modems' => $config['modems'], - 'selected_modem' => $selected, - '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'); - } - - 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); - - // при отправке смс на короткие номера не надо использовать libphonenumber и вот это вот всё - if (strlen($phone) > 4) { - $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(); - - 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 getCurrentUpstream() { global $config; |