diff --git a/main.go b/main.go index 8fb5d36..e86e736 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "./src/config" "./src/pollen" "./src/sl" + "./src/watersensor" "./src/weather" tm "github.com/buger/goterm" "time" @@ -16,6 +17,7 @@ func main() { clock := clock.Clock{Config: &config} pollen := pollen.Pollen{Config: &config} sl := sl.SL{Config: &config} + watersensor := watersensor.Watersensor{Config: &config} weather := weather.Weather{Config: &config} tm.Clear() @@ -42,6 +44,12 @@ func main() { tm.Print("\033[K\n") + for _, row := range watersensor.GetOutput() { + tm.Print(row + "\033[K\n") + } + + tm.Print("\033[K\n") + for _, row := range sl.GetOutput() { tm.Print(row + "\033[K\n") } diff --git a/src/config/config.go b/src/config/config.go index dca51f2..dfb1488 100644 --- a/src/config/config.go +++ b/src/config/config.go @@ -27,6 +27,10 @@ type Config struct { RefreshDelay int64 CityName string } + Watersensor struct { + Host string + RefreshDelay int64 + } } func New(configFile string) Config { diff --git a/src/watersensor/watersensor.go b/src/watersensor/watersensor.go new file mode 100644 index 0000000..be534e0 --- /dev/null +++ b/src/watersensor/watersensor.go @@ -0,0 +1,44 @@ +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 +} diff --git a/status.sample.toml b/status.sample.toml index 6c3164d..5b32d8d 100644 --- a/status.sample.toml +++ b/status.sample.toml @@ -15,3 +15,7 @@ RefreshDelay = 1800 URL = "https://pollenkoll.se/wp-content/themes/pollenkoll/api/get_cities.php" RefreshDelay = 1800 CityName = "Stockholm" + +[Watersensor] +Host = "example.com" +RefreshDelay = 1800