f56bc2db61063ba15c7846e3f5c6375e8fc1783d
[weathermon.git] / openwrt-web / meteo / meteo.js
1 urlbase="/meteo/cgi?"
2
3 currentState=""
4
5 function RefreshPageState() {
6
7   var req = new XMLHttpRequest();
8
9   req.onreadystatechange = function () {
10
11     if (this.readyState != 4) return;
12     if (this.status == 200) {
13
14       var returnedData = JSON.parse(this.responseText);
15
16       var template = document.getElementById("template").innerHTML;
17       var value_color = document.getElementById("value").style.color;
18
19       var html = "";
20       
21       for (var sensor_id in returnedData) {
22         var timestamp = returnedData[sensor_id]["timestamp"];
23         for (var sensor in returnedData[sensor_id]) {
24           if (sensor != "timestamp") {
25             for (var param in returnedData[sensor_id][sensor]) {
26               sensor_path = sensor_id+"."+sensor+"."+param;
27               value = returnedData[sensor_id][sensor][param];
28               name = properties["names"][sensor_path];
29               if (! name) { name = sensor_path; }
30               units = properties["units"][sensor_path];
31               scale = properties["scale"][sensor_path];
32               color = properties["colors"][sensor_path];
33               if (scale) {
34                 value = (scale[0] * value).toFixed(scale[1]);
35               }
36               if (! color) {
37                 color =  value_color;
38               }
39               var section = template.replace(/\$SENSOR_ID/g,sensor_id);
40               section = section.replace(/\$SENSOR/g,sensor);
41               section = section.replace(/\$PARAM/g,param);
42               section = section.replace(/\$NAME/g,name);
43               section = section.replace(/\$UNITS/g,units);
44               section = section.replace(/\$VALUE/g,value);
45               section = section.replace(/\$COLOR/g,color);
46               section = section.replace(/\$TIMESTAMP/g,timestamp);
47               html = html + section;
48             }
49           }  
50         }
51       }
52       
53       document.getElementById("meteo").innerHTML = html;
54
55       setTimeout(RefreshPageState,5000)
56     } else {
57       setTimeout(RefreshPageState,15000)
58     }    
59   };
60
61   req.open("GET", urlbase+"state", true);
62   req.send();
63
64 }
65
66 function GetProperties() {
67   var req = new XMLHttpRequest();
68
69   req.onreadystatechange = function () {
70     if (this.readyState != 4) return; 
71     if (this.status != 200) {
72       setTimeout(GetProperties,30000);
73       return;
74     }
75     properties = JSON.parse(this.responseText);
76     RefreshPageState();
77   };
78
79   req.open("GET", urlbase+"props", true);
80   req.send();
81
82 }
83
84
85 setTimeout(GetProperties,100)