diff --git a/modules/admin/views/trigger/index.php b/modules/admin/views/trigger/index.php index 82f60e4..d57a885 100644 --- a/modules/admin/views/trigger/index.php +++ b/modules/admin/views/trigger/index.php @@ -16,6 +16,14 @@ $this->params['breadcrumbs'][] = $this->title;
= Html::a('Добавить', ['create'], ['class' => 'btn btn-success']) ?> + = Html::a('Обновить', [ + '/api/panel/schedule-triggers', + 'access-token' => Yii::$app->user->identity->api_key + ], [ + 'class' => 'btn btn-default schedule-triggers', + ], [ + 'sdfsdfsd' => 'sdfsdf', + ]) ?>
diff --git a/modules/api/components/WebSocketAPI.php b/modules/api/components/WebSocketAPI.php index 068d2b4..71b77ae 100644 --- a/modules/api/components/WebSocketAPI.php +++ b/modules/api/components/WebSocketAPI.php @@ -19,7 +19,7 @@ class WebSocketAPI /** * @var string */ - public $localWSSUrl; + protected $localWSSUrl; /** * WebSocketAPI constructor. @@ -72,31 +72,30 @@ class WebSocketAPI * @param array $data * @return bool */ - protected function send($data) + public function send($data) { $loop = Factory::create(); $connector = new Connector($loop); - $afterConnect = function (WebSocket $conn) use ($data) { - // Send data - $conn->send(Json::encode($data)); - - // Job done. Close the connection - $conn->close(); - - return true; - }; - - $afterDisconnect = function(\Exception $e) use ($loop) { - echo "Could not connect: {$e->getMessage()}\n"; - $loop->stop(); - }; + $success = false; $connector($this->getWSSUrl(), [], ['Origin' => 'origin']) - ->then($afterConnect, $afterDisconnect); + ->then(function (WebSocket $conn) use ($data, &$success) { + // Send data + $conn->send(Json::encode($data)); + + // Job done. Close the connection + $conn->close(); + + $success = true; + }, function(\Exception $e) use ($loop) { + echo "Could not connect: {$e->getMessage()}\n"; + + $loop->stop(); + }); $loop->run(); - return ($afterConnect == true and $afterDisconnect == false); + return $success; } } diff --git a/modules/api/controllers/PanelController.php b/modules/api/controllers/PanelController.php new file mode 100644 index 0000000..782110d --- /dev/null +++ b/modules/api/controllers/PanelController.php @@ -0,0 +1,45 @@ + ['POST'], + ]; + } + + /** + * @throws NotSupportedException + */ + public function actionIndex() + { + throw new NotSupportedException(); + } + + /** + * @return array|bool + */ + public function actionScheduleTriggers() + { + $api = new WebSocketAPI(Yii::$app->user->identity); + + $result = $api->send([ + 'type' => 'schedule-triggers', + ]); + + return [ + 'success' => $result, + ]; + } +} diff --git a/servers/Panel.php b/servers/Panel.php index e58c5d7..305b407 100644 --- a/servers/Panel.php +++ b/servers/Panel.php @@ -150,13 +150,21 @@ class Panel implements MessageComponentInterface return $conn->close(); } + // API request + $api = false; + + if ($conn->remoteAddress && $conn->remoteAddress == '127.0.0.1') { + $api = true; + } + // Close previous connection - if (isset($this->user_clients[$user->id])) { + if (isset($this->user_clients[$user->id]) && !$api) { $this->user_clients[$user->id]->close(); } // Attach to users $conn->User = $user; + $conn->api = $api; $this->user_clients[$user->id] = $conn; $conn->send(Json::encode([ @@ -203,29 +211,6 @@ class Panel implements MessageComponentInterface $this->logBoardConnection($board, true); return $this->log("Connected board [{$board->id}]"); - case 'api_user': - $this->log('Connection type is API User. Authenticating...'); - - $userID = $query->get('id'); - $userAuthKey = $query->get('auth_key'); - - if (!$userID or !$userAuthKey) { - return $conn->close(); - } - - $user = User::findOne([ - 'id' => $userID, - 'auth_key' => $userAuthKey, - ]); - - if (!$user) { - return $conn->close(); - } - - $conn->api_user = true; - $conn->User = $user; - - return $this->log("Connected API User [{$user->id}]"); } return $this->log('Connection has unknown type. Disconnect'); @@ -249,10 +234,6 @@ class Panel implements MessageComponentInterface public function onClose(ConnectionInterface $conn) { if (isset($conn->User)) { - if (!$conn->api_user) { - unset($this->user_clients[$conn->User->id]); - } - $conn->User->generateAuthKey(); $conn->User->save(); @@ -317,6 +298,8 @@ class Panel implements MessageComponentInterface return $this->handleRgb($from, $user, $data); case 'rgbMode': return $this->handleRgbMode($from, $user, $data); + case 'schedule-triggers': + return $this->handleScheduleTriggers($from, $user, $data); } return $this->log("Unknown command from user: $msg"); @@ -364,24 +347,6 @@ class Panel implements MessageComponentInterface $value = $this->valueToBoolean($data['value']); } - if ($item->type === Item::TYPE_VARIABLE_BOOLEAN_DOOR) { - curl_setopt_array($ch = curl_init(), array( - CURLOPT_URL => 'https://pushall.ru/api.php', - CURLOPT_POSTFIELDS => array( - 'type' => 'self', - 'id' => Yii::$app->params['pushAllID'], - 'key' => Yii::$app->params['pushAllKey'], - 'text' => $value ? 'Дверь открыта' : 'Дверь закрыта', - 'title' => 'Сигнализация на двери' - ), - CURLOPT_RETURNTRANSFER => true - )); - $return = curl_exec($ch); //получить ответ или ошибку - curl_close($ch); - - $this->log($return); - } - if ($item->type === Item::TYPE_RGB) { $value = explode(',', $value); @@ -649,13 +614,6 @@ class Panel implements MessageComponentInterface $this->items[$item->id]['value'] = $rgbArray; -// $this->sendUsers([ -// 'type' => 'value', -// 'item_id' => $item->id, -// 'item_type' => Item::TYPE_RGB, -// 'value' => $rgbArray, -// ]); - $history = new History(); $history->type = History::TYPE_USER_ACTION; $history->user_id = $user->id; @@ -744,6 +702,16 @@ class Panel implements MessageComponentInterface return true; } + /** + * @param ConnectionInterface $from + * @param User $user + * @param array $data + */ + private function handleScheduleTriggers($from, $user, $data) + { + return $this->scheduleTriggers(); + } + /** * Send data to all users * @@ -879,6 +847,11 @@ class Panel implements MessageComponentInterface */ protected function logItemValue($item, $value) { + if (!$item->enable_log) { + $this->log("Logging for this item is disabled"); + return; + } + if ($item->type === Item::TYPE_RGB) { $value = implode(',', $value); } diff --git a/web/js/site.js b/web/js/site.js index e69de29..f4383d3 100644 --- a/web/js/site.js +++ b/web/js/site.js @@ -0,0 +1,5 @@ +$('.schedule-triggers').click(function (e) { + e.preventDefault(); + + $.post($(this).attr('href')); +}); diff --git a/web/js/wsclient.js b/web/js/wsclient.js index b600512..9015645 100644 --- a/web/js/wsclient.js +++ b/web/js/wsclient.js @@ -11,6 +11,7 @@ function showErrorMessage(message) { return noty({ layout: 'bottomRight', text: message, + timeout: 5000, type: 'error', theme: 'relax' }); @@ -20,6 +21,7 @@ function showSuccessMessage(message) { return noty({ layout: 'bottomRight', text: message, + timeout: 5000, type: 'success', theme: 'relax' });