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?
$arrayhas content in it afterexplode ()?in_array('-2'||'-3'||'-4'||'-5', $array, true)this makes no sense, the needle is set to "-2" because the value is truthy$arrayand I gotArray to string conversionPHP notice. I will look into this as maybe this is why my code is failing?print_r()I get a blank webpage, turn on error reporting