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

IQRF net: fix TR configuration form (fix #220)

Signed-off-by: Roman Ondráček's avatarRoman Ondráček <ondracek.roman@centrum.cz>
parent eddcf43b
Pipeline #2407 passed with stages
in 28 minutes and 4 seconds
......@@ -27,6 +27,8 @@ use App\IqrfNetModule\Exceptions\UserErrorException;
use App\IqrfNetModule\Models\TrConfigManager;
use App\IqrfNetModule\Presenters\TrConfigPresenter;
use Nette\Application\UI\Form;
use Nette\Caching\Cache;
use Nette\Caching\IStorage;
use Nette\Forms\Controls\TextInput;
use Nette\SmartObject;
use Nette\Utils\JsonException;
......@@ -39,11 +41,21 @@ class TrConfigFormFactory {
use SmartObject;
/**
* @var int Address
*/
private $address;
/**
* @var stdClass TR configuration
*/
protected $configuration;
/**
* @var Cache Chache
*/
private $cache;
/**
* @var TrConfigManager IQRF TR configuration manager
*/
......@@ -63,11 +75,12 @@ class TrConfigFormFactory {
* Constructor
* @param FormFactory $factory Generic form factory
* @param TrConfigManager $manager IQRF TR configuration manager
* @param IStorage $storage Cache storage
*/
public function __construct(FormFactory $factory, TrConfigManager $manager) {
public function __construct(FormFactory $factory, TrConfigManager $manager, IStorage $storage) {
$this->cache = new Cache($storage, 'trConfiguration');
$this->factory = $factory;
$this->manager = $manager;
$this->configuration = new stdClass();
}
/**
......@@ -77,7 +90,8 @@ class TrConfigFormFactory {
*/
public function create(TrConfigPresenter $presenter): Form {
$this->presenter = $presenter;
$this->configuration = $presenter->template->configuration ?? [];
$this->address = (int) $presenter->getParameter('address', 0);
$this->configuration = $this->cache->load('trConfiguration' . $this->address) ?? new stdClass();
$form = $this->factory->create('iqrfnet.trConfig');
$this->addRfConfiguration($form);
$this->addRfpgwConfiguration($form);
......@@ -108,14 +122,14 @@ class TrConfigFormFactory {
}
$rfChannels = ['rfChannelA', 'rfChannelB'];
foreach ($rfChannels as $rfChannel) {
$form->addInteger($rfChannel, $rfChannel);
$this->setRfChannelRule($form[$rfChannel]);
$input = $form->addInteger($rfChannel, $rfChannel);
$this->setRfChannelRule($input);
}
$subChannels = ['rfSubChannelA', 'rfSubChannelB'];
foreach ($subChannels as $subChannel) {
if (isset($this->configuration->$subChannel)) {
$form->addInteger($subChannel, $subChannel);
$this->setRfChannelRule($form[$subChannel]);
$input = $form->addInteger($subChannel, $subChannel);
$this->setRfChannelRule($input);
}
}
if (isset($this->configuration->stdAndLpNetwork)) {
......@@ -194,7 +208,7 @@ class TrConfigFormFactory {
foreach ($changeablePeripherals as $peripheral) {
$embeddedPeripherals->addCheckbox($peripheral, 'embPers.' . $peripheral);
}
$dpaVersion = $this->presenter->template->dpaVersion ?? '99.99';
$dpaVersion = $this->cache->load('dpaVersion' . $this->address) ?? '99.99';
if (version_compare($dpaVersion, '4.00', '>=')) {
if ($this->presenter->getUser()->isInRole('power')) {
$frc = $embeddedPeripherals->addCheckbox('frc', 'embPers.frc')
......@@ -214,7 +228,10 @@ class TrConfigFormFactory {
*/
private function addDpaOtherConfiguration(Form &$form): void {
$form->addGroup('dpaOther');
$form->addCheckbox('customDpaHandler', 'customDpaHandler');
$dpaCustomHandler = $form->addCheckbox('customDpaHandler', 'customDpaHandler');
if ($this->cache->load('dpaHandlerDetected' . $this->address) !== true) {
$dpaCustomHandler->setDisabled();
}
$form->addCheckbox('ioSetup', 'ioSetup');
$form->addCheckbox('dpaAutoexec', 'dpaAutoexec');
$form->addCheckbox('routingOff', 'routingOff');
......@@ -241,13 +258,13 @@ class TrConfigFormFactory {
* @param Form $form Set TR configuration form
*/
public function save(Form $form): void {
$address = (int) $this->presenter->getParameter('address', 0);
$config = $form->getValues('array');
if (array_key_exists('stdAndLpNetwork', $config)) {
$config['stdAndLpNetwork'] = (bool) $config['stdAndLpNetwork'];
}
try {
$this->manager->write($address, $config);
$this->manager->write($this->address, $config);
$this->cache->remove('trConfiguration' . $this->address);
$this->presenter->flashSuccess('iqrfnet.trConfiguration.write.success');
} catch (DpaErrorException | EmptyResponseException | JsonException | UserErrorException $e) {
$this->presenter->flashError('iqrfnet.trConfiguration.write.failure');
......
......@@ -30,6 +30,8 @@ use App\IqrfNetModule\Forms\TrConfigFormFactory;
use App\IqrfNetModule\Models\EnumerationManager;
use App\IqrfNetModule\Models\TrConfigManager;
use Nette\Application\UI\Form;
use Nette\Caching\Cache;
use Nette\Caching\IStorage;
use Nette\Utils\JsonException;
/**
......@@ -37,6 +39,11 @@ use Nette\Utils\JsonException;
*/
class TrConfigPresenter extends ProtectedPresenter {
/**
* @var Cache Cache
*/
private $cache;
/**
* @var ChangeAddressFormFactory Change device address form
* @inject
......@@ -68,11 +75,11 @@ class TrConfigPresenter extends ProtectedPresenter {
/**
* Constructor
* @param EnumerationManager $enumerationManager IQRF TR enumeration manager
* @param TrConfigManager $manager IQRF TR configuration manager
* @param IStorage $storage Cache storage
*/
public function __construct(EnumerationManager $enumerationManager, TrConfigManager $manager) {
public function __construct(EnumerationManager $enumerationManager, IStorage $storage) {
$this->cache = new Cache($storage, 'trConfiguration');
$this->enumerationManager = $enumerationManager;
$this->manager = $manager;
parent::__construct();
}
......@@ -102,32 +109,44 @@ class TrConfigPresenter extends ProtectedPresenter {
/**
* Loads TR configuration
* @param int $address TR address
*/
public function loadConfiguration(): void {
public function loadConfiguration(int $address): void {
$this->template->configuration = false;
if ($this->cache->load('trConfiguration' . $address) !== null &&
$this->cache->load('dpaVersion' . $address) !== null &&
$this->cache->load('dpaHandlerDetected' . $address) !== null) {
if (!$this->getUser()->isInRole('power')) {
$this->flashSuccess('iqrfnet.trConfiguration.read.success');
}
$this->template->configuration = true;
return;
}
try {
$address = (int) $this->getParameter('address');
$enumeration = $this->enumerationManager->device($address);
$dpaVersion = $enumeration['response']->data->rsp->peripheralEnumeration->dpaVer ?? '99.99';
$this->template->dpaVersion = ltrim($dpaVersion, '0');
$dpa = $this->manager->read($address);
if (!array_key_exists('response', $dpa)) {
$this->template->configuration = null;
if (!array_key_exists('response', $enumeration)) {
$this->cache->clean([Cache::ALL]);
$this->flashError('iqrfnet.trConfiguration.read.failure');
$this->redrawControl('forms');
return;
}
$configuration = $dpa['response']->data->rsp;
$response = $enumeration['response']->data->rsp;
$dpaVersion = $response->peripheralEnumeration->dpaVer ?? '99.99';
$this->cache->save('dpaVersion' . $address, ltrim($dpaVersion, '0'));
$this->cache->save('dpaHandlerDetected' . $address, $response->osRead->flags->dpaHandlerDetected);
$configuration = $response->trConfiguration;
if (property_exists($configuration, 'stdAndLpNetwork')) {
$configuration->stdAndLpNetwork = (int) $configuration->stdAndLpNetwork;
}
if (!$this->getUser()->isInRole('power')) {
$this->flashSuccess('iqrfnet.trConfiguration.read.success');
}
$this->template->configuration = $configuration;
$this->template->configuration = true;
$this->cache->save('trConfiguration' . $address, $configuration, [Cache::EXPIRE => '5 minutes']);
} catch (DpaErrorException | EmptyResponseException | JsonException | UserErrorException $e) {
$this->template->configuration = null;
$this->cache->clean([Cache::ALL]);
$this->flashError('iqrfnet.trConfiguration.read.failure');
}
$this->redrawControl('forms');
}
/**
......@@ -135,8 +154,9 @@ class TrConfigPresenter extends ProtectedPresenter {
* @param int $address TR address
*/
public function renderDefault(int $address = 0): void {
if (!$this->isAjax()) {
$this->loadConfiguration();
$this->loadConfiguration($address);
if ($this->isAjax()) {
$this->redrawControl('forms');
}
}
......
......@@ -27,7 +27,7 @@
</div>
{snippet forms}
{if $configuration !== null}
{if $configuration}
<div class='box box-primary'>
<header class='box-header with-border'>
<h2 class='box-title'>{_iqrfnet.trConfig.title}</h2>
......
......@@ -34,9 +34,6 @@ parameters:
-
message: '#Foreach overwrites \$unit with its value variable.#'
path: %currentWorkingDirectory%/app/GatewayModule/Models/InfoManager.php
-
message: '#Parameter \#1 \$input of method App\\IqrfNetModule\\Forms\\TrConfigFormFactory::setRfChannelRule\(\) expects Nette\\Forms\\Controls\\TextInput, Nette\\ComponentModel\\IComponent given.#'
path: %currentWorkingDirectory%/app/IqrfNetModule/Forms/TrConfigFormFactory.php
-
message: '#Variable property access on mixed.#'
path: %currentWorkingDirectory%/app/IqrfNetModule/Forms/TrConfigFormFactory.php
......
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