How to extract profile data and plot with PHP

Avatar

sdg.matiaan
2017-04-24 16:20

<?php

// this shows a graph of your daily consumption. you need to fill in the username, password, meter serial and date range

$username = 'sdg.demo';

$password = 'demo';

$meter = '63901282';

$start = '2017-04-01';

$end = '2017-04-10';

$profileData = file_get_contents('https://sdg-adam.pnpscada.com:445/readMeterProfile?LOGIN=' . $username . '&PWD=' . $password . '&key1=' . $meter . '&startdate=' . $start . '&enddate=' . $end);

$parser = xml_parser_create();

$chars = '';

$tag = null;

$profile = array();

$date = null;

function startElements($parser, $name, $attrs) {

  global $tag, $chars;

  $tag = $name;

  $chars = '';

}

function characterData($parser, $data) {

  global $chars;

  $chars .= $data;

}

function endElements($parser, $name) {

  global $date, $chars, $profile;

  switch ($name)

  {

  case 'DATE': $date = substr($chars, 0, 10); break;

  case 'P1': @$profile[$date] += $chars * 1; break;

  }

}

xml_set_element_handler($parser, 'startElements', 'endElements');

xml_set_character_data_handler($parser, 'characterData');

xml_parse($parser, $profileData);

xml_parser_free($parser); // deletes the parser

?>

<script type='text/javascript'>

var data = [

<?php

$first = true;

foreach ($profile as $date => $kwh) {

  if ($first) $first = false; else echo ',';

  echo '["' . $date . '",' . $kwh . ']';

}

?>

];

</script>

<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'></script>

<script type='text/javascript' src='http://adam.pnpscada.com/lib/jquery.jqplot.min.js'></script>

<script type='text/javascript' src='http://adam.pnpscada.com/lib/plugins/jqplot.dateAxisRenderer.min.js'></script>

<link rel='stylesheet' type='text/css' href='http://adam.pnpscada.com/lib/jquery.jqplot.min.css' />

<div id='chart1'></div>

<script type='text/javascript'>

$(function() {

  plot1 = $.jqplot('chart1', [data], {

    axes:{

        xaxis:{

            renderer:$.jqplot.DateAxisRenderer

        }

    }

  });

});

</script>

Please log in to post a comment