Added item value graphic

This commit is contained in:
Alex Solomaha
2017-03-28 01:57:45 +03:00
parent d9ba6ae656
commit badabc42e9
7 changed files with 167 additions and 1 deletions

View File

@@ -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