diff --git a/server/SHServ/Controllers/CronController.php b/server/SHServ/Controllers/CronController.php index bc075df..2211fbf 100644 --- a/server/SHServ/Controllers/CronController.php +++ b/server/SHServ/Controllers/CronController.php @@ -53,6 +53,15 @@ } if(!$is_active) { + for($i = 0; $i < 2; $i++) { + if($active_device -> ping()) { + $is_active = true; + break; + } + } + } + + if(!$is_active) { if($active_device -> connection_status == "active") { $active_device -> connection_status = "lost"; $is_changed = true; diff --git a/server/SHServ/Entities/Device.php b/server/SHServ/Entities/Device.php index de80a90..da68ead 100644 --- a/server/SHServ/Entities/Device.php +++ b/server/SHServ/Entities/Device.php @@ -96,4 +96,8 @@ public function resetup(String $token) { return $this -> set_device_token($token); } + + public function ping() { + return app() -> utils -> fast_ping_tcp("http://{$this -> device_ip}"); + } } \ No newline at end of file diff --git a/server/SHServ/Utils.php b/server/SHServ/Utils.php index e81b7dd..193af12 100644 --- a/server/SHServ/Utils.php +++ b/server/SHServ/Utils.php @@ -146,4 +146,18 @@ return substr($token, 0, $length); } + + public function fast_ping_tcp(string $host, int $port = 80, float $timeout = 0.2): bool { + $start = microtime(true); + + $fp = @fsockopen($host, $port, $errno, $errstr, $timeout); + + if (!$fp) { + return false; + } + + fclose($fp); + + return (microtime(true) - $start) < $timeout; + } } \ No newline at end of file