Commit eeae4c67 authored by Roman Ondráček's avatar Roman Ondráček

IQRF Net: refactor TR configuration

Signed-off-by: Roman Ondráček's avatarRoman Ondráček <ondracek.roman@centrum.cz>
parent 8d3193cc
Pipeline #2101 passed with stages
in 17 minutes and 34 seconds
......@@ -21,7 +21,7 @@ declare(strict_types = 1);
namespace App\IqrfNetModule\Forms;
use App\CoreModule\Forms\FormFactory;
use App\CoreModule\Presenters\ProtectedPresenter;
use App\IqrfNetModule\Presenters\TrConfigPresenter;
use Nette\Application\UI\Form;
use Nette\SmartObject;
......@@ -35,7 +35,7 @@ class ChangeAddressFormFactory {
private $factory;
/**
* @var ProtectedPresenter Protected presenter
* @var TrConfigPresenter TR configuration presenter
*/
private $presenter;
......@@ -49,10 +49,10 @@ class ChangeAddressFormFactory {
/**
* Creates change a network device address form
* @param ProtectedPresenter $presenter Protected presenter
* @param TrConfigPresenter $presenter TR configuration presenter
* @return Form Change a network device address
*/
public function create(ProtectedPresenter $presenter): Form {
public function create(TrConfigPresenter $presenter): Form {
$this->presenter = $presenter;
$form = $this->factory->create('iqrfnet.changeAddress');
$form->addInteger('address', 'address')
......
......@@ -75,15 +75,15 @@ class TrConfigFormFactory {
*/
public function create(TrConfigPresenter $presenter): Form {
$this->presenter = $presenter;
$this->load();
$form = $this->factory->create('iqrfnet.trConfig');
$this->addRfConfiguration($form);
$this->addRfpgwConfiguration($form);
$this->addDpaEmbeddedPeripherals($form);
$this->addDpaOtherConfiguration($form);
$form->addSubmit('save', 'save');
$form->addSubmit('save', 'save')
->setHtmlAttribute('class', 'ajax btn btn-primary');
$form->addProtection('core.errors.form-timeout');
$form->setDefaults($this->configuration);
$form->setDefaults($presenter->template->configuration ?? []);
$form->onSuccess[] = [$this, 'save'];
return $form;
}
......@@ -221,34 +221,15 @@ class TrConfigFormFactory {
}
}
/**
* Loads IQRF TR configuration into the form
*/
private function load(): void {
$address = (int) $this->presenter->getParameter('address', 0);
try {
$dpa = $this->manager->read($address);
} catch (DpaErrorException | EmptyResponseException | JsonException | UserErrorException $e) {
return;
}
if (!array_key_exists('response', $dpa)) {
return;
}
$this->configuration = $dpa['response']['data']['rsp'];
if (array_key_exists('stdAndLpNetwork', $this->configuration)) {
$this->configuration['stdAndLpNetwork'] = (int) $this->configuration['stdAndLpNetwork'];
}
}
/**
* Writes IQRF TR configuration from the form
* @param Form $form Set TR configuration form
*/
public function save(Form $form): void {
$address = $this->presenter->getParameter('id', 0);
$address = $this->presenter->getParameter('address', 0);
$config = $form->getValues('array');
if (array_key_exists('stdAndLpNetwork', $config)) {
$config['stdAndLpNetwork'] = boolval($config['stdAndLpNetwork']);
$config['stdAndLpNetwork'] = (bool) $config['stdAndLpNetwork'];
}
try {
$this->manager->write($address, $config);
......
......@@ -21,10 +21,15 @@ declare(strict_types = 1);
namespace App\IqrfNetModule\Presenters;
use App\CoreModule\Presenters\ProtectedPresenter;
use App\IqrfNetModule\Exceptions\DpaErrorException;
use App\IqrfNetModule\Exceptions\EmptyResponseException;
use App\IqrfNetModule\Exceptions\UserErrorException;
use App\IqrfNetModule\Forms\ChangeAddressFormFactory;
use App\IqrfNetModule\Forms\SecurityFormFactory;
use App\IqrfNetModule\Forms\TrConfigFormFactory;
use App\IqrfNetModule\Models\TrConfigManager;
use Nette\Application\UI\Form;
use Nette\Utils\JsonException;
/**
* TR configuration presenter
......@@ -49,6 +54,20 @@ class TrConfigPresenter extends ProtectedPresenter {
*/
public $securityFormFactory;
/**
* @var TrConfigManager IQRF TR configuration manager
*/
protected $manager;
/**
* Constructor
* @param TrConfigManager $manager IQRF TR configuration manager
*/
public function __construct(TrConfigManager $manager) {
$this->manager = $manager;
parent::__construct();
}
/**
* Create the change a network device address form
* @return Form Change a network device address form
......@@ -73,4 +92,37 @@ class TrConfigPresenter extends ProtectedPresenter {
return $this->securityFormFactory->create($this);
}
/**
* Loads TR configuration
*/
public function loadConfiguration(): void {
try {
$dpa = $this->manager->read((int) $this->getParameter('address'));
if (!array_key_exists('response', $dpa)) {
$this->template->configuration = null;
$this->flashError('iqrfnet.trConfiguration.read.failure');
return;
}
$configuration = $dpa['response']['data']['rsp'];
if (array_key_exists('stdAndLpNetwork', $configuration)) {
$configuration['stdAndLpNetwork'] = (int) $configuration['stdAndLpNetwork'];
}
$this->template->configuration = $configuration;
} catch (DpaErrorException | EmptyResponseException | JsonException | UserErrorException $e) {
$this->template->configuration = null;
$this->flashError('iqrfnet.trConfiguration.read.failure');
}
$this->redrawControl('forms');
}
/**
* Renders TR configuration page
* @param int $address TR address
*/
public function renderDefault(int $address = 0): void {
if (!$this->isAjax()) {
$this->loadConfiguration();
}
}
}
......@@ -25,19 +25,24 @@
{control iqrfNetAddressForm}
</div>
</div>
<div class='box box-primary'>
<header class='box-header with-border'>
<h2 class='box-title'>{_iqrfnet.trConfig.title}</h2>
</header>
<section class='box-body'>
{control iqrfNetTrForm}
</section>
</div>
<div class='box box-primary'>
<header class='box-header with-border'>
<h2 class='box-title'>{_iqrfnet.security.title}</h2>
</header>
<section class='box-body'>
{control iqrfNetSecurityForm}
</section>
</div>
{snippet forms}
{if $configuration !== null}
<div class='box box-primary'>
<header class='box-header with-border'>
<h2 class='box-title'>{_iqrfnet.trConfig.title}</h2>
</header>
<section class='box-body'>
{control iqrfNetTrForm}
</section>
</div>
<div class='box box-primary'>
<header class='box-header with-border'>
<h2 class='box-title'>{_iqrfnet.security.title}</h2>
</header>
<section class='box-body'>
{control iqrfNetSecurityForm}
</section>
</div>
{/if}
{/snippet}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment