public function updateStatus($stationId, $decimal) { $binary = Convert::dec2Bin($decimal, 10); // get equipments $query = new Query(); $query->select('b1.*, b2.sort')->from('equipment_status b1')->leftJoin('equipment b2', 'b1.equipment_id = b2.id')->where(['station_id' => $stationId]); $equipments = $query->createCommand()->queryAll(); if (!empty($equipments)) { foreach ($equipments as $e) { Yii::$app->db->createCommand()->update('equipment_status', ['status' => $binary[$e['sort']]], ['station_id' => $stationId, 'equipment_id' => $e['equipment_id']])->execute(); } } }
public static function multipleDelete($params) { //build conditions $conditions = []; if (isset($params['from_date']) && trim($params['from_date']) !== '') { $fromTime = Convert::date2Time($params['from_date'], 'd/m/Y'); $conditions[] = "w.warning_time >= " . $fromTime; } if (isset($params['to_date']) && trim($params['to_date']) !== '') { $toTime = Convert::date2Time($params['to_date'], 'd/m/Y'); $conditions[] = "w.warning_time <= " . $toTime; } if (isset($params['station']) && !empty($params['station'])) { $conditions[] = "s.id IN(" . implode(',', $params['station']) . ")"; } if (isset($params['area_id']) && $params['area_id'] > 0) { $conditions[] = "s.area_id = " . $params['area_id']; } if (isset($params['center_id']) && $params['center_id'] > 0) { $conditions[] = "s.center_id = " . $params['center_id']; } //delete real pictures $sql = "SELECT p.picture\n FROM warning_picture p\n INNER JOIN warning w ON(w.id = p.warning_id)\n INNER JOIN station s ON(s.id = w.station_id)\n WHERE 1"; if (!empty($conditions)) { $sql .= " AND " . implode(' AND ', $conditions); } $pictures = Yii::$app->db->createCommand($sql)->queryAll(); if (!empty($pictures)) { foreach ($pictures as $pic) { $path = $_SERVER['DOCUMENT_ROOT'] . '/frontend/web/uploads/' . $pic['picture']; if (file_exists($path)) { unlink($path); } } } //delete warning picture in db $sql = "DELETE p\n FROM warning_picture p\n INNER JOIN warning w ON(w.id = p.warning_id)\n INNER JOIN station s ON(s.id = w.station_id)\n WHERE 1"; if (!empty($conditions)) { $sql .= ' AND ' . implode(' AND ', $conditions); } Yii::$app->db->createCommand($sql)->execute(); //delete warning in db $sql = "DELETE w\n FROM warning w\n INNER JOIN station s ON(s.id = w.station_id)\n WHERE 1"; if (!empty($conditions)) { $sql .= ' AND ' . implode(' AND ', $conditions); } Yii::$app->db->createCommand($sql)->execute(); }
foreach ($collections as $col) { $stationList[] = ['id' => $col['id'], 'name' => $col['name']]; } } //print '<pre>'; print_r($stationList); die; ?> <!-- Filter --> <form role="form" style="display: block; margin-bottom: 10px;" action="" method="get"> <div class="filter-item" style="margin-top: 30px;"> <?php echo Show::datePicker('from_date', isset($_GET['from_date']) ? Convert::date2date($_GET['from_date'], 'd/m/Y', 'm/d/Y') : '', 'Start date'); ?> </div> <div class="filter-item"> <?php echo Show::datePicker('to_date', isset($_GET['to_date']) ? Convert::date2date($_GET['to_date'], 'd/m/Y', 'm/d/Y') : '', 'End date'); ?> </div> <div class="filter-item"> <select onchange="filter();" name="area" id="filter-area"> <option value="0">- Chọn tỉnh thành -</option> <?php if (isset($areaList)) { foreach ($areaList as $area) { $selected = ''; if (isset($_GET['area']) && $_GET['area'] == $area['id']) { $selected = 'selected="selected"'; } ?> <option <?php echo $selected;
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); }
/** * update sensor status with input status * - update temperature * - update humidity * - update security mode * - update all sensor equipment */ public function updateSensor() { $inputBin = Convert::powOf2($this->request['input_status']); // get all sensor status of this station $query = new Query(); $query->select('s.binary_pos, s.type, st.id, st.sensor_id, st.value'); $query->from('sensor_status st'); $query->leftJoin('sensor s', 'st.sensor_id = s.id'); $query->where('station_id = ' . $this->request['id']); $sensors = $query->all(); if (!empty($sensors)) { foreach ($sensors as $sensor) { $vs = in_array($sensor['binary_pos'], $inputBin) ? 1 : 0; if ($sensor['type'] == Sensor::TYPE_CONFIGURE) { Yii::$app->db->createCommand()->update('sensor_status', ['value' => $vs], ['id' => $sensor['id']])->execute(); } else { if ($sensor['type'] == Sensor::TYPE_VALUE) { $value = ''; // if this is security mode if ($sensor['sensor_id'] == Sensor::ID_SECURITY) { // security mode handler if ($this->request['message'] == self::MSG_ARMING) { $value = 1; } if ($this->request['message'] == self::MSG_DISARM) { $value = 0; } // compare with handler status if (isset($this->handler['security']['status']) && $value != $this->handler['security']['status']) { $value = $this->handler['security']['status']; $this->sendBack = true; } // if security has been turn off, create an alarm if ($sensor['value'] != $value) { if ($value == 0) { // create turn off security mode alarm $message = 'Tat bao dong'; } elseif ($value == 1) { // create turn on security mode alarm $message = 'Bat bao dong'; } $this->alarm($message); } } // if this is temperature if ($sensor['sensor_id'] == Sensor::ID_TEMPERATURE) { $value = $this->request['temp']; } // if this is humidity if ($sensor['sensor_id'] == Sensor::ID_HUMIDITY) { $value = $this->request['humi']; } Yii::$app->db->createCommand()->update('sensor_status', ['value' => $value], ['id' => $sensor['id']])->execute(); } } } } }
private function buildQuery($export = false) { $parseData = []; if ($export) { $query = new Query(); $query->select(['warning.message AS message', 'station.name AS station_name', 'DATE_FORMAT(FROM_UNIXTIME(warning.warning_time), "%d/%m/%Y %H:%i:%s") AS warning_date'])->from('warning')->leftJoin('station', 'station.id = warning.station_id')->where([]); } else { $query = Warning::find()->leftJoin('station', 'station.id = warning.station_id')->where([]); } // permission $stationIds = []; $role = new Role(); if (!$role->isAdministrator) { $position = $role->getPosition(); $stationIds = Station::getByRole($position, Yii::$app->user->id); } //filter by stations $stations = Yii::$app->request->get('station'); if (!empty($stations)) { $stationIds = array_merge($stationIds, $stations); } if (!empty($stationIds)) { foreach ($stationIds as $k => $id) { if (intval($id) <= 0) { unset($stationIds[$k]); } } } if (!empty($stationIds)) { $condition = ['in', 'station_id', $stationIds]; $query->where($condition); } // filter by area $areaId = Yii::$app->request->get('area'); if ($areaId > 0) { $query->andWhere(['station.area_id' => $areaId]); } // filter by center $center = Yii::$app->request->get('center'); if ($center > 0) { $query->andWhere(['station.center_id' => $center]); } // filter by time points $getBy = Yii::$app->request->get('get_by'); if ($getBy) { if ($getBy == 'today') { $timePoints = Convert::currentTimePoints(); } else { if ($getBy == 'week') { $timePoints = Convert::currentWeekTimePoints(); } else { if ($getBy == 'month') { $timePoints = Convert::currentMonthTimePoints(); } } } $query->andWhere(['>=', 'warning.warning_time', $timePoints['start']]); $query->andWhere(['<=', 'warning.warning_time', $timePoints['end']]); } // filter by time duration $fromDate = Yii::$app->request->get('from_date'); if ($fromDate) { $fromTime = Convert::date2Time($fromDate, 'd/m/Y'); $query->andWhere(['>=', 'warning_time', $fromTime]); } $toDate = Yii::$app->request->get('to_date'); if ($toDate) { $toTime = Convert::date2Time($toDate, 'd/m/Y', 'end'); $query->andWhere(['<=', 'warning_time', $toTime]); } $query->orderBy('warning_time DESC'); return ['query' => $query, 'parseData' => $parseData]; }
public function actionIndex() { $query = Station::find()->select('station.*'); // session $role = new Role(); if (!$role->isAdministrator) { $position = $role->getPosition(); $stationIds = Station::getByRole($position, Yii::$app->user->id); $condition = ['in', 'station.id', $stationIds]; $query->where($condition); } // conditions $areaId = Yii::$app->request->get('area_id'); if ($areaId > 0) { $query->andWhere(['area_id' => $areaId]); } // has_warning $hasWarning = Yii::$app->request->get('has_warning'); if ($hasWarning) { $getBy = Yii::$app->request->get('get_by'); if ($getBy == 'today') { $timePoints = Convert::currentTimePoints(); } else { if ($getBy == 'week') { $timePoints = Convert::currentWeekTimePoints(); } else { if ($getBy == 'month') { $timePoints = Convert::currentMonthTimePoints(); } } } if (isset($timePoints)) { $subSql = new Query(); $subSql->select('s.id')->from('station s')->innerJoin('warning w', 'w.station_id = s.id')->andWhere(['>=', 'w.warning_time', $timePoints['start']])->andWhere(['<=', 'w.warning_time', $timePoints['end']]); if ($hasWarning == 'yes') { $query->andWhere(['in', 'id', $subSql]); } else { if ($hasWarning == 'no') { $query->andWhere(['not in', 'id', $subSql]); } } } } // connect $connect = Yii::$app->request->get('connect'); if ($connect) { if ($connect == 'yes') { $query->andWhere(['status' => Station::STATUS_CONNECTED]); } else { if ($connect == 'no') { $query->andWhere(['status' => Station::STATUS_LOST]); } } } // security $security = Yii::$app->request->get('security'); if ($security) { $query->leftJoin('sensor_status', 'sensor_status.station_id = station.id'); $query->andWhere(['sensor_id' => Sensor::ID_SECURITY]); if ($security == 'on') { $query->andWhere(['sensor_status.value' => Sensor::SECURITY_ON]); } else { if ($security == 'off') { $query->andWhere(['sensor_status.value' => Sensor::SECURITY_OFF]); } } } $dataProvider = new ActiveDataProvider(['query' => $query->groupBy('station.id')]); return $this->render('index', ['dataProvider' => $dataProvider]); }
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); }