From 16d47968b4938f3b60b97f374d45ad39bb0071b1 Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Sun, 11 Dec 2022 01:25:29 +0300 Subject: lws: inverter: allow changing osp --- localwebsite/handlers/InverterHandler.php | 48 +++++++++++++++++---------- localwebsite/htdocs/index.php | 1 + localwebsite/templates-web/inverter_page.twig | 6 ++++ 3 files changed, 38 insertions(+), 17 deletions(-) diff --git a/localwebsite/handlers/InverterHandler.php b/localwebsite/handlers/InverterHandler.php index 78fc1ab..7098e2c 100644 --- a/localwebsite/handlers/InverterHandler.php +++ b/localwebsite/handlers/InverterHandler.php @@ -3,37 +3,45 @@ class InverterHandler extends RequestHandler { - public function __construct() - { + public function __construct() { parent::__construct(); $this->tpl->add_static('inverter.js'); } - public function GET_status_page() - { - global $config; - $inv = new InverterdClient($config['inverterd_host'], $config['inverterd_port']); - $inv->setFormat('json'); + 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, - 'html' => $this->renderStatusHtml($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() { - global $config; - $inv = new InverterdClient($config['inverterd_host'], $config['inverterd_port']); - $inv->setFormat('json'); + $inv = $this->getClient(); $status = jsonDecode($inv->exec('get-status'))['data']; - ajax_ok(['html' => $this->renderStatusHtml($status)]); + $rated = jsonDecode($inv->exec('get-rated'))['data']; + ajax_ok(['html' => $this->renderStatusHtml($status, $rated)]); } - protected function renderStatusHtml(array $status) - { + protected function renderStatusHtml(array $status, array $rated) { $power_direction = strtolower($status['battery_power_direction']); $power_direction = preg_replace('/ge$/', 'ging', $power_direction); @@ -77,12 +85,18 @@ class InverterHandler extends RequestHandler $status['grid_freq']['unit']); } + $html .= "\n".sprintf('Priority: %s', + $rated['output_source_priority']); + return nl2br($html); } - public function GET_status_page_update() - { - + protected function getClient(): InverterdClient { + global $config; + $inv = new InverterdClient($config['inverterd_host'], $config['inverterd_port']); + $inv->setFormat('json'); + return $inv; } + } \ No newline at end of file diff --git a/localwebsite/htdocs/index.php b/localwebsite/htdocs/index.php index 0a4e8c5..d1fc0fd 100644 --- a/localwebsite/htdocs/index.php +++ b/localwebsite/htdocs/index.php @@ -19,6 +19,7 @@ $router->add('sms/', 'Modem sms'); // inverter $router->add('inverter/', 'Inverter status_page'); +$router->add('inverter/set-osp/', 'Inverter set_osp'); $router->add('inverter/status.ajax', 'Inverter status_ajax'); // misc diff --git a/localwebsite/templates-web/inverter_page.twig b/localwebsite/templates-web/inverter_page.twig index 2c3f8dd..c51e1bf 100644 --- a/localwebsite/templates-web/inverter_page.twig +++ b/localwebsite/templates-web/inverter_page.twig @@ -9,6 +9,12 @@ {{ html|raw }} +
+ + + +
+ {% js %} Inverter.poll(); {% endjs %} \ No newline at end of file -- cgit v1.2.3