/
station.go
114 lines (103 loc) · 2.97 KB
/
station.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package application
import (
"errors"
"fmt"
"log"
"net/http"
"net/url"
"time"
"github.com/h00s-go/wunderground-bridge/config"
)
type Station struct {
config *config.Station
logger *log.Logger
Watchdog Watchdog
Weather *Weather
}
type Watchdog struct {
SuccessfulLastUpdate bool
FailedAttempts int
}
func NewStation(config *config.Station, logger *log.Logger) *Station {
return &Station{
config: config,
logger: logger,
Watchdog: Watchdog{},
Weather: &Weather{},
}
}
func (s *Station) NewWeather(r *http.Request) error {
d, err := url.ParseQuery(r.URL.RawQuery)
if err != nil {
return err
}
if d.Get("ID") != s.config.ID {
return errors.New("Station ID does not match")
}
if d.Get("PASSWORD") != s.config.Password {
return errors.New("Station password does not match")
}
updatedAt, err := time.Parse("2006-01-02 15:04:05", d.Get("dateutc"))
if err != nil {
return err
}
w := &Weather{
StationID: d.Get("ID"),
Temperature: convertFahrenheitToCelsius(strToFloat(d.Get("tempf"))),
DewPoint: convertFahrenheitToCelsius(strToFloat(d.Get("dewptf"))),
Humidity: strToInt(d.Get("humidity")),
Pressure: convertHGToKPA(strToFloat(d.Get("baromin"))),
Wind: Wind{
Chill: convertFahrenheitToCelsius(strToFloat(d.Get("windchillf"))),
Direction: strToInt(d.Get("winddir")),
Speed: convertMileToKilometer(strToFloat(d.Get("windspeedmph"))),
Gust: convertMileToKilometer(strToFloat(d.Get("windgustmph"))),
},
Rain: Rain{
In: convertInchToMillimeter(strToFloat(d.Get("rainin"))),
InDaily: convertInchToMillimeter(strToFloat(d.Get("dailyrainin"))),
InWeekly: convertInchToMillimeter(strToFloat(d.Get("weeklyrainin"))),
InMonthly: convertInchToMillimeter(strToFloat(d.Get("monthlyrainin"))),
InYearly: convertInchToMillimeter(strToFloat(d.Get("yearlyrainin"))),
},
Solar: Solar{
Radiation: strToDecimal(d.Get("solarradiation")),
UV: strToInt(d.Get("UV")),
},
Indoor: Indoor{
Temperature: convertFahrenheitToCelsius(strToFloat(d.Get("indoortempf"))),
Humidity: strToInt(d.Get("indoorhumidity")),
},
UpdatedAt: updatedAt,
}
validWeather := w.validate()
s.UpdateWatchDog(validWeather)
if !validWeather {
return errors.New("invalid weather data")
}
s.Weather = w
return nil
}
func (s *Station) UpdateWatchDog(success bool) {
if s.config.WatchdogEnabled {
if success {
s.Watchdog.SuccessfulLastUpdate = true
s.Watchdog.FailedAttempts = 0
return
}
if s.Watchdog.FailedAttempts >= s.config.RebootOnFailedAttempts {
s.logger.Println("Attempting reboot")
go s.attemptReboot()
s.Watchdog.FailedAttempts = 0
}
s.Watchdog.SuccessfulLastUpdate = false
s.Watchdog.FailedAttempts++
s.logger.Printf("Failed %v time(s) to update weather data", s.Watchdog.FailedAttempts)
}
}
func (s *Station) attemptReboot() {
_, err := http.Get(fmt.Sprintf("%s/msgreboot.htm", s.config.URL))
if err != nil {
s.logger.Printf("Error while reboot attempt: %v\n", err)
}
}