Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃敤 Allow calculValueOffset/evaluateExpression ("Formule de calcul") on every info cmd #2146

Merged
merged 2 commits into from Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 16 additions & 27 deletions core/class/cmd.class.php
Expand Up @@ -903,29 +903,30 @@ public function formatValue($_value, $_quote = false) {
return $_value;
}
if ($this->getType() == 'info') {
if ($this->getSubType() == 'numeric') { // Handle comma instead of period in a float value
$_value = floatval(str_replace(',', '.', $_value));
}
$calc = $this->getConfiguration('calculValueOffset');
if ($calc != '') {
try {
if (preg_match("/[a-zA-Z#]/", $_value)) { // Value is not just a number
$calc = str_replace('#value#', '"' . $_value . '"', str_replace('\'#value#\'', '#value#', str_replace('"#value#"', '#value#', $calc)));
} else { // Value is a number
$calc = str_replace('#value#', $_value, $calc);
}
$_value = jeedom::evaluateExpression($calc);
} catch (Exception $ex) {
} catch (Error $ex) {
}
}
switch ($this->getSubType()) {
case 'string':
if ($_quote) {
return '"' . $_value . '"';
}
return $_value;
case 'other':
if ($_quote) {
return '"' . $_value . '"';
}
return $_value;
case 'binary':
if ($this->getConfiguration('calculValueOffset') != '') {
try {
if (preg_match("/[a-zA-Z#]/", $_value)) {
$_value = jeedom::evaluateExpression(str_replace('#value#', '"' . $_value . '"', str_replace('\'#value#\'', '#value#', str_replace('"#value#"', '#value#', $this->getConfiguration('calculValueOffset')))));
} else {
$_value = jeedom::evaluateExpression(str_replace('#value#', $_value, $this->getConfiguration('calculValueOffset')));
}
} catch (Exception $ex) {
} catch (Error $ex) {
}
}
if ($_value === true || $_value === 1) { // Handle literal values
$binary = true;
} elseif ((is_numeric(intval($_value)) && intval($_value) >= 1)) { // Handle number and numeric string
Expand All @@ -938,18 +939,6 @@ public function formatValue($_value, $_quote = false) {
// Return int value negated according to invertBinary configuration
return intval($binary xor boolval($this->getConfiguration('invertBinary', false)));
case 'numeric':
$_value = floatval(str_replace(',', '.', $_value));
if ($this->getConfiguration('calculValueOffset') != '') {
try {
if (preg_match("/[a-zA-Z#]/", $_value)) {
$_value = jeedom::evaluateExpression(str_replace('#value#', '"' . $_value . '"', str_replace('\'#value#\'', '#value#', str_replace('"#value#"', '#value#', $this->getConfiguration('calculValueOffset')))));
} else {
$_value = jeedom::evaluateExpression(str_replace('#value#', $_value, $this->getConfiguration('calculValueOffset')));
}
} catch (Exception $ex) {
} catch (Error $ex) {
}
}
if ($this->getConfiguration('historizeRound') !== '' && is_numeric($this->getConfiguration('historizeRound')) && $this->getConfiguration('historizeRound') >= 0) {
$_value = round($_value, $this->getConfiguration('historizeRound'));
}
Expand Down
4 changes: 2 additions & 2 deletions desktop/modal/cmd.configure.php
Expand Up @@ -341,7 +341,7 @@
<br />
<form class="form-horizontal">
<fieldset>
<?php if (($cmd->getType() == 'info' && ($cmd->getSubType() == 'numeric' || $cmd->getSubType() == 'binary')) || ($cmd->getType() == 'action' && ($cmd->getSubType() == 'slider'))) {
<?php if (($cmd->getType() == 'info') || ($cmd->getType() == 'action' && ($cmd->getSubType() == 'slider'))) {
?>
<legend><i class="fas fa-table"></i> {{Calcul et arrondi}}</legend>
<div class="form-group">
Expand Down Expand Up @@ -1447,4 +1447,4 @@ function addActionCmd(_action, _type, _name) {
cmd.empty().append(_options.display_value + ' ' + _options.unit + ' {{le}} ' + _options.valueDate);
});
})
</script>
</script>