-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadapters_owm.go
142 lines (112 loc) · 3.66 KB
/
adapters_owm.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import "encoding/json"
type OwmLocationCoords struct {
Longitude float64 `json:"lon"`
Latitude float64 `json:"lat"`
}
type OwmWeatherInfo struct {
Id int `json:"id"`
Main string `json:"main"`
Description string `json:"description"`
Icon string `json:"icon"`
}
type OwmMainInfo struct {
Temp float64 `json:"temp"`
Pressure float64 `json:"pressure"`
Humidity int `json:"humidity"`
TempMin float64 `json:"temp_min"`
TempMax float64 `json:"temp_max"`
}
type OwmWindInfo struct {
Speed float64 `json:"speed"`
Degree float64 `json:"deg"`
}
type OwmPrecipInfo struct {
Expect3h float64 `json:"3h"`
}
type OwmCloudInfo struct {
All int `json:"all"`
}
type OwmWeatherStruct struct {
Name string `json:"name"`
Weather []OwmWeatherInfo `json:"weather"`
Main OwmMainInfo `json:"main"`
Visibility int `json:"visibility"`
Wind OwmWindInfo `json:"wind"`
Snow OwmPrecipInfo `json:"snow"`
Rain OwmPrecipInfo `json:"rain"`
Clouds OwmCloudInfo `json:"clouds"`
Timestamp int64 `json:"dt"`
Id int `json:"id"`
}
type OwmCityInfo struct {
OwmId int `json:"id"`
Name string `json:"name"`
Coord OwmLocationCoords `json:"coord"`
CountryISO string `json:"country"`
Population int `json:"population"`
}
type OwmCurrentStruct struct {
OwmWeatherStruct
Coord OwmLocationCoords `json:"coord"`
Code int `json:"cod"`
}
type OwmForecastStruct struct {
Code string `json:"cod"`
Message float64 `json:"message"`
City OwmCityInfo `json:"city"`
Count int `json:"cnt"`
Data []OwmWeatherStruct `json:"list"`
}
func owmCurrentDecode(s string) (data OwmCurrentStruct, err error) {
byteString := []byte(s)
err = json.Unmarshal(byteString, &data)
return data, err
}
func owmForecastDecode(s string) (data OwmForecastStruct, err error) {
byteString := []byte(s)
err = json.Unmarshal(byteString, &data)
return data, err
}
func OwmAdaptCurrentWeather(jsonString string) (measurements MeasurementArray, err error) {
defer func() {
if r := recover(); r != nil {
measurements = AdaptStub(jsonString)
err = AdapterPanicErr
}
}()
data, decodeErr := owmCurrentDecode(jsonString)
if decodeErr != nil {
return AdaptStub(jsonString), decodeErr
}
dt := int64(data.Timestamp)
temp := float64(data.Main.Temp)
pressure := float64(data.Main.Pressure)
wind := float64(data.Wind.Speed)
humidity := float64(data.Main.Humidity)
precipitation := float64(0)
measurements = append(measurements, MeasurementSchema{Data: Measurement{Humidity: humidity, Precipitation: precipitation, Pressure: pressure, Temp: temp, Wind: wind}, Timestamp: dt})
return measurements, err
}
func OwmAdaptForecast(jsonString string) (measurements MeasurementArray, err error) {
defer func() {
if r := recover(); r != nil {
measurements = AdaptStub(jsonString)
err = AdapterPanicErr
}
}()
data, decodeErr := owmForecastDecode(jsonString)
if decodeErr != nil {
return AdaptStub(jsonString), decodeErr
}
for _, entry := range data.Data {
dt := int64(entry.Timestamp)
temp := float64(entry.Main.Temp)
pressure := float64(entry.Main.Pressure)
wind := float64(entry.Wind.Speed)
humidity := float64(entry.Main.Humidity)
precipitation := float64(entry.Snow.Expect3h) + float64(entry.Rain.Expect3h)
measurements = append(measurements, MeasurementSchema{Data: Measurement{Humidity: humidity, Precipitation: precipitation, Pressure: pressure, Temp: temp, Wind: wind}, Timestamp: dt})
}
return measurements, err
}