/
application.go
49 lines (43 loc) · 1.11 KB
/
application.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package application
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/gorilla/websocket"
"github.com/h00s-go/wunderground-bridge/config"
"github.com/h00s-go/wunderground-bridge/mqtt"
)
type Application struct {
config *config.Config
logger *log.Logger
station *Station
mqtt *mqtt.MQTT
websocketUpgrader *websocket.Upgrader
}
func NewApplication(config *config.Config, logger *log.Logger, station *Station, mqtt *mqtt.MQTT) *Application {
return &Application{
config: config,
logger: logger,
station: station,
mqtt: mqtt,
websocketUpgrader: &websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
},
}
}
func (app *Application) publishWeatherToMQTT() {
w, err := json.Marshal(app.station.Weather)
if err == nil {
app.mqtt.Publish(app.config.MQTT.UpdateTopic, string(w))
}
}
func (app *Application) updateWunderground(query string) {
url := fmt.Sprintf("%v?%v", app.config.Wunderground.UpdateURL, query)
_, err := http.Get(url)
if err != nil {
app.logger.Printf("Error updating wunderground: %v\n", err)
}
}