Created Json endpoints and separated json stuff from http pages, moved chart JS to common file.

This commit is contained in:
Ziver Koc 2016-11-28 17:11:17 +01:00
parent 969f089a9e
commit 92f402a07b
12 changed files with 348 additions and 279 deletions

View file

@ -44,3 +44,80 @@ $.fn.relTimestamp = function() {
return this;
});
};
////////////////////////////////////// Hal functions
////////////// Chart functions
function createChart(elementId, url, updateTime=-1){
var tickConf = {count: 20};
if (updateTime < 60*60*1000)
tickConf['format'] = '%H:%M';
else if (updateTime < 24*60*60*1000)
tickConf['format'] = '%Y-%m-%d %H:%M';
else
tickConf['format'] = '%Y-%m-%d';
var chart = c3.generate({
bindto: elementId,
data: {json: []}, // set empty data, data will be loaded later
axis : {
x : {
type : 'timeseries',
label: 'Timestamp',
tick: tickConf,
},
y: {
label: 'Power (kWh)',
min: 0,
},
y2: {
show: true,
label: 'Temperature (C)',
min: 0,
}
},
grid: {
y: {show: true}
},
point: {
show: false
}
});
updateChart(chart, url, updateTime);;
}
function updateChart(chart, url, updateTime){
$.getJSON(url, function(json){
chart.load(getChartData(json));
});
if (updateTime > 0)
setTimeout(function(){ updateChart(chart, url, updateTime); }, updateTime);
}
function getChartData(json){
var dataXaxis = {};
var dataYaxis = {};
var data = [];
var labels = [];
json.forEach(function(sensor, i) {
var index = 'data'+i;
labels[index] = sensor.user +": "+ sensor.name;
dataXaxis[index] = 'data'+i+'x';
data.push([index+'x'].concat(sensor.timestamps));
data.push([index].concat(sensor.data));
if (sensor.type == "PowerConsumptionSensorData")
dataYaxis[index] = 'y';
else //if (sensor.type == "TemperatureSensorData")
dataYaxis[index] = 'y2';
});
return {
xs: dataXaxis,
columns: data,
names: labels,
type: 'spline',
axes: dataYaxis,
};
}