Skip to content

Commit

Permalink
EQ removal update: display how it used first and ask user confirmation
Browse files Browse the repository at this point in the history
  • Loading branch information
tcharp38 committed Nov 10, 2023
1 parent 971e970 commit 68a429e
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 71 deletions.
42 changes: 21 additions & 21 deletions core/ajax/Abeille.ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,27 +298,27 @@ function sendToCmd($topic, $payload = '') {
ajax::success(json_encode(array('status' => $status)));
}

/* Developer feature: Remove equipment(s) listed by id in 'eqList', from Jeedom DB.
Zigate is untouched.
Returns: status=0/-1, errors=<error message(s)> */
if (init('action') == 'removeEqJeedom') {
$eqList = init('eqList');

$status = 0;
$errors = ""; // Error messages
foreach ($eqList as $eqId) {
/* Collecting required infos */
$eqLogic = eqLogic::byId($eqId);
if (!is_object($eqLogic)) {
throw new Exception(__('EqLogic inconnu. Vérifiez l\'ID', __FILE__).' '.$eqId);
}

/* Removing device from Jeedom DB */
$eqLogic->remove();
}

ajax::success(json_encode(array('status' => $status, 'errors' => $errors)));
}
// /* Developer feature: Remove equipment(s) listed by id in 'eqList', from Jeedom DB.
// Zigate is untouched.
// Returns: status=0/-1, errors=<error message(s)> */
// if (init('action') == 'removeEqJeedom') {
// $eqList = init('eqList');

// $status = 0;
// $errors = ""; // Error messages
// foreach ($eqList as $eqId) {
// /* Collecting required infos */
// $eqLogic = eqLogic::byId($eqId);
// if (!is_object($eqLogic)) {
// throw new Exception(__('EqLogic inconnu. Vérifiez l\'ID', __FILE__).' '.$eqId);
// }

// /* Removing device from Jeedom DB */
// $eqLogic->remove();
// }

// ajax::success(json_encode(array('status' => $status, 'errors' => $errors)));
// }

/* Remove equipment(s) from zigbee listed by id in 'eqIdList'.
Returns: status=0/-1, errors=<error message(s)> */
Expand Down
214 changes: 165 additions & 49 deletions desktop/js/Abeille.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,68 +320,184 @@ function getSelectedEqs(zgId) {
}

/* Removes selected equipments for given zigate nb from Jeedom DB only. Zigate is untouched. */
function removeBeesJeedom(zgId) {
console.log("removeBeesJeedom(zgId=" + zgId + ")");
// function removeBeesJeedom(zgId) {
// console.log("removeBeesJeedom(zgId=" + zgId + ")");

// /* Any selected ? */
// var sel = getSelectedEqs(zgId);
// console.log(sel);
// if (sel["nb"] == 0) {
// alert("Aucun équipement sélectionné !");
// return;
// }
// var eqIdList = sel["ids"];
// console.log("eqIdList=" + eqIdList);

// var msg =
// "{{Vous êtes sur le point de supprimer les équipements selectionnés de Jeedom.";
// msg +=
// "<br><br>Si ils sont toujours dans le réseau, ils deviendront 'fantomes' et devraient être réinclus automatiquement au fur et à mesure de leur reveil et ce, tant qu'on ne les force pas à quitter le réseau.";
// msg += "<br><br>Etes vous sur de vouloir continuer ?}}";
// bootbox.confirm(msg, function (result) {
// if (result == false) return;

// // Collecting addresses before EQ is removed
// var eqAddrList = sel["addrs"];
// console.log("eqAddrList=" + eqAddrList);

// $.ajax({
// type: "POST",
// url: "plugins/Abeille/core/ajax/Abeille.ajax.php",
// data: {
// action: "removeEqJeedom",
// eqList: eqIdList,
// },
// dataType: "json",
// global: false,
// error: function (request, status, error) {
// bootbox.alert("ERREUR 'removeEqJeedom' !");
// },
// success: function (json_res) {
// window.location.reload();
// res = JSON.parse(json_res.result);
// if (res.status != 0) {
// var msg =
// "ERREUR ! Quelque chose s'est mal passé.\n" +
// res.errors;
// alert(msg);
// } else {
// // Informing parser that some equipements have to be considered "phantom"
// var xhr = new XMLHttpRequest();
// xhr.open(
// "GET",
// "plugins/Abeille/core/php/AbeilleCliToQueue.php?action=sendMsg&queueId=" +
// js_queueXToParser +
// "&msg=type:eqRemoved_net:Abeille" +
// zgId +
// "_eqList:" +
// eqAddrList,
// true
// );
// xhr.send();
// }
// },
// });
// });
// }

/* Removes selected equipments for given zigate nb from Jeedom DB only. Zigate is untouched. */
function removeSelectedEq(zgId) {
console.log("removeSelectedEq(zgId=" + zgId + ")");

/* Any selected ? */
var sel = getSelectedEqs(zgId);
console.log(sel);
if (sel["nb"] == 0) {
alert("Aucun équipement sélectionné !");
alert("{{Aucun équipement sélectionné !}}");
return;
}
var eqIdList = sel["ids"];
console.log("eqIdList=" + eqIdList);

var msg =
"{{Vous êtes sur le point de supprimer les équipements selectionnés de Jeedom.";
msg +=
"<br><br>Si ils sont toujours dans le réseau, ils deviendront 'fantomes' et devraient être réinclus automatiquement au fur et à mesure de leur reveil et ce, tant qu'on ne les force pas à quitter le réseau.";
msg += "<br><br>Etes vous sur de vouloir continuer ?}}";
bootbox.confirm(msg, function (result) {
if (result == false) return;
for (const eqId of eqIdList) removeEq(zgId, eqId);
}

// Collecting addresses before EQ is removed
var eqAddrList = sel["addrs"];
console.log("eqAddrList=" + eqAddrList);
/* Remove from Jeedom eq with ID 'eqId' but list first how it is used and ask confirmation to user */
function removeEq(zgId, eqId) {
console.log("removeEq(" + eqId + ")");

$.ajax({
type: "POST",
url: "plugins/Abeille/core/ajax/Abeille.ajax.php",
data: {
action: "removeEqJeedom",
eqList: eqIdList,
},
dataType: "json",
global: false,
error: function (request, status, error) {
bootbox.alert("ERREUR 'removeEqJeedom' !");
},
success: function (json_res) {
window.location.reload();
res = JSON.parse(json_res.result);
if (res.status != 0) {
var msg =
"ERREUR ! Quelque chose s'est mal passé.\n" +
res.errors;
alert(msg);
} else {
// Informing parser that some equipements have to be considered "phantom"
var xhr = new XMLHttpRequest();
xhr.open(
"GET",
"plugins/Abeille/core/php/AbeilleCliToQueue.php?action=sendMsg&queueId=" +
js_queueXToParser +
"&msg=type:eqRemoved_net:Abeille" +
zgId +
"_eqList:" +
eqAddrList,
true
);
xhr.send();
eval("var eqPerZigate = JSON.parse(js_eqPerZigate);");
eqName = eqPerZigate[zgId][eqId]["name"];
eqAddr = eqPerZigate[zgId][eqId]["addr"];
eqAddrList = new Array();
eqAddrList.push(eqAddr);

jeedom.eqLogic.getUseBeforeRemove({
id: eqId,
error: function (error) {
$.fn.showAlert({
message: error.message,
level: "danger",
});
},
success: function (data) {
var text =
"{{Êtes-vous sûr de vouloir supprimer l'équipement}} Abeille <b>" +
eqName +
"</b> ?";
if (Object.keys(data).length > 0) {
text += " </br> {{Il est utilisé par ou utilise :}}</br>";
var complement = null;
for (var i in data) {
complement = "";
if ("sourceName" in data[i]) {
complement = " (" + data[i].sourceName + ")";
}
text +=
"- " +
'<a href="' +
data[i].url +
'" target="_blank">' +
data[i].type +
"</a> : <b>" +
data[i].name +
"</b>" +
complement +
' <sup><a href="' +
data[i].url +
'" target="_blank"><i class="fas fa-external-link-alt"></i></a></sup></br>';
}
},
});
}
text = text.substring(0, text.length - 2);
bootbox.confirm(text, function (result) {
if (result) {
jeedom.eqLogic.remove({
type: "Abeille",
id: eqId,
error: function (error) {
$.fn.showAlert({
message: error.message,
level: "danger",
});
},
success: function () {
// Inform parser & cmd that this equipement has been removed
var xhr = new XMLHttpRequest();
xhr.open(
"POST",
"plugins/Abeille/core/php/AbeilleCliToQueue.php?action=sendMsg&queueId=" +
js_queueXToParser +
"&msg=type:eqRemoved_net:Abeille" +
zgId +
"_eqList:" +
eqAddrList,
true
);
xhr.send();

var vars = getUrlVars();
var url = "index.php?";
for (var i in vars) {
if (
i != "id" &&
i != "removeSuccessFull" &&
i != "saveSuccessFull"
) {
url +=
i +
"=" +
vars[i].replace("#", "") +
"&";
}
}
modifyWithoutSave = false;
url += "removeSuccessFull=1";
jeedomUtils.loadPage(url);
},
});
}
});
},
});
}

Expand Down
3 changes: 2 additions & 1 deletion desktop/php/Abeille-Bees.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ function displayBeeCard($eqLogic, $files, $zgId) {
echo '<span class="cursor" onclick="createRemote('.$zgId.')" title="Clic pour créer une télécommande virtuelle."><i class="fas fa-gamepad" style="font-size:160%;color:orange !important;"></i></span>';

/* Remove equipments from Jeedom only */
echo '<a onclick="removeBeesJeedom('.$zgId.')" class="btn btn-warning btn-xs" style="margin-top: -10px; margin-left:15px" title="Supprime les équipement(s) sélectionné(s) de Jeedom uniquement.">{{Supprimer de Jeedom}}</a>';
// echo '<a onclick="removeBeesJeedom('.$zgId.')" class="btn btn-warning btn-xs" style="margin-top: -10px; margin-left:15px" title="Supprime les équipement(s) sélectionné(s) de Jeedom uniquement.">{{Supprimer de Jeedom}}</a>';
echo '<a onclick="removeSelectedEq('.$zgId.')" class="btn btn-warning btn-xs" style="margin-top: -10px; margin-left:15px" title="{{Supprime les équipements sélectionnés de Jeedom uniquement}}">{{Supprimer de Jeedom}}</a>';

/* Exclude feature */
echo '<a onclick="removeBees('.$zgId.')" class="btn btn-warning btn-xs" style="margin-top:-10px; margin-left:8px" title="Demande aux équipement(s) sélectionné(s) de sortir du réseau.">{{Exclure}}</a>';
Expand Down
1 change: 1 addition & 0 deletions desktop/php/Abeille.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
$eq['mainEp'] = $eqLogic->getConfiguration('mainEP', '');
$eqModel = $eqLogic->getConfiguration('ab::eqModel', null);
$eq['jsonId'] = $eqModel ? $eqModel['id'] : '';
$eq['name'] = $eqLogic->getName();
if ($eqAddr == "0000") {
if (isset($eqPerZigate[$zgId][$eqId]))
array_unshift($eqPerZigate[$zgId][$eqId], $eq);
Expand Down
1 change: 1 addition & 0 deletions docs/fr_FR/Changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ ChangeLog
- Abeille remote control: Commandes 'on all' & 'off all' cachées par défaut. Trop dangereux.
- Interne: Mise-à-jour 'getDevicesList()' + 'getDeviceModel()'.
- Reset de modele: Correction pour signatures alternatives.
- Suppression d'un équipement: Correction permettant d'affichage comment il est utilisé avant suppression (2652).

231107-BETA-2
-------------
Expand Down

0 comments on commit 68a429e

Please sign in to comment.