-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadapters_forecastio.go
142 lines (117 loc) · 4.64 KB
/
adapters_forecastio.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 ForecastioWeatherBase struct {
Time int `json:"time"`
Summary string `json:"summary"`
Icon string `json:"icon"`
PrecipIntensity float64 `json:"precipIntensity"`
PrecipProbability float64 `json:"precipProbability"`
PrecipType string `json:"precipType"`
DewPoint float64 `json:"dewPoint"`
Humidity float64 `json:"humidity"`
WindSpeed float64 `json:"windSpeed"`
WindBearing float64 `json:"windBearing"`
CloudCover float64 `json:"cloudCover"`
Pressure float64 `json:"pressure"`
Ozone float64 `json:"ozone"`
}
type ForecastioCurrentWeather struct {
ForecastioWeatherBase
Temperature float64 `json:"temperature"`
ApparentTemperature float64 `json:"apparentTemperature"`
}
type ForecastioHourlyWeatherDataEntry struct {
ForecastioWeatherBase
Temperature float64 `json:"temperature"`
ApparentTemperature float64 `json:"apparentTemperature"`
}
type ForecastioHourlyWeather struct {
Summary string `json:"summary"`
Icon string `json:"icon"`
Data []ForecastioHourlyWeatherDataEntry `json:"data"`
}
type ForecastioDailyWeatherDataEntry struct {
ForecastioWeatherBase
MoonPhase float64 `json:"moonPhase"`
PrecipIntensityMax float64 `json:"precipIntensityMax"`
PrecipIntensityMaxTime int `json:"precipIntensityMaxTime"`
PrecipAccumulation float64 `json:"precipAccumulation"`
SunriseTime int `json:"sunriseTime"`
SunsetTime int `json:"sunsetTime"`
TemperatureMin float64 `json:"temperatureMin"`
TemperatureMax float64 `json:"temperatureMax"`
ApparentTemperatureMin float64 `json:"apparentTemperatureMin"`
ApparentTemperatureMinTime int `json:"apparentTemperatureMinTime"`
ApparentTemperatureMax float64 `json:"apparentTemperatureMax"`
ApparentTemperatureMaxTime int `json:"apparentTemperatureMaxTime"`
}
type ForecastioDailyWeather struct {
Summary string `json:"summary"`
Icon string `json:"icon"`
Data []ForecastioDailyWeatherDataEntry `json:"data"`
}
type ForecastioWeatherResponse struct {
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
Timezone string `json:"timezone"`
Offset int `json:"offset"`
Current ForecastioCurrentWeather `json:"currently"`
Hourly ForecastioHourlyWeather `json:"hourly"`
Daily ForecastioDailyWeather `json:"daily"`
}
func forecastioDecode(s string) (data ForecastioWeatherResponse, err error) {
var byteString = []byte(s)
err = json.Unmarshal(byteString, &data)
return data, err
}
func ForecastioAdaptCurrentWeather(jsonString string) (measurements MeasurementArray, err error) {
defer func() {
if r := recover(); r != nil {
measurements = AdaptStub(jsonString)
err = AdapterPanicErr
}
}()
data, decodeErr := forecastioDecode(jsonString)
if decodeErr != nil {
return AdaptStub(jsonString), decodeErr
}
dt := int64(data.Current.Time)
humidity_raw := data.Current.Humidity
pressure_raw := data.Current.Pressure
precipitation_raw := data.Current.PrecipIntensity
temp_raw := data.Current.Temperature
wind_raw := data.Current.WindSpeed
humidity := float64(humidity_raw) * 100
pressure := float64(pressure_raw)
precipitation := float64(precipitation_raw)
temp := temp_raw
wind := wind_raw
measurements = append(measurements, MeasurementSchema{Data: Measurement{Humidity: humidity, Precipitation: precipitation, Pressure: pressure, Temp: temp, Wind: wind}, Timestamp: dt})
return measurements, err
}
func ForecastioAdaptForecast(jsonString string) (measurements MeasurementArray, err error) {
defer func() {
if r := recover(); r != nil {
measurements = AdaptStub(jsonString)
}
}()
data, decodeErr := forecastioDecode(jsonString)
if decodeErr != nil {
panic(decodeErr.Error())
}
for _, entry := range data.Hourly.Data {
dt := int64(entry.Time)
humidity_raw := entry.Humidity
pressure_raw := entry.Pressure
precipitation_raw := entry.PrecipIntensity
temp_raw := entry.Temperature
wind_raw := entry.WindSpeed
humidity := float64(humidity_raw) * 100
pressure := float64(pressure_raw)
precipitation := float64(precipitation_raw)
temp := temp_raw
wind := wind_raw
measurements = append(measurements, MeasurementSchema{Data: Measurement{Humidity: humidity, Precipitation: precipitation, Pressure: pressure, Temp: temp, Wind: wind}, Timestamp: dt})
}
return measurements, err
}