-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.go
142 lines (130 loc) · 3.28 KB
/
client.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
package dg1670aexporter
import (
"bytes"
"context"
"errors"
"io"
"net/http"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/northbright/ctx/ctxcopy"
"golang.org/x/net/context/ctxhttp"
"golang.org/x/net/html"
)
type client struct {
client *http.Client
url string
}
func (c *client) fetch(ctx context.Context) (*modemData, error) {
req, err := http.NewRequest(http.MethodGet, c.url, nil)
if err != nil {
return nil, err
}
resp, err := ctxhttp.Do(ctx, c.client, req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, errors.New("modem returned non-200")
}
buf := make([]byte, 2*1024*1024)
var out bytes.Buffer
if err := ctxcopy.Copy(ctx, &out, resp.Body, buf); err != nil {
return nil, err
}
return parseResp(&out)
}
func parseResp(data io.Reader) (*modemData, error) {
node, err := html.Parse(data)
if err != nil {
return nil, err
}
doc := goquery.NewDocumentFromNode(node)
out := &modemData{
ds: parseDownstream(doc),
us: parseUpstream(doc),
}
return out, nil
}
func parseDownstream(doc *goquery.Document) []downstreamData {
downstream := doc.Find("h4:contains('Downstream')").Next()
var out []downstreamData
_ = downstream.Find("tr").Each(func(i int, selection *goquery.Selection) {
if i == 0 {
return
}
data := selection.Find("td")
ch := downstreamData{
DCID: mustNodeAsInt(data.Nodes[1]),
Freq: mustNodeAsFloat(data.Nodes[2]) * 1000,
Power: mustNodeAsFloat(data.Nodes[3]),
SNR: mustNodeAsFloat(data.Nodes[4]),
Modulation: mustNodeAsInt(data.Nodes[5]),
Octets: mustNodeAsInt(data.Nodes[6]),
Correcteds: mustNodeAsInt(data.Nodes[7]),
Uncorrectables: mustNodeAsInt(data.Nodes[8]),
}
out = append(out, ch)
})
return out
}
func parseUpstream(doc *goquery.Document) []upstreamData {
upstream := doc.Find("h4:contains('Upstream')").Next()
var out []upstreamData
_ = upstream.Find("tr").Each(func(i int, selection *goquery.Selection) {
if i < 2 {
return
}
data := selection.Find("td")
us := upstreamData{
UCID: mustNodeAsInt(data.Nodes[1]),
Freq: mustNodeAsFloat(data.Nodes[2]),
Power: mustNodeAsFloat(data.Nodes[3]),
ChannelType: data.Nodes[4].FirstChild.Data,
SymbolRate: mustNodeAsInt(data.Nodes[5]),
Modulation: mustNodeAsInt(data.Nodes[6]),
}
out = append(out, us)
})
return out
}
func mustNodeAsFloat(d *html.Node) float64 {
data := strings.Split(d.FirstChild.Data, " ")[0]
floatData, err := strconv.ParseFloat(data, 64)
if err != nil {
panic("bad HTML node from modem")
}
return floatData
}
func mustNodeAsInt(d *html.Node) int64 {
data := strings.Replace(d.FirstChild.Data, "QAM", "", 1)
data = strings.Split(data, " ")[0]
intData, err := strconv.ParseInt(data, 10, 64)
if err != nil {
panic("bad HTML node from modem")
}
return intData
}
type modemData struct {
ds []downstreamData
us []upstreamData
}
type downstreamData struct {
DCID int64
Freq float64
Power float64
SNR float64
Modulation int64
Octets int64
Correcteds int64
Uncorrectables int64
}
type upstreamData struct {
UCID int64
Freq float64
Power float64
ChannelType string
SymbolRate int64
Modulation int64
}