meterN 0.7

Talks about metering
Pongo
Posts: 30
Joined: Thu Sep 19, 2013 1:18 pm

Re: meterN 0.7

Post by Pongo » Mon Mar 09, 2015 4:07 pm

If I understood well, Dailycount will be available only at midnight. Right?
But I was asking if it was possible to have partial Dailycount (like on Dashboard page) available on shmop during all day.

Thanks

jeanmarc
Posts: 1888
Joined: Thu Aug 29, 2013 7:16 am

Re: meterN 0.7

Post by jeanmarc » Mon Mar 09, 2015 4:29 pm

You'll had midnight values at 00:00 ;) You can read midnight and the last 5 min row in shmop then make a simple calculation.
I don't get why you wish daily values in shmop, you can retrieve them in the csv with programs/programtotal.php :?:

Pongo
Posts: 30
Joined: Thu Sep 19, 2013 1:18 pm

Re: meterN 0.7

Post by Pongo » Mon Mar 09, 2015 5:34 pm

Sure, I could read dailycount from programs/programtotal.php,
but having meterN and 123solar on 2 different machines, I wrote a php page on 123solar to read meterN data (live and dailycount).

I worte also a php program on meterN to read and serve live and dailycount to 123solar, using shmop for live value and programs/programtotal.php for dailycount.
Everytime I invoke programs/programtotal.php it accesses csv files.
I read data every second, so I would prefer don't stress SD on meterN machine, reading csv files so often.
It could be better to have both live values and daily count on shmop.

I hope it's clear what I mean.

jeanmarc
Posts: 1888
Joined: Thu Aug 29, 2013 7:16 am

Re: meterN 0.7

Post by jeanmarc » Mon Mar 09, 2015 7:59 pm

I'll get it. Now, you can read last 5's min row and midnight values in shmop without stressing the csv reading. I' hope it fit to your need.

Pongo
Posts: 30
Joined: Thu Sep 19, 2013 1:18 pm

Re: meterN 0.7

Post by Pongo » Tue Mar 10, 2015 1:11 pm

It's clear now... after update to 0.7.4 I didn't see midnight values, till today :D

Now I can see
Live (#843): Array ( [UTC] => 1425989157 [PDC1] => 148.3 [FV2] => 0 [OLD_PDC3] => 0 )
Indicators (#844): Array ( [Power1] => 151.6 )
Memory (#845): Array ( [Mailflag1] => [Mailflag2] => [Mailflag3] => [5minflag] => 1 [Totalcounter1] => 215505 [Totalcounter2] => 32888685 [Totalcounter3] => 480254.39 [Midnight1] => 205696 [Midnight2] => 32874600 [Midnight3] => 480254.39 )
So I have

Code: Select all

Dailycount1 = Totalcounter1 - Midnight1
It's ok.

Thank you

EDIT: because I prefer to see Daily Total on main page title, instead of last 24h, I modified

Code: Select all

--- programs/bak/programmeter.php       2015-03-09 12:05:42.903095516 +0100
+++ programs/programmeter.php   2015-03-10 15:02:58.767301750 +0100
@@ -3,6 +3,7 @@
 define('checkaccess', TRUE);
 include('../config/config_main.php');
 include('../config/config_layout.php');
+include('../scripts/memory.php');

 date_default_timezone_set('GMT');

@@ -167,6 +168,16 @@
 $graphlist = array();
 $unitlist  = array();

+@$shmid = shmop_open($MEMORY, 'a', 0, 0);
+if (!empty($shmid)) {
+    $size = shmop_size($shmid);
+    shmop_close($shmid);
+    $shmid = shmop_open($MEMORY, 'c', 0644, $size);
+    $memdata  = shmop_read($shmid, 0, $size);
+    $memory = json_decode($memdata, true);
+    shmop_close($shmid);
+}
+
 $j = 0;
 for ($i = 1; $i <= $NUMMETER; $i++) {
     $prevarray = preg_split('/,/', $file[1]);
@@ -187,6 +198,13 @@
             $val_last += ${'PASSO' . $i} - $val_first;
         }
     }
+
+    $val_mid = $memory["Midnight$i"];
+    settype($val_mid, 'float');
+    $val_tot = $memory["Totalcounter$i"];
+    settype($val_tot, 'float');
+    $val_last = $val_tot - $val_mid;
+
     if (${'PRICE' . $i} > 0) {
         $money[$i] = number_format(($val_last * ${'PRICE' . $i}), 2, $DPOINT, $THSEP);
         $money[$i] = '/' . $money[$i] . $CURS;
and

Code: Select all

--- programs/bak/programmeter_feed.php  2015-03-09 12:05:33.763413218 +0100
+++ programs/programmeter_feed.php      2015-03-10 15:05:12.462683052 +0100
@@ -3,6 +3,7 @@
 define('checkaccess', TRUE);
 include('../config/config_main.php');
 include('../config/config_layout.php');
+include('../scripts/memory.php');

 date_default_timezone_set('GMT');

@@ -79,6 +80,16 @@
     $title = stripslashes("$dday");
 }

+@$shmid = shmop_open($MEMORY, 'a', 0, 0);
+if (!empty($shmid)) {
+    $size = shmop_size($shmid);
+    shmop_close($shmid);
+    $shmid = shmop_open($MEMORY, 'c', 0644, $size);
+    $memdata  = shmop_read($shmid, 0, $size);
+    $memory = json_decode($memdata, true);
+    shmop_close($shmid);
+}
+
 $graphlist = array();

 for ($i = 1; $i <= $NUMMETER; $i++) {
@@ -94,6 +105,13 @@
             $val_last += ${'PASSO' . $i} - $val_first;
         }
     }
+
+    $val_mid = $memory["Midnight$i"];
+    settype($val_mid, 'float');
+    $val_tot = $memory["Totalcounter$i"];
+    settype($val_tot, 'float');
+    $val_last = $val_tot - $val_mid;
+
     if (${'PRICE' . $i} > 0) {
         $money[$i] = number_format(($val_last * ${'PRICE' . $i}), 2, $DPOINT, $THSEP);
         $money[$i] = '/' . $money[$i] . $CURS;

jeanmarc
Posts: 1888
Joined: Thu Aug 29, 2013 7:16 am

Re: meterN 0.7

Post by jeanmarc » Thu Mar 12, 2015 11:31 am

Hi, their is a small bug in programmeter_feed.php line 19 please change to

Code: Select all

$dir    = '../data/csv/';
Forgot the last / :oops:

Pongo
Posts: 30
Joined: Thu Sep 19, 2013 1:18 pm

Re: meterN 0.7

Post by Pongo » Thu Mar 12, 2015 1:31 pm

OK.
Also

Code: Select all

$output = glob($dir . '*.csv');

ninodifr
Posts: 67
Joined: Wed May 07, 2014 11:03 pm

Re: meterN 0.7

Post by ninodifr » Thu Apr 02, 2015 2:31 pm

hello jeanmarc
these days I realized that programmeter can not calculate the local time
always take as a reference the timestamp of the time zone GMT
Consequently, from 00:00 to 02:00 the chart is not that of the previous day but that of two days ago,
in over the fascia sunset sunrise is offset two hours
previously I had not noticed because the phase shift was only an hour, with the arrival of summer time hours have become two, and the problem is now more evident.

jeanmarc
Posts: 1888
Joined: Thu Aug 29, 2013 7:16 am

Re: meterN 0.7

Post by jeanmarc » Thu Apr 02, 2015 6:53 pm

Hi,
I haven't the issue. Can you give me a look on your logger ? (in pm)

ninodifr
Posts: 67
Joined: Wed May 07, 2014 11:03 pm

Re: meterN 0.7

Post by ninodifr » Sun Apr 05, 2015 4:09 am

now works well
I solved thanks to your suggestion.

programmeter.php

Code: Select all

<?php
// Credit Louviaux Jean-Marc 2015
define('checkaccess', TRUE);
include('../config/config_main.php');
include('../config/config_layout.php');

date_default_timezone_set('GMT');

if (isset($_COOKIE['user_lang'])) {
    $user_lang = $_COOKIE['user_lang'];
} else {
    $user_lang = 'English';
}
include('../languages/' . $user_lang . '.php');

for ($i = 1; $i <= $NUMMETER; $i++) {
    include("../config/config_met$i.php");
}
$dir     = '../data/csv/';
$output  = array();
$stack[] = array();
$output  = glob($dir . '*.csv');
sort($output);
$cnt = count($output);

$file       = file($output[$cnt - 1]);
$contalines = count($file);

$startplot = 0;
$stopplot  = 0;
$linevalue = 0;

// Yesterday
date_default_timezone_set($DTZ); // local time
$yesterday = date('Ymd', time() - 60 * 60 * 24);
date_default_timezone_set('GMT'); // back to GMT
if ($contalines < 290 && file_exists($dir . "$yesterday.csv")) {
    $file = file($dir . "$yesterday.csv");
    
    $contalines2  = count($file) - 1;
    $prevyear     = substr($output[$cnt - 2], -12, 4);
    $prevmonth    = substr($output[$cnt - 2], -8, 2);
    $prevday      = substr($output[$cnt - 2], -6, 2);
	$fileUTCdate2 = strtotime(date("$prevyear$prevmonth$prevday"));

    $startline = $contalines2 - 290 + $contalines; // (24*12)+2 hearder
    if ($startline < 1) {
        $startline = 2;
    }
    
    for ($line_num = $startline; $line_num <= $contalines2; $line_num++) {
        $linearray    = preg_split("/,/", $file[$line_num]);
        $pastline_num = $line_num - 1;
        $prevarray    = preg_split("/,/", $file[$pastline_num]);
        
        $prevhour    = substr($prevarray[0], 0, 2);
        $prevmin     = substr($prevarray[0], 3, 2);
        $prevUTCdate = strtotime($prevyear . '-' . $prevmonth . '-' . $prevday . ' ' . $prevhour . ':' . $prevmin);
        $prevUTCdate *= 1000;
        
        $hour   = substr($linearray[0], 0, 2);
        $minute = substr($linearray[0], 3, 2);
        
        if ($line_num == $contalines2 && $hour == '00' && $minute == '00') { // today midnight
            $UTCdate = strtotime(date("$yesterday")) + 86400;
        } else {
            $UTCdate = strtotime($prevyear . '-' . $prevmonth . '-' . $prevday . ' ' . $hour . ':' . $minute);
        }
        $UTCdate *= 1000;
        
        for ($i = 1; $i <= $NUMMETER; $i++) {
            if (isset($prevarray[$i]) && isset($linearray[$i])) {
                $val_first = trim($prevarray[$i]);
                $val_last  = trim($linearray[$i]);
            } else {
                $val_first = 0;
                $val_last  = 0;
            }
            if (!empty($val_first) && $line_num == $startline + 1) {
                $val_first24[$i] = $val_first;
            }
            if (!empty($val_first) && !empty($val_last) && $UTCdate - $prevUTCdate == 300000) {
                settype($val_first, 'float');
                settype($val_last, 'float');
                if (${'TYPE' . $i} != 'Sensor') { // Meter
                    if ($val_first <= $val_last) {
                        $val_last -= $val_first;
                    } else { // counter pass over
                        $val_last += ${'PASSO' . $i} - $val_first;
                    }
                }
            } else {
                $val_last = 0;
            }
            $val_last             = round($val_last, ${'PRECI' . $i});
            $stack[$i][$line_num] = array(
                $UTCdate,
                $val_last
            );
        }
    }
    $linevalue = $line_num;
}

// Today
$year        = substr($output[$cnt - 1], -12, 4);
$month       = substr($output[$cnt - 1], -8, 2);
$day         = substr($output[$cnt - 1], -6, 2);
$file        = file($output[$cnt - 1]);
$fileUTCdate = strtotime(date("$year$month$day"));
$todayUTC    = strtotime(date('Ymd'));

$plotline = 1000 * $fileUTCdate; //plotline

for ($line_num = 2; $line_num < $contalines; $line_num++) {
    $linearray    = preg_split("/,/", $file[$line_num]);
    $pastline_num = $line_num - 1;
    $prevarray    = preg_split("/,/", $file[$pastline_num]);
    
    $prevhour    = substr($prevarray[0], 0, 2);
    $prevmin     = substr($prevarray[0], 3, 2);
    $prevUTCdate = strtotime($year . '-' . $month . '-' . $day . ' ' . $prevhour . ':' . $prevmin);
    $prevUTCdate *= 1000;
    
    $SDTE    = $linearray[0];
    $hour    = substr($SDTE, 0, 2);
    $minute  = substr($SDTE, 3, 2);
    $UTCdate = strtotime($year . '-' . $month . '-' . $day . ' ' . $hour . ':' . $minute);
    $UTCdate *= 1000;
    
    for ($i = 1; $i <= $NUMMETER; $i++) {
        if (isset($prevarray[$i]) && isset($linearray[$i])) {
            $val_first = trim($prevarray[$i]);
            $val_last  = trim($linearray[$i]);
        } else {
            $val_first = 0;
            $val_last  = 0;
        }
        if (!empty($val_first) && !empty($val_last) && $UTCdate - $prevUTCdate == 300000) {
            settype($val_first, 'float');
            settype($val_last, 'float');
            if (${'TYPE' . $i} != 'Sensor') { // Meter
                if ($val_first <= $val_last) {
                    $val_last -= $val_first;
                } else { // counter pass over
                    $val_last += ${'PASSO' . $i} - $val_first;
                }
            }
        } else {
            $val_last = 0;
        }
        $val_last                          = round($val_last, ${'PRECI' . $i});
        $stack[$i][$line_num + $linevalue] = array(
            $UTCdate,
            $val_last
        );
    }
}

if ($fileUTCdate == $todayUTC) {
    $title = '';
} elseif ($fileUTCdate == strtotime(date('Ymd', strtotime('-1 day')))) {
    $title = stripslashes("$lgYESTERDAYTITLE");
} else {
    $dday  = date($DATEFORMAT, $fileUTCdate);
    $title = stripslashes("$dday");
}

$graphlist = array();
$unitlist  = array();

$j = 0;
for ($i = 1; $i <= $NUMMETER; $i++) {
    $prevarray = preg_split('/,/', $file[1]);
    $linearray = preg_split('/,/', $file[$contalines - 1]);
    
    if (isset($val_first24[$i])) {
        $val_first = $val_first24[$i];
    } else {
        $val_first = trim($prevarray[$i]);
    }
    $val_last = trim($linearray[$i]);
    settype($val_first, 'float');
    settype($val_last, 'float');
    if (${'TYPE' . $i} != 'Sensor') { // Meter
        if ($val_first <= $val_last) {
            $val_last -= $val_first;
        } else { // counter pass over
            $val_last += ${'PASSO' . $i} - $val_first;
        }
    }
    if (${'PRICE' . $i} > 0) {
        $money[$i] = number_format(($val_last * ${'PRICE' . $i}), 2, $DPOINT, $THSEP);
        $money[$i] = '/' . $money[$i] . $CURS;
    } else {
        $money[$i] = '';
    }
    if (${'TYPE' . $i} == 'Elect') {
        if ($val_last <= 1000) {
            $prefix[$i] = '';
            $val_last   = number_format($val_last, 0, $DPOINT, $THSEP);
        } elseif ($val_last > 1000000) {
            $val_last /= 1000000;
            $prefix[$i] = 'M';
            $val_last   = number_format($val_last, 2, $DPOINT, $THSEP);
        } elseif ($val_last > 1000) {
            $val_last /= 1000;
            $prefix[$i] = 'k';
            $val_last   = number_format($val_last, 2, $DPOINT, $THSEP);
        }
    } else {
        $val_last   = number_format($val_last, ${'PRECI' . $i}, $DPOINT, $THSEP);
        $prefix[$i] = '';
    }
    
    $graphnum = ${'GRAPH_MET' . $i};
    if (!in_array($graphnum, $graphlist)) {
        $graphlist[]           = (int) $graphnum;
        $unitlist[$graphnum][] = ${'UNIT' . $i};
        $yaxis[$graphnum]      = (int) 0;
        
        $titlelist[$graphnum] = $title . ' ' . ${'METNAME' . $i} . " ($val_last$prefix[$i]${'UNIT'.$i}$money[$i]) ";
    } else {
        if (!in_array(${'UNIT' . $i}, $unitlist[$graphnum])) {
            $unitlist[$graphnum][] = ${'UNIT' . $i};
            $yaxis[$graphnum]++;
        }
        $titlelist[$graphnum] .= '- ' . ${'METNAME' . $i} . " ($val_last$prefix[$i]${'UNIT'.$i}$money[$i]) ";
    }
    sort($stack[$i]);
    //sort($unitlist[$graphnum]);
    $cntunit = count($unitlist[$graphnum]);
    
    for ($h = 0; $h < $cntunit; $h++) {
        if (${'UNIT' . $i} == $unitlist[$graphnum][$h]) {
            $thisyaxis = $h;
        }
    }
    
    if (${'FILL_MET' . $i} == 0) {
        $opa = 0.5;
    } else {
        $opa = 0;
    }
    
    $data[$j] = array(
        'name' => "${'METNAME'.$i}",
        'type' => 'areaspline',
        'fillOpacity' => $opa,
        'color' => "#${'COLOR'.$i}",
        'data' => $stack[$i],
        'yAxis' => $thisyaxis
    );
    
    $j++;
}
//?sort($data); 
ksort($titlelist);
$titlelist = array_values($titlelist);

$sun_info = date_sun_info($fileUTCdate, $LATITUDE, $LONGITUDE);

if (file_exists($dir . "$yesterday.csv")) {
    $sun_info2 = date_sun_info($fileUTCdate2, $LATITUDE, $LONGITUDE);
    date_default_timezone_set($DTZ); // local time
    $ystrtp = date('Ymd H:i', $sun_info2['sunset']); // yestd night plotbands
    $ystpp  = date('Ymd H:i', $sun_info['sunrise']);
    date_default_timezone_set('GMT'); // back to GMT
    $ystrtp = (strtotime($ystrtp)) * 1000;
    $ystpp  = (strtotime($ystpp)) * 1000;
} else {
    $ystrtp = 0;
    $ystpp  = 0;
}

date_default_timezone_set($DTZ); // local time
$tstrtp = date('Ymd H:i', $sun_info['sunset']); // today
date_default_timezone_set('GMT'); // back to GMT
$tstrtp = (strtotime($tstrtp)) * 1000;
$tstpp = (strtotime($year . '-' . $month . '-' . $day . ' 23:59:59')) * 1000;

$jsonreturn = array(
    'data' => $data,
    'title' => $titlelist,
    'plotline' => $plotline,
    'ystrtp' => $ystrtp,
    'ystpp' => $ystpp,
    'tstrtp' => $tstrtp,
    'tstpp' => $tstpp
);

header("Content-type: text/json");
echo json_encode($jsonreturn);

?>

Locked

Who is online

Users browsing this forum: No registered users and 2 guests