【发布时间】:2018-04-19 09:37:30
【问题描述】:
背景
我在各种树莓派上有一个 playlog.csv 文件,格式如下:
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
我可以通过以下方式连接到每个 PI 并跟踪 CSV 文件:
<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>
问题
查看最右边的值,如果值为'-2'或'-3'等,我想显示一个红色圆圈,但如果值为'0',我想在我的网页上显示一个绿色圆圈。我正在尝试对通过 SSH 连接的所有 PI 执行此操作。
但目前当我运行我的代码时,我得到一个空白网页,我无法弄清楚我做错了什么?
【问题讨论】:
-
您检查过
$array在explode ()之后的内容吗? -
in_array('-2'||'-3'||'-4'||'-5', $array, true)这没有意义,指针设置为“-2”,因为该值是真实的 -
嗨@wayneOS,不,我没有。但我刚刚在
$array上做了一个回声,我收到了Array to string conversionPHP 通知。我会调查一下,也许这就是我的代码失败的原因? -
使用
print_r() -
I get a blank webpage,开启报错
标签: javascript php html arrays explode