weather/main.go

63 lines
1 KiB
Go
Raw Normal View History

2019-08-13 20:14:09 +00:00
package main
import (
"fmt"
"flag"
"os"
"time"
)
var (
URI = "https://api.openweathermap.org/data/2.5"
CONFIG = flag.String("config", "", "config file path")
DAEMON = flag.Bool("daemon", false, "run in daemon mode")
HELP = flag.Bool("help", false, "print this help message")
)
func init() {
flag.Parse()
}
func main() {
var w Weather
if *HELP {
flag.PrintDefaults()
os.Exit(1)
}
if *CONFIG == "" {
fmt.Println("you must specify a config file with -config option")
os.Exit(1)
}
err := w.Config.Load(*CONFIG)
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(2)
}
for {
startTime := time.Now().Unix()
w.FetchData()
err = w.SendToInfluxDB()
if err != nil {
fmt.Printf("%s\n", err)
}
if *DAEMON == false {
if err == nil {
os.Exit(0)
} else {
os.Exit(2)
}
}
sleepTime := time.Now().Unix() - startTime + w.Config.Interval
if sleepTime > 0 {
time.Sleep(time.Duration(sleepTime) * time.Second)
}
}
}