<?php

namespace app\models\mikrotik\ip\snmp;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

use yii\base\Model;
use Yii;
use PEAR2\Net\RouterOS;
use app\models\mikrotik\MikrotikConnect;

class Snmp extends Model {

    public $enabled;
    public $contact;
    public $location;
    public $engine_id;
    public $trap_target;
    public $trap_community;
    public $trap_version;
    public $trap_generators;
    public $trap_interfaces;

    public function rules() {
        return [
            ['enabled', 'string'],
            ['contact', 'string'],
            ['location', 'string'],
            ['engine_id', 'string'],
            ['trap_target', 'string'],
            ['trap_community', 'string'],
            ['trap_version', 'string'],
        ];
    }

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

        $this->enabled = $response->getProperty('enabled');
        $this->contact = $response->getProperty('contact');
        $this->location = $response->getProperty('location');
        $this->engine_id = $response->getProperty('engine-id');
        $this->trap_target = $response->getProperty('trap-target');
        $this->trap_community = $response->getProperty('trap-community');
        $this->trap_version = $response->getProperty('trap-version');
        $this->trap_generators = $response->getProperty('trap-generators');
        $this->trap_interfaces = $response->getProperty('trap-interfaces');



        return $this;
    }

    public function update() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/snmp/set');

        if ($this->enabled != "") {
            $setRequest->setargument('enabled', (($this->enabled == true) ? "true" : "false"));
        }
        $setRequest->setargument('contact', $this->contact);
        $setRequest->setargument('location', $this->location);
        if ($this->engine_id != "") {
            $setRequest->setargument('engine-id', $this->engine_id);
        }
        if ($this->trap_target != "") {
            $setRequest->setargument('trap-target', $this->trap_target);
        }
        if ($this->trap_community != "") {
            $setRequest->setargument('trap-community', $this->trap_community);
        }
        if ($this->trap_version != "") {
            $setRequest->setargument('trap-version', $this->trap_version);
        }
        if ($this->trap_generators != "") {
            $setRequest->setargument('trap-generators', $this->trap_generators);
        }
        if ($this->trap_interfaces != "") {
            $setRequest->setargument('trap-interfaces', $this->trap_interfaces);
        }
        $connect->getConnection()->sendSync($setRequest);
    }

}
