0

Background

I have a playlog.csv file sitting on various Raspberry PI, and has the following format:

2018-03-22 12:43:21,NM_Test.h264,-2 //PI 1
2018-03-22 12:43:21,NM_Test.h264,-2 //PI 2
2018-03-22 12:43:21,vid.h264,0 //PI 3

I can connect to each PI and tail the CSV file by:

<DOCTYPE html>
<html>

<style>
#circleRed {
    background: #ff0000;
    width: 15px;
    height: 15px;
    border-radius: 50%;
}

#circleGreen {
    background: #00ff00;
    width: 15px;
    height: 15px;
    border-radius: 50px;
}
</style>

<?php
    require_once 'Net/SSH2.php';
    require_once 'phpseclib1.0.10/Crypt/RSA.php';
    $config = require 'config.php';
    $log = 'logfile.txt';

    if(is_array($config)){
        foreach($config as $cred){
            $ssh = new Net_SSH2($cred['ip'], $cred['port']); //get the IP and port 
            $key = new Crypt_RSA();
            $key->loadKey($cred['key']);

            if (!$ssh->login('pi', $key)){
                //logging with file_put_contants, Append mode, exclusive lock is more race condition safe then an open file handle.
                file_put_contents($log, "[".date('Y-m-d H:i:s')."]Login Failed for {$cred['ip']}\n", FILE_APPEND|LOCK_EX);
                continue;
            }

            $output = $ssh->exec('tail -1 /var/log/playlog.csv');    
        }
    };

    $array = explode(',',$output);

    if(in_array('0', $array, true)){
        echo '<div id="circleGreen"></div>';
    }
    if (in_array('-2'||'-3'||'-4'||'-5', $array, true)){
        echo '<div id="circleRed"></div>';
    }
 ?>

 </body>
 </html>

The Problem

Looking at the right most value, if the value is '-2' or '-3' etc, I'd like a red circle to be displayed, but if the value is '0', I'd like to show a green circle on my webpage. I'm trying to do this for all the PIs that I have connected via SSH.

But currently when I run my code I get a blank webpage and I can't figure out what I am doing wrong?

5
  • 1
    have you checked that $array has content in it after explode ()? Commented Apr 19, 2018 at 9:42
  • in_array('-2'||'-3'||'-4'||'-5', $array, true) this makes no sense, the needle is set to "-2" because the value is truthy Commented Apr 19, 2018 at 9:44
  • Hi @wayneOS , no I hadn't. But I just did an echo on $array and I got Array to string conversion PHP notice. I will look into this as maybe this is why my code is failing? Commented Apr 19, 2018 at 9:48
  • use print_r() Commented Apr 19, 2018 at 9:49
  • I get a blank webpage, turn on error reporting Commented Apr 19, 2018 at 9:50

2 Answers 2

1

You need to be carefull with strict-mode of in_array() because it is type-sensitive. For your case you can just check if the last element is less then zero or not. Here is an example. Although do everything in your foreach-loop to check every pi's return value.

foreach (...) {

    ...
    $output = $ssh -> exec ('tail -1 /var/log/playlog.csv');
    $array = explode (',', $output);

    if (end ($array) >= 0) {
        echo '<div id="circleGreen"></div>';
    } else {
        echo '<div id="circleRed"></div>';
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

Your code is such a simpler way for what I am trying to achieve! But your code will only work for the first returned output. So currently my $output will return 2018-04-18 12:55:29,nmmoney_2.h264,-2 2018-04-19 10:26:29,eurochange.h264,0 but the if statement only looks at 2018-04-18 12:55:29,nmmoney_2.h264,-2 and prints the red circle. It should read the next output 2018-04-19 10:26:29,eurochange.h264,0 and print a green circle?
then you need to put your output into the foreach-loop. I will unpdate my answer for that.
1

The line

in_array('-2'||'-3'||'-4'||'-5', $array, true)

isn't doing what you think. in_array can only accept one value for the $needle parameter - this line will evaluate the initial expression to boolean true, and then check if $array contains that exact value.

If you want to check if there's any overlap between two array (i.e. if the values -2, -3, -4 or -5 exist anywhere within the exploded line), you can use array_intersect, e.g.

if (count(array_intersect(['-2', '-3', '-4', '-5'], $array))) {
  ...

1 Comment

or instead of using all the various functions that you can use, just use explode, and get the last element in the exploded string and just simply use a comparison if ( last exploded element == 0) or if ( el == < 0), and thats it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.