更新三江源子平台可视化模块文件

This commit is contained in:
Li Jianxuan 2014-06-11 08:12:05 +00:00
parent a7b9e63e70
commit f0f0e639b5
7 changed files with 388 additions and 236 deletions

View File

@ -1,43 +1,64 @@
<?php <?php
namespace Westdc\Visual\Handle; namespace Westdc\Visual\Handle;
use \Helpers\View as view; use \Helpers\View as view;
use \Helpers\dbh;
use \Helpers\Table; use \Helpers\Table;
//事件中存在的操作 //事件中存在的操作
class VisualHandler class VisualHandle extends \Westdc\Visual\VariableEncoder
{ {
private $db; //传入PDO对象误 private $db; //传入PDO对象误
private $config; //全局配置 private $config; //全局配置
public $table; function __construct()
public $tbl_maillog = ""; //邮件日志表 {
$this->db = \Zend_Registry::get('db');
function __construct()
{
$this->db = \Zend_Registry::get('db'); $this->config = \Zend_Registry::get('config');
}
$this->config = \Zend_Registry::get('config'); public function checkParam(\Zend_EventManager_Event $e)
$this->table = new Table(); {
} $data = $e->getParam('data');
public function deleteAuthor(\Zend_EventManager_Event $e) if(empty($data['uuid']))
{ {
$id = $e->getParam('id'); return "请填写数据UUID";
}
$ref = new Reference();
if(!is_array($data['vars']) || count($data['vars']) < 1)
return $ref->deleteReferenceAuthor($id); {
} return "参数错误";
}
public function deleteTag(\Zend_EventManager_Event $e)
{ return true;
$id = $e->getParam('id'); }
$ref = new Reference(); public function processData(\Zend_EventManager_Event $e)
{
return $ref->deleteReferenceTag($id); $data = $e->getParam('data');
}
$data['vars'] = $this->encode($data['vars']);
if(!isset($data['status']))
{
$data['status'] = 1;
}
return $data;
}
public function recordPosted(\Zend_EventManager_Event $e)
{
$id = $e->getParam('id');
}
public function recordChanged(\Zend_EventManager_Event $e)
{
$id = $e->getParam('id');
}
} }

View File

@ -1,27 +1,29 @@
<?php <?php
namespace Westdc\Visual\Listener; namespace Westdc\Visual\Listener;
class VisualListener implements \Zend_EventManager_ListenerAggregate use Westdc\Visual\Handle\VisualHandle;
{
private $event; class VisualListener implements \Zend_EventManager_ListenerAggregate
{
function __construct() private $event;
{
$this->event = new \Zend_EventManager_EventManager(); function __construct()
} {
$this->event = new \Zend_EventManager_EventManager();
public function attach(\Zend_EventManager_EventCollection $events) }
{
$Handler = new ReferenceHandler(); public function attach(\Zend_EventManager_EventCollection $events)
$events->attach('submit.checkParam', array($Handler, 'checkReferenceParam'), 100); {
$events->attach('submit.processData', array($Handler, 'processReferenceData'), 100); $Handler = new VisualHandle();
$events->attach('submit.recordPosted', array($Handler, 'recordPosted'), 100); $events->attach('submit.checkParam', array($Handler, 'checkParam'), 100);
$events->attach('submit.recordChanged', array($Handler, 'recordChanged'), 100); $events->attach('submit.processData', array($Handler, 'processData'), 100);
} $events->attach('submit.recordPosted', array($Handler, 'recordPosted'), 100);
$events->attach('submit.recordChanged', array($Handler, 'recordChanged'), 100);
public function detach(\Zend_EventManager_EventCollection $events) }
{
public function detach(\Zend_EventManager_EventCollection $events)
} {
} }
}

View File

@ -1,71 +1,71 @@
<?php <?php
//含沙量 //含沙量
//Sediment Concentration //Sediment Concentration
namespace Westdc\Visual\Reader; namespace Westdc\Visual\Reader;
use Westdc\Visual\Record; use Westdc\Visual\Record;
use Westdc\Visual\RecordInterface; use Westdc\Visual\RecordInterface;
class Sc extends Record implements RecordInterface class Sc extends Record implements RecordInterface
{ {
function __construct() function __construct()
{ {
parent::__construct(__CLASS__); parent::__construct(__CLASS__);
} }
public $subset; public $subset;
public function switchDataset() public function switchDataset()
{ {
if(empty($this->subset)) if(empty($this->subset))
{ {
$this->subset = "avg_sand"; $this->subset = "avg_sand";
return; return;
} }
switch($this->subset){ switch($this->subset){
case "avg": case "avg":
$this->subset = "avg_sand"; $this->subset = "avg_sand";
break; break;
case "max" : case "max" :
$this->subset = "max_sand"; $this->subset = "max_sand";
break; break;
case "min" : case "min" :
$this->subset = "min_sand"; $this->subset = "min_sand";
break; break;
} }
return; return;
} }
public function getData() public function getData()
{ {
$sql = "SELECT * FROM data_sands $sql = "SELECT * FROM data_sands
ORDER BY ORDER BY
year ASC, year ASC,
month ASC month ASC
"; ";
$rs = $this->db->query($sql); $rs = $this->db->query($sql);
return $rs; return $rs;
} }
public function outPut() public function outPut()
{ {
$this->switchDataset(); $this->switchDataset();
$rs = $this->getData(); $rs = $this->getData();
$data = array(); $data = array();
while($row = $rs->fetch()) while($row = $rs->fetch())
{ {
$row['utctime'] = $this->utcMsTime(mktime(0,0,0,$row['month'],0,$row['year'])); $row['utctime'] = $this->utcMsTime(mktime(0,0,0,$row['month'],0,$row['year']));
$data[] = array($row['utctime'],$row[$this->subset]); $data[] = array($row['utctime'],$row[$this->subset]);
} }
return $data; return $data;
} }
} }

View File

@ -5,13 +5,15 @@ namespace Westdc\Visual\Reader;
use Westdc\Visual\Record; use Westdc\Visual\Record;
use Westdc\Visual\RecordInterface; use Westdc\Visual\RecordInterface;
class SoilMoisture extends Record implements RecordInterface abstract class SoilMoisture extends Record implements RecordInterface
{ {
function __construct() function __construct()
{ {
parent::__construct(__CLASS__); parent::__construct(__CLASS__);
} }
public $tablename;
public $subset; public $subset;
public function switchDataset() public function switchDataset()
@ -47,7 +49,7 @@ class SoilMoisture extends Record implements RecordInterface
{ {
$this->switchDataset(); $this->switchDataset();
$sql = "SELECT * FROM data_watercontent $sql = "SELECT * FROM {$this->tablename}
WHERE WHERE
$this->subset > 0 $this->subset > 0
ORDER BY ORDER BY

View File

@ -6,9 +6,30 @@ class Record
public $db; public $db;
function __construct($recordType) public $subdataset;
public $timeFiled = "utctime";
private $sql;
function __construct($uuid = NULL,$identifier = "")
{ {
$this->initDatabase(); $this->initDatabase();
if(!empty($identifier))
{
$this->subdataset = $identifier;
}
if(!empty($uuid) && !empty($identifier))
{
$this->initVisual($uuid);
}
}
public function __invoke()
{
return $this->getData();
} }
public function initDatabase() public function initDatabase()
@ -20,23 +41,60 @@ class Record
."dbname={$config->visual_db->database};" ."dbname={$config->visual_db->database};"
."user={$config->visual_db->username};" ."user={$config->visual_db->username};"
."password={$config->visual_db->password}"; ."password={$config->visual_db->password}";
$this->db = new \PDO($dsn); $this->db = new \PDO($dsn);
} }
public function makeSql($table,$index,$fieldValue,$fieldTime) public function initVisual($uuid)
{
$visual = new Visual;
$var_data = $visual->getVisualVars($uuid);
$this->sql = $this->makeSql($var_data['data'],$this->subdataset);
//echo $this->sql;exit();
}
public function getSql()
{
return $this->sql;
}
public function getData()
{
$rs = $this->db->query($this->sql);
$data = [];
while($row = $rs->fetch(\PDO::FETCH_ASSOC))
{
$row['utctime'] = $this->utcMsTime(strtotime($row['utctime']));
//var_dump($row);
$data[] = array(
$row['utctime'],
$row['value']
);
}
return $data;
}
public function makeSql($table,$fieldValue)
{ {
$sql = "SELECT $sql = "SELECT
\"$index\" as \"index\", \"$fieldValue\" as \"value\", \"$fieldTime\" as \"time\" {$this->timeFiled} as utctime , \"$fieldValue\" as value
FROM \"$table\" FROM $table
ORDER BY ORDER BY
extract(year from \"$fieldTime\") ASC, extract(YEAR from \"{$this->timeFiled}\") ASC,
extract(month from \"$fieldTime\") ASC, extract(MONTH from \"{$this->timeFiled}\") ASC,
extract(day from \"$fieldTime\") ASC, extract(DAY from \"{$this->timeFiled}\") ASC,
extract(HOUR from \"{$this->timeFiled}\") ASC,
extract(MINUTE from \"{$this->timeFiled}\") ASC,
extract(SECOND from \"{$this->timeFiled}\") ASC
"; ";
$rs = $this->db->query($sql); return $sql;
return $rs;
} }
public function utcMsTime($time=''){ public function utcMsTime($time=''){

View File

@ -0,0 +1,44 @@
<?php
namespace Westdc\Visual;
class VariableEncoder
{
function __construct()
{
}
public function __invoke($json)
{
$this->decode($json);
}
public function encode($vars)
{
$arr = [];
foreach($vars as $k=>$v)
{
$names = explode("|",$v);
$arr[] = [ 'dataset' => $names[0], 'subdataset' => $names[1],'seriename' => $names[2]];
}
return json_encode($arr);
}
public function decode($json)
{
$arr = json_decode($json,true);
//var_dump($arr);
$data = [];
foreach($arr as $k=>$v)
{
$data[$k] = join("|",$v);
}
return $data;
}
public function normaldecode($json)
{
return json_decode($json,true);
}
}

View File

@ -1,86 +1,111 @@
<?php <?php
namespace Westdc\Visual; namespace Westdc\Visual;
use Westdc\Visual\VisualListener; use Westdc\Visual\Listener\VisualListener as Listener;
use Helpers\dbh;
class Visual
{ class Visual
public $db; {
public $db;
function __construct($recordType) protected $events;
{
$this->db = \Zend_Registry::get('db'); function __construct()
$Listener = new Listener(); {
@$this->getEventManager()->attachAggregate($Listener); $this->db = \Zend_Registry::get('db');
} $this->mainTable = "datavisual";
public function getEventManager(\Zend_EventManager_EventCollection $events = NULL) $Listener = new Listener();
{ @$this->getEventManager()->attachAggregate($Listener);
if ($events !== NULL) { }
$this->events = $events;
} elseif ($this->events === NULL) { public function getEventManager(\Zend_EventManager_EventCollection $events = NULL)
$this->events = new \Zend_EventManager_EventManager(__CLASS__); {
} if ($events !== NULL) {
return $this->events; $this->events = $events;
} } elseif ($this->events === NULL) {
$this->events = new \Zend_EventManager_EventManager(__CLASS__);
//添加 }
public function add($data,$id = 0) return $this->events;
{ }
$params = compact('data');
$results = $this->getEventManager()->trigger('submit.checkParam', $this, $params); //添加
$cache_data = $results->last(); public function add($data)
{
if($cache_data !== true) $params = compact('data');
{ $results = $this->getEventManager()->trigger('submit.checkParam', $this, $params);
return $cache_data; $cache_data = $results->last();
}
if($cache_data !== true)
$results = $this->getEventManager()->trigger('submit.processData', $this, $params); {
$data = $results->last(); return $cache_data;
}
$dbh = new dbh();
$results = $this->getEventManager()->trigger('submit.processData', $this, $params);
if(empty($id)) $data = $results->last();
{
$id = $dbh->insert($this->mainTable,$data,true); $dbh = new dbh();
if(!empty($id) && is_numeric($id)) $record = $this->getVisualVars($data['uuid']);
{
$this->getEventManager()->trigger('submit.recordPosted', $this, $params); if(empty($record))
return true; {
}else{ $id = $dbh->insert($this->mainTable,$data);
if($id === false)
{ if($id)
return '服务器开小差了,请稍后再试'; {
}else{ $this->getEventManager()->trigger('submit.recordPosted', $this, $params);
return '服务器处理中遇到错误,请联系管理员'; return true;
} }else{
} if($id === false)
}//add {
return '服务器开小差了,请稍后再试';
else{ }else{
if(!$dbh->update($this->mainTable,$data," id=$id ",true)) return '服务器处理中遇到错误,请联系管理员';
{ }
$this->getEventManager()->trigger('submit.recordChanged', $this, $params); }
return "修改失败!请重试"; }//add
}else{
return true; else{
} if(!$dbh->update($this->mainTable,$data," uuid='{$data['uuid']}' "))
}//edit {
$this->getEventManager()->trigger('submit.recordChanged', $this, $params);
}// add() return "修改失败!请重试";
}else{
//删除 return true;
public function del($id) }
{ }//edit
if(!is_numeric($id) || empty($id) || $id< 0)
{ }// add()
return false;
} //删除
public function del($uuid)
$sql = "DELETE FROM {$this->mainTable} WHERE id=$id"; {
return $this->db->exec($sql); if(!is_numeric($id) || empty($id) || $id< 0)
} {
return false;
}
$sql = "DELETE FROM {$this->mainTable} WHERE uuid='$uuid'";
return $this->db->exec($sql);
}
//获得可视化变量
public function getVisualVars($uuid)
{
$sql = "SELECT * FROM {$this->mainTable} WHERE uuid='$uuid' LIMIT 1";
$rs = $this->db->query($sql);
return $rs->fetch(\PDO::FETCH_ASSOC);
}
//获得可以可视化的元数据
public function getVisualMetadata()
{
$sql = "SELECT * FROM metadata md
LEFT JOIN {$this->mainTable} v ON v.uuid = md.uuid
WHERE v.uuid IS NOT NULL";
$rs = $this->db->query($sql);
return $rs->fetchAll(\PDO::FETCH_ASSOC);
}
} }