-
Notifications
You must be signed in to change notification settings - Fork 24
/
mqforward.go
61 lines (50 loc) · 1.14 KB
/
mqforward.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
package main
import (
"fmt"
log "github.com/Sirupsen/logrus"
)
const (
StatusStopped = "stopped"
StatusStarted = "started"
MaxBufferSize = 4 // bufferd size to send influxDB
)
type Forwarder struct {
mqclient *MqttClient
ifclient *InfluxDBClient
mqttChan chan Message
ifChan chan Message
}
func NewForwarder(mqttconf MqttConf, ifconf InfluxDBConf) (*Forwarder, error) {
// channel from MQTT
mqttChan := make(chan Message, MaxBufferSize)
// channel to InfluxDB
ifChan := make(chan Message, MaxBufferSize)
mqclient, err := NewMqttClient(mqttconf, mqttChan)
if err != nil {
return nil, fmt.Errorf("mqtt init err: %s", err)
}
ifclient, err := NewInfluxDBClient(ifconf, ifChan)
if err != nil {
return nil, fmt.Errorf("influxdb init err: %s", err)
}
go ifclient.Start()
return &Forwarder{
mqclient: mqclient,
ifclient: ifclient,
mqttChan: mqttChan,
ifChan: ifChan,
}, nil
}
func (f *Forwarder) Start() error {
for {
select {
case msg, ok := <-f.mqttChan:
if !ok {
return fmt.Errorf("msg pipe closed")
}
log.Debug("msg comes from mqtt")
f.ifChan <- msg
}
}
return fmt.Errorf("quit start loop")
}