diff --git a/server/SHServ/Controllers/DevicesRESTAPIController.php b/server/SHServ/Controllers/DevicesRESTAPIController.php index 175b929..6f942f5 100644 --- a/server/SHServ/Controllers/DevicesRESTAPIController.php +++ b/server/SHServ/Controllers/DevicesRESTAPIController.php @@ -194,7 +194,6 @@ if($device -> connection_status != "active") { $device -> connection_status = "active"; - $device -> last_contact = date("Y-m-d H:i:s"); $device -> update(); } diff --git a/server/SHServ/Controllers/EventsController.php b/server/SHServ/Controllers/EventsController.php index a7247d0..bc22ce7 100644 --- a/server/SHServ/Controllers/EventsController.php +++ b/server/SHServ/Controllers/EventsController.php @@ -26,6 +26,8 @@ return $this -> utils() -> response_error("error_of_device_auth"); } + $device -> touch_last_contact(); + ignore_user_abort(true); set_time_limit(10); diff --git a/server/SHServ/Entities/Device.php b/server/SHServ/Entities/Device.php index e34ee6e..e2849d4 100644 --- a/server/SHServ/Entities/Device.php +++ b/server/SHServ/Entities/Device.php @@ -92,6 +92,10 @@ $this -> device_api_instance -> set_local_token($this -> auth() -> device_token); } + $this -> device_api_instance -> set_on_success_callback(function() { + $this -> touch_last_contact(); + }); + return $this -> device_api_instance ?? null; } @@ -113,4 +117,9 @@ public function ping() { return app() -> utils -> fast_ping_tcp($this -> device_ip); } + + public function touch_last_contact(): void { + $this -> last_contact = date("Y-m-d H:i:s"); + $this -> update(); + } } diff --git a/server/SHServ/Tools/DeviceAPI/Base.php b/server/SHServ/Tools/DeviceAPI/Base.php index e3bee9a..04e7e19 100644 --- a/server/SHServ/Tools/DeviceAPI/Base.php +++ b/server/SHServ/Tools/DeviceAPI/Base.php @@ -8,6 +8,7 @@ private ?string $token; private float $connect_timeout; private float $timeout; + private $on_success_callback; private const MAX_RETRIES = 3; private const RETRY_BACKOFF_MS = 100; @@ -38,6 +39,13 @@ } /** + * Установить callback, который вызывается после успешного HTTP-ответа от устройства. + */ + public function set_on_success_callback(callable $callback): void { + $this->on_success_callback = $callback; + } + + /** * GET /about (доступен всегда, без токена). */ public function get_about(): array { @@ -243,6 +251,10 @@ curl_close($ch); + if ($http_code > 0 && $this->on_success_callback !== null) { + ($this->on_success_callback)(); + } + $raw_headers = substr($raw_response, 0, $header_size); $raw_body = substr($raw_response, $header_size);