mirror of
https://github.com/CyanoFresh/SmartHomePHP.git
synced 2026-02-19 19:01:20 +01:00
Added item value graphic
This commit is contained in:
@@ -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',
|
||||
];
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -158,3 +158,21 @@ $this->title = 'Панель Управления';
|
||||
</div>
|
||||
</div>
|
||||
</script>
|
||||
|
||||
<!-- Modal -->
|
||||
<div class="modal fade" id="item-chart-modal" tabindex="-1" role="dialog" aria-labelledby="item-chart-modal-label">
|
||||
<div class="modal-dialog" role="document">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
|
||||
<h4 class="modal-title" id="item-chart-modal-label">График элемента <span class="item-chart-name"></span></h4>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<canvas id="item-chart"></canvas>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-default" data-dismiss="modal">Закрыть</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
9
views/profile/edit.php
Normal file
9
views/profile/edit.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
/** @var $this \yii\web\View */
|
||||
/** @var $user \app\models\User */
|
||||
|
||||
use yii\helpers\Url;
|
||||
|
||||
$this->title = 'Редактировать профиль';
|
||||
?>
|
||||
@@ -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({
|
||||
|
||||
Reference in New Issue
Block a user