ZSamuels28 / UnifiClientCheck-Docker

Monitor UniFi networks for new devices with Telegram or Ntfy alerts using Art-of-WiFi/UniFi-API-client.
Apache License 2.0
20 stars 3 forks source link

foreach() argument must be of type array|object #7

Closed pax0707 closed 7 months ago

pax0707 commented 7 months ago

After some time it starts spamming this in log (and not notifying ofc):

Warning: foreach() argument must be of type array|object, false given in /usr/src/myapp/src/Main.php on line 35
No devices currently connected to the network.
Checking again in 60 seconds...
Warning: foreach() argument must be of type array|object, false given in /usr/src/myapp/src/Main.php on line 35
No devices currently connected to the network.
Checking again in 60 seconds...
Warning: foreach() argument must be of type array|object, false given in /usr/src/myapp/src/Main.php on line 35
No devices currently connected to the network.
Checking again in 60 seconds...
Warning: foreach() argument must be of type array|object, false given in /usr/src/myapp/src/Main.php on line 35
No devices currently connected to the network.
Checking again in 60 seconds...
Warning: foreach() argument must be of type array|object, false given in /usr/src/myapp/src/Main.php on line 35
No devices currently connected to the network.
Checking again in 60 seconds...
Warning: foreach() argument must be of type array|object, false given in /usr/src/myapp/src/Main.php on line 35
No devices currently connected to the network.
Checking again in 60 seconds...
Warning: foreach() argument must be of type array|object, false given in /usr/src/myapp/src/Main.php on line 35
No devices currently connected to the network.
Checking again in 60 seconds...
ZSamuels28 commented 7 months ago

Strange, I've never had that issue. If it initially is working, but then after some time gives this error, I wonder if there is an issue with the client itself? Perhaps try adding this to see if this resolves the issue (to check if clients is an array):

        if ($clients === false) {
            echo "Error: Failed to retrieve clients from the UniFi Controller. Retrying in 60 seconds...\n";
            sleep(60);
            $unifiClient->logout();
            $unifiClient = createUnifiClient();
            continue;
        }

        // New section to add
        if (!is_array($clients)) {
            echo "Error in client data retrieval: Expected an array, received a different type. Attempting to reconnect to UniFi Controller...\n";
            sleep(60);
            $unifiClient->logout();
            $unifiClient = createUnifiClient();
            continue;
        }

Let me know if this works and I can add it to the current code.

pax0707 commented 7 months ago

Got a new notification after edit and restarting.

Monitoring.

ZSamuels28 commented 7 months ago

Thanks, keep me posted, it sounds to me like something weird happens with the controller and a reconnect would fix it

pax0707 commented 7 months ago

Looks to be working fine. No errors so far, tested moving my iPhone over WiFi networks, and notifications are triggered as expected.

New device found. Sending a notification.
Checking again in 60 seconds...
New device found. Sending a notification.
Checking again in 60 seconds...
No new devices found on the network.
Checking again in 60 seconds...
New device found. Sending a notification.
Checking again in 60 seconds...

image

ZSamuels28 commented 7 months ago

Just pushed a fix for this. Thanks for the update! Let me know if you see any further issues.