-
Notifications
You must be signed in to change notification settings - Fork 3
/
unit.go
174 lines (156 loc) · 4.76 KB
/
unit.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package handlers
import (
"encoding/json"
"net/http"
"strings"
"github.com/arnie97/emu-log/adapters"
"github.com/arnie97/emu-log/common"
"github.com/arnie97/emu-log/models"
"github.com/go-chi/chi"
"github.com/rs/zerolog/log"
)
type urlWrapper struct {
URL *string `json:"url,omitempty"`
}
// singleUnitNoHandler takes an exact unit number,
// and returns the 30 most recent log items for the unit.
func singleUnitNoHandler(w http.ResponseWriter, r *http.Request) {
unitNo := common.NormalizeUnitNo(chi.URLParam(r, "unitNo"))
results := models.ListTrainsForSingleUnitNo("%" + unitNo)
jsonResponse(results, w)
}
// multiUnitNoHandler takes multiple exact unit numbers,
// and returns the most recent log item for each of them.
func multiUnitNoHandler(w http.ResponseWriter, r *http.Request) {
unitNoList := strings.Split(chi.URLParam(r, "unitNo"), ",")
results := models.ListLatestTrainForMultiUnits(unitNoList)
jsonResponse(results, w)
}
// fuzzyUnitNoHandler takes an incomplete part of the unit number,
// and returns the most recent occurance for the first 30 units
// in lexicographical order that matches the given fuzzy pattern.
func fuzzyUnitNoHandler(w http.ResponseWriter, r *http.Request) {
unitNo := common.NormalizeUnitNo(chi.URLParam(r, "unitNo"))
results := models.ListLatestTrainForMatchedUnits(unitNo)
jsonResponse(results, w)
}
// unitBuildURLHandler takes an exact unit number, and rebuild
// the URL encoded in one of the QR code stickers attached to the unit.
func unitBuildURLHandler(w http.ResponseWriter, r *http.Request) {
unitNo := common.NormalizeUnitNo(chi.URLParam(r, "unitNo"))
serialModels := models.ListSerialsForSingleUnit(unitNo)
var results urlWrapper
for _, s := range serialModels {
if a := adapters.Adapters[s.Adapter]; a != nil {
url := adapters.BuildURL(a, s.SerialNo)
results.URL = &url
break
}
}
jsonResponse(results, w)
}
// unitParseURLHandler tries to parse the URL
// encoded in one of the QR code stickers attached to the unit.
func unitParseURLHandler(w http.ResponseWriter, r *http.Request) {
// case 1: invalid URL
var (
input urlWrapper
results = make([]models.LogModel, 0)
)
defer func() {
jsonResponse(results, w)
}()
if json.NewDecoder(r.Body).Decode(&input); input.URL == nil {
w.WriteHeader(400)
return
}
// case 2: unrecognized URL
a, serial := adapters.ParseURL(*input.URL)
unitNo := common.NormalizeUnitNo(chi.URLParam(r, "unitNo"))
if a == nil {
serialModel := models.SerialModel{
Adapter: "?",
Operator: "?",
UnitNo: unitNo,
SerialNo: *input.URL,
}
serialModel.Add()
log.Debug().Msgf("[%s] %v", serialModel.Adapter, serialModel)
return
}
// case 3: unit currently offline or unit number does not match
serialModel := models.SerialModel{
Adapter: a.Code(),
Operator: "?",
SerialNo: serial,
}
info, err := a.Info(serial)
if err == nil {
serialModel.UnitNo, err = a.UnitNo(serial, info)
}
serialModel.Operator, err = adapters.Operator(a, serial, info)
if !common.ApproxEqualUnitNo(unitNo, serialModel.UnitNo) {
serialModel.UnitNo = "-" + unitNo + "@" + serialModel.UnitNo
serialModel.Add()
log.Debug().Msgf("[%s] %v", serialModel.Adapter, serialModel)
return
}
// case 4: unit number matches user input
serialModel.Add()
serialModel.AddTrainOperationLogs(info)
results = models.ListTrainsForSingleUnitNo(serialModel.UnitNo)
return
}
// unitParseURLMapHandler tries to parse the URL in the QR code stickers
// attached to the unit and returns a map as result.
func unitParseURLMapHandler(w http.ResponseWriter, r *http.Request) {
// case 1: invalid URL
var (
input urlWrapper
mapResp interface{}
)
defer func() {
jsonResponse(mapResp, w)
}()
if json.NewDecoder(r.Body).Decode(&input); input.URL == nil {
w.WriteHeader(400)
return
}
// case 2: unrecognized URL
a, serial := adapters.ParseURL(*input.URL)
if a == nil {
return
}
// case 3: unit currently offline
serialModel := models.SerialModel{
Adapter: a.Code(),
SerialNo: serial,
}
info, err := a.Info(serial)
if err == nil {
serialModel.UnitNo, err = a.UnitNo(serial, info)
}
serialModel.Operator, err = adapters.Operator(a, serial, info)
serialModel.Add()
serialModel.AddTrainOperationLogs(info)
resp := struct {
Operator string `json:"operator,omitempty"`
UnitNo string `json:"emu_no,omitempty"`
SerialNo string `json:"serial_no"`
Adapter struct {
Code string `json:"code"`
Name string `json:"name"`
} `json:"adapter"`
Logs []models.LogModel `json:"logs,omitempty"`
}{
Operator: serialModel.Operator,
UnitNo: serialModel.UnitNo,
SerialNo: serial,
Logs: models.ListTrainsForSingleUnitNo(serialModel.UnitNo),
}
resp.Adapter.Code = a.Code()
resp.Adapter.Name = a.Name()
mapResp = resp
log.Debug().Msgf("[%s] %+v", serialModel.Adapter, mapResp)
return
}