2024-03-03 03:48:55 +00:00
|
|
|
package gotelem
|
2023-06-23 20:52:52 +00:00
|
|
|
|
|
|
|
// this file defines the HTTP handlers and routes.
|
|
|
|
|
|
|
|
import (
|
2023-06-27 23:22:24 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-06-23 20:52:52 +00:00
|
|
|
"net/http"
|
2024-03-03 03:48:55 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2023-06-23 20:52:52 +00:00
|
|
|
|
2024-02-19 04:41:22 +00:00
|
|
|
"log/slog"
|
|
|
|
|
2023-06-23 20:52:52 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
2023-06-27 23:22:24 +00:00
|
|
|
"github.com/google/uuid"
|
2023-06-23 20:52:52 +00:00
|
|
|
"github.com/kschamplin/gotelem/skylab"
|
|
|
|
"nhooyr.io/websocket"
|
2023-07-06 02:16:12 +00:00
|
|
|
"nhooyr.io/websocket/wsjson"
|
2023-06-23 20:52:52 +00:00
|
|
|
)
|
|
|
|
|
2024-03-03 03:48:55 +00:00
|
|
|
func extractBusEventFilter(r *http.Request) (*BusEventFilter, error) {
|
|
|
|
|
|
|
|
bef := &BusEventFilter{}
|
|
|
|
|
|
|
|
v := r.URL.Query()
|
|
|
|
bef.Names = v["name"] // put all the names in.
|
|
|
|
if el := v.Get("start"); el != "" {
|
|
|
|
// parse the start time query.
|
|
|
|
t, err := time.Parse(time.RFC3339, el)
|
|
|
|
if err != nil {
|
|
|
|
return bef, err
|
|
|
|
}
|
|
|
|
bef.TimerangeStart = t
|
|
|
|
}
|
|
|
|
if el := v.Get("end"); el != "" {
|
|
|
|
// parse the start time query.
|
|
|
|
t, err := time.Parse(time.RFC3339, el)
|
|
|
|
if err != nil {
|
|
|
|
return bef, err
|
|
|
|
}
|
2024-03-05 15:49:08 +00:00
|
|
|
bef.TimerangeEnd = t
|
2024-03-03 03:48:55 +00:00
|
|
|
}
|
|
|
|
return bef, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractLimitModifier(r *http.Request) (*LimitOffsetModifier, error) {
|
|
|
|
lim := &LimitOffsetModifier{}
|
|
|
|
v := r.URL.Query()
|
|
|
|
if el := v.Get("limit"); el != "" {
|
|
|
|
val, err := strconv.ParseInt(el, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
lim.Limit = int(val)
|
|
|
|
// next, we check if we have an offset.
|
|
|
|
// we only check offset if we also have a limit.
|
|
|
|
// offset without limit isn't valid and is ignored.
|
|
|
|
if el := v.Get("offset"); el != "" {
|
|
|
|
val, err := strconv.ParseInt(el, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
lim.Offset = int(val)
|
|
|
|
}
|
|
|
|
return lim, nil
|
|
|
|
}
|
|
|
|
// we use the nil case to indicate that no limit was provided.
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2024-03-05 15:49:08 +00:00
|
|
|
type RouterMod func(chi.Router)
|
2024-03-04 05:04:41 +00:00
|
|
|
|
2024-03-05 15:49:08 +00:00
|
|
|
var RouterMods = []RouterMod{}
|
2024-03-04 05:04:41 +00:00
|
|
|
|
2024-03-03 03:48:55 +00:00
|
|
|
func TelemRouter(log *slog.Logger, broker *Broker, db *TelemDb) http.Handler {
|
2023-06-23 20:52:52 +00:00
|
|
|
r := chi.NewRouter()
|
|
|
|
|
2023-06-29 00:23:08 +00:00
|
|
|
r.Use(middleware.RequestID)
|
|
|
|
r.Use(middleware.RealIP)
|
2023-06-30 12:40:50 +00:00
|
|
|
r.Use(middleware.Logger) // TODO: integrate with slog instead of go default logger.
|
2023-06-23 20:52:52 +00:00
|
|
|
r.Use(middleware.Recoverer)
|
2024-03-05 02:40:55 +00:00
|
|
|
r.Use(middleware.SetHeader("Access-Control-Allow-Origin", "*"))
|
2023-06-23 20:52:52 +00:00
|
|
|
|
2023-06-24 05:15:42 +00:00
|
|
|
// heartbeat request.
|
2023-06-23 20:52:52 +00:00
|
|
|
r.Get("/ping", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Write([]byte("pong"))
|
|
|
|
})
|
2023-06-24 05:15:42 +00:00
|
|
|
|
2023-06-27 23:22:24 +00:00
|
|
|
r.Mount("/api/v1", apiV1(broker, db))
|
2023-06-23 20:52:52 +00:00
|
|
|
|
2024-03-04 05:04:41 +00:00
|
|
|
for _, mod := range RouterMods {
|
|
|
|
mod(r)
|
|
|
|
}
|
2023-06-23 20:52:52 +00:00
|
|
|
// To future residents - you can add new API calls/systems in /api/v2
|
|
|
|
// Don't break anything in api v1! keep legacy code working!
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// define API version 1 routes.
|
2024-03-03 03:48:55 +00:00
|
|
|
func apiV1(broker *Broker, tdb *TelemDb) chi.Router {
|
2023-06-23 20:52:52 +00:00
|
|
|
r := chi.NewRouter()
|
2023-06-30 12:40:50 +00:00
|
|
|
// this API only accepts JSON.
|
|
|
|
r.Use(middleware.AllowContentType("application/json"))
|
|
|
|
// no caching - always get the latest data.
|
2024-02-12 15:45:23 +00:00
|
|
|
// TODO: add a smart short expiry cache for queries that take a while.
|
2023-06-30 12:40:50 +00:00
|
|
|
r.Use(middleware.NoCache)
|
|
|
|
|
2023-06-23 20:52:52 +00:00
|
|
|
r.Get("/schema", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2023-06-30 12:40:50 +00:00
|
|
|
// return the Skylab JSON definitions
|
2023-06-23 20:52:52 +00:00
|
|
|
w.Write([]byte(skylab.SkylabDefinitions))
|
|
|
|
})
|
|
|
|
|
2023-06-27 23:22:24 +00:00
|
|
|
r.Route("/packets", func(r chi.Router) {
|
2024-03-02 01:12:35 +00:00
|
|
|
r.Get("/subscribe", apiV1PacketSubscribe(broker, tdb))
|
2023-06-27 23:22:24 +00:00
|
|
|
r.Post("/", func(w http.ResponseWriter, r *http.Request) {
|
2024-02-13 16:03:39 +00:00
|
|
|
var pkgs []skylab.BusEvent
|
2023-06-27 23:22:24 +00:00
|
|
|
decoder := json.NewDecoder(r.Body)
|
2023-06-29 00:23:08 +00:00
|
|
|
if err := decoder.Decode(&pkgs); err != nil {
|
2024-03-02 01:12:35 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-03-02 06:46:24 +00:00
|
|
|
tdb.AddEvents(pkgs...)
|
2023-06-29 00:23:08 +00:00
|
|
|
})
|
2024-03-02 06:46:24 +00:00
|
|
|
// general packet history get.
|
|
|
|
r.Get("/", apiV1GetPackets(tdb))
|
2023-06-29 00:23:08 +00:00
|
|
|
|
2024-03-02 06:46:24 +00:00
|
|
|
// this is to get a single field from a packet.
|
2024-03-02 01:12:35 +00:00
|
|
|
r.Get("/{name:[a-z_]+}/{field:[a-z_]+}", apiV1GetValues(tdb))
|
2023-06-29 00:23:08 +00:00
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
// records are driving segments/runs.
|
|
|
|
r.Route("/records", func(r chi.Router) {
|
2024-03-02 01:12:35 +00:00
|
|
|
r.Get("/", apiV1GetRecords(tdb)) // get all runs
|
|
|
|
r.Get("/active", apiV1GetActiveRecord(tdb)) // get current run (no end time)
|
|
|
|
r.Post("/", apiV1StartRecord(tdb)) // create a new run (with note). Ends active run if any, and creates new active run (no end time)
|
|
|
|
r.Get("/{id}", apiV1GetRecord(tdb)) // get details on a specific run
|
|
|
|
r.Put("/{id}", apiV1UpdateRecord(tdb)) // update a specific run. Can only be used to add notes/metadata, and not to change time/id.
|
2023-06-29 00:23:08 +00:00
|
|
|
|
2023-06-27 23:22:24 +00:00
|
|
|
})
|
|
|
|
|
2023-09-19 19:17:22 +00:00
|
|
|
r.Get("/stats", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
}) // v1 api stats (calls, clients, xbee connected, meta health ok)
|
2023-06-27 23:22:24 +00:00
|
|
|
|
2023-06-23 20:52:52 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2024-02-12 15:45:23 +00:00
|
|
|
// this is a websocket stream.
|
2024-03-03 03:48:55 +00:00
|
|
|
func apiV1PacketSubscribe(broker *Broker, db *TelemDb) http.HandlerFunc {
|
2023-06-27 23:22:24 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-03-03 03:23:35 +00:00
|
|
|
// pull filter from url query params.
|
|
|
|
bef, err := extractBusEventFilter(r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
// setup connection
|
2023-06-27 23:22:24 +00:00
|
|
|
conn_id := r.RemoteAddr + uuid.New().String()
|
|
|
|
sub, err := broker.Subscribe(conn_id)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
fmt.Fprintf(w, "error subscribing: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer broker.Unsubscribe(conn_id)
|
2024-03-03 03:23:35 +00:00
|
|
|
|
|
|
|
// setup websocket
|
2023-06-27 23:22:24 +00:00
|
|
|
c, err := websocket.Accept(w, r, nil)
|
|
|
|
if err != nil {
|
2024-03-02 06:46:24 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2023-06-27 23:22:24 +00:00
|
|
|
return
|
|
|
|
}
|
2024-03-02 06:46:24 +00:00
|
|
|
c.Ping(r.Context())
|
2024-03-03 03:23:35 +00:00
|
|
|
// closeread handles protocol/status messages,
|
|
|
|
// also handles clients closing the connection.
|
|
|
|
// we get a context to use from it.
|
|
|
|
ctx := c.CloseRead(r.Context())
|
2023-06-27 23:22:24 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2024-03-03 03:23:35 +00:00
|
|
|
case <-ctx.Done():
|
2023-06-27 23:22:24 +00:00
|
|
|
return
|
|
|
|
case msgIn := <-sub:
|
2024-03-03 03:23:35 +00:00
|
|
|
// short circuit if there's no names - send everything
|
|
|
|
if len(bef.Names) == 0 {
|
2023-07-06 02:16:12 +00:00
|
|
|
wsjson.Write(r.Context(), c, msgIn)
|
2023-06-27 23:22:24 +00:00
|
|
|
}
|
2024-03-03 03:23:35 +00:00
|
|
|
// otherwise, send it if it matches one of our names.
|
|
|
|
for _, name := range bef.Names {
|
2024-02-12 20:38:01 +00:00
|
|
|
if name == msgIn.Name {
|
2023-06-27 23:22:24 +00:00
|
|
|
// send it
|
2024-03-03 03:23:35 +00:00
|
|
|
wsjson.Write(ctx, c, msgIn)
|
2023-07-06 02:16:12 +00:00
|
|
|
break
|
2023-06-27 23:22:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2024-03-02 06:46:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-03 03:48:55 +00:00
|
|
|
func apiV1GetPackets(tdb *TelemDb) http.HandlerFunc {
|
2024-03-02 06:46:24 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// this should use http query params to return a list of packets.
|
|
|
|
bef, err := extractBusEventFilter(r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
lim, err := extractLimitModifier(r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: is the following check needed?
|
|
|
|
var res []skylab.BusEvent
|
|
|
|
if lim != nil {
|
|
|
|
res, err = tdb.GetPackets(r.Context(), *bef, lim)
|
|
|
|
} else {
|
|
|
|
res, err = tdb.GetPackets(r.Context(), *bef)
|
2024-03-05 15:49:08 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
2023-06-27 23:22:24 +00:00
|
|
|
}
|
2024-03-02 06:46:24 +00:00
|
|
|
b, err := json.Marshal(res)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write(b)
|
2023-06-27 23:22:24 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2023-06-30 12:40:50 +00:00
|
|
|
|
2024-02-24 22:48:19 +00:00
|
|
|
// apiV1GetValues is a function that creates a handler for
|
|
|
|
// getting the specific value from a packet.
|
|
|
|
// this is useful for OpenMCT or other viewer APIs
|
2024-03-03 03:48:55 +00:00
|
|
|
func apiV1GetValues(db *TelemDb) http.HandlerFunc {
|
2024-02-24 22:48:19 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var err error
|
|
|
|
|
2024-03-02 03:15:51 +00:00
|
|
|
bef, err := extractBusEventFilter(r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
2024-02-24 22:48:19 +00:00
|
|
|
}
|
2024-03-02 03:15:51 +00:00
|
|
|
lim, err := extractLimitModifier(r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
2024-02-24 22:48:19 +00:00
|
|
|
}
|
2024-03-02 06:46:24 +00:00
|
|
|
|
|
|
|
// get the URL parameters, these are guaranteed to exist.
|
2024-02-24 22:48:19 +00:00
|
|
|
name := chi.URLParam(r, "name")
|
|
|
|
field := chi.URLParam(r, "field")
|
2024-03-02 01:12:35 +00:00
|
|
|
|
2024-03-02 03:15:51 +00:00
|
|
|
// override the bus event filter name option
|
|
|
|
bef.Names = []string{name}
|
2024-02-24 22:48:19 +00:00
|
|
|
|
2024-03-05 15:49:08 +00:00
|
|
|
var res []Datum
|
|
|
|
// make the call, skip the limit modifier if it's nil.
|
|
|
|
if lim == nil {
|
|
|
|
res, err = db.GetValues(r.Context(), *bef, field)
|
|
|
|
} else {
|
|
|
|
res, err = db.GetValues(r.Context(), *bef, field, lim)
|
|
|
|
}
|
2024-02-24 22:48:19 +00:00
|
|
|
if err != nil {
|
|
|
|
// 500 server error:
|
2024-03-02 06:46:24 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2024-02-29 19:11:49 +00:00
|
|
|
return
|
2024-02-24 22:48:19 +00:00
|
|
|
}
|
|
|
|
b, err := json.Marshal(res)
|
2024-02-28 20:10:40 +00:00
|
|
|
if err != nil {
|
2024-03-02 06:46:24 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2024-02-29 19:11:49 +00:00
|
|
|
return
|
2024-02-28 20:10:40 +00:00
|
|
|
}
|
2024-02-24 22:48:19 +00:00
|
|
|
w.Write(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-06-30 12:40:50 +00:00
|
|
|
// TODO: rename. record is not a clear name. Runs? drives? segments?
|
2024-03-03 03:48:55 +00:00
|
|
|
func apiV1GetRecords(db *TelemDb) http.HandlerFunc {
|
2023-06-30 12:40:50 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-03 03:48:55 +00:00
|
|
|
func apiV1GetActiveRecord(db *TelemDb) http.HandlerFunc {
|
2023-06-30 12:40:50 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-03 03:48:55 +00:00
|
|
|
func apiV1StartRecord(db *TelemDb) http.HandlerFunc {
|
2023-06-30 12:40:50 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {}
|
|
|
|
}
|
|
|
|
|
2024-03-03 03:48:55 +00:00
|
|
|
func apiV1GetRecord(db *TelemDb) http.HandlerFunc {
|
2023-06-30 12:40:50 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {}
|
|
|
|
}
|
|
|
|
|
2024-03-03 03:48:55 +00:00
|
|
|
func apiV1UpdateRecord(db *TelemDb) http.HandlerFunc {
|
2023-06-30 12:40:50 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {}
|
|
|
|
}
|