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)
86 format: function (d) {
87 var strftimeDE = strftime.localizeByIdentifier('de_DE');
88 return strftimeDE(xtickformat, new Date(d));
91 multiline: true, // broken? so →
97 text: 'Temperatur in °C',
98 position: 'outer-middle'
101 format: function (d) {
102 return d.toFixed(1) + '°';
110 onzoomend: function (domain) {
111 var duration = (domain[1] - domain[0]) / 1000; // duration is now the diff of the "viewport in seconds"
113 var days = duration / 86400;
115 loaddays(this, element, days); // this = chart
122 // TODO: maybe move to top
123 name: function (name, ratio, id, index) {
125 if (element.substr(0, 7) == 'chart_w') {
131 var result = re.exec(name);
132 return prefix + result;
134 value: function (value, ratio, id, index) {
135 return value.toFixed(2) + '°';
137 title: function (datetime) {
138 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString
141 year: 'numeric', month: 'numeric', day: 'numeric',
142 hour: 'numeric', minute: 'numeric', second: 'numeric',
143 timeZone: 'Europe/Vienna', timeZoneName: 'short'
145 return datetime.toLocaleString('de-AT', options);
158 {axis: 'y', start: -999, end: 0, class: 'region-frozen'},
159 {axis: 'y', start: 0, end: 25, class: 'region-ok'},
160 {axis: 'y', start: 25, end: 100, class: 'region-warm'},
161 {axis: 'y', start: 100, end: 999, class: 'region-boiling'}
165 // workaround for unrendered dots
166 setTimeout(function () { chart.flush(); }, 5000);
168 // reload data in charts
171 loaddays(chart, element, days);
177 function loaddays(chart, element, days) {
178 // TODO: maybe move to top or factor out somehow
179 if (element.substr(0, 7) == 'chart_w') {
181 url: api_base_url_sensors + beginend(days),
187 url: api_base_url_weather + beginend(days),
195 function beginend(days) {
196 var now = Date.now();
198 // TODO: this timezone calculation relies on the browser being in Europe/Vienna
199 var refdate = new Date(now);
200 var offset = refdate.getTimezoneOffset(); // -120 for UTC+2
202 var end = new Date(now - offset * 60 * 1000).toISOString().substr(0, 19); // 2018-06-13T16:52:30.995Z
203 var begin = new Date(now - days * 60*60*24 * 1000 - offset * 60 * 1000).toISOString().substr(0, 19);
204 return '&begin=' + begin + '&end=' + end;
207 function getwidth(element) {
208 return document.getElementById(element).parentElement.clientWidth * 80 / 100;
211 dayschart('chart_water_1', 'Der See (Tag)', 1, '%H:%M');
212 dayschart('chart_water_7', 'Der See (Woche)', 7, '%a %d');
213 dayschart('chart_water_31', 'Der See (Monat)', 31, '%Y-%m-%d');
214 dayschart('chart_water_365', 'Der See (Jahr)', 365, '%b %Y');
216 dayschart('chart_air_1', 'Die Luft (Tag)', 1, '%H:%M');
217 dayschart('chart_air_7', 'Die Luft (Woche)', 7, '%a %d');
218 dayschart('chart_air_31', 'Die Luft (Monat)', 31, '%Y-%m-%d');
219 dayschart('chart_air_365', 'Die Luft (Jahr)', 365, '%b %Y');
221 // reload current values
224 var airvalue = document.getElementById('currentairvalue');
225 var airtime = document.getElementById('currentairtime');
226 var watervalue = document.getElementById('currentwatervalue');
227 var watertime = document.getElementById('currentwatertime');
228 fetch(api_url_currentair)
229 .then((resp) => resp.json())
230 .then(function(data) {
231 airvalue.innerText = data['value'].toFixed(1);
232 airtime.innerText = strftime('%Y-%m-%d %H:%M', new Date(data['timestamp']));
234 .catch(function(error) {
237 fetch(api_url_currentwater)
238 .then((resp) => resp.json())
239 .then(function(data) {
240 watervalue.innerText = data['value'].toFixed(1);
241 watertime.innerText = strftime('%Y-%m-%d %H:%M', new Date(data['timestamp']));
243 .catch(function(error) {