Sfoglia il codice sorgente

Watersensor: Add module for my watersensor

master
Elis Hirwing 2 anni fa
parent
commit
316f8d5c7b
Firmato da: etu ID Chiave GPG: D57EFA625C9A925F
  1. 8
      main.go
  2. 4
      src/config/config.go
  3. 44
      src/watersensor/watersensor.go
  4. 4
      status.sample.toml

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

4
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 {

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

4
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
Caricamento…
Annulla
Salva