Version 0.9.1
function getHistory() {
var url = 'ajax/get_history.php',
data = {},
ajax = {url: url, data: data, dataType: "json", complete: function (r) { writeGraph(r); }};
$.ajax(ajax);
}
function ts2date(ts) {
var date = new Date(ts*1000),
year = date.getFullYear(),
month = "0" + date.getMonth(),
day = "0" + date.getDate();
return year + '-' + month.substr(-2) + '-' + day.substr(-2);
}
function ts2datetime(ts) {
var date = new Date(ts*1000),
year = date.getFullYear(),
month = "0" + date.getMonth(),
day = "0" + date.getDate(),
hours = "0" + date.getHours(),
minutes = "0" + date.getMinutes(),
seconds = "0" + date.getSeconds();
return year + '-' + month.substr(-2) + '-' + day.substr(-2) + ' ' + hours.substr(-2) + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
}
function prepareData(d) {
var o = [], ot = {}, s, step = 300, last, v, ar, now = Math.round((+ new Date())/1000);
$(d).each(function () {
ot.name = this.name;
ot.zwave_id = this.zwave_id;
ot.type = this.type;
ot.id = this.id;
ot.data = [];
if(this.data.length == 0 || this.data.length == 1) return -1;
ot.date = ts2date(this.data[0].ts);
v = (isNaN(this.data[0].value))? this.data[0].value.replace('off',0).replace('on',1) : this.data[0].value;
s = this.data[0].ts;
last = v;
ar = [ts2datetime(s),v];
ot.data.push(ar);
$(this.data).each(function () {
v = (isNaN(this.value))? this.value.replace('off',0).replace('on',1) : this.value;
ar = [ts2datetime(s),v];
while(this.ts > (s + step)) {
ot.data.push([ts2datetime(s),last]);
s += step;
}
s = this.ts;
last = v;
ot.data.push(ar);
});
while(s < now) {
s += step;
ot.data.push([ts2datetime(s),last]);
}
o.push(jQuery.extend(true, {}, ot));
});
console.log(o);
return o;
}
function writeGraph(d) {
var names = [];
d = d.responseJSON.data;
r = d;
var data = prepareData(d);
x = data;
$(data).each(function () {
if(this.data.length > 0) {
$('<div id="'+ this.zwave_id +'">').appendTo('#charts');
$.jqplot(this.zwave_id, [this.data], {
title: this.name +' ('+ this.date +')',
title: this.name,
axes:{
xaxis:{
renderer:$.jqplot.DateAxisRenderer,
tickOptions:{formatString:'%H:%M'},
tickInterval:'1 hour'
}
},
seriesDefaults: {
showMarker:false,
pointLabels: { show:true }
}
});
}
});
}
$(function(){
getHistory();
});