no message
This commit is contained in:
@@ -4,42 +4,80 @@ declare(strict_types=1);
|
||||
|
||||
class ShellyParser
|
||||
{
|
||||
/** Modell aus src extrahieren */
|
||||
/**
|
||||
* Extrahiert den Modell-Typ aus src, z.B.:
|
||||
* "shelly1g4-12345" => "1g4"
|
||||
* "shellyplusplugs-xyz" => "plusplugs"
|
||||
*/
|
||||
public static function ExtractType(string $src): string
|
||||
{
|
||||
// Beispiel: "shelly1g4-12345"
|
||||
if (!str_starts_with($src, "shelly")) {
|
||||
return "unknown";
|
||||
if (!str_starts_with($src, 'shelly')) {
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
$str = substr($src, 6); // "1g4-12345"
|
||||
$parts = explode("-", $str); // ["1g4", "12345"]
|
||||
return $parts[0] ?? "unknown";
|
||||
// alles nach "shelly"
|
||||
$rest = substr($src, 6); // z.B. "1g4-12345" oder "plusplugs-xyz"
|
||||
$parts = explode('-', $rest);
|
||||
return $parts[0] ?? 'unknown';
|
||||
}
|
||||
|
||||
|
||||
/** generisches Mapping für Shelly Daten */
|
||||
/**
|
||||
* Geht rekursiv durch params und sammelt bekannte Werte:
|
||||
* - input (bool)
|
||||
* - output (bool)
|
||||
* - temperature (float, inkl. tC)
|
||||
*/
|
||||
public static function MapParams(array $params): array
|
||||
{
|
||||
$mapped = [];
|
||||
$mapped = [
|
||||
'input' => null,
|
||||
'output' => null,
|
||||
'temperature' => null
|
||||
];
|
||||
|
||||
foreach ($params as $key => $val) {
|
||||
self::ExtractRecursive($params, $mapped);
|
||||
|
||||
if ($key === "input") {
|
||||
$mapped["input"] = (bool)$val;
|
||||
// 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;
|
||||
}
|
||||
|
||||
if ($key === "output") {
|
||||
$mapped["output"] = (bool)$val;
|
||||
}
|
||||
switch ($lowerKey) {
|
||||
case 'input':
|
||||
$mapped['input'] = (bool)$value;
|
||||
break;
|
||||
|
||||
if ($key === "temperature" || $key === "temp") {
|
||||
$mapped["temperature"] = (float)$val;
|
||||
}
|
||||
case 'output':
|
||||
$mapped['output'] = (bool)$value;
|
||||
break;
|
||||
|
||||
// weitere Shelly-Geräte können später hier ergänzt werden
|
||||
case 'temperature':
|
||||
case 'tc':
|
||||
case 't':
|
||||
if (is_numeric($value)) {
|
||||
$mapped['temperature'] = (float)$value;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $mapped;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user