-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
255 lines (218 loc) · 7.85 KB
/
handler.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package main
import (
"flag"
"fmt"
"net/http"
"reflect"
"strconv"
"strings"
"time"
"github.com/gorilla/mux"
"github.com/influxdata/influxdb/client/v2"
"github.com/mcuadros/go-lookup"
)
type WeatherData struct {
Passkey string `json:"passkey"`
//StationType string `json:"stationtype"`
DateUtc string `json:"dateutc"`
TempF float64 `json:"tempf"`
Humidity int `json:"humidity"`
WindSpeedMph float64 `json:"windspeedmph"`
WindGustMph float64 `json:"windgustmph"`
MaxDailyGust float64 `json:"maxdailygust"`
WindDir int `json:"winddir"`
WindDir_Avg10m int `json:"winddir_avg10m"`
Uv int `json:"uv"`
SolarRadiation float64 `json:"solarradiation"`
HourlyRainIn float64 `json:"hourlyrainin"`
EventRainIn float64 `json:"eventrainin"`
DailyRainIn float64 `json:"dailyrainin"`
WeeklyRainIn float64 `json:"weeklyrainin"`
MonthlyRainIn float64 `json:"monthlyrainin"`
YearlyRainIn float64 `json:"yearlyrainin"`
BattOut int `json:"battout"`
BattRain int `json:"battrain"`
TempInF float64 `json:"tempinf"`
HumidityIn int `json:"humidityin"`
BaromRelIn float64 `json:"baromrelin"`
BaromAbsIn float64 `json:"baromabsin"`
BattIn int `json:"battin"`
DewPt float64 `json:"dewpt"`
DewPtIn float64 `json:"dewptin"`
}
func computeDewPt(temp float64, humidity int) float64 {
// DP = T - 9/25(100 - RH)
dp := temp - (float64(9) / float64(25) * (100 - float64(humidity)))
return dp
}
func main() {
ListenPortPtr := flag.Int("port", 8080, "Port to listen on for requests from PWS")
// InfluxDB connection details (replace with your credentials)
IdbAddrPtr := flag.String("IdbAddr", "http://192.168.86.36:8086", "URL to InfluxDB")
IdbUserPtr := flag.String("IdbUser", "mydb", "InfluxDB username")
IdbPassPtr := flag.String("IdbPass", "mydb", "InfluxDB password")
flag.Parse()
// client := influxdb.NewClient(influxURL, token)
c, err := client.NewHTTPClient(client.HTTPConfig{
Addr: *IdbAddrPtr,
Username: *IdbUserPtr,
Password: *IdbPassPtr,
})
if err != nil {
fmt.Println("Error creating InfluxDB client:", err)
return
}
defer c.Close()
// Create a new point batch
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: "mydb",
Precision: "s",
})
if err != nil {
fmt.Println("Error creating point batch:", err)
return
}
// Create a new router
router := mux.NewRouter()
// Handler for weather data endpoint
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
//fmt.Println(r.URL)
// Parse the query string
queryParams := r.URL.Query()
// Create a new WeatherData struct
data := WeatherData{}
// Loop through query params and populate struct fields
for key, value := range queryParams {
switch key {
case "PASSKEY":
data.Passkey = value[0]
//case "stationtype":
// data.StationType = value[0]
case "dateutc":
data.DateUtc = strings.Replace(value[0], " ", "T", 1) + "Z"
// Parse numeric values
case "tempf", "tempinf", "windspeedmph", "windgustmph", "maxdailygust", "solarradiation", "hourlyrainin", "eventrainin", "dailyrainin", "weeklyrainin", "monthlyrainin", "yearlyrainin", "baromrelin", "baromabsin":
var fval float64
if _, err := fmt.Sscanln(value[0], &fval); err != nil {
fmt.Println("Error parsing value for", key, err)
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Error parsing value for %s", key)
return
}
fieldValue, _ := lookup.LookupI(&data, key)
fieldValue.Set(reflect.ValueOf(fval))
// Parse integer values
case "humidity", "winddir", "winddir_avg10m", "uv", "battout", "battrain", "humidityin", "battin":
var ival int
if _, err := fmt.Sscanln(value[0], &ival); err != nil {
fmt.Println("Error parsing value for", key, err)
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Error parsing value for %s", key)
return
}
fieldValue, _ := lookup.LookupI(&data, key)
fieldValue.Set(reflect.ValueOf(ival))
}
}
data.DewPt = computeDewPt(data.TempF, data.Humidity)
data.DewPtIn = computeDewPt(data.TempInF, data.HumidityIn)
// Convert struct to a point
tags := map[string]string{
"passkey": data.Passkey,
//"stationtype": data.StationType,
}
fields := map[string]interface{}{
//"passkey": data.Passkey,
//"stationtype": data.StationType,
"tempf": data.TempF,
"humidity": data.Humidity,
"windspeedmph": data.WindSpeedMph,
"windgustmph": data.WindGustMph,
"maxdailygust": data.MaxDailyGust,
"winddir": data.WindDir,
"winddir_avg10m": data.WindDir_Avg10m,
"uv": data.Uv,
"solarradiation": data.SolarRadiation,
"hourlyrainin": data.HourlyRainIn,
"eventrainin": data.EventRainIn,
"dailyrainin": data.DailyRainIn,
"weeklyrainin": data.WeeklyRainIn,
"monthlyrainin": data.MonthlyRainIn,
"yearlyrainin": data.YearlyRainIn,
"battout": data.BattOut,
"battrain": data.BattRain,
"tempinf": data.TempInF,
"humidityin": data.HumidityIn,
"baromrelin": data.BaromRelIn,
"baromabsin": data.BaromAbsIn,
"battin": data.BattIn,
"dewpt": data.DewPt,
"dewptin": data.DewPtIn,
}
pdate, err := time.Parse(time.RFC3339, data.DateUtc)
if err != nil {
fmt.Println("Error converting time:", err)
return
}
p, err := client.NewPoint("weather", tags, fields, pdate)
if err != nil {
fmt.Println("Error creating NewPoint", err)
return
}
bp.AddPoint(p)
// Write the point to InfluxDB
if err := c.Write(bp); err != nil {
fmt.Println("Error writing batch points:", err)
fmt.Println(tags)
fmt.Println(fields)
return
}
fmt.Fprintf(w, "Data posted to InfluxDB successfully!")
fmt.Println(time.Now(), " - ", tags, fields)
})
// Start the server
fmt.Printf("Server listening on port %d\n", *ListenPortPtr)
fmt.Printf("Muxing to %s\n", *IdbAddrPtr)
// Create the HTTP server
srv := http.Server{
Addr: ":" + strconv.Itoa(*ListenPortPtr),
// ReadHeaderTimeout is the amount of time allowed to read
// request headers. The connection's read deadline is reset
// after reading the headers and the Handler can decide what
// is considered too slow for the body. If ReadHeaderTimeout
// is zero, the value of ReadTimeout is used. If both are
// zero, there is no timeout.
ReadHeaderTimeout: 15 * time.Second,
// ReadTimeout is the maximum duration for reading the entire
// request, including the body. A zero or negative value means
// there will be no timeout.
//
// Because ReadTimeout does not let Handlers make per-request
// decisions on each request body's acceptable deadline or
// upload rate, most users will prefer to use
// ReadHeaderTimeout. It is valid to use them both.
ReadTimeout: 15 * time.Second,
// WriteTimeout is the maximum duration before timing out
// writes of the response. It is reset whenever a new
// request's header is read. Like ReadTimeout, it does not
// let Handlers make decisions on a per-request basis.
// A zero or negative value means there will be no timeout.
WriteTimeout: 10 * time.Second,
// IdleTimeout is the maximum amount of time to wait for the
// next request when keep-alives are enabled. If IdleTimeout
// is zero, the value of ReadTimeout is used. If both are
// zero, there is no timeout.
IdleTimeout: 30 * time.Second,
Handler: router,
}
// For per request timeouts applications can wrap all `http.HandlerFunc(...)` in
// `http.TimeoutHandler`` and specify a timeout, but note the TimeoutHandler does not
// start ticking until all headers have been read.
// Listen with our custom server with timeouts configured
//if srv.ListenAndServe() != nil {
// fmt.Println("ListenAndServe failed")
//}
if srv.ListenAndServe() != nil {
fmt.Println("ListenAndServe failed")
}
}