%PDF- %PDF-
Direktori : /var/www/spotreba/ |
Current File : //var/www/spotreba/api.php |
<?php include "db.php"; $out = array(); $waterPrice = 70.94; //$gasPrice = 12.82; $gasPrice = 23.00; $electricityHighPrice = 6.57; $electricityLowPrice = 6.57; if($_GET['action'] == 'water') { $res = dibi::query("select [timestamp], [amount] from [water] order by [timestamp] asc"); $prev = null; foreach($res as $r) { if($prev == null) { $prev = clone $r; $prev->timestamp = (int)$r->timestamp->format("U") - 1; // To solve daily amount problem } $o = new stdClass(); $o->date = $r->timestamp->format("d. m. Y H:i:s"); $o->timestamp = (int)$r->timestamp->format("U"); $o->amount = $r->amount; $o->diff = $o->amount - $prev->amount; $o->dailyAmount = ($o->diff / (double)($o->timestamp - $prev->timestamp)) * 3600 * 24; $o->price = $o->diff * $waterPrice; $o->dailyPrice = $o->dailyAmount * $waterPrice; $out[] = $o; $prev = $r; $prev->timestamp = (int)$r->timestamp->format("U"); } } elseif($_GET['action'] == 'gas') { $res = dibi::query("select [timestamp], [amount] from [gas] order by [timestamp] asc"); $prev = null; foreach($res as $r) { if($prev == null) { $prev = clone $r; $prev->timestamp = (int)$r->timestamp->format("U") - 1; // To solve daily amount problem } $o = new stdClass(); $o->date = $r->timestamp->format("d. m. Y H:i:s"); $o->timestamp = (int)$r->timestamp->format("U"); $o->amount = $r->amount; $o->diff = $o->amount - $prev->amount; $o->dailyAmount = ($o->diff / ($o->timestamp - $prev->timestamp)) * 3600 * 24; $o->price = $o->diff * $gasPrice; $o->dailyPrice = $o->dailyAmount * $gasPrice; $out[] = $o; $prev = $r; $prev->timestamp = (int)$r->timestamp->format("U"); } } elseif($_GET['action'] == 'electricity') { $res = dibi::query("select [timestamp], [amountlow], [amounthigh] from [electricity] order by [timestamp] asc"); $prev = null; foreach($res as $r) { if($prev == null) { $prev = clone $r; $prev->timestamp = (int)$r->timestamp->format("U") - 1; } $o = new stdClass(); $o->date = $r->timestamp->format("d. m. Y H:i:s"); $o->timestamp = (int)$r->timestamp->format("U"); $o->amountLow = $r->amountlow; $o->diffLow = $o->amountLow - $prev->amountlow; $o->dailyAmountLow = ($o->diffLow / ($o->timestamp - $prev->timestamp)) * 3600 * 24; $o->priceLow = $o->diffLow * $electricityLowPrice; $o->dailyPriceLow = $o->dailyAmountLow * $electricityLowPrice; $o->amountHigh = $r->amounthigh; $o->diffHigh = $o->amountHigh - $prev->amounthigh; $o->dailyAmountHigh = ($o->diffHigh / ($o->timestamp - $prev->timestamp)) * 3600 * 24; $o->priceHigh = $o->diffHigh * $electricityHighPrice; $o->dailyPriceHigh = $o->dailyAmountHigh * $electricityHighPrice; $out[] = $o; $prev = $r; $prev->timestamp = (int)$r->timestamp->format("U"); } } elseif($_GET['action'] == 'furnace') { $res = dibi::query("select [timestamp], [amount] from [furnace] order by [timestamp] asc"); $prev = null; foreach($res as $r) { if($prev == null) { $prev = clone $r; $prev->timestamp = (int)$r->timestamp->format("U") - 1; // To solve daily amount problem } $o = new stdClass(); $o->date = $r->timestamp->format("d. m. Y H:i:s"); $o->timestamp = (int)$r->timestamp->format("U"); $o->amount = $r->amount; $o->diff = $o->amount - $prev->amount; $o->dailyAmount = ($o->diff / ($o->timestamp - $prev->timestamp)) * 3600 * 24; $o->price = $o->diff * ($electricityHighPrice + $electricityLowPrice)/2; $o->dailyPrice = $o->dailyAmount * $electricityHighPrice; $out[] = $o; $prev = $r; $prev->timestamp = (int)$r->timestamp->format("U"); } } elseif($_GET['action'] == 'pc') { $res = dibi::query("select [timestamp], [amount] from [pc] order by [timestamp] asc"); $prev = null; foreach($res as $r) { if($prev == null) { $prev = clone $r; $prev->timestamp = (int)$r->timestamp->format("U") - 1; // To solve daily amount problem } $o = new stdClass(); $o->date = $r->timestamp->format("d. m. Y H:i:s"); $o->timestamp = (int)$r->timestamp->format("U"); $o->amount = $r->amount; $o->diff = $o->amount - $prev->amount; $o->dailyAmount = ($o->diff / ($o->timestamp - $prev->timestamp)) * 3600 * 24; $o->price = $o->diff * ($electricityHighPrice + $electricityLowPrice)/2; $o->dailyPrice = $o->dailyAmount * ($electricityHighPrice + $electricityLowPrice)/2; $out[] = $o; $prev = $r; $prev->timestamp = (int)$r->timestamp->format("U"); } } echo json_encode($out, JSON_PRETTY_PRINT);