/
core.go
504 lines (449 loc) · 10.5 KB
/
core.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
// Package senbay provides the functions to encode and
// decode to the senbay format.
package senbay
import (
"errors"
"fmt"
"math"
"strconv"
"strings"
)
const keyCodeEsc = 27
// BaseX descrive base number based on PN
type BaseX struct {
PN int
Table []int
ReverseTable []int
}
var (
reversedKeys = map[string]string{
"TIME": "0", "LONG": "1", "LATI": "2",
"ALTI": "3", "ACCX": "4", "ACCY": "5",
"ACCZ": "6", "YAW": "7", "ROLL": "8",
"PITC": "9", "HEAD": "A", "SPEE": "B",
"BRIG": "C", "AIRP": "D", "HTBT": "E",
}
)
// NewBaseX returns a new BaseX based on PN
func NewBaseX(positionalNotation int) (*BaseX, error) {
baseX := &BaseX{
PN: 121,
Table: []int{
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,
40, 41, 42, 43,
47, 48, 49,
50, 51, 52, 53, 54,
55, 56, 57, 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,
},
ReverseTable: []int{
0, 0, 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, 0,
38, 39, 40, 41, 0,
0, 0, 42, 43, 44,
45, 46, 47, 48, 49,
50, 51, 52, 0, 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,
},
}
if positionalNotation > len(baseX.Table) || positionalNotation < 2 {
msg := fmt.Sprint("shinsu must be 2-", len(baseX.Table))
return baseX, errors.New(msg)
}
baseX.PN = positionalNotation
return baseX, nil
}
// encodeLongValue returns
func (baseX BaseX) encodeLongValue(lVal int) []rune {
var isNegative bool
if lVal < 0 {
isNegative = true
lVal *= -1
}
var places []int
if lVal == 0 {
places = append(places, 0)
} else {
for lVal > 0 {
remainder := int(math.Mod(float64(lVal), float64(baseX.PN)))
places = append(places, baseX.Table[remainder])
lVal = lVal / baseX.PN
}
}
var muString []rune
for _, place := range places {
muString = append([]rune{int32(place)}, muString...)
}
if isNegative {
muString = append([]rune{'-'}, muString...)
}
return muString
}
func (baseX BaseX) encodeDoubleValue(dVal float64) []rune {
var isNegative bool
if dVal < 0 {
isNegative = true
dVal *= -1
}
strVal := strconv.FormatFloat(dVal, 'f', -1, 64)
vals := strings.Split(strVal, ".")
sVal, err := strconv.Atoi(vals[0])
if err != nil {
panic(err)
}
runeIntVal := baseX.encodeLongValue(sVal)
if len(vals) == 1 {
if isNegative {
runeIntVal = append([]rune{'-'}, runeIntVal...)
}
return runeIntVal
}
sVal, err = strconv.Atoi(vals[1])
if err != nil {
panic(err)
}
strDecVal := baseX.encodeLongValue(sVal)
var zeros []rune
aZero := baseX.encodeLongValue(0)
for _, aVal := range vals[1] {
if string(aVal) == "0" {
zeros = append(zeros, aZero...)
}
}
var encoded []rune
if isNegative {
encoded = append(encoded, '-')
}
encoded = append(encoded, runeIntVal...)
encoded = append(encoded, '.')
encoded = append(encoded, zeros...)
encoded = append(encoded, []rune(strDecVal)...)
return encoded
}
func (baseX BaseX) decodeLongValue(sVal []rune) int {
var isNegative bool
if sVal[0] == '-' {
isNegative = true
}
var totalVal float64
for i := len(sVal) - 1; i >= 0; i-- {
key := int(sVal[i])
v := math.Pow(
float64(baseX.PN),
float64(len(sVal)-i-1),
) * float64(baseX.ReverseTable[key])
totalVal += v
}
if isNegative {
return int(totalVal * -1)
}
return int(totalVal)
}
func (baseX BaseX) decodeDoubleValue(sVal []rune) float64 {
var isNegative bool
if sVal[0] == '-' {
isNegative = true
sVal = sVal[1:]
}
runeIntNum := []rune{}
runeFloatNum := []rune{}
var isFloat bool
for _, num := range sVal {
if num != '.' {
if isFloat {
runeFloatNum = append(runeFloatNum, num)
} else {
runeIntNum = append(runeIntNum, num)
}
} else {
isFloat = true
}
}
var intVal int
if len(runeIntNum) != 0 {
intVal = baseX.decodeLongValue(runeIntNum)
}
if len(runeFloatNum) == 0 {
return float64(intVal)
}
var zeros []rune
for _, aVal := range runeFloatNum {
if aVal == 0 {
zeros = append(zeros, aVal)
} else {
break
}
}
decVal := baseX.decodeLongValue(runeFloatNum[len(zeros):])
floatDigit := math.Floor(math.Log10(float64(decVal))+1.0) + float64(len(zeros))
floatNum := float64(decVal) * math.Pow(0.1, floatDigit)
if isNegative {
return (float64(intVal) + floatNum) * -1.0
}
return float64(intVal) + floatNum
}
// Format is
type Format struct {
ReversedKeys map[string]string
PN int
baseX *BaseX
}
// NewSenbayFormat returns a new Format based on PN
func NewSenbayFormat(PN int) (*Format, error) {
baseX, err := NewBaseX(PN)
if err != nil {
return nil, err
}
senbayFormat := &Format{
ReversedKeys: reversedKeys,
PN: PN,
baseX: baseX,
}
return senbayFormat, nil
}
func (senbayFormat Format) getReservedShortKey(key string) string {
for k, v := range senbayFormat.ReversedKeys {
if k == key {
return v
}
}
return ""
}
func (senbayFormat Format) getReservedOriginalKey(key string) string {
for k, v := range senbayFormat.ReversedKeys {
if v == key {
return k
}
}
return ""
}
func (senbayFormat Format) encode(text string) string {
var encodedText string
elements := strings.Split(text, ",")
var count int
for _, element := range elements {
contents := strings.Split(element, ":")
if len(contents) > 1 {
key := contents[0]
var val string
for _, con := range contents[1:] {
if val == "" {
val = con
} else {
val = val + ":" + con
}
}
reservedKey := senbayFormat.getReservedShortKey(key)
var isReservedKey bool
if len(reservedKey) != 0 {
isReservedKey = true
key = reservedKey
}
if len(val) > 0 {
if val[:1] != "'" {
floatVal, err := strconv.ParseFloat(val, 64)
if err != nil {
panic(err)
}
if isReservedKey {
encodedText = encodedText + key + string(senbayFormat.baseX.encodeDoubleValue(floatVal))
} else {
encodedText = encodedText + key + ":" + string(senbayFormat.baseX.encodeDoubleValue(floatVal))
}
} else {
if isReservedKey {
encodedText = encodedText + key + val
} else {
encodedText = encodedText + key + ":" + val
}
}
}
}
if count < len(elements)-1 {
count++
encodedText = encodedText + ","
}
}
return encodedText
}
// decode
func (senbayFormat Format) decode(text string) string {
var decodedText string
var count int
elements := strings.Split(text, ",")
for _, element := range elements {
var key []rune
var val []rune
contents := strings.Split(element, ":")
if len(contents) > 1 {
key = []rune(contents[0])
for _, con := range contents[1:] {
if len(val) == 0 {
val = []rune(con)
} else {
val = append(val, ':')
val = append(val, []rune(con)...)
}
}
} else {
key = []rune(contents[0])[:1]
val = []rune(contents[0])[1:]
}
reservedKey := senbayFormat.getReservedOriginalKey(string(key))
if reservedKey != "" {
key = []rune(reservedKey)
}
if val[0] != '\'' {
decodedDoubleValue := senbayFormat.baseX.decodeDoubleValue(val)
decodedText = decodedText + string(key) + ":" + strconv.FormatFloat(decodedDoubleValue, 'f', -1, 64)
} else {
decodedText = decodedText + string(key) + ":" + string(val)
}
if count < len(elements)-1 {
count++
decodedText = decodedText + ";"
}
}
return decodedText
}
// A Data is
type Data struct {
senbayData map[string]string
PN int
SF *Format
}
// NewSenbayData returns a new SenbayData struct based on PN
func NewSenbayData(PN int) (*Data, error) {
SD := &Data{
PN: PN,
}
SF, err := NewSenbayFormat(PN)
if err != nil {
return SD, err
}
SD.senbayData = map[string]string{}
SD.SF = SF
return SD, err
}
// AddInt add int value to senbayData
func (SD Data) AddInt(key string, value int) {
SD.senbayData[key] = strconv.Itoa(value)
}
// AddInt64 add int64 value to senbayData
func (SD Data) AddInt64(key string, value int64) {
SD.senbayData[key] = strconv.FormatInt(value, 10)
}
// AddFloat add float value to senbayData
func (SD Data) AddFloat(key string, value float32) {
SD.senbayData[key] = strconv.FormatFloat(float64(value), 'f', -1, 64)
}
// AddFloat64 add float64 value to senbayData
func (SD Data) AddFloat64(key string, value float64) {
SD.senbayData[key] = strconv.FormatFloat(value, 'f', -1, 64)
}
// AddText add string value to senbayData
func (SD Data) AddText(key string, value string) {
SD.senbayData[key] = "'" + value + "'"
}
// Clear empties the contents of Data
func (SD Data) Clear() {
for key := range SD.senbayData {
delete(SD.senbayData, key)
}
}
// Encode converts the data to decoded.
func (SD Data) Encode(compress bool) string {
var formattedData string
var count int
for k, v := range SD.senbayData {
formattedData = formattedData + k + ":" + v
if count < len(SD.senbayData)-1 {
count++
formattedData = formattedData + ","
}
}
if compress {
return "V:4," + SD.SF.encode(formattedData)
}
return "V:3," + formattedData
}
// Decode converts the decoded text to the original data.
func (SD Data) Decode(text string) map[string]string {
senbayMap := map[string]string{}
elements := strings.Split(text, ",")
var isCompress bool
for _, element := range elements {
contents := strings.Split(element, ":")
if len(contents) > 1 && contents[0] == "V" && contents[1] == "4" {
isCompress = true
break
}
}
if isCompress {
text = SD.SF.decode(text)
}
elements = strings.Split(text, ",")
for _, element := range elements {
contents := strings.Split(element, ":")
if len(contents) > 1 {
key := contents[0]
var value string
for _, con := range contents[1:] {
if value == "" {
value = con
} else {
value = value + ":" + con
}
}
if key != "V" {
if value != "None" {
if value[:1] == "'" {
senbayMap[key] = value[1 : len(value)-1]
} else {
senbayMap[key] = value
}
}
}
}
}
return senbayMap
}