weather/main.go

80 lines
1.6 KiB
Go
Raw Permalink Normal View History

2019-08-22 17:37:45 +00:00
/*
Copyright 2019 Adrien Waksberg
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2019-08-13 20:14:09 +00:00
package main
import (
2019-08-22 20:44:50 +00:00
"flag"
"fmt"
"os"
"time"
2019-08-13 20:14:09 +00:00
)
2019-08-22 17:47:25 +00:00
// Options and OpenWeatherMap URI
2019-08-13 20:14:09 +00:00
var (
2019-08-22 20:44:50 +00:00
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")
2019-08-13 20:14:09 +00:00
)
func init() {
2019-08-22 20:44:50 +00:00
flag.Parse()
2019-08-13 20:14:09 +00:00
}
func main() {
2019-08-22 20:44:50 +00:00
var w Weather
2019-08-13 20:14:09 +00:00
2019-08-22 20:44:50 +00:00
if *HELP {
flag.PrintDefaults()
os.Exit(1)
}
2019-08-13 20:14:09 +00:00
2019-08-22 20:44:50 +00:00
if *CONFIG == "" {
fmt.Println("you must specify a config file with -config option")
os.Exit(1)
}
2019-08-13 20:14:09 +00:00
2019-08-22 20:44:50 +00:00
err := w.Config.Load(*CONFIG)
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(2)
}
2019-08-13 20:14:09 +00:00
2019-08-22 20:44:50 +00:00
for {
startTime := time.Now().Unix()
2019-08-13 20:14:09 +00:00
2019-08-22 20:44:50 +00:00
w.FetchData()
err = w.SendToInfluxDB()
if err != nil {
fmt.Printf("%s\n", err)
}
2019-08-13 20:14:09 +00:00
2019-08-22 20:44:50 +00:00
if *DAEMON == false {
if err == nil {
os.Exit(0)
} else {
os.Exit(2)
}
}
2019-08-13 20:14:09 +00:00
2019-08-22 20:44:50 +00:00
sleepTime := time.Now().Unix() - startTime + w.Config.Interval
if sleepTime > 0 {
time.Sleep(time.Duration(sleepTime) * time.Second)
}
}
2019-08-13 20:14:09 +00:00
}