function get_cookie(n,d,ti=false) {
var r = d;
var c = document.cookie.split(';');
c.forEach(function(v,i) {
v = v.split('=');
if (v[0].trim() == n) {
if (ti) {
const par = parseInt(v[1].trim());
if (!isNaN(par)) {
r = par;
}
} else r = v[1].trim();
}
});
return r;
}
function mhdr_status(c) {
if (c.length == 0) $('#cfg-modal .modal-header').removeClass('suc err');
else $('#cfg-modal .modal-header').addClass(c);
}
function loader(a) {
if (a) $('#cfg-modal .modal-header img').addClass('loader');
else $('#cfg-modal .modal-header img').removeClass('loader');
}
function do_ajax(p_url,p_data,f,i = -1,a = true) {
$.ajax({
url: p_url,
type: 'POST',
data: p_data,
dataType: 'json',
async: a,
success: function (json) {
f(json,i);
},
error: function () {
// console.log('do_ajax error');
f();
},
timeout:2000
});
}
function copy_form_values(dir) {
$('#m-b input:text, #m-b input[type=range]').each(function() {
var id = $(this).attr('id');
if (dir == 0) $('#' + id + '_v').html($('#' + id).val());
else $('#' + id).val($('#' + id + '_v').html());
});
$('#m-b select').each(function() {
var id = $(this).attr('id');
text = $('#' + id + '_v').html();
if (dir == 1) {
$(this).find('option').filter(function () { return $(this).html() == text; }).attr('selected', 'selected');
} else {
$('#' + id + '_v').html($(this).find('option:selected').html());
}
});
$('#m-b input:radio').each(function() {
var id = $(this).attr('name');
text = $('#' + id + '_v').html();
label = $(this).parent().find('label').html();
if (dir == 1) {
if (label == text) $(this).attr('checked','checked');
} else {
if ($(this).is(':checked')) $('#' + id + '_v').html(label);
}
});
if (dir == 1) {
$('#m-b div.form-group').each(function() {
var len = $(this).find('div.invalid-feedback').length;
if (len == 0) {
$(this).append('
Wrong value!
');
}
});
}
}
function fill_values(d,ids) {
for (i = 0; i < ids.length; i++) {
$('#' + ids[i] + '_v').html(d[ids[i]]);
}
}
function show_errors(s,ids) {
if (s) mhdr_status('err');
for (i = 0; i < ids.length; i++) {
if (s & (1 << i)) $('#' + ids[i]).addClass('is-invalid').removeClass('is-valid');
else $('#' + ids[i]).removeClass('is-invalid').addClass('is-valid');
}
}
function close_modal() {
setTimeout(function() {
$('#m-b').empty();
$('#cfg-modal').modal('hide');
},500);
}
function pv(p) {
var s = '';
for (var i in p) {
var o = p[i];
if (i == 0) s += o + '=';
else s += '&' + o + '=';
if ($('#' + o).val() != undefined) s += $('#' + o).val();
else if ($('input[name=' + o + ']:checked').val() != undefined) s += $('input[name=' + o + ']:checked').val();
}
return s;
}
function c_txt(n,t,e,c = '',it = 'text') {
var html = '
';
html += '';
html += '';
html += e.length > 0 ? '
' + e + '
' : '';
html += '
';
return html;
}
function c_btn(t,v,c) {
return '';
}
function c_opt(n,t,o,pl,vp,lp,e) {
var html = '
';
html += '';
html += '';
html += e.length > 0 ? '
' + e + '
' : '';
html += '
';
return html;
}
function c_rad(n,t,r,e,c) {
var html = '
';
html += '';
for (i = 0; i < r.length; i++) {
html += '
';
html += '';
html += '';
html += '
';
}
html += e.length > 0 ? '
' + e + '
' : '';
html += '
';
return html;
}
function box(t,id,p,su = '',vid = '',bc = '') {
var s = '';
s += '
';
if (id >= 0) s += '
' + t + '
';
else s += '
' + t + '
';
s += '
';
for (var j in p) {
if (p[j][0].length > 0) s += '
' + p[j][0] + '
';
s += '
';
for (var i in p[j][1]) {
l = p[j][1][i].split(";");
if (l.length == 4) s += l[0];
else s += '
' + l[0] + '
';
}
s += '
';
s += '
';
for (var i in p[j][1]) {
l = p[j][1][i].split(";");
if (l.length == 4) {
var t = l[3].replace(/&DG/gi,'°');
s += t;
} else if (l.length == 3) s += l[2];
else if (l.length == 2) s += '
-
';
else s += '
-
';
}
s += '
';
}
s += su;
s += '
';
s += '
';
return s;
}
function gens() {
var o = '';
o += '';
o += '
';
o += '';
o += '';
o += '
';
o += '
';
o += '
';
o += '
';
o += '
';
o += '';
o += '';
o += '';
o += '
';
o += '
';
o += '
';
o += '';
o += '
';
o += '
';
o += '
';
return o;
}
var pre = '';
var dmxsnf = '';
function ptit(t) {
// console.log('setting title to ' + t);
if (t == $('#pth').data('title')) return;
// console.log('set');
if (t.length) $('#pth').data('title',t);
$('#pth').html($('#pth').data('title') + dmxsnf);
$('#pt_v').html(pre + $('#pth').html());
// console.log($('#pth').html());
}
function c_dt(n,t,e,c,it) {
var html = '
';
html += '';
html += '
';
html += '';
html += e.length > 0 ? '
' + e + '
' : '';
html += '
';
return html;
}
function c_fsens(i,t,u) {
var html = '';
html += '
';
html += '';
html += '';
html += '';
html += '';
html += '
';
return html;
}
$(document).ready(function() {
$('body').prepend(gens());
var am = 0;
var mip = 0;
mip = 1;
var href = $(location).attr('pathname');
if (href.indexOf('personality') != -1) am = 1;
if (href.indexOf('logs') != -1) am = 2;
if (href.indexOf('sensors') != -1) am = 3;
if (href.indexOf('discovery') != -1) am = (3 + mip);
$('#navbar ul li').eq(am).addClass('active');
//handle settings
$(document).on('click','#scfg',function() {
mhdr_status('');
$('#m-t').html('Site settings');
var h = '
Password settings
';
h += c_txt('opswd','Old password','Please put old password (four digits)','','password');
h += c_txt('npswd','New password','Please put new password (four digits)','','password');
h += c_txt('npsch','New password check','New passwords check failed','','password');
$('#m-b').html(h);
$('.sb').unbind();
$('#cfg-modal').modal();
$('.sb').bind('click',function() {
loader(1);
var par = ['opswd','npswd','npsch'];
do_ajax('/set_cfg',pv(par),function (json) {
if (json) {
if (json['status'] == 0) {
mhdr_status('suc');
close_modal();
location = '/';
}
show_errors(json['status'],par);
loader(0);
}
});
});
});
//periodicly get status
var bsy = false;
var ebli = 0;
var eblio = 255;
var idntb = $('#idnt img');
function sta() {
do_ajax('/serr',{},function (json) {
// console.log(json);
dmxsnf = '';
if (json ) {
if (json['err'] == 1) ebli = 0x80;
else ebli = 0;
if (json['dmxsnf'] == 1) dmxsnf = ' [sniffing DMX]';
if (json['idnt'] == 1) idntb.addClass('a');
else idntb.removeClass('a');
if (json['dls'] && json['dls'] == 1) {
$('.snz').show();
}
} else if (json == undefined) {
ebli = 0x40;
} else ebli = 0;
bsy = false;
});
}
setInterval(function() {
if (!bsy) {
bsy = true;
sta();
}
},3000);
sta();
setInterval(function() {
if (ebli == eblio) return;
var opre = pre;
if (ebli & 0x80) {
ebli ^= 0x01;
if (ebli & 0x01) pre = '(!) ';
$('#pth').removeClass('stou');
$('#pth').addClass('serr');
} else if (ebli & 0x40) {
ebli ^= 0x01;
if (ebli & 0x01) pre = '(?) ';
$('#pth').removeClass('serr');
$('#pth').addClass('stou');
} else {
ebli = 0;
$('#pth').removeClass('serr stou');
pre = '';
}
//$('#pt_v').html(pre + $('#pth').data('title') + dmxsnf);
if (opre != pre) ptit('');
eblio = ebli;
},1000);
$('#idnt').click(function() {
idntb.toggleClass('a');
do_ajax('/rdmi',{s:idntb.hasClass('a') ? 1:0},function(json) {});
});
});