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.

45 lines
948B

  1. package watersensor
  2. import (
  3. "../config"
  4. "bufio"
  5. "fmt"
  6. "net"
  7. "strings"
  8. "time"
  9. )
  10. type Watersensor struct {
  11. Config *config.Config
  12. lastResponse string
  13. nextRefresh int64
  14. }
  15. func (watersensor *Watersensor) GetOutput() []string {
  16. if watersensor.nextRefresh <= time.Now().Unix() {
  17. watersensor.nextRefresh += 10
  18. go watersensor.getData()
  19. }
  20. return strings.Split(watersensor.lastResponse, "\n")
  21. }
  22. func (watersensor *Watersensor) getData() {
  23. mySensorLevel, err := watersensor.getSensorLevel()
  24. if err == nil {
  25. watersensor.lastResponse = mySensorLevel
  26. watersensor.nextRefresh = time.Now().Unix() + watersensor.Config.Watersensor.RefreshDelay
  27. } else {
  28. // log.Fatal(err)
  29. }
  30. }
  31. func (watersensor *Watersensor) getSensorLevel() (string, error) {
  32. conn, _ := net.Dial("tcp", watersensor.Config.Watersensor.Host+":80")
  33. fmt.Fprintf(conn, "GET /\r\n\r\n")
  34. status, _ := bufio.NewReader(conn).ReadString('\n')
  35. return status, nil
  36. }