123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- namespace App\Utility;
-
- use Cake\Datasource\ConnectionManager;
-
- class WalletEprosumeModel
- {
- public $walletExecutable;
- public $walletConfiguration;
- public $walletAccount;
- public $walletAccountFolder;
- public $walletCommandUpdatePrices = "admin updatePrices";
- public $walletCommandBalance = "wallet balance";
- public $walletCommandHistory = "wallet history";
- public $walletCommandTransfer = "wallet transfer";
-
- protected $connection = null;
-
- public function __construct()
- {
- $this->connection = ConnectionManager::get('default');
- $ini = parse_ini_file(dirname(__FILE__).'/../../client.ini', true);
- $this->walletExecutable = $ini['wallet']['executable'] ?? null;
- $this->walletConfiguration = $ini['wallet']['configuration'] ?? null;
- $this->walletAccount = $ini['wallet']['account'] ?? null;
- $this->walletAccountFolder = dirname($ini['wallet']['account']) ?? null;
- }
-
- public function balance($meter)
- {
- $walletExec = "$this->walletExecutable $this->walletCommandBalance $meter --config $this->walletConfiguration " .
- "--accounts-file $this->walletAccountFolder$meter.txt 2>/dev/null";
-
- exec($walletExec, $arrayOutput);
-
- /*******
- * Asset: PRS
- * Balance: 263338
- *
- * ----------
- * Asset: TFT
- * Balance: 72864
- */
- $balance = [];
- $asset = null;
- foreach ($arrayOutput as $line) {
- $parameter = explode(': ', $line);
- if (isset($parameter[1])) {
- $key = $parameter[0];
- $value = $parameter[1];
- switch ($key) {
- case 'Asset':
- $asset = $value;
- break;
- case 'Balance':
- if ($asset) $balance[$asset] = $value;
- break;
- default:
- $asset = null;
- break;
- }
- }
- }
- return $balance;
- }
-
- public function history()
- {
- $walletExec = "$this->walletExecutable $this->walletCommandHistory --config $this->walletConfiguration " .
- "--accounts-file $this->walletAccount 2>/dev/null";
-
- exec($walletExec, $arrayOutput);
-
- $transaction = [];
- $history = [];
- foreach ($arrayOutput as $line) {
- $parameter = explode(': ', $line);
- if (isset($parameter[1])) {
- $key = $parameter[0];
- $value = $parameter[1];
- switch ($key) {
- case 'Block Number':
- $transaction['Blocknumber'] = $value;
- break;
-
- case 'Memo':
- $readings = json_decode($value, $assoc = TRUE);
- if (is_array($readings)) {
- foreach ($readings as $readingKey => $readingValue) {
- if (is_array($readingValue)) {
- if (!empty($readingValue['value'])) {
- $transaction[$key][$readingKey] = $readingValue['value'];
- }
- } else {
- $transaction[$key][$readingKey] = $readingValue;
- }
- }
- }
- break;
-
- default:
- $transaction[$key] = $value;
- break;
- }
- } else {
- $key = preg_replace('/\s/', '', $parameter[0]);
- switch ($key) {
- case '--------HistoryEnd--------':
- case '--------------------':
- array_push($history, $transaction);
- $transaction = [];
- break;
- }
- }
- }
- return $history;
- }
-
- public function updateHistoryDB($pdo = null)
- {
-
- foreach ($history = $this->history() as $block) {
- $stmt = $pdo->prepare('select id from blocks where timestamp=:Timestamp and blocknumber=:Blocknumber');
- $values = ['Blocknumber' => $block['Blocknumber'], 'Timestamp' => $this->convertUTCToLocalTime($block['Timestamp'])];
- $stmt->execute($values);
- if (empty($stmt->fetch())) {
- $stmt = $pdo->prepare('insert into blocks (id, blocknumber,wallet_type,wallet_from,wallet_to,asset,amount,timestamp) ' .
- ' values (null, :Blocknumber,:Type,:From,:To,:Asset,:Amount,:Timestamp ) ');
- $values = ['Blocknumber' => '', 'Type' => '', 'From' => '', 'To' => '', 'Asset' => '', 'Amount' => '', 'Timestamp' => ''];
- foreach (array_keys($values) as $memo_key) {
- if (isset($block[$memo_key])) {
- $values[$memo_key] = $block[$memo_key];
- }
- }
-
- $stmt->execute($values);
- $block_id = $pdo->lastInsertId();
- if (($block_id) && (!empty($block['Memo']))) {
- foreach ($block['Memo'] as $memo_key => $memo_value) {
- switch ($memo_key) {
- case 'timestamp_sospendi':
- break;
- default:
- $sql = 'insert into memo (block_id,memo_key,memo_value) values (:block_id,:memo_key,:memo_value)';
- $stmt = $pdo->prepare($sql);
- $stmt->execute(compact('block_id', 'memo_key', 'memo_value'));
- break;
- }
- }
- }
-
- print_r($block);
- }
- }
- }
-
- public function getProsumers($only=false, $except=false)
- {
- return array_filter(
- array_map(
- function($v) use($only, $except) {
- return $only && $v["id"] == $only
- ? null
- : $except
- ? $v["id"] != $except
- ? $v["name"]
- : null
- : $v["name"]
- ;
- },
- ($this->connection->execute('select id, subid, name from meter where `status` = "ACTIVE"'))->fetchAll(\PDO::FETCH_ASSOC)
- )
- );
- }
-
- public function setPrice($user, $buyPrice, $sellPrice)
- {
- $walletExec = "$this->walletExecutable $this->walletCommandUpdatePrices --accounts-file $this->walletAccount -b $buyPrice -s $sellPrice -u $user --config $this->walletConfiguration 2>/dev/null";
- exec($walletExec, $arrayOutput);
- return !empty($arrayOutput);
- }
-
- public function transfer($from, $to, $amount, $asset, $memo)
- {
- if(in_array(null, func_get_args(), true)) {
- return false;
- } else {
- $accountFile = $this->walletAccountFolder."/".$from.".txt";
- if(!file_exists($accountFile)) {
- return false;
- }
- $memo = str_replace("'", "\'", html_entity_decode($memo, ENT_QUOTES));
- $walletExec = "$this->walletExecutable $this->walletCommandTransfer --accounts-file $accountFile --to $to --asset $asset --amount $amount --memo '$memo' --config $this->walletConfiguration";
- $output = system($walletExec);
- return !empty($a);
- }
- }
-
- private function convertUTCToLocalTime($datestring) {
- return (new \DateTime($datestring, new \DateTimeZone("UTC")))->setTimeZone(new \DateTimeZone("Europe/Rome"))->format("Y-m-d H:i:s");
- }
-
- }
|