Миграция с OpenWeatherMap на OpenMeteo в связи с отключением бесплатного API с 26...
[weathermon.git] / web / meteo.js
1 urlbase="/meteo/cgi/"
2
3 currentState=""
4
5 function getCookie(name) {
6   var matches = document.cookie.match(new RegExp(
7     "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
8   ));
9   return matches ? decodeURIComponent(matches[1]) : undefined;
10 }
11
12 function RefreshPageState() {
13
14   var req = new XMLHttpRequest();
15
16   req.onreadystatechange = function () {
17
18     if (this.readyState != 4) return;
19     if (this.status == 200) {
20
21       var returnedData = JSON.parse(this.responseText);
22
23       var template = document.getElementById("template").innerHTML;
24       var divider_template = document.getElementById("divider-template").innerHTML;
25       var value_color = document.getElementById("value").style.color;
26
27       var html = "";
28
29       for (var place in returnedData) {  
30         var divider = divider_template.replace(/\$PLACE/g,place);
31         html = html + divider;
32         for (var sensor_id in returnedData[place]) {
33           var timestamp = returnedData[place][sensor_id]["timestamp"];
34           for (var sensor in returnedData[place][sensor_id]) {
35             if (sensor != "timestamp") {
36               for (var param in returnedData[place][sensor_id][sensor]) {
37                 sensor_path = sensor_id+"."+sensor+"."+param;
38                 name = properties["names"][sensor_path];
39                 if (! name.startsWith("-")) {
40                   value = returnedData[place][sensor_id][sensor][param];
41                   if (! name) { name = sensor_path; }
42                   units = properties["units"][sensor_path];
43                   scale = properties["scale"][sensor_path];
44                   color = properties["colors"][sensor_path];
45                   if (scale) {
46                     value = (scale[0] * value).toFixed(scale[1]);
47                   }
48                   if (! color) {
49                     color =  value_color;
50                   }
51                   var section = template.replace(/\$SENSOR_ID/g,sensor_id);
52                   section = section.replace(/\$SENSOR/g,sensor);
53                   section = section.replace(/\$PARAM/g,param);
54                   section = section.replace(/\$NAME/g,name);
55                   section = section.replace(/\$UNITS/g,units);
56                   section = section.replace(/\$VALUE/g,value);
57                   section = section.replace(/\$COLOR/g,color);
58                   section = section.replace(/\$TIMESTAMP/g,timestamp);
59                   html = html + section;
60                 }
61               }
62             }
63           }  
64         }
65       }
66       
67       document.getElementById("meteo").innerHTML = html;
68
69       setTimeout(RefreshPageState,60000)
70     } else {
71       setTimeout(RefreshPageState,60000)
72     }    
73   };
74
75   req.open("GET", urlbase+"state", true);
76   req.withCredentials = true;
77   req.send();
78
79 }
80
81 function GetProperties() {
82   var req = new XMLHttpRequest();
83
84   req.onreadystatechange = function () {
85     if (this.readyState != 4) return; 
86     if (this.status != 200) {
87       setTimeout(GetProperties,30000);
88       return;
89     }
90     properties = JSON.parse(this.responseText);
91     RefreshPageState();
92   };
93
94   req.open("GET", urlbase+"props", true);
95   req.withCredentials = true;
96   req.send();
97
98 }
99
100 function GetAuth() {
101   var req = new XMLHttpRequest();
102
103   req.onreadystatechange = function () {
104     if (this.readyState != 4) return; 
105     if (this.status != 200) {
106       setTimeout(GetAuth,30000);
107       return;
108     }
109     location.reload(); 
110   };
111
112   req.open("GET", "auth", true);
113   req.withCredentials = true;
114   req.send();
115
116 }
117
118 function CheckCookie() {
119
120   if (getCookie("auth-token")) {
121   
122     authDiv = document.getElementById("auth");
123     authDiv.innerHTML = "";
124
125   }
126
127 }
128
129 setTimeout(GetProperties,100);
130 setTimeout(CheckCookie,200);