update go/l17.go added check for env value
This commit is contained in:
parent
ae032b959d
commit
74ee16f775
2 changed files with 26 additions and 8 deletions
34
go/l17.go
34
go/l17.go
|
|
@ -23,10 +23,16 @@ type OpenWeather struct{
|
||||||
func WeatherReq() (string, float64) {
|
func WeatherReq() (string, float64) {
|
||||||
err := godotenv.Load()
|
err := godotenv.Load()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("%v\n", err)
|
log.Fatalf("upload your .env file %v\n", err)
|
||||||
}
|
}
|
||||||
api := os.Getenv("openweathermap")
|
api := os.Getenv("openweathermap")
|
||||||
|
if api == "" {
|
||||||
|
log.Fatalf("add your openweathermap api in .env file\n")
|
||||||
|
}
|
||||||
city := os.Getenv("city_openweather")
|
city := os.Getenv("city_openweather")
|
||||||
|
if city == "" {
|
||||||
|
log.Fatalf("add your city for openweathermap in .env file\n")
|
||||||
|
}
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Timeout: 20 * time.Second,
|
Timeout: 20 * time.Second,
|
||||||
}
|
}
|
||||||
|
|
@ -40,6 +46,9 @@ func WeatherReq() (string, float64) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("%v\n", err)
|
log.Fatalf("%v\n", err)
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
log.Fatalf("openweathermap return %d\n", resp.StatusCode)
|
||||||
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
var WeatherData OpenWeather
|
var WeatherData OpenWeather
|
||||||
err = json.NewDecoder(resp.Body).Decode(&WeatherData)
|
err = json.NewDecoder(resp.Body).Decode(&WeatherData)
|
||||||
|
|
@ -56,37 +65,46 @@ func Send(client mqtt.Client, topic string) {
|
||||||
token := client.Publish(topic, 1, false, payload)
|
token := client.Publish(topic, 1, false, payload)
|
||||||
token.Wait()
|
token.Wait()
|
||||||
if err := token.Error(); err != nil {
|
if err := token.Error(); err != nil {
|
||||||
log.Fatalf("%v\n", token.Error)
|
log.Fatalf("%v\n", token.Error())
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("Sent in", topic)
|
fmt.Println("Sent in", topic)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
opts := mqtt.NewClientOptions()
|
||||||
|
|
||||||
err := godotenv.Load()
|
err := godotenv.Load()
|
||||||
if err != nil { log.Fatalf("%v\n", err) }
|
if err != nil { log.Fatalf("%v\n", err) }
|
||||||
broker := os.Getenv("broker_mqtt")
|
broker := os.Getenv("broker_mqtt")
|
||||||
|
if broker == "" { log.Fatalf("add yout broker in .env file\n") }
|
||||||
ClientID := os.Getenv("clientid_mqtt")
|
ClientID := os.Getenv("clientid_mqtt")
|
||||||
|
if ClientID == "" { log.Fatalf("add your client id in .env file") }
|
||||||
topic := os.Getenv("topic_mqtt")
|
topic := os.Getenv("topic_mqtt")
|
||||||
|
if topic == "" { log.Fatalf("add your topic in .env file") }
|
||||||
username := os.Getenv("login_mqtt")
|
username := os.Getenv("login_mqtt")
|
||||||
|
if username != "" { opts.SetUsername(username) }
|
||||||
password := os.Getenv("password_mqtt")
|
password := os.Getenv("password_mqtt")
|
||||||
|
if password != "" { opts.SetPassword(password) }
|
||||||
TimeHour := os.Getenv("TimeHourSendMQTT")
|
TimeHour := os.Getenv("TimeHourSendMQTT")
|
||||||
|
|
||||||
ReallyTimeHour, err := strconv.Atoi(TimeHour)
|
|
||||||
|
|
||||||
opts := mqtt.NewClientOptions()
|
|
||||||
opts.AddBroker(broker)
|
opts.AddBroker(broker)
|
||||||
opts.SetClientID(ClientID)
|
opts.SetClientID(ClientID)
|
||||||
opts.SetConnectTimeout(5 * time.Second)
|
opts.SetConnectTimeout(5 * time.Second)
|
||||||
opts.SetUsername(username)
|
|
||||||
opts.SetPassword(password)
|
|
||||||
|
|
||||||
client := mqtt.NewClient(opts)
|
client := mqtt.NewClient(opts)
|
||||||
if token := client.Connect(); token.Wait() && token.Error() != nil { log.Fatalf("%v\n", token.Error()) }
|
if token := client.Connect(); token.Wait() && token.Error() != nil { log.Fatalf("%v\n", token.Error()) }
|
||||||
defer client.Disconnect(250)
|
defer client.Disconnect(250)
|
||||||
|
|
||||||
fmt.Println("Connect to a broker")
|
fmt.Println("Connect to a broker")
|
||||||
ticker := time.NewTicker(time.Duration(ReallyTimeHour) * time.Hour)
|
var ticker *time.Ticker
|
||||||
|
if TimeHour != "" {
|
||||||
|
ReallyTimeHour, err := strconv.Atoi(TimeHour)
|
||||||
|
if err != nil { log.Fatalf("%s\n", err) }
|
||||||
|
ticker = time.NewTicker(time.Duration(ReallyTimeHour) * time.Hour)
|
||||||
|
} else {
|
||||||
|
ticker = time.NewTicker(2 * time.Hour)
|
||||||
|
}
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
Send(client, topic)
|
Send(client, topic)
|
||||||
|
|
||||||
|
|
|
||||||
BIN
go/output/l17
BIN
go/output/l17
Binary file not shown.
Loading…
Reference in a new issue