From f0f0e639b545ae690bdfee75de6fe5d9a728bc72 Mon Sep 17 00:00:00 2001 From: Li Jianxuan Date: Wed, 11 Jun 2014 08:12:05 +0000 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=B8=89=E6=B1=9F=E6=BA=90?= =?UTF-8?q?=E5=AD=90=E5=B9=B3=E5=8F=B0=E5=8F=AF=E8=A7=86=E5=8C=96=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Westdc/Visual/Handle/VisualHandle.php | 105 ++++++---- .../Westdc/Visual/Listener/VisualListener.php | 56 ++--- .../module/Westdc/Visual/Reader/Sc.php | 140 ++++++------- .../Westdc/Visual/Reader/SoilMoisture.php | 6 +- application/module/Westdc/Visual/Record.php | 78 ++++++- .../module/Westdc/Visual/VariableEncoder.php | 44 ++++ application/module/Westdc/Visual/Visual.php | 195 ++++++++++-------- 7 files changed, 388 insertions(+), 236 deletions(-) create mode 100644 application/module/Westdc/Visual/VariableEncoder.php diff --git a/application/module/Westdc/Visual/Handle/VisualHandle.php b/application/module/Westdc/Visual/Handle/VisualHandle.php index 566e92e3..24850071 100644 --- a/application/module/Westdc/Visual/Handle/VisualHandle.php +++ b/application/module/Westdc/Visual/Handle/VisualHandle.php @@ -1,43 +1,64 @@ -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); - } +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'); + + } + } \ No newline at end of file diff --git a/application/module/Westdc/Visual/Listener/VisualListener.php b/application/module/Westdc/Visual/Listener/VisualListener.php index b5bcf3f5..7a172386 100644 --- a/application/module/Westdc/Visual/Listener/VisualListener.php +++ b/application/module/Westdc/Visual/Listener/VisualListener.php @@ -1,27 +1,29 @@ -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) - { - - } - -} +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) + { + + } + +} diff --git a/application/module/Westdc/Visual/Reader/Sc.php b/application/module/Westdc/Visual/Reader/Sc.php index 49b2f1f5..4fcbe1d4 100644 --- a/application/module/Westdc/Visual/Reader/Sc.php +++ b/application/module/Westdc/Visual/Reader/Sc.php @@ -1,71 +1,71 @@ -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; - } - +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; + } + } \ No newline at end of file diff --git a/application/module/Westdc/Visual/Reader/SoilMoisture.php b/application/module/Westdc/Visual/Reader/SoilMoisture.php index 0b363a0e..f7f98710 100644 --- a/application/module/Westdc/Visual/Reader/SoilMoisture.php +++ b/application/module/Westdc/Visual/Reader/SoilMoisture.php @@ -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 diff --git a/application/module/Westdc/Visual/Record.php b/application/module/Westdc/Visual/Record.php index 0b390e30..c368be6d 100644 --- a/application/module/Westdc/Visual/Record.php +++ b/application/module/Westdc/Visual/Record.php @@ -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=''){ diff --git a/application/module/Westdc/Visual/VariableEncoder.php b/application/module/Westdc/Visual/VariableEncoder.php new file mode 100644 index 00000000..986bf001 --- /dev/null +++ b/application/module/Westdc/Visual/VariableEncoder.php @@ -0,0 +1,44 @@ +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); + } + +} \ No newline at end of file diff --git a/application/module/Westdc/Visual/Visual.php b/application/module/Westdc/Visual/Visual.php index e97bfae8..0627fa5d 100644 --- a/application/module/Westdc/Visual/Visual.php +++ b/application/module/Westdc/Visual/Visual.php @@ -1,86 +1,111 @@ -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); - } +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); + } } \ No newline at end of file