changeset 152:2a38f2620e5b

try to read current led setting at page load. sometimes fails, sometimes readss an old value Ignore-this: 406031419412f0674bb7019f19e3c39f
author drewp@bigasterisk.com
date Wed, 16 Jul 2014 22:58:58 -0700
parents 4de6c7e5e959
children f8c5ec994768
files service/pilight/pilight.go service/pilight/static/big-picker.html service/pilight/static/index.html
diffstat 3 files changed, 86 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/service/pilight/pilight.go	Sat Jul 12 23:15:35 2014 -0700
+++ b/service/pilight/pilight.go	Wed Jul 16 22:58:58 2014 -0700
@@ -12,6 +12,7 @@
 import "net/textproto"
 import "bufio"
 import "errors"
+import "encoding/json"
 
 const ledCount = 3
 
@@ -79,6 +80,36 @@
 	return "", errors.New("failed after all retries")
 }
 
+
+func (b *Board) ReadLeds() (colors []color.Color, err error) {
+	err = b.Write(0x3, make([]byte, 0))
+	if err != nil {
+		return
+	}
+	reader := textproto.NewReader(bufio.NewReader(b.ser))
+	line, err := reader.ReadLineBytes()
+	if err != nil {
+		return
+	}
+
+	type LedsMessage struct {
+		Leds []string
+	}
+	var ret LedsMessage
+	err = json.Unmarshal(line, &ret)
+	if err != nil {
+		return
+	}
+
+	colors = make([]color.Color, len(ret.Leds))
+	for i, c := range ret.Leds {
+		r, g, b := hex.HexToRGB(hex.Hex(c))
+		colors[i] = color.RGBA{r, g, b, 0}
+	}
+	
+	return colors, nil
+}
+
 func getBodyStringColor(req *http.Request) (c color.Color, err error) {
 	bytes := make([]byte, 1024)
 	n, err := req.Body.Read(bytes)
@@ -117,8 +148,27 @@
 		
 		return 200, "ok"
 	})
-	m.Get("/led", func() string {
-		return "['#ff0000', '#ff0000']"
+	m.Get("/led", func() (int, string) {
+
+		colors, err := board.ReadLeds()
+		if err != nil {
+			return 500, ""
+		}
+		hexColors := make([]hex.Hex, len(colors))
+		for i, c := range colors {
+			// hex.HexModel.Convert(c) seems like the
+			// right call, but fails because it returns
+			// Color not string
+			r, g, b, _ := c.RGBA()
+			hexColors[i] = hex.RGBToHex(uint8(r), uint8(g), uint8(b))
+		}
+
+		// content-type json
+		j, err := json.Marshal(hexColors)
+		if err != nil {
+			return 500, ""
+		}
+		return 200, string(j)
 	})
 	m.Put("/led", func(req *http.Request) (int, string) {
 		color, err := getBodyStringColor(req)
--- a/service/pilight/static/big-picker.html	Sat Jul 12 23:15:35 2014 -0700
+++ b/service/pilight/static/big-picker.html	Wed Jul 16 22:58:58 2014 -0700
@@ -82,6 +82,9 @@
             var c = Color(e.target.templateInstance.model.c).setLightness(this.lightness);
             this.hex = c.toString();
         },
+        hexChanged: function() {
+            this.lightness = Color(this.hex).getLightness();
+        },
         lightnessChanged: function() {
             var c = Color(this.hex).setLightness(this.lightness);
             this.hex = c.toString();
--- 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();
           },