-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
169 lines (148 loc) · 4.25 KB
/
main.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// This program currently just drops OpenChirp device transducer values
// into Redis.
// The future goal of this program will be to manage all MQTT bridging and magic
// for the OpenChirp core.
// Craig Hesling <craig@hesling.com>
package main
import (
"io/ioutil"
"os"
"os/signal"
"strings"
"time"
"github.com/coreos/go-systemd/daemon"
"github.com/go-redis/redis"
"github.com/openchirp/framework/pubsub"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"github.com/wercker/journalhook"
)
const (
mqttQoS = pubsub.QoSExactlyOnce
)
var (
lastValueExpiration = time.Hour * time.Duration(24*32*4) // roughly 4 months
version = "1.0"
)
func run(ctx *cli.Context) error {
systemdIntegration := ctx.Bool("systemd")
/* Set logging level */
log := logrus.New()
log.SetLevel(logrus.Level(uint32(ctx.Int("log-level"))))
if systemdIntegration {
log.AddHook(&journalhook.JournalHook{})
log.Out = ioutil.Discard
}
/* Argument Receiving */
mqttBroker := ctx.String("mqtt-server")
mqttUser := ctx.String("mqtt-user")
mqttPass := ctx.String("mqtt-pass")
redisAddr := ctx.String("redis-server")
redisPassword := ctx.String("redis-password")
redisDB := ctx.Int("redis-db")
redisClient := redis.NewClient(&redis.Options{
Addr: redisAddr,
Password: redisPassword,
DB: redisDB,
})
defer redisClient.Close()
pong, err := redisClient.Ping().Result()
if err != nil {
log.Fatal("Failed to connect to Redis: ", err)
}
log.Debug("Redis pong: ", pong)
mqttClient, err := pubsub.NewMQTTClient(mqttBroker, mqttUser, mqttPass, mqttQoS, false)
if err != nil {
log.Fatal("Failed to connect to MQTT Broker: ", err)
}
defer mqttClient.Disconnect()
mqttClient.Subscribe("openchirp/device/+/+", func(topic string, payload []byte) {
now := time.Now()
log.Debugf("MQTT Message: %s = %s", topic, string(payload))
// Apply standard OpenChirp topic rules
key := strings.Replace(topic, "/", ":", 3)
key = strings.Replace(key, " ", "_", 3)
key = strings.ToLower(key)
value := string(payload)
p := redisClient.Pipeline()
res, err := p.Set(key, value, lastValueExpiration).Result()
if err != nil {
log.Errorf("Failed to set %s with %s: response=%v | err=%v", key, value, res, err)
}
timestampkey := key + ":time"
res, err = p.Set(timestampkey, now.Format(time.RFC3339Nano), lastValueExpiration).Result()
if err != nil {
log.Errorf("Failed to set %s with %s: response=%v | err=%v", key, value, res, err)
}
if _, err := p.Exec(); err != nil {
log.Errorf("Failed to set value/timestamp for %s with %s/%v: response=%v | err=%v", key, value, now, res, err)
}
})
if systemdIntegration {
daemon.SdNotify(false, daemon.SdNotifyReady)
}
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
<-sig
if systemdIntegration {
daemon.SdNotify(false, daemon.SdNotifyStopping)
}
return nil
}
func main() {
app := cli.NewApp()
app.Name = "openchirp_pubsub"
app.Usage = ""
app.Copyright = "See https://github.com/openchirp/openchirp_pubsub for copyright information"
app.Author = "Craig Hesling"
app.Version = version
app.Action = run
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "mqtt-server",
Usage: "MQTT server's URI (e.g. scheme://host:port where scheme is tcp or tls)",
Value: "tls://localhost:8883",
EnvVar: "MQTT_SERVER",
},
cli.StringFlag{
Name: "mqtt-user",
Usage: "Username to login to the MQTT server with",
EnvVar: "MQTT_USER",
},
cli.StringFlag{
Name: "mqtt-pass",
Usage: "Password to login to the MQTT server with",
EnvVar: "MQTT_PASS",
},
cli.StringFlag{
Name: "redis-server",
Value: "localhost:6379",
Usage: "The URI to the Redis server",
EnvVar: "REDIS_SERVER",
},
cli.StringFlag{
Name: "redis-pass",
Value: "",
Usage: "Password to login to the Redis server with",
EnvVar: "REDIS_PASS",
},
cli.IntFlag{
Name: "redis-db",
Value: 1,
Usage: "Selects which Redis DB to use",
EnvVar: "REDIS_DB",
},
cli.IntFlag{
Name: "log-level",
Value: 4,
Usage: "debug=5, info=4, warning=3, error=2, fatal=1, panic=0",
EnvVar: "LOG_LEVEL",
},
cli.BoolFlag{
Name: "systemd",
Usage: "Indicates that this service can use systemd specific interfaces.",
EnvVar: "SYSTEMD",
},
}
app.Run(os.Args)
}