-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrackingHandler.go
109 lines (97 loc) · 2.46 KB
/
TrackingHandler.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
package handlers
import (
"net/http"
"strconv"
"time"
"github.com/SharkEzz/sattrack/dto"
"github.com/SharkEzz/sattrack/services"
"github.com/SharkEzz/sgp4"
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/websocket/v2"
"gorm.io/gorm"
)
func HandlePostTracking(c *fiber.Ctx, db *gorm.DB, validator *validator.Validate) error {
requestData := &dto.TrackingRequest{}
c.BodyParser(requestData)
err := validator.Struct(requestData)
if err != nil {
c.Response().SetStatusCode(http.StatusBadRequest)
return c.JSON(dto.Error{
Status: http.StatusBadRequest,
Message: err.Error(),
})
}
tle, err := services.GetTLEFromDatabase(requestData.CatNBR, db)
if err != nil {
c.Response().SetStatusCode(http.StatusNotFound)
return c.JSON(dto.Error{
Status: http.StatusNotFound,
Message: "Satellite not found",
})
}
sgp4, err := sgp4.NewSGP4(tle)
if err != nil {
c.Response().SetStatusCode(http.StatusInternalServerError)
return c.JSON(dto.Error{
Status: http.StatusInternalServerError,
Message: "Error while initializing SGP4, please try again later",
})
}
observation := sgp4.ObservationFromLocation(requestData.Lat, requestData.Lng, requestData.Alt)
return c.JSON(dto.TrackingResponse{
Observation: observation,
GeneratedAt: time.Now(),
})
}
func HandleWsTracking(c *websocket.Conn, db *gorm.DB) {
defer c.Close()
var (
qCatNbr = c.Query("catNbr")
qLat = c.Query("lat")
qLng = c.Query("lng")
qAlt = c.Query("alt")
)
catNbr, err := strconv.ParseInt(qCatNbr, 10, 64)
if err != nil {
handleError(c, "Invalid CatNbr")
return
}
lat, err := strconv.ParseFloat(qLat, 64)
if err != nil {
handleError(c, "Invalid Latitude")
return
}
lng, err := strconv.ParseFloat(qLng, 64)
if err != nil {
handleError(c, "Invalid Longitude")
return
}
alt, err := strconv.ParseFloat(qAlt, 64)
if err != nil {
handleError(c, "Invalid Altitude")
return
}
tle, err := services.GetTLEFromDatabase(int(catNbr), db)
if err != nil {
handleError(c, "TLE not found")
return
}
sgp4, err := sgp4.NewSGP4(tle)
if err != nil {
handleError(c, "Error while initializing SGP4, try again later")
return
}
for {
observation := sgp4.ObservationFromLocation(lat, lng, alt)
if err := c.WriteJSON(observation); err != nil {
// error
break
}
time.Sleep(time.Millisecond * 500)
}
}
func handleError(c *websocket.Conn, errStr string) {
c.WriteMessage(1, []byte(errStr))
c.Close()
}