index.php 7.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
<?php
/**
 * @file snapshot.php
 * @brief snapshot
 * @copyright Copyright (C) 2017 Elphel Inc.
 * @author Oleg Dzhimiev <oleg@elphel.com>
 *
 * @par <b>License</b>:
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
?>
<?php

25 26
  include "../include/elphel_functions_include.php";

27 28
  $port0 = 2323;
  $path = "/sys/devices/soc0/elphel393-detect_sensors@0";
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
29
  $base_channel = 0;
30
  $available_ports = Array();
31

32 33
  $trig_master = -1;
  $trig_master_port = -1;
34 35 36

  $dl_exif_histories = 0;

37
  for($i=0;$i<4;$i++){
38

39 40 41 42 43 44 45 46 47 48
      $sensor = $path."/sensor{$i}0";
      if (is_file($sensor)){
          $c = trim(file_get_contents($sensor));
          if ($c!="none"){
              array_push($available_ports,$port0+$i);
          }
      }

  }

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
49 50
  $lowest_port = $available_ports[0]-$port0;

51 52
  // get TRIG_MASTER from lowest port
  if(!empty($available_ports)){
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
53
    $trig_master = intval(elphel_get_P_value($lowest_port,ELPHEL_TRIG_MASTER));
54
    $trig_master_port = $trig_master + $port0;
55
  }
56 57 58 59 60

  if (isset($_GET['debug'])){
    $dl_exif_histories = 1;
  }

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
61 62 63 64
  if (isset($_GET['bchn'])){
    $base_channel = $_GET['bchn'];
  }

65
  if ($trig_master>=0){
66

67
    if (isset($_GET['trig'])){
68

69 70 71 72
      // just in case one wants to override master
      if (isset($_GET['port'])){
        $trig_master_port = $_GET['port'];
      }
73

74 75
      $f = fopen("http://{$_SERVER['SERVER_ADDR']}:$trig_master_port/trig/pointers", 'r');
      fclose($f);
76
      die("trigger ok: http://{$_SERVER['SERVER_ADDR']}:$trig_master_port/trig/pointers");
77 78 79

    }

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
80 81
    if (isset($_GET['zip'])){

82 83 84 85
      $contents = Array();
      $filenames = Array();
      $rqs = Array();
      foreach($available_ports as $port){
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
86
        array_push($rqs,"http://{$_SERVER['SERVER_ADDR']}:$port/timestamp_name/bchn$base_channel/bimg");
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
87
      }
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
88
      // '1' in the end - get response with headers
89
      $cdata = curl_multi_start($rqs,1);
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
90
      // '1' in the end - parse response with headers
91
      $results = curl_multi_finish($cdata,false,0,false,1);
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
92

93 94 95 96
      $filenames = $results['names'];
      $contents = $results['contents'];

      /*
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
97
      foreach($available_ports as $port){
98 99 100 101 102 103 104 105 106
        //exec("wget --content-disposition -P $tmpdir http://{$_SERVER['SERVER_ADDR']}:$port/timestamp_name/bimg");
        //fopen("http://{$_SERVER['SERVER_ADDR']}:$port/timestamp_name/bimg",'r');
        $content = file_get_contents("http://{$_SERVER['SERVER_ADDR']}:$port/timestamp_name/bimg");
        $filename = get_filename_from_headers($http_response_header);
        if ($filename==""){
          $filename = "bimg.jp4";
        }
        array_push($filenames,$filename);
        array_push($contents,$content);
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
107
      }
108
      */
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
109

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
110
      $zipfilename = preg_replace("/_\d+\.jp(4|.*g)$/",".zip",$filenames[0]);
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
111

112 113 114
      //tmpfile
      $tmpfile = tmpfile();
      $tmpfilename = stream_get_meta_data($tmpfile)['uri'];
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
115

116
      $zip = new ZipArchive;
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
117

118 119
      if ($zip->open($tmpfilename, ZipArchive::OVERWRITE)!==TRUE) {
        die("cannot access temporary file <$tmpfilename>\n");
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
120 121
      }

122 123
      foreach($filenames as $k=>$v){
        $zip->addFromString($v,$contents[$k]);
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
124 125
      }

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
126 127 128 129 130 131 132 133 134 135
      if (isset($_GET['exifs'])){
        foreach($available_ports as $k=>$v){
          $i = $v-$port0;
          $fname = $filenames[$k];
          $v_new = preg_replace("/\.jp(4|.*g)$/","_exifs.txt",$fname);
          $exifs = get_all_exifs($i);
          $zip->addFromString($v_new,var_export($exifs,true));
        }
      }

136 137 138 139 140 141
      $zip->close();

      header('Content-type: application/zip');
      header('Content-Disposition: attachment; filename="'.$zipfilename.'"');
      echo file_get_contents($tmpfilename);

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
142 143 144 145
      die();

    }

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
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
    // get exif data from all buffers in a single text file
    if (isset($_GET['exifs'])){

      if (isset($_GET['sensor_port'])){
        $port = $_GET['sensor_port'];
      }else{
        $port = $lowest_port;
      }

      $meta = get_all_exifs(intval($port));

      print_r($meta);
      die();

    }

  }

  function get_all_exifs($port){

    $circbuf_pointers = elphel_get_circbuf_pointers($port,1);

    // get metas
    $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'])
      );
    }

    return $meta;

181 182 183 184 185 186 187 188
  }

?>
<!DOCTYPE HTML>
<html lang="en">
  <head>
    <meta charset="utf-8"/>
    <title>Snapshot</title>
189

190 191
    <script type='text/javascript' src='snapshot.js'></script>
    <script type='text/javascript' src='../js/jquery-3.1.1.js'></script>
192

193
    <style>
194

195 196 197
        body {
            font-family: "Helvetica Neue", Helvetica;
        }
198

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
199 200 201 202
        .button{
            font-weight: bold;
            border-radius:3px;
            outline:none;
203 204 205 206 207
            border: none;
            color: white;
            text-align: center;
            text-decoration: none;
            display: inline-block;
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
208
        }
209

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
210 211 212
        #snapshot{
            background-color: #CF4040; /* not Green */
            padding: 32px 32px;
213 214 215 216 217 218 219 220 221 222
            font-size: 20px;
        }

        #snapshot:hover{
            background-color: #BF4040; /* not Green */
        }

        #snapshot:active{
            background-color: #9F4040; /* not Green */
        }
223

224 225
        #snapshot:disabled{
            background-color: #A0A0A0; /* not Green */
226 227
        }

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
228
        #synced, #aszip{
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
229 230 231
            width:25px;
            height:25px;
        }
232

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
233 234 235 236 237
        #help_button{
            background-color: #404040; /* not Green */
            padding: 3px 7px;
            font-size: 15px;
        }
238

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
239 240 241
        #help_button:hover{
            background-color: #303030; /* not Green */
        }
242

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
243 244 245
        #help_button:active{
            background-color: #202020; /* not Green */
        }
246 247


248 249 250 251
    </style>

    <script>
        var ip = location.origin;
252
        //var href = location.href;
253 254 255
        var ports = [<?php echo implode(",",$available_ports);?>];
        var trig_master = <?php echo $trig_master;?>;
        var trig_master_port = <?php echo $trig_master_port;?>;
256 257 258

        var dl_exif_histories = <?php echo $dl_exif_histories;?>;

259
    </script>
260

261 262
  </head>
  <body>
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
263 264 265 266 267 268
    <div>
      <button title='Download images from all channels over network' id='snapshot' onclick='take_snapshot()' class='button'>Snapshot</button>
    </div>
    <br/>
    <div>
      <table>
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
269 270 271 272 273
      <tr>
        <td valign='middle'><span style='font-size:20px;line-height:25px;' title='checked = single zip
unchecked = multiple files'>zip</span></td>
        <td valign='middle'><input type='checkbox' id='aszip' checked/></td>
      </tr>
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
274 275 276 277 278 279 280 281 282
      <tr>
        <td valign='middle'><span style='font-size:20px;line-height:25px;'>sync</span></td>
        <td valign='middle'><input type='checkbox' id='synced' checked/></td>
        <td valign='middle'><button id='help_button' class='button' onclick='toggle_help()' >?</button></td>
      </tr>
      </table>
    </div>
    <br/>
    <div id='help' style='display:none;'>
283
      <b>if checked</b>:
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
284 285 286 287 288 289 290 291 292 293
      <ul>
        <li>all ports - same timestamp</li>
        <li>fps will be reprogrammed - set to single trigger mode then restored - careful if some other program is doing recording</li>
      </ul>
      <b>if unchecked</b>:
      <ul>
        <li>timestamps can be different</li>
        <li>fps will not be reprogrammed - no intereference with other recording programs, only network load.</li>
      </ul>
    </div>
294 295
  </body>
</html>