Informers.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace app\models;
  3. use Yii;
  4. use yii\base\Model;
  5. /**
  6. * This is the model class for table "Informers".
  7. *
  8. */
  9. class Informers extends Model
  10. {
  11. public $curs_usd;
  12. public $curs_eur;
  13. public $curs_cny;
  14. public $curs_btc;
  15. public $temperature;
  16. public static $filecbr = '/home/admin/temp/cbr';
  17. public static $filebtc = '/home/admin/import/btc';
  18. public static $filetemp = '/home/admin/temp/temperature';
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function rules()
  23. {
  24. return [
  25. [['curs_usd', 'curs_eur', 'curs_cny', 'curs_btc', 'temperature'], 'float'],
  26. ];
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function attributeLabels()
  32. {
  33. return [
  34. 'curs_usd' => 'USD',
  35. 'curs_eur' => 'EUR',
  36. 'curs_cny' => 'CNY',
  37. 'curs_btc' => 'BTC',
  38. 'temperature' => 'temperature'
  39. ];
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public static function GetСurrency()
  45. {
  46. return \Yii::$app->cache->getOrSet("curce", function (){
  47. $request = @file_get_contents( self::$filecbr );
  48. if( $request ){
  49. $request = json_decode($request, true);
  50. if( isset($request['Valute']['USD']) && isset($request['Valute']['EUR']) && isset($request['Valute']['CNY']) ){
  51. $result['last_updated'] = date('Y-m-d H:i:s', strtotime($request['Date']));
  52. //USD
  53. $result['USD']['value'] = round($request['Valute']['USD']['Value'], 2);
  54. $result['USD']['dynamics'] = $request['Valute']['USD']['Value']>$request['Valute']['USD']['Previous']?'up':'down';
  55. //EUR
  56. $result['EUR']['value'] = round($request['Valute']['EUR']['Value'], 2);
  57. $result['EUR']['dynamics'] = $request['Valute']['EUR']['Value']>$request['Valute']['CNY']['Previous']?'up':'down';
  58. //CNY
  59. $result['CNY']['value'] = round($request['Valute']['CNY']['Value']/$request['Valute']['CNY']['Nominal'], 2);
  60. $result['CNY']['dynamics'] = $request['Valute']['CNY']['Value']>$request['Valute']['CNY']['Previous']?'up':'down';
  61. $request2 = @file_get_contents( self::$filebtc );
  62. $result['BTC']['value'] = 0;
  63. $result['BTC']['dynamics'] = '';
  64. if( $request2 ){
  65. $request2 = json_decode($request2, true);
  66. if( isset($request2['USD']['last']) ){
  67. $result['BTC']['value'] = round($request2['USD']['last'], 2);
  68. $result['BTC']['dynamics'] = $request2['USD']['15m']>$request2['USD']['last']?'up':'down';
  69. }
  70. }
  71. return $result;
  72. }
  73. }else{
  74. return false;
  75. }
  76. }, 7200 );
  77. }
  78. public static function GetTemperature()
  79. {
  80. return \Yii::$app->cache->getOrSet("temperature", function (){
  81. $request = @file_get_contents( self::$filetemp );
  82. if( $request !== false ){
  83. return $request;
  84. }
  85. return false;
  86. },
  87. 1);
  88. }
  89. }