"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 = [ 'input' => null, 'output' => null, 'temperature' => null ]; // GEN3 / GEN4 Geräte → switch:0, switch:1, ... foreach ($params as $key => $value) { if (str_starts_with($key, 'switch:') && is_array($value)) { // Output if (isset($value['output'])) { $mapped['output'] = (bool)$value['output']; } // Input (für Pro / Gen4 Geräte) if (isset($value['input'])) { $mapped['input'] = (bool)$value['input']; } } } // Temperatur suchen (beliebig tief) self::ExtractRecursive($params, $mapped); return array_filter($mapped, fn($v) => $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; } } } } ?>