summaryrefslogtreecommitdiff
path: root/localwebsite/handlers/InverterHandler.php
blob: 7878c4fb8f3a7895d2fec77a8e576311a9e4e629 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php

class InverterHandler extends RequestHandler
{

    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');
        $status = jsonDecode($inv->exec('get-status'))['data'];

        $this->tpl->set([
            'status' => $status,
            'html' => $this->renderStatusHtml($status)
        ]);
        $this->tpl->set_title('Инвертор');
        $this->tpl->render_page('inverter_page.twig');
    }

    public function GET_status_ajax() {
        global $config;
        $inv = new InverterdClient($config['inverterd_host'], $config['inverterd_port']);
        $inv->setFormat('json');
        $status = jsonDecode($inv->exec('get-status'))['data'];
        ajax_ok(['html' => $this->renderStatusHtml($status)]);
    }

    protected function renderStatusHtml(array $status)
    {
        $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_charging_current']['value'],
                $status['battery_charging_current']['unit']);
        else if ($power_direction == 'discharging')
            $charging_rate = sprintf(' @ %s %s',
                $status['battery_discharging_current']['value'],
                $status['battery_discharging_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>Generator:</b> %s %s',
                $status['grid_voltage']['unit'],
                $status['grid_voltage']['value']);
            $html .= sprintf(', %s %s',
                $status['grid_freq']['value'],
                $status['grid_freq']['unit']);
        }

        return nl2br($html);
    }

    public function GET_status_page_update()
    {

    }

}