bolzano a écrit:C'est à dire ?
Si tu n'as qu'un périphérique qui te remonte ton nombre de kwh total il faut utiliser son API pour les calculs du tuto. (Dans le tuto il y a la partie somme HC+HP qui est inutile dans ton cas)
<values>
<action/>
<ids/>
<options>
<opts/>
</options>
<error>action inconnu</error>
</values>
<?php
/*
action : [get]
Parametre : ids=IDAPI[,...,IDAPI]
action : [addhisto]
Parametre : ids=IDAPI[,...,IDAPI]
opt=[minitems=2],[maxitems=2],prefix=['']
action : [gethisto]
Parametre : ids=IDAPI[,...,IDAPI]
opt=[minitems=2],prefix=['']
action : [clearhisto]
Parametre : ids=IDAPI[,...,IDAPI]
action : [datepart]
*/
$action = getArg('action', false, ''); // action a faire
$ids = getArg('ids', false, ''); // liste des id
$options = getArg('opt', false, ''); // options
$doecho = true;
$arrayids = explode(',',$ids);
$arrayTemp = explode(',',$options);
$arrayopts = array();
foreach ($arrayTemp as $optItemAndVal)
{
$optArrayItem = explode('=',$optItemAndVal);
if (count($optArrayItem) == 2)
{
$arrayopts[$optArrayItem[0]] = $optArrayItem[1];
$xmlopts.= '<'.$optArrayItem[0].'>'.$optArrayItem[1].'</'.$optArrayItem[0].'>'."\n";
}
}
switch ($action)
{
case 'get':
case 'addhisto':
case 'gethisto':
case 'clearhisto':
$xmlval = sdk_ActionValues($action, $arrayids , $arrayopts);
break;
case 'datepart':
$xmlval = sdk_DatePart();
break;
case 'dumphisto':
$prefix = $arrayopts['prefix'];
$periphId = $arrayids[0];
$var = loadVariable($prefix.'SaveArray'.$periphId);
var_dump($var);
$doecho = false;
break;
default:
$xmlval = ' <error>action inconnu</error>'."\n";
}
// construction xml en retour
if ($doecho)
{
sdk_header('text/xml');
$xml = '<?xml version="1.0" encoding="utf8" ?>'."\n";
$xml .= '<values>'."\n";
$xml .= ' <action>'.$action.'</action>'."\n";
$xml .= ' <ids>'.$ids.'</ids>'."\n";
$xml .= ' <options>'."\n";
$xml .= ' <opts>'.$options.'</opts>'."\n";
$xml .= $xmlopts;
$xml .= ' </options>'."\n";
$xml .= $xmlval;
$xml .= '</values>';
echo $xml;
}
function sdk_ActionValues($action, $arrayids, $arrayopts)
{
$ret = '';
$ret2 ='';
$maxitems = $arrayopts['maxitems'];
$minitems = $arrayopts['minitems'];
$prefix = $arrayopts['prefix'];
if ($maxitems == '' or $maxitems < 2) $maxitems = 2;
if ($minitems == '' or $minitems < 2) $minitems = 2;
if ($minitems > $maxitems) $maxitems = $minitems;
foreach ($arrayids as $periphId)
{
$ret .= '<periph id="'.$periphId.'">'."\n";
$value = getValue($periphId);
$valValue = $value['value'];
$valLastChange = $value['change'];
$valDuration = time() - strtotime($valLastChange);
$ret .= ' <value>'.$valValue.'</value>'."\n";
$ret .= ' <change>'.$valLastChange.'</change>'."\n";
$ret .= ' <duration>'.$valDuration.'</duration>'."\n";
if ($action == 'clearhisto')
{
saveVariable($prefix.'SaveArray'.$periphId, '');
}
if ($action == 'addhisto' || $action == 'gethisto')
{
$prevData = loadVariable($prefix.'SaveArray'.$periphId);
$newSaveVal = array('value' => $valValue, 'change' => $valLastChange, 'datesave' => date('Y-m-d H:i:s'));
if ($prevData == '')
{
$prevData = array();
$prevData[0]=$minitems;
for ($i=1; $i<=$minitems ; $i++)
{
$prevData[$i]=$newSaveVal;
}
saveVariable($prefix.'SaveArray'.$periphId, $prevData);
}
$nbvalues = count($prevData) - 1;
if ($nbvalues > 0 && $nbvalues < $minitems)
{
for ($i = $nbvalues ; $i < $minitems ; $i++)
{
$prevData[$i+1] = $prevData[$i];
}
saveVariable($prefix.'SaveArray'.$periphId, $prevData);
$nbvalues = count($prevData) - 1;
}
if ($action == 'addhisto')
{
$newData = array();
$newData[1] = $newSaveVal;
for ($i = 1; $i<$maxitems && $i<=$nbvalues ; $i++)
{
$newData[$i+1] = $prevData[$i];
}
$newData[0]=count($newData) - 1;
saveVariable($prefix.'SaveArray'.$periphId, $newData);
}
}
if ($action == 'addhisto' || $action == 'gethisto')
{
$prevData = loadVariable($prefix.'SaveArray'.$periphId);
if ($prevData != '')
{
$prevDate= time();
for ($i=0; $i < count($prevData)-1; $i++)
{
$oldSaveVal = $prevData[$i+1];
$duration = $prevDate - strtotime($oldSaveVal['change']);
$ValCurrent = $oldSaveVal['value'];
$ret2 .= '<histo id="'.$i.'">'."\n";
$ret2 .= ' <value>'.$oldSaveVal['value'].'</value>'."\n";
$ret2 .= ' <change>'.$oldSaveVal['change'].'</change>'."\n";
$ret2 .= ' <datesave>'.$oldSaveVal['datesave'].'</datesave>'."\n";
$ret2 .= ' <duration>'.$duration.'</duration>'."\n";
$ret2 .= '</histo>'."\n";
$prevDate = strtotime($oldSaveVal['change']);
$ValPrevious = $ValCurrent;
}
}
}
$ret .= $ret2;
$ret .= '</periph>'."\n";
}
return $ret;
}
function sdk_DatePart()
{
$now = time();
$ret = '<datepart>'."\n";
$ret .= ' <second>'.date('s', $now).'</second>'."\n";
$ret .= ' <minute>'.date('i', $now).'</minute>'."\n";
$ret .= ' <hour>'.date('H', $now).'</hour>'."\n";
$ret .= ' <day>'.date('d', $now).'</day>'."\n";
$ret .= ' <dayofweek>'.date('w', $now).'</dayofweek>'."\n";
$ret .= ' <dayofyear>'.date('z', $now).'</dayofyear>'."\n";
$ret .= ' <week>'.date('W', $now).'</week>'."\n";
$ret .= ' <month>'.date('m', $now).'</month>'."\n";
$ret .= ' <year>'.date('Y', $now).'</year>'."\n";
$ret .= ' <timenum>'.date('His', $now).'</timenum>'."\n";
$ret .= ' <time>'.date('H:i:s', $now).'</time>'."\n";
$ret .= ' <date>'.date('Y-m-d', $now).'</date>'."\n";
$ret .= ' <full>'.date('Y-m-d H:i:s', $now).'</full>'."\n";
$ret .= '</datepart>'."\n";
return $ret;
}
?>
<br/> <b> Warning</>: cannot....
<?php
/*
action : [get]
Parametre : ids=IDAPI[,...,IDAPI]
action : [addhisto]
Parametre : ids=IDAPI[,...,IDAPI]
opt=[minitems=2],[maxitems=2],prefix=['']
action : [gethisto]
Parametre : ids=IDAPI[,...,IDAPI]
opt=[minitems=2],prefix=['']
action : [clearhisto]
Parametre : ids=IDAPI[,...,IDAPI]
action : [datepart]
*/
$action = getArg('action', false, ''); // action a faire
$ids = getArg('ids', false, ''); // liste des id
$options = getArg('opt', false, ''); // options
$doecho = true;
$arrayids = explode(',',$ids);
$arrayTemp = explode(',',$options);
$arrayopts = array();
foreach ($arrayTemp as $optItemAndVal)
{
$optArrayItem = explode('=',$optItemAndVal);
if (count($optArrayItem) == 2)
{
$arrayopts[$optArrayItem[0]] = $optArrayItem[1];
$xmlopts.= '<'.$optArrayItem[0].'>'.$optArrayItem[1].'</'.$optArrayItem[0].'>'."\n";
}
}
switch ($action)
{
case 'get':
case 'addhisto':
case 'gethisto':
case 'clearhisto':
$xmlval = sdk_ActionValues($action, $arrayids , $arrayopts);
break;
case 'datepart':
$xmlval = sdk_DatePart();
break;
case 'dumphisto':
$prefix = $arrayopts['prefix'];
$periphId = $arrayids[0];
$var = loadVariable($prefix.'SaveArray'.$periphId);
var_dump($var);
$doecho = false;
break;
default:
$xmlval = ' <error>action inconnu</error>'."\n";
}
// construction xml en retour
if ($doecho)
{
sdk_header('text/xml');
$xml .= '<values>'."\n";
$xml .= ' <action>'.$action.'</action>'."\n";
$xml .= ' <ids>'.$ids.'</ids>'."\n";
$xml .= ' <options>'."\n";
$xml .= ' <opts>'.$options.'</opts>'."\n";
$xml .= $xmlopts;
$xml .= ' </options>'."\n";
$xml .= $xmlval;
$xml .= '</values>';
echo $xml;
}
function sdk_ActionValues($action, $arrayids, $arrayopts)
{
$ret = '';
$ret2 ='';
$maxitems = $arrayopts['maxitems'];
$minitems = $arrayopts['minitems'];
$prefix = $arrayopts['prefix'];
if ($maxitems == '' or $maxitems < 2) $maxitems = 2;
if ($minitems == '' or $minitems < 2) $minitems = 2;
if ($minitems > $maxitems) $maxitems = $minitems;
foreach ($arrayids as $periphId)
{
$ret .= '<periph id="'.$periphId.'">'."\n";
$value = getValue($periphId);
$valValue = $value['value'];
$valLastChange = $value['change'];
$valDuration = time() - strtotime($valLastChange);
$ret .= ' <value>'.$valValue.'</value>'."\n";
$ret .= ' <change>'.$valLastChange.'</change>'."\n";
$ret .= ' <duration>'.$valDuration.'</duration>'."\n";
if ($action == 'clearhisto')
{
saveVariable($prefix.'SaveArray'.$periphId, '');
}
if ($action == 'addhisto' || $action == 'gethisto')
{
$prevData = loadVariable($prefix.'SaveArray'.$periphId);
$newSaveVal = array('value' => $valValue, 'change' => $valLastChange, 'datesave' => date('Y-m-d H:i:s'));
if ($prevData == '')
{
$prevData = array();
$prevData[0]=$minitems;
for ($i=1; $i<=$minitems ; $i++)
{
$prevData[$i]=$newSaveVal;
}
saveVariable($prefix.'SaveArray'.$periphId, $prevData);
}
$nbvalues = count($prevData) - 1;
if ($nbvalues > 0 && $nbvalues < $minitems)
{
for ($i = $nbvalues ; $i < $minitems ; $i++)
{
$prevData[$i+1] = $prevData[$i];
}
saveVariable($prefix.'SaveArray'.$periphId, $prevData);
$nbvalues = count($prevData) - 1;
}
if ($action == 'addhisto')
{
$newData = array();
$newData[1] = $newSaveVal;
for ($i = 1; $i<$maxitems && $i<=$nbvalues ; $i++)
{
$newData[$i+1] = $prevData[$i];
}
$newData[0]=count($newData) - 1;
saveVariable($prefix.'SaveArray'.$periphId, $newData);
}
}
if ($action == 'addhisto' || $action == 'gethisto')
{
$prevData = loadVariable($prefix.'SaveArray'.$periphId);
if ($prevData != '')
{
$prevDate= time();
for ($i=0; $i < count($prevData)-1; $i++)
{
$oldSaveVal = $prevData[$i+1];
$duration = $prevDate - strtotime($oldSaveVal['change']);
$ValCurrent = $oldSaveVal['value'];
$ret2 .= '<histo id="'.$i.'">'."\n";
$ret2 .= ' <value>'.$oldSaveVal['value'].'</value>'."\n";
$ret2 .= ' <change>'.$oldSaveVal['change'].'</change>'."\n";
$ret2 .= ' <datesave>'.$oldSaveVal['datesave'].'</datesave>'."\n";
$ret2 .= ' <duration>'.$duration.'</duration>'."\n";
$ret2 .= '</histo>'."\n";
$prevDate = strtotime($oldSaveVal['change']);
$ValPrevious = $ValCurrent;
}
}
}
$ret .= $ret2;
$ret .= '</periph>'."\n";
}
return $ret;
}
function sdk_DatePart()
{
$now = time();
$ret = '<datepart>'."\n";
$ret .= ' <second>'.date('s', $now).'</second>'."\n";
$ret .= ' <minute>'.date('i', $now).'</minute>'."\n";
$ret .= ' <hour>'.date('H', $now).'</hour>'."\n";
$ret .= ' <day>'.date('d', $now).'</day>'."\n";
$ret .= ' <dayofweek>'.date('w', $now).'</dayofweek>'."\n";
$ret .= ' <dayofyear>'.date('z', $now).'</dayofyear>'."\n";
$ret .= ' <week>'.date('W', $now).'</week>'."\n";
$ret .= ' <month>'.date('m', $now).'</month>'."\n";
$ret .= ' <year>'.date('Y', $now).'</year>'."\n";
$ret .= ' <timenum>'.date('His', $now).'</timenum>'."\n";
$ret .= ' <time>'.date('H:i:s', $now).'</time>'."\n";
$ret .= ' <date>'.date('Y-m-d', $now).'</date>'."\n";
$ret .= ' <full>'.date('Y-m-d H:i:s', $now).'</full>'."\n";
$ret .= '</datepart>'."\n";
return $ret;
}
?>
Utilisateurs parcourant ce forum : Aucun utilisateur inscrit et 11 invité(s)