Browse Source

Watersensor: Add module for my watersensor

master
Elis Hirwing 6 months ago
parent
commit
316f8d5c7b
Signed by: Elis Hirwing <elis@hirwing.se> GPG Key ID: D57EFA625C9A925F
4 changed files with 60 additions and 0 deletions
  1. 8
    0
      main.go
  2. 4
    0
      src/config/config.go
  3. 44
    0
      src/watersensor/watersensor.go
  4. 4
    0
      status.sample.toml

+ 8
- 0
main.go View File

@@ -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")
}

+ 4
- 0
src/config/config.go View File

@@ -27,6 +27,10 @@ type Config struct {
RefreshDelay int64
CityName string
}
Watersensor struct {
Host string
RefreshDelay int64
}
}

func New(configFile string) Config {

+ 44
- 0
src/watersensor/watersensor.go View File

@@ -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
}

+ 4
- 0
status.sample.toml View File

@@ -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

Loading…
Cancel
Save