1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101:
<?php
declare(strict_types=1);
namespace App\IqrfAppModule\Model;
use Nette;
class IqrfMacroManager {
use Nette\SmartObject;
private $path;
public function __construct(string $path) {
$this->path = $path;
}
public function read() {
$config = parse_ini_file($this->path, true)['Macro'];
$array = $this->parseMacros($config['Macros']);
return $array;
}
public function parseMacros(string $hex) {
$array = explode("\r\n", trim($this->hex2ascii($hex)));
for ($i = 0; $i < 3; $i++) {
array_shift($array);
}
$macros = [];
foreach ($array as $key => $value) {
$category = $key % 63;
$categoryId = floor($key / 63);
$macro = (($key - ($categoryId * 63)) - 2) % 5;
$macroId = floor(($key - 3 - ($categoryId * 63)) / 5);
if ($category === 0) {
$macros[$categoryId]['Name'] = $value;
} elseif ($category === 1) {
$macros[$categoryId]['Enabled'] = $value === 'True';
} elseif ($macro === 1) {
$macros[$categoryId]['Macros'][$macroId]['Name'] = $value;
} elseif ($macro === 2) {
$macros[$categoryId]['Macros'][$macroId]['Packet'] = trim($value, '.');
} elseif ($macro === 3) {
$macros[$categoryId]['Macros'][$macroId]['Enabled'] = $value === 'True';
}
}
return $macros;
}
public function hex2ascii(string $hex) {
$string = '';
for ($i = 0; $i < strlen($hex); $i += 2) {
$string .= chr(hexdec(substr($hex, $i, 2)));
}
return $string;
}
}