-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
178 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# SatTrack | ||
|
||
Modern satellite tracking solution | ||
|
||
## TODO | ||
|
||
- [ ] Custom validation messages |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package dto | ||
|
||
type Error struct { | ||
Status uint | ||
Message string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package dto | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/SharkEzz/sgp4" | ||
) | ||
|
||
type TrackingRequest struct { | ||
CatNBR int `validate:"required"` | ||
Lat float64 `validate:"required,latitude"` | ||
Lng float64 `validate:"required,longitude"` | ||
Alt float64 `validate:"required"` | ||
} | ||
|
||
type TrackingResponse struct { | ||
sgp4.Observation | ||
GeneratedAt time.Time | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package handlers | ||
|
||
import ( | ||
"net/http" | ||
"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" | ||
"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(), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package services | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"strings" | ||
"time" | ||
|
||
"github.com/SharkEzz/sattrack/database/models" | ||
"github.com/SharkEzz/sgp4" | ||
"gorm.io/gorm" | ||
) | ||
|
||
// CelesTrack active satellites list | ||
const activeListUrl string = "https://celestrak.com/NORAD/elements/active.txt" | ||
|
||
// Fetch all active satellite from CelesTrack and update the satellites table | ||
func UpdateDatabase(db *gorm.DB) error { | ||
fmt.Println("Updating TLE database") | ||
fmt.Println("Deleting record from `tles` table") | ||
db.Exec("DELETE FROM tles") | ||
fmt.Println("Querying new TLE list") | ||
res, err := http.Get(activeListUrl) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
content, _ := io.ReadAll(res.Body) | ||
contentStr := strings.TrimSpace(string(content)) | ||
contentArr := strings.Split(string(contentStr), "\n") | ||
|
||
if len(contentArr)%3 != 0 { | ||
return errors.New("invalid TLE count") | ||
} | ||
|
||
fmt.Println("Inserting new records") | ||
start := time.Now() | ||
satellites := []models.TLE{} | ||
for i := 0; i < len(contentArr); i += 3 { | ||
tle, err := sgp4.NewTLE(contentArr[i][:24], contentArr[i+1][:69], contentArr[i+2][:69]) | ||
if err != nil { | ||
// TODO: error handling | ||
continue | ||
} | ||
satellites = append(satellites, models.TLE{ | ||
Name: tle.Name(), | ||
CatNBR: tle.NoradNumber(), | ||
Line1: tle.Line1(), | ||
Line2: tle.Line2(), | ||
}) | ||
} | ||
db.Create(&satellites) | ||
|
||
fmt.Printf("Inserted %v TLE in %s\n", len(contentArr)/3, time.Since(start)) | ||
|
||
return nil | ||
} | ||
|
||
// Get a TLE from the database by it's NORAD catalog number | ||
func GetTLEFromDatabase(catNbr int, db *gorm.DB) (*sgp4.TLE, error) { | ||
dbTLE := new(models.TLE) | ||
db.Where("cat_nbr = ?", catNbr).First(&dbTLE) | ||
|
||
if dbTLE == nil { | ||
return nil, errors.New("satellite not found") | ||
} | ||
|
||
tle, err := sgp4.NewTLE(dbTLE.Name, dbTLE.Line1, dbTLE.Line2) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return tle, nil | ||
} |