/
scooter.go
171 lines (139 loc) · 3.94 KB
/
scooter.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package seeder
import (
"context"
"fmt"
"log"
"math"
"time"
"github.com/google/uuid"
"github.com/pysf/special-umbrella/internal/scooter"
"github.com/pysf/special-umbrella/internal/scooter/scooteriface"
"github.com/pysf/special-umbrella/internal/scooter/scootertype"
"go.mongodb.org/mongo-driver/mongo"
)
type ScooterDataSeederOptions func(s *ScooterDataSeeder)
type ScooterDataSeeder struct {
scootersPerCircle int
numberOfScooters int
baseLat float64
baseLng float64
distanceShift float64
startDelay time.Duration
scooterCreator scooteriface.ScooterCreator
statusUpdater scooteriface.StatusUpdater
ctx context.Context
}
func NewScooterDataSeeder(ctx context.Context, DB *mongo.Database, options ...ScooterDataSeederOptions) *ScooterDataSeeder {
scooterReserver, err := scooter.NewScooterReserver(DB)
if err != nil {
panic(err)
}
statusUpdater, err := scooter.NewStatusUpdater(scooterReserver, DB)
if err != nil {
panic(err)
}
scooterCreator, err := scooter.NewScooterCreator(DB)
if err != nil {
panic(err)
}
seeder := &ScooterDataSeeder{
ctx: ctx,
statusUpdater: statusUpdater,
scooterCreator: scooterCreator,
}
for _, o := range options {
o(seeder)
}
return seeder
}
func (s *ScooterDataSeeder) Start() {
go func() {
time.Sleep(s.startDelay)
if err := s.addRandomScooters(); err != nil {
log.Printf("Start: failed to add scooters err=%v", err)
}
}()
}
func WithLat(lat float64) ScooterDataSeederOptions {
return func(s *ScooterDataSeeder) {
s.baseLat = lat
}
}
func WithLng(lng float64) ScooterDataSeederOptions {
return func(s *ScooterDataSeeder) {
s.baseLng = lng
}
}
func WithDistanceShift(distance float64) ScooterDataSeederOptions {
return func(s *ScooterDataSeeder) {
s.distanceShift = distance
}
}
func WithNumberOfInitialScooters(c int) ScooterDataSeederOptions {
return func(s *ScooterDataSeeder) {
s.numberOfScooters = c
}
}
func WithScooterPerCircle(spc int) ScooterDataSeederOptions {
return func(s *ScooterDataSeeder) {
s.scootersPerCircle = spc
}
}
func WithStartDelay(delay time.Duration) ScooterDataSeederOptions {
return func(s *ScooterDataSeeder) {
s.startDelay = delay
}
}
func (s *ScooterDataSeeder) addRandomScooters() error {
distance := s.distanceShift
var counter int
for {
for i := 1; i < s.scootersPerCircle; i++ {
// Add a new scooter
scooterID, err := CreateRandomScooter(s)
if err != nil {
return err
}
// Add a new scooter init event
lat, lng := ShiftLocation(s.baseLat, s.baseLng, distance, (20*i)%360)
s.statusUpdater.UpdateStatus(s.ctx, struct {
ScooterID string
Timestamp string
Latitude string
Longitude string
EventType string
}{
ScooterID: *scooterID,
EventType: scooter.EVENT_PERIODIC_UPDATE,
Timestamp: time.Now().Format(time.RFC3339),
Latitude: fmt.Sprintf("%f", lat),
Longitude: fmt.Sprintf("%f", lng),
})
counter = counter + 1
if counter >= s.numberOfScooters {
return nil
}
}
distance = distance + .2
}
}
func CreateRandomScooter(s *ScooterDataSeeder) (*string, error) {
scooterID := uuid.New().String()
if err := s.scooterCreator.Create(s.ctx, scootertype.Scooter{
ID: scooterID,
InUse: false,
}); err != nil {
return nil, err
}
return &scooterID, nil
}
func ShiftLocation(latitude, longitude float64, distance float64, bearing int) (lat, lng float64) {
R := 6378.1 // Radius of the Earth
brng := float64(bearing) * math.Pi / 180 // Convert bearing to radian
lat = latitude * math.Pi / 180 // Current coords to radians
lng = longitude * math.Pi / 180
// Do the math magic
lat = math.Asin(math.Sin(lat)*math.Cos(distance/R) + math.Cos(lat)*math.Sin(distance/R)*math.Cos(brng))
lng += math.Atan2(math.Sin(brng)*math.Sin(distance/R)*math.Cos(lat), math.Cos(distance/R)-math.Sin(lat)*math.Sin(lat))
return (lat * 180 / math.Pi), (lng * 180 / math.Pi)
}