-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquanterra.go
79 lines (63 loc) · 1.45 KB
/
quanterra.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
package dmc
import (
"net"
"regexp"
"strings"
"time"
"github.com/ozym/qdp"
)
type Quanterra struct {
}
func (q *Quanterra) Name() string {
return "Quanterra"
}
func (q *Quanterra) MatchString(s string) bool {
return regexp.MustCompile("^Quanterra").MatchString(s)
}
func (q *Quanterra) Groups() []ModelType {
return []ModelType{DataloggerModel}
}
func (q *Quanterra) Group(g ModelType) bool {
switch g {
case DataloggerModel:
return true
default:
return false
}
}
func (q *Quanterra) Identify(orig string, ip net.IP, timeout time.Duration, retries int) (*State, error) {
switch {
case strings.HasSuffix(orig, "+"):
if s, _ := q.discover(ip, "6330", timeout); s != nil {
return s, nil
}
if s, _ := q.discover(ip, "5330", timeout); s != nil {
return s, nil
}
default:
if s, _ := q.discover(ip, "5330", timeout); s != nil {
return s, nil
}
if s, _ := q.discover(ip, "6330", timeout); s != nil {
return s, nil
}
}
return nil, nil
}
func (q *Quanterra) discover(ip net.IP, port string, timeout time.Duration) (*State, error) {
ans, err := qdp.ReadSerial(ip.String(), port, timeout)
if ans == nil || err != nil {
return nil, err
}
s := State{Values: make(map[string]interface{})}
switch port {
case "6330":
s.Values["model"] = "Quanterra Q330+"
default:
s.Values["model"] = "Quanterra Q330"
}
s.Values["version"] = ans.Version
s.Values["serial"] = ans.Serial
s.Values["sysver"] = ans.SysVer
return &s, nil
}