123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- namespace app\models;
- use Yii;
- use yii\base\Model;
- /**
- * This is the model class for table "Informers".
- *
- */
- class Informers extends Model
- {
- public $curs_usd;
- public $curs_eur;
- public $curs_cny;
- public $curs_btc;
- public $temperature;
- public static $filecbr = '/home/admin/temp/cbr';
- public static $filebtc = '/home/admin/import/btc';
- public static $filetemp = '/home/admin/temp/temperature';
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['curs_usd', 'curs_eur', 'curs_cny', 'curs_btc', 'temperature'], 'float'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'curs_usd' => 'USD',
- 'curs_eur' => 'EUR',
- 'curs_cny' => 'CNY',
- 'curs_btc' => 'BTC',
- 'temperature' => 'temperature'
- ];
- }
- /**
- * {@inheritdoc}
- */
- public static function GetСurrency()
- {
- return \Yii::$app->cache->getOrSet("curce", function (){
- $request = @file_get_contents( self::$filecbr );
- if( $request ){
- $request = json_decode($request, true);
- if( isset($request['Valute']['USD']) && isset($request['Valute']['EUR']) && isset($request['Valute']['CNY']) ){
- $result['last_updated'] = date('Y-m-d H:i:s', strtotime($request['Date']));
- //USD
- $result['USD']['value'] = round($request['Valute']['USD']['Value'], 2);
- $result['USD']['dynamics'] = $request['Valute']['USD']['Value']>$request['Valute']['USD']['Previous']?'up':'down';
- //EUR
- $result['EUR']['value'] = round($request['Valute']['EUR']['Value'], 2);
- $result['EUR']['dynamics'] = $request['Valute']['EUR']['Value']>$request['Valute']['CNY']['Previous']?'up':'down';
- //CNY
- $result['CNY']['value'] = round($request['Valute']['CNY']['Value']/$request['Valute']['CNY']['Nominal'], 2);
- $result['CNY']['dynamics'] = $request['Valute']['CNY']['Value']>$request['Valute']['CNY']['Previous']?'up':'down';
- $request2 = @file_get_contents( self::$filebtc );
- $result['BTC']['value'] = 0;
- $result['BTC']['dynamics'] = '';
- if( $request2 ){
- $request2 = json_decode($request2, true);
- if( isset($request2['USD']['last']) ){
- $result['BTC']['value'] = round($request2['USD']['last'], 2);
- $result['BTC']['dynamics'] = $request2['USD']['15m']>$request2['USD']['last']?'up':'down';
- }
- }
- return $result;
- }
- }else{
- return false;
- }
- }, 7200 );
- }
- public static function GetTemperature()
- {
- return \Yii::$app->cache->getOrSet("temperature", function (){
- $request = @file_get_contents( self::$filetemp );
- if( $request !== false ){
- return $request;
- }
- return false;
- },
- 1);
- }
- }
|