예제 #1
0
 public function getStationStatusHandler()
 {
     // set handler
     $handles = StationStatusHandler::find()->where(['station_id' => $this->request['id'], 'updated' => StationStatusHandler::STATUS_NOT_UPDATE])->orderBy('created_at DESC')->all();
     if (!empty($handles)) {
         foreach ($handles as $hand) {
             if ($hand['type'] == StationStatusHandler::TYPE_EQUIPMENT) {
                 $this->handler['equip'][] = ['equip_id' => $hand['equip_id'], 'status' => $hand['status'], 'configure' => $hand['configure'], 'station_id' => $hand['station_id']];
             }
             if ($hand['type'] == StationStatusHandler::TYPE_SENSOR_SECURITY) {
                 $this->handler['security'] = ['equip_id' => Sensor::ID_SECURITY, 'status' => $hand['status'], 'station_id' => $hand['station_id']];
             }
         }
     }
 }
예제 #2
0
 public function actionChangeStationPart()
 {
     $get = Yii::$app->request->get();
     if (isset($get['part']) && $get['station_id'] > 0) {
         // find exist
         if ($get['part'] == 'equip') {
             $handler = StationStatusHandler::find()->where(['station_id' => $get['station_id'], 'updated' => StationStatusHandler::STATUS_NOT_UPDATE, 'equip_id' => $get['id'], 'type' => StationStatusHandler::TYPE_EQUIPMENT])->orderBy('created_at DESC')->one();
             // get equipment status
             $es = EquipmentStatus::findOne($get['id']);
             //write log action
             if ($get['configure'] == EquipmentStatus::CONFIGURE_AUTO) {
                 $action = Log::ACTION_AUTOMATIC_EQUIPMENT;
             } else {
                 if ($get['status'] == EquipmentStatus::STATUS_ON) {
                     $action = Log::ACTION_TURNON_EQUIPMENT;
                 } elseif ($get['status'] == EquipmentStatus::STATUS_OFF) {
                     $action = Log::ACTION_TURNOFF_EQUIPMENT;
                 }
             }
             if ($action) {
                 Log::logControl(Yii::$app->user->id, $action, $get['station_id'], $es['equipment_id']);
             }
         } else {
             if ($get['part'] == 'security') {
                 $handler = StationStatusHandler::find()->where(['station_id' => $get['station_id'], 'updated' => StationStatusHandler::STATUS_NOT_UPDATE, 'type' => StationStatusHandler::TYPE_SENSOR_SECURITY])->orderBy('created_at DESC')->one();
                 //write log action
                 $action = $get['status'] == Sensor::SECURITY_ON ? Log::ACTION_TURNON_SECURITY : ($get['status'] == Sensor::SECURITY_OFF ? Log::ACTION_TURNOFF_SECURITY : '');
                 if ($action) {
                     Log::logControl(Yii::$app->user->id, $action, $get['station_id'], Sensor::ID_SECURITY);
                 }
                 // create an alarm when user turn on or turn off security mode
                 $station = Station::findOne($get['station_id']);
                 $command[] = $station['code'];
                 $command[] = $station['name'];
                 $command[] = 'alarm';
                 $command[] = $get['status'] == Sensor::SECURITY_OFF ? 'Tat bao dong' : 'Bat bao dong';
                 $strCommand = implode('&', $command);
                 $observer = new Observer();
                 $observer->handleRequest($strCommand);
             }
         }
         // model
         if (!$handler) {
             $handler = new StationStatusHandler();
         }
         // if type is equipment
         if ($get['part'] == 'equip') {
             $handler->type = StationStatusHandler::TYPE_EQUIPMENT;
             $handler->configure = $get['configure'];
             $handler->equip_id = $es['equipment_id'];
         } else {
             if ($get['part'] == 'security') {
                 $handler->type = StationStatusHandler::TYPE_SENSOR_SECURITY;
                 $handler->equip_id = Sensor::ID_SECURITY;
             }
         }
         $handler->status = $get['status'];
         $handler->station_id = $get['station_id'];
         $handler->created_at = time();
         $handler->updated = StationStatusHandler::STATUS_NOT_UPDATE;
         // validate
         if ($handler->validate()) {
             $handler->save();
             // update station
             Yii::$app->db->createCommand()->update('station', ['change_equipment_status' => 1], ['id' => $get['station_id']])->execute();
             // create message
             Yii::$app->session->setFlash('update_success', 'Đã lưu thay đổi, sau khi gửi được dữ liệu sẽ hiển thị đã gửi dữ liệu.');
         }
     }
     $this->redirect(Yii::$app->homeUrl . 'station/default/view?id=' . $get['station_id']);
 }