-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
148 lines (127 loc) · 4.55 KB
/
main.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
package main
import (
"flag"
"log"
"os"
"time"
)
var (
uFlag = flag.String("u", "", "Username. (required)")
pFlag = flag.String("p", "", "Password. (required)")
rFlag = flag.String("r", "", "Reg. nr. of the car. (required)")
cFlag = flag.String("c", "", "Confirmation email address. (optional)")
eFlag = flag.Bool("e", false, "If the parking should be extended, ie. 72 hours, instead of 6 hours. (optional)")
vFalg = flag.Bool("v", false, "Verbose output. (optional)")
vvFalg = flag.Bool("vv", false, "Extra verbose output. Adds network request logging. (optional)")
)
func main() {
flag.Parse()
username := *uFlag
password := *pFlag
regNr := *rFlag
extended := *eFlag
confirmationEmail := *cFlag
verbose := *vFalg || *vvFalg
if username == "" || password == "" || regNr == "" {
log.Fatal("Missing required arguments.")
}
if verbose {
log.Println("Username:", username)
log.Println("Password:", password)
log.Println("Reg. nr.:", regNr)
log.Println("Email:", confirmationEmail)
log.Println("Extended:", extended)
log.Println("Verbose:", verbose)
}
httpClient := NewClient("https://pservice-permit.giantleap.no", "/api/kiosk-v2")
// Log in
authRes := AuthResponse{}
authReq := AuthRequest{
UserName: username,
Password: password,
}
if err := httpClient.Post("/authenticate.json", &authRes, &authReq); err != nil {
LogFatalError("Failed to login", err)
}
if authRes.ResultCode != "SUCCESS" || authRes.Token == "" {
LogFatalApiError("Authentication Failed", authRes.ApiError)
}
httpClient.SetToken(authRes.Token)
if verbose {
log.Println("Authentication Successful")
}
// Get profile data
profileRes := ProfileResponse{}
if err := httpClient.Get("/profile.json", &profileRes); err != nil {
LogFatalError("Failed to get profile data", err)
}
if profileRes.ResultCode != "SUCCESS" {
LogFatalApiError("Failed to get profile data", profileRes.ApiError)
}
if verbose {
log.Println(profileRes.Profile.WelcomeMessage)
log.Println(profileRes.Profile.Agents[0].Name)
log.Println("Possible durations:")
for _, product := range profileRes.Profile.Agents[0].KioskProducts {
log.Println(product.ProductName)
}
}
// Get plate data
plateRes := PlateResponse{}
if err := httpClient.Get("/platenumber/lookup/"+regNr+".json", &plateRes); err != nil {
LogFatalError("Failed to get plate data", err)
}
if plateRes.ResultCode != "SUCCESS" {
LogFatalApiError("Failed to get plate data", plateRes.ApiError)
}
if verbose {
log.Printf("Reg. nr. %q (%q)\n", regNr, plateRes.Message)
}
// Status
statusRes := StatusResponse{}
if err := httpClient.Get("/parking/status.json?kioskAgentId="+profileRes.Profile.Agents[0].Id+"&plateNumber="+regNr, &statusRes); err != nil {
LogFatalError("Failed to get status", err)
}
if statusRes.ResultCode != "SUCCESS" {
LogFatalApiError("Failed to get status", statusRes.ApiError)
}
if statusRes.StartTime != "" && statusRes.EndTime != "" {
now := time.Now()
startTime, _ := time.Parse("2006-01-02 15:04:05", statusRes.StartTime)
endTime, _ := time.Parse("2006-01-02 15:04:05", statusRes.EndTime)
endQuarantineTime, _ := time.Parse("2006-01-02 15:04:05", statusRes.EndOfWaitingPeriod)
duration := endTime.Sub(startTime)
if now.Before(endTime) {
log.Printf("Parking (%.1f hours) for %s is still active until %s\n", duration.Hours(), regNr, endTime.Format("2006-01-02 15:04:05"))
os.Exit(0)
return
}
if now.Before(endQuarantineTime) {
log.Printf("Failed to register parking (%.1f hours) for %s. Quarantined until %s\n", duration.Hours(), regNr, endQuarantineTime.Format("2006-01-02 15:04:05"))
os.Exit(1)
return
}
}
productId := profileRes.Profile.Agents[0].KioskProducts[0].Id
if extended {
productId = profileRes.Profile.Agents[0].KioskProducts[1].Id
}
registerRes := RegisterResponse{}
httpClient.Post("/parking/register.json", ®isterRes, RegisterRequest{
PlateNumber: regNr,
KioskAgentId: profileRes.Profile.Agents[0].Id,
ProductId: productId,
TermsAccepted: false,
AcceptedTermsAndConditionsId: nil,
QualificationForm: []string{},
ConfirmationEmail: confirmationEmail,
})
if registerRes.ResultCode != "SUCCESS" {
LogFatalApiError("Failed to register", registerRes.ApiError)
}
log.Printf("Successfully registered at %s parking for %q, from %s to %s\n", registerRes.Parking.AgentName, regNr, registerRes.Parking.StartTime, registerRes.Parking.EndTime)
if confirmationEmail != "" {
log.Printf("Confirmation email sent to %q\n", confirmationEmail)
}
os.Exit(0)
}