2013-09-26 10:28:55 +00:00
< ? php
2014-05-15 06:49:15 +00:00
set_time_limit ( 0 );
error_reporting ( - 1 );
ini_set ( " display_errors " , 'On' );
ini_set ( 'error_log' , '/tmp/wsn.log' );
2013-09-26 10:28:55 +00:00
//安全检查: 只接受来自westdc.westgis.ac.cn的访问
if ( @ $_SERVER [ 'HTTP_CLIENT_IP' ]){
$onlineip = $_SERVER [ 'HTTP_CLIENT_IP' ];
} elseif ( @ $_SERVER [ 'HTTP_X_FORWARDED_FOR' ]){
$onlineip = $_SERVER [ 'HTTP_X_FORWARDED_FOR' ];
} else {
$onlineip =@ $_SERVER [ 'REMOTE_ADDR' ];
}
2014-05-15 06:49:15 +00:00
if ( $argc != 2 && $onlineip != '210.77.68.241' && $onlineip != '210.77.68.215' && $onlineip != '2001:cc0:f006::9' && $onlineip != '2001:cc0:f006::405' )
2013-09-26 10:28:55 +00:00
die ( " error! You are not permitted to visit this page. " . $onlineip );
$db = new PDO ( 'pgsql:host=210.77.68.40;dbname=observation' , " gis " , " gispassword " );
2014-05-15 06:49:15 +00:00
$station = 0 ;
$site = 0 ;
$logger = 0 ;
$variable = 0 ;
2013-09-26 10:28:55 +00:00
$start ;
2014-05-15 06:49:15 +00:00
$end ;
$uid = 0 ;
$sitename = " " ;
$mailname = " " ;
$createtime = date ( " Ymd " , time ());
$endtime = " " ;
$html = " " ;
$time = " " ;
$value = " " ;
$var = " " ;
2013-09-26 10:28:55 +00:00
$file = array ();
2014-05-15 06:49:15 +00:00
$basepath = '/disk1/WestDC/wsndata/' ;
2013-09-26 10:28:55 +00:00
//接收用户uid, 只生成这个用户的数据
if ( $argc == 2 )
{
$uid = ( int ) $argv [ 1 ];
2014-05-15 06:49:15 +00:00
} else {
2013-09-26 10:28:55 +00:00
$uid = ( int ) @ $_GET [ 'uid' ];
2014-05-15 06:49:15 +00:00
}
2013-09-26 10:28:55 +00:00
if ( ! ( $uid > 0 ))
{
die ( " Error in parameter. " );
}
2014-05-15 06:49:15 +00:00
$action =@ $_GET [ 'action' ];
2013-09-26 10:28:55 +00:00
if ( ! empty ( $action ) && $action == 'prepare' )
{
$content = " php -f /var/www/ftp2.westgis.ac.cn/wsndata.php $uid " ;
2014-05-15 06:49:15 +00:00
$filename = 'wsndata.sh' ;
if ( ! file_put_contents ( $filename , $content , LOCK_EX ))
{
sleep ( 1 );
if ( ! file_put_contents ( $filename , $content , LOCK_EX ))
{
sleep ( 1 );
if ( ! file_put_contents ( $filename , $content , LOCK_EX ))
die ( " error in write wsndatar.sh " );
}
}
} else {
2014-06-06 01:02:29 +00:00
$sql_user = " SELECT distinct u.uid,u.site,u.starttime,u.endtime ,s.fld_name,case when (u.variable=-1) then '-1' else '1' end as variable
from users_pm_down u left join tbl_site_info s on u . site = s . fld_site_id
where starttime is not null and result =- 1 and type = 'down' and uid = $uid " ;
2014-05-15 06:49:15 +00:00
$sth_user = $db -> query ( $sql_user );
$rows_user = $sth_user -> fetchAll ();
//有需要生成的数据
if ( $rows_user )
{
foreach ( $rows_user as $k => $v )
{
2013-09-26 10:28:55 +00:00
//逐个生成
$site = $v [ 'site' ];
2014-05-15 06:49:15 +00:00
$variable = $v [ 'variable' ];
$start = date ( 'Y-m-d' , strtotime ( $v [ 'starttime' ]));
$end = date ( 'Y-m-d' , strtotime ( $v [ 'endtime' ]));
$uid = $v [ 'uid' ];
//计算有多少天
$days = round (( strtotime ( $end ) - strtotime ( $start )) / 3600 / 24 ) + 1 ;
//获取观测点的名称
2014-06-06 01:02:29 +00:00
$sitename = $v [ 'fld_name' ];
2014-05-15 06:49:15 +00:00
//先为该用户创建一个文件夹
$aimDir_user = $basepath . 'wsn_' . $uid ;
2013-09-26 10:28:55 +00:00
if ( ! file_exists ( $aimDir_user ))
{
$result = mkdir ( $aimDir_user );
2014-05-15 06:49:15 +00:00
}
//在该用户文件夹下面再建立一个观测点文件夹
$aimDir_site = $aimDir_user . DIRECTORY_SEPARATOR . $sitename ;
2013-09-26 10:28:55 +00:00
if ( ! file_exists ( $aimDir_site ))
{
$result = mkdir ( $aimDir_site );
2014-05-15 06:49:15 +00:00
}
2014-06-06 01:02:29 +00:00
//获取变量名称
$sql = " select distinct va.fld_name from tbl_value v left join tbl_variable_info va on v.fld_variable_id=va.fld_variable_id
WHERE v . fld_site_id = $site AND v . fld_time >= '$start' AND v . fld_time < '$end' " ;
if ( $variable == 1 )
2014-05-15 06:49:15 +00:00
{
2014-06-06 01:02:29 +00:00
$sql .= " and va.fld_variable_id in (select distinct variable from users_pm_down where starttime is not null and result=-1 and type='down' and uid= $uid ) " ;
}
$sql .= " order by va.fld_name " ;
$sth = $db -> query ( $sql );
$rs = $sth -> fetchAll ();
$head = " Time " ;
$sql2 = " select * from crosstab('SELECT v.fld_time as time,va.fld_name as variablename,v.fld_value as value
FROM tbl_value v left join tbl_variable_info va on v . fld_variable_id = va . fld_variable_id
WHERE v . fld_site_id = $site AND v . fld_time >= '' $start '' AND v . fld_time < '' $end '' " ;
if ( $variable == 1 )
{
$sql2 .= " and va.fld_variable_id in (select distinct variable from users_pm_down where starttime is not null and result=-1 and type=''down'' and uid= $uid ) " ;
}
$sql2 .= " ORDER BY time',
' select distinct va . fld_name from tbl_variable_info va left join tbl_value v on v . fld_variable_id = va . fld_variable_id
WHERE v . fld_site_id = $site AND v . fld_time >= '' $start '' AND v . fld_time < '' $end '' " ;
if ( $variable == 1 )
{
$sql2 .= " and va.fld_variable_id in (select distinct variable from users_pm_down where starttime is not null and result=-1 and type=''down'' and uid= $uid ) " ;
}
$sql2 .= " order by va.fld_name')
as ct ( time timestamp without time zone " ;
foreach ( $rs as $k => $var )
{
$head .= " , " . $var [ 'fld_name' ];
$sql2 .= " ,v " . $k . " real " ;
}
$head .= " \n " ;
$sql2 .= " ) " ;
//获取数据
$sth2 = $db -> query ( $sql2 );
//$rows2 = $sth2->fetchAll();
$endtime = date ( " Y-m-d " , strtotime ( $start ) + 24 * 3600 );
$html = " " ;
while ( $v = $sth2 -> fetch ())
{
//组装数据
if ( $v [ 'time' ] >= $start && $v [ 'time' ] < $endtime )
2013-09-26 10:28:55 +00:00
{
2014-06-06 01:02:29 +00:00
$html .= $v [ 'time' ];
foreach ( $rs as $k => $var )
$html .= " , " . $v [ 'v' . $k ];
$html .= " \n " ;
} elseif ( $v [ 'time' ] >= $endtime ) {
//写入文件
$resulttime = date ( " Ymd " , strtotime ( $start ));
$fp = fopen ( $aimDir_site . DIRECTORY_SEPARATOR . $sitename . (( $variable ==- 1 ) ? " _all_ " : " _selected_ " ) . $resulttime . " .CSV " , " w+ " );
fwrite ( $fp , $head . $html ); //写入数据
fclose ( $fp ); //关闭文件句柄
$start = $endtime ;
$endtime = date ( " Y-m-d " , strtotime ( $start ) + 24 * 3600 );
$html = $v [ 'time' ];
foreach ( $rs as $k => $var )
$html .= " , " . $v [ 'v' . $k ];
$html .= " \n " ;
}
2014-05-15 06:49:15 +00:00
}
2014-06-06 01:02:29 +00:00
//写入文件
$resulttime = date ( " Ymd " , strtotime ( $start ));
$fp = fopen ( $aimDir_site . DIRECTORY_SEPARATOR . $sitename . (( $variable ==- 1 ) ? " _all_ " : " _selected_ " ) . $resulttime . " .CSV " , " w+ " );
fwrite ( $fp , $head . $html ); //写入数据
fclose ( $fp ); //关闭文件句柄
print $sitename . " \r \n " ;
$sql_up = " update users_pm_down set result=0 WHERE site= $site and uid= $uid and result=-1 and type='down' " ;
$db -> query ( $sql_up );
2014-05-15 06:49:15 +00:00
}
}
2014-06-06 01:02:29 +00:00
}