forked from briandowns/openweathermap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
station.go
80 lines (70 loc) · 2.3 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
// Copyright 2015 Brian J. Downs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package openweathermap
import (
"fmt"
"net/http"
"net/url"
)
// Slice of type string of the valid parameters to be sent from a station.
// The API refers to this data as the "Weather station data transmission protocol"
var StationDataParameters = []string{
"wind_dir", // Wind direction
"wind_speed", // Wind speed
"wind_gust", // Wind gust speed
"temp", // Temperature
"humidity", // Relative humidty
"pressure", // Atmospheric pressure
"rain_1h", // Rain in the last hour
"rain_24h", // Rain in the last 24 hours
"rain_today", // Rain since midnight
"snow", // Snow in the last 24 hours
"lum", // Brightness
"lat", // Latitude
"long", // Longitude
"alt", // Altitude
"radiation", // Radiation
"dewpoint", // Dew point
"uv", // UV index
"name", // Weather station name
}
// ValidateStationDataParameter will make sure that whatever parameter
// supplied is one that can actually be used in the POST request.
func ValidateStationDataParameter(param string) bool {
for _, p := range StationDataParameters {
if param == p {
return true
}
}
return false
}
// ConvertToURLValues will convert a map to a url.Values instance. We're
// taking a map[string]string instead of something more type specific since
// the url.Values instance only takes strings to create the URL values.
func ConvertToURLValues(data map[string]string) string {
v := url.Values{}
for key, val := range data {
v.Set(key, val)
}
return v.Encode()
}
// SendStationData will send an instance the provided url.Values to the
// provided URL.
func SendStationData(data url.Values) {
resp, err := http.PostForm(dataPostURL, data)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Body)
}