1// Copyright 2015 Brian J. Downs 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package openweathermap 16 17import ( 18 "fmt" 19 "net/http" 20 "net/url" 21) 22 23// Slice of type string of the valid parameters to be sent from a station. 24// The API refers to this data as the "Weather station data transmission protocol" 25var StationDataParameters = []string{ 26 "wind_dir", // Wind direction 27 "wind_speed", // Wind speed 28 "wind_gust", // Wind gust speed 29 "temp", // Temperature 30 "humidity", // Relative humidty 31 "pressure", // Atmospheric pressure 32 "rain_1h", // Rain in the last hour 33 "rain_24h", // Rain in the last 24 hours 34 "rain_today", // Rain since midnight 35 "snow", // Snow in the last 24 hours 36 "lum", // Brightness 37 "lat", // Latitude 38 "long", // Longitude 39 "alt", // Altitude 40 "radiation", // Radiation 41 "dewpoint", // Dew point 42 "uv", // UV index 43 "name", // Weather station name 44} 45 46// ValidateStationDataParameter will make sure that whatever parameter 47// supplied is one that can actually be used in the POST request. 48func ValidateStationDataParameter(param string) bool { 49 for _, p := range StationDataParameters { 50 if param == p { 51 return true 52 } 53 } 54 55 return false 56} 57 58// ConvertToURLValues will convert a map to a url.Values instance. We're 59// taking a map[string]string instead of something more type specific since 60// the url.Values instance only takes strings to create the URL values. 61func ConvertToURLValues(data map[string]string) string { 62 v := url.Values{} 63 64 for key, val := range data { 65 v.Set(key, val) 66 } 67 68 return v.Encode() 69} 70 71// SendStationData will send an instance the provided url.Values to the 72// provided URL. 73func SendStationData(data url.Values) { 74 resp, err := http.PostForm(dataPostURL, data) 75 if err != nil { 76 fmt.Println(err) 77 } 78 79 fmt.Println(resp.Body) 80} 81