-
Notifications
You must be signed in to change notification settings - Fork 0
/
geolocate.go
134 lines (112 loc) · 3.01 KB
/
geolocate.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package services
import (
"context"
"errors"
"log"
"math/rand"
"strconv"
"strings"
"time"
domain "github.com/romeufcrosa/where-to-eat/domain/entities"
maps "googlemaps.github.io/maps"
)
// GeoLocator ...
type GeoLocator interface {
Geolocate(ctx context.Context, accessPoints []maps.WiFiAccessPoint) (*maps.GeolocationResult, error)
ListRestaurants(ctx context.Context, searchRequest *maps.NearbySearchRequest) (maps.PlacesSearchResponse, error)
PlaceDetails(ctx context.Context, detailsRequest *maps.PlaceDetailsRequest) (domain.Place, error)
}
// Wifi ...
type Wifi struct {
BSSID string
Channel int
}
// Locate ...
type Locate struct {
google GeoLocator
}
// NewGeolocatorWith ...
func NewGeolocatorWith(google GeoLocator) Locate {
return Locate{
google: google,
}
}
// FetchLocation ...
func (l Locate) FetchLocation(ctx context.Context, airportRows []string) (*maps.GeolocationResult, error) {
m := make(map[int]Wifi)
if (len(airportRows)) < 2 {
return nil, errors.New("no APs available")
}
for i := 0; i < 2; i++ {
row := strings.Split(airportRows[i], " ")
if strings.TrimSpace(strings.Join(row, "")) == "" {
return nil, errors.New("ap row incomplete")
}
ch, err := strconv.Atoi(row[1])
if err != nil {
log.Printf("conversion error: %s", err.Error())
return nil, err
}
m[i] = Wifi{
BSSID: row[0],
Channel: ch,
}
}
accessPoints := []maps.WiFiAccessPoint{
{
Channel: m[0].Channel,
MACAddress: m[0].BSSID,
},
{
Channel: m[1].Channel,
MACAddress: m[1].BSSID,
},
}
return l.google.Geolocate(ctx, accessPoints)
}
// FetchRestaurant ...
func (l Locate) FetchRestaurant(ctx context.Context, req domain.SearchRequest) (domain.Place, error) {
currentPosition := &maps.LatLng{
Lat: req.Lat,
Lng: req.Lng,
}
searchRequest := &maps.NearbySearchRequest{
Location: currentPosition,
Radius: req.Distance,
Type: maps.PlaceTypeRestaurant,
}
log.Println("Sending request to API")
response, err := l.google.ListRestaurants(ctx, searchRequest)
if err != nil {
log.Printf("error from google API: %s", err.Error())
return domain.Place{}, err
}
log.Printf("Found %d results in response", len(response.Results))
randomPlace, err := getRandomPlace(response.Results)
if err != nil {
log.Printf("Could not get random place, reason: %s", err.Error())
return domain.Place{}, err
}
placeDetail := domain.PlaceFrom(randomPlace)
return placeDetail, nil
}
func getRandomPlace(places []maps.PlacesSearchResult) (maps.PlacesSearchResult, error) {
if len(places) == 0 {
return maps.PlacesSearchResult{}, errors.New("no suitable place found")
}
rand.Seed(time.Now().UnixNano())
var randomPlace maps.PlacesSearchResult
arrayPos := rand.Intn(len(places))
if randomPlace = places[arrayPos]; randomPlace.Rating < 1 {
places = append(places[:arrayPos], places[arrayPos+1:]...)
return getRandomPlace(places)
}
return randomPlace, nil
}
// FormatBool transforms a boolean into a string
func FormatBool(b bool) string {
if b {
return "Sim"
}
return "Não"
}