-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
159 lines (143 loc) · 3.08 KB
/
utils.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
package soumuradio
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math"
"net/http"
"os"
"strconv"
"strings"
"unicode"
"github.com/tomato3713/soumuradio/internal"
)
func checkStatusCode(res *http.Response) error {
sc := res.StatusCode
if sc >= 200 && sc < 300 {
return nil
}
if sc == 400 {
var errJSON internal.ErrorBody
if err := decodeBody(res, &errJSON, nil); err != nil {
logf("invalid Request, but cannot decode JSON Response: %v", err)
}
for _, errArr := range errJSON.Err {
logf("invalid Request: %v", errArr.ErrMsg)
}
return errJSON.InvalidRequestError()
}
if sc == 500 {
logf("system error")
return SystemError()
}
return HTTPStatusError(sc)
}
func decodeBody(resp *http.Response, out interface{}, f *os.File) error {
defer resp.Body.Close()
if f != nil {
resp.Body = ioutil.NopCloser(io.TeeReader(resp.Body, f))
defer f.Close()
}
decorder := json.NewDecoder(resp.Body)
return decorder.Decode(out)
}
func removeAllSpace(str string) string {
var ret []rune
for _, c := range str {
if !unicode.IsSpace(c) {
ret = append(ret, c)
}
}
return string(ret)
}
func parseRadioSpec1(str string) ([]RadioSpec, error) {
var rs []RadioSpec
strRune := []rune(str)
for j := 0; j < len(strRune); j++ {
var v RadioSpec
for i := j; i < len(strRune); i++ {
if unicode.IsSpace(strRune[i]) {
continue
}
if i+2 < len(strRune) && string(strRune[i:i+2]) == string("\\t") {
if len(v.RadioFormat) == 0 {
rf := string(strRune[j:i])
v.RadioFormat = strings.Fields(rf)
} else {
v.Freq = removeAllSpace(string(strRune[j:i]))
}
i += 2
j = i
} else if i+2 < len(strRune) && string(strRune[i:i+2]) == string("\\n") || i+1 == len(strRune) {
var powerstr string
if i+1 == len(strRune) {
powerstr = removeAllSpace(string(strRune[j : i+1]))
} else {
powerstr = removeAllSpace(string(strRune[j:i]))
}
if len(powerstr) > 0 {
power, err := rmSIPrefix(powerstr)
if err != nil {
return nil, err
}
v.Power = power
}
rs = append(rs, v)
j = i + 1
break
}
}
}
return rs, nil
}
func rmSIPrefix(str string) (float64, error) {
strr := []rune(str)
u := "W"
byteIdx := strings.Index(str, u)
if byteIdx == -1 {
return 0, fmt.Errorf("could not parsed error: %v", str)
}
runeIdx := len([]rune(str[0:byteIdx])) + 1
corr := map[string]int{
"d": -1,
"c": -2,
"m": -3,
"μ": -6,
"n": -9,
"p": -12,
"f": -15,
"a": -18,
"z": -21,
"y": -24,
"h": 2,
"k": 3,
"M": 6,
"G": 9,
"T": 12,
"P": 15,
"E": 18,
"Z": 24,
}
if unicode.IsNumber(strr[runeIdx-2]) {
num, err := strconv.ParseFloat(string(strr[:runeIdx-1]), 64)
if err != nil {
return 0, fmt.Errorf("isnumber is true: %v", err)
}
return num, nil
}
for k, v := range corr {
if string(strr[runeIdx-2]) == k {
num, err := strconv.ParseFloat(string(strr[:runeIdx-2]), 64)
if err != nil {
return 0, err
}
if v > 0 {
return num * math.Pow10(v), nil
} else {
return num / math.Pow10(-v), nil
}
}
}
return 0, fmt.Errorf("could not parsed error: %v", str)
}