/
manager.go
150 lines (126 loc) · 3.4 KB
/
manager.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
package telemetry
import (
"fmt"
"gitlab.com/janstun/gear"
"regexp"
"strconv"
"strings"
"time"
)
type Devices map[string]device
type device struct {
DevName string
Protocol string
ProtocolOpt ProtocolOptType
Oids []string
}
type ProtocolOptType struct {
Username, Password, Community, Ip, Version string
}
// Construct a new device :
// devName = Device Name eg. dev1
// protocol = snmp , rtu , ...
// protocolOpt >> snmp = username/password/community/ip:port/version
// protocolOpt >> rtu = ...
// protocolOpt >> ... = ...
func (tm Devices) AddDevice(devName, protocol string, protocolOpt ProtocolOptType) device {
dev := device{}
dev.DevName = devName
dev.Protocol = protocol
dev.ProtocolOpt = protocolOpt
tm[devName] = dev
return dev
}
// Register Which Parameter of the device needs to read/write
func (tm Devices) RegisterDeviceParams(devName string, oids []string) {
for i, a := range tm {
if devName == i {
a.Oids = append(a.Oids, oids...)
tm[i] = a
}
}
}
// Create a Subscription upon on the name of topic : <device name>/<oid>/<optional interval in seconds>
// e.g : dev1/1.2.1.4.5.1.2/100 or testdev/1.2.1.4.2.1
func (tm Devices) Subscribe(topic string) (<-chan gear.Message, error) {
pattern := `^\w+\/[1]\.(\w|\.)+\??\d*$`
validation, _ := regexp.MatchString(pattern, topic)
if validation == false {
err := fmt.Errorf("please check your topic: %s again", topic)
return nil, err
}
topicSplited := strings.Split(topic, "/")
devName := topicSplited[0]
devParams := strings.Split(topicSplited[1],"?")
devOid := devParams[0]
// The interval creation , it is correct but commented because has not been completely coded.
var devInterval int
if len(devParams) > 1 {
devInterval, _ = strconv.Atoi(devParams[1])
if devInterval == 0 {
devInterval = 1
}
} else {
devInterval = 1
}
var result string
var err error
msg := gear.Message{
Topic: topic,
Reply: "",
Data: []byte(result),
}
msgRes := make(chan gear.Message)
for i, a := range tm {
if i == devName {
conn := makeConnection(a)
for _, o := range a.Oids {
fmt.Printf("%s , %s , %d , @ %s\n", devParams , devOid , devInterval , o)
if devOid == o {
fmt.Println("matched")
//tick
fmt.Println(time.Duration(devInterval) * time.Second)
tick := time.NewTicker(time.Duration(devInterval) * time.Second)
tickClose := make(chan struct{})
go func() {
for {
select {
case <-tick.C:
fmt.Println("tick")
result, err = conn.Get(o)
msg.Data = []byte(result)
msgRes <- msg
fmt.Printf(" result: %s\n", result)
case <-tickClose:
tick.Stop()
return
}
}
}()
break
} else {
fmt.Println("not matched!")
return nil , fmt.Errorf("Your desired OID has not been registered: %s" , devOid)
}
}
}
}
if err != nil {
return nil, err
}
return msgRes, err
}
func (tm Devices) Unsubscribe(topic string) error {
return nil
}
// Create a connection to use the desired protocol.
// Now only support snmp protocol
func makeConnection(device device) Connection {
var connection Connection
// Make Connection for SNMP :
if device.Protocol == "snmp" {
version, _ := strconv.Atoi(device.ProtocolOpt.Version)
connection = DialSNMP(device.ProtocolOpt.Username, device.ProtocolOpt.Password, device.ProtocolOpt.Community, device.ProtocolOpt.Ip, version)
}
return connection
}