apps/ccamftp/ccamftp.php

Go to the documentation of this file.
00001 #!/usr/local/sbin/php -q
00002 <?php
00036 
00037 
00038 
00039 set_time_limit(0);
00040 $ftpConfigFile='/etc/conf.d/ccamftp33.conf';
00041 $tmpDir=       '/tmp';
00042 $daemonParamsTemplate=array('FTP_PERIOD'=>null, 'FTP_TIMEOUT'=>null, 'FTP_UPDATE'=>null, 'FTP_NEXT_TIME'=>null);
00043 //echo "<pre>";
00044 $ftpConfig=parse_ini_file  ($ftpConfigFile );
00045 //$ftpConfig=file  ($ftpConfigFile );
00046 //print_r($ftpConfig);
00047   $lastCycleTime=0; 
00048 
00049   $framepars_file=fopen("/dev/frameparsall","r");
00051   if ((elphel_get_P_value(ELPHEL_DAEMON_EN) & (1 << ELPHEL_CONST_DAEMON_BIT_CCAMFTP))==0) {
00052      echo "ccamftp daemon is not enabled at startup. To activate it - please enable the control bit (DAEMON_EN_CCAMFTP) and restart the camera\n";
00053      echo "Exiting ...\n";
00054      exit (0);
00055   }
00056   $tmpJPEG=$tmpDir."/".$ftpConfig['INAME'].".jpeg";
00057   elphel_set_P_value(ELPHEL_FTP_NEXT_TIME, time()); 
00058   while (true) {
00060     fseek($framepars_file, ELPHEL_LSEEK_DAEMON_FRAME + ELPHEL_CONST_DAEMON_BIT_CCAMFTP, SEEK_END);
00061     $daemonParams=elphel_get_P_arr($daemonParamsTemplate);
00062 //   print_r($daemonParams);
00064     $remoteConfig=    getFTPFileWithTimeoutTo($ftpConfig['SERVER'],
00065                                                 $ftpConfig['USER'],
00066                                             $ftpConfig['PASSWORD'],
00067                                      $ftpConfig['REMOTE_CONF_DIR'],
00068                                                $ftpConfig['INAME'],
00069                                                            $tmpDir,
00070                                       $daemonParams['FTP_TIMEOUT']);
00071 
00073     if ($remoteConfig) {
00074       foreach ($remoteConfig as $key=>$value) $remoteConfig[$key]=myval($value);
00075        $frameWritten=elphel_set_P_arr($remoteConfig); 
00076        $daemonParams=elphel_get_P_arr($daemonParamsTemplate,$frameWritten);
00077        elphel_wait_frame_abs($frameWritten+2); 
00078     }
00079 //    print_r($daemonParams);
00080 //    print_r($remoteConfig);
00083     if ((($dt=($daemonParams['FTP_NEXT_TIME']-time())))>0) {
00084        if ($dt>$daemonParams['FTP_UPDATE']) $dt=$daemonParams['FTP_UPDATE'];
00085        sleep ($dt);
00086     } else { 
00087 
00088       if (file_exists($tmpName)) unlink ($tmpName);
00089       copy ('http://127.0.0.1:8081/img',$tmpJPEG);
00090       $cmd=sprintf("timeout %d sftpclient -s -p %s  -k %s -l %s -c %s -d %s.jpeg -t %s.tmp -u %s -w %s",
00091                     $daemonParams['FTP_TIMEOUT'],
00092                     $ftpConfig['SERVER'],
00093                     $tmpDir,
00094                     $tmpJPEG,
00095                     $ftpConfig['REMOTE_DIR'],
00096                     $ftpConfig['INAME'],
00097                     $ftpConfig['INAME'],
00098                     $ftpConfig['USER'],
00099                     $ftpConfig['PASSWORD']);
00100 //      echo ($cmd);
00101       exec ($cmd);
00103       getFileWithTimeoutTo($ftpConfig['REMOTE_SCRIPT_PATH'],
00104                            '/dev/null',
00105                            $daemonParams['FTP_TIMEOUT']);
00107       $daemonParams['FTP_NEXT_TIME']+=$daemonParams['FTP_PERIOD'];
00108       if ($daemonParams['FTP_NEXT_TIME'] < time()) { 
00109          $daemonParams['FTP_NEXT_TIME']=time()+$daemonParams['FTP_PERIOD'];
00110       }
00111       elphel_set_P_value(ELPHEL_FTP_NEXT_TIME, $daemonParams['FTP_NEXT_TIME']); 
00112     }
00113   }
00114 
00115 
00116   function getFTPFileWithTimeoutTo($server,$user,$password,$remoteConfDir,$iname,$tmpDir,$timeout) {
00117     $tmpName="${tmpDir}/${iname}.conf";
00118     getFileWithTimeoutTo("ftp://${user}:${password}@${server}${remoteConfDir}/${iname}.conf",$tmpName, $timeout);
00120     if (!file_exists($tmpName)) return null; 
00121     $remoteConfig=parse_ini_file  ($tmpName);
00122 //print_r ($remoteConfig);
00123     if ($remoteConfig['FINISH']==1) {
00124       unset ($remoteConfig['FINISH']);
00125       return $remoteConfig;
00126     }
00127     return null;
00128 
00129   }
00130   function getFileWithTimeoutTo($url,$tmpName,$timeout) {
00132     if ((file_exists($tmpName)) && (strpos($tmpName,'/dev/'!==0))) unlink ($tmpName);
00133     $cmd="timeout ${timeout} wget \"${url}\"  -q -O ${tmpName}";
00134 //echo ($cmd);
00135     exec ($cmd);
00136   }
00137 function myval ($s) {
00138   $s=trim($s,"\" ");
00139   if (strtoupper(substr($s,0,2))=="0X")   return intval(hexdec($s));
00140   else return intval($s);
00141 }
00142 ?>

Generated on Fri Nov 28 00:06:21 2008 for elphel by  doxygen 1.5.1