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