Files
Symcon_Belevo_Energiemanage…/Shelly_Parser_MQTT/libs/ShellyParser.php
2025-11-14 08:47:13 +01:00

84 lines
2.4 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';
}
// alles nach "shelly"
$rest = substr($src, 6); // z.B. "1g4-12345" oder "plusplugs-xyz"
$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 = [
'input' => null,
'output' => null,
'temperature' => null
];
self::ExtractRecursive($params, $mapped);
// null-Werte rauswerfen
return array_filter($mapped, static function ($v) {
return $v !== null;
});
}
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;
}
}
}
}