Skip to content

Commit

Permalink
Merge pull request #335 from emoncms/v8.5-unixtime
Browse files Browse the repository at this point in the history
unix time instead of date string for last time-values
  • Loading branch information
TrystanLea committed Jun 2, 2015
2 parents f78a9c9 + 4f6c7fe commit 49f0383
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Modules/feed/engine/PHPFina.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public function lastvalue($id)
fclose($fh);

$val = unpack("f",$d);
$time = date("Y-n-j H:i:s", $meta->start_time + $meta->interval * $meta->npoints);
$time = $meta->start_time + $meta->interval * $meta->npoints;

return array('time'=>$time, 'value'=>$val[1]);
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/feed/engine/PHPFiwa.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ public function lastvalue($id)
fclose($fh);

$val = unpack("f",$d);
$time = date("Y-n-j H:i:s", $meta->start_time + $meta->interval[0] * $meta->npoints[0]);
$time = $meta->start_time + $meta->interval[0] * $meta->npoints[0];

return array('time'=>$time, 'value'=>$val[1]);
}
Expand Down
1 change: 0 additions & 1 deletion Modules/feed/engine/PHPTimeSeries.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ public function lastvalue($feedid)
fseek($fh,$filesize-9);
$d = fread($fh,9);
$array = unpack("x/Itime/fvalue",$d);
$array['time'] = date("Y-n-j H:i:s", $array['time']);
return $array;
}
else
Expand Down
19 changes: 10 additions & 9 deletions Modules/feed/feed_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public function redis_get_user_feeds($userid)
$row = $this->redis->hGetAll("feed:$id");

$lastvalue = $this->get_timevalue($id);
$row['time'] = strtotime($lastvalue['time']);
$row['time'] = $lastvalue['time'];
$row['value'] = $lastvalue['value'];
$feeds[] = $row;
}
Expand All @@ -214,7 +214,6 @@ public function mysql_get_user_feeds($userid)
$result = $this->mysqli->query("SELECT * FROM feeds WHERE `userid` = '$userid'");
while ($row = (array)$result->fetch_object())
{
$row['time'] = strtotime($row['time']);
$feeds[] = $row;
}
return $feeds;
Expand Down Expand Up @@ -255,7 +254,6 @@ public function get($id)
// Get from mysql db
$result = $this->mysqli->query("SELECT * FROM feeds WHERE `id` = '$id'");
$row = (array) $result->fetch_object();
$row['time'] = strtotime($row['time']);
}

return $row;
Expand Down Expand Up @@ -307,15 +305,19 @@ public function get_timevalue($id)
$row = $result->fetch_array();
$lastvalue = array('time'=>$row['time'], 'value'=>$row['value']);
}

// check for date string format and convert to unix time.
if (!is_numeric($lastvalue['time'])) {
$lastvalue['time'] = strtotime($lastvalue['time']);
if ($lastvalue['time']=="") $lastvalue['time'] = 0;
}

return $lastvalue;
}

public function get_timevalue_seconds($id)
{
$lastvalue = $this->get_timevalue($id);
$lastvalue['time'] = strtotime($lastvalue['time']);
return $lastvalue;
return $this->get_timevalue($id);
}

public function get_value($id)
Expand Down Expand Up @@ -564,11 +566,10 @@ public function phpfina_export($feedid,$start) {

public function set_timevalue($feedid, $value, $time)
{
$updatetime = date("Y-n-j H:i:s", $time);
if ($this->redis) {
$this->redis->hMset("feed:lastvalue:$feedid", array('value' => $value, 'time' => $updatetime));
$this->redis->hMset("feed:lastvalue:$feedid", array('value' => $value, 'time' => $time));
} else {
$this->mysqli->query("UPDATE feeds SET `time` = '$updatetime', `value` = '$value' WHERE `id`= '$feedid'");
$this->mysqli->query("UPDATE feeds SET `time` = '$time', `value` = '$value' WHERE `id`= '$feedid'");
}
}

Expand Down
17 changes: 6 additions & 11 deletions Modules/input/process_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ public function power_to_kwh($feedid, $time_now, $value)
// Get last value
$last = $this->feed->get_timevalue($feedid);

$last['time'] = strtotime($last['time']);
if (!isset($last['value'])) $last['value'] = 0;
$last_kwh = $last['value']*1;
$last_time = $last['time']*1;
Expand Down Expand Up @@ -271,7 +270,6 @@ public function power_to_kwhd($feedid, $time_now, $value)
// Get last value
$last = $this->feed->get_timevalue($feedid);

$last['time'] = strtotime($last['time']);
if (!isset($last['value'])) $last['value'] = 0;
$last_kwh = $last['value']*1;
$last_time = $last['time']*1;
Expand Down Expand Up @@ -311,7 +309,7 @@ public function kwh_to_kwhd($feedid, $time_now, $value)
if (!$redis) return $value; // return if redis is not available

$currentkwhd = $this->feed->get_timevalue($feedid);
$last_time = strtotime($currentkwhd['time']);
$last_time = $currentkwhd['time'];

//$current_slot = floor($time_now / 86400) * 86400;
//$last_slot = floor($last_time / 86400) * 86400;
Expand Down Expand Up @@ -347,7 +345,7 @@ public function input_ontime($feedid, $time_now, $value)
{
// Get last value
$last = $this->feed->get_timevalue($feedid);
$last_time = strtotime($last['time']);
$last_time = $last['time'];

//$current_slot = floor($time_now / 86400) * 86400;
//$last_slot = floor($last_time / 86400) * 86400;
Expand Down Expand Up @@ -398,7 +396,7 @@ public function save_to_input($inputid, $time, $value)
public function kwhinc_to_kwhd($feedid, $time_now, $value)
{
$last = $this->feed->get_timevalue($feedid);
$last_time = strtotime($last['time']);
$last_time = $last['time'];

//$current_slot = floor($time_now / 86400) * 86400;
//$last_slot = floor($last_time / 86400) * 86400;
Expand Down Expand Up @@ -458,7 +456,7 @@ public function histogram($feedid, $time_now, $value)

// Get the last time
$lastvalue = $this->feed->get_timevalue($feedid);
$last_time = strtotime($lastvalue['time']);
$last_time = $lastvalue['time'];

// kWh calculation
if ((time()-$last_time)<7200) {
Expand Down Expand Up @@ -503,7 +501,6 @@ public function pulse_diff($feedid,$time_now,$value)
{
$pulse_diff = 0;
$last = $this->feed->get_timevalue($feedid);
$last['time'] = strtotime($last['time']);
if ($last['time']) {
// Need to handle resets of the pulse value (and negative 2**15?)
if ($value >= $last['value']) {
Expand Down Expand Up @@ -544,8 +541,7 @@ public function max_value($feedid, $time_now, $value)
$last = $this->feed->get_timevalue($feedid);

$last_val = $last['value'];
$last_time = strtotime($last['time']);
if ($last['time']=="") $last_time = 0;
$last_time = $last['time'];

$feedtime = $this->getstartday($time_now);
$time_check = $this->getstartday($last_time);
Expand All @@ -565,8 +561,7 @@ public function min_value($feedid, $time_now, $value)
$last = $this->feed->get_timevalue($feedid);

$last_val = $last['value'];
$last_time = strtotime($last['time']);
if ($last['time']=="") $last_time = 0;
$last_time = $last['time'];

$feedtime = $this->getstartday($time_now);
$time_check = $this->getstartday($last_time);
Expand Down
1 change: 0 additions & 1 deletion Modules/log/EmonLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public function __construct($clientFileName)
return;
}


if (LOG4PHP_INSTALLED){
Logger::configure( $log4php_configPath );
$clientFileNameWithoutPath = basename($clientFileName);
Expand Down
2 changes: 1 addition & 1 deletion logconfig.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<configuration xmlns="http://logging.apache.org/log4php/">
<appender name="myAppender" class="LoggerAppenderFile">
<param name="file" value="emoncms.log" />
<param name="file" value="/var/log/emoncms.log" />
</appender>
<root>
<level value="WARN" />
Expand Down

0 comments on commit 49f0383

Please sign in to comment.