Skip to content

Commit

Permalink
Merge branch 'alexandrecuer-emonpi-mod' into emonpi
Browse files Browse the repository at this point in the history
  • Loading branch information
glynhudson committed Feb 24, 2019
2 parents f0b0401 + 3727f56 commit 6583d87
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 30 deletions.
34 changes: 22 additions & 12 deletions postprocess-module/postprocess_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ function postprocess_controller()
// -------------------------------------------------------------------------
// PROCESS LIST
// -------------------------------------------------------------------------
if ($route->action == "list2" && $session['write']) {
$route->format = "json";
return $postprocess->get($session['userid']);
}

if ($route->action == "list" && $session['write']) {

$userid = $session['userid'];
Expand All @@ -127,12 +132,16 @@ function postprocess_controller()
if ($feed->exist((int)$id)) {
$f = $feed->get($id);
if ($f['userid']!=$session['userid']) return false;
$meta = $feed->get_meta($id);
$f['start_time'] = $meta->start_time;
$f['interval'] = $meta->interval;
$f['npoints'] = $meta->npoints;
$f['id'] = (int) $f['id'];

if ($meta = $feed->get_meta($id)) {
$f['start_time'] = $meta->start_time;
$f['interval'] = $meta->interval;
$f['npoints'] = $meta->npoints;
$f['id'] = (int) $f['id'];
$timevalue = $feed->get_timevalue($id);
$f['time'] = $timevalue["time"];
} else {
$valid = false;
}
$item->$key = $f;
} else {
$valid = false;
Expand All @@ -141,24 +150,25 @@ function postprocess_controller()

if ($option['type']=="formula"){
$formula=$processlist[$i]->$key;
$f=[];
$f=array();
$f['expression']=$formula;
//we catch feed numbers in the formula
$feed_ids=[];
$feed_ids=array();
while(preg_match("/(f\d+)/",$formula,$b)){
$feed_ids[]=substr($b[0],1,strlen($b[0])-1);
$formula=str_replace($b[0],"",$formula);
}
$all_intervals=[];
$all_start_times=[];
$all_ending_times=[];
$all_intervals=array();
$all_start_times=array();
$all_ending_times=array();
//we check feeds existence and stores all usefull metas
foreach($feed_ids as $id) {
if ($feed->exist((int)$id)){
$m=$feed->get_meta($id);
$all_intervals[]=$m->interval;
$all_start_times[]=$m->start_time;
$all_ending_times[]=$feed->get_timevalue($id)['time'];
$timevalue = $feed->get_timevalue($id);
$all_ending_times[] = $timevalue["time"];
} else {
$valid = false;
}
Expand Down
20 changes: 10 additions & 10 deletions postprocess-module/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@
overflow-x: hidden;
}
</style>
<table class="table table-hover">
<tr><td colspan=2><h2>Post Processor</h2></td></tr>
<tr>
<td><h3>Logger</h3><p>on file /home/pi/data/postprocess.log</p></td>
<td class="buttons">
<button id="getlog" type="button" class="btn btn-info" data-toggle="button" aria-pressed="false" autocomplete="off"><?php echo _('Auto refresh'); ?></button>
</td>
</tr>
<tr><td colspan=2><pre id="logreply-bound"><div id="logreply"></div></pre></td></tr>
</table>

<h2>Post Processor</h2>
<p>Rather than process inputs as data arrives in emoncms such as calculating cumulative kWh data from power data with the power to kWh input process, this module can be used to do these kind of processing steps after having recorded base data such as power data for some time. This removes the reliance on setting up everything right in the first instance providing the flexibility to recalculate processed feeds at a later date.</p>

<hr>
<button id="getlog" type="button" class="btn btn-info" data-toggle="button" aria-pressed="false" autocomplete="off" style="float:right; margin-top:10px"><?php echo _('Auto refresh'); ?></button>
<h3>Logger</h3>
<p>on file <?php global $homedir; echo $homedir; ?>/data/postprocess.log</p>
<pre id="logreply-bound"><div id="logreply"></div></pre>

<hr>

<h3>My processes</h3>

<table class="table">
Expand Down
31 changes: 23 additions & 8 deletions processes/powertokwh.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ function powertokwh($dir,$processitem)
}

$im = getmeta($dir,$input);
print "input meta: ".json_encode($im)."\n";

$om = getmeta($dir,$output);

print "output meta: ".json_encode($om)."\n";
/*
if ($im->interval != $om->interval) {
print "feed intervals do not match\n";
Expand Down Expand Up @@ -58,22 +60,35 @@ function powertokwh($dir,$processitem)
$wh = $tmp[1]*1000.0;
}

$spurious_value_count = 0;

for ($n=$om->npoints; $n<$im->npoints; $n++) {
$tmp = unpack("f",fread($if,4));

if (!is_nan($tmp[1])) $power = 1*$tmp[1];

// $joules += $power * $im->interval;
// $wh += floor($joules / 3600.0);
// $joules = $joules % 3600;

$wh += ($power * $im->interval) / 3600.0;

$buffer .= pack("f",$wh*0.001);
// filter spurious power values +-1MW
if ($power>-1000000.0 && $power<1000000.0) {

// $joules += $power * $im->interval;
// $wh += floor($joules / 3600.0);
// $joules = $joules % 3600;

$wh += ($power * $im->interval) / 3600.0;
$buffer .= pack("f",$wh*0.001);
} else {
$spurious_value_count++;
}
}

fwrite($of,$buffer);

if ($spurious_value_count>0) {
print "-------------------------------------------------------";
print "Found and filtered $spurious_value_count values +-1MW\n";
print "-------------------------------------------------------";
}

print "bytes written: ".strlen($buffer)."\n";
fclose($of);
fclose($if);
Expand Down

0 comments on commit 6583d87

Please sign in to comment.