<?php

    $sensorid  = $_GET['sensor'];
    $property  = $_GET['property'];
    $aggregate = $_GET['aggregate'];
    $period    = $_GET['period'];
    $scope     = $_GET['scope'];
    $color     = $_GET['color'];
    $axis      = $_GET['axis'];
    $type      = $_GET['type'];
    $nolabel   = $_GET['nolabel'];
    $towatt    = $_GET['towatt'];
    $barwidth  = 1;

    switch ($scope)
    {
      case "1h":
        $ts = "time > now() - interval 1 hour";
        $barwidth = 60000;
        break;
      case "6h":
        $ts = "time > now() - interval 6 hour";
        $barwidth = 60000;
        break;
      case "12h":
        $ts = "time > now() - interval 12 hour";
        $barwidth = 60000;
        break;
      case "1d":
        $ts = "time > now() - interval 1 day";
        $barwidth = 1440000;
        break;
      case "1w":
        $ts = "time > now() - interval 7 day";
        $barwidth = 1440000;
        break;
      case "1mo":
        $ts = "time > now() - interval 31 day";
        $barwidth = 86400000;
        break;
      case "3mo":
        $ts = "time > now() - interval 93 day";
        $barwidth = 86400000;
        break;
      case "6mo":
        $ts = "time > now() - interval 186 day";
        $barwidth = 86400000;
        break;
      case "1y":
        $ts = "time > now() - interval 365 day";
        $barwidth = 86400000;
        break;
      case "10y":
        $ts = "time > now() - interval 3650 day";
        $barwidth = 1500000000;
        break;
      default:
        $ts = "time > now() - interval 1 day";
    }

    $db = mysql_connect("rpi2", "pi", "");
    mysql_set_charset("utf8");
    mysql_select_db("onewire",$db);

    if (empty($towatt))
    {
      $query = "SELECT UNIX_TIMESTAMP(time)*1000 \"time\", value from data where sensorid=$sensorid and property=$property and aggregate=$aggregate and period=$period and $ts order by time";
    }
    else
    {
      $query = "SELECT UNIX_TIMESTAMP(time)*1000 \"time\", value*60 \"value\" from data where sensorid=$sensorid and property=$property and aggregate=$aggregate and period=$period and $ts order by time";
    }

    $result = @mysql_query ($query);
    $arr = array();
    while($row = mysql_fetch_object($result))
    {
        $arr[] = array($row->time*1,$row->value*1);
    }

    $query2 = "SELECT a.name \"sensor\", b.name \"signal\", b.enhet \"enhet\" FROM sensors a, properties b WHERE a.sensorid=$sensorid and b.sensorid=$sensorid and b.propertyid=$property";
    $result2 = @mysql_query ($query2);
    while($row = mysql_fetch_object($result2))
    {
      $sensor = $row->sensor;
      $signal = $row->signal;
      $enhet = $row->enhet;
    }

    echo "{";
    if (empty($nolabel))
    {
      echo "\n\"label\": \"$sensor - $signal ($enhet)\",";
    }
    echo "\n\"data\": ";
    echo json_encode($arr);

    if (!empty($type))
    {
      switch($type) {
        case "points" : echo "\n,\"points\": { \"show\": \"true\" }"; break;
        case "triangle" : echo "\n,\"points\": { \"show\": \"true\", \"symbol\": \"triangle\" }"; break;
        case "bars" : echo "\n,\"bars\": { \"show\": \"true\", \"barWidth\": $barwidth, \"align\": \"center\" }"; break;
        default : break;
      }
    }

    if (is_numeric($color))
    {
      echo ",\n\"color\": $color";
    }

    if (!empty($axis))
    {
      echo "\n,\"yaxis\": $axis";
    }

    echo "\n}\n";
    
    mysql_close();
?>
