diff --git a/modules/api/components/WebSocketAPI.php b/modules/api/components/WebSocketAPI.php index 71b77ae..c62327b 100644 --- a/modules/api/components/WebSocketAPI.php +++ b/modules/api/components/WebSocketAPI.php @@ -41,7 +41,7 @@ class WebSocketAPI */ protected function getWSSUrl() { - return $this->localWSSUrl . '/?type=user&id=' . $this->user->id . '&auth_key=' . $this->user->auth_key; + return $this->localWSSUrl . '/?type=user&id=' . $this->user->id . '&auth_token=' . $this->user->getAuthToken(); } /** @@ -68,6 +68,27 @@ class WebSocketAPI ]); } + /** + * @param int $itemID + * @param int $red + * @param int $green + * @param int $blue + * @param boolean $fade + * @return bool + * @internal param array $rgbData + */ + public function rgb($itemID, $red, $green, $blue, $fade) + { + return $this->send([ + 'type' => 'rgb', + 'item_id' => $itemID, + 'red' => $red, + 'green' => $green, + 'blue' => $blue, + 'fade' => $fade, + ]); + } + /** * @param array $data * @return bool diff --git a/modules/api/controllers/ItemController.php b/modules/api/controllers/ItemController.php index 10ba9b0..5a8f2cc 100644 --- a/modules/api/controllers/ItemController.php +++ b/modules/api/controllers/ItemController.php @@ -10,6 +10,7 @@ use yii\base\NotSupportedException; use yii\rest\Controller; use yii\web\BadRequestHttpException; use yii\web\NotFoundHttpException; +use yii\web\ServerErrorHttpException; class ItemController extends Controller { @@ -107,6 +108,42 @@ class ItemController extends Controller ]; } + /** + * @param int $item_id + * @param int $red + * @param int $green + * @param int $blue + * @param bool $fade + * @return array + * @throws BadRequestHttpException + * @throws NotSupportedException + * @throws ServerErrorHttpException + */ + public function actionRgb($item_id, $red = 0, $green = 0, $blue = 0, $fade = false) + { + $item = $this->findItem($item_id); + + if ($item->type !== Item::TYPE_RGB) { + throw new BadRequestHttpException(); + } + + $board = $item->board; + + switch ($board->type) { + case Board::TYPE_AREST: + throw new NotSupportedException(); + + case Board::TYPE_WEBSOCKET: + $api = new WebSocketAPI(Yii::$app->user->identity); + + return [ + 'success' => $api->rgb($item_id, $red, $green, $blue, $fade), + ]; + default: + throw new ServerErrorHttpException(); + } + } + /** * @param int $id * @return Item