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