Web Interfaces of PROSUME

WalletEprosumeModel.php 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace App\Utility;
  3. use Cake\Datasource\ConnectionManager;
  4. class WalletEprosumeModel
  5. {
  6. public $walletExecutable;
  7. public $walletConfiguration;
  8. public $walletAccount;
  9. public $walletAccountFolder;
  10. public $walletCommandUpdatePrices = "admin updatePrices";
  11. public $walletCommandBalance = "wallet balance";
  12. public $walletCommandHistory = "wallet history";
  13. public $walletCommandTransfer = "wallet transfer";
  14. protected $connection = null;
  15. public function __construct()
  16. {
  17. $this->connection = ConnectionManager::get('default');
  18. $ini = parse_ini_file(dirname(__FILE__).'/../../client.ini', true);
  19. $this->walletExecutable = $ini['wallet']['executable'] ?? null;
  20. $this->walletConfiguration = $ini['wallet']['configuration'] ?? null;
  21. $this->walletAccount = $ini['wallet']['account'] ?? null;
  22. $this->walletAccountFolder = dirname($ini['wallet']['account']) ?? null;
  23. }
  24. public function balance($meter)
  25. {
  26. $walletExec = "$this->walletExecutable $this->walletCommandBalance $meter --config $this->walletConfiguration " .
  27. "--accounts-file $this->walletAccountFolder$meter.txt 2>/dev/null";
  28. exec($walletExec, $arrayOutput);
  29. /*******
  30. * Asset: PRS
  31. * Balance: 263338
  32. *
  33. * ----------
  34. * Asset: TFT
  35. * Balance: 72864
  36. */
  37. $balance = [];
  38. $asset = null;
  39. foreach ($arrayOutput as $line) {
  40. $parameter = explode(': ', $line);
  41. if (isset($parameter[1])) {
  42. $key = $parameter[0];
  43. $value = $parameter[1];
  44. switch ($key) {
  45. case 'Asset':
  46. $asset = $value;
  47. break;
  48. case 'Balance':
  49. if ($asset) $balance[$asset] = $value;
  50. break;
  51. default:
  52. $asset = null;
  53. break;
  54. }
  55. }
  56. }
  57. return $balance;
  58. }
  59. public function history()
  60. {
  61. $walletExec = "$this->walletExecutable $this->walletCommandHistory --config $this->walletConfiguration " .
  62. "--accounts-file $this->walletAccount 2>/dev/null";
  63. exec($walletExec, $arrayOutput);
  64. $transaction = [];
  65. $history = [];
  66. foreach ($arrayOutput as $line) {
  67. $parameter = explode(': ', $line);
  68. if (isset($parameter[1])) {
  69. $key = $parameter[0];
  70. $value = $parameter[1];
  71. switch ($key) {
  72. case 'Block Number':
  73. $transaction['Blocknumber'] = $value;
  74. break;
  75. case 'Memo':
  76. $readings = json_decode($value, $assoc = TRUE);
  77. if (is_array($readings)) {
  78. foreach ($readings as $readingKey => $readingValue) {
  79. if (is_array($readingValue)) {
  80. if (!empty($readingValue['value'])) {
  81. $transaction[$key][$readingKey] = $readingValue['value'];
  82. }
  83. } else {
  84. $transaction[$key][$readingKey] = $readingValue;
  85. }
  86. }
  87. }
  88. break;
  89. default:
  90. $transaction[$key] = $value;
  91. break;
  92. }
  93. } else {
  94. $key = preg_replace('/\s/', '', $parameter[0]);
  95. switch ($key) {
  96. case '--------HistoryEnd--------':
  97. case '--------------------':
  98. array_push($history, $transaction);
  99. $transaction = [];
  100. break;
  101. }
  102. }
  103. }
  104. return $history;
  105. }
  106. public function updateHistoryDB($pdo = null)
  107. {
  108. foreach ($history = $this->history() as $block) {
  109. $stmt = $pdo->prepare('select id from blocks where timestamp=:Timestamp and blocknumber=:Blocknumber');
  110. $values = ['Blocknumber' => $block['Blocknumber'], 'Timestamp' => $this->convertUTCToLocalTime($block['Timestamp'])];
  111. $stmt->execute($values);
  112. if (empty($stmt->fetch())) {
  113. $stmt = $pdo->prepare('insert into blocks (id, blocknumber,wallet_type,wallet_from,wallet_to,asset,amount,timestamp) ' .
  114. ' values (null, :Blocknumber,:Type,:From,:To,:Asset,:Amount,:Timestamp ) ');
  115. $values = ['Blocknumber' => '', 'Type' => '', 'From' => '', 'To' => '', 'Asset' => '', 'Amount' => '', 'Timestamp' => ''];
  116. foreach (array_keys($values) as $memo_key) {
  117. if (isset($block[$memo_key])) {
  118. $values[$memo_key] = $block[$memo_key];
  119. }
  120. }
  121. $stmt->execute($values);
  122. $block_id = $pdo->lastInsertId();
  123. if (($block_id) && (!empty($block['Memo']))) {
  124. foreach ($block['Memo'] as $memo_key => $memo_value) {
  125. switch ($memo_key) {
  126. case 'timestamp_sospendi':
  127. break;
  128. default:
  129. $sql = 'insert into memo (block_id,memo_key,memo_value) values (:block_id,:memo_key,:memo_value)';
  130. $stmt = $pdo->prepare($sql);
  131. $stmt->execute(compact('block_id', 'memo_key', 'memo_value'));
  132. break;
  133. }
  134. }
  135. }
  136. print_r($block);
  137. }
  138. }
  139. }
  140. public function getProsumers($only=false, $except=false)
  141. {
  142. return array_filter(
  143. array_map(
  144. function($v) use($only, $except) {
  145. return $only && $v["id"] == $only
  146. ? null
  147. : $except
  148. ? $v["id"] != $except
  149. ? $v["name"]
  150. : null
  151. : $v["name"]
  152. ;
  153. },
  154. ($this->connection->execute('select id, subid, name from meter where `status` = "ACTIVE"'))->fetchAll(\PDO::FETCH_ASSOC)
  155. )
  156. );
  157. }
  158. public function setPrice($user, $buyPrice, $sellPrice)
  159. {
  160. $walletExec = "$this->walletExecutable $this->walletCommandUpdatePrices --accounts-file $this->walletAccount -b $buyPrice -s $sellPrice -u $user --config $this->walletConfiguration 2>/dev/null";
  161. exec($walletExec, $arrayOutput);
  162. return !empty($arrayOutput);
  163. }
  164. public function transfer($from, $to, $amount, $asset, $memo)
  165. {
  166. if(in_array(null, func_get_args(), true)) {
  167. return false;
  168. } else {
  169. $accountFile = $this->walletAccountFolder."/".$from.".txt";
  170. if(!file_exists($accountFile)) {
  171. return false;
  172. }
  173. $memo = str_replace("'", "\'", html_entity_decode($memo, ENT_QUOTES));
  174. $walletExec = "$this->walletExecutable $this->walletCommandTransfer --accounts-file $accountFile --to $to --asset $asset --amount $amount --memo '$memo' --config $this->walletConfiguration";
  175. $output = system($walletExec);
  176. return !empty($a);
  177. }
  178. }
  179. private function convertUTCToLocalTime($datestring) {
  180. return (new \DateTime($datestring, new \DateTimeZone("UTC")))->setTimeZone(new \DateTimeZone("Europe/Rome"))->format("Y-m-d H:i:s");
  181. }
  182. }