9b24b60d96570aec342e6ea6f675b421ba3cfea1
[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                                         '0316a2193bff': '0316a2193bff_x',
12                                         '0316a21383ff': '0316a21383ff_x'
13                                 },
14                                 names: {
15                                         '0316a2193bff': 'Wassertemperatur in 4 m Tiefe',
16                                         '0316a21383ff': 'Wassertemperatur in 5 m Tiefe'
17                                 },
18                                 xFormat: '%Y-%m-%d %H:%M:%S',
19                                 colors: {
20                                         '0316a2193bff': 'lightblue',
21                                         '0316a21383ff': 'darkblue'
22                                 }
23                 };
24         } else {
25                 chartdata = {
26                                 url: api_base_url_weather + beginend(days),
27                                 mimeType: 'json',
28                                 xs: {
29                                         '3319578': '3319578_x'
30                                 },
31                                 names: {
32                                         '3319578': 'Lufttemperatur in Obsteig'
33                                 },
34                                 xFormat: '%Y-%m-%d %H:%M:%S',
35                                 colors: {
36                                         '3319578': 'red'
37                                 }
38                 };
39         };
40         var chart = c3.generate({
41                 bindto: '#' + element,
42                 size: {
43                         width: document.getElementById(element).parentElement.clientWidth * 80 / 100
44                 },
45                 transition: {
46                         duration: null
47                 },
48                 title: {
49                         text: title
50                 },
51                 data: chartdata,
52                 point: {
53                         r: 1.5 // default 2.5
54                 },
55                 axis: {
56                         x: {
57                                 type: 'timeseries',
58                                 tick: {
59                                         format: xtickformat,
60                                         fit: false,
61                                         multiline: true, // broken? so →
62                                         rotate: -90
63                                 }
64                         },
65                         y: {
66                                 label: {
67                                         text: 'Temperatur in °C',
68                                         position: 'outer-middle'
69                                 },
70                                 tick: {
71                                         format: function (d) {
72                                                 return d.toFixed(1) + '°';
73                                         }
74                                 }
75                         }
76                 },
77                 zoom: {
78                         enabled: true,
79                         onzoomend: function (domain) {
80                                 // UNIX epoch
81                                 var start = domain[0].getTime()/1000;
82                                 var end   = domain[1].getTime()/1000;
83                                 var duration = end - start;
84                                 // duration is now the diff of the "viewport in seconds"
85                                 // FIXME
86                                 var days = Math.round(duration / 86400 / 2);
87                                 if (days > 0) {
88                                         loaddays(days);
89                                 }
90                         }
91                 },
92                 tooltip: {
93                         format: {
94                                 // TODO: maybe move to top
95                                 name: function (name, ratio, id, index) {
96                                         var prefix = '';
97                                         if (element.substr(0, 7) == 'chart_w') {
98                                                 var re = /\d m/;
99                                                 prefix = 'Wasser ';
100                                         } else {
101                                                 var re = /Luft/;
102                                         };
103                                         var result = re.exec(name);
104                                         return prefix + result;
105                                 },
106                                 value: function (value, ratio, id, index) {
107                                         return value.toFixed(2) + '°';
108                                 },
109                                 title: function (x) {
110                                         // TODO: format datetime into something readable
111                                         return x;
112                                 }
113                         }
114                 },
115                 grid: {
116                         y: {
117                                 show: true
118                         }
119                 },
120                 regions: [
121                         {axis: 'y', start: -999, end:   0, class: 'region-frozen'},
122                         {axis: 'y', start:    0, end:  25, class: 'region-ok'},
123                         {axis: 'y', start:   25, end: 100, class: 'region-warm'},
124                         {axis: 'y', start:  100, end: 999, class: 'region-boiling'}
125                 ]
126         });
127 };
128
129 function loaddays(days) {
130         chart.load({
131                 url: api_base_url_sensors + beginend(days),
132                 mimeType: 'json'
133         });
134 }
135
136 function beginend(days) {
137         var now = Date.now();
138
139         // TODO: this timezone calculation relies on the browser being in Europe/Vienna
140         var refdate = new Date(now);
141         var offset = refdate.getTimezoneOffset(); // -120 for UTC+2
142
143         var end = new Date(now - offset * 60 * 1000).toISOString().substr(0, 19); // 2018-06-13T16:52:30.995Z
144         var begin = new Date(now - days * 60*60*24 * 1000 - offset * 60 * 1000).toISOString().substr(0, 19);
145         return '&begin=' + begin + '&end=' + end;
146 }
147
148 dayschart('chart_water_1',   'Der See (Tag)',     1, '%H:%M');
149 dayschart('chart_water_7',   'Der See (Woche)',   7, '%a %d');
150 dayschart('chart_water_31',  'Der See (Monat)',  31, 'Woche %V');
151 dayschart('chart_water_365', 'Der See (Jahr)',  365, '%b %Y');
152
153 dayschart('chart_air_1',     'Die Luft (Tag)',     1, '%H:%M');
154 dayschart('chart_air_7',     'Die Luft (Woche)',   7, '%a %d');
155 dayschart('chart_air_31',    'Die Luft (Monat)',  31, 'Woche %V');
156 dayschart('chart_air_365',   'Die Luft (Jahr)',  365, '%b %Y');