$this->handleGetStates($department_id, $data), '/v2/devices/api/set/switch' => $this->handleSetSwitch($department_id, $data), default => throw new Exception('Unsupported gateway Shelly transport endpoint: ' . $endpoint), }; } /** * @return array> * @throws Exception */ private function handleGetStates(int $departmentId, array $data): array { $ids = array_values(array_filter( array_map(static fn(mixed $value): string => trim((string)$value), (array)($data['ids'] ?? [])), static fn(string $value): bool => $value !== '' )); $result = []; foreach ($ids as $logicalRelayId) { $status = $this->manager()->dispatchRelayStatus($departmentId, $logicalRelayId); $result[] = $this->normalizeRelayPayload($logicalRelayId, $status); } return $result; } /** * @throws Exception */ private function handleSetSwitch(int $departmentId, array $data): array { $logicalRelayId = trim((string)($data['id'] ?? '')); if ($logicalRelayId === '') { throw new Exception('Shelly gateway switch requests require an id'); } $status = $this->manager()->dispatchRelaySwitch( $departmentId, $logicalRelayId, (bool)($data['on'] ?? false) ); return [$this->normalizeRelayPayload($logicalRelayId, $status)]; } /** * @param array $status * @return array */ private function normalizeRelayPayload(string $logicalRelayId, array $status): array { $on = (bool)($status['on'] ?? $status['output'] ?? false); $online = (bool)($status['online'] ?? true); return [ 'id' => $logicalRelayId, 'online' => $online, 'on' => $on, 'status' => [ 'switch:0' => [ 'output' => $on, ], ], 'binding' => (array)($status['binding'] ?? []), 'raw' => (array)($status['raw'] ?? []), ]; } private function manager(): edge_gateway_manager { return $this->manager ?? new edge_gateway_manager(); } }