var api_base_url_sensors = '/api/1/sensor/type/Wassertemperatur?mode=consolidated&format=c3'; var api_base_url_weather = '/api/1/openweathermap/city/3319578?mode=consolidated&format=c3'; var api_url_currentair = '/api/1/currentairtemperature'; var api_url_currentwater = '/api/1/currentwatertemperature'; var refresh_interval = 5 * 60 * 1000; function dayschart(element, title, days, xtickformat) { var chartdata; if (element.substr(0, 7) == 'chart_w') { chartdata = { url: api_base_url_sensors + beginend(days), mimeType: 'json', xs: { '0416a1ac66ff': '0416a1ac66ff_x', '0516a207a4ff': '0516a207a4ff_x', '0416a1bab9ff': '0416a1bab9ff_x', '0316a2193bff': '0316a2193bff_x' }, names: { '0416a1ac66ff': 'Wassertemperatur 30cm Tiefe', '0516a207a4ff': 'Wassertemperatur 50cm Tiefe', '0416a1bab9ff': 'Wassertemperatur 1m Tiefe', '0316a2193bff': 'Wassertemperatur in 30 cm Tiefe' }, xFormat: '%Y-%m-%d %H:%M:%S', // https://en.wikipedia.org/wiki/Web_colors colors: { '0416a1ac66ff': 'lightskyblue', '0516a207a4ff': 'royalblue', '0416a1bab9ff': 'mediumblue', '0316a2193bff': 'midnightblue' }, /* xSort: false, order: function (a, b) { // gets 2 x objects. seems to be a "sort function" for js's sort() // -1: a before b etc. // only handle cases that appear in the wild // console.log("a.id", a.id, "b.id", b.id); if (a.id == '0416a1ac66ff') {return -1}; if (a.id == '0416a1bab9ff') {return 1}; } */ }; } else { chartdata = { url: api_base_url_weather + beginend(days), mimeType: 'json', xs: { '3319578': '3319578_x' }, names: { '3319578': 'Lufttemperatur in Obsteig' }, xFormat: '%Y-%m-%d %H:%M:%S', colors: { '3319578': 'red' } }; }; var chart = c3.generate({ bindto: '#' + element, size: { width: getwidth(element) }, onresized: function () { chart.resize({ width: getwidth(element) }); // chart.flush(); }, transition: { duration: null }, title: { text: title }, data: chartdata, point: { r: 1.5 // default 2.5 }, axis: { x: { type: 'timeseries', tick: { format: function (d) { var strftimeDE = strftime.localizeByIdentifier('de_DE'); return strftimeDE(xtickformat, new Date(d)); }, fit: false, multiline: true, // broken? so → rotate: -90 } }, y: { label: { text: 'Temperatur in °C', position: 'outer-middle' }, tick: { format: function (d) { return d.toFixed(1) + '°'; } } } }, zoom: { enabled: true, /* onzoomend: function (domain) { var duration = (domain[1] - domain[0]) / 1000; // duration is now the diff of the "viewport in seconds" // FIXME? var days = duration / 86400; if (days > 0) { loaddays(this, element, days); // this = chart } } */ }, tooltip: { format: { // TODO: maybe move to top name: function (name, ratio, id, index) { var prefix = ''; if (element.substr(0, 7) == 'chart_w') { var re = /\d+ ?c?m/; prefix = 'Wasser '; } else { var re = /Luft/; }; var result = re.exec(name); return prefix + result; }, value: function (value, ratio, id, index) { return value.toFixed(2) + '°'; }, title: function (datetime) { // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString options = { weekday: 'short', year: 'numeric', month: 'numeric', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric', timeZone: 'Europe/Vienna', timeZoneName: 'short' }; return datetime.toLocaleString('de-AT', options); } } }, grid: { y: { show: true } }, regions: [ {axis: 'y', start: -999, end: 0, class: 'region-frozen'}, {axis: 'y', start: 0, end: 25, class: 'region-ok'}, {axis: 'y', start: 25, end: 100, class: 'region-warm'}, {axis: 'y', start: 100, end: 999, class: 'region-boiling'} ] }); // reload data in charts setInterval( function() { loaddays(chart, element, days); }, refresh_interval ); }; function loaddays(chart, element, days) { // TODO: maybe move to top or factor out somehow if (element.substr(0, 7) == 'chart_w') { chart.load({ url: api_base_url_sensors + beginend(days), mimeType: 'json' }); } else { chart.load({ url: api_base_url_weather + beginend(days), mimeType: 'json' }); } } function beginend(days) { var now = Date.now(); // TODO: this timezone calculation relies on the browser being in Europe/Vienna var refdate = new Date(now); var offset = refdate.getTimezoneOffset(); // -120 for UTC+2 var end = new Date(now - offset * 60 * 1000).toISOString().substr(0, 19); // 2018-06-13T16:52:30.995Z var begin = new Date(now - days * 60*60*24 * 1000 - offset * 60 * 1000).toISOString().substr(0, 19); return '&begin=' + begin + '&end=' + end; } function getwidth(element) { return document.getElementById(element).parentElement.clientWidth * 80 / 100; } dayschart('chart_water_1', 'Der See (Tag)', 1, '%H:%M'); dayschart('chart_water_7', 'Der See (Woche)', 7, '%a %d'); dayschart('chart_water_31', 'Der See (Monat)', 31, '%Y-%m-%d'); dayschart('chart_water_365', 'Der See (Jahr)', 365, '%b %Y'); dayschart('chart_air_1', 'Die Luft (Tag)', 1, '%H:%M'); dayschart('chart_air_7', 'Die Luft (Woche)', 7, '%a %d'); dayschart('chart_air_31', 'Die Luft (Monat)', 31, '%Y-%m-%d'); dayschart('chart_air_365', 'Die Luft (Jahr)', 365, '%b %Y'); // reload current values setInterval( function() { var airvalue = document.getElementById('currentairvalue'); var airtime = document.getElementById('currentairtime'); var watervalue = document.getElementById('currentwatervalue'); var watertime = document.getElementById('currentwatertime'); fetch(api_url_currentair) .then((resp) => resp.json()) .then(function(data) { airvalue.innerText = data['value'].toFixed(1); airtime.innerText = strftime('%Y-%m-%d H:%M', data['timestamp']); }) .catch(function(error) { // handle error }); fetch(api_url_currentwater) .then((resp) => resp.json()) .then(function(data) { watervalue.innerText = data['value'].toFixed(1); watertime.innerText = strftime('%Y-%m-%d H:%M', data['timestamp']); }) .catch(function(error) { // handle error }); }, refresh_interval );