3 archive_base="/meteo/archive";
4 current_base = "/meteo/graph";
6 var url = window.location.pathname;
8 if (url.startsWith(archive_base)) {
9 url = url.substr(archive_base.length);
12 var params = url.split('/');
15 var month = params[2];
17 var devid = params[4];
18 var sensor = params[5];
19 var param = params[6];
27 function composeUrl() {
28 url = window.location.protocol+"//"+window.location.host+archive_base+"/";
32 url = url + "/" + month;
34 url = url + "/" + day;
36 url = url + "/" + devid;
38 url = url + "/" + sensor;
40 url = url + "/" + param;
47 history.pushState({}, null, url);
50 function selectChange() {
51 new_year = document.getElementById("year").value;
52 new_month = document.getElementById("month").value;
53 new_day = document.getElementById("day").value;
54 sensor_id = document.getElementById("sensor").value.split(".");
55 new_devid = sensor_id[0];
56 new_sensor = sensor_id[1];
57 new_param = sensor_id[2];
58 if (year != new_year) {
61 } else if (month != new_month) {
64 } else if (day != new_day) {
67 } else if ((new_devid != devid) || (new_sensor != sensor) || (new_param != param)) {
76 function processDataset(dataset,devid,sensorname,paramname) {
77 var scale = properties["scale"][devid+"."+sensorname+"."+paramname]
80 for (idx in dataset) {
82 newRec.t = dataset[idx].t
83 newRec.y = dataset[idx].y * scale[0];
92 function drawGraph(graphData) {
94 document.getElementById("current").href = current_base+"/"+devid + "/" + sensor + "/" + param;
96 var div = document.getElementById("chartdiv");
97 var canvas = document.getElementById("chart");
99 canvas.width = div.style.width;
100 canvas.height = div.style.height;
102 var ctx = canvas.getContext('2d');
103 var color = Chart.helpers.color;
105 var sensor_path = devid+"."+sensor+"."+param;
107 var y_label = properties["names"][sensor_path];
108 if (properties["units"][sensor_path]) {
109 y_label = y_label + ", " + properties["units"][sensor_path];
117 label: properties["places"][sensor_path] + ' - ' + properties["names"][sensor_path],
118 backgroundColor: color(properties["colors"][sensor_path]).alpha(0.5).rgbString(),
119 borderColor: properties["colors"][sensor_path],
120 data: processDataset(graphData,devid,sensor,param),
131 fontColor: properties["fonts"]["legend"]["color"],
132 fontSize: properties["fonts"]["legend"]["size"],
133 fontStyle: properties["fonts"]["legend"]["style"],
139 distribution: 'series',
141 fontColor: properties["fonts"]["axes"]["color"],
142 fontSize: properties["fonts"]["axes"]["size"],
143 fontStyle: properties["fonts"]["axes"]["style"],
149 labelString: y_label,
150 fontColor: properties["fonts"]["axes"]["color"],
151 fontSize: properties["fonts"]["axes"]["size"],
152 fontStyle: properties["fonts"]["axes"]["style"],
159 var chart = new Chart(ctx, cfg);
162 function RefreshGraph() {
164 var req = new XMLHttpRequest();
166 req.onreadystatechange = function () {
167 if (this.readyState != 4) return;
168 if (this.status != 200) {
169 setTimeout(RefreshGraph,30000);
172 var graphData = JSON.parse(this.responseText);
173 drawGraph(graphData);
176 req.open("GET", urlbase+"get-archive/"+year+"/"+month+"/"+day+"/"+devid+"/"+sensor+"/"+param, true);
177 req.withCredentials = true;
182 function GetProperties() {
183 var req = new XMLHttpRequest();
185 req.onreadystatechange = function () {
186 if (this.readyState != 4) return;
187 if (this.status != 200) {
188 setTimeout(GetProperties,30000);
191 properties = JSON.parse(this.responseText);
195 req.open("GET", urlbase+"props", true);
196 req.withCredentials = true;
201 function fillSelector(id,data,value,nameFunc) {
203 var element = document.getElementById(id);
209 line = "<option value=\""+data[i]+"\">"+nameFunc(data[i])+"</option>"
211 line = "<option value=\""+data[i]+"\">"+data[i]+"</option>"
216 element.innerHTML = html;
218 element.value = value;
220 return element.value;
224 function GetYears() {
225 var req = new XMLHttpRequest();
227 req.onreadystatechange = function () {
228 if (this.readyState != 4) return;
229 if (this.status != 200) {
230 setTimeout(GetYears,30000);
233 years = JSON.parse(this.responseText);
234 year = fillSelector("year", years, year);
241 req.open("GET", urlbase+"years", true);
242 req.withCredentials = true;
247 function GetMonths() {
248 var req = new XMLHttpRequest();
250 req.onreadystatechange = function () {
251 if (this.readyState != 4) return;
252 if (this.status != 200) {
253 setTimeout(GetMonths,30000);
256 months = JSON.parse(this.responseText);
257 month = fillSelector("month", months, month);
264 req.open("GET", urlbase+"months/"+year, true);
265 req.withCredentials = true;
271 var req = new XMLHttpRequest();
273 req.onreadystatechange = function () {
274 if (this.readyState != 4) return;
275 if (this.status != 200) {
276 setTimeout(GetDays,30000);
279 days = JSON.parse(this.responseText);
280 day = fillSelector("day", days, day);
287 req.open("GET", urlbase+"days/"+year+"/"+month, true);
288 req.withCredentials = true;
293 function SensorName(id) {
294 return properties["places"][id]+" - "+properties["names"][id];
297 function GetSensors() {
298 var req = new XMLHttpRequest();
300 req.onreadystatechange = function () {
301 if (this.readyState != 4) return;
302 if (this.status != 200) {
303 setTimeout(GetSensors,30000);
306 sensors = JSON.parse(this.responseText);
307 if (devid && sensor && param) {
308 sensor_id = devid+"."+sensor+"."+param;
312 sensor_id = fillSelector("sensor", sensors, sensor_id, SensorName).split(".");
313 devid = sensor_id[0];
314 sensor = sensor_id[1];
315 param = sensor_id[2];
322 req.open("GET", urlbase+"sensors/"+year+"/"+month+"/"+day, true);
323 req.withCredentials = true;
328 setTimeout(GetProperties,100);