c762fb0d31b000d41f952f80052906eb530cea19
[chrisu/seepark.git] / web / static / seepark_web.js
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
4 function dayschart(element, title, days, xtickformat) {
5         var chartdata;
6         if (element.substr(0, 7) == 'chart_w') {
7                 chartdata = {
8                                 url: api_base_url_sensors + beginend(days),
9                                 mimeType: 'json',
10                                 xs: {
11                                         '0416a1ac66ff': '0416a1ac66ff_x',
12                                         '0516a207a4ff': '0516a207a4ff_x',
13                                         '0416a1bab9ff': '0416a1bab9ff_x',
14                                         '0316a2193bff': '0316a2193bff_x'
15                                 },
16                                 names: {
17                                         '0416a1ac66ff': 'Wassertemperatur 30cm Tiefe',
18                                         '0516a207a4ff': 'Wassertemperatur 50cm Tiefe',
19                                         '0416a1bab9ff': 'Wassertemperatur 1m Tiefe',
20                                         '0316a2193bff': 'Wassertemperatur in 30 cm Tiefe'
21                                 },
22                                 xFormat: '%Y-%m-%d %H:%M:%S',
23                                 // https://en.wikipedia.org/wiki/Web_colors
24                                 colors: {
25                                         '0416a1ac66ff': 'lightskyblue',
26                                         '0516a207a4ff': 'royalblue',
27                                         '0416a1bab9ff': 'mediumblue',
28                                         '0316a2193bff': 'midnightblue'
29                                 },
30                                 /*
31                                 xSort: false,
32                                 order: function (a, b) {
33                                         // gets 2 x objects. seems to be a "sort function" for js's sort()
34                                         // -1: a before b etc.
35                                         // only handle cases that appear in the wild
36                                         // console.log("a.id", a.id, "b.id", b.id);
37                                         if (a.id == '0416a1ac66ff') {return -1};
38                                         if (a.id == '0416a1bab9ff') {return  1};
39                                 }
40                                 */
41                 };
42         } else {
43                 chartdata = {
44                                 url: api_base_url_weather + beginend(days),
45                                 mimeType: 'json',
46                                 xs: {
47                                         '3319578': '3319578_x'
48                                 },
49                                 names: {
50                                         '3319578': 'Lufttemperatur in Obsteig'
51                                 },
52                                 xFormat: '%Y-%m-%d %H:%M:%S',
53                                 colors: {
54                                         '3319578': 'red'
55                                 }
56                 };
57         };
58         var chart = c3.generate({
59                 bindto: '#' + element,
60                 size: {
61                         width: getwidth(element)
62                 },
63                 onresized: function () {
64                         chart.resize({
65                                 width: getwidth(element)
66                         });
67                         // chart.flush();
68                 },
69                 transition: {
70                         duration: null
71                 },
72                 title: {
73                         text: title
74                 },
75                 data: chartdata,
76                 point: {
77                         r: 1.5 // default 2.5
78                 },
79                 axis: {
80                         x: {
81                                 type: 'timeseries',
82                                 tick: {
83                                         format: xtickformat,
84                                         fit: false,
85                                         multiline: true, // broken? so →
86                                         rotate: -90
87                                 }
88                         },
89                         y: {
90                                 label: {
91                                         text: 'Temperatur in °C',
92                                         position: 'outer-middle'
93                                 },
94                                 tick: {
95                                         format: function (d) {
96                                                 return d.toFixed(1) + '°';
97                                         }
98                                 }
99                         }
100                 },
101                 zoom: {
102                         enabled: true,
103                         /*
104                         onzoomend: function (domain) {
105                                 // UNIX epoch
106                                 var start = domain[0].getTime()/1000;
107                                 var end   = domain[1].getTime()/1000;
108                                 var duration = end - start;
109                                 // duration is now the diff of the "viewport in seconds"
110                                 // FIXME
111                                 var days = Math.round(duration / 86400 / 2);
112                                 if (days > 0) {
113                                         loaddays(this, element, days); // this = chart
114                                 }
115                         }
116                         */
117                 },
118                 tooltip: {
119                         format: {
120                                 // TODO: maybe move to top
121                                 name: function (name, ratio, id, index) {
122                                         var prefix = '';
123                                         if (element.substr(0, 7) == 'chart_w') {
124                                                 var re = /\d+ ?c?m/;
125                                                 prefix = 'Wasser ';
126                                         } else {
127                                                 var re = /Luft/;
128                                         };
129                                         var result = re.exec(name);
130                                         return prefix + result;
131                                 },
132                                 value: function (value, ratio, id, index) {
133                                         return value.toFixed(2) + '°';
134                                 },
135                                 title: function (datetime) {
136                                         // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString
137                                         options = {
138                                                 weekday: 'short',
139                                                 year: 'numeric', month: 'numeric', day: 'numeric',
140                                                 hour: 'numeric', minute: 'numeric', second: 'numeric',
141                                                 timeZone: 'Europe/Vienna', timeZoneName: 'short'
142                                         };
143                                         return datetime.toLocaleString('de-AT', options);
144                                 }
145                         }
146                 },
147                 grid: {
148                         y: {
149                                 show: true
150                         }
151                 },
152                 regions: [
153                         {axis: 'y', start: -999, end:   0, class: 'region-frozen'},
154                         {axis: 'y', start:    0, end:  25, class: 'region-ok'},
155                         {axis: 'y', start:   25, end: 100, class: 'region-warm'},
156                         {axis: 'y', start:  100, end: 999, class: 'region-boiling'}
157                 ]
158         });
159 };
160
161 function loaddays(chart, element, days) {
162         // TODO: maybe move to top or factor out somehow
163         if (element.substr(0, 7) == 'chart_w') {
164                 chart.load({
165                         url: api_base_url_sensors + beginend(days),
166                         mimeType: 'json'
167                 });
168         } else {
169                 chart.load({
170                         url: api_base_url_weather + beginend(days),
171                         mimeType: 'json'
172                 });
173         }
174 }
175
176 function beginend(days) {
177         var now = Date.now();
178
179         // TODO: this timezone calculation relies on the browser being in Europe/Vienna
180         var refdate = new Date(now);
181         var offset = refdate.getTimezoneOffset(); // -120 for UTC+2
182
183         var end = new Date(now - offset * 60 * 1000).toISOString().substr(0, 19); // 2018-06-13T16:52:30.995Z
184         var begin = new Date(now - days * 60*60*24 * 1000 - offset * 60 * 1000).toISOString().substr(0, 19);
185         return '&begin=' + begin + '&end=' + end;
186 }
187
188 function getwidth(element) {
189         return document.getElementById(element).parentElement.clientWidth * 80 / 100;
190 }
191
192 dayschart('chart_water_1',   'Der See (Tag)',     1, '%H:%M');
193 dayschart('chart_water_7',   'Der See (Woche)',   7, '%a %d');
194 dayschart('chart_water_31',  'Der See (Monat)',  31, '%Y-%m-%d');
195 dayschart('chart_water_365', 'Der See (Jahr)',  365, '%b %Y');
196
197 dayschart('chart_air_1',     'Die Luft (Tag)',     1, '%H:%M');
198 dayschart('chart_air_7',     'Die Luft (Woche)',   7, '%a %d');
199 dayschart('chart_air_31',    'Die Luft (Monat)',  31, '%Y-%m-%d');
200 dayschart('chart_air_365',   'Die Luft (Jahr)',  365, '%b %Y');