From d053f7e3ebdeaa442a90ac6b39738839e09b1c9b Mon Sep 17 00:00:00 2001 From: Ben Menking Date: Sat, 9 May 2026 14:34:47 -0400 Subject: [PATCH] changed pollForMessages() impl --- src/Meshcore.php | 17 ++++++++++++----- tests/getStatusRequest.php | 1 - tests/getTelemetry.php | 11 +++++++---- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/Meshcore.php b/src/Meshcore.php index 066c01a..45f6faa 100644 --- a/src/Meshcore.php +++ b/src/Meshcore.php @@ -301,26 +301,33 @@ class Meshcore { } /** + * Poll for messages, returning after timeout or if a message with is received. This function can + * return zero to many results in an array. Not all messages may match . * * @param string $tag - * @param int $timeout_ms - * @return mixed + * @param int $timeout_ms defaults to 5 seconds + * @return array */ - public function pollForMessage(string $tag, int $timeout_ms = 5000): mixed { + public function pollForMessage(string $tag, int $timeout_ms = 5000): array { $mark = time(); + $messages = []; do { $msg = self::getNextMessage(); + if( $msg instanceof BinaryResponse ) { + $messages[] = $msg; + } + if( $msg instanceof BinaryResponse && $msg->tag == $tag ) { - return $msg; + return $messages; } usleep(500000); } while( ((time() - $mark) * 1000) < $timeout_ms ); - return false; + return $messages; } } \ No newline at end of file diff --git a/tests/getStatusRequest.php b/tests/getStatusRequest.php index 247983d..7cf434f 100644 --- a/tests/getStatusRequest.php +++ b/tests/getStatusRequest.php @@ -28,7 +28,6 @@ print_r($mc->login($contact['pub_key'], readline('Please enter the password: ')) $result = $mc->statusRequest($contact['pub_key']); print_r($result); -exit; $msg = $mc->pollForMessage($result->tag); diff --git a/tests/getTelemetry.php b/tests/getTelemetry.php index 10ecd3c..edad8f3 100644 --- a/tests/getTelemetry.php +++ b/tests/getTelemetry.php @@ -19,6 +19,7 @@ $repeaters = ['Simpsonville 3810gvr1', 'Simpsonville im37r1']; $contacts = $mc->getContacts(); echo "Found " . count($contacts) . " contacts\n"; + /* $idx = 1; foreach($contacts as $contact) { @@ -46,11 +47,13 @@ foreach($contacts as $contact) { echo "Tag: {$result->tag}\n"; - $msg = $mc->pollForMessage($result->tag); + $msgs = $mc->pollForMessage($result->tag); - echo json_encode($msg->lpp) . "\n"; - - sleep(1); + foreach($msgs as $msg) { + if( $msg->tag == $result->tag) { + echo json_encode($msg->lpp) . "\n"; + } + } } echo "Complete\n";