/
vehicle_position.go
74 lines (59 loc) · 2.51 KB
/
vehicle_position.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
package sptrans
import (
"fmt"
)
const (
defaultVehiclePositionPath = "Posicao"
defaultVehiclePositionLinePath = "Posicao/Linha"
defaultVehiclePositionCompanyPath = "Posicao/Garagem"
)
// VehiclePositionService provide functions to request vehicles position endpoints
type VehiclePositionService service
// VehiclePositionResponse structure, see documentation on http://www.sptrans.com.br/desenvolvedores/APIOlhoVivo/Documentacao.aspx?1#docApi-posicao
type VehiclePositionResponse struct {
Hour string `json:"hr"`
Lines []*VehiclePositionLine `json:"l"`
}
// VehiclePositionLineResponse structure
type VehiclePositionLineResponse struct {
Hour string `json:"hr"`
Vehicles []*VehiclePosition `json:"vs"`
}
// VehiclePositionLine relation of localized lines
type VehiclePositionLine 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 []*VehiclePosition `json:"vs"`
}
// VehiclePosition relation of localized vehicles
type VehiclePosition struct {
Prefix int64 `json:"p"`
Accessible bool `json:"a"`
Hour string `json:"ta"`
Latitude float64 `json:"py"`
Longitude float64 `json:"px"`
}
// All returns all vehicles position
func (r *VehiclePositionService) All() ([]*VehiclePositionLine, error) {
var vehiclePositionResponse *VehiclePositionResponse
_, err := r.client.Request("GET", defaultVehiclePositionPath, nil, &vehiclePositionResponse)
return vehiclePositionResponse.Lines, err
}
// SearchByLine search vehicles position by line id
func (r *VehiclePositionService) SearchByLine(lineId int) ([]*VehiclePosition, error) {
path := fmt.Sprintf("%s?codigoLinha=%d", defaultVehiclePositionLinePath, lineId)
var vehiclePositionLineResponse *VehiclePositionLineResponse
_, err := r.client.Request("GET", path, nil, &vehiclePositionLineResponse)
return vehiclePositionLineResponse.Vehicles, err
}
// SearchByCompany search vehicles position by company id
func (r *VehiclePositionService) SearchByCompany(companyId int) ([]*VehiclePositionLine, error) {
path := fmt.Sprintf("%s?codigoEmpresa=%d", defaultVehiclePositionCompanyPath, companyId)
var vehiclePositionResponse *VehiclePositionResponse
_, err := r.client.Request("GET", path, nil, &vehiclePositionResponse)
return vehiclePositionResponse.Lines, err
}