-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsers.go
113 lines (94 loc) · 2.32 KB
/
parsers.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
package lpr
import (
"errors"
"strconv"
"strings"
"time"
)
func parsePullAddress(body string) (string, error) {
address, err := stringInBetween(body, "<wsa:Address>", "</wsa:Address>")
if err != nil {
return "", errors.New("<wsa:Address> tag is not found in response")
}
return address, nil
}
func parseRecognition(body string) *Recognition {
if !strings.Contains(body, "PlateNumber") {
return nil
}
data, err := stringInBetween(body, "<tt:Data>", "</tt:Data>")
if err != nil {
return nil
}
split := strings.SplitAfter(data, ">")
split = split[:len(split)-1]
now := time.Now()
rec := &Recognition{
Timestamp: now,
}
for _, line := range split {
name, value, err := parseItem(line)
if err != nil {
return nil
}
switch name {
case "PlateNumber":
rec.LicencePlate = value
case "Likelihood":
intValue, err := strconv.Atoi(value)
if err == nil {
if intValue > 100 {
rec.Confidence = intValue / 10
} else {
rec.Confidence = intValue
}
}
case "Nation":
rec.Nation = value
case "Country":
rec.Country = value
case "VehicleDirection":
switch value {
case "reverse":
rec.Direction = Leaving
case "forward":
rec.Direction = Approaching
default:
rec.Direction = Unknown
}
}
}
if rec.LicencePlate == "" {
return nil
}
return rec
}
func parseItem(line string) (string, string, error) {
startIndex := strings.Index(line, "<tt:SimpleItem Name=\"")
if startIndex == -1 {
return "", "", errors.New("invalid input")
}
endIndex := strings.Index(line[startIndex:], "\" Value=\"")
if endIndex == -1 {
return "", "", errors.New("invalid input")
}
name := line[startIndex+len("<tt:SimpleItem Name=\"") : startIndex+endIndex]
startIndex = startIndex + endIndex + len("\" Value=\"")
endIndex = strings.Index(line[startIndex:], "\"/>")
if endIndex == -1 {
return "", "", errors.New("invalid input")
}
value := line[startIndex : startIndex+endIndex]
return name, value, nil
}
func stringInBetween(source, start, end string) (string, error) {
startIndex := strings.Index(source, start)
if startIndex == -1 {
return "", errors.New("no string found")
}
endIndex := strings.Index(source[startIndex:], end)
if endIndex == -1 {
return "", errors.New("no string found")
}
return source[startIndex+len(start) : startIndex+endIndex], nil
}