function getSQLDate(device,parameter,callback,type) {
var range12h = 1000*60*60*12;
var range24h = 1000*60*60*24;
if (type=='24h') var myQuery = "SELECT ts, parameter, val FROM iobroker.mihome_th where device = '" + device +"' and parameter in ('humidity','temperature') AND val is not null AND ts > unix_timestamp()*1000-((60*60*24)*1000) ORDER BY parameter,ts";
if (type=='last') var myQuery = "SELECT ts,val FROM iobroker.mihome_th where device = '" + device +"' and parameter = '" + parameter +"' and val is not NULL order by ts desc limit 1";
vis.conn._socket.emit('sendTo', 'sql.0', 'query', myQuery, function (callback,type,result) {
if (result.error) {
console.error(result.error);
} else {
if (type=='last') {
var datapoints = Object.keys(result.result).map(function(key) {
return [result.result[key].ts, result.result[key].val];
});
}
if (type=='24h') {
var datapoints = result.result.reduce(function(acc,obj) {
var key = obj['parameter'];
if(!acc[key]) {
acc[key] = [];
}
acc[key].push([obj.ts,obj.val]);
return acc;
},[]);
}
callback(datapoints);
}
}.bind(this,callback,type));
}
function humFormatter(v, axis) {
return v.toFixed(axis.tickDecimals) + " %H";
}
function tempFormatter(v, axis) {
return v.toFixed(axis.tickDecimals) + " °C";
}
function getTemp(device,parameter,placeholder,width,height) {
var data = {};
data.device = device;
data.parameter = parameter;
data.placeholder = placeholder;
getSQLDate(device,parameter,doTemp.bind(data),'last');
}
function doTemp(sqldata) {
var data = this;
$(data.placeholder).html(tempFormatter(sqldata[0][1]));
getTempUpdate(data);
}
function getTempUpdate(data) {
getSQLDate(data.device,data.parameter,doTempUpdate.bind(data),'last');
}
function doTempUpdate(sqldata) {
var data = this;
$(data.placeholder).html(tempFormatter(sqldata[0][1]));
setTimeout(function() {
getTempUpdate(data);
}.bind(data),5000);
}
function tempFormatter(value) {
return Math.round(value) + "°C";
}
function getTempchart(device,parameter,placeholder,width,height,type='inside') {
var data = {};
data.placeholder = placeholder;
data.width = width;
data.height = height;
data.device = device;
data.type = type;
getSQLDate(device,parameter,doTempchart.bind(data),'24h');
}
function doTempchart(sqldata) {
var data = this;
var new_tempdata = $.extend(true,{},temp_data);
new_tempdata[0].data = sqldata.temperature;
new_tempdata[1].data = sqldata.humidity;
debugger;
$(data.placeholder).width(data.width)
.height(data.height);
if (data.type=='inside') data.options = $.extend(true,{},temp_inside_options);
if (data.type=='outside') data.options = $.extend(true,{},temp_outside_options);
data.plot = $.plot(data.placeholder,[new_tempdata[0],new_tempdata[1]], data.options);
}
function getGauge(device,parameter,placeholder,width,height,type='inside') {
var data = {};
data.placeholder = placeholder;
data.width = width;
data.height = height;
data.device = device;
data.parameter = parameter;
data.type = type;
getSQLDate(device,parameter,doGauge.bind(data),'last');
}
function doGauge(sqldata) {
var data = this;
var new_humdata = hum_data.slice(0);
new_humdata[0].data = sqldata;
new_humdata[0].label = this.device;
if (data.type=='inside') data.options = $.extend(true,{},hum_threshold_inside,hum_options);
if (data.type=='outside') data.options = $.extend(true,{},hum_threshold_outside,hum_options);
$(this.placeholder).width(this.width)
.height(this.height);
data.plot = $.plot(this.placeholder,new_humdata, data.options);
$(this.placeholder).click(function() {
getTempchart(data.device,null,'#tempdialog',880,500,data.type);
$('#tempdialog').dialog({
dialogClass: 'noTitle',
minHeight: 527,
maxHeight: 527,
minWidth: 900,
maxWidth: 900,
position: [0,28],
}).on('click',function(event){
$('#tempdialog').dialog('close');
});
return false;
});
getGaugeUpdate(data);
}
function getGaugeUpdate(data) {
getSQLDate(data.device,data.parameter,doGaugeUpdate.bind(data),'last');
}
function doGaugeUpdate(sqldata) {
var data = this;
var new_humdata = hum_data.slice(0);
new_humdata[0].data = sqldata;
new_humdata[0].label = this.device;
data.options.series.gauges.value.color = getColor(data.options.series.gauges,new_humdata[0].data[0][1]);
data.plot = $.plot(this.placeholder,new_humdata, data.options);
setTimeout(function() {
getGaugeUpdate(data);
}.bind(data),5000);
}
function getColor(gaugeOptionsi, data) {
var color;
for (var i = 0; i < gaugeOptionsi.threshold.values.length; i++) {
var threshold = gaugeOptionsi.threshold.values[i];
color = threshold.color;
if (data < threshold.value) {
break;
}
}
return color;
}
var hum_data = [
{
label: "device",
data: [[0, 50]]
}
];
var hum_options = {
series: {
gauges: {
frame: false,
gauge: {
min: 30,
max: 70,
width: 15,
background: {
color: "#2f2f32"
},
border: {
color: "#2f2f32",
width: 0
},
shadow: {
show: false,
}
},
value: {
background: {
color: null
},
font: {
size: 40, // a specified number, or 'auto'
family: ",sans-serif"
},
// color: "#73bf69"
},
cell: {
background: {
color: null
},
border: {
show: false,
},
margin: 5,
vAlign: "middle" // 'top' or 'middle' or 'bottom'
},
debug: {
log: true,
layout: true
},
show: true,
label: {
show: true,
color: "#d8d9da",
margin: 0,
font: {
size: 20, // a specified number, or 'auto'
family: ",sans-serif"
}
}
}
}
};
var hum_threshold_outside = {
series: {
gauges: {
threshold: {
values: [
{
value: 40,
color: "#73bf69"
}, {
value: 60,
color: "#73bf69"
}, {
value: 100,
color: "#73bf69"
},
]
}
}
}
};
var hum_threshold_inside = {
series: {
gauges: {
threshold: {
values: [
{
value: 40,
color: "#ee485a"
}, {
value: 60,
color: "#73bf69"
}, {
value: 100,
color: "#ee485a"
},
]
}
}
}
};
var temp_data = [
{
data: null,
color: "#F2495C",
bars: {
fillColor: "#F2495C"
},
label: "temp"
},
{ data: null,
color: "#5794F2",
bars: {
fillColor: "#5794F2"
},
label: "hum",
yaxis: 2
}
];
var temp_inside_options = {
series: {
lines: {
lineWidth: 2,
fill: 0.1
}
},
xaxes: [
{
mode: "time",
timeformat: "%H:%M",
color: "#464648",
font: {
color: "#d8d9da"
}
}
],
yaxes: [
{
min: 5,
max: 35,
tickFormatter: tempFormatter,
color: "#464648",
font: {
color: "#d8d9da"
}
},
{
min: 30,
max: 90,
alignTicksWithAxis: 1,
position: "rigth",
tickFormatter: humFormatter,
color: "#464648",
font: {
color: "#d8d9da"
}
}
],
legend: { position: "sw" },
grid: {
borderWidth: 0,
color: "#464648",
labelMarginX: 0,
}
};
var temp_outside_options = {
series: {
lines: {
lineWidth: 2,
fill: 0.1
}
},
xaxes: [
{
mode: "time",
timeformat: "%H:%M",
color: "#464648",
font: {
color: "#d8d9da"
}
}
],
yaxes: [
{
min: -15,
max: 35,
tickFormatter: tempFormatter,
color: "#464648",
font: {
color: "#d8d9da"
}
},
{
min: 30,
max: 100,
alignTicksWithAxis: 1,
position: "rigth",
tickFormatter: humFormatter,
color: "#464648",
font: {
color: "#d8d9da"
}
}
],
legend: { position: "sw" },
grid: {
borderWidth: 0,
color: "#464648",
labelMarginX: 0,
}
};