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 (
|
|
|
|
"fmt"
|
|
|
|
"flag"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-08-22 17:47:25 +00:00
|
|
|
// Options and OpenWeatherMap URI
|
2019-08-13 20:14:09 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|