Example #1
0
 public function actionIndex()
 {
     $role = new Role();
     $position = $role->getPosition();
     if (Yii::$app->user->isGuest) {
         $this->doLogin();
     }
     // search model
     $searchModel = new StationSearch();
     $parseData['searchModel'] = $searchModel;
     // get latest warning
     if ($position != Role::POSITION_ADMINISTRATOR) {
         $stationIds = Station::getByRole($position, Yii::$app->user->id);
         $condition = ['in', 'station_id', $stationIds];
         $parseData['warnings'] = Warning::getWarning('warning_time DESC', 100, [$condition]);
     } else {
         $parseData['warnings'] = Warning::getWarning('warning_time DESC', 100, []);
     }
     // get stations
     if ($position != Role::POSITION_ADMINISTRATOR) {
         $sCondition = ['in', 'id', $stationIds];
     } else {
         $sCondition = [];
     }
     // data provider
     $data = $searchModel->search(Yii::$app->request->queryParams, $sCondition);
     $parseData['stationProvider'] = $data['provider'];
     // write station locator for map
     $this->_writeStationLocator($data['query']);
     return $this->render('index', $parseData);
 }
Example #2
0
 /**
  * get station id by station code
  * @param $code
  * @return null
  */
 public function getStationID($code)
 {
     $station = Station::findOne(['code' => $code]);
     if ($station) {
         return $station['id'];
     }
     return null;
 }
Example #3
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params, $condition)
 {
     $query = Station::find()->where($condition);
     $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => Yii::$app->params['page_size']]]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'center_id' => $this->center_id, 'area_id' => $this->area_id, 'type' => $this->type, 'user_id' => $this->user_id, 'status' => $this->status, 'picture_warning_numb' => $this->picture_warning_numb]);
     $query->andFilterWhere(['like', 'code', $this->code])->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'firmware', $this->firmware])->andFilterWhere(['like', 'staff', $this->staff])->andFilterWhere(['like', 'addition', $this->addition])->andFilterWhere(['like', 'picture_url', $this->picture_url])->andFilterWhere(['like', 'video_url', $this->video_url])->andFilterWhere(['like', 'latitude', $this->latitude])->andFilterWhere(['like', 'longtitude', $this->longtitude])->andFilterWhere(['like', 'phone', $this->phone])->andFilterWhere(['like', 'email', $this->email]);
     return ['provider' => $dataProvider, 'query' => $query];
 }
Example #4
0
 public function actionCreate()
 {
     $model = new PowerEquipment();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         //add new equipment to all exist stations
         $newID = Yii::$app->db->lastInsertID;
         $stations = Station::find()->all();
         if (!empty($stations)) {
             foreach ($stations as $station) {
                 var_dump($station['id']);
                 Yii::$app->db->createCommand()->insert('power_status', ['station_id' => $station['id'], 'item_id' => $newID])->execute();
             }
         }
         return $this->redirect(['index']);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Example #5
0
 public function actionIndex()
 {
     $stations = Station::find()->all();
     if (!empty($stations)) {
         foreach ($stations as $station) {
             $lastTime = 0;
             $lastStatus = StationStatus::find()->where(['station_id' => $station['id'], 'received' => StationStatus::STATUS_RECEIVED, 'status' => StationStatus::STATUS_OK])->orderBy('time_update DESC')->one();
             if ($lastStatus) {
                 $lastTime = $lastStatus['time_update'];
             }
             $currentTime = time();
             if ($lastTime > 0 && $lastTime >= $currentTime - self::LIMIT_DURATION) {
                 $status = Station::STATUS_CONNECTED;
             } else {
                 $status = Station::STATUS_LOST;
             }
             if ($status != $station['status']) {
                 Yii::$app->db->createCommand()->update('station', ['status' => $status, 'updated_at' => time()], ['id' => $station['id']])->execute();
             }
         }
     }
 }
Example #6
0
<?php

/**
 * Created by PhpStorm.
 * User: JFog
 * Date: 7/16/2015
 * Time: 12:33 AM
 */
use common\components\helpers\Show;
use common\components\helpers\Convert;
use common\models\Area;
use common\models\Center;
use common\models\Station;
$areaList = Area::find()->all();
$centerList = Center::find()->all();
$query = Station::find()->select('id, name')->where([]);
if (isset($_GET['area']) && $_GET['area'] > 0) {
    $query->andWhere(['area_id' => $_GET['area']]);
}
if (isset($_GET['center']) && $_GET['center'] > 0) {
    $query->andWhere(['center_id' => $_GET['center']]);
}
$collections = $query->all();
$stationList[] = ['id' => '0', 'name' => 'Chọn trạm'];
if (!empty($collections)) {
    foreach ($collections as $col) {
        $stationList[] = ['id' => $col['id'], 'name' => $col['name']];
    }
}
//print '<pre>'; print_r($stationList); die;
?>
Example #7
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getStations()
 {
     return $this->hasMany(Station::className(), ['area_id' => 'id']);
 }
Example #8
0
 public function actionIndex()
 {
     // get station ids by role
     $permCondition = [];
     $role = new Role();
     if (!$role->isAdministrator) {
         $stationIds = Station::getByRole($role->getPosition(), Yii::$app->user->id);
         $permCondition['s.id'] = $stationIds;
     }
     // default time points
     $timePoints = Convert::currentTimePoints();
     // get post form data
     $get = Yii::$app->request->get('get_by');
     if ($get == 'today') {
         $timePoints = Convert::currentTimePoints();
     } else {
         if ($get == 'week') {
             $timePoints = Convert::currentWeekTimePoints();
         } else {
             if ($get == 'month') {
                 $timePoints = Convert::currentMonthTimePoints();
             }
         }
     }
     $andWhere = ['>=', 'w.warning_time', $timePoints['start']];
     $andWhere1 = ['<=', 'w.warning_time', $timePoints['end']];
     // get data
     $query = new Query();
     $warnings = $query->select('w.warning_time, s.area_id')->from('warning w')->innerJoin('station s', 'w.station_id = s.id')->where($permCondition)->andWhere($andWhere)->andWhere($andWhere1)->all();
     // data
     $parseData = [];
     $data = [];
     // get areas
     $areaQuery = new Query();
     $areas = $areaQuery->select('a.*')->from('area a')->innerJoin('station s', 's.area_id = a.id')->where($permCondition)->groupBy('a.id')->all();
     $parseData['areas'] = $areas;
     if (!empty($areas)) {
         foreach ($areas as $area) {
             $data[$area['id']]['number'] = 0;
             $data[$area['id']]['start'] = 0;
             $data[$area['id']]['end'] = 0;
             if (!empty($warnings)) {
                 $no = 1;
                 foreach ($warnings as $w) {
                     if ($w['area_id'] == $area['id']) {
                         $data[$area['id']]['number']++;
                         if ($no == 1) {
                             $data[$area['id']]['start'] = $w['warning_time'];
                             $data[$area['id']]['end'] = $w['warning_time'];
                         } else {
                             if ($w['warning_time'] < $data[$area['id']]['start']) {
                                 $data[$area['id']]['start'] = $w['warning_time'];
                             }
                             if ($w['warning_time'] > $data[$area['id']]['end']) {
                                 $data[$area['id']]['end'] = $w['warning_time'];
                             }
                         }
                         $no++;
                     }
                 }
             }
         }
     }
     $parseData['data'] = $data;
     return $this->render('warning', $parseData);
 }
Example #9
0
echo Html::encode($this->title);
?>
</h4>
    <?php 
// echo $this->render('_search', ['model' => $searchModel]);
?>

    <?php 
echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [['class' => 'yii\\grid\\SerialColumn'], ['attribute' => 'user_id', 'value' => function ($model) {
    $user = User::findOne($model->user_id);
    return $user->username;
}], ['attribute' => 'action', 'filter' => LogModel::_prepareDataSelect($actionList, 'key', 'name', false), 'value' => function ($model) {
    $actionList = Log::actions();
    foreach ($actionList as $action) {
        if ($model->action === $action['key']) {
            return $action['name'];
        }
    }
}], ['attribute' => 'station_id', 'value' => function ($model) {
    $station = Station::findOne($model->station_id);
    return $station ? $station->name : '';
}], ['attribute' => 'related_id', 'value' => function ($model) {
    $equip = Equipment::findOne($model->related_id);
    return $equip ? $equip['name'] : '';
}], ['attribute' => 'time', 'filter' => false, 'value' => function ($model) {
    return $model->time > 0 ? date('d/m/Y h:i A', $model->time) : '';
}]]]);
?>

</div>
Example #10
0
 public function findStation($id)
 {
     $station = Station::findOne($id);
     if (!$station) {
         return false;
     }
     return $station;
 }
Example #11
0
 public function getConnectInfo($id)
 {
     $station = Station::findOne($id);
     if (!$station) {
         $this->error = 'Không tìm thấy thông tin trạm';
     }
     $this->info['id'] = $id;
     $this->info['code'] = $station['code'];
     $this->info['name'] = $station['name'];
     $this->info['host'] = $station['ip'];
     $this->info['port'] = $station['port'];
     $this->info['change_status'] = $station['change_equipment_status'];
     return $station;
 }
Example #12
0
 public function actionCronUnread()
 {
     $html = '';
     $count = 0;
     $post = Yii::$app->request->post();
     // permission
     $role = new Role();
     $condition = [];
     if (!$role->isAdministrator) {
         $position = $role->getPosition();
         $stationIds = Station::getByRole($position, Yii::$app->user->id);
         $condition[] = ['in', 'station_id', $stationIds];
     }
     $condition[] = ['=', 'read', Warning::STATUS_UNREAD];
     $warnings = Warning::getWarning('warning_time DESC', 0, $condition);
     if (!empty($warnings)) {
         $count = count($warnings);
         $html = Show::warningsTable($warnings);
     }
     $data['html'] = $html;
     $data['count'] = $count;
     print json_encode($data);
 }
Example #13
0
 public function getStationCode()
 {
     return $this->hasOne(Station::className(), ['code' => 'station_code']);
 }
Example #14
0
 public function sendMessage()
 {
     if (Yii::$app->request->isAjax) {
         $post = Yii::$app->request->post();
         if (isset($post['id']) && $post['id'] > 0) {
             $station = Station::findOne($post['id']);
             $ip = $station['ip'];
             $port = $station['port'];
             if ($ip != '' && $port != '') {
                 $client = new Client();
                 $init = $client->init($ip, $port);
                 if ($init) {
                     $send = $client->send($post['message']);
                     if ($send) {
                         print $client->returnMessage;
                     } else {
                         print $client->error;
                     }
                 } else {
                     print $client->error;
                 }
             }
         }
     }
     print 'failed';
 }
Example #15
0
 public function actionIndex()
 {
     // default time duration
     $timePoints = Convert::currentTimePoints();
     $getBy = Yii::$app->request->get('get_by');
     if ($getBy == 'week') {
         $timePoints = Convert::currentWeekTimePoints();
     } else {
         if ($getBy == 'month') {
             $timePoints = Convert::currentMonthTimePoints();
         }
     }
     $whereClause = ['sst.sensor_id' => Sensor::ID_SECURITY];
     // get station ids by role
     $role = new Role();
     if (!$role->isAdministrator) {
         $position = $role->getPosition();
         $stationIds = Station::getByRole($position, Yii::$app->user->id);
         $whereClause['s.id'] = $stationIds;
     }
     // get data
     $query = new Query();
     $stations = $query->select('s.area_id, s.status AS station_status, sst.value AS sensor_value, count(w.id) AS total_warning, max(w.warning_time) AS last_warning_time')->from('station s')->leftJoin('sensor_status sst', 'sst.station_id = s.id')->leftJoin('warning w', 'w.station_id = s.id')->where($whereClause)->groupBy('s.id')->all();
     // data
     $parseData = [];
     $data = [];
     // get areas
     $areas = Area::find()->all();
     $parseData['areas'] = $areas;
     if (!empty($areas)) {
         $notHave = [];
         foreach ($areas as $area) {
             $have = 0;
             $data[$area['id']]['has_warning'] = 0;
             $data[$area['id']]['no_warning'] = 0;
             $data[$area['id']]['security_on'] = 0;
             $data[$area['id']]['security_off'] = 0;
             $data[$area['id']]['connected'] = 0;
             $data[$area['id']]['lost_connect'] = 0;
             $data[$area['id']]['last_warning_time'] = 0;
             // security and connect stations
             if (!empty($stations)) {
                 foreach ($stations as $station) {
                     if ($station['area_id'] == $area['id']) {
                         $have = 1;
                         // warnings
                         if ($station['total_warning'] > 0 && $station['last_warning_time'] >= $timePoints['start'] && $station['last_warning_time'] <= $timePoints['end']) {
                             $data[$area['id']]['has_warning']++;
                             $data[$area['id']]['last_warning_time'] = $station['last_warning_time'] > $data[$area['id']]['last_warning_time'] ? $station['last_warning_time'] : $data[$area['id']]['last_warning_time'];
                         } else {
                             $data[$area['id']]['no_warning']++;
                         }
                         // status
                         if ($station['station_status'] == Station::STATUS_CONNECTED) {
                             $data[$area['id']]['connected']++;
                         } else {
                             if ($station['station_status'] == Station::STATUS_LOST) {
                                 $data[$area['id']]['lost_connect']++;
                             }
                         }
                         // security mode
                         if ($station['sensor_value'] == Sensor::SECURITY_ON) {
                             $data[$area['id']]['security_on']++;
                         } else {
                             if ($station['sensor_value'] == Sensor::SECURITY_OFF) {
                                 $data[$area['id']]['security_off']++;
                             }
                         }
                     }
                 }
             }
             if ($have == 0) {
                 $notHave[] = $area['id'];
             }
         }
         if (!empty($notHave)) {
             foreach ($notHave as $not) {
                 unset($data[$not]);
             }
         }
     }
     $parseData['data'] = $data;
     return $this->render('station', $parseData);
 }