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

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
namespace Westdc\Visual\Handle;
use \Helpers\View as view;
use \Helpers\dbh;
use \Helpers\Table;
//事件中存在的操作
class VisualHandler
{
private $db; //传入PDO对象误
private $config; //全局配置
public $table;
public $tbl_maillog = ""; //邮件日志表
function __construct()
{
$this->db = \Zend_Registry::get('db');
$this->config = \Zend_Registry::get('config');
$this->table = new Table();
}
public function deleteAuthor(\Zend_EventManager_Event $e)
{
$id = $e->getParam('id');
$ref = new Reference();
return $ref->deleteReferenceAuthor($id);
}
public function deleteTag(\Zend_EventManager_Event $e)
{
$id = $e->getParam('id');
$ref = new Reference();
return $ref->deleteReferenceTag($id);
}
<?php
namespace Westdc\Visual\Handle;
use \Helpers\View as view;
use \Helpers\Table;
//事件中存在的操作
class VisualHandle extends \Westdc\Visual\VariableEncoder
{
private $db; //传入PDO对象误
private $config; //全局配置
function __construct()
{
$this->db = \Zend_Registry::get('db');
$this->config = \Zend_Registry::get('config');
}
public function checkParam(\Zend_EventManager_Event $e)
{
$data = $e->getParam('data');
if(empty($data['uuid']))
{
return "请填写数据UUID";
}
if(!is_array($data['vars']) || count($data['vars']) < 1)
{
return "参数错误";
}
return true;
}
public function processData(\Zend_EventManager_Event $e)
{
$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
namespace Westdc\Visual\Listener;
class VisualListener implements \Zend_EventManager_ListenerAggregate
{
private $event;
function __construct()
{
$this->event = new \Zend_EventManager_EventManager();
}
public function attach(\Zend_EventManager_EventCollection $events)
{
$Handler = new ReferenceHandler();
$events->attach('submit.checkParam', array($Handler, 'checkReferenceParam'), 100);
$events->attach('submit.processData', array($Handler, 'processReferenceData'), 100);
$events->attach('submit.recordPosted', array($Handler, 'recordPosted'), 100);
$events->attach('submit.recordChanged', array($Handler, 'recordChanged'), 100);
}
public function detach(\Zend_EventManager_EventCollection $events)
{
}
}
<?php
namespace Westdc\Visual\Listener;
use Westdc\Visual\Handle\VisualHandle;
class VisualListener implements \Zend_EventManager_ListenerAggregate
{
private $event;
function __construct()
{
$this->event = new \Zend_EventManager_EventManager();
}
public function attach(\Zend_EventManager_EventCollection $events)
{
$Handler = new VisualHandle();
$events->attach('submit.checkParam', array($Handler, 'checkParam'), 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)
{
}
}

View File

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

View File

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

View File

@ -6,9 +6,30 @@ class Record
public $db;
function __construct($recordType)
public $subdataset;
public $timeFiled = "utctime";
private $sql;
function __construct($uuid = NULL,$identifier = "")
{
$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()
@ -20,23 +41,60 @@ class Record
."dbname={$config->visual_db->database};"
."user={$config->visual_db->username};"
."password={$config->visual_db->password}";
$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
\"$index\" as \"index\", \"$fieldValue\" as \"value\", \"$fieldTime\" as \"time\"
FROM \"$table\"
{$this->timeFiled} as utctime , \"$fieldValue\" as value
FROM $table
ORDER BY
extract(year from \"$fieldTime\") ASC,
extract(month from \"$fieldTime\") ASC,
extract(day from \"$fieldTime\") ASC,
extract(YEAR from \"{$this->timeFiled}\") ASC,
extract(MONTH from \"{$this->timeFiled}\") 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 $rs;
return $sql;
}
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
namespace Westdc\Visual;
use Westdc\Visual\VisualListener;
class Visual
{
public $db;
function __construct($recordType)
{
$this->db = \Zend_Registry::get('db');
$Listener = new Listener();
@$this->getEventManager()->attachAggregate($Listener);
}
public function getEventManager(\Zend_EventManager_EventCollection $events = NULL)
{
if ($events !== NULL) {
$this->events = $events;
} elseif ($this->events === NULL) {
$this->events = new \Zend_EventManager_EventManager(__CLASS__);
}
return $this->events;
}
//添加
public function add($data,$id = 0)
{
$params = compact('data');
$results = $this->getEventManager()->trigger('submit.checkParam', $this, $params);
$cache_data = $results->last();
if($cache_data !== true)
{
return $cache_data;
}
$results = $this->getEventManager()->trigger('submit.processData', $this, $params);
$data = $results->last();
$dbh = new dbh();
if(empty($id))
{
$id = $dbh->insert($this->mainTable,$data,true);
if(!empty($id) && is_numeric($id))
{
$this->getEventManager()->trigger('submit.recordPosted', $this, $params);
return true;
}else{
if($id === false)
{
return '服务器开小差了,请稍后再试';
}else{
return '服务器处理中遇到错误,请联系管理员';
}
}
}//add
else{
if(!$dbh->update($this->mainTable,$data," id=$id ",true))
{
$this->getEventManager()->trigger('submit.recordChanged', $this, $params);
return "修改失败!请重试";
}else{
return true;
}
}//edit
}// add()
//删除
public function del($id)
{
if(!is_numeric($id) || empty($id) || $id< 0)
{
return false;
}
$sql = "DELETE FROM {$this->mainTable} WHERE id=$id";
return $this->db->exec($sql);
}
<?php
namespace Westdc\Visual;
use Westdc\Visual\Listener\VisualListener as Listener;
use Helpers\dbh;
class Visual
{
public $db;
protected $events;
function __construct()
{
$this->db = \Zend_Registry::get('db');
$this->mainTable = "datavisual";
$Listener = new Listener();
@$this->getEventManager()->attachAggregate($Listener);
}
public function getEventManager(\Zend_EventManager_EventCollection $events = NULL)
{
if ($events !== NULL) {
$this->events = $events;
} elseif ($this->events === NULL) {
$this->events = new \Zend_EventManager_EventManager(__CLASS__);
}
return $this->events;
}
//添加
public function add($data)
{
$params = compact('data');
$results = $this->getEventManager()->trigger('submit.checkParam', $this, $params);
$cache_data = $results->last();
if($cache_data !== true)
{
return $cache_data;
}
$results = $this->getEventManager()->trigger('submit.processData', $this, $params);
$data = $results->last();
$dbh = new dbh();
$record = $this->getVisualVars($data['uuid']);
if(empty($record))
{
$id = $dbh->insert($this->mainTable,$data);
if($id)
{
$this->getEventManager()->trigger('submit.recordPosted', $this, $params);
return true;
}else{
if($id === false)
{
return '服务器开小差了,请稍后再试';
}else{
return '服务器处理中遇到错误,请联系管理员';
}
}
}//add
else{
if(!$dbh->update($this->mainTable,$data," uuid='{$data['uuid']}' "))
{
$this->getEventManager()->trigger('submit.recordChanged', $this, $params);
return "修改失败!请重试";
}else{
return true;
}
}//edit
}// add()
//删除
public function del($uuid)
{
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);
}
}