117 lines
3.2 KiB
PHP
117 lines
3.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
class ShellyParser
|
|
{
|
|
/**
|
|
* Extrahiert den Modell-Typ aus src, z.B.:
|
|
* "shelly1g4-12345" => "1g4"
|
|
* "shellyplusplugs-xyz" => "plusplugs"
|
|
*/
|
|
public static function ExtractType(string $src): string
|
|
{
|
|
if (!str_starts_with($src, 'shelly')) {
|
|
return 'unknown';
|
|
}
|
|
|
|
// nach 'shelly' den Rest nehmen
|
|
$rest = substr($src, 6); // z.B. "1g4-12345" oder "mini3-abc"
|
|
|
|
// alles vor dem ersten "-" ist der Typ
|
|
$parts = explode('-', $rest);
|
|
|
|
return $parts[0] ?? 'unknown';
|
|
}
|
|
|
|
/**
|
|
* Geht rekursiv durch params und sammelt bekannte Werte:
|
|
* - input (bool)
|
|
* - output (bool)
|
|
* - temperature (float, inkl. tC)
|
|
*/
|
|
public static function MapParams(array $params): array
|
|
{
|
|
$mapped = [
|
|
'outputs' => [], // switch:x → output
|
|
'inputs' => [], // input:x → state oder switch:x.input
|
|
'temperature' => null
|
|
];
|
|
|
|
foreach ($params as $key => $value) {
|
|
|
|
// OUTPUTS (switch:0.output)
|
|
if (str_starts_with($key, 'switch:') && is_array($value)) {
|
|
|
|
$index = (int)substr($key, 7);
|
|
|
|
if (isset($value['output'])) {
|
|
$mapped['outputs'][$index] = (bool)$value['output'];
|
|
}
|
|
|
|
// Gen4 / Pro input in switch
|
|
if (isset($value['input'])) {
|
|
$mapped['inputs'][$index] = (bool)$value['input'];
|
|
}
|
|
}
|
|
|
|
// INPUTS (input:0.state)
|
|
if (str_starts_with($key, 'input:') && is_array($value)) {
|
|
|
|
$index = (int)substr($key, 6);
|
|
|
|
if (isset($value['state'])) {
|
|
$mapped['inputs'][$index] = (bool)$value['state'];
|
|
}
|
|
}
|
|
}
|
|
|
|
// Temperatur tief suchen
|
|
self::ExtractRecursive($params, $mapped);
|
|
|
|
return $mapped;
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function ExtractRecursive(array $data, array &$mapped): void
|
|
{
|
|
foreach ($data as $key => $value) {
|
|
$lowerKey = strtolower((string)$key);
|
|
|
|
if (is_array($value)) {
|
|
// Temperatur in verschachtelter Struktur, z.B. ["temperature" => ["tC" => 41.2]]
|
|
if ($lowerKey === 'temperature') {
|
|
if (isset($value['tC']) && is_numeric($value['tC'])) {
|
|
$mapped['temperature'] = (float)$value['tC'];
|
|
} elseif (isset($value['t']) && is_numeric($value['t'])) {
|
|
$mapped['temperature'] = (float)$value['t'];
|
|
}
|
|
}
|
|
self::ExtractRecursive($value, $mapped);
|
|
continue;
|
|
}
|
|
|
|
switch ($lowerKey) {
|
|
case 'input':
|
|
$mapped['input'] = (bool)$value;
|
|
break;
|
|
|
|
case 'output':
|
|
$mapped['output'] = (bool)$value;
|
|
break;
|
|
|
|
case 'temperature':
|
|
case 'tc':
|
|
case 't':
|
|
if (is_numeric($value)) {
|
|
$mapped['temperature'] = (float)$value;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
?>
|