diagnostics.php 6.63 KB
Newer Older
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php

// GLOBALS
// path to sysfs for port scanning
$portspath = "/sys/devices/soc0/elphel393-detect_sensors@0";
// total number of ports in 10393
$nports = 4;
$muxports = 3;
$port0 = 2323;

$ports = getports();
$sample_port = get_sample_port($ports);
$master_port = elphel_get_P_value($sample_port,ELPHEL_TRIG_MASTER);

//print("<pre>");
//print_r($ports);
//print("sample port = ".$sample_port);

19 20 21 22 23
if (isset($_GET['pointers'])){
  $POINTERS_ONLY = true;
}else{
  $POINTERS_ONLY = false;
}
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
24 25 26

$res = "";
$res .= "<camera ip='".$_SERVER['SERVER_ADDR']."'>\n";
27 28 29 30 31 32 33 34 35 36 37

if (!$POINTERS_ONLY){
  $res .= "\t<master_port>".$master_port."</master_port>\n";
  $res .= "\t<systime>".get_system_time()."</systime>\n";
  $res .= "\t<systimestamp>".time()."</systimestamp>\n";
  $res .= "\t<uptime>".get_uptime()."</uptime>\n";
  $res .= "\t<temperature>".get_temperature()."\t</temperature>\n";
  $res .= "\t<storage>".check_storage()."\t</storage>\n";
  $res .= "\t<recorder name='camogm'>".check_camogm_running()."</recorder>\n";
  $res .= "\t<gps>".check_gps($master_port)."\t</gps>\n";
}
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222

for($i=0;$i<count($ports);$i++){
  $s = implode(', ',$ports[$i]['sensors']);
  $m = $ports[$i]['mux'];
  $res .= "\t<port index='$i' sensor='$s' mux='$m'>".get_port_info($i)."\t</port>\n";
}

$res .= "</camera>\n";

// allow CORS
header('Access-Control-Allow-Origin: *');
$xml  = "<?xml version='1.0' standalone='yes'?>\n";
$xml .= "<Document>\n";
$xml .= $res;
$xml .= "</Document>\n";
print($xml);

//functions

function check_camogm_running(){

  $camogm_running = false;
  exec('ps | grep "camogm"', $arr);
  $check = implode("<br/>",$arr);

  foreach($arr as $line){
    $result = preg_match('/grep/',$line);
    if (!$result) {
      $camogm_running = true;
    }
  }

  if ($camogm_running) $res = "on";
  else                 $res = "off";

  return $res;
}

function getports(){

  global $nports, $muxports, $portspath, $port0;

  $res = array();

  for($i=0;$i<$nports;$i++){
    $subres = array();

    $subres['mux'] = read_port_file($portspath."/port_mux{$i}");
    $subres['sensors'] = array();

    for($j=0;$j<$muxports+1;$j++){
      array_push($subres['sensors'],read_port_file($portspath."/sensor{$i}{$j}"));
    }

    array_push($res,$subres);
  }

  return $res;

}

function read_port_file($file){
  $v = "";
  if(is_file($file)){
    $v = trim(file_get_contents($file));
  }
  return $v;
}

function get_sample_port($a){
  $sample_found = false;
  // errors are not expected here
  //$res = -1;
  $res = 0;
  foreach($a as $k0=>$port){
    $b = $port['sensors'];
    foreach($b as $k1=>$sensor){
      if ($sensor!='none'){
        $sample_found = true;
        break;
      }
    }
    if ($sample_found){
      $res = $k0;
      break;
    }
  }
  return $res;
}

function check_storage(){

  $names = array();
  $regexp = '/([0-9]+) +(sd[a-z0-9]+$)/';
  exec("cat /proc/partitions", $partitions);

  // the first two elements of an array are table header and empty line delimiter, skip them
  for ($i = 2; $i < count($partitions); $i++) {
    // select SATA devices only
    if (preg_match($regexp, $partitions[$i], $name) == 1) {
      $names[$name[2]] = $name[1];
      $j++;
    }
  }

  //print_r($names);
  $res = "";
  foreach($names as $name=>$size){
    if (preg_match('/^sd[a-z]$/',$name,$matches)) {
      $dev = $matches[0];
      $res .= "\n\t\t<device name='$dev' size='$size'>\n";
      foreach($names as $partition=>$psize){
        if (preg_match('/^'.$dev.'[0-9]+$/',$partition)){
          $res .= "\t\t\t<partition name='$partition' size='$psize'></partition>\n";
        }
      }
      $res .= "\t\t</device>\n";

    }
  }

  return $res;
}

function check_gps($port){
  $circbuf_pointers = elphel_get_circbuf_pointers($port,1);
  $pointer = $circbuf_pointers[count($circbuf_pointers)-1];
  $exif = elphel_get_exif_elphel($port,$pointer['exif_pointer']);
  if ((isset($exif['GPSLongitude']))&&(isset($exif['GPSLatitude']))){
    $res = "\n";
    $res .= "\t\t<lat>".$exif['GPSLatitude']."</lat>\n";
    $res .= "\t\t<lon>".$exif['GPSLongitude']."</lon>\n";
  }else{
    $res = "N/A";
  }
  return $res;
}

function get_system_time(){
  $res = exec('date');
  return $res;
}

function get_uptime(){
  $res = exec('uptime');
  $res = trim($res);
  $res = preg_replace('/\s+/',' ',$res);
  $res = explode(' ',$res);
  $out = trim($res[2],',');
  return $out;
}

function get_temperature(){

  $t_cpu = round(floatval(trim(file_get_contents("/tmp/core_temp"))),1);
  $t_10389 = "";
  $t_sda = "";
  $t_sdb = "";

  $temp1_input = "/sys/devices/soc0/amba@0/e0004000.ps7-i2c/i2c-0/0-001a/hwmon/hwmon0/temp1_input";

  if (is_file($temp1_input)){
    $t_10389 = trim(file_get_contents($temp1_input));
    $t_10389 = intval($t_10389)/1000;
  }

  $t_sda = exec("smartctl -A /dev/sda | egrep ^194 | awk '{print $10}'");
  if ($t_sda=="") $t_sda = "-";
  else            $t_sda = intval($t_sda);

  $t_sdb = exec("smartctl -A /dev/sdb | egrep ^194 | awk '{print $10}'");
  $t_sdb = "";
  if ($t_sdb=="") $t_sdb = "-";
  else            $t_sdb = intval($t_sdb);

  $res = "\n\t\t<cpu>$t_cpu</cpu>\n";
  $res .= "\t\t<b10389>$t_10389</b10389>\n";
  $res .= "\t\t<sda>$t_sda</sda>\n";
  $res .= "\t\t<sdb>$t_sdb</sdb>\n";

  return $res;
}

function get_port_info($port){

223 224
  global $POINTERS_ONLY;

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
225 226 227 228 229
  $pars_res = "";
  $ts_res = "";

  $pars = array(
    'WB_EN' => 0,
230
    'AUTOEXP_ON' => 0,
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
    'COMPRESSOR_RUN'=> 0,
    'SENSOR_RUN'=> 0,
    'COLOR' => 0,
    'QUALITY' => 0,
    'EXPOS' => 0,
    'WOI_WIDTH' => 0,
    'WOI_HEIGHT' => 0,
    'TRIG' => 0,
    'TRIG_MASTER' => 0,
    'TRIG_PERIOD' => 0,
    'TRIG_CONDITION' => 0,
    'TRIG_OUT' => 0,
    'GAINR' => 0,
    'GAING' => 0,
    'GAINB' => 0,
    'GAINGB' => 0,
  );

249
  $pars_res .= "\n";
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
250

251 252
  if (!$POINTERS_ONLY){
    $ps = elphel_get_P_arr($port,$pars);
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
253

254 255 256 257 258 259
    $pars_res .= "\t\t<parameters>\n";
    foreach($ps as $k=>$v){
      $pars_res .= "\t\t\t<".strtolower($k).">$v</".strtolower($k).">\n";
    }
    $pars_res .= "\t\t</parameters>\n";
  }
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276

  // get recent timestamps
  $circbuf_pointers = elphel_get_circbuf_pointers($port,1);
  $meta = array();
  foreach($circbuf_pointers as $k=>$v){
    $meta[$k] = array (
      'circbuf_pointer' => $v['circbuf_pointer'],
      'meta' => elphel_get_interframe_meta($port,$v['circbuf_pointer']),
      'Exif' => elphel_get_exif_elphel($port, $v['exif_pointer'])
    );
  }

  $ts_res .= "\t\t<timestamps>\n";

  foreach($meta as $m){
    $sec  = $m['meta']['timestamp_sec'];
    $usec = sprintf("%06d", $m['meta']['timestamp_usec']);
277 278
    $ptr = $m['circbuf_pointer'];
    $ts_res .= "\t\t\t<ts frame='{$m['Exif']['FrameNumber']}' ts='$sec.$usec' ptr='$ptr'>$sec.$usec</ts>\n";
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
279 280 281 282 283 284 285 286 287 288
  }

  $ts_res .= "\t\t</timestamps>\n";

  $res = $pars_res.$ts_res;

  return $res;
}

?>