Транзакционная работа с БД для избежания блокировок.
[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               name = properties["names"][sensor_path];
28               if ((! name.startsWith("-")) && (name != "undefined")) {
29                 value = returnedData[sensor_id][sensor][param];
30                 if (! name) { name = sensor_path; }
31                 units = properties["units"][sensor_path];
32                 scale = properties["scale"][sensor_path];
33                 color = properties["colors"][sensor_path];
34                 if (scale) {
35                   value = (scale[0] * value).toFixed(scale[1]);
36                 }
37                 if (! color) {
38                   color =  value_color;
39                 }
40                 var section = template.replace(/\$SENSOR_ID/g,sensor_id);
41                 section = section.replace(/\$SENSOR/g,sensor);
42                 section = section.replace(/\$PARAM/g,param);
43                 section = section.replace(/\$NAME/g,name);
44                 section = section.replace(/\$UNITS/g,units);
45                 section = section.replace(/\$VALUE/g,value);
46                 section = section.replace(/\$COLOR/g,color);
47                 section = section.replace(/\$TIMESTAMP/g,timestamp);
48                 html = html + section;
49               }
50             }
51           }  
52         }
53       }
54       
55       document.getElementById("meteo").innerHTML = html;
56
57       setTimeout(RefreshPageState,5000)
58     } else {
59       setTimeout(RefreshPageState,15000)
60     }    
61   };
62
63   req.open("GET", urlbase+"state", true);
64   req.send();
65
66 }
67
68 function GetProperties() {
69   var req = new XMLHttpRequest();
70
71   req.onreadystatechange = function () {
72     if (this.readyState != 4) return; 
73     if (this.status != 200) {
74       setTimeout(GetProperties,30000);
75       return;
76     }
77     properties = JSON.parse(this.responseText);
78     RefreshPageState();
79   };
80
81   req.open("GET", urlbase+"props", true);
82   req.send();
83
84 }
85
86
87 setTimeout(GetProperties,100)