-
Notifications
You must be signed in to change notification settings - Fork 3
/
gauge.go
83 lines (78 loc) · 1.88 KB
/
gauge.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
package nzwko
import (
"fmt"
"regexp"
"strconv"
"strings"
"sync"
"github.com/sirupsen/logrus"
"github.com/whitewater-guide/gorge/core"
)
var locRegExp = regexp.MustCompile(`NZTM:\s*(\d+)\s*-\s*(\d+)`)
var spaces = regexp.MustCompile(`\s+`)
func (s *scriptWaikato) parseGaugePage(code string, hasFlow, hasLevel bool) (*core.Gauge, error) {
url := fmt.Sprintf(s.pageURL, code)
doc, err := core.Client.GetAsDoc(url, nil)
if err != nil {
return nil, err
}
defer doc.Close()
name := doc.Find(`td:contains("Site name:")`).Next().Text()
locStr := strings.TrimSpace(doc.Find(`td:contains("Map reference:")`).Next().Text())
var lat, lon float64
if locStr != "" {
match := locRegExp.FindAllStringSubmatch(locStr, -1)
xStr, yStr := match[0][1], match[0][2]
x, err := strconv.ParseFloat(xStr, 64)
if err != nil {
return nil, err
}
y, err := strconv.ParseFloat(yStr, 64)
if err != nil {
return nil, err
}
lon, lat, err = core.ToEPSG4326(x, y, "EPSG:2193")
if err != nil {
return nil, err
}
}
var levelUnit, flowUnit string
if hasLevel {
levelUnit = "m"
}
if hasFlow {
flowUnit = "m3/s"
}
return &core.Gauge{
GaugeID: core.GaugeID{
Script: s.name,
Code: code,
},
Name: strings.TrimSpace(spaces.ReplaceAllString(name, " ")),
URL: url,
LevelUnit: levelUnit,
FlowUnit: flowUnit,
Location: &core.Location{
Latitude: lat,
Longitude: lon,
},
Timezone: "Pacific/Auckland",
}, nil
}
func (s *scriptWaikato) gaugePageWorker(ms <-chan *core.Measurement, results chan<- *core.Gauge, wg *sync.WaitGroup) {
for m := range ms {
code := m.Code
gauge, err := s.parseGaugePage(code, m.Flow.Valid(), m.Level.Valid())
if err != nil {
fmt.Println(err)
s.GetLogger().WithFields(logrus.Fields{
"script": s.name,
"command": "harvest",
"code": code,
}).Error(err)
continue
}
results <- gauge
}
wg.Done()
}