-
Notifications
You must be signed in to change notification settings - Fork 1
/
forecast.go
95 lines (77 loc) · 3.01 KB
/
forecast.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
package sptrans
import (
"fmt"
)
const (
defaultForecastPath = "Previsao"
defaultForecastLinePath = "Previsao/Linha"
defaultForecastStopPath = "Previsao/Parada"
)
// ForecastService provide functions to request forecasts endpoints
type ForecastService service
// ForecastResponse structure, see documentation on http://www.sptrans.com.br/desenvolvedores/APIOlhoVivo/Documentacao.aspx?1#docApi-previsao
type ForecastResponse struct {
Hour string `json:"hr"`
Stop ForecastStop `json:"p"`
}
// ForecastLineResponse is a line response structure with hour and a list of stops
type ForecastLineResponse struct {
Hour string `json:"hr"`
Stops []*ForecastLineStop `json:"ps"`
}
// ForecastLineStop is a line stop structure
type ForecastLineStop struct {
Id int64 `json:"cp"`
Name string `json:"np"`
Latitude float64 `json:"py"`
Longitude float64 `json:"px"`
Vehicles []*ForecastVehicle `json:"vs"`
}
// ForecastStop is a stop structure
type ForecastStop struct {
Id int64 `json:"cp"`
Name string `json:"np"`
Latitude float64 `json:"py"`
Longitude float64 `json:"px"`
Lines []*ForecastLine `json:"l"`
}
//ForecastLine is a line structure
type ForecastLine struct {
FullSign string `json:"c"`
Id int64 `json:"cl"`
Direction int64 `json:"sl"`
DestinationSign string `json:"lt0"`
OriginSign string `json:"lt1"`
QuantityVehicles int64 `json:"qv"`
Vehicles []*ForecastVehicle `json:"vs"`
}
//ForecastVehicle is a vehicle structure
type ForecastVehicle struct {
Prefix int64 `json:"p"`
Hour string `json:"t"`
Accessible bool `json:"a"`
UpdateTime string `json:"ta"`
Latitude float64 `json:"py"`
Longitude float64 `json:"px"`
}
// Search performs a search of bus arrivals based on stop id or line id
func (r *ForecastService) Search(stopId int, lineId int) (ForecastStop, error) {
path := fmt.Sprintf("%s?codigoParada=%d&codigoLinha=%d", defaultForecastPath, stopId, lineId)
var forecastResponse *ForecastResponse
_, err := r.client.Request("GET", path, nil, &forecastResponse)
return forecastResponse.Stop, err
}
// SearchByLine performs a search of the bus arrivals based on the line id
func (r *ForecastService) SearchByLine(lineId int) ([]*ForecastLineStop, error) {
path := fmt.Sprintf("%s?codigoLinha=%d", defaultForecastLinePath, lineId)
var forecastLineResponse *ForecastLineResponse
_, err := r.client.Request("GET", path, nil, &forecastLineResponse)
return forecastLineResponse.Stops, err
}
// SearchByStop performs a search of the bus arrivals based on the stop id
func (r *ForecastService) SearchByStop(stopId int) (ForecastStop, error) {
path := fmt.Sprintf("%s?codigoParada=%d", defaultForecastStopPath, stopId)
var forecastResponse *ForecastResponse
_, err := r.client.Request("GET", path, nil, &forecastResponse)
return forecastResponse.Stop, err
}