Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cmd/geebee/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ func exitWithError(err error) {
log.Fatalf("An error occurred: %v\n", err)
}

func geebeeHandler(alreadySpottedAircraft *[]geebee.Aircraft) {
aircraft, err := geebee.HandleAircraft(alreadySpottedAircraft)
func geebeeHandler(spotted map[string]bool) {
aircraft, err := geebee.HandleAircraft(spotted)
if err != nil {
exitWithError(err)
}
Expand Down Expand Up @@ -62,10 +62,10 @@ func sendNotifications(aircraft []geebee.AircraftOutput) error {
func HandleGeeBee() {
log.Printf("Watching for the following tail numbers: %s", configuration.TailNumbers)

var alreadySpottedAircraft []geebee.Aircraft
spotted := map[string]bool{}

for {
geebeeHandler(&alreadySpottedAircraft)
geebeeHandler(spotted)

time.Sleep(time.Duration(configuration.FetchInterval) * time.Second)
}
Expand Down
61 changes: 17 additions & 44 deletions internal/geebee/geebee.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,58 +53,31 @@ func checkAircraft() (aircraft []Aircraft, err error) {
return flightData.AC, nil
}

func newlySpotted(aircraft Aircraft, spottedAircraft []Aircraft) bool {
return !containsAircraft(aircraft, spottedAircraft)
}

func containsAircraft(aircraft Aircraft, aircraftList []Aircraft) bool {
for _, ac := range aircraftList {
if ac.ICAO == aircraft.ICAO {
return true
}
func HandleAircraft(spotted map[string]bool) ([]AircraftOutput, error) {
allAircraftInRange, err := checkAircraft()
if err != nil {
return nil, err
}

return false
}

func updateSpottedAircraft(alreadySpottedAircraft, filteredAircraft []Aircraft) (aircraft []Aircraft) {
for _, ac := range alreadySpottedAircraft {
if containsAircraft(ac, filteredAircraft) {
aircraft = append(aircraft, ac)
// Collect aircraft seen for the first time this cycle.
var newlySpotted []Aircraft
current := make(map[string]bool, len(allAircraftInRange))
for _, ac := range allAircraftInRange {
current[ac.ICAO] = true
if !spotted[ac.ICAO] {
spotted[ac.ICAO] = true
newlySpotted = append(newlySpotted, ac)
}
}

return aircraft
}

func validateAircraft(allFilteredAircraft []Aircraft, alreadySpottedAircraft *[]Aircraft) (newlySpottedAircraft, updatedSpottedAircraft []Aircraft) {
for _, ac := range allFilteredAircraft {
if newlySpotted(ac, *alreadySpottedAircraft) {
newlySpottedAircraft = append(newlySpottedAircraft, ac)
*alreadySpottedAircraft = append(*alreadySpottedAircraft, ac)
// Evict aircraft that are no longer in range so they trigger an alert if they return.
for icao := range spotted {
if !current[icao] {
delete(spotted, icao)
}
}

*alreadySpottedAircraft = updateSpottedAircraft(*alreadySpottedAircraft, allFilteredAircraft)

return newlySpottedAircraft, *alreadySpottedAircraft
}

func HandleAircraft(alreadySpottedAircraft *[]Aircraft) (aircraft []AircraftOutput, err error) {
var newlySpottedAircraft []Aircraft

allAircraftInRange, err := checkAircraft()
if err != nil {
return nil, err
}

newlySpottedAircraft, *alreadySpottedAircraft = validateAircraft(allAircraftInRange, alreadySpottedAircraft)
newlySpottedAircraftOutput, err := CreateAircraftOutput(newlySpottedAircraft)
if err != nil {
return nil, err
}

return newlySpottedAircraftOutput, nil
return CreateAircraftOutput(newlySpotted)
}

func isAircraftMilitary(aircraft Aircraft) bool {
Expand Down