diff --git a/models/History.php b/models/History.php index 500bd48..7b9a520 100644 --- a/models/History.php +++ b/models/History.php @@ -26,6 +26,7 @@ class History extends \yii\db\ActiveRecord const TYPE_BOARD_CONNECTION = 30; const TYPE_USER_ACTION = 40; const TYPE_USER_CONNECTION = 50; + const TYPE_API_TRIGGER = 60; /** * @inheritdoc @@ -109,6 +110,7 @@ class History extends \yii\db\ActiveRecord self::TYPE_BOARD_CONNECTION => 'Состояние платы', self::TYPE_USER_ACTION => 'Действия пользователя', self::TYPE_USER_CONNECTION => 'Состояние пользователя', + self::TYPE_API_TRIGGER => 'API Trig', ]; } diff --git a/models/Trigger.php b/models/Trigger.php index f825eb5..267daa0 100644 --- a/models/Trigger.php +++ b/models/Trigger.php @@ -15,7 +15,7 @@ use yii\helpers\ArrayHelper; * @property integer $type * @property integer $trig_date * @property string $trig_time - * @property string $trig_time_wdays + * @property mixed $trig_time_wdays * @property integer $trig_item_id * @property string $trig_item_value * @property string $name @@ -28,6 +28,7 @@ class Trigger extends ActiveRecord const TYPE_BY_USER_ITEM_CHANGE = 20; const TYPE_BY_DATE = 30; const TYPE_BY_TIME = 40; + const TYPE_MANUAL = 50; /** * @inheritdoc @@ -46,8 +47,9 @@ class Trigger extends ActiveRecord [['type', 'name', 'active'], 'required'], [['type', 'trig_item_id', 'trig_date'], 'integer'], [['type'], 'in', 'range' => self::getTypesArray()], - [['trig_item_value', 'name', 'trig_time', 'trig_time_wdays'], 'string', 'max' => 255], - [['trig_time', 'trig_time_wdays', 'trig_date'], 'default', 'value' => null], + [['trig_item_value', 'name', 'trig_time'], 'string', 'max' => 255], + [['trig_time', 'trig_time_wdays', 'trig_item_value', 'trig_date'], 'default', 'value' => null], + [['trig_time_wdays'], 'each', 'rule' => ['in', 'range' => self::getWeekDaysArray()]], [['task_ids'], 'each', 'rule' => ['integer']], [['active'], 'boolean'], [['active'], 'default', 'value' => true], @@ -84,7 +86,7 @@ class Trigger extends ActiveRecord 'trig_item_id' => 'Элемент срабатывания', 'trig_item_value' => 'Значение элемента срабатывания', 'task_ids' => 'Задачи', - 'name' => 'Имя', + 'name' => 'Название', ]; } @@ -94,13 +96,38 @@ class Trigger extends ActiveRecord public static function getTypes() { return [ - self::TYPE_BY_ITEM_VALUE => 'Значение Элемента', - self::TYPE_BY_USER_ITEM_CHANGE => 'Изменение Значение Элемента', + self::TYPE_BY_ITEM_VALUE => 'Изменение Значения Элемента', + self::TYPE_BY_USER_ITEM_CHANGE => 'Изменение Пользователем Значения Элемента', self::TYPE_BY_DATE => 'Дата', self::TYPE_BY_TIME => 'Время', + self::TYPE_MANUAL => 'Вручную (API)', ]; } + /** + * @return array + */ + public static function getWeekDays() + { + return [ + 'Monday' => 'Понедельник', + 'Tuesday' => 'Вторник', + 'Wednesday' => 'Среда', + 'Thursday' => 'Четверг', + 'Friday' => 'Пятница', + 'Saturday' => 'Суббота', + 'Sunday' => 'Воскресенье', + ]; + } + + /** + * @return array + */ + public static function getWeekDaysArray() + { + return array_keys(self::getWeekDays()); + } + /** * @return array */ @@ -133,4 +160,20 @@ class Trigger extends ActiveRecord return $this->hasMany(Task::className(), ['id' => 'task_id']) ->viaTable('trigger_task', ['trigger_id' => 'id']); } + + /** + * @inheritdoc + */ + public function beforeSave($insert) + { + if (parent::beforeSave($insert)) { + if (is_array($this->trig_time_wdays)) { + $this->trig_time_wdays = implode($this->trig_time_wdays, ', '); + } + + return true; + } else { + return false; + } + } } diff --git a/modules/api/components/WebSocketAPI.php b/modules/api/components/WebSocketAPI.php index c62327b..08d9313 100644 --- a/modules/api/components/WebSocketAPI.php +++ b/modules/api/components/WebSocketAPI.php @@ -89,6 +89,35 @@ class WebSocketAPI ]); } + /** + * @param int $itemID + * @param string $mode + * @param bool $start + * @return bool + * @internal param array $rgbData + */ + public function rgbMode($itemID, $mode, $start) + { + return $this->send([ + 'type' => 'rgbMode', + 'item_id' => $itemID, + 'mode' => $mode, + 'start' => $start, + ]); + } + + /** + * @param int $triggerID + * @return bool + */ + public function trig($triggerID) + { + return $this->send([ + 'type' => 'trig', + 'trigger_id' => $triggerID, + ]); + } + /** * @param array $data * @return bool diff --git a/modules/api/controllers/TriggerController.php b/modules/api/controllers/TriggerController.php new file mode 100644 index 0000000..65efe44 --- /dev/null +++ b/modules/api/controllers/TriggerController.php @@ -0,0 +1,68 @@ + ['POST'], + ]; + } + + /** + * @throws NotSupportedException + */ + public function actionIndex() + { + throw new NotSupportedException(); + } + + /** + * @param int $trigger_id + * @return array + * @throws NotFoundHttpException + */ + public function actionTrig($trigger_id) + { + $trigger = $this->findTrigger($trigger_id); + + if ($trigger->type !== Trigger::TYPE_MANUAL) { + throw new InvalidParamException('This Trigger cannot be triggered by API call'); + } + + $api = new WebSocketAPI(Yii::$app->user->identity); + + return [ + 'success' => $api->trig($trigger_id), + ]; + } + + /** + * @param int $id + * @return Trigger + * @throws NotFoundHttpException + */ + private function findTrigger($id) + { + $item = Trigger::findOne($id); + + if (!$item) { + throw new NotFoundHttpException('Trigger was not found'); + } + + return $item; + } +}