-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexport.go
More file actions
268 lines (261 loc) · 6.83 KB
/
export.go
File metadata and controls
268 lines (261 loc) · 6.83 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package main
import (
"encoding/csv"
"fmt"
"net/http"
"os"
"strconv"
"time"
"github.com/xuri/excelize/v2"
)
var fields []string = []string{
"位次", "呼号", "日期时间", "频率", "模式",
"信号", "对方设备", "对方天线", "对方功率", "对方台址",
"己方设备", "己方天线", "己方功率", "己方台址", "备注",
}
func getCellIdx(row, col int64) string {
colStr := []byte{}
for col > 0 {
col -= 1
colStr = append(colStr, byte('A'+col%26))
col /= 26
}
return string(colStr) + strconv.FormatInt(row, 10)
}
func getADIF(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "Bad Method.", http.StatusMethodNotAllowed)
return
}
name := r.URL.Query().Get("n")
if name == "" {
http.Error(w, "Bad args.", http.StatusBadRequest)
return
}
file, err := os.OpenFile(name+".hjl", os.O_RDWR|os.O_SYNC, 0644)
if err != nil {
http.Error(w, "File error: "+err.Error(), http.StatusInternalServerError)
return
}
lines, err := parseLog(file)
if err != nil {
http.Error(w, "Parse data error: "+err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Disposition", "attachment; filename="+name+".adi")
w.Header().Set("Content-Type", "text/plain")
for _, line := range lines {
err = writeADIF(w, line)
if err != nil {
http.Error(w, "Write data error: "+err.Error(), http.StatusInternalServerError)
return
}
}
}
func getCSV(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "Bad Method.", http.StatusMethodNotAllowed)
return
}
name := r.URL.Query().Get("n")
if name == "" {
http.Error(w, "Bad args.", http.StatusBadRequest)
return
}
selects := r.URL.Query().Get("s")
if selects == "" {
http.Error(w, "您未选择任何列\nYou didn't select any column.", http.StatusBadRequest)
return
}
if len(selects) > 15 {
http.Error(w, "Bad args.", http.StatusBadRequest)
return
}
bjthhmm := r.URL.Query().Get("bjthhmm")
if bjthhmm == "" || len(bjthhmm) != 1 {
http.Error(w, "Bad args.", http.StatusBadRequest)
return
}
file, err := os.OpenFile(name+".hjl", os.O_RDWR|os.O_SYNC, 0644)
if err != nil {
http.Error(w, "File error: "+err.Error(), http.StatusInternalServerError)
return
}
lines, err := parseLog(file)
if err != nil {
http.Error(w, "Parse data error: "+err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Disposition", "attachment; filename="+name+".csv")
w.Header().Set("Content-Type", "application/csv")
cw := csv.NewWriter(w)
head := []string{}
for _, s := range selects {
if s-'a' < 15 {
head = append(head, fields[s-'a'])
}
}
cw.Write(head)
for _, line := range lines {
cl := []string{}
for _, s := range selects {
switch s - 'a' {
case 0:
cl = append(cl, strconv.FormatInt(line.Index, 10))
case 1:
cl = append(cl, line.Callsign)
case 2:
if bjthhmm == "1" {
t, err := time.Parse("2006-01-02T15:04", line.Dt)
if err == nil {
cl = append(cl, t.Add(8*time.Hour).Format("15:04"))
} else {
cl = append(cl, err.Error())
}
} else {
cl = append(cl, line.Dt)
}
case 3:
cl = append(cl, line.Freq)
case 4:
cl = append(cl, line.Mode)
case 5:
cl = append(cl, line.getRst())
case 6:
cl = append(cl, line.RRig)
case 7:
cl = append(cl, line.RAnt)
case 8:
cl = append(cl, line.RPwr)
case 9:
cl = append(cl, line.RQth)
case 10:
cl = append(cl, line.TRig)
case 11:
cl = append(cl, line.TAnt)
case 12:
cl = append(cl, line.TPwr)
case 13:
cl = append(cl, line.TQth)
case 14:
cl = append(cl, line.Rmks)
}
}
cw.Write(cl)
}
cw.Flush()
}
func getXLSX(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "Bad Method.", http.StatusMethodNotAllowed)
return
}
name := r.URL.Query().Get("n")
if name == "" {
http.Error(w, "Bad args.", http.StatusBadRequest)
return
}
selects := r.URL.Query().Get("s")
if selects == "" {
http.Error(w, "您未选择任何列\nYou didn't select any column.", http.StatusBadRequest)
return
}
if len(selects) > 15 {
http.Error(w, "Bad args.", http.StatusBadRequest)
return
}
bjthhmm := r.URL.Query().Get("bjthhmm")
if bjthhmm == "" || len(bjthhmm) != 1 {
http.Error(w, "Bad args.", http.StatusBadRequest)
return
}
file, err := os.OpenFile(name+".hjl", os.O_RDWR|os.O_SYNC, 0644)
if err != nil {
http.Error(w, "File error: "+err.Error(), http.StatusInternalServerError)
return
}
lines, err := parseLog(file)
if err != nil {
http.Error(w, "Parse data error: "+err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Disposition", "attachment; filename="+name+".xlsx")
w.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
xlsx := excelize.NewFile()
defer func() {
if err := xlsx.Close(); err != nil {
fmt.Println(err)
}
}()
var idx int64 = 1
for _, s := range selects {
if s-'a' < 15 {
xlsx.SetCellStr("Sheet1", getCellIdx(1, idx), fields[s-'a'])
idx++
}
}
for i, line := range lines {
idx = 1
for _, s := range selects {
switch s - 'a' {
case 0:
xlsx.SetCellInt("Sheet1", getCellIdx(int64(i+2), idx), line.Index)
idx++
case 1:
xlsx.SetCellStr("Sheet1", getCellIdx(int64(i+2), idx), line.Callsign)
idx++
case 2:
if bjthhmm == "1" {
t, err := time.Parse("2006-01-02T15:04", line.Dt)
if err == nil {
xlsx.SetCellStr("Sheet1", getCellIdx(int64(i+2), idx), t.Add(8*time.Hour).Format("15:04"))
} else {
xlsx.SetCellStr("Sheet1", getCellIdx(int64(i+2), idx), err.Error())
}
} else {
xlsx.SetCellStr("Sheet1", getCellIdx(int64(i+2), idx), line.Dt)
}
idx++
case 3:
xlsx.SetCellStr("Sheet1", getCellIdx(int64(i+2), idx), line.Freq)
idx++
case 4:
xlsx.SetCellStr("Sheet1", getCellIdx(int64(i+2), idx), line.Mode)
idx++
case 5:
xlsx.SetCellStr("Sheet1", getCellIdx(int64(i+2), idx), line.getRst())
idx++
case 6:
xlsx.SetCellStr("Sheet1", getCellIdx(int64(i+2), idx), line.RRig)
idx++
case 7:
xlsx.SetCellStr("Sheet1", getCellIdx(int64(i+2), idx), line.RAnt)
idx++
case 8:
xlsx.SetCellStr("Sheet1", getCellIdx(int64(i+2), idx), line.RPwr)
idx++
case 9:
xlsx.SetCellStr("Sheet1", getCellIdx(int64(i+2), idx), line.RQth)
idx++
case 10:
xlsx.SetCellStr("Sheet1", getCellIdx(int64(i+2), idx), line.TRig)
idx++
case 11:
xlsx.SetCellStr("Sheet1", getCellIdx(int64(i+2), idx), line.TAnt)
idx++
case 12:
xlsx.SetCellStr("Sheet1", getCellIdx(int64(i+2), idx), line.TPwr)
idx++
case 13:
xlsx.SetCellStr("Sheet1", getCellIdx(int64(i+2), idx), line.TQth)
idx++
case 14:
xlsx.SetCellStr("Sheet1", getCellIdx(int64(i+2), idx), line.Rmks)
idx++
}
}
}
if err := xlsx.Write(w); err != nil {
fmt.Fprint(w, err.Error())
}
}