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'
33 type: 'line', // default
36 order: function (a, b) {
37 // gets 2 x objects. seems to be a "sort function" for js's sort()
38 // -1: a before b etc.
39 // only handle cases that appear in the wild
40 // console.log("a.id", a.id, "b.id", b.id);
41 if (a.id == '0416a1ac66ff') {return -1};
42 if (a.id == '0416a1bab9ff') {return 1};
48 url: api_base_url_weather + beginend(days),
51 '3319578': '3319578_x'
54 '3319578': 'Lufttemperatur in Obsteig'
56 xFormat: '%Y-%m-%d %H:%M:%S',
63 var chart = c3.generate({
64 bindto: '#' + element,
66 width: getwidth(element)
68 onresized: function () {
70 width: getwidth(element)
87 format: function (d) {
88 var strftimeDE = strftime.localizeByIdentifier('de_DE');
89 return strftimeDE(xtickformat, new Date(d));
92 multiline: true, // broken? so →
98 text: 'Temperatur in °C',
99 position: 'outer-middle'
102 format: function (d) {
103 return d.toFixed(1) + '°';
111 onzoomend: function (domain) {
112 var duration = (domain[1] - domain[0]) / 1000; // duration is now the diff of the "viewport in seconds"
114 var days = duration / 86400;
116 loaddays(this, element, days); // this = chart
123 // TODO: maybe move to top
124 name: function (name, ratio, id, index) {
126 if (element.substr(0, 7) == 'chart_w') {
132 var result = re.exec(name);
133 return prefix + result;
135 value: function (value, ratio, id, index) {
136 return value.toFixed(2) + '°';
138 title: function (datetime) {
139 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString
142 year: 'numeric', month: 'numeric', day: 'numeric',
143 hour: 'numeric', minute: 'numeric', second: 'numeric',
144 timeZone: 'Europe/Vienna', timeZoneName: 'short'
146 return datetime.toLocaleString('de-AT', options);
159 {axis: 'y', start: -999, end: 0, class: 'region-frozen'},
160 {axis: 'y', start: 0, end: 25, class: 'region-ok'},
161 {axis: 'y', start: 25, end: 100, class: 'region-warm'},
162 {axis: 'y', start: 100, end: 999, class: 'region-boiling'}
166 // workaround for unrendered dots
167 setTimeout(function () { chart.flush(); }, 5000);
169 // reload data in charts
172 loaddays(chart, element, days);
178 function loaddays(chart, element, days) {
179 // TODO: maybe move to top or factor out somehow
180 if (element.substr(0, 7) == 'chart_w') {
182 url: api_base_url_sensors + beginend(days),
188 url: api_base_url_weather + beginend(days),
193 setTimeout(function () { chart.flush(); }, 5000);
196 function beginend(days) {
197 var now = Date.now();
199 // TODO: this timezone calculation relies on the browser being in Europe/Vienna
200 var refdate = new Date(now);
201 var offset = refdate.getTimezoneOffset(); // -120 for UTC+2
203 var end = new Date(now - offset * 60 * 1000).toISOString().substr(0, 19); // 2018-06-13T16:52:30.995Z
204 var begin = new Date(now - days * 60*60*24 * 1000 - offset * 60 * 1000).toISOString().substr(0, 19);
205 return '&begin=' + begin + '&end=' + end;
208 function getwidth(element) {
209 return document.getElementById(element).parentElement.clientWidth * 80 / 100;
212 dayschart('chart_water_1', 'Der See (Tag)', 1, '%H:%M');
213 dayschart('chart_water_7', 'Der See (Woche)', 7, '%a %d');
214 dayschart('chart_water_31', 'Der See (Monat)', 31, '%Y-%m-%d');
215 dayschart('chart_water_365', 'Der See (Jahr)', 365, '%b %Y');
217 dayschart('chart_air_1', 'Die Luft (Tag)', 1, '%H:%M');
218 dayschart('chart_air_7', 'Die Luft (Woche)', 7, '%a %d');
219 dayschart('chart_air_31', 'Die Luft (Monat)', 31, '%Y-%m-%d');
220 dayschart('chart_air_365', 'Die Luft (Jahr)', 365, '%b %Y');
222 // reload current values + owm script
225 var airvalue = document.getElementById('currentairvalue');
226 var airtime = document.getElementById('currentairtime');
227 var watervalue = document.getElementById('currentwatervalue');
228 var watertime = document.getElementById('currentwatertime');
229 fetch(api_url_currentair)
230 .then((resp) => resp.json())
231 .then(function(data) {
232 airvalue.innerText = data['value'].toFixed(1);
233 airtime.innerText = strftime('%Y-%m-%d %H:%M', new Date(data['timestamp']));
235 .catch(function(error) {
238 fetch(api_url_currentwater)
239 .then((resp) => resp.json())
240 .then(function(data) {
241 watervalue.innerText = data['value'].toFixed(1);
242 watertime.innerText = strftime('%Y-%m-%d %H:%M', new Date(data['timestamp']));
244 .catch(function(error) {
248 var owmwidget = document.getElementById('openweathermap-widget-15');
249 owmwidget.removeChild(owmwidget.childNodes[0]);
250 var owmscript = document.head.lastElementChild;
251 document.head.removeChild(owmscript);
252 var newowmscript = document.createElement('script');
253 newowmscript.src = '//openweathermap.org/themes/openweathermap/assets/vendor/owm/js/weather-widget-generator.js';
254 document.head.appendChild(newowmscript);