Commit 39a86a2d authored by Roman Ondráček's avatar Roman Ondráček

IQRF Net: remove JSON API messages conversion to array (fix #194)

Signed-off-by: Roman Ondráček's avatarRoman Ondráček <ondracek.roman@centrum.cz>
parent 83282366
Pipeline #2111 passed with stages
in 14 minutes and 25 seconds
......@@ -119,24 +119,21 @@ class InfoManager {
* @param mixed[] $dpa Information about the Coordinator
*/
private function parseRfMode(array &$dpa): void {
if (!isset($dpa['response']['data']['rsp']['peripheralEnumeration'])) {
if (!isset($dpa['response']->data->rsp->peripheralEnumeration)) {
return;
}
$enumeration = &$dpa['response']['data']['rsp']['peripheralEnumeration'];
$flags = &$enumeration['flags'];
if (!is_array($flags) || array_key_exists('rfMode', $flags)) {
return;
}
if (version_compare($enumeration['dpaVer'], '4.00', '<')) {
$array = &$flags['rfMode'];
$enumeration = &$dpa['response']->data->rsp->peripheralEnumeration;
$flags = &$enumeration->flags;
if (version_compare($enumeration->dpaVer, '4.00', '<')) {
$array = &$flags->rfMode;
} else {
$array = &$flags['networkType'];
$array = &$flags->networkType;
}
if (array_key_exists('stdAndLpNetwork', $flags) && $flags['stdAndLpNetwork']) {
if (isset($flags->stdAndLpNetwork) && $flags->stdAndLpNetwork) {
$array = 'STD+LP';
} elseif (array_key_exists('rfModeStd', $flags) && $flags['rfModeStd']) {
} elseif (isset($flags->rfModeStd) && $flags->rfModeStd) {
$array = 'STD';
} elseif (array_key_exists('rfModeLp', $flags) && $flags['rfModeLp']) {
} elseif (isset($flags->rfModeLp) && $flags->rfModeLp) {
$array = 'LP';
}
}
......
......@@ -129,9 +129,9 @@ class VersionManager {
'returnVerbose' => true,
],
];
$this->apiRequest->setRequest($request);
$this->apiRequest->set($request);
$api = $this->wsClient->sendSync($this->apiRequest);
return $api['response']['data']['rsp']['version'];
return $api['response']->data->rsp->version;
}
/**
......
......@@ -97,7 +97,7 @@ class InfoPresenter extends ProtectedPresenter {
$this->template->gwId = $this->infoManager->getId();
$this->template->gwmodId = $this->infoManager->getPixlaToken();
try {
$this->template->module = $this->infoManager->getCoordinatorInfo()['response']['data']['rsp'];
$this->template->module = $this->infoManager->getCoordinatorInfo()['response']->data->rsp;
} catch (DpaErrorException | EmptyResponseException $e) {
$this->flashError('gateway.info.tr.error');
}
......
......@@ -111,19 +111,19 @@
<th>{_gateway.info.tr.title}</th>
<td>
{ifset $module}
{var $osInfo = $module['osRead']}
{var $enumeration = $module['peripheralEnumeration']}
<strong>{_gateway.info.tr.moduleType}:</strong> {$osInfo['trMcuType']['trType']}<br>
<strong>{_gateway.info.tr.mcuType}:</strong> {$osInfo['trMcuType']['mcuType']}<br>
<strong>{_gateway.info.tr.moduleId}:</strong> {$osInfo['mid']}<br>
<strong>{_gateway.info.tr.os}:</strong> {$osInfo['osVersion']} ({$osInfo['osBuild']})<br>
<strong>{_gateway.info.tr.dpa}:</strong> {$enumeration['dpaVer']}<br>
{ifset $enumeration['flags']['rfMode']}
<strong>{_gateway.info.tr.rfMode}:</strong> {$enumeration['flags']['rfMode']}<br>
{elseifset $enumeration['flags']['networkType']}
<strong>{_gateway.info.tr.networkType}:</strong> {$enumeration['flags']['networkType']}<br>
{var $osInfo = $module->osRead}
{var $enumeration = $module->peripheralEnumeration}
<strong>{_gateway.info.tr.moduleType}:</strong> {$osInfo->trMcuType->trType}<br>
<strong>{_gateway.info.tr.mcuType}:</strong> {$osInfo->trMcuType->mcuType}<br>
<strong>{_gateway.info.tr.moduleId}:</strong> {$osInfo->mid}<br>
<strong>{_gateway.info.tr.os}:</strong> {$osInfo->osVersion} ({$osInfo->osBuild})<br>
<strong>{_gateway.info.tr.dpa}:</strong> {$enumeration->dpaVer}<br>
{ifset $enumeration->flags->rfMode}
<strong>{_gateway.info.tr.rfMode}:</strong> {$enumeration->flags->rfMode}<br>
{elseifset $enumeration->flags->networkType}
<strong>{_gateway.info.tr.networkType}:</strong> {$enumeration->flags->networkType}<br>
{/ifset}
<strong>{_gateway.info.tr.voltage}:</strong> {$osInfo['supplyVoltage']}
<strong>{_gateway.info.tr.voltage}:</strong> {$osInfo->supplyVoltage}
{else}
{_gateway.info.tr.error}
{/ifset}
......
......@@ -61,12 +61,12 @@ class IqrfOs {
* @return IqrfOs IQRF OS entity
*/
public static function fromOsRead(array $api): self {
$data = $api['response']['data']['rsp']['result'];
$build = Strings::upper(Strings::padLeft(dechex($data['osBuild']), 4, '0'));
$versionHi = dechex($data['osVersion'] >> 4);
$versionLo = Strings::padLeft(dechex($data['osVersion'] & 0xf), 2, '0');
$data = $api['response']->data->rsp->result;
$build = Strings::upper(Strings::padLeft(dechex($data->osBuild), 4, '0'));
$versionHi = dechex($data->osVersion >> 4);
$versionLo = Strings::padLeft(dechex($data->osVersion & 0xf), 2, '0');
$version = Strings::upper($versionHi . $versionLo);
$trSeries = TrSeries::fromTrMcuType($data['trMcuType']);
$trSeries = TrSeries::fromTrMcuType($data->trMcuType);
return new self($build, $version, $trSeries);
}
......
......@@ -116,11 +116,11 @@ class SendJsonFormFactory {
$this->presenter->flashError('iqrfnet.send-json.messages.missingSchema');
}
try {
$this->request->setRequest($json);
$this->request->set($json);
$response = $this->wsClient->sendSync($this->request, false);
$this->presenter->handleShowResponse($response);
$rsp = new ApiResponse();
$rsp->setResponse(Json::encode($response['response']));
$rsp->set(Json::encode($response['response']));
$rsp->checkStatus();
$this->presenter->flashSuccess('iqrfnet.send-json.messages.success');
} catch (EmptyResponseException $e) {
......
......@@ -30,6 +30,7 @@ use Nette\Application\UI\Form;
use Nette\Forms\Controls\TextInput;
use Nette\SmartObject;
use Nette\Utils\JsonException;
use stdClass;
/**
* IQRF TR configuration form factory
......@@ -39,9 +40,9 @@ class TrConfigFormFactory {
use SmartObject;
/**
* @var mixed[] TR configuration
* @var stdClass TR configuration
*/
protected $configuration = [];
protected $configuration;
/**
* @var TrConfigManager IQRF TR configuration manager
......@@ -66,6 +67,7 @@ class TrConfigFormFactory {
public function __construct(FormFactory $factory, TrConfigManager $manager) {
$this->factory = $factory;
$this->manager = $manager;
$this->configuration = new stdClass();
}
/**
......@@ -101,8 +103,8 @@ class TrConfigFormFactory {
unset($rfBands[$key]);
}
$form->addSelect('rfBand', 'rfBand', $rfBands)->setDisabled();
if (array_key_exists('rfBand', $this->configuration)) {
$form['rfBand']->setDefaultValue($this->configuration['rfBand']);
if (isset($this->configuration->rfBand)) {
$form['rfBand']->setDefaultValue($this->configuration->rfBand);
}
$rfChannels = ['rfChannelA', 'rfChannelB'];
foreach ($rfChannels as $rfChannel) {
......@@ -111,12 +113,12 @@ class TrConfigFormFactory {
}
$subChannels = ['rfSubChannelA', 'rfSubChannelB'];
foreach ($subChannels as $subChannel) {
if (array_key_exists($subChannel, $this->configuration)) {
if (isset($this->configuration->$subChannel)) {
$form->addInteger($subChannel, $subChannel);
$this->setRfChannelRule($form[$subChannel]);
}
}
if (array_key_exists('stdAndLpNetwork', $this->configuration)) {
if (isset($this->configuration->stdAndLpNetwork)) {
$warning = $form->getTranslator()->translate('messages.breakInteroperability');
$form->addCheckbox('stdAndLpNetwork', 'stdAndLpNetwork')
->setHtmlAttribute('data-warning', $warning);
......@@ -137,7 +139,7 @@ class TrConfigFormFactory {
* @param TextInput $input RF channel input
*/
private function setRfChannelRule(TextInput $input): void {
$rfBand = $this->configuration['rfBand'] ?? null;
$rfBand = $this->configuration->rfBand ?? null;
switch ($rfBand) {
case '443':
$input->addRule(Form::RANGE, 'messages.rfChannel443', [0, 16]);
......@@ -166,8 +168,8 @@ class TrConfigFormFactory {
$form->addCheckbox('rfPgmDualChannel', 'rfPgmDualChannel');
$form->addCheckbox('rfPgmLpMode', 'rfPgmLpMode');
$form->addCheckbox('rfPgmIncorrectUpload', 'rfPgmIncorrectUpload')->setDisabled();
if (array_key_exists('rfPgmIncorrectUpload', $this->configuration)) {
$form['rfPgmIncorrectUpload']->setDefaultValue($this->configuration['rfPgmIncorrectUpload']);
if (isset($this->configuration->rfPgmIncorrectUpload)) {
$form['rfPgmIncorrectUpload']->setDefaultValue($this->configuration->rfPgmIncorrectUpload);
}
}
......@@ -183,8 +185,8 @@ class TrConfigFormFactory {
foreach ($unchangeablePeripherals as $peripheral) {
$embeddedPeripherals->addCheckbox($peripheral, 'embPers.' . $peripheral)
->setDisabled();
if (array_key_exists('embPers', $this->configuration)) {
$embeddedPeripherals[$peripheral]->setValue($this->configuration['embPers'][$peripheral]);
if (isset($this->configuration->embPers)) {
$embeddedPeripherals[$peripheral]->setValue($this->configuration->embPers->$peripheral);
}
}
}
......@@ -205,10 +207,10 @@ class TrConfigFormFactory {
$form->addCheckbox('dpaAutoexec', 'dpaAutoexec');
$form->addCheckbox('routingOff', 'routingOff');
$form->addCheckbox('peerToPeer', 'peerToPeer');
if (array_key_exists('dpaPeerToPeer', $this->configuration)) {
if (isset($this->configuration->dpaPeerToPeer)) {
$form->addCheckbox('dpaPeerToPeer', 'dpaPeerToPeer');
}
if (array_key_exists('neverSleep', $this->configuration)) {
if (isset($this->configuration->neverSleep)) {
$form->addCheckbox('neverSleep', 'neverSleep');
}
$uartBaudrates = [1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400];
......@@ -217,7 +219,7 @@ class TrConfigFormFactory {
unset($uartBaudrates[$key]);
}
$form->addSelect('uartBaudrate', 'uartBaudrate', $uartBaudrates);
if (array_key_exists('nodeDpaInterface', $this->configuration)) {
if (isset($this->configuration->nodeDpaInterface)) {
$form->addCheckbox('nodeDpaInterface', 'nodeDpaInterface');
}
}
......
......@@ -74,7 +74,7 @@ class BondingManager {
'returnVerbose' => true,
],
];
$this->request->setRequest($array);
$this->request->set($array);
return $this->wsClient->sendSync($this->request);
}
......@@ -102,7 +102,7 @@ class BondingManager {
'returnVerbose' => true,
],
];
$this->request->setRequest($array);
$this->request->set($array);
return $this->wsClient->sendSync($this->request);
}
......@@ -127,7 +127,7 @@ class BondingManager {
'returnVerbose' => true,
],
];
$this->request->setRequest($array);
$this->request->set($array);
return $this->wsClient->sendSync($this->request);
}
return $this->remove(255, false);
......@@ -169,7 +169,7 @@ class BondingManager {
],
];
}
$this->request->setRequest($array);
$this->request->set($array);
return $this->wsClient->sendSync($this->request);
}
......
......@@ -144,9 +144,9 @@ class DevicesManager {
'returnVerbose' => true,
],
];
$this->request->setRequest($request);
$this->request->set($request);
$apiData = $this->wsClient->sendSync($this->request);
return $apiData['response']['data']['rsp']['result']['bondedDevices'] ?? [];
return $apiData['response']->data->rsp->result->bondedDevices ?? [];
}
/**
......@@ -168,9 +168,9 @@ class DevicesManager {
'returnVerbose' => true,
],
];
$this->request->setRequest($request);
$this->request->set($request);
$apiData = $this->wsClient->sendSync($this->request);
return $apiData['response']['data']['rsp']['result']['discoveredDevices'] ?? [];
return $apiData['response']->data->rsp->result->discoveredDevices ?? [];
}
/**
......@@ -195,9 +195,9 @@ class DevicesManager {
],
],
];
$this->request->setRequest($request);
$this->request->set($request);
$apiData = $this->wsClient->sendSync($this->request);
$frcData = $apiData['response']['data']['rsp']['result']['frcData'] ?? [];
$frcData = $apiData['response']->data->rsp->result->frcData ?? [];
$data = [];
foreach ($frcData as $i => $byte) {
for ($j = 0; $j < 8; ++$j) {
......
......@@ -78,7 +78,7 @@ class DiscoveryManager {
],
],
];
$this->request->setRequest($array);
$this->request->set($array);
return $this->wsClient->sendSync($this->request);
}
......
......@@ -92,7 +92,7 @@ class DpaRawManager {
if (!isset($timeout)) {
unset($array['data']['timeout']);
}
$this->request->setRequest($array);
$this->request->set($array);
$data = $this->wsClient->sendSync($this->request);
foreach ($data as &$json) {
$json = Json::encode($json, Json::PRETTY);
......
......@@ -75,7 +75,7 @@ class EnumerationManager {
'returnVerbose' => true,
],
];
$this->request->setRequest($array);
$this->request->set($array);
return $this->wsClient->sendSync($this->request);
}
......
......@@ -78,7 +78,7 @@ class GwModeManager {
'returnVerbose' => true,
],
];
$this->request->setRequest($request);
$this->request->set($request);
$this->wsClient->sendSync($this->request);
}
......@@ -100,9 +100,9 @@ class GwModeManager {
'returnVerbose' => true,
],
];
$this->request->setRequest($request);
$this->request->set($request);
$api = $this->wsClient->sendSync($this->request);
return $api['response']['data']['rsp']['operMode'] ?? 'unknown';
return $api['response']->data->rsp->operMode ?? 'unknown';
}
}
......@@ -74,7 +74,7 @@ class NativeUploadManager {
if ($format !== null) {
$request['data']['req']['target'] = $format->toScalar();
}
$this->apiRequest->setRequest($request);
$this->apiRequest->set($request);
return $this->wsClient->sendSync($this->apiRequest);
}
......
......@@ -76,7 +76,7 @@ class OsManager {
'returnVerbose' => true,
],
];
$this->apiRequest->setRequest($request);
$this->apiRequest->set($request);
return $this->wsClient->sendSync($this->apiRequest);
}
......
......@@ -100,7 +100,7 @@ class SecurityManager {
'returnVerbose' => true,
],
];
$this->request->setRequest($array);
$this->request->set($array);
return $this->wsClient->sendSync($this->request);
}
......
......@@ -75,7 +75,7 @@ class StandardBinaryOutputManager {
'returnVerbose' => true,
],
];
$this->request->setRequest($array);
$this->request->set($array);
return $this->wsClient->sendSync($this->request);
}
......@@ -121,7 +121,7 @@ class StandardBinaryOutputManager {
foreach ($outputs as $output) {
$array['data']['req']['param']['binOuts'][] = $output->toArray();
}
$this->request->setRequest($array);
$this->request->set($array);
return $this->wsClient->sendSync($this->request);
}
......
......@@ -77,7 +77,7 @@ class StandardDaliManager {
'returnVerbose' => true,
],
];
$this->request->setRequest($array);
$this->request->set($array);
return $this->wsClient->sendSync($this->request);
}
......
......@@ -75,7 +75,7 @@ class StandardLightManager {
'returnVerbose' => true,
],
];
$this->request->setRequest($array);
$this->request->set($array);
return $this->wsClient->sendSync($this->request);
}
......@@ -123,7 +123,7 @@ class StandardLightManager {
],
];
$this->convertEntityToArray($array, $lights);
$this->request->setRequest($array);
$this->request->set($array);
return $this->wsClient->sendSync($this->request);
}
......@@ -165,7 +165,7 @@ class StandardLightManager {
],
];
$this->convertEntityToArray($array, $lights);
$this->request->setRequest($array);
$this->request->set($array);
return $this->wsClient->sendSync($this->request);
}
......@@ -193,7 +193,7 @@ class StandardLightManager {
],
];
$this->convertEntityToArray($array, $lights);
$this->request->setRequest($array);
$this->request->set($array);
return $this->wsClient->sendSync($this->request);
}
......
......@@ -74,7 +74,7 @@ class StandardSensorManager {
'returnVerbose' => true,
],
];
$this->request->setRequest($array);
$this->request->set($array);
return $this->wsClient->sendSync($this->request);
}
......@@ -98,7 +98,7 @@ class StandardSensorManager {
'returnVerbose' => true,
],
];
$this->request->setRequest($array);
$this->request->set($array);
return $this->wsClient->sendSync($this->request);
}
......
......@@ -75,7 +75,7 @@ class TrConfigManager {
'returnVerbose' => true,
],
];
$this->request->setRequest($array);
$this->request->set($array);
return $this->wsClient->sendSync($this->request);
}
......@@ -101,7 +101,7 @@ class TrConfigManager {
],
];
$array['data']['req'] = Arrays::mergeTree($array['data']['req'], $config);
$this->request->setRequest($array);
$this->request->set($array);
return $this->wsClient->sendSync($this->request);
}
......
......@@ -149,10 +149,10 @@ class WebSocketClient {
* @throws JsonException
*/
private function checkMessage(ApiRequest $request, MessageInterface $response): bool {
$requestJson = Json::decode($request->toJson(), Json::FORCE_ARRAY);
$responseJson = Json::decode($response->getPayload(), Json::FORCE_ARRAY);
$correctMsqId = $requestJson['data']['msgId'] === $responseJson['data']['msgId'];
$correctMType = $requestJson['mType'] === $responseJson['mType'];
$requestJson = Json::decode($request->toJson());
$responseJson = Json::decode($response->getPayload());
$correctMsqId = $requestJson->data->msgId === $responseJson->data->msgId;
$correctMType = $requestJson->mType === $responseJson->mType;
return $correctMsqId && $correctMType;
}
......@@ -163,23 +163,22 @@ class WebSocketClient {
* @param bool $checkStatus Check response status
* @return mixed[] IQRF JSON API response in an array
* @throws EmptyResponseException
* @throws JsonException
* @throws DpaErrorException
* @throws UserErrorException
*/
private function parseResponse(ApiRequest $request, ?MessageInterface $response, bool $checkStatus): array {
$data = ['request' => $request->toArray()];
$data = ['request' => $request->get()];
if ($response === null) {
$data['status'] = 'Empty response.';
Debugger::barDump($data, 'WebSocket client');
throw new EmptyResponseException();
}
$apiResponse = new ApiResponse();
$apiResponse->setResponse($response->getPayload());
$apiResponse->set($response->getPayload());
if ($checkStatus) {
$apiResponse->checkStatus();
}
$data['response'] = $apiResponse->toArray();
$data['response'] = $apiResponse->get();
Debugger::barDump($data, 'WebSocket client');
return $data;
}
......
......@@ -64,13 +64,13 @@ class EnumerationPresenter extends ProtectedPresenter {
*/
public function renderDefault(int $address = 0): void {
try {
$data = $this->manager->device($address)['response']['data']['rsp'];
$data = $this->manager->device($address)['response']->data->rsp;
if (isset($data)) {
$this->template->data = $data;
$this->template->osData = $data['osRead'];
$this->template->peripheralData = $data['peripheralEnumeration'];
$this->template->osData = $data->osRead;
$this->template->peripheralData = $data->peripheralEnumeration;
try {
$hwpId = $data['peripheralEnumeration']['hwpId'];
$hwpId = $data->peripheralEnumeration->hwpId;
$this->template->product = $this->productManager->get($hwpId);
} catch (ProductNotFound $e) {
// Do nothing
......
......@@ -103,9 +103,9 @@ class TrConfigPresenter extends ProtectedPresenter {
$this->flashError('iqrfnet.trConfiguration.read.failure');
return;
}
$configuration = $dpa['response']['data']['rsp'];
if (array_key_exists('stdAndLpNetwork', $configuration)) {
$configuration['stdAndLpNetwork'] = (int) $configuration['stdAndLpNetwork'];
$configuration = $dpa['response']->data->rsp;
if (property_exists($configuration, 'stdAndLpNetwork')) {
$configuration->stdAndLpNetwork = (int) $configuration->stdAndLpNetwork;
}
$this->template->configuration = $configuration;
} catch (DpaErrorException | EmptyResponseException | JsonException | UserErrorException $e) {
......
......@@ -49,11 +49,19 @@ class ApiRequest {
$this->msgIdManager = $msgIdManager;
}
/**
* Returns the IQRF JSON API request
* @return mixed[]|stdClass IQRF JSON API request
*/
public function get() {
return $this->request;
}
/**
* Sets the IQRF JSON API request
* @param mixed $request IQRF JSON API request
*/
public function setRequest($request): void {
public function set($request): void {
if (!is_array($request) && !($request instanceof stdClass)) {
throw new InvalidJsonException();
}
......@@ -72,15 +80,6 @@ class ApiRequest {
}
}
/**
* Converts the IQRF JSON API request to a array
* @return mixed[] IQRF JSON API request
* @throws JsonException
*/
public function toArray(): array {
return Json::decode($this->toJson(), Json::FORCE_ARRAY);
}
/**
* Converts the IQRF JSON DPA request to JSON string
* @param bool $pretty Pretty formatted JSON
......
......@@ -31,8 +31,8 @@ class DpaRequest extends ApiRequest {
* Sets the JSON DPA request
* @param mixed $request JSON DPA request
*/
public function setRequest($request): void {
parent::setRequest($request);
public function set($request): void {
parent::set($request);
$this->fixRequest();
}
......
......@@ -42,6 +42,7 @@ use App\IqrfNetModule\Exceptions\TimeoutException;
use App\IqrfNetModule\Exceptions\UserErrorException;
use Nette\Utils\Json;
use Nette\Utils\JsonException;
use stdClass;
/**
* IQRF JSON API request
......@@ -49,7 +50,7 @@ use Nette\Utils\JsonException;
class ApiResponse {
/**
* @var mixed[] JSON API response
* @var stdClass JSON API response
*/
protected $response;
......@@ -83,7 +84,7 @@ class ApiResponse {
* @throws UserErrorException
*/
public function checkStatus(): void {
$status = $this->response['data']['status'];
$status = $this->response->data->status;
if ($status === 0) {
return;
}
......@@ -93,26 +94,26 @@ class ApiResponse {
throw new UserErrorException();
}
/**
* Returns the IQRF JSON API response
* @return stdClass IQRF JSON API response
*/
public function get(): stdClass {
return $this->response;
}
/**
* Sets the IQRF JSON API response
* @param string $response IQRF JSON API response
*/
public function setResponse(string $response): void {
public function set(string $response): void {
try {
$this->response = Json::decode($response, Json::FORCE_ARRAY);
$this->response = Json::decode($response);
} catch (JsonException $e) {
$this->response = [];
$this->response = (object) [];
}
}
/**
* Converts IQRF JSON API request to a array
* @return mixed[] JSON API request
*/
public function toArray(): array {
return $this->response;
}
/**
* Converts IQRF JSON API request to a JSON string
* @param bool $pretty Pretty formatted JSON
......
......@@ -23,33 +23,33 @@
<table class='table table-striped'>
<tr>
<th>{_iqrfnet.enumeration.deviceAddr}</th>
<td>{$data['deviceAddr']}</td>
<td>{$data->deviceAddr}</td>
</tr>
<tr>
<th>{_iqrfnet.enumeration.hwpid}</th>
<td>{$peripheralData['hwpId']}</td>
<td>{$peripheralData->hwpId}</td>
</tr>