utils.php 2.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php
/**
 * @copyright Copyright (C) 2020 Elphel, Inc.
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 * @author Oleg Dzhimiev <oleg@elphel.com>
 * @brief Access info or perform actions using GET requests
 */

include "include/elphel_functions_include.php";

$cmd = "donothing";

if (isset($_GET['cmd']))
15
    $cmd = $_GET['cmd'];
16
else if (isset($argv[1]))
17
    $cmd = $argv[1];
18 19 20 21 22

// allow CORS
header('Access-Control-Allow-Origin: *');

switch($cmd){
23 24 25 26 27 28 29 30
    case "sensors":
        print(cmd_sensors());
        break;
    case "time":
        cmd_time();
        break;
    default:
        print("OK");
31 32 33 34 35 36 37 38 39 40 41 42
}

function cmd_sensors(){
    $p0 = 2323;
    $sensors = get_sensors();

    $res = "\t<camera ip='".$_SERVER['SERVER_ADDR']."'>\n";
    foreach($sensors as $i => $sensor){
        $p = $p0+$i;
        $res .= "\t\t<port index='$i' port='$p'>$sensor</port>\n";
    }
    $res .= "\t</camera>\n";
43 44 45
    return wrap_into_xml($res);
}

46 47 48 49 50 51 52 53 54
function get_formatted_ts($ts){

    $ts_s  = substr($ts,0,10);
    $ts_ms  = substr($ts,11);

    $ts_formatted = date("Y-m-d H:i:s.$ts_ms",$ts_s);
    return $ts_formatted;
}

55 56 57
function cmd_time(){

    date_default_timezone_set('UTC');
58
    exec("date +%s.%3N",$ots);
59 60

    $t = elphel_get_fpga_time();
61 62 63 64 65 66
    $tsys = $ots[0];

    print("Camera time (fpga):  $t (".get_formatted_ts($t).")\n");
    print("Camera time (sys):   $tsys (".get_formatted_ts($tsys).")\n");

    if (isset($_GET['ts'])){
67 68
        // ts is in ms
        $ts_s  = substr($_GET['ts'],0,10);
69 70 71 72
        $ts_ms = substr($_GET['ts'],11);

        $ts_formatted = get_formatted_ts($_GET['ts']);
        print("Your (browser) time: $ts_s.$ts_ms ($ts_formatted)\n");
73

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
74
        if (isset($_GET['apply'])||(abs($ts_s-$t)>24*3600)){
75

76
            elphel_set_fpga_time($_GET['ts']/1000);
77
            exec("date -s '$ts_formatted'");
78 79 80 81 82 83 84
            exec("hwclock --systohc");
            print("Timestamps differ by more than 24h. Camera and fpga time updated.\n");
        }
    }
}

function wrap_into_xml($s){
85 86
    $xml  = "<?xml version='1.0' standalone='yes'?>\n";
    $xml .= "<Document>\n";
87
    $xml .= $s;
88 89 90 91 92
    $xml .= "</Document>\n";
    return $xml;
}

?>