You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

44 lines
948 B

package watersensor
import (
"../config"
"bufio"
"fmt"
"net"
"strings"
"time"
)
type Watersensor struct {
Config *config.Config
lastResponse string
nextRefresh int64
}
func (watersensor *Watersensor) GetOutput() []string {
if watersensor.nextRefresh <= time.Now().Unix() {
watersensor.nextRefresh += 10
go watersensor.getData()
}
return strings.Split(watersensor.lastResponse, "\n")
}
func (watersensor *Watersensor) getData() {
mySensorLevel, err := watersensor.getSensorLevel()
if err == nil {
watersensor.lastResponse = mySensorLevel
watersensor.nextRefresh = time.Now().Unix() + watersensor.Config.Watersensor.RefreshDelay
} else {
// log.Fatal(err)
}
}
func (watersensor *Watersensor) getSensorLevel() (string, error) {
conn, _ := net.Dial("tcp", watersensor.Config.Watersensor.Host+":80")
fmt.Fprintf(conn, "GET /\r\n\r\n")
status, _ := bufio.NewReader(conn).ReadString('\n')
return status, nil
}