2020-07-16 22:47:01 +00:00
|
|
|
package service
|
2020-07-16 03:26:31 +00:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
2026-04-10 03:39:46 +00:00
|
|
|
"strings"
|
2020-08-12 00:57:09 +00:00
|
|
|
|
2026-04-10 03:39:46 +00:00
|
|
|
"github.com/acaloiaro/roam-location/weather"
|
2020-07-16 03:26:31 +00:00
|
|
|
)
|
|
|
|
|
|
2026-04-10 03:39:46 +00:00
|
|
|
const port = 22495
|
2020-07-16 22:47:01 +00:00
|
|
|
|
2026-04-10 03:39:46 +00:00
|
|
|
type locationData struct {
|
|
|
|
|
Lat float64 `json:"lat"`
|
|
|
|
|
Lon float64 `json:"lon"`
|
|
|
|
|
Name string `json:"name,omitempty"`
|
2020-07-16 22:47:01 +00:00
|
|
|
}
|
|
|
|
|
|
2026-04-10 03:39:46 +00:00
|
|
|
type whereAmI struct {
|
|
|
|
|
Location *locationData `json:"location,omitempty"`
|
|
|
|
|
Weather *weather.Data `json:"weather,omitempty"`
|
|
|
|
|
Conditions *weather.Condition `json:"conditions,omitempty"`
|
|
|
|
|
}
|
2020-07-16 03:26:31 +00:00
|
|
|
|
2026-04-10 03:39:46 +00:00
|
|
|
var allowedOrigins = map[string]bool{
|
|
|
|
|
"https://adriano.fyi": true,
|
|
|
|
|
"http://localhost:1313": true,
|
2020-07-16 03:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
2026-04-10 03:39:46 +00:00
|
|
|
func setCORS(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if origin := r.Header.Get("Origin"); allowedOrigins[origin] {
|
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", origin)
|
2020-07-16 03:26:31 +00:00
|
|
|
}
|
2026-04-10 03:39:46 +00:00
|
|
|
}
|
2020-07-16 03:26:31 +00:00
|
|
|
|
2026-04-10 03:39:46 +00:00
|
|
|
// Listen registers all HTTP endpoints and starts the server.
|
|
|
|
|
func Listen(getLocation func() (lat, lon float64, name string, ok bool), wCache *weather.Cache) {
|
|
|
|
|
http.HandleFunc("/whereami", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
setCORS(w, r)
|
2020-07-16 03:26:31 +00:00
|
|
|
|
2026-04-10 03:39:46 +00:00
|
|
|
resp := whereAmI{}
|
2020-07-16 03:26:31 +00:00
|
|
|
|
2026-04-10 03:39:46 +00:00
|
|
|
if lat, lon, name, ok := getLocation(); ok {
|
|
|
|
|
resp.Location = &locationData{Lat: lat, Lon: lon, Name: name}
|
|
|
|
|
}
|
2020-08-10 01:28:22 +00:00
|
|
|
|
2026-04-10 03:39:46 +00:00
|
|
|
if data, ok := wCache.Get(); ok {
|
|
|
|
|
resp.Weather = &data
|
|
|
|
|
if resp.Location != nil {
|
|
|
|
|
resp.Conditions = weather.Infer(data, resp.Location.Lat, resp.Location.Lon)
|
|
|
|
|
} else {
|
|
|
|
|
resp.Conditions = weather.Infer(data, 0, 0)
|
2020-08-10 01:28:22 +00:00
|
|
|
}
|
2020-07-16 03:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
2026-04-10 03:39:46 +00:00
|
|
|
if resp.Location == nil && resp.Weather == nil {
|
|
|
|
|
http.Error(w, "no data yet", http.StatusServiceUnavailable)
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-07-16 03:26:31 +00:00
|
|
|
|
2026-04-10 03:39:46 +00:00
|
|
|
b, err := json.MarshalIndent(resp, "", " ")
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("error marshaling whereami: %v", err)
|
|
|
|
|
http.Error(w, "internal error", http.StatusInternalServerError)
|
|
|
|
|
return
|
2020-07-16 03:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
2026-04-10 03:39:46 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
fmt.Fprintf(w, string(b))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Receives pushes from the Ambient Weather WS-5000 via the awnet custom server config.
|
|
|
|
|
// The station sends params as /data/report/&key=val&... (& instead of ?) so we fix that up.
|
|
|
|
|
http.HandleFunc("/data/report/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if r.URL.RawQuery == "" {
|
|
|
|
|
if i := strings.Index(r.RequestURI, "&"); i >= 0 {
|
|
|
|
|
r.URL.RawQuery = r.RequestURI[i+1:]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
wCache.Update(r)
|
2026-04-11 18:42:06 +00:00
|
|
|
if data, ok := wCache.Get(); ok {
|
|
|
|
|
tempStr := "n/a"
|
|
|
|
|
if data.TempF != nil {
|
|
|
|
|
tempStr = fmt.Sprintf("%.1f°F", *data.TempF)
|
|
|
|
|
}
|
|
|
|
|
log.Printf("weather update received: temp=%s station=%s", tempStr, data.StationType)
|
|
|
|
|
}
|
2026-04-10 03:39:46 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
log.Printf("listening on 0.0.0.0:%d", port)
|
|
|
|
|
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
|
2020-08-10 01:28:22 +00:00
|
|
|
}
|