Skip to content

Commit

Permalink
Merge pull request #2307 from tysauron/beta
Browse files Browse the repository at this point in the history
Add jeedom.history.getLast function
  • Loading branch information
zoic21 committed Oct 5, 2023
2 parents 8e04b18 + f9603f5 commit 77b63f1
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
8 changes: 8 additions & 0 deletions core/ajax/cmd.ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,14 @@
ajax::success($return);
}

if (init('action') == 'getLastHistory') {
if(is_numeric(init('id'))){
$date = date('Y-m-d H:i:s', strtotime(init('time')));
ajax::success(history::lastFromDate(init('id'), $date));
}
return ajax::success(0);
}

if (init('action') == 'emptyHistory') {
if (!isConnect('admin')) {
throw new Exception(__('401 - Accès non autorisé', __FILE__), -1234);
Expand Down
50 changes: 49 additions & 1 deletion core/class/history.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,55 @@ public static function emptyHistory($_cmd_id, $_date = '') {
$sql .= ' AND `datetime` <= :date';
}
return DB::Prepare($sql, $values, DB::FETCH_TYPE_ROW);
}
}

public static function lastFromDate($_cmd_id, $_time){
$cmd = cmd::byId($_cmd_id);
if(is_object($cmd) && $cmd->getIsHistorized() == 1 && !$cmd->getConfiguration('isHistorizedCalc', 0)){
$values = array(
'cmd_id' => $_cmd_id,
'time' => $_time
);

$sql = 'SELECT (CAST(value AS DECIMAL(12,2))) as value
FROM (
(SELECT value, datetime from history WHERE value is not null AND cmd_id=:cmd_id AND datetime<=:time ORDER by datetime DESC LIMIT 0,1)
UNION ALL
(SELECT value, datetime from historyArch WHERE value is not null AND cmd_id=:cmd_id AND datetime<=:time ORDER by datetime DESC LIMIT 0,1)
)a
ORDER by datetime DESC LIMIT 0,1';

$return = DB::Prepare($sql, $values, DB::FETCH_TYPE_ALL, PDO::FETCH_CLASS, __CLASS__);
if(isset($return[0]))
return array('unite' => $cmd->getUnite(), 'value' => $return[0]->getValue());
}
elseif(is_object($cmd)){
return array('unite' => $cmd->getUnite(), 'value' =>history::getLastHistoryFromCalcul (jeedom::fromHumanReadable($cmd->getConfiguration('calcul')), $_time));
}
return array('unite' => $cmd->getUnite(), 'value' => 0);
}

public static function getLastHistoryFromCalcul($_strcalcul, $_time){
$cmd_histories = array();
preg_match_all("/#([0-9]*)#/", $_strcalcul, $matches);
if (count($matches[1]) > 0) {
foreach ($matches[1] as $cmd_id) {
if (is_numeric($cmd_id)) {
$cmd = cmd::byId($cmd_id);
if (is_object($cmd) && $cmd->getIsHistorized() == 1 && !$cmd->getConfiguration('isHistorizedCalc', 0)) {

$cmd_histories['#' . $cmd_id . '#'] = history::lastFromDate($cmd_id, $_time);
}
elseif(is_object($cmd)){
$cmd_histories['#' . $cmd_id . '#'] = history::getLastHistoryFromCalcul(jeedom::fromHumanReadable($cmd->getConfiguration('calcul')), $_time);
}
}
}
}
// echo '<br/>uuu'.template_replace($cmd_histories, $_strcalcul).' pour : '.$_strcalcul;
$calcul = template_replace($cmd_histories, $_strcalcul);
return floatval(jeedom::evaluateExpression($calcul));;
}

public static function getHistoryFromCalcul($_strcalcul, $_dateStart = null, $_dateEnd = null, $_noCalcul = false, $_groupingType = null) {
$now = strtotime('now');
Expand Down
28 changes: 28 additions & 0 deletions core/js/history.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,34 @@ jeedom.history.get = function(_params) {
domUtils.ajax(paramsAJAX);
}

jeedom.history.getLast = function(_params) {
var paramsRequired = ['cmd_id', 'time'];
var paramsSpecifics = {
global: _params.global || true,
pre_success: function(data) {
if (isset(jeedom.cmd.cache.byId[data.result.id])) {
delete jeedom.cmd.cache.byId[data.result.id];
}
return data;
}
};
try {
jeedom.private.checkParamsRequired(_params || {}, paramsRequired);
} catch (e) {
(_params.error || paramsSpecifics.error || jeedom.private.default_params.error)(e);
return;
}
var params = $.extend({}, jeedom.private.default_params, paramsSpecifics, _params || {});
var paramsAJAX = jeedom.private.getParamsAJAX(params);
paramsAJAX.url = 'core/ajax/cmd.ajax.php';
paramsAJAX.data = {
action: 'getLastHistory',
id: _params.cmd_id,
time: _params.time || '',
};
$.ajax(paramsAJAX);
}

jeedom.history.getInitDates = function(_params) {
var paramsRequired = [];
var paramsSpecifics = {};
Expand Down

0 comments on commit 77b63f1

Please sign in to comment.