diff service/pilight/static/index.html @ 957:443aa07b81c8

try to read current led setting at page load. sometimes fails, sometimes readss an old value Ignore-this: 406031419412f0674bb7019f19e3c39f darcs-hash:20140717055858-312f9-62b960fa1aed71c78b051f129d4228894e108cfb
author drewp <drewp@bigasterisk.com>
date Wed, 16 Jul 2014 22:58:58 -0700
parents 8d6e92564fe8
children
line wrap: on
line diff
--- a/service/pilight/static/index.html	Sat Jul 12 23:15:35 2014 -0700
+++ b/service/pilight/static/index.html	Wed Jul 16 22:58:58 2014 -0700
@@ -17,9 +17,11 @@
   <body>
 
     <polymer-element name="led-output" attributes="url color">
-      <!-- calls PUT <url> with color string as the body, but also
-      avoids sending many repeated calls to url while the first one
-      is still in progress -->
+      <!--
+      First fetches the first led color and writes it on the color
+      attribute, then whenever color changes, calls PUT <url> with color
+      string as the body, but also avoids sending many repeated calls
+      to url while the first one is still in progress -->
       <template>
         {{status}}
       </template>
@@ -27,8 +29,34 @@
           created: function() {
               this.pending = {}; // url: data
               this.inflight = false;
+              this.enableWrites = false;
+
+              var retryFetch = function (triesLeft) {
+                  if (triesLeft < 1) {
+                      return;
+                  }
+                  $.ajax({
+                      type: "GET",
+                      url: "led",
+                      dataType: "json",
+                      success: function(result) {
+                          if (!result.length) {
+                              setTimeout(function() {
+                                  retryFetch(triesLeft - 1);
+                              }.bind(this), 100);
+                              return;
+                          }
+                          this.color = result[0];
+                          this.enableWrites = true;
+                      }.bind(this)
+                  });
+              }.bind(this);
+              retryFetch(5);
           },
           colorChanged: function() {
+              if (!this.enableWrites) {
+                  return;
+              }
               this.pending["led"] = this.color;
               this.sendMore();
           },