Mercurial > code > home > repos > homeauto
changeset 144:2e4a9204f23b
pilight read dht sensor
Ignore-this: fdaa9e3d1fead200485c939ac760fa9d
author | drewp@bigasterisk.com |
---|---|
date | Sat, 12 Jul 2014 16:35:39 -0700 |
parents | 30d0ba11603a |
children | 7233fdb6b30a |
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() }