Полностью новая версия веб-интерфейса на базе chart.js
[weathermon.git] / web / meteo.js
diff --git a/web/meteo.js b/web/meteo.js
new file mode 100644 (file)
index 0000000..ee8947e
--- /dev/null
@@ -0,0 +1,130 @@
+urlbase="/meteo/cgi/"
+
+currentState=""
+
+function getCookie(name) {
+  var matches = document.cookie.match(new RegExp(
+    "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
+  ));
+  return matches ? decodeURIComponent(matches[1]) : undefined;
+}
+
+function RefreshPageState() {
+
+  var req = new XMLHttpRequest();
+
+  req.onreadystatechange = function () {
+
+    if (this.readyState != 4) return;
+    if (this.status == 200) {
+
+      var returnedData = JSON.parse(this.responseText);
+
+      var template = document.getElementById("template").innerHTML;
+      var divider_template = document.getElementById("divider-template").innerHTML;
+      var value_color = document.getElementById("value").style.color;
+
+      var html = "";
+
+      for (var place in returnedData) {  
+        var divider = divider_template.replace(/\$PLACE/g,place);
+        html = html + divider;
+        for (var sensor_id in returnedData[place]) {
+          var timestamp = returnedData[place][sensor_id]["timestamp"];
+          for (var sensor in returnedData[place][sensor_id]) {
+            if (sensor != "timestamp") {
+              for (var param in returnedData[place][sensor_id][sensor]) {
+                sensor_path = sensor_id+"."+sensor+"."+param;
+                name = properties["names"][sensor_path];
+                if (! name.startsWith("-")) {
+                  value = returnedData[place][sensor_id][sensor][param];
+                  if (! name) { name = sensor_path; }
+                  units = properties["units"][sensor_path];
+                  scale = properties["scale"][sensor_path];
+                  color = properties["colors"][sensor_path];
+                  if (scale) {
+                    value = (scale[0] * value).toFixed(scale[1]);
+                  }
+                  if (! color) {
+                    color =  value_color;
+                  }
+                  var section = template.replace(/\$SENSOR_ID/g,sensor_id);
+                  section = section.replace(/\$SENSOR/g,sensor);
+                  section = section.replace(/\$PARAM/g,param);
+                  section = section.replace(/\$NAME/g,name);
+                  section = section.replace(/\$UNITS/g,units);
+                  section = section.replace(/\$VALUE/g,value);
+                  section = section.replace(/\$COLOR/g,color);
+                  section = section.replace(/\$TIMESTAMP/g,timestamp);
+                  html = html + section;
+                }
+              }
+            }
+          }  
+        }
+      }
+      
+      document.getElementById("meteo").innerHTML = html;
+
+      setTimeout(RefreshPageState,60000)
+    } else {
+      setTimeout(RefreshPageState,60000)
+    }    
+  };
+
+  req.open("GET", urlbase+"state", true);
+  req.withCredentials = true;
+  req.send();
+
+}
+
+function GetProperties() {
+  var req = new XMLHttpRequest();
+
+  req.onreadystatechange = function () {
+    if (this.readyState != 4) return; 
+    if (this.status != 200) {
+      setTimeout(GetProperties,30000);
+      return;
+    }
+    properties = JSON.parse(this.responseText);
+    RefreshPageState();
+  };
+
+  req.open("GET", urlbase+"props", true);
+  req.withCredentials = true;
+  req.send();
+
+}
+
+function GetAuth() {
+  var req = new XMLHttpRequest();
+
+  req.onreadystatechange = function () {
+    if (this.readyState != 4) return; 
+    if (this.status != 200) {
+      setTimeout(GetAuth,30000);
+      return;
+    }
+    location.reload(); 
+  };
+
+  req.open("GET", "auth", true);
+  req.withCredentials = true;
+  req.send();
+
+}
+
+function CheckCookie() {
+
+  if (getCookie("auth-token")) {
+  
+    authDiv = document.getElementById("auth");
+    authDiv.innerHTML = "";
+
+  }
+
+}
+
+setTimeout(GetProperties,100);
+setTimeout(CheckCookie,200);