From 54b0cb32f517bc28d8906bba1eb89ea88e02ddb6 Mon Sep 17 00:00:00 2001 From: Alex Solomaha Date: Sat, 26 Nov 2016 22:35:03 +0200 Subject: [PATCH] #14 Events --- migrations/m161126_184646_create_events.php | 32 ++++ migrations/m161126_185030_create_task.php | 25 +++ migrations/m161126_185035_create_action.php | 29 ++++ models/Event.php | 108 ++++++++++++ models/Item.php | 9 + models/Task.php | 60 +++++++ models/TaskAction.php | 72 ++++++++ modules/admin/controllers/EventController.php | 124 +++++++++++++ .../controllers/TaskActionController.php | 124 +++++++++++++ modules/admin/controllers/TaskController.php | 124 +++++++++++++ modules/admin/models/EventSearch.php | 76 ++++++++ modules/admin/models/TaskActionSearch.php | 72 ++++++++ modules/admin/models/TaskSearch.php | 69 ++++++++ modules/admin/views/event/_form.php | 53 ++++++ modules/admin/views/event/create.php | 18 ++ modules/admin/views/event/index.php | 41 +++++ modules/admin/views/event/update.php | 19 ++ modules/admin/views/event/view.php | 41 +++++ modules/admin/views/task-action/_form.php | 40 +++++ modules/admin/views/task-action/create.php | 18 ++ modules/admin/views/task-action/index.php | 37 ++++ modules/admin/views/task-action/update.php | 19 ++ modules/admin/views/task-action/view.php | 37 ++++ modules/admin/views/task/_form.php | 23 +++ modules/admin/views/task/create.php | 18 ++ modules/admin/views/task/index.php | 34 ++++ modules/admin/views/task/update.php | 19 ++ modules/admin/views/task/view.php | 34 ++++ servers/Panel.php | 164 +++++++++++++++++- views/layouts/_left.php | 7 +- 30 files changed, 1544 insertions(+), 2 deletions(-) create mode 100644 migrations/m161126_184646_create_events.php create mode 100644 migrations/m161126_185030_create_task.php create mode 100644 migrations/m161126_185035_create_action.php create mode 100644 models/Event.php create mode 100644 models/Task.php create mode 100644 models/TaskAction.php create mode 100644 modules/admin/controllers/EventController.php create mode 100644 modules/admin/controllers/TaskActionController.php create mode 100644 modules/admin/controllers/TaskController.php create mode 100644 modules/admin/models/EventSearch.php create mode 100644 modules/admin/models/TaskActionSearch.php create mode 100644 modules/admin/models/TaskSearch.php create mode 100644 modules/admin/views/event/_form.php create mode 100644 modules/admin/views/event/create.php create mode 100644 modules/admin/views/event/index.php create mode 100644 modules/admin/views/event/update.php create mode 100644 modules/admin/views/event/view.php create mode 100644 modules/admin/views/task-action/_form.php create mode 100644 modules/admin/views/task-action/create.php create mode 100644 modules/admin/views/task-action/index.php create mode 100644 modules/admin/views/task-action/update.php create mode 100644 modules/admin/views/task-action/view.php create mode 100644 modules/admin/views/task/_form.php create mode 100644 modules/admin/views/task/create.php create mode 100644 modules/admin/views/task/index.php create mode 100644 modules/admin/views/task/update.php create mode 100644 modules/admin/views/task/view.php diff --git a/migrations/m161126_184646_create_events.php b/migrations/m161126_184646_create_events.php new file mode 100644 index 0000000..5445f7a --- /dev/null +++ b/migrations/m161126_184646_create_events.php @@ -0,0 +1,32 @@ +db->driverName === 'mysql') { + $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; + } + + $this->createTable('event', [ + 'id' => $this->primaryKey(), + 'type' => $this->smallInteger()->notNull(), + 'trig_date' => $this->string(), + 'trig_time' => $this->string(), + 'trig_time_wdays' => $this->string(), + 'trig_item_id' => $this->integer(), + 'trig_item_value' => $this->string(), + 'task_id' => $this->integer(), + 'name' => $this->string()->notNull(), + ], $tableOptions); + } + + public function safeDown() + { + $this->dropTable('event'); + } +} diff --git a/migrations/m161126_185030_create_task.php b/migrations/m161126_185030_create_task.php new file mode 100644 index 0000000..9f240f6 --- /dev/null +++ b/migrations/m161126_185030_create_task.php @@ -0,0 +1,25 @@ +db->driverName === 'mysql') { + $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; + } + + $this->createTable('task', [ + 'id' => $this->primaryKey(), + 'name' => $this->string()->notNull(), + ], $tableOptions); + } + + public function safeDown() + { + $this->dropTable('task'); + } +} diff --git a/migrations/m161126_185035_create_action.php b/migrations/m161126_185035_create_action.php new file mode 100644 index 0000000..5649a1f --- /dev/null +++ b/migrations/m161126_185035_create_action.php @@ -0,0 +1,29 @@ +db->driverName === 'mysql') { + $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; + } + + $this->createTable('task_action', [ + 'id' => $this->primaryKey(), + 'type' => $this->smallInteger()->notNull(), + 'item_id' => $this->integer(), + 'item_value' => $this->string(), + 'task_id' => $this->integer(), + 'name' => $this->string()->notNull(), + ], $tableOptions); + } + + public function safeDown() + { + $this->dropTable('task_action'); + } +} diff --git a/models/Event.php b/models/Event.php new file mode 100644 index 0000000..8b6d490 --- /dev/null +++ b/models/Event.php @@ -0,0 +1,108 @@ + self::getStatusesArray()], + [['trig_date', 'trig_item_value', 'name', 'trig_time', 'trig_time_wdays'], 'string', 'max' => 255], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'id' => 'ID', + 'type' => 'Тип', + 'trig_date' => 'Дата срабатывания', + 'trig_time' => 'Время срабатывания', + 'trig_time_wdays' => 'Дни срабатывания', + 'trig_item_id' => 'Элемент срабатывания', + 'trig_item_value' => 'Значение элемента срабатывания', + 'task_id' => 'Задача', + 'name' => 'Имя', + ]; + } + + /** + * @return array + */ + public static function getStatuses() + { + return [ + self::TYPE_BY_ITEM_VALUE => 'Значение Элемента', + self::TYPE_BY_USER_ITEM_CHANGE => 'Изменение Значение Элемента', + self::TYPE_BY_DATE => 'Дата', + self::TYPE_BY_TIME => 'Время', + ]; + } + + /** + * @return array + */ + public static function getStatusesArray() + { + return [ + self::TYPE_BY_ITEM_VALUE, + self::TYPE_BY_USER_ITEM_CHANGE, + self::TYPE_BY_DATE, + self::TYPE_BY_TIME, + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getTask() + { + return $this->hasOne(Task::className(), ['id' => 'task_id'])->inverseOf('events'); + } + + public static function getList() + { + return ArrayHelper::map(self::find()->all(), 'id', 'name'); + } +} diff --git a/models/Item.php b/models/Item.php index 447303a..fd024d4 100644 --- a/models/Item.php +++ b/models/Item.php @@ -5,6 +5,7 @@ namespace app\models; use linslin\yii2\curl\Curl; use Yii; use yii\db\ActiveRecord; +use yii\helpers\ArrayHelper; /** * This is the model class for table "item". @@ -41,6 +42,9 @@ class Item extends ActiveRecord const VALUE_ON = 1; const VALUE_OFF = 0; + const MODE_RAINBOW = 'rainbow'; + const MODE_BREATH = 'breath'; + /** * Used for WS handler * @var mixed @@ -145,4 +149,9 @@ class Item extends ActiveRecord { return self::getTypesArray()[$this->type]; } + + public static function getList() + { + return ArrayHelper::map(self::find()->all(), 'id', 'name'); + } } diff --git a/models/Task.php b/models/Task.php new file mode 100644 index 0000000..17bbf41 --- /dev/null +++ b/models/Task.php @@ -0,0 +1,60 @@ + 255], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'id' => 'ID', + 'name' => 'Name', + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getTaskActions() + { + return $this->hasMany(TaskAction::className(), ['task_id' => 'id']); + } + + public static function getList() + { + return ArrayHelper::map(self::find()->all(), 'id', 'name'); + } +} diff --git a/models/TaskAction.php b/models/TaskAction.php new file mode 100644 index 0000000..66b9f01 --- /dev/null +++ b/models/TaskAction.php @@ -0,0 +1,72 @@ + 255], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'id' => 'ID', + 'type' => 'Тип', + 'item_id' => 'Элемент', + 'item_value' => 'Значение Элемента', + 'task_id' => 'Задача', + 'name' => 'Название', + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getItem() + { + return $this->hasOne(Item::className(), ['id' => 'item_id']); + } + + public static function getTypes() + { + return [ + self::TYPE_CHANGE_ITEM_VALUE => 'Изменить значение Элемента', + ]; + } +} diff --git a/modules/admin/controllers/EventController.php b/modules/admin/controllers/EventController.php new file mode 100644 index 0000000..aaa48b5 --- /dev/null +++ b/modules/admin/controllers/EventController.php @@ -0,0 +1,124 @@ + [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => ['POST'], + ], + ], + ]; + } + + /** + * Lists all Event models. + * @return mixed + */ + public function actionIndex() + { + $searchModel = new EventSearch(); + $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + + return $this->render('index', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + ]); + } + + /** + * Displays a single Event model. + * @param integer $id + * @return mixed + */ + public function actionView($id) + { + return $this->render('view', [ + 'model' => $this->findModel($id), + ]); + } + + /** + * Creates a new Event model. + * If creation is successful, the browser will be redirected to the 'view' page. + * @return mixed + */ + public function actionCreate() + { + $model = new Event(); + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->id]); + } else { + return $this->render('create', [ + 'model' => $model, + ]); + } + } + + /** + * Updates an existing Event model. + * If update is successful, the browser will be redirected to the 'view' page. + * @param integer $id + * @return mixed + */ + public function actionUpdate($id) + { + $model = $this->findModel($id); + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->id]); + } else { + return $this->render('update', [ + 'model' => $model, + ]); + } + } + + /** + * Deletes an existing Event model. + * If deletion is successful, the browser will be redirected to the 'index' page. + * @param integer $id + * @return mixed + */ + public function actionDelete($id) + { + $this->findModel($id)->delete(); + + return $this->redirect(['index']); + } + + /** + * Finds the Event model based on its primary key value. + * If the model is not found, a 404 HTTP exception will be thrown. + * @param integer $id + * @return Event the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel($id) + { + if (($model = Event::findOne($id)) !== null) { + return $model; + } else { + throw new NotFoundHttpException('The requested page does not exist.'); + } + } +} diff --git a/modules/admin/controllers/TaskActionController.php b/modules/admin/controllers/TaskActionController.php new file mode 100644 index 0000000..8265518 --- /dev/null +++ b/modules/admin/controllers/TaskActionController.php @@ -0,0 +1,124 @@ + [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => ['POST'], + ], + ], + ]; + } + + /** + * Lists all TaskAction models. + * @return mixed + */ + public function actionIndex() + { + $searchModel = new TaskActionSearch(); + $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + + return $this->render('index', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + ]); + } + + /** + * Displays a single TaskAction model. + * @param integer $id + * @return mixed + */ + public function actionView($id) + { + return $this->render('view', [ + 'model' => $this->findModel($id), + ]); + } + + /** + * Creates a new TaskAction model. + * If creation is successful, the browser will be redirected to the 'view' page. + * @return mixed + */ + public function actionCreate() + { + $model = new TaskAction(); + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->id]); + } else { + return $this->render('create', [ + 'model' => $model, + ]); + } + } + + /** + * Updates an existing TaskAction model. + * If update is successful, the browser will be redirected to the 'view' page. + * @param integer $id + * @return mixed + */ + public function actionUpdate($id) + { + $model = $this->findModel($id); + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->id]); + } else { + return $this->render('update', [ + 'model' => $model, + ]); + } + } + + /** + * Deletes an existing TaskAction model. + * If deletion is successful, the browser will be redirected to the 'index' page. + * @param integer $id + * @return mixed + */ + public function actionDelete($id) + { + $this->findModel($id)->delete(); + + return $this->redirect(['index']); + } + + /** + * Finds the TaskAction model based on its primary key value. + * If the model is not found, a 404 HTTP exception will be thrown. + * @param integer $id + * @return TaskAction the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel($id) + { + if (($model = TaskAction::findOne($id)) !== null) { + return $model; + } else { + throw new NotFoundHttpException('The requested page does not exist.'); + } + } +} diff --git a/modules/admin/controllers/TaskController.php b/modules/admin/controllers/TaskController.php new file mode 100644 index 0000000..700616e --- /dev/null +++ b/modules/admin/controllers/TaskController.php @@ -0,0 +1,124 @@ + [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => ['POST'], + ], + ], + ]; + } + + /** + * Lists all Task models. + * @return mixed + */ + public function actionIndex() + { + $searchModel = new TaskSearch(); + $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + + return $this->render('index', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + ]); + } + + /** + * Displays a single Task model. + * @param integer $id + * @return mixed + */ + public function actionView($id) + { + return $this->render('view', [ + 'model' => $this->findModel($id), + ]); + } + + /** + * Creates a new Task model. + * If creation is successful, the browser will be redirected to the 'view' page. + * @return mixed + */ + public function actionCreate() + { + $model = new Task(); + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->id]); + } else { + return $this->render('create', [ + 'model' => $model, + ]); + } + } + + /** + * Updates an existing Task model. + * If update is successful, the browser will be redirected to the 'view' page. + * @param integer $id + * @return mixed + */ + public function actionUpdate($id) + { + $model = $this->findModel($id); + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->id]); + } else { + return $this->render('update', [ + 'model' => $model, + ]); + } + } + + /** + * Deletes an existing Task model. + * If deletion is successful, the browser will be redirected to the 'index' page. + * @param integer $id + * @return mixed + */ + public function actionDelete($id) + { + $this->findModel($id)->delete(); + + return $this->redirect(['index']); + } + + /** + * Finds the Task model based on its primary key value. + * If the model is not found, a 404 HTTP exception will be thrown. + * @param integer $id + * @return Task the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel($id) + { + if (($model = Task::findOne($id)) !== null) { + return $model; + } else { + throw new NotFoundHttpException('The requested page does not exist.'); + } + } +} diff --git a/modules/admin/models/EventSearch.php b/modules/admin/models/EventSearch.php new file mode 100644 index 0000000..b530874 --- /dev/null +++ b/modules/admin/models/EventSearch.php @@ -0,0 +1,76 @@ + $query, + ]); + + $this->load($params); + + if (!$this->validate()) { + // uncomment the following line if you do not want to return any records when validation fails + // $query->where('0=1'); + return $dataProvider; + } + + // grid filtering conditions + $query->andFilterWhere([ + 'id' => $this->id, + 'type' => $this->type, + 'trig_item_id' => $this->trig_item_id, + 'task_id' => $this->task_id, + ]); + + $query->andFilterWhere(['like', 'trig_date', $this->trig_date]) + ->andFilterWhere(['like', 'trig_time', $this->trig_time]) + ->andFilterWhere(['like', 'trig_time_wdays', $this->trig_time_wdays]) + ->andFilterWhere(['like', 'trig_item_value', $this->trig_item_value]) + ->andFilterWhere(['like', 'name', $this->name]); + + return $dataProvider; + } +} diff --git a/modules/admin/models/TaskActionSearch.php b/modules/admin/models/TaskActionSearch.php new file mode 100644 index 0000000..6b1ee51 --- /dev/null +++ b/modules/admin/models/TaskActionSearch.php @@ -0,0 +1,72 @@ + $query, + ]); + + $this->load($params); + + if (!$this->validate()) { + // uncomment the following line if you do not want to return any records when validation fails + // $query->where('0=1'); + return $dataProvider; + } + + // grid filtering conditions + $query->andFilterWhere([ + 'id' => $this->id, + 'type' => $this->type, + 'item_id' => $this->item_id, + ]); + + $query->andFilterWhere(['like', 'item_value', $this->item_value]) + ->andFilterWhere(['like', 'name', $this->name]); + + return $dataProvider; + } +} diff --git a/modules/admin/models/TaskSearch.php b/modules/admin/models/TaskSearch.php new file mode 100644 index 0000000..f0b9967 --- /dev/null +++ b/modules/admin/models/TaskSearch.php @@ -0,0 +1,69 @@ + $query, + ]); + + $this->load($params); + + if (!$this->validate()) { + // uncomment the following line if you do not want to return any records when validation fails + // $query->where('0=1'); + return $dataProvider; + } + + // grid filtering conditions + $query->andFilterWhere([ + 'id' => $this->id, + ]); + + $query->andFilterWhere(['like', 'name', $this->name]); + + return $dataProvider; + } +} diff --git a/modules/admin/views/event/_form.php b/modules/admin/views/event/_form.php new file mode 100644 index 0000000..2b0367d --- /dev/null +++ b/modules/admin/views/event/_form.php @@ -0,0 +1,53 @@ + + +
+ + + + field($model, 'type')->dropDownList(Event::getStatuses(), [ + 'prompt' => '-- выберите тип --' + ]) ?> + +
+
+ field($model, 'trig_time')->textInput(['maxlength' => true]) ?> +
+
+ field($model, 'trig_time_wdays')->textInput(['maxlength' => true]) ?> +
+
+ + field($model, 'trig_date')->textInput(['maxlength' => true]) ?> + + field($model, 'trig_item_id')->dropDownList(Item::getList(), [ + 'prompt' => '-- выберите элемент --' + ]) ?> + + field($model, 'trig_item_value')->textInput(['maxlength' => true]) ?> + + field($model, 'task_id')->dropDownList(Task::getList(), [ + 'prompt' => '-- выберите задачу --' + ]) ?> + + field($model, 'name')->textInput(['maxlength' => true]) ?> + +
+ isNewRecord ? 'Добавить' : 'Сохранить', + ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> +
+ + + +
diff --git a/modules/admin/views/event/create.php b/modules/admin/views/event/create.php new file mode 100644 index 0000000..edf3c1f --- /dev/null +++ b/modules/admin/views/event/create.php @@ -0,0 +1,18 @@ +title = 'Добавить Event'; +$this->params['breadcrumbs'][] = ['label' => 'Events', 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ + render('_form', [ + 'model' => $model, + ]) ?> + +
diff --git a/modules/admin/views/event/index.php b/modules/admin/views/event/index.php new file mode 100644 index 0000000..3a3b742 --- /dev/null +++ b/modules/admin/views/event/index.php @@ -0,0 +1,41 @@ +title = 'Events'; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

+ 'btn btn-success']) ?> +

+ render('_search', ['model' => $searchModel]); ?> + + + $dataProvider, + 'summaryOptions' => ['class' => 'alert alert-info'], + 'layout' => '{summary}
{items}
{pager}', + 'filterModel' => $searchModel, + 'columns' => [ + 'id', + 'type', + 'trig_date', + 'trig_time', + 'trig_time_wdays', + // 'trig_item_id', + // 'trig_item_value', + // 'task_id', + // 'name', + + ['class' => 'yii\grid\ActionColumn'], + ], + ]); ?> + +
diff --git a/modules/admin/views/event/update.php b/modules/admin/views/event/update.php new file mode 100644 index 0000000..c23321c --- /dev/null +++ b/modules/admin/views/event/update.php @@ -0,0 +1,19 @@ +title = 'Изменить Event: ' . $model->name; +$this->params['breadcrumbs'][] = ['label' => 'Events', 'url' => ['index']]; +$this->params['breadcrumbs'][] = ['label' => $model->name, 'url' => ['view', 'id' => $model->id]]; +$this->params['breadcrumbs'][] = 'Редактирование'; +?> +
+ + render('_form', [ + 'model' => $model, + ]) ?> + +
diff --git a/modules/admin/views/event/view.php b/modules/admin/views/event/view.php new file mode 100644 index 0000000..5540f47 --- /dev/null +++ b/modules/admin/views/event/view.php @@ -0,0 +1,41 @@ +title = $model->name; +$this->params['breadcrumbs'][] = ['label' => 'Events', 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

+ $model->id], ['class' => 'btn btn-primary']) ?> + $model->id], [ + 'class' => 'btn btn-danger', + 'data' => [ + 'confirm' => 'Are you sure you want to delete this item?', + 'method' => 'post', + ], + ]) ?> +

+ + $model, + 'attributes' => [ + 'id', + 'type', + 'trig_date', + 'trig_time', + 'trig_time_wdays', + 'trig_item_id', + 'trig_item_value', + 'task_id', + 'name', + ], + ]) ?> + +
diff --git a/modules/admin/views/task-action/_form.php b/modules/admin/views/task-action/_form.php new file mode 100644 index 0000000..331a5b6 --- /dev/null +++ b/modules/admin/views/task-action/_form.php @@ -0,0 +1,40 @@ + + +
+ + + + field($model, 'task_id')->dropDownList(Task::getList(), [ + 'prompt' => '-- выберите задачу --', + ]) ?> + + field($model, 'type')->dropDownList(TaskAction::getTypes(), [ + 'prompt' => '-- выберите тип --', + ]) ?> + + field($model, 'item_id')->dropDownList(Item::getList(), [ + 'prompt' => '-- выберите элемент --', + ]) ?> + + field($model, 'item_value')->textInput(['maxlength' => true]) ?> + + field($model, 'name')->textInput(['maxlength' => true]) ?> + +
+ isNewRecord ? 'Добавить' : 'Сохранить', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> +
+ + + +
diff --git a/modules/admin/views/task-action/create.php b/modules/admin/views/task-action/create.php new file mode 100644 index 0000000..9ad6032 --- /dev/null +++ b/modules/admin/views/task-action/create.php @@ -0,0 +1,18 @@ +title = 'Добавить Task Action'; +$this->params['breadcrumbs'][] = ['label' => 'Task Actions', 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ + render('_form', [ + 'model' => $model, + ]) ?> + +
diff --git a/modules/admin/views/task-action/index.php b/modules/admin/views/task-action/index.php new file mode 100644 index 0000000..7750b70 --- /dev/null +++ b/modules/admin/views/task-action/index.php @@ -0,0 +1,37 @@ +title = 'Task Actions'; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

+ 'btn btn-success']) ?> +

+ render('_search', ['model' => $searchModel]); ?> + + + $dataProvider, + 'summaryOptions' => ['class' => 'alert alert-info'], + 'layout' => '{summary}
{items}
{pager}', + 'filterModel' => $searchModel, + 'columns' => [ + 'id', + 'type', + 'item_id', + 'item_value', + 'name', + + ['class' => 'yii\grid\ActionColumn'], + ], + ]); ?> + +
diff --git a/modules/admin/views/task-action/update.php b/modules/admin/views/task-action/update.php new file mode 100644 index 0000000..9880b3d --- /dev/null +++ b/modules/admin/views/task-action/update.php @@ -0,0 +1,19 @@ +title = 'Изменить Task Action: ' . $model->name; +$this->params['breadcrumbs'][] = ['label' => 'Task Actions', 'url' => ['index']]; +$this->params['breadcrumbs'][] = ['label' => $model->name, 'url' => ['view', 'id' => $model->id]]; +$this->params['breadcrumbs'][] = 'Редактирование'; +?> +
+ + render('_form', [ + 'model' => $model, + ]) ?> + +
diff --git a/modules/admin/views/task-action/view.php b/modules/admin/views/task-action/view.php new file mode 100644 index 0000000..9e54856 --- /dev/null +++ b/modules/admin/views/task-action/view.php @@ -0,0 +1,37 @@ +title = $model->name; +$this->params['breadcrumbs'][] = ['label' => 'Task Actions', 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

+ $model->id], ['class' => 'btn btn-primary']) ?> + $model->id], [ + 'class' => 'btn btn-danger', + 'data' => [ + 'confirm' => 'Are you sure you want to delete this item?', + 'method' => 'post', + ], + ]) ?> +

+ + $model, + 'attributes' => [ + 'id', + 'type', + 'item_id', + 'item_value', + 'name', + ], + ]) ?> + +
diff --git a/modules/admin/views/task/_form.php b/modules/admin/views/task/_form.php new file mode 100644 index 0000000..5c589a3 --- /dev/null +++ b/modules/admin/views/task/_form.php @@ -0,0 +1,23 @@ + + +
+ + + + field($model, 'name')->textInput(['maxlength' => true]) ?> + +
+ isNewRecord ? 'Добавить' : 'Сохранить', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> +
+ + + +
diff --git a/modules/admin/views/task/create.php b/modules/admin/views/task/create.php new file mode 100644 index 0000000..f63b7c8 --- /dev/null +++ b/modules/admin/views/task/create.php @@ -0,0 +1,18 @@ +title = 'Добавить Task'; +$this->params['breadcrumbs'][] = ['label' => 'Tasks', 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ + render('_form', [ + 'model' => $model, + ]) ?> + +
diff --git a/modules/admin/views/task/index.php b/modules/admin/views/task/index.php new file mode 100644 index 0000000..14b85bc --- /dev/null +++ b/modules/admin/views/task/index.php @@ -0,0 +1,34 @@ +title = 'Tasks'; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

+ 'btn btn-success']) ?> +

+ render('_search', ['model' => $searchModel]); ?> + + + $dataProvider, + 'summaryOptions' => ['class' => 'alert alert-info'], + 'layout' => '{summary}
{items}
{pager}', + 'filterModel' => $searchModel, + 'columns' => [ + 'id', + 'name', + + ['class' => 'yii\grid\ActionColumn'], + ], + ]); ?> + +
diff --git a/modules/admin/views/task/update.php b/modules/admin/views/task/update.php new file mode 100644 index 0000000..78599d3 --- /dev/null +++ b/modules/admin/views/task/update.php @@ -0,0 +1,19 @@ +title = 'Изменить Task: ' . $model->name; +$this->params['breadcrumbs'][] = ['label' => 'Tasks', 'url' => ['index']]; +$this->params['breadcrumbs'][] = ['label' => $model->name, 'url' => ['view', 'id' => $model->id]]; +$this->params['breadcrumbs'][] = 'Редактирование'; +?> +
+ + render('_form', [ + 'model' => $model, + ]) ?> + +
diff --git a/modules/admin/views/task/view.php b/modules/admin/views/task/view.php new file mode 100644 index 0000000..4e8fb46 --- /dev/null +++ b/modules/admin/views/task/view.php @@ -0,0 +1,34 @@ +title = $model->name; +$this->params['breadcrumbs'][] = ['label' => 'Tasks', 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

+ $model->id], ['class' => 'btn btn-primary']) ?> + $model->id], [ + 'class' => 'btn btn-danger', + 'data' => [ + 'confirm' => 'Are you sure you want to delete this item?', + 'method' => 'post', + ], + ]) ?> +

+ + $model, + 'attributes' => [ + 'id', + 'name', + ], + ]) ?> + +
diff --git a/servers/Panel.php b/servers/Panel.php index 9ce5bf6..2ae377e 100644 --- a/servers/Panel.php +++ b/servers/Panel.php @@ -3,8 +3,10 @@ namespace app\servers; use app\models\Board; +use app\models\Event; use app\models\History; use app\models\Item; +use app\models\TaskAction; use app\models\User; use Ratchet\ConnectionInterface; use Ratchet\MessageComponentInterface; @@ -306,9 +308,11 @@ class Panel implements MessageComponentInterface return $this->handleTurnOff($from, $user, $data); case 'rgb': return $this->handleRgb($from, $user, $data); + case 'rgbMode': + return $this->handleRgbMode($from, $user, $data); } - return false; + return $this->log("Unknown command from user: $msg"); } /** @@ -342,6 +346,9 @@ class Panel implements MessageComponentInterface return $this->log('Trying to use unknown item'); } + // Trig event + $this->trigItemValueEvent($item, $value); + if (in_array($item->type, [ Item::TYPE_SWITCH, Item::TYPE_VARIABLE_BOOLEAN, @@ -398,6 +405,9 @@ class Panel implements MessageComponentInterface return $this->log('Trying to use unknown item'); } + // Trig event + $this->trigItemValueEvent($item, $value); + if (in_array($item->type, [ Item::TYPE_SWITCH, Item::TYPE_VARIABLE_BOOLEAN, @@ -644,6 +654,79 @@ class Panel implements MessageComponentInterface return true; } + /** + * @param ConnectionInterface $from + * @param User $user + * @param array $data + * @return bool|mixed + * @throws NotSupportedException + */ + private function handleRgbMode($from, $user, $data) + { + $item_id = (int)$data['item_id']; + $item = Item::findOne($item_id); + + if (!$item) { + return $from->send([ + 'type' => 'error', + 'message' => 'Такое устройство не существует', + ]); + } + + if ($item->type !== Item::TYPE_RGB) { + return $from->send([ + 'type' => 'error', + 'message' => 'Данный тип устройства не является RGB', + ]); + } + + $mode = $data['mode']; + + if (!in_array($mode, [Item::MODE_BREATH, Item::MODE_RAINBOW])) { + return $from->send([ + 'type' => 'error', + 'message' => 'Неизвестный режим', + ]); + } + + $board = $item->board; + + switch ($board->type) { + case Board::TYPE_AREST: + throw new NotSupportedException(); + + break; + case Board::TYPE_WEBSOCKET: + if (!$this->isBoardConnected($board->id)) { + return $from->send(Json::encode([ + 'type' => 'error', + 'message' => 'Устройство не подключено', + ])); + } + + $this->sendToBoard($board->id, [ + 'type' => 'rgbMode', + 'mode' => $mode, + ]); + + break; + } + + $history = new History(); + $history->type = History::TYPE_USER_ACTION; + $history->user_id = $user->id; + $history->item_id = $item->id; + $history->commited_at = time(); + $history->value = $mode; + + if (!$history->save()) { + $this->log("Cannot log: "); + var_dump($history->errors); + } + + return true; + } + /** * Send data to all users * @@ -828,4 +911,83 @@ class Panel implements MessageComponentInterface return true; } + + /** + * @param Item $item + * @param string $value + */ + private function trigItemValueEvent($item, $value) + { + // Find event + $event = Event::findOne([ + 'trig_item_id' => $item->id, + 'trig_item_value' => $value, + ]); + + if (!$event) { + return; + } + + // Do the tasks + $task = $event->task; + + foreach ($task->taskActions as $action) { + $this->doTaskAction($action, $item); + } + } + + /** + * @param TaskAction $action + * @param null $item + */ + private function doTaskAction($action, $item = null) + { + switch ($action->type) { + case TaskAction::TYPE_CHANGE_ITEM_VALUE: + $item = $item ? $item : $action->item; + + switch ($item->type) { + case Item::TYPE_SWITCH: + $data = [ + 'type' => $action->item_value == '1' ? 'turnON' : 'turnOFF', + 'pin' => $item->pin, + ]; + + break; + case Item::TYPE_RGB: + $rgbData = $this->valueToRgbData($action->item_value); + $red = $rgbData[0]; + $green = $rgbData[1]; + $blue = $rgbData[2]; + $fade = $rgbData[3]; + + $data = [ + 'type' => 'rgb', + 'red' => $red * 4, + 'green' => $green * 4, + 'blue' => $blue * 4, + 'fade' => $fade, + ]; + + break; + default: + $data = [ + 'type' => 'value', + 'value' => $action->item_value, + 'pin' => $item->pin, + ]; + + break; + } + + $this->sendToBoard($item->board_id, $data); + + break; + } + } + + private function valueToRgbData($value) + { + return explode(',', $value); + } } diff --git a/views/layouts/_left.php b/views/layouts/_left.php index 98fa357..5072cea 100644 --- a/views/layouts/_left.php +++ b/views/layouts/_left.php @@ -26,9 +26,14 @@ ['label' => 'Настройки', 'options' => ['class' => 'header']], ['label' => 'Элементы', 'icon' => 'fa fa-cubes', 'url' => ['/admin/item/index']], + ['label' => 'Платы', 'icon' => 'fa fa-cogs', 'url' => ['/admin/board/index']], + ['label' => 'Events', 'icon' => 'fa fa-cogs', 'items' => [ + ['label' => 'События', 'icon' => 'fa fa-cubes', 'url' => ['/admin/event/index']], + ['label' => 'Задачи', 'icon' => 'fa fa-cubes', 'url' => ['/admin/task/index']], + ['label' => 'Действия', 'icon' => 'fa fa-cubes', 'url' => ['/admin/task-action/index']], + ]], ['label' => 'Комнаты', 'icon' => 'fa fa-folder-open', 'url' => ['/admin/room/index']], ['label' => 'История', 'icon' => 'fa fa-bar-chart', 'url' => ['/admin/history/index']], - ['label' => 'Платы', 'icon' => 'fa fa-cogs', 'url' => ['/admin/board/index']], ['label' => 'Пользователи', 'icon' => 'fa fa-users', 'url' => ['/admin/user/index']], ['label' => 'Yii2', 'options' => ['class' => 'header'], 'visible' => YII_DEBUG],