changeset 949:fa8710124ff0

pilight read dht sensor Ignore-this: fdaa9e3d1fead200485c939ac760fa9d darcs-hash:20140712233539-312f9-417b38cc72fe500bb06792dabd316060a1baadc9
author drewp <drewp@bigasterisk.com>
date Sat, 12 Jul 2014 16:35:39 -0700
parents faefd71c45ae
children 44fc68ca5c92
files service/pilight/pilight.go
diffstat 1 files changed, 27 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/service/pilight/pilight.go	Sat Jul 12 16:35:28 2014 -0700
+++ b/service/pilight/pilight.go	Sat Jul 12 16:35:39 2014 -0700
@@ -9,6 +9,9 @@
 import "color/hex"
 import "strconv"
 import "time"
+import "net/textproto"
+import "bufio"
+import "errors"
 
 const ledCount = 3
 
@@ -59,6 +62,23 @@
 	return b.Write(0, bytes)
 }
 
+func (b *Board) ReadDht() (string, error) {
+	for try := 0; try < 5; try++ {
+		err := b.Write(0x2, make([]byte, 0))
+		if err != nil {
+			continue
+		}
+
+		reader := textproto.NewReader(bufio.NewReader(b.ser))
+		json, err := reader.ReadLine()
+		if err != nil {
+			continue
+		}
+		return json, nil
+	}
+	return "", errors.New("failed after all retries")
+}
+
 func getBodyStringColor(req *http.Request) (c color.Color, err error) {
 	bytes := make([]byte, 1024)
 	n, err := req.Body.Read(bytes)
@@ -113,6 +133,13 @@
 
 		return 200, "ok"
 	})
+	m.Get("/dht", func() (int, string) {
+		json, err := board.ReadDht()
+		if err != nil {
+			return 500, ""
+		}
+		return 200, json
+	})
 	log.Printf("serving")
 	m.Run()
 }