123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- <?php
- namespace App\Controller;
-
- use App\Utility\EprosumeModel;
- use App\Utility\WalletEprosumeModel;
-
- use Cake\Core\Configure;
- use Cake\Http\Exception\ForbiddenException;
- use Cake\Http\Exception\NotFoundException;
- use Cake\View\Exception\MissingTemplateException;
-
- abstract class SSPController extends AppController
- {
- protected $EprosumeModel = null;
- protected $WalletEprosumeModel = null;
-
- public function initialize()
- {
- parent::initialize();
- $this->EprosumeModel = new EprosumeModel();
- $this->WalletEprosumeModel = new WalletEprosumeModel();
- }
-
- protected function doSetPrice($prosumer, $buyPrice, $sellPrice)
- {
- $this->autoRender = false;
- if(
- !in_array($prosumer, $this->WalletEprosumeModel->getProsumers(1))
- ||
- !is_numeric($buyPrice)
- ||
- !is_numeric($sellPrice)
- ) {
- return $this->response->withStatus(400);
- }
-
- $results = $this->WalletEprosumeModel->setPrice($prosumer, $buyPrice, $sellPrice);
-
- if(empty($results)) {
- return $this->response->withStatus(502);
- } else {
- $this->EprosumeModel->setPrice($prosumer, $buyPrice, $sellPrice);
- return $this->response = $this->response
- ->withStatus(200)
- ->withType('application/json')
- ;
- }
- }
-
- protected function doTransfer($from, $prosumer, $amount, $asset, $memo)
- {
- $this->autoRender = false;
- if(
- !in_array($from, $this->WalletEprosumeModel->getProsumers(1))
- ||
- !in_array($prosumer, $this->WalletEprosumeModel->getProsumers(1))
- ||
- $from == $prosumer
- ||
- !is_numeric($amount)
- ||
- !in_array($asset, $this->EprosumeModel->getAssetsType())
- ||
- !is_string($memo)
- ) {
- return $this->response->withStatus(400);
- }
-
- $results = $this->WalletEprosumeModel->transfer($from, $prosumer, $amount, $asset, $memo);
- if(empty($results)) {
- return $this->response->withStatus(502);
- } else {
- return $this->response
- ->withStatus(200)
- ->withType('application/json')
- ;
- }
- }
-
- public function blocks() {
- $qs = $this->request->getAttribute('params')["?"] ?? [];
- $asset = $qs["asset"] ?? null;
- $date_from = $qs["from"] ?? (new \DateTime())->sub(new \DateInterval("P1D"))->format("Y-m-d H:i:s");
- $date_to = $qs["to"] ?? (new \DateTime())->format("Y-m-d H:i:s");
- $meter = $qs["meter"] ?? null;
- $filters = [
- 'date_from' => $date_from,
- 'date_to' => $date_to,
- 'meter' => $meter,
- 'asset' => $asset,
- ];
- $asset && $filters['asset'] = $asset;
- $meter && $filters['meter'] = $meter;
-
- $blocks = $this->EprosumeModel->blocksList(
- $filters
- );
- return $blocks
- ? $this->response
- ->withStatus(200)
- ->withType('application/json')
- ->withStringBody(
- $this->jsonEncodeGenerator($blocks)
- )
- : $this->response
- ->withStatus(200)
- ->withType('application/json')
- ->withStringBody(
- $this->jsonEncodeGenerator([])
- )
- ;
- }
-
- public function balances() {
- $qs = $this->request->getAttribute('params')["?"] ?? [];
- $date_from = $qs["from"] ?? (new \DateTime())->sub(new \DateInterval("P1D"))->format("Y-m-d H:i:s");
- $date_to = $qs["to"] ?? (new \DateTime())->format("Y-m-d H:i:s");
- $meter = $qs["meter"] ?? null;
-
- $balances = $this->EprosumeModel->balanceHistory($date_from, $date_to, $meter);
- return $balances
- ? $this->response
- ->withStatus(200)
- ->withType('application/json')
- ->withStringBody(
- $this->jsonEncodeGenerator($balances)
- )
- : $this->response
- ->withStatus(200)
- ->withType('application/json')
- ->withStringBody(
- $this->jsonEncodeGenerator([])
- )
- ;
- }
-
- public function charts($type, $meter_name) {
- $qs = $this->request->getAttribute('params')["?"] ?? $queryString;
- $period = $qs['period'] ?? 0;
- $from = $qs['from'] ?? null;
- $to = $qs['to'] ?? null;
- $data = $this->EprosumeModel->charts($type, $meter_name, $from, $to, $period);
- return $data
- ? $this->response
- ->withStatus(200)
- ->withType('application/json')
- ->withStringBody(
- $this->jsonEncodeGenerator($data)
- )
- : $this->response
- ->withStatus(200)
- ->withType('application/json')
- ->withStringBody(
- $this->jsonEncodeGenerator([])
- )
- ;
- }
-
- protected function SSPResponse($dataset, $user_meter_id=null, array $queryString = [])
- {
- $this->autoRender = false;
- $qs = $this->request->getAttribute('params')["?"] ?? $queryString;
- $format = $qs["format"] ?? "json";
- if(
- $format == "csv"
- ) {
- unset($qs["start"], $qs["length"]);
- }
-
- $qs["user_meter_id"] = $user_meter_id;
- $results = $this->EprosumeModel->dataTableSSP(
- $dataset,
- $qs["user_meter_id"],
- ($qs["draw"] ?? 1),
- ($qs["columns"] ?? [["data"=>"timestamp"]]),
- ($qs["order"] ?? [["column"=>0,"dir"=>"desc"]]),
- ($qs["start"] ?? null),
- ($qs["length"] ?? null),
- ($qs["search"] ?? null),
- ($qs["meter"] ?? null),
- ($qs["txid"] ?? null),
- ($qs["from"] ?? null),
- ($qs["to"] ?? null),
- ($qs["period"] ?? null)
- );
- if($format == "json") {
- $dataString = $this->jsonEncodeGenerator($results["data"]);
- if( $this->request->is('ajax') ) {
- $draw = $results["draw"];
- $recordsFiltered = $results["recordsFiltered"];
- $recordsTotal = $results["recordsTotal"];
- $body = $this->setDataTableResponse($dataString, $recordsTotal, $recordsFiltered, $draw);
- } else {
- $body = $dataString;
- }
- $this->response = $this->response
- ->withStatus(200)
- ->withType('application/json')
- ->withStringBody($body);
- return $this->response;
- } else if($format == "csv") {
- $csvFileHandle = $this->csvTmpFileGenerator($results["data"], $qs["columns"]);
- $csvFilePath = stream_get_meta_data($csvFileHandle)['uri'];
- $csvContent = file_get_contents($csvFilePath);
- return $this->response
- ->withType("text/csv")
- ->withDownload("eProsume_export_".$dataset."_".date("YmdHis").".csv")
- ->withLength(strlen($csvContent))
- ->withStringBody($csvContent);
- }
- return null;
- }
-
- protected function getRequestInput() {
- return array_map(
- function($v) {
- return filter_var($v, FILTER_SANITIZE_STRING);
- },
- json_decode($this->request->input(), true) ?? []
- );
- }
-
- protected function jsonEncodeGenerator(iterable $generator)
- {
- $memory_limit = 5 * 1024 * 1024;
- $fp = fopen("php://temp", 'r+');
- fputs($fp, '[');
- foreach ($generator as $key => $value) {
- if(is_null($value)) {
- continue;
- }
- if ($key != 0) {
- fputs($fp, ',');
- }
- fputs($fp, json_encode($value));
- }
- fputs($fp, ']');
- rewind($fp);
- return stream_get_contents($fp);
- }
-
- protected function csvTmpFileGenerator(iterable $generator, array $columns)
- {
- $memory_limit = 5 * 1024 * 1024;
- $fp = tmpfile();
- foreach ($columns as $column) {
- fputs($fp, (empty($column["name"]) ? $column["data"] : $column["name"]) );
- fputs($fp, ";");
- }
- fputs($fp, PHP_EOL);
- foreach ($generator as $key => $row) {
- if(is_null($row)) {
- continue;
- }
- if ($key != 0) {
- fputs($fp, PHP_EOL);
- }
- foreach($row as $k=>$value) {
- if($k=="id") {
- continue;
- }
- fputs($fp, $value);
- fputs($fp, ";");
- }
- }
- return $fp;
- }
-
- protected function setDataTableResponse($dataString, $recordsTotal, $recordsFiltered, $draw)
- {
- $memory_limit = 5 * 1024 * 1024;
- $fp = fopen("php://temp", 'r+');
- fputs($fp, '{');
- fputs($fp, '"draw": '.$draw.',');
- fputs($fp, '"recordsTotal": '.$recordsTotal.',');
- fputs($fp, '"recordsFiltered": '.$recordsFiltered.',');
- fputs($fp, '"data": '.$dataString);
- fputs($fp, '}');
- rewind($fp);
- return stream_get_contents($fp);
- }
-
- }
|