<?php

namespace app\models\mikrotik\system;

use yii\base\Model;
use Yii;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

use PEAR2\Net\RouterOS;
use app\models\mikrotik\MikrotikConnect;

class Resources extends Model {

    public $uptime;
    public $version;
    public $build_time;
    public $free_memory;
    public $total_memory;
    public $cpu;
    public $cpu_count;
    public $cpu_frequency;
    public $cpu_load;
    public $free_hdd_space;
    public $total_hdd_space;
    public $write_sect_since_reboot;
    public $write_sect_total;
    public $bad_blocks;
    public $architecture_name;
    public $board_name;
    public $platform;

    public function rules() {
        return [
            ['uptime', 'string'],
            ['version', 'string'],
            ['build_time', 'string'],
            ['free_memory', 'string'],
            ['total_memory', 'string'],
            ['cpu', 'string'],
            ['cpu_count', 'string'],
            ['cpu_frequency', 'string'],
            ['cpu_load', 'string'],
            ['free_hdd_space', 'string'],
            ['total_hdd_space', 'string'],
            ['write_sect_since_reboot', 'string'],
            ['write_sect_total', 'string'],
            ['bad_blocks', 'string'],
            ['architecture_name', 'string'],
            ['board_name', 'string'],
            ['platform', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/system/resource/getall');
        $response = $connect->getConnection()->sendSync($getRequest);

        $this->uptime = $response->getProperty('uptime');
        $this->version = $response->getProperty('version');
        $this->build_time = $response->getProperty('build-time');
        $this->free_memory = $response->getProperty('free-memory');
        $this->total_memory = $response->getProperty('total-memory');
        $this->cpu = $response->getProperty('cpu');
        $this->cpu_count = $response->getProperty('cpu-count');
        $this->cpu_frequency = $response->getProperty('cpu-frequency');
        $this->cpu_load = $response->getProperty('cpu-load');
        $this->free_hdd_space = $response->getProperty('free-hdd-space');
        $this->total_hdd_space = $response->getProperty('total-hdd-space');
        $this->write_sect_since_reboot = $response->getProperty('write-sect-since-reboot');
        $this->write_sect_total = $response->getProperty('write-sect-total');
        $this->bad_blocks = $response->getProperty('bad-blocks');
        $this->architecture_name = $response->getProperty('architecture-name');
        $this->board_name = $response->getProperty('board-name');
        $this->platform = $response->getProperty('platform');


        return;
    }

    public function reboot() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/system/reboot');
        $connect->getConnection()->sendSync($getRequest);
        return;
        
    }

}
