Exemple #1
0
 public function actionStatus()
 {
     $items = [0 => '123', 1 => 'Tram_so_1', 2 => 'status', 3 => 'DISARM', 4 => '1', 5 => '6', 6 => '32', 7 => '15', 8 => '12', 9 => 'p1', 10 => 'p2', 11 => 'p3', 12 => 'p4', 13 => 'p5', 14 => 'p6', 15 => 'p7', 16 => 'p8', 17 => 'p9', 18 => '1', 19 => '2', 20 => '3', 21 => '4'];
     $requestString = implode('&', $items);
     $observer = new Observer();
     $observer->handleRequest($requestString);
 }
 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']);
 }