-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwatcher.go
67 lines (53 loc) · 1.19 KB
/
watcher.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
package communautowatcher
import (
"fmt"
"time"
)
type Car struct {
Distance float64
Latitude float64
Longitude float64
LocationName string
}
type CarQuery struct {
FromLatitude string
FromLongitude string
CityID string
MaxDistance float64
StartDate time.Time
EndDate time.Time
}
type CityID string
const (
Montreal CityID = "59"
Quebec = "90"
Sherbrooke = "89"
// Inspected requests from https://www.reservauto.net/Scripts/Client/Mobile/Default.asp?BranchID=1#
)
type Watcher interface {
GetQueries() []CarQuery
OnCarAvailable(query CarQuery, cars []Car)
}
type WatcherOptions struct {
Interval time.Duration
Watcher Watcher
}
func StartWatcher(options WatcherOptions) {
checkForAvailabilities(options)
for range time.Tick(options.Interval) {
checkForAvailabilities(options)
}
}
func checkForAvailabilities(options WatcherOptions) {
watcher := options.Watcher
queries := options.Watcher.GetQueries()
for _, query := range queries {
cars, err := GetAvailableCars(query)
if err != nil {
fmt.Printf("Could not retrieve available cars: %v\n", err)
}
if len(cars) > 0 {
watcher.OnCarAvailable(query, cars)
}
}
}