From badabc42e968fecaa2446d545df8d6956e2a3811 Mon Sep 17 00:00:00 2001 From: Alex Solomaha Date: Tue, 28 Mar 2017 01:57:45 +0300 Subject: [PATCH] Added item value graphic --- assets/PanelAsset.php | 2 +- controllers/PanelController.php | 2 + controllers/ProfileController.php | 38 ++++++++++++++ modules/api/controllers/ItemController.php | 41 +++++++++++++++ views/panel/index.php | 18 +++++++ views/profile/edit.php | 9 ++++ web/js/panel.js | 58 ++++++++++++++++++++++ 7 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 views/profile/edit.php diff --git a/assets/PanelAsset.php b/assets/PanelAsset.php index 833d0ff..24da424 100644 --- a/assets/PanelAsset.php +++ b/assets/PanelAsset.php @@ -18,7 +18,7 @@ class PanelAsset extends AssetBundle 'app\assets\AppAsset', 'app\assets\HandlebarsAsset', 'app\assets\SpectrumAsset', -// 'app\assets\ChartjsAsset', + 'app\assets\ChartjsAsset', 'yii\web\JqueryAsset', ]; } diff --git a/controllers/PanelController.php b/controllers/PanelController.php index 93def03..370730a 100644 --- a/controllers/PanelController.php +++ b/controllers/PanelController.php @@ -5,6 +5,7 @@ namespace app\controllers; use app\models\Room; use Yii; use yii\filters\AccessControl; +use yii\helpers\Url; use yii\web\Controller; use yii\web\View; @@ -37,6 +38,7 @@ class PanelController extends Controller . '/?type=user&id=' . Yii::$app->user->identity->id . '&auth_token=' . Yii::$app->user->identity->getAuthToken() . '"; + var itemValueChartUrl = "' . Url::to(['/api/item/chart-data', 'access-token' => Yii::$app->user->identity->api_key]) . '" ', View::POS_HEAD); $roomModels = Room::find()->orderBy('sort_order')->all(); diff --git a/controllers/ProfileController.php b/controllers/ProfileController.php index 7812001..306adbc 100644 --- a/controllers/ProfileController.php +++ b/controllers/ProfileController.php @@ -4,10 +4,29 @@ namespace app\controllers; use app\models\User; use Yii; +use yii\filters\AccessControl; use yii\web\Controller; class ProfileController extends Controller { + /** + * @inheritdoc + */ + public function behaviors() + { + return [ + 'access' => [ + 'class' => AccessControl::className(), + 'rules' => [ + [ + 'allow' => true, + 'roles' => ['@'], + ], + ], + ], + ]; + } + public function actionIndex($id = false) { $user = $id ? User::findOne($id) : Yii::$app->user->identity; @@ -16,4 +35,23 @@ class ProfileController extends Controller 'user' => $user, ]); } + + public function actionEdit() + { + $user = Yii::$app->user->identity; + $user->scenario = User::SCENARIO_UPDATE; + + if ($user->load(Yii::$app->request->post())) { + $user->setPassword($user->password); + $user->generateAuthKey(); + + if ($user->save()) { + return $this->redirect(['index']); + } + } + + return $this->render('edit', [ + 'user' => $user, + ]); + } } diff --git a/modules/api/controllers/ItemController.php b/modules/api/controllers/ItemController.php index 14978d1..9caeef4 100644 --- a/modules/api/controllers/ItemController.php +++ b/modules/api/controllers/ItemController.php @@ -3,6 +3,7 @@ namespace app\modules\api\controllers; use app\models\Board; +use app\models\History; use app\models\Item; use app\modules\api\components\WebSocketAPIBridge; use Yii; @@ -258,6 +259,46 @@ class ItemController extends ActiveController } } + /** + * @param int $item_id + * @return array + * @throws BadRequestHttpException + */ + public function actionChartData($item_id) + { + $item = $this->findItem($item_id); + + if (!in_array($item->type, [ + Item::TYPE_VARIABLE, + Item::TYPE_VARIABLE_HUMIDITY, + Item::TYPE_VARIABLE_LIGHT, + Item::TYPE_VARIABLE_TEMPERATURE + ]) + ) { + throw new BadRequestHttpException('This item is not the variable one'); + } + + $historyModels = History::find() + ->where([ + 'type' => History::TYPE_ITEM_VALUE, + 'item_id' => $item->id, + ]) + ->andWhere(['>=', 'commited_at', time() - 21600]) + ->orderBy('commited_at DESC') + ->all(); + + $data = []; + + foreach ($historyModels as $historyModel) { + $data[$historyModel->commited_at] = $historyModel->value; + } + + return [ + 'success' => true, + 'data' => $data, + ]; + } + /** * @param int $id * @return Item diff --git a/views/panel/index.php b/views/panel/index.php index b968728..7902fcb 100644 --- a/views/panel/index.php +++ b/views/panel/index.php @@ -158,3 +158,21 @@ $this->title = 'Панель Управления'; + + + diff --git a/views/profile/edit.php b/views/profile/edit.php new file mode 100644 index 0000000..0f3e9e7 --- /dev/null +++ b/views/profile/edit.php @@ -0,0 +1,9 @@ +title = 'Редактировать профиль'; +?> diff --git a/web/js/panel.js b/web/js/panel.js index 2334a4e..f0ddc31 100644 --- a/web/js/panel.js +++ b/web/js/panel.js @@ -1,4 +1,5 @@ var wsURL; +var itemValueChartUrl; var WS; var WSConnectionOpened; @@ -217,6 +218,63 @@ $(document).ready(function () { $('.panel-item-variable').tooltip(); + // Value widget + $('body').on('click', '.panel-item-variable', function () { + var $modal = $('#item-chart-modal'); + var $this = $(this); + + var title = $this.data('original-title'); + var itemId = $this.data('item-id'); + + var data; + + $.ajax({ + url: itemValueChartUrl + '&item_id=' + itemId, + dataType: 'json', + }).success(function (result) { + $modal.find('.item-chart-name').html(title); + var $canvas = $modal.find('#item-chart'); + + Date.prototype.formatMMDDYYYY = function () { + return this.getHours() + ':' + this.getMinutes() + ':' + this.getSeconds(); + }; + + // Split timestamp and data into separate arrays + var labels = [], + data = []; + + $.each(result.data, function (key, value) { + console.log(key); + labels.push(new Date(key * 1000).formatMMDDYYYY()); + data.push(parseInt(value)); + }); + + // Create the chart.js data structure using 'labels' and 'data' + var tempData = { + labels: labels, + datasets: [{ + label: title, + data: data, + backgroundColor: 'transparent', + borderColor: '#009688', + }], + }; + + // Get the context of the canvas element we want to select + var ctx = $canvas.get(0).getContext("2d"); + + // Instantiate a new chart + var myLineChart = new Chart(ctx, { + type: 'line', + data: tempData + }); + + $modal.modal('show'); + }).fail(function () { + showErrorMessage('Не удалось получить данные'); + }); + }); + // RGB Widget $('.panel-item-rgb') .popover({