1 var api_base_url_sensors = '/api/1/sensor/type/Wassertemperatur?mode=consolidated&format=c3';
2 var api_base_url_weather = '/api/1/openweathermap/city/3319578?mode=consolidated&format=c3';
3 var api_url_currentair = '/api/1/currentairtemperature';
4 var api_url_currentwater = '/api/1/currentwatertemperature';
5 var refresh_interval = 5 * 60 * 1000;
7 function dayschart(element, title, days, xtickformat) {
9 if (element.substr(0, 7) == 'chart_w') {
11 url: api_base_url_sensors + beginend(days),
14 '0416a1ac66ff': '0416a1ac66ff_x',
15 '0516a207a4ff': '0516a207a4ff_x',
16 '0416a1bab9ff': '0416a1bab9ff_x',
17 '0316a2193bff': '0316a2193bff_x'
20 '0416a1ac66ff': 'Wassertemperatur 30cm Tiefe',
21 '0516a207a4ff': 'Wassertemperatur 50cm Tiefe',
22 '0416a1bab9ff': 'Wassertemperatur 1m Tiefe',
23 '0316a2193bff': 'Wassertemperatur in 30 cm Tiefe'
25 xFormat: '%Y-%m-%d %H:%M:%S',
26 // https://en.wikipedia.org/wiki/Web_colors
28 '0416a1ac66ff': 'lightskyblue',
29 '0516a207a4ff': 'royalblue',
30 '0416a1bab9ff': 'mediumblue',
31 '0316a2193bff': 'midnightblue'
35 order: function (a, b) {
36 // gets 2 x objects. seems to be a "sort function" for js's sort()
37 // -1: a before b etc.
38 // only handle cases that appear in the wild
39 // console.log("a.id", a.id, "b.id", b.id);
40 if (a.id == '0416a1ac66ff') {return -1};
41 if (a.id == '0416a1bab9ff') {return 1};
47 url: api_base_url_weather + beginend(days),
50 '3319578': '3319578_x'
53 '3319578': 'Lufttemperatur in Obsteig'
55 xFormat: '%Y-%m-%d %H:%M:%S',
61 var chart = c3.generate({
62 bindto: '#' + element,
64 width: getwidth(element)
66 onresized: function () {
68 width: getwidth(element)
85 format: function (d) {
86 var strftimeDE = strftime.localizeByIdentifier('de_DE');
87 return strftimeDE(xtickformat, new Date(d));
90 multiline: true, // broken? so →
96 text: 'Temperatur in °C',
97 position: 'outer-middle'
100 format: function (d) {
101 return d.toFixed(1) + '°';
109 onzoomend: function (domain) {
110 var duration = (domain[1] - domain[0]) / 1000; // duration is now the diff of the "viewport in seconds"
112 var days = duration / 86400;
114 loaddays(this, element, days); // this = chart
121 // TODO: maybe move to top
122 name: function (name, ratio, id, index) {
124 if (element.substr(0, 7) == 'chart_w') {
130 var result = re.exec(name);
131 return prefix + result;
133 value: function (value, ratio, id, index) {
134 return value.toFixed(2) + '°';
136 title: function (datetime) {
137 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString
140 year: 'numeric', month: 'numeric', day: 'numeric',
141 hour: 'numeric', minute: 'numeric', second: 'numeric',
142 timeZone: 'Europe/Vienna', timeZoneName: 'short'
144 return datetime.toLocaleString('de-AT', options);
157 {axis: 'y', start: -999, end: 0, class: 'region-frozen'},
158 {axis: 'y', start: 0, end: 25, class: 'region-ok'},
159 {axis: 'y', start: 25, end: 100, class: 'region-warm'},
160 {axis: 'y', start: 100, end: 999, class: 'region-boiling'}
164 // workaround for unrendered dots
165 setTimeout(function () { chart.flush(); }, 5000);
167 // reload data in charts
170 loaddays(chart, element, days);
176 function loaddays(chart, element, days) {
177 // TODO: maybe move to top or factor out somehow
178 if (element.substr(0, 7) == 'chart_w') {
180 url: api_base_url_sensors + beginend(days),
186 url: api_base_url_weather + beginend(days),
191 setTimeout(function () { chart.flush(); }, 5000);
194 function beginend(days) {
195 var now = Date.now();
197 // TODO: this timezone calculation relies on the browser being in Europe/Vienna
198 var refdate = new Date(now);
199 var offset = refdate.getTimezoneOffset(); // -120 for UTC+2
201 var end = new Date(now - offset * 60 * 1000).toISOString().substr(0, 19); // 2018-06-13T16:52:30.995Z
202 var begin = new Date(now - days * 60*60*24 * 1000 - offset * 60 * 1000).toISOString().substr(0, 19);
203 return '&begin=' + begin + '&end=' + end;
206 function getwidth(element) {
207 return document.getElementById(element).parentElement.clientWidth * 80 / 100;
210 dayschart('chart_water_1', 'Der See (Tag)', 1, '%H:%M');
211 dayschart('chart_water_7', 'Der See (Woche)', 7, '%a %d');
212 dayschart('chart_water_31', 'Der See (Monat)', 31, '%Y-%m-%d');
213 dayschart('chart_water_365', 'Der See (Jahr)', 365, '%b %Y');
215 dayschart('chart_air_1', 'Die Luft (Tag)', 1, '%H:%M');
216 dayschart('chart_air_7', 'Die Luft (Woche)', 7, '%a %d');
217 dayschart('chart_air_31', 'Die Luft (Monat)', 31, '%Y-%m-%d');
218 dayschart('chart_air_365', 'Die Luft (Jahr)', 365, '%b %Y');
220 // reload current values
223 var airvalue = document.getElementById('currentairvalue');
224 var airtime = document.getElementById('currentairtime');
225 var watervalue = document.getElementById('currentwatervalue');
226 var watertime = document.getElementById('currentwatertime');
227 fetch(api_url_currentair)
228 .then((resp) => resp.json())
229 .then(function(data) {
230 airvalue.innerText = data['value'].toFixed(1);
231 airtime.innerText = strftime('%Y-%m-%d %H:%M', new Date(data['timestamp']));
233 .catch(function(error) {
236 fetch(api_url_currentwater)
237 .then((resp) => resp.json())
238 .then(function(data) {
239 watervalue.innerText = data['value'].toFixed(1);
240 watertime.innerText = strftime('%Y-%m-%d %H:%M', new Date(data['timestamp']));
242 .catch(function(error) {