diff --git a/server/SHServ/Helpers/DeviceScriptsHelper.php b/server/SHServ/Helpers/DeviceScriptsHelper.php index 57d56aa..8730ed1 100644 --- a/server/SHServ/Helpers/DeviceScriptsHelper.php +++ b/server/SHServ/Helpers/DeviceScriptsHelper.php @@ -301,6 +301,15 @@ * @return string|null — "open", "closed" или null */ public function get_hatch_state(string $hatch_alias): ?string { + $info = $this -> get_hatch_info($hatch_alias); + return $info ? $info['state'] : null; + } + + /** + * Получить полную информацию о люке. + * @return array|null — ['state', 'position_sec', 'position_pct'] или null + */ + public function get_hatch_info(string $hatch_alias): ?array { try { $hatch = $this -> devices() -> by_alias($hatch_alias); if(!$hatch) { @@ -311,10 +320,18 @@ return null; } $status = $hatch_api -> get_status(); - if(empty($status)) { + if(empty($status) || ($status['status'] ?? null) != 'ok') { return null; } - return $status["state"] ?? null; + $hatch_data = $status['hatch'] ?? null; + if(!is_array($hatch_data) || !isset($hatch_data['state'])) { + return null; + } + return [ + 'state' => $hatch_data['state'], + 'position_sec' => $hatch_data['position_sec'] ?? null, + 'position_pct' => $hatch_data['position_pct'] ?? null, + ]; } catch(\Throwable $e) { return null; } @@ -357,16 +374,41 @@ /** * Сформировать индикатор для люка. - * @return array — [["label" => "Open|Closed|?", "variant" => "warning|secondary|secondary"]] + * @return array — [["label" => "Открыт — 50%", "variant" => "success|warning|secondary|primary"]] */ public function make_hatch_indicator(string $hatch_alias): array { - $state = $this -> get_hatch_state($hatch_alias); - if($state === null) { + $info = $this -> get_hatch_info($hatch_alias); + if($info === null) { return [["label" => "?", "variant" => "secondary"]]; } - $is_open = $state == "open"; + + $state = $info['state']; + $pct = $info['position_pct']; + + $labels = [ + 'closed' => 'Закрыт', + 'open' => 'Открыт', + 'opening' => 'Открывается', + 'closing' => 'Закрывается', + 'calibrating' => 'Калибровка', + ]; + $variants = [ + 'closed' => 'secondary', + 'open' => 'success', + 'opening' => 'warning', + 'closing' => 'warning', + 'calibrating' => 'primary', + ]; + + $label = $labels[$state] ?? $state; + + // Показываем позицию для open / opening / closing + if($pct !== null && in_array($state, ['open', 'opening', 'closing'], true)) { + $label .= ' — ' . $pct . '%'; + } + return [ - ["label" => $is_open ? "Open" : "Closed", "variant" => $is_open ? "warning" : "secondary"] + ["label" => $label, "variant" => $variants[$state] ?? 'secondary'] ]; } }