2014-05-12 06:53:21 +00:00
|
|
|
<?php
|
|
|
|
namespace Westdc\Visual;
|
|
|
|
|
|
|
|
class Record
|
|
|
|
{
|
2014-05-22 02:44:18 +00:00
|
|
|
|
|
|
|
public $db;
|
|
|
|
|
2014-06-11 08:12:05 +00:00
|
|
|
public $subdataset;
|
|
|
|
|
|
|
|
public $timeFiled = "utctime";
|
|
|
|
|
|
|
|
private $sql;
|
|
|
|
|
|
|
|
function __construct($uuid = NULL,$identifier = "")
|
2014-05-12 06:53:21 +00:00
|
|
|
{
|
2014-05-22 02:44:18 +00:00
|
|
|
$this->initDatabase();
|
2014-06-11 08:12:05 +00:00
|
|
|
|
|
|
|
if(!empty($identifier))
|
|
|
|
{
|
|
|
|
$this->subdataset = $identifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!empty($uuid) && !empty($identifier))
|
|
|
|
{
|
|
|
|
$this->initVisual($uuid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __invoke()
|
|
|
|
{
|
|
|
|
return $this->getData();
|
2014-05-22 02:44:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function initDatabase()
|
|
|
|
{
|
|
|
|
$config = \Zend_Registry::get('config');
|
|
|
|
|
|
|
|
$dsn = "pgsql:host={$config->visual_db->hostname};"
|
|
|
|
."port={$config->visual_db->port};"
|
|
|
|
."dbname={$config->visual_db->database};"
|
|
|
|
."user={$config->visual_db->username};"
|
|
|
|
."password={$config->visual_db->password}";
|
2014-06-11 08:12:05 +00:00
|
|
|
|
2014-05-22 02:44:18 +00:00
|
|
|
$this->db = new \PDO($dsn);
|
2014-05-21 09:26:15 +00:00
|
|
|
}
|
|
|
|
|
2014-06-11 08:12:05 +00:00
|
|
|
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)
|
2014-05-21 09:26:15 +00:00
|
|
|
{
|
|
|
|
$sql = "SELECT
|
2014-06-11 08:12:05 +00:00
|
|
|
{$this->timeFiled} as utctime , \"$fieldValue\" as value
|
|
|
|
FROM $table
|
2014-05-21 09:26:15 +00:00
|
|
|
ORDER BY
|
2014-06-11 08:12:05 +00:00
|
|
|
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
|
2014-05-21 09:26:15 +00:00
|
|
|
";
|
|
|
|
|
2014-06-11 08:12:05 +00:00
|
|
|
return $sql;
|
2014-05-12 06:53:21 +00:00
|
|
|
}
|
|
|
|
|
2014-05-21 09:26:15 +00:00
|
|
|
public function utcMsTime($time=''){
|
|
|
|
if(empty($time))
|
|
|
|
return (time()+8*3600)*1000;
|
|
|
|
else
|
|
|
|
return $time*1000;
|
|
|
|
}
|
2014-05-12 06:53:21 +00:00
|
|
|
}
|