-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadapters_myweather2.go
67 lines (51 loc) · 1.77 KB
/
adapters_myweather2.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
package main
import (
"encoding/json"
"strconv"
"strings"
"time"
)
type Myweather2WindInfo struct {
Speed string `json:"speed"`
}
type Myweather2WeatherData struct {
Temp string `json:"temp"`
Humidity string `json:"humidity"`
Pressure string `json:"pressure"`
Wind []Myweather2WindInfo `json:"wind"`
}
type Myweather2Weather struct {
CurrentWeather []Myweather2WeatherData `json:"curren_weather"`
}
type Myweather2Response struct {
Weather Myweather2Weather `json:"weather"`
}
func myweather2Decode(s string) (data Myweather2Response, err error) {
byteString := []byte(s)
err = json.Unmarshal(byteString, &data)
return data, err
}
func Myweather2AdaptCurrentWeather(jsonString string) (measurements MeasurementArray, err error) {
defer func() {
if r := recover(); r != nil {
measurements = AdaptStub(jsonString)
err = AdapterPanicErr
}
}()
data, decodeErr := myweather2Decode(jsonString)
if decodeErr != nil {
return AdaptStub(jsonString), decodeErr
}
dt := time.Now().Unix()
humidity_raw := strings.TrimSpace(data.Weather.CurrentWeather[0].Humidity)
pressure_raw := strings.TrimSpace(data.Weather.CurrentWeather[0].Pressure)
temp_raw := strings.TrimSpace(data.Weather.CurrentWeather[0].Temp)
wind_raw := strings.TrimSpace(data.Weather.CurrentWeather[0].Wind[0].Speed)
humidity, _ := strconv.ParseFloat(humidity_raw, 64)
pressure, _ := strconv.ParseFloat(pressure_raw, 64)
temp, _ := strconv.ParseFloat(temp_raw, 64)
wind, _ := strconv.ParseFloat(wind_raw, 64)
precipitation := float64(0)
measurements = append(measurements, MeasurementSchema{Data: Measurement{Humidity: humidity, Precipitation: precipitation, Pressure: pressure, Temp: temp, Wind: wind}, Timestamp: dt})
return measurements, err
}