/* This file is part of Jeedom.
*
* Jeedom is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Jeedom is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Jeedom. If not, see .
*/
$("#table_cmd").sortable({axis: "y", cursor: "move", items: ".cmd", placeholder: "ui-state-highlight", tolerance: "intersect", forcePlaceholderSize: true});
$('#bt_healthSwitchbot').on('click', function () {
$('#md_modal').dialog({title: "{{Santé Switchbot}}"});
$('#md_modal').load('index.php?v=d&plugin=switchbot&modal=health').dialog('open');
});
$('.eqLogicAction[data-action=addEquipment]').off('click').on('click', function () {
var message = `"
"`;
bootbox.confirm( message,
function (result) {
if (result == false) {
return;
}
if( !$('#name').val()) {
$('#div_alert').showAlert({message: '{{Il faut renseigner un nom à l\'équipement}}', level: 'danger'});
return;
}
var name = $('#name').val(),
type = $('#sel_type').find(":selected").val();;
jeedom.eqLogic.save({
type: eqType,
eqLogics: [{name: name,configuration: {'type':type}}],
error: function (error) {
$('#div_alert').showAlert({message: error.message, level: 'danger'});
},
success: function (data) {
modifyWithoutSave = false;
var vars = getUrlVars();
var url = 'index.php?';
for (var i in vars) {
if (i != 'id' && i != 'saveSuccessFull' && i != 'removeSuccessFull') {
url += i + '=' + vars[i].replace('#', '') + '&';
}
}
url += 'id=' + data.id + '&saveSuccessFull=1';
if (document.location.toString().match('#')) {
url += '#' + document.location.toString().split('#')[1];
}
loadPage(url);
modifyWithoutSave = false;
}
});
}
);
});
function getHtmlCode(callback,_cond){
$.ajax({
url: 'plugins/switchbot/core/config/html/'+ _cond + '.html',
async:false,
dataType: 'html',
type: 'GET',
success: callback
});
}
$('.eqLogicAction[data-action=syncSwitchbot]').off('click').on('click', function () {
$.ajax({// fonction permettant de faire de l'ajax
type: "POST", // methode de transmission des données au fichier php
url: "plugins/switchbot/core/ajax/switchbot.ajax.php", // url du fichier php
data: {
action: "syncSwitchbot"
},
dataType: 'json',
error: function (request, status, error) {
$('#div_alert').showAlert({message: error.message, level: 'danger'});
},
success: function (data) { // si l'appel a bien fonctionné
loadPage('index.php?v=d&m=switchbot&p=switchbot');
}
});
});
$('.switchbotRemoteAction[data-action=sendFiles]').on('click',function(){
$.ajax({
type: "POST",
url: "plugins/switchbot/core/ajax/switchbot.ajax.php",
data: {
action: "sendRemoteFiles",
id: $('.eqLogicAttr[data-l1key=id]').val()
},
dataType: 'json',
error: function (request, status, error) {
handleAjaxError(request, status, error,$('#div_alert'));
},
success: function (data) {
if (data.state != 'ok') {
$('#div_alert').showAlert({message: error.message, level: 'danger'});
return;
}
$('#div_alert').showAlert({message: '{{Envoi réussi}}', level: 'success'});
}
});
});
$('.switchbotRemoteAction[data-action=dependancyRemote]').on('click',function(){
$.ajax({
type: "POST",
url: "plugins/switchbot/core/ajax/switchbot.ajax.php",
data: {
action: "dependancyRemote",
id: $('.eqLogicAttr[data-l1key=id]').val()
},
dataType: 'json',
error: function (request, status, error) {
handleAjaxError(request, status, error,$('#div_alert'));
},
success: function (data) {
if (data.state != 'ok') {
$('#div_alert').showAlert({message: data.result, level: 'danger'});
return;
}
$('#div_alert').showAlert({message: '{{Envoi réussi}}', level: 'success'});
}
});
});
function printEqLogic(_eqLogic) {
if (!isset(_eqLogic)) {
var _eqLogic = {configuration: {}};
}
if (!isset(_eqLogic.configuration)) {
_eqLogic.configuration = {};
}
if (isset(_eqLogic.configuration) && isset(_eqLogic.configuration.type) && _eqLogic.configuration.type == 'remoteControl') {
$('.item-conf').empty();
$('.item-conf').load('index.php?v=d&plugin=switchbot&modal=remote.configuration', function () {
$('body').setValues(_eqLogic, '.eqLogicAttr');
initCheckBox();
modifyWithoutSave = false;
});
} else {
$('.item-conf').empty();
$('.item-conf').load('index.php?v=d&plugin=switchbot&modal=' + _eqLogic.configuration.type + '.configuration', function () {
$('body').setValues(_eqLogic, '.eqLogicAttr');
var type = _eqLogic.configuration.type;
jeedom.eqLogic.getCmd({
id: _eqLogic.id,
error: function (error) {
$('#div_alert').showAlert({message: error.message, level: 'danger'});
},
success: function (data) {
$('.cmd').remove();
var thermo = false;
for (var i in data) {
if(data[i].logicalId == "refresh") {
thermo = true;
}
addCmdToTable(data[i]);
}
$('#iconDevice').attr('src','/plugins/switchbot/core/config/icones/' + type +'.png');
}
});
$('#iconDevice').attr('src','/plugins/switchbot/core/config/icones/' + _eqLogic.configuration.type + '.png');
initCheckBox();
modifyWithoutSave = false;
});
}
}
/*
* Fonction pour l'ajout de commande, appellé automatiquement par plugin.template
*/
function addCmdToTable(_cmd) {
if (!isset(_cmd)) {
var _cmd = {configuration: {}};
}
var tr = '