diff --git a/go/go.mod b/go/go.mod index 084ec69..57e626a 100644 --- a/go/go.mod +++ b/go/go.mod @@ -1,8 +1,12 @@ module my-app -go 1.23.4 +go 1.24.0 require ( + github.com/eclipse/paho.mqtt.golang v1.5.1 // indirect github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 // indirect + github.com/gorilla/websocket v1.5.3 // indirect github.com/joho/godotenv v1.5.1 // indirect + golang.org/x/net v0.44.0 // indirect + golang.org/x/sync v0.17.0 // indirect ) diff --git a/go/go.sum b/go/go.sum index 0467a41..355572a 100644 --- a/go/go.sum +++ b/go/go.sum @@ -1,4 +1,12 @@ +github.com/eclipse/paho.mqtt.golang v1.5.1 h1:/VSOv3oDLlpqR2Epjn1Q7b2bSTplJIeV2ISgCl2W7nE= +github.com/eclipse/paho.mqtt.golang v1.5.1/go.mod h1:1/yJCneuyOoCOzKSsOTUc0AJfpsItBGWvYpBLimhArU= github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc= github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8= +github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= +github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +golang.org/x/net v0.44.0 h1:evd8IRDyfNBMBTTY5XRF1vaZlD+EmWx6x8PkhR04H/I= +golang.org/x/net v0.44.0/go.mod h1:ECOoLqd5U3Lhyeyo/QDCEVQ4sNgYsqvCZ722XogGieY= +golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug= +golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI= diff --git a/go/l17.go b/go/l17.go index b17c2d6..db3c22d 100644 --- a/go/l17.go +++ b/go/l17.go @@ -8,6 +8,7 @@ import ( "net/http" "time" "encoding/json" + mqtt "github.com/eclipse/paho.mqtt.golang" "fmt" ) @@ -18,7 +19,7 @@ type OpenWeather struct{ } `json:"main"` } -func main() { +func WeatherReq() (string, float64) { err := godotenv.Load() if err != nil { log.Fatalf("%v\n", err) @@ -38,10 +39,40 @@ func main() { if err != nil { log.Fatalf("%v\n", err) } + defer resp.Body.Close() var WeatherData OpenWeather err = json.NewDecoder(resp.Body).Decode(&WeatherData) if err != nil { log.Fatalf("%v\n", err) } - fmt.Println(WeatherData.City, WeatherData.Main.Temp) + return WeatherData.City, WeatherData.Main.Temp +} + +func main() { + err := godotenv.Load() + if err != nil { log.Fatalf("%v\n", err) } + broker := os.Getenv("broker_mqtt") + ClientID := os.Getenv("clientid_mqtt") + topic := os.Getenv("topic_mqtt") + + opts := mqtt.NewClientOptions() + opts.AddBroker(broker) + opts.SetClientID(ClientID) + opts.SetConnectTimeout(5 * time.Second) + + client := mqtt.NewClient(opts) + if token := client.Connect(); token.Wait() && token.Error() != nil { log.Fatalf("%v\n", token.Error()) } + defer client.Disconnect(250) + fmt.Println("Connect to a broker") + + city, temp := WeatherReq() + payload := fmt.Sprintf("City: %s, Temperature: %.2f°C", city, temp) + + token := client.Publish(topic, 0, false, payload) + token.Wait() + if err = token.Error(); err != nil { + log.Fatalf("%v\n", token.Error) + } else { + fmt.Println("Sent in", topic) + } } \ No newline at end of file