<?
//Gaz meter total - Total compteur gaz
//The goal is to have a counter that display a value identical to the one of the physical gaz meter
//Le but est d'avoir un compteur qui affiche une valeur identique à celle du compteur physique

//http://localhost/script/?exec=gaz_total.php&perif_id=xxxxxx&action=update/origin&origin=gaz counter current value
//perif_id: This is the API ID of for the gaz counter in m3
//action
//	update=get current value from gaz counter and update total
//	origin=set the current value of the gaz counter

//ID pour le perif compteur de gaz en m3
$perif_id = getArg('perif_id');
//Get the action
$action = getArg('action');

//Si action=origin on doit avoir un 3ème paramètre qui nous donnne la valeur d'origine
if ($action = 'origin') {
	$origin = getArg('origin');
	saveVariable('total_gaz', $origin)
	exit; //Not sure this is supported
}

//Real job is done here
//Get current value for the gaz counter (m3)
$value_array = getValue($periph_id);
//Save the value in tmp variable
$today_m3 = $value_array['value'];

//Control if the counter was not reset after midnight
if ( $last_value <= $today_m3 )
	$delta = $today_m3 - $last_value
//$last_value > $today_m3 => the counter was reset after midnight
else
	$delta = $today_m3
	
//Update the counter
$total_gaz = $total_gaz + $delta
echo $total_gaz

//Display the value as xml that could be read by HTTP actioner with a XPATH /root/gaz_meter_total
sdk_header('text/xml');
$xmloutput="<root>";
$xmloutput .="<gaz_meter_total>";
$xmloutput .="$total_gaz";
$xmloutput .="</gaz_meter_total>";
$xmloutput .="</root>";
echo $xmloutput;

//Save the total counter
saveVariable('total_gaz', $total_gaz)
	
//Save the current value as last_value for next time the script is called
//saveVariable($last_value, $today_m3)
saveVariable('last_value', $today_m3)

?>