-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrimble.go
More file actions
148 lines (125 loc) · 3.62 KB
/
trimble.go
File metadata and controls
148 lines (125 loc) · 3.62 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
package dmc
import (
"io/ioutil"
"net"
"net/http"
"regexp"
"strconv"
"strings"
"time"
)
type Trimble struct {
Username string
Password string
}
func (t *Trimble) Name() string {
return "Trimble"
}
func (t *Trimble) MatchString(s string) bool {
return regexp.MustCompile("^Trimble").MatchString(s)
}
func (t *Trimble) Groups() []ModelType {
return []ModelType{GNSSModel}
}
func (t *Trimble) Group(g ModelType) bool {
switch g {
case GNSSModel:
return true
default:
return false
}
}
func (t *Trimble) Identify(orig string, ip net.IP, timeout time.Duration, retries int) (*State, error) {
switch {
case strings.Contains(orig, "NetRS"):
if s, err := t.IdentifyNetRS(ip, timeout, retries); s != nil && err == nil {
return s, nil
}
if s, err := t.IdentifyNetR9(ip, timeout, retries); s != nil && err == nil {
return s, nil
}
case strings.Contains(orig, "NetR9"):
if s, err := t.IdentifyNetR9(ip, timeout, retries); s != nil && err == nil {
return s, nil
}
if s, err := t.IdentifyNetRS(ip, timeout, retries); s != nil && err == nil {
return s, nil
}
}
return nil, nil
}
func (t *Trimble) IdentifyNetRS(ip net.IP, timeout time.Duration, retries int) (*State, error) {
username := env(t.Username, "NETRS_USERNAME", "sysadmin")
password := env(t.Password, "NETRS_PASSWORD", "")
return t.identify(username, password, ip, timeout, retries)
}
func (t *Trimble) IdentifyNetR9(ip net.IP, timeout time.Duration, retries int) (*State, error) {
username := env(t.Username, "NETR9_USERNAME", "admin")
password := env(t.Password, "NETR9_PASSWORD", "")
return t.identify(username, password, ip, timeout, retries)
}
func (t *Trimble) identify(username, password string, ip net.IP, timeout time.Duration, retries int) (*State, error) {
cli := &http.Client{}
s := State{Values: make(map[string]interface{})}
for _, n := range []string{"serialNumber", "FirmwareVersion", "RefStation"} {
r, err := t.show(cli, username, password, ip, n)
if err != nil {
return nil, err
}
if f := strings.Fields(r); len(f) > 1 {
switch {
case strings.Contains(r, "SerialNumber"):
s.Values["serial"] = strings.Replace(f[len(f)-1], "sn=", "", -1)
case strings.Contains(r, "FirmwareVersion"):
s.Values["firmware"] = strings.Replace(f[1], "version=", "", -1)
case strings.Contains(r, "RefStation"):
for _, a := range f {
if b := strings.Split(a, "="); len(b) > 1 {
switch b[0] {
case "lat":
if v, err := strconv.ParseFloat(b[1], 64); err == nil {
s.Values["latitude"] = v
}
case "lon":
if v, err := strconv.ParseFloat(b[1], 64); err == nil {
s.Values["longitude"] = v
}
case "height":
if v, err := strconv.ParseFloat(b[1], 64); err == nil {
s.Values["height"] = v
}
case "Code":
s.Values["site"] = strings.Replace(b[1], "'", "", -1)
s.Values["model"] = "Trimble NetR9"
case "CmrStationName":
s.Values["site"] = strings.Replace(b[1], "'", "", -1)
s.Values["model"] = "Trimble NetRS"
}
}
}
}
}
}
// had to have found a model
if _, ok := s.Values["model"]; !ok {
return nil, nil
}
return &s, nil
}
func (t *Trimble) show(cli *http.Client, username, password string, ip net.IP, value string) (string, error) {
request, err := http.NewRequest("GET", "http://"+ip.String()+"/prog/show?"+value, nil)
if err != nil {
return "", err
}
request.SetBasicAuth(username, password)
resp, err := cli.Do(request)
if err != nil {
return "", err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
defer resp.Body.Close()
return (string)(body), nil
}