110 lines
3.3 KiB
PHP
110 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace classes;
|
|
|
|
require_once WD . '/interfaces/shelly_transport_i.php';
|
|
require_once WD . '/classes/edge_gateway_manager.php';
|
|
|
|
use Exception;
|
|
use interfaces\shelly_transport_i;
|
|
|
|
class gateway_shelly_transport implements shelly_transport_i
|
|
{
|
|
public function __construct(private readonly ?edge_gateway_manager $manager = null)
|
|
{
|
|
}
|
|
|
|
public function requireModuleEnabled(): void
|
|
{
|
|
// Gateway mode is department-scoped and does not depend on the Shelly cloud module flag.
|
|
}
|
|
|
|
public function requireValidSecretKey(): void
|
|
{
|
|
// Gateway mode does not use the Shelly cloud auth key.
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public function sendPostRequest(string $endpoint, array $data, ?int $department_id = null): array|object|null
|
|
{
|
|
if ($department_id === null || $department_id <= 0) {
|
|
throw new Exception('A department_id is required for gateway Shelly transport');
|
|
}
|
|
|
|
return match ($endpoint) {
|
|
'/v2/devices/api/get' => $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<int,array<string,mixed>>
|
|
* @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<string,mixed> $status
|
|
* @return array<string,mixed>
|
|
*/
|
|
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();
|
|
}
|
|
}
|